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