phy.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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. if (autoneg == AUTONEG_DISABLE) {
  289. phydev->speed = speed;
  290. phydev->duplex = duplex;
  291. }
  292. phydev->advertising = advertising;
  293. if (autoneg == AUTONEG_ENABLE)
  294. phydev->advertising |= ADVERTISED_Autoneg;
  295. else
  296. phydev->advertising &= ~ADVERTISED_Autoneg;
  297. phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
  298. /* Restart the PHY */
  299. phy_start_aneg(phydev);
  300. return 0;
  301. }
  302. EXPORT_SYMBOL(phy_ethtool_ksettings_set);
  303. void phy_ethtool_ksettings_get(struct phy_device *phydev,
  304. struct ethtool_link_ksettings *cmd)
  305. {
  306. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  307. phydev->supported);
  308. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  309. phydev->advertising);
  310. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
  311. phydev->lp_advertising);
  312. cmd->base.speed = phydev->speed;
  313. cmd->base.duplex = phydev->duplex;
  314. if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
  315. cmd->base.port = PORT_BNC;
  316. else
  317. cmd->base.port = PORT_MII;
  318. cmd->base.transceiver = phy_is_internal(phydev) ?
  319. XCVR_INTERNAL : XCVR_EXTERNAL;
  320. cmd->base.phy_address = phydev->mdio.addr;
  321. cmd->base.autoneg = phydev->autoneg;
  322. cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
  323. cmd->base.eth_tp_mdix = phydev->mdix;
  324. }
  325. EXPORT_SYMBOL(phy_ethtool_ksettings_get);
  326. /**
  327. * phy_mii_ioctl - generic PHY MII ioctl interface
  328. * @phydev: the phy_device struct
  329. * @ifr: &struct ifreq for socket ioctl's
  330. * @cmd: ioctl cmd to execute
  331. *
  332. * Note that this function is currently incompatible with the
  333. * PHYCONTROL layer. It changes registers without regard to
  334. * current state. Use at own risk.
  335. */
  336. int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
  337. {
  338. struct mii_ioctl_data *mii_data = if_mii(ifr);
  339. u16 val = mii_data->val_in;
  340. bool change_autoneg = false;
  341. switch (cmd) {
  342. case SIOCGMIIPHY:
  343. mii_data->phy_id = phydev->mdio.addr;
  344. /* fall through */
  345. case SIOCGMIIREG:
  346. mii_data->val_out = mdiobus_read(phydev->mdio.bus,
  347. mii_data->phy_id,
  348. mii_data->reg_num);
  349. return 0;
  350. case SIOCSMIIREG:
  351. if (mii_data->phy_id == phydev->mdio.addr) {
  352. switch (mii_data->reg_num) {
  353. case MII_BMCR:
  354. if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
  355. if (phydev->autoneg == AUTONEG_ENABLE)
  356. change_autoneg = true;
  357. phydev->autoneg = AUTONEG_DISABLE;
  358. if (val & BMCR_FULLDPLX)
  359. phydev->duplex = DUPLEX_FULL;
  360. else
  361. phydev->duplex = DUPLEX_HALF;
  362. if (val & BMCR_SPEED1000)
  363. phydev->speed = SPEED_1000;
  364. else if (val & BMCR_SPEED100)
  365. phydev->speed = SPEED_100;
  366. else phydev->speed = SPEED_10;
  367. }
  368. else {
  369. if (phydev->autoneg == AUTONEG_DISABLE)
  370. change_autoneg = true;
  371. phydev->autoneg = AUTONEG_ENABLE;
  372. }
  373. break;
  374. case MII_ADVERTISE:
  375. phydev->advertising = mii_adv_to_ethtool_adv_t(val);
  376. change_autoneg = true;
  377. break;
  378. default:
  379. /* do nothing */
  380. break;
  381. }
  382. }
  383. mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
  384. mii_data->reg_num, val);
  385. if (mii_data->phy_id == phydev->mdio.addr &&
  386. mii_data->reg_num == MII_BMCR &&
  387. val & BMCR_RESET)
  388. return phy_init_hw(phydev);
  389. if (change_autoneg)
  390. return phy_start_aneg(phydev);
  391. return 0;
  392. case SIOCSHWTSTAMP:
  393. if (phydev->drv && phydev->drv->hwtstamp)
  394. return phydev->drv->hwtstamp(phydev, ifr);
  395. /* fall through */
  396. default:
  397. return -EOPNOTSUPP;
  398. }
  399. }
  400. EXPORT_SYMBOL(phy_mii_ioctl);
  401. static int phy_config_aneg(struct phy_device *phydev)
  402. {
  403. if (phydev->drv->config_aneg)
  404. return phydev->drv->config_aneg(phydev);
  405. /* Clause 45 PHYs that don't implement Clause 22 registers are not
  406. * allowed to call genphy_config_aneg()
  407. */
  408. if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
  409. return -EOPNOTSUPP;
  410. return genphy_config_aneg(phydev);
  411. }
  412. /**
  413. * phy_start_aneg_priv - start auto-negotiation for this PHY device
  414. * @phydev: the phy_device struct
  415. * @sync: indicate whether we should wait for the workqueue cancelation
  416. *
  417. * Description: Sanitizes the settings (if we're not autonegotiating
  418. * them), and then calls the driver's config_aneg function.
  419. * If the PHYCONTROL Layer is operating, we change the state to
  420. * reflect the beginning of Auto-negotiation or forcing.
  421. */
  422. static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
  423. {
  424. bool trigger = 0;
  425. int err;
  426. if (!phydev->drv)
  427. return -EIO;
  428. mutex_lock(&phydev->lock);
  429. if (AUTONEG_DISABLE == phydev->autoneg)
  430. phy_sanitize_settings(phydev);
  431. /* Invalidate LP advertising flags */
  432. phydev->lp_advertising = 0;
  433. err = phy_config_aneg(phydev);
  434. if (err < 0)
  435. goto out_unlock;
  436. if (phydev->state != PHY_HALTED) {
  437. if (AUTONEG_ENABLE == phydev->autoneg) {
  438. phydev->state = PHY_AN;
  439. phydev->link_timeout = PHY_AN_TIMEOUT;
  440. } else {
  441. phydev->state = PHY_FORCING;
  442. phydev->link_timeout = PHY_FORCE_TIMEOUT;
  443. }
  444. }
  445. /* Re-schedule a PHY state machine to check PHY status because
  446. * negotiation may already be done and aneg interrupt may not be
  447. * generated.
  448. */
  449. if (!phy_polling_mode(phydev) && phydev->state == PHY_AN) {
  450. err = phy_aneg_done(phydev);
  451. if (err > 0) {
  452. trigger = true;
  453. err = 0;
  454. }
  455. }
  456. out_unlock:
  457. mutex_unlock(&phydev->lock);
  458. if (trigger)
  459. phy_trigger_machine(phydev, sync);
  460. return err;
  461. }
  462. /**
  463. * phy_start_aneg - start auto-negotiation for this PHY device
  464. * @phydev: the phy_device struct
  465. *
  466. * Description: Sanitizes the settings (if we're not autonegotiating
  467. * them), and then calls the driver's config_aneg function.
  468. * If the PHYCONTROL Layer is operating, we change the state to
  469. * reflect the beginning of Auto-negotiation or forcing.
  470. */
  471. int phy_start_aneg(struct phy_device *phydev)
  472. {
  473. return phy_start_aneg_priv(phydev, true);
  474. }
  475. EXPORT_SYMBOL(phy_start_aneg);
  476. static int phy_poll_aneg_done(struct phy_device *phydev)
  477. {
  478. unsigned int retries = 100;
  479. int ret;
  480. do {
  481. msleep(100);
  482. ret = phy_aneg_done(phydev);
  483. } while (!ret && --retries);
  484. if (!ret)
  485. return -ETIMEDOUT;
  486. return ret < 0 ? ret : 0;
  487. }
  488. /**
  489. * phy_speed_down - set speed to lowest speed supported by both link partners
  490. * @phydev: the phy_device struct
  491. * @sync: perform action synchronously
  492. *
  493. * Description: Typically used to save energy when waiting for a WoL packet
  494. *
  495. * WARNING: Setting sync to false may cause the system being unable to suspend
  496. * in case the PHY generates an interrupt when finishing the autonegotiation.
  497. * This interrupt may wake up the system immediately after suspend.
  498. * Therefore use sync = false only if you're sure it's safe with the respective
  499. * network chip.
  500. */
  501. int phy_speed_down(struct phy_device *phydev, bool sync)
  502. {
  503. u32 adv = phydev->lp_advertising & phydev->supported;
  504. u32 adv_old = phydev->advertising;
  505. int ret;
  506. if (phydev->autoneg != AUTONEG_ENABLE)
  507. return 0;
  508. if (adv & PHY_10BT_FEATURES)
  509. phydev->advertising &= ~(PHY_100BT_FEATURES |
  510. PHY_1000BT_FEATURES);
  511. else if (adv & PHY_100BT_FEATURES)
  512. phydev->advertising &= ~PHY_1000BT_FEATURES;
  513. if (phydev->advertising == adv_old)
  514. return 0;
  515. ret = phy_config_aneg(phydev);
  516. if (ret)
  517. return ret;
  518. return sync ? phy_poll_aneg_done(phydev) : 0;
  519. }
  520. EXPORT_SYMBOL_GPL(phy_speed_down);
  521. /**
  522. * phy_speed_up - (re)set advertised speeds to all supported speeds
  523. * @phydev: the phy_device struct
  524. *
  525. * Description: Used to revert the effect of phy_speed_down
  526. */
  527. int phy_speed_up(struct phy_device *phydev)
  528. {
  529. u32 mask = PHY_10BT_FEATURES | PHY_100BT_FEATURES | PHY_1000BT_FEATURES;
  530. u32 adv_old = phydev->advertising;
  531. if (phydev->autoneg != AUTONEG_ENABLE)
  532. return 0;
  533. phydev->advertising = (adv_old & ~mask) | (phydev->supported & mask);
  534. if (phydev->advertising == adv_old)
  535. return 0;
  536. return phy_config_aneg(phydev);
  537. }
  538. EXPORT_SYMBOL_GPL(phy_speed_up);
  539. /**
  540. * phy_start_machine - start PHY state machine tracking
  541. * @phydev: the phy_device struct
  542. *
  543. * Description: The PHY infrastructure can run a state machine
  544. * which tracks whether the PHY is starting up, negotiating,
  545. * etc. This function starts the delayed workqueue which tracks
  546. * the state of the PHY. If you want to maintain your own state machine,
  547. * do not call this function.
  548. */
  549. void phy_start_machine(struct phy_device *phydev)
  550. {
  551. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
  552. }
  553. EXPORT_SYMBOL_GPL(phy_start_machine);
  554. /**
  555. * phy_trigger_machine - trigger the state machine to run
  556. *
  557. * @phydev: the phy_device struct
  558. * @sync: indicate whether we should wait for the workqueue cancelation
  559. *
  560. * Description: There has been a change in state which requires that the
  561. * state machine runs.
  562. */
  563. void phy_trigger_machine(struct phy_device *phydev, bool sync)
  564. {
  565. if (sync)
  566. cancel_delayed_work_sync(&phydev->state_queue);
  567. else
  568. cancel_delayed_work(&phydev->state_queue);
  569. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
  570. }
  571. /**
  572. * phy_stop_machine - stop the PHY state machine tracking
  573. * @phydev: target phy_device struct
  574. *
  575. * Description: Stops the state machine delayed workqueue, sets the
  576. * state to UP (unless it wasn't up yet). This function must be
  577. * called BEFORE phy_detach.
  578. */
  579. void phy_stop_machine(struct phy_device *phydev)
  580. {
  581. cancel_delayed_work_sync(&phydev->state_queue);
  582. mutex_lock(&phydev->lock);
  583. if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
  584. phydev->state = PHY_UP;
  585. mutex_unlock(&phydev->lock);
  586. }
  587. /**
  588. * phy_error - enter HALTED state for this PHY device
  589. * @phydev: target phy_device struct
  590. *
  591. * Moves the PHY to the HALTED state in response to a read
  592. * or write error, and tells the controller the link is down.
  593. * Must not be called from interrupt context, or while the
  594. * phydev->lock is held.
  595. */
  596. static void phy_error(struct phy_device *phydev)
  597. {
  598. mutex_lock(&phydev->lock);
  599. phydev->state = PHY_HALTED;
  600. mutex_unlock(&phydev->lock);
  601. phy_trigger_machine(phydev, false);
  602. }
  603. /**
  604. * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
  605. * @phydev: target phy_device struct
  606. */
  607. static int phy_disable_interrupts(struct phy_device *phydev)
  608. {
  609. int err;
  610. /* Disable PHY interrupts */
  611. err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  612. if (err)
  613. return err;
  614. /* Clear the interrupt */
  615. return phy_clear_interrupt(phydev);
  616. }
  617. /**
  618. * phy_change - Called by the phy_interrupt to handle PHY changes
  619. * @phydev: phy_device struct that interrupted
  620. */
  621. static irqreturn_t phy_change(struct phy_device *phydev)
  622. {
  623. if (phy_interrupt_is_valid(phydev)) {
  624. if (phydev->drv->did_interrupt &&
  625. !phydev->drv->did_interrupt(phydev))
  626. return IRQ_NONE;
  627. if (phydev->state == PHY_HALTED)
  628. if (phy_disable_interrupts(phydev))
  629. goto phy_err;
  630. }
  631. mutex_lock(&phydev->lock);
  632. if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
  633. phydev->state = PHY_CHANGELINK;
  634. mutex_unlock(&phydev->lock);
  635. /* reschedule state queue work to run as soon as possible */
  636. phy_trigger_machine(phydev, true);
  637. if (phy_interrupt_is_valid(phydev) && phy_clear_interrupt(phydev))
  638. goto phy_err;
  639. return IRQ_HANDLED;
  640. phy_err:
  641. phy_error(phydev);
  642. return IRQ_NONE;
  643. }
  644. /**
  645. * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
  646. * @work: work_struct that describes the work to be done
  647. */
  648. void phy_change_work(struct work_struct *work)
  649. {
  650. struct phy_device *phydev =
  651. container_of(work, struct phy_device, phy_queue);
  652. phy_change(phydev);
  653. }
  654. /**
  655. * phy_interrupt - PHY interrupt handler
  656. * @irq: interrupt line
  657. * @phy_dat: phy_device pointer
  658. *
  659. * Description: When a PHY interrupt occurs, the handler disables
  660. * interrupts, and uses phy_change to handle the interrupt.
  661. */
  662. static irqreturn_t phy_interrupt(int irq, void *phy_dat)
  663. {
  664. struct phy_device *phydev = phy_dat;
  665. if (PHY_HALTED == phydev->state)
  666. return IRQ_NONE; /* It can't be ours. */
  667. return phy_change(phydev);
  668. }
  669. /**
  670. * phy_enable_interrupts - Enable the interrupts from the PHY side
  671. * @phydev: target phy_device struct
  672. */
  673. static int phy_enable_interrupts(struct phy_device *phydev)
  674. {
  675. int err = phy_clear_interrupt(phydev);
  676. if (err < 0)
  677. return err;
  678. return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  679. }
  680. /**
  681. * phy_start_interrupts - request and enable interrupts for a PHY device
  682. * @phydev: target phy_device struct
  683. *
  684. * Description: Request the interrupt for the given PHY.
  685. * If this fails, then we set irq to PHY_POLL.
  686. * Otherwise, we enable the interrupts in the PHY.
  687. * This should only be called with a valid IRQ number.
  688. * Returns 0 on success or < 0 on error.
  689. */
  690. int phy_start_interrupts(struct phy_device *phydev)
  691. {
  692. if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
  693. IRQF_ONESHOT | IRQF_SHARED,
  694. phydev_name(phydev), phydev) < 0) {
  695. pr_warn("%s: Can't get IRQ %d (PHY)\n",
  696. phydev->mdio.bus->name, phydev->irq);
  697. phydev->irq = PHY_POLL;
  698. return 0;
  699. }
  700. return phy_enable_interrupts(phydev);
  701. }
  702. EXPORT_SYMBOL(phy_start_interrupts);
  703. /**
  704. * phy_stop_interrupts - disable interrupts from a PHY device
  705. * @phydev: target phy_device struct
  706. */
  707. int phy_stop_interrupts(struct phy_device *phydev)
  708. {
  709. int err = phy_disable_interrupts(phydev);
  710. if (err)
  711. phy_error(phydev);
  712. free_irq(phydev->irq, phydev);
  713. return err;
  714. }
  715. EXPORT_SYMBOL(phy_stop_interrupts);
  716. /**
  717. * phy_stop - Bring down the PHY link, and stop checking the status
  718. * @phydev: target phy_device struct
  719. */
  720. void phy_stop(struct phy_device *phydev)
  721. {
  722. mutex_lock(&phydev->lock);
  723. if (PHY_HALTED == phydev->state)
  724. goto out_unlock;
  725. if (phy_interrupt_is_valid(phydev))
  726. phy_disable_interrupts(phydev);
  727. phydev->state = PHY_HALTED;
  728. out_unlock:
  729. mutex_unlock(&phydev->lock);
  730. /* Cannot call flush_scheduled_work() here as desired because
  731. * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
  732. * will not reenable interrupts.
  733. */
  734. }
  735. EXPORT_SYMBOL(phy_stop);
  736. /**
  737. * phy_start - start or restart a PHY device
  738. * @phydev: target phy_device struct
  739. *
  740. * Description: Indicates the attached device's readiness to
  741. * handle PHY-related work. Used during startup to start the
  742. * PHY, and after a call to phy_stop() to resume operation.
  743. * Also used to indicate the MDIO bus has cleared an error
  744. * condition.
  745. */
  746. void phy_start(struct phy_device *phydev)
  747. {
  748. int err = 0;
  749. mutex_lock(&phydev->lock);
  750. switch (phydev->state) {
  751. case PHY_STARTING:
  752. phydev->state = PHY_PENDING;
  753. break;
  754. case PHY_READY:
  755. phydev->state = PHY_UP;
  756. break;
  757. case PHY_HALTED:
  758. /* if phy was suspended, bring the physical link up again */
  759. __phy_resume(phydev);
  760. /* make sure interrupts are re-enabled for the PHY */
  761. if (phy_interrupt_is_valid(phydev)) {
  762. err = phy_enable_interrupts(phydev);
  763. if (err < 0)
  764. break;
  765. }
  766. phydev->state = PHY_RESUMING;
  767. break;
  768. default:
  769. break;
  770. }
  771. mutex_unlock(&phydev->lock);
  772. phy_trigger_machine(phydev, true);
  773. }
  774. EXPORT_SYMBOL(phy_start);
  775. static void phy_link_up(struct phy_device *phydev)
  776. {
  777. phydev->phy_link_change(phydev, true, true);
  778. phy_led_trigger_change_speed(phydev);
  779. }
  780. static void phy_link_down(struct phy_device *phydev, bool do_carrier)
  781. {
  782. phydev->phy_link_change(phydev, false, do_carrier);
  783. phy_led_trigger_change_speed(phydev);
  784. }
  785. /**
  786. * phy_state_machine - Handle the state machine
  787. * @work: work_struct that describes the work to be done
  788. */
  789. void phy_state_machine(struct work_struct *work)
  790. {
  791. struct delayed_work *dwork = to_delayed_work(work);
  792. struct phy_device *phydev =
  793. container_of(dwork, struct phy_device, state_queue);
  794. bool needs_aneg = false, do_suspend = false;
  795. enum phy_state old_state;
  796. int err = 0;
  797. int old_link;
  798. mutex_lock(&phydev->lock);
  799. old_state = phydev->state;
  800. if (phydev->drv && phydev->drv->link_change_notify)
  801. phydev->drv->link_change_notify(phydev);
  802. switch (phydev->state) {
  803. case PHY_DOWN:
  804. case PHY_STARTING:
  805. case PHY_READY:
  806. case PHY_PENDING:
  807. break;
  808. case PHY_UP:
  809. needs_aneg = true;
  810. phydev->link_timeout = PHY_AN_TIMEOUT;
  811. break;
  812. case PHY_AN:
  813. err = phy_read_status(phydev);
  814. if (err < 0)
  815. break;
  816. /* If the link is down, give up on negotiation for now */
  817. if (!phydev->link) {
  818. phydev->state = PHY_NOLINK;
  819. phy_link_down(phydev, true);
  820. break;
  821. }
  822. /* Check if negotiation is done. Break if there's an error */
  823. err = phy_aneg_done(phydev);
  824. if (err < 0)
  825. break;
  826. /* If AN is done, we're running */
  827. if (err > 0) {
  828. phydev->state = PHY_RUNNING;
  829. phy_link_up(phydev);
  830. } else if (0 == phydev->link_timeout--)
  831. needs_aneg = true;
  832. break;
  833. case PHY_NOLINK:
  834. if (!phy_polling_mode(phydev))
  835. break;
  836. err = phy_read_status(phydev);
  837. if (err)
  838. break;
  839. if (phydev->link) {
  840. if (AUTONEG_ENABLE == phydev->autoneg) {
  841. err = phy_aneg_done(phydev);
  842. if (err < 0)
  843. break;
  844. if (!err) {
  845. phydev->state = PHY_AN;
  846. phydev->link_timeout = PHY_AN_TIMEOUT;
  847. break;
  848. }
  849. }
  850. phydev->state = PHY_RUNNING;
  851. phy_link_up(phydev);
  852. }
  853. break;
  854. case PHY_FORCING:
  855. err = genphy_update_link(phydev);
  856. if (err)
  857. break;
  858. if (phydev->link) {
  859. phydev->state = PHY_RUNNING;
  860. phy_link_up(phydev);
  861. } else {
  862. if (0 == phydev->link_timeout--)
  863. needs_aneg = true;
  864. phy_link_down(phydev, false);
  865. }
  866. break;
  867. case PHY_RUNNING:
  868. /* Only register a CHANGE if we are polling and link changed
  869. * since latest checking.
  870. */
  871. if (phy_polling_mode(phydev)) {
  872. old_link = phydev->link;
  873. err = phy_read_status(phydev);
  874. if (err)
  875. break;
  876. if (old_link != phydev->link)
  877. phydev->state = PHY_CHANGELINK;
  878. }
  879. /*
  880. * Failsafe: check that nobody set phydev->link=0 between two
  881. * poll cycles, otherwise we won't leave RUNNING state as long
  882. * as link remains down.
  883. */
  884. if (!phydev->link && phydev->state == PHY_RUNNING) {
  885. phydev->state = PHY_CHANGELINK;
  886. phydev_err(phydev, "no link in PHY_RUNNING\n");
  887. }
  888. break;
  889. case PHY_CHANGELINK:
  890. err = phy_read_status(phydev);
  891. if (err)
  892. break;
  893. if (phydev->link) {
  894. phydev->state = PHY_RUNNING;
  895. phy_link_up(phydev);
  896. } else {
  897. phydev->state = PHY_NOLINK;
  898. phy_link_down(phydev, true);
  899. }
  900. break;
  901. case PHY_HALTED:
  902. if (phydev->link) {
  903. phydev->link = 0;
  904. phy_link_down(phydev, true);
  905. do_suspend = true;
  906. }
  907. break;
  908. case PHY_RESUMING:
  909. if (AUTONEG_ENABLE == phydev->autoneg) {
  910. err = phy_aneg_done(phydev);
  911. if (err < 0)
  912. break;
  913. /* err > 0 if AN is done.
  914. * Otherwise, it's 0, and we're still waiting for AN
  915. */
  916. if (err > 0) {
  917. err = phy_read_status(phydev);
  918. if (err)
  919. break;
  920. if (phydev->link) {
  921. phydev->state = PHY_RUNNING;
  922. phy_link_up(phydev);
  923. } else {
  924. phydev->state = PHY_NOLINK;
  925. phy_link_down(phydev, false);
  926. }
  927. } else {
  928. phydev->state = PHY_AN;
  929. phydev->link_timeout = PHY_AN_TIMEOUT;
  930. }
  931. } else {
  932. err = phy_read_status(phydev);
  933. if (err)
  934. break;
  935. if (phydev->link) {
  936. phydev->state = PHY_RUNNING;
  937. phy_link_up(phydev);
  938. } else {
  939. phydev->state = PHY_NOLINK;
  940. phy_link_down(phydev, false);
  941. }
  942. }
  943. break;
  944. }
  945. mutex_unlock(&phydev->lock);
  946. if (needs_aneg)
  947. err = phy_start_aneg_priv(phydev, false);
  948. else if (do_suspend)
  949. phy_suspend(phydev);
  950. if (err < 0)
  951. phy_error(phydev);
  952. if (old_state != phydev->state)
  953. phydev_dbg(phydev, "PHY state change %s -> %s\n",
  954. phy_state_to_str(old_state),
  955. phy_state_to_str(phydev->state));
  956. /* Only re-schedule a PHY state machine change if we are polling the
  957. * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
  958. * between states from phy_mac_interrupt()
  959. */
  960. if (phy_polling_mode(phydev))
  961. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
  962. PHY_STATE_TIME * HZ);
  963. }
  964. /**
  965. * phy_mac_interrupt - MAC says the link has changed
  966. * @phydev: phy_device struct with changed link
  967. *
  968. * The MAC layer is able to indicate there has been a change in the PHY link
  969. * status. Trigger the state machine and work a work queue.
  970. */
  971. void phy_mac_interrupt(struct phy_device *phydev)
  972. {
  973. /* Trigger a state machine change */
  974. queue_work(system_power_efficient_wq, &phydev->phy_queue);
  975. }
  976. EXPORT_SYMBOL(phy_mac_interrupt);
  977. /**
  978. * phy_init_eee - init and check the EEE feature
  979. * @phydev: target phy_device struct
  980. * @clk_stop_enable: PHY may stop the clock during LPI
  981. *
  982. * Description: it checks if the Energy-Efficient Ethernet (EEE)
  983. * is supported by looking at the MMD registers 3.20 and 7.60/61
  984. * and it programs the MMD register 3.0 setting the "Clock stop enable"
  985. * bit if required.
  986. */
  987. int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
  988. {
  989. if (!phydev->drv)
  990. return -EIO;
  991. /* According to 802.3az,the EEE is supported only in full duplex-mode.
  992. */
  993. if (phydev->duplex == DUPLEX_FULL) {
  994. int eee_lp, eee_cap, eee_adv;
  995. u32 lp, cap, adv;
  996. int status;
  997. /* Read phy status to properly get the right settings */
  998. status = phy_read_status(phydev);
  999. if (status)
  1000. return status;
  1001. /* First check if the EEE ability is supported */
  1002. eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  1003. if (eee_cap <= 0)
  1004. goto eee_exit_err;
  1005. cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
  1006. if (!cap)
  1007. goto eee_exit_err;
  1008. /* Check which link settings negotiated and verify it in
  1009. * the EEE advertising registers.
  1010. */
  1011. eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
  1012. if (eee_lp <= 0)
  1013. goto eee_exit_err;
  1014. eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  1015. if (eee_adv <= 0)
  1016. goto eee_exit_err;
  1017. adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
  1018. lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
  1019. if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
  1020. goto eee_exit_err;
  1021. if (clk_stop_enable) {
  1022. /* Configure the PHY to stop receiving xMII
  1023. * clock while it is signaling LPI.
  1024. */
  1025. int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
  1026. if (val < 0)
  1027. return val;
  1028. val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
  1029. phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
  1030. }
  1031. return 0; /* EEE supported */
  1032. }
  1033. eee_exit_err:
  1034. return -EPROTONOSUPPORT;
  1035. }
  1036. EXPORT_SYMBOL(phy_init_eee);
  1037. /**
  1038. * phy_get_eee_err - report the EEE wake error count
  1039. * @phydev: target phy_device struct
  1040. *
  1041. * Description: it is to report the number of time where the PHY
  1042. * failed to complete its normal wake sequence.
  1043. */
  1044. int phy_get_eee_err(struct phy_device *phydev)
  1045. {
  1046. if (!phydev->drv)
  1047. return -EIO;
  1048. return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
  1049. }
  1050. EXPORT_SYMBOL(phy_get_eee_err);
  1051. /**
  1052. * phy_ethtool_get_eee - get EEE supported and status
  1053. * @phydev: target phy_device struct
  1054. * @data: ethtool_eee data
  1055. *
  1056. * Description: it reportes the Supported/Advertisement/LP Advertisement
  1057. * capabilities.
  1058. */
  1059. int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
  1060. {
  1061. int val;
  1062. if (!phydev->drv)
  1063. return -EIO;
  1064. /* Get Supported EEE */
  1065. val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  1066. if (val < 0)
  1067. return val;
  1068. data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
  1069. /* Get advertisement EEE */
  1070. val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  1071. if (val < 0)
  1072. return val;
  1073. data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  1074. /* Get LP advertisement EEE */
  1075. val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
  1076. if (val < 0)
  1077. return val;
  1078. data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  1079. return 0;
  1080. }
  1081. EXPORT_SYMBOL(phy_ethtool_get_eee);
  1082. /**
  1083. * phy_ethtool_set_eee - set EEE supported and status
  1084. * @phydev: target phy_device struct
  1085. * @data: ethtool_eee data
  1086. *
  1087. * Description: it is to program the Advertisement EEE register.
  1088. */
  1089. int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
  1090. {
  1091. int cap, old_adv, adv, ret;
  1092. if (!phydev->drv)
  1093. return -EIO;
  1094. /* Get Supported EEE */
  1095. cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  1096. if (cap < 0)
  1097. return cap;
  1098. old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  1099. if (old_adv < 0)
  1100. return old_adv;
  1101. adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
  1102. /* Mask prohibited EEE modes */
  1103. adv &= ~phydev->eee_broken_modes;
  1104. if (old_adv != adv) {
  1105. ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
  1106. if (ret < 0)
  1107. return ret;
  1108. /* Restart autonegotiation so the new modes get sent to the
  1109. * link partner.
  1110. */
  1111. if (phydev->autoneg == AUTONEG_ENABLE) {
  1112. ret = phy_restart_aneg(phydev);
  1113. if (ret < 0)
  1114. return ret;
  1115. }
  1116. }
  1117. return 0;
  1118. }
  1119. EXPORT_SYMBOL(phy_ethtool_set_eee);
  1120. int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1121. {
  1122. if (phydev->drv && phydev->drv->set_wol)
  1123. return phydev->drv->set_wol(phydev, wol);
  1124. return -EOPNOTSUPP;
  1125. }
  1126. EXPORT_SYMBOL(phy_ethtool_set_wol);
  1127. void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1128. {
  1129. if (phydev->drv && phydev->drv->get_wol)
  1130. phydev->drv->get_wol(phydev, wol);
  1131. }
  1132. EXPORT_SYMBOL(phy_ethtool_get_wol);
  1133. int phy_ethtool_get_link_ksettings(struct net_device *ndev,
  1134. struct ethtool_link_ksettings *cmd)
  1135. {
  1136. struct phy_device *phydev = ndev->phydev;
  1137. if (!phydev)
  1138. return -ENODEV;
  1139. phy_ethtool_ksettings_get(phydev, cmd);
  1140. return 0;
  1141. }
  1142. EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
  1143. int phy_ethtool_set_link_ksettings(struct net_device *ndev,
  1144. const struct ethtool_link_ksettings *cmd)
  1145. {
  1146. struct phy_device *phydev = ndev->phydev;
  1147. if (!phydev)
  1148. return -ENODEV;
  1149. return phy_ethtool_ksettings_set(phydev, cmd);
  1150. }
  1151. EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
  1152. int phy_ethtool_nway_reset(struct net_device *ndev)
  1153. {
  1154. struct phy_device *phydev = ndev->phydev;
  1155. if (!phydev)
  1156. return -ENODEV;
  1157. if (!phydev->drv)
  1158. return -EIO;
  1159. return phy_restart_aneg(phydev);
  1160. }
  1161. EXPORT_SYMBOL(phy_ethtool_nway_reset);