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,
  428. const struct phylink_mac_ops *ops)
  429. {
  430. struct phylink *pl;
  431. int ret;
  432. pl = kzalloc(sizeof(*pl), GFP_KERNEL);
  433. if (!pl)
  434. return ERR_PTR(-ENOMEM);
  435. mutex_init(&pl->state_mutex);
  436. INIT_WORK(&pl->resolve, phylink_resolve);
  437. pl->netdev = ndev;
  438. pl->phy_state.interface = iface;
  439. pl->link_interface = iface;
  440. pl->link_port = PORT_MII;
  441. pl->link_config.interface = iface;
  442. pl->link_config.pause = MLO_PAUSE_AN;
  443. pl->link_config.speed = SPEED_UNKNOWN;
  444. pl->link_config.duplex = DUPLEX_UNKNOWN;
  445. pl->ops = ops;
  446. __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
  447. bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
  448. linkmode_copy(pl->link_config.advertising, pl->supported);
  449. phylink_validate(pl, pl->supported, &pl->link_config);
  450. ret = phylink_parse_mode(pl, np);
  451. if (ret < 0) {
  452. kfree(pl);
  453. return ERR_PTR(ret);
  454. }
  455. if (pl->link_an_mode == MLO_AN_FIXED) {
  456. ret = phylink_parse_fixedlink(pl, np);
  457. if (ret < 0) {
  458. kfree(pl);
  459. return ERR_PTR(ret);
  460. }
  461. }
  462. ret = phylink_register_sfp(pl, np);
  463. if (ret < 0) {
  464. kfree(pl);
  465. return ERR_PTR(ret);
  466. }
  467. return pl;
  468. }
  469. EXPORT_SYMBOL_GPL(phylink_create);
  470. void phylink_destroy(struct phylink *pl)
  471. {
  472. if (pl->sfp_bus)
  473. sfp_unregister_upstream(pl->sfp_bus);
  474. cancel_work_sync(&pl->resolve);
  475. kfree(pl);
  476. }
  477. EXPORT_SYMBOL_GPL(phylink_destroy);
  478. static void phylink_phy_change(struct phy_device *phydev, bool up,
  479. bool do_carrier)
  480. {
  481. struct phylink *pl = phydev->phylink;
  482. mutex_lock(&pl->state_mutex);
  483. pl->phy_state.speed = phydev->speed;
  484. pl->phy_state.duplex = phydev->duplex;
  485. pl->phy_state.pause = MLO_PAUSE_NONE;
  486. if (phydev->pause)
  487. pl->phy_state.pause |= MLO_PAUSE_SYM;
  488. if (phydev->asym_pause)
  489. pl->phy_state.pause |= MLO_PAUSE_ASYM;
  490. pl->phy_state.interface = phydev->interface;
  491. pl->phy_state.link = up;
  492. mutex_unlock(&pl->state_mutex);
  493. phylink_run_resolve(pl);
  494. netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
  495. phy_modes(phydev->interface),
  496. phy_speed_to_str(phydev->speed),
  497. phy_duplex_to_str(phydev->duplex));
  498. }
  499. static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
  500. {
  501. struct phylink_link_state config;
  502. __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
  503. u32 advertising;
  504. int ret;
  505. memset(&config, 0, sizeof(config));
  506. ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported);
  507. ethtool_convert_legacy_u32_to_link_mode(config.advertising,
  508. phy->advertising);
  509. config.interface = pl->link_config.interface;
  510. /*
  511. * This is the new way of dealing with flow control for PHYs,
  512. * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
  513. * phy drivers should not set SUPPORTED_[Asym_]Pause") except
  514. * using our validate call to the MAC, we rely upon the MAC
  515. * clearing the bits from both supported and advertising fields.
  516. */
  517. if (phylink_test(supported, Pause))
  518. phylink_set(config.advertising, Pause);
  519. if (phylink_test(supported, Asym_Pause))
  520. phylink_set(config.advertising, Asym_Pause);
  521. ret = phylink_validate(pl, supported, &config);
  522. if (ret)
  523. return ret;
  524. phy->phylink = pl;
  525. phy->phy_link_change = phylink_phy_change;
  526. netdev_info(pl->netdev,
  527. "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
  528. phy->drv->name);
  529. mutex_lock(&phy->lock);
  530. mutex_lock(&pl->state_mutex);
  531. pl->netdev->phydev = phy;
  532. pl->phydev = phy;
  533. linkmode_copy(pl->supported, supported);
  534. linkmode_copy(pl->link_config.advertising, config.advertising);
  535. /* Restrict the phy advertisment according to the MAC support. */
  536. ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising);
  537. phy->advertising = advertising;
  538. mutex_unlock(&pl->state_mutex);
  539. mutex_unlock(&phy->lock);
  540. netdev_dbg(pl->netdev,
  541. "phy: setting supported %*pb advertising 0x%08x\n",
  542. __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
  543. phy->advertising);
  544. phy_start_machine(phy);
  545. if (phy->irq > 0)
  546. phy_start_interrupts(phy);
  547. return 0;
  548. }
  549. int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
  550. {
  551. int ret;
  552. ret = phy_attach_direct(pl->netdev, phy, 0, pl->link_interface);
  553. if (ret)
  554. return ret;
  555. ret = phylink_bringup_phy(pl, phy);
  556. if (ret)
  557. phy_detach(phy);
  558. return ret;
  559. }
  560. EXPORT_SYMBOL_GPL(phylink_connect_phy);
  561. int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn)
  562. {
  563. struct device_node *phy_node;
  564. struct phy_device *phy_dev;
  565. int ret;
  566. /* Fixed links are handled without needing a PHY */
  567. if (pl->link_an_mode == MLO_AN_FIXED)
  568. return 0;
  569. phy_node = of_parse_phandle(dn, "phy-handle", 0);
  570. if (!phy_node)
  571. phy_node = of_parse_phandle(dn, "phy", 0);
  572. if (!phy_node)
  573. phy_node = of_parse_phandle(dn, "phy-device", 0);
  574. if (!phy_node) {
  575. if (pl->link_an_mode == MLO_AN_PHY) {
  576. netdev_err(pl->netdev, "unable to find PHY node\n");
  577. return -ENODEV;
  578. }
  579. return 0;
  580. }
  581. phy_dev = of_phy_attach(pl->netdev, phy_node, 0, pl->link_interface);
  582. /* We're done with the phy_node handle */
  583. of_node_put(phy_node);
  584. if (!phy_dev)
  585. return -ENODEV;
  586. ret = phylink_bringup_phy(pl, phy_dev);
  587. if (ret)
  588. phy_detach(phy_dev);
  589. return ret;
  590. }
  591. EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
  592. void phylink_disconnect_phy(struct phylink *pl)
  593. {
  594. struct phy_device *phy;
  595. WARN_ON(!lockdep_rtnl_is_held());
  596. phy = pl->phydev;
  597. if (phy) {
  598. mutex_lock(&phy->lock);
  599. mutex_lock(&pl->state_mutex);
  600. pl->netdev->phydev = NULL;
  601. pl->phydev = NULL;
  602. mutex_unlock(&pl->state_mutex);
  603. mutex_unlock(&phy->lock);
  604. flush_work(&pl->resolve);
  605. phy_disconnect(phy);
  606. }
  607. }
  608. EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
  609. void phylink_mac_change(struct phylink *pl, bool up)
  610. {
  611. if (!up)
  612. pl->mac_link_dropped = true;
  613. phylink_run_resolve(pl);
  614. netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
  615. }
  616. EXPORT_SYMBOL_GPL(phylink_mac_change);
  617. void phylink_start(struct phylink *pl)
  618. {
  619. WARN_ON(!lockdep_rtnl_is_held());
  620. netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
  621. phylink_an_mode_str(pl->link_an_mode),
  622. phy_modes(pl->link_config.interface));
  623. /* Apply the link configuration to the MAC when starting. This allows
  624. * a fixed-link to start with the correct parameters, and also
  625. * ensures that we set the appropriate advertisment for Serdes links.
  626. */
  627. phylink_resolve_flow(pl, &pl->link_config);
  628. phylink_mac_config(pl, &pl->link_config);
  629. clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
  630. phylink_run_resolve(pl);
  631. if (pl->sfp_bus)
  632. sfp_upstream_start(pl->sfp_bus);
  633. if (pl->phydev)
  634. phy_start(pl->phydev);
  635. }
  636. EXPORT_SYMBOL_GPL(phylink_start);
  637. void phylink_stop(struct phylink *pl)
  638. {
  639. WARN_ON(!lockdep_rtnl_is_held());
  640. if (pl->phydev)
  641. phy_stop(pl->phydev);
  642. if (pl->sfp_bus)
  643. sfp_upstream_stop(pl->sfp_bus);
  644. set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
  645. flush_work(&pl->resolve);
  646. }
  647. EXPORT_SYMBOL_GPL(phylink_stop);
  648. void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
  649. {
  650. WARN_ON(!lockdep_rtnl_is_held());
  651. wol->supported = 0;
  652. wol->wolopts = 0;
  653. if (pl->phydev)
  654. phy_ethtool_get_wol(pl->phydev, wol);
  655. }
  656. EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
  657. int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
  658. {
  659. int ret = -EOPNOTSUPP;
  660. WARN_ON(!lockdep_rtnl_is_held());
  661. if (pl->phydev)
  662. ret = phy_ethtool_set_wol(pl->phydev, wol);
  663. return ret;
  664. }
  665. EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
  666. static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
  667. {
  668. __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
  669. linkmode_zero(mask);
  670. phylink_set_port_modes(mask);
  671. linkmode_and(dst, dst, mask);
  672. linkmode_or(dst, dst, b);
  673. }
  674. static void phylink_get_ksettings(const struct phylink_link_state *state,
  675. struct ethtool_link_ksettings *kset)
  676. {
  677. phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
  678. linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
  679. kset->base.speed = state->speed;
  680. kset->base.duplex = state->duplex;
  681. kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
  682. AUTONEG_DISABLE;
  683. }
  684. int phylink_ethtool_ksettings_get(struct phylink *pl,
  685. struct ethtool_link_ksettings *kset)
  686. {
  687. struct phylink_link_state link_state;
  688. WARN_ON(!lockdep_rtnl_is_held());
  689. if (pl->phydev) {
  690. phy_ethtool_ksettings_get(pl->phydev, kset);
  691. } else {
  692. kset->base.port = pl->link_port;
  693. }
  694. linkmode_copy(kset->link_modes.supported, pl->supported);
  695. switch (pl->link_an_mode) {
  696. case MLO_AN_FIXED:
  697. /* We are using fixed settings. Report these as the
  698. * current link settings - and note that these also
  699. * represent the supported speeds/duplex/pause modes.
  700. */
  701. phylink_get_fixed_state(pl, &link_state);
  702. phylink_get_ksettings(&link_state, kset);
  703. break;
  704. case MLO_AN_SGMII:
  705. /* If there is a phy attached, then use the reported
  706. * settings from the phy with no modification.
  707. */
  708. if (pl->phydev)
  709. break;
  710. case MLO_AN_8023Z:
  711. phylink_get_mac_state(pl, &link_state);
  712. /* The MAC is reporting the link results from its own PCS
  713. * layer via in-band status. Report these as the current
  714. * link settings.
  715. */
  716. phylink_get_ksettings(&link_state, kset);
  717. break;
  718. }
  719. return 0;
  720. }
  721. EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
  722. int phylink_ethtool_ksettings_set(struct phylink *pl,
  723. const struct ethtool_link_ksettings *kset)
  724. {
  725. struct ethtool_link_ksettings our_kset;
  726. struct phylink_link_state config;
  727. int ret;
  728. WARN_ON(!lockdep_rtnl_is_held());
  729. if (kset->base.autoneg != AUTONEG_DISABLE &&
  730. kset->base.autoneg != AUTONEG_ENABLE)
  731. return -EINVAL;
  732. config = pl->link_config;
  733. /* Mask out unsupported advertisments */
  734. linkmode_and(config.advertising, kset->link_modes.advertising,
  735. pl->supported);
  736. /* FIXME: should we reject autoneg if phy/mac does not support it? */
  737. if (kset->base.autoneg == AUTONEG_DISABLE) {
  738. const struct phy_setting *s;
  739. /* Autonegotiation disabled, select a suitable speed and
  740. * duplex.
  741. */
  742. s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
  743. pl->supported,
  744. __ETHTOOL_LINK_MODE_MASK_NBITS, false);
  745. if (!s)
  746. return -EINVAL;
  747. /* If we have a fixed link (as specified by firmware), refuse
  748. * to change link parameters.
  749. */
  750. if (pl->link_an_mode == MLO_AN_FIXED &&
  751. (s->speed != pl->link_config.speed ||
  752. s->duplex != pl->link_config.duplex))
  753. return -EINVAL;
  754. config.speed = s->speed;
  755. config.duplex = s->duplex;
  756. config.an_enabled = false;
  757. __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
  758. } else {
  759. /* If we have a fixed link, refuse to enable autonegotiation */
  760. if (pl->link_an_mode == MLO_AN_FIXED)
  761. return -EINVAL;
  762. config.speed = SPEED_UNKNOWN;
  763. config.duplex = DUPLEX_UNKNOWN;
  764. config.an_enabled = true;
  765. __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
  766. }
  767. if (phylink_validate(pl, pl->supported, &config))
  768. return -EINVAL;
  769. /* If autonegotiation is enabled, we must have an advertisment */
  770. if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
  771. return -EINVAL;
  772. our_kset = *kset;
  773. linkmode_copy(our_kset.link_modes.advertising, config.advertising);
  774. our_kset.base.speed = config.speed;
  775. our_kset.base.duplex = config.duplex;
  776. /* If we have a PHY, configure the phy */
  777. if (pl->phydev) {
  778. ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
  779. if (ret)
  780. return ret;
  781. }
  782. mutex_lock(&pl->state_mutex);
  783. /* Configure the MAC to match the new settings */
  784. linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
  785. pl->link_config.speed = our_kset.base.speed;
  786. pl->link_config.duplex = our_kset.base.duplex;
  787. pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
  788. if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
  789. phylink_mac_config(pl, &pl->link_config);
  790. phylink_mac_an_restart(pl);
  791. }
  792. mutex_unlock(&pl->state_mutex);
  793. return 0;
  794. }
  795. EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
  796. int phylink_ethtool_nway_reset(struct phylink *pl)
  797. {
  798. int ret = 0;
  799. WARN_ON(!lockdep_rtnl_is_held());
  800. if (pl->phydev)
  801. ret = phy_restart_aneg(pl->phydev);
  802. phylink_mac_an_restart(pl);
  803. return ret;
  804. }
  805. EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
  806. void phylink_ethtool_get_pauseparam(struct phylink *pl,
  807. struct ethtool_pauseparam *pause)
  808. {
  809. WARN_ON(!lockdep_rtnl_is_held());
  810. pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
  811. pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
  812. pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
  813. }
  814. EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
  815. int phylink_ethtool_set_pauseparam(struct phylink *pl,
  816. struct ethtool_pauseparam *pause)
  817. {
  818. struct phylink_link_state *config = &pl->link_config;
  819. WARN_ON(!lockdep_rtnl_is_held());
  820. if (!phylink_test(pl->supported, Pause) &&
  821. !phylink_test(pl->supported, Asym_Pause))
  822. return -EOPNOTSUPP;
  823. if (!phylink_test(pl->supported, Asym_Pause) &&
  824. !pause->autoneg && pause->rx_pause != pause->tx_pause)
  825. return -EINVAL;
  826. config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
  827. if (pause->autoneg)
  828. config->pause |= MLO_PAUSE_AN;
  829. if (pause->rx_pause)
  830. config->pause |= MLO_PAUSE_RX;
  831. if (pause->tx_pause)
  832. config->pause |= MLO_PAUSE_TX;
  833. if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
  834. switch (pl->link_an_mode) {
  835. case MLO_AN_PHY:
  836. /* Silently mark the carrier down, and then trigger a resolve */
  837. netif_carrier_off(pl->netdev);
  838. phylink_run_resolve(pl);
  839. break;
  840. case MLO_AN_FIXED:
  841. /* Should we allow fixed links to change against the config? */
  842. phylink_resolve_flow(pl, config);
  843. phylink_mac_config(pl, config);
  844. break;
  845. case MLO_AN_SGMII:
  846. case MLO_AN_8023Z:
  847. phylink_mac_config(pl, config);
  848. phylink_mac_an_restart(pl);
  849. break;
  850. }
  851. }
  852. return 0;
  853. }
  854. EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
  855. int phylink_ethtool_get_module_info(struct phylink *pl,
  856. struct ethtool_modinfo *modinfo)
  857. {
  858. int ret = -EOPNOTSUPP;
  859. WARN_ON(!lockdep_rtnl_is_held());
  860. if (pl->sfp_bus)
  861. ret = sfp_get_module_info(pl->sfp_bus, modinfo);
  862. return ret;
  863. }
  864. EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info);
  865. int phylink_ethtool_get_module_eeprom(struct phylink *pl,
  866. struct ethtool_eeprom *ee, u8 *buf)
  867. {
  868. int ret = -EOPNOTSUPP;
  869. WARN_ON(!lockdep_rtnl_is_held());
  870. if (pl->sfp_bus)
  871. ret = sfp_get_module_eeprom(pl->sfp_bus, ee, buf);
  872. return ret;
  873. }
  874. EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom);
  875. int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
  876. {
  877. int ret = -EPROTONOSUPPORT;
  878. WARN_ON(!lockdep_rtnl_is_held());
  879. if (pl->phydev)
  880. ret = phy_init_eee(pl->phydev, clk_stop_enable);
  881. return ret;
  882. }
  883. EXPORT_SYMBOL_GPL(phylink_init_eee);
  884. int phylink_get_eee_err(struct phylink *pl)
  885. {
  886. int ret = 0;
  887. WARN_ON(!lockdep_rtnl_is_held());
  888. if (pl->phydev)
  889. ret = phy_get_eee_err(pl->phydev);
  890. return ret;
  891. }
  892. EXPORT_SYMBOL_GPL(phylink_get_eee_err);
  893. int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
  894. {
  895. int ret = -EOPNOTSUPP;
  896. WARN_ON(!lockdep_rtnl_is_held());
  897. if (pl->phydev)
  898. ret = phy_ethtool_get_eee(pl->phydev, eee);
  899. return ret;
  900. }
  901. EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
  902. int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
  903. {
  904. int ret = -EOPNOTSUPP;
  905. WARN_ON(!lockdep_rtnl_is_held());
  906. if (pl->phydev)
  907. ret = phy_ethtool_set_eee(pl->phydev, eee);
  908. return ret;
  909. }
  910. EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
  911. /* This emulates MII registers for a fixed-mode phy operating as per the
  912. * passed in state. "aneg" defines if we report negotiation is possible.
  913. *
  914. * FIXME: should deal with negotiation state too.
  915. */
  916. static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
  917. struct phylink_link_state *state, bool aneg)
  918. {
  919. struct fixed_phy_status fs;
  920. int val;
  921. fs.link = state->link;
  922. fs.speed = state->speed;
  923. fs.duplex = state->duplex;
  924. fs.pause = state->pause & MLO_PAUSE_SYM;
  925. fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
  926. val = swphy_read_reg(reg, &fs);
  927. if (reg == MII_BMSR) {
  928. if (!state->an_complete)
  929. val &= ~BMSR_ANEGCOMPLETE;
  930. if (!aneg)
  931. val &= ~BMSR_ANEGCAPABLE;
  932. }
  933. return val;
  934. }
  935. static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
  936. unsigned int reg)
  937. {
  938. struct phy_device *phydev = pl->phydev;
  939. int prtad, devad;
  940. if (mdio_phy_id_is_c45(phy_id)) {
  941. prtad = mdio_phy_id_prtad(phy_id);
  942. devad = mdio_phy_id_devad(phy_id);
  943. devad = MII_ADDR_C45 | devad << 16 | reg;
  944. } else if (phydev->is_c45) {
  945. switch (reg) {
  946. case MII_BMCR:
  947. case MII_BMSR:
  948. case MII_PHYSID1:
  949. case MII_PHYSID2:
  950. devad = __ffs(phydev->c45_ids.devices_in_package);
  951. break;
  952. case MII_ADVERTISE:
  953. case MII_LPA:
  954. if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
  955. return -EINVAL;
  956. devad = MDIO_MMD_AN;
  957. if (reg == MII_ADVERTISE)
  958. reg = MDIO_AN_ADVERTISE;
  959. else
  960. reg = MDIO_AN_LPA;
  961. break;
  962. default:
  963. return -EINVAL;
  964. }
  965. prtad = phy_id;
  966. devad = MII_ADDR_C45 | devad << 16 | reg;
  967. } else {
  968. prtad = phy_id;
  969. devad = reg;
  970. }
  971. return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
  972. }
  973. static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
  974. unsigned int reg, unsigned int val)
  975. {
  976. struct phy_device *phydev = pl->phydev;
  977. int prtad, devad;
  978. if (mdio_phy_id_is_c45(phy_id)) {
  979. prtad = mdio_phy_id_prtad(phy_id);
  980. devad = mdio_phy_id_devad(phy_id);
  981. devad = MII_ADDR_C45 | devad << 16 | reg;
  982. } else if (phydev->is_c45) {
  983. switch (reg) {
  984. case MII_BMCR:
  985. case MII_BMSR:
  986. case MII_PHYSID1:
  987. case MII_PHYSID2:
  988. devad = __ffs(phydev->c45_ids.devices_in_package);
  989. break;
  990. case MII_ADVERTISE:
  991. case MII_LPA:
  992. if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
  993. return -EINVAL;
  994. devad = MDIO_MMD_AN;
  995. if (reg == MII_ADVERTISE)
  996. reg = MDIO_AN_ADVERTISE;
  997. else
  998. reg = MDIO_AN_LPA;
  999. break;
  1000. default:
  1001. return -EINVAL;
  1002. }
  1003. prtad = phy_id;
  1004. devad = MII_ADDR_C45 | devad << 16 | reg;
  1005. } else {
  1006. prtad = phy_id;
  1007. devad = reg;
  1008. }
  1009. return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
  1010. }
  1011. static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
  1012. unsigned int reg)
  1013. {
  1014. struct phylink_link_state state;
  1015. int val = 0xffff;
  1016. switch (pl->link_an_mode) {
  1017. case MLO_AN_FIXED:
  1018. if (phy_id == 0) {
  1019. phylink_get_fixed_state(pl, &state);
  1020. val = phylink_mii_emul_read(pl->netdev, reg, &state,
  1021. true);
  1022. }
  1023. break;
  1024. case MLO_AN_PHY:
  1025. return -EOPNOTSUPP;
  1026. case MLO_AN_SGMII:
  1027. /* No phy, fall through to 8023z method */
  1028. case MLO_AN_8023Z:
  1029. if (phy_id == 0) {
  1030. val = phylink_get_mac_state(pl, &state);
  1031. if (val < 0)
  1032. return val;
  1033. val = phylink_mii_emul_read(pl->netdev, reg, &state,
  1034. true);
  1035. }
  1036. break;
  1037. }
  1038. return val & 0xffff;
  1039. }
  1040. static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
  1041. unsigned int reg, unsigned int val)
  1042. {
  1043. switch (pl->link_an_mode) {
  1044. case MLO_AN_FIXED:
  1045. break;
  1046. case MLO_AN_PHY:
  1047. return -EOPNOTSUPP;
  1048. case MLO_AN_SGMII:
  1049. /* No phy, fall through to 8023z method */
  1050. case MLO_AN_8023Z:
  1051. break;
  1052. }
  1053. return 0;
  1054. }
  1055. int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
  1056. {
  1057. struct mii_ioctl_data *mii = if_mii(ifr);
  1058. int ret;
  1059. WARN_ON(!lockdep_rtnl_is_held());
  1060. if (pl->phydev) {
  1061. /* PHYs only exist for MLO_AN_PHY and MLO_AN_SGMII */
  1062. switch (cmd) {
  1063. case SIOCGMIIPHY:
  1064. mii->phy_id = pl->phydev->mdio.addr;
  1065. case SIOCGMIIREG:
  1066. ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
  1067. if (ret >= 0) {
  1068. mii->val_out = ret;
  1069. ret = 0;
  1070. }
  1071. break;
  1072. case SIOCSMIIREG:
  1073. ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
  1074. mii->val_in);
  1075. break;
  1076. default:
  1077. ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
  1078. break;
  1079. }
  1080. } else {
  1081. switch (cmd) {
  1082. case SIOCGMIIPHY:
  1083. mii->phy_id = 0;
  1084. case SIOCGMIIREG:
  1085. ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
  1086. if (ret >= 0) {
  1087. mii->val_out = ret;
  1088. ret = 0;
  1089. }
  1090. break;
  1091. case SIOCSMIIREG:
  1092. ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
  1093. mii->val_in);
  1094. break;
  1095. default:
  1096. ret = -EOPNOTSUPP;
  1097. break;
  1098. }
  1099. }
  1100. return ret;
  1101. }
  1102. EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
  1103. static int phylink_sfp_module_insert(void *upstream,
  1104. const struct sfp_eeprom_id *id)
  1105. {
  1106. struct phylink *pl = upstream;
  1107. __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
  1108. struct phylink_link_state config;
  1109. phy_interface_t iface;
  1110. int mode, ret = 0;
  1111. bool changed;
  1112. u8 port;
  1113. sfp_parse_support(pl->sfp_bus, id, support);
  1114. port = sfp_parse_port(pl->sfp_bus, id, support);
  1115. iface = sfp_parse_interface(pl->sfp_bus, id);
  1116. WARN_ON(!lockdep_rtnl_is_held());
  1117. switch (iface) {
  1118. case PHY_INTERFACE_MODE_SGMII:
  1119. mode = MLO_AN_SGMII;
  1120. break;
  1121. case PHY_INTERFACE_MODE_1000BASEX:
  1122. mode = MLO_AN_8023Z;
  1123. break;
  1124. default:
  1125. return -EINVAL;
  1126. }
  1127. memset(&config, 0, sizeof(config));
  1128. linkmode_copy(config.advertising, support);
  1129. config.interface = iface;
  1130. config.speed = SPEED_UNKNOWN;
  1131. config.duplex = DUPLEX_UNKNOWN;
  1132. config.pause = MLO_PAUSE_AN;
  1133. config.an_enabled = pl->link_config.an_enabled;
  1134. /* Ignore errors if we're expecting a PHY to attach later */
  1135. ret = phylink_validate(pl, support, &config);
  1136. if (ret) {
  1137. netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
  1138. phylink_an_mode_str(mode), phy_modes(config.interface),
  1139. __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
  1140. return ret;
  1141. }
  1142. netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
  1143. phylink_an_mode_str(mode), phy_modes(config.interface),
  1144. __ETHTOOL_LINK_MODE_MASK_NBITS, support);
  1145. if (mode == MLO_AN_8023Z && pl->phydev)
  1146. return -EINVAL;
  1147. changed = !bitmap_equal(pl->supported, support,
  1148. __ETHTOOL_LINK_MODE_MASK_NBITS);
  1149. if (changed) {
  1150. linkmode_copy(pl->supported, support);
  1151. linkmode_copy(pl->link_config.advertising, config.advertising);
  1152. }
  1153. if (pl->link_an_mode != mode ||
  1154. pl->link_config.interface != config.interface) {
  1155. pl->link_config.interface = config.interface;
  1156. pl->link_an_mode = mode;
  1157. changed = true;
  1158. netdev_info(pl->netdev, "switched to %s/%s link mode\n",
  1159. phylink_an_mode_str(mode),
  1160. phy_modes(config.interface));
  1161. }
  1162. pl->link_port = port;
  1163. if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
  1164. &pl->phylink_disable_state))
  1165. phylink_mac_config(pl, &pl->link_config);
  1166. return ret;
  1167. }
  1168. static void phylink_sfp_link_down(void *upstream)
  1169. {
  1170. struct phylink *pl = upstream;
  1171. WARN_ON(!lockdep_rtnl_is_held());
  1172. set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
  1173. flush_work(&pl->resolve);
  1174. netif_carrier_off(pl->netdev);
  1175. }
  1176. static void phylink_sfp_link_up(void *upstream)
  1177. {
  1178. struct phylink *pl = upstream;
  1179. WARN_ON(!lockdep_rtnl_is_held());
  1180. clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
  1181. phylink_run_resolve(pl);
  1182. }
  1183. static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
  1184. {
  1185. return phylink_connect_phy(upstream, phy);
  1186. }
  1187. static void phylink_sfp_disconnect_phy(void *upstream)
  1188. {
  1189. phylink_disconnect_phy(upstream);
  1190. }
  1191. static const struct sfp_upstream_ops sfp_phylink_ops = {
  1192. .module_insert = phylink_sfp_module_insert,
  1193. .link_up = phylink_sfp_link_up,
  1194. .link_down = phylink_sfp_link_down,
  1195. .connect_phy = phylink_sfp_connect_phy,
  1196. .disconnect_phy = phylink_sfp_disconnect_phy,
  1197. };
  1198. MODULE_LICENSE("GPL");