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 if (test_bit(wil_status_collecting_dumps, wil->status)) { 149 /* Device collects crash dump, cancel the suspend */ 150 wil_dbg_pm(wil, "reject suspend while collecting crash dump\n"); 151 clear_bit(wil_status_suspending, wil->status); 152 wil->suspend_stats.rejected_by_host++; 153 return -EBUSY; 154 } 155 wil_update_net_queues_bh(wil, NULL, true); 156 157 if (!wil_is_tx_idle(wil)) { 158 wil_dbg_pm(wil, "Pending TX data, reject suspend\n"); 159 wil->suspend_stats.rejected_by_host++; 160 goto reject_suspend; 161 } 162 163 if (!wil_is_rx_idle(wil)) { 164 wil_dbg_pm(wil, "Pending RX data, reject suspend\n"); 165 wil->suspend_stats.rejected_by_host++; 166 goto reject_suspend; 167 } 168 169 if (!wil_is_wmi_idle(wil)) { 170 wil_dbg_pm(wil, "Pending WMI events, reject suspend\n"); 171 wil->suspend_stats.rejected_by_host++; 172 goto reject_suspend; 173 } 174 175 /* Send WMI suspend request to the device */ 176 rc = wmi_suspend(wil); 177 if (rc) { 178 wil_dbg_pm(wil, "wmi_suspend failed, reject suspend (%d)\n", 179 rc); 180 goto reject_suspend; 181 } 182 183 /* Wait for completion of the pending RX packets */ 184 start = jiffies; 185 data_comp_to = jiffies + msecs_to_jiffies(WIL_DATA_COMPLETION_TO_MS); 186 if (test_bit(wil_status_napi_en, wil->status)) { 187 while (!wil_is_rx_idle(wil)) { 188 if (time_after(jiffies, data_comp_to)) { 189 if (wil_is_rx_idle(wil)) 190 break; 191 wil_err(wil, 192 "TO waiting for idle RX, suspend failed\n"); 193 wil->suspend_stats.r_on.failed_suspends++; 194 goto resume_after_fail; 195 } 196 wil_dbg_ratelimited(wil, "rx vring is not empty -> NAPI\n"); 197 napi_synchronize(&wil->napi_rx); 198 msleep(20); 199 } 200 } 201 202 /* In case of pending WMI events, reject the suspend 203 * and resume the device. 204 * This can happen if the device sent the WMI events before 205 * approving the suspend. 206 */ 207 if (!wil_is_wmi_idle(wil)) { 208 wil_err(wil, "suspend failed due to pending WMI events\n"); 209 wil->suspend_stats.r_on.failed_suspends++; 210 goto resume_after_fail; 211 } 212 213 wil_mask_irq(wil); 214 215 /* Disable device reset on PERST */ 216 wil_s(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD); 217 218 if (wil->platform_ops.suspend) { 219 rc = wil->platform_ops.suspend(wil->platform_handle, true); 220 if (rc) { 221 wil_err(wil, "platform device failed to suspend (%d)\n", 222 rc); 223 wil->suspend_stats.r_on.failed_suspends++; 224 wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD); 225 wil_unmask_irq(wil); 226 goto resume_after_fail; 227 } 228 } 229 230 /* Save the current bus request to return to the same in resume */ 231 wil->bus_request_kbps_pre_suspend = wil->bus_request_kbps; 232 wil6210_bus_request(wil, 0); 233 234 set_bit(wil_status_suspended, wil->status); 235 clear_bit(wil_status_suspending, wil->status); 236 237 return rc; 238 239 resume_after_fail: 240 set_bit(wil_status_resuming, wil->status); 241 clear_bit(wil_status_suspending, wil->status); 242 rc = wmi_resume(wil); 243 /* if resume succeeded, reject the suspend */ 244 if (!rc) { 245 rc = -EBUSY; 246 if (test_bit(wil_status_fwconnected, wil->status)) 247 wil_update_net_queues_bh(wil, NULL, false); 248 } 249 return rc; 250 251 reject_suspend: 252 clear_bit(wil_status_suspending, wil->status); 253 if (test_bit(wil_status_fwconnected, wil->status)) 254 wil_update_net_queues_bh(wil, NULL, false); 255 return -EBUSY; 256 } 257 258 static int wil_suspend_radio_off(struct wil6210_priv *wil) 259 { 260 int rc = 0; 261 struct net_device *ndev = wil_to_ndev(wil); 262 263 wil_dbg_pm(wil, "suspend radio off\n"); 264 265 set_bit(wil_status_suspending, wil->status); 266 if (test_bit(wil_status_collecting_dumps, wil->status)) { 267 /* Device collects crash dump, cancel the suspend */ 268 wil_dbg_pm(wil, "reject suspend while collecting crash dump\n"); 269 clear_bit(wil_status_suspending, wil->status); 270 wil->suspend_stats.rejected_by_host++; 271 return -EBUSY; 272 } 273 274 /* if netif up, hardware is alive, shut it down */ 275 if (ndev->flags & IFF_UP) { 276 rc = wil_down(wil); 277 if (rc) { 278 wil_err(wil, "wil_down : %d\n", rc); 279 wil->suspend_stats.r_off.failed_suspends++; 280 goto out; 281 } 282 } 283 284 /* Disable PCIe IRQ to prevent sporadic IRQs when PCIe is suspending */ 285 wil_dbg_pm(wil, "Disabling PCIe IRQ before suspending\n"); 286 wil_disable_irq(wil); 287 288 if (wil->platform_ops.suspend) { 289 rc = wil->platform_ops.suspend(wil->platform_handle, false); 290 if (rc) { 291 wil_enable_irq(wil); 292 wil->suspend_stats.r_off.failed_suspends++; 293 goto out; 294 } 295 } 296 297 set_bit(wil_status_suspended, wil->status); 298 299 out: 300 clear_bit(wil_status_suspending, wil->status); 301 wil_dbg_pm(wil, "suspend radio off: %d\n", rc); 302 303 return rc; 304 } 305 306 static int wil_resume_radio_off(struct wil6210_priv *wil) 307 { 308 int rc = 0; 309 struct net_device *ndev = wil_to_ndev(wil); 310 311 wil_dbg_pm(wil, "Enabling PCIe IRQ\n"); 312 wil_enable_irq(wil); 313 /* if netif up, bring hardware up 314 * During open(), IFF_UP set after actual device method 315 * invocation. This prevent recursive call to wil_up() 316 * wil_status_suspended will be cleared in wil_reset 317 */ 318 if (ndev->flags & IFF_UP) 319 rc = wil_up(wil); 320 else 321 clear_bit(wil_status_suspended, wil->status); 322 323 return rc; 324 } 325 326 int wil_suspend(struct wil6210_priv *wil, bool is_runtime, bool keep_radio_on) 327 { 328 int rc = 0; 329 330 wil_dbg_pm(wil, "suspend: %s\n", is_runtime ? "runtime" : "system"); 331 332 if (test_bit(wil_status_suspended, wil->status)) { 333 wil_dbg_pm(wil, "trying to suspend while suspended\n"); 334 return 0; 335 } 336 337 if (!keep_radio_on) 338 rc = wil_suspend_radio_off(wil); 339 else 340 rc = wil_suspend_keep_radio_on(wil); 341 342 wil_dbg_pm(wil, "suspend: %s => %d\n", 343 is_runtime ? "runtime" : "system", rc); 344 345 return rc; 346 } 347 348 int wil_resume(struct wil6210_priv *wil, bool is_runtime, bool keep_radio_on) 349 { 350 int rc = 0; 351 352 wil_dbg_pm(wil, "resume: %s\n", is_runtime ? "runtime" : "system"); 353 354 if (wil->platform_ops.resume) { 355 rc = wil->platform_ops.resume(wil->platform_handle, 356 keep_radio_on); 357 if (rc) { 358 wil_err(wil, "platform_ops.resume : %d\n", rc); 359 goto out; 360 } 361 } 362 363 if (keep_radio_on) 364 rc = wil_resume_keep_radio_on(wil); 365 else 366 rc = wil_resume_radio_off(wil); 367 368 out: 369 wil_dbg_pm(wil, "resume: %s => %d\n", is_runtime ? "runtime" : "system", 370 rc); 371 return rc; 372 } 373 374 void wil_pm_runtime_allow(struct wil6210_priv *wil) 375 { 376 struct device *dev = wil_to_dev(wil); 377 378 pm_runtime_put_noidle(dev); 379 pm_runtime_set_autosuspend_delay(dev, WIL6210_AUTOSUSPEND_DELAY_MS); 380 pm_runtime_use_autosuspend(dev); 381 pm_runtime_allow(dev); 382 } 383 384 void wil_pm_runtime_forbid(struct wil6210_priv *wil) 385 { 386 struct device *dev = wil_to_dev(wil); 387 388 pm_runtime_forbid(dev); 389 pm_runtime_get_noresume(dev); 390 } 391 392 int wil_pm_runtime_get(struct wil6210_priv *wil) 393 { 394 int rc; 395 struct device *dev = wil_to_dev(wil); 396 397 rc = pm_runtime_get_sync(dev); 398 if (rc < 0) { 399 wil_err(wil, "pm_runtime_get_sync() failed, rc = %d\n", rc); 400 pm_runtime_put_noidle(dev); 401 return rc; 402 } 403 404 return 0; 405 } 406 407 void wil_pm_runtime_put(struct wil6210_priv *wil) 408 { 409 struct device *dev = wil_to_dev(wil); 410 411 pm_runtime_mark_last_busy(dev); 412 pm_runtime_put_autosuspend(dev); 413 } 414