phylink.c 45 KB

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