phy.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Framework for configuring and reading PHY devices
  3. * Based on code in sungem_phy.c and gianfar_phy.c
  4. *
  5. * Author: Andy Fleming
  6. *
  7. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  8. * Copyright (c) 2006, 2007 Maciej W. Rozycki
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/errno.h>
  13. #include <linux/unistd.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/delay.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/netlink.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/mii.h>
  23. #include <linux/ethtool.h>
  24. #include <linux/ethtool_netlink.h>
  25. #include <linux/phy.h>
  26. #include <linux/phy_led_triggers.h>
  27. #include <linux/sfp.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/mdio.h>
  30. #include <linux/io.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/atomic.h>
  33. #include <linux/suspend.h>
  34. #include <net/netlink.h>
  35. #include <net/genetlink.h>
  36. #include <net/sock.h>
  37. #include "phylib-internal.h"
  38. #include "phy-caps.h"
  39. #define PHY_STATE_TIME HZ
  40. #define PHY_STATE_STR(_state) \
  41. case PHY_##_state: \
  42. return __stringify(_state); \
  43. static const char *phy_state_to_str(enum phy_state st)
  44. {
  45. switch (st) {
  46. PHY_STATE_STR(DOWN)
  47. PHY_STATE_STR(READY)
  48. PHY_STATE_STR(UP)
  49. PHY_STATE_STR(RUNNING)
  50. PHY_STATE_STR(NOLINK)
  51. PHY_STATE_STR(CABLETEST)
  52. PHY_STATE_STR(HALTED)
  53. PHY_STATE_STR(ERROR)
  54. }
  55. return NULL;
  56. }
  57. static void phy_process_state_change(struct phy_device *phydev,
  58. enum phy_state old_state)
  59. {
  60. if (old_state != phydev->state) {
  61. phydev_dbg(phydev, "PHY state change %s -> %s\n",
  62. phy_state_to_str(old_state),
  63. phy_state_to_str(phydev->state));
  64. if (phydev->drv && phydev->drv->link_change_notify)
  65. phydev->drv->link_change_notify(phydev);
  66. }
  67. }
  68. static void phy_link_up(struct phy_device *phydev)
  69. {
  70. phydev->phy_link_change(phydev, true);
  71. phy_led_trigger_change_speed(phydev);
  72. }
  73. static void phy_link_down(struct phy_device *phydev)
  74. {
  75. phydev->phy_link_change(phydev, false);
  76. phy_led_trigger_change_speed(phydev);
  77. WRITE_ONCE(phydev->link_down_events, phydev->link_down_events + 1);
  78. }
  79. static const char *phy_pause_str(struct phy_device *phydev)
  80. {
  81. bool local_pause, local_asym_pause;
  82. if (phydev->autoneg == AUTONEG_DISABLE)
  83. goto no_pause;
  84. local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  85. phydev->advertising);
  86. local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  87. phydev->advertising);
  88. if (local_pause && phydev->pause)
  89. return "rx/tx";
  90. if (local_asym_pause && phydev->asym_pause) {
  91. if (local_pause)
  92. return "rx";
  93. if (phydev->pause)
  94. return "tx";
  95. }
  96. no_pause:
  97. return "off";
  98. }
  99. /**
  100. * phy_print_status - Convenience function to print out the current phy status
  101. * @phydev: the phy_device struct
  102. */
  103. void phy_print_status(struct phy_device *phydev)
  104. {
  105. if (phydev->link) {
  106. netdev_info(phydev->attached_dev,
  107. "Link is Up - %s/%s %s- flow control %s\n",
  108. phy_speed_to_str(phydev->speed),
  109. phy_duplex_to_str(phydev->duplex),
  110. phydev->downshifted_rate ? "(downshifted) " : "",
  111. phy_pause_str(phydev));
  112. } else {
  113. netdev_info(phydev->attached_dev, "Link is Down\n");
  114. }
  115. }
  116. EXPORT_SYMBOL(phy_print_status);
  117. /**
  118. * phy_get_rate_matching - determine if rate matching is supported
  119. * @phydev: The phy device to return rate matching for
  120. * @iface: The interface mode to use
  121. *
  122. * This determines the type of rate matching (if any) that @phy supports
  123. * using @iface. @iface may be %PHY_INTERFACE_MODE_NA to determine if any
  124. * interface supports rate matching.
  125. *
  126. * Return: The type of rate matching @phy supports for @iface, or
  127. * %RATE_MATCH_NONE.
  128. */
  129. int phy_get_rate_matching(struct phy_device *phydev,
  130. phy_interface_t iface)
  131. {
  132. int ret = RATE_MATCH_NONE;
  133. if (phydev->drv->get_rate_matching) {
  134. mutex_lock(&phydev->lock);
  135. ret = phydev->drv->get_rate_matching(phydev, iface);
  136. mutex_unlock(&phydev->lock);
  137. }
  138. return ret;
  139. }
  140. EXPORT_SYMBOL_GPL(phy_get_rate_matching);
  141. /**
  142. * phy_config_interrupt - configure the PHY device for the requested interrupts
  143. * @phydev: the phy_device struct
  144. * @interrupts: interrupt flags to configure for this @phydev
  145. *
  146. * Returns 0 on success or < 0 on error.
  147. */
  148. static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
  149. {
  150. phydev->interrupts = interrupts ? 1 : 0;
  151. if (phydev->drv->config_intr)
  152. return phydev->drv->config_intr(phydev);
  153. return 0;
  154. }
  155. /**
  156. * phy_restart_aneg - restart auto-negotiation
  157. * @phydev: target phy_device struct
  158. *
  159. * Restart the autonegotiation on @phydev. Returns >= 0 on success or
  160. * negative errno on error.
  161. */
  162. int phy_restart_aneg(struct phy_device *phydev)
  163. {
  164. int ret;
  165. if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
  166. ret = genphy_c45_restart_aneg(phydev);
  167. else
  168. ret = genphy_restart_aneg(phydev);
  169. return ret;
  170. }
  171. EXPORT_SYMBOL_GPL(phy_restart_aneg);
  172. /**
  173. * phy_aneg_done - return auto-negotiation status
  174. * @phydev: target phy_device struct
  175. *
  176. * Description: Return the auto-negotiation status from this @phydev
  177. * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
  178. * is still pending.
  179. */
  180. int phy_aneg_done(struct phy_device *phydev)
  181. {
  182. if (phydev->drv && phydev->drv->aneg_done)
  183. return phydev->drv->aneg_done(phydev);
  184. else if (phydev->is_c45)
  185. return genphy_c45_aneg_done(phydev);
  186. else
  187. return genphy_aneg_done(phydev);
  188. }
  189. EXPORT_SYMBOL(phy_aneg_done);
  190. /**
  191. * phy_supported_speeds - return all speeds currently supported by a phy device
  192. * @phy: The phy device to return supported speeds of.
  193. * @speeds: buffer to store supported speeds in.
  194. * @size: size of speeds buffer.
  195. *
  196. * Description: Returns the number of supported speeds, and fills the speeds
  197. * buffer with the supported speeds. If speeds buffer is too small to contain
  198. * all currently supported speeds, will return as many speeds as can fit.
  199. */
  200. unsigned int phy_supported_speeds(struct phy_device *phy,
  201. unsigned int *speeds,
  202. unsigned int size)
  203. {
  204. return phy_caps_speeds(speeds, size, phy->supported);
  205. }
  206. /**
  207. * phy_check_valid - check if there is a valid PHY setting which matches
  208. * speed, duplex, and feature mask
  209. * @speed: speed to match
  210. * @duplex: duplex to match
  211. * @features: A mask of the valid settings
  212. *
  213. * Description: Returns true if there is a valid setting, false otherwise.
  214. */
  215. bool phy_check_valid(int speed, int duplex, unsigned long *features)
  216. {
  217. return phy_caps_valid(speed, duplex, features);
  218. }
  219. EXPORT_SYMBOL(phy_check_valid);
  220. /**
  221. * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
  222. * @phydev: the target phy_device struct
  223. *
  224. * Description: Make sure the PHY is set to supported speeds and
  225. * duplexes. Drop down by one in this order: 1000/FULL,
  226. * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
  227. */
  228. static void phy_sanitize_settings(struct phy_device *phydev)
  229. {
  230. const struct link_capabilities *c;
  231. c = phy_caps_lookup(phydev->speed, phydev->duplex, phydev->supported,
  232. false);
  233. if (c) {
  234. phydev->speed = c->speed;
  235. phydev->duplex = c->duplex;
  236. } else {
  237. /* We failed to find anything (no supported speeds?) */
  238. phydev->speed = SPEED_UNKNOWN;
  239. phydev->duplex = DUPLEX_UNKNOWN;
  240. }
  241. }
  242. void phy_ethtool_ksettings_get(struct phy_device *phydev,
  243. struct ethtool_link_ksettings *cmd)
  244. {
  245. mutex_lock(&phydev->lock);
  246. linkmode_copy(cmd->link_modes.supported, phydev->supported);
  247. linkmode_copy(cmd->link_modes.advertising, phydev->advertising);
  248. linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising);
  249. cmd->base.speed = phydev->speed;
  250. cmd->base.duplex = phydev->duplex;
  251. cmd->base.master_slave_cfg = phydev->master_slave_get;
  252. cmd->base.master_slave_state = phydev->master_slave_state;
  253. cmd->base.rate_matching = phydev->rate_matching;
  254. if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
  255. cmd->base.port = PORT_BNC;
  256. else
  257. cmd->base.port = phydev->port;
  258. cmd->base.transceiver = phydev->is_internal ?
  259. XCVR_INTERNAL : XCVR_EXTERNAL;
  260. cmd->base.phy_address = phydev->mdio.addr;
  261. cmd->base.autoneg = phydev->autoneg;
  262. cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
  263. cmd->base.eth_tp_mdix = phydev->mdix;
  264. mutex_unlock(&phydev->lock);
  265. }
  266. EXPORT_SYMBOL(phy_ethtool_ksettings_get);
  267. /**
  268. * phy_mii_ioctl - generic PHY MII ioctl interface
  269. * @phydev: the phy_device struct
  270. * @ifr: &struct ifreq for socket ioctl's
  271. * @cmd: ioctl cmd to execute
  272. *
  273. * Note that this function is currently incompatible with the
  274. * PHYCONTROL layer. It changes registers without regard to
  275. * current state. Use at own risk.
  276. */
  277. int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
  278. {
  279. struct mii_ioctl_data *mii_data = if_mii(ifr);
  280. struct kernel_hwtstamp_config kernel_cfg;
  281. struct netlink_ext_ack extack = {};
  282. u16 val = mii_data->val_in;
  283. bool change_autoneg = false;
  284. struct hwtstamp_config cfg;
  285. int prtad, devad;
  286. int ret;
  287. switch (cmd) {
  288. case SIOCGMIIPHY:
  289. mii_data->phy_id = phydev->mdio.addr;
  290. fallthrough;
  291. case SIOCGMIIREG:
  292. if (mdio_phy_id_is_c45(mii_data->phy_id)) {
  293. prtad = mdio_phy_id_prtad(mii_data->phy_id);
  294. devad = mdio_phy_id_devad(mii_data->phy_id);
  295. ret = mdiobus_c45_read(phydev->mdio.bus, prtad, devad,
  296. mii_data->reg_num);
  297. } else {
  298. ret = mdiobus_read(phydev->mdio.bus, mii_data->phy_id,
  299. mii_data->reg_num);
  300. }
  301. if (ret < 0)
  302. return ret;
  303. mii_data->val_out = ret;
  304. return 0;
  305. case SIOCSMIIREG:
  306. if (mdio_phy_id_is_c45(mii_data->phy_id)) {
  307. prtad = mdio_phy_id_prtad(mii_data->phy_id);
  308. devad = mdio_phy_id_devad(mii_data->phy_id);
  309. } else {
  310. prtad = mii_data->phy_id;
  311. devad = mii_data->reg_num;
  312. }
  313. if (prtad == phydev->mdio.addr) {
  314. switch (devad) {
  315. case MII_BMCR:
  316. if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
  317. if (phydev->autoneg == AUTONEG_ENABLE)
  318. change_autoneg = true;
  319. phydev->autoneg = AUTONEG_DISABLE;
  320. if (val & BMCR_FULLDPLX)
  321. phydev->duplex = DUPLEX_FULL;
  322. else
  323. phydev->duplex = DUPLEX_HALF;
  324. if (val & BMCR_SPEED1000)
  325. phydev->speed = SPEED_1000;
  326. else if (val & BMCR_SPEED100)
  327. phydev->speed = SPEED_100;
  328. else phydev->speed = SPEED_10;
  329. } else {
  330. if (phydev->autoneg == AUTONEG_DISABLE)
  331. change_autoneg = true;
  332. phydev->autoneg = AUTONEG_ENABLE;
  333. }
  334. break;
  335. case MII_ADVERTISE:
  336. mii_adv_mod_linkmode_adv_t(phydev->advertising,
  337. val);
  338. change_autoneg = true;
  339. break;
  340. case MII_CTRL1000:
  341. mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising,
  342. val);
  343. change_autoneg = true;
  344. break;
  345. default:
  346. /* do nothing */
  347. break;
  348. }
  349. }
  350. if (mdio_phy_id_is_c45(mii_data->phy_id))
  351. mdiobus_c45_write(phydev->mdio.bus, prtad, devad,
  352. mii_data->reg_num, val);
  353. else
  354. mdiobus_write(phydev->mdio.bus, prtad, devad, val);
  355. if (prtad == phydev->mdio.addr &&
  356. devad == MII_BMCR &&
  357. val & BMCR_RESET)
  358. return phy_init_hw(phydev);
  359. if (change_autoneg)
  360. return phy_start_aneg(phydev);
  361. return 0;
  362. case SIOCSHWTSTAMP:
  363. if (phydev->mii_ts && phydev->mii_ts->hwtstamp_set) {
  364. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  365. return -EFAULT;
  366. hwtstamp_config_to_kernel(&kernel_cfg, &cfg);
  367. ret = phydev->mii_ts->hwtstamp_set(phydev->mii_ts,
  368. &kernel_cfg,
  369. &extack);
  370. if (ret)
  371. return ret;
  372. hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
  373. if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
  374. return -EFAULT;
  375. return 0;
  376. }
  377. fallthrough;
  378. default:
  379. return -EOPNOTSUPP;
  380. }
  381. }
  382. EXPORT_SYMBOL(phy_mii_ioctl);
  383. /**
  384. * phy_do_ioctl - generic ndo_eth_ioctl implementation
  385. * @dev: the net_device struct
  386. * @ifr: &struct ifreq for socket ioctl's
  387. * @cmd: ioctl cmd to execute
  388. */
  389. int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  390. {
  391. if (!dev->phydev)
  392. return -ENODEV;
  393. return phy_mii_ioctl(dev->phydev, ifr, cmd);
  394. }
  395. EXPORT_SYMBOL(phy_do_ioctl);
  396. /**
  397. * phy_do_ioctl_running - generic ndo_eth_ioctl implementation but test first
  398. *
  399. * @dev: the net_device struct
  400. * @ifr: &struct ifreq for socket ioctl's
  401. * @cmd: ioctl cmd to execute
  402. *
  403. * Same as phy_do_ioctl, but ensures that net_device is running before
  404. * handling the ioctl.
  405. */
  406. int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd)
  407. {
  408. if (!netif_running(dev))
  409. return -ENODEV;
  410. return phy_do_ioctl(dev, ifr, cmd);
  411. }
  412. EXPORT_SYMBOL(phy_do_ioctl_running);
  413. /**
  414. * __phy_hwtstamp_get - Get hardware timestamping configuration from PHY
  415. *
  416. * @phydev: the PHY device structure
  417. * @config: structure holding the timestamping configuration
  418. *
  419. * Query the PHY device for its current hardware timestamping configuration.
  420. */
  421. int __phy_hwtstamp_get(struct phy_device *phydev,
  422. struct kernel_hwtstamp_config *config)
  423. {
  424. if (!phydev)
  425. return -ENODEV;
  426. if (phydev->mii_ts && phydev->mii_ts->hwtstamp_get)
  427. return phydev->mii_ts->hwtstamp_get(phydev->mii_ts, config);
  428. return -EOPNOTSUPP;
  429. }
  430. /**
  431. * __phy_hwtstamp_set - Modify PHY hardware timestamping configuration
  432. *
  433. * @phydev: the PHY device structure
  434. * @config: structure holding the timestamping configuration
  435. * @extack: netlink extended ack structure, for error reporting
  436. */
  437. int __phy_hwtstamp_set(struct phy_device *phydev,
  438. struct kernel_hwtstamp_config *config,
  439. struct netlink_ext_ack *extack)
  440. {
  441. if (!phydev)
  442. return -ENODEV;
  443. if (phydev->mii_ts && phydev->mii_ts->hwtstamp_set)
  444. return phydev->mii_ts->hwtstamp_set(phydev->mii_ts, config,
  445. extack);
  446. return -EOPNOTSUPP;
  447. }
  448. /**
  449. * phy_queue_state_machine - Trigger the state machine to run soon
  450. *
  451. * @phydev: the phy_device struct
  452. * @jiffies: Run the state machine after these jiffies
  453. */
  454. static void phy_queue_state_machine(struct phy_device *phydev,
  455. unsigned long jiffies)
  456. {
  457. mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
  458. jiffies);
  459. }
  460. /**
  461. * phy_trigger_machine - Trigger the state machine to run now
  462. *
  463. * @phydev: the phy_device struct
  464. */
  465. void phy_trigger_machine(struct phy_device *phydev)
  466. {
  467. phy_queue_state_machine(phydev, 0);
  468. }
  469. EXPORT_SYMBOL(phy_trigger_machine);
  470. static void phy_abort_cable_test(struct phy_device *phydev)
  471. {
  472. int err;
  473. ethnl_cable_test_finished(phydev);
  474. err = phy_init_hw(phydev);
  475. if (err)
  476. phydev_err(phydev, "Error while aborting cable test");
  477. }
  478. /**
  479. * phy_ethtool_get_strings - Get the statistic counter names
  480. *
  481. * @phydev: the phy_device struct
  482. * @data: Where to put the strings
  483. */
  484. int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
  485. {
  486. if (!phydev->drv)
  487. return -EIO;
  488. mutex_lock(&phydev->lock);
  489. phydev->drv->get_strings(phydev, data);
  490. mutex_unlock(&phydev->lock);
  491. return 0;
  492. }
  493. EXPORT_SYMBOL(phy_ethtool_get_strings);
  494. /**
  495. * phy_ethtool_get_sset_count - Get the number of statistic counters
  496. *
  497. * @phydev: the phy_device struct
  498. */
  499. int phy_ethtool_get_sset_count(struct phy_device *phydev)
  500. {
  501. int ret;
  502. if (!phydev->drv)
  503. return -EIO;
  504. if (phydev->drv->get_sset_count &&
  505. phydev->drv->get_strings &&
  506. phydev->drv->get_stats) {
  507. mutex_lock(&phydev->lock);
  508. ret = phydev->drv->get_sset_count(phydev);
  509. mutex_unlock(&phydev->lock);
  510. return ret;
  511. }
  512. return -EOPNOTSUPP;
  513. }
  514. EXPORT_SYMBOL(phy_ethtool_get_sset_count);
  515. /**
  516. * phy_ethtool_get_stats - Get the statistic counters
  517. *
  518. * @phydev: the phy_device struct
  519. * @stats: What counters to get
  520. * @data: Where to store the counters
  521. */
  522. int phy_ethtool_get_stats(struct phy_device *phydev,
  523. struct ethtool_stats *stats, u64 *data)
  524. {
  525. if (!phydev->drv)
  526. return -EIO;
  527. mutex_lock(&phydev->lock);
  528. phydev->drv->get_stats(phydev, stats, data);
  529. mutex_unlock(&phydev->lock);
  530. return 0;
  531. }
  532. EXPORT_SYMBOL(phy_ethtool_get_stats);
  533. /**
  534. * __phy_ethtool_get_phy_stats - Retrieve standardized PHY statistics
  535. * @phydev: Pointer to the PHY device
  536. * @phy_stats: Pointer to ethtool_eth_phy_stats structure
  537. * @phydev_stats: Pointer to ethtool_phy_stats structure
  538. *
  539. * Fetches PHY statistics using a kernel-defined interface for consistent
  540. * diagnostics. Unlike phy_ethtool_get_stats(), which allows custom stats,
  541. * this function enforces a standardized format for better interoperability.
  542. */
  543. void __phy_ethtool_get_phy_stats(struct phy_device *phydev,
  544. struct ethtool_eth_phy_stats *phy_stats,
  545. struct ethtool_phy_stats *phydev_stats)
  546. {
  547. if (!phydev->drv || !phydev->drv->get_phy_stats)
  548. return;
  549. mutex_lock(&phydev->lock);
  550. phydev->drv->get_phy_stats(phydev, phy_stats, phydev_stats);
  551. mutex_unlock(&phydev->lock);
  552. }
  553. /**
  554. * __phy_ethtool_get_link_ext_stats - Retrieve extended link statistics for a PHY
  555. * @phydev: Pointer to the PHY device
  556. * @link_stats: Pointer to the structure to store extended link statistics
  557. *
  558. * Populates the ethtool_link_ext_stats structure with link down event counts
  559. * and additional driver-specific link statistics, if available.
  560. */
  561. void __phy_ethtool_get_link_ext_stats(struct phy_device *phydev,
  562. struct ethtool_link_ext_stats *link_stats)
  563. {
  564. link_stats->link_down_events = READ_ONCE(phydev->link_down_events);
  565. if (!phydev->drv || !phydev->drv->get_link_stats)
  566. return;
  567. mutex_lock(&phydev->lock);
  568. phydev->drv->get_link_stats(phydev, link_stats);
  569. mutex_unlock(&phydev->lock);
  570. }
  571. /**
  572. * phy_ethtool_get_plca_cfg - Get PLCA RS configuration
  573. * @phydev: the phy_device struct
  574. * @plca_cfg: where to store the retrieved configuration
  575. *
  576. * Retrieve the PLCA configuration from the PHY. Return 0 on success or a
  577. * negative value if an error occurred.
  578. */
  579. int phy_ethtool_get_plca_cfg(struct phy_device *phydev,
  580. struct phy_plca_cfg *plca_cfg)
  581. {
  582. int ret;
  583. if (!phydev->drv) {
  584. ret = -EIO;
  585. goto out;
  586. }
  587. if (!phydev->drv->get_plca_cfg) {
  588. ret = -EOPNOTSUPP;
  589. goto out;
  590. }
  591. mutex_lock(&phydev->lock);
  592. ret = phydev->drv->get_plca_cfg(phydev, plca_cfg);
  593. mutex_unlock(&phydev->lock);
  594. out:
  595. return ret;
  596. }
  597. /**
  598. * plca_check_valid - Check PLCA configuration before enabling
  599. * @phydev: the phy_device struct
  600. * @plca_cfg: current PLCA configuration
  601. * @extack: extack for reporting useful error messages
  602. *
  603. * Checks whether the PLCA and PHY configuration are consistent and it is safe
  604. * to enable PLCA. Returns 0 on success or a negative value if the PLCA or PHY
  605. * configuration is not consistent.
  606. */
  607. static int plca_check_valid(struct phy_device *phydev,
  608. const struct phy_plca_cfg *plca_cfg,
  609. struct netlink_ext_ack *extack)
  610. {
  611. int ret = 0;
  612. if (!linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT,
  613. phydev->advertising)) {
  614. ret = -EOPNOTSUPP;
  615. NL_SET_ERR_MSG(extack,
  616. "Point to Multi-Point mode is not enabled");
  617. } else if (plca_cfg->node_id >= 255) {
  618. NL_SET_ERR_MSG(extack, "PLCA node ID is not set");
  619. ret = -EINVAL;
  620. }
  621. return ret;
  622. }
  623. /**
  624. * phy_ethtool_set_plca_cfg - Set PLCA RS configuration
  625. * @phydev: the phy_device struct
  626. * @plca_cfg: new PLCA configuration to apply
  627. * @extack: extack for reporting useful error messages
  628. *
  629. * Sets the PLCA configuration in the PHY. Return 0 on success or a
  630. * negative value if an error occurred.
  631. */
  632. int phy_ethtool_set_plca_cfg(struct phy_device *phydev,
  633. const struct phy_plca_cfg *plca_cfg,
  634. struct netlink_ext_ack *extack)
  635. {
  636. struct phy_plca_cfg *curr_plca_cfg;
  637. int ret;
  638. if (!phydev->drv) {
  639. ret = -EIO;
  640. goto out;
  641. }
  642. if (!phydev->drv->set_plca_cfg ||
  643. !phydev->drv->get_plca_cfg) {
  644. ret = -EOPNOTSUPP;
  645. goto out;
  646. }
  647. curr_plca_cfg = kmalloc_obj(*curr_plca_cfg);
  648. if (!curr_plca_cfg) {
  649. ret = -ENOMEM;
  650. goto out;
  651. }
  652. mutex_lock(&phydev->lock);
  653. ret = phydev->drv->get_plca_cfg(phydev, curr_plca_cfg);
  654. if (ret)
  655. goto out_drv;
  656. if (curr_plca_cfg->enabled < 0 && plca_cfg->enabled >= 0) {
  657. NL_SET_ERR_MSG(extack,
  658. "PHY does not support changing the PLCA 'enable' attribute");
  659. ret = -EINVAL;
  660. goto out_drv;
  661. }
  662. if (curr_plca_cfg->node_id < 0 && plca_cfg->node_id >= 0) {
  663. NL_SET_ERR_MSG(extack,
  664. "PHY does not support changing the PLCA 'local node ID' attribute");
  665. ret = -EINVAL;
  666. goto out_drv;
  667. }
  668. if (curr_plca_cfg->node_cnt < 0 && plca_cfg->node_cnt >= 0) {
  669. NL_SET_ERR_MSG(extack,
  670. "PHY does not support changing the PLCA 'node count' attribute");
  671. ret = -EINVAL;
  672. goto out_drv;
  673. }
  674. if (curr_plca_cfg->to_tmr < 0 && plca_cfg->to_tmr >= 0) {
  675. NL_SET_ERR_MSG(extack,
  676. "PHY does not support changing the PLCA 'TO timer' attribute");
  677. ret = -EINVAL;
  678. goto out_drv;
  679. }
  680. if (curr_plca_cfg->burst_cnt < 0 && plca_cfg->burst_cnt >= 0) {
  681. NL_SET_ERR_MSG(extack,
  682. "PHY does not support changing the PLCA 'burst count' attribute");
  683. ret = -EINVAL;
  684. goto out_drv;
  685. }
  686. if (curr_plca_cfg->burst_tmr < 0 && plca_cfg->burst_tmr >= 0) {
  687. NL_SET_ERR_MSG(extack,
  688. "PHY does not support changing the PLCA 'burst timer' attribute");
  689. ret = -EINVAL;
  690. goto out_drv;
  691. }
  692. // if enabling PLCA, perform a few sanity checks
  693. if (plca_cfg->enabled > 0) {
  694. // allow setting node_id concurrently with enabled
  695. if (plca_cfg->node_id >= 0)
  696. curr_plca_cfg->node_id = plca_cfg->node_id;
  697. ret = plca_check_valid(phydev, curr_plca_cfg, extack);
  698. if (ret)
  699. goto out_drv;
  700. }
  701. ret = phydev->drv->set_plca_cfg(phydev, plca_cfg);
  702. out_drv:
  703. kfree(curr_plca_cfg);
  704. mutex_unlock(&phydev->lock);
  705. out:
  706. return ret;
  707. }
  708. /**
  709. * phy_ethtool_get_plca_status - Get PLCA RS status information
  710. * @phydev: the phy_device struct
  711. * @plca_st: where to store the retrieved status information
  712. *
  713. * Retrieve the PLCA status information from the PHY. Return 0 on success or a
  714. * negative value if an error occurred.
  715. */
  716. int phy_ethtool_get_plca_status(struct phy_device *phydev,
  717. struct phy_plca_status *plca_st)
  718. {
  719. int ret;
  720. if (!phydev->drv) {
  721. ret = -EIO;
  722. goto out;
  723. }
  724. if (!phydev->drv->get_plca_status) {
  725. ret = -EOPNOTSUPP;
  726. goto out;
  727. }
  728. mutex_lock(&phydev->lock);
  729. ret = phydev->drv->get_plca_status(phydev, plca_st);
  730. mutex_unlock(&phydev->lock);
  731. out:
  732. return ret;
  733. }
  734. /**
  735. * phy_start_cable_test - Start a cable test
  736. *
  737. * @phydev: the phy_device struct
  738. * @extack: extack for reporting useful error messages
  739. */
  740. int phy_start_cable_test(struct phy_device *phydev,
  741. struct netlink_ext_ack *extack)
  742. {
  743. struct net_device *dev = phydev->attached_dev;
  744. int err = -ENOMEM;
  745. if (!(phydev->drv &&
  746. phydev->drv->cable_test_start &&
  747. phydev->drv->cable_test_get_status)) {
  748. NL_SET_ERR_MSG(extack,
  749. "PHY driver does not support cable testing");
  750. return -EOPNOTSUPP;
  751. }
  752. mutex_lock(&phydev->lock);
  753. if (phydev->state == PHY_CABLETEST) {
  754. NL_SET_ERR_MSG(extack,
  755. "PHY already performing a test");
  756. err = -EBUSY;
  757. goto out;
  758. }
  759. if (phydev->state < PHY_UP ||
  760. phydev->state > PHY_CABLETEST) {
  761. NL_SET_ERR_MSG(extack,
  762. "PHY not configured. Try setting interface up");
  763. err = -EBUSY;
  764. goto out;
  765. }
  766. err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
  767. if (err)
  768. goto out;
  769. /* Mark the carrier down until the test is complete */
  770. phy_link_down(phydev);
  771. netif_testing_on(dev);
  772. err = phydev->drv->cable_test_start(phydev);
  773. if (err) {
  774. netif_testing_off(dev);
  775. phy_link_up(phydev);
  776. goto out_free;
  777. }
  778. phydev->state = PHY_CABLETEST;
  779. if (phy_polling_mode(phydev))
  780. phy_trigger_machine(phydev);
  781. mutex_unlock(&phydev->lock);
  782. return 0;
  783. out_free:
  784. ethnl_cable_test_free(phydev);
  785. out:
  786. mutex_unlock(&phydev->lock);
  787. return err;
  788. }
  789. EXPORT_SYMBOL(phy_start_cable_test);
  790. /**
  791. * phy_start_cable_test_tdr - Start a raw TDR cable test
  792. *
  793. * @phydev: the phy_device struct
  794. * @extack: extack for reporting useful error messages
  795. * @config: Configuration of the test to run
  796. */
  797. int phy_start_cable_test_tdr(struct phy_device *phydev,
  798. struct netlink_ext_ack *extack,
  799. const struct phy_tdr_config *config)
  800. {
  801. struct net_device *dev = phydev->attached_dev;
  802. int err = -ENOMEM;
  803. if (!(phydev->drv &&
  804. phydev->drv->cable_test_tdr_start &&
  805. phydev->drv->cable_test_get_status)) {
  806. NL_SET_ERR_MSG(extack,
  807. "PHY driver does not support cable test TDR");
  808. return -EOPNOTSUPP;
  809. }
  810. mutex_lock(&phydev->lock);
  811. if (phydev->state == PHY_CABLETEST) {
  812. NL_SET_ERR_MSG(extack,
  813. "PHY already performing a test");
  814. err = -EBUSY;
  815. goto out;
  816. }
  817. if (phydev->state < PHY_UP ||
  818. phydev->state > PHY_CABLETEST) {
  819. NL_SET_ERR_MSG(extack,
  820. "PHY not configured. Try setting interface up");
  821. err = -EBUSY;
  822. goto out;
  823. }
  824. err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_TDR_NTF);
  825. if (err)
  826. goto out;
  827. /* Mark the carrier down until the test is complete */
  828. phy_link_down(phydev);
  829. netif_testing_on(dev);
  830. err = phydev->drv->cable_test_tdr_start(phydev, config);
  831. if (err) {
  832. netif_testing_off(dev);
  833. phy_link_up(phydev);
  834. goto out_free;
  835. }
  836. phydev->state = PHY_CABLETEST;
  837. if (phy_polling_mode(phydev))
  838. phy_trigger_machine(phydev);
  839. mutex_unlock(&phydev->lock);
  840. return 0;
  841. out_free:
  842. ethnl_cable_test_free(phydev);
  843. out:
  844. mutex_unlock(&phydev->lock);
  845. return err;
  846. }
  847. EXPORT_SYMBOL(phy_start_cable_test_tdr);
  848. int phy_config_aneg(struct phy_device *phydev)
  849. {
  850. if (phydev->drv->config_aneg)
  851. return phydev->drv->config_aneg(phydev);
  852. /* Clause 45 PHYs that don't implement Clause 22 registers are not
  853. * allowed to call genphy_config_aneg()
  854. */
  855. if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
  856. return genphy_c45_config_aneg(phydev);
  857. return genphy_config_aneg(phydev);
  858. }
  859. EXPORT_SYMBOL(phy_config_aneg);
  860. /**
  861. * phy_check_link_status - check link status and set state accordingly
  862. * @phydev: the phy_device struct
  863. *
  864. * Description: Check for link and whether autoneg was triggered / is running
  865. * and set state accordingly
  866. */
  867. static int phy_check_link_status(struct phy_device *phydev)
  868. {
  869. int err;
  870. lockdep_assert_held(&phydev->lock);
  871. /* Keep previous state if loopback is enabled because some PHYs
  872. * report that Link is Down when loopback is enabled.
  873. */
  874. if (phydev->loopback_enabled)
  875. return 0;
  876. err = phy_read_status(phydev);
  877. if (err)
  878. return err;
  879. if (phydev->link && phydev->state != PHY_RUNNING) {
  880. phy_check_downshift(phydev);
  881. phydev->state = PHY_RUNNING;
  882. err = genphy_c45_eee_is_active(phydev, NULL);
  883. phydev->eee_active = err > 0;
  884. phydev->enable_tx_lpi = phydev->eee_cfg.tx_lpi_enabled &&
  885. phydev->eee_active;
  886. phy_link_up(phydev);
  887. } else if (!phydev->link && phydev->state != PHY_NOLINK) {
  888. phydev->state = PHY_NOLINK;
  889. phydev->eee_active = false;
  890. phydev->enable_tx_lpi = false;
  891. phy_link_down(phydev);
  892. }
  893. return 0;
  894. }
  895. /**
  896. * phy_inband_caps - query which in-band signalling modes are supported
  897. * @phydev: a pointer to a &struct phy_device
  898. * @interface: the interface mode for the PHY
  899. *
  900. * Returns zero if it is unknown what in-band signalling is supported by the
  901. * PHY (e.g. because the PHY driver doesn't implement the method.) Otherwise,
  902. * returns a bit mask of the LINK_INBAND_* values from
  903. * &enum link_inband_signalling to describe which inband modes are supported
  904. * by the PHY for this interface mode.
  905. */
  906. unsigned int phy_inband_caps(struct phy_device *phydev,
  907. phy_interface_t interface)
  908. {
  909. if (phydev->drv && phydev->drv->inband_caps)
  910. return phydev->drv->inband_caps(phydev, interface);
  911. return 0;
  912. }
  913. EXPORT_SYMBOL_GPL(phy_inband_caps);
  914. /**
  915. * phy_config_inband - configure the desired PHY in-band mode
  916. * @phydev: the phy_device struct
  917. * @modes: in-band modes to configure
  918. *
  919. * Description: disables, enables or enables-with-bypass in-band signalling
  920. * between the PHY and host system.
  921. *
  922. * Returns: zero on success, or negative errno value.
  923. */
  924. int phy_config_inband(struct phy_device *phydev, unsigned int modes)
  925. {
  926. lockdep_assert_held(&phydev->lock);
  927. if (!!(modes & LINK_INBAND_DISABLE) +
  928. !!(modes & LINK_INBAND_ENABLE) +
  929. !!(modes & LINK_INBAND_BYPASS) != 1)
  930. return -EINVAL;
  931. if (!phydev->drv)
  932. return -EIO;
  933. else if (!phydev->drv->config_inband)
  934. return -EOPNOTSUPP;
  935. return phydev->drv->config_inband(phydev, modes);
  936. }
  937. EXPORT_SYMBOL(phy_config_inband);
  938. /**
  939. * _phy_start_aneg - start auto-negotiation for this PHY device
  940. * @phydev: the phy_device struct
  941. *
  942. * Description: Sanitizes the settings (if we're not autonegotiating
  943. * them), and then calls the driver's config_aneg function.
  944. * If the PHYCONTROL Layer is operating, we change the state to
  945. * reflect the beginning of Auto-negotiation or forcing.
  946. */
  947. int _phy_start_aneg(struct phy_device *phydev)
  948. {
  949. int err;
  950. lockdep_assert_held(&phydev->lock);
  951. if (!phydev->drv)
  952. return -EIO;
  953. if (AUTONEG_DISABLE == phydev->autoneg)
  954. phy_sanitize_settings(phydev);
  955. err = phy_config_aneg(phydev);
  956. if (err < 0)
  957. return err;
  958. if (phy_is_started(phydev))
  959. err = phy_check_link_status(phydev);
  960. return err;
  961. }
  962. EXPORT_SYMBOL(_phy_start_aneg);
  963. /**
  964. * phy_start_aneg - start auto-negotiation for this PHY device
  965. * @phydev: the phy_device struct
  966. *
  967. * Description: Sanitizes the settings (if we're not autonegotiating
  968. * them), and then calls the driver's config_aneg function.
  969. * If the PHYCONTROL Layer is operating, we change the state to
  970. * reflect the beginning of Auto-negotiation or forcing.
  971. */
  972. int phy_start_aneg(struct phy_device *phydev)
  973. {
  974. int err;
  975. mutex_lock(&phydev->lock);
  976. err = _phy_start_aneg(phydev);
  977. mutex_unlock(&phydev->lock);
  978. return err;
  979. }
  980. EXPORT_SYMBOL(phy_start_aneg);
  981. static int phy_poll_aneg_done(struct phy_device *phydev)
  982. {
  983. unsigned int retries = 100;
  984. int ret;
  985. do {
  986. msleep(100);
  987. ret = phy_aneg_done(phydev);
  988. } while (!ret && --retries);
  989. if (!ret)
  990. return -ETIMEDOUT;
  991. return ret < 0 ? ret : 0;
  992. }
  993. int phy_ethtool_ksettings_set(struct phy_device *phydev,
  994. const struct ethtool_link_ksettings *cmd)
  995. {
  996. __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
  997. u8 autoneg = cmd->base.autoneg;
  998. u8 duplex = cmd->base.duplex;
  999. u32 speed = cmd->base.speed;
  1000. if (cmd->base.phy_address != phydev->mdio.addr)
  1001. return -EINVAL;
  1002. linkmode_copy(advertising, cmd->link_modes.advertising);
  1003. /* We make sure that we don't pass unsupported values in to the PHY */
  1004. linkmode_and(advertising, advertising, phydev->supported);
  1005. /* Verify the settings we care about. */
  1006. if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
  1007. return -EINVAL;
  1008. if (autoneg == AUTONEG_ENABLE &&
  1009. (linkmode_empty(advertising) ||
  1010. !linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  1011. phydev->supported)))
  1012. return -EINVAL;
  1013. if (autoneg == AUTONEG_DISABLE &&
  1014. ((speed != SPEED_1000 &&
  1015. speed != SPEED_100 &&
  1016. speed != SPEED_10) ||
  1017. (duplex != DUPLEX_HALF &&
  1018. duplex != DUPLEX_FULL)))
  1019. return -EINVAL;
  1020. mutex_lock(&phydev->lock);
  1021. phydev->autoneg = autoneg;
  1022. if (autoneg == AUTONEG_DISABLE) {
  1023. phydev->speed = speed;
  1024. phydev->duplex = duplex;
  1025. }
  1026. linkmode_copy(phydev->advertising, advertising);
  1027. linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  1028. phydev->advertising, autoneg == AUTONEG_ENABLE);
  1029. phydev->master_slave_set = cmd->base.master_slave_cfg;
  1030. phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
  1031. /* Restart the PHY */
  1032. if (phy_is_started(phydev)) {
  1033. phydev->state = PHY_UP;
  1034. phy_trigger_machine(phydev);
  1035. } else {
  1036. _phy_start_aneg(phydev);
  1037. }
  1038. mutex_unlock(&phydev->lock);
  1039. return 0;
  1040. }
  1041. EXPORT_SYMBOL(phy_ethtool_ksettings_set);
  1042. /**
  1043. * phy_speed_down - set speed to lowest speed supported by both link partners
  1044. * @phydev: the phy_device struct
  1045. * @sync: perform action synchronously
  1046. *
  1047. * Description: Typically used to save energy when waiting for a WoL packet
  1048. *
  1049. * WARNING: Setting sync to false may cause the system being unable to suspend
  1050. * in case the PHY generates an interrupt when finishing the autonegotiation.
  1051. * This interrupt may wake up the system immediately after suspend.
  1052. * Therefore use sync = false only if you're sure it's safe with the respective
  1053. * network chip.
  1054. */
  1055. int phy_speed_down(struct phy_device *phydev, bool sync)
  1056. {
  1057. __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
  1058. int ret = 0;
  1059. mutex_lock(&phydev->lock);
  1060. if (phydev->autoneg != AUTONEG_ENABLE)
  1061. goto out;
  1062. linkmode_copy(adv_tmp, phydev->advertising);
  1063. ret = phy_speed_down_core(phydev);
  1064. if (ret)
  1065. goto out;
  1066. linkmode_copy(phydev->adv_old, adv_tmp);
  1067. if (linkmode_equal(phydev->advertising, adv_tmp)) {
  1068. ret = 0;
  1069. goto out;
  1070. }
  1071. ret = phy_config_aneg(phydev);
  1072. if (ret)
  1073. goto out;
  1074. ret = sync ? phy_poll_aneg_done(phydev) : 0;
  1075. out:
  1076. mutex_unlock(&phydev->lock);
  1077. return ret;
  1078. }
  1079. EXPORT_SYMBOL_GPL(phy_speed_down);
  1080. /**
  1081. * phy_speed_up - (re)set advertised speeds to all supported speeds
  1082. * @phydev: the phy_device struct
  1083. *
  1084. * Description: Used to revert the effect of phy_speed_down
  1085. */
  1086. int phy_speed_up(struct phy_device *phydev)
  1087. {
  1088. __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
  1089. int ret = 0;
  1090. mutex_lock(&phydev->lock);
  1091. if (phydev->autoneg != AUTONEG_ENABLE)
  1092. goto out;
  1093. if (linkmode_empty(phydev->adv_old))
  1094. goto out;
  1095. linkmode_copy(adv_tmp, phydev->advertising);
  1096. linkmode_copy(phydev->advertising, phydev->adv_old);
  1097. linkmode_zero(phydev->adv_old);
  1098. if (linkmode_equal(phydev->advertising, adv_tmp))
  1099. goto out;
  1100. ret = phy_config_aneg(phydev);
  1101. out:
  1102. mutex_unlock(&phydev->lock);
  1103. return ret;
  1104. }
  1105. EXPORT_SYMBOL_GPL(phy_speed_up);
  1106. /**
  1107. * phy_start_machine - start PHY state machine tracking
  1108. * @phydev: the phy_device struct
  1109. *
  1110. * Description: The PHY infrastructure can run a state machine
  1111. * which tracks whether the PHY is starting up, negotiating,
  1112. * etc. This function starts the delayed workqueue which tracks
  1113. * the state of the PHY. If you want to maintain your own state machine,
  1114. * do not call this function.
  1115. */
  1116. void phy_start_machine(struct phy_device *phydev)
  1117. {
  1118. phy_trigger_machine(phydev);
  1119. }
  1120. EXPORT_SYMBOL_GPL(phy_start_machine);
  1121. /**
  1122. * phy_stop_machine - stop the PHY state machine tracking
  1123. * @phydev: target phy_device struct
  1124. *
  1125. * Description: Stops the state machine delayed workqueue, sets the
  1126. * state to UP (unless it wasn't up yet). This function must be
  1127. * called BEFORE phy_detach.
  1128. */
  1129. void phy_stop_machine(struct phy_device *phydev)
  1130. {
  1131. cancel_delayed_work_sync(&phydev->state_queue);
  1132. mutex_lock(&phydev->lock);
  1133. if (phy_is_started(phydev))
  1134. phydev->state = PHY_UP;
  1135. mutex_unlock(&phydev->lock);
  1136. }
  1137. static void phy_process_error(struct phy_device *phydev)
  1138. {
  1139. /* phydev->lock must be held for the state change to be safe */
  1140. if (!mutex_is_locked(&phydev->lock))
  1141. phydev_err(phydev, "PHY-device data unsafe context\n");
  1142. phydev->state = PHY_ERROR;
  1143. phy_trigger_machine(phydev);
  1144. }
  1145. static void phy_error_precise(struct phy_device *phydev,
  1146. const void *func, int err)
  1147. {
  1148. WARN(1, "%pS: returned: %d\n", func, err);
  1149. phy_process_error(phydev);
  1150. }
  1151. /**
  1152. * phy_error - enter ERROR state for this PHY device
  1153. * @phydev: target phy_device struct
  1154. *
  1155. * Moves the PHY to the ERROR state in response to a read
  1156. * or write error, and tells the controller the link is down.
  1157. * Must be called with phydev->lock held.
  1158. */
  1159. void phy_error(struct phy_device *phydev)
  1160. {
  1161. WARN_ON(1);
  1162. phy_process_error(phydev);
  1163. }
  1164. EXPORT_SYMBOL(phy_error);
  1165. /**
  1166. * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
  1167. * @phydev: target phy_device struct
  1168. */
  1169. int phy_disable_interrupts(struct phy_device *phydev)
  1170. {
  1171. /* Disable PHY interrupts */
  1172. return phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  1173. }
  1174. /**
  1175. * phy_interrupt - PHY interrupt handler
  1176. * @irq: interrupt line
  1177. * @phy_dat: phy_device pointer
  1178. *
  1179. * Description: Handle PHY interrupt
  1180. */
  1181. static irqreturn_t phy_interrupt(int irq, void *phy_dat)
  1182. {
  1183. struct phy_device *phydev = phy_dat;
  1184. irqreturn_t ret;
  1185. /* Wakeup interrupts may occur during a system sleep transition.
  1186. * Postpone handling until the PHY has resumed.
  1187. */
  1188. if (IS_ENABLED(CONFIG_PM_SLEEP) && phydev->irq_suspended) {
  1189. struct net_device *netdev = phydev->attached_dev;
  1190. if (netdev) {
  1191. struct device *parent = netdev->dev.parent;
  1192. if (netdev->ethtool->wol_enabled)
  1193. pm_system_wakeup();
  1194. else if (device_may_wakeup(&netdev->dev))
  1195. pm_wakeup_dev_event(&netdev->dev, 0, true);
  1196. else if (parent && device_may_wakeup(parent))
  1197. pm_wakeup_dev_event(parent, 0, true);
  1198. }
  1199. phydev->irq_rerun = 1;
  1200. disable_irq_nosync(irq);
  1201. return IRQ_HANDLED;
  1202. }
  1203. mutex_lock(&phydev->lock);
  1204. ret = phydev->drv->handle_interrupt(phydev);
  1205. mutex_unlock(&phydev->lock);
  1206. return ret;
  1207. }
  1208. /**
  1209. * phy_enable_interrupts - Enable the interrupts from the PHY side
  1210. * @phydev: target phy_device struct
  1211. */
  1212. static int phy_enable_interrupts(struct phy_device *phydev)
  1213. {
  1214. return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  1215. }
  1216. /**
  1217. * phy_update_stats - Update PHY device statistics if supported.
  1218. * @phydev: Pointer to the PHY device structure.
  1219. *
  1220. * If the PHY driver provides an update_stats callback, this function
  1221. * invokes it to update the PHY statistics. If not, it returns 0.
  1222. *
  1223. * Return: 0 on success, or a negative error code if the callback fails.
  1224. */
  1225. static int phy_update_stats(struct phy_device *phydev)
  1226. {
  1227. if (!phydev->drv->update_stats)
  1228. return 0;
  1229. return phydev->drv->update_stats(phydev);
  1230. }
  1231. /**
  1232. * phy_request_interrupt - request and enable interrupt for a PHY device
  1233. * @phydev: target phy_device struct
  1234. *
  1235. * Description: Request and enable the interrupt for the given PHY.
  1236. * If this fails, then we set irq to PHY_POLL.
  1237. * This should only be called with a valid IRQ number.
  1238. */
  1239. void phy_request_interrupt(struct phy_device *phydev)
  1240. {
  1241. int err;
  1242. err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
  1243. IRQF_ONESHOT | IRQF_SHARED,
  1244. phydev_name(phydev), phydev);
  1245. if (err) {
  1246. phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
  1247. err, phydev->irq);
  1248. phydev->irq = PHY_POLL;
  1249. } else {
  1250. if (phy_enable_interrupts(phydev)) {
  1251. phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
  1252. phy_free_interrupt(phydev);
  1253. phydev->irq = PHY_POLL;
  1254. }
  1255. }
  1256. }
  1257. EXPORT_SYMBOL(phy_request_interrupt);
  1258. /**
  1259. * phy_free_interrupt - disable and free interrupt for a PHY device
  1260. * @phydev: target phy_device struct
  1261. *
  1262. * Description: Disable and free the interrupt for the given PHY.
  1263. * This should only be called with a valid IRQ number.
  1264. */
  1265. void phy_free_interrupt(struct phy_device *phydev)
  1266. {
  1267. phy_disable_interrupts(phydev);
  1268. free_irq(phydev->irq, phydev);
  1269. }
  1270. EXPORT_SYMBOL(phy_free_interrupt);
  1271. /**
  1272. * phy_get_next_update_time - Determine the next PHY update time
  1273. * @phydev: Pointer to the phy_device structure
  1274. *
  1275. * This function queries the PHY driver to get the time for the next polling
  1276. * event. If the driver does not implement the callback, a default value is
  1277. * used.
  1278. *
  1279. * Return: The time for the next polling event in jiffies
  1280. */
  1281. static unsigned int phy_get_next_update_time(struct phy_device *phydev)
  1282. {
  1283. if (phydev->drv && phydev->drv->get_next_update_time)
  1284. return phydev->drv->get_next_update_time(phydev);
  1285. return PHY_STATE_TIME;
  1286. }
  1287. enum phy_state_work {
  1288. PHY_STATE_WORK_NONE,
  1289. PHY_STATE_WORK_ANEG,
  1290. PHY_STATE_WORK_SUSPEND,
  1291. };
  1292. static enum phy_state_work _phy_state_machine(struct phy_device *phydev)
  1293. {
  1294. enum phy_state_work state_work = PHY_STATE_WORK_NONE;
  1295. struct net_device *dev = phydev->attached_dev;
  1296. enum phy_state old_state = phydev->state;
  1297. const void *func = NULL;
  1298. bool finished = false;
  1299. int err = 0;
  1300. switch (phydev->state) {
  1301. case PHY_DOWN:
  1302. case PHY_READY:
  1303. break;
  1304. case PHY_UP:
  1305. state_work = PHY_STATE_WORK_ANEG;
  1306. break;
  1307. case PHY_NOLINK:
  1308. case PHY_RUNNING:
  1309. err = phy_check_link_status(phydev);
  1310. func = &phy_check_link_status;
  1311. if (!err)
  1312. err = phy_update_stats(phydev);
  1313. break;
  1314. case PHY_CABLETEST:
  1315. err = phydev->drv->cable_test_get_status(phydev, &finished);
  1316. if (err) {
  1317. phy_abort_cable_test(phydev);
  1318. netif_testing_off(dev);
  1319. state_work = PHY_STATE_WORK_ANEG;
  1320. phydev->state = PHY_UP;
  1321. break;
  1322. }
  1323. if (finished) {
  1324. ethnl_cable_test_finished(phydev);
  1325. netif_testing_off(dev);
  1326. state_work = PHY_STATE_WORK_ANEG;
  1327. phydev->state = PHY_UP;
  1328. }
  1329. break;
  1330. case PHY_HALTED:
  1331. if (phydev->link) {
  1332. if (phydev->autoneg == AUTONEG_ENABLE) {
  1333. phydev->speed = SPEED_UNKNOWN;
  1334. phydev->duplex = DUPLEX_UNKNOWN;
  1335. }
  1336. if (phydev->master_slave_state !=
  1337. MASTER_SLAVE_STATE_UNSUPPORTED)
  1338. phydev->master_slave_state =
  1339. MASTER_SLAVE_STATE_UNKNOWN;
  1340. phydev->mdix = ETH_TP_MDI_INVALID;
  1341. linkmode_zero(phydev->lp_advertising);
  1342. }
  1343. fallthrough;
  1344. case PHY_ERROR:
  1345. if (phydev->link) {
  1346. phydev->link = 0;
  1347. phydev->eee_active = false;
  1348. phydev->enable_tx_lpi = false;
  1349. phy_link_down(phydev);
  1350. }
  1351. state_work = PHY_STATE_WORK_SUSPEND;
  1352. break;
  1353. }
  1354. if (state_work == PHY_STATE_WORK_ANEG) {
  1355. err = _phy_start_aneg(phydev);
  1356. func = &_phy_start_aneg;
  1357. }
  1358. if (err == -ENODEV)
  1359. return state_work;
  1360. if (err < 0)
  1361. phy_error_precise(phydev, func, err);
  1362. phy_process_state_change(phydev, old_state);
  1363. /* Only re-schedule a PHY state machine change if we are polling the
  1364. * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving
  1365. * between states from phy_mac_interrupt().
  1366. *
  1367. * In state PHY_HALTED the PHY gets suspended, so rescheduling the
  1368. * state machine would be pointless and possibly error prone when
  1369. * called from phy_disconnect() synchronously.
  1370. */
  1371. if (phy_polling_mode(phydev) && phy_is_started(phydev))
  1372. phy_queue_state_machine(phydev,
  1373. phy_get_next_update_time(phydev));
  1374. return state_work;
  1375. }
  1376. /* unlocked part of the PHY state machine */
  1377. static void _phy_state_machine_post_work(struct phy_device *phydev,
  1378. enum phy_state_work state_work)
  1379. {
  1380. if (state_work == PHY_STATE_WORK_SUSPEND)
  1381. phy_suspend(phydev);
  1382. }
  1383. /**
  1384. * phy_state_machine - Handle the state machine
  1385. * @work: work_struct that describes the work to be done
  1386. */
  1387. void phy_state_machine(struct work_struct *work)
  1388. {
  1389. struct delayed_work *dwork = to_delayed_work(work);
  1390. struct phy_device *phydev =
  1391. container_of(dwork, struct phy_device, state_queue);
  1392. enum phy_state_work state_work;
  1393. mutex_lock(&phydev->lock);
  1394. state_work = _phy_state_machine(phydev);
  1395. mutex_unlock(&phydev->lock);
  1396. _phy_state_machine_post_work(phydev, state_work);
  1397. }
  1398. /**
  1399. * phy_stop - Bring down the PHY link, and stop checking the status
  1400. * @phydev: target phy_device struct
  1401. */
  1402. void phy_stop(struct phy_device *phydev)
  1403. {
  1404. struct net_device *dev = phydev->attached_dev;
  1405. enum phy_state_work state_work;
  1406. enum phy_state old_state;
  1407. if (!phy_is_started(phydev) && phydev->state != PHY_DOWN &&
  1408. phydev->state != PHY_ERROR) {
  1409. WARN(1, "called from state %s\n",
  1410. phy_state_to_str(phydev->state));
  1411. return;
  1412. }
  1413. mutex_lock(&phydev->lock);
  1414. old_state = phydev->state;
  1415. if (phydev->state == PHY_CABLETEST) {
  1416. phy_abort_cable_test(phydev);
  1417. netif_testing_off(dev);
  1418. }
  1419. if (phydev->sfp_bus)
  1420. sfp_upstream_stop(phydev->sfp_bus);
  1421. phydev->state = PHY_HALTED;
  1422. phy_process_state_change(phydev, old_state);
  1423. state_work = _phy_state_machine(phydev);
  1424. mutex_unlock(&phydev->lock);
  1425. _phy_state_machine_post_work(phydev, state_work);
  1426. phy_stop_machine(phydev);
  1427. /* Cannot call flush_scheduled_work() here as desired because
  1428. * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
  1429. * will not reenable interrupts.
  1430. */
  1431. }
  1432. EXPORT_SYMBOL(phy_stop);
  1433. /**
  1434. * phy_start - start or restart a PHY device
  1435. * @phydev: target phy_device struct
  1436. *
  1437. * Description: Indicates the attached device's readiness to
  1438. * handle PHY-related work. Used during startup to start the
  1439. * PHY, and after a call to phy_stop() to resume operation.
  1440. * Also used to indicate the MDIO bus has cleared an error
  1441. * condition.
  1442. */
  1443. void phy_start(struct phy_device *phydev)
  1444. {
  1445. mutex_lock(&phydev->lock);
  1446. if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
  1447. WARN(1, "called from state %s\n",
  1448. phy_state_to_str(phydev->state));
  1449. goto out;
  1450. }
  1451. if (phydev->sfp_bus)
  1452. sfp_upstream_start(phydev->sfp_bus);
  1453. /* if phy was suspended, bring the physical link up again */
  1454. __phy_resume(phydev);
  1455. phydev->state = PHY_UP;
  1456. phy_start_machine(phydev);
  1457. out:
  1458. mutex_unlock(&phydev->lock);
  1459. }
  1460. EXPORT_SYMBOL(phy_start);
  1461. /**
  1462. * phy_mac_interrupt - MAC says the link has changed
  1463. * @phydev: phy_device struct with changed link
  1464. *
  1465. * The MAC layer is able to indicate there has been a change in the PHY link
  1466. * status. Trigger the state machine and work a work queue.
  1467. */
  1468. void phy_mac_interrupt(struct phy_device *phydev)
  1469. {
  1470. /* Trigger a state machine change */
  1471. phy_trigger_machine(phydev);
  1472. }
  1473. EXPORT_SYMBOL(phy_mac_interrupt);
  1474. /**
  1475. * phy_loopback - Configure loopback mode of PHY
  1476. * @phydev: target phy_device struct
  1477. * @enable: enable or disable loopback mode
  1478. * @speed: enable loopback mode with speed
  1479. *
  1480. * Configure loopback mode of PHY and signal link down and link up if speed is
  1481. * changing.
  1482. *
  1483. * Return: 0 on success, negative error code on failure.
  1484. */
  1485. int phy_loopback(struct phy_device *phydev, bool enable, int speed)
  1486. {
  1487. bool link_up = false;
  1488. int ret = 0;
  1489. if (!phydev->drv)
  1490. return -EIO;
  1491. mutex_lock(&phydev->lock);
  1492. if (enable && phydev->loopback_enabled) {
  1493. ret = -EBUSY;
  1494. goto out;
  1495. }
  1496. if (!enable && !phydev->loopback_enabled) {
  1497. ret = -EINVAL;
  1498. goto out;
  1499. }
  1500. if (enable) {
  1501. /*
  1502. * Link up is signaled with a defined speed. If speed changes,
  1503. * then first link down and after that link up needs to be
  1504. * signaled.
  1505. */
  1506. if (phydev->link && phydev->state == PHY_RUNNING) {
  1507. /* link is up and signaled */
  1508. if (speed && phydev->speed != speed) {
  1509. /* signal link down and up for new speed */
  1510. phydev->link = false;
  1511. phydev->state = PHY_NOLINK;
  1512. phy_link_down(phydev);
  1513. link_up = true;
  1514. }
  1515. } else {
  1516. /* link is not signaled */
  1517. if (speed) {
  1518. /* signal link up for new speed */
  1519. link_up = true;
  1520. }
  1521. }
  1522. }
  1523. if (phydev->drv->set_loopback)
  1524. ret = phydev->drv->set_loopback(phydev, enable, speed);
  1525. else
  1526. ret = genphy_loopback(phydev, enable, speed);
  1527. if (ret) {
  1528. if (enable) {
  1529. /* try to restore link if enabling loopback fails */
  1530. if (phydev->drv->set_loopback)
  1531. phydev->drv->set_loopback(phydev, false, 0);
  1532. else
  1533. genphy_loopback(phydev, false, 0);
  1534. }
  1535. goto out;
  1536. }
  1537. if (link_up) {
  1538. phydev->link = true;
  1539. phydev->state = PHY_RUNNING;
  1540. phy_link_up(phydev);
  1541. }
  1542. phydev->loopback_enabled = enable;
  1543. out:
  1544. mutex_unlock(&phydev->lock);
  1545. return ret;
  1546. }
  1547. EXPORT_SYMBOL(phy_loopback);
  1548. /**
  1549. * phy_eee_tx_clock_stop_capable() - indicate whether the MAC can stop tx clock
  1550. * @phydev: target phy_device struct
  1551. *
  1552. * Indicate whether the MAC can disable the transmit xMII clock while in LPI
  1553. * state. Returns 1 if the MAC may stop the transmit clock, 0 if the MAC must
  1554. * not stop the transmit clock, or negative error.
  1555. */
  1556. int phy_eee_tx_clock_stop_capable(struct phy_device *phydev)
  1557. {
  1558. int stat1;
  1559. stat1 = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1);
  1560. if (stat1 < 0)
  1561. return stat1;
  1562. return !!(stat1 & MDIO_PCS_STAT1_CLKSTOP_CAP);
  1563. }
  1564. EXPORT_SYMBOL_GPL(phy_eee_tx_clock_stop_capable);
  1565. /**
  1566. * phy_eee_rx_clock_stop() - configure PHY receive clock in LPI
  1567. * @phydev: target phy_device struct
  1568. * @clk_stop_enable: flag to indicate whether the clock can be stopped
  1569. *
  1570. * Configure whether the PHY can disable its receive clock during LPI mode,
  1571. * See IEEE 802.3 sections 22.2.2.2, 35.2.2.10, and 45.2.3.1.4.
  1572. *
  1573. * Returns: 0 or negative error.
  1574. */
  1575. int phy_eee_rx_clock_stop(struct phy_device *phydev, bool clk_stop_enable)
  1576. {
  1577. /* Configure the PHY to stop receiving xMII
  1578. * clock while it is signaling LPI.
  1579. */
  1580. return phy_modify_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
  1581. MDIO_PCS_CTRL1_CLKSTOP_EN,
  1582. clk_stop_enable ? MDIO_PCS_CTRL1_CLKSTOP_EN : 0);
  1583. }
  1584. EXPORT_SYMBOL_GPL(phy_eee_rx_clock_stop);
  1585. /**
  1586. * phy_init_eee - init and check the EEE feature
  1587. * @phydev: target phy_device struct
  1588. * @clk_stop_enable: PHY may stop the clock during LPI
  1589. *
  1590. * Description: it checks if the Energy-Efficient Ethernet (EEE)
  1591. * is supported by looking at the MMD registers 3.20 and 7.60/61
  1592. * and it programs the MMD register 3.0 setting the "Clock stop enable"
  1593. * bit if required.
  1594. */
  1595. int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
  1596. {
  1597. int ret;
  1598. if (!phydev->drv)
  1599. return -EIO;
  1600. ret = genphy_c45_eee_is_active(phydev, NULL);
  1601. if (ret < 0)
  1602. return ret;
  1603. if (!ret)
  1604. return -EPROTONOSUPPORT;
  1605. if (clk_stop_enable)
  1606. ret = phy_eee_rx_clock_stop(phydev, true);
  1607. return ret < 0 ? ret : 0;
  1608. }
  1609. EXPORT_SYMBOL(phy_init_eee);
  1610. /**
  1611. * phy_get_eee_err - report the EEE wake error count
  1612. * @phydev: target phy_device struct
  1613. *
  1614. * Description: it is to report the number of time where the PHY
  1615. * failed to complete its normal wake sequence.
  1616. */
  1617. int phy_get_eee_err(struct phy_device *phydev)
  1618. {
  1619. int ret;
  1620. if (!phydev->drv)
  1621. return -EIO;
  1622. mutex_lock(&phydev->lock);
  1623. ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
  1624. mutex_unlock(&phydev->lock);
  1625. return ret;
  1626. }
  1627. EXPORT_SYMBOL(phy_get_eee_err);
  1628. /**
  1629. * phy_ethtool_get_eee - get EEE supported and status
  1630. * @phydev: target phy_device struct
  1631. * @data: ethtool_keee data
  1632. *
  1633. * Description: get the current EEE settings, filling in all members of
  1634. * @data.
  1635. */
  1636. int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_keee *data)
  1637. {
  1638. int ret;
  1639. if (!phydev->drv)
  1640. return -EIO;
  1641. mutex_lock(&phydev->lock);
  1642. ret = genphy_c45_ethtool_get_eee(phydev, data);
  1643. eeecfg_to_eee(data, &phydev->eee_cfg);
  1644. mutex_unlock(&phydev->lock);
  1645. return ret;
  1646. }
  1647. EXPORT_SYMBOL(phy_ethtool_get_eee);
  1648. /**
  1649. * phy_ethtool_set_eee_noneg - Adjusts MAC LPI configuration without PHY
  1650. * renegotiation
  1651. * @phydev: pointer to the target PHY device structure
  1652. * @old_cfg: pointer to the eee_config structure containing the old EEE settings
  1653. *
  1654. * This function updates the Energy Efficient Ethernet (EEE) configuration
  1655. * for cases where only the MAC's Low Power Idle (LPI) configuration changes,
  1656. * without triggering PHY renegotiation. It ensures that the MAC is properly
  1657. * informed of the new LPI settings by cycling the link down and up, which
  1658. * is necessary for the MAC to adopt the new configuration. This adjustment
  1659. * is done only if there is a change in the tx_lpi_enabled or tx_lpi_timer
  1660. * configuration.
  1661. */
  1662. static void phy_ethtool_set_eee_noneg(struct phy_device *phydev,
  1663. const struct eee_config *old_cfg)
  1664. {
  1665. bool enable_tx_lpi;
  1666. if (!phydev->link)
  1667. return;
  1668. enable_tx_lpi = phydev->eee_cfg.tx_lpi_enabled && phydev->eee_active;
  1669. if (phydev->enable_tx_lpi != enable_tx_lpi ||
  1670. phydev->eee_cfg.tx_lpi_timer != old_cfg->tx_lpi_timer) {
  1671. phydev->enable_tx_lpi = false;
  1672. phydev->link = false;
  1673. phy_link_down(phydev);
  1674. phydev->enable_tx_lpi = enable_tx_lpi;
  1675. phydev->link = true;
  1676. phy_link_up(phydev);
  1677. }
  1678. }
  1679. /**
  1680. * phy_ethtool_set_eee - set EEE supported and status
  1681. * @phydev: target phy_device struct
  1682. * @data: ethtool_keee data
  1683. *
  1684. * Description: it is to program the Advertisement EEE register.
  1685. */
  1686. int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_keee *data)
  1687. {
  1688. struct eee_config old_cfg;
  1689. int ret;
  1690. if (!phydev->drv)
  1691. return -EIO;
  1692. mutex_lock(&phydev->lock);
  1693. old_cfg = phydev->eee_cfg;
  1694. eee_to_eeecfg(&phydev->eee_cfg, data);
  1695. ret = genphy_c45_ethtool_set_eee(phydev, data);
  1696. if (ret == 0)
  1697. phy_ethtool_set_eee_noneg(phydev, &old_cfg);
  1698. else if (ret < 0)
  1699. phydev->eee_cfg = old_cfg;
  1700. mutex_unlock(&phydev->lock);
  1701. return ret < 0 ? ret : 0;
  1702. }
  1703. EXPORT_SYMBOL(phy_ethtool_set_eee);
  1704. /**
  1705. * phy_ethtool_set_wol - Configure Wake On LAN
  1706. *
  1707. * @phydev: target phy_device struct
  1708. * @wol: Configuration requested
  1709. */
  1710. int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1711. {
  1712. int ret;
  1713. if (phydev->drv && phydev->drv->set_wol) {
  1714. mutex_lock(&phydev->lock);
  1715. ret = phydev->drv->set_wol(phydev, wol);
  1716. mutex_unlock(&phydev->lock);
  1717. return ret;
  1718. }
  1719. return -EOPNOTSUPP;
  1720. }
  1721. EXPORT_SYMBOL(phy_ethtool_set_wol);
  1722. /**
  1723. * phy_ethtool_get_wol - Get the current Wake On LAN configuration
  1724. *
  1725. * @phydev: target phy_device struct
  1726. * @wol: Store the current configuration here
  1727. */
  1728. void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1729. {
  1730. if (phydev->drv && phydev->drv->get_wol) {
  1731. mutex_lock(&phydev->lock);
  1732. phydev->drv->get_wol(phydev, wol);
  1733. mutex_unlock(&phydev->lock);
  1734. }
  1735. }
  1736. EXPORT_SYMBOL(phy_ethtool_get_wol);
  1737. int phy_ethtool_get_link_ksettings(struct net_device *ndev,
  1738. struct ethtool_link_ksettings *cmd)
  1739. {
  1740. struct phy_device *phydev = ndev->phydev;
  1741. if (!phydev)
  1742. return -ENODEV;
  1743. phy_ethtool_ksettings_get(phydev, cmd);
  1744. return 0;
  1745. }
  1746. EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
  1747. int phy_ethtool_set_link_ksettings(struct net_device *ndev,
  1748. const struct ethtool_link_ksettings *cmd)
  1749. {
  1750. struct phy_device *phydev = ndev->phydev;
  1751. if (!phydev)
  1752. return -ENODEV;
  1753. return phy_ethtool_ksettings_set(phydev, cmd);
  1754. }
  1755. EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
  1756. /**
  1757. * phy_ethtool_nway_reset - Restart auto negotiation
  1758. * @ndev: Network device to restart autoneg for
  1759. */
  1760. int phy_ethtool_nway_reset(struct net_device *ndev)
  1761. {
  1762. struct phy_device *phydev = ndev->phydev;
  1763. int ret;
  1764. if (!phydev)
  1765. return -ENODEV;
  1766. if (!phydev->drv)
  1767. return -EIO;
  1768. mutex_lock(&phydev->lock);
  1769. ret = phy_restart_aneg(phydev);
  1770. mutex_unlock(&phydev->lock);
  1771. return ret;
  1772. }
  1773. EXPORT_SYMBOL(phy_ethtool_nway_reset);