000-enable-alx-wol-5.4.x.patch 12 KB

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