PageRenderTime 72ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/net/dsa/port.c

https://gitlab.com/freesoftware/linux
C | 573 lines | 415 code | 116 blank | 42 comment | 67 complexity | 704bf1e5d6a05f1342a28c396ca74c33 MD5 | raw file
  1. /*
  2. * Handling of a single switch port
  3. *
  4. * Copyright (c) 2017 Savoir-faire Linux Inc.
  5. * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/if_bridge.h>
  13. #include <linux/notifier.h>
  14. #include <linux/of_mdio.h>
  15. #include <linux/of_net.h>
  16. #include "dsa_priv.h"
  17. static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
  18. {
  19. struct raw_notifier_head *nh = &dp->ds->dst->nh;
  20. int err;
  21. err = raw_notifier_call_chain(nh, e, v);
  22. return notifier_to_errno(err);
  23. }
  24. int dsa_port_set_state(struct dsa_port *dp, u8 state,
  25. struct switchdev_trans *trans)
  26. {
  27. struct dsa_switch *ds = dp->ds;
  28. int port = dp->index;
  29. if (switchdev_trans_ph_prepare(trans))
  30. return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
  31. if (ds->ops->port_stp_state_set)
  32. ds->ops->port_stp_state_set(ds, port, state);
  33. if (ds->ops->port_fast_age) {
  34. /* Fast age FDB entries or flush appropriate forwarding database
  35. * for the given port, if we are moving it from Learning or
  36. * Forwarding state, to Disabled or Blocking or Listening state.
  37. */
  38. if ((dp->stp_state == BR_STATE_LEARNING ||
  39. dp->stp_state == BR_STATE_FORWARDING) &&
  40. (state == BR_STATE_DISABLED ||
  41. state == BR_STATE_BLOCKING ||
  42. state == BR_STATE_LISTENING))
  43. ds->ops->port_fast_age(ds, port);
  44. }
  45. dp->stp_state = state;
  46. return 0;
  47. }
  48. static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
  49. {
  50. int err;
  51. err = dsa_port_set_state(dp, state, NULL);
  52. if (err)
  53. pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
  54. }
  55. int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
  56. {
  57. struct dsa_switch *ds = dp->ds;
  58. int port = dp->index;
  59. int err;
  60. if (ds->ops->port_enable) {
  61. err = ds->ops->port_enable(ds, port, phy);
  62. if (err)
  63. return err;
  64. }
  65. if (!dp->bridge_dev)
  66. dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
  67. return 0;
  68. }
  69. void dsa_port_disable(struct dsa_port *dp)
  70. {
  71. struct dsa_switch *ds = dp->ds;
  72. int port = dp->index;
  73. if (!dp->bridge_dev)
  74. dsa_port_set_state_now(dp, BR_STATE_DISABLED);
  75. if (ds->ops->port_disable)
  76. ds->ops->port_disable(ds, port);
  77. }
  78. int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
  79. {
  80. struct dsa_notifier_bridge_info info = {
  81. .sw_index = dp->ds->index,
  82. .port = dp->index,
  83. .br = br,
  84. };
  85. int err;
  86. /* Set the flooding mode before joining the port in the switch */
  87. err = dsa_port_bridge_flags(dp, BR_FLOOD | BR_MCAST_FLOOD, NULL);
  88. if (err)
  89. return err;
  90. /* Here the interface is already bridged. Reflect the current
  91. * configuration so that drivers can program their chips accordingly.
  92. */
  93. dp->bridge_dev = br;
  94. err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
  95. /* The bridging is rolled back on error */
  96. if (err) {
  97. dsa_port_bridge_flags(dp, 0, NULL);
  98. dp->bridge_dev = NULL;
  99. }
  100. return err;
  101. }
  102. void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
  103. {
  104. struct dsa_notifier_bridge_info info = {
  105. .sw_index = dp->ds->index,
  106. .port = dp->index,
  107. .br = br,
  108. };
  109. int err;
  110. /* Here the port is already unbridged. Reflect the current configuration
  111. * so that drivers can program their chips accordingly.
  112. */
  113. dp->bridge_dev = NULL;
  114. err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
  115. if (err)
  116. pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
  117. /* Port is leaving the bridge, disable flooding */
  118. dsa_port_bridge_flags(dp, 0, NULL);
  119. /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
  120. * so allow it to be in BR_STATE_FORWARDING to be kept functional
  121. */
  122. dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
  123. }
  124. static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
  125. bool vlan_filtering)
  126. {
  127. struct dsa_switch *ds = dp->ds;
  128. int i;
  129. if (!ds->vlan_filtering_is_global)
  130. return true;
  131. /* For cases where enabling/disabling VLAN awareness is global to the
  132. * switch, we need to handle the case where multiple bridges span
  133. * different ports of the same switch device and one of them has a
  134. * different setting than what is being requested.
  135. */
  136. for (i = 0; i < ds->num_ports; i++) {
  137. struct net_device *other_bridge;
  138. other_bridge = dsa_to_port(ds, i)->bridge_dev;
  139. if (!other_bridge)
  140. continue;
  141. /* If it's the same bridge, it also has same
  142. * vlan_filtering setting => no need to check
  143. */
  144. if (other_bridge == dp->bridge_dev)
  145. continue;
  146. if (br_vlan_enabled(other_bridge) != vlan_filtering) {
  147. dev_err(ds->dev, "VLAN filtering is a global setting\n");
  148. return false;
  149. }
  150. }
  151. return true;
  152. }
  153. int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
  154. struct switchdev_trans *trans)
  155. {
  156. struct dsa_switch *ds = dp->ds;
  157. int err;
  158. /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
  159. if (switchdev_trans_ph_prepare(trans))
  160. return 0;
  161. if (!ds->ops->port_vlan_filtering)
  162. return 0;
  163. if (!dsa_port_can_apply_vlan_filtering(dp, vlan_filtering))
  164. return -EINVAL;
  165. if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
  166. return 0;
  167. err = ds->ops->port_vlan_filtering(ds, dp->index,
  168. vlan_filtering);
  169. if (err)
  170. return err;
  171. if (ds->vlan_filtering_is_global)
  172. ds->vlan_filtering = vlan_filtering;
  173. else
  174. dp->vlan_filtering = vlan_filtering;
  175. return 0;
  176. }
  177. int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock,
  178. struct switchdev_trans *trans)
  179. {
  180. unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
  181. unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
  182. struct dsa_notifier_ageing_time_info info = {
  183. .ageing_time = ageing_time,
  184. .trans = trans,
  185. };
  186. if (switchdev_trans_ph_prepare(trans))
  187. return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
  188. dp->ageing_time = ageing_time;
  189. return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
  190. }
  191. int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags,
  192. struct switchdev_trans *trans)
  193. {
  194. struct dsa_switch *ds = dp->ds;
  195. if (!ds->ops->port_egress_floods ||
  196. (flags & ~(BR_FLOOD | BR_MCAST_FLOOD)))
  197. return -EINVAL;
  198. return 0;
  199. }
  200. int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags,
  201. struct switchdev_trans *trans)
  202. {
  203. struct dsa_switch *ds = dp->ds;
  204. int port = dp->index;
  205. int err = 0;
  206. if (switchdev_trans_ph_prepare(trans))
  207. return 0;
  208. if (ds->ops->port_egress_floods)
  209. err = ds->ops->port_egress_floods(ds, port, flags & BR_FLOOD,
  210. flags & BR_MCAST_FLOOD);
  211. return err;
  212. }
  213. int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
  214. u16 vid)
  215. {
  216. struct dsa_notifier_fdb_info info = {
  217. .sw_index = dp->ds->index,
  218. .port = dp->index,
  219. .addr = addr,
  220. .vid = vid,
  221. };
  222. return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
  223. }
  224. int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
  225. u16 vid)
  226. {
  227. struct dsa_notifier_fdb_info info = {
  228. .sw_index = dp->ds->index,
  229. .port = dp->index,
  230. .addr = addr,
  231. .vid = vid,
  232. };
  233. return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
  234. }
  235. int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
  236. {
  237. struct dsa_switch *ds = dp->ds;
  238. int port = dp->index;
  239. if (!ds->ops->port_fdb_dump)
  240. return -EOPNOTSUPP;
  241. return ds->ops->port_fdb_dump(ds, port, cb, data);
  242. }
  243. int dsa_port_mdb_add(const struct dsa_port *dp,
  244. const struct switchdev_obj_port_mdb *mdb,
  245. struct switchdev_trans *trans)
  246. {
  247. struct dsa_notifier_mdb_info info = {
  248. .sw_index = dp->ds->index,
  249. .port = dp->index,
  250. .trans = trans,
  251. .mdb = mdb,
  252. };
  253. return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
  254. }
  255. int dsa_port_mdb_del(const struct dsa_port *dp,
  256. const struct switchdev_obj_port_mdb *mdb)
  257. {
  258. struct dsa_notifier_mdb_info info = {
  259. .sw_index = dp->ds->index,
  260. .port = dp->index,
  261. .mdb = mdb,
  262. };
  263. return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
  264. }
  265. int dsa_port_vlan_add(struct dsa_port *dp,
  266. const struct switchdev_obj_port_vlan *vlan,
  267. struct switchdev_trans *trans)
  268. {
  269. struct dsa_notifier_vlan_info info = {
  270. .sw_index = dp->ds->index,
  271. .port = dp->index,
  272. .trans = trans,
  273. .vlan = vlan,
  274. };
  275. /* Can be called from dsa_slave_port_obj_add() or
  276. * dsa_slave_vlan_rx_add_vid()
  277. */
  278. if (!dp->bridge_dev || br_vlan_enabled(dp->bridge_dev))
  279. return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
  280. return 0;
  281. }
  282. int dsa_port_vlan_del(struct dsa_port *dp,
  283. const struct switchdev_obj_port_vlan *vlan)
  284. {
  285. struct dsa_notifier_vlan_info info = {
  286. .sw_index = dp->ds->index,
  287. .port = dp->index,
  288. .vlan = vlan,
  289. };
  290. if (vlan->obj.orig_dev && netif_is_bridge_master(vlan->obj.orig_dev))
  291. return -EOPNOTSUPP;
  292. /* Can be called from dsa_slave_port_obj_del() or
  293. * dsa_slave_vlan_rx_kill_vid()
  294. */
  295. if (!dp->bridge_dev || br_vlan_enabled(dp->bridge_dev))
  296. return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
  297. return 0;
  298. }
  299. int dsa_port_vid_add(struct dsa_port *dp, u16 vid, u16 flags)
  300. {
  301. struct switchdev_obj_port_vlan vlan = {
  302. .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
  303. .flags = flags,
  304. .vid_begin = vid,
  305. .vid_end = vid,
  306. };
  307. struct switchdev_trans trans;
  308. int err;
  309. trans.ph_prepare = true;
  310. err = dsa_port_vlan_add(dp, &vlan, &trans);
  311. if (err == -EOPNOTSUPP)
  312. return 0;
  313. trans.ph_prepare = false;
  314. return dsa_port_vlan_add(dp, &vlan, &trans);
  315. }
  316. EXPORT_SYMBOL(dsa_port_vid_add);
  317. int dsa_port_vid_del(struct dsa_port *dp, u16 vid)
  318. {
  319. struct switchdev_obj_port_vlan vlan = {
  320. .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
  321. .vid_begin = vid,
  322. .vid_end = vid,
  323. };
  324. return dsa_port_vlan_del(dp, &vlan);
  325. }
  326. EXPORT_SYMBOL(dsa_port_vid_del);
  327. static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
  328. {
  329. struct device_node *phy_dn;
  330. struct phy_device *phydev;
  331. phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
  332. if (!phy_dn)
  333. return NULL;
  334. phydev = of_phy_find_device(phy_dn);
  335. if (!phydev) {
  336. of_node_put(phy_dn);
  337. return ERR_PTR(-EPROBE_DEFER);
  338. }
  339. of_node_put(phy_dn);
  340. return phydev;
  341. }
  342. static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
  343. {
  344. struct dsa_switch *ds = dp->ds;
  345. struct phy_device *phydev;
  346. int port = dp->index;
  347. int err = 0;
  348. phydev = dsa_port_get_phy_device(dp);
  349. if (!phydev)
  350. return 0;
  351. if (IS_ERR(phydev))
  352. return PTR_ERR(phydev);
  353. if (enable) {
  354. err = genphy_config_init(phydev);
  355. if (err < 0)
  356. goto err_put_dev;
  357. err = genphy_resume(phydev);
  358. if (err < 0)
  359. goto err_put_dev;
  360. err = genphy_read_status(phydev);
  361. if (err < 0)
  362. goto err_put_dev;
  363. } else {
  364. err = genphy_suspend(phydev);
  365. if (err < 0)
  366. goto err_put_dev;
  367. }
  368. if (ds->ops->adjust_link)
  369. ds->ops->adjust_link(ds, port, phydev);
  370. dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
  371. err_put_dev:
  372. put_device(&phydev->mdio.dev);
  373. return err;
  374. }
  375. static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
  376. {
  377. struct device_node *dn = dp->dn;
  378. struct dsa_switch *ds = dp->ds;
  379. struct phy_device *phydev;
  380. int port = dp->index;
  381. int mode;
  382. int err;
  383. err = of_phy_register_fixed_link(dn);
  384. if (err) {
  385. dev_err(ds->dev,
  386. "failed to register the fixed PHY of port %d\n",
  387. port);
  388. return err;
  389. }
  390. phydev = of_phy_find_device(dn);
  391. mode = of_get_phy_mode(dn);
  392. if (mode < 0)
  393. mode = PHY_INTERFACE_MODE_NA;
  394. phydev->interface = mode;
  395. genphy_config_init(phydev);
  396. genphy_read_status(phydev);
  397. if (ds->ops->adjust_link)
  398. ds->ops->adjust_link(ds, port, phydev);
  399. put_device(&phydev->mdio.dev);
  400. return 0;
  401. }
  402. int dsa_port_link_register_of(struct dsa_port *dp)
  403. {
  404. if (of_phy_is_fixed_link(dp->dn))
  405. return dsa_port_fixed_link_register_of(dp);
  406. else
  407. return dsa_port_setup_phy_of(dp, true);
  408. }
  409. void dsa_port_link_unregister_of(struct dsa_port *dp)
  410. {
  411. if (of_phy_is_fixed_link(dp->dn))
  412. of_phy_deregister_fixed_link(dp->dn);
  413. else
  414. dsa_port_setup_phy_of(dp, false);
  415. }
  416. int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
  417. {
  418. struct phy_device *phydev;
  419. int ret = -EOPNOTSUPP;
  420. if (of_phy_is_fixed_link(dp->dn))
  421. return ret;
  422. phydev = dsa_port_get_phy_device(dp);
  423. if (IS_ERR_OR_NULL(phydev))
  424. return ret;
  425. ret = phy_ethtool_get_strings(phydev, data);
  426. put_device(&phydev->mdio.dev);
  427. return ret;
  428. }
  429. EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
  430. int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
  431. {
  432. struct phy_device *phydev;
  433. int ret = -EOPNOTSUPP;
  434. if (of_phy_is_fixed_link(dp->dn))
  435. return ret;
  436. phydev = dsa_port_get_phy_device(dp);
  437. if (IS_ERR_OR_NULL(phydev))
  438. return ret;
  439. ret = phy_ethtool_get_stats(phydev, NULL, data);
  440. put_device(&phydev->mdio.dev);
  441. return ret;
  442. }
  443. EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
  444. int dsa_port_get_phy_sset_count(struct dsa_port *dp)
  445. {
  446. struct phy_device *phydev;
  447. int ret = -EOPNOTSUPP;
  448. if (of_phy_is_fixed_link(dp->dn))
  449. return ret;
  450. phydev = dsa_port_get_phy_device(dp);
  451. if (IS_ERR_OR_NULL(phydev))
  452. return ret;
  453. ret = phy_ethtool_get_sset_count(phydev);
  454. put_device(&phydev->mdio.dev);
  455. return ret;
  456. }
  457. EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);