phylink.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  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/timer.h>
  23. #include <linux/workqueue.h>
  24. #include "sfp.h"
  25. #include "swphy.h"
  26. #define SUPPORTED_INTERFACES \
  27. (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
  28. SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
  29. #define ADVERTISED_INTERFACES \
  30. (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
  31. ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
  32. enum {
  33. PHYLINK_DISABLE_STOPPED,
  34. PHYLINK_DISABLE_LINK,
  35. };
  36. /**
  37. * struct phylink - internal data type for phylink
  38. */
  39. struct phylink {
  40. /* private: */
  41. struct net_device *netdev;
  42. const struct phylink_mac_ops *ops;
  43. unsigned long phylink_disable_state; /* bitmask of disables */
  44. struct phy_device *phydev;
  45. phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
  46. u8 link_an_mode; /* MLO_AN_xxx */
  47. u8 link_port; /* The current non-phy ethtool port */
  48. __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
  49. /* The link configuration settings */
  50. struct phylink_link_state link_config;
  51. /* The current settings */
  52. phy_interface_t cur_interface;
  53. struct gpio_desc *link_gpio;
  54. struct timer_list link_poll;
  55. void (*get_fixed_state)(struct net_device *dev,
  56. struct phylink_link_state *s);
  57. struct mutex state_mutex;
  58. struct phylink_link_state phy_state;
  59. struct work_struct resolve;
  60. bool mac_link_dropped;
  61. struct sfp_bus *sfp_bus;
  62. };
  63. static inline void linkmode_zero(unsigned long *dst)
  64. {
  65. bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
  66. }
  67. static inline void linkmode_copy(unsigned long *dst, const unsigned long *src)
  68. {
  69. bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS);
  70. }
  71. static inline void linkmode_and(unsigned long *dst, const unsigned long *a,
  72. const unsigned long *b)
  73. {
  74. bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
  75. }
  76. static inline void linkmode_or(unsigned long *dst, const unsigned long *a,
  77. const unsigned long *b)
  78. {
  79. bitmap_or(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
  80. }
  81. static inline bool linkmode_empty(const unsigned long *src)
  82. {
  83. return bitmap_empty(src, __ETHTOOL_LINK_MODE_MASK_NBITS);
  84. }
  85. /**
  86. * phylink_set_port_modes() - set the port type modes in the ethtool mask
  87. * @mask: ethtool link mode mask
  88. *
  89. * Sets all the port type modes in the ethtool mask. MAC drivers should
  90. * use this in their 'validate' callback.
  91. */
  92. void phylink_set_port_modes(unsigned long *mask)
  93. {
  94. phylink_set(mask, TP);
  95. phylink_set(mask, AUI);
  96. phylink_set(mask, MII);
  97. phylink_set(mask, FIBRE);
  98. phylink_set(mask, BNC);
  99. phylink_set(mask, Backplane);
  100. }
  101. EXPORT_SYMBOL_GPL(phylink_set_port_modes);
  102. static int phylink_is_empty_linkmode(const unsigned long *linkmode)
  103. {
  104. __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
  105. phylink_set_port_modes(tmp);
  106. phylink_set(tmp, Autoneg);
  107. phylink_set(tmp, Pause);
  108. phylink_set(tmp, Asym_Pause);
  109. bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
  110. return linkmode_empty(tmp);
  111. }
  112. static const char *phylink_an_mode_str(unsigned int mode)
  113. {
  114. static const char *modestr[] = {
  115. [MLO_AN_PHY] = "phy",
  116. [MLO_AN_FIXED] = "fixed",
  117. [MLO_AN_INBAND] = "inband",
  118. };
  119. return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
  120. }
  121. static int phylink_validate(struct phylink *pl, unsigned long *supported,
  122. struct phylink_link_state *state)
  123. {
  124. pl->ops->validate(pl->netdev, supported, state);
  125. return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
  126. }
  127. static int phylink_parse_fixedlink(struct phylink *pl,
  128. struct fwnode_handle *fwnode)
  129. {
  130. struct fwnode_handle *fixed_node;
  131. const struct phy_setting *s;
  132. struct gpio_desc *desc;
  133. u32 speed;
  134. int ret;
  135. fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
  136. if (fixed_node) {
  137. ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
  138. pl->link_config.speed = speed;
  139. pl->link_config.duplex = DUPLEX_HALF;
  140. if (fwnode_property_read_bool(fixed_node, "full-duplex"))
  141. pl->link_config.duplex = DUPLEX_FULL;
  142. /* We treat the "pause" and "asym-pause" terminology as
  143. * defining the link partner's ability. */
  144. if (fwnode_property_read_bool(fixed_node, "pause"))
  145. pl->link_config.pause |= MLO_PAUSE_SYM;
  146. if (fwnode_property_read_bool(fixed_node, "asym-pause"))
  147. pl->link_config.pause |= MLO_PAUSE_ASYM;
  148. if (ret == 0) {
  149. desc = fwnode_get_named_gpiod(fixed_node, "link-gpios",
  150. 0, GPIOD_IN, "?");
  151. if (!IS_ERR(desc))
  152. pl->link_gpio = desc;
  153. else if (desc == ERR_PTR(-EPROBE_DEFER))
  154. ret = -EPROBE_DEFER;
  155. }
  156. fwnode_handle_put(fixed_node);
  157. if (ret)
  158. return ret;
  159. } else {
  160. u32 prop[5];
  161. ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
  162. NULL, 0);
  163. if (ret != ARRAY_SIZE(prop)) {
  164. netdev_err(pl->netdev, "broken fixed-link?\n");
  165. return -EINVAL;
  166. }
  167. ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
  168. prop, ARRAY_SIZE(prop));
  169. if (!ret) {
  170. pl->link_config.duplex = prop[1] ?
  171. DUPLEX_FULL : DUPLEX_HALF;
  172. pl->link_config.speed = prop[2];
  173. if (prop[3])
  174. pl->link_config.pause |= MLO_PAUSE_SYM;
  175. if (prop[4])
  176. pl->link_config.pause |= MLO_PAUSE_ASYM;
  177. }
  178. }
  179. if (pl->link_config.speed > SPEED_1000 &&
  180. pl->link_config.duplex != DUPLEX_FULL)
  181. netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
  182. pl->link_config.speed);
  183. bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
  184. linkmode_copy(pl->link_config.advertising, pl->supported);
  185. phylink_validate(pl, pl->supported, &pl->link_config);
  186. s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
  187. pl->supported,
  188. __ETHTOOL_LINK_MODE_MASK_NBITS, true);
  189. linkmode_zero(pl->supported);
  190. phylink_set(pl->supported, MII);
  191. phylink_set(pl->supported, Pause);
  192. phylink_set(pl->supported, Asym_Pause);
  193. if (s) {
  194. __set_bit(s->bit, pl->supported);
  195. } else {
  196. netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n",
  197. pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
  198. pl->link_config.speed);
  199. }
  200. linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
  201. pl->supported);
  202. pl->link_config.link = 1;
  203. pl->link_config.an_complete = 1;
  204. return 0;
  205. }
  206. static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
  207. {
  208. struct fwnode_handle *dn;
  209. const char *managed;
  210. dn = fwnode_get_named_child_node(fwnode, "fixed-link");
  211. if (dn || fwnode_property_present(fwnode, "fixed-link"))
  212. pl->link_an_mode = MLO_AN_FIXED;
  213. fwnode_handle_put(dn);
  214. if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
  215. strcmp(managed, "in-band-status") == 0) {
  216. if (pl->link_an_mode == MLO_AN_FIXED) {
  217. netdev_err(pl->netdev,
  218. "can't use both fixed-link and in-band-status\n");
  219. return -EINVAL;
  220. }
  221. linkmode_zero(pl->supported);
  222. phylink_set(pl->supported, MII);
  223. phylink_set(pl->supported, Autoneg);
  224. phylink_set(pl->supported, Asym_Pause);
  225. phylink_set(pl->supported, Pause);
  226. pl->link_config.an_enabled = true;
  227. pl->link_an_mode = MLO_AN_INBAND;
  228. switch (pl->link_config.interface) {
  229. case PHY_INTERFACE_MODE_SGMII:
  230. phylink_set(pl->supported, 10baseT_Half);
  231. phylink_set(pl->supported, 10baseT_Full);
  232. phylink_set(pl->supported, 100baseT_Half);
  233. phylink_set(pl->supported, 100baseT_Full);
  234. phylink_set(pl->supported, 1000baseT_Half);
  235. phylink_set(pl->supported, 1000baseT_Full);
  236. break;
  237. case PHY_INTERFACE_MODE_1000BASEX:
  238. phylink_set(pl->supported, 1000baseX_Full);
  239. break;
  240. case PHY_INTERFACE_MODE_2500BASEX:
  241. phylink_set(pl->supported, 2500baseX_Full);
  242. break;
  243. case PHY_INTERFACE_MODE_10GKR:
  244. phylink_set(pl->supported, 10baseT_Half);
  245. phylink_set(pl->supported, 10baseT_Full);
  246. phylink_set(pl->supported, 100baseT_Half);
  247. phylink_set(pl->supported, 100baseT_Full);
  248. phylink_set(pl->supported, 1000baseT_Half);
  249. phylink_set(pl->supported, 1000baseT_Full);
  250. phylink_set(pl->supported, 1000baseX_Full);
  251. phylink_set(pl->supported, 10000baseKR_Full);
  252. phylink_set(pl->supported, 10000baseCR_Full);
  253. phylink_set(pl->supported, 10000baseSR_Full);
  254. phylink_set(pl->supported, 10000baseLR_Full);
  255. phylink_set(pl->supported, 10000baseLRM_Full);
  256. phylink_set(pl->supported, 10000baseER_Full);
  257. break;
  258. default:
  259. netdev_err(pl->netdev,
  260. "incorrect link mode %s for in-band status\n",
  261. phy_modes(pl->link_config.interface));
  262. return -EINVAL;
  263. }
  264. linkmode_copy(pl->link_config.advertising, pl->supported);
  265. if (phylink_validate(pl, pl->supported, &pl->link_config)) {
  266. netdev_err(pl->netdev,
  267. "failed to validate link configuration for in-band status\n");
  268. return -EINVAL;
  269. }
  270. }
  271. return 0;
  272. }
  273. static void phylink_mac_config(struct phylink *pl,
  274. const struct phylink_link_state *state)
  275. {
  276. netdev_dbg(pl->netdev,
  277. "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
  278. __func__, phylink_an_mode_str(pl->link_an_mode),
  279. phy_modes(state->interface),
  280. phy_speed_to_str(state->speed),
  281. phy_duplex_to_str(state->duplex),
  282. __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
  283. state->pause, state->link, state->an_enabled);
  284. pl->ops->mac_config(pl->netdev, pl->link_an_mode, state);
  285. }
  286. static void phylink_mac_an_restart(struct phylink *pl)
  287. {
  288. if (pl->link_config.an_enabled &&
  289. phy_interface_mode_is_8023z(pl->link_config.interface))
  290. pl->ops->mac_an_restart(pl->netdev);
  291. }
  292. static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
  293. {
  294. struct net_device *ndev = pl->netdev;
  295. linkmode_copy(state->advertising, pl->link_config.advertising);
  296. linkmode_zero(state->lp_advertising);
  297. state->interface = pl->link_config.interface;
  298. state->an_enabled = pl->link_config.an_enabled;
  299. state->speed = SPEED_UNKNOWN;
  300. state->duplex = DUPLEX_UNKNOWN;
  301. state->pause = MLO_PAUSE_NONE;
  302. state->an_complete = 0;
  303. state->link = 1;
  304. return pl->ops->mac_link_state(ndev, state);
  305. }
  306. /* The fixed state is... fixed except for the link state,
  307. * which may be determined by a GPIO or a callback.
  308. */
  309. static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
  310. {
  311. *state = pl->link_config;
  312. if (pl->get_fixed_state)
  313. pl->get_fixed_state(pl->netdev, state);
  314. else if (pl->link_gpio)
  315. state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
  316. }
  317. /* Flow control is resolved according to our and the link partners
  318. * advertisements using the following drawn from the 802.3 specs:
  319. * Local device Link partner
  320. * Pause AsymDir Pause AsymDir Result
  321. * 1 X 1 X TX+RX
  322. * 0 1 1 1 TX
  323. * 1 1 0 1 RX
  324. */
  325. static void phylink_resolve_flow(struct phylink *pl,
  326. struct phylink_link_state *state)
  327. {
  328. int new_pause = 0;
  329. if (pl->link_config.pause & MLO_PAUSE_AN) {
  330. int pause = 0;
  331. if (phylink_test(pl->link_config.advertising, Pause))
  332. pause |= MLO_PAUSE_SYM;
  333. if (phylink_test(pl->link_config.advertising, Asym_Pause))
  334. pause |= MLO_PAUSE_ASYM;
  335. pause &= state->pause;
  336. if (pause & MLO_PAUSE_SYM)
  337. new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
  338. else if (pause & MLO_PAUSE_ASYM)
  339. new_pause = state->pause & MLO_PAUSE_SYM ?
  340. MLO_PAUSE_TX : MLO_PAUSE_RX;
  341. } else {
  342. new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
  343. }
  344. state->pause &= ~MLO_PAUSE_TXRX_MASK;
  345. state->pause |= new_pause;
  346. }
  347. static const char *phylink_pause_to_str(int pause)
  348. {
  349. switch (pause & MLO_PAUSE_TXRX_MASK) {
  350. case MLO_PAUSE_TX | MLO_PAUSE_RX:
  351. return "rx/tx";
  352. case MLO_PAUSE_TX:
  353. return "tx";
  354. case MLO_PAUSE_RX:
  355. return "rx";
  356. default:
  357. return "off";
  358. }
  359. }
  360. static void phylink_resolve(struct work_struct *w)
  361. {
  362. struct phylink *pl = container_of(w, struct phylink, resolve);
  363. struct phylink_link_state link_state;
  364. struct net_device *ndev = pl->netdev;
  365. mutex_lock(&pl->state_mutex);
  366. if (pl->phylink_disable_state) {
  367. pl->mac_link_dropped = false;
  368. link_state.link = false;
  369. } else if (pl->mac_link_dropped) {
  370. link_state.link = false;
  371. } else {
  372. switch (pl->link_an_mode) {
  373. case MLO_AN_PHY:
  374. link_state = pl->phy_state;
  375. phylink_resolve_flow(pl, &link_state);
  376. phylink_mac_config(pl, &link_state);
  377. break;
  378. case MLO_AN_FIXED:
  379. phylink_get_fixed_state(pl, &link_state);
  380. phylink_mac_config(pl, &link_state);
  381. break;
  382. case MLO_AN_INBAND:
  383. phylink_get_mac_state(pl, &link_state);
  384. if (pl->phydev) {
  385. bool changed = false;
  386. link_state.link = link_state.link &&
  387. pl->phy_state.link;
  388. if (pl->phy_state.interface !=
  389. link_state.interface) {
  390. link_state.interface = pl->phy_state.interface;
  391. changed = true;
  392. }
  393. /* Propagate the flow control from the PHY
  394. * to the MAC. Also propagate the interface
  395. * if changed.
  396. */
  397. if (pl->phy_state.link || changed) {
  398. link_state.pause |= pl->phy_state.pause;
  399. phylink_resolve_flow(pl, &link_state);
  400. phylink_mac_config(pl, &link_state);
  401. }
  402. }
  403. break;
  404. }
  405. }
  406. if (link_state.link != netif_carrier_ok(ndev)) {
  407. if (!link_state.link) {
  408. netif_carrier_off(ndev);
  409. pl->ops->mac_link_down(ndev, pl->link_an_mode,
  410. pl->cur_interface);
  411. netdev_info(ndev, "Link is Down\n");
  412. } else {
  413. pl->cur_interface = link_state.interface;
  414. pl->ops->mac_link_up(ndev, pl->link_an_mode,
  415. pl->cur_interface, pl->phydev);
  416. netif_carrier_on(ndev);
  417. netdev_info(ndev,
  418. "Link is Up - %s/%s - flow control %s\n",
  419. phy_speed_to_str(link_state.speed),
  420. phy_duplex_to_str(link_state.duplex),
  421. phylink_pause_to_str(link_state.pause));
  422. }
  423. }
  424. if (!link_state.link && pl->mac_link_dropped) {
  425. pl->mac_link_dropped = false;
  426. queue_work(system_power_efficient_wq, &pl->resolve);
  427. }
  428. mutex_unlock(&pl->state_mutex);
  429. }
  430. static void phylink_run_resolve(struct phylink *pl)
  431. {
  432. if (!pl->phylink_disable_state)
  433. queue_work(system_power_efficient_wq, &pl->resolve);
  434. }
  435. static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
  436. {
  437. unsigned long state = pl->phylink_disable_state;
  438. set_bit(bit, &pl->phylink_disable_state);
  439. if (state == 0) {
  440. queue_work(system_power_efficient_wq, &pl->resolve);
  441. flush_work(&pl->resolve);
  442. }
  443. }
  444. static void phylink_fixed_poll(struct timer_list *t)
  445. {
  446. struct phylink *pl = container_of(t, struct phylink, link_poll);
  447. mod_timer(t, jiffies + HZ);
  448. phylink_run_resolve(pl);
  449. }
  450. static const struct sfp_upstream_ops sfp_phylink_ops;
  451. static int phylink_register_sfp(struct phylink *pl,
  452. struct fwnode_handle *fwnode)
  453. {
  454. struct fwnode_reference_args ref;
  455. int ret;
  456. if (!fwnode)
  457. return 0;
  458. ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
  459. 0, 0, &ref);
  460. if (ret < 0) {
  461. if (ret == -ENOENT)
  462. return 0;
  463. netdev_err(pl->netdev, "unable to parse \"sfp\" node: %d\n",
  464. ret);
  465. return ret;
  466. }
  467. pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl->netdev, pl,
  468. &sfp_phylink_ops);
  469. if (!pl->sfp_bus)
  470. return -ENOMEM;
  471. return 0;
  472. }
  473. /**
  474. * phylink_create() - create a phylink instance
  475. * @ndev: a pointer to the &struct net_device
  476. * @fwnode: a pointer to a &struct fwnode_handle describing the network
  477. * interface
  478. * @iface: the desired link mode defined by &typedef phy_interface_t
  479. * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
  480. *
  481. * Create a new phylink instance, and parse the link parameters found in @np.
  482. * This will parse in-band modes, fixed-link or SFP configuration.
  483. *
  484. * Returns a pointer to a &struct phylink, or an error-pointer value. Users
  485. * must use IS_ERR() to check for errors from this function.
  486. */
  487. struct phylink *phylink_create(struct net_device *ndev,
  488. struct fwnode_handle *fwnode,
  489. phy_interface_t iface,
  490. const struct phylink_mac_ops *ops)
  491. {
  492. struct phylink *pl;
  493. int ret;
  494. pl = kzalloc(sizeof(*pl), GFP_KERNEL);
  495. if (!pl)
  496. return ERR_PTR(-ENOMEM);
  497. mutex_init(&pl->state_mutex);
  498. INIT_WORK(&pl->resolve, phylink_resolve);
  499. pl->netdev = ndev;
  500. pl->phy_state.interface = iface;
  501. pl->link_interface = iface;
  502. if (iface == PHY_INTERFACE_MODE_MOCA)
  503. pl->link_port = PORT_BNC;
  504. else
  505. pl->link_port = PORT_MII;
  506. pl->link_config.interface = iface;
  507. pl->link_config.pause = MLO_PAUSE_AN;
  508. pl->link_config.speed = SPEED_UNKNOWN;
  509. pl->link_config.duplex = DUPLEX_UNKNOWN;
  510. pl->link_config.an_enabled = true;
  511. pl->ops = ops;
  512. __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
  513. timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
  514. bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
  515. linkmode_copy(pl->link_config.advertising, pl->supported);
  516. phylink_validate(pl, pl->supported, &pl->link_config);
  517. ret = phylink_parse_mode(pl, fwnode);
  518. if (ret < 0) {
  519. kfree(pl);
  520. return ERR_PTR(ret);
  521. }
  522. if (pl->link_an_mode == MLO_AN_FIXED) {
  523. ret = phylink_parse_fixedlink(pl, fwnode);
  524. if (ret < 0) {
  525. kfree(pl);
  526. return ERR_PTR(ret);
  527. }
  528. }
  529. ret = phylink_register_sfp(pl, fwnode);
  530. if (ret < 0) {
  531. kfree(pl);
  532. return ERR_PTR(ret);
  533. }
  534. return pl;
  535. }
  536. EXPORT_SYMBOL_GPL(phylink_create);
  537. /**
  538. * phylink_destroy() - cleanup and destroy the phylink instance
  539. * @pl: a pointer to a &struct phylink returned from phylink_create()
  540. *
  541. * Destroy a phylink instance. Any PHY that has been attached must have been
  542. * cleaned up via phylink_disconnect_phy() prior to calling this function.
  543. */
  544. void phylink_destroy(struct phylink *pl)
  545. {
  546. if (pl->sfp_bus)
  547. sfp_unregister_upstream(pl->sfp_bus);
  548. if (!IS_ERR_OR_NULL(pl->link_gpio))
  549. gpiod_put(pl->link_gpio);
  550. cancel_work_sync(&pl->resolve);
  551. kfree(pl);
  552. }
  553. EXPORT_SYMBOL_GPL(phylink_destroy);
  554. static void phylink_phy_change(struct phy_device *phydev, bool up,
  555. bool do_carrier)
  556. {
  557. struct phylink *pl = phydev->phylink;
  558. mutex_lock(&pl->state_mutex);
  559. pl->phy_state.speed = phydev->speed;
  560. pl->phy_state.duplex = phydev->duplex;
  561. pl->phy_state.pause = MLO_PAUSE_NONE;
  562. if (phydev->pause)
  563. pl->phy_state.pause |= MLO_PAUSE_SYM;
  564. if (phydev->asym_pause)
  565. pl->phy_state.pause |= MLO_PAUSE_ASYM;
  566. pl->phy_state.interface = phydev->interface;
  567. pl->phy_state.link = up;
  568. mutex_unlock(&pl->state_mutex);
  569. phylink_run_resolve(pl);
  570. netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
  571. phy_modes(phydev->interface),
  572. phy_speed_to_str(phydev->speed),
  573. phy_duplex_to_str(phydev->duplex));
  574. }
  575. static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
  576. {
  577. struct phylink_link_state config;
  578. __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
  579. u32 advertising;
  580. int ret;
  581. memset(&config, 0, sizeof(config));
  582. ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported);
  583. ethtool_convert_legacy_u32_to_link_mode(config.advertising,
  584. phy->advertising);
  585. config.interface = pl->link_config.interface;
  586. /*
  587. * This is the new way of dealing with flow control for PHYs,
  588. * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
  589. * phy drivers should not set SUPPORTED_[Asym_]Pause") except
  590. * using our validate call to the MAC, we rely upon the MAC
  591. * clearing the bits from both supported and advertising fields.
  592. */
  593. if (phylink_test(supported, Pause))
  594. phylink_set(config.advertising, Pause);
  595. if (phylink_test(supported, Asym_Pause))
  596. phylink_set(config.advertising, Asym_Pause);
  597. ret = phylink_validate(pl, supported, &config);
  598. if (ret)
  599. return ret;
  600. phy->phylink = pl;
  601. phy->phy_link_change = phylink_phy_change;
  602. netdev_info(pl->netdev,
  603. "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
  604. phy->drv->name);
  605. mutex_lock(&phy->lock);
  606. mutex_lock(&pl->state_mutex);
  607. pl->phydev = phy;
  608. linkmode_copy(pl->supported, supported);
  609. linkmode_copy(pl->link_config.advertising, config.advertising);
  610. /* Restrict the phy advertisement according to the MAC support. */
  611. ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising);
  612. phy->advertising = advertising;
  613. mutex_unlock(&pl->state_mutex);
  614. mutex_unlock(&phy->lock);
  615. netdev_dbg(pl->netdev,
  616. "phy: setting supported %*pb advertising 0x%08x\n",
  617. __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
  618. phy->advertising);
  619. phy_start_machine(phy);
  620. if (phy->irq > 0)
  621. phy_start_interrupts(phy);
  622. return 0;
  623. }
  624. static int __phylink_connect_phy(struct phylink *pl, struct phy_device *phy,
  625. phy_interface_t interface)
  626. {
  627. int ret;
  628. if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
  629. (pl->link_an_mode == MLO_AN_INBAND &&
  630. phy_interface_mode_is_8023z(interface))))
  631. return -EINVAL;
  632. if (pl->phydev)
  633. return -EBUSY;
  634. ret = phy_attach_direct(pl->netdev, phy, 0, interface);
  635. if (ret)
  636. return ret;
  637. ret = phylink_bringup_phy(pl, phy);
  638. if (ret)
  639. phy_detach(phy);
  640. return ret;
  641. }
  642. /**
  643. * phylink_connect_phy() - connect a PHY to the phylink instance
  644. * @pl: a pointer to a &struct phylink returned from phylink_create()
  645. * @phy: a pointer to a &struct phy_device.
  646. *
  647. * Connect @phy to the phylink instance specified by @pl by calling
  648. * phy_attach_direct(). Configure the @phy according to the MAC driver's
  649. * capabilities, start the PHYLIB state machine and enable any interrupts
  650. * that the PHY supports.
  651. *
  652. * This updates the phylink's ethtool supported and advertising link mode
  653. * masks.
  654. *
  655. * Returns 0 on success or a negative errno.
  656. */
  657. int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
  658. {
  659. /* Use PHY device/driver interface */
  660. if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
  661. pl->link_interface = phy->interface;
  662. pl->link_config.interface = pl->link_interface;
  663. }
  664. return __phylink_connect_phy(pl, phy, pl->link_interface);
  665. }
  666. EXPORT_SYMBOL_GPL(phylink_connect_phy);
  667. /**
  668. * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
  669. * @pl: a pointer to a &struct phylink returned from phylink_create()
  670. * @dn: a pointer to a &struct device_node.
  671. * @flags: PHY-specific flags to communicate to the PHY device driver
  672. *
  673. * Connect the phy specified in the device node @dn to the phylink instance
  674. * specified by @pl. Actions specified in phylink_connect_phy() will be
  675. * performed.
  676. *
  677. * Returns 0 on success or a negative errno.
  678. */
  679. int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
  680. u32 flags)
  681. {
  682. struct device_node *phy_node;
  683. struct phy_device *phy_dev;
  684. int ret;
  685. /* Fixed links and 802.3z are handled without needing a PHY */
  686. if (pl->link_an_mode == MLO_AN_FIXED ||
  687. (pl->link_an_mode == MLO_AN_INBAND &&
  688. phy_interface_mode_is_8023z(pl->link_interface)))
  689. return 0;
  690. phy_node = of_parse_phandle(dn, "phy-handle", 0);
  691. if (!phy_node)
  692. phy_node = of_parse_phandle(dn, "phy", 0);
  693. if (!phy_node)
  694. phy_node = of_parse_phandle(dn, "phy-device", 0);
  695. if (!phy_node) {
  696. if (pl->link_an_mode == MLO_AN_PHY)
  697. return -ENODEV;
  698. return 0;
  699. }
  700. phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
  701. pl->link_interface);
  702. /* We're done with the phy_node handle */
  703. of_node_put(phy_node);
  704. if (!phy_dev)
  705. return -ENODEV;
  706. ret = phylink_bringup_phy(pl, phy_dev);
  707. if (ret)
  708. phy_detach(phy_dev);
  709. return ret;
  710. }
  711. EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
  712. /**
  713. * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
  714. * instance.
  715. * @pl: a pointer to a &struct phylink returned from phylink_create()
  716. *
  717. * Disconnect any current PHY from the phylink instance described by @pl.
  718. */
  719. void phylink_disconnect_phy(struct phylink *pl)
  720. {
  721. struct phy_device *phy;
  722. ASSERT_RTNL();
  723. phy = pl->phydev;
  724. if (phy) {
  725. mutex_lock(&phy->lock);
  726. mutex_lock(&pl->state_mutex);
  727. pl->phydev = NULL;
  728. mutex_unlock(&pl->state_mutex);
  729. mutex_unlock(&phy->lock);
  730. flush_work(&pl->resolve);
  731. phy_disconnect(phy);
  732. }
  733. }
  734. EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
  735. /**
  736. * phylink_fixed_state_cb() - allow setting a fixed link callback
  737. * @pl: a pointer to a &struct phylink returned from phylink_create()
  738. * @cb: callback to execute to determine the fixed link state.
  739. *
  740. * The MAC driver should call this driver when the state of its link
  741. * can be determined through e.g: an out of band MMIO register.
  742. */
  743. int phylink_fixed_state_cb(struct phylink *pl,
  744. void (*cb)(struct net_device *dev,
  745. struct phylink_link_state *state))
  746. {
  747. /* It does not make sense to let the link be overriden unless we use
  748. * MLO_AN_FIXED
  749. */
  750. if (pl->link_an_mode != MLO_AN_FIXED)
  751. return -EINVAL;
  752. mutex_lock(&pl->state_mutex);
  753. pl->get_fixed_state = cb;
  754. mutex_unlock(&pl->state_mutex);
  755. return 0;
  756. }
  757. EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
  758. /**
  759. * phylink_mac_change() - notify phylink of a change in MAC state
  760. * @pl: a pointer to a &struct phylink returned from phylink_create()
  761. * @up: indicates whether the link is currently up.
  762. *
  763. * The MAC driver should call this driver when the state of its link
  764. * changes (eg, link failure, new negotiation results, etc.)
  765. */
  766. void phylink_mac_change(struct phylink *pl, bool up)
  767. {
  768. if (!up)
  769. pl->mac_link_dropped = true;
  770. phylink_run_resolve(pl);
  771. netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
  772. }
  773. EXPORT_SYMBOL_GPL(phylink_mac_change);
  774. /**
  775. * phylink_start() - start a phylink instance
  776. * @pl: a pointer to a &struct phylink returned from phylink_create()
  777. *
  778. * Start the phylink instance specified by @pl, configuring the MAC for the
  779. * desired link mode(s) and negotiation style. This should be called from the
  780. * network device driver's &struct net_device_ops ndo_open() method.
  781. */
  782. void phylink_start(struct phylink *pl)
  783. {
  784. ASSERT_RTNL();
  785. netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
  786. phylink_an_mode_str(pl->link_an_mode),
  787. phy_modes(pl->link_config.interface));
  788. /* Always set the carrier off */
  789. netif_carrier_off(pl->netdev);
  790. /* Apply the link configuration to the MAC when starting. This allows
  791. * a fixed-link to start with the correct parameters, and also
  792. * ensures that we set the appropriate advertisement for Serdes links.
  793. */
  794. phylink_resolve_flow(pl, &pl->link_config);
  795. phylink_mac_config(pl, &pl->link_config);
  796. /* Restart autonegotiation if using 802.3z to ensure that the link
  797. * parameters are properly negotiated. This is necessary for DSA
  798. * switches using 802.3z negotiation to ensure they see our modes.
  799. */
  800. phylink_mac_an_restart(pl);
  801. clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
  802. phylink_run_resolve(pl);
  803. if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
  804. mod_timer(&pl->link_poll, jiffies + HZ);
  805. if (pl->sfp_bus)
  806. sfp_upstream_start(pl->sfp_bus);
  807. if (pl->phydev)
  808. phy_start(pl->phydev);
  809. }
  810. EXPORT_SYMBOL_GPL(phylink_start);
  811. /**
  812. * phylink_stop() - stop a phylink instance
  813. * @pl: a pointer to a &struct phylink returned from phylink_create()
  814. *
  815. * Stop the phylink instance specified by @pl. This should be called from the
  816. * network device driver's &struct net_device_ops ndo_stop() method. The
  817. * network device's carrier state should not be changed prior to calling this
  818. * function.
  819. */
  820. void phylink_stop(struct phylink *pl)
  821. {
  822. ASSERT_RTNL();
  823. if (pl->phydev)
  824. phy_stop(pl->phydev);
  825. if (pl->sfp_bus)
  826. sfp_upstream_stop(pl->sfp_bus);
  827. if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
  828. del_timer_sync(&pl->link_poll);
  829. phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
  830. }
  831. EXPORT_SYMBOL_GPL(phylink_stop);
  832. /**
  833. * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
  834. * @pl: a pointer to a &struct phylink returned from phylink_create()
  835. * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
  836. *
  837. * Read the wake on lan parameters from the PHY attached to the phylink
  838. * instance specified by @pl. If no PHY is currently attached, report no
  839. * support for wake on lan.
  840. */
  841. void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
  842. {
  843. ASSERT_RTNL();
  844. wol->supported = 0;
  845. wol->wolopts = 0;
  846. if (pl->phydev)
  847. phy_ethtool_get_wol(pl->phydev, wol);
  848. }
  849. EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
  850. /**
  851. * phylink_ethtool_set_wol() - set wake on lan parameters
  852. * @pl: a pointer to a &struct phylink returned from phylink_create()
  853. * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
  854. *
  855. * Set the wake on lan parameters for the PHY attached to the phylink
  856. * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
  857. * error.
  858. *
  859. * Returns zero on success or negative errno code.
  860. */
  861. int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
  862. {
  863. int ret = -EOPNOTSUPP;
  864. ASSERT_RTNL();
  865. if (pl->phydev)
  866. ret = phy_ethtool_set_wol(pl->phydev, wol);
  867. return ret;
  868. }
  869. EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
  870. static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
  871. {
  872. __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
  873. linkmode_zero(mask);
  874. phylink_set_port_modes(mask);
  875. linkmode_and(dst, dst, mask);
  876. linkmode_or(dst, dst, b);
  877. }
  878. static void phylink_get_ksettings(const struct phylink_link_state *state,
  879. struct ethtool_link_ksettings *kset)
  880. {
  881. phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
  882. linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
  883. kset->base.speed = state->speed;
  884. kset->base.duplex = state->duplex;
  885. kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
  886. AUTONEG_DISABLE;
  887. }
  888. /**
  889. * phylink_ethtool_ksettings_get() - get the current link settings
  890. * @pl: a pointer to a &struct phylink returned from phylink_create()
  891. * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
  892. *
  893. * Read the current link settings for the phylink instance specified by @pl.
  894. * This will be the link settings read from the MAC, PHY or fixed link
  895. * settings depending on the current negotiation mode.
  896. */
  897. int phylink_ethtool_ksettings_get(struct phylink *pl,
  898. struct ethtool_link_ksettings *kset)
  899. {
  900. struct phylink_link_state link_state;
  901. ASSERT_RTNL();
  902. if (pl->phydev) {
  903. phy_ethtool_ksettings_get(pl->phydev, kset);
  904. } else {
  905. kset->base.port = pl->link_port;
  906. }
  907. linkmode_copy(kset->link_modes.supported, pl->supported);
  908. switch (pl->link_an_mode) {
  909. case MLO_AN_FIXED:
  910. /* We are using fixed settings. Report these as the
  911. * current link settings - and note that these also
  912. * represent the supported speeds/duplex/pause modes.
  913. */
  914. phylink_get_fixed_state(pl, &link_state);
  915. phylink_get_ksettings(&link_state, kset);
  916. break;
  917. case MLO_AN_INBAND:
  918. /* If there is a phy attached, then use the reported
  919. * settings from the phy with no modification.
  920. */
  921. if (pl->phydev)
  922. break;
  923. phylink_get_mac_state(pl, &link_state);
  924. /* The MAC is reporting the link results from its own PCS
  925. * layer via in-band status. Report these as the current
  926. * link settings.
  927. */
  928. phylink_get_ksettings(&link_state, kset);
  929. break;
  930. }
  931. return 0;
  932. }
  933. EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
  934. /**
  935. * phylink_ethtool_ksettings_set() - set the link settings
  936. * @pl: a pointer to a &struct phylink returned from phylink_create()
  937. * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
  938. */
  939. int phylink_ethtool_ksettings_set(struct phylink *pl,
  940. const struct ethtool_link_ksettings *kset)
  941. {
  942. struct ethtool_link_ksettings our_kset;
  943. struct phylink_link_state config;
  944. int ret;
  945. ASSERT_RTNL();
  946. if (kset->base.autoneg != AUTONEG_DISABLE &&
  947. kset->base.autoneg != AUTONEG_ENABLE)
  948. return -EINVAL;
  949. config = pl->link_config;
  950. /* Mask out unsupported advertisements */
  951. linkmode_and(config.advertising, kset->link_modes.advertising,
  952. pl->supported);
  953. /* FIXME: should we reject autoneg if phy/mac does not support it? */
  954. if (kset->base.autoneg == AUTONEG_DISABLE) {
  955. const struct phy_setting *s;
  956. /* Autonegotiation disabled, select a suitable speed and
  957. * duplex.
  958. */
  959. s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
  960. pl->supported,
  961. __ETHTOOL_LINK_MODE_MASK_NBITS, false);
  962. if (!s)
  963. return -EINVAL;
  964. /* If we have a fixed link (as specified by firmware), refuse
  965. * to change link parameters.
  966. */
  967. if (pl->link_an_mode == MLO_AN_FIXED &&
  968. (s->speed != pl->link_config.speed ||
  969. s->duplex != pl->link_config.duplex))
  970. return -EINVAL;
  971. config.speed = s->speed;
  972. config.duplex = s->duplex;
  973. config.an_enabled = false;
  974. __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
  975. } else {
  976. /* If we have a fixed link, refuse to enable autonegotiation */
  977. if (pl->link_an_mode == MLO_AN_FIXED)
  978. return -EINVAL;
  979. config.speed = SPEED_UNKNOWN;
  980. config.duplex = DUPLEX_UNKNOWN;
  981. config.an_enabled = true;
  982. __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
  983. }
  984. if (phylink_validate(pl, pl->supported, &config))
  985. return -EINVAL;
  986. /* If autonegotiation is enabled, we must have an advertisement */
  987. if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
  988. return -EINVAL;
  989. our_kset = *kset;
  990. linkmode_copy(our_kset.link_modes.advertising, config.advertising);
  991. our_kset.base.speed = config.speed;
  992. our_kset.base.duplex = config.duplex;
  993. /* If we have a PHY, configure the phy */
  994. if (pl->phydev) {
  995. ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
  996. if (ret)
  997. return ret;
  998. }
  999. mutex_lock(&pl->state_mutex);
  1000. /* Configure the MAC to match the new settings */
  1001. linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
  1002. pl->link_config.interface = config.interface;
  1003. pl->link_config.speed = our_kset.base.speed;
  1004. pl->link_config.duplex = our_kset.base.duplex;
  1005. pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
  1006. if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
  1007. phylink_mac_config(pl, &pl->link_config);
  1008. phylink_mac_an_restart(pl);
  1009. }
  1010. mutex_unlock(&pl->state_mutex);
  1011. return 0;
  1012. }
  1013. EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
  1014. /**
  1015. * phylink_ethtool_nway_reset() - restart negotiation
  1016. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1017. *
  1018. * Restart negotiation for the phylink instance specified by @pl. This will
  1019. * cause any attached phy to restart negotiation with the link partner, and
  1020. * if the MAC is in a BaseX mode, the MAC will also be requested to restart
  1021. * negotiation.
  1022. *
  1023. * Returns zero on success, or negative error code.
  1024. */
  1025. int phylink_ethtool_nway_reset(struct phylink *pl)
  1026. {
  1027. int ret = 0;
  1028. ASSERT_RTNL();
  1029. if (pl->phydev)
  1030. ret = phy_restart_aneg(pl->phydev);
  1031. phylink_mac_an_restart(pl);
  1032. return ret;
  1033. }
  1034. EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
  1035. /**
  1036. * phylink_ethtool_get_pauseparam() - get the current pause parameters
  1037. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1038. * @pause: a pointer to a &struct ethtool_pauseparam
  1039. */
  1040. void phylink_ethtool_get_pauseparam(struct phylink *pl,
  1041. struct ethtool_pauseparam *pause)
  1042. {
  1043. ASSERT_RTNL();
  1044. pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
  1045. pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
  1046. pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
  1047. }
  1048. EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
  1049. /**
  1050. * phylink_ethtool_set_pauseparam() - set the current pause parameters
  1051. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1052. * @pause: a pointer to a &struct ethtool_pauseparam
  1053. */
  1054. int phylink_ethtool_set_pauseparam(struct phylink *pl,
  1055. struct ethtool_pauseparam *pause)
  1056. {
  1057. struct phylink_link_state *config = &pl->link_config;
  1058. ASSERT_RTNL();
  1059. if (!phylink_test(pl->supported, Pause) &&
  1060. !phylink_test(pl->supported, Asym_Pause))
  1061. return -EOPNOTSUPP;
  1062. if (!phylink_test(pl->supported, Asym_Pause) &&
  1063. !pause->autoneg && pause->rx_pause != pause->tx_pause)
  1064. return -EINVAL;
  1065. config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
  1066. if (pause->autoneg)
  1067. config->pause |= MLO_PAUSE_AN;
  1068. if (pause->rx_pause)
  1069. config->pause |= MLO_PAUSE_RX;
  1070. if (pause->tx_pause)
  1071. config->pause |= MLO_PAUSE_TX;
  1072. if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
  1073. switch (pl->link_an_mode) {
  1074. case MLO_AN_PHY:
  1075. /* Silently mark the carrier down, and then trigger a resolve */
  1076. netif_carrier_off(pl->netdev);
  1077. phylink_run_resolve(pl);
  1078. break;
  1079. case MLO_AN_FIXED:
  1080. /* Should we allow fixed links to change against the config? */
  1081. phylink_resolve_flow(pl, config);
  1082. phylink_mac_config(pl, config);
  1083. break;
  1084. case MLO_AN_INBAND:
  1085. phylink_mac_config(pl, config);
  1086. phylink_mac_an_restart(pl);
  1087. break;
  1088. }
  1089. }
  1090. return 0;
  1091. }
  1092. EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
  1093. /**
  1094. * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
  1095. * counter
  1096. * @pl: a pointer to a &struct phylink returned from phylink_create().
  1097. *
  1098. * Read the Energy Efficient Ethernet error counter from the PHY associated
  1099. * with the phylink instance specified by @pl.
  1100. *
  1101. * Returns positive error counter value, or negative error code.
  1102. */
  1103. int phylink_get_eee_err(struct phylink *pl)
  1104. {
  1105. int ret = 0;
  1106. ASSERT_RTNL();
  1107. if (pl->phydev)
  1108. ret = phy_get_eee_err(pl->phydev);
  1109. return ret;
  1110. }
  1111. EXPORT_SYMBOL_GPL(phylink_get_eee_err);
  1112. /**
  1113. * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
  1114. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1115. * @eee: a pointer to a &struct ethtool_eee for the read parameters
  1116. */
  1117. int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
  1118. {
  1119. int ret = -EOPNOTSUPP;
  1120. ASSERT_RTNL();
  1121. if (pl->phydev)
  1122. ret = phy_ethtool_get_eee(pl->phydev, eee);
  1123. return ret;
  1124. }
  1125. EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
  1126. /**
  1127. * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
  1128. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1129. * @eee: a pointer to a &struct ethtool_eee for the desired parameters
  1130. */
  1131. int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
  1132. {
  1133. int ret = -EOPNOTSUPP;
  1134. ASSERT_RTNL();
  1135. if (pl->phydev)
  1136. ret = phy_ethtool_set_eee(pl->phydev, eee);
  1137. return ret;
  1138. }
  1139. EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
  1140. /* This emulates MII registers for a fixed-mode phy operating as per the
  1141. * passed in state. "aneg" defines if we report negotiation is possible.
  1142. *
  1143. * FIXME: should deal with negotiation state too.
  1144. */
  1145. static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
  1146. struct phylink_link_state *state, bool aneg)
  1147. {
  1148. struct fixed_phy_status fs;
  1149. int val;
  1150. fs.link = state->link;
  1151. fs.speed = state->speed;
  1152. fs.duplex = state->duplex;
  1153. fs.pause = state->pause & MLO_PAUSE_SYM;
  1154. fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
  1155. val = swphy_read_reg(reg, &fs);
  1156. if (reg == MII_BMSR) {
  1157. if (!state->an_complete)
  1158. val &= ~BMSR_ANEGCOMPLETE;
  1159. if (!aneg)
  1160. val &= ~BMSR_ANEGCAPABLE;
  1161. }
  1162. return val;
  1163. }
  1164. static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
  1165. unsigned int reg)
  1166. {
  1167. struct phy_device *phydev = pl->phydev;
  1168. int prtad, devad;
  1169. if (mdio_phy_id_is_c45(phy_id)) {
  1170. prtad = mdio_phy_id_prtad(phy_id);
  1171. devad = mdio_phy_id_devad(phy_id);
  1172. devad = MII_ADDR_C45 | devad << 16 | reg;
  1173. } else if (phydev->is_c45) {
  1174. switch (reg) {
  1175. case MII_BMCR:
  1176. case MII_BMSR:
  1177. case MII_PHYSID1:
  1178. case MII_PHYSID2:
  1179. devad = __ffs(phydev->c45_ids.devices_in_package);
  1180. break;
  1181. case MII_ADVERTISE:
  1182. case MII_LPA:
  1183. if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
  1184. return -EINVAL;
  1185. devad = MDIO_MMD_AN;
  1186. if (reg == MII_ADVERTISE)
  1187. reg = MDIO_AN_ADVERTISE;
  1188. else
  1189. reg = MDIO_AN_LPA;
  1190. break;
  1191. default:
  1192. return -EINVAL;
  1193. }
  1194. prtad = phy_id;
  1195. devad = MII_ADDR_C45 | devad << 16 | reg;
  1196. } else {
  1197. prtad = phy_id;
  1198. devad = reg;
  1199. }
  1200. return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
  1201. }
  1202. static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
  1203. unsigned int reg, unsigned int val)
  1204. {
  1205. struct phy_device *phydev = pl->phydev;
  1206. int prtad, devad;
  1207. if (mdio_phy_id_is_c45(phy_id)) {
  1208. prtad = mdio_phy_id_prtad(phy_id);
  1209. devad = mdio_phy_id_devad(phy_id);
  1210. devad = MII_ADDR_C45 | devad << 16 | reg;
  1211. } else if (phydev->is_c45) {
  1212. switch (reg) {
  1213. case MII_BMCR:
  1214. case MII_BMSR:
  1215. case MII_PHYSID1:
  1216. case MII_PHYSID2:
  1217. devad = __ffs(phydev->c45_ids.devices_in_package);
  1218. break;
  1219. case MII_ADVERTISE:
  1220. case MII_LPA:
  1221. if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
  1222. return -EINVAL;
  1223. devad = MDIO_MMD_AN;
  1224. if (reg == MII_ADVERTISE)
  1225. reg = MDIO_AN_ADVERTISE;
  1226. else
  1227. reg = MDIO_AN_LPA;
  1228. break;
  1229. default:
  1230. return -EINVAL;
  1231. }
  1232. prtad = phy_id;
  1233. devad = MII_ADDR_C45 | devad << 16 | reg;
  1234. } else {
  1235. prtad = phy_id;
  1236. devad = reg;
  1237. }
  1238. return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
  1239. }
  1240. static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
  1241. unsigned int reg)
  1242. {
  1243. struct phylink_link_state state;
  1244. int val = 0xffff;
  1245. switch (pl->link_an_mode) {
  1246. case MLO_AN_FIXED:
  1247. if (phy_id == 0) {
  1248. phylink_get_fixed_state(pl, &state);
  1249. val = phylink_mii_emul_read(pl->netdev, reg, &state,
  1250. true);
  1251. }
  1252. break;
  1253. case MLO_AN_PHY:
  1254. return -EOPNOTSUPP;
  1255. case MLO_AN_INBAND:
  1256. if (phy_id == 0) {
  1257. val = phylink_get_mac_state(pl, &state);
  1258. if (val < 0)
  1259. return val;
  1260. val = phylink_mii_emul_read(pl->netdev, reg, &state,
  1261. true);
  1262. }
  1263. break;
  1264. }
  1265. return val & 0xffff;
  1266. }
  1267. static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
  1268. unsigned int reg, unsigned int val)
  1269. {
  1270. switch (pl->link_an_mode) {
  1271. case MLO_AN_FIXED:
  1272. break;
  1273. case MLO_AN_PHY:
  1274. return -EOPNOTSUPP;
  1275. case MLO_AN_INBAND:
  1276. break;
  1277. }
  1278. return 0;
  1279. }
  1280. /**
  1281. * phylink_mii_ioctl() - generic mii ioctl interface
  1282. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1283. * @ifr: a pointer to a &struct ifreq for socket ioctls
  1284. * @cmd: ioctl cmd to execute
  1285. *
  1286. * Perform the specified MII ioctl on the PHY attached to the phylink instance
  1287. * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
  1288. *
  1289. * Returns: zero on success or negative error code.
  1290. *
  1291. * %SIOCGMIIPHY:
  1292. * read register from the current PHY.
  1293. * %SIOCGMIIREG:
  1294. * read register from the specified PHY.
  1295. * %SIOCSMIIREG:
  1296. * set a register on the specified PHY.
  1297. */
  1298. int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
  1299. {
  1300. struct mii_ioctl_data *mii = if_mii(ifr);
  1301. int ret;
  1302. ASSERT_RTNL();
  1303. if (pl->phydev) {
  1304. /* PHYs only exist for MLO_AN_PHY and SGMII */
  1305. switch (cmd) {
  1306. case SIOCGMIIPHY:
  1307. mii->phy_id = pl->phydev->mdio.addr;
  1308. /* fall through */
  1309. case SIOCGMIIREG:
  1310. ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
  1311. if (ret >= 0) {
  1312. mii->val_out = ret;
  1313. ret = 0;
  1314. }
  1315. break;
  1316. case SIOCSMIIREG:
  1317. ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
  1318. mii->val_in);
  1319. break;
  1320. default:
  1321. ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
  1322. break;
  1323. }
  1324. } else {
  1325. switch (cmd) {
  1326. case SIOCGMIIPHY:
  1327. mii->phy_id = 0;
  1328. /* fall through */
  1329. case SIOCGMIIREG:
  1330. ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
  1331. if (ret >= 0) {
  1332. mii->val_out = ret;
  1333. ret = 0;
  1334. }
  1335. break;
  1336. case SIOCSMIIREG:
  1337. ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
  1338. mii->val_in);
  1339. break;
  1340. default:
  1341. ret = -EOPNOTSUPP;
  1342. break;
  1343. }
  1344. }
  1345. return ret;
  1346. }
  1347. EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
  1348. static int phylink_sfp_module_insert(void *upstream,
  1349. const struct sfp_eeprom_id *id)
  1350. {
  1351. struct phylink *pl = upstream;
  1352. __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
  1353. struct phylink_link_state config;
  1354. phy_interface_t iface;
  1355. int ret = 0;
  1356. bool changed;
  1357. u8 port;
  1358. ASSERT_RTNL();
  1359. sfp_parse_support(pl->sfp_bus, id, support);
  1360. port = sfp_parse_port(pl->sfp_bus, id, support);
  1361. memset(&config, 0, sizeof(config));
  1362. linkmode_copy(config.advertising, support);
  1363. config.interface = PHY_INTERFACE_MODE_NA;
  1364. config.speed = SPEED_UNKNOWN;
  1365. config.duplex = DUPLEX_UNKNOWN;
  1366. config.pause = MLO_PAUSE_AN;
  1367. config.an_enabled = pl->link_config.an_enabled;
  1368. /* Ignore errors if we're expecting a PHY to attach later */
  1369. ret = phylink_validate(pl, support, &config);
  1370. if (ret) {
  1371. netdev_err(pl->netdev, "validation with support %*pb failed: %d\n",
  1372. __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
  1373. return ret;
  1374. }
  1375. iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
  1376. if (iface == PHY_INTERFACE_MODE_NA) {
  1377. netdev_err(pl->netdev,
  1378. "selection of interface failed, advertisement %*pb\n",
  1379. __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
  1380. return -EINVAL;
  1381. }
  1382. config.interface = iface;
  1383. ret = phylink_validate(pl, support, &config);
  1384. if (ret) {
  1385. netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
  1386. phylink_an_mode_str(MLO_AN_INBAND),
  1387. phy_modes(config.interface),
  1388. __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
  1389. return ret;
  1390. }
  1391. netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
  1392. phylink_an_mode_str(MLO_AN_INBAND),
  1393. phy_modes(config.interface),
  1394. __ETHTOOL_LINK_MODE_MASK_NBITS, support);
  1395. if (phy_interface_mode_is_8023z(iface) && pl->phydev)
  1396. return -EINVAL;
  1397. changed = !bitmap_equal(pl->supported, support,
  1398. __ETHTOOL_LINK_MODE_MASK_NBITS);
  1399. if (changed) {
  1400. linkmode_copy(pl->supported, support);
  1401. linkmode_copy(pl->link_config.advertising, config.advertising);
  1402. }
  1403. if (pl->link_an_mode != MLO_AN_INBAND ||
  1404. pl->link_config.interface != config.interface) {
  1405. pl->link_config.interface = config.interface;
  1406. pl->link_an_mode = MLO_AN_INBAND;
  1407. changed = true;
  1408. netdev_info(pl->netdev, "switched to %s/%s link mode\n",
  1409. phylink_an_mode_str(MLO_AN_INBAND),
  1410. phy_modes(config.interface));
  1411. }
  1412. pl->link_port = port;
  1413. if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
  1414. &pl->phylink_disable_state))
  1415. phylink_mac_config(pl, &pl->link_config);
  1416. return ret;
  1417. }
  1418. static void phylink_sfp_link_down(void *upstream)
  1419. {
  1420. struct phylink *pl = upstream;
  1421. ASSERT_RTNL();
  1422. phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
  1423. }
  1424. static void phylink_sfp_link_up(void *upstream)
  1425. {
  1426. struct phylink *pl = upstream;
  1427. ASSERT_RTNL();
  1428. clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
  1429. phylink_run_resolve(pl);
  1430. }
  1431. static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
  1432. {
  1433. struct phylink *pl = upstream;
  1434. return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
  1435. }
  1436. static void phylink_sfp_disconnect_phy(void *upstream)
  1437. {
  1438. phylink_disconnect_phy(upstream);
  1439. }
  1440. static const struct sfp_upstream_ops sfp_phylink_ops = {
  1441. .module_insert = phylink_sfp_module_insert,
  1442. .link_up = phylink_sfp_link_up,
  1443. .link_down = phylink_sfp_link_down,
  1444. .connect_phy = phylink_sfp_connect_phy,
  1445. .disconnect_phy = phylink_sfp_disconnect_phy,
  1446. };
  1447. /* Helpers for MAC drivers */
  1448. /**
  1449. * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
  1450. * @state: a pointer to a &struct phylink_link_state
  1451. *
  1452. * Inspect the interface mode, advertising mask or forced speed and
  1453. * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
  1454. * the interface mode to suit. @state->interface is appropriately
  1455. * updated, and the advertising mask has the "other" baseX_Full flag
  1456. * cleared.
  1457. */
  1458. void phylink_helper_basex_speed(struct phylink_link_state *state)
  1459. {
  1460. if (phy_interface_mode_is_8023z(state->interface)) {
  1461. bool want_2500 = state->an_enabled ?
  1462. phylink_test(state->advertising, 2500baseX_Full) :
  1463. state->speed == SPEED_2500;
  1464. if (want_2500) {
  1465. phylink_clear(state->advertising, 1000baseX_Full);
  1466. state->interface = PHY_INTERFACE_MODE_2500BASEX;
  1467. } else {
  1468. phylink_clear(state->advertising, 2500baseX_Full);
  1469. state->interface = PHY_INTERFACE_MODE_1000BASEX;
  1470. }
  1471. }
  1472. }
  1473. EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
  1474. MODULE_LICENSE("GPL");