phy.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  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. if (phydev->drv->config_aneg)
  421. err = phydev->drv->config_aneg(phydev);
  422. else
  423. err = genphy_config_aneg(phydev);
  424. if (err < 0)
  425. goto out_unlock;
  426. if (phydev->state != PHY_HALTED) {
  427. if (AUTONEG_ENABLE == phydev->autoneg) {
  428. phydev->state = PHY_AN;
  429. phydev->link_timeout = PHY_AN_TIMEOUT;
  430. } else {
  431. phydev->state = PHY_FORCING;
  432. phydev->link_timeout = PHY_FORCE_TIMEOUT;
  433. }
  434. }
  435. /* Re-schedule a PHY state machine to check PHY status because
  436. * negotiation may already be done and aneg interrupt may not be
  437. * generated.
  438. */
  439. if (phy_interrupt_is_valid(phydev) && (phydev->state == PHY_AN)) {
  440. err = phy_aneg_done(phydev);
  441. if (err > 0) {
  442. trigger = true;
  443. err = 0;
  444. }
  445. }
  446. out_unlock:
  447. mutex_unlock(&phydev->lock);
  448. if (trigger)
  449. phy_trigger_machine(phydev, sync);
  450. return err;
  451. }
  452. /**
  453. * phy_start_aneg - start auto-negotiation for this PHY device
  454. * @phydev: the phy_device struct
  455. *
  456. * Description: Sanitizes the settings (if we're not autonegotiating
  457. * them), and then calls the driver's config_aneg function.
  458. * If the PHYCONTROL Layer is operating, we change the state to
  459. * reflect the beginning of Auto-negotiation or forcing.
  460. */
  461. int phy_start_aneg(struct phy_device *phydev)
  462. {
  463. return phy_start_aneg_priv(phydev, true);
  464. }
  465. EXPORT_SYMBOL(phy_start_aneg);
  466. /**
  467. * phy_start_machine - start PHY state machine tracking
  468. * @phydev: the phy_device struct
  469. *
  470. * Description: The PHY infrastructure can run a state machine
  471. * which tracks whether the PHY is starting up, negotiating,
  472. * etc. This function starts the delayed workqueue which tracks
  473. * the state of the PHY. If you want to maintain your own state machine,
  474. * do not call this function.
  475. */
  476. void phy_start_machine(struct phy_device *phydev)
  477. {
  478. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
  479. }
  480. EXPORT_SYMBOL_GPL(phy_start_machine);
  481. /**
  482. * phy_trigger_machine - trigger the state machine to run
  483. *
  484. * @phydev: the phy_device struct
  485. * @sync: indicate whether we should wait for the workqueue cancelation
  486. *
  487. * Description: There has been a change in state which requires that the
  488. * state machine runs.
  489. */
  490. void phy_trigger_machine(struct phy_device *phydev, bool sync)
  491. {
  492. if (sync)
  493. cancel_delayed_work_sync(&phydev->state_queue);
  494. else
  495. cancel_delayed_work(&phydev->state_queue);
  496. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
  497. }
  498. /**
  499. * phy_stop_machine - stop the PHY state machine tracking
  500. * @phydev: target phy_device struct
  501. *
  502. * Description: Stops the state machine delayed workqueue, sets the
  503. * state to UP (unless it wasn't up yet). This function must be
  504. * called BEFORE phy_detach.
  505. */
  506. void phy_stop_machine(struct phy_device *phydev)
  507. {
  508. cancel_delayed_work_sync(&phydev->state_queue);
  509. mutex_lock(&phydev->lock);
  510. if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
  511. phydev->state = PHY_UP;
  512. mutex_unlock(&phydev->lock);
  513. }
  514. /**
  515. * phy_error - enter HALTED state for this PHY device
  516. * @phydev: target phy_device struct
  517. *
  518. * Moves the PHY to the HALTED state in response to a read
  519. * or write error, and tells the controller the link is down.
  520. * Must not be called from interrupt context, or while the
  521. * phydev->lock is held.
  522. */
  523. static void phy_error(struct phy_device *phydev)
  524. {
  525. mutex_lock(&phydev->lock);
  526. phydev->state = PHY_HALTED;
  527. mutex_unlock(&phydev->lock);
  528. phy_trigger_machine(phydev, false);
  529. }
  530. /**
  531. * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
  532. * @phydev: target phy_device struct
  533. */
  534. static int phy_disable_interrupts(struct phy_device *phydev)
  535. {
  536. int err;
  537. /* Disable PHY interrupts */
  538. err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  539. if (err)
  540. return err;
  541. /* Clear the interrupt */
  542. return phy_clear_interrupt(phydev);
  543. }
  544. /**
  545. * phy_change - Called by the phy_interrupt to handle PHY changes
  546. * @phydev: phy_device struct that interrupted
  547. */
  548. static irqreturn_t phy_change(struct phy_device *phydev)
  549. {
  550. if (phy_interrupt_is_valid(phydev)) {
  551. if (phydev->drv->did_interrupt &&
  552. !phydev->drv->did_interrupt(phydev))
  553. return IRQ_NONE;
  554. if (phydev->state == PHY_HALTED)
  555. if (phy_disable_interrupts(phydev))
  556. goto phy_err;
  557. }
  558. mutex_lock(&phydev->lock);
  559. if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
  560. phydev->state = PHY_CHANGELINK;
  561. mutex_unlock(&phydev->lock);
  562. /* reschedule state queue work to run as soon as possible */
  563. phy_trigger_machine(phydev, true);
  564. if (phy_interrupt_is_valid(phydev) && phy_clear_interrupt(phydev))
  565. goto phy_err;
  566. return IRQ_HANDLED;
  567. phy_err:
  568. phy_error(phydev);
  569. return IRQ_NONE;
  570. }
  571. /**
  572. * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
  573. * @work: work_struct that describes the work to be done
  574. */
  575. void phy_change_work(struct work_struct *work)
  576. {
  577. struct phy_device *phydev =
  578. container_of(work, struct phy_device, phy_queue);
  579. phy_change(phydev);
  580. }
  581. /**
  582. * phy_interrupt - PHY interrupt handler
  583. * @irq: interrupt line
  584. * @phy_dat: phy_device pointer
  585. *
  586. * Description: When a PHY interrupt occurs, the handler disables
  587. * interrupts, and uses phy_change to handle the interrupt.
  588. */
  589. static irqreturn_t phy_interrupt(int irq, void *phy_dat)
  590. {
  591. struct phy_device *phydev = phy_dat;
  592. if (PHY_HALTED == phydev->state)
  593. return IRQ_NONE; /* It can't be ours. */
  594. return phy_change(phydev);
  595. }
  596. /**
  597. * phy_enable_interrupts - Enable the interrupts from the PHY side
  598. * @phydev: target phy_device struct
  599. */
  600. static int phy_enable_interrupts(struct phy_device *phydev)
  601. {
  602. int err = phy_clear_interrupt(phydev);
  603. if (err < 0)
  604. return err;
  605. return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  606. }
  607. /**
  608. * phy_start_interrupts - request and enable interrupts for a PHY device
  609. * @phydev: target phy_device struct
  610. *
  611. * Description: Request the interrupt for the given PHY.
  612. * If this fails, then we set irq to PHY_POLL.
  613. * Otherwise, we enable the interrupts in the PHY.
  614. * This should only be called with a valid IRQ number.
  615. * Returns 0 on success or < 0 on error.
  616. */
  617. int phy_start_interrupts(struct phy_device *phydev)
  618. {
  619. if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
  620. IRQF_ONESHOT | IRQF_SHARED,
  621. phydev_name(phydev), phydev) < 0) {
  622. pr_warn("%s: Can't get IRQ %d (PHY)\n",
  623. phydev->mdio.bus->name, phydev->irq);
  624. phydev->irq = PHY_POLL;
  625. return 0;
  626. }
  627. return phy_enable_interrupts(phydev);
  628. }
  629. EXPORT_SYMBOL(phy_start_interrupts);
  630. /**
  631. * phy_stop_interrupts - disable interrupts from a PHY device
  632. * @phydev: target phy_device struct
  633. */
  634. int phy_stop_interrupts(struct phy_device *phydev)
  635. {
  636. int err = phy_disable_interrupts(phydev);
  637. if (err)
  638. phy_error(phydev);
  639. free_irq(phydev->irq, phydev);
  640. return err;
  641. }
  642. EXPORT_SYMBOL(phy_stop_interrupts);
  643. /**
  644. * phy_stop - Bring down the PHY link, and stop checking the status
  645. * @phydev: target phy_device struct
  646. */
  647. void phy_stop(struct phy_device *phydev)
  648. {
  649. mutex_lock(&phydev->lock);
  650. if (PHY_HALTED == phydev->state)
  651. goto out_unlock;
  652. if (phy_interrupt_is_valid(phydev))
  653. phy_disable_interrupts(phydev);
  654. phydev->state = PHY_HALTED;
  655. out_unlock:
  656. mutex_unlock(&phydev->lock);
  657. /* Cannot call flush_scheduled_work() here as desired because
  658. * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
  659. * will not reenable interrupts.
  660. */
  661. }
  662. EXPORT_SYMBOL(phy_stop);
  663. /**
  664. * phy_start - start or restart a PHY device
  665. * @phydev: target phy_device struct
  666. *
  667. * Description: Indicates the attached device's readiness to
  668. * handle PHY-related work. Used during startup to start the
  669. * PHY, and after a call to phy_stop() to resume operation.
  670. * Also used to indicate the MDIO bus has cleared an error
  671. * condition.
  672. */
  673. void phy_start(struct phy_device *phydev)
  674. {
  675. int err = 0;
  676. mutex_lock(&phydev->lock);
  677. switch (phydev->state) {
  678. case PHY_STARTING:
  679. phydev->state = PHY_PENDING;
  680. break;
  681. case PHY_READY:
  682. phydev->state = PHY_UP;
  683. break;
  684. case PHY_HALTED:
  685. /* if phy was suspended, bring the physical link up again */
  686. __phy_resume(phydev);
  687. /* make sure interrupts are re-enabled for the PHY */
  688. if (phy_interrupt_is_valid(phydev)) {
  689. err = phy_enable_interrupts(phydev);
  690. if (err < 0)
  691. break;
  692. }
  693. phydev->state = PHY_RESUMING;
  694. break;
  695. default:
  696. break;
  697. }
  698. mutex_unlock(&phydev->lock);
  699. phy_trigger_machine(phydev, true);
  700. }
  701. EXPORT_SYMBOL(phy_start);
  702. static void phy_link_up(struct phy_device *phydev)
  703. {
  704. phydev->phy_link_change(phydev, true, true);
  705. phy_led_trigger_change_speed(phydev);
  706. }
  707. static void phy_link_down(struct phy_device *phydev, bool do_carrier)
  708. {
  709. phydev->phy_link_change(phydev, false, do_carrier);
  710. phy_led_trigger_change_speed(phydev);
  711. }
  712. /**
  713. * phy_state_machine - Handle the state machine
  714. * @work: work_struct that describes the work to be done
  715. */
  716. void phy_state_machine(struct work_struct *work)
  717. {
  718. struct delayed_work *dwork = to_delayed_work(work);
  719. struct phy_device *phydev =
  720. container_of(dwork, struct phy_device, state_queue);
  721. bool needs_aneg = false, do_suspend = false;
  722. enum phy_state old_state;
  723. int err = 0;
  724. int old_link;
  725. mutex_lock(&phydev->lock);
  726. old_state = phydev->state;
  727. if (phydev->drv && phydev->drv->link_change_notify)
  728. phydev->drv->link_change_notify(phydev);
  729. switch (phydev->state) {
  730. case PHY_DOWN:
  731. case PHY_STARTING:
  732. case PHY_READY:
  733. case PHY_PENDING:
  734. break;
  735. case PHY_UP:
  736. needs_aneg = true;
  737. phydev->link_timeout = PHY_AN_TIMEOUT;
  738. break;
  739. case PHY_AN:
  740. err = phy_read_status(phydev);
  741. if (err < 0)
  742. break;
  743. /* If the link is down, give up on negotiation for now */
  744. if (!phydev->link) {
  745. phydev->state = PHY_NOLINK;
  746. phy_link_down(phydev, true);
  747. break;
  748. }
  749. /* Check if negotiation is done. Break if there's an error */
  750. err = phy_aneg_done(phydev);
  751. if (err < 0)
  752. break;
  753. /* If AN is done, we're running */
  754. if (err > 0) {
  755. phydev->state = PHY_RUNNING;
  756. phy_link_up(phydev);
  757. } else if (0 == phydev->link_timeout--)
  758. needs_aneg = true;
  759. break;
  760. case PHY_NOLINK:
  761. if (phydev->irq != PHY_POLL)
  762. break;
  763. err = phy_read_status(phydev);
  764. if (err)
  765. break;
  766. if (phydev->link) {
  767. if (AUTONEG_ENABLE == phydev->autoneg) {
  768. err = phy_aneg_done(phydev);
  769. if (err < 0)
  770. break;
  771. if (!err) {
  772. phydev->state = PHY_AN;
  773. phydev->link_timeout = PHY_AN_TIMEOUT;
  774. break;
  775. }
  776. }
  777. phydev->state = PHY_RUNNING;
  778. phy_link_up(phydev);
  779. }
  780. break;
  781. case PHY_FORCING:
  782. err = genphy_update_link(phydev);
  783. if (err)
  784. break;
  785. if (phydev->link) {
  786. phydev->state = PHY_RUNNING;
  787. phy_link_up(phydev);
  788. } else {
  789. if (0 == phydev->link_timeout--)
  790. needs_aneg = true;
  791. phy_link_down(phydev, false);
  792. }
  793. break;
  794. case PHY_RUNNING:
  795. /* Only register a CHANGE if we are polling and link changed
  796. * since latest checking.
  797. */
  798. if (phydev->irq == PHY_POLL) {
  799. old_link = phydev->link;
  800. err = phy_read_status(phydev);
  801. if (err)
  802. break;
  803. if (old_link != phydev->link)
  804. phydev->state = PHY_CHANGELINK;
  805. }
  806. /*
  807. * Failsafe: check that nobody set phydev->link=0 between two
  808. * poll cycles, otherwise we won't leave RUNNING state as long
  809. * as link remains down.
  810. */
  811. if (!phydev->link && phydev->state == PHY_RUNNING) {
  812. phydev->state = PHY_CHANGELINK;
  813. phydev_err(phydev, "no link in PHY_RUNNING\n");
  814. }
  815. break;
  816. case PHY_CHANGELINK:
  817. err = phy_read_status(phydev);
  818. if (err)
  819. break;
  820. if (phydev->link) {
  821. phydev->state = PHY_RUNNING;
  822. phy_link_up(phydev);
  823. } else {
  824. phydev->state = PHY_NOLINK;
  825. phy_link_down(phydev, true);
  826. }
  827. break;
  828. case PHY_HALTED:
  829. if (phydev->link) {
  830. phydev->link = 0;
  831. phy_link_down(phydev, true);
  832. do_suspend = true;
  833. }
  834. break;
  835. case PHY_RESUMING:
  836. if (AUTONEG_ENABLE == phydev->autoneg) {
  837. err = phy_aneg_done(phydev);
  838. if (err < 0)
  839. break;
  840. /* err > 0 if AN is done.
  841. * Otherwise, it's 0, and we're still waiting for AN
  842. */
  843. if (err > 0) {
  844. err = phy_read_status(phydev);
  845. if (err)
  846. break;
  847. if (phydev->link) {
  848. phydev->state = PHY_RUNNING;
  849. phy_link_up(phydev);
  850. } else {
  851. phydev->state = PHY_NOLINK;
  852. phy_link_down(phydev, false);
  853. }
  854. } else {
  855. phydev->state = PHY_AN;
  856. phydev->link_timeout = PHY_AN_TIMEOUT;
  857. }
  858. } else {
  859. err = phy_read_status(phydev);
  860. if (err)
  861. break;
  862. if (phydev->link) {
  863. phydev->state = PHY_RUNNING;
  864. phy_link_up(phydev);
  865. } else {
  866. phydev->state = PHY_NOLINK;
  867. phy_link_down(phydev, false);
  868. }
  869. }
  870. break;
  871. }
  872. mutex_unlock(&phydev->lock);
  873. if (needs_aneg)
  874. err = phy_start_aneg_priv(phydev, false);
  875. else if (do_suspend)
  876. phy_suspend(phydev);
  877. if (err < 0)
  878. phy_error(phydev);
  879. if (old_state != phydev->state)
  880. phydev_dbg(phydev, "PHY state change %s -> %s\n",
  881. phy_state_to_str(old_state),
  882. phy_state_to_str(phydev->state));
  883. /* Only re-schedule a PHY state machine change if we are polling the
  884. * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
  885. * between states from phy_mac_interrupt()
  886. */
  887. if (phydev->irq == PHY_POLL)
  888. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
  889. PHY_STATE_TIME * HZ);
  890. }
  891. /**
  892. * phy_mac_interrupt - MAC says the link has changed
  893. * @phydev: phy_device struct with changed link
  894. *
  895. * The MAC layer is able to indicate there has been a change in the PHY link
  896. * status. Trigger the state machine and work a work queue.
  897. */
  898. void phy_mac_interrupt(struct phy_device *phydev)
  899. {
  900. /* Trigger a state machine change */
  901. queue_work(system_power_efficient_wq, &phydev->phy_queue);
  902. }
  903. EXPORT_SYMBOL(phy_mac_interrupt);
  904. /**
  905. * phy_init_eee - init and check the EEE feature
  906. * @phydev: target phy_device struct
  907. * @clk_stop_enable: PHY may stop the clock during LPI
  908. *
  909. * Description: it checks if the Energy-Efficient Ethernet (EEE)
  910. * is supported by looking at the MMD registers 3.20 and 7.60/61
  911. * and it programs the MMD register 3.0 setting the "Clock stop enable"
  912. * bit if required.
  913. */
  914. int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
  915. {
  916. if (!phydev->drv)
  917. return -EIO;
  918. /* According to 802.3az,the EEE is supported only in full duplex-mode.
  919. */
  920. if (phydev->duplex == DUPLEX_FULL) {
  921. int eee_lp, eee_cap, eee_adv;
  922. u32 lp, cap, adv;
  923. int status;
  924. /* Read phy status to properly get the right settings */
  925. status = phy_read_status(phydev);
  926. if (status)
  927. return status;
  928. /* First check if the EEE ability is supported */
  929. eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  930. if (eee_cap <= 0)
  931. goto eee_exit_err;
  932. cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
  933. if (!cap)
  934. goto eee_exit_err;
  935. /* Check which link settings negotiated and verify it in
  936. * the EEE advertising registers.
  937. */
  938. eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
  939. if (eee_lp <= 0)
  940. goto eee_exit_err;
  941. eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  942. if (eee_adv <= 0)
  943. goto eee_exit_err;
  944. adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
  945. lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
  946. if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
  947. goto eee_exit_err;
  948. if (clk_stop_enable) {
  949. /* Configure the PHY to stop receiving xMII
  950. * clock while it is signaling LPI.
  951. */
  952. int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
  953. if (val < 0)
  954. return val;
  955. val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
  956. phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
  957. }
  958. return 0; /* EEE supported */
  959. }
  960. eee_exit_err:
  961. return -EPROTONOSUPPORT;
  962. }
  963. EXPORT_SYMBOL(phy_init_eee);
  964. /**
  965. * phy_get_eee_err - report the EEE wake error count
  966. * @phydev: target phy_device struct
  967. *
  968. * Description: it is to report the number of time where the PHY
  969. * failed to complete its normal wake sequence.
  970. */
  971. int phy_get_eee_err(struct phy_device *phydev)
  972. {
  973. if (!phydev->drv)
  974. return -EIO;
  975. return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
  976. }
  977. EXPORT_SYMBOL(phy_get_eee_err);
  978. /**
  979. * phy_ethtool_get_eee - get EEE supported and status
  980. * @phydev: target phy_device struct
  981. * @data: ethtool_eee data
  982. *
  983. * Description: it reportes the Supported/Advertisement/LP Advertisement
  984. * capabilities.
  985. */
  986. int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
  987. {
  988. int val;
  989. if (!phydev->drv)
  990. return -EIO;
  991. /* Get Supported EEE */
  992. val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  993. if (val < 0)
  994. return val;
  995. data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
  996. /* Get advertisement EEE */
  997. val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  998. if (val < 0)
  999. return val;
  1000. data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  1001. /* Get LP advertisement EEE */
  1002. val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
  1003. if (val < 0)
  1004. return val;
  1005. data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  1006. return 0;
  1007. }
  1008. EXPORT_SYMBOL(phy_ethtool_get_eee);
  1009. /**
  1010. * phy_ethtool_set_eee - set EEE supported and status
  1011. * @phydev: target phy_device struct
  1012. * @data: ethtool_eee data
  1013. *
  1014. * Description: it is to program the Advertisement EEE register.
  1015. */
  1016. int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
  1017. {
  1018. int cap, old_adv, adv, ret;
  1019. if (!phydev->drv)
  1020. return -EIO;
  1021. /* Get Supported EEE */
  1022. cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  1023. if (cap < 0)
  1024. return cap;
  1025. old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  1026. if (old_adv < 0)
  1027. return old_adv;
  1028. adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
  1029. /* Mask prohibited EEE modes */
  1030. adv &= ~phydev->eee_broken_modes;
  1031. if (old_adv != adv) {
  1032. ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
  1033. if (ret < 0)
  1034. return ret;
  1035. /* Restart autonegotiation so the new modes get sent to the
  1036. * link partner.
  1037. */
  1038. ret = phy_restart_aneg(phydev);
  1039. if (ret < 0)
  1040. return ret;
  1041. }
  1042. return 0;
  1043. }
  1044. EXPORT_SYMBOL(phy_ethtool_set_eee);
  1045. int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1046. {
  1047. if (phydev->drv && phydev->drv->set_wol)
  1048. return phydev->drv->set_wol(phydev, wol);
  1049. return -EOPNOTSUPP;
  1050. }
  1051. EXPORT_SYMBOL(phy_ethtool_set_wol);
  1052. void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1053. {
  1054. if (phydev->drv && phydev->drv->get_wol)
  1055. phydev->drv->get_wol(phydev, wol);
  1056. }
  1057. EXPORT_SYMBOL(phy_ethtool_get_wol);
  1058. int phy_ethtool_get_link_ksettings(struct net_device *ndev,
  1059. struct ethtool_link_ksettings *cmd)
  1060. {
  1061. struct phy_device *phydev = ndev->phydev;
  1062. if (!phydev)
  1063. return -ENODEV;
  1064. phy_ethtool_ksettings_get(phydev, cmd);
  1065. return 0;
  1066. }
  1067. EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
  1068. int phy_ethtool_set_link_ksettings(struct net_device *ndev,
  1069. const struct ethtool_link_ksettings *cmd)
  1070. {
  1071. struct phy_device *phydev = ndev->phydev;
  1072. if (!phydev)
  1073. return -ENODEV;
  1074. return phy_ethtool_ksettings_set(phydev, cmd);
  1075. }
  1076. EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
  1077. int phy_ethtool_nway_reset(struct net_device *ndev)
  1078. {
  1079. struct phy_device *phydev = ndev->phydev;
  1080. if (!phydev)
  1081. return -ENODEV;
  1082. if (!phydev->drv)
  1083. return -EIO;
  1084. return phy_restart_aneg(phydev);
  1085. }
  1086. EXPORT_SYMBOL(phy_ethtool_nway_reset);