phy.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. EXPORT_SYMBOL(phy_ethtool_sset);
  244. int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
  245. {
  246. cmd->supported = phydev->supported;
  247. cmd->advertising = phydev->advertising;
  248. cmd->speed = phydev->speed;
  249. cmd->duplex = phydev->duplex;
  250. cmd->port = PORT_MII;
  251. cmd->phy_address = phydev->addr;
  252. cmd->transceiver = XCVR_EXTERNAL;
  253. cmd->autoneg = phydev->autoneg;
  254. return 0;
  255. }
  256. EXPORT_SYMBOL(phy_ethtool_gset);
  257. /* Note that this function is currently incompatible with the
  258. * PHYCONTROL layer. It changes registers without regard to
  259. * current state. Use at own risk
  260. */
  261. int phy_mii_ioctl(struct phy_device *phydev,
  262. struct mii_ioctl_data *mii_data, int cmd)
  263. {
  264. u16 val = mii_data->val_in;
  265. switch (cmd) {
  266. case SIOCGMIIPHY:
  267. mii_data->phy_id = phydev->addr;
  268. break;
  269. case SIOCGMIIREG:
  270. mii_data->val_out = phy_read(phydev, mii_data->reg_num);
  271. break;
  272. case SIOCSMIIREG:
  273. if (!capable(CAP_NET_ADMIN))
  274. return -EPERM;
  275. if (mii_data->phy_id == phydev->addr) {
  276. switch(mii_data->reg_num) {
  277. case MII_BMCR:
  278. if (val & (BMCR_RESET|BMCR_ANENABLE))
  279. phydev->autoneg = AUTONEG_DISABLE;
  280. else
  281. phydev->autoneg = AUTONEG_ENABLE;
  282. if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
  283. phydev->duplex = DUPLEX_FULL;
  284. else
  285. phydev->duplex = DUPLEX_HALF;
  286. break;
  287. case MII_ADVERTISE:
  288. phydev->advertising = val;
  289. break;
  290. default:
  291. /* do nothing */
  292. break;
  293. }
  294. }
  295. phy_write(phydev, mii_data->reg_num, val);
  296. if (mii_data->reg_num == MII_BMCR
  297. && val & BMCR_RESET
  298. && phydev->drv->config_init)
  299. phydev->drv->config_init(phydev);
  300. break;
  301. }
  302. return 0;
  303. }
  304. /* phy_start_aneg
  305. *
  306. * description: Sanitizes the settings (if we're not
  307. * autonegotiating them), and then calls the driver's
  308. * config_aneg function. If the PHYCONTROL Layer is operating,
  309. * we change the state to reflect the beginning of
  310. * Auto-negotiation or forcing.
  311. */
  312. int phy_start_aneg(struct phy_device *phydev)
  313. {
  314. int err;
  315. spin_lock(&phydev->lock);
  316. if (AUTONEG_DISABLE == phydev->autoneg)
  317. phy_sanitize_settings(phydev);
  318. err = phydev->drv->config_aneg(phydev);
  319. if (err < 0)
  320. goto out_unlock;
  321. if (phydev->state != PHY_HALTED) {
  322. if (AUTONEG_ENABLE == phydev->autoneg) {
  323. phydev->state = PHY_AN;
  324. phydev->link_timeout = PHY_AN_TIMEOUT;
  325. } else {
  326. phydev->state = PHY_FORCING;
  327. phydev->link_timeout = PHY_FORCE_TIMEOUT;
  328. }
  329. }
  330. out_unlock:
  331. spin_unlock(&phydev->lock);
  332. return err;
  333. }
  334. EXPORT_SYMBOL(phy_start_aneg);
  335. static void phy_change(struct work_struct *work);
  336. static void phy_timer(unsigned long data);
  337. /* phy_start_machine:
  338. *
  339. * description: The PHY infrastructure can run a state machine
  340. * which tracks whether the PHY is starting up, negotiating,
  341. * etc. This function starts the timer which tracks the state
  342. * of the PHY. If you want to be notified when the state
  343. * changes, pass in the callback, otherwise, pass NULL. If you
  344. * want to maintain your own state machine, do not call this
  345. * function. */
  346. void phy_start_machine(struct phy_device *phydev,
  347. void (*handler)(struct net_device *))
  348. {
  349. phydev->adjust_state = handler;
  350. init_timer(&phydev->phy_timer);
  351. phydev->phy_timer.function = &phy_timer;
  352. phydev->phy_timer.data = (unsigned long) phydev;
  353. mod_timer(&phydev->phy_timer, jiffies + HZ);
  354. }
  355. /* phy_stop_machine
  356. *
  357. * description: Stops the state machine timer, sets the state to UP
  358. * (unless it wasn't up yet). This function must be called BEFORE
  359. * phy_detach.
  360. */
  361. void phy_stop_machine(struct phy_device *phydev)
  362. {
  363. del_timer_sync(&phydev->phy_timer);
  364. spin_lock(&phydev->lock);
  365. if (phydev->state > PHY_UP)
  366. phydev->state = PHY_UP;
  367. spin_unlock(&phydev->lock);
  368. phydev->adjust_state = NULL;
  369. }
  370. /* phy_force_reduction
  371. *
  372. * description: Reduces the speed/duplex settings by
  373. * one notch. The order is so:
  374. * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
  375. * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
  376. */
  377. static void phy_force_reduction(struct phy_device *phydev)
  378. {
  379. int idx;
  380. idx = phy_find_setting(phydev->speed, phydev->duplex);
  381. idx++;
  382. idx = phy_find_valid(idx, phydev->supported);
  383. phydev->speed = settings[idx].speed;
  384. phydev->duplex = settings[idx].duplex;
  385. pr_info("Trying %d/%s\n", phydev->speed,
  386. DUPLEX_FULL == phydev->duplex ?
  387. "FULL" : "HALF");
  388. }
  389. /* phy_error:
  390. *
  391. * Moves the PHY to the HALTED state in response to a read
  392. * or write error, and tells the controller the link is down.
  393. * Must not be called from interrupt context, or while the
  394. * phydev->lock is held.
  395. */
  396. void phy_error(struct phy_device *phydev)
  397. {
  398. spin_lock(&phydev->lock);
  399. phydev->state = PHY_HALTED;
  400. spin_unlock(&phydev->lock);
  401. }
  402. /* phy_interrupt
  403. *
  404. * description: When a PHY interrupt occurs, the handler disables
  405. * interrupts, and schedules a work task to clear the interrupt.
  406. */
  407. static irqreturn_t phy_interrupt(int irq, void *phy_dat)
  408. {
  409. struct phy_device *phydev = phy_dat;
  410. if (PHY_HALTED == phydev->state)
  411. return IRQ_NONE; /* It can't be ours. */
  412. /* The MDIO bus is not allowed to be written in interrupt
  413. * context, so we need to disable the irq here. A work
  414. * queue will write the PHY to disable and clear the
  415. * interrupt, and then reenable the irq line. */
  416. disable_irq_nosync(irq);
  417. schedule_work(&phydev->phy_queue);
  418. return IRQ_HANDLED;
  419. }
  420. /* Enable the interrupts from the PHY side */
  421. int phy_enable_interrupts(struct phy_device *phydev)
  422. {
  423. int err;
  424. err = phy_clear_interrupt(phydev);
  425. if (err < 0)
  426. return err;
  427. err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  428. return err;
  429. }
  430. EXPORT_SYMBOL(phy_enable_interrupts);
  431. /* Disable the PHY interrupts from the PHY side */
  432. int phy_disable_interrupts(struct phy_device *phydev)
  433. {
  434. int err;
  435. /* Disable PHY interrupts */
  436. err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  437. if (err)
  438. goto phy_err;
  439. /* Clear the interrupt */
  440. err = phy_clear_interrupt(phydev);
  441. if (err)
  442. goto phy_err;
  443. return 0;
  444. phy_err:
  445. phy_error(phydev);
  446. return err;
  447. }
  448. EXPORT_SYMBOL(phy_disable_interrupts);
  449. /* phy_start_interrupts
  450. *
  451. * description: Request the interrupt for the given PHY. If
  452. * this fails, then we set irq to PHY_POLL.
  453. * Otherwise, we enable the interrupts in the PHY.
  454. * Returns 0 on success.
  455. * This should only be called with a valid IRQ number.
  456. */
  457. int phy_start_interrupts(struct phy_device *phydev)
  458. {
  459. int err = 0;
  460. INIT_WORK(&phydev->phy_queue, phy_change);
  461. if (request_irq(phydev->irq, phy_interrupt,
  462. IRQF_SHARED,
  463. "phy_interrupt",
  464. phydev) < 0) {
  465. printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
  466. phydev->bus->name,
  467. phydev->irq);
  468. phydev->irq = PHY_POLL;
  469. return 0;
  470. }
  471. err = phy_enable_interrupts(phydev);
  472. return err;
  473. }
  474. EXPORT_SYMBOL(phy_start_interrupts);
  475. int phy_stop_interrupts(struct phy_device *phydev)
  476. {
  477. int err;
  478. err = phy_disable_interrupts(phydev);
  479. if (err)
  480. phy_error(phydev);
  481. /*
  482. * Finish any pending work; we might have been scheduled
  483. * to be called from keventd ourselves, though.
  484. */
  485. run_scheduled_work(&phydev->phy_queue);
  486. free_irq(phydev->irq, phydev);
  487. return err;
  488. }
  489. EXPORT_SYMBOL(phy_stop_interrupts);
  490. /* Scheduled by the phy_interrupt/timer to handle PHY changes */
  491. static void phy_change(struct work_struct *work)
  492. {
  493. int err;
  494. struct phy_device *phydev =
  495. container_of(work, struct phy_device, phy_queue);
  496. err = phy_disable_interrupts(phydev);
  497. if (err)
  498. goto phy_err;
  499. spin_lock(&phydev->lock);
  500. if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
  501. phydev->state = PHY_CHANGELINK;
  502. spin_unlock(&phydev->lock);
  503. enable_irq(phydev->irq);
  504. /* Reenable interrupts */
  505. if (PHY_HALTED != phydev->state)
  506. err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  507. if (err)
  508. goto irq_enable_err;
  509. return;
  510. irq_enable_err:
  511. disable_irq(phydev->irq);
  512. phy_err:
  513. phy_error(phydev);
  514. }
  515. /* Bring down the PHY link, and stop checking the status. */
  516. void phy_stop(struct phy_device *phydev)
  517. {
  518. spin_lock(&phydev->lock);
  519. if (PHY_HALTED == phydev->state)
  520. goto out_unlock;
  521. phydev->state = PHY_HALTED;
  522. if (phydev->irq != PHY_POLL) {
  523. /* Disable PHY Interrupts */
  524. phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  525. /* Clear any pending interrupts */
  526. phy_clear_interrupt(phydev);
  527. }
  528. out_unlock:
  529. spin_unlock(&phydev->lock);
  530. /*
  531. * Cannot call flush_scheduled_work() here as desired because
  532. * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
  533. * will not reenable interrupts.
  534. */
  535. }
  536. /* phy_start
  537. *
  538. * description: Indicates the attached device's readiness to
  539. * handle PHY-related work. Used during startup to start the
  540. * PHY, and after a call to phy_stop() to resume operation.
  541. * Also used to indicate the MDIO bus has cleared an error
  542. * condition.
  543. */
  544. void phy_start(struct phy_device *phydev)
  545. {
  546. spin_lock(&phydev->lock);
  547. switch (phydev->state) {
  548. case PHY_STARTING:
  549. phydev->state = PHY_PENDING;
  550. break;
  551. case PHY_READY:
  552. phydev->state = PHY_UP;
  553. break;
  554. case PHY_HALTED:
  555. phydev->state = PHY_RESUMING;
  556. default:
  557. break;
  558. }
  559. spin_unlock(&phydev->lock);
  560. }
  561. EXPORT_SYMBOL(phy_stop);
  562. EXPORT_SYMBOL(phy_start);
  563. /* PHY timer which handles the state machine */
  564. static void phy_timer(unsigned long data)
  565. {
  566. struct phy_device *phydev = (struct phy_device *)data;
  567. int needs_aneg = 0;
  568. int err = 0;
  569. spin_lock(&phydev->lock);
  570. if (phydev->adjust_state)
  571. phydev->adjust_state(phydev->attached_dev);
  572. switch(phydev->state) {
  573. case PHY_DOWN:
  574. case PHY_STARTING:
  575. case PHY_READY:
  576. case PHY_PENDING:
  577. break;
  578. case PHY_UP:
  579. needs_aneg = 1;
  580. phydev->link_timeout = PHY_AN_TIMEOUT;
  581. break;
  582. case PHY_AN:
  583. err = phy_read_status(phydev);
  584. if (err < 0)
  585. break;
  586. /* If the link is down, give up on
  587. * negotiation for now */
  588. if (!phydev->link) {
  589. phydev->state = PHY_NOLINK;
  590. netif_carrier_off(phydev->attached_dev);
  591. phydev->adjust_link(phydev->attached_dev);
  592. break;
  593. }
  594. /* Check if negotiation is done. Break
  595. * if there's an error */
  596. err = phy_aneg_done(phydev);
  597. if (err < 0)
  598. break;
  599. /* If AN is done, we're running */
  600. if (err > 0) {
  601. phydev->state = PHY_RUNNING;
  602. netif_carrier_on(phydev->attached_dev);
  603. phydev->adjust_link(phydev->attached_dev);
  604. } else if (0 == phydev->link_timeout--) {
  605. int idx;
  606. needs_aneg = 1;
  607. /* If we have the magic_aneg bit,
  608. * we try again */
  609. if (phydev->drv->flags & PHY_HAS_MAGICANEG)
  610. break;
  611. /* The timer expired, and we still
  612. * don't have a setting, so we try
  613. * forcing it until we find one that
  614. * works, starting from the fastest speed,
  615. * and working our way down */
  616. idx = phy_find_valid(0, phydev->supported);
  617. phydev->speed = settings[idx].speed;
  618. phydev->duplex = settings[idx].duplex;
  619. phydev->autoneg = AUTONEG_DISABLE;
  620. pr_info("Trying %d/%s\n", phydev->speed,
  621. DUPLEX_FULL ==
  622. phydev->duplex ?
  623. "FULL" : "HALF");
  624. }
  625. break;
  626. case PHY_NOLINK:
  627. err = phy_read_status(phydev);
  628. if (err)
  629. break;
  630. if (phydev->link) {
  631. phydev->state = PHY_RUNNING;
  632. netif_carrier_on(phydev->attached_dev);
  633. phydev->adjust_link(phydev->attached_dev);
  634. }
  635. break;
  636. case PHY_FORCING:
  637. err = genphy_update_link(phydev);
  638. if (err)
  639. break;
  640. if (phydev->link) {
  641. phydev->state = PHY_RUNNING;
  642. netif_carrier_on(phydev->attached_dev);
  643. } else {
  644. if (0 == phydev->link_timeout--) {
  645. phy_force_reduction(phydev);
  646. needs_aneg = 1;
  647. }
  648. }
  649. phydev->adjust_link(phydev->attached_dev);
  650. break;
  651. case PHY_RUNNING:
  652. /* Only register a CHANGE if we are
  653. * polling */
  654. if (PHY_POLL == phydev->irq)
  655. phydev->state = PHY_CHANGELINK;
  656. break;
  657. case PHY_CHANGELINK:
  658. err = phy_read_status(phydev);
  659. if (err)
  660. break;
  661. if (phydev->link) {
  662. phydev->state = PHY_RUNNING;
  663. netif_carrier_on(phydev->attached_dev);
  664. } else {
  665. phydev->state = PHY_NOLINK;
  666. netif_carrier_off(phydev->attached_dev);
  667. }
  668. phydev->adjust_link(phydev->attached_dev);
  669. if (PHY_POLL != phydev->irq)
  670. err = phy_config_interrupt(phydev,
  671. PHY_INTERRUPT_ENABLED);
  672. break;
  673. case PHY_HALTED:
  674. if (phydev->link) {
  675. phydev->link = 0;
  676. netif_carrier_off(phydev->attached_dev);
  677. phydev->adjust_link(phydev->attached_dev);
  678. }
  679. break;
  680. case PHY_RESUMING:
  681. err = phy_clear_interrupt(phydev);
  682. if (err)
  683. break;
  684. err = phy_config_interrupt(phydev,
  685. PHY_INTERRUPT_ENABLED);
  686. if (err)
  687. break;
  688. if (AUTONEG_ENABLE == phydev->autoneg) {
  689. err = phy_aneg_done(phydev);
  690. if (err < 0)
  691. break;
  692. /* err > 0 if AN is done.
  693. * Otherwise, it's 0, and we're
  694. * still waiting for AN */
  695. if (err > 0) {
  696. phydev->state = PHY_RUNNING;
  697. } else {
  698. phydev->state = PHY_AN;
  699. phydev->link_timeout = PHY_AN_TIMEOUT;
  700. }
  701. } else
  702. phydev->state = PHY_RUNNING;
  703. break;
  704. }
  705. spin_unlock(&phydev->lock);
  706. if (needs_aneg)
  707. err = phy_start_aneg(phydev);
  708. if (err < 0)
  709. phy_error(phydev);
  710. mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);
  711. }