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