phy.c 19 KB

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