phy.c 36 KB

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