phy.c 34 KB

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