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. 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. /* Clause 45 PHYs that don't implement Clause 22 registers are not
  404. * allowed to call genphy_config_aneg()
  405. */
  406. if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
  407. return -EOPNOTSUPP;
  408. return genphy_config_aneg(phydev);
  409. }
  410. /**
  411. * phy_start_aneg_priv - start auto-negotiation for this PHY device
  412. * @phydev: the phy_device struct
  413. * @sync: indicate whether we should wait for the workqueue cancelation
  414. *
  415. * Description: Sanitizes the settings (if we're not autonegotiating
  416. * them), and then calls the driver's config_aneg function.
  417. * If the PHYCONTROL Layer is operating, we change the state to
  418. * reflect the beginning of Auto-negotiation or forcing.
  419. */
  420. static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
  421. {
  422. bool trigger = 0;
  423. int err;
  424. if (!phydev->drv)
  425. return -EIO;
  426. mutex_lock(&phydev->lock);
  427. if (AUTONEG_DISABLE == phydev->autoneg)
  428. phy_sanitize_settings(phydev);
  429. /* Invalidate LP advertising flags */
  430. phydev->lp_advertising = 0;
  431. err = phy_config_aneg(phydev);
  432. if (err < 0)
  433. goto out_unlock;
  434. if (phydev->state != PHY_HALTED) {
  435. if (AUTONEG_ENABLE == phydev->autoneg) {
  436. phydev->state = PHY_AN;
  437. phydev->link_timeout = PHY_AN_TIMEOUT;
  438. } else {
  439. phydev->state = PHY_FORCING;
  440. phydev->link_timeout = PHY_FORCE_TIMEOUT;
  441. }
  442. }
  443. /* Re-schedule a PHY state machine to check PHY status because
  444. * negotiation may already be done and aneg interrupt may not be
  445. * generated.
  446. */
  447. if (!phy_polling_mode(phydev) && phydev->state == PHY_AN) {
  448. err = phy_aneg_done(phydev);
  449. if (err > 0) {
  450. trigger = true;
  451. err = 0;
  452. }
  453. }
  454. out_unlock:
  455. mutex_unlock(&phydev->lock);
  456. if (trigger)
  457. phy_trigger_machine(phydev, sync);
  458. return err;
  459. }
  460. /**
  461. * phy_start_aneg - start auto-negotiation for this PHY device
  462. * @phydev: the phy_device struct
  463. *
  464. * Description: Sanitizes the settings (if we're not autonegotiating
  465. * them), and then calls the driver's config_aneg function.
  466. * If the PHYCONTROL Layer is operating, we change the state to
  467. * reflect the beginning of Auto-negotiation or forcing.
  468. */
  469. int phy_start_aneg(struct phy_device *phydev)
  470. {
  471. return phy_start_aneg_priv(phydev, true);
  472. }
  473. EXPORT_SYMBOL(phy_start_aneg);
  474. static int phy_poll_aneg_done(struct phy_device *phydev)
  475. {
  476. unsigned int retries = 100;
  477. int ret;
  478. do {
  479. msleep(100);
  480. ret = phy_aneg_done(phydev);
  481. } while (!ret && --retries);
  482. if (!ret)
  483. return -ETIMEDOUT;
  484. return ret < 0 ? ret : 0;
  485. }
  486. /**
  487. * phy_speed_down - set speed to lowest speed supported by both link partners
  488. * @phydev: the phy_device struct
  489. * @sync: perform action synchronously
  490. *
  491. * Description: Typically used to save energy when waiting for a WoL packet
  492. *
  493. * WARNING: Setting sync to false may cause the system being unable to suspend
  494. * in case the PHY generates an interrupt when finishing the autonegotiation.
  495. * This interrupt may wake up the system immediately after suspend.
  496. * Therefore use sync = false only if you're sure it's safe with the respective
  497. * network chip.
  498. */
  499. int phy_speed_down(struct phy_device *phydev, bool sync)
  500. {
  501. u32 adv = phydev->lp_advertising & phydev->supported;
  502. u32 adv_old = phydev->advertising;
  503. int ret;
  504. if (phydev->autoneg != AUTONEG_ENABLE)
  505. return 0;
  506. if (adv & PHY_10BT_FEATURES)
  507. phydev->advertising &= ~(PHY_100BT_FEATURES |
  508. PHY_1000BT_FEATURES);
  509. else if (adv & PHY_100BT_FEATURES)
  510. phydev->advertising &= ~PHY_1000BT_FEATURES;
  511. if (phydev->advertising == adv_old)
  512. return 0;
  513. ret = phy_config_aneg(phydev);
  514. if (ret)
  515. return ret;
  516. return sync ? phy_poll_aneg_done(phydev) : 0;
  517. }
  518. EXPORT_SYMBOL_GPL(phy_speed_down);
  519. /**
  520. * phy_speed_up - (re)set advertised speeds to all supported speeds
  521. * @phydev: the phy_device struct
  522. *
  523. * Description: Used to revert the effect of phy_speed_down
  524. */
  525. int phy_speed_up(struct phy_device *phydev)
  526. {
  527. u32 mask = PHY_10BT_FEATURES | PHY_100BT_FEATURES | PHY_1000BT_FEATURES;
  528. u32 adv_old = phydev->advertising;
  529. if (phydev->autoneg != AUTONEG_ENABLE)
  530. return 0;
  531. phydev->advertising = (adv_old & ~mask) | (phydev->supported & mask);
  532. if (phydev->advertising == adv_old)
  533. return 0;
  534. return phy_config_aneg(phydev);
  535. }
  536. EXPORT_SYMBOL_GPL(phy_speed_up);
  537. /**
  538. * phy_start_machine - start PHY state machine tracking
  539. * @phydev: the phy_device struct
  540. *
  541. * Description: The PHY infrastructure can run a state machine
  542. * which tracks whether the PHY is starting up, negotiating,
  543. * etc. This function starts the delayed workqueue which tracks
  544. * the state of the PHY. If you want to maintain your own state machine,
  545. * do not call this function.
  546. */
  547. void phy_start_machine(struct phy_device *phydev)
  548. {
  549. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
  550. }
  551. EXPORT_SYMBOL_GPL(phy_start_machine);
  552. /**
  553. * phy_trigger_machine - trigger the state machine to run
  554. *
  555. * @phydev: the phy_device struct
  556. * @sync: indicate whether we should wait for the workqueue cancelation
  557. *
  558. * Description: There has been a change in state which requires that the
  559. * state machine runs.
  560. */
  561. void phy_trigger_machine(struct phy_device *phydev, bool sync)
  562. {
  563. if (sync)
  564. cancel_delayed_work_sync(&phydev->state_queue);
  565. else
  566. cancel_delayed_work(&phydev->state_queue);
  567. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
  568. }
  569. /**
  570. * phy_stop_machine - stop the PHY state machine tracking
  571. * @phydev: target phy_device struct
  572. *
  573. * Description: Stops the state machine delayed workqueue, sets the
  574. * state to UP (unless it wasn't up yet). This function must be
  575. * called BEFORE phy_detach.
  576. */
  577. void phy_stop_machine(struct phy_device *phydev)
  578. {
  579. cancel_delayed_work_sync(&phydev->state_queue);
  580. mutex_lock(&phydev->lock);
  581. if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
  582. phydev->state = PHY_UP;
  583. mutex_unlock(&phydev->lock);
  584. }
  585. /**
  586. * phy_error - enter HALTED state for this PHY device
  587. * @phydev: target phy_device struct
  588. *
  589. * Moves the PHY to the HALTED state in response to a read
  590. * or write error, and tells the controller the link is down.
  591. * Must not be called from interrupt context, or while the
  592. * phydev->lock is held.
  593. */
  594. static void phy_error(struct phy_device *phydev)
  595. {
  596. mutex_lock(&phydev->lock);
  597. phydev->state = PHY_HALTED;
  598. mutex_unlock(&phydev->lock);
  599. phy_trigger_machine(phydev, false);
  600. }
  601. /**
  602. * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
  603. * @phydev: target phy_device struct
  604. */
  605. static int phy_disable_interrupts(struct phy_device *phydev)
  606. {
  607. int err;
  608. /* Disable PHY interrupts */
  609. err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  610. if (err)
  611. return err;
  612. /* Clear the interrupt */
  613. return phy_clear_interrupt(phydev);
  614. }
  615. /**
  616. * phy_change - Called by the phy_interrupt to handle PHY changes
  617. * @phydev: phy_device struct that interrupted
  618. */
  619. static irqreturn_t phy_change(struct phy_device *phydev)
  620. {
  621. if (phy_interrupt_is_valid(phydev)) {
  622. if (phydev->drv->did_interrupt &&
  623. !phydev->drv->did_interrupt(phydev))
  624. return IRQ_NONE;
  625. if (phydev->state == PHY_HALTED)
  626. if (phy_disable_interrupts(phydev))
  627. goto phy_err;
  628. }
  629. mutex_lock(&phydev->lock);
  630. if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
  631. phydev->state = PHY_CHANGELINK;
  632. mutex_unlock(&phydev->lock);
  633. /* reschedule state queue work to run as soon as possible */
  634. phy_trigger_machine(phydev, true);
  635. if (phy_interrupt_is_valid(phydev) && phy_clear_interrupt(phydev))
  636. goto phy_err;
  637. return IRQ_HANDLED;
  638. phy_err:
  639. phy_error(phydev);
  640. return IRQ_NONE;
  641. }
  642. /**
  643. * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
  644. * @work: work_struct that describes the work to be done
  645. */
  646. void phy_change_work(struct work_struct *work)
  647. {
  648. struct phy_device *phydev =
  649. container_of(work, struct phy_device, phy_queue);
  650. phy_change(phydev);
  651. }
  652. /**
  653. * phy_interrupt - PHY interrupt handler
  654. * @irq: interrupt line
  655. * @phy_dat: phy_device pointer
  656. *
  657. * Description: When a PHY interrupt occurs, the handler disables
  658. * interrupts, and uses phy_change to handle the interrupt.
  659. */
  660. static irqreturn_t phy_interrupt(int irq, void *phy_dat)
  661. {
  662. struct phy_device *phydev = phy_dat;
  663. if (PHY_HALTED == phydev->state)
  664. return IRQ_NONE; /* It can't be ours. */
  665. return phy_change(phydev);
  666. }
  667. /**
  668. * phy_enable_interrupts - Enable the interrupts from the PHY side
  669. * @phydev: target phy_device struct
  670. */
  671. static int phy_enable_interrupts(struct phy_device *phydev)
  672. {
  673. int err = phy_clear_interrupt(phydev);
  674. if (err < 0)
  675. return err;
  676. return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  677. }
  678. /**
  679. * phy_start_interrupts - request and enable interrupts for a PHY device
  680. * @phydev: target phy_device struct
  681. *
  682. * Description: Request the interrupt for the given PHY.
  683. * If this fails, then we set irq to PHY_POLL.
  684. * Otherwise, we enable the interrupts in the PHY.
  685. * This should only be called with a valid IRQ number.
  686. * Returns 0 on success or < 0 on error.
  687. */
  688. int phy_start_interrupts(struct phy_device *phydev)
  689. {
  690. if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
  691. IRQF_ONESHOT | IRQF_SHARED,
  692. phydev_name(phydev), phydev) < 0) {
  693. pr_warn("%s: Can't get IRQ %d (PHY)\n",
  694. phydev->mdio.bus->name, phydev->irq);
  695. phydev->irq = PHY_POLL;
  696. return 0;
  697. }
  698. return phy_enable_interrupts(phydev);
  699. }
  700. EXPORT_SYMBOL(phy_start_interrupts);
  701. /**
  702. * phy_stop_interrupts - disable interrupts from a PHY device
  703. * @phydev: target phy_device struct
  704. */
  705. int phy_stop_interrupts(struct phy_device *phydev)
  706. {
  707. int err = phy_disable_interrupts(phydev);
  708. if (err)
  709. phy_error(phydev);
  710. free_irq(phydev->irq, phydev);
  711. return err;
  712. }
  713. EXPORT_SYMBOL(phy_stop_interrupts);
  714. /**
  715. * phy_stop - Bring down the PHY link, and stop checking the status
  716. * @phydev: target phy_device struct
  717. */
  718. void phy_stop(struct phy_device *phydev)
  719. {
  720. mutex_lock(&phydev->lock);
  721. if (PHY_HALTED == phydev->state)
  722. goto out_unlock;
  723. if (phy_interrupt_is_valid(phydev))
  724. phy_disable_interrupts(phydev);
  725. phydev->state = PHY_HALTED;
  726. out_unlock:
  727. mutex_unlock(&phydev->lock);
  728. phy_state_machine(&phydev->state_queue.work);
  729. phy_stop_machine(phydev);
  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. ret = phy_restart_aneg(phydev);
  1112. if (ret < 0)
  1113. return ret;
  1114. }
  1115. return 0;
  1116. }
  1117. EXPORT_SYMBOL(phy_ethtool_set_eee);
  1118. int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1119. {
  1120. if (phydev->drv && phydev->drv->set_wol)
  1121. return phydev->drv->set_wol(phydev, wol);
  1122. return -EOPNOTSUPP;
  1123. }
  1124. EXPORT_SYMBOL(phy_ethtool_set_wol);
  1125. void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1126. {
  1127. if (phydev->drv && phydev->drv->get_wol)
  1128. phydev->drv->get_wol(phydev, wol);
  1129. }
  1130. EXPORT_SYMBOL(phy_ethtool_get_wol);
  1131. int phy_ethtool_get_link_ksettings(struct net_device *ndev,
  1132. struct ethtool_link_ksettings *cmd)
  1133. {
  1134. struct phy_device *phydev = ndev->phydev;
  1135. if (!phydev)
  1136. return -ENODEV;
  1137. phy_ethtool_ksettings_get(phydev, cmd);
  1138. return 0;
  1139. }
  1140. EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
  1141. int phy_ethtool_set_link_ksettings(struct net_device *ndev,
  1142. const struct ethtool_link_ksettings *cmd)
  1143. {
  1144. struct phy_device *phydev = ndev->phydev;
  1145. if (!phydev)
  1146. return -ENODEV;
  1147. return phy_ethtool_ksettings_set(phydev, cmd);
  1148. }
  1149. EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
  1150. int phy_ethtool_nway_reset(struct net_device *ndev)
  1151. {
  1152. struct phy_device *phydev = ndev->phydev;
  1153. if (!phydev)
  1154. return -ENODEV;
  1155. if (!phydev->drv)
  1156. return -EIO;
  1157. return phy_restart_aneg(phydev);
  1158. }
  1159. EXPORT_SYMBOL(phy_ethtool_nway_reset);