phy.c 23 KB

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