phy.c 20 KB

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