phylink.c 124 KB

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