18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: ISC
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2014,2017 Qualcomm Atheros, Inc.
48c2ecf20Sopenharmony_ci * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include "wil6210.h"
88c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
98c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#define WIL6210_AUTOSUSPEND_DELAY_MS (1000)
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic void wil_pm_wake_connected_net_queues(struct wil6210_priv *wil)
148c2ecf20Sopenharmony_ci{
158c2ecf20Sopenharmony_ci	int i;
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci	mutex_lock(&wil->vif_mutex);
188c2ecf20Sopenharmony_ci	for (i = 0; i < GET_MAX_VIFS(wil); i++) {
198c2ecf20Sopenharmony_ci		struct wil6210_vif *vif = wil->vifs[i];
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci		if (vif && test_bit(wil_vif_fwconnected, vif->status))
228c2ecf20Sopenharmony_ci			wil_update_net_queues_bh(wil, vif, NULL, false);
238c2ecf20Sopenharmony_ci	}
248c2ecf20Sopenharmony_ci	mutex_unlock(&wil->vif_mutex);
258c2ecf20Sopenharmony_ci}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic void wil_pm_stop_all_net_queues(struct wil6210_priv *wil)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	int i;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	mutex_lock(&wil->vif_mutex);
328c2ecf20Sopenharmony_ci	for (i = 0; i < GET_MAX_VIFS(wil); i++) {
338c2ecf20Sopenharmony_ci		struct wil6210_vif *vif = wil->vifs[i];
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci		if (vif)
368c2ecf20Sopenharmony_ci			wil_update_net_queues_bh(wil, vif, NULL, true);
378c2ecf20Sopenharmony_ci	}
388c2ecf20Sopenharmony_ci	mutex_unlock(&wil->vif_mutex);
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic bool
428c2ecf20Sopenharmony_ciwil_can_suspend_vif(struct wil6210_priv *wil, struct wil6210_vif *vif,
438c2ecf20Sopenharmony_ci		    bool is_runtime)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct wireless_dev *wdev = vif_to_wdev(vif);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	switch (wdev->iftype) {
488c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_MONITOR:
498c2ecf20Sopenharmony_ci		wil_dbg_pm(wil, "Sniffer\n");
508c2ecf20Sopenharmony_ci		return false;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	/* for STA-like interface, don't runtime suspend */
538c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_STATION:
548c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_P2P_CLIENT:
558c2ecf20Sopenharmony_ci		if (test_bit(wil_vif_fwconnecting, vif->status)) {
568c2ecf20Sopenharmony_ci			wil_dbg_pm(wil, "Delay suspend when connecting\n");
578c2ecf20Sopenharmony_ci			return false;
588c2ecf20Sopenharmony_ci		}
598c2ecf20Sopenharmony_ci		if (is_runtime) {
608c2ecf20Sopenharmony_ci			wil_dbg_pm(wil, "STA-like interface\n");
618c2ecf20Sopenharmony_ci			return false;
628c2ecf20Sopenharmony_ci		}
638c2ecf20Sopenharmony_ci		break;
648c2ecf20Sopenharmony_ci	/* AP-like interface - can't suspend */
658c2ecf20Sopenharmony_ci	default:
668c2ecf20Sopenharmony_ci		wil_dbg_pm(wil, "AP-like interface\n");
678c2ecf20Sopenharmony_ci		return false;
688c2ecf20Sopenharmony_ci	}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	return true;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciint wil_can_suspend(struct wil6210_priv *wil, bool is_runtime)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	int rc = 0, i;
768c2ecf20Sopenharmony_ci	bool wmi_only = test_bit(WMI_FW_CAPABILITY_WMI_ONLY,
778c2ecf20Sopenharmony_ci				 wil->fw_capabilities);
788c2ecf20Sopenharmony_ci	bool active_ifaces;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	wil_dbg_pm(wil, "can_suspend: %s\n", is_runtime ? "runtime" : "system");
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	if (wmi_only || debug_fw) {
838c2ecf20Sopenharmony_ci		wil_dbg_pm(wil, "Deny any suspend - %s mode\n",
848c2ecf20Sopenharmony_ci			   wmi_only ? "wmi_only" : "debug_fw");
858c2ecf20Sopenharmony_ci		rc = -EBUSY;
868c2ecf20Sopenharmony_ci		goto out;
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci	if (is_runtime && !wil->platform_ops.suspend) {
898c2ecf20Sopenharmony_ci		rc = -EBUSY;
908c2ecf20Sopenharmony_ci		goto out;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	mutex_lock(&wil->vif_mutex);
948c2ecf20Sopenharmony_ci	active_ifaces = wil_has_active_ifaces(wil, true, false);
958c2ecf20Sopenharmony_ci	mutex_unlock(&wil->vif_mutex);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	if (!active_ifaces) {
988c2ecf20Sopenharmony_ci		/* can always sleep when down */
998c2ecf20Sopenharmony_ci		wil_dbg_pm(wil, "Interface is down\n");
1008c2ecf20Sopenharmony_ci		goto out;
1018c2ecf20Sopenharmony_ci	}
1028c2ecf20Sopenharmony_ci	if (test_bit(wil_status_resetting, wil->status)) {
1038c2ecf20Sopenharmony_ci		wil_dbg_pm(wil, "Delay suspend when resetting\n");
1048c2ecf20Sopenharmony_ci		rc = -EBUSY;
1058c2ecf20Sopenharmony_ci		goto out;
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci	if (wil->recovery_state != fw_recovery_idle) {
1088c2ecf20Sopenharmony_ci		wil_dbg_pm(wil, "Delay suspend during recovery\n");
1098c2ecf20Sopenharmony_ci		rc = -EBUSY;
1108c2ecf20Sopenharmony_ci		goto out;
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/* interface is running */
1148c2ecf20Sopenharmony_ci	mutex_lock(&wil->vif_mutex);
1158c2ecf20Sopenharmony_ci	for (i = 0; i < GET_MAX_VIFS(wil); i++) {
1168c2ecf20Sopenharmony_ci		struct wil6210_vif *vif = wil->vifs[i];
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci		if (!vif)
1198c2ecf20Sopenharmony_ci			continue;
1208c2ecf20Sopenharmony_ci		if (!wil_can_suspend_vif(wil, vif, is_runtime)) {
1218c2ecf20Sopenharmony_ci			rc = -EBUSY;
1228c2ecf20Sopenharmony_ci			mutex_unlock(&wil->vif_mutex);
1238c2ecf20Sopenharmony_ci			goto out;
1248c2ecf20Sopenharmony_ci		}
1258c2ecf20Sopenharmony_ci	}
1268c2ecf20Sopenharmony_ci	mutex_unlock(&wil->vif_mutex);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ciout:
1298c2ecf20Sopenharmony_ci	wil_dbg_pm(wil, "can_suspend: %s => %s (%d)\n",
1308c2ecf20Sopenharmony_ci		   is_runtime ? "runtime" : "system", rc ? "No" : "Yes", rc);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	if (rc)
1338c2ecf20Sopenharmony_ci		wil->suspend_stats.rejected_by_host++;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	return rc;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic int wil_resume_keep_radio_on(struct wil6210_priv *wil)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	int rc = 0;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	/* wil_status_resuming will be cleared when getting
1438c2ecf20Sopenharmony_ci	 * WMI_TRAFFIC_RESUME_EVENTID
1448c2ecf20Sopenharmony_ci	 */
1458c2ecf20Sopenharmony_ci	set_bit(wil_status_resuming, wil->status);
1468c2ecf20Sopenharmony_ci	clear_bit(wil_status_suspended, wil->status);
1478c2ecf20Sopenharmony_ci	wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
1488c2ecf20Sopenharmony_ci	wil_unmask_irq(wil);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	wil6210_bus_request(wil, wil->bus_request_kbps_pre_suspend);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	/* Send WMI resume request to the device */
1538c2ecf20Sopenharmony_ci	rc = wmi_resume(wil);
1548c2ecf20Sopenharmony_ci	if (rc) {
1558c2ecf20Sopenharmony_ci		wil_err(wil, "device failed to resume (%d)\n", rc);
1568c2ecf20Sopenharmony_ci		if (no_fw_recovery)
1578c2ecf20Sopenharmony_ci			goto out;
1588c2ecf20Sopenharmony_ci		rc = wil_down(wil);
1598c2ecf20Sopenharmony_ci		if (rc) {
1608c2ecf20Sopenharmony_ci			wil_err(wil, "wil_down failed (%d)\n", rc);
1618c2ecf20Sopenharmony_ci			goto out;
1628c2ecf20Sopenharmony_ci		}
1638c2ecf20Sopenharmony_ci		rc = wil_up(wil);
1648c2ecf20Sopenharmony_ci		if (rc) {
1658c2ecf20Sopenharmony_ci			wil_err(wil, "wil_up failed (%d)\n", rc);
1668c2ecf20Sopenharmony_ci			goto out;
1678c2ecf20Sopenharmony_ci		}
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	/* Wake all queues */
1718c2ecf20Sopenharmony_ci	wil_pm_wake_connected_net_queues(wil);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ciout:
1748c2ecf20Sopenharmony_ci	if (rc)
1758c2ecf20Sopenharmony_ci		set_bit(wil_status_suspended, wil->status);
1768c2ecf20Sopenharmony_ci	return rc;
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic int wil_suspend_keep_radio_on(struct wil6210_priv *wil)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	int rc = 0;
1828c2ecf20Sopenharmony_ci	unsigned long data_comp_to;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	wil_dbg_pm(wil, "suspend keep radio on\n");
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	/* Prevent handling of new tx and wmi commands */
1878c2ecf20Sopenharmony_ci	rc = down_write_trylock(&wil->mem_lock);
1888c2ecf20Sopenharmony_ci	if (!rc) {
1898c2ecf20Sopenharmony_ci		wil_err(wil,
1908c2ecf20Sopenharmony_ci			"device is busy. down_write_trylock failed, returned (0x%x)\n",
1918c2ecf20Sopenharmony_ci			rc);
1928c2ecf20Sopenharmony_ci		wil->suspend_stats.rejected_by_host++;
1938c2ecf20Sopenharmony_ci		return -EBUSY;
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	set_bit(wil_status_suspending, wil->status);
1978c2ecf20Sopenharmony_ci	up_write(&wil->mem_lock);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	wil_pm_stop_all_net_queues(wil);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	if (!wil_is_tx_idle(wil)) {
2028c2ecf20Sopenharmony_ci		wil_dbg_pm(wil, "Pending TX data, reject suspend\n");
2038c2ecf20Sopenharmony_ci		wil->suspend_stats.rejected_by_host++;
2048c2ecf20Sopenharmony_ci		goto reject_suspend;
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	if (!wil->txrx_ops.is_rx_idle(wil)) {
2088c2ecf20Sopenharmony_ci		wil_dbg_pm(wil, "Pending RX data, reject suspend\n");
2098c2ecf20Sopenharmony_ci		wil->suspend_stats.rejected_by_host++;
2108c2ecf20Sopenharmony_ci		goto reject_suspend;
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	if (!wil_is_wmi_idle(wil)) {
2148c2ecf20Sopenharmony_ci		wil_dbg_pm(wil, "Pending WMI events, reject suspend\n");
2158c2ecf20Sopenharmony_ci		wil->suspend_stats.rejected_by_host++;
2168c2ecf20Sopenharmony_ci		goto reject_suspend;
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	/* Send WMI suspend request to the device */
2208c2ecf20Sopenharmony_ci	rc = wmi_suspend(wil);
2218c2ecf20Sopenharmony_ci	if (rc) {
2228c2ecf20Sopenharmony_ci		wil_dbg_pm(wil, "wmi_suspend failed, reject suspend (%d)\n",
2238c2ecf20Sopenharmony_ci			   rc);
2248c2ecf20Sopenharmony_ci		goto reject_suspend;
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	/* Wait for completion of the pending RX packets */
2288c2ecf20Sopenharmony_ci	data_comp_to = jiffies + msecs_to_jiffies(WIL_DATA_COMPLETION_TO_MS);
2298c2ecf20Sopenharmony_ci	if (test_bit(wil_status_napi_en, wil->status)) {
2308c2ecf20Sopenharmony_ci		while (!wil->txrx_ops.is_rx_idle(wil)) {
2318c2ecf20Sopenharmony_ci			if (time_after(jiffies, data_comp_to)) {
2328c2ecf20Sopenharmony_ci				if (wil->txrx_ops.is_rx_idle(wil))
2338c2ecf20Sopenharmony_ci					break;
2348c2ecf20Sopenharmony_ci				wil_err(wil,
2358c2ecf20Sopenharmony_ci					"TO waiting for idle RX, suspend failed\n");
2368c2ecf20Sopenharmony_ci				wil->suspend_stats.r_on.failed_suspends++;
2378c2ecf20Sopenharmony_ci				goto resume_after_fail;
2388c2ecf20Sopenharmony_ci			}
2398c2ecf20Sopenharmony_ci			wil_dbg_ratelimited(wil, "rx vring is not empty -> NAPI\n");
2408c2ecf20Sopenharmony_ci			napi_synchronize(&wil->napi_rx);
2418c2ecf20Sopenharmony_ci			msleep(20);
2428c2ecf20Sopenharmony_ci		}
2438c2ecf20Sopenharmony_ci	}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	/* In case of pending WMI events, reject the suspend
2468c2ecf20Sopenharmony_ci	 * and resume the device.
2478c2ecf20Sopenharmony_ci	 * This can happen if the device sent the WMI events before
2488c2ecf20Sopenharmony_ci	 * approving the suspend.
2498c2ecf20Sopenharmony_ci	 */
2508c2ecf20Sopenharmony_ci	if (!wil_is_wmi_idle(wil)) {
2518c2ecf20Sopenharmony_ci		wil_err(wil, "suspend failed due to pending WMI events\n");
2528c2ecf20Sopenharmony_ci		wil->suspend_stats.r_on.failed_suspends++;
2538c2ecf20Sopenharmony_ci		goto resume_after_fail;
2548c2ecf20Sopenharmony_ci	}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	wil_mask_irq(wil);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	/* Disable device reset on PERST */
2598c2ecf20Sopenharmony_ci	wil_s(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	if (wil->platform_ops.suspend) {
2628c2ecf20Sopenharmony_ci		rc = wil->platform_ops.suspend(wil->platform_handle, true);
2638c2ecf20Sopenharmony_ci		if (rc) {
2648c2ecf20Sopenharmony_ci			wil_err(wil, "platform device failed to suspend (%d)\n",
2658c2ecf20Sopenharmony_ci				rc);
2668c2ecf20Sopenharmony_ci			wil->suspend_stats.r_on.failed_suspends++;
2678c2ecf20Sopenharmony_ci			wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
2688c2ecf20Sopenharmony_ci			wil_unmask_irq(wil);
2698c2ecf20Sopenharmony_ci			goto resume_after_fail;
2708c2ecf20Sopenharmony_ci		}
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	/* Save the current bus request to return to the same in resume */
2748c2ecf20Sopenharmony_ci	wil->bus_request_kbps_pre_suspend = wil->bus_request_kbps;
2758c2ecf20Sopenharmony_ci	wil6210_bus_request(wil, 0);
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	set_bit(wil_status_suspended, wil->status);
2788c2ecf20Sopenharmony_ci	clear_bit(wil_status_suspending, wil->status);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	return rc;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ciresume_after_fail:
2838c2ecf20Sopenharmony_ci	set_bit(wil_status_resuming, wil->status);
2848c2ecf20Sopenharmony_ci	clear_bit(wil_status_suspending, wil->status);
2858c2ecf20Sopenharmony_ci	rc = wmi_resume(wil);
2868c2ecf20Sopenharmony_ci	/* if resume succeeded, reject the suspend */
2878c2ecf20Sopenharmony_ci	if (!rc) {
2888c2ecf20Sopenharmony_ci		rc = -EBUSY;
2898c2ecf20Sopenharmony_ci		wil_pm_wake_connected_net_queues(wil);
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci	return rc;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cireject_suspend:
2948c2ecf20Sopenharmony_ci	clear_bit(wil_status_suspending, wil->status);
2958c2ecf20Sopenharmony_ci	wil_pm_wake_connected_net_queues(wil);
2968c2ecf20Sopenharmony_ci	return -EBUSY;
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic int wil_suspend_radio_off(struct wil6210_priv *wil)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	int rc = 0;
3028c2ecf20Sopenharmony_ci	bool active_ifaces;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	wil_dbg_pm(wil, "suspend radio off\n");
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	rc = down_write_trylock(&wil->mem_lock);
3078c2ecf20Sopenharmony_ci	if (!rc) {
3088c2ecf20Sopenharmony_ci		wil_err(wil,
3098c2ecf20Sopenharmony_ci			"device is busy. down_write_trylock failed, returned (0x%x)\n",
3108c2ecf20Sopenharmony_ci			rc);
3118c2ecf20Sopenharmony_ci		wil->suspend_stats.rejected_by_host++;
3128c2ecf20Sopenharmony_ci		return -EBUSY;
3138c2ecf20Sopenharmony_ci	}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	set_bit(wil_status_suspending, wil->status);
3168c2ecf20Sopenharmony_ci	up_write(&wil->mem_lock);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	/* if netif up, hardware is alive, shut it down */
3198c2ecf20Sopenharmony_ci	mutex_lock(&wil->vif_mutex);
3208c2ecf20Sopenharmony_ci	active_ifaces = wil_has_active_ifaces(wil, true, false);
3218c2ecf20Sopenharmony_ci	mutex_unlock(&wil->vif_mutex);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	if (active_ifaces) {
3248c2ecf20Sopenharmony_ci		rc = wil_down(wil);
3258c2ecf20Sopenharmony_ci		if (rc) {
3268c2ecf20Sopenharmony_ci			wil_err(wil, "wil_down : %d\n", rc);
3278c2ecf20Sopenharmony_ci			wil->suspend_stats.r_off.failed_suspends++;
3288c2ecf20Sopenharmony_ci			goto out;
3298c2ecf20Sopenharmony_ci		}
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	/* Disable PCIe IRQ to prevent sporadic IRQs when PCIe is suspending */
3338c2ecf20Sopenharmony_ci	wil_dbg_pm(wil, "Disabling PCIe IRQ before suspending\n");
3348c2ecf20Sopenharmony_ci	wil_disable_irq(wil);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	if (wil->platform_ops.suspend) {
3378c2ecf20Sopenharmony_ci		rc = wil->platform_ops.suspend(wil->platform_handle, false);
3388c2ecf20Sopenharmony_ci		if (rc) {
3398c2ecf20Sopenharmony_ci			wil_enable_irq(wil);
3408c2ecf20Sopenharmony_ci			wil->suspend_stats.r_off.failed_suspends++;
3418c2ecf20Sopenharmony_ci			goto out;
3428c2ecf20Sopenharmony_ci		}
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	set_bit(wil_status_suspended, wil->status);
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ciout:
3488c2ecf20Sopenharmony_ci	clear_bit(wil_status_suspending, wil->status);
3498c2ecf20Sopenharmony_ci	wil_dbg_pm(wil, "suspend radio off: %d\n", rc);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	return rc;
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic int wil_resume_radio_off(struct wil6210_priv *wil)
3558c2ecf20Sopenharmony_ci{
3568c2ecf20Sopenharmony_ci	int rc = 0;
3578c2ecf20Sopenharmony_ci	bool active_ifaces;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	wil_dbg_pm(wil, "Enabling PCIe IRQ\n");
3608c2ecf20Sopenharmony_ci	wil_enable_irq(wil);
3618c2ecf20Sopenharmony_ci	/* if any netif up, bring hardware up
3628c2ecf20Sopenharmony_ci	 * During open(), IFF_UP set after actual device method
3638c2ecf20Sopenharmony_ci	 * invocation. This prevent recursive call to wil_up()
3648c2ecf20Sopenharmony_ci	 * wil_status_suspended will be cleared in wil_reset
3658c2ecf20Sopenharmony_ci	 */
3668c2ecf20Sopenharmony_ci	mutex_lock(&wil->vif_mutex);
3678c2ecf20Sopenharmony_ci	active_ifaces = wil_has_active_ifaces(wil, true, false);
3688c2ecf20Sopenharmony_ci	mutex_unlock(&wil->vif_mutex);
3698c2ecf20Sopenharmony_ci	if (active_ifaces)
3708c2ecf20Sopenharmony_ci		rc = wil_up(wil);
3718c2ecf20Sopenharmony_ci	else
3728c2ecf20Sopenharmony_ci		clear_bit(wil_status_suspended, wil->status);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	return rc;
3758c2ecf20Sopenharmony_ci}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ciint wil_suspend(struct wil6210_priv *wil, bool is_runtime, bool keep_radio_on)
3788c2ecf20Sopenharmony_ci{
3798c2ecf20Sopenharmony_ci	int rc = 0;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	wil_dbg_pm(wil, "suspend: %s\n", is_runtime ? "runtime" : "system");
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	if (test_bit(wil_status_suspended, wil->status)) {
3848c2ecf20Sopenharmony_ci		wil_dbg_pm(wil, "trying to suspend while suspended\n");
3858c2ecf20Sopenharmony_ci		return 0;
3868c2ecf20Sopenharmony_ci	}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	if (!keep_radio_on)
3898c2ecf20Sopenharmony_ci		rc = wil_suspend_radio_off(wil);
3908c2ecf20Sopenharmony_ci	else
3918c2ecf20Sopenharmony_ci		rc = wil_suspend_keep_radio_on(wil);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	wil_dbg_pm(wil, "suspend: %s => %d\n",
3948c2ecf20Sopenharmony_ci		   is_runtime ? "runtime" : "system", rc);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	return rc;
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ciint wil_resume(struct wil6210_priv *wil, bool is_runtime, bool keep_radio_on)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	int rc = 0;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	wil_dbg_pm(wil, "resume: %s\n", is_runtime ? "runtime" : "system");
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	if (wil->platform_ops.resume) {
4068c2ecf20Sopenharmony_ci		rc = wil->platform_ops.resume(wil->platform_handle,
4078c2ecf20Sopenharmony_ci					      keep_radio_on);
4088c2ecf20Sopenharmony_ci		if (rc) {
4098c2ecf20Sopenharmony_ci			wil_err(wil, "platform_ops.resume : %d\n", rc);
4108c2ecf20Sopenharmony_ci			goto out;
4118c2ecf20Sopenharmony_ci		}
4128c2ecf20Sopenharmony_ci	}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	if (keep_radio_on)
4158c2ecf20Sopenharmony_ci		rc = wil_resume_keep_radio_on(wil);
4168c2ecf20Sopenharmony_ci	else
4178c2ecf20Sopenharmony_ci		rc = wil_resume_radio_off(wil);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ciout:
4208c2ecf20Sopenharmony_ci	wil_dbg_pm(wil, "resume: %s => %d\n", is_runtime ? "runtime" : "system",
4218c2ecf20Sopenharmony_ci		   rc);
4228c2ecf20Sopenharmony_ci	return rc;
4238c2ecf20Sopenharmony_ci}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_civoid wil_pm_runtime_allow(struct wil6210_priv *wil)
4268c2ecf20Sopenharmony_ci{
4278c2ecf20Sopenharmony_ci	struct device *dev = wil_to_dev(wil);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	pm_runtime_put_noidle(dev);
4308c2ecf20Sopenharmony_ci	pm_runtime_set_autosuspend_delay(dev, WIL6210_AUTOSUSPEND_DELAY_MS);
4318c2ecf20Sopenharmony_ci	pm_runtime_use_autosuspend(dev);
4328c2ecf20Sopenharmony_ci	pm_runtime_allow(dev);
4338c2ecf20Sopenharmony_ci}
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_civoid wil_pm_runtime_forbid(struct wil6210_priv *wil)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	struct device *dev = wil_to_dev(wil);
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	pm_runtime_forbid(dev);
4408c2ecf20Sopenharmony_ci	pm_runtime_get_noresume(dev);
4418c2ecf20Sopenharmony_ci}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ciint wil_pm_runtime_get(struct wil6210_priv *wil)
4448c2ecf20Sopenharmony_ci{
4458c2ecf20Sopenharmony_ci	int rc;
4468c2ecf20Sopenharmony_ci	struct device *dev = wil_to_dev(wil);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	rc = pm_runtime_get_sync(dev);
4498c2ecf20Sopenharmony_ci	if (rc < 0) {
4508c2ecf20Sopenharmony_ci		wil_err(wil, "pm_runtime_get_sync() failed, rc = %d\n", rc);
4518c2ecf20Sopenharmony_ci		pm_runtime_put_noidle(dev);
4528c2ecf20Sopenharmony_ci		return rc;
4538c2ecf20Sopenharmony_ci	}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	return 0;
4568c2ecf20Sopenharmony_ci}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_civoid wil_pm_runtime_put(struct wil6210_priv *wil)
4598c2ecf20Sopenharmony_ci{
4608c2ecf20Sopenharmony_ci	struct device *dev = wil_to_dev(wil);
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	pm_runtime_mark_last_busy(dev);
4638c2ecf20Sopenharmony_ci	pm_runtime_put_autosuspend(dev);
4648c2ecf20Sopenharmony_ci}
465