phy.c 36 KB

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