phy.c 31 KB

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