phy_device.c 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Framework for finding and configuring PHYs.
  3. * Also contains generic PHY driver
  4. *
  5. * Author: Andy Fleming
  6. *
  7. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/acpi.h>
  11. #include <linux/bitmap.h>
  12. #include <linux/delay.h>
  13. #include <linux/errno.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/ethtool.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/list.h>
  21. #include <linux/mdio.h>
  22. #include <linux/mii.h>
  23. #include <linux/mm.h>
  24. #include <linux/module.h>
  25. #include <linux/of.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/phy.h>
  28. #include <linux/phylib_stubs.h>
  29. #include <linux/phy_led_triggers.h>
  30. #include <linux/phy_link_topology.h>
  31. #include <linux/pse-pd/pse.h>
  32. #include <linux/property.h>
  33. #include <linux/rtnetlink.h>
  34. #include <linux/sfp.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/slab.h>
  37. #include <linux/string.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/unistd.h>
  40. MODULE_DESCRIPTION("PHY library");
  41. MODULE_AUTHOR("Andy Fleming");
  42. MODULE_LICENSE("GPL");
  43. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
  44. EXPORT_SYMBOL_GPL(phy_basic_features);
  45. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
  46. EXPORT_SYMBOL_GPL(phy_basic_t1_features);
  47. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1s_p2mp_features) __ro_after_init;
  48. EXPORT_SYMBOL_GPL(phy_basic_t1s_p2mp_features);
  49. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
  50. EXPORT_SYMBOL_GPL(phy_gbit_features);
  51. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
  52. EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
  53. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
  54. EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features);
  55. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
  56. EXPORT_SYMBOL_GPL(phy_10gbit_features);
  57. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
  58. EXPORT_SYMBOL_GPL(phy_10gbit_fec_features);
  59. const int phy_basic_ports_array[3] = {
  60. ETHTOOL_LINK_MODE_Autoneg_BIT,
  61. ETHTOOL_LINK_MODE_TP_BIT,
  62. ETHTOOL_LINK_MODE_MII_BIT,
  63. };
  64. EXPORT_SYMBOL_GPL(phy_basic_ports_array);
  65. const int phy_fibre_port_array[1] = {
  66. ETHTOOL_LINK_MODE_FIBRE_BIT,
  67. };
  68. EXPORT_SYMBOL_GPL(phy_fibre_port_array);
  69. const int phy_all_ports_features_array[7] = {
  70. ETHTOOL_LINK_MODE_Autoneg_BIT,
  71. ETHTOOL_LINK_MODE_TP_BIT,
  72. ETHTOOL_LINK_MODE_MII_BIT,
  73. ETHTOOL_LINK_MODE_FIBRE_BIT,
  74. ETHTOOL_LINK_MODE_AUI_BIT,
  75. ETHTOOL_LINK_MODE_BNC_BIT,
  76. ETHTOOL_LINK_MODE_Backplane_BIT,
  77. };
  78. EXPORT_SYMBOL_GPL(phy_all_ports_features_array);
  79. const int phy_10_100_features_array[4] = {
  80. ETHTOOL_LINK_MODE_10baseT_Half_BIT,
  81. ETHTOOL_LINK_MODE_10baseT_Full_BIT,
  82. ETHTOOL_LINK_MODE_100baseT_Half_BIT,
  83. ETHTOOL_LINK_MODE_100baseT_Full_BIT,
  84. };
  85. EXPORT_SYMBOL_GPL(phy_10_100_features_array);
  86. const int phy_basic_t1_features_array[3] = {
  87. ETHTOOL_LINK_MODE_TP_BIT,
  88. ETHTOOL_LINK_MODE_10baseT1L_Full_BIT,
  89. ETHTOOL_LINK_MODE_100baseT1_Full_BIT,
  90. };
  91. EXPORT_SYMBOL_GPL(phy_basic_t1_features_array);
  92. const int phy_basic_t1s_p2mp_features_array[2] = {
  93. ETHTOOL_LINK_MODE_TP_BIT,
  94. ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT,
  95. };
  96. EXPORT_SYMBOL_GPL(phy_basic_t1s_p2mp_features_array);
  97. const int phy_gbit_features_array[2] = {
  98. ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
  99. ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  100. };
  101. EXPORT_SYMBOL_GPL(phy_gbit_features_array);
  102. const int phy_10gbit_features_array[1] = {
  103. ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
  104. };
  105. EXPORT_SYMBOL_GPL(phy_10gbit_features_array);
  106. static const int phy_10gbit_fec_features_array[1] = {
  107. ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
  108. };
  109. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
  110. EXPORT_SYMBOL_GPL(phy_10gbit_full_features);
  111. static const int phy_10gbit_full_features_array[] = {
  112. ETHTOOL_LINK_MODE_10baseT_Full_BIT,
  113. ETHTOOL_LINK_MODE_100baseT_Full_BIT,
  114. ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  115. ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
  116. };
  117. static const int phy_eee_cap1_features_array[] = {
  118. ETHTOOL_LINK_MODE_100baseT_Full_BIT,
  119. ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  120. ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
  121. ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
  122. ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
  123. ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
  124. };
  125. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap1_features) __ro_after_init;
  126. EXPORT_SYMBOL_GPL(phy_eee_cap1_features);
  127. static const int phy_eee_cap2_features_array[] = {
  128. ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
  129. ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
  130. };
  131. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap2_features) __ro_after_init;
  132. EXPORT_SYMBOL_GPL(phy_eee_cap2_features);
  133. static void features_init(void)
  134. {
  135. /* 10/100 half/full*/
  136. linkmode_set_bit_array(phy_basic_ports_array,
  137. ARRAY_SIZE(phy_basic_ports_array),
  138. phy_basic_features);
  139. linkmode_set_bit_array(phy_10_100_features_array,
  140. ARRAY_SIZE(phy_10_100_features_array),
  141. phy_basic_features);
  142. /* 100 full, TP */
  143. linkmode_set_bit_array(phy_basic_t1_features_array,
  144. ARRAY_SIZE(phy_basic_t1_features_array),
  145. phy_basic_t1_features);
  146. /* 10 half, P2MP, TP */
  147. linkmode_set_bit_array(phy_basic_t1s_p2mp_features_array,
  148. ARRAY_SIZE(phy_basic_t1s_p2mp_features_array),
  149. phy_basic_t1s_p2mp_features);
  150. /* 10/100 half/full + 1000 half/full */
  151. linkmode_set_bit_array(phy_basic_ports_array,
  152. ARRAY_SIZE(phy_basic_ports_array),
  153. phy_gbit_features);
  154. linkmode_set_bit_array(phy_10_100_features_array,
  155. ARRAY_SIZE(phy_10_100_features_array),
  156. phy_gbit_features);
  157. linkmode_set_bit_array(phy_gbit_features_array,
  158. ARRAY_SIZE(phy_gbit_features_array),
  159. phy_gbit_features);
  160. /* 10/100 half/full + 1000 half/full + fibre*/
  161. linkmode_set_bit_array(phy_basic_ports_array,
  162. ARRAY_SIZE(phy_basic_ports_array),
  163. phy_gbit_fibre_features);
  164. linkmode_set_bit_array(phy_10_100_features_array,
  165. ARRAY_SIZE(phy_10_100_features_array),
  166. phy_gbit_fibre_features);
  167. linkmode_set_bit_array(phy_gbit_features_array,
  168. ARRAY_SIZE(phy_gbit_features_array),
  169. phy_gbit_fibre_features);
  170. linkmode_set_bit_array(phy_fibre_port_array,
  171. ARRAY_SIZE(phy_fibre_port_array),
  172. phy_gbit_fibre_features);
  173. /* 10/100 half/full + 1000 half/full + TP/MII/FIBRE/AUI/BNC/Backplane*/
  174. linkmode_set_bit_array(phy_all_ports_features_array,
  175. ARRAY_SIZE(phy_all_ports_features_array),
  176. phy_gbit_all_ports_features);
  177. linkmode_set_bit_array(phy_10_100_features_array,
  178. ARRAY_SIZE(phy_10_100_features_array),
  179. phy_gbit_all_ports_features);
  180. linkmode_set_bit_array(phy_gbit_features_array,
  181. ARRAY_SIZE(phy_gbit_features_array),
  182. phy_gbit_all_ports_features);
  183. /* 10/100 half/full + 1000 half/full + 10G full*/
  184. linkmode_set_bit_array(phy_all_ports_features_array,
  185. ARRAY_SIZE(phy_all_ports_features_array),
  186. phy_10gbit_features);
  187. linkmode_set_bit_array(phy_10_100_features_array,
  188. ARRAY_SIZE(phy_10_100_features_array),
  189. phy_10gbit_features);
  190. linkmode_set_bit_array(phy_gbit_features_array,
  191. ARRAY_SIZE(phy_gbit_features_array),
  192. phy_10gbit_features);
  193. linkmode_set_bit_array(phy_10gbit_features_array,
  194. ARRAY_SIZE(phy_10gbit_features_array),
  195. phy_10gbit_features);
  196. /* 10/100/1000/10G full */
  197. linkmode_set_bit_array(phy_all_ports_features_array,
  198. ARRAY_SIZE(phy_all_ports_features_array),
  199. phy_10gbit_full_features);
  200. linkmode_set_bit_array(phy_10gbit_full_features_array,
  201. ARRAY_SIZE(phy_10gbit_full_features_array),
  202. phy_10gbit_full_features);
  203. /* 10G FEC only */
  204. linkmode_set_bit_array(phy_10gbit_fec_features_array,
  205. ARRAY_SIZE(phy_10gbit_fec_features_array),
  206. phy_10gbit_fec_features);
  207. linkmode_set_bit_array(phy_eee_cap1_features_array,
  208. ARRAY_SIZE(phy_eee_cap1_features_array),
  209. phy_eee_cap1_features);
  210. linkmode_set_bit_array(phy_eee_cap2_features_array,
  211. ARRAY_SIZE(phy_eee_cap2_features_array),
  212. phy_eee_cap2_features);
  213. }
  214. void phy_device_free(struct phy_device *phydev)
  215. {
  216. put_device(&phydev->mdio.dev);
  217. }
  218. EXPORT_SYMBOL(phy_device_free);
  219. static void phy_mdio_device_free(struct mdio_device *mdiodev)
  220. {
  221. struct phy_device *phydev;
  222. phydev = container_of(mdiodev, struct phy_device, mdio);
  223. phy_device_free(phydev);
  224. }
  225. static void phy_device_release(struct device *dev)
  226. {
  227. fwnode_handle_put(dev->fwnode);
  228. kfree(to_phy_device(dev));
  229. }
  230. static void phy_mdio_device_remove(struct mdio_device *mdiodev)
  231. {
  232. struct phy_device *phydev;
  233. phydev = container_of(mdiodev, struct phy_device, mdio);
  234. phy_device_remove(phydev);
  235. }
  236. static struct phy_driver genphy_driver;
  237. static LIST_HEAD(phy_fixup_list);
  238. static DEFINE_MUTEX(phy_fixup_lock);
  239. static bool phy_drv_wol_enabled(struct phy_device *phydev)
  240. {
  241. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  242. phy_ethtool_get_wol(phydev, &wol);
  243. return wol.wolopts != 0;
  244. }
  245. static void phy_link_change(struct phy_device *phydev, bool up)
  246. {
  247. struct net_device *netdev = phydev->attached_dev;
  248. if (up)
  249. netif_carrier_on(netdev);
  250. else
  251. netif_carrier_off(netdev);
  252. phydev->adjust_link(netdev);
  253. if (phydev->mii_ts && phydev->mii_ts->link_state)
  254. phydev->mii_ts->link_state(phydev->mii_ts, phydev);
  255. }
  256. /**
  257. * phy_uses_state_machine - test whether consumer driver uses PAL state machine
  258. * @phydev: the target PHY device structure
  259. *
  260. * Ultimately, this aims to indirectly determine whether the PHY is attached
  261. * to a consumer which uses the state machine by calling phy_start() and
  262. * phy_stop().
  263. *
  264. * When the PHY driver consumer uses phylib, it must have previously called
  265. * phy_connect_direct() or one of its derivatives, so that phy_prepare_link()
  266. * has set up a hook for monitoring state changes.
  267. *
  268. * When the PHY driver is used by the MAC driver consumer through phylink (the
  269. * only other provider of a phy_link_change() method), using the PHY state
  270. * machine is not optional.
  271. *
  272. * Return: true if consumer calls phy_start() and phy_stop(), false otherwise.
  273. */
  274. static bool phy_uses_state_machine(struct phy_device *phydev)
  275. {
  276. if (phydev->phy_link_change == phy_link_change)
  277. return phydev->attached_dev && phydev->adjust_link;
  278. /* phydev->phy_link_change is implicitly phylink_phy_change() */
  279. return true;
  280. }
  281. static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
  282. {
  283. struct device_driver *drv = phydev->mdio.dev.driver;
  284. struct phy_driver *phydrv = to_phy_driver(drv);
  285. struct net_device *netdev = phydev->attached_dev;
  286. if (!drv || !phydrv->suspend)
  287. return false;
  288. /* If the PHY on the mido bus is not attached but has WOL enabled
  289. * we cannot suspend the PHY.
  290. */
  291. if (!netdev && phy_drv_wol_enabled(phydev))
  292. return false;
  293. /* PHY not attached? May suspend if the PHY has not already been
  294. * suspended as part of a prior call to phy_disconnect() ->
  295. * phy_detach() -> phy_suspend() because the parent netdev might be the
  296. * MDIO bus driver and clock gated at this point.
  297. */
  298. if (!netdev)
  299. goto out;
  300. if (netdev->ethtool->wol_enabled)
  301. return false;
  302. /* As long as not all affected network drivers support the
  303. * wol_enabled flag, let's check for hints that WoL is enabled.
  304. * Don't suspend PHY if the attached netdev parent may wake up.
  305. * The parent may point to a PCI device, as in tg3 driver.
  306. */
  307. if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
  308. return false;
  309. /* Also don't suspend PHY if the netdev itself may wakeup. This
  310. * is the case for devices w/o underlaying pwr. mgmt. aware bus,
  311. * e.g. SoC devices.
  312. */
  313. if (device_may_wakeup(&netdev->dev))
  314. return false;
  315. out:
  316. return !phydev->suspended;
  317. }
  318. static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
  319. {
  320. struct phy_device *phydev = to_phy_device(dev);
  321. if (phydev->mac_managed_pm)
  322. return 0;
  323. /* Wakeup interrupts may occur during the system sleep transition when
  324. * the PHY is inaccessible. Set flag to postpone handling until the PHY
  325. * has resumed. Wait for concurrent interrupt handler to complete.
  326. */
  327. if (phy_interrupt_is_valid(phydev)) {
  328. phydev->irq_suspended = 1;
  329. synchronize_irq(phydev->irq);
  330. }
  331. /* We must stop the state machine manually, otherwise it stops out of
  332. * control, possibly with the phydev->lock held. Upon resume, netdev
  333. * may call phy routines that try to grab the same lock, and that may
  334. * lead to a deadlock.
  335. */
  336. if (phy_uses_state_machine(phydev))
  337. phy_stop_machine(phydev);
  338. if (!mdio_bus_phy_may_suspend(phydev))
  339. return 0;
  340. phydev->suspended_by_mdio_bus = 1;
  341. return phy_suspend(phydev);
  342. }
  343. static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
  344. {
  345. struct phy_device *phydev = to_phy_device(dev);
  346. int ret;
  347. if (phydev->mac_managed_pm)
  348. return 0;
  349. if (!phydev->suspended_by_mdio_bus)
  350. goto no_resume;
  351. phydev->suspended_by_mdio_bus = 0;
  352. /* If we managed to get here with the PHY state machine in a state
  353. * neither PHY_HALTED, PHY_READY nor PHY_UP, this is an indication
  354. * that something went wrong and we should most likely be using
  355. * MAC managed PM, but we are not.
  356. */
  357. WARN_ON(phydev->state != PHY_HALTED && phydev->state != PHY_READY &&
  358. phydev->state != PHY_UP);
  359. ret = phy_init_hw(phydev);
  360. if (ret < 0)
  361. return ret;
  362. ret = phy_resume(phydev);
  363. if (ret < 0)
  364. return ret;
  365. no_resume:
  366. if (phy_interrupt_is_valid(phydev)) {
  367. phydev->irq_suspended = 0;
  368. synchronize_irq(phydev->irq);
  369. /* Rerun interrupts which were postponed by phy_interrupt()
  370. * because they occurred during the system sleep transition.
  371. */
  372. if (phydev->irq_rerun) {
  373. phydev->irq_rerun = 0;
  374. enable_irq(phydev->irq);
  375. irq_wake_thread(phydev->irq, phydev);
  376. }
  377. }
  378. if (phy_uses_state_machine(phydev))
  379. phy_start_machine(phydev);
  380. return 0;
  381. }
  382. static SIMPLE_DEV_PM_OPS(mdio_bus_phy_pm_ops, mdio_bus_phy_suspend,
  383. mdio_bus_phy_resume);
  384. /**
  385. * phy_register_fixup - creates a new phy_fixup and adds it to the list
  386. * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
  387. * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
  388. * It can also be PHY_ANY_UID
  389. * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
  390. * comparison
  391. * @run: The actual code to be run when a matching PHY is found
  392. */
  393. int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
  394. int (*run)(struct phy_device *))
  395. {
  396. struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
  397. if (!fixup)
  398. return -ENOMEM;
  399. strscpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
  400. fixup->phy_uid = phy_uid;
  401. fixup->phy_uid_mask = phy_uid_mask;
  402. fixup->run = run;
  403. mutex_lock(&phy_fixup_lock);
  404. list_add_tail(&fixup->list, &phy_fixup_list);
  405. mutex_unlock(&phy_fixup_lock);
  406. return 0;
  407. }
  408. EXPORT_SYMBOL(phy_register_fixup);
  409. /* Registers a fixup to be run on any PHY with the UID in phy_uid */
  410. int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
  411. int (*run)(struct phy_device *))
  412. {
  413. return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
  414. }
  415. EXPORT_SYMBOL(phy_register_fixup_for_uid);
  416. /* Registers a fixup to be run on the PHY with id string bus_id */
  417. int phy_register_fixup_for_id(const char *bus_id,
  418. int (*run)(struct phy_device *))
  419. {
  420. return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
  421. }
  422. EXPORT_SYMBOL(phy_register_fixup_for_id);
  423. /**
  424. * phy_unregister_fixup - remove a phy_fixup from the list
  425. * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
  426. * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
  427. * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
  428. */
  429. int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
  430. {
  431. struct list_head *pos, *n;
  432. struct phy_fixup *fixup;
  433. int ret;
  434. ret = -ENODEV;
  435. mutex_lock(&phy_fixup_lock);
  436. list_for_each_safe(pos, n, &phy_fixup_list) {
  437. fixup = list_entry(pos, struct phy_fixup, list);
  438. if ((!strcmp(fixup->bus_id, bus_id)) &&
  439. phy_id_compare(fixup->phy_uid, phy_uid, phy_uid_mask)) {
  440. list_del(&fixup->list);
  441. kfree(fixup);
  442. ret = 0;
  443. break;
  444. }
  445. }
  446. mutex_unlock(&phy_fixup_lock);
  447. return ret;
  448. }
  449. EXPORT_SYMBOL(phy_unregister_fixup);
  450. /* Unregisters a fixup of any PHY with the UID in phy_uid */
  451. int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
  452. {
  453. return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
  454. }
  455. EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
  456. /* Unregisters a fixup of the PHY with id string bus_id */
  457. int phy_unregister_fixup_for_id(const char *bus_id)
  458. {
  459. return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
  460. }
  461. EXPORT_SYMBOL(phy_unregister_fixup_for_id);
  462. /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
  463. * Fixups can be set to match any in one or more fields.
  464. */
  465. static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
  466. {
  467. if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
  468. if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
  469. return 0;
  470. if (!phy_id_compare(phydev->phy_id, fixup->phy_uid,
  471. fixup->phy_uid_mask))
  472. if (fixup->phy_uid != PHY_ANY_UID)
  473. return 0;
  474. return 1;
  475. }
  476. /* Runs any matching fixups for this phydev */
  477. static int phy_scan_fixups(struct phy_device *phydev)
  478. {
  479. struct phy_fixup *fixup;
  480. mutex_lock(&phy_fixup_lock);
  481. list_for_each_entry(fixup, &phy_fixup_list, list) {
  482. if (phy_needs_fixup(phydev, fixup)) {
  483. int err = fixup->run(phydev);
  484. if (err < 0) {
  485. mutex_unlock(&phy_fixup_lock);
  486. return err;
  487. }
  488. phydev->has_fixups = true;
  489. }
  490. }
  491. mutex_unlock(&phy_fixup_lock);
  492. return 0;
  493. }
  494. static int phy_bus_match(struct device *dev, const struct device_driver *drv)
  495. {
  496. struct phy_device *phydev = to_phy_device(dev);
  497. const struct phy_driver *phydrv = to_phy_driver(drv);
  498. const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
  499. int i;
  500. if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
  501. return 0;
  502. if (phydrv->match_phy_device)
  503. return phydrv->match_phy_device(phydev);
  504. if (phydev->is_c45) {
  505. for (i = 1; i < num_ids; i++) {
  506. if (phydev->c45_ids.device_ids[i] == 0xffffffff)
  507. continue;
  508. if (phy_id_compare(phydev->c45_ids.device_ids[i],
  509. phydrv->phy_id, phydrv->phy_id_mask))
  510. return 1;
  511. }
  512. return 0;
  513. } else {
  514. return phy_id_compare(phydev->phy_id, phydrv->phy_id,
  515. phydrv->phy_id_mask);
  516. }
  517. }
  518. static ssize_t
  519. phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
  520. {
  521. struct phy_device *phydev = to_phy_device(dev);
  522. return sysfs_emit(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
  523. }
  524. static DEVICE_ATTR_RO(phy_id);
  525. static ssize_t
  526. phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
  527. {
  528. struct phy_device *phydev = to_phy_device(dev);
  529. const char *mode = NULL;
  530. if (phy_is_internal(phydev))
  531. mode = "internal";
  532. else
  533. mode = phy_modes(phydev->interface);
  534. return sysfs_emit(buf, "%s\n", mode);
  535. }
  536. static DEVICE_ATTR_RO(phy_interface);
  537. static ssize_t
  538. phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
  539. char *buf)
  540. {
  541. struct phy_device *phydev = to_phy_device(dev);
  542. return sysfs_emit(buf, "%d\n", phydev->has_fixups);
  543. }
  544. static DEVICE_ATTR_RO(phy_has_fixups);
  545. static ssize_t phy_dev_flags_show(struct device *dev,
  546. struct device_attribute *attr,
  547. char *buf)
  548. {
  549. struct phy_device *phydev = to_phy_device(dev);
  550. return sysfs_emit(buf, "0x%08x\n", phydev->dev_flags);
  551. }
  552. static DEVICE_ATTR_RO(phy_dev_flags);
  553. static struct attribute *phy_dev_attrs[] = {
  554. &dev_attr_phy_id.attr,
  555. &dev_attr_phy_interface.attr,
  556. &dev_attr_phy_has_fixups.attr,
  557. &dev_attr_phy_dev_flags.attr,
  558. NULL,
  559. };
  560. ATTRIBUTE_GROUPS(phy_dev);
  561. static const struct device_type mdio_bus_phy_type = {
  562. .name = "PHY",
  563. .groups = phy_dev_groups,
  564. .release = phy_device_release,
  565. .pm = pm_ptr(&mdio_bus_phy_pm_ops),
  566. };
  567. static int phy_request_driver_module(struct phy_device *dev, u32 phy_id)
  568. {
  569. int ret;
  570. ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
  571. MDIO_ID_ARGS(phy_id));
  572. /* We only check for failures in executing the usermode binary,
  573. * not whether a PHY driver module exists for the PHY ID.
  574. * Accept -ENOENT because this may occur in case no initramfs exists,
  575. * then modprobe isn't available.
  576. */
  577. if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
  578. phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n",
  579. ret, (unsigned long)phy_id);
  580. return ret;
  581. }
  582. return 0;
  583. }
  584. struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
  585. bool is_c45,
  586. struct phy_c45_device_ids *c45_ids)
  587. {
  588. struct phy_device *dev;
  589. struct mdio_device *mdiodev;
  590. int ret = 0;
  591. /* We allocate the device, and initialize the default values */
  592. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  593. if (!dev)
  594. return ERR_PTR(-ENOMEM);
  595. mdiodev = &dev->mdio;
  596. mdiodev->dev.parent = &bus->dev;
  597. mdiodev->dev.bus = &mdio_bus_type;
  598. mdiodev->dev.type = &mdio_bus_phy_type;
  599. mdiodev->bus = bus;
  600. mdiodev->bus_match = phy_bus_match;
  601. mdiodev->addr = addr;
  602. mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
  603. mdiodev->device_free = phy_mdio_device_free;
  604. mdiodev->device_remove = phy_mdio_device_remove;
  605. mdiodev->reset_state = -1;
  606. dev->speed = SPEED_UNKNOWN;
  607. dev->duplex = DUPLEX_UNKNOWN;
  608. dev->pause = 0;
  609. dev->asym_pause = 0;
  610. dev->link = 0;
  611. dev->port = PORT_TP;
  612. dev->interface = PHY_INTERFACE_MODE_GMII;
  613. dev->autoneg = AUTONEG_ENABLE;
  614. dev->pma_extable = -ENODATA;
  615. dev->is_c45 = is_c45;
  616. dev->phy_id = phy_id;
  617. if (c45_ids)
  618. dev->c45_ids = *c45_ids;
  619. dev->irq = bus->irq[addr];
  620. dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
  621. device_initialize(&mdiodev->dev);
  622. dev->state = PHY_DOWN;
  623. INIT_LIST_HEAD(&dev->leds);
  624. mutex_init(&dev->lock);
  625. INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
  626. /* Request the appropriate module unconditionally; don't
  627. * bother trying to do so only if it isn't already loaded,
  628. * because that gets complicated. A hotplug event would have
  629. * done an unconditional modprobe anyway.
  630. * We don't do normal hotplug because it won't work for MDIO
  631. * -- because it relies on the device staying around for long
  632. * enough for the driver to get loaded. With MDIO, the NIC
  633. * driver will get bored and give up as soon as it finds that
  634. * there's no driver _already_ loaded.
  635. */
  636. if (is_c45 && c45_ids) {
  637. const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
  638. int i;
  639. for (i = 1; i < num_ids; i++) {
  640. if (c45_ids->device_ids[i] == 0xffffffff)
  641. continue;
  642. ret = phy_request_driver_module(dev,
  643. c45_ids->device_ids[i]);
  644. if (ret)
  645. break;
  646. }
  647. } else {
  648. ret = phy_request_driver_module(dev, phy_id);
  649. }
  650. if (ret) {
  651. put_device(&mdiodev->dev);
  652. dev = ERR_PTR(ret);
  653. }
  654. return dev;
  655. }
  656. EXPORT_SYMBOL(phy_device_create);
  657. /* phy_c45_probe_present - checks to see if a MMD is present in the package
  658. * @bus: the target MII bus
  659. * @prtad: PHY package address on the MII bus
  660. * @devad: PHY device (MMD) address
  661. *
  662. * Read the MDIO_STAT2 register, and check whether a device is responding
  663. * at this address.
  664. *
  665. * Returns: negative error number on bus access error, zero if no device
  666. * is responding, or positive if a device is present.
  667. */
  668. static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad)
  669. {
  670. int stat2;
  671. stat2 = mdiobus_c45_read(bus, prtad, devad, MDIO_STAT2);
  672. if (stat2 < 0)
  673. return stat2;
  674. return (stat2 & MDIO_STAT2_DEVPRST) == MDIO_STAT2_DEVPRST_VAL;
  675. }
  676. /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
  677. * @bus: the target MII bus
  678. * @addr: PHY address on the MII bus
  679. * @dev_addr: MMD address in the PHY.
  680. * @devices_in_package: where to store the devices in package information.
  681. *
  682. * Description: reads devices in package registers of a MMD at @dev_addr
  683. * from PHY at @addr on @bus.
  684. *
  685. * Returns: 0 on success, -EIO on failure.
  686. */
  687. static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
  688. u32 *devices_in_package)
  689. {
  690. int phy_reg;
  691. phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS2);
  692. if (phy_reg < 0)
  693. return -EIO;
  694. *devices_in_package = phy_reg << 16;
  695. phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS1);
  696. if (phy_reg < 0)
  697. return -EIO;
  698. *devices_in_package |= phy_reg;
  699. return 0;
  700. }
  701. /**
  702. * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
  703. * @bus: the target MII bus
  704. * @addr: PHY address on the MII bus
  705. * @c45_ids: where to store the c45 ID information.
  706. *
  707. * Read the PHY "devices in package". If this appears to be valid, read
  708. * the PHY identifiers for each device. Return the "devices in package"
  709. * and identifiers in @c45_ids.
  710. *
  711. * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
  712. * the "devices in package" is invalid or no device responds.
  713. */
  714. static int get_phy_c45_ids(struct mii_bus *bus, int addr,
  715. struct phy_c45_device_ids *c45_ids)
  716. {
  717. const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
  718. u32 devs_in_pkg = 0;
  719. int i, ret, phy_reg;
  720. /* Find first non-zero Devices In package. Device zero is reserved
  721. * for 802.3 c45 complied PHYs, so don't probe it at first.
  722. */
  723. for (i = 1; i < MDIO_MMD_NUM && (devs_in_pkg == 0 ||
  724. (devs_in_pkg & 0x1fffffff) == 0x1fffffff); i++) {
  725. if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
  726. /* Check that there is a device present at this
  727. * address before reading the devices-in-package
  728. * register to avoid reading garbage from the PHY.
  729. * Some PHYs (88x3310) vendor space is not IEEE802.3
  730. * compliant.
  731. */
  732. ret = phy_c45_probe_present(bus, addr, i);
  733. if (ret < 0)
  734. /* returning -ENODEV doesn't stop bus
  735. * scanning
  736. */
  737. return (phy_reg == -EIO ||
  738. phy_reg == -ENODEV) ? -ENODEV : -EIO;
  739. if (!ret)
  740. continue;
  741. }
  742. phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg);
  743. if (phy_reg < 0)
  744. return -EIO;
  745. }
  746. if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) {
  747. /* If mostly Fs, there is no device there, then let's probe
  748. * MMD 0, as some 10G PHYs have zero Devices In package,
  749. * e.g. Cortina CS4315/CS4340 PHY.
  750. */
  751. phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg);
  752. if (phy_reg < 0)
  753. return -EIO;
  754. /* no device there, let's get out of here */
  755. if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff)
  756. return -ENODEV;
  757. }
  758. /* Now probe Device Identifiers for each device present. */
  759. for (i = 1; i < num_ids; i++) {
  760. if (!(devs_in_pkg & (1 << i)))
  761. continue;
  762. if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
  763. /* Probe the "Device Present" bits for the vendor MMDs
  764. * to ignore these if they do not contain IEEE 802.3
  765. * registers.
  766. */
  767. ret = phy_c45_probe_present(bus, addr, i);
  768. if (ret < 0)
  769. return ret;
  770. if (!ret)
  771. continue;
  772. }
  773. phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID1);
  774. if (phy_reg < 0)
  775. return -EIO;
  776. c45_ids->device_ids[i] = phy_reg << 16;
  777. phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID2);
  778. if (phy_reg < 0)
  779. return -EIO;
  780. c45_ids->device_ids[i] |= phy_reg;
  781. }
  782. c45_ids->devices_in_package = devs_in_pkg;
  783. /* Bit 0 doesn't represent a device, it indicates c22 regs presence */
  784. c45_ids->mmds_present = devs_in_pkg & ~BIT(0);
  785. return 0;
  786. }
  787. /**
  788. * get_phy_c22_id - reads the specified addr for its clause 22 ID.
  789. * @bus: the target MII bus
  790. * @addr: PHY address on the MII bus
  791. * @phy_id: where to store the ID retrieved.
  792. *
  793. * Read the 802.3 clause 22 PHY ID from the PHY at @addr on the @bus,
  794. * placing it in @phy_id. Return zero on successful read and the ID is
  795. * valid, %-EIO on bus access error, or %-ENODEV if no device responds
  796. * or invalid ID.
  797. */
  798. static int get_phy_c22_id(struct mii_bus *bus, int addr, u32 *phy_id)
  799. {
  800. int phy_reg;
  801. /* Grab the bits from PHYIR1, and put them in the upper half */
  802. phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
  803. if (phy_reg < 0) {
  804. /* returning -ENODEV doesn't stop bus scanning */
  805. return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
  806. }
  807. *phy_id = phy_reg << 16;
  808. /* Grab the bits from PHYIR2, and put them in the lower half */
  809. phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
  810. if (phy_reg < 0) {
  811. /* returning -ENODEV doesn't stop bus scanning */
  812. return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
  813. }
  814. *phy_id |= phy_reg;
  815. /* If the phy_id is mostly Fs, there is no device there */
  816. if ((*phy_id & 0x1fffffff) == 0x1fffffff)
  817. return -ENODEV;
  818. return 0;
  819. }
  820. /* Extract the phy ID from the compatible string of the form
  821. * ethernet-phy-idAAAA.BBBB.
  822. */
  823. int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id)
  824. {
  825. unsigned int upper, lower;
  826. const char *cp;
  827. int ret;
  828. ret = fwnode_property_read_string(fwnode, "compatible", &cp);
  829. if (ret)
  830. return ret;
  831. if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) != 2)
  832. return -EINVAL;
  833. *phy_id = ((upper & GENMASK(15, 0)) << 16) | (lower & GENMASK(15, 0));
  834. return 0;
  835. }
  836. EXPORT_SYMBOL(fwnode_get_phy_id);
  837. /**
  838. * get_phy_device - reads the specified PHY device and returns its @phy_device
  839. * struct
  840. * @bus: the target MII bus
  841. * @addr: PHY address on the MII bus
  842. * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
  843. *
  844. * Probe for a PHY at @addr on @bus.
  845. *
  846. * When probing for a clause 22 PHY, then read the ID registers. If we find
  847. * a valid ID, allocate and return a &struct phy_device.
  848. *
  849. * When probing for a clause 45 PHY, read the "devices in package" registers.
  850. * If the "devices in package" appears valid, read the ID registers for each
  851. * MMD, allocate and return a &struct phy_device.
  852. *
  853. * Returns an allocated &struct phy_device on success, %-ENODEV if there is
  854. * no PHY present, or %-EIO on bus access error.
  855. */
  856. struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
  857. {
  858. struct phy_c45_device_ids c45_ids;
  859. u32 phy_id = 0;
  860. int r;
  861. c45_ids.devices_in_package = 0;
  862. c45_ids.mmds_present = 0;
  863. memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids));
  864. if (is_c45)
  865. r = get_phy_c45_ids(bus, addr, &c45_ids);
  866. else
  867. r = get_phy_c22_id(bus, addr, &phy_id);
  868. if (r)
  869. return ERR_PTR(r);
  870. /* PHY device such as the Marvell Alaska 88E2110 will return a PHY ID
  871. * of 0 when probed using get_phy_c22_id() with no error. Proceed to
  872. * probe with C45 to see if we're able to get a valid PHY ID in the C45
  873. * space, if successful, create the C45 PHY device.
  874. */
  875. if (!is_c45 && phy_id == 0 && bus->read_c45) {
  876. r = get_phy_c45_ids(bus, addr, &c45_ids);
  877. if (!r)
  878. return phy_device_create(bus, addr, phy_id,
  879. true, &c45_ids);
  880. }
  881. return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
  882. }
  883. EXPORT_SYMBOL(get_phy_device);
  884. /**
  885. * phy_device_register - Register the phy device on the MDIO bus
  886. * @phydev: phy_device structure to be added to the MDIO bus
  887. */
  888. int phy_device_register(struct phy_device *phydev)
  889. {
  890. int err;
  891. err = mdiobus_register_device(&phydev->mdio);
  892. if (err)
  893. return err;
  894. /* Deassert the reset signal */
  895. phy_device_reset(phydev, 0);
  896. /* Run all of the fixups for this PHY */
  897. err = phy_scan_fixups(phydev);
  898. if (err) {
  899. phydev_err(phydev, "failed to initialize\n");
  900. goto out;
  901. }
  902. err = device_add(&phydev->mdio.dev);
  903. if (err) {
  904. phydev_err(phydev, "failed to add\n");
  905. goto out;
  906. }
  907. return 0;
  908. out:
  909. /* Assert the reset signal */
  910. phy_device_reset(phydev, 1);
  911. mdiobus_unregister_device(&phydev->mdio);
  912. return err;
  913. }
  914. EXPORT_SYMBOL(phy_device_register);
  915. /**
  916. * phy_device_remove - Remove a previously registered phy device from the MDIO bus
  917. * @phydev: phy_device structure to remove
  918. *
  919. * This doesn't free the phy_device itself, it merely reverses the effects
  920. * of phy_device_register(). Use phy_device_free() to free the device
  921. * after calling this function.
  922. */
  923. void phy_device_remove(struct phy_device *phydev)
  924. {
  925. unregister_mii_timestamper(phydev->mii_ts);
  926. pse_control_put(phydev->psec);
  927. device_del(&phydev->mdio.dev);
  928. /* Assert the reset signal */
  929. phy_device_reset(phydev, 1);
  930. mdiobus_unregister_device(&phydev->mdio);
  931. }
  932. EXPORT_SYMBOL(phy_device_remove);
  933. /**
  934. * phy_get_c45_ids - Read 802.3-c45 IDs for phy device.
  935. * @phydev: phy_device structure to read 802.3-c45 IDs
  936. *
  937. * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
  938. * the "devices in package" is invalid.
  939. */
  940. int phy_get_c45_ids(struct phy_device *phydev)
  941. {
  942. return get_phy_c45_ids(phydev->mdio.bus, phydev->mdio.addr,
  943. &phydev->c45_ids);
  944. }
  945. EXPORT_SYMBOL(phy_get_c45_ids);
  946. /**
  947. * phy_find_first - finds the first PHY device on the bus
  948. * @bus: the target MII bus
  949. */
  950. struct phy_device *phy_find_first(struct mii_bus *bus)
  951. {
  952. struct phy_device *phydev;
  953. int addr;
  954. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  955. phydev = mdiobus_get_phy(bus, addr);
  956. if (phydev)
  957. return phydev;
  958. }
  959. return NULL;
  960. }
  961. EXPORT_SYMBOL(phy_find_first);
  962. /**
  963. * phy_prepare_link - prepares the PHY layer to monitor link status
  964. * @phydev: target phy_device struct
  965. * @handler: callback function for link status change notifications
  966. *
  967. * Description: Tells the PHY infrastructure to handle the
  968. * gory details on monitoring link status (whether through
  969. * polling or an interrupt), and to call back to the
  970. * connected device driver when the link status changes.
  971. * If you want to monitor your own link state, don't call
  972. * this function.
  973. */
  974. static void phy_prepare_link(struct phy_device *phydev,
  975. void (*handler)(struct net_device *))
  976. {
  977. phydev->adjust_link = handler;
  978. }
  979. /**
  980. * phy_connect_direct - connect an ethernet device to a specific phy_device
  981. * @dev: the network device to connect
  982. * @phydev: the pointer to the phy device
  983. * @handler: callback function for state change notifications
  984. * @interface: PHY device's interface
  985. */
  986. int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
  987. void (*handler)(struct net_device *),
  988. phy_interface_t interface)
  989. {
  990. int rc;
  991. if (!dev)
  992. return -EINVAL;
  993. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  994. if (rc)
  995. return rc;
  996. phy_prepare_link(phydev, handler);
  997. if (phy_interrupt_is_valid(phydev))
  998. phy_request_interrupt(phydev);
  999. return 0;
  1000. }
  1001. EXPORT_SYMBOL(phy_connect_direct);
  1002. /**
  1003. * phy_connect - connect an ethernet device to a PHY device
  1004. * @dev: the network device to connect
  1005. * @bus_id: the id string of the PHY device to connect
  1006. * @handler: callback function for state change notifications
  1007. * @interface: PHY device's interface
  1008. *
  1009. * Description: Convenience function for connecting ethernet
  1010. * devices to PHY devices. The default behavior is for
  1011. * the PHY infrastructure to handle everything, and only notify
  1012. * the connected driver when the link status changes. If you
  1013. * don't want, or can't use the provided functionality, you may
  1014. * choose to call only the subset of functions which provide
  1015. * the desired functionality.
  1016. */
  1017. struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
  1018. void (*handler)(struct net_device *),
  1019. phy_interface_t interface)
  1020. {
  1021. struct phy_device *phydev;
  1022. struct device *d;
  1023. int rc;
  1024. /* Search the list of PHY devices on the mdio bus for the
  1025. * PHY with the requested name
  1026. */
  1027. d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
  1028. if (!d) {
  1029. pr_err("PHY %s not found\n", bus_id);
  1030. return ERR_PTR(-ENODEV);
  1031. }
  1032. phydev = to_phy_device(d);
  1033. rc = phy_connect_direct(dev, phydev, handler, interface);
  1034. put_device(d);
  1035. if (rc)
  1036. return ERR_PTR(rc);
  1037. return phydev;
  1038. }
  1039. EXPORT_SYMBOL(phy_connect);
  1040. /**
  1041. * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
  1042. * device
  1043. * @phydev: target phy_device struct
  1044. */
  1045. void phy_disconnect(struct phy_device *phydev)
  1046. {
  1047. if (phy_is_started(phydev))
  1048. phy_stop(phydev);
  1049. if (phy_interrupt_is_valid(phydev))
  1050. phy_free_interrupt(phydev);
  1051. phydev->adjust_link = NULL;
  1052. phy_detach(phydev);
  1053. }
  1054. EXPORT_SYMBOL(phy_disconnect);
  1055. /**
  1056. * phy_poll_reset - Safely wait until a PHY reset has properly completed
  1057. * @phydev: The PHY device to poll
  1058. *
  1059. * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
  1060. * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
  1061. * register must be polled until the BMCR_RESET bit clears.
  1062. *
  1063. * Furthermore, any attempts to write to PHY registers may have no effect
  1064. * or even generate MDIO bus errors until this is complete.
  1065. *
  1066. * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
  1067. * standard and do not fully reset after the BMCR_RESET bit is set, and may
  1068. * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
  1069. * effort to support such broken PHYs, this function is separate from the
  1070. * standard phy_init_hw() which will zero all the other bits in the BMCR
  1071. * and reapply all driver-specific and board-specific fixups.
  1072. */
  1073. static int phy_poll_reset(struct phy_device *phydev)
  1074. {
  1075. /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
  1076. int ret, val;
  1077. ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET),
  1078. 50000, 600000, true);
  1079. if (ret)
  1080. return ret;
  1081. /* Some chips (smsc911x) may still need up to another 1ms after the
  1082. * BMCR_RESET bit is cleared before they are usable.
  1083. */
  1084. msleep(1);
  1085. return 0;
  1086. }
  1087. int phy_init_hw(struct phy_device *phydev)
  1088. {
  1089. int ret = 0;
  1090. /* Deassert the reset signal */
  1091. phy_device_reset(phydev, 0);
  1092. if (!phydev->drv)
  1093. return 0;
  1094. if (phydev->drv->soft_reset) {
  1095. ret = phydev->drv->soft_reset(phydev);
  1096. if (ret < 0)
  1097. return ret;
  1098. /* see comment in genphy_soft_reset for an explanation */
  1099. phydev->suspended = 0;
  1100. }
  1101. ret = phy_scan_fixups(phydev);
  1102. if (ret < 0)
  1103. return ret;
  1104. phy_interface_zero(phydev->possible_interfaces);
  1105. if (phydev->drv->config_init) {
  1106. ret = phydev->drv->config_init(phydev);
  1107. if (ret < 0)
  1108. return ret;
  1109. }
  1110. if (phydev->drv->config_intr) {
  1111. ret = phydev->drv->config_intr(phydev);
  1112. if (ret < 0)
  1113. return ret;
  1114. }
  1115. return 0;
  1116. }
  1117. EXPORT_SYMBOL(phy_init_hw);
  1118. void phy_attached_info(struct phy_device *phydev)
  1119. {
  1120. phy_attached_print(phydev, NULL);
  1121. }
  1122. EXPORT_SYMBOL(phy_attached_info);
  1123. #define ATTACHED_FMT "attached PHY driver %s(mii_bus:phy_addr=%s, irq=%s)"
  1124. char *phy_attached_info_irq(struct phy_device *phydev)
  1125. {
  1126. char *irq_str;
  1127. char irq_num[8];
  1128. switch(phydev->irq) {
  1129. case PHY_POLL:
  1130. irq_str = "POLL";
  1131. break;
  1132. case PHY_MAC_INTERRUPT:
  1133. irq_str = "MAC";
  1134. break;
  1135. default:
  1136. snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
  1137. irq_str = irq_num;
  1138. break;
  1139. }
  1140. return kasprintf(GFP_KERNEL, "%s", irq_str);
  1141. }
  1142. EXPORT_SYMBOL(phy_attached_info_irq);
  1143. void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
  1144. {
  1145. const char *unbound = phydev->drv ? "" : "[unbound] ";
  1146. char *irq_str = phy_attached_info_irq(phydev);
  1147. if (!fmt) {
  1148. phydev_info(phydev, ATTACHED_FMT "\n", unbound,
  1149. phydev_name(phydev), irq_str);
  1150. } else {
  1151. va_list ap;
  1152. phydev_info(phydev, ATTACHED_FMT, unbound,
  1153. phydev_name(phydev), irq_str);
  1154. va_start(ap, fmt);
  1155. vprintk(fmt, ap);
  1156. va_end(ap);
  1157. }
  1158. kfree(irq_str);
  1159. }
  1160. EXPORT_SYMBOL(phy_attached_print);
  1161. static void phy_sysfs_create_links(struct phy_device *phydev)
  1162. {
  1163. struct net_device *dev = phydev->attached_dev;
  1164. int err;
  1165. if (!dev)
  1166. return;
  1167. err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
  1168. "attached_dev");
  1169. if (err)
  1170. return;
  1171. err = sysfs_create_link_nowarn(&dev->dev.kobj,
  1172. &phydev->mdio.dev.kobj,
  1173. "phydev");
  1174. if (err) {
  1175. dev_err(&dev->dev, "could not add device link to %s err %d\n",
  1176. kobject_name(&phydev->mdio.dev.kobj),
  1177. err);
  1178. /* non-fatal - some net drivers can use one netdevice
  1179. * with more then one phy
  1180. */
  1181. }
  1182. phydev->sysfs_links = true;
  1183. }
  1184. static ssize_t
  1185. phy_standalone_show(struct device *dev, struct device_attribute *attr,
  1186. char *buf)
  1187. {
  1188. struct phy_device *phydev = to_phy_device(dev);
  1189. return sysfs_emit(buf, "%d\n", !phydev->attached_dev);
  1190. }
  1191. static DEVICE_ATTR_RO(phy_standalone);
  1192. /**
  1193. * phy_sfp_connect_phy - Connect the SFP module's PHY to the upstream PHY
  1194. * @upstream: pointer to the upstream phy device
  1195. * @phy: pointer to the SFP module's phy device
  1196. *
  1197. * This helper allows keeping track of PHY devices on the link. It adds the
  1198. * SFP module's phy to the phy namespace of the upstream phy
  1199. *
  1200. * Return: 0 on success, otherwise a negative error code.
  1201. */
  1202. int phy_sfp_connect_phy(void *upstream, struct phy_device *phy)
  1203. {
  1204. struct phy_device *phydev = upstream;
  1205. struct net_device *dev = phydev->attached_dev;
  1206. if (dev)
  1207. return phy_link_topo_add_phy(dev, phy, PHY_UPSTREAM_PHY, phydev);
  1208. return 0;
  1209. }
  1210. EXPORT_SYMBOL(phy_sfp_connect_phy);
  1211. /**
  1212. * phy_sfp_disconnect_phy - Disconnect the SFP module's PHY from the upstream PHY
  1213. * @upstream: pointer to the upstream phy device
  1214. * @phy: pointer to the SFP module's phy device
  1215. *
  1216. * This helper allows keeping track of PHY devices on the link. It removes the
  1217. * SFP module's phy to the phy namespace of the upstream phy. As the module phy
  1218. * will be destroyed, re-inserting the same module will add a new phy with a
  1219. * new index.
  1220. */
  1221. void phy_sfp_disconnect_phy(void *upstream, struct phy_device *phy)
  1222. {
  1223. struct phy_device *phydev = upstream;
  1224. struct net_device *dev = phydev->attached_dev;
  1225. if (dev)
  1226. phy_link_topo_del_phy(dev, phy);
  1227. }
  1228. EXPORT_SYMBOL(phy_sfp_disconnect_phy);
  1229. /**
  1230. * phy_sfp_attach - attach the SFP bus to the PHY upstream network device
  1231. * @upstream: pointer to the phy device
  1232. * @bus: sfp bus representing cage being attached
  1233. *
  1234. * This is used to fill in the sfp_upstream_ops .attach member.
  1235. */
  1236. void phy_sfp_attach(void *upstream, struct sfp_bus *bus)
  1237. {
  1238. struct phy_device *phydev = upstream;
  1239. if (phydev->attached_dev)
  1240. phydev->attached_dev->sfp_bus = bus;
  1241. phydev->sfp_bus_attached = true;
  1242. }
  1243. EXPORT_SYMBOL(phy_sfp_attach);
  1244. /**
  1245. * phy_sfp_detach - detach the SFP bus from the PHY upstream network device
  1246. * @upstream: pointer to the phy device
  1247. * @bus: sfp bus representing cage being attached
  1248. *
  1249. * This is used to fill in the sfp_upstream_ops .detach member.
  1250. */
  1251. void phy_sfp_detach(void *upstream, struct sfp_bus *bus)
  1252. {
  1253. struct phy_device *phydev = upstream;
  1254. if (phydev->attached_dev)
  1255. phydev->attached_dev->sfp_bus = NULL;
  1256. phydev->sfp_bus_attached = false;
  1257. }
  1258. EXPORT_SYMBOL(phy_sfp_detach);
  1259. /**
  1260. * phy_sfp_probe - probe for a SFP cage attached to this PHY device
  1261. * @phydev: Pointer to phy_device
  1262. * @ops: SFP's upstream operations
  1263. */
  1264. int phy_sfp_probe(struct phy_device *phydev,
  1265. const struct sfp_upstream_ops *ops)
  1266. {
  1267. struct sfp_bus *bus;
  1268. int ret = 0;
  1269. if (phydev->mdio.dev.fwnode) {
  1270. bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode);
  1271. if (IS_ERR(bus))
  1272. return PTR_ERR(bus);
  1273. phydev->sfp_bus = bus;
  1274. ret = sfp_bus_add_upstream(bus, phydev, ops);
  1275. sfp_bus_put(bus);
  1276. }
  1277. return ret;
  1278. }
  1279. EXPORT_SYMBOL(phy_sfp_probe);
  1280. static bool phy_drv_supports_irq(const struct phy_driver *phydrv)
  1281. {
  1282. return phydrv->config_intr && phydrv->handle_interrupt;
  1283. }
  1284. /**
  1285. * phy_attach_direct - attach a network device to a given PHY device pointer
  1286. * @dev: network device to attach
  1287. * @phydev: Pointer to phy_device to attach
  1288. * @flags: PHY device's dev_flags
  1289. * @interface: PHY device's interface
  1290. *
  1291. * Description: Called by drivers to attach to a particular PHY
  1292. * device. The phy_device is found, and properly hooked up
  1293. * to the phy_driver. If no driver is attached, then a
  1294. * generic driver is used. The phy_device is given a ptr to
  1295. * the attaching device, and given a callback for link status
  1296. * change. The phy_device is returned to the attaching driver.
  1297. * This function takes a reference on the phy device.
  1298. */
  1299. int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  1300. u32 flags, phy_interface_t interface)
  1301. {
  1302. struct mii_bus *bus = phydev->mdio.bus;
  1303. struct device *d = &phydev->mdio.dev;
  1304. struct module *ndev_owner = NULL;
  1305. bool using_genphy = false;
  1306. int err;
  1307. /* For Ethernet device drivers that register their own MDIO bus, we
  1308. * will have bus->owner match ndev_mod, so we do not want to increment
  1309. * our own module->refcnt here, otherwise we would not be able to
  1310. * unload later on.
  1311. */
  1312. if (dev)
  1313. ndev_owner = dev->dev.parent->driver->owner;
  1314. if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
  1315. phydev_err(phydev, "failed to get the bus module\n");
  1316. return -EIO;
  1317. }
  1318. get_device(d);
  1319. /* Assume that if there is no driver, that it doesn't
  1320. * exist, and we should use the genphy driver.
  1321. */
  1322. if (!d->driver) {
  1323. if (phydev->is_c45)
  1324. d->driver = &genphy_c45_driver.mdiodrv.driver;
  1325. else
  1326. d->driver = &genphy_driver.mdiodrv.driver;
  1327. using_genphy = true;
  1328. }
  1329. if (!try_module_get(d->driver->owner)) {
  1330. phydev_err(phydev, "failed to get the device driver module\n");
  1331. err = -EIO;
  1332. goto error_put_device;
  1333. }
  1334. if (using_genphy) {
  1335. err = d->driver->probe(d);
  1336. if (err >= 0)
  1337. err = device_bind_driver(d);
  1338. if (err)
  1339. goto error_module_put;
  1340. }
  1341. if (phydev->attached_dev) {
  1342. dev_err(&dev->dev, "PHY already attached\n");
  1343. err = -EBUSY;
  1344. goto error;
  1345. }
  1346. phydev->phy_link_change = phy_link_change;
  1347. if (dev) {
  1348. phydev->attached_dev = dev;
  1349. dev->phydev = phydev;
  1350. if (phydev->sfp_bus_attached)
  1351. dev->sfp_bus = phydev->sfp_bus;
  1352. err = phy_link_topo_add_phy(dev, phydev, PHY_UPSTREAM_MAC, dev);
  1353. if (err)
  1354. goto error;
  1355. }
  1356. /* Some Ethernet drivers try to connect to a PHY device before
  1357. * calling register_netdevice() -> netdev_register_kobject() and
  1358. * does the dev->dev.kobj initialization. Here we only check for
  1359. * success which indicates that the network device kobject is
  1360. * ready. Once we do that we still need to keep track of whether
  1361. * links were successfully set up or not for phy_detach() to
  1362. * remove them accordingly.
  1363. */
  1364. phydev->sysfs_links = false;
  1365. phy_sysfs_create_links(phydev);
  1366. if (!phydev->attached_dev) {
  1367. err = sysfs_create_file(&phydev->mdio.dev.kobj,
  1368. &dev_attr_phy_standalone.attr);
  1369. if (err)
  1370. phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
  1371. }
  1372. phydev->dev_flags |= flags;
  1373. phydev->interface = interface;
  1374. phydev->state = PHY_READY;
  1375. phydev->interrupts = PHY_INTERRUPT_DISABLED;
  1376. /* PHYs can request to use poll mode even though they have an
  1377. * associated interrupt line. This could be the case if they
  1378. * detect a broken interrupt handling.
  1379. */
  1380. if (phydev->dev_flags & PHY_F_NO_IRQ)
  1381. phydev->irq = PHY_POLL;
  1382. if (!phy_drv_supports_irq(phydev->drv) && phy_interrupt_is_valid(phydev))
  1383. phydev->irq = PHY_POLL;
  1384. /* Port is set to PORT_TP by default and the actual PHY driver will set
  1385. * it to different value depending on the PHY configuration. If we have
  1386. * the generic PHY driver we can't figure it out, thus set the old
  1387. * legacy PORT_MII value.
  1388. */
  1389. if (using_genphy)
  1390. phydev->port = PORT_MII;
  1391. /* Initial carrier state is off as the phy is about to be
  1392. * (re)initialized.
  1393. */
  1394. if (dev)
  1395. netif_carrier_off(phydev->attached_dev);
  1396. /* Do initial configuration here, now that
  1397. * we have certain key parameters
  1398. * (dev_flags and interface)
  1399. */
  1400. err = phy_init_hw(phydev);
  1401. if (err)
  1402. goto error;
  1403. phy_resume(phydev);
  1404. if (!phydev->is_on_sfp_module)
  1405. phy_led_triggers_register(phydev);
  1406. /**
  1407. * If the external phy used by current mac interface is managed by
  1408. * another mac interface, so we should create a device link between
  1409. * phy dev and mac dev.
  1410. */
  1411. if (dev && phydev->mdio.bus->parent && dev->dev.parent != phydev->mdio.bus->parent)
  1412. phydev->devlink = device_link_add(dev->dev.parent, &phydev->mdio.dev,
  1413. DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS);
  1414. return err;
  1415. error:
  1416. /* phy_detach() does all of the cleanup below */
  1417. phy_detach(phydev);
  1418. return err;
  1419. error_module_put:
  1420. module_put(d->driver->owner);
  1421. d->driver = NULL;
  1422. error_put_device:
  1423. put_device(d);
  1424. if (ndev_owner != bus->owner)
  1425. module_put(bus->owner);
  1426. return err;
  1427. }
  1428. EXPORT_SYMBOL(phy_attach_direct);
  1429. /**
  1430. * phy_attach - attach a network device to a particular PHY device
  1431. * @dev: network device to attach
  1432. * @bus_id: Bus ID of PHY device to attach
  1433. * @interface: PHY device's interface
  1434. *
  1435. * Description: Same as phy_attach_direct() except that a PHY bus_id
  1436. * string is passed instead of a pointer to a struct phy_device.
  1437. */
  1438. struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
  1439. phy_interface_t interface)
  1440. {
  1441. struct phy_device *phydev;
  1442. struct device *d;
  1443. int rc;
  1444. if (!dev)
  1445. return ERR_PTR(-EINVAL);
  1446. /* Search the list of PHY devices on the mdio bus for the
  1447. * PHY with the requested name
  1448. */
  1449. d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
  1450. if (!d) {
  1451. pr_err("PHY %s not found\n", bus_id);
  1452. return ERR_PTR(-ENODEV);
  1453. }
  1454. phydev = to_phy_device(d);
  1455. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  1456. put_device(d);
  1457. if (rc)
  1458. return ERR_PTR(rc);
  1459. return phydev;
  1460. }
  1461. EXPORT_SYMBOL(phy_attach);
  1462. static bool phy_driver_is_genphy_kind(struct phy_device *phydev,
  1463. struct device_driver *driver)
  1464. {
  1465. struct device *d = &phydev->mdio.dev;
  1466. bool ret = false;
  1467. if (!phydev->drv)
  1468. return ret;
  1469. get_device(d);
  1470. ret = d->driver == driver;
  1471. put_device(d);
  1472. return ret;
  1473. }
  1474. bool phy_driver_is_genphy(struct phy_device *phydev)
  1475. {
  1476. return phy_driver_is_genphy_kind(phydev,
  1477. &genphy_driver.mdiodrv.driver);
  1478. }
  1479. EXPORT_SYMBOL_GPL(phy_driver_is_genphy);
  1480. bool phy_driver_is_genphy_10g(struct phy_device *phydev)
  1481. {
  1482. return phy_driver_is_genphy_kind(phydev,
  1483. &genphy_c45_driver.mdiodrv.driver);
  1484. }
  1485. EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
  1486. /**
  1487. * phy_package_join - join a common PHY group
  1488. * @phydev: target phy_device struct
  1489. * @base_addr: cookie and base PHY address of PHY package for offset
  1490. * calculation of global register access
  1491. * @priv_size: if non-zero allocate this amount of bytes for private data
  1492. *
  1493. * This joins a PHY group and provides a shared storage for all phydevs in
  1494. * this group. This is intended to be used for packages which contain
  1495. * more than one PHY, for example a quad PHY transceiver.
  1496. *
  1497. * The base_addr parameter serves as cookie which has to have the same values
  1498. * for all members of one group and as the base PHY address of the PHY package
  1499. * for offset calculation to access generic registers of a PHY package.
  1500. * Usually, one of the PHY addresses of the different PHYs in the package
  1501. * provides access to these global registers.
  1502. * The address which is given here, will be used in the phy_package_read()
  1503. * and phy_package_write() convenience functions as base and added to the
  1504. * passed offset in those functions.
  1505. *
  1506. * This will set the shared pointer of the phydev to the shared storage.
  1507. * If this is the first call for a this cookie the shared storage will be
  1508. * allocated. If priv_size is non-zero, the given amount of bytes are
  1509. * allocated for the priv member.
  1510. *
  1511. * Returns < 1 on error, 0 on success. Esp. calling phy_package_join()
  1512. * with the same cookie but a different priv_size is an error.
  1513. */
  1514. int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size)
  1515. {
  1516. struct mii_bus *bus = phydev->mdio.bus;
  1517. struct phy_package_shared *shared;
  1518. int ret;
  1519. if (base_addr < 0 || base_addr >= PHY_MAX_ADDR)
  1520. return -EINVAL;
  1521. mutex_lock(&bus->shared_lock);
  1522. shared = bus->shared[base_addr];
  1523. if (!shared) {
  1524. ret = -ENOMEM;
  1525. shared = kzalloc(sizeof(*shared), GFP_KERNEL);
  1526. if (!shared)
  1527. goto err_unlock;
  1528. if (priv_size) {
  1529. shared->priv = kzalloc(priv_size, GFP_KERNEL);
  1530. if (!shared->priv)
  1531. goto err_free;
  1532. shared->priv_size = priv_size;
  1533. }
  1534. shared->base_addr = base_addr;
  1535. shared->np = NULL;
  1536. refcount_set(&shared->refcnt, 1);
  1537. bus->shared[base_addr] = shared;
  1538. } else {
  1539. ret = -EINVAL;
  1540. if (priv_size && priv_size != shared->priv_size)
  1541. goto err_unlock;
  1542. refcount_inc(&shared->refcnt);
  1543. }
  1544. mutex_unlock(&bus->shared_lock);
  1545. phydev->shared = shared;
  1546. return 0;
  1547. err_free:
  1548. kfree(shared);
  1549. err_unlock:
  1550. mutex_unlock(&bus->shared_lock);
  1551. return ret;
  1552. }
  1553. EXPORT_SYMBOL_GPL(phy_package_join);
  1554. /**
  1555. * of_phy_package_join - join a common PHY group in PHY package
  1556. * @phydev: target phy_device struct
  1557. * @priv_size: if non-zero allocate this amount of bytes for private data
  1558. *
  1559. * This is a variant of phy_package_join for PHY package defined in DT.
  1560. *
  1561. * The parent node of the @phydev is checked as a valid PHY package node
  1562. * structure (by matching the node name "ethernet-phy-package") and the
  1563. * base_addr for the PHY package is passed to phy_package_join.
  1564. *
  1565. * With this configuration the shared struct will also have the np value
  1566. * filled to use additional DT defined properties in PHY specific
  1567. * probe_once and config_init_once PHY package OPs.
  1568. *
  1569. * Returns < 0 on error, 0 on success. Esp. calling phy_package_join()
  1570. * with the same cookie but a different priv_size is an error. Or a parent
  1571. * node is not detected or is not valid or doesn't match the expected node
  1572. * name for PHY package.
  1573. */
  1574. int of_phy_package_join(struct phy_device *phydev, size_t priv_size)
  1575. {
  1576. struct device_node *node = phydev->mdio.dev.of_node;
  1577. struct device_node *package_node;
  1578. u32 base_addr;
  1579. int ret;
  1580. if (!node)
  1581. return -EINVAL;
  1582. package_node = of_get_parent(node);
  1583. if (!package_node)
  1584. return -EINVAL;
  1585. if (!of_node_name_eq(package_node, "ethernet-phy-package")) {
  1586. ret = -EINVAL;
  1587. goto exit;
  1588. }
  1589. if (of_property_read_u32(package_node, "reg", &base_addr)) {
  1590. ret = -EINVAL;
  1591. goto exit;
  1592. }
  1593. ret = phy_package_join(phydev, base_addr, priv_size);
  1594. if (ret)
  1595. goto exit;
  1596. phydev->shared->np = package_node;
  1597. return 0;
  1598. exit:
  1599. of_node_put(package_node);
  1600. return ret;
  1601. }
  1602. EXPORT_SYMBOL_GPL(of_phy_package_join);
  1603. /**
  1604. * phy_package_leave - leave a common PHY group
  1605. * @phydev: target phy_device struct
  1606. *
  1607. * This leaves a PHY group created by phy_package_join(). If this phydev
  1608. * was the last user of the shared data between the group, this data is
  1609. * freed. Resets the phydev->shared pointer to NULL.
  1610. */
  1611. void phy_package_leave(struct phy_device *phydev)
  1612. {
  1613. struct phy_package_shared *shared = phydev->shared;
  1614. struct mii_bus *bus = phydev->mdio.bus;
  1615. if (!shared)
  1616. return;
  1617. /* Decrease the node refcount on leave if present */
  1618. if (shared->np)
  1619. of_node_put(shared->np);
  1620. if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) {
  1621. bus->shared[shared->base_addr] = NULL;
  1622. mutex_unlock(&bus->shared_lock);
  1623. kfree(shared->priv);
  1624. kfree(shared);
  1625. }
  1626. phydev->shared = NULL;
  1627. }
  1628. EXPORT_SYMBOL_GPL(phy_package_leave);
  1629. static void devm_phy_package_leave(struct device *dev, void *res)
  1630. {
  1631. phy_package_leave(*(struct phy_device **)res);
  1632. }
  1633. /**
  1634. * devm_phy_package_join - resource managed phy_package_join()
  1635. * @dev: device that is registering this PHY package
  1636. * @phydev: target phy_device struct
  1637. * @base_addr: cookie and base PHY address of PHY package for offset
  1638. * calculation of global register access
  1639. * @priv_size: if non-zero allocate this amount of bytes for private data
  1640. *
  1641. * Managed phy_package_join(). Shared storage fetched by this function,
  1642. * phy_package_leave() is automatically called on driver detach. See
  1643. * phy_package_join() for more information.
  1644. */
  1645. int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
  1646. int base_addr, size_t priv_size)
  1647. {
  1648. struct phy_device **ptr;
  1649. int ret;
  1650. ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
  1651. GFP_KERNEL);
  1652. if (!ptr)
  1653. return -ENOMEM;
  1654. ret = phy_package_join(phydev, base_addr, priv_size);
  1655. if (!ret) {
  1656. *ptr = phydev;
  1657. devres_add(dev, ptr);
  1658. } else {
  1659. devres_free(ptr);
  1660. }
  1661. return ret;
  1662. }
  1663. EXPORT_SYMBOL_GPL(devm_phy_package_join);
  1664. /**
  1665. * devm_of_phy_package_join - resource managed of_phy_package_join()
  1666. * @dev: device that is registering this PHY package
  1667. * @phydev: target phy_device struct
  1668. * @priv_size: if non-zero allocate this amount of bytes for private data
  1669. *
  1670. * Managed of_phy_package_join(). Shared storage fetched by this function,
  1671. * phy_package_leave() is automatically called on driver detach. See
  1672. * of_phy_package_join() for more information.
  1673. */
  1674. int devm_of_phy_package_join(struct device *dev, struct phy_device *phydev,
  1675. size_t priv_size)
  1676. {
  1677. struct phy_device **ptr;
  1678. int ret;
  1679. ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
  1680. GFP_KERNEL);
  1681. if (!ptr)
  1682. return -ENOMEM;
  1683. ret = of_phy_package_join(phydev, priv_size);
  1684. if (!ret) {
  1685. *ptr = phydev;
  1686. devres_add(dev, ptr);
  1687. } else {
  1688. devres_free(ptr);
  1689. }
  1690. return ret;
  1691. }
  1692. EXPORT_SYMBOL_GPL(devm_of_phy_package_join);
  1693. /**
  1694. * phy_detach - detach a PHY device from its network device
  1695. * @phydev: target phy_device struct
  1696. *
  1697. * This detaches the phy device from its network device and the phy
  1698. * driver, and drops the reference count taken in phy_attach_direct().
  1699. */
  1700. void phy_detach(struct phy_device *phydev)
  1701. {
  1702. struct net_device *dev = phydev->attached_dev;
  1703. struct module *ndev_owner = NULL;
  1704. struct mii_bus *bus;
  1705. if (phydev->devlink) {
  1706. device_link_del(phydev->devlink);
  1707. phydev->devlink = NULL;
  1708. }
  1709. if (phydev->sysfs_links) {
  1710. if (dev)
  1711. sysfs_remove_link(&dev->dev.kobj, "phydev");
  1712. sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
  1713. }
  1714. if (!phydev->attached_dev)
  1715. sysfs_remove_file(&phydev->mdio.dev.kobj,
  1716. &dev_attr_phy_standalone.attr);
  1717. phy_suspend(phydev);
  1718. if (dev) {
  1719. phydev->attached_dev->phydev = NULL;
  1720. phydev->attached_dev = NULL;
  1721. phy_link_topo_del_phy(dev, phydev);
  1722. }
  1723. phydev->phylink = NULL;
  1724. if (!phydev->is_on_sfp_module)
  1725. phy_led_triggers_unregister(phydev);
  1726. if (phydev->mdio.dev.driver)
  1727. module_put(phydev->mdio.dev.driver->owner);
  1728. /* If the device had no specific driver before (i.e. - it
  1729. * was using the generic driver), we unbind the device
  1730. * from the generic driver so that there's a chance a
  1731. * real driver could be loaded
  1732. */
  1733. if (phy_driver_is_genphy(phydev) ||
  1734. phy_driver_is_genphy_10g(phydev))
  1735. device_release_driver(&phydev->mdio.dev);
  1736. /* Assert the reset signal */
  1737. phy_device_reset(phydev, 1);
  1738. /*
  1739. * The phydev might go away on the put_device() below, so avoid
  1740. * a use-after-free bug by reading the underlying bus first.
  1741. */
  1742. bus = phydev->mdio.bus;
  1743. put_device(&phydev->mdio.dev);
  1744. if (dev)
  1745. ndev_owner = dev->dev.parent->driver->owner;
  1746. if (ndev_owner != bus->owner)
  1747. module_put(bus->owner);
  1748. }
  1749. EXPORT_SYMBOL(phy_detach);
  1750. int phy_suspend(struct phy_device *phydev)
  1751. {
  1752. struct net_device *netdev = phydev->attached_dev;
  1753. const struct phy_driver *phydrv = phydev->drv;
  1754. int ret;
  1755. if (phydev->suspended || !phydrv)
  1756. return 0;
  1757. phydev->wol_enabled = phy_drv_wol_enabled(phydev) ||
  1758. (netdev && netdev->ethtool->wol_enabled);
  1759. /* If the device has WOL enabled, we cannot suspend the PHY */
  1760. if (phydev->wol_enabled && !(phydrv->flags & PHY_ALWAYS_CALL_SUSPEND))
  1761. return -EBUSY;
  1762. if (!phydrv->suspend)
  1763. return 0;
  1764. ret = phydrv->suspend(phydev);
  1765. if (!ret)
  1766. phydev->suspended = true;
  1767. return ret;
  1768. }
  1769. EXPORT_SYMBOL(phy_suspend);
  1770. int __phy_resume(struct phy_device *phydev)
  1771. {
  1772. const struct phy_driver *phydrv = phydev->drv;
  1773. int ret;
  1774. lockdep_assert_held(&phydev->lock);
  1775. if (!phydrv || !phydrv->resume)
  1776. return 0;
  1777. ret = phydrv->resume(phydev);
  1778. if (!ret)
  1779. phydev->suspended = false;
  1780. return ret;
  1781. }
  1782. EXPORT_SYMBOL(__phy_resume);
  1783. int phy_resume(struct phy_device *phydev)
  1784. {
  1785. int ret;
  1786. mutex_lock(&phydev->lock);
  1787. ret = __phy_resume(phydev);
  1788. mutex_unlock(&phydev->lock);
  1789. return ret;
  1790. }
  1791. EXPORT_SYMBOL(phy_resume);
  1792. int phy_loopback(struct phy_device *phydev, bool enable)
  1793. {
  1794. int ret = 0;
  1795. if (!phydev->drv)
  1796. return -EIO;
  1797. mutex_lock(&phydev->lock);
  1798. if (enable && phydev->loopback_enabled) {
  1799. ret = -EBUSY;
  1800. goto out;
  1801. }
  1802. if (!enable && !phydev->loopback_enabled) {
  1803. ret = -EINVAL;
  1804. goto out;
  1805. }
  1806. if (phydev->drv->set_loopback)
  1807. ret = phydev->drv->set_loopback(phydev, enable);
  1808. else
  1809. ret = genphy_loopback(phydev, enable);
  1810. if (ret)
  1811. goto out;
  1812. phydev->loopback_enabled = enable;
  1813. out:
  1814. mutex_unlock(&phydev->lock);
  1815. return ret;
  1816. }
  1817. EXPORT_SYMBOL(phy_loopback);
  1818. /**
  1819. * phy_reset_after_clk_enable - perform a PHY reset if needed
  1820. * @phydev: target phy_device struct
  1821. *
  1822. * Description: Some PHYs are known to need a reset after their refclk was
  1823. * enabled. This function evaluates the flags and perform the reset if it's
  1824. * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
  1825. * was reset.
  1826. */
  1827. int phy_reset_after_clk_enable(struct phy_device *phydev)
  1828. {
  1829. if (!phydev || !phydev->drv)
  1830. return -ENODEV;
  1831. if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
  1832. phy_device_reset(phydev, 1);
  1833. phy_device_reset(phydev, 0);
  1834. return 1;
  1835. }
  1836. return 0;
  1837. }
  1838. EXPORT_SYMBOL(phy_reset_after_clk_enable);
  1839. /* Generic PHY support and helper functions */
  1840. /**
  1841. * genphy_config_advert - sanitize and advertise auto-negotiation parameters
  1842. * @phydev: target phy_device struct
  1843. * @advert: auto-negotiation parameters to advertise
  1844. *
  1845. * Description: Writes MII_ADVERTISE with the appropriate values,
  1846. * after sanitizing the values to make sure we only advertise
  1847. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  1848. * hasn't changed, and > 0 if it has changed.
  1849. */
  1850. static int genphy_config_advert(struct phy_device *phydev,
  1851. const unsigned long *advert)
  1852. {
  1853. int err, bmsr, changed = 0;
  1854. u32 adv;
  1855. adv = linkmode_adv_to_mii_adv_t(advert);
  1856. /* Setup standard advertisement */
  1857. err = phy_modify_changed(phydev, MII_ADVERTISE,
  1858. ADVERTISE_ALL | ADVERTISE_100BASE4 |
  1859. ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
  1860. adv);
  1861. if (err < 0)
  1862. return err;
  1863. if (err > 0)
  1864. changed = 1;
  1865. bmsr = phy_read(phydev, MII_BMSR);
  1866. if (bmsr < 0)
  1867. return bmsr;
  1868. /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
  1869. * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
  1870. * logical 1.
  1871. */
  1872. if (!(bmsr & BMSR_ESTATEN))
  1873. return changed;
  1874. adv = linkmode_adv_to_mii_ctrl1000_t(advert);
  1875. err = phy_modify_changed(phydev, MII_CTRL1000,
  1876. ADVERTISE_1000FULL | ADVERTISE_1000HALF,
  1877. adv);
  1878. if (err < 0)
  1879. return err;
  1880. if (err > 0)
  1881. changed = 1;
  1882. return changed;
  1883. }
  1884. /**
  1885. * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
  1886. * @phydev: target phy_device struct
  1887. *
  1888. * Description: Writes MII_ADVERTISE with the appropriate values,
  1889. * after sanitizing the values to make sure we only advertise
  1890. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  1891. * hasn't changed, and > 0 if it has changed. This function is intended
  1892. * for Clause 37 1000Base-X mode.
  1893. */
  1894. static int genphy_c37_config_advert(struct phy_device *phydev)
  1895. {
  1896. u16 adv = 0;
  1897. /* Only allow advertising what this PHY supports */
  1898. linkmode_and(phydev->advertising, phydev->advertising,
  1899. phydev->supported);
  1900. if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
  1901. phydev->advertising))
  1902. adv |= ADVERTISE_1000XFULL;
  1903. if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  1904. phydev->advertising))
  1905. adv |= ADVERTISE_1000XPAUSE;
  1906. if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  1907. phydev->advertising))
  1908. adv |= ADVERTISE_1000XPSE_ASYM;
  1909. return phy_modify_changed(phydev, MII_ADVERTISE,
  1910. ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
  1911. ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
  1912. adv);
  1913. }
  1914. /**
  1915. * genphy_config_eee_advert - disable unwanted eee mode advertisement
  1916. * @phydev: target phy_device struct
  1917. *
  1918. * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
  1919. * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
  1920. * changed, and 1 if it has changed.
  1921. */
  1922. int genphy_config_eee_advert(struct phy_device *phydev)
  1923. {
  1924. int err;
  1925. /* Nothing to disable */
  1926. if (!phydev->eee_broken_modes)
  1927. return 0;
  1928. err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
  1929. phydev->eee_broken_modes, 0);
  1930. /* If the call failed, we assume that EEE is not supported */
  1931. return err < 0 ? 0 : err;
  1932. }
  1933. EXPORT_SYMBOL(genphy_config_eee_advert);
  1934. /**
  1935. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  1936. * @phydev: target phy_device struct
  1937. *
  1938. * Description: Configures MII_BMCR to force speed/duplex
  1939. * to the values in phydev. Assumes that the values are valid.
  1940. * Please see phy_sanitize_settings().
  1941. */
  1942. int genphy_setup_forced(struct phy_device *phydev)
  1943. {
  1944. u16 ctl;
  1945. phydev->pause = 0;
  1946. phydev->asym_pause = 0;
  1947. ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
  1948. return phy_modify(phydev, MII_BMCR,
  1949. ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
  1950. }
  1951. EXPORT_SYMBOL(genphy_setup_forced);
  1952. static int genphy_setup_master_slave(struct phy_device *phydev)
  1953. {
  1954. u16 ctl = 0;
  1955. if (!phydev->is_gigabit_capable)
  1956. return 0;
  1957. switch (phydev->master_slave_set) {
  1958. case MASTER_SLAVE_CFG_MASTER_PREFERRED:
  1959. ctl |= CTL1000_PREFER_MASTER;
  1960. break;
  1961. case MASTER_SLAVE_CFG_SLAVE_PREFERRED:
  1962. break;
  1963. case MASTER_SLAVE_CFG_MASTER_FORCE:
  1964. ctl |= CTL1000_AS_MASTER;
  1965. fallthrough;
  1966. case MASTER_SLAVE_CFG_SLAVE_FORCE:
  1967. ctl |= CTL1000_ENABLE_MASTER;
  1968. break;
  1969. case MASTER_SLAVE_CFG_UNKNOWN:
  1970. case MASTER_SLAVE_CFG_UNSUPPORTED:
  1971. return 0;
  1972. default:
  1973. phydev_warn(phydev, "Unsupported Master/Slave mode\n");
  1974. return -EOPNOTSUPP;
  1975. }
  1976. return phy_modify_changed(phydev, MII_CTRL1000,
  1977. (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER |
  1978. CTL1000_PREFER_MASTER), ctl);
  1979. }
  1980. int genphy_read_master_slave(struct phy_device *phydev)
  1981. {
  1982. int cfg, state;
  1983. int val;
  1984. phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN;
  1985. phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN;
  1986. val = phy_read(phydev, MII_CTRL1000);
  1987. if (val < 0)
  1988. return val;
  1989. if (val & CTL1000_ENABLE_MASTER) {
  1990. if (val & CTL1000_AS_MASTER)
  1991. cfg = MASTER_SLAVE_CFG_MASTER_FORCE;
  1992. else
  1993. cfg = MASTER_SLAVE_CFG_SLAVE_FORCE;
  1994. } else {
  1995. if (val & CTL1000_PREFER_MASTER)
  1996. cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED;
  1997. else
  1998. cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED;
  1999. }
  2000. val = phy_read(phydev, MII_STAT1000);
  2001. if (val < 0)
  2002. return val;
  2003. if (val & LPA_1000MSFAIL) {
  2004. state = MASTER_SLAVE_STATE_ERR;
  2005. } else if (phydev->link) {
  2006. /* this bits are valid only for active link */
  2007. if (val & LPA_1000MSRES)
  2008. state = MASTER_SLAVE_STATE_MASTER;
  2009. else
  2010. state = MASTER_SLAVE_STATE_SLAVE;
  2011. } else {
  2012. state = MASTER_SLAVE_STATE_UNKNOWN;
  2013. }
  2014. phydev->master_slave_get = cfg;
  2015. phydev->master_slave_state = state;
  2016. return 0;
  2017. }
  2018. EXPORT_SYMBOL(genphy_read_master_slave);
  2019. /**
  2020. * genphy_restart_aneg - Enable and Restart Autonegotiation
  2021. * @phydev: target phy_device struct
  2022. */
  2023. int genphy_restart_aneg(struct phy_device *phydev)
  2024. {
  2025. /* Don't isolate the PHY if we're negotiating */
  2026. return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
  2027. BMCR_ANENABLE | BMCR_ANRESTART);
  2028. }
  2029. EXPORT_SYMBOL(genphy_restart_aneg);
  2030. /**
  2031. * genphy_check_and_restart_aneg - Enable and restart auto-negotiation
  2032. * @phydev: target phy_device struct
  2033. * @restart: whether aneg restart is requested
  2034. *
  2035. * Check, and restart auto-negotiation if needed.
  2036. */
  2037. int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
  2038. {
  2039. int ret;
  2040. if (!restart) {
  2041. /* Advertisement hasn't changed, but maybe aneg was never on to
  2042. * begin with? Or maybe phy was isolated?
  2043. */
  2044. ret = phy_read(phydev, MII_BMCR);
  2045. if (ret < 0)
  2046. return ret;
  2047. if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
  2048. restart = true;
  2049. }
  2050. if (restart)
  2051. return genphy_restart_aneg(phydev);
  2052. return 0;
  2053. }
  2054. EXPORT_SYMBOL(genphy_check_and_restart_aneg);
  2055. /**
  2056. * __genphy_config_aneg - restart auto-negotiation or write BMCR
  2057. * @phydev: target phy_device struct
  2058. * @changed: whether autoneg is requested
  2059. *
  2060. * Description: If auto-negotiation is enabled, we configure the
  2061. * advertising, and then restart auto-negotiation. If it is not
  2062. * enabled, then we write the BMCR.
  2063. */
  2064. int __genphy_config_aneg(struct phy_device *phydev, bool changed)
  2065. {
  2066. __ETHTOOL_DECLARE_LINK_MODE_MASK(fixed_advert);
  2067. const struct phy_setting *set;
  2068. unsigned long *advert;
  2069. int err;
  2070. err = genphy_c45_an_config_eee_aneg(phydev);
  2071. if (err < 0)
  2072. return err;
  2073. else if (err)
  2074. changed = true;
  2075. err = genphy_setup_master_slave(phydev);
  2076. if (err < 0)
  2077. return err;
  2078. else if (err)
  2079. changed = true;
  2080. if (phydev->autoneg == AUTONEG_ENABLE) {
  2081. /* Only allow advertising what this PHY supports */
  2082. linkmode_and(phydev->advertising, phydev->advertising,
  2083. phydev->supported);
  2084. advert = phydev->advertising;
  2085. } else if (phydev->speed < SPEED_1000) {
  2086. return genphy_setup_forced(phydev);
  2087. } else {
  2088. linkmode_zero(fixed_advert);
  2089. set = phy_lookup_setting(phydev->speed, phydev->duplex,
  2090. phydev->supported, true);
  2091. if (set)
  2092. linkmode_set_bit(set->bit, fixed_advert);
  2093. advert = fixed_advert;
  2094. }
  2095. err = genphy_config_advert(phydev, advert);
  2096. if (err < 0) /* error */
  2097. return err;
  2098. else if (err)
  2099. changed = true;
  2100. return genphy_check_and_restart_aneg(phydev, changed);
  2101. }
  2102. EXPORT_SYMBOL(__genphy_config_aneg);
  2103. /**
  2104. * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
  2105. * @phydev: target phy_device struct
  2106. *
  2107. * Description: If auto-negotiation is enabled, we configure the
  2108. * advertising, and then restart auto-negotiation. If it is not
  2109. * enabled, then we write the BMCR. This function is intended
  2110. * for use with Clause 37 1000Base-X mode.
  2111. */
  2112. int genphy_c37_config_aneg(struct phy_device *phydev)
  2113. {
  2114. int err, changed;
  2115. if (phydev->autoneg != AUTONEG_ENABLE)
  2116. return genphy_setup_forced(phydev);
  2117. err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
  2118. BMCR_SPEED1000);
  2119. if (err)
  2120. return err;
  2121. changed = genphy_c37_config_advert(phydev);
  2122. if (changed < 0) /* error */
  2123. return changed;
  2124. if (!changed) {
  2125. /* Advertisement hasn't changed, but maybe aneg was never on to
  2126. * begin with? Or maybe phy was isolated?
  2127. */
  2128. int ctl = phy_read(phydev, MII_BMCR);
  2129. if (ctl < 0)
  2130. return ctl;
  2131. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  2132. changed = 1; /* do restart aneg */
  2133. }
  2134. /* Only restart aneg if we are advertising something different
  2135. * than we were before.
  2136. */
  2137. if (changed > 0)
  2138. return genphy_restart_aneg(phydev);
  2139. return 0;
  2140. }
  2141. EXPORT_SYMBOL(genphy_c37_config_aneg);
  2142. /**
  2143. * genphy_aneg_done - return auto-negotiation status
  2144. * @phydev: target phy_device struct
  2145. *
  2146. * Description: Reads the status register and returns 0 either if
  2147. * auto-negotiation is incomplete, or if there was an error.
  2148. * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
  2149. */
  2150. int genphy_aneg_done(struct phy_device *phydev)
  2151. {
  2152. int retval = phy_read(phydev, MII_BMSR);
  2153. return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
  2154. }
  2155. EXPORT_SYMBOL(genphy_aneg_done);
  2156. /**
  2157. * genphy_update_link - update link status in @phydev
  2158. * @phydev: target phy_device struct
  2159. *
  2160. * Description: Update the value in phydev->link to reflect the
  2161. * current link value. In order to do this, we need to read
  2162. * the status register twice, keeping the second value.
  2163. */
  2164. int genphy_update_link(struct phy_device *phydev)
  2165. {
  2166. int status = 0, bmcr;
  2167. bmcr = phy_read(phydev, MII_BMCR);
  2168. if (bmcr < 0)
  2169. return bmcr;
  2170. /* Autoneg is being started, therefore disregard BMSR value and
  2171. * report link as down.
  2172. */
  2173. if (bmcr & BMCR_ANRESTART)
  2174. goto done;
  2175. /* The link state is latched low so that momentary link
  2176. * drops can be detected. Do not double-read the status
  2177. * in polling mode to detect such short link drops except
  2178. * the link was already down.
  2179. */
  2180. if (!phy_polling_mode(phydev) || !phydev->link) {
  2181. status = phy_read(phydev, MII_BMSR);
  2182. if (status < 0)
  2183. return status;
  2184. else if (status & BMSR_LSTATUS)
  2185. goto done;
  2186. }
  2187. /* Read link and autonegotiation status */
  2188. status = phy_read(phydev, MII_BMSR);
  2189. if (status < 0)
  2190. return status;
  2191. done:
  2192. phydev->link = status & BMSR_LSTATUS ? 1 : 0;
  2193. phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
  2194. /* Consider the case that autoneg was started and "aneg complete"
  2195. * bit has been reset, but "link up" bit not yet.
  2196. */
  2197. if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
  2198. phydev->link = 0;
  2199. return 0;
  2200. }
  2201. EXPORT_SYMBOL(genphy_update_link);
  2202. int genphy_read_lpa(struct phy_device *phydev)
  2203. {
  2204. int lpa, lpagb;
  2205. if (phydev->autoneg == AUTONEG_ENABLE) {
  2206. if (!phydev->autoneg_complete) {
  2207. mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
  2208. 0);
  2209. mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
  2210. return 0;
  2211. }
  2212. if (phydev->is_gigabit_capable) {
  2213. lpagb = phy_read(phydev, MII_STAT1000);
  2214. if (lpagb < 0)
  2215. return lpagb;
  2216. if (lpagb & LPA_1000MSFAIL) {
  2217. int adv = phy_read(phydev, MII_CTRL1000);
  2218. if (adv < 0)
  2219. return adv;
  2220. if (adv & CTL1000_ENABLE_MASTER)
  2221. phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
  2222. else
  2223. phydev_err(phydev, "Master/Slave resolution failed\n");
  2224. return -ENOLINK;
  2225. }
  2226. mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
  2227. lpagb);
  2228. }
  2229. lpa = phy_read(phydev, MII_LPA);
  2230. if (lpa < 0)
  2231. return lpa;
  2232. mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
  2233. } else {
  2234. linkmode_zero(phydev->lp_advertising);
  2235. }
  2236. return 0;
  2237. }
  2238. EXPORT_SYMBOL(genphy_read_lpa);
  2239. /**
  2240. * genphy_read_status_fixed - read the link parameters for !aneg mode
  2241. * @phydev: target phy_device struct
  2242. *
  2243. * Read the current duplex and speed state for a PHY operating with
  2244. * autonegotiation disabled.
  2245. */
  2246. int genphy_read_status_fixed(struct phy_device *phydev)
  2247. {
  2248. int bmcr = phy_read(phydev, MII_BMCR);
  2249. if (bmcr < 0)
  2250. return bmcr;
  2251. if (bmcr & BMCR_FULLDPLX)
  2252. phydev->duplex = DUPLEX_FULL;
  2253. else
  2254. phydev->duplex = DUPLEX_HALF;
  2255. if (bmcr & BMCR_SPEED1000)
  2256. phydev->speed = SPEED_1000;
  2257. else if (bmcr & BMCR_SPEED100)
  2258. phydev->speed = SPEED_100;
  2259. else
  2260. phydev->speed = SPEED_10;
  2261. return 0;
  2262. }
  2263. EXPORT_SYMBOL(genphy_read_status_fixed);
  2264. /**
  2265. * genphy_read_status - check the link status and update current link state
  2266. * @phydev: target phy_device struct
  2267. *
  2268. * Description: Check the link, then figure out the current state
  2269. * by comparing what we advertise with what the link partner
  2270. * advertises. Start by checking the gigabit possibilities,
  2271. * then move on to 10/100.
  2272. */
  2273. int genphy_read_status(struct phy_device *phydev)
  2274. {
  2275. int err, old_link = phydev->link;
  2276. /* Update the link, but return if there was an error */
  2277. err = genphy_update_link(phydev);
  2278. if (err)
  2279. return err;
  2280. /* why bother the PHY if nothing can have changed */
  2281. if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
  2282. return 0;
  2283. phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED;
  2284. phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
  2285. phydev->speed = SPEED_UNKNOWN;
  2286. phydev->duplex = DUPLEX_UNKNOWN;
  2287. phydev->pause = 0;
  2288. phydev->asym_pause = 0;
  2289. if (phydev->is_gigabit_capable) {
  2290. err = genphy_read_master_slave(phydev);
  2291. if (err < 0)
  2292. return err;
  2293. }
  2294. err = genphy_read_lpa(phydev);
  2295. if (err < 0)
  2296. return err;
  2297. if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
  2298. phy_resolve_aneg_linkmode(phydev);
  2299. } else if (phydev->autoneg == AUTONEG_DISABLE) {
  2300. err = genphy_read_status_fixed(phydev);
  2301. if (err < 0)
  2302. return err;
  2303. }
  2304. return 0;
  2305. }
  2306. EXPORT_SYMBOL(genphy_read_status);
  2307. /**
  2308. * genphy_c37_read_status - check the link status and update current link state
  2309. * @phydev: target phy_device struct
  2310. * @changed: pointer where to store if link changed
  2311. *
  2312. * Description: Check the link, then figure out the current state
  2313. * by comparing what we advertise with what the link partner
  2314. * advertises. This function is for Clause 37 1000Base-X mode.
  2315. *
  2316. * If link has changed, @changed is set to true, false otherwise.
  2317. */
  2318. int genphy_c37_read_status(struct phy_device *phydev, bool *changed)
  2319. {
  2320. int lpa, err, old_link = phydev->link;
  2321. /* Update the link, but return if there was an error */
  2322. err = genphy_update_link(phydev);
  2323. if (err)
  2324. return err;
  2325. /* why bother the PHY if nothing can have changed */
  2326. if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) {
  2327. *changed = false;
  2328. return 0;
  2329. }
  2330. /* Signal link has changed */
  2331. *changed = true;
  2332. phydev->duplex = DUPLEX_UNKNOWN;
  2333. phydev->pause = 0;
  2334. phydev->asym_pause = 0;
  2335. if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
  2336. lpa = phy_read(phydev, MII_LPA);
  2337. if (lpa < 0)
  2338. return lpa;
  2339. linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  2340. phydev->lp_advertising, lpa & LPA_LPACK);
  2341. linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
  2342. phydev->lp_advertising, lpa & LPA_1000XFULL);
  2343. linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  2344. phydev->lp_advertising, lpa & LPA_1000XPAUSE);
  2345. linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  2346. phydev->lp_advertising,
  2347. lpa & LPA_1000XPAUSE_ASYM);
  2348. phy_resolve_aneg_linkmode(phydev);
  2349. } else if (phydev->autoneg == AUTONEG_DISABLE) {
  2350. int bmcr = phy_read(phydev, MII_BMCR);
  2351. if (bmcr < 0)
  2352. return bmcr;
  2353. if (bmcr & BMCR_FULLDPLX)
  2354. phydev->duplex = DUPLEX_FULL;
  2355. else
  2356. phydev->duplex = DUPLEX_HALF;
  2357. }
  2358. return 0;
  2359. }
  2360. EXPORT_SYMBOL(genphy_c37_read_status);
  2361. /**
  2362. * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
  2363. * @phydev: target phy_device struct
  2364. *
  2365. * Description: Perform a software PHY reset using the standard
  2366. * BMCR_RESET bit and poll for the reset bit to be cleared.
  2367. *
  2368. * Returns: 0 on success, < 0 on failure
  2369. */
  2370. int genphy_soft_reset(struct phy_device *phydev)
  2371. {
  2372. u16 res = BMCR_RESET;
  2373. int ret;
  2374. if (phydev->autoneg == AUTONEG_ENABLE)
  2375. res |= BMCR_ANRESTART;
  2376. ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
  2377. if (ret < 0)
  2378. return ret;
  2379. /* Clause 22 states that setting bit BMCR_RESET sets control registers
  2380. * to their default value. Therefore the POWER DOWN bit is supposed to
  2381. * be cleared after soft reset.
  2382. */
  2383. phydev->suspended = 0;
  2384. ret = phy_poll_reset(phydev);
  2385. if (ret)
  2386. return ret;
  2387. /* BMCR may be reset to defaults */
  2388. if (phydev->autoneg == AUTONEG_DISABLE)
  2389. ret = genphy_setup_forced(phydev);
  2390. return ret;
  2391. }
  2392. EXPORT_SYMBOL(genphy_soft_reset);
  2393. irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev)
  2394. {
  2395. /* It seems there are cases where the interrupts are handled by another
  2396. * entity (ie an IRQ controller embedded inside the PHY) and do not
  2397. * need any other interraction from phylib. In this case, just trigger
  2398. * the state machine directly.
  2399. */
  2400. phy_trigger_machine(phydev);
  2401. return 0;
  2402. }
  2403. EXPORT_SYMBOL(genphy_handle_interrupt_no_ack);
  2404. /**
  2405. * genphy_read_abilities - read PHY abilities from Clause 22 registers
  2406. * @phydev: target phy_device struct
  2407. *
  2408. * Description: Reads the PHY's abilities and populates
  2409. * phydev->supported accordingly.
  2410. *
  2411. * Returns: 0 on success, < 0 on failure
  2412. */
  2413. int genphy_read_abilities(struct phy_device *phydev)
  2414. {
  2415. int val;
  2416. linkmode_set_bit_array(phy_basic_ports_array,
  2417. ARRAY_SIZE(phy_basic_ports_array),
  2418. phydev->supported);
  2419. val = phy_read(phydev, MII_BMSR);
  2420. if (val < 0)
  2421. return val;
  2422. linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
  2423. val & BMSR_ANEGCAPABLE);
  2424. linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
  2425. val & BMSR_100FULL);
  2426. linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
  2427. val & BMSR_100HALF);
  2428. linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
  2429. val & BMSR_10FULL);
  2430. linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
  2431. val & BMSR_10HALF);
  2432. if (val & BMSR_ESTATEN) {
  2433. val = phy_read(phydev, MII_ESTATUS);
  2434. if (val < 0)
  2435. return val;
  2436. linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  2437. phydev->supported, val & ESTATUS_1000_TFULL);
  2438. linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
  2439. phydev->supported, val & ESTATUS_1000_THALF);
  2440. linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
  2441. phydev->supported, val & ESTATUS_1000_XFULL);
  2442. }
  2443. /* This is optional functionality. If not supported, we may get an error
  2444. * which should be ignored.
  2445. */
  2446. genphy_c45_read_eee_abilities(phydev);
  2447. return 0;
  2448. }
  2449. EXPORT_SYMBOL(genphy_read_abilities);
  2450. /* This is used for the phy device which doesn't support the MMD extended
  2451. * register access, but it does have side effect when we are trying to access
  2452. * the MMD register via indirect method.
  2453. */
  2454. int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
  2455. {
  2456. return -EOPNOTSUPP;
  2457. }
  2458. EXPORT_SYMBOL(genphy_read_mmd_unsupported);
  2459. int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
  2460. u16 regnum, u16 val)
  2461. {
  2462. return -EOPNOTSUPP;
  2463. }
  2464. EXPORT_SYMBOL(genphy_write_mmd_unsupported);
  2465. int genphy_suspend(struct phy_device *phydev)
  2466. {
  2467. return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
  2468. }
  2469. EXPORT_SYMBOL(genphy_suspend);
  2470. int genphy_resume(struct phy_device *phydev)
  2471. {
  2472. return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
  2473. }
  2474. EXPORT_SYMBOL(genphy_resume);
  2475. int genphy_loopback(struct phy_device *phydev, bool enable)
  2476. {
  2477. if (enable) {
  2478. u16 ctl = BMCR_LOOPBACK;
  2479. int ret, val;
  2480. ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
  2481. phy_modify(phydev, MII_BMCR, ~0, ctl);
  2482. ret = phy_read_poll_timeout(phydev, MII_BMSR, val,
  2483. val & BMSR_LSTATUS,
  2484. 5000, 500000, true);
  2485. if (ret)
  2486. return ret;
  2487. } else {
  2488. phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 0);
  2489. phy_config_aneg(phydev);
  2490. }
  2491. return 0;
  2492. }
  2493. EXPORT_SYMBOL(genphy_loopback);
  2494. /**
  2495. * phy_remove_link_mode - Remove a supported link mode
  2496. * @phydev: phy_device structure to remove link mode from
  2497. * @link_mode: Link mode to be removed
  2498. *
  2499. * Description: Some MACs don't support all link modes which the PHY
  2500. * does. e.g. a 1G MAC often does not support 1000Half. Add a helper
  2501. * to remove a link mode.
  2502. */
  2503. void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
  2504. {
  2505. linkmode_clear_bit(link_mode, phydev->supported);
  2506. phy_advertise_supported(phydev);
  2507. }
  2508. EXPORT_SYMBOL(phy_remove_link_mode);
  2509. static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
  2510. {
  2511. linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
  2512. linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
  2513. linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
  2514. linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
  2515. }
  2516. /**
  2517. * phy_advertise_supported - Advertise all supported modes
  2518. * @phydev: target phy_device struct
  2519. *
  2520. * Description: Called to advertise all supported modes, doesn't touch
  2521. * pause mode advertising.
  2522. */
  2523. void phy_advertise_supported(struct phy_device *phydev)
  2524. {
  2525. __ETHTOOL_DECLARE_LINK_MODE_MASK(new);
  2526. linkmode_copy(new, phydev->supported);
  2527. phy_copy_pause_bits(new, phydev->advertising);
  2528. linkmode_copy(phydev->advertising, new);
  2529. }
  2530. EXPORT_SYMBOL(phy_advertise_supported);
  2531. /**
  2532. * phy_advertise_eee_all - Advertise all supported EEE modes
  2533. * @phydev: target phy_device struct
  2534. *
  2535. * Description: Per default phylib preserves the EEE advertising at the time of
  2536. * phy probing, which might be a subset of the supported EEE modes. Use this
  2537. * function when all supported EEE modes should be advertised. This does not
  2538. * trigger auto-negotiation, so must be called before phy_start()/
  2539. * phylink_start() which will start auto-negotiation.
  2540. */
  2541. void phy_advertise_eee_all(struct phy_device *phydev)
  2542. {
  2543. linkmode_copy(phydev->advertising_eee, phydev->supported_eee);
  2544. }
  2545. EXPORT_SYMBOL_GPL(phy_advertise_eee_all);
  2546. /**
  2547. * phy_support_eee - Set initial EEE policy configuration
  2548. * @phydev: Target phy_device struct
  2549. *
  2550. * This function configures the initial policy for Energy Efficient Ethernet
  2551. * (EEE) on the specified PHY device, influencing that EEE capabilities are
  2552. * advertised before the link is established. It should be called during PHY
  2553. * registration by the MAC driver and/or the PHY driver (for SmartEEE PHYs)
  2554. * if MAC supports LPI or PHY is capable to compensate missing LPI functionality
  2555. * of the MAC.
  2556. *
  2557. * The function sets default EEE policy parameters, including preparing the PHY
  2558. * to advertise EEE capabilities based on hardware support.
  2559. *
  2560. * It also sets the expected configuration for Low Power Idle (LPI) in the MAC
  2561. * driver. If the PHY framework determines that both local and remote
  2562. * advertisements support EEE, and the negotiated link mode is compatible with
  2563. * EEE, it will set enable_tx_lpi = true. The MAC driver is expected to act on
  2564. * this setting by enabling the LPI timer if enable_tx_lpi is set.
  2565. */
  2566. void phy_support_eee(struct phy_device *phydev)
  2567. {
  2568. linkmode_copy(phydev->advertising_eee, phydev->supported_eee);
  2569. phydev->eee_cfg.tx_lpi_enabled = true;
  2570. phydev->eee_cfg.eee_enabled = true;
  2571. }
  2572. EXPORT_SYMBOL(phy_support_eee);
  2573. /**
  2574. * phy_support_sym_pause - Enable support of symmetrical pause
  2575. * @phydev: target phy_device struct
  2576. *
  2577. * Description: Called by the MAC to indicate is supports symmetrical
  2578. * Pause, but not asym pause.
  2579. */
  2580. void phy_support_sym_pause(struct phy_device *phydev)
  2581. {
  2582. linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
  2583. phy_copy_pause_bits(phydev->advertising, phydev->supported);
  2584. }
  2585. EXPORT_SYMBOL(phy_support_sym_pause);
  2586. /**
  2587. * phy_support_asym_pause - Enable support of asym pause
  2588. * @phydev: target phy_device struct
  2589. *
  2590. * Description: Called by the MAC to indicate is supports Asym Pause.
  2591. */
  2592. void phy_support_asym_pause(struct phy_device *phydev)
  2593. {
  2594. phy_copy_pause_bits(phydev->advertising, phydev->supported);
  2595. }
  2596. EXPORT_SYMBOL(phy_support_asym_pause);
  2597. /**
  2598. * phy_set_sym_pause - Configure symmetric Pause
  2599. * @phydev: target phy_device struct
  2600. * @rx: Receiver Pause is supported
  2601. * @tx: Transmit Pause is supported
  2602. * @autoneg: Auto neg should be used
  2603. *
  2604. * Description: Configure advertised Pause support depending on if
  2605. * receiver pause and pause auto neg is supported. Generally called
  2606. * from the set_pauseparam .ndo.
  2607. */
  2608. void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
  2609. bool autoneg)
  2610. {
  2611. linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
  2612. if (rx && tx && autoneg)
  2613. linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  2614. phydev->supported);
  2615. linkmode_copy(phydev->advertising, phydev->supported);
  2616. }
  2617. EXPORT_SYMBOL(phy_set_sym_pause);
  2618. /**
  2619. * phy_set_asym_pause - Configure Pause and Asym Pause
  2620. * @phydev: target phy_device struct
  2621. * @rx: Receiver Pause is supported
  2622. * @tx: Transmit Pause is supported
  2623. *
  2624. * Description: Configure advertised Pause support depending on if
  2625. * transmit and receiver pause is supported. If there has been a
  2626. * change in adverting, trigger a new autoneg. Generally called from
  2627. * the set_pauseparam .ndo.
  2628. */
  2629. void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
  2630. {
  2631. __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
  2632. linkmode_copy(oldadv, phydev->advertising);
  2633. linkmode_set_pause(phydev->advertising, tx, rx);
  2634. if (!linkmode_equal(oldadv, phydev->advertising) &&
  2635. phydev->autoneg)
  2636. phy_start_aneg(phydev);
  2637. }
  2638. EXPORT_SYMBOL(phy_set_asym_pause);
  2639. /**
  2640. * phy_validate_pause - Test if the PHY/MAC support the pause configuration
  2641. * @phydev: phy_device struct
  2642. * @pp: requested pause configuration
  2643. *
  2644. * Description: Test if the PHY/MAC combination supports the Pause
  2645. * configuration the user is requesting. Returns True if it is
  2646. * supported, false otherwise.
  2647. */
  2648. bool phy_validate_pause(struct phy_device *phydev,
  2649. struct ethtool_pauseparam *pp)
  2650. {
  2651. if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  2652. phydev->supported) && pp->rx_pause)
  2653. return false;
  2654. if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  2655. phydev->supported) &&
  2656. pp->rx_pause != pp->tx_pause)
  2657. return false;
  2658. return true;
  2659. }
  2660. EXPORT_SYMBOL(phy_validate_pause);
  2661. /**
  2662. * phy_get_pause - resolve negotiated pause modes
  2663. * @phydev: phy_device struct
  2664. * @tx_pause: pointer to bool to indicate whether transmit pause should be
  2665. * enabled.
  2666. * @rx_pause: pointer to bool to indicate whether receive pause should be
  2667. * enabled.
  2668. *
  2669. * Resolve and return the flow control modes according to the negotiation
  2670. * result. This includes checking that we are operating in full duplex mode.
  2671. * See linkmode_resolve_pause() for further details.
  2672. */
  2673. void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause)
  2674. {
  2675. if (phydev->duplex != DUPLEX_FULL) {
  2676. *tx_pause = false;
  2677. *rx_pause = false;
  2678. return;
  2679. }
  2680. return linkmode_resolve_pause(phydev->advertising,
  2681. phydev->lp_advertising,
  2682. tx_pause, rx_pause);
  2683. }
  2684. EXPORT_SYMBOL(phy_get_pause);
  2685. #if IS_ENABLED(CONFIG_OF_MDIO)
  2686. static int phy_get_int_delay_property(struct device *dev, const char *name)
  2687. {
  2688. s32 int_delay;
  2689. int ret;
  2690. ret = device_property_read_u32(dev, name, &int_delay);
  2691. if (ret)
  2692. return ret;
  2693. return int_delay;
  2694. }
  2695. #else
  2696. static int phy_get_int_delay_property(struct device *dev, const char *name)
  2697. {
  2698. return -EINVAL;
  2699. }
  2700. #endif
  2701. /**
  2702. * phy_get_internal_delay - returns the index of the internal delay
  2703. * @phydev: phy_device struct
  2704. * @dev: pointer to the devices device struct
  2705. * @delay_values: array of delays the PHY supports
  2706. * @size: the size of the delay array
  2707. * @is_rx: boolean to indicate to get the rx internal delay
  2708. *
  2709. * Returns the index within the array of internal delay passed in.
  2710. * If the device property is not present then the interface type is checked
  2711. * if the interface defines use of internal delay then a 1 is returned otherwise
  2712. * a 0 is returned.
  2713. * The array must be in ascending order. If PHY does not have an ascending order
  2714. * array then size = 0 and the value of the delay property is returned.
  2715. * Return -EINVAL if the delay is invalid or cannot be found.
  2716. */
  2717. s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
  2718. const int *delay_values, int size, bool is_rx)
  2719. {
  2720. s32 delay;
  2721. int i;
  2722. if (is_rx) {
  2723. delay = phy_get_int_delay_property(dev, "rx-internal-delay-ps");
  2724. if (delay < 0 && size == 0) {
  2725. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
  2726. phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
  2727. return 1;
  2728. else
  2729. return 0;
  2730. }
  2731. } else {
  2732. delay = phy_get_int_delay_property(dev, "tx-internal-delay-ps");
  2733. if (delay < 0 && size == 0) {
  2734. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
  2735. phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
  2736. return 1;
  2737. else
  2738. return 0;
  2739. }
  2740. }
  2741. if (delay < 0)
  2742. return delay;
  2743. if (size == 0)
  2744. return delay;
  2745. if (delay < delay_values[0] || delay > delay_values[size - 1]) {
  2746. phydev_err(phydev, "Delay %d is out of range\n", delay);
  2747. return -EINVAL;
  2748. }
  2749. if (delay == delay_values[0])
  2750. return 0;
  2751. for (i = 1; i < size; i++) {
  2752. if (delay == delay_values[i])
  2753. return i;
  2754. /* Find an approximate index by looking up the table */
  2755. if (delay > delay_values[i - 1] &&
  2756. delay < delay_values[i]) {
  2757. if (delay - delay_values[i - 1] <
  2758. delay_values[i] - delay)
  2759. return i - 1;
  2760. else
  2761. return i;
  2762. }
  2763. }
  2764. phydev_err(phydev, "error finding internal delay index for %d\n",
  2765. delay);
  2766. return -EINVAL;
  2767. }
  2768. EXPORT_SYMBOL(phy_get_internal_delay);
  2769. static int phy_led_set_brightness(struct led_classdev *led_cdev,
  2770. enum led_brightness value)
  2771. {
  2772. struct phy_led *phyled = to_phy_led(led_cdev);
  2773. struct phy_device *phydev = phyled->phydev;
  2774. int err;
  2775. mutex_lock(&phydev->lock);
  2776. err = phydev->drv->led_brightness_set(phydev, phyled->index, value);
  2777. mutex_unlock(&phydev->lock);
  2778. return err;
  2779. }
  2780. static int phy_led_blink_set(struct led_classdev *led_cdev,
  2781. unsigned long *delay_on,
  2782. unsigned long *delay_off)
  2783. {
  2784. struct phy_led *phyled = to_phy_led(led_cdev);
  2785. struct phy_device *phydev = phyled->phydev;
  2786. int err;
  2787. mutex_lock(&phydev->lock);
  2788. err = phydev->drv->led_blink_set(phydev, phyled->index,
  2789. delay_on, delay_off);
  2790. mutex_unlock(&phydev->lock);
  2791. return err;
  2792. }
  2793. static __maybe_unused struct device *
  2794. phy_led_hw_control_get_device(struct led_classdev *led_cdev)
  2795. {
  2796. struct phy_led *phyled = to_phy_led(led_cdev);
  2797. struct phy_device *phydev = phyled->phydev;
  2798. if (phydev->attached_dev)
  2799. return &phydev->attached_dev->dev;
  2800. return NULL;
  2801. }
  2802. static int __maybe_unused
  2803. phy_led_hw_control_get(struct led_classdev *led_cdev,
  2804. unsigned long *rules)
  2805. {
  2806. struct phy_led *phyled = to_phy_led(led_cdev);
  2807. struct phy_device *phydev = phyled->phydev;
  2808. int err;
  2809. mutex_lock(&phydev->lock);
  2810. err = phydev->drv->led_hw_control_get(phydev, phyled->index, rules);
  2811. mutex_unlock(&phydev->lock);
  2812. return err;
  2813. }
  2814. static int __maybe_unused
  2815. phy_led_hw_control_set(struct led_classdev *led_cdev,
  2816. unsigned long rules)
  2817. {
  2818. struct phy_led *phyled = to_phy_led(led_cdev);
  2819. struct phy_device *phydev = phyled->phydev;
  2820. int err;
  2821. mutex_lock(&phydev->lock);
  2822. err = phydev->drv->led_hw_control_set(phydev, phyled->index, rules);
  2823. mutex_unlock(&phydev->lock);
  2824. return err;
  2825. }
  2826. static __maybe_unused int phy_led_hw_is_supported(struct led_classdev *led_cdev,
  2827. unsigned long rules)
  2828. {
  2829. struct phy_led *phyled = to_phy_led(led_cdev);
  2830. struct phy_device *phydev = phyled->phydev;
  2831. int err;
  2832. mutex_lock(&phydev->lock);
  2833. err = phydev->drv->led_hw_is_supported(phydev, phyled->index, rules);
  2834. mutex_unlock(&phydev->lock);
  2835. return err;
  2836. }
  2837. static void phy_leds_unregister(struct phy_device *phydev)
  2838. {
  2839. struct phy_led *phyled, *tmp;
  2840. list_for_each_entry_safe(phyled, tmp, &phydev->leds, list) {
  2841. led_classdev_unregister(&phyled->led_cdev);
  2842. list_del(&phyled->list);
  2843. }
  2844. }
  2845. static int of_phy_led(struct phy_device *phydev,
  2846. struct device_node *led)
  2847. {
  2848. struct device *dev = &phydev->mdio.dev;
  2849. struct led_init_data init_data = {};
  2850. struct led_classdev *cdev;
  2851. unsigned long modes = 0;
  2852. struct phy_led *phyled;
  2853. u32 index;
  2854. int err;
  2855. phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL);
  2856. if (!phyled)
  2857. return -ENOMEM;
  2858. cdev = &phyled->led_cdev;
  2859. phyled->phydev = phydev;
  2860. err = of_property_read_u32(led, "reg", &index);
  2861. if (err)
  2862. return err;
  2863. if (index > U8_MAX)
  2864. return -EINVAL;
  2865. if (of_property_read_bool(led, "active-low"))
  2866. set_bit(PHY_LED_ACTIVE_LOW, &modes);
  2867. if (of_property_read_bool(led, "inactive-high-impedance"))
  2868. set_bit(PHY_LED_INACTIVE_HIGH_IMPEDANCE, &modes);
  2869. if (modes) {
  2870. /* Return error if asked to set polarity modes but not supported */
  2871. if (!phydev->drv->led_polarity_set)
  2872. return -EINVAL;
  2873. err = phydev->drv->led_polarity_set(phydev, index, modes);
  2874. if (err)
  2875. return err;
  2876. }
  2877. phyled->index = index;
  2878. if (phydev->drv->led_brightness_set)
  2879. cdev->brightness_set_blocking = phy_led_set_brightness;
  2880. if (phydev->drv->led_blink_set)
  2881. cdev->blink_set = phy_led_blink_set;
  2882. #ifdef CONFIG_LEDS_TRIGGERS
  2883. if (phydev->drv->led_hw_is_supported &&
  2884. phydev->drv->led_hw_control_set &&
  2885. phydev->drv->led_hw_control_get) {
  2886. cdev->hw_control_is_supported = phy_led_hw_is_supported;
  2887. cdev->hw_control_set = phy_led_hw_control_set;
  2888. cdev->hw_control_get = phy_led_hw_control_get;
  2889. cdev->hw_control_trigger = "netdev";
  2890. }
  2891. cdev->hw_control_get_device = phy_led_hw_control_get_device;
  2892. #endif
  2893. cdev->max_brightness = 1;
  2894. init_data.devicename = dev_name(&phydev->mdio.dev);
  2895. init_data.fwnode = of_fwnode_handle(led);
  2896. init_data.devname_mandatory = true;
  2897. err = led_classdev_register_ext(dev, cdev, &init_data);
  2898. if (err)
  2899. return err;
  2900. list_add(&phyled->list, &phydev->leds);
  2901. return 0;
  2902. }
  2903. static int of_phy_leds(struct phy_device *phydev)
  2904. {
  2905. struct device_node *node = phydev->mdio.dev.of_node;
  2906. struct device_node *leds;
  2907. int err;
  2908. if (!IS_ENABLED(CONFIG_OF_MDIO))
  2909. return 0;
  2910. if (!node)
  2911. return 0;
  2912. leds = of_get_child_by_name(node, "leds");
  2913. if (!leds)
  2914. return 0;
  2915. for_each_available_child_of_node_scoped(leds, led) {
  2916. err = of_phy_led(phydev, led);
  2917. if (err) {
  2918. of_node_put(leds);
  2919. phy_leds_unregister(phydev);
  2920. return err;
  2921. }
  2922. }
  2923. of_node_put(leds);
  2924. return 0;
  2925. }
  2926. /**
  2927. * fwnode_mdio_find_device - Given a fwnode, find the mdio_device
  2928. * @fwnode: pointer to the mdio_device's fwnode
  2929. *
  2930. * If successful, returns a pointer to the mdio_device with the embedded
  2931. * struct device refcount incremented by one, or NULL on failure.
  2932. * The caller should call put_device() on the mdio_device after its use.
  2933. */
  2934. struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode)
  2935. {
  2936. struct device *d;
  2937. if (!fwnode)
  2938. return NULL;
  2939. d = bus_find_device_by_fwnode(&mdio_bus_type, fwnode);
  2940. if (!d)
  2941. return NULL;
  2942. return to_mdio_device(d);
  2943. }
  2944. EXPORT_SYMBOL(fwnode_mdio_find_device);
  2945. /**
  2946. * fwnode_phy_find_device - For provided phy_fwnode, find phy_device.
  2947. *
  2948. * @phy_fwnode: Pointer to the phy's fwnode.
  2949. *
  2950. * If successful, returns a pointer to the phy_device with the embedded
  2951. * struct device refcount incremented by one, or NULL on failure.
  2952. */
  2953. struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode)
  2954. {
  2955. struct mdio_device *mdiodev;
  2956. mdiodev = fwnode_mdio_find_device(phy_fwnode);
  2957. if (!mdiodev)
  2958. return NULL;
  2959. if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY)
  2960. return to_phy_device(&mdiodev->dev);
  2961. put_device(&mdiodev->dev);
  2962. return NULL;
  2963. }
  2964. EXPORT_SYMBOL(fwnode_phy_find_device);
  2965. /**
  2966. * device_phy_find_device - For the given device, get the phy_device
  2967. * @dev: Pointer to the given device
  2968. *
  2969. * Refer return conditions of fwnode_phy_find_device().
  2970. */
  2971. struct phy_device *device_phy_find_device(struct device *dev)
  2972. {
  2973. return fwnode_phy_find_device(dev_fwnode(dev));
  2974. }
  2975. EXPORT_SYMBOL_GPL(device_phy_find_device);
  2976. /**
  2977. * fwnode_get_phy_node - Get the phy_node using the named reference.
  2978. * @fwnode: Pointer to fwnode from which phy_node has to be obtained.
  2979. *
  2980. * Refer return conditions of fwnode_find_reference().
  2981. * For ACPI, only "phy-handle" is supported. Legacy DT properties "phy"
  2982. * and "phy-device" are not supported in ACPI. DT supports all the three
  2983. * named references to the phy node.
  2984. */
  2985. struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode)
  2986. {
  2987. struct fwnode_handle *phy_node;
  2988. /* Only phy-handle is used for ACPI */
  2989. phy_node = fwnode_find_reference(fwnode, "phy-handle", 0);
  2990. if (is_acpi_node(fwnode) || !IS_ERR(phy_node))
  2991. return phy_node;
  2992. phy_node = fwnode_find_reference(fwnode, "phy", 0);
  2993. if (IS_ERR(phy_node))
  2994. phy_node = fwnode_find_reference(fwnode, "phy-device", 0);
  2995. return phy_node;
  2996. }
  2997. EXPORT_SYMBOL_GPL(fwnode_get_phy_node);
  2998. /**
  2999. * phy_probe - probe and init a PHY device
  3000. * @dev: device to probe and init
  3001. *
  3002. * Take care of setting up the phy_device structure, set the state to READY.
  3003. */
  3004. static int phy_probe(struct device *dev)
  3005. {
  3006. struct phy_device *phydev = to_phy_device(dev);
  3007. struct device_driver *drv = phydev->mdio.dev.driver;
  3008. struct phy_driver *phydrv = to_phy_driver(drv);
  3009. int err = 0;
  3010. phydev->drv = phydrv;
  3011. /* Disable the interrupt if the PHY doesn't support it
  3012. * but the interrupt is still a valid one
  3013. */
  3014. if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
  3015. phydev->irq = PHY_POLL;
  3016. if (phydrv->flags & PHY_IS_INTERNAL)
  3017. phydev->is_internal = true;
  3018. /* Deassert the reset signal */
  3019. phy_device_reset(phydev, 0);
  3020. if (phydev->drv->probe) {
  3021. err = phydev->drv->probe(phydev);
  3022. if (err)
  3023. goto out;
  3024. }
  3025. phy_disable_interrupts(phydev);
  3026. /* Start out supporting everything. Eventually,
  3027. * a controller will attach, and may modify one
  3028. * or both of these values
  3029. */
  3030. if (phydrv->features) {
  3031. linkmode_copy(phydev->supported, phydrv->features);
  3032. genphy_c45_read_eee_abilities(phydev);
  3033. }
  3034. else if (phydrv->get_features)
  3035. err = phydrv->get_features(phydev);
  3036. else if (phydev->is_c45)
  3037. err = genphy_c45_pma_read_abilities(phydev);
  3038. else
  3039. err = genphy_read_abilities(phydev);
  3040. if (err)
  3041. goto out;
  3042. if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  3043. phydev->supported))
  3044. phydev->autoneg = 0;
  3045. if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
  3046. phydev->supported))
  3047. phydev->is_gigabit_capable = 1;
  3048. if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  3049. phydev->supported))
  3050. phydev->is_gigabit_capable = 1;
  3051. of_set_phy_supported(phydev);
  3052. phy_advertise_supported(phydev);
  3053. /* Get PHY default EEE advertising modes and handle them as potentially
  3054. * safe initial configuration.
  3055. */
  3056. err = genphy_c45_read_eee_adv(phydev, phydev->advertising_eee);
  3057. if (err)
  3058. goto out;
  3059. /* There is no "enabled" flag. If PHY is advertising, assume it is
  3060. * kind of enabled.
  3061. */
  3062. phydev->eee_enabled = !linkmode_empty(phydev->advertising_eee);
  3063. /* Some PHYs may advertise, by default, not support EEE modes. So,
  3064. * we need to clean them.
  3065. */
  3066. if (phydev->eee_enabled)
  3067. linkmode_and(phydev->advertising_eee, phydev->supported_eee,
  3068. phydev->advertising_eee);
  3069. /* Get the EEE modes we want to prohibit. We will ask
  3070. * the PHY stop advertising these mode later on
  3071. */
  3072. of_set_phy_eee_broken(phydev);
  3073. /* The Pause Frame bits indicate that the PHY can support passing
  3074. * pause frames. During autonegotiation, the PHYs will determine if
  3075. * they should allow pause frames to pass. The MAC driver should then
  3076. * use that result to determine whether to enable flow control via
  3077. * pause frames.
  3078. *
  3079. * Normally, PHY drivers should not set the Pause bits, and instead
  3080. * allow phylib to do that. However, there may be some situations
  3081. * (e.g. hardware erratum) where the driver wants to set only one
  3082. * of these bits.
  3083. */
  3084. if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
  3085. !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
  3086. linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  3087. phydev->supported);
  3088. linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  3089. phydev->supported);
  3090. }
  3091. /* Set the state to READY by default */
  3092. phydev->state = PHY_READY;
  3093. /* Get the LEDs from the device tree, and instantiate standard
  3094. * LEDs for them.
  3095. */
  3096. if (IS_ENABLED(CONFIG_PHYLIB_LEDS) && !phy_driver_is_genphy(phydev) &&
  3097. !phy_driver_is_genphy_10g(phydev))
  3098. err = of_phy_leds(phydev);
  3099. out:
  3100. /* Re-assert the reset signal on error */
  3101. if (err)
  3102. phy_device_reset(phydev, 1);
  3103. return err;
  3104. }
  3105. static int phy_remove(struct device *dev)
  3106. {
  3107. struct phy_device *phydev = to_phy_device(dev);
  3108. cancel_delayed_work_sync(&phydev->state_queue);
  3109. if (IS_ENABLED(CONFIG_PHYLIB_LEDS) && !phy_driver_is_genphy(phydev) &&
  3110. !phy_driver_is_genphy_10g(phydev))
  3111. phy_leds_unregister(phydev);
  3112. phydev->state = PHY_DOWN;
  3113. sfp_bus_del_upstream(phydev->sfp_bus);
  3114. phydev->sfp_bus = NULL;
  3115. if (phydev->drv && phydev->drv->remove)
  3116. phydev->drv->remove(phydev);
  3117. /* Assert the reset signal */
  3118. phy_device_reset(phydev, 1);
  3119. phydev->drv = NULL;
  3120. return 0;
  3121. }
  3122. /**
  3123. * phy_driver_register - register a phy_driver with the PHY layer
  3124. * @new_driver: new phy_driver to register
  3125. * @owner: module owning this PHY
  3126. */
  3127. int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
  3128. {
  3129. int retval;
  3130. /* Either the features are hard coded, or dynamically
  3131. * determined. It cannot be both.
  3132. */
  3133. if (WARN_ON(new_driver->features && new_driver->get_features)) {
  3134. pr_err("%s: features and get_features must not both be set\n",
  3135. new_driver->name);
  3136. return -EINVAL;
  3137. }
  3138. /* PHYLIB device drivers must not match using a DT compatible table
  3139. * as this bypasses our checks that the mdiodev that is being matched
  3140. * is backed by a struct phy_device. If such a case happens, we will
  3141. * make out-of-bounds accesses and lockup in phydev->lock.
  3142. */
  3143. if (WARN(new_driver->mdiodrv.driver.of_match_table,
  3144. "%s: driver must not provide a DT match table\n",
  3145. new_driver->name))
  3146. return -EINVAL;
  3147. new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
  3148. new_driver->mdiodrv.driver.name = new_driver->name;
  3149. new_driver->mdiodrv.driver.bus = &mdio_bus_type;
  3150. new_driver->mdiodrv.driver.probe = phy_probe;
  3151. new_driver->mdiodrv.driver.remove = phy_remove;
  3152. new_driver->mdiodrv.driver.owner = owner;
  3153. new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
  3154. retval = driver_register(&new_driver->mdiodrv.driver);
  3155. if (retval) {
  3156. pr_err("%s: Error %d in registering driver\n",
  3157. new_driver->name, retval);
  3158. return retval;
  3159. }
  3160. pr_debug("%s: Registered new driver\n", new_driver->name);
  3161. return 0;
  3162. }
  3163. EXPORT_SYMBOL(phy_driver_register);
  3164. int phy_drivers_register(struct phy_driver *new_driver, int n,
  3165. struct module *owner)
  3166. {
  3167. int i, ret = 0;
  3168. for (i = 0; i < n; i++) {
  3169. ret = phy_driver_register(new_driver + i, owner);
  3170. if (ret) {
  3171. while (i-- > 0)
  3172. phy_driver_unregister(new_driver + i);
  3173. break;
  3174. }
  3175. }
  3176. return ret;
  3177. }
  3178. EXPORT_SYMBOL(phy_drivers_register);
  3179. void phy_driver_unregister(struct phy_driver *drv)
  3180. {
  3181. driver_unregister(&drv->mdiodrv.driver);
  3182. }
  3183. EXPORT_SYMBOL(phy_driver_unregister);
  3184. void phy_drivers_unregister(struct phy_driver *drv, int n)
  3185. {
  3186. int i;
  3187. for (i = 0; i < n; i++)
  3188. phy_driver_unregister(drv + i);
  3189. }
  3190. EXPORT_SYMBOL(phy_drivers_unregister);
  3191. static struct phy_driver genphy_driver = {
  3192. .phy_id = 0xffffffff,
  3193. .phy_id_mask = 0xffffffff,
  3194. .name = "Generic PHY",
  3195. .get_features = genphy_read_abilities,
  3196. .suspend = genphy_suspend,
  3197. .resume = genphy_resume,
  3198. .set_loopback = genphy_loopback,
  3199. };
  3200. static const struct ethtool_phy_ops phy_ethtool_phy_ops = {
  3201. .get_sset_count = phy_ethtool_get_sset_count,
  3202. .get_strings = phy_ethtool_get_strings,
  3203. .get_stats = phy_ethtool_get_stats,
  3204. .get_plca_cfg = phy_ethtool_get_plca_cfg,
  3205. .set_plca_cfg = phy_ethtool_set_plca_cfg,
  3206. .get_plca_status = phy_ethtool_get_plca_status,
  3207. .start_cable_test = phy_start_cable_test,
  3208. .start_cable_test_tdr = phy_start_cable_test_tdr,
  3209. };
  3210. static const struct phylib_stubs __phylib_stubs = {
  3211. .hwtstamp_get = __phy_hwtstamp_get,
  3212. .hwtstamp_set = __phy_hwtstamp_set,
  3213. .get_phy_stats = __phy_ethtool_get_phy_stats,
  3214. .get_link_ext_stats = __phy_ethtool_get_link_ext_stats,
  3215. };
  3216. static void phylib_register_stubs(void)
  3217. {
  3218. phylib_stubs = &__phylib_stubs;
  3219. }
  3220. static void phylib_unregister_stubs(void)
  3221. {
  3222. phylib_stubs = NULL;
  3223. }
  3224. static int __init phy_init(void)
  3225. {
  3226. int rc;
  3227. rtnl_lock();
  3228. ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops);
  3229. phylib_register_stubs();
  3230. rtnl_unlock();
  3231. rc = mdio_bus_init();
  3232. if (rc)
  3233. goto err_ethtool_phy_ops;
  3234. features_init();
  3235. rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
  3236. if (rc)
  3237. goto err_mdio_bus;
  3238. rc = phy_driver_register(&genphy_driver, THIS_MODULE);
  3239. if (rc)
  3240. goto err_c45;
  3241. return 0;
  3242. err_c45:
  3243. phy_driver_unregister(&genphy_c45_driver);
  3244. err_mdio_bus:
  3245. mdio_bus_exit();
  3246. err_ethtool_phy_ops:
  3247. rtnl_lock();
  3248. phylib_unregister_stubs();
  3249. ethtool_set_ethtool_phy_ops(NULL);
  3250. rtnl_unlock();
  3251. return rc;
  3252. }
  3253. static void __exit phy_exit(void)
  3254. {
  3255. phy_driver_unregister(&genphy_c45_driver);
  3256. phy_driver_unregister(&genphy_driver);
  3257. mdio_bus_exit();
  3258. rtnl_lock();
  3259. phylib_unregister_stubs();
  3260. ethtool_set_ethtool_phy_ops(NULL);
  3261. rtnl_unlock();
  3262. }
  3263. subsys_initcall(phy_init);
  3264. module_exit(phy_exit);