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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 --git a/drivers/net/ethernet/atheros/alx/main.c b/drivers/net/ethernet/atheros/alx/main.c
  265. index 4ad3fc72e..74785ae2b 100644
  266. --- a/drivers/net/ethernet/atheros/alx/main.c
  267. +++ b/drivers/net/ethernet/atheros/alx/main.c
  268. @@ -1069,6 +1069,7 @@ static int alx_init_sw(struct alx_priv *alx)
  269. alx->dev->max_mtu = ALX_MAX_FRAME_LEN(ALX_MAX_FRAME_SIZE);
  270. alx->tx_ringsz = 256;
  271. alx->rx_ringsz = 512;
  272. + hw->sleep_ctrl = ALX_SLEEP_WOL_MAGIC | ALX_SLEEP_WOL_PHY;
  273. hw->imt = 200;
  274. alx->int_mask = ALX_ISR_MISC;
  275. hw->dma_chnl = hw->max_dma_chnl;
  276. @@ -1368,6 +1369,66 @@ static int alx_stop(struct net_device *netdev)
  277. return 0;
  278. }
  279. +static int __alx_shutdown(struct pci_dev *pdev, bool *wol_en)
  280. +{
  281. + struct alx_priv *alx = pci_get_drvdata(pdev);
  282. + struct net_device *netdev = alx->dev;
  283. + struct alx_hw *hw = &alx->hw;
  284. + int err, speed;
  285. + u8 duplex;
  286. +
  287. + netif_device_detach(netdev);
  288. +
  289. + if (netif_running(netdev))
  290. + __alx_stop(alx);
  291. +
  292. +#ifdef CONFIG_PM_SLEEP
  293. + err = pci_save_state(pdev);
  294. + if (err)
  295. + return err;
  296. +#endif
  297. +
  298. + err = alx_select_powersaving_speed(hw, &speed, &duplex);
  299. + if (err)
  300. + return err;
  301. + err = alx_clear_phy_intr(hw);
  302. + if (err)
  303. + return err;
  304. + err = alx_pre_suspend(hw, speed, duplex);
  305. + if (err)
  306. + return err;
  307. + err = alx_config_wol(hw);
  308. + if (err)
  309. + return err;
  310. +
  311. + *wol_en = false;
  312. + if (hw->sleep_ctrl & ALX_SLEEP_ACTIVE) {
  313. + netif_info(alx, wol, netdev,
  314. + "wol: ctrl=%X, speed=%X\n",
  315. + hw->sleep_ctrl, speed);
  316. + device_set_wakeup_enable(&pdev->dev, true);
  317. + *wol_en = true;
  318. + }
  319. +
  320. + pci_disable_device(pdev);
  321. +
  322. + return 0;
  323. +}
  324. +
  325. +static void alx_shutdown(struct pci_dev *pdev)
  326. +{
  327. + int err;
  328. + bool wol_en;
  329. +
  330. + err = __alx_shutdown(pdev, &wol_en);
  331. + if (!err) {
  332. + pci_wake_from_d3(pdev, wol_en);
  333. + pci_set_power_state(pdev, PCI_D3hot);
  334. + } else {
  335. + dev_err(&pdev->dev, "shutdown fail %d\n", err);
  336. + }
  337. +}
  338. +
  339. static void alx_link_check(struct work_struct *work)
  340. {
  341. struct alx_priv *alx;
  342. @@ -1862,6 +1923,7 @@ static int alx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  343. goto out_unmap;
  344. }
  345. + device_set_wakeup_enable(&pdev->dev, hw->sleep_ctrl);
  346. netdev_info(netdev,
  347. "Qualcomm Atheros AR816x/AR817x Ethernet [%pM]\n",
  348. netdev->dev_addr);
  349. @@ -1907,13 +1969,26 @@ static int alx_suspend(struct device *dev)
  350. {
  351. struct alx_priv *alx = dev_get_drvdata(dev);
  352. - if (!netif_running(alx->dev))
  353. - return 0;
  354. - netif_device_detach(alx->dev);
  355. + struct pci_dev *pdev = alx->hw.pdev;
  356. + int err;
  357. + bool wol_en;
  358. - mutex_lock(&alx->mtx);
  359. - __alx_stop(alx);
  360. - mutex_unlock(&alx->mtx);
  361. + //if (!netif_running(alx->dev))
  362. + // return 0;
  363. + //netif_device_detach(alx->dev);
  364. + //__alx_stop(alx);
  365. + err = __alx_shutdown(pdev, &wol_en);
  366. + if (err) {
  367. + dev_err(&pdev->dev, "shutdown fail in suspend %d\n", err);
  368. + return err;
  369. + }
  370. +
  371. + if (wol_en) {
  372. + pci_prepare_to_sleep(pdev);
  373. + } else {
  374. + pci_wake_from_d3(pdev, false);
  375. + pci_set_power_state(pdev, PCI_D3hot);
  376. + }
  377. return 0;
  378. }
  379. @@ -1921,20 +1996,45 @@ static int alx_suspend(struct device *dev)
  380. static int alx_resume(struct device *dev)
  381. {
  382. struct alx_priv *alx = dev_get_drvdata(dev);
  383. + struct net_device *netdev = alx->dev;
  384. struct alx_hw *hw = &alx->hw;
  385. + struct pci_dev *pdev = hw->pdev;
  386. int err;
  387. mutex_lock(&alx->mtx);
  388. +
  389. + pci_set_power_state(pdev, PCI_D0);
  390. + pci_restore_state(pdev);
  391. + pci_save_state(pdev);
  392. +
  393. + pci_enable_wake(pdev, PCI_D3hot, 0);
  394. + pci_enable_wake(pdev, PCI_D3cold, 0);
  395. +
  396. + hw->link_speed = SPEED_UNKNOWN;
  397. + alx->int_mask = ALX_ISR_MISC;
  398. +
  399. + alx_reset_pcie(hw);
  400. alx_reset_phy(hw);
  401. - if (!netif_running(alx->dev)) {
  402. - err = 0;
  403. - goto unlock;
  404. + //if (!netif_running(alx->dev)) {
  405. + // err = 0;
  406. + // goto unlock;
  407. + //}
  408. + err = alx_reset_mac(hw);
  409. + if (err) {
  410. + netif_err(alx, hw, alx->dev, "resume:reset_mac fail %d\n", err);
  411. + err = -EIO;
  412. + goto unlock;
  413. }
  414. - err = __alx_open(alx, true);
  415. - if (err)
  416. - goto unlock;
  417. + //err = __alx_open(alx, true);
  418. + //if (err)
  419. + // goto unlock;
  420. + if (netif_running(netdev)) {
  421. + err = __alx_open(alx, true);
  422. + if (err)
  423. + goto unlock;
  424. + }
  425. netif_device_attach(alx->dev);
  426. @@ -1943,10 +2043,6 @@ static int alx_resume(struct device *dev)
  427. return err;
  428. }
  429. -static SIMPLE_DEV_PM_OPS(alx_pm_ops, alx_suspend, alx_resume);
  430. -#define ALX_PM_OPS (&alx_pm_ops)
  431. -#else
  432. -#define ALX_PM_OPS NULL
  433. #endif
  434. @@ -1992,6 +2088,8 @@ static pci_ers_result_t alx_pci_error_slot_reset(struct pci_dev *pdev)
  435. }
  436. pci_set_master(pdev);
  437. + pci_enable_wake(pdev, PCI_D3hot, 0);
  438. + pci_enable_wake(pdev, PCI_D3cold, 0);
  439. alx_reset_pcie(hw);
  440. if (!alx_reset_mac(hw))
  441. @@ -2041,11 +2139,20 @@ static const struct pci_device_id alx_pci_tbl[] = {
  442. {}
  443. };
  444. +#ifdef CONFIG_PM_SLEEP
  445. +static SIMPLE_DEV_PM_OPS(alx_pm_ops, alx_suspend, alx_resume);
  446. +#define ALX_PM_OPS (&alx_pm_ops)
  447. +#else
  448. +#define ALX_PM_OPS NULL
  449. +#endif
  450. +
  451. +
  452. static struct pci_driver alx_driver = {
  453. .name = alx_drv_name,
  454. .id_table = alx_pci_tbl,
  455. .probe = alx_probe,
  456. .remove = alx_remove,
  457. + .shutdown = alx_shutdown,
  458. .err_handler = &alx_err_handlers,
  459. .driver.pm = ALX_PM_OPS,
  460. };