phylink.c 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * phylink models the MAC to optional PHY connection, supporting
  4. * technologies such as SFP cages where the PHY is hot-pluggable.
  5. *
  6. * Copyright (C) 2015 Russell King
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/ethtool.h>
  10. #include <linux/export.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/of.h>
  14. #include <linux/of_mdio.h>
  15. #include <linux/phy.h>
  16. #include <linux/phy_fixed.h>
  17. #include <linux/phylink.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/timer.h>
  21. #include <linux/workqueue.h>
  22. #include "sfp.h"
  23. #include "swphy.h"
  24. #define SUPPORTED_INTERFACES \
  25. (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
  26. SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
  27. #define ADVERTISED_INTERFACES \
  28. (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
  29. ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
  30. enum {
  31. PHYLINK_DISABLE_STOPPED,
  32. PHYLINK_DISABLE_LINK,
  33. PHYLINK_DISABLE_MAC_WOL,
  34. PCS_STATE_DOWN = 0,
  35. PCS_STATE_STARTING,
  36. PCS_STATE_STARTED,
  37. };
  38. /**
  39. * struct phylink - internal data type for phylink
  40. */
  41. struct phylink {
  42. /* private: */
  43. struct net_device *netdev;
  44. const struct phylink_mac_ops *mac_ops;
  45. struct phylink_config *config;
  46. struct phylink_pcs *pcs;
  47. struct device *dev;
  48. unsigned int old_link_state:1;
  49. unsigned long phylink_disable_state; /* bitmask of disables */
  50. struct phy_device *phydev;
  51. phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
  52. u8 cfg_link_an_mode; /* MLO_AN_xxx */
  53. u8 cur_link_an_mode;
  54. u8 link_port; /* The current non-phy ethtool port */
  55. __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
  56. /* The link configuration settings */
  57. struct phylink_link_state link_config;
  58. /* The current settings */
  59. phy_interface_t cur_interface;
  60. struct gpio_desc *link_gpio;
  61. unsigned int link_irq;
  62. struct timer_list link_poll;
  63. void (*get_fixed_state)(struct net_device *dev,
  64. struct phylink_link_state *s);
  65. struct mutex state_mutex;
  66. struct phylink_link_state phy_state;
  67. struct work_struct resolve;
  68. unsigned int pcs_neg_mode;
  69. unsigned int pcs_state;
  70. bool link_failed;
  71. bool using_mac_select_pcs;
  72. struct sfp_bus *sfp_bus;
  73. bool sfp_may_have_phy;
  74. DECLARE_PHY_INTERFACE_MASK(sfp_interfaces);
  75. __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
  76. u8 sfp_port;
  77. };
  78. #define phylink_printk(level, pl, fmt, ...) \
  79. do { \
  80. if ((pl)->config->type == PHYLINK_NETDEV) \
  81. netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
  82. else if ((pl)->config->type == PHYLINK_DEV) \
  83. dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
  84. } while (0)
  85. #define phylink_err(pl, fmt, ...) \
  86. phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
  87. #define phylink_warn(pl, fmt, ...) \
  88. phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
  89. #define phylink_info(pl, fmt, ...) \
  90. phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
  91. #if defined(CONFIG_DYNAMIC_DEBUG)
  92. #define phylink_dbg(pl, fmt, ...) \
  93. do { \
  94. if ((pl)->config->type == PHYLINK_NETDEV) \
  95. netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \
  96. else if ((pl)->config->type == PHYLINK_DEV) \
  97. dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \
  98. } while (0)
  99. #elif defined(DEBUG)
  100. #define phylink_dbg(pl, fmt, ...) \
  101. phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
  102. #else
  103. #define phylink_dbg(pl, fmt, ...) \
  104. ({ \
  105. if (0) \
  106. phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \
  107. })
  108. #endif
  109. static const phy_interface_t phylink_sfp_interface_preference[] = {
  110. PHY_INTERFACE_MODE_25GBASER,
  111. PHY_INTERFACE_MODE_USXGMII,
  112. PHY_INTERFACE_MODE_10GBASER,
  113. PHY_INTERFACE_MODE_5GBASER,
  114. PHY_INTERFACE_MODE_2500BASEX,
  115. PHY_INTERFACE_MODE_SGMII,
  116. PHY_INTERFACE_MODE_1000BASEX,
  117. PHY_INTERFACE_MODE_100BASEX,
  118. };
  119. static DECLARE_PHY_INTERFACE_MASK(phylink_sfp_interfaces);
  120. /**
  121. * phylink_set_port_modes() - set the port type modes in the ethtool mask
  122. * @mask: ethtool link mode mask
  123. *
  124. * Sets all the port type modes in the ethtool mask. MAC drivers should
  125. * use this in their 'validate' callback.
  126. */
  127. void phylink_set_port_modes(unsigned long *mask)
  128. {
  129. phylink_set(mask, TP);
  130. phylink_set(mask, AUI);
  131. phylink_set(mask, MII);
  132. phylink_set(mask, FIBRE);
  133. phylink_set(mask, BNC);
  134. phylink_set(mask, Backplane);
  135. }
  136. EXPORT_SYMBOL_GPL(phylink_set_port_modes);
  137. static int phylink_is_empty_linkmode(const unsigned long *linkmode)
  138. {
  139. __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
  140. phylink_set_port_modes(tmp);
  141. phylink_set(tmp, Autoneg);
  142. phylink_set(tmp, Pause);
  143. phylink_set(tmp, Asym_Pause);
  144. return linkmode_subset(linkmode, tmp);
  145. }
  146. static const char *phylink_an_mode_str(unsigned int mode)
  147. {
  148. static const char *modestr[] = {
  149. [MLO_AN_PHY] = "phy",
  150. [MLO_AN_FIXED] = "fixed",
  151. [MLO_AN_INBAND] = "inband",
  152. };
  153. return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
  154. }
  155. static unsigned int phylink_interface_signal_rate(phy_interface_t interface)
  156. {
  157. switch (interface) {
  158. case PHY_INTERFACE_MODE_SGMII:
  159. case PHY_INTERFACE_MODE_1000BASEX: /* 1.25Mbd */
  160. return 1250;
  161. case PHY_INTERFACE_MODE_2500BASEX: /* 3.125Mbd */
  162. return 3125;
  163. case PHY_INTERFACE_MODE_5GBASER: /* 5.15625Mbd */
  164. return 5156;
  165. case PHY_INTERFACE_MODE_10GBASER: /* 10.3125Mbd */
  166. return 10313;
  167. default:
  168. return 0;
  169. }
  170. }
  171. /**
  172. * phylink_interface_max_speed() - get the maximum speed of a phy interface
  173. * @interface: phy interface mode defined by &typedef phy_interface_t
  174. *
  175. * Determine the maximum speed of a phy interface. This is intended to help
  176. * determine the correct speed to pass to the MAC when the phy is performing
  177. * rate matching.
  178. *
  179. * Return: The maximum speed of @interface
  180. */
  181. static int phylink_interface_max_speed(phy_interface_t interface)
  182. {
  183. switch (interface) {
  184. case PHY_INTERFACE_MODE_100BASEX:
  185. case PHY_INTERFACE_MODE_REVRMII:
  186. case PHY_INTERFACE_MODE_RMII:
  187. case PHY_INTERFACE_MODE_SMII:
  188. case PHY_INTERFACE_MODE_REVMII:
  189. case PHY_INTERFACE_MODE_MII:
  190. return SPEED_100;
  191. case PHY_INTERFACE_MODE_TBI:
  192. case PHY_INTERFACE_MODE_MOCA:
  193. case PHY_INTERFACE_MODE_RTBI:
  194. case PHY_INTERFACE_MODE_1000BASEX:
  195. case PHY_INTERFACE_MODE_1000BASEKX:
  196. case PHY_INTERFACE_MODE_TRGMII:
  197. case PHY_INTERFACE_MODE_RGMII_TXID:
  198. case PHY_INTERFACE_MODE_RGMII_RXID:
  199. case PHY_INTERFACE_MODE_RGMII_ID:
  200. case PHY_INTERFACE_MODE_RGMII:
  201. case PHY_INTERFACE_MODE_PSGMII:
  202. case PHY_INTERFACE_MODE_QSGMII:
  203. case PHY_INTERFACE_MODE_QUSGMII:
  204. case PHY_INTERFACE_MODE_SGMII:
  205. case PHY_INTERFACE_MODE_GMII:
  206. return SPEED_1000;
  207. case PHY_INTERFACE_MODE_2500BASEX:
  208. case PHY_INTERFACE_MODE_10G_QXGMII:
  209. return SPEED_2500;
  210. case PHY_INTERFACE_MODE_5GBASER:
  211. return SPEED_5000;
  212. case PHY_INTERFACE_MODE_XGMII:
  213. case PHY_INTERFACE_MODE_RXAUI:
  214. case PHY_INTERFACE_MODE_XAUI:
  215. case PHY_INTERFACE_MODE_10GBASER:
  216. case PHY_INTERFACE_MODE_10GKR:
  217. case PHY_INTERFACE_MODE_USXGMII:
  218. return SPEED_10000;
  219. case PHY_INTERFACE_MODE_25GBASER:
  220. return SPEED_25000;
  221. case PHY_INTERFACE_MODE_XLGMII:
  222. return SPEED_40000;
  223. case PHY_INTERFACE_MODE_INTERNAL:
  224. case PHY_INTERFACE_MODE_NA:
  225. case PHY_INTERFACE_MODE_MAX:
  226. /* No idea! Garbage in, unknown out */
  227. return SPEED_UNKNOWN;
  228. }
  229. /* If we get here, someone forgot to add an interface mode above */
  230. WARN_ON_ONCE(1);
  231. return SPEED_UNKNOWN;
  232. }
  233. /**
  234. * phylink_caps_to_linkmodes() - Convert capabilities to ethtool link modes
  235. * @linkmodes: ethtool linkmode mask (must be already initialised)
  236. * @caps: bitmask of MAC capabilities
  237. *
  238. * Set all possible pause, speed and duplex linkmodes in @linkmodes that are
  239. * supported by the @caps. @linkmodes must have been initialised previously.
  240. */
  241. static void phylink_caps_to_linkmodes(unsigned long *linkmodes,
  242. unsigned long caps)
  243. {
  244. if (caps & MAC_SYM_PAUSE)
  245. __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, linkmodes);
  246. if (caps & MAC_ASYM_PAUSE)
  247. __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, linkmodes);
  248. if (caps & MAC_10HD) {
  249. __set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, linkmodes);
  250. __set_bit(ETHTOOL_LINK_MODE_10baseT1S_Half_BIT, linkmodes);
  251. __set_bit(ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT, linkmodes);
  252. }
  253. if (caps & MAC_10FD) {
  254. __set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, linkmodes);
  255. __set_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, linkmodes);
  256. __set_bit(ETHTOOL_LINK_MODE_10baseT1S_Full_BIT, linkmodes);
  257. }
  258. if (caps & MAC_100HD) {
  259. __set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, linkmodes);
  260. __set_bit(ETHTOOL_LINK_MODE_100baseFX_Half_BIT, linkmodes);
  261. }
  262. if (caps & MAC_100FD) {
  263. __set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, linkmodes);
  264. __set_bit(ETHTOOL_LINK_MODE_100baseT1_Full_BIT, linkmodes);
  265. __set_bit(ETHTOOL_LINK_MODE_100baseFX_Full_BIT, linkmodes);
  266. }
  267. if (caps & MAC_1000HD)
  268. __set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, linkmodes);
  269. if (caps & MAC_1000FD) {
  270. __set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, linkmodes);
  271. __set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, linkmodes);
  272. __set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, linkmodes);
  273. __set_bit(ETHTOOL_LINK_MODE_1000baseT1_Full_BIT, linkmodes);
  274. }
  275. if (caps & MAC_2500FD) {
  276. __set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, linkmodes);
  277. __set_bit(ETHTOOL_LINK_MODE_2500baseX_Full_BIT, linkmodes);
  278. }
  279. if (caps & MAC_5000FD)
  280. __set_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, linkmodes);
  281. if (caps & MAC_10000FD) {
  282. __set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, linkmodes);
  283. __set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, linkmodes);
  284. __set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, linkmodes);
  285. __set_bit(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT, linkmodes);
  286. __set_bit(ETHTOOL_LINK_MODE_10000baseCR_Full_BIT, linkmodes);
  287. __set_bit(ETHTOOL_LINK_MODE_10000baseSR_Full_BIT, linkmodes);
  288. __set_bit(ETHTOOL_LINK_MODE_10000baseLR_Full_BIT, linkmodes);
  289. __set_bit(ETHTOOL_LINK_MODE_10000baseLRM_Full_BIT, linkmodes);
  290. __set_bit(ETHTOOL_LINK_MODE_10000baseER_Full_BIT, linkmodes);
  291. }
  292. if (caps & MAC_25000FD) {
  293. __set_bit(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT, linkmodes);
  294. __set_bit(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT, linkmodes);
  295. __set_bit(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT, linkmodes);
  296. }
  297. if (caps & MAC_40000FD) {
  298. __set_bit(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, linkmodes);
  299. __set_bit(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, linkmodes);
  300. __set_bit(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT, linkmodes);
  301. __set_bit(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT, linkmodes);
  302. }
  303. if (caps & MAC_50000FD) {
  304. __set_bit(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT, linkmodes);
  305. __set_bit(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT, linkmodes);
  306. __set_bit(ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT, linkmodes);
  307. __set_bit(ETHTOOL_LINK_MODE_50000baseKR_Full_BIT, linkmodes);
  308. __set_bit(ETHTOOL_LINK_MODE_50000baseSR_Full_BIT, linkmodes);
  309. __set_bit(ETHTOOL_LINK_MODE_50000baseCR_Full_BIT, linkmodes);
  310. __set_bit(ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
  311. linkmodes);
  312. __set_bit(ETHTOOL_LINK_MODE_50000baseDR_Full_BIT, linkmodes);
  313. }
  314. if (caps & MAC_56000FD) {
  315. __set_bit(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT, linkmodes);
  316. __set_bit(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT, linkmodes);
  317. __set_bit(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT, linkmodes);
  318. __set_bit(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT, linkmodes);
  319. }
  320. if (caps & MAC_100000FD) {
  321. __set_bit(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, linkmodes);
  322. __set_bit(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT, linkmodes);
  323. __set_bit(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, linkmodes);
  324. __set_bit(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
  325. linkmodes);
  326. __set_bit(ETHTOOL_LINK_MODE_100000baseKR2_Full_BIT, linkmodes);
  327. __set_bit(ETHTOOL_LINK_MODE_100000baseSR2_Full_BIT, linkmodes);
  328. __set_bit(ETHTOOL_LINK_MODE_100000baseCR2_Full_BIT, linkmodes);
  329. __set_bit(ETHTOOL_LINK_MODE_100000baseLR2_ER2_FR2_Full_BIT,
  330. linkmodes);
  331. __set_bit(ETHTOOL_LINK_MODE_100000baseDR2_Full_BIT, linkmodes);
  332. __set_bit(ETHTOOL_LINK_MODE_100000baseKR_Full_BIT, linkmodes);
  333. __set_bit(ETHTOOL_LINK_MODE_100000baseSR_Full_BIT, linkmodes);
  334. __set_bit(ETHTOOL_LINK_MODE_100000baseLR_ER_FR_Full_BIT,
  335. linkmodes);
  336. __set_bit(ETHTOOL_LINK_MODE_100000baseCR_Full_BIT, linkmodes);
  337. __set_bit(ETHTOOL_LINK_MODE_100000baseDR_Full_BIT, linkmodes);
  338. }
  339. if (caps & MAC_200000FD) {
  340. __set_bit(ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT, linkmodes);
  341. __set_bit(ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT, linkmodes);
  342. __set_bit(ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT,
  343. linkmodes);
  344. __set_bit(ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT, linkmodes);
  345. __set_bit(ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT, linkmodes);
  346. __set_bit(ETHTOOL_LINK_MODE_200000baseKR2_Full_BIT, linkmodes);
  347. __set_bit(ETHTOOL_LINK_MODE_200000baseSR2_Full_BIT, linkmodes);
  348. __set_bit(ETHTOOL_LINK_MODE_200000baseLR2_ER2_FR2_Full_BIT,
  349. linkmodes);
  350. __set_bit(ETHTOOL_LINK_MODE_200000baseDR2_Full_BIT, linkmodes);
  351. __set_bit(ETHTOOL_LINK_MODE_200000baseCR2_Full_BIT, linkmodes);
  352. }
  353. if (caps & MAC_400000FD) {
  354. __set_bit(ETHTOOL_LINK_MODE_400000baseKR8_Full_BIT, linkmodes);
  355. __set_bit(ETHTOOL_LINK_MODE_400000baseSR8_Full_BIT, linkmodes);
  356. __set_bit(ETHTOOL_LINK_MODE_400000baseLR8_ER8_FR8_Full_BIT,
  357. linkmodes);
  358. __set_bit(ETHTOOL_LINK_MODE_400000baseDR8_Full_BIT, linkmodes);
  359. __set_bit(ETHTOOL_LINK_MODE_400000baseCR8_Full_BIT, linkmodes);
  360. __set_bit(ETHTOOL_LINK_MODE_400000baseKR4_Full_BIT, linkmodes);
  361. __set_bit(ETHTOOL_LINK_MODE_400000baseSR4_Full_BIT, linkmodes);
  362. __set_bit(ETHTOOL_LINK_MODE_400000baseLR4_ER4_FR4_Full_BIT,
  363. linkmodes);
  364. __set_bit(ETHTOOL_LINK_MODE_400000baseDR4_Full_BIT, linkmodes);
  365. __set_bit(ETHTOOL_LINK_MODE_400000baseCR4_Full_BIT, linkmodes);
  366. }
  367. }
  368. static struct {
  369. unsigned long mask;
  370. int speed;
  371. unsigned int duplex;
  372. } phylink_caps_params[] = {
  373. { MAC_400000FD, SPEED_400000, DUPLEX_FULL },
  374. { MAC_200000FD, SPEED_200000, DUPLEX_FULL },
  375. { MAC_100000FD, SPEED_100000, DUPLEX_FULL },
  376. { MAC_56000FD, SPEED_56000, DUPLEX_FULL },
  377. { MAC_50000FD, SPEED_50000, DUPLEX_FULL },
  378. { MAC_40000FD, SPEED_40000, DUPLEX_FULL },
  379. { MAC_25000FD, SPEED_25000, DUPLEX_FULL },
  380. { MAC_20000FD, SPEED_20000, DUPLEX_FULL },
  381. { MAC_10000FD, SPEED_10000, DUPLEX_FULL },
  382. { MAC_5000FD, SPEED_5000, DUPLEX_FULL },
  383. { MAC_2500FD, SPEED_2500, DUPLEX_FULL },
  384. { MAC_1000FD, SPEED_1000, DUPLEX_FULL },
  385. { MAC_1000HD, SPEED_1000, DUPLEX_HALF },
  386. { MAC_100FD, SPEED_100, DUPLEX_FULL },
  387. { MAC_100HD, SPEED_100, DUPLEX_HALF },
  388. { MAC_10FD, SPEED_10, DUPLEX_FULL },
  389. { MAC_10HD, SPEED_10, DUPLEX_HALF },
  390. };
  391. /**
  392. * phylink_limit_mac_speed - limit the phylink_config to a maximum speed
  393. * @config: pointer to a &struct phylink_config
  394. * @max_speed: maximum speed
  395. *
  396. * Mask off MAC capabilities for speeds higher than the @max_speed parameter.
  397. * Any further motifications of config.mac_capabilities will override this.
  398. */
  399. void phylink_limit_mac_speed(struct phylink_config *config, u32 max_speed)
  400. {
  401. int i;
  402. for (i = 0; i < ARRAY_SIZE(phylink_caps_params) &&
  403. phylink_caps_params[i].speed > max_speed; i++)
  404. config->mac_capabilities &= ~phylink_caps_params[i].mask;
  405. }
  406. EXPORT_SYMBOL_GPL(phylink_limit_mac_speed);
  407. /**
  408. * phylink_cap_from_speed_duplex - Get mac capability from speed/duplex
  409. * @speed: the speed to search for
  410. * @duplex: the duplex to search for
  411. *
  412. * Find the mac capability for a given speed and duplex.
  413. *
  414. * Return: A mask with the mac capability patching @speed and @duplex, or 0 if
  415. * there were no matches.
  416. */
  417. static unsigned long phylink_cap_from_speed_duplex(int speed,
  418. unsigned int duplex)
  419. {
  420. int i;
  421. for (i = 0; i < ARRAY_SIZE(phylink_caps_params); i++) {
  422. if (speed == phylink_caps_params[i].speed &&
  423. duplex == phylink_caps_params[i].duplex)
  424. return phylink_caps_params[i].mask;
  425. }
  426. return 0;
  427. }
  428. /**
  429. * phylink_get_capabilities() - get capabilities for a given MAC
  430. * @interface: phy interface mode defined by &typedef phy_interface_t
  431. * @mac_capabilities: bitmask of MAC capabilities
  432. * @rate_matching: type of rate matching being performed
  433. *
  434. * Get the MAC capabilities that are supported by the @interface mode and
  435. * @mac_capabilities.
  436. */
  437. static unsigned long phylink_get_capabilities(phy_interface_t interface,
  438. unsigned long mac_capabilities,
  439. int rate_matching)
  440. {
  441. int max_speed = phylink_interface_max_speed(interface);
  442. unsigned long caps = MAC_SYM_PAUSE | MAC_ASYM_PAUSE;
  443. unsigned long matched_caps = 0;
  444. switch (interface) {
  445. case PHY_INTERFACE_MODE_USXGMII:
  446. caps |= MAC_10000FD | MAC_5000FD;
  447. fallthrough;
  448. case PHY_INTERFACE_MODE_10G_QXGMII:
  449. caps |= MAC_2500FD;
  450. fallthrough;
  451. case PHY_INTERFACE_MODE_RGMII_TXID:
  452. case PHY_INTERFACE_MODE_RGMII_RXID:
  453. case PHY_INTERFACE_MODE_RGMII_ID:
  454. case PHY_INTERFACE_MODE_RGMII:
  455. case PHY_INTERFACE_MODE_PSGMII:
  456. case PHY_INTERFACE_MODE_QSGMII:
  457. case PHY_INTERFACE_MODE_QUSGMII:
  458. case PHY_INTERFACE_MODE_SGMII:
  459. case PHY_INTERFACE_MODE_GMII:
  460. caps |= MAC_1000HD | MAC_1000FD;
  461. fallthrough;
  462. case PHY_INTERFACE_MODE_REVRMII:
  463. case PHY_INTERFACE_MODE_RMII:
  464. case PHY_INTERFACE_MODE_SMII:
  465. case PHY_INTERFACE_MODE_REVMII:
  466. case PHY_INTERFACE_MODE_MII:
  467. caps |= MAC_10HD | MAC_10FD;
  468. fallthrough;
  469. case PHY_INTERFACE_MODE_100BASEX:
  470. caps |= MAC_100HD | MAC_100FD;
  471. break;
  472. case PHY_INTERFACE_MODE_TBI:
  473. case PHY_INTERFACE_MODE_MOCA:
  474. case PHY_INTERFACE_MODE_RTBI:
  475. case PHY_INTERFACE_MODE_1000BASEX:
  476. caps |= MAC_1000HD;
  477. fallthrough;
  478. case PHY_INTERFACE_MODE_1000BASEKX:
  479. case PHY_INTERFACE_MODE_TRGMII:
  480. caps |= MAC_1000FD;
  481. break;
  482. case PHY_INTERFACE_MODE_2500BASEX:
  483. caps |= MAC_2500FD;
  484. break;
  485. case PHY_INTERFACE_MODE_5GBASER:
  486. caps |= MAC_5000FD;
  487. break;
  488. case PHY_INTERFACE_MODE_XGMII:
  489. case PHY_INTERFACE_MODE_RXAUI:
  490. case PHY_INTERFACE_MODE_XAUI:
  491. case PHY_INTERFACE_MODE_10GBASER:
  492. case PHY_INTERFACE_MODE_10GKR:
  493. caps |= MAC_10000FD;
  494. break;
  495. case PHY_INTERFACE_MODE_25GBASER:
  496. caps |= MAC_25000FD;
  497. break;
  498. case PHY_INTERFACE_MODE_XLGMII:
  499. caps |= MAC_40000FD;
  500. break;
  501. case PHY_INTERFACE_MODE_INTERNAL:
  502. caps |= ~0;
  503. break;
  504. case PHY_INTERFACE_MODE_NA:
  505. case PHY_INTERFACE_MODE_MAX:
  506. break;
  507. }
  508. switch (rate_matching) {
  509. case RATE_MATCH_OPEN_LOOP:
  510. /* TODO */
  511. fallthrough;
  512. case RATE_MATCH_NONE:
  513. matched_caps = 0;
  514. break;
  515. case RATE_MATCH_PAUSE: {
  516. /* The MAC must support asymmetric pause towards the local
  517. * device for this. We could allow just symmetric pause, but
  518. * then we might have to renegotiate if the link partner
  519. * doesn't support pause. This is because there's no way to
  520. * accept pause frames without transmitting them if we only
  521. * support symmetric pause.
  522. */
  523. if (!(mac_capabilities & MAC_SYM_PAUSE) ||
  524. !(mac_capabilities & MAC_ASYM_PAUSE))
  525. break;
  526. /* We can't adapt if the MAC doesn't support the interface's
  527. * max speed at full duplex.
  528. */
  529. if (mac_capabilities &
  530. phylink_cap_from_speed_duplex(max_speed, DUPLEX_FULL)) {
  531. /* Although a duplex-matching phy might exist, we
  532. * conservatively remove these modes because the MAC
  533. * will not be aware of the half-duplex nature of the
  534. * link.
  535. */
  536. matched_caps = GENMASK(__fls(caps), __fls(MAC_10HD));
  537. matched_caps &= ~(MAC_1000HD | MAC_100HD | MAC_10HD);
  538. }
  539. break;
  540. }
  541. case RATE_MATCH_CRS:
  542. /* The MAC must support half duplex at the interface's max
  543. * speed.
  544. */
  545. if (mac_capabilities &
  546. phylink_cap_from_speed_duplex(max_speed, DUPLEX_HALF)) {
  547. matched_caps = GENMASK(__fls(caps), __fls(MAC_10HD));
  548. matched_caps &= mac_capabilities;
  549. }
  550. break;
  551. }
  552. return (caps & mac_capabilities) | matched_caps;
  553. }
  554. /**
  555. * phylink_validate_mask_caps() - Restrict link modes based on caps
  556. * @supported: ethtool bitmask for supported link modes.
  557. * @state: pointer to a &struct phylink_link_state.
  558. * @mac_capabilities: bitmask of MAC capabilities
  559. *
  560. * Calculate the supported link modes based on @mac_capabilities, and restrict
  561. * @supported and @state based on that. Use this function if your capabiliies
  562. * aren't constant, such as if they vary depending on the interface.
  563. */
  564. static void phylink_validate_mask_caps(unsigned long *supported,
  565. struct phylink_link_state *state,
  566. unsigned long mac_capabilities)
  567. {
  568. __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
  569. unsigned long caps;
  570. phylink_set_port_modes(mask);
  571. phylink_set(mask, Autoneg);
  572. caps = phylink_get_capabilities(state->interface, mac_capabilities,
  573. state->rate_matching);
  574. phylink_caps_to_linkmodes(mask, caps);
  575. linkmode_and(supported, supported, mask);
  576. linkmode_and(state->advertising, state->advertising, mask);
  577. }
  578. static int phylink_validate_mac_and_pcs(struct phylink *pl,
  579. unsigned long *supported,
  580. struct phylink_link_state *state)
  581. {
  582. unsigned long capabilities;
  583. struct phylink_pcs *pcs;
  584. int ret;
  585. /* Get the PCS for this interface mode */
  586. if (pl->using_mac_select_pcs) {
  587. pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
  588. if (IS_ERR(pcs))
  589. return PTR_ERR(pcs);
  590. } else {
  591. pcs = pl->pcs;
  592. }
  593. if (pcs) {
  594. /* The PCS, if present, must be setup before phylink_create()
  595. * has been called. If the ops is not initialised, print an
  596. * error and backtrace rather than oopsing the kernel.
  597. */
  598. if (!pcs->ops) {
  599. phylink_err(pl, "interface %s: uninitialised PCS\n",
  600. phy_modes(state->interface));
  601. dump_stack();
  602. return -EINVAL;
  603. }
  604. /* Validate the link parameters with the PCS */
  605. if (pcs->ops->pcs_validate) {
  606. ret = pcs->ops->pcs_validate(pcs, supported, state);
  607. if (ret < 0 || phylink_is_empty_linkmode(supported))
  608. return -EINVAL;
  609. /* Ensure the advertising mask is a subset of the
  610. * supported mask.
  611. */
  612. linkmode_and(state->advertising, state->advertising,
  613. supported);
  614. }
  615. }
  616. /* Then validate the link parameters with the MAC */
  617. if (pl->mac_ops->mac_get_caps)
  618. capabilities = pl->mac_ops->mac_get_caps(pl->config,
  619. state->interface);
  620. else
  621. capabilities = pl->config->mac_capabilities;
  622. phylink_validate_mask_caps(supported, state, capabilities);
  623. return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
  624. }
  625. static void phylink_validate_one(struct phylink *pl, struct phy_device *phy,
  626. const unsigned long *supported,
  627. const struct phylink_link_state *state,
  628. phy_interface_t interface,
  629. unsigned long *accum_supported,
  630. unsigned long *accum_advertising)
  631. {
  632. __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp_supported);
  633. struct phylink_link_state tmp_state;
  634. linkmode_copy(tmp_supported, supported);
  635. tmp_state = *state;
  636. tmp_state.interface = interface;
  637. if (phy)
  638. tmp_state.rate_matching = phy_get_rate_matching(phy, interface);
  639. if (!phylink_validate_mac_and_pcs(pl, tmp_supported, &tmp_state)) {
  640. phylink_dbg(pl, " interface %u (%s) rate match %s supports %*pbl\n",
  641. interface, phy_modes(interface),
  642. phy_rate_matching_to_str(tmp_state.rate_matching),
  643. __ETHTOOL_LINK_MODE_MASK_NBITS, tmp_supported);
  644. linkmode_or(accum_supported, accum_supported, tmp_supported);
  645. linkmode_or(accum_advertising, accum_advertising,
  646. tmp_state.advertising);
  647. }
  648. }
  649. static int phylink_validate_mask(struct phylink *pl, struct phy_device *phy,
  650. unsigned long *supported,
  651. struct phylink_link_state *state,
  652. const unsigned long *interfaces)
  653. {
  654. __ETHTOOL_DECLARE_LINK_MODE_MASK(all_adv) = { 0, };
  655. __ETHTOOL_DECLARE_LINK_MODE_MASK(all_s) = { 0, };
  656. int interface;
  657. for_each_set_bit(interface, interfaces, PHY_INTERFACE_MODE_MAX)
  658. phylink_validate_one(pl, phy, supported, state, interface,
  659. all_s, all_adv);
  660. linkmode_copy(supported, all_s);
  661. linkmode_copy(state->advertising, all_adv);
  662. return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
  663. }
  664. static int phylink_validate(struct phylink *pl, unsigned long *supported,
  665. struct phylink_link_state *state)
  666. {
  667. const unsigned long *interfaces = pl->config->supported_interfaces;
  668. if (state->interface == PHY_INTERFACE_MODE_NA)
  669. return phylink_validate_mask(pl, NULL, supported, state,
  670. interfaces);
  671. if (!test_bit(state->interface, interfaces))
  672. return -EINVAL;
  673. return phylink_validate_mac_and_pcs(pl, supported, state);
  674. }
  675. static int phylink_parse_fixedlink(struct phylink *pl,
  676. const struct fwnode_handle *fwnode)
  677. {
  678. struct fwnode_handle *fixed_node;
  679. bool pause, asym_pause, autoneg;
  680. const struct phy_setting *s;
  681. struct gpio_desc *desc;
  682. u32 speed;
  683. int ret;
  684. fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
  685. if (fixed_node) {
  686. ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
  687. pl->link_config.speed = speed;
  688. pl->link_config.duplex = DUPLEX_HALF;
  689. if (fwnode_property_read_bool(fixed_node, "full-duplex"))
  690. pl->link_config.duplex = DUPLEX_FULL;
  691. /* We treat the "pause" and "asym-pause" terminology as
  692. * defining the link partner's ability.
  693. */
  694. if (fwnode_property_read_bool(fixed_node, "pause"))
  695. __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  696. pl->link_config.lp_advertising);
  697. if (fwnode_property_read_bool(fixed_node, "asym-pause"))
  698. __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  699. pl->link_config.lp_advertising);
  700. if (ret == 0) {
  701. desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
  702. GPIOD_IN, "?");
  703. if (!IS_ERR(desc))
  704. pl->link_gpio = desc;
  705. else if (desc == ERR_PTR(-EPROBE_DEFER))
  706. ret = -EPROBE_DEFER;
  707. }
  708. fwnode_handle_put(fixed_node);
  709. if (ret)
  710. return ret;
  711. } else {
  712. u32 prop[5];
  713. ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
  714. NULL, 0);
  715. if (ret != ARRAY_SIZE(prop)) {
  716. phylink_err(pl, "broken fixed-link?\n");
  717. return -EINVAL;
  718. }
  719. ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
  720. prop, ARRAY_SIZE(prop));
  721. if (!ret) {
  722. pl->link_config.duplex = prop[1] ?
  723. DUPLEX_FULL : DUPLEX_HALF;
  724. pl->link_config.speed = prop[2];
  725. if (prop[3])
  726. __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  727. pl->link_config.lp_advertising);
  728. if (prop[4])
  729. __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  730. pl->link_config.lp_advertising);
  731. }
  732. }
  733. if (pl->link_config.speed > SPEED_1000 &&
  734. pl->link_config.duplex != DUPLEX_FULL)
  735. phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
  736. pl->link_config.speed);
  737. linkmode_fill(pl->supported);
  738. linkmode_copy(pl->link_config.advertising, pl->supported);
  739. phylink_validate(pl, pl->supported, &pl->link_config);
  740. pause = phylink_test(pl->supported, Pause);
  741. asym_pause = phylink_test(pl->supported, Asym_Pause);
  742. autoneg = phylink_test(pl->supported, Autoneg);
  743. s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
  744. pl->supported, true);
  745. linkmode_zero(pl->supported);
  746. phylink_set(pl->supported, MII);
  747. if (pause)
  748. phylink_set(pl->supported, Pause);
  749. if (asym_pause)
  750. phylink_set(pl->supported, Asym_Pause);
  751. if (autoneg)
  752. phylink_set(pl->supported, Autoneg);
  753. if (s) {
  754. __set_bit(s->bit, pl->supported);
  755. __set_bit(s->bit, pl->link_config.lp_advertising);
  756. } else {
  757. phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
  758. pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
  759. pl->link_config.speed);
  760. }
  761. linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
  762. pl->supported);
  763. pl->link_config.link = 1;
  764. pl->link_config.an_complete = 1;
  765. return 0;
  766. }
  767. static int phylink_parse_mode(struct phylink *pl,
  768. const struct fwnode_handle *fwnode)
  769. {
  770. struct fwnode_handle *dn;
  771. const char *managed;
  772. unsigned long caps;
  773. if (pl->config->default_an_inband)
  774. pl->cfg_link_an_mode = MLO_AN_INBAND;
  775. dn = fwnode_get_named_child_node(fwnode, "fixed-link");
  776. if (dn || fwnode_property_present(fwnode, "fixed-link"))
  777. pl->cfg_link_an_mode = MLO_AN_FIXED;
  778. fwnode_handle_put(dn);
  779. if ((fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
  780. strcmp(managed, "in-band-status") == 0)) {
  781. if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
  782. phylink_err(pl,
  783. "can't use both fixed-link and in-band-status\n");
  784. return -EINVAL;
  785. }
  786. pl->cfg_link_an_mode = MLO_AN_INBAND;
  787. }
  788. if (pl->cfg_link_an_mode == MLO_AN_INBAND) {
  789. linkmode_zero(pl->supported);
  790. phylink_set(pl->supported, MII);
  791. phylink_set(pl->supported, Autoneg);
  792. phylink_set(pl->supported, Asym_Pause);
  793. phylink_set(pl->supported, Pause);
  794. switch (pl->link_config.interface) {
  795. case PHY_INTERFACE_MODE_SGMII:
  796. case PHY_INTERFACE_MODE_PSGMII:
  797. case PHY_INTERFACE_MODE_QSGMII:
  798. case PHY_INTERFACE_MODE_QUSGMII:
  799. case PHY_INTERFACE_MODE_RGMII:
  800. case PHY_INTERFACE_MODE_RGMII_ID:
  801. case PHY_INTERFACE_MODE_RGMII_RXID:
  802. case PHY_INTERFACE_MODE_RGMII_TXID:
  803. case PHY_INTERFACE_MODE_RTBI:
  804. case PHY_INTERFACE_MODE_1000BASEX:
  805. case PHY_INTERFACE_MODE_2500BASEX:
  806. case PHY_INTERFACE_MODE_5GBASER:
  807. case PHY_INTERFACE_MODE_25GBASER:
  808. case PHY_INTERFACE_MODE_USXGMII:
  809. case PHY_INTERFACE_MODE_10G_QXGMII:
  810. case PHY_INTERFACE_MODE_10GKR:
  811. case PHY_INTERFACE_MODE_10GBASER:
  812. case PHY_INTERFACE_MODE_XLGMII:
  813. caps = ~(MAC_SYM_PAUSE | MAC_ASYM_PAUSE);
  814. caps = phylink_get_capabilities(pl->link_config.interface, caps,
  815. RATE_MATCH_NONE);
  816. phylink_caps_to_linkmodes(pl->supported, caps);
  817. break;
  818. default:
  819. phylink_err(pl,
  820. "incorrect link mode %s for in-band status\n",
  821. phy_modes(pl->link_config.interface));
  822. return -EINVAL;
  823. }
  824. linkmode_copy(pl->link_config.advertising, pl->supported);
  825. if (phylink_validate(pl, pl->supported, &pl->link_config)) {
  826. phylink_err(pl,
  827. "failed to validate link configuration for in-band status\n");
  828. return -EINVAL;
  829. }
  830. }
  831. return 0;
  832. }
  833. static void phylink_apply_manual_flow(struct phylink *pl,
  834. struct phylink_link_state *state)
  835. {
  836. /* If autoneg is disabled, pause AN is also disabled */
  837. if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  838. state->advertising))
  839. state->pause &= ~MLO_PAUSE_AN;
  840. /* Manual configuration of pause modes */
  841. if (!(pl->link_config.pause & MLO_PAUSE_AN))
  842. state->pause = pl->link_config.pause;
  843. }
  844. static void phylink_resolve_an_pause(struct phylink_link_state *state)
  845. {
  846. bool tx_pause, rx_pause;
  847. if (state->duplex == DUPLEX_FULL) {
  848. linkmode_resolve_pause(state->advertising,
  849. state->lp_advertising,
  850. &tx_pause, &rx_pause);
  851. if (tx_pause)
  852. state->pause |= MLO_PAUSE_TX;
  853. if (rx_pause)
  854. state->pause |= MLO_PAUSE_RX;
  855. }
  856. }
  857. static void phylink_pcs_pre_config(struct phylink_pcs *pcs,
  858. phy_interface_t interface)
  859. {
  860. if (pcs && pcs->ops->pcs_pre_config)
  861. pcs->ops->pcs_pre_config(pcs, interface);
  862. }
  863. static int phylink_pcs_post_config(struct phylink_pcs *pcs,
  864. phy_interface_t interface)
  865. {
  866. int err = 0;
  867. if (pcs && pcs->ops->pcs_post_config)
  868. err = pcs->ops->pcs_post_config(pcs, interface);
  869. return err;
  870. }
  871. static void phylink_pcs_disable(struct phylink_pcs *pcs)
  872. {
  873. if (pcs && pcs->ops->pcs_disable)
  874. pcs->ops->pcs_disable(pcs);
  875. }
  876. static int phylink_pcs_enable(struct phylink_pcs *pcs)
  877. {
  878. int err = 0;
  879. if (pcs && pcs->ops->pcs_enable)
  880. err = pcs->ops->pcs_enable(pcs);
  881. return err;
  882. }
  883. static int phylink_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
  884. const struct phylink_link_state *state,
  885. bool permit_pause_to_mac)
  886. {
  887. if (!pcs)
  888. return 0;
  889. return pcs->ops->pcs_config(pcs, neg_mode, state->interface,
  890. state->advertising, permit_pause_to_mac);
  891. }
  892. static void phylink_pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
  893. phy_interface_t interface, int speed,
  894. int duplex)
  895. {
  896. if (pcs && pcs->ops->pcs_link_up)
  897. pcs->ops->pcs_link_up(pcs, neg_mode, interface, speed, duplex);
  898. }
  899. static void phylink_pcs_poll_stop(struct phylink *pl)
  900. {
  901. if (pl->cfg_link_an_mode == MLO_AN_INBAND)
  902. del_timer(&pl->link_poll);
  903. }
  904. static void phylink_pcs_poll_start(struct phylink *pl)
  905. {
  906. if (pl->pcs && pl->pcs->poll && pl->cfg_link_an_mode == MLO_AN_INBAND)
  907. mod_timer(&pl->link_poll, jiffies + HZ);
  908. }
  909. int phylink_pcs_pre_init(struct phylink *pl, struct phylink_pcs *pcs)
  910. {
  911. int ret = 0;
  912. /* Signal to PCS driver that MAC requires RX clock for init */
  913. if (pl->config->mac_requires_rxc)
  914. pcs->rxc_always_on = true;
  915. if (pcs->ops->pcs_pre_init)
  916. ret = pcs->ops->pcs_pre_init(pcs);
  917. return ret;
  918. }
  919. EXPORT_SYMBOL_GPL(phylink_pcs_pre_init);
  920. static void phylink_mac_config(struct phylink *pl,
  921. const struct phylink_link_state *state)
  922. {
  923. struct phylink_link_state st = *state;
  924. /* Stop drivers incorrectly using these */
  925. linkmode_zero(st.lp_advertising);
  926. st.speed = SPEED_UNKNOWN;
  927. st.duplex = DUPLEX_UNKNOWN;
  928. st.an_complete = false;
  929. st.link = false;
  930. phylink_dbg(pl,
  931. "%s: mode=%s/%s/%s adv=%*pb pause=%02x\n",
  932. __func__, phylink_an_mode_str(pl->cur_link_an_mode),
  933. phy_modes(st.interface),
  934. phy_rate_matching_to_str(st.rate_matching),
  935. __ETHTOOL_LINK_MODE_MASK_NBITS, st.advertising,
  936. st.pause);
  937. pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, &st);
  938. }
  939. static void phylink_pcs_an_restart(struct phylink *pl)
  940. {
  941. if (pl->pcs && linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  942. pl->link_config.advertising) &&
  943. phy_interface_mode_is_8023z(pl->link_config.interface) &&
  944. phylink_autoneg_inband(pl->cur_link_an_mode))
  945. pl->pcs->ops->pcs_an_restart(pl->pcs);
  946. }
  947. /**
  948. * phylink_pcs_neg_mode() - helper to determine PCS inband mode
  949. * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
  950. * @interface: interface mode to be used
  951. * @advertising: adertisement ethtool link mode mask
  952. *
  953. * Determines the negotiation mode to be used by the PCS, and returns
  954. * one of:
  955. *
  956. * - %PHYLINK_PCS_NEG_NONE: interface mode does not support inband
  957. * - %PHYLINK_PCS_NEG_OUTBAND: an out of band mode (e.g. reading the PHY)
  958. * will be used.
  959. * - %PHYLINK_PCS_NEG_INBAND_DISABLED: inband mode selected but autoneg
  960. * disabled
  961. * - %PHYLINK_PCS_NEG_INBAND_ENABLED: inband mode selected and autoneg enabled
  962. *
  963. * Note: this is for cases where the PCS itself is involved in negotiation
  964. * (e.g. Clause 37, SGMII and similar) not Clause 73.
  965. */
  966. static unsigned int phylink_pcs_neg_mode(unsigned int mode,
  967. phy_interface_t interface,
  968. const unsigned long *advertising)
  969. {
  970. unsigned int neg_mode;
  971. switch (interface) {
  972. case PHY_INTERFACE_MODE_SGMII:
  973. case PHY_INTERFACE_MODE_QSGMII:
  974. case PHY_INTERFACE_MODE_QUSGMII:
  975. case PHY_INTERFACE_MODE_USXGMII:
  976. case PHY_INTERFACE_MODE_10G_QXGMII:
  977. /* These protocols are designed for use with a PHY which
  978. * communicates its negotiation result back to the MAC via
  979. * inband communication. Note: there exist PHYs that run
  980. * with SGMII but do not send the inband data.
  981. */
  982. if (!phylink_autoneg_inband(mode))
  983. neg_mode = PHYLINK_PCS_NEG_OUTBAND;
  984. else
  985. neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED;
  986. break;
  987. case PHY_INTERFACE_MODE_1000BASEX:
  988. case PHY_INTERFACE_MODE_2500BASEX:
  989. /* 1000base-X is designed for use media-side for Fibre
  990. * connections, and thus the Autoneg bit needs to be
  991. * taken into account. We also do this for 2500base-X
  992. * as well, but drivers may not support this, so may
  993. * need to override this.
  994. */
  995. if (!phylink_autoneg_inband(mode))
  996. neg_mode = PHYLINK_PCS_NEG_OUTBAND;
  997. else if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  998. advertising))
  999. neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED;
  1000. else
  1001. neg_mode = PHYLINK_PCS_NEG_INBAND_DISABLED;
  1002. break;
  1003. default:
  1004. neg_mode = PHYLINK_PCS_NEG_NONE;
  1005. break;
  1006. }
  1007. return neg_mode;
  1008. }
  1009. static void phylink_major_config(struct phylink *pl, bool restart,
  1010. const struct phylink_link_state *state)
  1011. {
  1012. struct phylink_pcs *pcs = NULL;
  1013. bool pcs_changed = false;
  1014. unsigned int rate_kbd;
  1015. unsigned int neg_mode;
  1016. int err;
  1017. phylink_dbg(pl, "major config %s\n", phy_modes(state->interface));
  1018. pl->pcs_neg_mode = phylink_pcs_neg_mode(pl->cur_link_an_mode,
  1019. state->interface,
  1020. state->advertising);
  1021. if (pl->using_mac_select_pcs) {
  1022. pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
  1023. if (IS_ERR(pcs)) {
  1024. phylink_err(pl,
  1025. "mac_select_pcs unexpectedly failed: %pe\n",
  1026. pcs);
  1027. return;
  1028. }
  1029. pcs_changed = pcs && pl->pcs != pcs;
  1030. }
  1031. phylink_pcs_poll_stop(pl);
  1032. if (pl->mac_ops->mac_prepare) {
  1033. err = pl->mac_ops->mac_prepare(pl->config, pl->cur_link_an_mode,
  1034. state->interface);
  1035. if (err < 0) {
  1036. phylink_err(pl, "mac_prepare failed: %pe\n",
  1037. ERR_PTR(err));
  1038. return;
  1039. }
  1040. }
  1041. /* If we have a new PCS, switch to the new PCS after preparing the MAC
  1042. * for the change.
  1043. */
  1044. if (pcs_changed) {
  1045. phylink_pcs_disable(pl->pcs);
  1046. if (pl->pcs)
  1047. pl->pcs->phylink = NULL;
  1048. pcs->phylink = pl;
  1049. pl->pcs = pcs;
  1050. }
  1051. if (pl->pcs)
  1052. phylink_pcs_pre_config(pl->pcs, state->interface);
  1053. phylink_mac_config(pl, state);
  1054. if (pl->pcs)
  1055. phylink_pcs_post_config(pl->pcs, state->interface);
  1056. if (pl->pcs_state == PCS_STATE_STARTING || pcs_changed)
  1057. phylink_pcs_enable(pl->pcs);
  1058. neg_mode = pl->cur_link_an_mode;
  1059. if (pl->pcs && pl->pcs->neg_mode)
  1060. neg_mode = pl->pcs_neg_mode;
  1061. err = phylink_pcs_config(pl->pcs, neg_mode, state,
  1062. !!(pl->link_config.pause & MLO_PAUSE_AN));
  1063. if (err < 0)
  1064. phylink_err(pl, "pcs_config failed: %pe\n",
  1065. ERR_PTR(err));
  1066. else if (err > 0)
  1067. restart = true;
  1068. if (restart)
  1069. phylink_pcs_an_restart(pl);
  1070. if (pl->mac_ops->mac_finish) {
  1071. err = pl->mac_ops->mac_finish(pl->config, pl->cur_link_an_mode,
  1072. state->interface);
  1073. if (err < 0)
  1074. phylink_err(pl, "mac_finish failed: %pe\n",
  1075. ERR_PTR(err));
  1076. }
  1077. if (pl->sfp_bus) {
  1078. rate_kbd = phylink_interface_signal_rate(state->interface);
  1079. if (rate_kbd)
  1080. sfp_upstream_set_signal_rate(pl->sfp_bus, rate_kbd);
  1081. }
  1082. phylink_pcs_poll_start(pl);
  1083. }
  1084. /*
  1085. * Reconfigure for a change of inband advertisement.
  1086. * If we have a separate PCS, we only need to call its pcs_config() method,
  1087. * and then restart AN if it indicates something changed. Otherwise, we do
  1088. * the full MAC reconfiguration.
  1089. */
  1090. static int phylink_change_inband_advert(struct phylink *pl)
  1091. {
  1092. unsigned int neg_mode;
  1093. int ret;
  1094. if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
  1095. return 0;
  1096. phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
  1097. phylink_an_mode_str(pl->cur_link_an_mode),
  1098. phy_modes(pl->link_config.interface),
  1099. __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
  1100. pl->link_config.pause);
  1101. /* Recompute the PCS neg mode */
  1102. pl->pcs_neg_mode = phylink_pcs_neg_mode(pl->cur_link_an_mode,
  1103. pl->link_config.interface,
  1104. pl->link_config.advertising);
  1105. neg_mode = pl->cur_link_an_mode;
  1106. if (pl->pcs->neg_mode)
  1107. neg_mode = pl->pcs_neg_mode;
  1108. /* Modern PCS-based method; update the advert at the PCS, and
  1109. * restart negotiation if the pcs_config() helper indicates that
  1110. * the programmed advertisement has changed.
  1111. */
  1112. ret = phylink_pcs_config(pl->pcs, neg_mode, &pl->link_config,
  1113. !!(pl->link_config.pause & MLO_PAUSE_AN));
  1114. if (ret < 0)
  1115. return ret;
  1116. if (ret > 0)
  1117. phylink_pcs_an_restart(pl);
  1118. return 0;
  1119. }
  1120. static void phylink_mac_pcs_get_state(struct phylink *pl,
  1121. struct phylink_link_state *state)
  1122. {
  1123. linkmode_copy(state->advertising, pl->link_config.advertising);
  1124. linkmode_zero(state->lp_advertising);
  1125. state->interface = pl->link_config.interface;
  1126. state->rate_matching = pl->link_config.rate_matching;
  1127. if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  1128. state->advertising)) {
  1129. state->speed = SPEED_UNKNOWN;
  1130. state->duplex = DUPLEX_UNKNOWN;
  1131. state->pause = MLO_PAUSE_NONE;
  1132. } else {
  1133. state->speed = pl->link_config.speed;
  1134. state->duplex = pl->link_config.duplex;
  1135. state->pause = pl->link_config.pause;
  1136. }
  1137. state->an_complete = 0;
  1138. state->link = 1;
  1139. if (pl->pcs)
  1140. pl->pcs->ops->pcs_get_state(pl->pcs, state);
  1141. else
  1142. state->link = 0;
  1143. }
  1144. /* The fixed state is... fixed except for the link state,
  1145. * which may be determined by a GPIO or a callback.
  1146. */
  1147. static void phylink_get_fixed_state(struct phylink *pl,
  1148. struct phylink_link_state *state)
  1149. {
  1150. *state = pl->link_config;
  1151. if (pl->config->get_fixed_state)
  1152. pl->config->get_fixed_state(pl->config, state);
  1153. else if (pl->link_gpio)
  1154. state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
  1155. state->pause = MLO_PAUSE_NONE;
  1156. phylink_resolve_an_pause(state);
  1157. }
  1158. static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
  1159. {
  1160. struct phylink_link_state link_state;
  1161. switch (pl->cur_link_an_mode) {
  1162. case MLO_AN_PHY:
  1163. link_state = pl->phy_state;
  1164. break;
  1165. case MLO_AN_FIXED:
  1166. phylink_get_fixed_state(pl, &link_state);
  1167. break;
  1168. case MLO_AN_INBAND:
  1169. link_state = pl->link_config;
  1170. if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
  1171. link_state.pause = MLO_PAUSE_NONE;
  1172. break;
  1173. default: /* can't happen */
  1174. return;
  1175. }
  1176. link_state.link = false;
  1177. phylink_apply_manual_flow(pl, &link_state);
  1178. phylink_major_config(pl, force_restart, &link_state);
  1179. }
  1180. static const char *phylink_pause_to_str(int pause)
  1181. {
  1182. switch (pause & MLO_PAUSE_TXRX_MASK) {
  1183. case MLO_PAUSE_TX | MLO_PAUSE_RX:
  1184. return "rx/tx";
  1185. case MLO_PAUSE_TX:
  1186. return "tx";
  1187. case MLO_PAUSE_RX:
  1188. return "rx";
  1189. default:
  1190. return "off";
  1191. }
  1192. }
  1193. static void phylink_link_up(struct phylink *pl,
  1194. struct phylink_link_state link_state)
  1195. {
  1196. struct net_device *ndev = pl->netdev;
  1197. unsigned int neg_mode;
  1198. int speed, duplex;
  1199. bool rx_pause;
  1200. speed = link_state.speed;
  1201. duplex = link_state.duplex;
  1202. rx_pause = !!(link_state.pause & MLO_PAUSE_RX);
  1203. switch (link_state.rate_matching) {
  1204. case RATE_MATCH_PAUSE:
  1205. /* The PHY is doing rate matchion from the media rate (in
  1206. * the link_state) to the interface speed, and will send
  1207. * pause frames to the MAC to limit its transmission speed.
  1208. */
  1209. speed = phylink_interface_max_speed(link_state.interface);
  1210. duplex = DUPLEX_FULL;
  1211. rx_pause = true;
  1212. break;
  1213. case RATE_MATCH_CRS:
  1214. /* The PHY is doing rate matchion from the media rate (in
  1215. * the link_state) to the interface speed, and will cause
  1216. * collisions to the MAC to limit its transmission speed.
  1217. */
  1218. speed = phylink_interface_max_speed(link_state.interface);
  1219. duplex = DUPLEX_HALF;
  1220. break;
  1221. }
  1222. pl->cur_interface = link_state.interface;
  1223. neg_mode = pl->cur_link_an_mode;
  1224. if (pl->pcs && pl->pcs->neg_mode)
  1225. neg_mode = pl->pcs_neg_mode;
  1226. phylink_pcs_link_up(pl->pcs, neg_mode, pl->cur_interface, speed,
  1227. duplex);
  1228. pl->mac_ops->mac_link_up(pl->config, pl->phydev, pl->cur_link_an_mode,
  1229. pl->cur_interface, speed, duplex,
  1230. !!(link_state.pause & MLO_PAUSE_TX), rx_pause);
  1231. if (ndev)
  1232. netif_carrier_on(ndev);
  1233. phylink_info(pl,
  1234. "Link is Up - %s/%s - flow control %s\n",
  1235. phy_speed_to_str(link_state.speed),
  1236. phy_duplex_to_str(link_state.duplex),
  1237. phylink_pause_to_str(link_state.pause));
  1238. }
  1239. static void phylink_link_down(struct phylink *pl)
  1240. {
  1241. struct net_device *ndev = pl->netdev;
  1242. if (ndev)
  1243. netif_carrier_off(ndev);
  1244. pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode,
  1245. pl->cur_interface);
  1246. phylink_info(pl, "Link is Down\n");
  1247. }
  1248. static void phylink_resolve(struct work_struct *w)
  1249. {
  1250. struct phylink *pl = container_of(w, struct phylink, resolve);
  1251. struct phylink_link_state link_state;
  1252. struct net_device *ndev = pl->netdev;
  1253. bool mac_config = false;
  1254. bool retrigger = false;
  1255. bool cur_link_state;
  1256. mutex_lock(&pl->state_mutex);
  1257. if (pl->netdev)
  1258. cur_link_state = netif_carrier_ok(ndev);
  1259. else
  1260. cur_link_state = pl->old_link_state;
  1261. if (pl->phylink_disable_state) {
  1262. pl->link_failed = false;
  1263. link_state.link = false;
  1264. } else if (pl->link_failed) {
  1265. link_state.link = false;
  1266. retrigger = true;
  1267. } else {
  1268. switch (pl->cur_link_an_mode) {
  1269. case MLO_AN_PHY:
  1270. link_state = pl->phy_state;
  1271. phylink_apply_manual_flow(pl, &link_state);
  1272. mac_config = link_state.link;
  1273. break;
  1274. case MLO_AN_FIXED:
  1275. phylink_get_fixed_state(pl, &link_state);
  1276. mac_config = link_state.link;
  1277. break;
  1278. case MLO_AN_INBAND:
  1279. phylink_mac_pcs_get_state(pl, &link_state);
  1280. /* The PCS may have a latching link-fail indicator.
  1281. * If the link was up, bring the link down and
  1282. * re-trigger the resolve. Otherwise, re-read the
  1283. * PCS state to get the current status of the link.
  1284. */
  1285. if (!link_state.link) {
  1286. if (cur_link_state)
  1287. retrigger = true;
  1288. else
  1289. phylink_mac_pcs_get_state(pl,
  1290. &link_state);
  1291. }
  1292. /* If we have a phy, the "up" state is the union of
  1293. * both the PHY and the MAC
  1294. */
  1295. if (pl->phydev)
  1296. link_state.link &= pl->phy_state.link;
  1297. /* Only update if the PHY link is up */
  1298. if (pl->phydev && pl->phy_state.link) {
  1299. /* If the interface has changed, force a
  1300. * link down event if the link isn't already
  1301. * down, and re-resolve.
  1302. */
  1303. if (link_state.interface !=
  1304. pl->phy_state.interface) {
  1305. retrigger = true;
  1306. link_state.link = false;
  1307. }
  1308. link_state.interface = pl->phy_state.interface;
  1309. /* If we are doing rate matching, then the
  1310. * link speed/duplex comes from the PHY
  1311. */
  1312. if (pl->phy_state.rate_matching) {
  1313. link_state.rate_matching =
  1314. pl->phy_state.rate_matching;
  1315. link_state.speed = pl->phy_state.speed;
  1316. link_state.duplex =
  1317. pl->phy_state.duplex;
  1318. }
  1319. /* If we have a PHY, we need to update with
  1320. * the PHY flow control bits.
  1321. */
  1322. link_state.pause = pl->phy_state.pause;
  1323. mac_config = true;
  1324. }
  1325. phylink_apply_manual_flow(pl, &link_state);
  1326. break;
  1327. }
  1328. }
  1329. if (mac_config) {
  1330. if (link_state.interface != pl->link_config.interface) {
  1331. /* The interface has changed, force the link down and
  1332. * then reconfigure.
  1333. */
  1334. if (cur_link_state) {
  1335. phylink_link_down(pl);
  1336. cur_link_state = false;
  1337. }
  1338. phylink_major_config(pl, false, &link_state);
  1339. pl->link_config.interface = link_state.interface;
  1340. }
  1341. }
  1342. if (link_state.link != cur_link_state) {
  1343. pl->old_link_state = link_state.link;
  1344. if (!link_state.link)
  1345. phylink_link_down(pl);
  1346. else
  1347. phylink_link_up(pl, link_state);
  1348. }
  1349. if (!link_state.link && retrigger) {
  1350. pl->link_failed = false;
  1351. queue_work(system_power_efficient_wq, &pl->resolve);
  1352. }
  1353. mutex_unlock(&pl->state_mutex);
  1354. }
  1355. static void phylink_run_resolve(struct phylink *pl)
  1356. {
  1357. if (!pl->phylink_disable_state)
  1358. queue_work(system_power_efficient_wq, &pl->resolve);
  1359. }
  1360. static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
  1361. {
  1362. unsigned long state = pl->phylink_disable_state;
  1363. set_bit(bit, &pl->phylink_disable_state);
  1364. if (state == 0) {
  1365. queue_work(system_power_efficient_wq, &pl->resolve);
  1366. flush_work(&pl->resolve);
  1367. }
  1368. }
  1369. static void phylink_enable_and_run_resolve(struct phylink *pl, int bit)
  1370. {
  1371. clear_bit(bit, &pl->phylink_disable_state);
  1372. phylink_run_resolve(pl);
  1373. }
  1374. static void phylink_fixed_poll(struct timer_list *t)
  1375. {
  1376. struct phylink *pl = container_of(t, struct phylink, link_poll);
  1377. mod_timer(t, jiffies + HZ);
  1378. phylink_run_resolve(pl);
  1379. }
  1380. static const struct sfp_upstream_ops sfp_phylink_ops;
  1381. static int phylink_register_sfp(struct phylink *pl,
  1382. const struct fwnode_handle *fwnode)
  1383. {
  1384. struct sfp_bus *bus;
  1385. int ret;
  1386. if (!fwnode)
  1387. return 0;
  1388. bus = sfp_bus_find_fwnode(fwnode);
  1389. if (IS_ERR(bus)) {
  1390. phylink_err(pl, "unable to attach SFP bus: %pe\n", bus);
  1391. return PTR_ERR(bus);
  1392. }
  1393. pl->sfp_bus = bus;
  1394. ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
  1395. sfp_bus_put(bus);
  1396. return ret;
  1397. }
  1398. /**
  1399. * phylink_set_fixed_link() - set the fixed link
  1400. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1401. * @state: a pointer to a struct phylink_link_state.
  1402. *
  1403. * This function is used when the link parameters are known and do not change,
  1404. * making it suitable for certain types of network connections.
  1405. *
  1406. * Returns: zero on success or negative error code.
  1407. */
  1408. int phylink_set_fixed_link(struct phylink *pl,
  1409. const struct phylink_link_state *state)
  1410. {
  1411. const struct phy_setting *s;
  1412. unsigned long *adv;
  1413. if (pl->cfg_link_an_mode != MLO_AN_PHY || !state ||
  1414. !test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
  1415. return -EINVAL;
  1416. s = phy_lookup_setting(state->speed, state->duplex,
  1417. pl->supported, true);
  1418. if (!s)
  1419. return -EINVAL;
  1420. adv = pl->link_config.advertising;
  1421. linkmode_zero(adv);
  1422. linkmode_set_bit(s->bit, adv);
  1423. linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, adv);
  1424. pl->link_config.speed = state->speed;
  1425. pl->link_config.duplex = state->duplex;
  1426. pl->link_config.link = 1;
  1427. pl->link_config.an_complete = 1;
  1428. pl->cfg_link_an_mode = MLO_AN_FIXED;
  1429. pl->cur_link_an_mode = pl->cfg_link_an_mode;
  1430. return 0;
  1431. }
  1432. EXPORT_SYMBOL_GPL(phylink_set_fixed_link);
  1433. /**
  1434. * phylink_create() - create a phylink instance
  1435. * @config: a pointer to the target &struct phylink_config
  1436. * @fwnode: a pointer to a &struct fwnode_handle describing the network
  1437. * interface
  1438. * @iface: the desired link mode defined by &typedef phy_interface_t
  1439. * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
  1440. *
  1441. * Create a new phylink instance, and parse the link parameters found in @np.
  1442. * This will parse in-band modes, fixed-link or SFP configuration.
  1443. *
  1444. * Note: the rtnl lock must not be held when calling this function.
  1445. *
  1446. * Returns a pointer to a &struct phylink, or an error-pointer value. Users
  1447. * must use IS_ERR() to check for errors from this function.
  1448. */
  1449. struct phylink *phylink_create(struct phylink_config *config,
  1450. const struct fwnode_handle *fwnode,
  1451. phy_interface_t iface,
  1452. const struct phylink_mac_ops *mac_ops)
  1453. {
  1454. bool using_mac_select_pcs = false;
  1455. struct phylink *pl;
  1456. int ret;
  1457. /* Validate the supplied configuration */
  1458. if (phy_interface_empty(config->supported_interfaces)) {
  1459. dev_err(config->dev,
  1460. "phylink: error: empty supported_interfaces\n");
  1461. return ERR_PTR(-EINVAL);
  1462. }
  1463. if (mac_ops->mac_select_pcs &&
  1464. mac_ops->mac_select_pcs(config, PHY_INTERFACE_MODE_NA) !=
  1465. ERR_PTR(-EOPNOTSUPP))
  1466. using_mac_select_pcs = true;
  1467. pl = kzalloc(sizeof(*pl), GFP_KERNEL);
  1468. if (!pl)
  1469. return ERR_PTR(-ENOMEM);
  1470. mutex_init(&pl->state_mutex);
  1471. INIT_WORK(&pl->resolve, phylink_resolve);
  1472. pl->config = config;
  1473. if (config->type == PHYLINK_NETDEV) {
  1474. pl->netdev = to_net_dev(config->dev);
  1475. netif_carrier_off(pl->netdev);
  1476. } else if (config->type == PHYLINK_DEV) {
  1477. pl->dev = config->dev;
  1478. } else {
  1479. kfree(pl);
  1480. return ERR_PTR(-EINVAL);
  1481. }
  1482. pl->using_mac_select_pcs = using_mac_select_pcs;
  1483. pl->phy_state.interface = iface;
  1484. pl->link_interface = iface;
  1485. if (iface == PHY_INTERFACE_MODE_MOCA)
  1486. pl->link_port = PORT_BNC;
  1487. else
  1488. pl->link_port = PORT_MII;
  1489. pl->link_config.interface = iface;
  1490. pl->link_config.pause = MLO_PAUSE_AN;
  1491. pl->link_config.speed = SPEED_UNKNOWN;
  1492. pl->link_config.duplex = DUPLEX_UNKNOWN;
  1493. pl->pcs_state = PCS_STATE_DOWN;
  1494. pl->mac_ops = mac_ops;
  1495. __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
  1496. timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
  1497. linkmode_fill(pl->supported);
  1498. linkmode_copy(pl->link_config.advertising, pl->supported);
  1499. phylink_validate(pl, pl->supported, &pl->link_config);
  1500. ret = phylink_parse_mode(pl, fwnode);
  1501. if (ret < 0) {
  1502. kfree(pl);
  1503. return ERR_PTR(ret);
  1504. }
  1505. if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
  1506. ret = phylink_parse_fixedlink(pl, fwnode);
  1507. if (ret < 0) {
  1508. kfree(pl);
  1509. return ERR_PTR(ret);
  1510. }
  1511. }
  1512. pl->cur_link_an_mode = pl->cfg_link_an_mode;
  1513. ret = phylink_register_sfp(pl, fwnode);
  1514. if (ret < 0) {
  1515. kfree(pl);
  1516. return ERR_PTR(ret);
  1517. }
  1518. return pl;
  1519. }
  1520. EXPORT_SYMBOL_GPL(phylink_create);
  1521. /**
  1522. * phylink_destroy() - cleanup and destroy the phylink instance
  1523. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1524. *
  1525. * Destroy a phylink instance. Any PHY that has been attached must have been
  1526. * cleaned up via phylink_disconnect_phy() prior to calling this function.
  1527. *
  1528. * Note: the rtnl lock must not be held when calling this function.
  1529. */
  1530. void phylink_destroy(struct phylink *pl)
  1531. {
  1532. sfp_bus_del_upstream(pl->sfp_bus);
  1533. if (pl->link_gpio)
  1534. gpiod_put(pl->link_gpio);
  1535. cancel_work_sync(&pl->resolve);
  1536. kfree(pl);
  1537. }
  1538. EXPORT_SYMBOL_GPL(phylink_destroy);
  1539. /**
  1540. * phylink_expects_phy() - Determine if phylink expects a phy to be attached
  1541. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1542. *
  1543. * When using fixed-link mode, or in-band mode with 1000base-X or 2500base-X,
  1544. * no PHY is needed.
  1545. *
  1546. * Returns true if phylink will be expecting a PHY.
  1547. */
  1548. bool phylink_expects_phy(struct phylink *pl)
  1549. {
  1550. if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
  1551. (pl->cfg_link_an_mode == MLO_AN_INBAND &&
  1552. phy_interface_mode_is_8023z(pl->link_interface)))
  1553. return false;
  1554. return true;
  1555. }
  1556. EXPORT_SYMBOL_GPL(phylink_expects_phy);
  1557. static void phylink_phy_change(struct phy_device *phydev, bool up)
  1558. {
  1559. struct phylink *pl = phydev->phylink;
  1560. bool tx_pause, rx_pause;
  1561. phy_get_pause(phydev, &tx_pause, &rx_pause);
  1562. mutex_lock(&pl->state_mutex);
  1563. pl->phy_state.speed = phydev->speed;
  1564. pl->phy_state.duplex = phydev->duplex;
  1565. pl->phy_state.rate_matching = phydev->rate_matching;
  1566. pl->phy_state.pause = MLO_PAUSE_NONE;
  1567. if (tx_pause)
  1568. pl->phy_state.pause |= MLO_PAUSE_TX;
  1569. if (rx_pause)
  1570. pl->phy_state.pause |= MLO_PAUSE_RX;
  1571. pl->phy_state.interface = phydev->interface;
  1572. pl->phy_state.link = up;
  1573. if (!up)
  1574. pl->link_failed = true;
  1575. mutex_unlock(&pl->state_mutex);
  1576. phylink_run_resolve(pl);
  1577. phylink_dbg(pl, "phy link %s %s/%s/%s/%s/%s\n", up ? "up" : "down",
  1578. phy_modes(phydev->interface),
  1579. phy_speed_to_str(phydev->speed),
  1580. phy_duplex_to_str(phydev->duplex),
  1581. phy_rate_matching_to_str(phydev->rate_matching),
  1582. phylink_pause_to_str(pl->phy_state.pause));
  1583. }
  1584. static int phylink_validate_phy(struct phylink *pl, struct phy_device *phy,
  1585. unsigned long *supported,
  1586. struct phylink_link_state *state)
  1587. {
  1588. DECLARE_PHY_INTERFACE_MASK(interfaces);
  1589. /* If the PHY provides a bitmap of the interfaces it will be using
  1590. * depending on the negotiated media speeds, use this to validate
  1591. * which ethtool link modes can be used.
  1592. */
  1593. if (!phy_interface_empty(phy->possible_interfaces)) {
  1594. /* We only care about the union of the PHY's interfaces and
  1595. * those which the host supports.
  1596. */
  1597. phy_interface_and(interfaces, phy->possible_interfaces,
  1598. pl->config->supported_interfaces);
  1599. if (phy_interface_empty(interfaces)) {
  1600. phylink_err(pl, "PHY has no common interfaces\n");
  1601. return -EINVAL;
  1602. }
  1603. if (phy_on_sfp(phy)) {
  1604. /* If the PHY is on a SFP, limit the interfaces to
  1605. * those that can be used with a SFP module.
  1606. */
  1607. phy_interface_and(interfaces, interfaces,
  1608. phylink_sfp_interfaces);
  1609. if (phy_interface_empty(interfaces)) {
  1610. phylink_err(pl, "SFP PHY's possible interfaces becomes empty\n");
  1611. return -EINVAL;
  1612. }
  1613. }
  1614. phylink_dbg(pl, "PHY %s uses interfaces %*pbl, validating %*pbl\n",
  1615. phydev_name(phy),
  1616. (int)PHY_INTERFACE_MODE_MAX,
  1617. phy->possible_interfaces,
  1618. (int)PHY_INTERFACE_MODE_MAX, interfaces);
  1619. return phylink_validate_mask(pl, phy, supported, state,
  1620. interfaces);
  1621. }
  1622. phylink_dbg(pl, "PHY %s doesn't supply possible interfaces\n",
  1623. phydev_name(phy));
  1624. /* Check whether we would use rate matching for the proposed interface
  1625. * mode.
  1626. */
  1627. state->rate_matching = phy_get_rate_matching(phy, state->interface);
  1628. /* Clause 45 PHYs may switch their Serdes lane between, e.g. 10GBASE-R,
  1629. * 5GBASE-R, 2500BASE-X and SGMII if they are not using rate matching.
  1630. * For some interface modes (e.g. RXAUI, XAUI and USXGMII) switching
  1631. * their Serdes is either unnecessary or not reasonable.
  1632. *
  1633. * For these which switch interface modes, we really need to know which
  1634. * interface modes the PHY supports to properly work out which ethtool
  1635. * linkmodes can be supported. For now, as a work-around, we validate
  1636. * against all interface modes, which may lead to more ethtool link
  1637. * modes being advertised than are actually supported.
  1638. */
  1639. if (phy->is_c45 && state->rate_matching == RATE_MATCH_NONE &&
  1640. state->interface != PHY_INTERFACE_MODE_RXAUI &&
  1641. state->interface != PHY_INTERFACE_MODE_XAUI &&
  1642. state->interface != PHY_INTERFACE_MODE_USXGMII)
  1643. state->interface = PHY_INTERFACE_MODE_NA;
  1644. return phylink_validate(pl, supported, state);
  1645. }
  1646. static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
  1647. phy_interface_t interface)
  1648. {
  1649. struct phylink_link_state config;
  1650. __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
  1651. char *irq_str;
  1652. int ret;
  1653. /*
  1654. * This is the new way of dealing with flow control for PHYs,
  1655. * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
  1656. * phy drivers should not set SUPPORTED_[Asym_]Pause") except
  1657. * using our validate call to the MAC, we rely upon the MAC
  1658. * clearing the bits from both supported and advertising fields.
  1659. */
  1660. phy_support_asym_pause(phy);
  1661. memset(&config, 0, sizeof(config));
  1662. linkmode_copy(supported, phy->supported);
  1663. linkmode_copy(config.advertising, phy->advertising);
  1664. config.interface = interface;
  1665. ret = phylink_validate_phy(pl, phy, supported, &config);
  1666. if (ret) {
  1667. phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %pe\n",
  1668. phy_modes(config.interface),
  1669. __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
  1670. __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
  1671. ERR_PTR(ret));
  1672. return ret;
  1673. }
  1674. phy->phylink = pl;
  1675. phy->phy_link_change = phylink_phy_change;
  1676. irq_str = phy_attached_info_irq(phy);
  1677. phylink_info(pl,
  1678. "PHY [%s] driver [%s] (irq=%s)\n",
  1679. dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
  1680. kfree(irq_str);
  1681. mutex_lock(&phy->lock);
  1682. mutex_lock(&pl->state_mutex);
  1683. pl->phydev = phy;
  1684. pl->phy_state.interface = interface;
  1685. pl->phy_state.pause = MLO_PAUSE_NONE;
  1686. pl->phy_state.speed = SPEED_UNKNOWN;
  1687. pl->phy_state.duplex = DUPLEX_UNKNOWN;
  1688. pl->phy_state.rate_matching = RATE_MATCH_NONE;
  1689. linkmode_copy(pl->supported, supported);
  1690. linkmode_copy(pl->link_config.advertising, config.advertising);
  1691. /* Restrict the phy advertisement according to the MAC support. */
  1692. linkmode_copy(phy->advertising, config.advertising);
  1693. mutex_unlock(&pl->state_mutex);
  1694. mutex_unlock(&phy->lock);
  1695. phylink_dbg(pl,
  1696. "phy: %s setting supported %*pb advertising %*pb\n",
  1697. phy_modes(interface),
  1698. __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
  1699. __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
  1700. if (phy_interrupt_is_valid(phy))
  1701. phy_request_interrupt(phy);
  1702. if (pl->config->mac_managed_pm)
  1703. phy->mac_managed_pm = true;
  1704. return 0;
  1705. }
  1706. static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
  1707. phy_interface_t interface)
  1708. {
  1709. u32 flags = 0;
  1710. if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
  1711. (pl->cfg_link_an_mode == MLO_AN_INBAND &&
  1712. phy_interface_mode_is_8023z(interface) && !pl->sfp_bus)))
  1713. return -EINVAL;
  1714. if (pl->phydev)
  1715. return -EBUSY;
  1716. if (pl->config->mac_requires_rxc)
  1717. flags |= PHY_F_RXC_ALWAYS_ON;
  1718. return phy_attach_direct(pl->netdev, phy, flags, interface);
  1719. }
  1720. /**
  1721. * phylink_connect_phy() - connect a PHY to the phylink instance
  1722. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1723. * @phy: a pointer to a &struct phy_device.
  1724. *
  1725. * Connect @phy to the phylink instance specified by @pl by calling
  1726. * phy_attach_direct(). Configure the @phy according to the MAC driver's
  1727. * capabilities, start the PHYLIB state machine and enable any interrupts
  1728. * that the PHY supports.
  1729. *
  1730. * This updates the phylink's ethtool supported and advertising link mode
  1731. * masks.
  1732. *
  1733. * Returns 0 on success or a negative errno.
  1734. */
  1735. int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
  1736. {
  1737. int ret;
  1738. /* Use PHY device/driver interface */
  1739. if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
  1740. pl->link_interface = phy->interface;
  1741. pl->link_config.interface = pl->link_interface;
  1742. }
  1743. ret = phylink_attach_phy(pl, phy, pl->link_interface);
  1744. if (ret < 0)
  1745. return ret;
  1746. ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
  1747. if (ret)
  1748. phy_detach(phy);
  1749. return ret;
  1750. }
  1751. EXPORT_SYMBOL_GPL(phylink_connect_phy);
  1752. /**
  1753. * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
  1754. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1755. * @dn: a pointer to a &struct device_node.
  1756. * @flags: PHY-specific flags to communicate to the PHY device driver
  1757. *
  1758. * Connect the phy specified in the device node @dn to the phylink instance
  1759. * specified by @pl. Actions specified in phylink_connect_phy() will be
  1760. * performed.
  1761. *
  1762. * Returns 0 on success or a negative errno.
  1763. */
  1764. int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
  1765. u32 flags)
  1766. {
  1767. return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
  1768. }
  1769. EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
  1770. /**
  1771. * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
  1772. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1773. * @fwnode: a pointer to a &struct fwnode_handle.
  1774. * @flags: PHY-specific flags to communicate to the PHY device driver
  1775. *
  1776. * Connect the phy specified @fwnode to the phylink instance specified
  1777. * by @pl.
  1778. *
  1779. * Returns 0 on success or a negative errno.
  1780. */
  1781. int phylink_fwnode_phy_connect(struct phylink *pl,
  1782. const struct fwnode_handle *fwnode,
  1783. u32 flags)
  1784. {
  1785. struct fwnode_handle *phy_fwnode;
  1786. struct phy_device *phy_dev;
  1787. int ret;
  1788. /* Fixed links and 802.3z are handled without needing a PHY */
  1789. if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
  1790. (pl->cfg_link_an_mode == MLO_AN_INBAND &&
  1791. phy_interface_mode_is_8023z(pl->link_interface)))
  1792. return 0;
  1793. phy_fwnode = fwnode_get_phy_node(fwnode);
  1794. if (IS_ERR(phy_fwnode)) {
  1795. if (pl->cfg_link_an_mode == MLO_AN_PHY)
  1796. return -ENODEV;
  1797. return 0;
  1798. }
  1799. phy_dev = fwnode_phy_find_device(phy_fwnode);
  1800. /* We're done with the phy_node handle */
  1801. fwnode_handle_put(phy_fwnode);
  1802. if (!phy_dev)
  1803. return -ENODEV;
  1804. /* Use PHY device/driver interface */
  1805. if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
  1806. pl->link_interface = phy_dev->interface;
  1807. pl->link_config.interface = pl->link_interface;
  1808. }
  1809. if (pl->config->mac_requires_rxc)
  1810. flags |= PHY_F_RXC_ALWAYS_ON;
  1811. ret = phy_attach_direct(pl->netdev, phy_dev, flags,
  1812. pl->link_interface);
  1813. phy_device_free(phy_dev);
  1814. if (ret)
  1815. return ret;
  1816. ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
  1817. if (ret)
  1818. phy_detach(phy_dev);
  1819. return ret;
  1820. }
  1821. EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
  1822. /**
  1823. * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
  1824. * instance.
  1825. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1826. *
  1827. * Disconnect any current PHY from the phylink instance described by @pl.
  1828. */
  1829. void phylink_disconnect_phy(struct phylink *pl)
  1830. {
  1831. struct phy_device *phy;
  1832. ASSERT_RTNL();
  1833. phy = pl->phydev;
  1834. if (phy) {
  1835. mutex_lock(&phy->lock);
  1836. mutex_lock(&pl->state_mutex);
  1837. pl->phydev = NULL;
  1838. mutex_unlock(&pl->state_mutex);
  1839. mutex_unlock(&phy->lock);
  1840. flush_work(&pl->resolve);
  1841. phy_disconnect(phy);
  1842. }
  1843. }
  1844. EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
  1845. static void phylink_link_changed(struct phylink *pl, bool up, const char *what)
  1846. {
  1847. if (!up)
  1848. pl->link_failed = true;
  1849. phylink_run_resolve(pl);
  1850. phylink_dbg(pl, "%s link %s\n", what, up ? "up" : "down");
  1851. }
  1852. /**
  1853. * phylink_mac_change() - notify phylink of a change in MAC state
  1854. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1855. * @up: indicates whether the link is currently up.
  1856. *
  1857. * The MAC driver should call this driver when the state of its link
  1858. * changes (eg, link failure, new negotiation results, etc.)
  1859. */
  1860. void phylink_mac_change(struct phylink *pl, bool up)
  1861. {
  1862. phylink_link_changed(pl, up, "mac");
  1863. }
  1864. EXPORT_SYMBOL_GPL(phylink_mac_change);
  1865. /**
  1866. * phylink_pcs_change() - notify phylink of a change to PCS link state
  1867. * @pcs: pointer to &struct phylink_pcs
  1868. * @up: indicates whether the link is currently up.
  1869. *
  1870. * The PCS driver should call this when the state of its link changes
  1871. * (e.g. link failure, new negotiation results, etc.) Note: it should
  1872. * not determine "up" by reading the BMSR. If in doubt about the link
  1873. * state at interrupt time, then pass true if pcs_get_state() returns
  1874. * the latched link-down state, otherwise pass false.
  1875. */
  1876. void phylink_pcs_change(struct phylink_pcs *pcs, bool up)
  1877. {
  1878. struct phylink *pl = pcs->phylink;
  1879. if (pl)
  1880. phylink_link_changed(pl, up, "pcs");
  1881. }
  1882. EXPORT_SYMBOL_GPL(phylink_pcs_change);
  1883. static irqreturn_t phylink_link_handler(int irq, void *data)
  1884. {
  1885. struct phylink *pl = data;
  1886. phylink_run_resolve(pl);
  1887. return IRQ_HANDLED;
  1888. }
  1889. /**
  1890. * phylink_start() - start a phylink instance
  1891. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1892. *
  1893. * Start the phylink instance specified by @pl, configuring the MAC for the
  1894. * desired link mode(s) and negotiation style. This should be called from the
  1895. * network device driver's &struct net_device_ops ndo_open() method.
  1896. */
  1897. void phylink_start(struct phylink *pl)
  1898. {
  1899. bool poll = false;
  1900. ASSERT_RTNL();
  1901. phylink_info(pl, "configuring for %s/%s link mode\n",
  1902. phylink_an_mode_str(pl->cur_link_an_mode),
  1903. phy_modes(pl->link_config.interface));
  1904. /* Always set the carrier off */
  1905. if (pl->netdev)
  1906. netif_carrier_off(pl->netdev);
  1907. pl->pcs_state = PCS_STATE_STARTING;
  1908. /* Apply the link configuration to the MAC when starting. This allows
  1909. * a fixed-link to start with the correct parameters, and also
  1910. * ensures that we set the appropriate advertisement for Serdes links.
  1911. *
  1912. * Restart autonegotiation if using 802.3z to ensure that the link
  1913. * parameters are properly negotiated. This is necessary for DSA
  1914. * switches using 802.3z negotiation to ensure they see our modes.
  1915. */
  1916. phylink_mac_initial_config(pl, true);
  1917. pl->pcs_state = PCS_STATE_STARTED;
  1918. phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_STOPPED);
  1919. if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
  1920. int irq = gpiod_to_irq(pl->link_gpio);
  1921. if (irq > 0) {
  1922. if (!request_irq(irq, phylink_link_handler,
  1923. IRQF_TRIGGER_RISING |
  1924. IRQF_TRIGGER_FALLING,
  1925. "netdev link", pl))
  1926. pl->link_irq = irq;
  1927. else
  1928. irq = 0;
  1929. }
  1930. if (irq <= 0)
  1931. poll = true;
  1932. }
  1933. if (pl->cfg_link_an_mode == MLO_AN_FIXED)
  1934. poll |= pl->config->poll_fixed_state;
  1935. if (poll)
  1936. mod_timer(&pl->link_poll, jiffies + HZ);
  1937. if (pl->phydev)
  1938. phy_start(pl->phydev);
  1939. if (pl->sfp_bus)
  1940. sfp_upstream_start(pl->sfp_bus);
  1941. }
  1942. EXPORT_SYMBOL_GPL(phylink_start);
  1943. /**
  1944. * phylink_stop() - stop a phylink instance
  1945. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1946. *
  1947. * Stop the phylink instance specified by @pl. This should be called from the
  1948. * network device driver's &struct net_device_ops ndo_stop() method. The
  1949. * network device's carrier state should not be changed prior to calling this
  1950. * function.
  1951. *
  1952. * This will synchronously bring down the link if the link is not already
  1953. * down (in other words, it will trigger a mac_link_down() method call.)
  1954. */
  1955. void phylink_stop(struct phylink *pl)
  1956. {
  1957. ASSERT_RTNL();
  1958. if (pl->sfp_bus)
  1959. sfp_upstream_stop(pl->sfp_bus);
  1960. if (pl->phydev)
  1961. phy_stop(pl->phydev);
  1962. del_timer_sync(&pl->link_poll);
  1963. if (pl->link_irq) {
  1964. free_irq(pl->link_irq, pl);
  1965. pl->link_irq = 0;
  1966. }
  1967. phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
  1968. pl->pcs_state = PCS_STATE_DOWN;
  1969. phylink_pcs_disable(pl->pcs);
  1970. }
  1971. EXPORT_SYMBOL_GPL(phylink_stop);
  1972. /**
  1973. * phylink_suspend() - handle a network device suspend event
  1974. * @pl: a pointer to a &struct phylink returned from phylink_create()
  1975. * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
  1976. *
  1977. * Handle a network device suspend event. There are several cases:
  1978. *
  1979. * - If Wake-on-Lan is not active, we can bring down the link between
  1980. * the MAC and PHY by calling phylink_stop().
  1981. * - If Wake-on-Lan is active, and being handled only by the PHY, we
  1982. * can also bring down the link between the MAC and PHY.
  1983. * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
  1984. * still needs to receive packets, so we can not bring the link down.
  1985. */
  1986. void phylink_suspend(struct phylink *pl, bool mac_wol)
  1987. {
  1988. ASSERT_RTNL();
  1989. if (mac_wol && (!pl->netdev || pl->netdev->ethtool->wol_enabled)) {
  1990. /* Wake-on-Lan enabled, MAC handling */
  1991. mutex_lock(&pl->state_mutex);
  1992. /* Stop the resolver bringing the link up */
  1993. __set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
  1994. /* Disable the carrier, to prevent transmit timeouts,
  1995. * but one would hope all packets have been sent. This
  1996. * also means phylink_resolve() will do nothing.
  1997. */
  1998. if (pl->netdev)
  1999. netif_carrier_off(pl->netdev);
  2000. else
  2001. pl->old_link_state = false;
  2002. /* We do not call mac_link_down() here as we want the
  2003. * link to remain up to receive the WoL packets.
  2004. */
  2005. mutex_unlock(&pl->state_mutex);
  2006. } else {
  2007. phylink_stop(pl);
  2008. }
  2009. }
  2010. EXPORT_SYMBOL_GPL(phylink_suspend);
  2011. /**
  2012. * phylink_resume() - handle a network device resume event
  2013. * @pl: a pointer to a &struct phylink returned from phylink_create()
  2014. *
  2015. * Undo the effects of phylink_suspend(), returning the link to an
  2016. * operational state.
  2017. */
  2018. void phylink_resume(struct phylink *pl)
  2019. {
  2020. ASSERT_RTNL();
  2021. if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
  2022. /* Wake-on-Lan enabled, MAC handling */
  2023. /* Call mac_link_down() so we keep the overall state balanced.
  2024. * Do this under the state_mutex lock for consistency. This
  2025. * will cause a "Link Down" message to be printed during
  2026. * resume, which is harmless - the true link state will be
  2027. * printed when we run a resolve.
  2028. */
  2029. mutex_lock(&pl->state_mutex);
  2030. phylink_link_down(pl);
  2031. mutex_unlock(&pl->state_mutex);
  2032. /* Re-apply the link parameters so that all the settings get
  2033. * restored to the MAC.
  2034. */
  2035. phylink_mac_initial_config(pl, true);
  2036. /* Re-enable and re-resolve the link parameters */
  2037. phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_MAC_WOL);
  2038. } else {
  2039. phylink_start(pl);
  2040. }
  2041. }
  2042. EXPORT_SYMBOL_GPL(phylink_resume);
  2043. /**
  2044. * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
  2045. * @pl: a pointer to a &struct phylink returned from phylink_create()
  2046. * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
  2047. *
  2048. * Read the wake on lan parameters from the PHY attached to the phylink
  2049. * instance specified by @pl. If no PHY is currently attached, report no
  2050. * support for wake on lan.
  2051. */
  2052. void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
  2053. {
  2054. ASSERT_RTNL();
  2055. wol->supported = 0;
  2056. wol->wolopts = 0;
  2057. if (pl->phydev)
  2058. phy_ethtool_get_wol(pl->phydev, wol);
  2059. }
  2060. EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
  2061. /**
  2062. * phylink_ethtool_set_wol() - set wake on lan parameters
  2063. * @pl: a pointer to a &struct phylink returned from phylink_create()
  2064. * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
  2065. *
  2066. * Set the wake on lan parameters for the PHY attached to the phylink
  2067. * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
  2068. * error.
  2069. *
  2070. * Returns zero on success or negative errno code.
  2071. */
  2072. int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
  2073. {
  2074. int ret = -EOPNOTSUPP;
  2075. ASSERT_RTNL();
  2076. if (pl->phydev)
  2077. ret = phy_ethtool_set_wol(pl->phydev, wol);
  2078. return ret;
  2079. }
  2080. EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
  2081. static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
  2082. {
  2083. __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
  2084. linkmode_zero(mask);
  2085. phylink_set_port_modes(mask);
  2086. linkmode_and(dst, dst, mask);
  2087. linkmode_or(dst, dst, b);
  2088. }
  2089. static void phylink_get_ksettings(const struct phylink_link_state *state,
  2090. struct ethtool_link_ksettings *kset)
  2091. {
  2092. phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
  2093. linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
  2094. if (kset->base.rate_matching == RATE_MATCH_NONE) {
  2095. kset->base.speed = state->speed;
  2096. kset->base.duplex = state->duplex;
  2097. }
  2098. kset->base.autoneg = linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  2099. state->advertising) ?
  2100. AUTONEG_ENABLE : AUTONEG_DISABLE;
  2101. }
  2102. /**
  2103. * phylink_ethtool_ksettings_get() - get the current link settings
  2104. * @pl: a pointer to a &struct phylink returned from phylink_create()
  2105. * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
  2106. *
  2107. * Read the current link settings for the phylink instance specified by @pl.
  2108. * This will be the link settings read from the MAC, PHY or fixed link
  2109. * settings depending on the current negotiation mode.
  2110. */
  2111. int phylink_ethtool_ksettings_get(struct phylink *pl,
  2112. struct ethtool_link_ksettings *kset)
  2113. {
  2114. struct phylink_link_state link_state;
  2115. ASSERT_RTNL();
  2116. if (pl->phydev)
  2117. phy_ethtool_ksettings_get(pl->phydev, kset);
  2118. else
  2119. kset->base.port = pl->link_port;
  2120. linkmode_copy(kset->link_modes.supported, pl->supported);
  2121. switch (pl->cur_link_an_mode) {
  2122. case MLO_AN_FIXED:
  2123. /* We are using fixed settings. Report these as the
  2124. * current link settings - and note that these also
  2125. * represent the supported speeds/duplex/pause modes.
  2126. */
  2127. phylink_get_fixed_state(pl, &link_state);
  2128. phylink_get_ksettings(&link_state, kset);
  2129. break;
  2130. case MLO_AN_INBAND:
  2131. /* If there is a phy attached, then use the reported
  2132. * settings from the phy with no modification.
  2133. */
  2134. if (pl->phydev)
  2135. break;
  2136. phylink_mac_pcs_get_state(pl, &link_state);
  2137. /* The MAC is reporting the link results from its own PCS
  2138. * layer via in-band status. Report these as the current
  2139. * link settings.
  2140. */
  2141. phylink_get_ksettings(&link_state, kset);
  2142. break;
  2143. }
  2144. return 0;
  2145. }
  2146. EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
  2147. /**
  2148. * phylink_ethtool_ksettings_set() - set the link settings
  2149. * @pl: a pointer to a &struct phylink returned from phylink_create()
  2150. * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
  2151. */
  2152. int phylink_ethtool_ksettings_set(struct phylink *pl,
  2153. const struct ethtool_link_ksettings *kset)
  2154. {
  2155. __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
  2156. struct phylink_link_state config;
  2157. const struct phy_setting *s;
  2158. ASSERT_RTNL();
  2159. if (pl->phydev) {
  2160. struct ethtool_link_ksettings phy_kset = *kset;
  2161. linkmode_and(phy_kset.link_modes.advertising,
  2162. phy_kset.link_modes.advertising,
  2163. pl->supported);
  2164. /* We can rely on phylib for this update; we also do not need
  2165. * to update the pl->link_config settings:
  2166. * - the configuration returned via ksettings_get() will come
  2167. * from phylib whenever a PHY is present.
  2168. * - link_config.interface will be updated by the PHY calling
  2169. * back via phylink_phy_change() and a subsequent resolve.
  2170. * - initial link configuration for PHY mode comes from the
  2171. * last phy state updated via phylink_phy_change().
  2172. * - other configuration changes (e.g. pause modes) are
  2173. * performed directly via phylib.
  2174. * - if in in-band mode with a PHY, the link configuration
  2175. * is passed on the link from the PHY, and all of
  2176. * link_config.{speed,duplex,an_enabled,pause} are not used.
  2177. * - the only possible use would be link_config.advertising
  2178. * pause modes when in 1000base-X mode with a PHY, but in
  2179. * the presence of a PHY, this should not be changed as that
  2180. * should be determined from the media side advertisement.
  2181. */
  2182. return phy_ethtool_ksettings_set(pl->phydev, &phy_kset);
  2183. }
  2184. config = pl->link_config;
  2185. /* Mask out unsupported advertisements */
  2186. linkmode_and(config.advertising, kset->link_modes.advertising,
  2187. pl->supported);
  2188. /* FIXME: should we reject autoneg if phy/mac does not support it? */
  2189. switch (kset->base.autoneg) {
  2190. case AUTONEG_DISABLE:
  2191. /* Autonegotiation disabled, select a suitable speed and
  2192. * duplex.
  2193. */
  2194. s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
  2195. pl->supported, false);
  2196. if (!s)
  2197. return -EINVAL;
  2198. /* If we have a fixed link, refuse to change link parameters.
  2199. * If the link parameters match, accept them but do nothing.
  2200. */
  2201. if (pl->cur_link_an_mode == MLO_AN_FIXED) {
  2202. if (s->speed != pl->link_config.speed ||
  2203. s->duplex != pl->link_config.duplex)
  2204. return -EINVAL;
  2205. return 0;
  2206. }
  2207. config.speed = s->speed;
  2208. config.duplex = s->duplex;
  2209. break;
  2210. case AUTONEG_ENABLE:
  2211. /* If we have a fixed link, allow autonegotiation (since that
  2212. * is our default case) but do not allow the advertisement to
  2213. * be changed. If the advertisement matches, simply return.
  2214. */
  2215. if (pl->cur_link_an_mode == MLO_AN_FIXED) {
  2216. if (!linkmode_equal(config.advertising,
  2217. pl->link_config.advertising))
  2218. return -EINVAL;
  2219. return 0;
  2220. }
  2221. config.speed = SPEED_UNKNOWN;
  2222. config.duplex = DUPLEX_UNKNOWN;
  2223. break;
  2224. default:
  2225. return -EINVAL;
  2226. }
  2227. /* We have ruled out the case with a PHY attached, and the
  2228. * fixed-link cases. All that is left are in-band links.
  2229. */
  2230. linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
  2231. kset->base.autoneg == AUTONEG_ENABLE);
  2232. /* If this link is with an SFP, ensure that changes to advertised modes
  2233. * also cause the associated interface to be selected such that the
  2234. * link can be configured correctly.
  2235. */
  2236. if (pl->sfp_bus) {
  2237. config.interface = sfp_select_interface(pl->sfp_bus,
  2238. config.advertising);
  2239. if (config.interface == PHY_INTERFACE_MODE_NA) {
  2240. phylink_err(pl,
  2241. "selection of interface failed, advertisement %*pb\n",
  2242. __ETHTOOL_LINK_MODE_MASK_NBITS,
  2243. config.advertising);
  2244. return -EINVAL;
  2245. }
  2246. /* Revalidate with the selected interface */
  2247. linkmode_copy(support, pl->supported);
  2248. if (phylink_validate(pl, support, &config)) {
  2249. phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
  2250. phylink_an_mode_str(pl->cur_link_an_mode),
  2251. phy_modes(config.interface),
  2252. __ETHTOOL_LINK_MODE_MASK_NBITS, support);
  2253. return -EINVAL;
  2254. }
  2255. } else {
  2256. /* Validate without changing the current supported mask. */
  2257. linkmode_copy(support, pl->supported);
  2258. if (phylink_validate(pl, support, &config))
  2259. return -EINVAL;
  2260. }
  2261. /* If autonegotiation is enabled, we must have an advertisement */
  2262. if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  2263. config.advertising) &&
  2264. phylink_is_empty_linkmode(config.advertising))
  2265. return -EINVAL;
  2266. mutex_lock(&pl->state_mutex);
  2267. pl->link_config.speed = config.speed;
  2268. pl->link_config.duplex = config.duplex;
  2269. if (pl->link_config.interface != config.interface) {
  2270. /* The interface changed, e.g. 1000base-X <-> 2500base-X */
  2271. /* We need to force the link down, then change the interface */
  2272. if (pl->old_link_state) {
  2273. phylink_link_down(pl);
  2274. pl->old_link_state = false;
  2275. }
  2276. if (!test_bit(PHYLINK_DISABLE_STOPPED,
  2277. &pl->phylink_disable_state))
  2278. phylink_major_config(pl, false, &config);
  2279. pl->link_config.interface = config.interface;
  2280. linkmode_copy(pl->link_config.advertising, config.advertising);
  2281. } else if (!linkmode_equal(pl->link_config.advertising,
  2282. config.advertising)) {
  2283. linkmode_copy(pl->link_config.advertising, config.advertising);
  2284. phylink_change_inband_advert(pl);
  2285. }
  2286. mutex_unlock(&pl->state_mutex);
  2287. return 0;
  2288. }
  2289. EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
  2290. /**
  2291. * phylink_ethtool_nway_reset() - restart negotiation
  2292. * @pl: a pointer to a &struct phylink returned from phylink_create()
  2293. *
  2294. * Restart negotiation for the phylink instance specified by @pl. This will
  2295. * cause any attached phy to restart negotiation with the link partner, and
  2296. * if the MAC is in a BaseX mode, the MAC will also be requested to restart
  2297. * negotiation.
  2298. *
  2299. * Returns zero on success, or negative error code.
  2300. */
  2301. int phylink_ethtool_nway_reset(struct phylink *pl)
  2302. {
  2303. int ret = 0;
  2304. ASSERT_RTNL();
  2305. if (pl->phydev)
  2306. ret = phy_restart_aneg(pl->phydev);
  2307. phylink_pcs_an_restart(pl);
  2308. return ret;
  2309. }
  2310. EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
  2311. /**
  2312. * phylink_ethtool_get_pauseparam() - get the current pause parameters
  2313. * @pl: a pointer to a &struct phylink returned from phylink_create()
  2314. * @pause: a pointer to a &struct ethtool_pauseparam
  2315. */
  2316. void phylink_ethtool_get_pauseparam(struct phylink *pl,
  2317. struct ethtool_pauseparam *pause)
  2318. {
  2319. ASSERT_RTNL();
  2320. pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
  2321. pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
  2322. pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
  2323. }
  2324. EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
  2325. /**
  2326. * phylink_ethtool_set_pauseparam() - set the current pause parameters
  2327. * @pl: a pointer to a &struct phylink returned from phylink_create()
  2328. * @pause: a pointer to a &struct ethtool_pauseparam
  2329. */
  2330. int phylink_ethtool_set_pauseparam(struct phylink *pl,
  2331. struct ethtool_pauseparam *pause)
  2332. {
  2333. struct phylink_link_state *config = &pl->link_config;
  2334. bool manual_changed;
  2335. int pause_state;
  2336. ASSERT_RTNL();
  2337. if (pl->cur_link_an_mode == MLO_AN_FIXED)
  2338. return -EOPNOTSUPP;
  2339. if (!phylink_test(pl->supported, Pause) &&
  2340. !phylink_test(pl->supported, Asym_Pause))
  2341. return -EOPNOTSUPP;
  2342. if (!phylink_test(pl->supported, Asym_Pause) &&
  2343. pause->rx_pause != pause->tx_pause)
  2344. return -EINVAL;
  2345. pause_state = 0;
  2346. if (pause->autoneg)
  2347. pause_state |= MLO_PAUSE_AN;
  2348. if (pause->rx_pause)
  2349. pause_state |= MLO_PAUSE_RX;
  2350. if (pause->tx_pause)
  2351. pause_state |= MLO_PAUSE_TX;
  2352. mutex_lock(&pl->state_mutex);
  2353. /*
  2354. * See the comments for linkmode_set_pause(), wrt the deficiencies
  2355. * with the current implementation. A solution to this issue would
  2356. * be:
  2357. * ethtool Local device
  2358. * rx tx Pause AsymDir
  2359. * 0 0 0 0
  2360. * 1 0 1 1
  2361. * 0 1 0 1
  2362. * 1 1 1 1
  2363. * and then use the ethtool rx/tx enablement status to mask the
  2364. * rx/tx pause resolution.
  2365. */
  2366. linkmode_set_pause(config->advertising, pause->tx_pause,
  2367. pause->rx_pause);
  2368. manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
  2369. (!(pause_state & MLO_PAUSE_AN) &&
  2370. (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
  2371. config->pause = pause_state;
  2372. /* Update our in-band advertisement, triggering a renegotiation if
  2373. * the advertisement changed.
  2374. */
  2375. if (!pl->phydev)
  2376. phylink_change_inband_advert(pl);
  2377. mutex_unlock(&pl->state_mutex);
  2378. /* If we have a PHY, a change of the pause frame advertisement will
  2379. * cause phylib to renegotiate (if AN is enabled) which will in turn
  2380. * call our phylink_phy_change() and trigger a resolve. Note that
  2381. * we can't hold our state mutex while calling phy_set_asym_pause().
  2382. */
  2383. if (pl->phydev)
  2384. phy_set_asym_pause(pl->phydev, pause->rx_pause,
  2385. pause->tx_pause);
  2386. /* If the manual pause settings changed, make sure we trigger a
  2387. * resolve to update their state; we can not guarantee that the
  2388. * link will cycle.
  2389. */
  2390. if (manual_changed) {
  2391. pl->link_failed = true;
  2392. phylink_run_resolve(pl);
  2393. }
  2394. return 0;
  2395. }
  2396. EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
  2397. /**
  2398. * phylink_get_eee_err() - read the energy efficient ethernet error
  2399. * counter
  2400. * @pl: a pointer to a &struct phylink returned from phylink_create().
  2401. *
  2402. * Read the Energy Efficient Ethernet error counter from the PHY associated
  2403. * with the phylink instance specified by @pl.
  2404. *
  2405. * Returns positive error counter value, or negative error code.
  2406. */
  2407. int phylink_get_eee_err(struct phylink *pl)
  2408. {
  2409. int ret = 0;
  2410. ASSERT_RTNL();
  2411. if (pl->phydev)
  2412. ret = phy_get_eee_err(pl->phydev);
  2413. return ret;
  2414. }
  2415. EXPORT_SYMBOL_GPL(phylink_get_eee_err);
  2416. /**
  2417. * phylink_init_eee() - init and check the EEE features
  2418. * @pl: a pointer to a &struct phylink returned from phylink_create()
  2419. * @clk_stop_enable: allow PHY to stop receive clock
  2420. *
  2421. * Must be called either with RTNL held or within mac_link_up()
  2422. */
  2423. int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
  2424. {
  2425. int ret = -EOPNOTSUPP;
  2426. if (pl->phydev)
  2427. ret = phy_init_eee(pl->phydev, clk_stop_enable);
  2428. return ret;
  2429. }
  2430. EXPORT_SYMBOL_GPL(phylink_init_eee);
  2431. /**
  2432. * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
  2433. * @pl: a pointer to a &struct phylink returned from phylink_create()
  2434. * @eee: a pointer to a &struct ethtool_keee for the read parameters
  2435. */
  2436. int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_keee *eee)
  2437. {
  2438. int ret = -EOPNOTSUPP;
  2439. ASSERT_RTNL();
  2440. if (pl->phydev)
  2441. ret = phy_ethtool_get_eee(pl->phydev, eee);
  2442. return ret;
  2443. }
  2444. EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
  2445. /**
  2446. * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
  2447. * @pl: a pointer to a &struct phylink returned from phylink_create()
  2448. * @eee: a pointer to a &struct ethtool_keee for the desired parameters
  2449. */
  2450. int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_keee *eee)
  2451. {
  2452. int ret = -EOPNOTSUPP;
  2453. ASSERT_RTNL();
  2454. if (pl->phydev)
  2455. ret = phy_ethtool_set_eee(pl->phydev, eee);
  2456. return ret;
  2457. }
  2458. EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
  2459. /* This emulates MII registers for a fixed-mode phy operating as per the
  2460. * passed in state. "aneg" defines if we report negotiation is possible.
  2461. *
  2462. * FIXME: should deal with negotiation state too.
  2463. */
  2464. static int phylink_mii_emul_read(unsigned int reg,
  2465. struct phylink_link_state *state)
  2466. {
  2467. struct fixed_phy_status fs;
  2468. unsigned long *lpa = state->lp_advertising;
  2469. int val;
  2470. fs.link = state->link;
  2471. fs.speed = state->speed;
  2472. fs.duplex = state->duplex;
  2473. fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
  2474. fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
  2475. val = swphy_read_reg(reg, &fs);
  2476. if (reg == MII_BMSR) {
  2477. if (!state->an_complete)
  2478. val &= ~BMSR_ANEGCOMPLETE;
  2479. }
  2480. return val;
  2481. }
  2482. static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
  2483. unsigned int reg)
  2484. {
  2485. struct phy_device *phydev = pl->phydev;
  2486. int prtad, devad;
  2487. if (mdio_phy_id_is_c45(phy_id)) {
  2488. prtad = mdio_phy_id_prtad(phy_id);
  2489. devad = mdio_phy_id_devad(phy_id);
  2490. return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
  2491. reg);
  2492. }
  2493. if (phydev->is_c45) {
  2494. switch (reg) {
  2495. case MII_BMCR:
  2496. case MII_BMSR:
  2497. case MII_PHYSID1:
  2498. case MII_PHYSID2:
  2499. devad = __ffs(phydev->c45_ids.mmds_present);
  2500. break;
  2501. case MII_ADVERTISE:
  2502. case MII_LPA:
  2503. if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
  2504. return -EINVAL;
  2505. devad = MDIO_MMD_AN;
  2506. if (reg == MII_ADVERTISE)
  2507. reg = MDIO_AN_ADVERTISE;
  2508. else
  2509. reg = MDIO_AN_LPA;
  2510. break;
  2511. default:
  2512. return -EINVAL;
  2513. }
  2514. prtad = phy_id;
  2515. return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
  2516. reg);
  2517. }
  2518. return mdiobus_read(pl->phydev->mdio.bus, phy_id, reg);
  2519. }
  2520. static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
  2521. unsigned int reg, unsigned int val)
  2522. {
  2523. struct phy_device *phydev = pl->phydev;
  2524. int prtad, devad;
  2525. if (mdio_phy_id_is_c45(phy_id)) {
  2526. prtad = mdio_phy_id_prtad(phy_id);
  2527. devad = mdio_phy_id_devad(phy_id);
  2528. return mdiobus_c45_write(pl->phydev->mdio.bus, prtad, devad,
  2529. reg, val);
  2530. }
  2531. if (phydev->is_c45) {
  2532. switch (reg) {
  2533. case MII_BMCR:
  2534. case MII_BMSR:
  2535. case MII_PHYSID1:
  2536. case MII_PHYSID2:
  2537. devad = __ffs(phydev->c45_ids.mmds_present);
  2538. break;
  2539. case MII_ADVERTISE:
  2540. case MII_LPA:
  2541. if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
  2542. return -EINVAL;
  2543. devad = MDIO_MMD_AN;
  2544. if (reg == MII_ADVERTISE)
  2545. reg = MDIO_AN_ADVERTISE;
  2546. else
  2547. reg = MDIO_AN_LPA;
  2548. break;
  2549. default:
  2550. return -EINVAL;
  2551. }
  2552. return mdiobus_c45_write(pl->phydev->mdio.bus, phy_id, devad,
  2553. reg, val);
  2554. }
  2555. return mdiobus_write(phydev->mdio.bus, phy_id, reg, val);
  2556. }
  2557. static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
  2558. unsigned int reg)
  2559. {
  2560. struct phylink_link_state state;
  2561. int val = 0xffff;
  2562. switch (pl->cur_link_an_mode) {
  2563. case MLO_AN_FIXED:
  2564. if (phy_id == 0) {
  2565. phylink_get_fixed_state(pl, &state);
  2566. val = phylink_mii_emul_read(reg, &state);
  2567. }
  2568. break;
  2569. case MLO_AN_PHY:
  2570. return -EOPNOTSUPP;
  2571. case MLO_AN_INBAND:
  2572. if (phy_id == 0) {
  2573. phylink_mac_pcs_get_state(pl, &state);
  2574. val = phylink_mii_emul_read(reg, &state);
  2575. }
  2576. break;
  2577. }
  2578. return val & 0xffff;
  2579. }
  2580. static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
  2581. unsigned int reg, unsigned int val)
  2582. {
  2583. switch (pl->cur_link_an_mode) {
  2584. case MLO_AN_FIXED:
  2585. break;
  2586. case MLO_AN_PHY:
  2587. return -EOPNOTSUPP;
  2588. case MLO_AN_INBAND:
  2589. break;
  2590. }
  2591. return 0;
  2592. }
  2593. /**
  2594. * phylink_mii_ioctl() - generic mii ioctl interface
  2595. * @pl: a pointer to a &struct phylink returned from phylink_create()
  2596. * @ifr: a pointer to a &struct ifreq for socket ioctls
  2597. * @cmd: ioctl cmd to execute
  2598. *
  2599. * Perform the specified MII ioctl on the PHY attached to the phylink instance
  2600. * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
  2601. *
  2602. * Returns: zero on success or negative error code.
  2603. *
  2604. * %SIOCGMIIPHY:
  2605. * read register from the current PHY.
  2606. * %SIOCGMIIREG:
  2607. * read register from the specified PHY.
  2608. * %SIOCSMIIREG:
  2609. * set a register on the specified PHY.
  2610. */
  2611. int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
  2612. {
  2613. struct mii_ioctl_data *mii = if_mii(ifr);
  2614. int ret;
  2615. ASSERT_RTNL();
  2616. if (pl->phydev) {
  2617. /* PHYs only exist for MLO_AN_PHY and SGMII */
  2618. switch (cmd) {
  2619. case SIOCGMIIPHY:
  2620. mii->phy_id = pl->phydev->mdio.addr;
  2621. fallthrough;
  2622. case SIOCGMIIREG:
  2623. ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
  2624. if (ret >= 0) {
  2625. mii->val_out = ret;
  2626. ret = 0;
  2627. }
  2628. break;
  2629. case SIOCSMIIREG:
  2630. ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
  2631. mii->val_in);
  2632. break;
  2633. default:
  2634. ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
  2635. break;
  2636. }
  2637. } else {
  2638. switch (cmd) {
  2639. case SIOCGMIIPHY:
  2640. mii->phy_id = 0;
  2641. fallthrough;
  2642. case SIOCGMIIREG:
  2643. ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
  2644. if (ret >= 0) {
  2645. mii->val_out = ret;
  2646. ret = 0;
  2647. }
  2648. break;
  2649. case SIOCSMIIREG:
  2650. ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
  2651. mii->val_in);
  2652. break;
  2653. default:
  2654. ret = -EOPNOTSUPP;
  2655. break;
  2656. }
  2657. }
  2658. return ret;
  2659. }
  2660. EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
  2661. /**
  2662. * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
  2663. * link partners
  2664. * @pl: a pointer to a &struct phylink returned from phylink_create()
  2665. * @sync: perform action synchronously
  2666. *
  2667. * If we have a PHY that is not part of a SFP module, then set the speed
  2668. * as described in the phy_speed_down() function. Please see this function
  2669. * for a description of the @sync parameter.
  2670. *
  2671. * Returns zero if there is no PHY, otherwise as per phy_speed_down().
  2672. */
  2673. int phylink_speed_down(struct phylink *pl, bool sync)
  2674. {
  2675. int ret = 0;
  2676. ASSERT_RTNL();
  2677. if (!pl->sfp_bus && pl->phydev)
  2678. ret = phy_speed_down(pl->phydev, sync);
  2679. return ret;
  2680. }
  2681. EXPORT_SYMBOL_GPL(phylink_speed_down);
  2682. /**
  2683. * phylink_speed_up() - restore the advertised speeds prior to the call to
  2684. * phylink_speed_down()
  2685. * @pl: a pointer to a &struct phylink returned from phylink_create()
  2686. *
  2687. * If we have a PHY that is not part of a SFP module, then restore the
  2688. * PHY speeds as per phy_speed_up().
  2689. *
  2690. * Returns zero if there is no PHY, otherwise as per phy_speed_up().
  2691. */
  2692. int phylink_speed_up(struct phylink *pl)
  2693. {
  2694. int ret = 0;
  2695. ASSERT_RTNL();
  2696. if (!pl->sfp_bus && pl->phydev)
  2697. ret = phy_speed_up(pl->phydev);
  2698. return ret;
  2699. }
  2700. EXPORT_SYMBOL_GPL(phylink_speed_up);
  2701. static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
  2702. {
  2703. struct phylink *pl = upstream;
  2704. pl->netdev->sfp_bus = bus;
  2705. }
  2706. static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
  2707. {
  2708. struct phylink *pl = upstream;
  2709. pl->netdev->sfp_bus = NULL;
  2710. }
  2711. static phy_interface_t phylink_choose_sfp_interface(struct phylink *pl,
  2712. const unsigned long *intf)
  2713. {
  2714. phy_interface_t interface;
  2715. size_t i;
  2716. interface = PHY_INTERFACE_MODE_NA;
  2717. for (i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); i++)
  2718. if (test_bit(phylink_sfp_interface_preference[i], intf)) {
  2719. interface = phylink_sfp_interface_preference[i];
  2720. break;
  2721. }
  2722. return interface;
  2723. }
  2724. static void phylink_sfp_set_config(struct phylink *pl, u8 mode,
  2725. unsigned long *supported,
  2726. struct phylink_link_state *state)
  2727. {
  2728. bool changed = false;
  2729. phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
  2730. phylink_an_mode_str(mode), phy_modes(state->interface),
  2731. __ETHTOOL_LINK_MODE_MASK_NBITS, supported);
  2732. if (!linkmode_equal(pl->supported, supported)) {
  2733. linkmode_copy(pl->supported, supported);
  2734. changed = true;
  2735. }
  2736. if (!linkmode_equal(pl->link_config.advertising, state->advertising)) {
  2737. linkmode_copy(pl->link_config.advertising, state->advertising);
  2738. changed = true;
  2739. }
  2740. if (pl->cur_link_an_mode != mode ||
  2741. pl->link_config.interface != state->interface) {
  2742. pl->cur_link_an_mode = mode;
  2743. pl->link_config.interface = state->interface;
  2744. changed = true;
  2745. phylink_info(pl, "switched to %s/%s link mode\n",
  2746. phylink_an_mode_str(mode),
  2747. phy_modes(state->interface));
  2748. }
  2749. if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
  2750. &pl->phylink_disable_state))
  2751. phylink_mac_initial_config(pl, false);
  2752. }
  2753. static int phylink_sfp_config_phy(struct phylink *pl, u8 mode,
  2754. struct phy_device *phy)
  2755. {
  2756. __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
  2757. __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
  2758. struct phylink_link_state config;
  2759. phy_interface_t iface;
  2760. int ret;
  2761. linkmode_copy(support, phy->supported);
  2762. memset(&config, 0, sizeof(config));
  2763. linkmode_copy(config.advertising, phy->advertising);
  2764. config.interface = PHY_INTERFACE_MODE_NA;
  2765. config.speed = SPEED_UNKNOWN;
  2766. config.duplex = DUPLEX_UNKNOWN;
  2767. config.pause = MLO_PAUSE_AN;
  2768. /* Ignore errors if we're expecting a PHY to attach later */
  2769. ret = phylink_validate(pl, support, &config);
  2770. if (ret) {
  2771. phylink_err(pl, "validation with support %*pb failed: %pe\n",
  2772. __ETHTOOL_LINK_MODE_MASK_NBITS, support,
  2773. ERR_PTR(ret));
  2774. return ret;
  2775. }
  2776. iface = sfp_select_interface(pl->sfp_bus, config.advertising);
  2777. if (iface == PHY_INTERFACE_MODE_NA) {
  2778. phylink_err(pl,
  2779. "selection of interface failed, advertisement %*pb\n",
  2780. __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
  2781. return -EINVAL;
  2782. }
  2783. config.interface = iface;
  2784. linkmode_copy(support1, support);
  2785. ret = phylink_validate(pl, support1, &config);
  2786. if (ret) {
  2787. phylink_err(pl,
  2788. "validation of %s/%s with support %*pb failed: %pe\n",
  2789. phylink_an_mode_str(mode),
  2790. phy_modes(config.interface),
  2791. __ETHTOOL_LINK_MODE_MASK_NBITS, support,
  2792. ERR_PTR(ret));
  2793. return ret;
  2794. }
  2795. pl->link_port = pl->sfp_port;
  2796. phylink_sfp_set_config(pl, mode, support, &config);
  2797. return 0;
  2798. }
  2799. static int phylink_sfp_config_optical(struct phylink *pl)
  2800. {
  2801. __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
  2802. DECLARE_PHY_INTERFACE_MASK(interfaces);
  2803. struct phylink_link_state config;
  2804. phy_interface_t interface;
  2805. int ret;
  2806. phylink_dbg(pl, "optical SFP: interfaces=[mac=%*pbl, sfp=%*pbl]\n",
  2807. (int)PHY_INTERFACE_MODE_MAX,
  2808. pl->config->supported_interfaces,
  2809. (int)PHY_INTERFACE_MODE_MAX,
  2810. pl->sfp_interfaces);
  2811. /* Find the union of the supported interfaces by the PCS/MAC and
  2812. * the SFP module.
  2813. */
  2814. phy_interface_and(interfaces, pl->config->supported_interfaces,
  2815. pl->sfp_interfaces);
  2816. if (phy_interface_empty(interfaces)) {
  2817. phylink_err(pl, "unsupported SFP module: no common interface modes\n");
  2818. return -EINVAL;
  2819. }
  2820. memset(&config, 0, sizeof(config));
  2821. linkmode_copy(support, pl->sfp_support);
  2822. linkmode_copy(config.advertising, pl->sfp_support);
  2823. config.speed = SPEED_UNKNOWN;
  2824. config.duplex = DUPLEX_UNKNOWN;
  2825. config.pause = MLO_PAUSE_AN;
  2826. /* For all the interfaces that are supported, reduce the sfp_support
  2827. * mask to only those link modes that can be supported.
  2828. */
  2829. ret = phylink_validate_mask(pl, NULL, pl->sfp_support, &config,
  2830. interfaces);
  2831. if (ret) {
  2832. phylink_err(pl, "unsupported SFP module: validation with support %*pb failed\n",
  2833. __ETHTOOL_LINK_MODE_MASK_NBITS, support);
  2834. return ret;
  2835. }
  2836. interface = phylink_choose_sfp_interface(pl, interfaces);
  2837. if (interface == PHY_INTERFACE_MODE_NA) {
  2838. phylink_err(pl, "failed to select SFP interface\n");
  2839. return -EINVAL;
  2840. }
  2841. phylink_dbg(pl, "optical SFP: chosen %s interface\n",
  2842. phy_modes(interface));
  2843. config.interface = interface;
  2844. /* Ignore errors if we're expecting a PHY to attach later */
  2845. ret = phylink_validate(pl, support, &config);
  2846. if (ret) {
  2847. phylink_err(pl, "validation with support %*pb failed: %pe\n",
  2848. __ETHTOOL_LINK_MODE_MASK_NBITS, support,
  2849. ERR_PTR(ret));
  2850. return ret;
  2851. }
  2852. pl->link_port = pl->sfp_port;
  2853. phylink_sfp_set_config(pl, MLO_AN_INBAND, pl->sfp_support, &config);
  2854. return 0;
  2855. }
  2856. static int phylink_sfp_module_insert(void *upstream,
  2857. const struct sfp_eeprom_id *id)
  2858. {
  2859. struct phylink *pl = upstream;
  2860. ASSERT_RTNL();
  2861. linkmode_zero(pl->sfp_support);
  2862. phy_interface_zero(pl->sfp_interfaces);
  2863. sfp_parse_support(pl->sfp_bus, id, pl->sfp_support, pl->sfp_interfaces);
  2864. pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, pl->sfp_support);
  2865. /* If this module may have a PHY connecting later, defer until later */
  2866. pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
  2867. if (pl->sfp_may_have_phy)
  2868. return 0;
  2869. return phylink_sfp_config_optical(pl);
  2870. }
  2871. static int phylink_sfp_module_start(void *upstream)
  2872. {
  2873. struct phylink *pl = upstream;
  2874. /* If this SFP module has a PHY, start the PHY now. */
  2875. if (pl->phydev) {
  2876. phy_start(pl->phydev);
  2877. return 0;
  2878. }
  2879. /* If the module may have a PHY but we didn't detect one we
  2880. * need to configure the MAC here.
  2881. */
  2882. if (!pl->sfp_may_have_phy)
  2883. return 0;
  2884. return phylink_sfp_config_optical(pl);
  2885. }
  2886. static void phylink_sfp_module_stop(void *upstream)
  2887. {
  2888. struct phylink *pl = upstream;
  2889. /* If this SFP module has a PHY, stop it. */
  2890. if (pl->phydev)
  2891. phy_stop(pl->phydev);
  2892. }
  2893. static void phylink_sfp_link_down(void *upstream)
  2894. {
  2895. struct phylink *pl = upstream;
  2896. ASSERT_RTNL();
  2897. phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
  2898. }
  2899. static void phylink_sfp_link_up(void *upstream)
  2900. {
  2901. struct phylink *pl = upstream;
  2902. ASSERT_RTNL();
  2903. phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
  2904. }
  2905. /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
  2906. * or 802.3z control word, so inband will not work.
  2907. */
  2908. static bool phylink_phy_no_inband(struct phy_device *phy)
  2909. {
  2910. return phy->is_c45 && phy_id_compare(phy->c45_ids.device_ids[1],
  2911. 0xae025150, 0xfffffff0);
  2912. }
  2913. static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
  2914. {
  2915. struct phylink *pl = upstream;
  2916. phy_interface_t interface;
  2917. u8 mode;
  2918. int ret;
  2919. /*
  2920. * This is the new way of dealing with flow control for PHYs,
  2921. * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
  2922. * phy drivers should not set SUPPORTED_[Asym_]Pause") except
  2923. * using our validate call to the MAC, we rely upon the MAC
  2924. * clearing the bits from both supported and advertising fields.
  2925. */
  2926. phy_support_asym_pause(phy);
  2927. if (phylink_phy_no_inband(phy))
  2928. mode = MLO_AN_PHY;
  2929. else
  2930. mode = MLO_AN_INBAND;
  2931. /* Set the PHY's host supported interfaces */
  2932. phy_interface_and(phy->host_interfaces, phylink_sfp_interfaces,
  2933. pl->config->supported_interfaces);
  2934. /* Do the initial configuration */
  2935. ret = phylink_sfp_config_phy(pl, mode, phy);
  2936. if (ret < 0)
  2937. return ret;
  2938. interface = pl->link_config.interface;
  2939. ret = phylink_attach_phy(pl, phy, interface);
  2940. if (ret < 0)
  2941. return ret;
  2942. ret = phylink_bringup_phy(pl, phy, interface);
  2943. if (ret)
  2944. phy_detach(phy);
  2945. return ret;
  2946. }
  2947. static void phylink_sfp_disconnect_phy(void *upstream,
  2948. struct phy_device *phydev)
  2949. {
  2950. phylink_disconnect_phy(upstream);
  2951. }
  2952. static const struct sfp_upstream_ops sfp_phylink_ops = {
  2953. .attach = phylink_sfp_attach,
  2954. .detach = phylink_sfp_detach,
  2955. .module_insert = phylink_sfp_module_insert,
  2956. .module_start = phylink_sfp_module_start,
  2957. .module_stop = phylink_sfp_module_stop,
  2958. .link_up = phylink_sfp_link_up,
  2959. .link_down = phylink_sfp_link_down,
  2960. .connect_phy = phylink_sfp_connect_phy,
  2961. .disconnect_phy = phylink_sfp_disconnect_phy,
  2962. };
  2963. /* Helpers for MAC drivers */
  2964. static struct {
  2965. int bit;
  2966. int speed;
  2967. } phylink_c73_priority_resolution[] = {
  2968. { ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, SPEED_100000 },
  2969. { ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, SPEED_100000 },
  2970. /* 100GBASE-KP4 and 100GBASE-CR10 not supported */
  2971. { ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, SPEED_40000 },
  2972. { ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, SPEED_40000 },
  2973. { ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, SPEED_10000 },
  2974. { ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, SPEED_10000 },
  2975. /* 5GBASE-KR not supported */
  2976. { ETHTOOL_LINK_MODE_2500baseX_Full_BIT, SPEED_2500 },
  2977. { ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, SPEED_1000 },
  2978. };
  2979. void phylink_resolve_c73(struct phylink_link_state *state)
  2980. {
  2981. int i;
  2982. for (i = 0; i < ARRAY_SIZE(phylink_c73_priority_resolution); i++) {
  2983. int bit = phylink_c73_priority_resolution[i].bit;
  2984. if (linkmode_test_bit(bit, state->advertising) &&
  2985. linkmode_test_bit(bit, state->lp_advertising))
  2986. break;
  2987. }
  2988. if (i < ARRAY_SIZE(phylink_c73_priority_resolution)) {
  2989. state->speed = phylink_c73_priority_resolution[i].speed;
  2990. state->duplex = DUPLEX_FULL;
  2991. } else {
  2992. /* negotiation failure */
  2993. state->link = false;
  2994. }
  2995. phylink_resolve_an_pause(state);
  2996. }
  2997. EXPORT_SYMBOL_GPL(phylink_resolve_c73);
  2998. static void phylink_decode_c37_word(struct phylink_link_state *state,
  2999. uint16_t config_reg, int speed)
  3000. {
  3001. int fd_bit;
  3002. if (speed == SPEED_2500)
  3003. fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
  3004. else
  3005. fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
  3006. mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
  3007. if (linkmode_test_bit(fd_bit, state->advertising) &&
  3008. linkmode_test_bit(fd_bit, state->lp_advertising)) {
  3009. state->speed = speed;
  3010. state->duplex = DUPLEX_FULL;
  3011. } else {
  3012. /* negotiation failure */
  3013. state->link = false;
  3014. }
  3015. phylink_resolve_an_pause(state);
  3016. }
  3017. static void phylink_decode_sgmii_word(struct phylink_link_state *state,
  3018. uint16_t config_reg)
  3019. {
  3020. if (!(config_reg & LPA_SGMII_LINK)) {
  3021. state->link = false;
  3022. return;
  3023. }
  3024. switch (config_reg & LPA_SGMII_SPD_MASK) {
  3025. case LPA_SGMII_10:
  3026. state->speed = SPEED_10;
  3027. break;
  3028. case LPA_SGMII_100:
  3029. state->speed = SPEED_100;
  3030. break;
  3031. case LPA_SGMII_1000:
  3032. state->speed = SPEED_1000;
  3033. break;
  3034. default:
  3035. state->link = false;
  3036. return;
  3037. }
  3038. if (config_reg & LPA_SGMII_FULL_DUPLEX)
  3039. state->duplex = DUPLEX_FULL;
  3040. else
  3041. state->duplex = DUPLEX_HALF;
  3042. }
  3043. /**
  3044. * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
  3045. * @state: a pointer to a struct phylink_link_state.
  3046. * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
  3047. *
  3048. * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
  3049. * code word. Decode the USXGMII code word and populate the corresponding fields
  3050. * (speed, duplex) into the phylink_link_state structure.
  3051. */
  3052. void phylink_decode_usxgmii_word(struct phylink_link_state *state,
  3053. uint16_t lpa)
  3054. {
  3055. switch (lpa & MDIO_USXGMII_SPD_MASK) {
  3056. case MDIO_USXGMII_10:
  3057. state->speed = SPEED_10;
  3058. break;
  3059. case MDIO_USXGMII_100:
  3060. state->speed = SPEED_100;
  3061. break;
  3062. case MDIO_USXGMII_1000:
  3063. state->speed = SPEED_1000;
  3064. break;
  3065. case MDIO_USXGMII_2500:
  3066. state->speed = SPEED_2500;
  3067. break;
  3068. case MDIO_USXGMII_5000:
  3069. state->speed = SPEED_5000;
  3070. break;
  3071. case MDIO_USXGMII_10G:
  3072. state->speed = SPEED_10000;
  3073. break;
  3074. default:
  3075. state->link = false;
  3076. return;
  3077. }
  3078. if (lpa & MDIO_USXGMII_FULL_DUPLEX)
  3079. state->duplex = DUPLEX_FULL;
  3080. else
  3081. state->duplex = DUPLEX_HALF;
  3082. }
  3083. EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
  3084. /**
  3085. * phylink_decode_usgmii_word() - decode the USGMII word from a MAC PCS
  3086. * @state: a pointer to a struct phylink_link_state.
  3087. * @lpa: a 16 bit value which stores the USGMII auto-negotiation word
  3088. *
  3089. * Helper for MAC PCS supporting the USGMII protocol and the auto-negotiation
  3090. * code word. Decode the USGMII code word and populate the corresponding fields
  3091. * (speed, duplex) into the phylink_link_state structure. The structure for this
  3092. * word is the same as the USXGMII word, except it only supports speeds up to
  3093. * 1Gbps.
  3094. */
  3095. static void phylink_decode_usgmii_word(struct phylink_link_state *state,
  3096. uint16_t lpa)
  3097. {
  3098. switch (lpa & MDIO_USXGMII_SPD_MASK) {
  3099. case MDIO_USXGMII_10:
  3100. state->speed = SPEED_10;
  3101. break;
  3102. case MDIO_USXGMII_100:
  3103. state->speed = SPEED_100;
  3104. break;
  3105. case MDIO_USXGMII_1000:
  3106. state->speed = SPEED_1000;
  3107. break;
  3108. default:
  3109. state->link = false;
  3110. return;
  3111. }
  3112. if (lpa & MDIO_USXGMII_FULL_DUPLEX)
  3113. state->duplex = DUPLEX_FULL;
  3114. else
  3115. state->duplex = DUPLEX_HALF;
  3116. }
  3117. /**
  3118. * phylink_mii_c22_pcs_decode_state() - Decode MAC PCS state from MII registers
  3119. * @state: a pointer to a &struct phylink_link_state.
  3120. * @bmsr: The value of the %MII_BMSR register
  3121. * @lpa: The value of the %MII_LPA register
  3122. *
  3123. * Helper for MAC PCS supporting the 802.3 clause 22 register set for
  3124. * clause 37 negotiation and/or SGMII control.
  3125. *
  3126. * Parse the Clause 37 or Cisco SGMII link partner negotiation word into
  3127. * the phylink @state structure. This is suitable to be used for implementing
  3128. * the pcs_get_state() member of the struct phylink_pcs_ops structure if
  3129. * accessing @bmsr and @lpa cannot be done with MDIO directly.
  3130. */
  3131. void phylink_mii_c22_pcs_decode_state(struct phylink_link_state *state,
  3132. u16 bmsr, u16 lpa)
  3133. {
  3134. state->link = !!(bmsr & BMSR_LSTATUS);
  3135. state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
  3136. /* If there is no link or autonegotiation is disabled, the LP advertisement
  3137. * data is not meaningful, so don't go any further.
  3138. */
  3139. if (!state->link || !linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  3140. state->advertising))
  3141. return;
  3142. switch (state->interface) {
  3143. case PHY_INTERFACE_MODE_1000BASEX:
  3144. phylink_decode_c37_word(state, lpa, SPEED_1000);
  3145. break;
  3146. case PHY_INTERFACE_MODE_2500BASEX:
  3147. phylink_decode_c37_word(state, lpa, SPEED_2500);
  3148. break;
  3149. case PHY_INTERFACE_MODE_SGMII:
  3150. case PHY_INTERFACE_MODE_QSGMII:
  3151. phylink_decode_sgmii_word(state, lpa);
  3152. break;
  3153. case PHY_INTERFACE_MODE_QUSGMII:
  3154. phylink_decode_usgmii_word(state, lpa);
  3155. break;
  3156. default:
  3157. state->link = false;
  3158. break;
  3159. }
  3160. }
  3161. EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_decode_state);
  3162. /**
  3163. * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
  3164. * @pcs: a pointer to a &struct mdio_device.
  3165. * @state: a pointer to a &struct phylink_link_state.
  3166. *
  3167. * Helper for MAC PCS supporting the 802.3 clause 22 register set for
  3168. * clause 37 negotiation and/or SGMII control.
  3169. *
  3170. * Read the MAC PCS state from the MII device configured in @config and
  3171. * parse the Clause 37 or Cisco SGMII link partner negotiation word into
  3172. * the phylink @state structure. This is suitable to be directly plugged
  3173. * into the pcs_get_state() member of the struct phylink_pcs_ops
  3174. * structure.
  3175. */
  3176. void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
  3177. struct phylink_link_state *state)
  3178. {
  3179. int bmsr, lpa;
  3180. bmsr = mdiodev_read(pcs, MII_BMSR);
  3181. lpa = mdiodev_read(pcs, MII_LPA);
  3182. if (bmsr < 0 || lpa < 0) {
  3183. state->link = false;
  3184. return;
  3185. }
  3186. phylink_mii_c22_pcs_decode_state(state, bmsr, lpa);
  3187. }
  3188. EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
  3189. /**
  3190. * phylink_mii_c22_pcs_encode_advertisement() - configure the clause 37 PCS
  3191. * advertisement
  3192. * @interface: the PHY interface mode being configured
  3193. * @advertising: the ethtool advertisement mask
  3194. *
  3195. * Helper for MAC PCS supporting the 802.3 clause 22 register set for
  3196. * clause 37 negotiation and/or SGMII control.
  3197. *
  3198. * Encode the clause 37 PCS advertisement as specified by @interface and
  3199. * @advertising.
  3200. *
  3201. * Return: The new value for @adv, or ``-EINVAL`` if it should not be changed.
  3202. */
  3203. int phylink_mii_c22_pcs_encode_advertisement(phy_interface_t interface,
  3204. const unsigned long *advertising)
  3205. {
  3206. u16 adv;
  3207. switch (interface) {
  3208. case PHY_INTERFACE_MODE_1000BASEX:
  3209. case PHY_INTERFACE_MODE_2500BASEX:
  3210. adv = ADVERTISE_1000XFULL;
  3211. if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  3212. advertising))
  3213. adv |= ADVERTISE_1000XPAUSE;
  3214. if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  3215. advertising))
  3216. adv |= ADVERTISE_1000XPSE_ASYM;
  3217. return adv;
  3218. case PHY_INTERFACE_MODE_SGMII:
  3219. case PHY_INTERFACE_MODE_QSGMII:
  3220. return 0x0001;
  3221. default:
  3222. /* Nothing to do for other modes */
  3223. return -EINVAL;
  3224. }
  3225. }
  3226. EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_encode_advertisement);
  3227. /**
  3228. * phylink_mii_c22_pcs_config() - configure clause 22 PCS
  3229. * @pcs: a pointer to a &struct mdio_device.
  3230. * @interface: the PHY interface mode being configured
  3231. * @advertising: the ethtool advertisement mask
  3232. * @neg_mode: PCS negotiation mode
  3233. *
  3234. * Configure a Clause 22 PCS PHY with the appropriate negotiation
  3235. * parameters for the @mode, @interface and @advertising parameters.
  3236. * Returns negative error number on failure, zero if the advertisement
  3237. * has not changed, or positive if there is a change.
  3238. */
  3239. int phylink_mii_c22_pcs_config(struct mdio_device *pcs,
  3240. phy_interface_t interface,
  3241. const unsigned long *advertising,
  3242. unsigned int neg_mode)
  3243. {
  3244. bool changed = 0;
  3245. u16 bmcr;
  3246. int ret, adv;
  3247. adv = phylink_mii_c22_pcs_encode_advertisement(interface, advertising);
  3248. if (adv >= 0) {
  3249. ret = mdiobus_modify_changed(pcs->bus, pcs->addr,
  3250. MII_ADVERTISE, 0xffff, adv);
  3251. if (ret < 0)
  3252. return ret;
  3253. changed = ret;
  3254. }
  3255. if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
  3256. bmcr = BMCR_ANENABLE;
  3257. else
  3258. bmcr = 0;
  3259. /* Configure the inband state. Ensure ISOLATE bit is disabled */
  3260. ret = mdiodev_modify(pcs, MII_BMCR, BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
  3261. if (ret < 0)
  3262. return ret;
  3263. return changed;
  3264. }
  3265. EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
  3266. /**
  3267. * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
  3268. * @pcs: a pointer to a &struct mdio_device.
  3269. *
  3270. * Helper for MAC PCS supporting the 802.3 clause 22 register set for
  3271. * clause 37 negotiation.
  3272. *
  3273. * Restart the clause 37 negotiation with the link partner. This is
  3274. * suitable to be directly plugged into the pcs_get_state() member
  3275. * of the struct phylink_pcs_ops structure.
  3276. */
  3277. void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
  3278. {
  3279. int val = mdiodev_read(pcs, MII_BMCR);
  3280. if (val >= 0) {
  3281. val |= BMCR_ANRESTART;
  3282. mdiodev_write(pcs, MII_BMCR, val);
  3283. }
  3284. }
  3285. EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
  3286. void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
  3287. struct phylink_link_state *state)
  3288. {
  3289. struct mii_bus *bus = pcs->bus;
  3290. int addr = pcs->addr;
  3291. int stat;
  3292. stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
  3293. if (stat < 0) {
  3294. state->link = false;
  3295. return;
  3296. }
  3297. state->link = !!(stat & MDIO_STAT1_LSTATUS);
  3298. if (!state->link)
  3299. return;
  3300. switch (state->interface) {
  3301. case PHY_INTERFACE_MODE_10GBASER:
  3302. state->speed = SPEED_10000;
  3303. state->duplex = DUPLEX_FULL;
  3304. break;
  3305. default:
  3306. break;
  3307. }
  3308. }
  3309. EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
  3310. static int __init phylink_init(void)
  3311. {
  3312. for (int i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); ++i)
  3313. __set_bit(phylink_sfp_interface_preference[i],
  3314. phylink_sfp_interfaces);
  3315. return 0;
  3316. }
  3317. module_init(phylink_init);
  3318. MODULE_LICENSE("GPL v2");
  3319. MODULE_DESCRIPTION("phylink models the MAC to optional PHY connection");