phylink.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. /*
  2. * phylink models the MAC to optional PHY connection, supporting
  3. * technologies such as SFP cages where the PHY is hot-pluggable.
  4. *
  5. * Copyright (C) 2015 Russell King
  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 version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/ethtool.h>
  12. #include <linux/export.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/of.h>
  16. #include <linux/of_mdio.h>
  17. #include <linux/phy.h>
  18. #include <linux/phy_fixed.h>
  19. #include <linux/phylink.h>
  20. #include <linux/rtnetlink.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/workqueue.h>
  23. #include "sfp.h"
  24. #include "swphy.h"
  25. #define SUPPORTED_INTERFACES \
  26. (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
  27. SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
  28. #define ADVERTISED_INTERFACES \
  29. (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
  30. ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
  31. enum {
  32. PHYLINK_DISABLE_STOPPED,
  33. PHYLINK_DISABLE_LINK,
  34. };
  35. struct phylink {
  36. struct net_device *netdev;
  37. const struct phylink_mac_ops *ops;
  38. unsigned long phylink_disable_state; /* bitmask of disables */
  39. struct phy_device *phydev;
  40. phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
  41. u8 link_an_mode; /* MLO_AN_xxx */
  42. u8 link_port; /* The current non-phy ethtool port */
  43. __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
  44. /* The link configuration settings */
  45. struct phylink_link_state link_config;
  46. struct gpio_desc *link_gpio;
  47. struct mutex state_mutex;
  48. struct phylink_link_state phy_state;
  49. struct work_struct resolve;
  50. bool mac_link_dropped;
  51. struct sfp_bus *sfp_bus;
  52. };
  53. static inline void linkmode_zero(unsigned long *dst)
  54. {
  55. bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
  56. }
  57. static inline void linkmode_copy(unsigned long *dst, const unsigned long *src)
  58. {
  59. bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS);
  60. }
  61. static inline void linkmode_and(unsigned long *dst, const unsigned long *a,
  62. const unsigned long *b)
  63. {
  64. bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
  65. }
  66. static inline void linkmode_or(unsigned long *dst, const unsigned long *a,
  67. const unsigned long *b)
  68. {
  69. bitmap_or(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
  70. }
  71. static inline bool linkmode_empty(const unsigned long *src)
  72. {
  73. return bitmap_empty(src, __ETHTOOL_LINK_MODE_MASK_NBITS);
  74. }
  75. void phylink_set_port_modes(unsigned long *mask)
  76. {
  77. phylink_set(mask, TP);
  78. phylink_set(mask, AUI);
  79. phylink_set(mask, MII);
  80. phylink_set(mask, FIBRE);
  81. phylink_set(mask, BNC);
  82. phylink_set(mask, Backplane);
  83. }
  84. EXPORT_SYMBOL_GPL(phylink_set_port_modes);
  85. static int phylink_is_empty_linkmode(const unsigned long *linkmode)
  86. {
  87. __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
  88. phylink_set_port_modes(tmp);
  89. phylink_set(tmp, Autoneg);
  90. phylink_set(tmp, Pause);
  91. phylink_set(tmp, Asym_Pause);
  92. bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
  93. return linkmode_empty(tmp);
  94. }
  95. static const char *phylink_an_mode_str(unsigned int mode)
  96. {
  97. static const char *modestr[] = {
  98. [MLO_AN_PHY] = "phy",
  99. [MLO_AN_FIXED] = "fixed",
  100. [MLO_AN_SGMII] = "SGMII",
  101. [MLO_AN_8023Z] = "802.3z",
  102. };
  103. return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
  104. }
  105. static int phylink_validate(struct phylink *pl, unsigned long *supported,
  106. struct phylink_link_state *state)
  107. {
  108. pl->ops->validate(pl->netdev, supported, state);
  109. return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
  110. }
  111. static int phylink_parse_fixedlink(struct phylink *pl, struct device_node *np)
  112. {
  113. struct device_node *fixed_node;
  114. const struct phy_setting *s;
  115. struct gpio_desc *desc;
  116. const __be32 *fixed_prop;
  117. u32 speed;
  118. int ret, len;
  119. fixed_node = of_get_child_by_name(np, "fixed-link");
  120. if (fixed_node) {
  121. ret = of_property_read_u32(fixed_node, "speed", &speed);
  122. pl->link_config.speed = speed;
  123. pl->link_config.duplex = DUPLEX_HALF;
  124. if (of_property_read_bool(fixed_node, "full-duplex"))
  125. pl->link_config.duplex = DUPLEX_FULL;
  126. /* We treat the "pause" and "asym-pause" terminology as
  127. * defining the link partner's ability. */
  128. if (of_property_read_bool(fixed_node, "pause"))
  129. pl->link_config.pause |= MLO_PAUSE_SYM;
  130. if (of_property_read_bool(fixed_node, "asym-pause"))
  131. pl->link_config.pause |= MLO_PAUSE_ASYM;
  132. if (ret == 0) {
  133. desc = fwnode_get_named_gpiod(&fixed_node->fwnode,
  134. "link-gpios", 0,
  135. GPIOD_IN, "?");
  136. if (!IS_ERR(desc))
  137. pl->link_gpio = desc;
  138. else if (desc == ERR_PTR(-EPROBE_DEFER))
  139. ret = -EPROBE_DEFER;
  140. }
  141. of_node_put(fixed_node);
  142. if (ret)
  143. return ret;
  144. } else {
  145. fixed_prop = of_get_property(np, "fixed-link", &len);
  146. if (!fixed_prop) {
  147. netdev_err(pl->netdev, "broken fixed-link?\n");
  148. return -EINVAL;
  149. }
  150. if (len == 5 * sizeof(*fixed_prop)) {
  151. pl->link_config.duplex = be32_to_cpu(fixed_prop[1]) ?
  152. DUPLEX_FULL : DUPLEX_HALF;
  153. pl->link_config.speed = be32_to_cpu(fixed_prop[2]);
  154. if (be32_to_cpu(fixed_prop[3]))
  155. pl->link_config.pause |= MLO_PAUSE_SYM;
  156. if (be32_to_cpu(fixed_prop[4]))
  157. pl->link_config.pause |= MLO_PAUSE_ASYM;
  158. }
  159. }
  160. if (pl->link_config.speed > SPEED_1000 &&
  161. pl->link_config.duplex != DUPLEX_FULL)
  162. netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
  163. pl->link_config.speed);
  164. bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
  165. linkmode_copy(pl->link_config.advertising, pl->supported);
  166. phylink_validate(pl, pl->supported, &pl->link_config);
  167. s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
  168. pl->supported,
  169. __ETHTOOL_LINK_MODE_MASK_NBITS, true);
  170. linkmode_zero(pl->supported);
  171. phylink_set(pl->supported, MII);
  172. if (s) {
  173. __set_bit(s->bit, pl->supported);
  174. } else {
  175. netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n",
  176. pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
  177. pl->link_config.speed);
  178. }
  179. linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
  180. pl->supported);
  181. pl->link_config.link = 1;
  182. pl->link_config.an_complete = 1;
  183. return 0;
  184. }
  185. static int phylink_parse_mode(struct phylink *pl, struct device_node *np)
  186. {
  187. struct device_node *dn;
  188. const char *managed;
  189. dn = of_get_child_by_name(np, "fixed-link");
  190. if (dn || of_find_property(np, "fixed-link", NULL))
  191. pl->link_an_mode = MLO_AN_FIXED;
  192. of_node_put(dn);
  193. if (of_property_read_string(np, "managed", &managed) == 0 &&
  194. strcmp(managed, "in-band-status") == 0) {
  195. if (pl->link_an_mode == MLO_AN_FIXED) {
  196. netdev_err(pl->netdev,
  197. "can't use both fixed-link and in-band-status\n");
  198. return -EINVAL;
  199. }
  200. linkmode_zero(pl->supported);
  201. phylink_set(pl->supported, MII);
  202. phylink_set(pl->supported, Autoneg);
  203. phylink_set(pl->supported, Asym_Pause);
  204. phylink_set(pl->supported, Pause);
  205. pl->link_config.an_enabled = true;
  206. switch (pl->link_config.interface) {
  207. case PHY_INTERFACE_MODE_SGMII:
  208. phylink_set(pl->supported, 10baseT_Half);
  209. phylink_set(pl->supported, 10baseT_Full);
  210. phylink_set(pl->supported, 100baseT_Half);
  211. phylink_set(pl->supported, 100baseT_Full);
  212. phylink_set(pl->supported, 1000baseT_Half);
  213. phylink_set(pl->supported, 1000baseT_Full);
  214. pl->link_an_mode = MLO_AN_SGMII;
  215. break;
  216. case PHY_INTERFACE_MODE_1000BASEX:
  217. phylink_set(pl->supported, 1000baseX_Full);
  218. pl->link_an_mode = MLO_AN_8023Z;
  219. break;
  220. case PHY_INTERFACE_MODE_2500BASEX:
  221. phylink_set(pl->supported, 2500baseX_Full);
  222. pl->link_an_mode = MLO_AN_8023Z;
  223. break;
  224. case PHY_INTERFACE_MODE_10GKR:
  225. phylink_set(pl->supported, 10baseT_Half);
  226. phylink_set(pl->supported, 10baseT_Full);
  227. phylink_set(pl->supported, 100baseT_Half);
  228. phylink_set(pl->supported, 100baseT_Full);
  229. phylink_set(pl->supported, 1000baseT_Half);
  230. phylink_set(pl->supported, 1000baseT_Full);
  231. phylink_set(pl->supported, 1000baseX_Full);
  232. phylink_set(pl->supported, 10000baseKR_Full);
  233. phylink_set(pl->supported, 10000baseCR_Full);
  234. phylink_set(pl->supported, 10000baseSR_Full);
  235. phylink_set(pl->supported, 10000baseLR_Full);
  236. phylink_set(pl->supported, 10000baseLRM_Full);
  237. phylink_set(pl->supported, 10000baseER_Full);
  238. pl->link_an_mode = MLO_AN_SGMII;
  239. break;
  240. default:
  241. netdev_err(pl->netdev,
  242. "incorrect link mode %s for in-band status\n",
  243. phy_modes(pl->link_config.interface));
  244. return -EINVAL;
  245. }
  246. linkmode_copy(pl->link_config.advertising, pl->supported);
  247. if (phylink_validate(pl, pl->supported, &pl->link_config)) {
  248. netdev_err(pl->netdev,
  249. "failed to validate link configuration for in-band status\n");
  250. return -EINVAL;
  251. }
  252. }
  253. return 0;
  254. }
  255. static void phylink_mac_config(struct phylink *pl,
  256. const struct phylink_link_state *state)
  257. {
  258. netdev_dbg(pl->netdev,
  259. "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
  260. __func__, phylink_an_mode_str(pl->link_an_mode),
  261. phy_modes(state->interface),
  262. phy_speed_to_str(state->speed),
  263. phy_duplex_to_str(state->duplex),
  264. __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
  265. state->pause, state->link, state->an_enabled);
  266. pl->ops->mac_config(pl->netdev, pl->link_an_mode, state);
  267. }
  268. static void phylink_mac_an_restart(struct phylink *pl)
  269. {
  270. if (pl->link_config.an_enabled &&
  271. (pl->link_config.interface == PHY_INTERFACE_MODE_1000BASEX ||
  272. pl->link_config.interface == PHY_INTERFACE_MODE_2500BASEX))
  273. pl->ops->mac_an_restart(pl->netdev);
  274. }
  275. static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
  276. {
  277. struct net_device *ndev = pl->netdev;
  278. linkmode_copy(state->advertising, pl->link_config.advertising);
  279. linkmode_zero(state->lp_advertising);
  280. state->interface = pl->link_config.interface;
  281. state->an_enabled = pl->link_config.an_enabled;
  282. state->link = 1;
  283. return pl->ops->mac_link_state(ndev, state);
  284. }
  285. /* The fixed state is... fixed except for the link state,
  286. * which may be determined by a GPIO.
  287. */
  288. static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
  289. {
  290. *state = pl->link_config;
  291. if (pl->link_gpio)
  292. state->link = !!gpiod_get_value(pl->link_gpio);
  293. }
  294. /* Flow control is resolved according to our and the link partners
  295. * advertisments using the following drawn from the 802.3 specs:
  296. * Local device Link partner
  297. * Pause AsymDir Pause AsymDir Result
  298. * 1 X 1 X TX+RX
  299. * 0 1 1 1 RX
  300. * 1 1 0 1 TX
  301. */
  302. static void phylink_resolve_flow(struct phylink *pl,
  303. struct phylink_link_state *state)
  304. {
  305. int new_pause = 0;
  306. if (pl->link_config.pause & MLO_PAUSE_AN) {
  307. int pause = 0;
  308. if (phylink_test(pl->link_config.advertising, Pause))
  309. pause |= MLO_PAUSE_SYM;
  310. if (phylink_test(pl->link_config.advertising, Asym_Pause))
  311. pause |= MLO_PAUSE_ASYM;
  312. pause &= state->pause;
  313. if (pause & MLO_PAUSE_SYM)
  314. new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
  315. else if (pause & MLO_PAUSE_ASYM)
  316. new_pause = state->pause & MLO_PAUSE_SYM ?
  317. MLO_PAUSE_RX : MLO_PAUSE_TX;
  318. } else {
  319. new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
  320. }
  321. state->pause &= ~MLO_PAUSE_TXRX_MASK;
  322. state->pause |= new_pause;
  323. }
  324. static const char *phylink_pause_to_str(int pause)
  325. {
  326. switch (pause & MLO_PAUSE_TXRX_MASK) {
  327. case MLO_PAUSE_TX | MLO_PAUSE_RX:
  328. return "rx/tx";
  329. case MLO_PAUSE_TX:
  330. return "tx";
  331. case MLO_PAUSE_RX:
  332. return "rx";
  333. default:
  334. return "off";
  335. }
  336. }
  337. static void phylink_resolve(struct work_struct *w)
  338. {
  339. struct phylink *pl = container_of(w, struct phylink, resolve);
  340. struct phylink_link_state link_state;
  341. struct net_device *ndev = pl->netdev;
  342. mutex_lock(&pl->state_mutex);
  343. if (pl->phylink_disable_state) {
  344. pl->mac_link_dropped = false;
  345. link_state.link = false;
  346. } else if (pl->mac_link_dropped) {
  347. link_state.link = false;
  348. } else {
  349. switch (pl->link_an_mode) {
  350. case MLO_AN_PHY:
  351. link_state = pl->phy_state;
  352. phylink_resolve_flow(pl, &link_state);
  353. phylink_mac_config(pl, &link_state);
  354. break;
  355. case MLO_AN_FIXED:
  356. phylink_get_fixed_state(pl, &link_state);
  357. phylink_mac_config(pl, &link_state);
  358. break;
  359. case MLO_AN_SGMII:
  360. phylink_get_mac_state(pl, &link_state);
  361. if (pl->phydev) {
  362. bool changed = false;
  363. link_state.link = link_state.link &&
  364. pl->phy_state.link;
  365. if (pl->phy_state.interface !=
  366. link_state.interface) {
  367. link_state.interface = pl->phy_state.interface;
  368. changed = true;
  369. }
  370. /* Propagate the flow control from the PHY
  371. * to the MAC. Also propagate the interface
  372. * if changed.
  373. */
  374. if (pl->phy_state.link || changed) {
  375. link_state.pause |= pl->phy_state.pause;
  376. phylink_resolve_flow(pl, &link_state);
  377. phylink_mac_config(pl, &link_state);
  378. }
  379. }
  380. break;
  381. case MLO_AN_8023Z:
  382. phylink_get_mac_state(pl, &link_state);
  383. break;
  384. }
  385. }
  386. if (link_state.link != netif_carrier_ok(ndev)) {
  387. if (!link_state.link) {
  388. netif_carrier_off(ndev);
  389. pl->ops->mac_link_down(ndev, pl->link_an_mode);
  390. netdev_info(ndev, "Link is Down\n");
  391. } else {
  392. pl->ops->mac_link_up(ndev, pl->link_an_mode,
  393. pl->phydev);
  394. netif_carrier_on(ndev);
  395. netdev_info(ndev,
  396. "Link is Up - %s/%s - flow control %s\n",
  397. phy_speed_to_str(link_state.speed),
  398. phy_duplex_to_str(link_state.duplex),
  399. phylink_pause_to_str(link_state.pause));
  400. }
  401. }
  402. if (!link_state.link && pl->mac_link_dropped) {
  403. pl->mac_link_dropped = false;
  404. queue_work(system_power_efficient_wq, &pl->resolve);
  405. }
  406. mutex_unlock(&pl->state_mutex);
  407. }
  408. static void phylink_run_resolve(struct phylink *pl)
  409. {
  410. if (!pl->phylink_disable_state)
  411. queue_work(system_power_efficient_wq, &pl->resolve);
  412. }
  413. static const struct sfp_upstream_ops sfp_phylink_ops;
  414. static int phylink_register_sfp(struct phylink *pl, struct device_node *np)
  415. {
  416. struct device_node *sfp_np;
  417. sfp_np = of_parse_phandle(np, "sfp", 0);
  418. if (!sfp_np)
  419. return 0;
  420. pl->sfp_bus = sfp_register_upstream(sfp_np, pl->netdev, pl,
  421. &sfp_phylink_ops);
  422. if (!pl->sfp_bus)
  423. return -ENOMEM;
  424. return 0;
  425. }
  426. struct phylink *phylink_create(struct net_device *ndev, struct device_node *np,
  427. phy_interface_t iface, const struct phylink_mac_ops *ops)
  428. {
  429. struct phylink *pl;
  430. int ret;
  431. pl = kzalloc(sizeof(*pl), GFP_KERNEL);
  432. if (!pl)
  433. return ERR_PTR(-ENOMEM);
  434. mutex_init(&pl->state_mutex);
  435. INIT_WORK(&pl->resolve, phylink_resolve);
  436. pl->netdev = ndev;
  437. pl->phy_state.interface = iface;
  438. pl->link_interface = iface;
  439. pl->link_port = PORT_MII;
  440. pl->link_config.interface = iface;
  441. pl->link_config.pause = MLO_PAUSE_AN;
  442. pl->link_config.speed = SPEED_UNKNOWN;
  443. pl->link_config.duplex = DUPLEX_UNKNOWN;
  444. pl->ops = ops;
  445. __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
  446. bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
  447. linkmode_copy(pl->link_config.advertising, pl->supported);
  448. phylink_validate(pl, pl->supported, &pl->link_config);
  449. ret = phylink_parse_mode(pl, np);
  450. if (ret < 0) {
  451. kfree(pl);
  452. return ERR_PTR(ret);
  453. }
  454. if (pl->link_an_mode == MLO_AN_FIXED) {
  455. ret = phylink_parse_fixedlink(pl, np);
  456. if (ret < 0) {
  457. kfree(pl);
  458. return ERR_PTR(ret);
  459. }
  460. }
  461. ret = phylink_register_sfp(pl, np);
  462. if (ret < 0) {
  463. kfree(pl);
  464. return ERR_PTR(ret);
  465. }
  466. return pl;
  467. }
  468. EXPORT_SYMBOL_GPL(phylink_create);
  469. void phylink_destroy(struct phylink *pl)
  470. {
  471. if (pl->sfp_bus)
  472. sfp_unregister_upstream(pl->sfp_bus);
  473. cancel_work_sync(&pl->resolve);
  474. kfree(pl);
  475. }
  476. EXPORT_SYMBOL_GPL(phylink_destroy);
  477. void phylink_phy_change(struct phy_device *phydev, bool up, bool do_carrier)
  478. {
  479. struct phylink *pl = phydev->phylink;
  480. mutex_lock(&pl->state_mutex);
  481. pl->phy_state.speed = phydev->speed;
  482. pl->phy_state.duplex = phydev->duplex;
  483. pl->phy_state.pause = MLO_PAUSE_NONE;
  484. if (phydev->pause)
  485. pl->phy_state.pause |= MLO_PAUSE_SYM;
  486. if (phydev->asym_pause)
  487. pl->phy_state.pause |= MLO_PAUSE_ASYM;
  488. pl->phy_state.interface = phydev->interface;
  489. pl->phy_state.link = up;
  490. mutex_unlock(&pl->state_mutex);
  491. phylink_run_resolve(pl);
  492. netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
  493. phy_modes(phydev->interface),
  494. phy_speed_to_str(phydev->speed),
  495. phy_duplex_to_str(phydev->duplex));
  496. }
  497. static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
  498. {
  499. struct phylink_link_state config;
  500. __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
  501. u32 advertising;
  502. int ret;
  503. memset(&config, 0, sizeof(config));
  504. ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported);
  505. ethtool_convert_legacy_u32_to_link_mode(config.advertising,
  506. phy->advertising);
  507. config.interface = pl->link_config.interface;
  508. /*
  509. * This is the new way of dealing with flow control for PHYs,
  510. * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
  511. * phy drivers should not set SUPPORTED_[Asym_]Pause") except
  512. * using our validate call to the MAC, we rely upon the MAC
  513. * clearing the bits from both supported and advertising fields.
  514. */
  515. if (phylink_test(supported, Pause))
  516. phylink_set(config.advertising, Pause);
  517. if (phylink_test(supported, Asym_Pause))
  518. phylink_set(config.advertising, Asym_Pause);
  519. ret = phylink_validate(pl, supported, &config);
  520. if (ret)
  521. return ret;
  522. phy->phylink = pl;
  523. phy->phy_link_change = phylink_phy_change;
  524. netdev_info(pl->netdev,
  525. "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
  526. phy->drv->name);
  527. mutex_lock(&phy->lock);
  528. mutex_lock(&pl->state_mutex);
  529. pl->netdev->phydev = phy;
  530. pl->phydev = phy;
  531. linkmode_copy(pl->supported, supported);
  532. linkmode_copy(pl->link_config.advertising, config.advertising);
  533. /* Restrict the phy advertisment according to the MAC support. */
  534. ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising);
  535. phy->advertising = advertising;
  536. mutex_unlock(&pl->state_mutex);
  537. mutex_unlock(&phy->lock);
  538. netdev_dbg(pl->netdev,
  539. "phy: setting supported %*pb advertising 0x%08x\n",
  540. __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
  541. phy->advertising);
  542. phy_start_machine(phy);
  543. if (phy->irq > 0)
  544. phy_start_interrupts(phy);
  545. return 0;
  546. }
  547. int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
  548. {
  549. int ret;
  550. ret = phy_attach_direct(pl->netdev, phy, 0, pl->link_interface);
  551. if (ret)
  552. return ret;
  553. ret = phylink_bringup_phy(pl, phy);
  554. if (ret)
  555. phy_detach(phy);
  556. return ret;
  557. }
  558. EXPORT_SYMBOL_GPL(phylink_connect_phy);
  559. int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn)
  560. {
  561. struct device_node *phy_node;
  562. struct phy_device *phy_dev;
  563. int ret;
  564. /* Fixed links are handled without needing a PHY */
  565. if (pl->link_an_mode == MLO_AN_FIXED)
  566. return 0;
  567. phy_node = of_parse_phandle(dn, "phy-handle", 0);
  568. if (!phy_node)
  569. phy_node = of_parse_phandle(dn, "phy", 0);
  570. if (!phy_node)
  571. phy_node = of_parse_phandle(dn, "phy-device", 0);
  572. if (!phy_node) {
  573. if (pl->link_an_mode == MLO_AN_PHY) {
  574. netdev_err(pl->netdev, "unable to find PHY node\n");
  575. return -ENODEV;
  576. }
  577. return 0;
  578. }
  579. phy_dev = of_phy_attach(pl->netdev, phy_node, 0, pl->link_interface);
  580. /* We're done with the phy_node handle */
  581. of_node_put(phy_node);
  582. if (!phy_dev)
  583. return -ENODEV;
  584. ret = phylink_bringup_phy(pl, phy_dev);
  585. if (ret)
  586. phy_detach(phy_dev);
  587. return ret;
  588. }
  589. EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
  590. void phylink_disconnect_phy(struct phylink *pl)
  591. {
  592. struct phy_device *phy;
  593. WARN_ON(!lockdep_rtnl_is_held());
  594. phy = pl->phydev;
  595. if (phy) {
  596. mutex_lock(&phy->lock);
  597. mutex_lock(&pl->state_mutex);
  598. pl->netdev->phydev = NULL;
  599. pl->phydev = NULL;
  600. mutex_unlock(&pl->state_mutex);
  601. mutex_unlock(&phy->lock);
  602. flush_work(&pl->resolve);
  603. phy_disconnect(phy);
  604. }
  605. }
  606. EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
  607. void phylink_mac_change(struct phylink *pl, bool up)
  608. {
  609. if (!up)
  610. pl->mac_link_dropped = true;
  611. phylink_run_resolve(pl);
  612. netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
  613. }
  614. EXPORT_SYMBOL_GPL(phylink_mac_change);
  615. void phylink_start(struct phylink *pl)
  616. {
  617. WARN_ON(!lockdep_rtnl_is_held());
  618. netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
  619. phylink_an_mode_str(pl->link_an_mode),
  620. phy_modes(pl->link_config.interface));
  621. /* Apply the link configuration to the MAC when starting. This allows
  622. * a fixed-link to start with the correct parameters, and also
  623. * ensures that we set the appropriate advertisment for Serdes links.
  624. */
  625. phylink_resolve_flow(pl, &pl->link_config);
  626. phylink_mac_config(pl, &pl->link_config);
  627. clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
  628. phylink_run_resolve(pl);
  629. if (pl->sfp_bus)
  630. sfp_upstream_start(pl->sfp_bus);
  631. if (pl->phydev)
  632. phy_start(pl->phydev);
  633. }
  634. EXPORT_SYMBOL_GPL(phylink_start);
  635. void phylink_stop(struct phylink *pl)
  636. {
  637. WARN_ON(!lockdep_rtnl_is_held());
  638. if (pl->phydev)
  639. phy_stop(pl->phydev);
  640. if (pl->sfp_bus)
  641. sfp_upstream_stop(pl->sfp_bus);
  642. set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
  643. flush_work(&pl->resolve);
  644. }
  645. EXPORT_SYMBOL_GPL(phylink_stop);
  646. void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
  647. {
  648. WARN_ON(!lockdep_rtnl_is_held());
  649. wol->supported = 0;
  650. wol->wolopts = 0;
  651. if (pl->phydev)
  652. phy_ethtool_get_wol(pl->phydev, wol);
  653. }
  654. EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
  655. int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
  656. {
  657. int ret = -EOPNOTSUPP;
  658. WARN_ON(!lockdep_rtnl_is_held());
  659. if (pl->phydev)
  660. ret = phy_ethtool_set_wol(pl->phydev, wol);
  661. return ret;
  662. }
  663. EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
  664. static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
  665. {
  666. __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
  667. linkmode_zero(mask);
  668. phylink_set_port_modes(mask);
  669. linkmode_and(dst, dst, mask);
  670. linkmode_or(dst, dst, b);
  671. }
  672. static void phylink_get_ksettings(const struct phylink_link_state *state,
  673. struct ethtool_link_ksettings *kset)
  674. {
  675. phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
  676. linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
  677. kset->base.speed = state->speed;
  678. kset->base.duplex = state->duplex;
  679. kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
  680. AUTONEG_DISABLE;
  681. }
  682. int phylink_ethtool_ksettings_get(struct phylink *pl,
  683. struct ethtool_link_ksettings *kset)
  684. {
  685. struct phylink_link_state link_state;
  686. WARN_ON(!lockdep_rtnl_is_held());
  687. if (pl->phydev) {
  688. phy_ethtool_ksettings_get(pl->phydev, kset);
  689. } else {
  690. kset->base.port = pl->link_port;
  691. }
  692. linkmode_copy(kset->link_modes.supported, pl->supported);
  693. switch (pl->link_an_mode) {
  694. case MLO_AN_FIXED:
  695. /* We are using fixed settings. Report these as the
  696. * current link settings - and note that these also
  697. * represent the supported speeds/duplex/pause modes.
  698. */
  699. phylink_get_fixed_state(pl, &link_state);
  700. phylink_get_ksettings(&link_state, kset);
  701. break;
  702. case MLO_AN_SGMII:
  703. /* If there is a phy attached, then use the reported
  704. * settings from the phy with no modification.
  705. */
  706. if (pl->phydev)
  707. break;
  708. case MLO_AN_8023Z:
  709. phylink_get_mac_state(pl, &link_state);
  710. /* The MAC is reporting the link results from its own PCS
  711. * layer via in-band status. Report these as the current
  712. * link settings.
  713. */
  714. phylink_get_ksettings(&link_state, kset);
  715. break;
  716. }
  717. return 0;
  718. }
  719. EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
  720. int phylink_ethtool_ksettings_set(struct phylink *pl,
  721. const struct ethtool_link_ksettings *kset)
  722. {
  723. struct ethtool_link_ksettings our_kset;
  724. struct phylink_link_state config;
  725. int ret;
  726. WARN_ON(!lockdep_rtnl_is_held());
  727. if (kset->base.autoneg != AUTONEG_DISABLE &&
  728. kset->base.autoneg != AUTONEG_ENABLE)
  729. return -EINVAL;
  730. config = pl->link_config;
  731. /* Mask out unsupported advertisments */
  732. linkmode_and(config.advertising, kset->link_modes.advertising,
  733. pl->supported);
  734. /* FIXME: should we reject autoneg if phy/mac does not support it? */
  735. if (kset->base.autoneg == AUTONEG_DISABLE) {
  736. const struct phy_setting *s;
  737. /* Autonegotiation disabled, select a suitable speed and
  738. * duplex.
  739. */
  740. s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
  741. pl->supported,
  742. __ETHTOOL_LINK_MODE_MASK_NBITS, false);
  743. if (!s)
  744. return -EINVAL;
  745. /* If we have a fixed link (as specified by firmware), refuse
  746. * to change link parameters.
  747. */
  748. if (pl->link_an_mode == MLO_AN_FIXED &&
  749. (s->speed != pl->link_config.speed ||
  750. s->duplex != pl->link_config.duplex))
  751. return -EINVAL;
  752. config.speed = s->speed;
  753. config.duplex = s->duplex;
  754. config.an_enabled = false;
  755. __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
  756. } else {
  757. /* If we have a fixed link, refuse to enable autonegotiation */
  758. if (pl->link_an_mode == MLO_AN_FIXED)
  759. return -EINVAL;
  760. config.speed = SPEED_UNKNOWN;
  761. config.duplex = DUPLEX_UNKNOWN;
  762. config.an_enabled = true;
  763. __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
  764. }
  765. if (phylink_validate(pl, pl->supported, &config))
  766. return -EINVAL;
  767. /* If autonegotiation is enabled, we must have an advertisment */
  768. if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
  769. return -EINVAL;
  770. our_kset = *kset;
  771. linkmode_copy(our_kset.link_modes.advertising, config.advertising);
  772. our_kset.base.speed = config.speed;
  773. our_kset.base.duplex = config.duplex;
  774. /* If we have a PHY, configure the phy */
  775. if (pl->phydev) {
  776. ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
  777. if (ret)
  778. return ret;
  779. }
  780. mutex_lock(&pl->state_mutex);
  781. /* Configure the MAC to match the new settings */
  782. linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
  783. pl->link_config.speed = our_kset.base.speed;
  784. pl->link_config.duplex = our_kset.base.duplex;
  785. pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
  786. if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
  787. phylink_mac_config(pl, &pl->link_config);
  788. phylink_mac_an_restart(pl);
  789. }
  790. mutex_unlock(&pl->state_mutex);
  791. return 0;
  792. }
  793. EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
  794. int phylink_ethtool_nway_reset(struct phylink *pl)
  795. {
  796. int ret = 0;
  797. WARN_ON(!lockdep_rtnl_is_held());
  798. if (pl->phydev)
  799. ret = phy_restart_aneg(pl->phydev);
  800. phylink_mac_an_restart(pl);
  801. return ret;
  802. }
  803. EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
  804. void phylink_ethtool_get_pauseparam(struct phylink *pl,
  805. struct ethtool_pauseparam *pause)
  806. {
  807. WARN_ON(!lockdep_rtnl_is_held());
  808. pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
  809. pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
  810. pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
  811. }
  812. EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
  813. int phylink_ethtool_set_pauseparam(struct phylink *pl,
  814. struct ethtool_pauseparam *pause)
  815. {
  816. struct phylink_link_state *config = &pl->link_config;
  817. WARN_ON(!lockdep_rtnl_is_held());
  818. if (!phylink_test(pl->supported, Pause) &&
  819. !phylink_test(pl->supported, Asym_Pause))
  820. return -EOPNOTSUPP;
  821. if (!phylink_test(pl->supported, Asym_Pause) &&
  822. !pause->autoneg && pause->rx_pause != pause->tx_pause)
  823. return -EINVAL;
  824. config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
  825. if (pause->autoneg)
  826. config->pause |= MLO_PAUSE_AN;
  827. if (pause->rx_pause)
  828. config->pause |= MLO_PAUSE_RX;
  829. if (pause->tx_pause)
  830. config->pause |= MLO_PAUSE_TX;
  831. if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
  832. switch (pl->link_an_mode) {
  833. case MLO_AN_PHY:
  834. /* Silently mark the carrier down, and then trigger a resolve */
  835. netif_carrier_off(pl->netdev);
  836. phylink_run_resolve(pl);
  837. break;
  838. case MLO_AN_FIXED:
  839. /* Should we allow fixed links to change against the config? */
  840. phylink_resolve_flow(pl, config);
  841. phylink_mac_config(pl, config);
  842. break;
  843. case MLO_AN_SGMII:
  844. case MLO_AN_8023Z:
  845. phylink_mac_config(pl, config);
  846. phylink_mac_an_restart(pl);
  847. break;
  848. }
  849. }
  850. return 0;
  851. }
  852. EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
  853. int phylink_ethtool_get_module_info(struct phylink *pl,
  854. struct ethtool_modinfo *modinfo)
  855. {
  856. int ret = -EOPNOTSUPP;
  857. WARN_ON(!lockdep_rtnl_is_held());
  858. if (pl->sfp_bus)
  859. ret = sfp_get_module_info(pl->sfp_bus, modinfo);
  860. return ret;
  861. }
  862. EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info);
  863. int phylink_ethtool_get_module_eeprom(struct phylink *pl,
  864. struct ethtool_eeprom *ee, u8 *buf)
  865. {
  866. int ret = -EOPNOTSUPP;
  867. WARN_ON(!lockdep_rtnl_is_held());
  868. if (pl->sfp_bus)
  869. ret = sfp_get_module_eeprom(pl->sfp_bus, ee, buf);
  870. return ret;
  871. }
  872. EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom);
  873. int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
  874. {
  875. int ret = -EPROTONOSUPPORT;
  876. WARN_ON(!lockdep_rtnl_is_held());
  877. if (pl->phydev)
  878. ret = phy_init_eee(pl->phydev, clk_stop_enable);
  879. return ret;
  880. }
  881. EXPORT_SYMBOL_GPL(phylink_init_eee);
  882. int phylink_get_eee_err(struct phylink *pl)
  883. {
  884. int ret = 0;
  885. WARN_ON(!lockdep_rtnl_is_held());
  886. if (pl->phydev)
  887. ret = phy_get_eee_err(pl->phydev);
  888. return ret;
  889. }
  890. EXPORT_SYMBOL_GPL(phylink_get_eee_err);
  891. int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
  892. {
  893. int ret = -EOPNOTSUPP;
  894. WARN_ON(!lockdep_rtnl_is_held());
  895. if (pl->phydev)
  896. ret = phy_ethtool_get_eee(pl->phydev, eee);
  897. return ret;
  898. }
  899. EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
  900. int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
  901. {
  902. int ret = -EOPNOTSUPP;
  903. WARN_ON(!lockdep_rtnl_is_held());
  904. if (pl->phydev)
  905. ret = phy_ethtool_set_eee(pl->phydev, eee);
  906. return ret;
  907. }
  908. EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
  909. /* This emulates MII registers for a fixed-mode phy operating as per the
  910. * passed in state. "aneg" defines if we report negotiation is possible.
  911. *
  912. * FIXME: should deal with negotiation state too.
  913. */
  914. static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
  915. struct phylink_link_state *state, bool aneg)
  916. {
  917. struct fixed_phy_status fs;
  918. int val;
  919. fs.link = state->link;
  920. fs.speed = state->speed;
  921. fs.duplex = state->duplex;
  922. fs.pause = state->pause & MLO_PAUSE_SYM;
  923. fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
  924. val = swphy_read_reg(reg, &fs);
  925. if (reg == MII_BMSR) {
  926. if (!state->an_complete)
  927. val &= ~BMSR_ANEGCOMPLETE;
  928. if (!aneg)
  929. val &= ~BMSR_ANEGCAPABLE;
  930. }
  931. return val;
  932. }
  933. static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
  934. unsigned int reg)
  935. {
  936. struct phy_device *phydev = pl->phydev;
  937. int prtad, devad;
  938. if (mdio_phy_id_is_c45(phy_id)) {
  939. prtad = mdio_phy_id_prtad(phy_id);
  940. devad = mdio_phy_id_devad(phy_id);
  941. devad = MII_ADDR_C45 | devad << 16 | reg;
  942. } else if (phydev->is_c45) {
  943. switch (reg) {
  944. case MII_BMCR:
  945. case MII_BMSR:
  946. case MII_PHYSID1:
  947. case MII_PHYSID2:
  948. devad = __ffs(phydev->c45_ids.devices_in_package);
  949. break;
  950. case MII_ADVERTISE:
  951. case MII_LPA:
  952. if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
  953. return -EINVAL;
  954. devad = MDIO_MMD_AN;
  955. if (reg == MII_ADVERTISE)
  956. reg = MDIO_AN_ADVERTISE;
  957. else
  958. reg = MDIO_AN_LPA;
  959. break;
  960. default:
  961. return -EINVAL;
  962. }
  963. prtad = phy_id;
  964. devad = MII_ADDR_C45 | devad << 16 | reg;
  965. } else {
  966. prtad = phy_id;
  967. devad = reg;
  968. }
  969. return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
  970. }
  971. static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
  972. unsigned int reg, unsigned int val)
  973. {
  974. struct phy_device *phydev = pl->phydev;
  975. int prtad, devad;
  976. if (mdio_phy_id_is_c45(phy_id)) {
  977. prtad = mdio_phy_id_prtad(phy_id);
  978. devad = mdio_phy_id_devad(phy_id);
  979. devad = MII_ADDR_C45 | devad << 16 | reg;
  980. } else if (phydev->is_c45) {
  981. switch (reg) {
  982. case MII_BMCR:
  983. case MII_BMSR:
  984. case MII_PHYSID1:
  985. case MII_PHYSID2:
  986. devad = __ffs(phydev->c45_ids.devices_in_package);
  987. break;
  988. case MII_ADVERTISE:
  989. case MII_LPA:
  990. if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
  991. return -EINVAL;
  992. devad = MDIO_MMD_AN;
  993. if (reg == MII_ADVERTISE)
  994. reg = MDIO_AN_ADVERTISE;
  995. else
  996. reg = MDIO_AN_LPA;
  997. break;
  998. default:
  999. return -EINVAL;
  1000. }
  1001. prtad = phy_id;
  1002. devad = MII_ADDR_C45 | devad << 16 | reg;
  1003. } else {
  1004. prtad = phy_id;
  1005. devad = reg;
  1006. }
  1007. return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
  1008. }
  1009. static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
  1010. unsigned int reg)
  1011. {
  1012. struct phylink_link_state state;
  1013. int val = 0xffff;
  1014. switch (pl->link_an_mode) {
  1015. case MLO_AN_FIXED:
  1016. if (phy_id == 0) {
  1017. phylink_get_fixed_state(pl, &state);
  1018. val = phylink_mii_emul_read(pl->netdev, reg, &state,
  1019. true);
  1020. }
  1021. break;
  1022. case MLO_AN_PHY:
  1023. return -EOPNOTSUPP;
  1024. case MLO_AN_SGMII:
  1025. /* No phy, fall through to 8023z method */
  1026. case MLO_AN_8023Z:
  1027. if (phy_id == 0) {
  1028. val = phylink_get_mac_state(pl, &state);
  1029. if (val < 0)
  1030. return val;
  1031. val = phylink_mii_emul_read(pl->netdev, reg, &state,
  1032. true);
  1033. }
  1034. break;
  1035. }
  1036. return val & 0xffff;
  1037. }
  1038. static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
  1039. unsigned int reg, unsigned int val)
  1040. {
  1041. switch (pl->link_an_mode) {
  1042. case MLO_AN_FIXED:
  1043. break;
  1044. case MLO_AN_PHY:
  1045. return -EOPNOTSUPP;
  1046. case MLO_AN_SGMII:
  1047. /* No phy, fall through to 8023z method */
  1048. case MLO_AN_8023Z:
  1049. break;
  1050. }
  1051. return 0;
  1052. }
  1053. int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
  1054. {
  1055. struct mii_ioctl_data *mii = if_mii(ifr);
  1056. int ret;
  1057. WARN_ON(!lockdep_rtnl_is_held());
  1058. if (pl->phydev) {
  1059. /* PHYs only exist for MLO_AN_PHY and MLO_AN_SGMII */
  1060. switch (cmd) {
  1061. case SIOCGMIIPHY:
  1062. mii->phy_id = pl->phydev->mdio.addr;
  1063. case SIOCGMIIREG:
  1064. ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
  1065. if (ret >= 0) {
  1066. mii->val_out = ret;
  1067. ret = 0;
  1068. }
  1069. break;
  1070. case SIOCSMIIREG:
  1071. ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
  1072. mii->val_in);
  1073. break;
  1074. default:
  1075. ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
  1076. break;
  1077. }
  1078. } else {
  1079. switch (cmd) {
  1080. case SIOCGMIIPHY:
  1081. mii->phy_id = 0;
  1082. case SIOCGMIIREG:
  1083. ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
  1084. if (ret >= 0) {
  1085. mii->val_out = ret;
  1086. ret = 0;
  1087. }
  1088. break;
  1089. case SIOCSMIIREG:
  1090. ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
  1091. mii->val_in);
  1092. break;
  1093. default:
  1094. ret = -EOPNOTSUPP;
  1095. break;
  1096. }
  1097. }
  1098. return ret;
  1099. }
  1100. EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
  1101. static int phylink_sfp_module_insert(void *upstream,
  1102. const struct sfp_eeprom_id *id)
  1103. {
  1104. struct phylink *pl = upstream;
  1105. __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
  1106. struct phylink_link_state config;
  1107. phy_interface_t iface;
  1108. int mode, ret = 0;
  1109. bool changed;
  1110. u8 port;
  1111. sfp_parse_support(pl->sfp_bus, id, support);
  1112. port = sfp_parse_port(pl->sfp_bus, id, support);
  1113. iface = sfp_parse_interface(pl->sfp_bus, id);
  1114. WARN_ON(!lockdep_rtnl_is_held());
  1115. switch (iface) {
  1116. case PHY_INTERFACE_MODE_SGMII:
  1117. mode = MLO_AN_SGMII;
  1118. break;
  1119. case PHY_INTERFACE_MODE_1000BASEX:
  1120. mode = MLO_AN_8023Z;
  1121. break;
  1122. default:
  1123. return -EINVAL;
  1124. }
  1125. memset(&config, 0, sizeof(config));
  1126. linkmode_copy(config.advertising, support);
  1127. config.interface = iface;
  1128. config.speed = SPEED_UNKNOWN;
  1129. config.duplex = DUPLEX_UNKNOWN;
  1130. config.pause = MLO_PAUSE_AN;
  1131. config.an_enabled = pl->link_config.an_enabled;
  1132. /* Ignore errors if we're expecting a PHY to attach later */
  1133. ret = phylink_validate(pl, support, &config);
  1134. if (ret) {
  1135. netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
  1136. phylink_an_mode_str(mode), phy_modes(config.interface),
  1137. __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
  1138. return ret;
  1139. }
  1140. netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
  1141. phylink_an_mode_str(mode), phy_modes(config.interface),
  1142. __ETHTOOL_LINK_MODE_MASK_NBITS, support);
  1143. if (mode == MLO_AN_8023Z && pl->phydev)
  1144. return -EINVAL;
  1145. changed = !bitmap_equal(pl->supported, support,
  1146. __ETHTOOL_LINK_MODE_MASK_NBITS);
  1147. if (changed) {
  1148. linkmode_copy(pl->supported, support);
  1149. linkmode_copy(pl->link_config.advertising, config.advertising);
  1150. }
  1151. if (pl->link_an_mode != mode ||
  1152. pl->link_config.interface != config.interface) {
  1153. pl->link_config.interface = config.interface;
  1154. pl->link_an_mode = mode;
  1155. changed = true;
  1156. netdev_info(pl->netdev, "switched to %s/%s link mode\n",
  1157. phylink_an_mode_str(mode),
  1158. phy_modes(config.interface));
  1159. }
  1160. pl->link_port = port;
  1161. if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
  1162. &pl->phylink_disable_state))
  1163. phylink_mac_config(pl, &pl->link_config);
  1164. return ret;
  1165. }
  1166. static void phylink_sfp_link_down(void *upstream)
  1167. {
  1168. struct phylink *pl = upstream;
  1169. WARN_ON(!lockdep_rtnl_is_held());
  1170. set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
  1171. flush_work(&pl->resolve);
  1172. netif_carrier_off(pl->netdev);
  1173. }
  1174. static void phylink_sfp_link_up(void *upstream)
  1175. {
  1176. struct phylink *pl = upstream;
  1177. WARN_ON(!lockdep_rtnl_is_held());
  1178. clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
  1179. phylink_run_resolve(pl);
  1180. }
  1181. static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
  1182. {
  1183. return phylink_connect_phy(upstream, phy);
  1184. }
  1185. static void phylink_sfp_disconnect_phy(void *upstream)
  1186. {
  1187. phylink_disconnect_phy(upstream);
  1188. }
  1189. static const struct sfp_upstream_ops sfp_phylink_ops = {
  1190. .module_insert = phylink_sfp_module_insert,
  1191. .link_up = phylink_sfp_link_up,
  1192. .link_down = phylink_sfp_link_down,
  1193. .connect_phy = phylink_sfp_connect_phy,
  1194. .disconnect_phy = phylink_sfp_disconnect_phy,
  1195. };
  1196. MODULE_LICENSE("GPL");