phy.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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/version.h>
  34. #include <linux/mii.h>
  35. #include <linux/ethtool.h>
  36. #include <linux/phy.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("%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 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. int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
  243. {
  244. cmd->supported = phydev->supported;
  245. cmd->advertising = phydev->advertising;
  246. cmd->speed = phydev->speed;
  247. cmd->duplex = phydev->duplex;
  248. cmd->port = PORT_MII;
  249. cmd->phy_address = phydev->addr;
  250. cmd->transceiver = XCVR_EXTERNAL;
  251. cmd->autoneg = phydev->autoneg;
  252. return 0;
  253. }
  254. /* Note that this function is currently incompatible with the
  255. * PHYCONTROL layer. It changes registers without regard to
  256. * current state. Use at own risk
  257. */
  258. int phy_mii_ioctl(struct phy_device *phydev,
  259. struct mii_ioctl_data *mii_data, int cmd)
  260. {
  261. u16 val = mii_data->val_in;
  262. switch (cmd) {
  263. case SIOCGMIIPHY:
  264. mii_data->phy_id = phydev->addr;
  265. break;
  266. case SIOCGMIIREG:
  267. mii_data->val_out = phy_read(phydev, mii_data->reg_num);
  268. break;
  269. case SIOCSMIIREG:
  270. if (!capable(CAP_NET_ADMIN))
  271. return -EPERM;
  272. if (mii_data->phy_id == phydev->addr) {
  273. switch(mii_data->reg_num) {
  274. case MII_BMCR:
  275. if (val & (BMCR_RESET|BMCR_ANENABLE))
  276. phydev->autoneg = AUTONEG_DISABLE;
  277. else
  278. phydev->autoneg = AUTONEG_ENABLE;
  279. if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
  280. phydev->duplex = DUPLEX_FULL;
  281. else
  282. phydev->duplex = DUPLEX_HALF;
  283. break;
  284. case MII_ADVERTISE:
  285. phydev->advertising = val;
  286. break;
  287. default:
  288. /* do nothing */
  289. break;
  290. }
  291. }
  292. phy_write(phydev, mii_data->reg_num, val);
  293. if (mii_data->reg_num == MII_BMCR
  294. && val & BMCR_RESET
  295. && phydev->drv->config_init)
  296. phydev->drv->config_init(phydev);
  297. break;
  298. }
  299. return 0;
  300. }
  301. /* phy_start_aneg
  302. *
  303. * description: Sanitizes the settings (if we're not
  304. * autonegotiating them), and then calls the driver's
  305. * config_aneg function. If the PHYCONTROL Layer is operating,
  306. * we change the state to reflect the beginning of
  307. * Auto-negotiation or forcing.
  308. */
  309. int phy_start_aneg(struct phy_device *phydev)
  310. {
  311. int err;
  312. spin_lock(&phydev->lock);
  313. if (AUTONEG_DISABLE == phydev->autoneg)
  314. phy_sanitize_settings(phydev);
  315. err = phydev->drv->config_aneg(phydev);
  316. if (err < 0)
  317. goto out_unlock;
  318. if (phydev->state != PHY_HALTED) {
  319. if (AUTONEG_ENABLE == phydev->autoneg) {
  320. phydev->state = PHY_AN;
  321. phydev->link_timeout = PHY_AN_TIMEOUT;
  322. } else {
  323. phydev->state = PHY_FORCING;
  324. phydev->link_timeout = PHY_FORCE_TIMEOUT;
  325. }
  326. }
  327. out_unlock:
  328. spin_unlock(&phydev->lock);
  329. return err;
  330. }
  331. EXPORT_SYMBOL(phy_start_aneg);
  332. static void phy_change(void *data);
  333. static void phy_timer(unsigned long data);
  334. /* phy_start_machine:
  335. *
  336. * description: The PHY infrastructure can run a state machine
  337. * which tracks whether the PHY is starting up, negotiating,
  338. * etc. This function starts the timer which tracks the state
  339. * of the PHY. If you want to be notified when the state
  340. * changes, pass in the callback, otherwise, pass NULL. If you
  341. * want to maintain your own state machine, do not call this
  342. * function. */
  343. void phy_start_machine(struct phy_device *phydev,
  344. void (*handler)(struct net_device *))
  345. {
  346. phydev->adjust_state = handler;
  347. init_timer(&phydev->phy_timer);
  348. phydev->phy_timer.function = &phy_timer;
  349. phydev->phy_timer.data = (unsigned long) phydev;
  350. mod_timer(&phydev->phy_timer, jiffies + HZ);
  351. }
  352. /* phy_stop_machine
  353. *
  354. * description: Stops the state machine timer, sets the state to
  355. * UP (unless it wasn't up yet), and then frees the interrupt,
  356. * if it is in use. 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. if (phydev->irq != PHY_POLL)
  367. phy_stop_interrupts(phydev);
  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, struct pt_regs *regs)
  408. {
  409. struct phy_device *phydev = phy_dat;
  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. SA_SHIRQ,
  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. free_irq(phydev->irq, phydev);
  480. return err;
  481. }
  482. EXPORT_SYMBOL(phy_stop_interrupts);
  483. /* Scheduled by the phy_interrupt/timer to handle PHY changes */
  484. static void phy_change(void *data)
  485. {
  486. int err;
  487. struct phy_device *phydev = data;
  488. err = phy_disable_interrupts(phydev);
  489. if (err)
  490. goto phy_err;
  491. spin_lock(&phydev->lock);
  492. if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
  493. phydev->state = PHY_CHANGELINK;
  494. spin_unlock(&phydev->lock);
  495. enable_irq(phydev->irq);
  496. /* Reenable interrupts */
  497. err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  498. if (err)
  499. goto irq_enable_err;
  500. return;
  501. irq_enable_err:
  502. disable_irq(phydev->irq);
  503. phy_err:
  504. phy_error(phydev);
  505. }
  506. /* Bring down the PHY link, and stop checking the status. */
  507. void phy_stop(struct phy_device *phydev)
  508. {
  509. spin_lock(&phydev->lock);
  510. if (PHY_HALTED == phydev->state)
  511. goto out_unlock;
  512. if (phydev->irq != PHY_POLL) {
  513. /* Clear any pending interrupts */
  514. phy_clear_interrupt(phydev);
  515. /* Disable PHY Interrupts */
  516. phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  517. }
  518. phydev->state = PHY_HALTED;
  519. out_unlock:
  520. spin_unlock(&phydev->lock);
  521. }
  522. /* phy_start
  523. *
  524. * description: Indicates the attached device's readiness to
  525. * handle PHY-related work. Used during startup to start the
  526. * PHY, and after a call to phy_stop() to resume operation.
  527. * Also used to indicate the MDIO bus has cleared an error
  528. * condition.
  529. */
  530. void phy_start(struct phy_device *phydev)
  531. {
  532. spin_lock(&phydev->lock);
  533. switch (phydev->state) {
  534. case PHY_STARTING:
  535. phydev->state = PHY_PENDING;
  536. break;
  537. case PHY_READY:
  538. phydev->state = PHY_UP;
  539. break;
  540. case PHY_HALTED:
  541. phydev->state = PHY_RESUMING;
  542. default:
  543. break;
  544. }
  545. spin_unlock(&phydev->lock);
  546. }
  547. EXPORT_SYMBOL(phy_stop);
  548. EXPORT_SYMBOL(phy_start);
  549. /* PHY timer which handles the state machine */
  550. static void phy_timer(unsigned long data)
  551. {
  552. struct phy_device *phydev = (struct phy_device *)data;
  553. int needs_aneg = 0;
  554. int err = 0;
  555. spin_lock(&phydev->lock);
  556. if (phydev->adjust_state)
  557. phydev->adjust_state(phydev->attached_dev);
  558. switch(phydev->state) {
  559. case PHY_DOWN:
  560. case PHY_STARTING:
  561. case PHY_READY:
  562. case PHY_PENDING:
  563. break;
  564. case PHY_UP:
  565. needs_aneg = 1;
  566. phydev->link_timeout = PHY_AN_TIMEOUT;
  567. break;
  568. case PHY_AN:
  569. /* Check if negotiation is done. Break
  570. * if there's an error */
  571. err = phy_aneg_done(phydev);
  572. if (err < 0)
  573. break;
  574. /* If auto-negotiation is done, we change to
  575. * either RUNNING, or NOLINK */
  576. if (err > 0) {
  577. err = phy_read_status(phydev);
  578. if (err)
  579. break;
  580. if (phydev->link) {
  581. phydev->state = PHY_RUNNING;
  582. netif_carrier_on(phydev->attached_dev);
  583. } else {
  584. phydev->state = PHY_NOLINK;
  585. netif_carrier_off(phydev->attached_dev);
  586. }
  587. phydev->adjust_link(phydev->attached_dev);
  588. } else if (0 == phydev->link_timeout--) {
  589. /* The counter expired, so either we
  590. * switch to forced mode, or the
  591. * magic_aneg bit exists, and we try aneg
  592. * again */
  593. if (!(phydev->drv->flags & PHY_HAS_MAGICANEG)) {
  594. int idx;
  595. /* We'll start from the
  596. * fastest speed, and work
  597. * our way down */
  598. idx = phy_find_valid(0,
  599. phydev->supported);
  600. phydev->speed = settings[idx].speed;
  601. phydev->duplex = settings[idx].duplex;
  602. phydev->autoneg = AUTONEG_DISABLE;
  603. phydev->state = PHY_FORCING;
  604. phydev->link_timeout =
  605. PHY_FORCE_TIMEOUT;
  606. pr_info("Trying %d/%s\n",
  607. phydev->speed,
  608. DUPLEX_FULL ==
  609. phydev->duplex ?
  610. "FULL" : "HALF");
  611. }
  612. needs_aneg = 1;
  613. }
  614. break;
  615. case PHY_NOLINK:
  616. err = phy_read_status(phydev);
  617. if (err)
  618. break;
  619. if (phydev->link) {
  620. phydev->state = PHY_RUNNING;
  621. netif_carrier_on(phydev->attached_dev);
  622. phydev->adjust_link(phydev->attached_dev);
  623. }
  624. break;
  625. case PHY_FORCING:
  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. } else {
  633. if (0 == phydev->link_timeout--) {
  634. phy_force_reduction(phydev);
  635. needs_aneg = 1;
  636. }
  637. }
  638. phydev->adjust_link(phydev->attached_dev);
  639. break;
  640. case PHY_RUNNING:
  641. /* Only register a CHANGE if we are
  642. * polling */
  643. if (PHY_POLL == phydev->irq)
  644. phydev->state = PHY_CHANGELINK;
  645. break;
  646. case PHY_CHANGELINK:
  647. err = phy_read_status(phydev);
  648. if (err)
  649. break;
  650. if (phydev->link) {
  651. phydev->state = PHY_RUNNING;
  652. netif_carrier_on(phydev->attached_dev);
  653. } else {
  654. phydev->state = PHY_NOLINK;
  655. netif_carrier_off(phydev->attached_dev);
  656. }
  657. phydev->adjust_link(phydev->attached_dev);
  658. if (PHY_POLL != phydev->irq)
  659. err = phy_config_interrupt(phydev,
  660. PHY_INTERRUPT_ENABLED);
  661. break;
  662. case PHY_HALTED:
  663. if (phydev->link) {
  664. phydev->link = 0;
  665. netif_carrier_off(phydev->attached_dev);
  666. phydev->adjust_link(phydev->attached_dev);
  667. }
  668. break;
  669. case PHY_RESUMING:
  670. err = phy_clear_interrupt(phydev);
  671. if (err)
  672. break;
  673. err = phy_config_interrupt(phydev,
  674. PHY_INTERRUPT_ENABLED);
  675. if (err)
  676. break;
  677. if (AUTONEG_ENABLE == phydev->autoneg) {
  678. err = phy_aneg_done(phydev);
  679. if (err < 0)
  680. break;
  681. /* err > 0 if AN is done.
  682. * Otherwise, it's 0, and we're
  683. * still waiting for AN */
  684. if (err > 0) {
  685. phydev->state = PHY_RUNNING;
  686. } else {
  687. phydev->state = PHY_AN;
  688. phydev->link_timeout = PHY_AN_TIMEOUT;
  689. }
  690. } else
  691. phydev->state = PHY_RUNNING;
  692. break;
  693. }
  694. spin_unlock(&phydev->lock);
  695. if (needs_aneg)
  696. err = phy_start_aneg(phydev);
  697. if (err < 0)
  698. phy_error(phydev);
  699. mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);
  700. }