phy.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. /* Framework for configuring and reading PHY devices
  2. * Based on code in sungem_phy.c and gianfar_phy.c
  3. *
  4. * Author: Andy Fleming
  5. *
  6. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  7. * Copyright (c) 2006, 2007 Maciej W. Rozycki
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/errno.h>
  19. #include <linux/unistd.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/delay.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/mm.h>
  26. #include <linux/module.h>
  27. #include <linux/mii.h>
  28. #include <linux/ethtool.h>
  29. #include <linux/phy.h>
  30. #include <linux/phy_led_triggers.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/mdio.h>
  33. #include <linux/io.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/atomic.h>
  36. #include <asm/irq.h>
  37. #define PHY_STATE_STR(_state) \
  38. case PHY_##_state: \
  39. return __stringify(_state); \
  40. static const char *phy_state_to_str(enum phy_state st)
  41. {
  42. switch (st) {
  43. PHY_STATE_STR(DOWN)
  44. PHY_STATE_STR(STARTING)
  45. PHY_STATE_STR(READY)
  46. PHY_STATE_STR(PENDING)
  47. PHY_STATE_STR(UP)
  48. PHY_STATE_STR(AN)
  49. PHY_STATE_STR(RUNNING)
  50. PHY_STATE_STR(NOLINK)
  51. PHY_STATE_STR(FORCING)
  52. PHY_STATE_STR(CHANGELINK)
  53. PHY_STATE_STR(HALTED)
  54. PHY_STATE_STR(RESUMING)
  55. }
  56. return NULL;
  57. }
  58. /**
  59. * phy_print_status - Convenience function to print out the current phy status
  60. * @phydev: the phy_device struct
  61. */
  62. void phy_print_status(struct phy_device *phydev)
  63. {
  64. if (phydev->link) {
  65. netdev_info(phydev->attached_dev,
  66. "Link is Up - %s/%s - flow control %s\n",
  67. phy_speed_to_str(phydev->speed),
  68. phy_duplex_to_str(phydev->duplex),
  69. phydev->pause ? "rx/tx" : "off");
  70. } else {
  71. netdev_info(phydev->attached_dev, "Link is Down\n");
  72. }
  73. }
  74. EXPORT_SYMBOL(phy_print_status);
  75. /**
  76. * phy_clear_interrupt - Ack the phy device's interrupt
  77. * @phydev: the phy_device struct
  78. *
  79. * If the @phydev driver has an ack_interrupt function, call it to
  80. * ack and clear the phy device's interrupt.
  81. *
  82. * Returns 0 on success or < 0 on error.
  83. */
  84. static int phy_clear_interrupt(struct phy_device *phydev)
  85. {
  86. if (phydev->drv->ack_interrupt)
  87. return phydev->drv->ack_interrupt(phydev);
  88. return 0;
  89. }
  90. /**
  91. * phy_config_interrupt - configure the PHY device for the requested interrupts
  92. * @phydev: the phy_device struct
  93. * @interrupts: interrupt flags to configure for this @phydev
  94. *
  95. * Returns 0 on success or < 0 on error.
  96. */
  97. static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
  98. {
  99. phydev->interrupts = interrupts;
  100. if (phydev->drv->config_intr)
  101. return phydev->drv->config_intr(phydev);
  102. return 0;
  103. }
  104. /**
  105. * phy_restart_aneg - restart auto-negotiation
  106. * @phydev: target phy_device struct
  107. *
  108. * Restart the autonegotiation on @phydev. Returns >= 0 on success or
  109. * negative errno on error.
  110. */
  111. int phy_restart_aneg(struct phy_device *phydev)
  112. {
  113. int ret;
  114. if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
  115. ret = genphy_c45_restart_aneg(phydev);
  116. else
  117. ret = genphy_restart_aneg(phydev);
  118. return ret;
  119. }
  120. EXPORT_SYMBOL_GPL(phy_restart_aneg);
  121. /**
  122. * phy_aneg_done - return auto-negotiation status
  123. * @phydev: target phy_device struct
  124. *
  125. * Description: Return the auto-negotiation status from this @phydev
  126. * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
  127. * is still pending.
  128. */
  129. int phy_aneg_done(struct phy_device *phydev)
  130. {
  131. if (phydev->drv && phydev->drv->aneg_done)
  132. return phydev->drv->aneg_done(phydev);
  133. /* Avoid genphy_aneg_done() if the Clause 45 PHY does not
  134. * implement Clause 22 registers
  135. */
  136. if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
  137. return -EINVAL;
  138. return genphy_aneg_done(phydev);
  139. }
  140. EXPORT_SYMBOL(phy_aneg_done);
  141. /**
  142. * phy_find_valid - find a PHY setting that matches the requested parameters
  143. * @speed: desired speed
  144. * @duplex: desired duplex
  145. * @supported: mask of supported link modes
  146. *
  147. * Locate a supported phy setting that is, in priority order:
  148. * - an exact match for the specified speed and duplex mode
  149. * - a match for the specified speed, or slower speed
  150. * - the slowest supported speed
  151. * Returns the matched phy_setting entry, or %NULL if no supported phy
  152. * settings were found.
  153. */
  154. static const struct phy_setting *
  155. phy_find_valid(int speed, int duplex, u32 supported)
  156. {
  157. unsigned long mask = supported;
  158. return phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, false);
  159. }
  160. /**
  161. * phy_supported_speeds - return all speeds currently supported by a phy device
  162. * @phy: The phy device to return supported speeds of.
  163. * @speeds: buffer to store supported speeds in.
  164. * @size: size of speeds buffer.
  165. *
  166. * Description: Returns the number of supported speeds, and fills the speeds
  167. * buffer with the supported speeds. If speeds buffer is too small to contain
  168. * all currently supported speeds, will return as many speeds as can fit.
  169. */
  170. unsigned int phy_supported_speeds(struct phy_device *phy,
  171. unsigned int *speeds,
  172. unsigned int size)
  173. {
  174. unsigned long supported = phy->supported;
  175. return phy_speeds(speeds, size, &supported, BITS_PER_LONG);
  176. }
  177. /**
  178. * phy_check_valid - check if there is a valid PHY setting which matches
  179. * speed, duplex, and feature mask
  180. * @speed: speed to match
  181. * @duplex: duplex to match
  182. * @features: A mask of the valid settings
  183. *
  184. * Description: Returns true if there is a valid setting, false otherwise.
  185. */
  186. static inline bool phy_check_valid(int speed, int duplex, u32 features)
  187. {
  188. unsigned long mask = features;
  189. return !!phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, true);
  190. }
  191. /**
  192. * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
  193. * @phydev: the target phy_device struct
  194. *
  195. * Description: Make sure the PHY is set to supported speeds and
  196. * duplexes. Drop down by one in this order: 1000/FULL,
  197. * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
  198. */
  199. static void phy_sanitize_settings(struct phy_device *phydev)
  200. {
  201. const struct phy_setting *setting;
  202. u32 features = phydev->supported;
  203. /* Sanitize settings based on PHY capabilities */
  204. if ((features & SUPPORTED_Autoneg) == 0)
  205. phydev->autoneg = AUTONEG_DISABLE;
  206. setting = phy_find_valid(phydev->speed, phydev->duplex, features);
  207. if (setting) {
  208. phydev->speed = setting->speed;
  209. phydev->duplex = setting->duplex;
  210. } else {
  211. /* We failed to find anything (no supported speeds?) */
  212. phydev->speed = SPEED_UNKNOWN;
  213. phydev->duplex = DUPLEX_UNKNOWN;
  214. }
  215. }
  216. /**
  217. * phy_ethtool_sset - generic ethtool sset function, handles all the details
  218. * @phydev: target phy_device struct
  219. * @cmd: ethtool_cmd
  220. *
  221. * A few notes about parameter checking:
  222. *
  223. * - We don't set port or transceiver, so we don't care what they
  224. * were set to.
  225. * - phy_start_aneg() will make sure forced settings are sane, and
  226. * choose the next best ones from the ones selected, so we don't
  227. * care if ethtool tries to give us bad values.
  228. */
  229. int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
  230. {
  231. u32 speed = ethtool_cmd_speed(cmd);
  232. if (cmd->phy_address != phydev->mdio.addr)
  233. return -EINVAL;
  234. /* We make sure that we don't pass unsupported values in to the PHY */
  235. cmd->advertising &= phydev->supported;
  236. /* Verify the settings we care about. */
  237. if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
  238. return -EINVAL;
  239. if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
  240. return -EINVAL;
  241. if (cmd->autoneg == AUTONEG_DISABLE &&
  242. ((speed != SPEED_1000 &&
  243. speed != SPEED_100 &&
  244. speed != SPEED_10) ||
  245. (cmd->duplex != DUPLEX_HALF &&
  246. cmd->duplex != DUPLEX_FULL)))
  247. return -EINVAL;
  248. phydev->autoneg = cmd->autoneg;
  249. phydev->speed = speed;
  250. phydev->advertising = cmd->advertising;
  251. if (AUTONEG_ENABLE == cmd->autoneg)
  252. phydev->advertising |= ADVERTISED_Autoneg;
  253. else
  254. phydev->advertising &= ~ADVERTISED_Autoneg;
  255. phydev->duplex = cmd->duplex;
  256. phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl;
  257. /* Restart the PHY */
  258. phy_start_aneg(phydev);
  259. return 0;
  260. }
  261. EXPORT_SYMBOL(phy_ethtool_sset);
  262. int phy_ethtool_ksettings_set(struct phy_device *phydev,
  263. const struct ethtool_link_ksettings *cmd)
  264. {
  265. u8 autoneg = cmd->base.autoneg;
  266. u8 duplex = cmd->base.duplex;
  267. u32 speed = cmd->base.speed;
  268. u32 advertising;
  269. if (cmd->base.phy_address != phydev->mdio.addr)
  270. return -EINVAL;
  271. ethtool_convert_link_mode_to_legacy_u32(&advertising,
  272. cmd->link_modes.advertising);
  273. /* We make sure that we don't pass unsupported values in to the PHY */
  274. advertising &= phydev->supported;
  275. /* Verify the settings we care about. */
  276. if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
  277. return -EINVAL;
  278. if (autoneg == AUTONEG_ENABLE && advertising == 0)
  279. return -EINVAL;
  280. if (autoneg == AUTONEG_DISABLE &&
  281. ((speed != SPEED_1000 &&
  282. speed != SPEED_100 &&
  283. speed != SPEED_10) ||
  284. (duplex != DUPLEX_HALF &&
  285. duplex != DUPLEX_FULL)))
  286. return -EINVAL;
  287. phydev->autoneg = autoneg;
  288. phydev->speed = speed;
  289. phydev->advertising = advertising;
  290. if (autoneg == AUTONEG_ENABLE)
  291. phydev->advertising |= ADVERTISED_Autoneg;
  292. else
  293. phydev->advertising &= ~ADVERTISED_Autoneg;
  294. phydev->duplex = duplex;
  295. phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
  296. /* Restart the PHY */
  297. phy_start_aneg(phydev);
  298. return 0;
  299. }
  300. EXPORT_SYMBOL(phy_ethtool_ksettings_set);
  301. void phy_ethtool_ksettings_get(struct phy_device *phydev,
  302. struct ethtool_link_ksettings *cmd)
  303. {
  304. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  305. phydev->supported);
  306. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  307. phydev->advertising);
  308. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
  309. phydev->lp_advertising);
  310. cmd->base.speed = phydev->speed;
  311. cmd->base.duplex = phydev->duplex;
  312. if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
  313. cmd->base.port = PORT_BNC;
  314. else
  315. cmd->base.port = PORT_MII;
  316. cmd->base.transceiver = phy_is_internal(phydev) ?
  317. XCVR_INTERNAL : XCVR_EXTERNAL;
  318. cmd->base.phy_address = phydev->mdio.addr;
  319. cmd->base.autoneg = phydev->autoneg;
  320. cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
  321. cmd->base.eth_tp_mdix = phydev->mdix;
  322. }
  323. EXPORT_SYMBOL(phy_ethtool_ksettings_get);
  324. /**
  325. * phy_mii_ioctl - generic PHY MII ioctl interface
  326. * @phydev: the phy_device struct
  327. * @ifr: &struct ifreq for socket ioctl's
  328. * @cmd: ioctl cmd to execute
  329. *
  330. * Note that this function is currently incompatible with the
  331. * PHYCONTROL layer. It changes registers without regard to
  332. * current state. Use at own risk.
  333. */
  334. int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
  335. {
  336. struct mii_ioctl_data *mii_data = if_mii(ifr);
  337. u16 val = mii_data->val_in;
  338. bool change_autoneg = false;
  339. switch (cmd) {
  340. case SIOCGMIIPHY:
  341. mii_data->phy_id = phydev->mdio.addr;
  342. /* fall through */
  343. case SIOCGMIIREG:
  344. mii_data->val_out = mdiobus_read(phydev->mdio.bus,
  345. mii_data->phy_id,
  346. mii_data->reg_num);
  347. return 0;
  348. case SIOCSMIIREG:
  349. if (mii_data->phy_id == phydev->mdio.addr) {
  350. switch (mii_data->reg_num) {
  351. case MII_BMCR:
  352. if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
  353. if (phydev->autoneg == AUTONEG_ENABLE)
  354. change_autoneg = true;
  355. phydev->autoneg = AUTONEG_DISABLE;
  356. if (val & BMCR_FULLDPLX)
  357. phydev->duplex = DUPLEX_FULL;
  358. else
  359. phydev->duplex = DUPLEX_HALF;
  360. if (val & BMCR_SPEED1000)
  361. phydev->speed = SPEED_1000;
  362. else if (val & BMCR_SPEED100)
  363. phydev->speed = SPEED_100;
  364. else phydev->speed = SPEED_10;
  365. }
  366. else {
  367. if (phydev->autoneg == AUTONEG_DISABLE)
  368. change_autoneg = true;
  369. phydev->autoneg = AUTONEG_ENABLE;
  370. }
  371. break;
  372. case MII_ADVERTISE:
  373. phydev->advertising = mii_adv_to_ethtool_adv_t(val);
  374. change_autoneg = true;
  375. break;
  376. default:
  377. /* do nothing */
  378. break;
  379. }
  380. }
  381. mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
  382. mii_data->reg_num, val);
  383. if (mii_data->phy_id == phydev->mdio.addr &&
  384. mii_data->reg_num == MII_BMCR &&
  385. val & BMCR_RESET)
  386. return phy_init_hw(phydev);
  387. if (change_autoneg)
  388. return phy_start_aneg(phydev);
  389. return 0;
  390. case SIOCSHWTSTAMP:
  391. if (phydev->drv && phydev->drv->hwtstamp)
  392. return phydev->drv->hwtstamp(phydev, ifr);
  393. /* fall through */
  394. default:
  395. return -EOPNOTSUPP;
  396. }
  397. }
  398. EXPORT_SYMBOL(phy_mii_ioctl);
  399. /**
  400. * phy_start_aneg_priv - start auto-negotiation for this PHY device
  401. * @phydev: the phy_device struct
  402. * @sync: indicate whether we should wait for the workqueue cancelation
  403. *
  404. * Description: Sanitizes the settings (if we're not autonegotiating
  405. * them), and then calls the driver's config_aneg function.
  406. * If the PHYCONTROL Layer is operating, we change the state to
  407. * reflect the beginning of Auto-negotiation or forcing.
  408. */
  409. static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
  410. {
  411. bool trigger = 0;
  412. int err;
  413. if (!phydev->drv)
  414. return -EIO;
  415. mutex_lock(&phydev->lock);
  416. if (AUTONEG_DISABLE == phydev->autoneg)
  417. phy_sanitize_settings(phydev);
  418. /* Invalidate LP advertising flags */
  419. phydev->lp_advertising = 0;
  420. err = phydev->drv->config_aneg(phydev);
  421. if (err < 0)
  422. goto out_unlock;
  423. if (phydev->state != PHY_HALTED) {
  424. if (AUTONEG_ENABLE == phydev->autoneg) {
  425. phydev->state = PHY_AN;
  426. phydev->link_timeout = PHY_AN_TIMEOUT;
  427. } else {
  428. phydev->state = PHY_FORCING;
  429. phydev->link_timeout = PHY_FORCE_TIMEOUT;
  430. }
  431. }
  432. /* Re-schedule a PHY state machine to check PHY status because
  433. * negotiation may already be done and aneg interrupt may not be
  434. * generated.
  435. */
  436. if (phy_interrupt_is_valid(phydev) && (phydev->state == PHY_AN)) {
  437. err = phy_aneg_done(phydev);
  438. if (err > 0) {
  439. trigger = true;
  440. err = 0;
  441. }
  442. }
  443. out_unlock:
  444. mutex_unlock(&phydev->lock);
  445. if (trigger)
  446. phy_trigger_machine(phydev, sync);
  447. return err;
  448. }
  449. /**
  450. * phy_start_aneg - start auto-negotiation for this PHY device
  451. * @phydev: the phy_device struct
  452. *
  453. * Description: Sanitizes the settings (if we're not autonegotiating
  454. * them), and then calls the driver's config_aneg function.
  455. * If the PHYCONTROL Layer is operating, we change the state to
  456. * reflect the beginning of Auto-negotiation or forcing.
  457. */
  458. int phy_start_aneg(struct phy_device *phydev)
  459. {
  460. return phy_start_aneg_priv(phydev, true);
  461. }
  462. EXPORT_SYMBOL(phy_start_aneg);
  463. /**
  464. * phy_start_machine - start PHY state machine tracking
  465. * @phydev: the phy_device struct
  466. *
  467. * Description: The PHY infrastructure can run a state machine
  468. * which tracks whether the PHY is starting up, negotiating,
  469. * etc. This function starts the delayed workqueue which tracks
  470. * the state of the PHY. If you want to maintain your own state machine,
  471. * do not call this function.
  472. */
  473. void phy_start_machine(struct phy_device *phydev)
  474. {
  475. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
  476. }
  477. EXPORT_SYMBOL_GPL(phy_start_machine);
  478. /**
  479. * phy_trigger_machine - trigger the state machine to run
  480. *
  481. * @phydev: the phy_device struct
  482. * @sync: indicate whether we should wait for the workqueue cancelation
  483. *
  484. * Description: There has been a change in state which requires that the
  485. * state machine runs.
  486. */
  487. void phy_trigger_machine(struct phy_device *phydev, bool sync)
  488. {
  489. if (sync)
  490. cancel_delayed_work_sync(&phydev->state_queue);
  491. else
  492. cancel_delayed_work(&phydev->state_queue);
  493. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
  494. }
  495. /**
  496. * phy_stop_machine - stop the PHY state machine tracking
  497. * @phydev: target phy_device struct
  498. *
  499. * Description: Stops the state machine delayed workqueue, sets the
  500. * state to UP (unless it wasn't up yet). This function must be
  501. * called BEFORE phy_detach.
  502. */
  503. void phy_stop_machine(struct phy_device *phydev)
  504. {
  505. cancel_delayed_work_sync(&phydev->state_queue);
  506. mutex_lock(&phydev->lock);
  507. if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
  508. phydev->state = PHY_UP;
  509. mutex_unlock(&phydev->lock);
  510. }
  511. /**
  512. * phy_error - enter HALTED state for this PHY device
  513. * @phydev: target phy_device struct
  514. *
  515. * Moves the PHY to the HALTED state in response to a read
  516. * or write error, and tells the controller the link is down.
  517. * Must not be called from interrupt context, or while the
  518. * phydev->lock is held.
  519. */
  520. static void phy_error(struct phy_device *phydev)
  521. {
  522. mutex_lock(&phydev->lock);
  523. phydev->state = PHY_HALTED;
  524. mutex_unlock(&phydev->lock);
  525. phy_trigger_machine(phydev, false);
  526. }
  527. /**
  528. * phy_interrupt - PHY interrupt handler
  529. * @irq: interrupt line
  530. * @phy_dat: phy_device pointer
  531. *
  532. * Description: When a PHY interrupt occurs, the handler disables
  533. * interrupts, and uses phy_change to handle the interrupt.
  534. */
  535. static irqreturn_t phy_interrupt(int irq, void *phy_dat)
  536. {
  537. struct phy_device *phydev = phy_dat;
  538. if (PHY_HALTED == phydev->state)
  539. return IRQ_NONE; /* It can't be ours. */
  540. disable_irq_nosync(irq);
  541. atomic_inc(&phydev->irq_disable);
  542. phy_change(phydev);
  543. return IRQ_HANDLED;
  544. }
  545. /**
  546. * phy_enable_interrupts - Enable the interrupts from the PHY side
  547. * @phydev: target phy_device struct
  548. */
  549. static int phy_enable_interrupts(struct phy_device *phydev)
  550. {
  551. int err = phy_clear_interrupt(phydev);
  552. if (err < 0)
  553. return err;
  554. return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  555. }
  556. /**
  557. * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
  558. * @phydev: target phy_device struct
  559. */
  560. static int phy_disable_interrupts(struct phy_device *phydev)
  561. {
  562. int err;
  563. /* Disable PHY interrupts */
  564. err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  565. if (err)
  566. goto phy_err;
  567. /* Clear the interrupt */
  568. err = phy_clear_interrupt(phydev);
  569. if (err)
  570. goto phy_err;
  571. return 0;
  572. phy_err:
  573. phy_error(phydev);
  574. return err;
  575. }
  576. /**
  577. * phy_start_interrupts - request and enable interrupts for a PHY device
  578. * @phydev: target phy_device struct
  579. *
  580. * Description: Request the interrupt for the given PHY.
  581. * If this fails, then we set irq to PHY_POLL.
  582. * Otherwise, we enable the interrupts in the PHY.
  583. * This should only be called with a valid IRQ number.
  584. * Returns 0 on success or < 0 on error.
  585. */
  586. int phy_start_interrupts(struct phy_device *phydev)
  587. {
  588. atomic_set(&phydev->irq_disable, 0);
  589. if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
  590. IRQF_ONESHOT | IRQF_SHARED,
  591. phydev_name(phydev), phydev) < 0) {
  592. pr_warn("%s: Can't get IRQ %d (PHY)\n",
  593. phydev->mdio.bus->name, phydev->irq);
  594. phydev->irq = PHY_POLL;
  595. return 0;
  596. }
  597. return phy_enable_interrupts(phydev);
  598. }
  599. EXPORT_SYMBOL(phy_start_interrupts);
  600. /**
  601. * phy_stop_interrupts - disable interrupts from a PHY device
  602. * @phydev: target phy_device struct
  603. */
  604. int phy_stop_interrupts(struct phy_device *phydev)
  605. {
  606. int err = phy_disable_interrupts(phydev);
  607. if (err)
  608. phy_error(phydev);
  609. free_irq(phydev->irq, phydev);
  610. /* If work indeed has been cancelled, disable_irq() will have
  611. * been left unbalanced from phy_interrupt() and enable_irq()
  612. * has to be called so that other devices on the line work.
  613. */
  614. while (atomic_dec_return(&phydev->irq_disable) >= 0)
  615. enable_irq(phydev->irq);
  616. return err;
  617. }
  618. EXPORT_SYMBOL(phy_stop_interrupts);
  619. /**
  620. * phy_change - Called by the phy_interrupt to handle PHY changes
  621. * @phydev: phy_device struct that interrupted
  622. */
  623. void phy_change(struct phy_device *phydev)
  624. {
  625. if (phy_interrupt_is_valid(phydev)) {
  626. if (phydev->drv->did_interrupt &&
  627. !phydev->drv->did_interrupt(phydev))
  628. goto ignore;
  629. if (phy_disable_interrupts(phydev))
  630. goto phy_err;
  631. }
  632. mutex_lock(&phydev->lock);
  633. if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
  634. phydev->state = PHY_CHANGELINK;
  635. mutex_unlock(&phydev->lock);
  636. if (phy_interrupt_is_valid(phydev)) {
  637. atomic_dec(&phydev->irq_disable);
  638. enable_irq(phydev->irq);
  639. /* Reenable interrupts */
  640. if (PHY_HALTED != phydev->state &&
  641. phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
  642. goto irq_enable_err;
  643. }
  644. /* reschedule state queue work to run as soon as possible */
  645. phy_trigger_machine(phydev, true);
  646. return;
  647. ignore:
  648. atomic_dec(&phydev->irq_disable);
  649. enable_irq(phydev->irq);
  650. return;
  651. irq_enable_err:
  652. disable_irq(phydev->irq);
  653. atomic_inc(&phydev->irq_disable);
  654. phy_err:
  655. phy_error(phydev);
  656. }
  657. /**
  658. * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
  659. * @work: work_struct that describes the work to be done
  660. */
  661. void phy_change_work(struct work_struct *work)
  662. {
  663. struct phy_device *phydev =
  664. container_of(work, struct phy_device, phy_queue);
  665. phy_change(phydev);
  666. }
  667. /**
  668. * phy_stop - Bring down the PHY link, and stop checking the status
  669. * @phydev: target phy_device struct
  670. */
  671. void phy_stop(struct phy_device *phydev)
  672. {
  673. mutex_lock(&phydev->lock);
  674. if (PHY_HALTED == phydev->state)
  675. goto out_unlock;
  676. if (phy_interrupt_is_valid(phydev)) {
  677. /* Disable PHY Interrupts */
  678. phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  679. /* Clear any pending interrupts */
  680. phy_clear_interrupt(phydev);
  681. }
  682. phydev->state = PHY_HALTED;
  683. out_unlock:
  684. mutex_unlock(&phydev->lock);
  685. /* Cannot call flush_scheduled_work() here as desired because
  686. * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
  687. * will not reenable interrupts.
  688. */
  689. }
  690. EXPORT_SYMBOL(phy_stop);
  691. /**
  692. * phy_start - start or restart a PHY device
  693. * @phydev: target phy_device struct
  694. *
  695. * Description: Indicates the attached device's readiness to
  696. * handle PHY-related work. Used during startup to start the
  697. * PHY, and after a call to phy_stop() to resume operation.
  698. * Also used to indicate the MDIO bus has cleared an error
  699. * condition.
  700. */
  701. void phy_start(struct phy_device *phydev)
  702. {
  703. bool do_resume = false;
  704. int err = 0;
  705. mutex_lock(&phydev->lock);
  706. switch (phydev->state) {
  707. case PHY_STARTING:
  708. phydev->state = PHY_PENDING;
  709. break;
  710. case PHY_READY:
  711. phydev->state = PHY_UP;
  712. break;
  713. case PHY_HALTED:
  714. /* make sure interrupts are re-enabled for the PHY */
  715. if (phydev->irq != PHY_POLL) {
  716. err = phy_enable_interrupts(phydev);
  717. if (err < 0)
  718. break;
  719. }
  720. phydev->state = PHY_RESUMING;
  721. do_resume = true;
  722. break;
  723. default:
  724. break;
  725. }
  726. mutex_unlock(&phydev->lock);
  727. /* if phy was suspended, bring the physical link up again */
  728. if (do_resume)
  729. phy_resume(phydev);
  730. phy_trigger_machine(phydev, true);
  731. }
  732. EXPORT_SYMBOL(phy_start);
  733. static void phy_link_up(struct phy_device *phydev)
  734. {
  735. phydev->phy_link_change(phydev, true, true);
  736. phy_led_trigger_change_speed(phydev);
  737. }
  738. static void phy_link_down(struct phy_device *phydev, bool do_carrier)
  739. {
  740. phydev->phy_link_change(phydev, false, do_carrier);
  741. phy_led_trigger_change_speed(phydev);
  742. }
  743. /**
  744. * phy_state_machine - Handle the state machine
  745. * @work: work_struct that describes the work to be done
  746. */
  747. void phy_state_machine(struct work_struct *work)
  748. {
  749. struct delayed_work *dwork = to_delayed_work(work);
  750. struct phy_device *phydev =
  751. container_of(dwork, struct phy_device, state_queue);
  752. bool needs_aneg = false, do_suspend = false;
  753. enum phy_state old_state;
  754. int err = 0;
  755. int old_link;
  756. mutex_lock(&phydev->lock);
  757. old_state = phydev->state;
  758. if (phydev->drv && phydev->drv->link_change_notify)
  759. phydev->drv->link_change_notify(phydev);
  760. switch (phydev->state) {
  761. case PHY_DOWN:
  762. case PHY_STARTING:
  763. case PHY_READY:
  764. case PHY_PENDING:
  765. break;
  766. case PHY_UP:
  767. needs_aneg = true;
  768. phydev->link_timeout = PHY_AN_TIMEOUT;
  769. break;
  770. case PHY_AN:
  771. err = phy_read_status(phydev);
  772. if (err < 0)
  773. break;
  774. /* If the link is down, give up on negotiation for now */
  775. if (!phydev->link) {
  776. phydev->state = PHY_NOLINK;
  777. phy_link_down(phydev, true);
  778. break;
  779. }
  780. /* Check if negotiation is done. Break if there's an error */
  781. err = phy_aneg_done(phydev);
  782. if (err < 0)
  783. break;
  784. /* If AN is done, we're running */
  785. if (err > 0) {
  786. phydev->state = PHY_RUNNING;
  787. phy_link_up(phydev);
  788. } else if (0 == phydev->link_timeout--)
  789. needs_aneg = true;
  790. break;
  791. case PHY_NOLINK:
  792. if (phy_interrupt_is_valid(phydev))
  793. break;
  794. err = phy_read_status(phydev);
  795. if (err)
  796. break;
  797. if (phydev->link) {
  798. if (AUTONEG_ENABLE == phydev->autoneg) {
  799. err = phy_aneg_done(phydev);
  800. if (err < 0)
  801. break;
  802. if (!err) {
  803. phydev->state = PHY_AN;
  804. phydev->link_timeout = PHY_AN_TIMEOUT;
  805. break;
  806. }
  807. }
  808. phydev->state = PHY_RUNNING;
  809. phy_link_up(phydev);
  810. }
  811. break;
  812. case PHY_FORCING:
  813. err = genphy_update_link(phydev);
  814. if (err)
  815. break;
  816. if (phydev->link) {
  817. phydev->state = PHY_RUNNING;
  818. phy_link_up(phydev);
  819. } else {
  820. if (0 == phydev->link_timeout--)
  821. needs_aneg = true;
  822. phy_link_down(phydev, false);
  823. }
  824. break;
  825. case PHY_RUNNING:
  826. /* Only register a CHANGE if we are polling and link changed
  827. * since latest checking.
  828. */
  829. if (phydev->irq == PHY_POLL) {
  830. old_link = phydev->link;
  831. err = phy_read_status(phydev);
  832. if (err)
  833. break;
  834. if (old_link != phydev->link)
  835. phydev->state = PHY_CHANGELINK;
  836. }
  837. /*
  838. * Failsafe: check that nobody set phydev->link=0 between two
  839. * poll cycles, otherwise we won't leave RUNNING state as long
  840. * as link remains down.
  841. */
  842. if (!phydev->link && phydev->state == PHY_RUNNING) {
  843. phydev->state = PHY_CHANGELINK;
  844. phydev_err(phydev, "no link in PHY_RUNNING\n");
  845. }
  846. break;
  847. case PHY_CHANGELINK:
  848. err = phy_read_status(phydev);
  849. if (err)
  850. break;
  851. if (phydev->link) {
  852. phydev->state = PHY_RUNNING;
  853. phy_link_up(phydev);
  854. } else {
  855. phydev->state = PHY_NOLINK;
  856. phy_link_down(phydev, true);
  857. }
  858. if (phy_interrupt_is_valid(phydev))
  859. err = phy_config_interrupt(phydev,
  860. PHY_INTERRUPT_ENABLED);
  861. break;
  862. case PHY_HALTED:
  863. if (phydev->link) {
  864. phydev->link = 0;
  865. phy_link_down(phydev, true);
  866. do_suspend = true;
  867. }
  868. break;
  869. case PHY_RESUMING:
  870. if (AUTONEG_ENABLE == phydev->autoneg) {
  871. err = phy_aneg_done(phydev);
  872. if (err < 0)
  873. break;
  874. /* err > 0 if AN is done.
  875. * Otherwise, it's 0, and we're still waiting for AN
  876. */
  877. if (err > 0) {
  878. err = phy_read_status(phydev);
  879. if (err)
  880. break;
  881. if (phydev->link) {
  882. phydev->state = PHY_RUNNING;
  883. phy_link_up(phydev);
  884. } else {
  885. phydev->state = PHY_NOLINK;
  886. phy_link_down(phydev, false);
  887. }
  888. } else {
  889. phydev->state = PHY_AN;
  890. phydev->link_timeout = PHY_AN_TIMEOUT;
  891. }
  892. } else {
  893. err = phy_read_status(phydev);
  894. if (err)
  895. break;
  896. if (phydev->link) {
  897. phydev->state = PHY_RUNNING;
  898. phy_link_up(phydev);
  899. } else {
  900. phydev->state = PHY_NOLINK;
  901. phy_link_down(phydev, false);
  902. }
  903. }
  904. break;
  905. }
  906. mutex_unlock(&phydev->lock);
  907. if (needs_aneg)
  908. err = phy_start_aneg_priv(phydev, false);
  909. else if (do_suspend)
  910. phy_suspend(phydev);
  911. if (err < 0)
  912. phy_error(phydev);
  913. if (old_state != phydev->state)
  914. phydev_dbg(phydev, "PHY state change %s -> %s\n",
  915. phy_state_to_str(old_state),
  916. phy_state_to_str(phydev->state));
  917. /* Only re-schedule a PHY state machine change if we are polling the
  918. * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
  919. * between states from phy_mac_interrupt()
  920. */
  921. if (phydev->irq == PHY_POLL)
  922. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
  923. PHY_STATE_TIME * HZ);
  924. }
  925. /**
  926. * phy_mac_interrupt - MAC says the link has changed
  927. * @phydev: phy_device struct with changed link
  928. * @new_link: Link is Up/Down.
  929. *
  930. * Description: The MAC layer is able indicate there has been a change
  931. * in the PHY link status. Set the new link status, and trigger the
  932. * state machine, work a work queue.
  933. */
  934. void phy_mac_interrupt(struct phy_device *phydev, int new_link)
  935. {
  936. phydev->link = new_link;
  937. /* Trigger a state machine change */
  938. queue_work(system_power_efficient_wq, &phydev->phy_queue);
  939. }
  940. EXPORT_SYMBOL(phy_mac_interrupt);
  941. /**
  942. * phy_init_eee - init and check the EEE feature
  943. * @phydev: target phy_device struct
  944. * @clk_stop_enable: PHY may stop the clock during LPI
  945. *
  946. * Description: it checks if the Energy-Efficient Ethernet (EEE)
  947. * is supported by looking at the MMD registers 3.20 and 7.60/61
  948. * and it programs the MMD register 3.0 setting the "Clock stop enable"
  949. * bit if required.
  950. */
  951. int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
  952. {
  953. if (!phydev->drv)
  954. return -EIO;
  955. /* According to 802.3az,the EEE is supported only in full duplex-mode.
  956. */
  957. if (phydev->duplex == DUPLEX_FULL) {
  958. int eee_lp, eee_cap, eee_adv;
  959. u32 lp, cap, adv;
  960. int status;
  961. /* Read phy status to properly get the right settings */
  962. status = phy_read_status(phydev);
  963. if (status)
  964. return status;
  965. /* First check if the EEE ability is supported */
  966. eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  967. if (eee_cap <= 0)
  968. goto eee_exit_err;
  969. cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
  970. if (!cap)
  971. goto eee_exit_err;
  972. /* Check which link settings negotiated and verify it in
  973. * the EEE advertising registers.
  974. */
  975. eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
  976. if (eee_lp <= 0)
  977. goto eee_exit_err;
  978. eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  979. if (eee_adv <= 0)
  980. goto eee_exit_err;
  981. adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
  982. lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
  983. if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
  984. goto eee_exit_err;
  985. if (clk_stop_enable) {
  986. /* Configure the PHY to stop receiving xMII
  987. * clock while it is signaling LPI.
  988. */
  989. int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
  990. if (val < 0)
  991. return val;
  992. val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
  993. phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
  994. }
  995. return 0; /* EEE supported */
  996. }
  997. eee_exit_err:
  998. return -EPROTONOSUPPORT;
  999. }
  1000. EXPORT_SYMBOL(phy_init_eee);
  1001. /**
  1002. * phy_get_eee_err - report the EEE wake error count
  1003. * @phydev: target phy_device struct
  1004. *
  1005. * Description: it is to report the number of time where the PHY
  1006. * failed to complete its normal wake sequence.
  1007. */
  1008. int phy_get_eee_err(struct phy_device *phydev)
  1009. {
  1010. if (!phydev->drv)
  1011. return -EIO;
  1012. return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
  1013. }
  1014. EXPORT_SYMBOL(phy_get_eee_err);
  1015. /**
  1016. * phy_ethtool_get_eee - get EEE supported and status
  1017. * @phydev: target phy_device struct
  1018. * @data: ethtool_eee data
  1019. *
  1020. * Description: it reportes the Supported/Advertisement/LP Advertisement
  1021. * capabilities.
  1022. */
  1023. int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
  1024. {
  1025. int val;
  1026. if (!phydev->drv)
  1027. return -EIO;
  1028. /* Get Supported EEE */
  1029. val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  1030. if (val < 0)
  1031. return val;
  1032. data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
  1033. /* Get advertisement EEE */
  1034. val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  1035. if (val < 0)
  1036. return val;
  1037. data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  1038. /* Get LP advertisement EEE */
  1039. val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
  1040. if (val < 0)
  1041. return val;
  1042. data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  1043. return 0;
  1044. }
  1045. EXPORT_SYMBOL(phy_ethtool_get_eee);
  1046. /**
  1047. * phy_ethtool_set_eee - set EEE supported and status
  1048. * @phydev: target phy_device struct
  1049. * @data: ethtool_eee data
  1050. *
  1051. * Description: it is to program the Advertisement EEE register.
  1052. */
  1053. int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
  1054. {
  1055. int cap, old_adv, adv, ret;
  1056. if (!phydev->drv)
  1057. return -EIO;
  1058. /* Get Supported EEE */
  1059. cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  1060. if (cap < 0)
  1061. return cap;
  1062. old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  1063. if (old_adv < 0)
  1064. return old_adv;
  1065. adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
  1066. /* Mask prohibited EEE modes */
  1067. adv &= ~phydev->eee_broken_modes;
  1068. if (old_adv != adv) {
  1069. ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
  1070. if (ret < 0)
  1071. return ret;
  1072. /* Restart autonegotiation so the new modes get sent to the
  1073. * link partner.
  1074. */
  1075. ret = phy_restart_aneg(phydev);
  1076. if (ret < 0)
  1077. return ret;
  1078. }
  1079. return 0;
  1080. }
  1081. EXPORT_SYMBOL(phy_ethtool_set_eee);
  1082. int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1083. {
  1084. if (phydev->drv && phydev->drv->set_wol)
  1085. return phydev->drv->set_wol(phydev, wol);
  1086. return -EOPNOTSUPP;
  1087. }
  1088. EXPORT_SYMBOL(phy_ethtool_set_wol);
  1089. void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1090. {
  1091. if (phydev->drv && phydev->drv->get_wol)
  1092. phydev->drv->get_wol(phydev, wol);
  1093. }
  1094. EXPORT_SYMBOL(phy_ethtool_get_wol);
  1095. int phy_ethtool_get_link_ksettings(struct net_device *ndev,
  1096. struct ethtool_link_ksettings *cmd)
  1097. {
  1098. struct phy_device *phydev = ndev->phydev;
  1099. if (!phydev)
  1100. return -ENODEV;
  1101. phy_ethtool_ksettings_get(phydev, cmd);
  1102. return 0;
  1103. }
  1104. EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
  1105. int phy_ethtool_set_link_ksettings(struct net_device *ndev,
  1106. const struct ethtool_link_ksettings *cmd)
  1107. {
  1108. struct phy_device *phydev = ndev->phydev;
  1109. if (!phydev)
  1110. return -ENODEV;
  1111. return phy_ethtool_ksettings_set(phydev, cmd);
  1112. }
  1113. EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
  1114. int phy_ethtool_nway_reset(struct net_device *ndev)
  1115. {
  1116. struct phy_device *phydev = ndev->phydev;
  1117. if (!phydev)
  1118. return -ENODEV;
  1119. if (!phydev->drv)
  1120. return -EIO;
  1121. return phy_restart_aneg(phydev);
  1122. }
  1123. EXPORT_SYMBOL(phy_ethtool_nway_reset);