1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2014,2017 Qualcomm Atheros, Inc.
4 * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
8 #include <linux/jiffies.h>
9 #include <linux/pm_runtime.h>
11 #define WIL6210_AUTOSUSPEND_DELAY_MS (1000)
13 static void wil_pm_wake_connected_net_queues(struct wil6210_priv *wil)
17 mutex_lock(&wil->vif_mutex);
18 for (i = 0; i < GET_MAX_VIFS(wil); i++) {
19 struct wil6210_vif *vif = wil->vifs[i];
21 if (vif && test_bit(wil_vif_fwconnected, vif->status))
22 wil_update_net_queues_bh(wil, vif, NULL, false);
24 mutex_unlock(&wil->vif_mutex);
27 static void wil_pm_stop_all_net_queues(struct wil6210_priv *wil)
31 mutex_lock(&wil->vif_mutex);
32 for (i = 0; i < GET_MAX_VIFS(wil); i++) {
33 struct wil6210_vif *vif = wil->vifs[i];
36 wil_update_net_queues_bh(wil, vif, NULL, true);
38 mutex_unlock(&wil->vif_mutex);
42 wil_can_suspend_vif(struct wil6210_priv *wil, struct wil6210_vif *vif,
45 struct wireless_dev *wdev = vif_to_wdev(vif);
47 switch (wdev->iftype) {
48 case NL80211_IFTYPE_MONITOR:
49 wil_dbg_pm(wil, "Sniffer\n");
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");
60 wil_dbg_pm(wil, "STA-like interface\n");
64 /* AP-like interface - can't suspend */
66 wil_dbg_pm(wil, "AP-like interface\n");
73 int wil_can_suspend(struct wil6210_priv *wil, bool is_runtime)
76 bool wmi_only = test_bit(WMI_FW_CAPABILITY_WMI_ONLY,
77 wil->fw_capabilities);
80 wil_dbg_pm(wil, "can_suspend: %s\n", is_runtime ? "runtime" : "system");
82 if (wmi_only || debug_fw) {
83 wil_dbg_pm(wil, "Deny any suspend - %s mode\n",
84 wmi_only ? "wmi_only" : "debug_fw");
88 if (is_runtime && !wil->platform_ops.suspend) {
93 mutex_lock(&wil->vif_mutex);
94 active_ifaces = wil_has_active_ifaces(wil, true, false);
95 mutex_unlock(&wil->vif_mutex);
98 /* can always sleep when down */
99 wil_dbg_pm(wil, "Interface is down\n");
102 if (test_bit(wil_status_resetting, wil->status)) {
103 wil_dbg_pm(wil, "Delay suspend when resetting\n");
107 if (wil->recovery_state != fw_recovery_idle) {
108 wil_dbg_pm(wil, "Delay suspend during recovery\n");
113 /* interface is running */
114 mutex_lock(&wil->vif_mutex);
115 for (i = 0; i < GET_MAX_VIFS(wil); i++) {
116 struct wil6210_vif *vif = wil->vifs[i];
120 if (!wil_can_suspend_vif(wil, vif, is_runtime)) {
122 mutex_unlock(&wil->vif_mutex);
126 mutex_unlock(&wil->vif_mutex);
129 wil_dbg_pm(wil, "can_suspend: %s => %s (%d)\n",
130 is_runtime ? "runtime" : "system", rc ? "No" : "Yes", rc);
133 wil->suspend_stats.rejected_by_host++;
138 static int wil_resume_keep_radio_on(struct wil6210_priv *wil)
142 /* wil_status_resuming will be cleared when getting
143 * WMI_TRAFFIC_RESUME_EVENTID
145 set_bit(wil_status_resuming, wil->status);
146 clear_bit(wil_status_suspended, wil->status);
147 wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
150 wil6210_bus_request(wil, wil->bus_request_kbps_pre_suspend);
152 /* Send WMI resume request to the device */
153 rc = wmi_resume(wil);
155 wil_err(wil, "device failed to resume (%d)\n", rc);
160 wil_err(wil, "wil_down failed (%d)\n", rc);
165 wil_err(wil, "wil_up failed (%d)\n", rc);
170 /* Wake all queues */
171 wil_pm_wake_connected_net_queues(wil);
175 set_bit(wil_status_suspended, wil->status);
179 static int wil_suspend_keep_radio_on(struct wil6210_priv *wil)
182 unsigned long data_comp_to;
184 wil_dbg_pm(wil, "suspend keep radio on\n");
186 /* Prevent handling of new tx and wmi commands */
187 rc = down_write_trylock(&wil->mem_lock);
190 "device is busy. down_write_trylock failed, returned (0x%x)\n",
192 wil->suspend_stats.rejected_by_host++;
196 set_bit(wil_status_suspending, wil->status);
197 up_write(&wil->mem_lock);
199 wil_pm_stop_all_net_queues(wil);
201 if (!wil_is_tx_idle(wil)) {
202 wil_dbg_pm(wil, "Pending TX data, reject suspend\n");
203 wil->suspend_stats.rejected_by_host++;
207 if (!wil->txrx_ops.is_rx_idle(wil)) {
208 wil_dbg_pm(wil, "Pending RX data, reject suspend\n");
209 wil->suspend_stats.rejected_by_host++;
213 if (!wil_is_wmi_idle(wil)) {
214 wil_dbg_pm(wil, "Pending WMI events, reject suspend\n");
215 wil->suspend_stats.rejected_by_host++;
219 /* Send WMI suspend request to the device */
220 rc = wmi_suspend(wil);
222 wil_dbg_pm(wil, "wmi_suspend failed, reject suspend (%d)\n",
227 /* Wait for completion of the pending RX packets */
228 data_comp_to = jiffies + msecs_to_jiffies(WIL_DATA_COMPLETION_TO_MS);
229 if (test_bit(wil_status_napi_en, wil->status)) {
230 while (!wil->txrx_ops.is_rx_idle(wil)) {
231 if (time_after(jiffies, data_comp_to)) {
232 if (wil->txrx_ops.is_rx_idle(wil))
235 "TO waiting for idle RX, suspend failed\n");
236 wil->suspend_stats.r_on.failed_suspends++;
237 goto resume_after_fail;
239 wil_dbg_ratelimited(wil, "rx vring is not empty -> NAPI\n");
240 napi_synchronize(&wil->napi_rx);
245 /* In case of pending WMI events, reject the suspend
246 * and resume the device.
247 * This can happen if the device sent the WMI events before
248 * approving the suspend.
250 if (!wil_is_wmi_idle(wil)) {
251 wil_err(wil, "suspend failed due to pending WMI events\n");
252 wil->suspend_stats.r_on.failed_suspends++;
253 goto resume_after_fail;
258 /* Disable device reset on PERST */
259 wil_s(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
261 if (wil->platform_ops.suspend) {
262 rc = wil->platform_ops.suspend(wil->platform_handle, true);
264 wil_err(wil, "platform device failed to suspend (%d)\n",
266 wil->suspend_stats.r_on.failed_suspends++;
267 wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
269 goto resume_after_fail;
273 /* Save the current bus request to return to the same in resume */
274 wil->bus_request_kbps_pre_suspend = wil->bus_request_kbps;
275 wil6210_bus_request(wil, 0);
277 set_bit(wil_status_suspended, wil->status);
278 clear_bit(wil_status_suspending, wil->status);
283 set_bit(wil_status_resuming, wil->status);
284 clear_bit(wil_status_suspending, wil->status);
285 rc = wmi_resume(wil);
286 /* if resume succeeded, reject the suspend */
289 wil_pm_wake_connected_net_queues(wil);
294 clear_bit(wil_status_suspending, wil->status);
295 wil_pm_wake_connected_net_queues(wil);
299 static int wil_suspend_radio_off(struct wil6210_priv *wil)
304 wil_dbg_pm(wil, "suspend radio off\n");
306 rc = down_write_trylock(&wil->mem_lock);
309 "device is busy. down_write_trylock failed, returned (0x%x)\n",
311 wil->suspend_stats.rejected_by_host++;
315 set_bit(wil_status_suspending, wil->status);
316 up_write(&wil->mem_lock);
318 /* if netif up, hardware is alive, shut it down */
319 mutex_lock(&wil->vif_mutex);
320 active_ifaces = wil_has_active_ifaces(wil, true, false);
321 mutex_unlock(&wil->vif_mutex);
326 wil_err(wil, "wil_down : %d\n", rc);
327 wil->suspend_stats.r_off.failed_suspends++;
332 /* Disable PCIe IRQ to prevent sporadic IRQs when PCIe is suspending */
333 wil_dbg_pm(wil, "Disabling PCIe IRQ before suspending\n");
334 wil_disable_irq(wil);
336 if (wil->platform_ops.suspend) {
337 rc = wil->platform_ops.suspend(wil->platform_handle, false);
340 wil->suspend_stats.r_off.failed_suspends++;
345 set_bit(wil_status_suspended, wil->status);
348 clear_bit(wil_status_suspending, wil->status);
349 wil_dbg_pm(wil, "suspend radio off: %d\n", rc);
354 static int wil_resume_radio_off(struct wil6210_priv *wil)
359 wil_dbg_pm(wil, "Enabling PCIe IRQ\n");
361 /* if any netif up, bring hardware up
362 * During open(), IFF_UP set after actual device method
363 * invocation. This prevent recursive call to wil_up()
364 * wil_status_suspended will be cleared in wil_reset
366 mutex_lock(&wil->vif_mutex);
367 active_ifaces = wil_has_active_ifaces(wil, true, false);
368 mutex_unlock(&wil->vif_mutex);
372 clear_bit(wil_status_suspended, wil->status);
377 int wil_suspend(struct wil6210_priv *wil, bool is_runtime, bool keep_radio_on)
381 wil_dbg_pm(wil, "suspend: %s\n", is_runtime ? "runtime" : "system");
383 if (test_bit(wil_status_suspended, wil->status)) {
384 wil_dbg_pm(wil, "trying to suspend while suspended\n");
389 rc = wil_suspend_radio_off(wil);
391 rc = wil_suspend_keep_radio_on(wil);
393 wil_dbg_pm(wil, "suspend: %s => %d\n",
394 is_runtime ? "runtime" : "system", rc);
399 int wil_resume(struct wil6210_priv *wil, bool is_runtime, bool keep_radio_on)
403 wil_dbg_pm(wil, "resume: %s\n", is_runtime ? "runtime" : "system");
405 if (wil->platform_ops.resume) {
406 rc = wil->platform_ops.resume(wil->platform_handle,
409 wil_err(wil, "platform_ops.resume : %d\n", rc);
415 rc = wil_resume_keep_radio_on(wil);
417 rc = wil_resume_radio_off(wil);
420 wil_dbg_pm(wil, "resume: %s => %d\n", is_runtime ? "runtime" : "system",
425 void wil_pm_runtime_allow(struct wil6210_priv *wil)
427 struct device *dev = wil_to_dev(wil);
429 pm_runtime_put_noidle(dev);
430 pm_runtime_set_autosuspend_delay(dev, WIL6210_AUTOSUSPEND_DELAY_MS);
431 pm_runtime_use_autosuspend(dev);
432 pm_runtime_allow(dev);
435 void wil_pm_runtime_forbid(struct wil6210_priv *wil)
437 struct device *dev = wil_to_dev(wil);
439 pm_runtime_forbid(dev);
440 pm_runtime_get_noresume(dev);
443 int wil_pm_runtime_get(struct wil6210_priv *wil)
446 struct device *dev = wil_to_dev(wil);
448 rc = pm_runtime_resume_and_get(dev);
450 wil_err(wil, "pm_runtime_resume_and_get() failed, rc = %d\n", rc);
457 void wil_pm_runtime_put(struct wil6210_priv *wil)
459 struct device *dev = wil_to_dev(wil);
461 pm_runtime_mark_last_busy(dev);
462 pm_runtime_put_autosuspend(dev);