phy_device.c 87 KB

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