phy_device.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421
  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/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/errno.h>
  13. #include <linux/unistd.h>
  14. #include <linux/slab.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/mm.h>
  22. #include <linux/module.h>
  23. #include <linux/mii.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/bitmap.h>
  26. #include <linux/phy.h>
  27. #include <linux/phy_led_triggers.h>
  28. #include <linux/mdio.h>
  29. #include <linux/io.h>
  30. #include <linux/uaccess.h>
  31. MODULE_DESCRIPTION("PHY library");
  32. MODULE_AUTHOR("Andy Fleming");
  33. MODULE_LICENSE("GPL");
  34. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
  35. EXPORT_SYMBOL_GPL(phy_basic_features);
  36. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
  37. EXPORT_SYMBOL_GPL(phy_basic_t1_features);
  38. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
  39. EXPORT_SYMBOL_GPL(phy_gbit_features);
  40. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
  41. EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
  42. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
  43. EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features);
  44. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
  45. EXPORT_SYMBOL_GPL(phy_10gbit_features);
  46. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
  47. EXPORT_SYMBOL_GPL(phy_10gbit_fec_features);
  48. const int phy_basic_ports_array[3] = {
  49. ETHTOOL_LINK_MODE_Autoneg_BIT,
  50. ETHTOOL_LINK_MODE_TP_BIT,
  51. ETHTOOL_LINK_MODE_MII_BIT,
  52. };
  53. EXPORT_SYMBOL_GPL(phy_basic_ports_array);
  54. const int phy_fibre_port_array[1] = {
  55. ETHTOOL_LINK_MODE_FIBRE_BIT,
  56. };
  57. EXPORT_SYMBOL_GPL(phy_fibre_port_array);
  58. const int phy_all_ports_features_array[7] = {
  59. ETHTOOL_LINK_MODE_Autoneg_BIT,
  60. ETHTOOL_LINK_MODE_TP_BIT,
  61. ETHTOOL_LINK_MODE_MII_BIT,
  62. ETHTOOL_LINK_MODE_FIBRE_BIT,
  63. ETHTOOL_LINK_MODE_AUI_BIT,
  64. ETHTOOL_LINK_MODE_BNC_BIT,
  65. ETHTOOL_LINK_MODE_Backplane_BIT,
  66. };
  67. EXPORT_SYMBOL_GPL(phy_all_ports_features_array);
  68. const int phy_10_100_features_array[4] = {
  69. ETHTOOL_LINK_MODE_10baseT_Half_BIT,
  70. ETHTOOL_LINK_MODE_10baseT_Full_BIT,
  71. ETHTOOL_LINK_MODE_100baseT_Half_BIT,
  72. ETHTOOL_LINK_MODE_100baseT_Full_BIT,
  73. };
  74. EXPORT_SYMBOL_GPL(phy_10_100_features_array);
  75. const int phy_basic_t1_features_array[2] = {
  76. ETHTOOL_LINK_MODE_TP_BIT,
  77. ETHTOOL_LINK_MODE_100baseT1_Full_BIT,
  78. };
  79. EXPORT_SYMBOL_GPL(phy_basic_t1_features_array);
  80. const int phy_gbit_features_array[2] = {
  81. ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
  82. ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  83. };
  84. EXPORT_SYMBOL_GPL(phy_gbit_features_array);
  85. const int phy_10gbit_features_array[1] = {
  86. ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
  87. };
  88. EXPORT_SYMBOL_GPL(phy_10gbit_features_array);
  89. const int phy_10gbit_fec_features_array[1] = {
  90. ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
  91. };
  92. EXPORT_SYMBOL_GPL(phy_10gbit_fec_features_array);
  93. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
  94. EXPORT_SYMBOL_GPL(phy_10gbit_full_features);
  95. static const int phy_10gbit_full_features_array[] = {
  96. ETHTOOL_LINK_MODE_10baseT_Full_BIT,
  97. ETHTOOL_LINK_MODE_100baseT_Full_BIT,
  98. ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  99. ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
  100. };
  101. static void features_init(void)
  102. {
  103. /* 10/100 half/full*/
  104. linkmode_set_bit_array(phy_basic_ports_array,
  105. ARRAY_SIZE(phy_basic_ports_array),
  106. phy_basic_features);
  107. linkmode_set_bit_array(phy_10_100_features_array,
  108. ARRAY_SIZE(phy_10_100_features_array),
  109. phy_basic_features);
  110. /* 100 full, TP */
  111. linkmode_set_bit_array(phy_basic_t1_features_array,
  112. ARRAY_SIZE(phy_basic_t1_features_array),
  113. phy_basic_t1_features);
  114. /* 10/100 half/full + 1000 half/full */
  115. linkmode_set_bit_array(phy_basic_ports_array,
  116. ARRAY_SIZE(phy_basic_ports_array),
  117. phy_gbit_features);
  118. linkmode_set_bit_array(phy_10_100_features_array,
  119. ARRAY_SIZE(phy_10_100_features_array),
  120. phy_gbit_features);
  121. linkmode_set_bit_array(phy_gbit_features_array,
  122. ARRAY_SIZE(phy_gbit_features_array),
  123. phy_gbit_features);
  124. /* 10/100 half/full + 1000 half/full + fibre*/
  125. linkmode_set_bit_array(phy_basic_ports_array,
  126. ARRAY_SIZE(phy_basic_ports_array),
  127. phy_gbit_fibre_features);
  128. linkmode_set_bit_array(phy_10_100_features_array,
  129. ARRAY_SIZE(phy_10_100_features_array),
  130. phy_gbit_fibre_features);
  131. linkmode_set_bit_array(phy_gbit_features_array,
  132. ARRAY_SIZE(phy_gbit_features_array),
  133. phy_gbit_fibre_features);
  134. linkmode_set_bit_array(phy_fibre_port_array,
  135. ARRAY_SIZE(phy_fibre_port_array),
  136. phy_gbit_fibre_features);
  137. /* 10/100 half/full + 1000 half/full + TP/MII/FIBRE/AUI/BNC/Backplane*/
  138. linkmode_set_bit_array(phy_all_ports_features_array,
  139. ARRAY_SIZE(phy_all_ports_features_array),
  140. phy_gbit_all_ports_features);
  141. linkmode_set_bit_array(phy_10_100_features_array,
  142. ARRAY_SIZE(phy_10_100_features_array),
  143. phy_gbit_all_ports_features);
  144. linkmode_set_bit_array(phy_gbit_features_array,
  145. ARRAY_SIZE(phy_gbit_features_array),
  146. phy_gbit_all_ports_features);
  147. /* 10/100 half/full + 1000 half/full + 10G full*/
  148. linkmode_set_bit_array(phy_all_ports_features_array,
  149. ARRAY_SIZE(phy_all_ports_features_array),
  150. phy_10gbit_features);
  151. linkmode_set_bit_array(phy_10_100_features_array,
  152. ARRAY_SIZE(phy_10_100_features_array),
  153. phy_10gbit_features);
  154. linkmode_set_bit_array(phy_gbit_features_array,
  155. ARRAY_SIZE(phy_gbit_features_array),
  156. phy_10gbit_features);
  157. linkmode_set_bit_array(phy_10gbit_features_array,
  158. ARRAY_SIZE(phy_10gbit_features_array),
  159. phy_10gbit_features);
  160. /* 10/100/1000/10G full */
  161. linkmode_set_bit_array(phy_all_ports_features_array,
  162. ARRAY_SIZE(phy_all_ports_features_array),
  163. phy_10gbit_full_features);
  164. linkmode_set_bit_array(phy_10gbit_full_features_array,
  165. ARRAY_SIZE(phy_10gbit_full_features_array),
  166. phy_10gbit_full_features);
  167. /* 10G FEC only */
  168. linkmode_set_bit_array(phy_10gbit_fec_features_array,
  169. ARRAY_SIZE(phy_10gbit_fec_features_array),
  170. phy_10gbit_fec_features);
  171. }
  172. void phy_device_free(struct phy_device *phydev)
  173. {
  174. put_device(&phydev->mdio.dev);
  175. }
  176. EXPORT_SYMBOL(phy_device_free);
  177. static void phy_mdio_device_free(struct mdio_device *mdiodev)
  178. {
  179. struct phy_device *phydev;
  180. phydev = container_of(mdiodev, struct phy_device, mdio);
  181. phy_device_free(phydev);
  182. }
  183. static void phy_device_release(struct device *dev)
  184. {
  185. kfree(to_phy_device(dev));
  186. }
  187. static void phy_mdio_device_remove(struct mdio_device *mdiodev)
  188. {
  189. struct phy_device *phydev;
  190. phydev = container_of(mdiodev, struct phy_device, mdio);
  191. phy_device_remove(phydev);
  192. }
  193. static struct phy_driver genphy_driver;
  194. extern struct phy_driver genphy_c45_driver;
  195. static LIST_HEAD(phy_fixup_list);
  196. static DEFINE_MUTEX(phy_fixup_lock);
  197. #ifdef CONFIG_PM
  198. static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
  199. {
  200. struct device_driver *drv = phydev->mdio.dev.driver;
  201. struct phy_driver *phydrv = to_phy_driver(drv);
  202. struct net_device *netdev = phydev->attached_dev;
  203. if (!drv || !phydrv->suspend)
  204. return false;
  205. /* PHY not attached? May suspend if the PHY has not already been
  206. * suspended as part of a prior call to phy_disconnect() ->
  207. * phy_detach() -> phy_suspend() because the parent netdev might be the
  208. * MDIO bus driver and clock gated at this point.
  209. */
  210. if (!netdev)
  211. goto out;
  212. if (netdev->wol_enabled)
  213. return false;
  214. /* As long as not all affected network drivers support the
  215. * wol_enabled flag, let's check for hints that WoL is enabled.
  216. * Don't suspend PHY if the attached netdev parent may wake up.
  217. * The parent may point to a PCI device, as in tg3 driver.
  218. */
  219. if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
  220. return false;
  221. /* Also don't suspend PHY if the netdev itself may wakeup. This
  222. * is the case for devices w/o underlaying pwr. mgmt. aware bus,
  223. * e.g. SoC devices.
  224. */
  225. if (device_may_wakeup(&netdev->dev))
  226. return false;
  227. out:
  228. return !phydev->suspended;
  229. }
  230. static int mdio_bus_phy_suspend(struct device *dev)
  231. {
  232. struct phy_device *phydev = to_phy_device(dev);
  233. /* We must stop the state machine manually, otherwise it stops out of
  234. * control, possibly with the phydev->lock held. Upon resume, netdev
  235. * may call phy routines that try to grab the same lock, and that may
  236. * lead to a deadlock.
  237. */
  238. if (phydev->attached_dev && phydev->adjust_link)
  239. phy_stop_machine(phydev);
  240. if (!mdio_bus_phy_may_suspend(phydev))
  241. return 0;
  242. phydev->suspended_by_mdio_bus = 1;
  243. return phy_suspend(phydev);
  244. }
  245. static int mdio_bus_phy_resume(struct device *dev)
  246. {
  247. struct phy_device *phydev = to_phy_device(dev);
  248. int ret;
  249. if (!phydev->suspended_by_mdio_bus)
  250. goto no_resume;
  251. phydev->suspended_by_mdio_bus = 0;
  252. ret = phy_resume(phydev);
  253. if (ret < 0)
  254. return ret;
  255. no_resume:
  256. if (phydev->attached_dev && phydev->adjust_link)
  257. phy_start_machine(phydev);
  258. return 0;
  259. }
  260. static int mdio_bus_phy_restore(struct device *dev)
  261. {
  262. struct phy_device *phydev = to_phy_device(dev);
  263. struct net_device *netdev = phydev->attached_dev;
  264. int ret;
  265. if (!netdev)
  266. return 0;
  267. ret = phy_init_hw(phydev);
  268. if (ret < 0)
  269. return ret;
  270. if (phydev->attached_dev && phydev->adjust_link)
  271. phy_start_machine(phydev);
  272. return 0;
  273. }
  274. static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
  275. .suspend = mdio_bus_phy_suspend,
  276. .resume = mdio_bus_phy_resume,
  277. .freeze = mdio_bus_phy_suspend,
  278. .thaw = mdio_bus_phy_resume,
  279. .restore = mdio_bus_phy_restore,
  280. };
  281. #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
  282. #else
  283. #define MDIO_BUS_PHY_PM_OPS NULL
  284. #endif /* CONFIG_PM */
  285. /**
  286. * phy_register_fixup - creates a new phy_fixup and adds it to the list
  287. * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
  288. * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
  289. * It can also be PHY_ANY_UID
  290. * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
  291. * comparison
  292. * @run: The actual code to be run when a matching PHY is found
  293. */
  294. int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
  295. int (*run)(struct phy_device *))
  296. {
  297. struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
  298. if (!fixup)
  299. return -ENOMEM;
  300. strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
  301. fixup->phy_uid = phy_uid;
  302. fixup->phy_uid_mask = phy_uid_mask;
  303. fixup->run = run;
  304. mutex_lock(&phy_fixup_lock);
  305. list_add_tail(&fixup->list, &phy_fixup_list);
  306. mutex_unlock(&phy_fixup_lock);
  307. return 0;
  308. }
  309. EXPORT_SYMBOL(phy_register_fixup);
  310. /* Registers a fixup to be run on any PHY with the UID in phy_uid */
  311. int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
  312. int (*run)(struct phy_device *))
  313. {
  314. return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
  315. }
  316. EXPORT_SYMBOL(phy_register_fixup_for_uid);
  317. /* Registers a fixup to be run on the PHY with id string bus_id */
  318. int phy_register_fixup_for_id(const char *bus_id,
  319. int (*run)(struct phy_device *))
  320. {
  321. return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
  322. }
  323. EXPORT_SYMBOL(phy_register_fixup_for_id);
  324. /**
  325. * phy_unregister_fixup - remove a phy_fixup from the list
  326. * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
  327. * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
  328. * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
  329. */
  330. int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
  331. {
  332. struct list_head *pos, *n;
  333. struct phy_fixup *fixup;
  334. int ret;
  335. ret = -ENODEV;
  336. mutex_lock(&phy_fixup_lock);
  337. list_for_each_safe(pos, n, &phy_fixup_list) {
  338. fixup = list_entry(pos, struct phy_fixup, list);
  339. if ((!strcmp(fixup->bus_id, bus_id)) &&
  340. ((fixup->phy_uid & phy_uid_mask) ==
  341. (phy_uid & phy_uid_mask))) {
  342. list_del(&fixup->list);
  343. kfree(fixup);
  344. ret = 0;
  345. break;
  346. }
  347. }
  348. mutex_unlock(&phy_fixup_lock);
  349. return ret;
  350. }
  351. EXPORT_SYMBOL(phy_unregister_fixup);
  352. /* Unregisters a fixup of any PHY with the UID in phy_uid */
  353. int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
  354. {
  355. return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
  356. }
  357. EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
  358. /* Unregisters a fixup of the PHY with id string bus_id */
  359. int phy_unregister_fixup_for_id(const char *bus_id)
  360. {
  361. return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
  362. }
  363. EXPORT_SYMBOL(phy_unregister_fixup_for_id);
  364. /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
  365. * Fixups can be set to match any in one or more fields.
  366. */
  367. static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
  368. {
  369. if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
  370. if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
  371. return 0;
  372. if ((fixup->phy_uid & fixup->phy_uid_mask) !=
  373. (phydev->phy_id & fixup->phy_uid_mask))
  374. if (fixup->phy_uid != PHY_ANY_UID)
  375. return 0;
  376. return 1;
  377. }
  378. /* Runs any matching fixups for this phydev */
  379. static int phy_scan_fixups(struct phy_device *phydev)
  380. {
  381. struct phy_fixup *fixup;
  382. mutex_lock(&phy_fixup_lock);
  383. list_for_each_entry(fixup, &phy_fixup_list, list) {
  384. if (phy_needs_fixup(phydev, fixup)) {
  385. int err = fixup->run(phydev);
  386. if (err < 0) {
  387. mutex_unlock(&phy_fixup_lock);
  388. return err;
  389. }
  390. phydev->has_fixups = true;
  391. }
  392. }
  393. mutex_unlock(&phy_fixup_lock);
  394. return 0;
  395. }
  396. static int phy_bus_match(struct device *dev, struct device_driver *drv)
  397. {
  398. struct phy_device *phydev = to_phy_device(dev);
  399. struct phy_driver *phydrv = to_phy_driver(drv);
  400. const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
  401. int i;
  402. if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
  403. return 0;
  404. if (phydrv->match_phy_device)
  405. return phydrv->match_phy_device(phydev);
  406. if (phydev->is_c45) {
  407. for (i = 1; i < num_ids; i++) {
  408. if (phydev->c45_ids.device_ids[i] == 0xffffffff)
  409. continue;
  410. if ((phydrv->phy_id & phydrv->phy_id_mask) ==
  411. (phydev->c45_ids.device_ids[i] &
  412. phydrv->phy_id_mask))
  413. return 1;
  414. }
  415. return 0;
  416. } else {
  417. return (phydrv->phy_id & phydrv->phy_id_mask) ==
  418. (phydev->phy_id & phydrv->phy_id_mask);
  419. }
  420. }
  421. static ssize_t
  422. phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
  423. {
  424. struct phy_device *phydev = to_phy_device(dev);
  425. return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
  426. }
  427. static DEVICE_ATTR_RO(phy_id);
  428. static ssize_t
  429. phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
  430. {
  431. struct phy_device *phydev = to_phy_device(dev);
  432. const char *mode = NULL;
  433. if (phy_is_internal(phydev))
  434. mode = "internal";
  435. else
  436. mode = phy_modes(phydev->interface);
  437. return sprintf(buf, "%s\n", mode);
  438. }
  439. static DEVICE_ATTR_RO(phy_interface);
  440. static ssize_t
  441. phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
  442. char *buf)
  443. {
  444. struct phy_device *phydev = to_phy_device(dev);
  445. return sprintf(buf, "%d\n", phydev->has_fixups);
  446. }
  447. static DEVICE_ATTR_RO(phy_has_fixups);
  448. static struct attribute *phy_dev_attrs[] = {
  449. &dev_attr_phy_id.attr,
  450. &dev_attr_phy_interface.attr,
  451. &dev_attr_phy_has_fixups.attr,
  452. NULL,
  453. };
  454. ATTRIBUTE_GROUPS(phy_dev);
  455. static const struct device_type mdio_bus_phy_type = {
  456. .name = "PHY",
  457. .groups = phy_dev_groups,
  458. .release = phy_device_release,
  459. .pm = MDIO_BUS_PHY_PM_OPS,
  460. };
  461. static int phy_request_driver_module(struct phy_device *dev, u32 phy_id)
  462. {
  463. int ret;
  464. ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
  465. MDIO_ID_ARGS(phy_id));
  466. /* We only check for failures in executing the usermode binary,
  467. * not whether a PHY driver module exists for the PHY ID.
  468. * Accept -ENOENT because this may occur in case no initramfs exists,
  469. * then modprobe isn't available.
  470. */
  471. if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
  472. phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n",
  473. ret, (unsigned long)phy_id);
  474. return ret;
  475. }
  476. return 0;
  477. }
  478. struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
  479. bool is_c45,
  480. struct phy_c45_device_ids *c45_ids)
  481. {
  482. struct phy_device *dev;
  483. struct mdio_device *mdiodev;
  484. int ret = 0;
  485. /* We allocate the device, and initialize the default values */
  486. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  487. if (!dev)
  488. return ERR_PTR(-ENOMEM);
  489. mdiodev = &dev->mdio;
  490. mdiodev->dev.parent = &bus->dev;
  491. mdiodev->dev.bus = &mdio_bus_type;
  492. mdiodev->dev.type = &mdio_bus_phy_type;
  493. mdiodev->bus = bus;
  494. mdiodev->bus_match = phy_bus_match;
  495. mdiodev->addr = addr;
  496. mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
  497. mdiodev->device_free = phy_mdio_device_free;
  498. mdiodev->device_remove = phy_mdio_device_remove;
  499. dev->speed = SPEED_UNKNOWN;
  500. dev->duplex = DUPLEX_UNKNOWN;
  501. dev->pause = 0;
  502. dev->asym_pause = 0;
  503. dev->link = 0;
  504. dev->interface = PHY_INTERFACE_MODE_GMII;
  505. dev->autoneg = AUTONEG_ENABLE;
  506. dev->is_c45 = is_c45;
  507. dev->phy_id = phy_id;
  508. if (c45_ids)
  509. dev->c45_ids = *c45_ids;
  510. dev->irq = bus->irq[addr];
  511. dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
  512. device_initialize(&mdiodev->dev);
  513. dev->state = PHY_DOWN;
  514. mutex_init(&dev->lock);
  515. INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
  516. /* Request the appropriate module unconditionally; don't
  517. * bother trying to do so only if it isn't already loaded,
  518. * because that gets complicated. A hotplug event would have
  519. * done an unconditional modprobe anyway.
  520. * We don't do normal hotplug because it won't work for MDIO
  521. * -- because it relies on the device staying around for long
  522. * enough for the driver to get loaded. With MDIO, the NIC
  523. * driver will get bored and give up as soon as it finds that
  524. * there's no driver _already_ loaded.
  525. */
  526. if (is_c45 && c45_ids) {
  527. const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
  528. int i;
  529. for (i = 1; i < num_ids; i++) {
  530. if (c45_ids->device_ids[i] == 0xffffffff)
  531. continue;
  532. ret = phy_request_driver_module(dev,
  533. c45_ids->device_ids[i]);
  534. if (ret)
  535. break;
  536. }
  537. } else {
  538. ret = phy_request_driver_module(dev, phy_id);
  539. }
  540. if (ret) {
  541. put_device(&mdiodev->dev);
  542. dev = ERR_PTR(ret);
  543. }
  544. return dev;
  545. }
  546. EXPORT_SYMBOL(phy_device_create);
  547. /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
  548. * @bus: the target MII bus
  549. * @addr: PHY address on the MII bus
  550. * @dev_addr: MMD address in the PHY.
  551. * @devices_in_package: where to store the devices in package information.
  552. *
  553. * Description: reads devices in package registers of a MMD at @dev_addr
  554. * from PHY at @addr on @bus.
  555. *
  556. * Returns: 0 on success, -EIO on failure.
  557. */
  558. static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
  559. u32 *devices_in_package)
  560. {
  561. int phy_reg, reg_addr;
  562. reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
  563. phy_reg = mdiobus_read(bus, addr, reg_addr);
  564. if (phy_reg < 0)
  565. return -EIO;
  566. *devices_in_package = phy_reg << 16;
  567. reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
  568. phy_reg = mdiobus_read(bus, addr, reg_addr);
  569. if (phy_reg < 0)
  570. return -EIO;
  571. *devices_in_package |= phy_reg;
  572. /* Bit 0 doesn't represent a device, it indicates c22 regs presence */
  573. *devices_in_package &= ~BIT(0);
  574. return 0;
  575. }
  576. /**
  577. * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
  578. * @bus: the target MII bus
  579. * @addr: PHY address on the MII bus
  580. * @phy_id: where to store the ID retrieved.
  581. * @c45_ids: where to store the c45 ID information.
  582. *
  583. * If the PHY devices-in-package appears to be valid, it and the
  584. * corresponding identifiers are stored in @c45_ids, zero is stored
  585. * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
  586. * zero on success.
  587. *
  588. */
  589. static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
  590. struct phy_c45_device_ids *c45_ids) {
  591. int phy_reg;
  592. int i, reg_addr;
  593. const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
  594. u32 *devs = &c45_ids->devices_in_package;
  595. /* Find first non-zero Devices In package. Device zero is reserved
  596. * for 802.3 c45 complied PHYs, so don't probe it at first.
  597. */
  598. for (i = 1; i < num_ids && *devs == 0; i++) {
  599. phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
  600. if (phy_reg < 0)
  601. return -EIO;
  602. if ((*devs & 0x1fffffff) == 0x1fffffff) {
  603. /* If mostly Fs, there is no device there,
  604. * then let's continue to probe more, as some
  605. * 10G PHYs have zero Devices In package,
  606. * e.g. Cortina CS4315/CS4340 PHY.
  607. */
  608. phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
  609. if (phy_reg < 0)
  610. return -EIO;
  611. /* no device there, let's get out of here */
  612. if ((*devs & 0x1fffffff) == 0x1fffffff) {
  613. *phy_id = 0xffffffff;
  614. return 0;
  615. } else {
  616. break;
  617. }
  618. }
  619. }
  620. /* Now probe Device Identifiers for each device present. */
  621. for (i = 1; i < num_ids; i++) {
  622. if (!(c45_ids->devices_in_package & (1 << i)))
  623. continue;
  624. reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
  625. phy_reg = mdiobus_read(bus, addr, reg_addr);
  626. if (phy_reg < 0)
  627. return -EIO;
  628. c45_ids->device_ids[i] = phy_reg << 16;
  629. reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
  630. phy_reg = mdiobus_read(bus, addr, reg_addr);
  631. if (phy_reg < 0)
  632. return -EIO;
  633. c45_ids->device_ids[i] |= phy_reg;
  634. }
  635. *phy_id = 0;
  636. return 0;
  637. }
  638. /**
  639. * get_phy_id - reads the specified addr for its ID.
  640. * @bus: the target MII bus
  641. * @addr: PHY address on the MII bus
  642. * @phy_id: where to store the ID retrieved.
  643. * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
  644. * @c45_ids: where to store the c45 ID information.
  645. *
  646. * Description: In the case of a 802.3-c22 PHY, reads the ID registers
  647. * of the PHY at @addr on the @bus, stores it in @phy_id and returns
  648. * zero on success.
  649. *
  650. * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
  651. * its return value is in turn returned.
  652. *
  653. */
  654. static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
  655. bool is_c45, struct phy_c45_device_ids *c45_ids)
  656. {
  657. int phy_reg;
  658. if (is_c45)
  659. return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
  660. /* Grab the bits from PHYIR1, and put them in the upper half */
  661. phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
  662. if (phy_reg < 0) {
  663. /* returning -ENODEV doesn't stop bus scanning */
  664. return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
  665. }
  666. *phy_id = phy_reg << 16;
  667. /* Grab the bits from PHYIR2, and put them in the lower half */
  668. phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
  669. if (phy_reg < 0) {
  670. /* returning -ENODEV doesn't stop bus scanning */
  671. return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
  672. }
  673. *phy_id |= phy_reg;
  674. return 0;
  675. }
  676. /**
  677. * get_phy_device - reads the specified PHY device and returns its @phy_device
  678. * struct
  679. * @bus: the target MII bus
  680. * @addr: PHY address on the MII bus
  681. * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
  682. *
  683. * Description: Reads the ID registers of the PHY at @addr on the
  684. * @bus, then allocates and returns the phy_device to represent it.
  685. */
  686. struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
  687. {
  688. struct phy_c45_device_ids c45_ids;
  689. u32 phy_id = 0;
  690. int r;
  691. c45_ids.devices_in_package = 0;
  692. memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids));
  693. r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
  694. if (r)
  695. return ERR_PTR(r);
  696. /* If the phy_id is mostly Fs, there is no device there */
  697. if ((phy_id & 0x1fffffff) == 0x1fffffff)
  698. return ERR_PTR(-ENODEV);
  699. return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
  700. }
  701. EXPORT_SYMBOL(get_phy_device);
  702. /**
  703. * phy_device_register - Register the phy device on the MDIO bus
  704. * @phydev: phy_device structure to be added to the MDIO bus
  705. */
  706. int phy_device_register(struct phy_device *phydev)
  707. {
  708. int err;
  709. err = mdiobus_register_device(&phydev->mdio);
  710. if (err)
  711. return err;
  712. /* Deassert the reset signal */
  713. phy_device_reset(phydev, 0);
  714. /* Run all of the fixups for this PHY */
  715. err = phy_scan_fixups(phydev);
  716. if (err) {
  717. phydev_err(phydev, "failed to initialize\n");
  718. goto out;
  719. }
  720. err = device_add(&phydev->mdio.dev);
  721. if (err) {
  722. phydev_err(phydev, "failed to add\n");
  723. goto out;
  724. }
  725. return 0;
  726. out:
  727. /* Assert the reset signal */
  728. phy_device_reset(phydev, 1);
  729. mdiobus_unregister_device(&phydev->mdio);
  730. return err;
  731. }
  732. EXPORT_SYMBOL(phy_device_register);
  733. /**
  734. * phy_device_remove - Remove a previously registered phy device from the MDIO bus
  735. * @phydev: phy_device structure to remove
  736. *
  737. * This doesn't free the phy_device itself, it merely reverses the effects
  738. * of phy_device_register(). Use phy_device_free() to free the device
  739. * after calling this function.
  740. */
  741. void phy_device_remove(struct phy_device *phydev)
  742. {
  743. device_del(&phydev->mdio.dev);
  744. /* Assert the reset signal */
  745. phy_device_reset(phydev, 1);
  746. mdiobus_unregister_device(&phydev->mdio);
  747. }
  748. EXPORT_SYMBOL(phy_device_remove);
  749. /**
  750. * phy_find_first - finds the first PHY device on the bus
  751. * @bus: the target MII bus
  752. */
  753. struct phy_device *phy_find_first(struct mii_bus *bus)
  754. {
  755. struct phy_device *phydev;
  756. int addr;
  757. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  758. phydev = mdiobus_get_phy(bus, addr);
  759. if (phydev)
  760. return phydev;
  761. }
  762. return NULL;
  763. }
  764. EXPORT_SYMBOL(phy_find_first);
  765. static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
  766. {
  767. struct net_device *netdev = phydev->attached_dev;
  768. if (do_carrier) {
  769. if (up)
  770. netif_carrier_on(netdev);
  771. else
  772. netif_carrier_off(netdev);
  773. }
  774. phydev->adjust_link(netdev);
  775. }
  776. /**
  777. * phy_prepare_link - prepares the PHY layer to monitor link status
  778. * @phydev: target phy_device struct
  779. * @handler: callback function for link status change notifications
  780. *
  781. * Description: Tells the PHY infrastructure to handle the
  782. * gory details on monitoring link status (whether through
  783. * polling or an interrupt), and to call back to the
  784. * connected device driver when the link status changes.
  785. * If you want to monitor your own link state, don't call
  786. * this function.
  787. */
  788. static void phy_prepare_link(struct phy_device *phydev,
  789. void (*handler)(struct net_device *))
  790. {
  791. phydev->adjust_link = handler;
  792. }
  793. /**
  794. * phy_connect_direct - connect an ethernet device to a specific phy_device
  795. * @dev: the network device to connect
  796. * @phydev: the pointer to the phy device
  797. * @handler: callback function for state change notifications
  798. * @interface: PHY device's interface
  799. */
  800. int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
  801. void (*handler)(struct net_device *),
  802. phy_interface_t interface)
  803. {
  804. int rc;
  805. if (!dev)
  806. return -EINVAL;
  807. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  808. if (rc)
  809. return rc;
  810. phy_prepare_link(phydev, handler);
  811. if (phy_interrupt_is_valid(phydev))
  812. phy_request_interrupt(phydev);
  813. return 0;
  814. }
  815. EXPORT_SYMBOL(phy_connect_direct);
  816. /**
  817. * phy_connect - connect an ethernet device to a PHY device
  818. * @dev: the network device to connect
  819. * @bus_id: the id string of the PHY device to connect
  820. * @handler: callback function for state change notifications
  821. * @interface: PHY device's interface
  822. *
  823. * Description: Convenience function for connecting ethernet
  824. * devices to PHY devices. The default behavior is for
  825. * the PHY infrastructure to handle everything, and only notify
  826. * the connected driver when the link status changes. If you
  827. * don't want, or can't use the provided functionality, you may
  828. * choose to call only the subset of functions which provide
  829. * the desired functionality.
  830. */
  831. struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
  832. void (*handler)(struct net_device *),
  833. phy_interface_t interface)
  834. {
  835. struct phy_device *phydev;
  836. struct device *d;
  837. int rc;
  838. /* Search the list of PHY devices on the mdio bus for the
  839. * PHY with the requested name
  840. */
  841. d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
  842. if (!d) {
  843. pr_err("PHY %s not found\n", bus_id);
  844. return ERR_PTR(-ENODEV);
  845. }
  846. phydev = to_phy_device(d);
  847. rc = phy_connect_direct(dev, phydev, handler, interface);
  848. put_device(d);
  849. if (rc)
  850. return ERR_PTR(rc);
  851. return phydev;
  852. }
  853. EXPORT_SYMBOL(phy_connect);
  854. /**
  855. * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
  856. * device
  857. * @phydev: target phy_device struct
  858. */
  859. void phy_disconnect(struct phy_device *phydev)
  860. {
  861. if (phy_is_started(phydev))
  862. phy_stop(phydev);
  863. if (phy_interrupt_is_valid(phydev))
  864. phy_free_interrupt(phydev);
  865. phydev->adjust_link = NULL;
  866. phy_detach(phydev);
  867. }
  868. EXPORT_SYMBOL(phy_disconnect);
  869. /**
  870. * phy_poll_reset - Safely wait until a PHY reset has properly completed
  871. * @phydev: The PHY device to poll
  872. *
  873. * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
  874. * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
  875. * register must be polled until the BMCR_RESET bit clears.
  876. *
  877. * Furthermore, any attempts to write to PHY registers may have no effect
  878. * or even generate MDIO bus errors until this is complete.
  879. *
  880. * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
  881. * standard and do not fully reset after the BMCR_RESET bit is set, and may
  882. * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
  883. * effort to support such broken PHYs, this function is separate from the
  884. * standard phy_init_hw() which will zero all the other bits in the BMCR
  885. * and reapply all driver-specific and board-specific fixups.
  886. */
  887. static int phy_poll_reset(struct phy_device *phydev)
  888. {
  889. /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
  890. unsigned int retries = 12;
  891. int ret;
  892. do {
  893. msleep(50);
  894. ret = phy_read(phydev, MII_BMCR);
  895. if (ret < 0)
  896. return ret;
  897. } while (ret & BMCR_RESET && --retries);
  898. if (ret & BMCR_RESET)
  899. return -ETIMEDOUT;
  900. /* Some chips (smsc911x) may still need up to another 1ms after the
  901. * BMCR_RESET bit is cleared before they are usable.
  902. */
  903. msleep(1);
  904. return 0;
  905. }
  906. int phy_init_hw(struct phy_device *phydev)
  907. {
  908. int ret = 0;
  909. /* Deassert the reset signal */
  910. phy_device_reset(phydev, 0);
  911. if (!phydev->drv)
  912. return 0;
  913. if (phydev->drv->soft_reset)
  914. ret = phydev->drv->soft_reset(phydev);
  915. if (ret < 0)
  916. return ret;
  917. ret = phy_scan_fixups(phydev);
  918. if (ret < 0)
  919. return ret;
  920. if (phydev->drv->config_init)
  921. ret = phydev->drv->config_init(phydev);
  922. return ret;
  923. }
  924. EXPORT_SYMBOL(phy_init_hw);
  925. void phy_attached_info(struct phy_device *phydev)
  926. {
  927. phy_attached_print(phydev, NULL);
  928. }
  929. EXPORT_SYMBOL(phy_attached_info);
  930. #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
  931. void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
  932. {
  933. const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
  934. char *irq_str;
  935. char irq_num[8];
  936. switch(phydev->irq) {
  937. case PHY_POLL:
  938. irq_str = "POLL";
  939. break;
  940. case PHY_IGNORE_INTERRUPT:
  941. irq_str = "IGNORE";
  942. break;
  943. default:
  944. snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
  945. irq_str = irq_num;
  946. break;
  947. }
  948. if (!fmt) {
  949. phydev_info(phydev, ATTACHED_FMT "\n",
  950. drv_name, phydev_name(phydev),
  951. irq_str);
  952. } else {
  953. va_list ap;
  954. phydev_info(phydev, ATTACHED_FMT,
  955. drv_name, phydev_name(phydev),
  956. irq_str);
  957. va_start(ap, fmt);
  958. vprintk(fmt, ap);
  959. va_end(ap);
  960. }
  961. }
  962. EXPORT_SYMBOL(phy_attached_print);
  963. static void phy_sysfs_create_links(struct phy_device *phydev)
  964. {
  965. struct net_device *dev = phydev->attached_dev;
  966. int err;
  967. if (!dev)
  968. return;
  969. err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
  970. "attached_dev");
  971. if (err)
  972. return;
  973. err = sysfs_create_link_nowarn(&dev->dev.kobj,
  974. &phydev->mdio.dev.kobj,
  975. "phydev");
  976. if (err) {
  977. dev_err(&dev->dev, "could not add device link to %s err %d\n",
  978. kobject_name(&phydev->mdio.dev.kobj),
  979. err);
  980. /* non-fatal - some net drivers can use one netdevice
  981. * with more then one phy
  982. */
  983. }
  984. phydev->sysfs_links = true;
  985. }
  986. static ssize_t
  987. phy_standalone_show(struct device *dev, struct device_attribute *attr,
  988. char *buf)
  989. {
  990. struct phy_device *phydev = to_phy_device(dev);
  991. return sprintf(buf, "%d\n", !phydev->attached_dev);
  992. }
  993. static DEVICE_ATTR_RO(phy_standalone);
  994. /**
  995. * phy_attach_direct - attach a network device to a given PHY device pointer
  996. * @dev: network device to attach
  997. * @phydev: Pointer to phy_device to attach
  998. * @flags: PHY device's dev_flags
  999. * @interface: PHY device's interface
  1000. *
  1001. * Description: Called by drivers to attach to a particular PHY
  1002. * device. The phy_device is found, and properly hooked up
  1003. * to the phy_driver. If no driver is attached, then a
  1004. * generic driver is used. The phy_device is given a ptr to
  1005. * the attaching device, and given a callback for link status
  1006. * change. The phy_device is returned to the attaching driver.
  1007. * This function takes a reference on the phy device.
  1008. */
  1009. int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  1010. u32 flags, phy_interface_t interface)
  1011. {
  1012. struct mii_bus *bus = phydev->mdio.bus;
  1013. struct device *d = &phydev->mdio.dev;
  1014. struct module *ndev_owner = NULL;
  1015. bool using_genphy = false;
  1016. int err;
  1017. /* For Ethernet device drivers that register their own MDIO bus, we
  1018. * will have bus->owner match ndev_mod, so we do not want to increment
  1019. * our own module->refcnt here, otherwise we would not be able to
  1020. * unload later on.
  1021. */
  1022. if (dev)
  1023. ndev_owner = dev->dev.parent->driver->owner;
  1024. if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
  1025. phydev_err(phydev, "failed to get the bus module\n");
  1026. return -EIO;
  1027. }
  1028. get_device(d);
  1029. /* Assume that if there is no driver, that it doesn't
  1030. * exist, and we should use the genphy driver.
  1031. */
  1032. if (!d->driver) {
  1033. if (phydev->is_c45)
  1034. d->driver = &genphy_c45_driver.mdiodrv.driver;
  1035. else
  1036. d->driver = &genphy_driver.mdiodrv.driver;
  1037. using_genphy = true;
  1038. }
  1039. if (!try_module_get(d->driver->owner)) {
  1040. phydev_err(phydev, "failed to get the device driver module\n");
  1041. err = -EIO;
  1042. goto error_put_device;
  1043. }
  1044. if (using_genphy) {
  1045. err = d->driver->probe(d);
  1046. if (err >= 0)
  1047. err = device_bind_driver(d);
  1048. if (err)
  1049. goto error_module_put;
  1050. }
  1051. if (phydev->attached_dev) {
  1052. dev_err(&dev->dev, "PHY already attached\n");
  1053. err = -EBUSY;
  1054. goto error;
  1055. }
  1056. phydev->phy_link_change = phy_link_change;
  1057. if (dev) {
  1058. phydev->attached_dev = dev;
  1059. dev->phydev = phydev;
  1060. }
  1061. /* Some Ethernet drivers try to connect to a PHY device before
  1062. * calling register_netdevice() -> netdev_register_kobject() and
  1063. * does the dev->dev.kobj initialization. Here we only check for
  1064. * success which indicates that the network device kobject is
  1065. * ready. Once we do that we still need to keep track of whether
  1066. * links were successfully set up or not for phy_detach() to
  1067. * remove them accordingly.
  1068. */
  1069. phydev->sysfs_links = false;
  1070. phy_sysfs_create_links(phydev);
  1071. if (!phydev->attached_dev) {
  1072. err = sysfs_create_file(&phydev->mdio.dev.kobj,
  1073. &dev_attr_phy_standalone.attr);
  1074. if (err)
  1075. phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
  1076. }
  1077. phydev->dev_flags = flags;
  1078. phydev->interface = interface;
  1079. phydev->state = PHY_READY;
  1080. /* Initial carrier state is off as the phy is about to be
  1081. * (re)initialized.
  1082. */
  1083. if (dev)
  1084. netif_carrier_off(phydev->attached_dev);
  1085. /* Do initial configuration here, now that
  1086. * we have certain key parameters
  1087. * (dev_flags and interface)
  1088. */
  1089. err = phy_init_hw(phydev);
  1090. if (err)
  1091. goto error;
  1092. phy_resume(phydev);
  1093. phy_led_triggers_register(phydev);
  1094. return err;
  1095. error:
  1096. /* phy_detach() does all of the cleanup below */
  1097. phy_detach(phydev);
  1098. return err;
  1099. error_module_put:
  1100. module_put(d->driver->owner);
  1101. error_put_device:
  1102. put_device(d);
  1103. if (ndev_owner != bus->owner)
  1104. module_put(bus->owner);
  1105. return err;
  1106. }
  1107. EXPORT_SYMBOL(phy_attach_direct);
  1108. /**
  1109. * phy_attach - attach a network device to a particular PHY device
  1110. * @dev: network device to attach
  1111. * @bus_id: Bus ID of PHY device to attach
  1112. * @interface: PHY device's interface
  1113. *
  1114. * Description: Same as phy_attach_direct() except that a PHY bus_id
  1115. * string is passed instead of a pointer to a struct phy_device.
  1116. */
  1117. struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
  1118. phy_interface_t interface)
  1119. {
  1120. struct bus_type *bus = &mdio_bus_type;
  1121. struct phy_device *phydev;
  1122. struct device *d;
  1123. int rc;
  1124. if (!dev)
  1125. return ERR_PTR(-EINVAL);
  1126. /* Search the list of PHY devices on the mdio bus for the
  1127. * PHY with the requested name
  1128. */
  1129. d = bus_find_device_by_name(bus, NULL, bus_id);
  1130. if (!d) {
  1131. pr_err("PHY %s not found\n", bus_id);
  1132. return ERR_PTR(-ENODEV);
  1133. }
  1134. phydev = to_phy_device(d);
  1135. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  1136. put_device(d);
  1137. if (rc)
  1138. return ERR_PTR(rc);
  1139. return phydev;
  1140. }
  1141. EXPORT_SYMBOL(phy_attach);
  1142. static bool phy_driver_is_genphy_kind(struct phy_device *phydev,
  1143. struct device_driver *driver)
  1144. {
  1145. struct device *d = &phydev->mdio.dev;
  1146. bool ret = false;
  1147. if (!phydev->drv)
  1148. return ret;
  1149. get_device(d);
  1150. ret = d->driver == driver;
  1151. put_device(d);
  1152. return ret;
  1153. }
  1154. bool phy_driver_is_genphy(struct phy_device *phydev)
  1155. {
  1156. return phy_driver_is_genphy_kind(phydev,
  1157. &genphy_driver.mdiodrv.driver);
  1158. }
  1159. EXPORT_SYMBOL_GPL(phy_driver_is_genphy);
  1160. bool phy_driver_is_genphy_10g(struct phy_device *phydev)
  1161. {
  1162. return phy_driver_is_genphy_kind(phydev,
  1163. &genphy_c45_driver.mdiodrv.driver);
  1164. }
  1165. EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
  1166. /**
  1167. * phy_detach - detach a PHY device from its network device
  1168. * @phydev: target phy_device struct
  1169. *
  1170. * This detaches the phy device from its network device and the phy
  1171. * driver, and drops the reference count taken in phy_attach_direct().
  1172. */
  1173. void phy_detach(struct phy_device *phydev)
  1174. {
  1175. struct net_device *dev = phydev->attached_dev;
  1176. struct module *ndev_owner = NULL;
  1177. struct mii_bus *bus;
  1178. if (phydev->sysfs_links) {
  1179. if (dev)
  1180. sysfs_remove_link(&dev->dev.kobj, "phydev");
  1181. sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
  1182. }
  1183. if (!phydev->attached_dev)
  1184. sysfs_remove_file(&phydev->mdio.dev.kobj,
  1185. &dev_attr_phy_standalone.attr);
  1186. phy_suspend(phydev);
  1187. if (dev) {
  1188. phydev->attached_dev->phydev = NULL;
  1189. phydev->attached_dev = NULL;
  1190. }
  1191. phydev->phylink = NULL;
  1192. phy_led_triggers_unregister(phydev);
  1193. if (phydev->mdio.dev.driver)
  1194. module_put(phydev->mdio.dev.driver->owner);
  1195. /* If the device had no specific driver before (i.e. - it
  1196. * was using the generic driver), we unbind the device
  1197. * from the generic driver so that there's a chance a
  1198. * real driver could be loaded
  1199. */
  1200. if (phy_driver_is_genphy(phydev) ||
  1201. phy_driver_is_genphy_10g(phydev))
  1202. device_release_driver(&phydev->mdio.dev);
  1203. /*
  1204. * The phydev might go away on the put_device() below, so avoid
  1205. * a use-after-free bug by reading the underlying bus first.
  1206. */
  1207. bus = phydev->mdio.bus;
  1208. put_device(&phydev->mdio.dev);
  1209. if (dev)
  1210. ndev_owner = dev->dev.parent->driver->owner;
  1211. if (ndev_owner != bus->owner)
  1212. module_put(bus->owner);
  1213. /* Assert the reset signal */
  1214. phy_device_reset(phydev, 1);
  1215. }
  1216. EXPORT_SYMBOL(phy_detach);
  1217. int phy_suspend(struct phy_device *phydev)
  1218. {
  1219. struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
  1220. struct net_device *netdev = phydev->attached_dev;
  1221. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  1222. int ret = 0;
  1223. /* If the device has WOL enabled, we cannot suspend the PHY */
  1224. phy_ethtool_get_wol(phydev, &wol);
  1225. if (wol.wolopts || (netdev && netdev->wol_enabled))
  1226. return -EBUSY;
  1227. if (phydev->drv && phydrv->suspend)
  1228. ret = phydrv->suspend(phydev);
  1229. if (ret)
  1230. return ret;
  1231. phydev->suspended = true;
  1232. return ret;
  1233. }
  1234. EXPORT_SYMBOL(phy_suspend);
  1235. int __phy_resume(struct phy_device *phydev)
  1236. {
  1237. struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
  1238. int ret = 0;
  1239. WARN_ON(!mutex_is_locked(&phydev->lock));
  1240. if (phydev->drv && phydrv->resume)
  1241. ret = phydrv->resume(phydev);
  1242. if (ret)
  1243. return ret;
  1244. phydev->suspended = false;
  1245. return ret;
  1246. }
  1247. EXPORT_SYMBOL(__phy_resume);
  1248. int phy_resume(struct phy_device *phydev)
  1249. {
  1250. int ret;
  1251. mutex_lock(&phydev->lock);
  1252. ret = __phy_resume(phydev);
  1253. mutex_unlock(&phydev->lock);
  1254. return ret;
  1255. }
  1256. EXPORT_SYMBOL(phy_resume);
  1257. int phy_loopback(struct phy_device *phydev, bool enable)
  1258. {
  1259. struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
  1260. int ret = 0;
  1261. mutex_lock(&phydev->lock);
  1262. if (enable && phydev->loopback_enabled) {
  1263. ret = -EBUSY;
  1264. goto out;
  1265. }
  1266. if (!enable && !phydev->loopback_enabled) {
  1267. ret = -EINVAL;
  1268. goto out;
  1269. }
  1270. if (phydev->drv && phydrv->set_loopback)
  1271. ret = phydrv->set_loopback(phydev, enable);
  1272. else
  1273. ret = -EOPNOTSUPP;
  1274. if (ret)
  1275. goto out;
  1276. phydev->loopback_enabled = enable;
  1277. out:
  1278. mutex_unlock(&phydev->lock);
  1279. return ret;
  1280. }
  1281. EXPORT_SYMBOL(phy_loopback);
  1282. /**
  1283. * phy_reset_after_clk_enable - perform a PHY reset if needed
  1284. * @phydev: target phy_device struct
  1285. *
  1286. * Description: Some PHYs are known to need a reset after their refclk was
  1287. * enabled. This function evaluates the flags and perform the reset if it's
  1288. * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
  1289. * was reset.
  1290. */
  1291. int phy_reset_after_clk_enable(struct phy_device *phydev)
  1292. {
  1293. if (!phydev || !phydev->drv)
  1294. return -ENODEV;
  1295. if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
  1296. phy_device_reset(phydev, 1);
  1297. phy_device_reset(phydev, 0);
  1298. return 1;
  1299. }
  1300. return 0;
  1301. }
  1302. EXPORT_SYMBOL(phy_reset_after_clk_enable);
  1303. /* Generic PHY support and helper functions */
  1304. /**
  1305. * genphy_config_advert - sanitize and advertise auto-negotiation parameters
  1306. * @phydev: target phy_device struct
  1307. *
  1308. * Description: Writes MII_ADVERTISE with the appropriate values,
  1309. * after sanitizing the values to make sure we only advertise
  1310. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  1311. * hasn't changed, and > 0 if it has changed.
  1312. */
  1313. static int genphy_config_advert(struct phy_device *phydev)
  1314. {
  1315. int err, bmsr, changed = 0;
  1316. u32 adv;
  1317. /* Only allow advertising what this PHY supports */
  1318. linkmode_and(phydev->advertising, phydev->advertising,
  1319. phydev->supported);
  1320. adv = linkmode_adv_to_mii_adv_t(phydev->advertising);
  1321. /* Setup standard advertisement */
  1322. err = phy_modify_changed(phydev, MII_ADVERTISE,
  1323. ADVERTISE_ALL | ADVERTISE_100BASE4 |
  1324. ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
  1325. adv);
  1326. if (err < 0)
  1327. return err;
  1328. if (err > 0)
  1329. changed = 1;
  1330. bmsr = phy_read(phydev, MII_BMSR);
  1331. if (bmsr < 0)
  1332. return bmsr;
  1333. /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
  1334. * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
  1335. * logical 1.
  1336. */
  1337. if (!(bmsr & BMSR_ESTATEN))
  1338. return changed;
  1339. adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
  1340. err = phy_modify_changed(phydev, MII_CTRL1000,
  1341. ADVERTISE_1000FULL | ADVERTISE_1000HALF,
  1342. adv);
  1343. if (err < 0)
  1344. return err;
  1345. if (err > 0)
  1346. changed = 1;
  1347. return changed;
  1348. }
  1349. /**
  1350. * genphy_config_eee_advert - disable unwanted eee mode advertisement
  1351. * @phydev: target phy_device struct
  1352. *
  1353. * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
  1354. * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
  1355. * changed, and 1 if it has changed.
  1356. */
  1357. int genphy_config_eee_advert(struct phy_device *phydev)
  1358. {
  1359. int err;
  1360. /* Nothing to disable */
  1361. if (!phydev->eee_broken_modes)
  1362. return 0;
  1363. err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
  1364. phydev->eee_broken_modes, 0);
  1365. /* If the call failed, we assume that EEE is not supported */
  1366. return err < 0 ? 0 : err;
  1367. }
  1368. EXPORT_SYMBOL(genphy_config_eee_advert);
  1369. /**
  1370. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  1371. * @phydev: target phy_device struct
  1372. *
  1373. * Description: Configures MII_BMCR to force speed/duplex
  1374. * to the values in phydev. Assumes that the values are valid.
  1375. * Please see phy_sanitize_settings().
  1376. */
  1377. int genphy_setup_forced(struct phy_device *phydev)
  1378. {
  1379. u16 ctl = 0;
  1380. phydev->pause = 0;
  1381. phydev->asym_pause = 0;
  1382. if (SPEED_1000 == phydev->speed)
  1383. ctl |= BMCR_SPEED1000;
  1384. else if (SPEED_100 == phydev->speed)
  1385. ctl |= BMCR_SPEED100;
  1386. if (DUPLEX_FULL == phydev->duplex)
  1387. ctl |= BMCR_FULLDPLX;
  1388. return phy_modify(phydev, MII_BMCR,
  1389. ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
  1390. }
  1391. EXPORT_SYMBOL(genphy_setup_forced);
  1392. /**
  1393. * genphy_restart_aneg - Enable and Restart Autonegotiation
  1394. * @phydev: target phy_device struct
  1395. */
  1396. int genphy_restart_aneg(struct phy_device *phydev)
  1397. {
  1398. /* Don't isolate the PHY if we're negotiating */
  1399. return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
  1400. BMCR_ANENABLE | BMCR_ANRESTART);
  1401. }
  1402. EXPORT_SYMBOL(genphy_restart_aneg);
  1403. /**
  1404. * __genphy_config_aneg - restart auto-negotiation or write BMCR
  1405. * @phydev: target phy_device struct
  1406. * @changed: whether autoneg is requested
  1407. *
  1408. * Description: If auto-negotiation is enabled, we configure the
  1409. * advertising, and then restart auto-negotiation. If it is not
  1410. * enabled, then we write the BMCR.
  1411. */
  1412. int __genphy_config_aneg(struct phy_device *phydev, bool changed)
  1413. {
  1414. int err;
  1415. if (genphy_config_eee_advert(phydev))
  1416. changed = true;
  1417. if (AUTONEG_ENABLE != phydev->autoneg)
  1418. return genphy_setup_forced(phydev);
  1419. err = genphy_config_advert(phydev);
  1420. if (err < 0) /* error */
  1421. return err;
  1422. else if (err)
  1423. changed = true;
  1424. if (!changed) {
  1425. /* Advertisement hasn't changed, but maybe aneg was never on to
  1426. * begin with? Or maybe phy was isolated?
  1427. */
  1428. int ctl = phy_read(phydev, MII_BMCR);
  1429. if (ctl < 0)
  1430. return ctl;
  1431. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  1432. changed = true; /* do restart aneg */
  1433. }
  1434. /* Only restart aneg if we are advertising something different
  1435. * than we were before.
  1436. */
  1437. return changed ? genphy_restart_aneg(phydev) : 0;
  1438. }
  1439. EXPORT_SYMBOL(__genphy_config_aneg);
  1440. /**
  1441. * genphy_aneg_done - return auto-negotiation status
  1442. * @phydev: target phy_device struct
  1443. *
  1444. * Description: Reads the status register and returns 0 either if
  1445. * auto-negotiation is incomplete, or if there was an error.
  1446. * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
  1447. */
  1448. int genphy_aneg_done(struct phy_device *phydev)
  1449. {
  1450. int retval = phy_read(phydev, MII_BMSR);
  1451. return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
  1452. }
  1453. EXPORT_SYMBOL(genphy_aneg_done);
  1454. /**
  1455. * genphy_update_link - update link status in @phydev
  1456. * @phydev: target phy_device struct
  1457. *
  1458. * Description: Update the value in phydev->link to reflect the
  1459. * current link value. In order to do this, we need to read
  1460. * the status register twice, keeping the second value.
  1461. */
  1462. int genphy_update_link(struct phy_device *phydev)
  1463. {
  1464. int status = 0, bmcr;
  1465. bmcr = phy_read(phydev, MII_BMCR);
  1466. if (bmcr < 0)
  1467. return bmcr;
  1468. /* Autoneg is being started, therefore disregard BMSR value and
  1469. * report link as down.
  1470. */
  1471. if (bmcr & BMCR_ANRESTART)
  1472. goto done;
  1473. /* The link state is latched low so that momentary link
  1474. * drops can be detected. Do not double-read the status
  1475. * in polling mode to detect such short link drops except
  1476. * the link was already down.
  1477. */
  1478. if (!phy_polling_mode(phydev) || !phydev->link) {
  1479. status = phy_read(phydev, MII_BMSR);
  1480. if (status < 0)
  1481. return status;
  1482. else if (status & BMSR_LSTATUS)
  1483. goto done;
  1484. }
  1485. /* Read link and autonegotiation status */
  1486. status = phy_read(phydev, MII_BMSR);
  1487. if (status < 0)
  1488. return status;
  1489. done:
  1490. phydev->link = status & BMSR_LSTATUS ? 1 : 0;
  1491. phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
  1492. /* Consider the case that autoneg was started and "aneg complete"
  1493. * bit has been reset, but "link up" bit not yet.
  1494. */
  1495. if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
  1496. phydev->link = 0;
  1497. return 0;
  1498. }
  1499. EXPORT_SYMBOL(genphy_update_link);
  1500. int genphy_read_lpa(struct phy_device *phydev)
  1501. {
  1502. int lpa, lpagb;
  1503. if (phydev->autoneg == AUTONEG_ENABLE) {
  1504. if (!phydev->autoneg_complete) {
  1505. mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
  1506. 0);
  1507. mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
  1508. return 0;
  1509. }
  1510. if (phydev->is_gigabit_capable) {
  1511. lpagb = phy_read(phydev, MII_STAT1000);
  1512. if (lpagb < 0)
  1513. return lpagb;
  1514. if (lpagb & LPA_1000MSFAIL) {
  1515. int adv = phy_read(phydev, MII_CTRL1000);
  1516. if (adv < 0)
  1517. return adv;
  1518. if (adv & CTL1000_ENABLE_MASTER)
  1519. phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
  1520. else
  1521. phydev_err(phydev, "Master/Slave resolution failed\n");
  1522. return -ENOLINK;
  1523. }
  1524. mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
  1525. lpagb);
  1526. }
  1527. lpa = phy_read(phydev, MII_LPA);
  1528. if (lpa < 0)
  1529. return lpa;
  1530. mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
  1531. } else {
  1532. linkmode_zero(phydev->lp_advertising);
  1533. }
  1534. return 0;
  1535. }
  1536. EXPORT_SYMBOL(genphy_read_lpa);
  1537. /**
  1538. * genphy_read_status - check the link status and update current link state
  1539. * @phydev: target phy_device struct
  1540. *
  1541. * Description: Check the link, then figure out the current state
  1542. * by comparing what we advertise with what the link partner
  1543. * advertises. Start by checking the gigabit possibilities,
  1544. * then move on to 10/100.
  1545. */
  1546. int genphy_read_status(struct phy_device *phydev)
  1547. {
  1548. int err, old_link = phydev->link;
  1549. /* Update the link, but return if there was an error */
  1550. err = genphy_update_link(phydev);
  1551. if (err)
  1552. return err;
  1553. /* why bother the PHY if nothing can have changed */
  1554. if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
  1555. return 0;
  1556. phydev->speed = SPEED_UNKNOWN;
  1557. phydev->duplex = DUPLEX_UNKNOWN;
  1558. phydev->pause = 0;
  1559. phydev->asym_pause = 0;
  1560. err = genphy_read_lpa(phydev);
  1561. if (err < 0)
  1562. return err;
  1563. if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
  1564. phy_resolve_aneg_linkmode(phydev);
  1565. } else if (phydev->autoneg == AUTONEG_DISABLE) {
  1566. int bmcr = phy_read(phydev, MII_BMCR);
  1567. if (bmcr < 0)
  1568. return bmcr;
  1569. if (bmcr & BMCR_FULLDPLX)
  1570. phydev->duplex = DUPLEX_FULL;
  1571. else
  1572. phydev->duplex = DUPLEX_HALF;
  1573. if (bmcr & BMCR_SPEED1000)
  1574. phydev->speed = SPEED_1000;
  1575. else if (bmcr & BMCR_SPEED100)
  1576. phydev->speed = SPEED_100;
  1577. else
  1578. phydev->speed = SPEED_10;
  1579. }
  1580. return 0;
  1581. }
  1582. EXPORT_SYMBOL(genphy_read_status);
  1583. /**
  1584. * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
  1585. * @phydev: target phy_device struct
  1586. *
  1587. * Description: Perform a software PHY reset using the standard
  1588. * BMCR_RESET bit and poll for the reset bit to be cleared.
  1589. *
  1590. * Returns: 0 on success, < 0 on failure
  1591. */
  1592. int genphy_soft_reset(struct phy_device *phydev)
  1593. {
  1594. u16 res = BMCR_RESET;
  1595. int ret;
  1596. if (phydev->autoneg == AUTONEG_ENABLE)
  1597. res |= BMCR_ANRESTART;
  1598. ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
  1599. if (ret < 0)
  1600. return ret;
  1601. ret = phy_poll_reset(phydev);
  1602. if (ret)
  1603. return ret;
  1604. /* BMCR may be reset to defaults */
  1605. if (phydev->autoneg == AUTONEG_DISABLE)
  1606. ret = genphy_setup_forced(phydev);
  1607. return ret;
  1608. }
  1609. EXPORT_SYMBOL(genphy_soft_reset);
  1610. /**
  1611. * genphy_read_abilities - read PHY abilities from Clause 22 registers
  1612. * @phydev: target phy_device struct
  1613. *
  1614. * Description: Reads the PHY's abilities and populates
  1615. * phydev->supported accordingly.
  1616. *
  1617. * Returns: 0 on success, < 0 on failure
  1618. */
  1619. int genphy_read_abilities(struct phy_device *phydev)
  1620. {
  1621. int val;
  1622. linkmode_set_bit_array(phy_basic_ports_array,
  1623. ARRAY_SIZE(phy_basic_ports_array),
  1624. phydev->supported);
  1625. val = phy_read(phydev, MII_BMSR);
  1626. if (val < 0)
  1627. return val;
  1628. linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
  1629. val & BMSR_ANEGCAPABLE);
  1630. linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
  1631. val & BMSR_100FULL);
  1632. linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
  1633. val & BMSR_100HALF);
  1634. linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
  1635. val & BMSR_10FULL);
  1636. linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
  1637. val & BMSR_10HALF);
  1638. if (val & BMSR_ESTATEN) {
  1639. val = phy_read(phydev, MII_ESTATUS);
  1640. if (val < 0)
  1641. return val;
  1642. linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  1643. phydev->supported, val & ESTATUS_1000_TFULL);
  1644. linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
  1645. phydev->supported, val & ESTATUS_1000_THALF);
  1646. linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
  1647. phydev->supported, val & ESTATUS_1000_XFULL);
  1648. }
  1649. return 0;
  1650. }
  1651. EXPORT_SYMBOL(genphy_read_abilities);
  1652. /* This is used for the phy device which doesn't support the MMD extended
  1653. * register access, but it does have side effect when we are trying to access
  1654. * the MMD register via indirect method.
  1655. */
  1656. int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
  1657. {
  1658. return -EOPNOTSUPP;
  1659. }
  1660. EXPORT_SYMBOL(genphy_read_mmd_unsupported);
  1661. int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
  1662. u16 regnum, u16 val)
  1663. {
  1664. return -EOPNOTSUPP;
  1665. }
  1666. EXPORT_SYMBOL(genphy_write_mmd_unsupported);
  1667. int genphy_suspend(struct phy_device *phydev)
  1668. {
  1669. return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
  1670. }
  1671. EXPORT_SYMBOL(genphy_suspend);
  1672. int genphy_resume(struct phy_device *phydev)
  1673. {
  1674. return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
  1675. }
  1676. EXPORT_SYMBOL(genphy_resume);
  1677. int genphy_loopback(struct phy_device *phydev, bool enable)
  1678. {
  1679. return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
  1680. enable ? BMCR_LOOPBACK : 0);
  1681. }
  1682. EXPORT_SYMBOL(genphy_loopback);
  1683. /**
  1684. * phy_remove_link_mode - Remove a supported link mode
  1685. * @phydev: phy_device structure to remove link mode from
  1686. * @link_mode: Link mode to be removed
  1687. *
  1688. * Description: Some MACs don't support all link modes which the PHY
  1689. * does. e.g. a 1G MAC often does not support 1000Half. Add a helper
  1690. * to remove a link mode.
  1691. */
  1692. void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
  1693. {
  1694. linkmode_clear_bit(link_mode, phydev->supported);
  1695. phy_advertise_supported(phydev);
  1696. }
  1697. EXPORT_SYMBOL(phy_remove_link_mode);
  1698. static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
  1699. {
  1700. linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
  1701. linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
  1702. linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
  1703. linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
  1704. }
  1705. /**
  1706. * phy_advertise_supported - Advertise all supported modes
  1707. * @phydev: target phy_device struct
  1708. *
  1709. * Description: Called to advertise all supported modes, doesn't touch
  1710. * pause mode advertising.
  1711. */
  1712. void phy_advertise_supported(struct phy_device *phydev)
  1713. {
  1714. __ETHTOOL_DECLARE_LINK_MODE_MASK(new);
  1715. linkmode_copy(new, phydev->supported);
  1716. phy_copy_pause_bits(new, phydev->advertising);
  1717. linkmode_copy(phydev->advertising, new);
  1718. }
  1719. EXPORT_SYMBOL(phy_advertise_supported);
  1720. /**
  1721. * phy_support_sym_pause - Enable support of symmetrical pause
  1722. * @phydev: target phy_device struct
  1723. *
  1724. * Description: Called by the MAC to indicate is supports symmetrical
  1725. * Pause, but not asym pause.
  1726. */
  1727. void phy_support_sym_pause(struct phy_device *phydev)
  1728. {
  1729. linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
  1730. phy_copy_pause_bits(phydev->advertising, phydev->supported);
  1731. }
  1732. EXPORT_SYMBOL(phy_support_sym_pause);
  1733. /**
  1734. * phy_support_asym_pause - Enable support of asym pause
  1735. * @phydev: target phy_device struct
  1736. *
  1737. * Description: Called by the MAC to indicate is supports Asym Pause.
  1738. */
  1739. void phy_support_asym_pause(struct phy_device *phydev)
  1740. {
  1741. phy_copy_pause_bits(phydev->advertising, phydev->supported);
  1742. }
  1743. EXPORT_SYMBOL(phy_support_asym_pause);
  1744. /**
  1745. * phy_set_sym_pause - Configure symmetric Pause
  1746. * @phydev: target phy_device struct
  1747. * @rx: Receiver Pause is supported
  1748. * @tx: Transmit Pause is supported
  1749. * @autoneg: Auto neg should be used
  1750. *
  1751. * Description: Configure advertised Pause support depending on if
  1752. * receiver pause and pause auto neg is supported. Generally called
  1753. * from the set_pauseparam .ndo.
  1754. */
  1755. void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
  1756. bool autoneg)
  1757. {
  1758. linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
  1759. if (rx && tx && autoneg)
  1760. linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  1761. phydev->supported);
  1762. linkmode_copy(phydev->advertising, phydev->supported);
  1763. }
  1764. EXPORT_SYMBOL(phy_set_sym_pause);
  1765. /**
  1766. * phy_set_asym_pause - Configure Pause and Asym Pause
  1767. * @phydev: target phy_device struct
  1768. * @rx: Receiver Pause is supported
  1769. * @tx: Transmit Pause is supported
  1770. *
  1771. * Description: Configure advertised Pause support depending on if
  1772. * transmit and receiver pause is supported. If there has been a
  1773. * change in adverting, trigger a new autoneg. Generally called from
  1774. * the set_pauseparam .ndo.
  1775. */
  1776. void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
  1777. {
  1778. __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
  1779. linkmode_copy(oldadv, phydev->advertising);
  1780. linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  1781. phydev->advertising);
  1782. linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  1783. phydev->advertising);
  1784. if (rx) {
  1785. linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  1786. phydev->advertising);
  1787. linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  1788. phydev->advertising);
  1789. }
  1790. if (tx)
  1791. linkmode_change_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  1792. phydev->advertising);
  1793. if (!linkmode_equal(oldadv, phydev->advertising) &&
  1794. phydev->autoneg)
  1795. phy_start_aneg(phydev);
  1796. }
  1797. EXPORT_SYMBOL(phy_set_asym_pause);
  1798. /**
  1799. * phy_validate_pause - Test if the PHY/MAC support the pause configuration
  1800. * @phydev: phy_device struct
  1801. * @pp: requested pause configuration
  1802. *
  1803. * Description: Test if the PHY/MAC combination supports the Pause
  1804. * configuration the user is requesting. Returns True if it is
  1805. * supported, false otherwise.
  1806. */
  1807. bool phy_validate_pause(struct phy_device *phydev,
  1808. struct ethtool_pauseparam *pp)
  1809. {
  1810. if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  1811. phydev->supported) && pp->rx_pause)
  1812. return false;
  1813. if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  1814. phydev->supported) &&
  1815. pp->rx_pause != pp->tx_pause)
  1816. return false;
  1817. return true;
  1818. }
  1819. EXPORT_SYMBOL(phy_validate_pause);
  1820. static bool phy_drv_supports_irq(struct phy_driver *phydrv)
  1821. {
  1822. return phydrv->config_intr && phydrv->ack_interrupt;
  1823. }
  1824. /**
  1825. * phy_probe - probe and init a PHY device
  1826. * @dev: device to probe and init
  1827. *
  1828. * Description: Take care of setting up the phy_device structure,
  1829. * set the state to READY (the driver's init function should
  1830. * set it to STARTING if needed).
  1831. */
  1832. static int phy_probe(struct device *dev)
  1833. {
  1834. struct phy_device *phydev = to_phy_device(dev);
  1835. struct device_driver *drv = phydev->mdio.dev.driver;
  1836. struct phy_driver *phydrv = to_phy_driver(drv);
  1837. int err = 0;
  1838. phydev->drv = phydrv;
  1839. /* Disable the interrupt if the PHY doesn't support it
  1840. * but the interrupt is still a valid one
  1841. */
  1842. if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
  1843. phydev->irq = PHY_POLL;
  1844. if (phydrv->flags & PHY_IS_INTERNAL)
  1845. phydev->is_internal = true;
  1846. mutex_lock(&phydev->lock);
  1847. if (phydev->drv->probe) {
  1848. /* Deassert the reset signal */
  1849. phy_device_reset(phydev, 0);
  1850. err = phydev->drv->probe(phydev);
  1851. if (err) {
  1852. /* Assert the reset signal */
  1853. phy_device_reset(phydev, 1);
  1854. goto out;
  1855. }
  1856. }
  1857. /* Start out supporting everything. Eventually,
  1858. * a controller will attach, and may modify one
  1859. * or both of these values
  1860. */
  1861. if (phydrv->features) {
  1862. linkmode_copy(phydev->supported, phydrv->features);
  1863. } else if (phydrv->get_features) {
  1864. err = phydrv->get_features(phydev);
  1865. } else if (phydev->is_c45) {
  1866. err = genphy_c45_pma_read_abilities(phydev);
  1867. } else {
  1868. err = genphy_read_abilities(phydev);
  1869. }
  1870. if (err)
  1871. goto out;
  1872. if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  1873. phydev->supported))
  1874. phydev->autoneg = 0;
  1875. if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
  1876. phydev->supported))
  1877. phydev->is_gigabit_capable = 1;
  1878. if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  1879. phydev->supported))
  1880. phydev->is_gigabit_capable = 1;
  1881. of_set_phy_supported(phydev);
  1882. phy_advertise_supported(phydev);
  1883. /* Get the EEE modes we want to prohibit. We will ask
  1884. * the PHY stop advertising these mode later on
  1885. */
  1886. of_set_phy_eee_broken(phydev);
  1887. /* The Pause Frame bits indicate that the PHY can support passing
  1888. * pause frames. During autonegotiation, the PHYs will determine if
  1889. * they should allow pause frames to pass. The MAC driver should then
  1890. * use that result to determine whether to enable flow control via
  1891. * pause frames.
  1892. *
  1893. * Normally, PHY drivers should not set the Pause bits, and instead
  1894. * allow phylib to do that. However, there may be some situations
  1895. * (e.g. hardware erratum) where the driver wants to set only one
  1896. * of these bits.
  1897. */
  1898. if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
  1899. !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
  1900. linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  1901. phydev->supported);
  1902. linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  1903. phydev->supported);
  1904. }
  1905. /* Set the state to READY by default */
  1906. phydev->state = PHY_READY;
  1907. out:
  1908. mutex_unlock(&phydev->lock);
  1909. return err;
  1910. }
  1911. static int phy_remove(struct device *dev)
  1912. {
  1913. struct phy_device *phydev = to_phy_device(dev);
  1914. cancel_delayed_work_sync(&phydev->state_queue);
  1915. mutex_lock(&phydev->lock);
  1916. phydev->state = PHY_DOWN;
  1917. mutex_unlock(&phydev->lock);
  1918. if (phydev->drv && phydev->drv->remove) {
  1919. phydev->drv->remove(phydev);
  1920. /* Assert the reset signal */
  1921. phy_device_reset(phydev, 1);
  1922. }
  1923. phydev->drv = NULL;
  1924. return 0;
  1925. }
  1926. /**
  1927. * phy_driver_register - register a phy_driver with the PHY layer
  1928. * @new_driver: new phy_driver to register
  1929. * @owner: module owning this PHY
  1930. */
  1931. int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
  1932. {
  1933. int retval;
  1934. /* Either the features are hard coded, or dynamically
  1935. * determined. It cannot be both.
  1936. */
  1937. if (WARN_ON(new_driver->features && new_driver->get_features)) {
  1938. pr_err("%s: features and get_features must not both be set\n",
  1939. new_driver->name);
  1940. return -EINVAL;
  1941. }
  1942. new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
  1943. new_driver->mdiodrv.driver.name = new_driver->name;
  1944. new_driver->mdiodrv.driver.bus = &mdio_bus_type;
  1945. new_driver->mdiodrv.driver.probe = phy_probe;
  1946. new_driver->mdiodrv.driver.remove = phy_remove;
  1947. new_driver->mdiodrv.driver.owner = owner;
  1948. retval = driver_register(&new_driver->mdiodrv.driver);
  1949. if (retval) {
  1950. pr_err("%s: Error %d in registering driver\n",
  1951. new_driver->name, retval);
  1952. return retval;
  1953. }
  1954. pr_debug("%s: Registered new driver\n", new_driver->name);
  1955. return 0;
  1956. }
  1957. EXPORT_SYMBOL(phy_driver_register);
  1958. int phy_drivers_register(struct phy_driver *new_driver, int n,
  1959. struct module *owner)
  1960. {
  1961. int i, ret = 0;
  1962. for (i = 0; i < n; i++) {
  1963. ret = phy_driver_register(new_driver + i, owner);
  1964. if (ret) {
  1965. while (i-- > 0)
  1966. phy_driver_unregister(new_driver + i);
  1967. break;
  1968. }
  1969. }
  1970. return ret;
  1971. }
  1972. EXPORT_SYMBOL(phy_drivers_register);
  1973. void phy_driver_unregister(struct phy_driver *drv)
  1974. {
  1975. driver_unregister(&drv->mdiodrv.driver);
  1976. }
  1977. EXPORT_SYMBOL(phy_driver_unregister);
  1978. void phy_drivers_unregister(struct phy_driver *drv, int n)
  1979. {
  1980. int i;
  1981. for (i = 0; i < n; i++)
  1982. phy_driver_unregister(drv + i);
  1983. }
  1984. EXPORT_SYMBOL(phy_drivers_unregister);
  1985. static struct phy_driver genphy_driver = {
  1986. .phy_id = 0xffffffff,
  1987. .phy_id_mask = 0xffffffff,
  1988. .name = "Generic PHY",
  1989. .soft_reset = genphy_no_soft_reset,
  1990. .get_features = genphy_read_abilities,
  1991. .aneg_done = genphy_aneg_done,
  1992. .suspend = genphy_suspend,
  1993. .resume = genphy_resume,
  1994. .set_loopback = genphy_loopback,
  1995. };
  1996. static int __init phy_init(void)
  1997. {
  1998. int rc;
  1999. rc = mdio_bus_init();
  2000. if (rc)
  2001. return rc;
  2002. features_init();
  2003. rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
  2004. if (rc)
  2005. goto err_c45;
  2006. rc = phy_driver_register(&genphy_driver, THIS_MODULE);
  2007. if (rc) {
  2008. phy_driver_unregister(&genphy_c45_driver);
  2009. err_c45:
  2010. mdio_bus_exit();
  2011. }
  2012. return rc;
  2013. }
  2014. static void __exit phy_exit(void)
  2015. {
  2016. phy_driver_unregister(&genphy_c45_driver);
  2017. phy_driver_unregister(&genphy_driver);
  2018. mdio_bus_exit();
  2019. }
  2020. subsys_initcall(phy_init);
  2021. module_exit(phy_exit);