phy.c 35 KB

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