phy.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Framework for configuring and reading PHY devices
  3. * Based on code in sungem_phy.c and gianfar_phy.c
  4. *
  5. * Author: Andy Fleming
  6. *
  7. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  8. * Copyright (c) 2006, 2007 Maciej W. Rozycki
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/errno.h>
  13. #include <linux/unistd.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/delay.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/netlink.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/mii.h>
  23. #include <linux/ethtool.h>
  24. #include <linux/ethtool_netlink.h>
  25. #include <linux/phy.h>
  26. #include <linux/phy_led_triggers.h>
  27. #include <linux/sfp.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/mdio.h>
  30. #include <linux/io.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/atomic.h>
  33. #include <linux/suspend.h>
  34. #include <net/netlink.h>
  35. #include <net/genetlink.h>
  36. #include <net/sock.h>
  37. #define PHY_STATE_TIME HZ
  38. #define PHY_STATE_STR(_state) \
  39. case PHY_##_state: \
  40. return __stringify(_state); \
  41. static const char *phy_state_to_str(enum phy_state st)
  42. {
  43. switch (st) {
  44. PHY_STATE_STR(DOWN)
  45. PHY_STATE_STR(READY)
  46. PHY_STATE_STR(UP)
  47. PHY_STATE_STR(RUNNING)
  48. PHY_STATE_STR(NOLINK)
  49. PHY_STATE_STR(CABLETEST)
  50. PHY_STATE_STR(HALTED)
  51. }
  52. return NULL;
  53. }
  54. static void phy_process_state_change(struct phy_device *phydev,
  55. enum phy_state old_state)
  56. {
  57. if (old_state != phydev->state) {
  58. phydev_dbg(phydev, "PHY state change %s -> %s\n",
  59. phy_state_to_str(old_state),
  60. phy_state_to_str(phydev->state));
  61. if (phydev->drv && phydev->drv->link_change_notify)
  62. phydev->drv->link_change_notify(phydev);
  63. }
  64. }
  65. static void phy_link_up(struct phy_device *phydev)
  66. {
  67. phydev->phy_link_change(phydev, true);
  68. phy_led_trigger_change_speed(phydev);
  69. }
  70. static void phy_link_down(struct phy_device *phydev)
  71. {
  72. phydev->phy_link_change(phydev, false);
  73. phy_led_trigger_change_speed(phydev);
  74. }
  75. static const char *phy_pause_str(struct phy_device *phydev)
  76. {
  77. bool local_pause, local_asym_pause;
  78. if (phydev->autoneg == AUTONEG_DISABLE)
  79. goto no_pause;
  80. local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  81. phydev->advertising);
  82. local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  83. phydev->advertising);
  84. if (local_pause && phydev->pause)
  85. return "rx/tx";
  86. if (local_asym_pause && phydev->asym_pause) {
  87. if (local_pause)
  88. return "rx";
  89. if (phydev->pause)
  90. return "tx";
  91. }
  92. no_pause:
  93. return "off";
  94. }
  95. /**
  96. * phy_print_status - Convenience function to print out the current phy status
  97. * @phydev: the phy_device struct
  98. */
  99. void phy_print_status(struct phy_device *phydev)
  100. {
  101. if (phydev->link) {
  102. netdev_info(phydev->attached_dev,
  103. "Link is Up - %s/%s %s- flow control %s\n",
  104. phy_speed_to_str(phydev->speed),
  105. phy_duplex_to_str(phydev->duplex),
  106. phydev->downshifted_rate ? "(downshifted) " : "",
  107. phy_pause_str(phydev));
  108. } else {
  109. netdev_info(phydev->attached_dev, "Link is Down\n");
  110. }
  111. }
  112. EXPORT_SYMBOL(phy_print_status);
  113. /**
  114. * phy_get_rate_matching - determine if rate matching is supported
  115. * @phydev: The phy device to return rate matching for
  116. * @iface: The interface mode to use
  117. *
  118. * This determines the type of rate matching (if any) that @phy supports
  119. * using @iface. @iface may be %PHY_INTERFACE_MODE_NA to determine if any
  120. * interface supports rate matching.
  121. *
  122. * Return: The type of rate matching @phy supports for @iface, or
  123. * %RATE_MATCH_NONE.
  124. */
  125. int phy_get_rate_matching(struct phy_device *phydev,
  126. phy_interface_t iface)
  127. {
  128. int ret = RATE_MATCH_NONE;
  129. if (phydev->drv->get_rate_matching) {
  130. mutex_lock(&phydev->lock);
  131. ret = phydev->drv->get_rate_matching(phydev, iface);
  132. mutex_unlock(&phydev->lock);
  133. }
  134. return ret;
  135. }
  136. EXPORT_SYMBOL_GPL(phy_get_rate_matching);
  137. /**
  138. * phy_config_interrupt - configure the PHY device for the requested interrupts
  139. * @phydev: the phy_device struct
  140. * @interrupts: interrupt flags to configure for this @phydev
  141. *
  142. * Returns 0 on success or < 0 on error.
  143. */
  144. static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
  145. {
  146. phydev->interrupts = interrupts ? 1 : 0;
  147. if (phydev->drv->config_intr)
  148. return phydev->drv->config_intr(phydev);
  149. return 0;
  150. }
  151. /**
  152. * phy_restart_aneg - restart auto-negotiation
  153. * @phydev: target phy_device struct
  154. *
  155. * Restart the autonegotiation on @phydev. Returns >= 0 on success or
  156. * negative errno on error.
  157. */
  158. int phy_restart_aneg(struct phy_device *phydev)
  159. {
  160. int ret;
  161. if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
  162. ret = genphy_c45_restart_aneg(phydev);
  163. else
  164. ret = genphy_restart_aneg(phydev);
  165. return ret;
  166. }
  167. EXPORT_SYMBOL_GPL(phy_restart_aneg);
  168. /**
  169. * phy_aneg_done - return auto-negotiation status
  170. * @phydev: target phy_device struct
  171. *
  172. * Description: Return the auto-negotiation status from this @phydev
  173. * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
  174. * is still pending.
  175. */
  176. int phy_aneg_done(struct phy_device *phydev)
  177. {
  178. if (phydev->drv && phydev->drv->aneg_done)
  179. return phydev->drv->aneg_done(phydev);
  180. else if (phydev->is_c45)
  181. return genphy_c45_aneg_done(phydev);
  182. else
  183. return genphy_aneg_done(phydev);
  184. }
  185. EXPORT_SYMBOL(phy_aneg_done);
  186. /**
  187. * phy_find_valid - find a PHY setting that matches the requested parameters
  188. * @speed: desired speed
  189. * @duplex: desired duplex
  190. * @supported: mask of supported link modes
  191. *
  192. * Locate a supported phy setting that is, in priority order:
  193. * - an exact match for the specified speed and duplex mode
  194. * - a match for the specified speed, or slower speed
  195. * - the slowest supported speed
  196. * Returns the matched phy_setting entry, or %NULL if no supported phy
  197. * settings were found.
  198. */
  199. static const struct phy_setting *
  200. phy_find_valid(int speed, int duplex, unsigned long *supported)
  201. {
  202. return phy_lookup_setting(speed, duplex, supported, false);
  203. }
  204. /**
  205. * phy_supported_speeds - return all speeds currently supported by a phy device
  206. * @phy: The phy device to return supported speeds of.
  207. * @speeds: buffer to store supported speeds in.
  208. * @size: size of speeds buffer.
  209. *
  210. * Description: Returns the number of supported speeds, and fills the speeds
  211. * buffer with the supported speeds. If speeds buffer is too small to contain
  212. * all currently supported speeds, will return as many speeds as can fit.
  213. */
  214. unsigned int phy_supported_speeds(struct phy_device *phy,
  215. unsigned int *speeds,
  216. unsigned int size)
  217. {
  218. return phy_speeds(speeds, size, phy->supported);
  219. }
  220. /**
  221. * phy_check_valid - check if there is a valid PHY setting which matches
  222. * speed, duplex, and feature mask
  223. * @speed: speed to match
  224. * @duplex: duplex to match
  225. * @features: A mask of the valid settings
  226. *
  227. * Description: Returns true if there is a valid setting, false otherwise.
  228. */
  229. static inline bool phy_check_valid(int speed, int duplex,
  230. unsigned long *features)
  231. {
  232. return !!phy_lookup_setting(speed, duplex, features, true);
  233. }
  234. /**
  235. * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
  236. * @phydev: the target phy_device struct
  237. *
  238. * Description: Make sure the PHY is set to supported speeds and
  239. * duplexes. Drop down by one in this order: 1000/FULL,
  240. * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
  241. */
  242. static void phy_sanitize_settings(struct phy_device *phydev)
  243. {
  244. const struct phy_setting *setting;
  245. setting = phy_find_valid(phydev->speed, phydev->duplex,
  246. phydev->supported);
  247. if (setting) {
  248. phydev->speed = setting->speed;
  249. phydev->duplex = setting->duplex;
  250. } else {
  251. /* We failed to find anything (no supported speeds?) */
  252. phydev->speed = SPEED_UNKNOWN;
  253. phydev->duplex = DUPLEX_UNKNOWN;
  254. }
  255. }
  256. void phy_ethtool_ksettings_get(struct phy_device *phydev,
  257. struct ethtool_link_ksettings *cmd)
  258. {
  259. mutex_lock(&phydev->lock);
  260. linkmode_copy(cmd->link_modes.supported, phydev->supported);
  261. linkmode_copy(cmd->link_modes.advertising, phydev->advertising);
  262. linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising);
  263. cmd->base.speed = phydev->speed;
  264. cmd->base.duplex = phydev->duplex;
  265. cmd->base.master_slave_cfg = phydev->master_slave_get;
  266. cmd->base.master_slave_state = phydev->master_slave_state;
  267. cmd->base.rate_matching = phydev->rate_matching;
  268. if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
  269. cmd->base.port = PORT_BNC;
  270. else
  271. cmd->base.port = phydev->port;
  272. cmd->base.transceiver = phy_is_internal(phydev) ?
  273. XCVR_INTERNAL : XCVR_EXTERNAL;
  274. cmd->base.phy_address = phydev->mdio.addr;
  275. cmd->base.autoneg = phydev->autoneg;
  276. cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
  277. cmd->base.eth_tp_mdix = phydev->mdix;
  278. mutex_unlock(&phydev->lock);
  279. }
  280. EXPORT_SYMBOL(phy_ethtool_ksettings_get);
  281. /**
  282. * phy_mii_ioctl - generic PHY MII ioctl interface
  283. * @phydev: the phy_device struct
  284. * @ifr: &struct ifreq for socket ioctl's
  285. * @cmd: ioctl cmd to execute
  286. *
  287. * Note that this function is currently incompatible with the
  288. * PHYCONTROL layer. It changes registers without regard to
  289. * current state. Use at own risk.
  290. */
  291. int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
  292. {
  293. struct mii_ioctl_data *mii_data = if_mii(ifr);
  294. u16 val = mii_data->val_in;
  295. bool change_autoneg = false;
  296. int prtad, devad;
  297. switch (cmd) {
  298. case SIOCGMIIPHY:
  299. mii_data->phy_id = phydev->mdio.addr;
  300. fallthrough;
  301. case SIOCGMIIREG:
  302. if (mdio_phy_id_is_c45(mii_data->phy_id)) {
  303. prtad = mdio_phy_id_prtad(mii_data->phy_id);
  304. devad = mdio_phy_id_devad(mii_data->phy_id);
  305. mii_data->val_out = mdiobus_c45_read(
  306. phydev->mdio.bus, prtad, devad,
  307. mii_data->reg_num);
  308. } else {
  309. mii_data->val_out = mdiobus_read(
  310. phydev->mdio.bus, mii_data->phy_id,
  311. mii_data->reg_num);
  312. }
  313. return 0;
  314. case SIOCSMIIREG:
  315. if (mdio_phy_id_is_c45(mii_data->phy_id)) {
  316. prtad = mdio_phy_id_prtad(mii_data->phy_id);
  317. devad = mdio_phy_id_devad(mii_data->phy_id);
  318. } else {
  319. prtad = mii_data->phy_id;
  320. devad = mii_data->reg_num;
  321. }
  322. if (prtad == phydev->mdio.addr) {
  323. switch (devad) {
  324. case MII_BMCR:
  325. if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
  326. if (phydev->autoneg == AUTONEG_ENABLE)
  327. change_autoneg = true;
  328. phydev->autoneg = AUTONEG_DISABLE;
  329. if (val & BMCR_FULLDPLX)
  330. phydev->duplex = DUPLEX_FULL;
  331. else
  332. phydev->duplex = DUPLEX_HALF;
  333. if (val & BMCR_SPEED1000)
  334. phydev->speed = SPEED_1000;
  335. else if (val & BMCR_SPEED100)
  336. phydev->speed = SPEED_100;
  337. else phydev->speed = SPEED_10;
  338. } else {
  339. if (phydev->autoneg == AUTONEG_DISABLE)
  340. change_autoneg = true;
  341. phydev->autoneg = AUTONEG_ENABLE;
  342. }
  343. break;
  344. case MII_ADVERTISE:
  345. mii_adv_mod_linkmode_adv_t(phydev->advertising,
  346. val);
  347. change_autoneg = true;
  348. break;
  349. case MII_CTRL1000:
  350. mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising,
  351. val);
  352. change_autoneg = true;
  353. break;
  354. default:
  355. /* do nothing */
  356. break;
  357. }
  358. }
  359. if (mdio_phy_id_is_c45(mii_data->phy_id))
  360. mdiobus_c45_write(phydev->mdio.bus, prtad, devad,
  361. mii_data->reg_num, val);
  362. else
  363. mdiobus_write(phydev->mdio.bus, prtad, devad, val);
  364. if (prtad == phydev->mdio.addr &&
  365. devad == MII_BMCR &&
  366. val & BMCR_RESET)
  367. return phy_init_hw(phydev);
  368. if (change_autoneg)
  369. return phy_start_aneg(phydev);
  370. return 0;
  371. case SIOCSHWTSTAMP:
  372. if (phydev->mii_ts && phydev->mii_ts->hwtstamp)
  373. return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
  374. fallthrough;
  375. default:
  376. return -EOPNOTSUPP;
  377. }
  378. }
  379. EXPORT_SYMBOL(phy_mii_ioctl);
  380. /**
  381. * phy_do_ioctl - generic ndo_eth_ioctl implementation
  382. * @dev: the net_device struct
  383. * @ifr: &struct ifreq for socket ioctl's
  384. * @cmd: ioctl cmd to execute
  385. */
  386. int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  387. {
  388. if (!dev->phydev)
  389. return -ENODEV;
  390. return phy_mii_ioctl(dev->phydev, ifr, cmd);
  391. }
  392. EXPORT_SYMBOL(phy_do_ioctl);
  393. /**
  394. * phy_do_ioctl_running - generic ndo_eth_ioctl implementation but test first
  395. *
  396. * @dev: the net_device struct
  397. * @ifr: &struct ifreq for socket ioctl's
  398. * @cmd: ioctl cmd to execute
  399. *
  400. * Same as phy_do_ioctl, but ensures that net_device is running before
  401. * handling the ioctl.
  402. */
  403. int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd)
  404. {
  405. if (!netif_running(dev))
  406. return -ENODEV;
  407. return phy_do_ioctl(dev, ifr, cmd);
  408. }
  409. EXPORT_SYMBOL(phy_do_ioctl_running);
  410. /**
  411. * phy_queue_state_machine - Trigger the state machine to run soon
  412. *
  413. * @phydev: the phy_device struct
  414. * @jiffies: Run the state machine after these jiffies
  415. */
  416. void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies)
  417. {
  418. mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
  419. jiffies);
  420. }
  421. EXPORT_SYMBOL(phy_queue_state_machine);
  422. /**
  423. * phy_trigger_machine - Trigger the state machine to run now
  424. *
  425. * @phydev: the phy_device struct
  426. */
  427. void phy_trigger_machine(struct phy_device *phydev)
  428. {
  429. phy_queue_state_machine(phydev, 0);
  430. }
  431. EXPORT_SYMBOL(phy_trigger_machine);
  432. static void phy_abort_cable_test(struct phy_device *phydev)
  433. {
  434. int err;
  435. ethnl_cable_test_finished(phydev);
  436. err = phy_init_hw(phydev);
  437. if (err)
  438. phydev_err(phydev, "Error while aborting cable test");
  439. }
  440. /**
  441. * phy_ethtool_get_strings - Get the statistic counter names
  442. *
  443. * @phydev: the phy_device struct
  444. * @data: Where to put the strings
  445. */
  446. int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
  447. {
  448. if (!phydev->drv)
  449. return -EIO;
  450. mutex_lock(&phydev->lock);
  451. phydev->drv->get_strings(phydev, data);
  452. mutex_unlock(&phydev->lock);
  453. return 0;
  454. }
  455. EXPORT_SYMBOL(phy_ethtool_get_strings);
  456. /**
  457. * phy_ethtool_get_sset_count - Get the number of statistic counters
  458. *
  459. * @phydev: the phy_device struct
  460. */
  461. int phy_ethtool_get_sset_count(struct phy_device *phydev)
  462. {
  463. int ret;
  464. if (!phydev->drv)
  465. return -EIO;
  466. if (phydev->drv->get_sset_count &&
  467. phydev->drv->get_strings &&
  468. phydev->drv->get_stats) {
  469. mutex_lock(&phydev->lock);
  470. ret = phydev->drv->get_sset_count(phydev);
  471. mutex_unlock(&phydev->lock);
  472. return ret;
  473. }
  474. return -EOPNOTSUPP;
  475. }
  476. EXPORT_SYMBOL(phy_ethtool_get_sset_count);
  477. /**
  478. * phy_ethtool_get_stats - Get the statistic counters
  479. *
  480. * @phydev: the phy_device struct
  481. * @stats: What counters to get
  482. * @data: Where to store the counters
  483. */
  484. int phy_ethtool_get_stats(struct phy_device *phydev,
  485. struct ethtool_stats *stats, u64 *data)
  486. {
  487. if (!phydev->drv)
  488. return -EIO;
  489. mutex_lock(&phydev->lock);
  490. phydev->drv->get_stats(phydev, stats, data);
  491. mutex_unlock(&phydev->lock);
  492. return 0;
  493. }
  494. EXPORT_SYMBOL(phy_ethtool_get_stats);
  495. /**
  496. * phy_start_cable_test - Start a cable test
  497. *
  498. * @phydev: the phy_device struct
  499. * @extack: extack for reporting useful error messages
  500. */
  501. int phy_start_cable_test(struct phy_device *phydev,
  502. struct netlink_ext_ack *extack)
  503. {
  504. struct net_device *dev = phydev->attached_dev;
  505. int err = -ENOMEM;
  506. if (!(phydev->drv &&
  507. phydev->drv->cable_test_start &&
  508. phydev->drv->cable_test_get_status)) {
  509. NL_SET_ERR_MSG(extack,
  510. "PHY driver does not support cable testing");
  511. return -EOPNOTSUPP;
  512. }
  513. mutex_lock(&phydev->lock);
  514. if (phydev->state == PHY_CABLETEST) {
  515. NL_SET_ERR_MSG(extack,
  516. "PHY already performing a test");
  517. err = -EBUSY;
  518. goto out;
  519. }
  520. if (phydev->state < PHY_UP ||
  521. phydev->state > PHY_CABLETEST) {
  522. NL_SET_ERR_MSG(extack,
  523. "PHY not configured. Try setting interface up");
  524. err = -EBUSY;
  525. goto out;
  526. }
  527. err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
  528. if (err)
  529. goto out;
  530. /* Mark the carrier down until the test is complete */
  531. phy_link_down(phydev);
  532. netif_testing_on(dev);
  533. err = phydev->drv->cable_test_start(phydev);
  534. if (err) {
  535. netif_testing_off(dev);
  536. phy_link_up(phydev);
  537. goto out_free;
  538. }
  539. phydev->state = PHY_CABLETEST;
  540. if (phy_polling_mode(phydev))
  541. phy_trigger_machine(phydev);
  542. mutex_unlock(&phydev->lock);
  543. return 0;
  544. out_free:
  545. ethnl_cable_test_free(phydev);
  546. out:
  547. mutex_unlock(&phydev->lock);
  548. return err;
  549. }
  550. EXPORT_SYMBOL(phy_start_cable_test);
  551. /**
  552. * phy_start_cable_test_tdr - Start a raw TDR cable test
  553. *
  554. * @phydev: the phy_device struct
  555. * @extack: extack for reporting useful error messages
  556. * @config: Configuration of the test to run
  557. */
  558. int phy_start_cable_test_tdr(struct phy_device *phydev,
  559. struct netlink_ext_ack *extack,
  560. const struct phy_tdr_config *config)
  561. {
  562. struct net_device *dev = phydev->attached_dev;
  563. int err = -ENOMEM;
  564. if (!(phydev->drv &&
  565. phydev->drv->cable_test_tdr_start &&
  566. phydev->drv->cable_test_get_status)) {
  567. NL_SET_ERR_MSG(extack,
  568. "PHY driver does not support cable test TDR");
  569. return -EOPNOTSUPP;
  570. }
  571. mutex_lock(&phydev->lock);
  572. if (phydev->state == PHY_CABLETEST) {
  573. NL_SET_ERR_MSG(extack,
  574. "PHY already performing a test");
  575. err = -EBUSY;
  576. goto out;
  577. }
  578. if (phydev->state < PHY_UP ||
  579. phydev->state > PHY_CABLETEST) {
  580. NL_SET_ERR_MSG(extack,
  581. "PHY not configured. Try setting interface up");
  582. err = -EBUSY;
  583. goto out;
  584. }
  585. err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_TDR_NTF);
  586. if (err)
  587. goto out;
  588. /* Mark the carrier down until the test is complete */
  589. phy_link_down(phydev);
  590. netif_testing_on(dev);
  591. err = phydev->drv->cable_test_tdr_start(phydev, config);
  592. if (err) {
  593. netif_testing_off(dev);
  594. phy_link_up(phydev);
  595. goto out_free;
  596. }
  597. phydev->state = PHY_CABLETEST;
  598. if (phy_polling_mode(phydev))
  599. phy_trigger_machine(phydev);
  600. mutex_unlock(&phydev->lock);
  601. return 0;
  602. out_free:
  603. ethnl_cable_test_free(phydev);
  604. out:
  605. mutex_unlock(&phydev->lock);
  606. return err;
  607. }
  608. EXPORT_SYMBOL(phy_start_cable_test_tdr);
  609. int phy_config_aneg(struct phy_device *phydev)
  610. {
  611. if (phydev->drv->config_aneg)
  612. return phydev->drv->config_aneg(phydev);
  613. /* Clause 45 PHYs that don't implement Clause 22 registers are not
  614. * allowed to call genphy_config_aneg()
  615. */
  616. if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
  617. return genphy_c45_config_aneg(phydev);
  618. return genphy_config_aneg(phydev);
  619. }
  620. EXPORT_SYMBOL(phy_config_aneg);
  621. /**
  622. * phy_check_link_status - check link status and set state accordingly
  623. * @phydev: the phy_device struct
  624. *
  625. * Description: Check for link and whether autoneg was triggered / is running
  626. * and set state accordingly
  627. */
  628. static int phy_check_link_status(struct phy_device *phydev)
  629. {
  630. int err;
  631. lockdep_assert_held(&phydev->lock);
  632. /* Keep previous state if loopback is enabled because some PHYs
  633. * report that Link is Down when loopback is enabled.
  634. */
  635. if (phydev->loopback_enabled)
  636. return 0;
  637. err = phy_read_status(phydev);
  638. if (err)
  639. return err;
  640. if (phydev->link && phydev->state != PHY_RUNNING) {
  641. phy_check_downshift(phydev);
  642. phydev->state = PHY_RUNNING;
  643. phy_link_up(phydev);
  644. } else if (!phydev->link && phydev->state != PHY_NOLINK) {
  645. phydev->state = PHY_NOLINK;
  646. phy_link_down(phydev);
  647. }
  648. return 0;
  649. }
  650. /**
  651. * _phy_start_aneg - start auto-negotiation for this PHY device
  652. * @phydev: the phy_device struct
  653. *
  654. * Description: Sanitizes the settings (if we're not autonegotiating
  655. * them), and then calls the driver's config_aneg function.
  656. * If the PHYCONTROL Layer is operating, we change the state to
  657. * reflect the beginning of Auto-negotiation or forcing.
  658. */
  659. static int _phy_start_aneg(struct phy_device *phydev)
  660. {
  661. int err;
  662. lockdep_assert_held(&phydev->lock);
  663. if (!phydev->drv)
  664. return -EIO;
  665. if (AUTONEG_DISABLE == phydev->autoneg)
  666. phy_sanitize_settings(phydev);
  667. err = phy_config_aneg(phydev);
  668. if (err < 0)
  669. return err;
  670. if (phy_is_started(phydev))
  671. err = phy_check_link_status(phydev);
  672. return err;
  673. }
  674. /**
  675. * phy_start_aneg - start auto-negotiation for this PHY device
  676. * @phydev: the phy_device struct
  677. *
  678. * Description: Sanitizes the settings (if we're not autonegotiating
  679. * them), and then calls the driver's config_aneg function.
  680. * If the PHYCONTROL Layer is operating, we change the state to
  681. * reflect the beginning of Auto-negotiation or forcing.
  682. */
  683. int phy_start_aneg(struct phy_device *phydev)
  684. {
  685. int err;
  686. mutex_lock(&phydev->lock);
  687. err = _phy_start_aneg(phydev);
  688. mutex_unlock(&phydev->lock);
  689. return err;
  690. }
  691. EXPORT_SYMBOL(phy_start_aneg);
  692. static int phy_poll_aneg_done(struct phy_device *phydev)
  693. {
  694. unsigned int retries = 100;
  695. int ret;
  696. do {
  697. msleep(100);
  698. ret = phy_aneg_done(phydev);
  699. } while (!ret && --retries);
  700. if (!ret)
  701. return -ETIMEDOUT;
  702. return ret < 0 ? ret : 0;
  703. }
  704. int phy_ethtool_ksettings_set(struct phy_device *phydev,
  705. const struct ethtool_link_ksettings *cmd)
  706. {
  707. __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
  708. u8 autoneg = cmd->base.autoneg;
  709. u8 duplex = cmd->base.duplex;
  710. u32 speed = cmd->base.speed;
  711. if (cmd->base.phy_address != phydev->mdio.addr)
  712. return -EINVAL;
  713. linkmode_copy(advertising, cmd->link_modes.advertising);
  714. /* We make sure that we don't pass unsupported values in to the PHY */
  715. linkmode_and(advertising, advertising, phydev->supported);
  716. /* Verify the settings we care about. */
  717. if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
  718. return -EINVAL;
  719. if (autoneg == AUTONEG_ENABLE && linkmode_empty(advertising))
  720. return -EINVAL;
  721. if (autoneg == AUTONEG_DISABLE &&
  722. ((speed != SPEED_1000 &&
  723. speed != SPEED_100 &&
  724. speed != SPEED_10) ||
  725. (duplex != DUPLEX_HALF &&
  726. duplex != DUPLEX_FULL)))
  727. return -EINVAL;
  728. mutex_lock(&phydev->lock);
  729. phydev->autoneg = autoneg;
  730. if (autoneg == AUTONEG_DISABLE) {
  731. phydev->speed = speed;
  732. phydev->duplex = duplex;
  733. }
  734. linkmode_copy(phydev->advertising, advertising);
  735. linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  736. phydev->advertising, autoneg == AUTONEG_ENABLE);
  737. phydev->master_slave_set = cmd->base.master_slave_cfg;
  738. phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
  739. /* Restart the PHY */
  740. if (phy_is_started(phydev)) {
  741. phydev->state = PHY_UP;
  742. phy_trigger_machine(phydev);
  743. } else {
  744. _phy_start_aneg(phydev);
  745. }
  746. mutex_unlock(&phydev->lock);
  747. return 0;
  748. }
  749. EXPORT_SYMBOL(phy_ethtool_ksettings_set);
  750. /**
  751. * phy_speed_down - set speed to lowest speed supported by both link partners
  752. * @phydev: the phy_device struct
  753. * @sync: perform action synchronously
  754. *
  755. * Description: Typically used to save energy when waiting for a WoL packet
  756. *
  757. * WARNING: Setting sync to false may cause the system being unable to suspend
  758. * in case the PHY generates an interrupt when finishing the autonegotiation.
  759. * This interrupt may wake up the system immediately after suspend.
  760. * Therefore use sync = false only if you're sure it's safe with the respective
  761. * network chip.
  762. */
  763. int phy_speed_down(struct phy_device *phydev, bool sync)
  764. {
  765. __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
  766. int ret;
  767. if (phydev->autoneg != AUTONEG_ENABLE)
  768. return 0;
  769. linkmode_copy(adv_tmp, phydev->advertising);
  770. ret = phy_speed_down_core(phydev);
  771. if (ret)
  772. return ret;
  773. linkmode_copy(phydev->adv_old, adv_tmp);
  774. if (linkmode_equal(phydev->advertising, adv_tmp))
  775. return 0;
  776. ret = phy_config_aneg(phydev);
  777. if (ret)
  778. return ret;
  779. return sync ? phy_poll_aneg_done(phydev) : 0;
  780. }
  781. EXPORT_SYMBOL_GPL(phy_speed_down);
  782. /**
  783. * phy_speed_up - (re)set advertised speeds to all supported speeds
  784. * @phydev: the phy_device struct
  785. *
  786. * Description: Used to revert the effect of phy_speed_down
  787. */
  788. int phy_speed_up(struct phy_device *phydev)
  789. {
  790. __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
  791. if (phydev->autoneg != AUTONEG_ENABLE)
  792. return 0;
  793. if (linkmode_empty(phydev->adv_old))
  794. return 0;
  795. linkmode_copy(adv_tmp, phydev->advertising);
  796. linkmode_copy(phydev->advertising, phydev->adv_old);
  797. linkmode_zero(phydev->adv_old);
  798. if (linkmode_equal(phydev->advertising, adv_tmp))
  799. return 0;
  800. return phy_config_aneg(phydev);
  801. }
  802. EXPORT_SYMBOL_GPL(phy_speed_up);
  803. /**
  804. * phy_start_machine - start PHY state machine tracking
  805. * @phydev: the phy_device struct
  806. *
  807. * Description: The PHY infrastructure can run a state machine
  808. * which tracks whether the PHY is starting up, negotiating,
  809. * etc. This function starts the delayed workqueue which tracks
  810. * the state of the PHY. If you want to maintain your own state machine,
  811. * do not call this function.
  812. */
  813. void phy_start_machine(struct phy_device *phydev)
  814. {
  815. phy_trigger_machine(phydev);
  816. }
  817. EXPORT_SYMBOL_GPL(phy_start_machine);
  818. /**
  819. * phy_stop_machine - stop the PHY state machine tracking
  820. * @phydev: target phy_device struct
  821. *
  822. * Description: Stops the state machine delayed workqueue, sets the
  823. * state to UP (unless it wasn't up yet). This function must be
  824. * called BEFORE phy_detach.
  825. */
  826. void phy_stop_machine(struct phy_device *phydev)
  827. {
  828. cancel_delayed_work_sync(&phydev->state_queue);
  829. mutex_lock(&phydev->lock);
  830. if (phy_is_started(phydev))
  831. phydev->state = PHY_UP;
  832. mutex_unlock(&phydev->lock);
  833. }
  834. /**
  835. * phy_error - enter HALTED state for this PHY device
  836. * @phydev: target phy_device struct
  837. *
  838. * Moves the PHY to the HALTED state in response to a read
  839. * or write error, and tells the controller the link is down.
  840. * Must not be called from interrupt context, or while the
  841. * phydev->lock is held.
  842. */
  843. void phy_error(struct phy_device *phydev)
  844. {
  845. WARN_ON(1);
  846. mutex_lock(&phydev->lock);
  847. phydev->state = PHY_HALTED;
  848. mutex_unlock(&phydev->lock);
  849. phy_trigger_machine(phydev);
  850. }
  851. EXPORT_SYMBOL(phy_error);
  852. /**
  853. * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
  854. * @phydev: target phy_device struct
  855. */
  856. int phy_disable_interrupts(struct phy_device *phydev)
  857. {
  858. /* Disable PHY interrupts */
  859. return phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  860. }
  861. /**
  862. * phy_interrupt - PHY interrupt handler
  863. * @irq: interrupt line
  864. * @phy_dat: phy_device pointer
  865. *
  866. * Description: Handle PHY interrupt
  867. */
  868. static irqreturn_t phy_interrupt(int irq, void *phy_dat)
  869. {
  870. struct phy_device *phydev = phy_dat;
  871. struct phy_driver *drv = phydev->drv;
  872. irqreturn_t ret;
  873. /* Wakeup interrupts may occur during a system sleep transition.
  874. * Postpone handling until the PHY has resumed.
  875. */
  876. if (IS_ENABLED(CONFIG_PM_SLEEP) && phydev->irq_suspended) {
  877. struct net_device *netdev = phydev->attached_dev;
  878. if (netdev) {
  879. struct device *parent = netdev->dev.parent;
  880. if (netdev->wol_enabled)
  881. pm_system_wakeup();
  882. else if (device_may_wakeup(&netdev->dev))
  883. pm_wakeup_dev_event(&netdev->dev, 0, true);
  884. else if (parent && device_may_wakeup(parent))
  885. pm_wakeup_dev_event(parent, 0, true);
  886. }
  887. phydev->irq_rerun = 1;
  888. disable_irq_nosync(irq);
  889. return IRQ_HANDLED;
  890. }
  891. mutex_lock(&phydev->lock);
  892. ret = drv->handle_interrupt(phydev);
  893. mutex_unlock(&phydev->lock);
  894. return ret;
  895. }
  896. /**
  897. * phy_enable_interrupts - Enable the interrupts from the PHY side
  898. * @phydev: target phy_device struct
  899. */
  900. static int phy_enable_interrupts(struct phy_device *phydev)
  901. {
  902. return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  903. }
  904. /**
  905. * phy_request_interrupt - request and enable interrupt for a PHY device
  906. * @phydev: target phy_device struct
  907. *
  908. * Description: Request and enable the interrupt for the given PHY.
  909. * If this fails, then we set irq to PHY_POLL.
  910. * This should only be called with a valid IRQ number.
  911. */
  912. void phy_request_interrupt(struct phy_device *phydev)
  913. {
  914. int err;
  915. err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
  916. IRQF_ONESHOT | IRQF_SHARED,
  917. phydev_name(phydev), phydev);
  918. if (err) {
  919. phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
  920. err, phydev->irq);
  921. phydev->irq = PHY_POLL;
  922. } else {
  923. if (phy_enable_interrupts(phydev)) {
  924. phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
  925. phy_free_interrupt(phydev);
  926. phydev->irq = PHY_POLL;
  927. }
  928. }
  929. }
  930. EXPORT_SYMBOL(phy_request_interrupt);
  931. /**
  932. * phy_free_interrupt - disable and free interrupt for a PHY device
  933. * @phydev: target phy_device struct
  934. *
  935. * Description: Disable and free the interrupt for the given PHY.
  936. * This should only be called with a valid IRQ number.
  937. */
  938. void phy_free_interrupt(struct phy_device *phydev)
  939. {
  940. phy_disable_interrupts(phydev);
  941. free_irq(phydev->irq, phydev);
  942. }
  943. EXPORT_SYMBOL(phy_free_interrupt);
  944. /**
  945. * phy_stop - Bring down the PHY link, and stop checking the status
  946. * @phydev: target phy_device struct
  947. */
  948. void phy_stop(struct phy_device *phydev)
  949. {
  950. struct net_device *dev = phydev->attached_dev;
  951. enum phy_state old_state;
  952. if (!phy_is_started(phydev) && phydev->state != PHY_DOWN) {
  953. WARN(1, "called from state %s\n",
  954. phy_state_to_str(phydev->state));
  955. return;
  956. }
  957. mutex_lock(&phydev->lock);
  958. old_state = phydev->state;
  959. if (phydev->state == PHY_CABLETEST) {
  960. phy_abort_cable_test(phydev);
  961. netif_testing_off(dev);
  962. }
  963. if (phydev->sfp_bus)
  964. sfp_upstream_stop(phydev->sfp_bus);
  965. phydev->state = PHY_HALTED;
  966. phy_process_state_change(phydev, old_state);
  967. mutex_unlock(&phydev->lock);
  968. phy_state_machine(&phydev->state_queue.work);
  969. phy_stop_machine(phydev);
  970. /* Cannot call flush_scheduled_work() here as desired because
  971. * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
  972. * will not reenable interrupts.
  973. */
  974. }
  975. EXPORT_SYMBOL(phy_stop);
  976. /**
  977. * phy_start - start or restart a PHY device
  978. * @phydev: target phy_device struct
  979. *
  980. * Description: Indicates the attached device's readiness to
  981. * handle PHY-related work. Used during startup to start the
  982. * PHY, and after a call to phy_stop() to resume operation.
  983. * Also used to indicate the MDIO bus has cleared an error
  984. * condition.
  985. */
  986. void phy_start(struct phy_device *phydev)
  987. {
  988. mutex_lock(&phydev->lock);
  989. if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
  990. WARN(1, "called from state %s\n",
  991. phy_state_to_str(phydev->state));
  992. goto out;
  993. }
  994. if (phydev->sfp_bus)
  995. sfp_upstream_start(phydev->sfp_bus);
  996. /* if phy was suspended, bring the physical link up again */
  997. __phy_resume(phydev);
  998. phydev->state = PHY_UP;
  999. phy_start_machine(phydev);
  1000. out:
  1001. mutex_unlock(&phydev->lock);
  1002. }
  1003. EXPORT_SYMBOL(phy_start);
  1004. /**
  1005. * phy_state_machine - Handle the state machine
  1006. * @work: work_struct that describes the work to be done
  1007. */
  1008. void phy_state_machine(struct work_struct *work)
  1009. {
  1010. struct delayed_work *dwork = to_delayed_work(work);
  1011. struct phy_device *phydev =
  1012. container_of(dwork, struct phy_device, state_queue);
  1013. struct net_device *dev = phydev->attached_dev;
  1014. bool needs_aneg = false, do_suspend = false;
  1015. enum phy_state old_state;
  1016. bool finished = false;
  1017. int err = 0;
  1018. mutex_lock(&phydev->lock);
  1019. old_state = phydev->state;
  1020. switch (phydev->state) {
  1021. case PHY_DOWN:
  1022. case PHY_READY:
  1023. break;
  1024. case PHY_UP:
  1025. needs_aneg = true;
  1026. break;
  1027. case PHY_NOLINK:
  1028. case PHY_RUNNING:
  1029. err = phy_check_link_status(phydev);
  1030. break;
  1031. case PHY_CABLETEST:
  1032. err = phydev->drv->cable_test_get_status(phydev, &finished);
  1033. if (err) {
  1034. phy_abort_cable_test(phydev);
  1035. netif_testing_off(dev);
  1036. needs_aneg = true;
  1037. phydev->state = PHY_UP;
  1038. break;
  1039. }
  1040. if (finished) {
  1041. ethnl_cable_test_finished(phydev);
  1042. netif_testing_off(dev);
  1043. needs_aneg = true;
  1044. phydev->state = PHY_UP;
  1045. }
  1046. break;
  1047. case PHY_HALTED:
  1048. if (phydev->link) {
  1049. phydev->link = 0;
  1050. phy_link_down(phydev);
  1051. }
  1052. do_suspend = true;
  1053. break;
  1054. }
  1055. mutex_unlock(&phydev->lock);
  1056. if (needs_aneg)
  1057. err = phy_start_aneg(phydev);
  1058. else if (do_suspend)
  1059. phy_suspend(phydev);
  1060. if (err == -ENODEV)
  1061. return;
  1062. if (err < 0)
  1063. phy_error(phydev);
  1064. phy_process_state_change(phydev, old_state);
  1065. /* Only re-schedule a PHY state machine change if we are polling the
  1066. * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving
  1067. * between states from phy_mac_interrupt().
  1068. *
  1069. * In state PHY_HALTED the PHY gets suspended, so rescheduling the
  1070. * state machine would be pointless and possibly error prone when
  1071. * called from phy_disconnect() synchronously.
  1072. */
  1073. mutex_lock(&phydev->lock);
  1074. if (phy_polling_mode(phydev) && phy_is_started(phydev))
  1075. phy_queue_state_machine(phydev, PHY_STATE_TIME);
  1076. mutex_unlock(&phydev->lock);
  1077. }
  1078. /**
  1079. * phy_mac_interrupt - MAC says the link has changed
  1080. * @phydev: phy_device struct with changed link
  1081. *
  1082. * The MAC layer is able to indicate there has been a change in the PHY link
  1083. * status. Trigger the state machine and work a work queue.
  1084. */
  1085. void phy_mac_interrupt(struct phy_device *phydev)
  1086. {
  1087. /* Trigger a state machine change */
  1088. phy_trigger_machine(phydev);
  1089. }
  1090. EXPORT_SYMBOL(phy_mac_interrupt);
  1091. static void mmd_eee_adv_to_linkmode(unsigned long *advertising, u16 eee_adv)
  1092. {
  1093. linkmode_zero(advertising);
  1094. if (eee_adv & MDIO_EEE_100TX)
  1095. linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
  1096. advertising);
  1097. if (eee_adv & MDIO_EEE_1000T)
  1098. linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  1099. advertising);
  1100. if (eee_adv & MDIO_EEE_10GT)
  1101. linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
  1102. advertising);
  1103. if (eee_adv & MDIO_EEE_1000KX)
  1104. linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
  1105. advertising);
  1106. if (eee_adv & MDIO_EEE_10GKX4)
  1107. linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
  1108. advertising);
  1109. if (eee_adv & MDIO_EEE_10GKR)
  1110. linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
  1111. advertising);
  1112. }
  1113. /**
  1114. * phy_init_eee - init and check the EEE feature
  1115. * @phydev: target phy_device struct
  1116. * @clk_stop_enable: PHY may stop the clock during LPI
  1117. *
  1118. * Description: it checks if the Energy-Efficient Ethernet (EEE)
  1119. * is supported by looking at the MMD registers 3.20 and 7.60/61
  1120. * and it programs the MMD register 3.0 setting the "Clock stop enable"
  1121. * bit if required.
  1122. */
  1123. int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
  1124. {
  1125. if (!phydev->drv)
  1126. return -EIO;
  1127. /* According to 802.3az,the EEE is supported only in full duplex-mode.
  1128. */
  1129. if (phydev->duplex == DUPLEX_FULL) {
  1130. __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
  1131. __ETHTOOL_DECLARE_LINK_MODE_MASK(lp);
  1132. __ETHTOOL_DECLARE_LINK_MODE_MASK(adv);
  1133. int eee_lp, eee_cap, eee_adv;
  1134. int status;
  1135. u32 cap;
  1136. /* Read phy status to properly get the right settings */
  1137. status = phy_read_status(phydev);
  1138. if (status)
  1139. return status;
  1140. /* First check if the EEE ability is supported */
  1141. eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  1142. if (eee_cap <= 0)
  1143. goto eee_exit_err;
  1144. cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
  1145. if (!cap)
  1146. goto eee_exit_err;
  1147. /* Check which link settings negotiated and verify it in
  1148. * the EEE advertising registers.
  1149. */
  1150. eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
  1151. if (eee_lp <= 0)
  1152. goto eee_exit_err;
  1153. eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  1154. if (eee_adv <= 0)
  1155. goto eee_exit_err;
  1156. mmd_eee_adv_to_linkmode(adv, eee_adv);
  1157. mmd_eee_adv_to_linkmode(lp, eee_lp);
  1158. linkmode_and(common, adv, lp);
  1159. if (!phy_check_valid(phydev->speed, phydev->duplex, common))
  1160. goto eee_exit_err;
  1161. if (clk_stop_enable)
  1162. /* Configure the PHY to stop receiving xMII
  1163. * clock while it is signaling LPI.
  1164. */
  1165. phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
  1166. MDIO_PCS_CTRL1_CLKSTOP_EN);
  1167. return 0; /* EEE supported */
  1168. }
  1169. eee_exit_err:
  1170. return -EPROTONOSUPPORT;
  1171. }
  1172. EXPORT_SYMBOL(phy_init_eee);
  1173. /**
  1174. * phy_get_eee_err - report the EEE wake error count
  1175. * @phydev: target phy_device struct
  1176. *
  1177. * Description: it is to report the number of time where the PHY
  1178. * failed to complete its normal wake sequence.
  1179. */
  1180. int phy_get_eee_err(struct phy_device *phydev)
  1181. {
  1182. if (!phydev->drv)
  1183. return -EIO;
  1184. return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
  1185. }
  1186. EXPORT_SYMBOL(phy_get_eee_err);
  1187. /**
  1188. * phy_ethtool_get_eee - get EEE supported and status
  1189. * @phydev: target phy_device struct
  1190. * @data: ethtool_eee data
  1191. *
  1192. * Description: it reportes the Supported/Advertisement/LP Advertisement
  1193. * capabilities.
  1194. */
  1195. int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
  1196. {
  1197. int val;
  1198. if (!phydev->drv)
  1199. return -EIO;
  1200. /* Get Supported EEE */
  1201. val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  1202. if (val < 0)
  1203. return val;
  1204. data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
  1205. /* Get advertisement EEE */
  1206. val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  1207. if (val < 0)
  1208. return val;
  1209. data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  1210. data->eee_enabled = !!data->advertised;
  1211. /* Get LP advertisement EEE */
  1212. val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
  1213. if (val < 0)
  1214. return val;
  1215. data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  1216. data->eee_active = !!(data->advertised & data->lp_advertised);
  1217. return 0;
  1218. }
  1219. EXPORT_SYMBOL(phy_ethtool_get_eee);
  1220. /**
  1221. * phy_ethtool_set_eee - set EEE supported and status
  1222. * @phydev: target phy_device struct
  1223. * @data: ethtool_eee data
  1224. *
  1225. * Description: it is to program the Advertisement EEE register.
  1226. */
  1227. int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
  1228. {
  1229. int cap, old_adv, adv = 0, ret;
  1230. if (!phydev->drv)
  1231. return -EIO;
  1232. /* Get Supported EEE */
  1233. cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  1234. if (cap < 0)
  1235. return cap;
  1236. old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  1237. if (old_adv < 0)
  1238. return old_adv;
  1239. if (data->eee_enabled) {
  1240. adv = !data->advertised ? cap :
  1241. ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
  1242. /* Mask prohibited EEE modes */
  1243. adv &= ~phydev->eee_broken_modes;
  1244. }
  1245. if (old_adv != adv) {
  1246. ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
  1247. if (ret < 0)
  1248. return ret;
  1249. /* Restart autonegotiation so the new modes get sent to the
  1250. * link partner.
  1251. */
  1252. if (phydev->autoneg == AUTONEG_ENABLE) {
  1253. ret = phy_restart_aneg(phydev);
  1254. if (ret < 0)
  1255. return ret;
  1256. }
  1257. }
  1258. return 0;
  1259. }
  1260. EXPORT_SYMBOL(phy_ethtool_set_eee);
  1261. /**
  1262. * phy_ethtool_set_wol - Configure Wake On LAN
  1263. *
  1264. * @phydev: target phy_device struct
  1265. * @wol: Configuration requested
  1266. */
  1267. int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1268. {
  1269. if (phydev->drv && phydev->drv->set_wol)
  1270. return phydev->drv->set_wol(phydev, wol);
  1271. return -EOPNOTSUPP;
  1272. }
  1273. EXPORT_SYMBOL(phy_ethtool_set_wol);
  1274. /**
  1275. * phy_ethtool_get_wol - Get the current Wake On LAN configuration
  1276. *
  1277. * @phydev: target phy_device struct
  1278. * @wol: Store the current configuration here
  1279. */
  1280. void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1281. {
  1282. if (phydev->drv && phydev->drv->get_wol)
  1283. phydev->drv->get_wol(phydev, wol);
  1284. }
  1285. EXPORT_SYMBOL(phy_ethtool_get_wol);
  1286. int phy_ethtool_get_link_ksettings(struct net_device *ndev,
  1287. struct ethtool_link_ksettings *cmd)
  1288. {
  1289. struct phy_device *phydev = ndev->phydev;
  1290. if (!phydev)
  1291. return -ENODEV;
  1292. phy_ethtool_ksettings_get(phydev, cmd);
  1293. return 0;
  1294. }
  1295. EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
  1296. int phy_ethtool_set_link_ksettings(struct net_device *ndev,
  1297. const struct ethtool_link_ksettings *cmd)
  1298. {
  1299. struct phy_device *phydev = ndev->phydev;
  1300. if (!phydev)
  1301. return -ENODEV;
  1302. return phy_ethtool_ksettings_set(phydev, cmd);
  1303. }
  1304. EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
  1305. /**
  1306. * phy_ethtool_nway_reset - Restart auto negotiation
  1307. * @ndev: Network device to restart autoneg for
  1308. */
  1309. int phy_ethtool_nway_reset(struct net_device *ndev)
  1310. {
  1311. struct phy_device *phydev = ndev->phydev;
  1312. if (!phydev)
  1313. return -ENODEV;
  1314. if (!phydev->drv)
  1315. return -EIO;
  1316. return phy_restart_aneg(phydev);
  1317. }
  1318. EXPORT_SYMBOL(phy_ethtool_nway_reset);