phy.c 23 KB

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