000-enable-alx-wol-4.18.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. diff --git a/drivers/net/ethernet/atheros/alx/ethtool.c b/drivers/net/ethernet/atheros/alx/ethtool.c
  2. index 2f4eabf652e80..859e27236ce4f 100644
  3. --- a/drivers/net/ethernet/atheros/alx/ethtool.c
  4. +++ b/drivers/net/ethernet/atheros/alx/ethtool.c
  5. @@ -310,11 +310,47 @@ static int alx_get_sset_count(struct net_device *netdev, int sset)
  6. }
  7. }
  8. +static void alx_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  9. +{
  10. + struct alx_priv *alx = netdev_priv(netdev);
  11. + struct alx_hw *hw = &alx->hw;
  12. +
  13. + wol->supported = WAKE_MAGIC | WAKE_PHY;
  14. + wol->wolopts = 0;
  15. +
  16. + if (hw->sleep_ctrl & ALX_SLEEP_WOL_MAGIC)
  17. + wol->wolopts |= WAKE_MAGIC;
  18. + if (hw->sleep_ctrl & ALX_SLEEP_WOL_PHY)
  19. + wol->wolopts |= WAKE_PHY;
  20. +}
  21. +
  22. +static int alx_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  23. +{
  24. + struct alx_priv *alx = netdev_priv(netdev);
  25. + struct alx_hw *hw = &alx->hw;
  26. +
  27. + if (wol->wolopts & ~(WAKE_MAGIC | WAKE_PHY))
  28. + return -EOPNOTSUPP;
  29. +
  30. + hw->sleep_ctrl = 0;
  31. +
  32. + if (wol->wolopts & WAKE_MAGIC)
  33. + hw->sleep_ctrl |= ALX_SLEEP_WOL_MAGIC;
  34. + if (wol->wolopts & WAKE_PHY)
  35. + hw->sleep_ctrl |= ALX_SLEEP_WOL_PHY;
  36. +
  37. + device_set_wakeup_enable(&alx->hw.pdev->dev, hw->sleep_ctrl);
  38. +
  39. + return 0;
  40. +}
  41. +
  42. const struct ethtool_ops alx_ethtool_ops = {
  43. .get_pauseparam = alx_get_pauseparam,
  44. .set_pauseparam = alx_set_pauseparam,
  45. .get_msglevel = alx_get_msglevel,
  46. .set_msglevel = alx_set_msglevel,
  47. + .get_wol = alx_get_wol,
  48. + .set_wol = alx_set_wol,
  49. .get_link = ethtool_op_get_link,
  50. .get_strings = alx_get_strings,
  51. .get_sset_count = alx_get_sset_count,
  52. diff --git a/drivers/net/ethernet/atheros/alx/hw.c b/drivers/net/ethernet/atheros/alx/hw.c
  53. index 6ac40b0003a34..4791b9dbbe265 100644
  54. --- a/drivers/net/ethernet/atheros/alx/hw.c
  55. +++ b/drivers/net/ethernet/atheros/alx/hw.c
  56. @@ -332,6 +332,16 @@ void alx_set_macaddr(struct alx_hw *hw, const u8 *addr)
  57. alx_write_mem32(hw, ALX_STAD1, val);
  58. }
  59. +static void alx_enable_osc(struct alx_hw *hw)
  60. +{
  61. + u32 val;
  62. +
  63. + /* rising edge */
  64. + val = alx_read_mem32(hw, ALX_MISC);
  65. + alx_write_mem32(hw, ALX_MISC, val & ~ALX_MISC_INTNLOSC_OPEN);
  66. + alx_write_mem32(hw, ALX_MISC, val | ALX_MISC_INTNLOSC_OPEN);
  67. +}
  68. +
  69. static void alx_reset_osc(struct alx_hw *hw, u8 rev)
  70. {
  71. u32 val, val2;
  72. @@ -848,6 +858,66 @@ void alx_post_phy_link(struct alx_hw *hw)
  73. }
  74. }
  75. +
  76. +/* NOTE:
  77. + * 1. phy link must be established before calling this function
  78. + * 2. wol option (pattern,magic,link,etc.) is configed before call it.
  79. + */
  80. +int alx_pre_suspend(struct alx_hw *hw, int speed, u8 duplex)
  81. +{
  82. + u32 master, mac, phy, val;
  83. + int err = 0;
  84. +
  85. + master = alx_read_mem32(hw, ALX_MASTER);
  86. + master &= ~ALX_MASTER_PCLKSEL_SRDS;
  87. + mac = hw->rx_ctrl;
  88. + /* 10/100 half */
  89. + ALX_SET_FIELD(mac, ALX_MAC_CTRL_SPEED, ALX_MAC_CTRL_SPEED_10_100);
  90. + mac &= ~(ALX_MAC_CTRL_FULLD | ALX_MAC_CTRL_RX_EN | ALX_MAC_CTRL_TX_EN);
  91. +
  92. + phy = alx_read_mem32(hw, ALX_PHY_CTRL);
  93. + phy &= ~(ALX_PHY_CTRL_DSPRST_OUT | ALX_PHY_CTRL_CLS);
  94. + phy |= ALX_PHY_CTRL_RST_ANALOG | ALX_PHY_CTRL_HIB_PULSE |
  95. + ALX_PHY_CTRL_HIB_EN;
  96. +
  97. + /* without any activity */
  98. + if (!(hw->sleep_ctrl & ALX_SLEEP_ACTIVE)) {
  99. + err = alx_write_phy_reg(hw, ALX_MII_IER, 0);
  100. + if (err)
  101. + return err;
  102. + phy |= ALX_PHY_CTRL_IDDQ | ALX_PHY_CTRL_POWER_DOWN;
  103. + } else {
  104. + if (hw->sleep_ctrl & (ALX_SLEEP_WOL_MAGIC | ALX_SLEEP_CIFS))
  105. + mac |= ALX_MAC_CTRL_RX_EN | ALX_MAC_CTRL_BRD_EN;
  106. + if (hw->sleep_ctrl & ALX_SLEEP_CIFS)
  107. + mac |= ALX_MAC_CTRL_TX_EN;
  108. + if (duplex == DUPLEX_FULL)
  109. + mac |= ALX_MAC_CTRL_FULLD;
  110. + if (speed == SPEED_1000)
  111. + ALX_SET_FIELD(mac, ALX_MAC_CTRL_SPEED,
  112. + ALX_MAC_CTRL_SPEED_1000);
  113. + phy |= ALX_PHY_CTRL_DSPRST_OUT;
  114. + err = alx_write_phy_ext(hw, ALX_MIIEXT_ANEG,
  115. + ALX_MIIEXT_S3DIG10,
  116. + ALX_MIIEXT_S3DIG10_SL);
  117. + if (err)
  118. + return err;
  119. + }
  120. +
  121. + alx_enable_osc(hw);
  122. + hw->rx_ctrl = mac;
  123. + alx_write_mem32(hw, ALX_MASTER, master);
  124. + alx_write_mem32(hw, ALX_MAC_CTRL, mac);
  125. + alx_write_mem32(hw, ALX_PHY_CTRL, phy);
  126. +
  127. + /* set val of PDLL D3PLLOFF */
  128. + val = alx_read_mem32(hw, ALX_PDLL_TRNS1);
  129. + val |= ALX_PDLL_TRNS1_D3PLLOFF_EN;
  130. + alx_write_mem32(hw, ALX_PDLL_TRNS1, val);
  131. +
  132. + return 0;
  133. +}
  134. +
  135. bool alx_phy_configured(struct alx_hw *hw)
  136. {
  137. u32 cfg, hw_cfg;
  138. @@ -920,6 +990,26 @@ int alx_clear_phy_intr(struct alx_hw *hw)
  139. return alx_read_phy_reg(hw, ALX_MII_ISR, &isr);
  140. }
  141. +int alx_config_wol(struct alx_hw *hw)
  142. +{
  143. + u32 wol = 0;
  144. + int err = 0;
  145. +
  146. + /* turn on magic packet event */
  147. + if (hw->sleep_ctrl & ALX_SLEEP_WOL_MAGIC)
  148. + wol |= ALX_WOL0_MAGIC_EN | ALX_WOL0_PME_MAGIC_EN;
  149. +
  150. + /* turn on link up event */
  151. + if (hw->sleep_ctrl & ALX_SLEEP_WOL_PHY) {
  152. + wol |= ALX_WOL0_LINK_EN | ALX_WOL0_PME_LINK;
  153. + /* only link up can wake up */
  154. + err = alx_write_phy_reg(hw, ALX_MII_IER, ALX_IER_LINK_UP);
  155. + }
  156. + alx_write_mem32(hw, ALX_WOL0, wol);
  157. +
  158. + return err;
  159. +}
  160. +
  161. void alx_disable_rss(struct alx_hw *hw)
  162. {
  163. u32 ctrl = alx_read_mem32(hw, ALX_RXQ0);
  164. @@ -1045,6 +1135,71 @@ void alx_mask_msix(struct alx_hw *hw, int index, bool mask)
  165. }
  166. +int alx_select_powersaving_speed(struct alx_hw *hw, int *speed, u8 *duplex)
  167. +{
  168. + int i, err;
  169. + u16 lpa;
  170. +
  171. + err = alx_read_phy_link(hw);
  172. + if (err)
  173. + return err;
  174. +
  175. + if (hw->link_speed == SPEED_UNKNOWN) {
  176. + *speed = SPEED_UNKNOWN;
  177. + *duplex = DUPLEX_UNKNOWN;
  178. + return 0;
  179. + }
  180. +
  181. + err = alx_read_phy_reg(hw, MII_LPA, &lpa);
  182. + if (err)
  183. + return err;
  184. +
  185. + if (!(lpa & LPA_LPACK)) {
  186. + *speed = hw->link_speed;
  187. + return 0;
  188. + }
  189. +
  190. + if (lpa & LPA_10FULL) {
  191. + *speed = SPEED_10;
  192. + *duplex = DUPLEX_FULL;
  193. + } else if (lpa & LPA_10HALF) {
  194. + *speed = SPEED_10;
  195. + *duplex = DUPLEX_HALF;
  196. + } else if (lpa & LPA_100FULL) {
  197. + *speed = SPEED_100;
  198. + *duplex = DUPLEX_FULL;
  199. + } else {
  200. + *speed = SPEED_100;
  201. + *duplex = DUPLEX_HALF;
  202. + }
  203. +
  204. + if (*speed == hw->link_speed && *duplex == hw->duplex)
  205. + return 0;
  206. + err = alx_write_phy_reg(hw, ALX_MII_IER, 0);
  207. + if (err)
  208. + return err;
  209. + err = alx_setup_speed_duplex(hw, alx_speed_to_ethadv(*speed, *duplex) |
  210. + ADVERTISED_Autoneg, ALX_FC_ANEG |
  211. + ALX_FC_RX | ALX_FC_TX);
  212. + if (err)
  213. + return err;
  214. +
  215. + /* wait for linkup */
  216. + for (i = 0; i < ALX_MAX_SETUP_LNK_CYCLE; i++) {
  217. + msleep(100);
  218. +
  219. + err = alx_read_phy_link(hw);
  220. + if (err < 0)
  221. + return err;
  222. + if (hw->link_speed != SPEED_UNKNOWN)
  223. + break;
  224. + }
  225. + if (i == ALX_MAX_SETUP_LNK_CYCLE)
  226. + return -ETIMEDOUT;
  227. +
  228. + return 0;
  229. +}
  230. +
  231. bool alx_get_phy_info(struct alx_hw *hw)
  232. {
  233. u16 devs1, devs2;
  234. diff --git a/drivers/net/ethernet/atheros/alx/hw.h b/drivers/net/ethernet/atheros/alx/hw.h
  235. index e42d7e0947ebe..a7fb6c8d846a9 100644
  236. --- a/drivers/net/ethernet/atheros/alx/hw.h
  237. +++ b/drivers/net/ethernet/atheros/alx/hw.h
  238. @@ -487,6 +487,8 @@ struct alx_hw {
  239. u8 flowctrl;
  240. u32 adv_cfg;
  241. + u32 sleep_ctrl;
  242. +
  243. spinlock_t mdio_lock;
  244. struct mdio_if_info mdio;
  245. u16 phy_id[2];
  246. @@ -549,12 +551,14 @@ void alx_reset_pcie(struct alx_hw *hw);
  247. void alx_enable_aspm(struct alx_hw *hw, bool l0s_en, bool l1_en);
  248. int alx_setup_speed_duplex(struct alx_hw *hw, u32 ethadv, u8 flowctrl);
  249. void alx_post_phy_link(struct alx_hw *hw);
  250. +int alx_pre_suspend(struct alx_hw *hw, int speed, u8 duplex);
  251. int alx_read_phy_reg(struct alx_hw *hw, u16 reg, u16 *phy_data);
  252. int alx_write_phy_reg(struct alx_hw *hw, u16 reg, u16 phy_data);
  253. int alx_read_phy_ext(struct alx_hw *hw, u8 dev, u16 reg, u16 *pdata);
  254. int alx_write_phy_ext(struct alx_hw *hw, u8 dev, u16 reg, u16 data);
  255. int alx_read_phy_link(struct alx_hw *hw);
  256. int alx_clear_phy_intr(struct alx_hw *hw);
  257. +int alx_config_wol(struct alx_hw *hw);
  258. void alx_cfg_mac_flowcontrol(struct alx_hw *hw, u8 fc);
  259. void alx_start_mac(struct alx_hw *hw);
  260. int alx_reset_mac(struct alx_hw *hw);
  261. @@ -563,6 +567,7 @@ bool alx_phy_configured(struct alx_hw *hw);
  262. void alx_configure_basic(struct alx_hw *hw);
  263. void alx_mask_msix(struct alx_hw *hw, int index, bool mask);
  264. void alx_disable_rss(struct alx_hw *hw);
  265. +int alx_select_powersaving_speed(struct alx_hw *hw, int *speed, u8 *duplex);
  266. bool alx_get_phy_info(struct alx_hw *hw);
  267. void alx_update_hw_stats(struct alx_hw *hw);
  268. diff --git a/drivers/net/ethernet/atheros/alx/main.c b/drivers/net/ethernet/atheros/alx/main.c
  269. index 567ee54504bcd..8252d53e3d1e2 100644
  270. --- a/drivers/net/ethernet/atheros/alx/main.c
  271. +++ b/drivers/net/ethernet/atheros/alx/main.c
  272. @@ -1070,6 +1070,7 @@ static int alx_init_sw(struct alx_priv *alx)
  273. alx->dev->max_mtu = ALX_MAX_FRAME_LEN(ALX_MAX_FRAME_SIZE);
  274. alx->tx_ringsz = 256;
  275. alx->rx_ringsz = 512;
  276. + hw->sleep_ctrl = ALX_SLEEP_WOL_MAGIC | ALX_SLEEP_WOL_PHY;
  277. hw->imt = 200;
  278. alx->int_mask = ALX_ISR_MISC;
  279. hw->dma_chnl = hw->max_dma_chnl;
  280. @@ -1345,6 +1346,65 @@ static int alx_stop(struct net_device *netdev)
  281. __alx_stop(netdev_priv(netdev));
  282. return 0;
  283. }
  284. +static int __alx_shutdown(struct pci_dev *pdev, bool *wol_en)
  285. +{
  286. + struct alx_priv *alx = pci_get_drvdata(pdev);
  287. + struct net_device *netdev = alx->dev;
  288. + struct alx_hw *hw = &alx->hw;
  289. + int err, speed;
  290. + u8 duplex;
  291. +
  292. + netif_device_detach(netdev);
  293. +
  294. + if (netif_running(netdev))
  295. + __alx_stop(alx);
  296. +
  297. +#ifdef CONFIG_PM_SLEEP
  298. + err = pci_save_state(pdev);
  299. + if (err)
  300. + return err;
  301. +#endif
  302. +
  303. + err = alx_select_powersaving_speed(hw, &speed, &duplex);
  304. + if (err)
  305. + return err;
  306. + err = alx_clear_phy_intr(hw);
  307. + if (err)
  308. + return err;
  309. + err = alx_pre_suspend(hw, speed, duplex);
  310. + if (err)
  311. + return err;
  312. + err = alx_config_wol(hw);
  313. + if (err)
  314. + return err;
  315. +
  316. + *wol_en = false;
  317. + if (hw->sleep_ctrl & ALX_SLEEP_ACTIVE) {
  318. + netif_info(alx, wol, netdev,
  319. + "wol: ctrl=%X, speed=%X\n",
  320. + hw->sleep_ctrl, speed);
  321. + device_set_wakeup_enable(&pdev->dev, true);
  322. + *wol_en = true;
  323. + }
  324. +
  325. + pci_disable_device(pdev);
  326. +
  327. + return 0;
  328. +}
  329. +
  330. +static void alx_shutdown(struct pci_dev *pdev)
  331. +{
  332. + int err;
  333. + bool wol_en;
  334. +
  335. + err = __alx_shutdown(pdev, &wol_en);
  336. + if (!err) {
  337. + pci_wake_from_d3(pdev, wol_en);
  338. + pci_set_power_state(pdev, PCI_D3hot);
  339. + } else {
  340. + dev_err(&pdev->dev, "shutdown fail %d\n", err);
  341. + }
  342. +}
  343. static void alx_link_check(struct work_struct *work)
  344. {
  345. @@ -1841,6 +1901,8 @@ static int alx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  346. goto out_unmap;
  347. }
  348. + device_set_wakeup_enable(&pdev->dev, hw->sleep_ctrl);
  349. +
  350. netdev_info(netdev,
  351. "Qualcomm Atheros AR816x/AR817x Ethernet [%pM]\n",
  352. netdev->dev_addr);
  353. @@ -1883,12 +1945,21 @@ static void alx_remove(struct pci_dev *pdev)
  354. static int alx_suspend(struct device *dev)
  355. {
  356. struct pci_dev *pdev = to_pci_dev(dev);
  357. - struct alx_priv *alx = pci_get_drvdata(pdev);
  358. + int err;
  359. + bool wol_en;
  360. - if (!netif_running(alx->dev))
  361. - return 0;
  362. - netif_device_detach(alx->dev);
  363. - __alx_stop(alx);
  364. + err = __alx_shutdown(pdev, &wol_en);
  365. + if (err) {
  366. + dev_err(&pdev->dev, "shutdown fail in suspend %d\n", err);
  367. + return err;
  368. + }
  369. +
  370. + if (wol_en) {
  371. + pci_prepare_to_sleep(pdev);
  372. + } else {
  373. + pci_wake_from_d3(pdev, false);
  374. + pci_set_power_state(pdev, PCI_D3hot);
  375. + }
  376. return 0;
  377. }
  378. @@ -1896,26 +1967,48 @@
  379. {
  380. struct pci_dev *pdev = to_pci_dev(dev);
  381. struct alx_priv *alx = pci_get_drvdata(pdev);
  382. + struct net_device *netdev = alx->dev;
  383. struct alx_hw *hw = &alx->hw;
  384. int err;
  385. + pci_set_power_state(pdev, PCI_D0);
  386. + pci_restore_state(pdev);
  387. + pci_save_state(pdev);
  388. +
  389. + pci_enable_wake(pdev, PCI_D3hot, 0);
  390. + pci_enable_wake(pdev, PCI_D3cold, 0);
  391. +
  392. + hw->link_speed = SPEED_UNKNOWN;
  393. + alx->int_mask = ALX_ISR_MISC;
  394. +
  395. + alx_reset_pcie(hw);
  396. alx_reset_phy(hw);
  397. - if (!netif_running(alx->dev))
  398. - return 0;
  399. - netif_device_attach(alx->dev);
  400. - rtnl_lock();
  401. - err = __alx_open(alx, true);
  402. - rtnl_unlock();
  403. + err = alx_reset_mac(hw);
  404. + if (err) {
  405. + netif_err(alx, hw, alx->dev,
  406. + "resume:reset_mac fail %d\n", err);
  407. + return -EIO;
  408. + }
  409. +
  410. + err = alx_setup_speed_duplex(hw, hw->adv_cfg, hw->flowctrl);
  411. + if (err) {
  412. + netif_err(alx, hw, alx->dev,
  413. + "resume:setup_speed_duplex fail %d\n", err);
  414. + return -EIO;
  415. + }
  416. +
  417. + if (netif_running(netdev)) {
  418. + err = __alx_open(alx, true);
  419. + if (err)
  420. + return err;
  421. + }
  422. - return err;
  423. + netif_device_attach(netdev);
  424. + return err;
  425. }
  426. -static SIMPLE_DEV_PM_OPS(alx_pm_ops, alx_suspend, alx_resume);
  427. -#define ALX_PM_OPS (&alx_pm_ops)
  428. -#else
  429. -#define ALX_PM_OPS NULL
  430. #endif
  431. @@ -1955,6 +2053,8 @@ static pci_ers_result_t alx_pci_error_slot_reset(struct pci_dev *pdev)
  432. }
  433. pci_set_master(pdev);
  434. + pci_enable_wake(pdev, PCI_D3hot, 0);
  435. + pci_enable_wake(pdev, PCI_D3cold, 0);
  436. alx_reset_pcie(hw);
  437. if (!alx_reset_mac(hw))
  438. @@ -2006,11 +2106,19 @@ static const struct pci_device_id alx_pci_tbl[] = {
  439. {}
  440. };
  441. +#ifdef CONFIG_PM_SLEEP
  442. +static SIMPLE_DEV_PM_OPS(alx_pm_ops, alx_suspend, alx_resume);
  443. +#define ALX_PM_OPS (&alx_pm_ops)
  444. +#else
  445. +#define ALX_PM_OPS NULL
  446. +#endif
  447. +
  448. static struct pci_driver alx_driver = {
  449. .name = alx_drv_name,
  450. .id_table = alx_pci_tbl,
  451. .probe = alx_probe,
  452. .remove = alx_remove,
  453. + .shutdown = alx_shutdown,
  454. .err_handler = &alx_err_handlers,
  455. .driver.pm = ALX_PM_OPS,
  456. };