phy.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * drivers/net/phy/phy.c
  3. *
  4. * Framework for configuring and reading PHY devices
  5. * Based on code in sungem_phy.c and gianfar_phy.c
  6. *
  7. * Author: Andy Fleming
  8. *
  9. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/string.h>
  20. #include <linux/errno.h>
  21. #include <linux/unistd.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/mm.h>
  31. #include <linux/module.h>
  32. #include <linux/mii.h>
  33. #include <linux/ethtool.h>
  34. #include <linux/phy.h>
  35. #include <asm/io.h>
  36. #include <asm/irq.h>
  37. #include <asm/uaccess.h>
  38. /* Convenience function to print out the current phy status
  39. */
  40. void phy_print_status(struct phy_device *phydev)
  41. {
  42. pr_info("PHY: %s - Link is %s", phydev->dev.bus_id,
  43. phydev->link ? "Up" : "Down");
  44. if (phydev->link)
  45. printk(" - %d/%s", phydev->speed,
  46. DUPLEX_FULL == phydev->duplex ?
  47. "Full" : "Half");
  48. printk("\n");
  49. }
  50. EXPORT_SYMBOL(phy_print_status);
  51. /* Convenience functions for reading/writing a given PHY
  52. * register. They MUST NOT be called from interrupt context,
  53. * because the bus read/write functions may wait for an interrupt
  54. * to conclude the operation. */
  55. int phy_read(struct phy_device *phydev, u16 regnum)
  56. {
  57. int retval;
  58. struct mii_bus *bus = phydev->bus;
  59. spin_lock_bh(&bus->mdio_lock);
  60. retval = bus->read(bus, phydev->addr, regnum);
  61. spin_unlock_bh(&bus->mdio_lock);
  62. return retval;
  63. }
  64. EXPORT_SYMBOL(phy_read);
  65. int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
  66. {
  67. int err;
  68. struct mii_bus *bus = phydev->bus;
  69. spin_lock_bh(&bus->mdio_lock);
  70. err = bus->write(bus, phydev->addr, regnum, val);
  71. spin_unlock_bh(&bus->mdio_lock);
  72. return err;
  73. }
  74. EXPORT_SYMBOL(phy_write);
  75. int phy_clear_interrupt(struct phy_device *phydev)
  76. {
  77. int err = 0;
  78. if (phydev->drv->ack_interrupt)
  79. err = phydev->drv->ack_interrupt(phydev);
  80. return err;
  81. }
  82. int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
  83. {
  84. int err = 0;
  85. phydev->interrupts = interrupts;
  86. if (phydev->drv->config_intr)
  87. err = phydev->drv->config_intr(phydev);
  88. return err;
  89. }
  90. /* phy_aneg_done
  91. *
  92. * description: Reads the status register and returns 0 either if
  93. * auto-negotiation is incomplete, or if there was an error.
  94. * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
  95. */
  96. static inline int phy_aneg_done(struct phy_device *phydev)
  97. {
  98. int retval;
  99. retval = phy_read(phydev, MII_BMSR);
  100. return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
  101. }
  102. /* A structure for mapping a particular speed and duplex
  103. * combination to a particular SUPPORTED and ADVERTISED value */
  104. struct phy_setting {
  105. int speed;
  106. int duplex;
  107. u32 setting;
  108. };
  109. /* A mapping of all SUPPORTED settings to speed/duplex */
  110. static const struct phy_setting settings[] = {
  111. {
  112. .speed = 10000,
  113. .duplex = DUPLEX_FULL,
  114. .setting = SUPPORTED_10000baseT_Full,
  115. },
  116. {
  117. .speed = SPEED_1000,
  118. .duplex = DUPLEX_FULL,
  119. .setting = SUPPORTED_1000baseT_Full,
  120. },
  121. {
  122. .speed = SPEED_1000,
  123. .duplex = DUPLEX_HALF,
  124. .setting = SUPPORTED_1000baseT_Half,
  125. },
  126. {
  127. .speed = SPEED_100,
  128. .duplex = DUPLEX_FULL,
  129. .setting = SUPPORTED_100baseT_Full,
  130. },
  131. {
  132. .speed = SPEED_100,
  133. .duplex = DUPLEX_HALF,
  134. .setting = SUPPORTED_100baseT_Half,
  135. },
  136. {
  137. .speed = SPEED_10,
  138. .duplex = DUPLEX_FULL,
  139. .setting = SUPPORTED_10baseT_Full,
  140. },
  141. {
  142. .speed = SPEED_10,
  143. .duplex = DUPLEX_HALF,
  144. .setting = SUPPORTED_10baseT_Half,
  145. },
  146. };
  147. #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
  148. /* phy_find_setting
  149. *
  150. * description: Searches the settings array for the setting which
  151. * matches the desired speed and duplex, and returns the index
  152. * of that setting. Returns the index of the last setting if
  153. * none of the others match.
  154. */
  155. static inline int phy_find_setting(int speed, int duplex)
  156. {
  157. int idx = 0;
  158. while (idx < ARRAY_SIZE(settings) &&
  159. (settings[idx].speed != speed ||
  160. settings[idx].duplex != duplex))
  161. idx++;
  162. return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
  163. }
  164. /* phy_find_valid
  165. * idx: The first index in settings[] to search
  166. * features: A mask of the valid settings
  167. *
  168. * description: Returns the index of the first valid setting less
  169. * than or equal to the one pointed to by idx, as determined by
  170. * the mask in features. Returns the index of the last setting
  171. * if nothing else matches.
  172. */
  173. static inline int phy_find_valid(int idx, u32 features)
  174. {
  175. while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
  176. idx++;
  177. return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
  178. }
  179. /* phy_sanitize_settings
  180. *
  181. * description: Make sure the PHY is set to supported speeds and
  182. * duplexes. Drop down by one in this order: 1000/FULL,
  183. * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
  184. */
  185. void phy_sanitize_settings(struct phy_device *phydev)
  186. {
  187. u32 features = phydev->supported;
  188. int idx;
  189. /* Sanitize settings based on PHY capabilities */
  190. if ((features & SUPPORTED_Autoneg) == 0)
  191. phydev->autoneg = 0;
  192. idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
  193. features);
  194. phydev->speed = settings[idx].speed;
  195. phydev->duplex = settings[idx].duplex;
  196. }
  197. EXPORT_SYMBOL(phy_sanitize_settings);
  198. /* phy_ethtool_sset:
  199. * A generic ethtool sset function. Handles all the details
  200. *
  201. * A few notes about parameter checking:
  202. * - We don't set port or transceiver, so we don't care what they
  203. * were set to.
  204. * - phy_start_aneg() will make sure forced settings are sane, and
  205. * choose the next best ones from the ones selected, so we don't
  206. * care if ethtool tries to give us bad values
  207. *
  208. */
  209. int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
  210. {
  211. if (cmd->phy_address != phydev->addr)
  212. return -EINVAL;
  213. /* We make sure that we don't pass unsupported
  214. * values in to the PHY */
  215. cmd->advertising &= phydev->supported;
  216. /* Verify the settings we care about. */
  217. if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
  218. return -EINVAL;
  219. if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
  220. return -EINVAL;
  221. if (cmd->autoneg == AUTONEG_DISABLE
  222. && ((cmd->speed != SPEED_1000
  223. && cmd->speed != SPEED_100
  224. && cmd->speed != SPEED_10)
  225. || (cmd->duplex != DUPLEX_HALF
  226. && cmd->duplex != DUPLEX_FULL)))
  227. return -EINVAL;
  228. phydev->autoneg = cmd->autoneg;
  229. phydev->speed = cmd->speed;
  230. phydev->advertising = cmd->advertising;
  231. if (AUTONEG_ENABLE == cmd->autoneg)
  232. phydev->advertising |= ADVERTISED_Autoneg;
  233. else
  234. phydev->advertising &= ~ADVERTISED_Autoneg;
  235. phydev->duplex = cmd->duplex;
  236. /* Restart the PHY */
  237. phy_start_aneg(phydev);
  238. return 0;
  239. }
  240. int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
  241. {
  242. cmd->supported = phydev->supported;
  243. cmd->advertising = phydev->advertising;
  244. cmd->speed = phydev->speed;
  245. cmd->duplex = phydev->duplex;
  246. cmd->port = PORT_MII;
  247. cmd->phy_address = phydev->addr;
  248. cmd->transceiver = XCVR_EXTERNAL;
  249. cmd->autoneg = phydev->autoneg;
  250. return 0;
  251. }
  252. /* Note that this function is currently incompatible with the
  253. * PHYCONTROL layer. It changes registers without regard to
  254. * current state. Use at own risk
  255. */
  256. int phy_mii_ioctl(struct phy_device *phydev,
  257. struct mii_ioctl_data *mii_data, int cmd)
  258. {
  259. u16 val = mii_data->val_in;
  260. switch (cmd) {
  261. case SIOCGMIIPHY:
  262. mii_data->phy_id = phydev->addr;
  263. break;
  264. case SIOCGMIIREG:
  265. mii_data->val_out = phy_read(phydev, mii_data->reg_num);
  266. break;
  267. case SIOCSMIIREG:
  268. if (!capable(CAP_NET_ADMIN))
  269. return -EPERM;
  270. if (mii_data->phy_id == phydev->addr) {
  271. switch(mii_data->reg_num) {
  272. case MII_BMCR:
  273. if (val & (BMCR_RESET|BMCR_ANENABLE))
  274. phydev->autoneg = AUTONEG_DISABLE;
  275. else
  276. phydev->autoneg = AUTONEG_ENABLE;
  277. if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
  278. phydev->duplex = DUPLEX_FULL;
  279. else
  280. phydev->duplex = DUPLEX_HALF;
  281. break;
  282. case MII_ADVERTISE:
  283. phydev->advertising = val;
  284. break;
  285. default:
  286. /* do nothing */
  287. break;
  288. }
  289. }
  290. phy_write(phydev, mii_data->reg_num, val);
  291. if (mii_data->reg_num == MII_BMCR
  292. && val & BMCR_RESET
  293. && phydev->drv->config_init)
  294. phydev->drv->config_init(phydev);
  295. break;
  296. }
  297. return 0;
  298. }
  299. /* phy_start_aneg
  300. *
  301. * description: Sanitizes the settings (if we're not
  302. * autonegotiating them), and then calls the driver's
  303. * config_aneg function. If the PHYCONTROL Layer is operating,
  304. * we change the state to reflect the beginning of
  305. * Auto-negotiation or forcing.
  306. */
  307. int phy_start_aneg(struct phy_device *phydev)
  308. {
  309. int err;
  310. spin_lock(&phydev->lock);
  311. if (AUTONEG_DISABLE == phydev->autoneg)
  312. phy_sanitize_settings(phydev);
  313. err = phydev->drv->config_aneg(phydev);
  314. if (err < 0)
  315. goto out_unlock;
  316. if (phydev->state != PHY_HALTED) {
  317. if (AUTONEG_ENABLE == phydev->autoneg) {
  318. phydev->state = PHY_AN;
  319. phydev->link_timeout = PHY_AN_TIMEOUT;
  320. } else {
  321. phydev->state = PHY_FORCING;
  322. phydev->link_timeout = PHY_FORCE_TIMEOUT;
  323. }
  324. }
  325. out_unlock:
  326. spin_unlock(&phydev->lock);
  327. return err;
  328. }
  329. EXPORT_SYMBOL(phy_start_aneg);
  330. static void phy_change(void *data);
  331. static void phy_timer(unsigned long data);
  332. /* phy_start_machine:
  333. *
  334. * description: The PHY infrastructure can run a state machine
  335. * which tracks whether the PHY is starting up, negotiating,
  336. * etc. This function starts the timer which tracks the state
  337. * of the PHY. If you want to be notified when the state
  338. * changes, pass in the callback, otherwise, pass NULL. If you
  339. * want to maintain your own state machine, do not call this
  340. * function. */
  341. void phy_start_machine(struct phy_device *phydev,
  342. void (*handler)(struct net_device *))
  343. {
  344. phydev->adjust_state = handler;
  345. init_timer(&phydev->phy_timer);
  346. phydev->phy_timer.function = &phy_timer;
  347. phydev->phy_timer.data = (unsigned long) phydev;
  348. mod_timer(&phydev->phy_timer, jiffies + HZ);
  349. }
  350. /* phy_stop_machine
  351. *
  352. * description: Stops the state machine timer, sets the state to UP
  353. * (unless it wasn't up yet). This function must be called BEFORE
  354. * phy_detach.
  355. */
  356. void phy_stop_machine(struct phy_device *phydev)
  357. {
  358. del_timer_sync(&phydev->phy_timer);
  359. spin_lock(&phydev->lock);
  360. if (phydev->state > PHY_UP)
  361. phydev->state = PHY_UP;
  362. spin_unlock(&phydev->lock);
  363. phydev->adjust_state = NULL;
  364. }
  365. /* phy_force_reduction
  366. *
  367. * description: Reduces the speed/duplex settings by
  368. * one notch. The order is so:
  369. * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
  370. * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
  371. */
  372. static void phy_force_reduction(struct phy_device *phydev)
  373. {
  374. int idx;
  375. idx = phy_find_setting(phydev->speed, phydev->duplex);
  376. idx++;
  377. idx = phy_find_valid(idx, phydev->supported);
  378. phydev->speed = settings[idx].speed;
  379. phydev->duplex = settings[idx].duplex;
  380. pr_info("Trying %d/%s\n", phydev->speed,
  381. DUPLEX_FULL == phydev->duplex ?
  382. "FULL" : "HALF");
  383. }
  384. /* phy_error:
  385. *
  386. * Moves the PHY to the HALTED state in response to a read
  387. * or write error, and tells the controller the link is down.
  388. * Must not be called from interrupt context, or while the
  389. * phydev->lock is held.
  390. */
  391. void phy_error(struct phy_device *phydev)
  392. {
  393. spin_lock(&phydev->lock);
  394. phydev->state = PHY_HALTED;
  395. spin_unlock(&phydev->lock);
  396. }
  397. /* phy_interrupt
  398. *
  399. * description: When a PHY interrupt occurs, the handler disables
  400. * interrupts, and schedules a work task to clear the interrupt.
  401. */
  402. static irqreturn_t phy_interrupt(int irq, void *phy_dat)
  403. {
  404. struct phy_device *phydev = phy_dat;
  405. /* The MDIO bus is not allowed to be written in interrupt
  406. * context, so we need to disable the irq here. A work
  407. * queue will write the PHY to disable and clear the
  408. * interrupt, and then reenable the irq line. */
  409. disable_irq_nosync(irq);
  410. schedule_work(&phydev->phy_queue);
  411. return IRQ_HANDLED;
  412. }
  413. /* Enable the interrupts from the PHY side */
  414. int phy_enable_interrupts(struct phy_device *phydev)
  415. {
  416. int err;
  417. err = phy_clear_interrupt(phydev);
  418. if (err < 0)
  419. return err;
  420. err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  421. return err;
  422. }
  423. EXPORT_SYMBOL(phy_enable_interrupts);
  424. /* Disable the PHY interrupts from the PHY side */
  425. int phy_disable_interrupts(struct phy_device *phydev)
  426. {
  427. int err;
  428. /* Disable PHY interrupts */
  429. err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  430. if (err)
  431. goto phy_err;
  432. /* Clear the interrupt */
  433. err = phy_clear_interrupt(phydev);
  434. if (err)
  435. goto phy_err;
  436. return 0;
  437. phy_err:
  438. phy_error(phydev);
  439. return err;
  440. }
  441. EXPORT_SYMBOL(phy_disable_interrupts);
  442. /* phy_start_interrupts
  443. *
  444. * description: Request the interrupt for the given PHY. If
  445. * this fails, then we set irq to PHY_POLL.
  446. * Otherwise, we enable the interrupts in the PHY.
  447. * Returns 0 on success.
  448. * This should only be called with a valid IRQ number.
  449. */
  450. int phy_start_interrupts(struct phy_device *phydev)
  451. {
  452. int err = 0;
  453. INIT_WORK(&phydev->phy_queue, phy_change, phydev);
  454. if (request_irq(phydev->irq, phy_interrupt,
  455. IRQF_SHARED,
  456. "phy_interrupt",
  457. phydev) < 0) {
  458. printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
  459. phydev->bus->name,
  460. phydev->irq);
  461. phydev->irq = PHY_POLL;
  462. return 0;
  463. }
  464. err = phy_enable_interrupts(phydev);
  465. return err;
  466. }
  467. EXPORT_SYMBOL(phy_start_interrupts);
  468. int phy_stop_interrupts(struct phy_device *phydev)
  469. {
  470. int err;
  471. err = phy_disable_interrupts(phydev);
  472. if (err)
  473. phy_error(phydev);
  474. free_irq(phydev->irq, phydev);
  475. return err;
  476. }
  477. EXPORT_SYMBOL(phy_stop_interrupts);
  478. /* Scheduled by the phy_interrupt/timer to handle PHY changes */
  479. static void phy_change(void *data)
  480. {
  481. int err;
  482. struct phy_device *phydev = data;
  483. err = phy_disable_interrupts(phydev);
  484. if (err)
  485. goto phy_err;
  486. spin_lock(&phydev->lock);
  487. if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
  488. phydev->state = PHY_CHANGELINK;
  489. spin_unlock(&phydev->lock);
  490. enable_irq(phydev->irq);
  491. /* Reenable interrupts */
  492. err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  493. if (err)
  494. goto irq_enable_err;
  495. return;
  496. irq_enable_err:
  497. disable_irq(phydev->irq);
  498. phy_err:
  499. phy_error(phydev);
  500. }
  501. /* Bring down the PHY link, and stop checking the status. */
  502. void phy_stop(struct phy_device *phydev)
  503. {
  504. spin_lock(&phydev->lock);
  505. if (PHY_HALTED == phydev->state)
  506. goto out_unlock;
  507. if (phydev->irq != PHY_POLL) {
  508. /* Clear any pending interrupts */
  509. phy_clear_interrupt(phydev);
  510. /* Disable PHY Interrupts */
  511. phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  512. }
  513. phydev->state = PHY_HALTED;
  514. out_unlock:
  515. spin_unlock(&phydev->lock);
  516. }
  517. /* phy_start
  518. *
  519. * description: Indicates the attached device's readiness to
  520. * handle PHY-related work. Used during startup to start the
  521. * PHY, and after a call to phy_stop() to resume operation.
  522. * Also used to indicate the MDIO bus has cleared an error
  523. * condition.
  524. */
  525. void phy_start(struct phy_device *phydev)
  526. {
  527. spin_lock(&phydev->lock);
  528. switch (phydev->state) {
  529. case PHY_STARTING:
  530. phydev->state = PHY_PENDING;
  531. break;
  532. case PHY_READY:
  533. phydev->state = PHY_UP;
  534. break;
  535. case PHY_HALTED:
  536. phydev->state = PHY_RESUMING;
  537. default:
  538. break;
  539. }
  540. spin_unlock(&phydev->lock);
  541. }
  542. EXPORT_SYMBOL(phy_stop);
  543. EXPORT_SYMBOL(phy_start);
  544. /* PHY timer which handles the state machine */
  545. static void phy_timer(unsigned long data)
  546. {
  547. struct phy_device *phydev = (struct phy_device *)data;
  548. int needs_aneg = 0;
  549. int err = 0;
  550. spin_lock(&phydev->lock);
  551. if (phydev->adjust_state)
  552. phydev->adjust_state(phydev->attached_dev);
  553. switch(phydev->state) {
  554. case PHY_DOWN:
  555. case PHY_STARTING:
  556. case PHY_READY:
  557. case PHY_PENDING:
  558. break;
  559. case PHY_UP:
  560. needs_aneg = 1;
  561. phydev->link_timeout = PHY_AN_TIMEOUT;
  562. break;
  563. case PHY_AN:
  564. /* Check if negotiation is done. Break
  565. * if there's an error */
  566. err = phy_aneg_done(phydev);
  567. if (err < 0)
  568. break;
  569. /* If auto-negotiation is done, we change to
  570. * either RUNNING, or NOLINK */
  571. if (err > 0) {
  572. err = phy_read_status(phydev);
  573. if (err)
  574. break;
  575. if (phydev->link) {
  576. phydev->state = PHY_RUNNING;
  577. netif_carrier_on(phydev->attached_dev);
  578. } else {
  579. phydev->state = PHY_NOLINK;
  580. netif_carrier_off(phydev->attached_dev);
  581. }
  582. phydev->adjust_link(phydev->attached_dev);
  583. } else if (0 == phydev->link_timeout--) {
  584. /* The counter expired, so either we
  585. * switch to forced mode, or the
  586. * magic_aneg bit exists, and we try aneg
  587. * again */
  588. if (!(phydev->drv->flags & PHY_HAS_MAGICANEG)) {
  589. int idx;
  590. /* We'll start from the
  591. * fastest speed, and work
  592. * our way down */
  593. idx = phy_find_valid(0,
  594. phydev->supported);
  595. phydev->speed = settings[idx].speed;
  596. phydev->duplex = settings[idx].duplex;
  597. phydev->autoneg = AUTONEG_DISABLE;
  598. phydev->state = PHY_FORCING;
  599. phydev->link_timeout =
  600. PHY_FORCE_TIMEOUT;
  601. pr_info("Trying %d/%s\n",
  602. phydev->speed,
  603. DUPLEX_FULL ==
  604. phydev->duplex ?
  605. "FULL" : "HALF");
  606. }
  607. needs_aneg = 1;
  608. }
  609. break;
  610. case PHY_NOLINK:
  611. err = phy_read_status(phydev);
  612. if (err)
  613. break;
  614. if (phydev->link) {
  615. phydev->state = PHY_RUNNING;
  616. netif_carrier_on(phydev->attached_dev);
  617. phydev->adjust_link(phydev->attached_dev);
  618. }
  619. break;
  620. case PHY_FORCING:
  621. err = phy_read_status(phydev);
  622. if (err)
  623. break;
  624. if (phydev->link) {
  625. phydev->state = PHY_RUNNING;
  626. netif_carrier_on(phydev->attached_dev);
  627. } else {
  628. if (0 == phydev->link_timeout--) {
  629. phy_force_reduction(phydev);
  630. needs_aneg = 1;
  631. }
  632. }
  633. phydev->adjust_link(phydev->attached_dev);
  634. break;
  635. case PHY_RUNNING:
  636. /* Only register a CHANGE if we are
  637. * polling */
  638. if (PHY_POLL == phydev->irq)
  639. phydev->state = PHY_CHANGELINK;
  640. break;
  641. case PHY_CHANGELINK:
  642. err = phy_read_status(phydev);
  643. if (err)
  644. break;
  645. if (phydev->link) {
  646. phydev->state = PHY_RUNNING;
  647. netif_carrier_on(phydev->attached_dev);
  648. } else {
  649. phydev->state = PHY_NOLINK;
  650. netif_carrier_off(phydev->attached_dev);
  651. }
  652. phydev->adjust_link(phydev->attached_dev);
  653. if (PHY_POLL != phydev->irq)
  654. err = phy_config_interrupt(phydev,
  655. PHY_INTERRUPT_ENABLED);
  656. break;
  657. case PHY_HALTED:
  658. if (phydev->link) {
  659. phydev->link = 0;
  660. netif_carrier_off(phydev->attached_dev);
  661. phydev->adjust_link(phydev->attached_dev);
  662. }
  663. break;
  664. case PHY_RESUMING:
  665. err = phy_clear_interrupt(phydev);
  666. if (err)
  667. break;
  668. err = phy_config_interrupt(phydev,
  669. PHY_INTERRUPT_ENABLED);
  670. if (err)
  671. break;
  672. if (AUTONEG_ENABLE == phydev->autoneg) {
  673. err = phy_aneg_done(phydev);
  674. if (err < 0)
  675. break;
  676. /* err > 0 if AN is done.
  677. * Otherwise, it's 0, and we're
  678. * still waiting for AN */
  679. if (err > 0) {
  680. phydev->state = PHY_RUNNING;
  681. } else {
  682. phydev->state = PHY_AN;
  683. phydev->link_timeout = PHY_AN_TIMEOUT;
  684. }
  685. } else
  686. phydev->state = PHY_RUNNING;
  687. break;
  688. }
  689. spin_unlock(&phydev->lock);
  690. if (needs_aneg)
  691. err = phy_start_aneg(phydev);
  692. if (err < 0)
  693. phy_error(phydev);
  694. mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);
  695. }