000-enable-alx-wol-5.10.patch 12 KB

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