port.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Handling of a single switch port
  4. *
  5. * Copyright (c) 2017 Savoir-faire Linux Inc.
  6. * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  7. */
  8. #include <linux/if_bridge.h>
  9. #include <linux/notifier.h>
  10. #include <linux/of_mdio.h>
  11. #include <linux/of_net.h>
  12. #include "dsa_priv.h"
  13. static int dsa_broadcast(unsigned long e, void *v)
  14. {
  15. struct dsa_switch_tree *dst;
  16. int err = 0;
  17. list_for_each_entry(dst, &dsa_tree_list, list) {
  18. struct raw_notifier_head *nh = &dst->nh;
  19. err = raw_notifier_call_chain(nh, e, v);
  20. err = notifier_to_errno(err);
  21. if (err)
  22. break;
  23. }
  24. return err;
  25. }
  26. static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
  27. {
  28. struct raw_notifier_head *nh = &dp->ds->dst->nh;
  29. int err;
  30. err = raw_notifier_call_chain(nh, e, v);
  31. return notifier_to_errno(err);
  32. }
  33. int dsa_port_set_state(struct dsa_port *dp, u8 state,
  34. struct switchdev_trans *trans)
  35. {
  36. struct dsa_switch *ds = dp->ds;
  37. int port = dp->index;
  38. if (switchdev_trans_ph_prepare(trans))
  39. return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
  40. if (ds->ops->port_stp_state_set)
  41. ds->ops->port_stp_state_set(ds, port, state);
  42. if (ds->ops->port_fast_age) {
  43. /* Fast age FDB entries or flush appropriate forwarding database
  44. * for the given port, if we are moving it from Learning or
  45. * Forwarding state, to Disabled or Blocking or Listening state.
  46. */
  47. if ((dp->stp_state == BR_STATE_LEARNING ||
  48. dp->stp_state == BR_STATE_FORWARDING) &&
  49. (state == BR_STATE_DISABLED ||
  50. state == BR_STATE_BLOCKING ||
  51. state == BR_STATE_LISTENING))
  52. ds->ops->port_fast_age(ds, port);
  53. }
  54. dp->stp_state = state;
  55. return 0;
  56. }
  57. static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
  58. {
  59. int err;
  60. err = dsa_port_set_state(dp, state, NULL);
  61. if (err)
  62. pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
  63. }
  64. int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
  65. {
  66. struct dsa_switch *ds = dp->ds;
  67. int port = dp->index;
  68. int err;
  69. if (ds->ops->port_enable) {
  70. err = ds->ops->port_enable(ds, port, phy);
  71. if (err)
  72. return err;
  73. }
  74. if (!dp->bridge_dev)
  75. dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
  76. if (dp->pl)
  77. phylink_start(dp->pl);
  78. return 0;
  79. }
  80. int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
  81. {
  82. int err;
  83. rtnl_lock();
  84. err = dsa_port_enable_rt(dp, phy);
  85. rtnl_unlock();
  86. return err;
  87. }
  88. void dsa_port_disable_rt(struct dsa_port *dp)
  89. {
  90. struct dsa_switch *ds = dp->ds;
  91. int port = dp->index;
  92. if (dp->pl)
  93. phylink_stop(dp->pl);
  94. if (!dp->bridge_dev)
  95. dsa_port_set_state_now(dp, BR_STATE_DISABLED);
  96. if (ds->ops->port_disable)
  97. ds->ops->port_disable(ds, port);
  98. }
  99. void dsa_port_disable(struct dsa_port *dp)
  100. {
  101. rtnl_lock();
  102. dsa_port_disable_rt(dp);
  103. rtnl_unlock();
  104. }
  105. int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
  106. {
  107. struct dsa_notifier_bridge_info info = {
  108. .tree_index = dp->ds->dst->index,
  109. .sw_index = dp->ds->index,
  110. .port = dp->index,
  111. .br = br,
  112. };
  113. int err;
  114. /* Set the flooding mode before joining the port in the switch */
  115. err = dsa_port_bridge_flags(dp, BR_FLOOD | BR_MCAST_FLOOD, NULL);
  116. if (err)
  117. return err;
  118. /* Here the interface is already bridged. Reflect the current
  119. * configuration so that drivers can program their chips accordingly.
  120. */
  121. dp->bridge_dev = br;
  122. err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
  123. /* The bridging is rolled back on error */
  124. if (err) {
  125. dsa_port_bridge_flags(dp, 0, NULL);
  126. dp->bridge_dev = NULL;
  127. }
  128. return err;
  129. }
  130. void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
  131. {
  132. struct dsa_notifier_bridge_info info = {
  133. .tree_index = dp->ds->dst->index,
  134. .sw_index = dp->ds->index,
  135. .port = dp->index,
  136. .br = br,
  137. };
  138. int err;
  139. /* Here the port is already unbridged. Reflect the current configuration
  140. * so that drivers can program their chips accordingly.
  141. */
  142. dp->bridge_dev = NULL;
  143. err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
  144. if (err)
  145. pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
  146. /* Port is leaving the bridge, disable flooding */
  147. dsa_port_bridge_flags(dp, 0, NULL);
  148. /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
  149. * so allow it to be in BR_STATE_FORWARDING to be kept functional
  150. */
  151. dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
  152. }
  153. /* Must be called under rcu_read_lock() */
  154. static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
  155. bool vlan_filtering)
  156. {
  157. struct dsa_switch *ds = dp->ds;
  158. int err, i;
  159. /* VLAN awareness was off, so the question is "can we turn it on".
  160. * We may have had 8021q uppers, those need to go. Make sure we don't
  161. * enter an inconsistent state: deny changing the VLAN awareness state
  162. * as long as we have 8021q uppers.
  163. */
  164. if (vlan_filtering && dsa_is_user_port(ds, dp->index)) {
  165. struct net_device *upper_dev, *slave = dp->slave;
  166. struct net_device *br = dp->bridge_dev;
  167. struct list_head *iter;
  168. netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
  169. struct bridge_vlan_info br_info;
  170. u16 vid;
  171. if (!is_vlan_dev(upper_dev))
  172. continue;
  173. vid = vlan_dev_vlan_id(upper_dev);
  174. /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
  175. * device, respectively the VID is not found, returning
  176. * 0 means success, which is a failure for us here.
  177. */
  178. err = br_vlan_get_info(br, vid, &br_info);
  179. if (err == 0) {
  180. dev_err(ds->dev, "Must remove upper %s first\n",
  181. upper_dev->name);
  182. return false;
  183. }
  184. }
  185. }
  186. if (!ds->vlan_filtering_is_global)
  187. return true;
  188. /* For cases where enabling/disabling VLAN awareness is global to the
  189. * switch, we need to handle the case where multiple bridges span
  190. * different ports of the same switch device and one of them has a
  191. * different setting than what is being requested.
  192. */
  193. for (i = 0; i < ds->num_ports; i++) {
  194. struct net_device *other_bridge;
  195. other_bridge = dsa_to_port(ds, i)->bridge_dev;
  196. if (!other_bridge)
  197. continue;
  198. /* If it's the same bridge, it also has same
  199. * vlan_filtering setting => no need to check
  200. */
  201. if (other_bridge == dp->bridge_dev)
  202. continue;
  203. if (br_vlan_enabled(other_bridge) != vlan_filtering) {
  204. dev_err(ds->dev, "VLAN filtering is a global setting\n");
  205. return false;
  206. }
  207. }
  208. return true;
  209. }
  210. int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
  211. struct switchdev_trans *trans)
  212. {
  213. struct dsa_switch *ds = dp->ds;
  214. int err;
  215. if (switchdev_trans_ph_prepare(trans)) {
  216. bool apply;
  217. if (!ds->ops->port_vlan_filtering)
  218. return -EOPNOTSUPP;
  219. /* We are called from dsa_slave_switchdev_blocking_event(),
  220. * which is not under rcu_read_lock(), unlike
  221. * dsa_slave_switchdev_event().
  222. */
  223. rcu_read_lock();
  224. apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering);
  225. rcu_read_unlock();
  226. if (!apply)
  227. return -EINVAL;
  228. }
  229. if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
  230. return 0;
  231. err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
  232. trans);
  233. if (err)
  234. return err;
  235. if (switchdev_trans_ph_commit(trans)) {
  236. if (ds->vlan_filtering_is_global)
  237. ds->vlan_filtering = vlan_filtering;
  238. else
  239. dp->vlan_filtering = vlan_filtering;
  240. }
  241. return 0;
  242. }
  243. /* This enforces legacy behavior for switch drivers which assume they can't
  244. * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
  245. */
  246. bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
  247. {
  248. struct dsa_switch *ds = dp->ds;
  249. if (!dp->bridge_dev)
  250. return false;
  251. return (!ds->configure_vlan_while_not_filtering &&
  252. !br_vlan_enabled(dp->bridge_dev));
  253. }
  254. int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock,
  255. struct switchdev_trans *trans)
  256. {
  257. unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
  258. unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
  259. struct dsa_notifier_ageing_time_info info = {
  260. .ageing_time = ageing_time,
  261. .trans = trans,
  262. };
  263. if (switchdev_trans_ph_prepare(trans))
  264. return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
  265. dp->ageing_time = ageing_time;
  266. return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
  267. }
  268. int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags,
  269. struct switchdev_trans *trans)
  270. {
  271. struct dsa_switch *ds = dp->ds;
  272. if (!ds->ops->port_egress_floods ||
  273. (flags & ~(BR_FLOOD | BR_MCAST_FLOOD)))
  274. return -EINVAL;
  275. return 0;
  276. }
  277. int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags,
  278. struct switchdev_trans *trans)
  279. {
  280. struct dsa_switch *ds = dp->ds;
  281. int port = dp->index;
  282. int err = 0;
  283. if (switchdev_trans_ph_prepare(trans))
  284. return 0;
  285. if (ds->ops->port_egress_floods)
  286. err = ds->ops->port_egress_floods(ds, port, flags & BR_FLOOD,
  287. flags & BR_MCAST_FLOOD);
  288. return err;
  289. }
  290. int dsa_port_mrouter(struct dsa_port *dp, bool mrouter,
  291. struct switchdev_trans *trans)
  292. {
  293. struct dsa_switch *ds = dp->ds;
  294. int port = dp->index;
  295. if (switchdev_trans_ph_prepare(trans))
  296. return ds->ops->port_egress_floods ? 0 : -EOPNOTSUPP;
  297. return ds->ops->port_egress_floods(ds, port, true, mrouter);
  298. }
  299. int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
  300. bool propagate_upstream)
  301. {
  302. struct dsa_notifier_mtu_info info = {
  303. .sw_index = dp->ds->index,
  304. .propagate_upstream = propagate_upstream,
  305. .port = dp->index,
  306. .mtu = new_mtu,
  307. };
  308. return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
  309. }
  310. int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
  311. u16 vid)
  312. {
  313. struct dsa_notifier_fdb_info info = {
  314. .sw_index = dp->ds->index,
  315. .port = dp->index,
  316. .addr = addr,
  317. .vid = vid,
  318. };
  319. return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
  320. }
  321. int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
  322. u16 vid)
  323. {
  324. struct dsa_notifier_fdb_info info = {
  325. .sw_index = dp->ds->index,
  326. .port = dp->index,
  327. .addr = addr,
  328. .vid = vid,
  329. };
  330. return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
  331. }
  332. int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
  333. {
  334. struct dsa_switch *ds = dp->ds;
  335. int port = dp->index;
  336. if (!ds->ops->port_fdb_dump)
  337. return -EOPNOTSUPP;
  338. return ds->ops->port_fdb_dump(ds, port, cb, data);
  339. }
  340. int dsa_port_mdb_add(const struct dsa_port *dp,
  341. const struct switchdev_obj_port_mdb *mdb,
  342. struct switchdev_trans *trans)
  343. {
  344. struct dsa_notifier_mdb_info info = {
  345. .sw_index = dp->ds->index,
  346. .port = dp->index,
  347. .trans = trans,
  348. .mdb = mdb,
  349. };
  350. return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
  351. }
  352. int dsa_port_mdb_del(const struct dsa_port *dp,
  353. const struct switchdev_obj_port_mdb *mdb)
  354. {
  355. struct dsa_notifier_mdb_info info = {
  356. .sw_index = dp->ds->index,
  357. .port = dp->index,
  358. .mdb = mdb,
  359. };
  360. return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
  361. }
  362. int dsa_port_vlan_add(struct dsa_port *dp,
  363. const struct switchdev_obj_port_vlan *vlan,
  364. struct switchdev_trans *trans)
  365. {
  366. struct dsa_notifier_vlan_info info = {
  367. .sw_index = dp->ds->index,
  368. .port = dp->index,
  369. .trans = trans,
  370. .vlan = vlan,
  371. };
  372. return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
  373. }
  374. int dsa_port_vlan_del(struct dsa_port *dp,
  375. const struct switchdev_obj_port_vlan *vlan)
  376. {
  377. struct dsa_notifier_vlan_info info = {
  378. .sw_index = dp->ds->index,
  379. .port = dp->index,
  380. .vlan = vlan,
  381. };
  382. return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
  383. }
  384. static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
  385. {
  386. struct device_node *phy_dn;
  387. struct phy_device *phydev;
  388. phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
  389. if (!phy_dn)
  390. return NULL;
  391. phydev = of_phy_find_device(phy_dn);
  392. if (!phydev) {
  393. of_node_put(phy_dn);
  394. return ERR_PTR(-EPROBE_DEFER);
  395. }
  396. of_node_put(phy_dn);
  397. return phydev;
  398. }
  399. static void dsa_port_phylink_validate(struct phylink_config *config,
  400. unsigned long *supported,
  401. struct phylink_link_state *state)
  402. {
  403. struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
  404. struct dsa_switch *ds = dp->ds;
  405. if (!ds->ops->phylink_validate)
  406. return;
  407. ds->ops->phylink_validate(ds, dp->index, supported, state);
  408. }
  409. static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
  410. struct phylink_link_state *state)
  411. {
  412. struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
  413. struct dsa_switch *ds = dp->ds;
  414. int err;
  415. /* Only called for inband modes */
  416. if (!ds->ops->phylink_mac_link_state) {
  417. state->link = 0;
  418. return;
  419. }
  420. err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
  421. if (err < 0) {
  422. dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
  423. dp->index, err);
  424. state->link = 0;
  425. }
  426. }
  427. static void dsa_port_phylink_mac_config(struct phylink_config *config,
  428. unsigned int mode,
  429. const struct phylink_link_state *state)
  430. {
  431. struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
  432. struct dsa_switch *ds = dp->ds;
  433. if (!ds->ops->phylink_mac_config)
  434. return;
  435. ds->ops->phylink_mac_config(ds, dp->index, mode, state);
  436. }
  437. static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
  438. {
  439. struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
  440. struct dsa_switch *ds = dp->ds;
  441. if (!ds->ops->phylink_mac_an_restart)
  442. return;
  443. ds->ops->phylink_mac_an_restart(ds, dp->index);
  444. }
  445. static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
  446. unsigned int mode,
  447. phy_interface_t interface)
  448. {
  449. struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
  450. struct phy_device *phydev = NULL;
  451. struct dsa_switch *ds = dp->ds;
  452. if (dsa_is_user_port(ds, dp->index))
  453. phydev = dp->slave->phydev;
  454. if (!ds->ops->phylink_mac_link_down) {
  455. if (ds->ops->adjust_link && phydev)
  456. ds->ops->adjust_link(ds, dp->index, phydev);
  457. return;
  458. }
  459. ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
  460. }
  461. static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
  462. struct phy_device *phydev,
  463. unsigned int mode,
  464. phy_interface_t interface,
  465. int speed, int duplex,
  466. bool tx_pause, bool rx_pause)
  467. {
  468. struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
  469. struct dsa_switch *ds = dp->ds;
  470. if (!ds->ops->phylink_mac_link_up) {
  471. if (ds->ops->adjust_link && phydev)
  472. ds->ops->adjust_link(ds, dp->index, phydev);
  473. return;
  474. }
  475. ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
  476. speed, duplex, tx_pause, rx_pause);
  477. }
  478. const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
  479. .validate = dsa_port_phylink_validate,
  480. .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
  481. .mac_config = dsa_port_phylink_mac_config,
  482. .mac_an_restart = dsa_port_phylink_mac_an_restart,
  483. .mac_link_down = dsa_port_phylink_mac_link_down,
  484. .mac_link_up = dsa_port_phylink_mac_link_up,
  485. };
  486. static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
  487. {
  488. struct dsa_switch *ds = dp->ds;
  489. struct phy_device *phydev;
  490. int port = dp->index;
  491. int err = 0;
  492. phydev = dsa_port_get_phy_device(dp);
  493. if (!phydev)
  494. return 0;
  495. if (IS_ERR(phydev))
  496. return PTR_ERR(phydev);
  497. if (enable) {
  498. err = genphy_resume(phydev);
  499. if (err < 0)
  500. goto err_put_dev;
  501. err = genphy_read_status(phydev);
  502. if (err < 0)
  503. goto err_put_dev;
  504. } else {
  505. err = genphy_suspend(phydev);
  506. if (err < 0)
  507. goto err_put_dev;
  508. }
  509. if (ds->ops->adjust_link)
  510. ds->ops->adjust_link(ds, port, phydev);
  511. dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
  512. err_put_dev:
  513. put_device(&phydev->mdio.dev);
  514. return err;
  515. }
  516. static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
  517. {
  518. struct device_node *dn = dp->dn;
  519. struct dsa_switch *ds = dp->ds;
  520. struct phy_device *phydev;
  521. int port = dp->index;
  522. phy_interface_t mode;
  523. int err;
  524. err = of_phy_register_fixed_link(dn);
  525. if (err) {
  526. dev_err(ds->dev,
  527. "failed to register the fixed PHY of port %d\n",
  528. port);
  529. return err;
  530. }
  531. phydev = of_phy_find_device(dn);
  532. err = of_get_phy_mode(dn, &mode);
  533. if (err)
  534. mode = PHY_INTERFACE_MODE_NA;
  535. phydev->interface = mode;
  536. genphy_read_status(phydev);
  537. if (ds->ops->adjust_link)
  538. ds->ops->adjust_link(ds, port, phydev);
  539. put_device(&phydev->mdio.dev);
  540. return 0;
  541. }
  542. static int dsa_port_phylink_register(struct dsa_port *dp)
  543. {
  544. struct dsa_switch *ds = dp->ds;
  545. struct device_node *port_dn = dp->dn;
  546. phy_interface_t mode;
  547. int err;
  548. err = of_get_phy_mode(port_dn, &mode);
  549. if (err)
  550. mode = PHY_INTERFACE_MODE_NA;
  551. dp->pl_config.dev = ds->dev;
  552. dp->pl_config.type = PHYLINK_DEV;
  553. dp->pl_config.pcs_poll = ds->pcs_poll;
  554. dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
  555. mode, &dsa_port_phylink_mac_ops);
  556. if (IS_ERR(dp->pl)) {
  557. pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
  558. return PTR_ERR(dp->pl);
  559. }
  560. err = phylink_of_phy_connect(dp->pl, port_dn, 0);
  561. if (err && err != -ENODEV) {
  562. pr_err("could not attach to PHY: %d\n", err);
  563. goto err_phy_connect;
  564. }
  565. return 0;
  566. err_phy_connect:
  567. phylink_destroy(dp->pl);
  568. return err;
  569. }
  570. int dsa_port_link_register_of(struct dsa_port *dp)
  571. {
  572. struct dsa_switch *ds = dp->ds;
  573. struct device_node *phy_np;
  574. int port = dp->index;
  575. if (!ds->ops->adjust_link) {
  576. phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
  577. if (of_phy_is_fixed_link(dp->dn) || phy_np) {
  578. if (ds->ops->phylink_mac_link_down)
  579. ds->ops->phylink_mac_link_down(ds, port,
  580. MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
  581. return dsa_port_phylink_register(dp);
  582. }
  583. return 0;
  584. }
  585. dev_warn(ds->dev,
  586. "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
  587. if (of_phy_is_fixed_link(dp->dn))
  588. return dsa_port_fixed_link_register_of(dp);
  589. else
  590. return dsa_port_setup_phy_of(dp, true);
  591. }
  592. void dsa_port_link_unregister_of(struct dsa_port *dp)
  593. {
  594. struct dsa_switch *ds = dp->ds;
  595. if (!ds->ops->adjust_link && dp->pl) {
  596. rtnl_lock();
  597. phylink_disconnect_phy(dp->pl);
  598. rtnl_unlock();
  599. phylink_destroy(dp->pl);
  600. dp->pl = NULL;
  601. return;
  602. }
  603. if (of_phy_is_fixed_link(dp->dn))
  604. of_phy_deregister_fixed_link(dp->dn);
  605. else
  606. dsa_port_setup_phy_of(dp, false);
  607. }
  608. int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
  609. {
  610. struct phy_device *phydev;
  611. int ret = -EOPNOTSUPP;
  612. if (of_phy_is_fixed_link(dp->dn))
  613. return ret;
  614. phydev = dsa_port_get_phy_device(dp);
  615. if (IS_ERR_OR_NULL(phydev))
  616. return ret;
  617. ret = phy_ethtool_get_strings(phydev, data);
  618. put_device(&phydev->mdio.dev);
  619. return ret;
  620. }
  621. EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
  622. int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
  623. {
  624. struct phy_device *phydev;
  625. int ret = -EOPNOTSUPP;
  626. if (of_phy_is_fixed_link(dp->dn))
  627. return ret;
  628. phydev = dsa_port_get_phy_device(dp);
  629. if (IS_ERR_OR_NULL(phydev))
  630. return ret;
  631. ret = phy_ethtool_get_stats(phydev, NULL, data);
  632. put_device(&phydev->mdio.dev);
  633. return ret;
  634. }
  635. EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
  636. int dsa_port_get_phy_sset_count(struct dsa_port *dp)
  637. {
  638. struct phy_device *phydev;
  639. int ret = -EOPNOTSUPP;
  640. if (of_phy_is_fixed_link(dp->dn))
  641. return ret;
  642. phydev = dsa_port_get_phy_device(dp);
  643. if (IS_ERR_OR_NULL(phydev))
  644. return ret;
  645. ret = phy_ethtool_get_sset_count(phydev);
  646. put_device(&phydev->mdio.dev);
  647. return ret;
  648. }
  649. EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);