phylink.c 45 KB

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