2 * Copyright (c) 2014,2017 Qualcomm Atheros, Inc.
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.
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.
18 #include <linux/jiffies.h>
20 int wil_can_suspend(struct wil6210_priv *wil, bool is_runtime)
23 struct wireless_dev *wdev = wil->wdev;
24 struct net_device *ndev = wil_to_ndev(wil);
26 wil_dbg_pm(wil, "can_suspend: %s\n", is_runtime ? "runtime" : "system");
28 if (!(ndev->flags & IFF_UP)) {
29 /* can always sleep when down */
30 wil_dbg_pm(wil, "Interface is down\n");
33 if (test_bit(wil_status_resetting, wil->status)) {
34 wil_dbg_pm(wil, "Delay suspend when resetting\n");
38 if (wil->recovery_state != fw_recovery_idle) {
39 wil_dbg_pm(wil, "Delay suspend during recovery\n");
44 /* interface is running */
45 switch (wdev->iftype) {
46 case NL80211_IFTYPE_MONITOR:
47 case NL80211_IFTYPE_STATION:
48 case NL80211_IFTYPE_P2P_CLIENT:
49 if (test_bit(wil_status_fwconnecting, wil->status)) {
50 wil_dbg_pm(wil, "Delay suspend when connecting\n");
55 /* AP-like interface - can't suspend */
57 wil_dbg_pm(wil, "AP-like interface\n");
63 wil_dbg_pm(wil, "can_suspend: %s => %s (%d)\n",
64 is_runtime ? "runtime" : "system", rc ? "No" : "Yes", rc);
67 wil->suspend_stats.rejected_by_host++;
72 static int wil_resume_keep_radio_on(struct wil6210_priv *wil)
76 /* wil_status_resuming will be cleared when getting
77 * WMI_TRAFFIC_RESUME_EVENTID
79 set_bit(wil_status_resuming, wil->status);
80 clear_bit(wil_status_suspended, wil->status);
81 wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
84 wil6210_bus_request(wil, wil->bus_request_kbps_pre_suspend);
86 /* Send WMI resume request to the device */
89 wil_err(wil, "device failed to resume (%d)\n", rc);
94 wil_err(wil, "wil_down failed (%d)\n", rc);
99 wil_err(wil, "wil_up failed (%d)\n", rc);
104 /* Wake all queues */
105 if (test_bit(wil_status_fwconnected, wil->status))
106 wil_update_net_queues_bh(wil, NULL, false);
110 set_bit(wil_status_suspended, wil->status);
114 static int wil_suspend_keep_radio_on(struct wil6210_priv *wil)
117 unsigned long start, data_comp_to;
119 wil_dbg_pm(wil, "suspend keep radio on\n");
121 /* Prevent handling of new tx and wmi commands */
122 set_bit(wil_status_suspending, wil->status);
123 wil_update_net_queues_bh(wil, NULL, true);
125 if (!wil_is_tx_idle(wil)) {
126 wil_dbg_pm(wil, "Pending TX data, reject suspend\n");
127 wil->suspend_stats.rejected_by_host++;
131 if (!wil_is_rx_idle(wil)) {
132 wil_dbg_pm(wil, "Pending RX data, reject suspend\n");
133 wil->suspend_stats.rejected_by_host++;
137 if (!wil_is_wmi_idle(wil)) {
138 wil_dbg_pm(wil, "Pending WMI events, reject suspend\n");
139 wil->suspend_stats.rejected_by_host++;
143 /* Send WMI suspend request to the device */
144 rc = wmi_suspend(wil);
146 wil_dbg_pm(wil, "wmi_suspend failed, reject suspend (%d)\n",
151 /* Wait for completion of the pending RX packets */
153 data_comp_to = jiffies + msecs_to_jiffies(WIL_DATA_COMPLETION_TO_MS);
154 if (test_bit(wil_status_napi_en, wil->status)) {
155 while (!wil_is_rx_idle(wil)) {
156 if (time_after(jiffies, data_comp_to)) {
157 if (wil_is_rx_idle(wil))
160 "TO waiting for idle RX, suspend failed\n");
161 wil->suspend_stats.failed_suspends++;
162 goto resume_after_fail;
164 wil_dbg_ratelimited(wil, "rx vring is not empty -> NAPI\n");
165 napi_synchronize(&wil->napi_rx);
170 /* In case of pending WMI events, reject the suspend
171 * and resume the device.
172 * This can happen if the device sent the WMI events before
173 * approving the suspend.
175 if (!wil_is_wmi_idle(wil)) {
176 wil_err(wil, "suspend failed due to pending WMI events\n");
177 wil->suspend_stats.failed_suspends++;
178 goto resume_after_fail;
183 /* Disable device reset on PERST */
184 wil_s(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
186 if (wil->platform_ops.suspend) {
187 rc = wil->platform_ops.suspend(wil->platform_handle, true);
189 wil_err(wil, "platform device failed to suspend (%d)\n",
191 wil->suspend_stats.failed_suspends++;
192 wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
194 goto resume_after_fail;
198 /* Save the current bus request to return to the same in resume */
199 wil->bus_request_kbps_pre_suspend = wil->bus_request_kbps;
200 wil6210_bus_request(wil, 0);
202 set_bit(wil_status_suspended, wil->status);
203 clear_bit(wil_status_suspending, wil->status);
208 set_bit(wil_status_resuming, wil->status);
209 clear_bit(wil_status_suspending, wil->status);
210 rc = wmi_resume(wil);
211 /* if resume succeeded, reject the suspend */
214 if (test_bit(wil_status_fwconnected, wil->status))
215 wil_update_net_queues_bh(wil, NULL, false);
220 clear_bit(wil_status_suspending, wil->status);
221 if (test_bit(wil_status_fwconnected, wil->status))
222 wil_update_net_queues_bh(wil, NULL, false);
226 static int wil_suspend_radio_off(struct wil6210_priv *wil)
229 struct net_device *ndev = wil_to_ndev(wil);
231 wil_dbg_pm(wil, "suspend radio off\n");
233 /* if netif up, hardware is alive, shut it down */
234 if (ndev->flags & IFF_UP) {
237 wil_err(wil, "wil_down : %d\n", rc);
242 /* Disable PCIe IRQ to prevent sporadic IRQs when PCIe is suspending */
243 wil_dbg_pm(wil, "Disabling PCIe IRQ before suspending\n");
244 wil_disable_irq(wil);
246 if (wil->platform_ops.suspend) {
247 rc = wil->platform_ops.suspend(wil->platform_handle, false);
254 set_bit(wil_status_suspended, wil->status);
257 wil_dbg_pm(wil, "suspend radio off: %d\n", rc);
262 static int wil_resume_radio_off(struct wil6210_priv *wil)
265 struct net_device *ndev = wil_to_ndev(wil);
267 wil_dbg_pm(wil, "Enabling PCIe IRQ\n");
269 /* if netif up, bring hardware up
270 * During open(), IFF_UP set after actual device method
271 * invocation. This prevent recursive call to wil_up()
272 * wil_status_suspended will be cleared in wil_reset
274 if (ndev->flags & IFF_UP)
277 clear_bit(wil_status_suspended, wil->status);
282 int wil_suspend(struct wil6210_priv *wil, bool is_runtime, bool keep_radio_on)
286 wil_dbg_pm(wil, "suspend: %s\n", is_runtime ? "runtime" : "system");
288 if (test_bit(wil_status_suspended, wil->status)) {
289 wil_dbg_pm(wil, "trying to suspend while suspended\n");
294 rc = wil_suspend_radio_off(wil);
296 rc = wil_suspend_keep_radio_on(wil);
298 wil_dbg_pm(wil, "suspend: %s => %d\n",
299 is_runtime ? "runtime" : "system", rc);
302 wil->suspend_stats.suspend_start_time = ktime_get();
307 int wil_resume(struct wil6210_priv *wil, bool is_runtime, bool keep_radio_on)
310 unsigned long long suspend_time_usec = 0;
312 wil_dbg_pm(wil, "resume: %s\n", is_runtime ? "runtime" : "system");
314 if (wil->platform_ops.resume) {
315 rc = wil->platform_ops.resume(wil->platform_handle,
318 wil_err(wil, "platform_ops.resume : %d\n", rc);
324 rc = wil_resume_keep_radio_on(wil);
326 rc = wil_resume_radio_off(wil);
332 ktime_to_us(ktime_sub(ktime_get(),
333 wil->suspend_stats.suspend_start_time));
334 wil->suspend_stats.total_suspend_time += suspend_time_usec;
335 if (suspend_time_usec < wil->suspend_stats.min_suspend_time)
336 wil->suspend_stats.min_suspend_time = suspend_time_usec;
337 if (suspend_time_usec > wil->suspend_stats.max_suspend_time)
338 wil->suspend_stats.max_suspend_time = suspend_time_usec;
341 wil_dbg_pm(wil, "resume: %s => %d, suspend time %lld usec\n",
342 is_runtime ? "runtime" : "system", rc, suspend_time_usec);