pm.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2014,2017 Qualcomm Atheros, Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "wil6210.h"
  17. #include <linux/jiffies.h>
  18. int wil_can_suspend(struct wil6210_priv *wil, bool is_runtime)
  19. {
  20. int rc = 0;
  21. struct wireless_dev *wdev = wil->wdev;
  22. struct net_device *ndev = wil_to_ndev(wil);
  23. wil_dbg_pm(wil, "can_suspend: %s\n", is_runtime ? "runtime" : "system");
  24. if (!(ndev->flags & IFF_UP)) {
  25. /* can always sleep when down */
  26. wil_dbg_pm(wil, "Interface is down\n");
  27. goto out;
  28. }
  29. if (test_bit(wil_status_resetting, wil->status)) {
  30. wil_dbg_pm(wil, "Delay suspend when resetting\n");
  31. rc = -EBUSY;
  32. goto out;
  33. }
  34. if (wil->recovery_state != fw_recovery_idle) {
  35. wil_dbg_pm(wil, "Delay suspend during recovery\n");
  36. rc = -EBUSY;
  37. goto out;
  38. }
  39. /* interface is running */
  40. switch (wdev->iftype) {
  41. case NL80211_IFTYPE_MONITOR:
  42. case NL80211_IFTYPE_STATION:
  43. case NL80211_IFTYPE_P2P_CLIENT:
  44. if (test_bit(wil_status_fwconnecting, wil->status)) {
  45. wil_dbg_pm(wil, "Delay suspend when connecting\n");
  46. rc = -EBUSY;
  47. goto out;
  48. }
  49. break;
  50. /* AP-like interface - can't suspend */
  51. default:
  52. wil_dbg_pm(wil, "AP-like interface\n");
  53. rc = -EBUSY;
  54. break;
  55. }
  56. out:
  57. wil_dbg_pm(wil, "can_suspend: %s => %s (%d)\n",
  58. is_runtime ? "runtime" : "system", rc ? "No" : "Yes", rc);
  59. if (rc)
  60. wil->suspend_stats.rejected_by_host++;
  61. return rc;
  62. }
  63. static int wil_resume_keep_radio_on(struct wil6210_priv *wil)
  64. {
  65. int rc = 0;
  66. /* wil_status_resuming will be cleared when getting
  67. * WMI_TRAFFIC_RESUME_EVENTID
  68. */
  69. set_bit(wil_status_resuming, wil->status);
  70. clear_bit(wil_status_suspended, wil->status);
  71. wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
  72. wil_unmask_irq(wil);
  73. wil6210_bus_request(wil, wil->bus_request_kbps_pre_suspend);
  74. /* Send WMI resume request to the device */
  75. rc = wmi_resume(wil);
  76. if (rc) {
  77. wil_err(wil, "device failed to resume (%d)\n", rc);
  78. if (no_fw_recovery)
  79. goto out;
  80. rc = wil_down(wil);
  81. if (rc) {
  82. wil_err(wil, "wil_down failed (%d)\n", rc);
  83. goto out;
  84. }
  85. rc = wil_up(wil);
  86. if (rc) {
  87. wil_err(wil, "wil_up failed (%d)\n", rc);
  88. goto out;
  89. }
  90. }
  91. /* Wake all queues */
  92. if (test_bit(wil_status_fwconnected, wil->status))
  93. wil_update_net_queues_bh(wil, NULL, false);
  94. out:
  95. if (rc)
  96. set_bit(wil_status_suspended, wil->status);
  97. return rc;
  98. }
  99. static int wil_suspend_keep_radio_on(struct wil6210_priv *wil)
  100. {
  101. int rc = 0;
  102. unsigned long start, data_comp_to;
  103. wil_dbg_pm(wil, "suspend keep radio on\n");
  104. /* Prevent handling of new tx and wmi commands */
  105. set_bit(wil_status_suspending, wil->status);
  106. wil_update_net_queues_bh(wil, NULL, true);
  107. if (!wil_is_tx_idle(wil)) {
  108. wil_dbg_pm(wil, "Pending TX data, reject suspend\n");
  109. wil->suspend_stats.rejected_by_host++;
  110. goto reject_suspend;
  111. }
  112. if (!wil_is_rx_idle(wil)) {
  113. wil_dbg_pm(wil, "Pending RX data, reject suspend\n");
  114. wil->suspend_stats.rejected_by_host++;
  115. goto reject_suspend;
  116. }
  117. if (!wil_is_wmi_idle(wil)) {
  118. wil_dbg_pm(wil, "Pending WMI events, reject suspend\n");
  119. wil->suspend_stats.rejected_by_host++;
  120. goto reject_suspend;
  121. }
  122. /* Send WMI suspend request to the device */
  123. rc = wmi_suspend(wil);
  124. if (rc) {
  125. wil_dbg_pm(wil, "wmi_suspend failed, reject suspend (%d)\n",
  126. rc);
  127. goto reject_suspend;
  128. }
  129. /* Wait for completion of the pending RX packets */
  130. start = jiffies;
  131. data_comp_to = jiffies + msecs_to_jiffies(WIL_DATA_COMPLETION_TO_MS);
  132. if (test_bit(wil_status_napi_en, wil->status)) {
  133. while (!wil_is_rx_idle(wil)) {
  134. if (time_after(jiffies, data_comp_to)) {
  135. if (wil_is_rx_idle(wil))
  136. break;
  137. wil_err(wil,
  138. "TO waiting for idle RX, suspend failed\n");
  139. wil->suspend_stats.failed_suspends++;
  140. goto resume_after_fail;
  141. }
  142. wil_dbg_ratelimited(wil, "rx vring is not empty -> NAPI\n");
  143. napi_synchronize(&wil->napi_rx);
  144. msleep(20);
  145. }
  146. }
  147. /* In case of pending WMI events, reject the suspend
  148. * and resume the device.
  149. * This can happen if the device sent the WMI events before
  150. * approving the suspend.
  151. */
  152. if (!wil_is_wmi_idle(wil)) {
  153. wil_err(wil, "suspend failed due to pending WMI events\n");
  154. wil->suspend_stats.failed_suspends++;
  155. goto resume_after_fail;
  156. }
  157. wil_mask_irq(wil);
  158. /* Disable device reset on PERST */
  159. wil_s(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
  160. if (wil->platform_ops.suspend) {
  161. rc = wil->platform_ops.suspend(wil->platform_handle, true);
  162. if (rc) {
  163. wil_err(wil, "platform device failed to suspend (%d)\n",
  164. rc);
  165. wil->suspend_stats.failed_suspends++;
  166. wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
  167. wil_unmask_irq(wil);
  168. goto resume_after_fail;
  169. }
  170. }
  171. /* Save the current bus request to return to the same in resume */
  172. wil->bus_request_kbps_pre_suspend = wil->bus_request_kbps;
  173. wil6210_bus_request(wil, 0);
  174. set_bit(wil_status_suspended, wil->status);
  175. clear_bit(wil_status_suspending, wil->status);
  176. return rc;
  177. resume_after_fail:
  178. set_bit(wil_status_resuming, wil->status);
  179. clear_bit(wil_status_suspending, wil->status);
  180. rc = wmi_resume(wil);
  181. /* if resume succeeded, reject the suspend */
  182. if (!rc) {
  183. rc = -EBUSY;
  184. if (test_bit(wil_status_fwconnected, wil->status))
  185. wil_update_net_queues_bh(wil, NULL, false);
  186. }
  187. return rc;
  188. reject_suspend:
  189. clear_bit(wil_status_suspending, wil->status);
  190. if (test_bit(wil_status_fwconnected, wil->status))
  191. wil_update_net_queues_bh(wil, NULL, false);
  192. return -EBUSY;
  193. }
  194. static int wil_suspend_radio_off(struct wil6210_priv *wil)
  195. {
  196. int rc = 0;
  197. struct net_device *ndev = wil_to_ndev(wil);
  198. wil_dbg_pm(wil, "suspend radio off\n");
  199. /* if netif up, hardware is alive, shut it down */
  200. if (ndev->flags & IFF_UP) {
  201. rc = wil_down(wil);
  202. if (rc) {
  203. wil_err(wil, "wil_down : %d\n", rc);
  204. goto out;
  205. }
  206. }
  207. /* Disable PCIe IRQ to prevent sporadic IRQs when PCIe is suspending */
  208. wil_dbg_pm(wil, "Disabling PCIe IRQ before suspending\n");
  209. wil_disable_irq(wil);
  210. if (wil->platform_ops.suspend) {
  211. rc = wil->platform_ops.suspend(wil->platform_handle, false);
  212. if (rc) {
  213. wil_enable_irq(wil);
  214. goto out;
  215. }
  216. }
  217. set_bit(wil_status_suspended, wil->status);
  218. out:
  219. wil_dbg_pm(wil, "suspend radio off: %d\n", rc);
  220. return rc;
  221. }
  222. static int wil_resume_radio_off(struct wil6210_priv *wil)
  223. {
  224. int rc = 0;
  225. struct net_device *ndev = wil_to_ndev(wil);
  226. wil_dbg_pm(wil, "Enabling PCIe IRQ\n");
  227. wil_enable_irq(wil);
  228. /* if netif up, bring hardware up
  229. * During open(), IFF_UP set after actual device method
  230. * invocation. This prevent recursive call to wil_up()
  231. * wil_status_suspended will be cleared in wil_reset
  232. */
  233. if (ndev->flags & IFF_UP)
  234. rc = wil_up(wil);
  235. else
  236. clear_bit(wil_status_suspended, wil->status);
  237. return rc;
  238. }
  239. int wil_suspend(struct wil6210_priv *wil, bool is_runtime)
  240. {
  241. int rc = 0;
  242. struct net_device *ndev = wil_to_ndev(wil);
  243. bool keep_radio_on = ndev->flags & IFF_UP &&
  244. wil->keep_radio_on_during_sleep;
  245. wil_dbg_pm(wil, "suspend: %s\n", is_runtime ? "runtime" : "system");
  246. if (test_bit(wil_status_suspended, wil->status)) {
  247. wil_dbg_pm(wil, "trying to suspend while suspended\n");
  248. return 0;
  249. }
  250. if (!keep_radio_on)
  251. rc = wil_suspend_radio_off(wil);
  252. else
  253. rc = wil_suspend_keep_radio_on(wil);
  254. wil_dbg_pm(wil, "suspend: %s => %d\n",
  255. is_runtime ? "runtime" : "system", rc);
  256. if (!rc)
  257. wil->suspend_stats.suspend_start_time = ktime_get();
  258. return rc;
  259. }
  260. int wil_resume(struct wil6210_priv *wil, bool is_runtime)
  261. {
  262. int rc = 0;
  263. struct net_device *ndev = wil_to_ndev(wil);
  264. bool keep_radio_on = ndev->flags & IFF_UP &&
  265. wil->keep_radio_on_during_sleep;
  266. unsigned long long suspend_time_usec = 0;
  267. wil_dbg_pm(wil, "resume: %s\n", is_runtime ? "runtime" : "system");
  268. if (wil->platform_ops.resume) {
  269. rc = wil->platform_ops.resume(wil->platform_handle,
  270. keep_radio_on);
  271. if (rc) {
  272. wil_err(wil, "platform_ops.resume : %d\n", rc);
  273. goto out;
  274. }
  275. }
  276. if (keep_radio_on)
  277. rc = wil_resume_keep_radio_on(wil);
  278. else
  279. rc = wil_resume_radio_off(wil);
  280. if (rc)
  281. goto out;
  282. suspend_time_usec =
  283. ktime_to_us(ktime_sub(ktime_get(),
  284. wil->suspend_stats.suspend_start_time));
  285. wil->suspend_stats.total_suspend_time += suspend_time_usec;
  286. if (suspend_time_usec < wil->suspend_stats.min_suspend_time)
  287. wil->suspend_stats.min_suspend_time = suspend_time_usec;
  288. if (suspend_time_usec > wil->suspend_stats.max_suspend_time)
  289. wil->suspend_stats.max_suspend_time = suspend_time_usec;
  290. out:
  291. wil_dbg_pm(wil, "resume: %s => %d, suspend time %lld usec\n",
  292. is_runtime ? "runtime" : "system", rc, suspend_time_usec);
  293. return rc;
  294. }