phy.c 36 KB

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