pm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Copyright (c) 2014,2017 Qualcomm Atheros, Inc.
  3. * Copyright (c) 2018, The Linux Foundation. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "wil6210.h"
  18. #include <linux/jiffies.h>
  19. #include <linux/pm_runtime.h>
  20. #define WIL6210_AUTOSUSPEND_DELAY_MS (1000)
  21. static void wil_pm_wake_connected_net_queues(struct wil6210_priv *wil)
  22. {
  23. int i;
  24. mutex_lock(&wil->vif_mutex);
  25. for (i = 0; i < wil->max_vifs; i++) {
  26. struct wil6210_vif *vif = wil->vifs[i];
  27. if (vif && test_bit(wil_vif_fwconnected, vif->status))
  28. wil_update_net_queues_bh(wil, vif, NULL, false);
  29. }
  30. mutex_unlock(&wil->vif_mutex);
  31. }
  32. static void wil_pm_stop_all_net_queues(struct wil6210_priv *wil)
  33. {
  34. int i;
  35. mutex_lock(&wil->vif_mutex);
  36. for (i = 0; i < wil->max_vifs; i++) {
  37. struct wil6210_vif *vif = wil->vifs[i];
  38. if (vif)
  39. wil_update_net_queues_bh(wil, vif, NULL, true);
  40. }
  41. mutex_unlock(&wil->vif_mutex);
  42. }
  43. static bool
  44. wil_can_suspend_vif(struct wil6210_priv *wil, struct wil6210_vif *vif,
  45. bool is_runtime)
  46. {
  47. struct wireless_dev *wdev = vif_to_wdev(vif);
  48. switch (wdev->iftype) {
  49. case NL80211_IFTYPE_MONITOR:
  50. wil_dbg_pm(wil, "Sniffer\n");
  51. return false;
  52. /* for STA-like interface, don't runtime suspend */
  53. case NL80211_IFTYPE_STATION:
  54. case NL80211_IFTYPE_P2P_CLIENT:
  55. if (test_bit(wil_vif_fwconnecting, vif->status)) {
  56. wil_dbg_pm(wil, "Delay suspend when connecting\n");
  57. return false;
  58. }
  59. if (is_runtime) {
  60. wil_dbg_pm(wil, "STA-like interface\n");
  61. return false;
  62. }
  63. break;
  64. /* AP-like interface - can't suspend */
  65. default:
  66. wil_dbg_pm(wil, "AP-like interface\n");
  67. return false;
  68. }
  69. return true;
  70. }
  71. int wil_can_suspend(struct wil6210_priv *wil, bool is_runtime)
  72. {
  73. int rc = 0, i;
  74. bool wmi_only = test_bit(WMI_FW_CAPABILITY_WMI_ONLY,
  75. wil->fw_capabilities);
  76. bool active_ifaces;
  77. wil_dbg_pm(wil, "can_suspend: %s\n", is_runtime ? "runtime" : "system");
  78. if (wmi_only || debug_fw) {
  79. wil_dbg_pm(wil, "Deny any suspend - %s mode\n",
  80. wmi_only ? "wmi_only" : "debug_fw");
  81. rc = -EBUSY;
  82. goto out;
  83. }
  84. if (is_runtime && !wil->platform_ops.suspend) {
  85. rc = -EBUSY;
  86. goto out;
  87. }
  88. mutex_lock(&wil->vif_mutex);
  89. active_ifaces = wil_has_active_ifaces(wil, true, false);
  90. mutex_unlock(&wil->vif_mutex);
  91. if (!active_ifaces) {
  92. /* can always sleep when down */
  93. wil_dbg_pm(wil, "Interface is down\n");
  94. goto out;
  95. }
  96. if (test_bit(wil_status_resetting, wil->status)) {
  97. wil_dbg_pm(wil, "Delay suspend when resetting\n");
  98. rc = -EBUSY;
  99. goto out;
  100. }
  101. if (wil->recovery_state != fw_recovery_idle) {
  102. wil_dbg_pm(wil, "Delay suspend during recovery\n");
  103. rc = -EBUSY;
  104. goto out;
  105. }
  106. /* interface is running */
  107. mutex_lock(&wil->vif_mutex);
  108. for (i = 0; i < wil->max_vifs; i++) {
  109. struct wil6210_vif *vif = wil->vifs[i];
  110. if (!vif)
  111. continue;
  112. if (!wil_can_suspend_vif(wil, vif, is_runtime)) {
  113. rc = -EBUSY;
  114. mutex_unlock(&wil->vif_mutex);
  115. goto out;
  116. }
  117. }
  118. mutex_unlock(&wil->vif_mutex);
  119. out:
  120. wil_dbg_pm(wil, "can_suspend: %s => %s (%d)\n",
  121. is_runtime ? "runtime" : "system", rc ? "No" : "Yes", rc);
  122. if (rc)
  123. wil->suspend_stats.rejected_by_host++;
  124. return rc;
  125. }
  126. static int wil_resume_keep_radio_on(struct wil6210_priv *wil)
  127. {
  128. int rc = 0;
  129. /* wil_status_resuming will be cleared when getting
  130. * WMI_TRAFFIC_RESUME_EVENTID
  131. */
  132. set_bit(wil_status_resuming, wil->status);
  133. clear_bit(wil_status_suspended, wil->status);
  134. wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
  135. wil_unmask_irq(wil);
  136. wil6210_bus_request(wil, wil->bus_request_kbps_pre_suspend);
  137. /* Send WMI resume request to the device */
  138. rc = wmi_resume(wil);
  139. if (rc) {
  140. wil_err(wil, "device failed to resume (%d)\n", rc);
  141. if (no_fw_recovery)
  142. goto out;
  143. rc = wil_down(wil);
  144. if (rc) {
  145. wil_err(wil, "wil_down failed (%d)\n", rc);
  146. goto out;
  147. }
  148. rc = wil_up(wil);
  149. if (rc) {
  150. wil_err(wil, "wil_up failed (%d)\n", rc);
  151. goto out;
  152. }
  153. }
  154. /* Wake all queues */
  155. wil_pm_wake_connected_net_queues(wil);
  156. out:
  157. if (rc)
  158. set_bit(wil_status_suspended, wil->status);
  159. return rc;
  160. }
  161. static int wil_suspend_keep_radio_on(struct wil6210_priv *wil)
  162. {
  163. int rc = 0;
  164. unsigned long start, data_comp_to;
  165. wil_dbg_pm(wil, "suspend keep radio on\n");
  166. /* Prevent handling of new tx and wmi commands */
  167. set_bit(wil_status_suspending, wil->status);
  168. if (test_bit(wil_status_collecting_dumps, wil->status)) {
  169. /* Device collects crash dump, cancel the suspend */
  170. wil_dbg_pm(wil, "reject suspend while collecting crash dump\n");
  171. clear_bit(wil_status_suspending, wil->status);
  172. wil->suspend_stats.rejected_by_host++;
  173. return -EBUSY;
  174. }
  175. wil_pm_stop_all_net_queues(wil);
  176. if (!wil_is_tx_idle(wil)) {
  177. wil_dbg_pm(wil, "Pending TX data, reject suspend\n");
  178. wil->suspend_stats.rejected_by_host++;
  179. goto reject_suspend;
  180. }
  181. if (!wil_is_rx_idle(wil)) {
  182. wil_dbg_pm(wil, "Pending RX data, reject suspend\n");
  183. wil->suspend_stats.rejected_by_host++;
  184. goto reject_suspend;
  185. }
  186. if (!wil_is_wmi_idle(wil)) {
  187. wil_dbg_pm(wil, "Pending WMI events, reject suspend\n");
  188. wil->suspend_stats.rejected_by_host++;
  189. goto reject_suspend;
  190. }
  191. /* Send WMI suspend request to the device */
  192. rc = wmi_suspend(wil);
  193. if (rc) {
  194. wil_dbg_pm(wil, "wmi_suspend failed, reject suspend (%d)\n",
  195. rc);
  196. goto reject_suspend;
  197. }
  198. /* Wait for completion of the pending RX packets */
  199. start = jiffies;
  200. data_comp_to = jiffies + msecs_to_jiffies(WIL_DATA_COMPLETION_TO_MS);
  201. if (test_bit(wil_status_napi_en, wil->status)) {
  202. while (!wil_is_rx_idle(wil)) {
  203. if (time_after(jiffies, data_comp_to)) {
  204. if (wil_is_rx_idle(wil))
  205. break;
  206. wil_err(wil,
  207. "TO waiting for idle RX, suspend failed\n");
  208. wil->suspend_stats.r_on.failed_suspends++;
  209. goto resume_after_fail;
  210. }
  211. wil_dbg_ratelimited(wil, "rx vring is not empty -> NAPI\n");
  212. napi_synchronize(&wil->napi_rx);
  213. msleep(20);
  214. }
  215. }
  216. /* In case of pending WMI events, reject the suspend
  217. * and resume the device.
  218. * This can happen if the device sent the WMI events before
  219. * approving the suspend.
  220. */
  221. if (!wil_is_wmi_idle(wil)) {
  222. wil_err(wil, "suspend failed due to pending WMI events\n");
  223. wil->suspend_stats.r_on.failed_suspends++;
  224. goto resume_after_fail;
  225. }
  226. wil_mask_irq(wil);
  227. /* Disable device reset on PERST */
  228. wil_s(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
  229. if (wil->platform_ops.suspend) {
  230. rc = wil->platform_ops.suspend(wil->platform_handle, true);
  231. if (rc) {
  232. wil_err(wil, "platform device failed to suspend (%d)\n",
  233. rc);
  234. wil->suspend_stats.r_on.failed_suspends++;
  235. wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
  236. wil_unmask_irq(wil);
  237. goto resume_after_fail;
  238. }
  239. }
  240. /* Save the current bus request to return to the same in resume */
  241. wil->bus_request_kbps_pre_suspend = wil->bus_request_kbps;
  242. wil6210_bus_request(wil, 0);
  243. set_bit(wil_status_suspended, wil->status);
  244. clear_bit(wil_status_suspending, wil->status);
  245. return rc;
  246. resume_after_fail:
  247. set_bit(wil_status_resuming, wil->status);
  248. clear_bit(wil_status_suspending, wil->status);
  249. rc = wmi_resume(wil);
  250. /* if resume succeeded, reject the suspend */
  251. if (!rc) {
  252. rc = -EBUSY;
  253. wil_pm_wake_connected_net_queues(wil);
  254. }
  255. return rc;
  256. reject_suspend:
  257. clear_bit(wil_status_suspending, wil->status);
  258. wil_pm_wake_connected_net_queues(wil);
  259. return -EBUSY;
  260. }
  261. static int wil_suspend_radio_off(struct wil6210_priv *wil)
  262. {
  263. int rc = 0;
  264. bool active_ifaces;
  265. wil_dbg_pm(wil, "suspend radio off\n");
  266. set_bit(wil_status_suspending, wil->status);
  267. if (test_bit(wil_status_collecting_dumps, wil->status)) {
  268. /* Device collects crash dump, cancel the suspend */
  269. wil_dbg_pm(wil, "reject suspend while collecting crash dump\n");
  270. clear_bit(wil_status_suspending, wil->status);
  271. wil->suspend_stats.rejected_by_host++;
  272. return -EBUSY;
  273. }
  274. /* if netif up, hardware is alive, shut it down */
  275. mutex_lock(&wil->vif_mutex);
  276. active_ifaces = wil_has_active_ifaces(wil, true, false);
  277. mutex_unlock(&wil->vif_mutex);
  278. if (active_ifaces) {
  279. rc = wil_down(wil);
  280. if (rc) {
  281. wil_err(wil, "wil_down : %d\n", rc);
  282. wil->suspend_stats.r_off.failed_suspends++;
  283. goto out;
  284. }
  285. }
  286. /* Disable PCIe IRQ to prevent sporadic IRQs when PCIe is suspending */
  287. wil_dbg_pm(wil, "Disabling PCIe IRQ before suspending\n");
  288. wil_disable_irq(wil);
  289. if (wil->platform_ops.suspend) {
  290. rc = wil->platform_ops.suspend(wil->platform_handle, false);
  291. if (rc) {
  292. wil_enable_irq(wil);
  293. wil->suspend_stats.r_off.failed_suspends++;
  294. goto out;
  295. }
  296. }
  297. set_bit(wil_status_suspended, wil->status);
  298. out:
  299. clear_bit(wil_status_suspending, wil->status);
  300. wil_dbg_pm(wil, "suspend radio off: %d\n", rc);
  301. return rc;
  302. }
  303. static int wil_resume_radio_off(struct wil6210_priv *wil)
  304. {
  305. int rc = 0;
  306. bool active_ifaces;
  307. wil_dbg_pm(wil, "Enabling PCIe IRQ\n");
  308. wil_enable_irq(wil);
  309. /* if any netif up, bring hardware up
  310. * During open(), IFF_UP set after actual device method
  311. * invocation. This prevent recursive call to wil_up()
  312. * wil_status_suspended will be cleared in wil_reset
  313. */
  314. mutex_lock(&wil->vif_mutex);
  315. active_ifaces = wil_has_active_ifaces(wil, true, false);
  316. mutex_unlock(&wil->vif_mutex);
  317. if (active_ifaces)
  318. rc = wil_up(wil);
  319. else
  320. clear_bit(wil_status_suspended, wil->status);
  321. return rc;
  322. }
  323. int wil_suspend(struct wil6210_priv *wil, bool is_runtime, bool keep_radio_on)
  324. {
  325. int rc = 0;
  326. wil_dbg_pm(wil, "suspend: %s\n", is_runtime ? "runtime" : "system");
  327. if (test_bit(wil_status_suspended, wil->status)) {
  328. wil_dbg_pm(wil, "trying to suspend while suspended\n");
  329. return 0;
  330. }
  331. if (!keep_radio_on)
  332. rc = wil_suspend_radio_off(wil);
  333. else
  334. rc = wil_suspend_keep_radio_on(wil);
  335. wil_dbg_pm(wil, "suspend: %s => %d\n",
  336. is_runtime ? "runtime" : "system", rc);
  337. return rc;
  338. }
  339. int wil_resume(struct wil6210_priv *wil, bool is_runtime, bool keep_radio_on)
  340. {
  341. int rc = 0;
  342. wil_dbg_pm(wil, "resume: %s\n", is_runtime ? "runtime" : "system");
  343. if (wil->platform_ops.resume) {
  344. rc = wil->platform_ops.resume(wil->platform_handle,
  345. keep_radio_on);
  346. if (rc) {
  347. wil_err(wil, "platform_ops.resume : %d\n", rc);
  348. goto out;
  349. }
  350. }
  351. if (keep_radio_on)
  352. rc = wil_resume_keep_radio_on(wil);
  353. else
  354. rc = wil_resume_radio_off(wil);
  355. out:
  356. wil_dbg_pm(wil, "resume: %s => %d\n", is_runtime ? "runtime" : "system",
  357. rc);
  358. return rc;
  359. }
  360. void wil_pm_runtime_allow(struct wil6210_priv *wil)
  361. {
  362. struct device *dev = wil_to_dev(wil);
  363. pm_runtime_put_noidle(dev);
  364. pm_runtime_set_autosuspend_delay(dev, WIL6210_AUTOSUSPEND_DELAY_MS);
  365. pm_runtime_use_autosuspend(dev);
  366. pm_runtime_allow(dev);
  367. }
  368. void wil_pm_runtime_forbid(struct wil6210_priv *wil)
  369. {
  370. struct device *dev = wil_to_dev(wil);
  371. pm_runtime_forbid(dev);
  372. pm_runtime_get_noresume(dev);
  373. }
  374. int wil_pm_runtime_get(struct wil6210_priv *wil)
  375. {
  376. int rc;
  377. struct device *dev = wil_to_dev(wil);
  378. rc = pm_runtime_get_sync(dev);
  379. if (rc < 0) {
  380. wil_err(wil, "pm_runtime_get_sync() failed, rc = %d\n", rc);
  381. pm_runtime_put_noidle(dev);
  382. return rc;
  383. }
  384. return 0;
  385. }
  386. void wil_pm_runtime_put(struct wil6210_priv *wil)
  387. {
  388. struct device *dev = wil_to_dev(wil);
  389. pm_runtime_mark_last_busy(dev);
  390. pm_runtime_put_autosuspend(dev);
  391. }