162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Portions
462306a36Sopenharmony_ci * Copyright (C) 2020-2021 Intel Corporation
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci#include <net/mac80211.h>
762306a36Sopenharmony_ci#include <net/rtnetlink.h>
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include "ieee80211_i.h"
1062306a36Sopenharmony_ci#include "mesh.h"
1162306a36Sopenharmony_ci#include "driver-ops.h"
1262306a36Sopenharmony_ci#include "led.h"
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_cistatic void ieee80211_sched_scan_cancel(struct ieee80211_local *local)
1562306a36Sopenharmony_ci{
1662306a36Sopenharmony_ci	if (ieee80211_request_sched_scan_stop(local))
1762306a36Sopenharmony_ci		return;
1862306a36Sopenharmony_ci	cfg80211_sched_scan_stopped_locked(local->hw.wiphy, 0);
1962306a36Sopenharmony_ci}
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ciint __ieee80211_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
2262306a36Sopenharmony_ci{
2362306a36Sopenharmony_ci	struct ieee80211_local *local = hw_to_local(hw);
2462306a36Sopenharmony_ci	struct ieee80211_sub_if_data *sdata;
2562306a36Sopenharmony_ci	struct sta_info *sta;
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci	if (!local->open_count)
2862306a36Sopenharmony_ci		goto suspend;
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci	local->suspending = true;
3162306a36Sopenharmony_ci	mb(); /* make suspending visible before any cancellation */
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci	ieee80211_scan_cancel(local);
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	ieee80211_dfs_cac_cancel(local);
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	ieee80211_roc_purge(local, NULL);
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	ieee80211_del_virtual_monitor(local);
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION) &&
4262306a36Sopenharmony_ci	    !(wowlan && wowlan->any)) {
4362306a36Sopenharmony_ci		mutex_lock(&local->sta_mtx);
4462306a36Sopenharmony_ci		list_for_each_entry(sta, &local->sta_list, list) {
4562306a36Sopenharmony_ci			set_sta_flag(sta, WLAN_STA_BLOCK_BA);
4662306a36Sopenharmony_ci			ieee80211_sta_tear_down_BA_sessions(
4762306a36Sopenharmony_ci					sta, AGG_STOP_LOCAL_REQUEST);
4862306a36Sopenharmony_ci		}
4962306a36Sopenharmony_ci		mutex_unlock(&local->sta_mtx);
5062306a36Sopenharmony_ci	}
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	/* keep sched_scan only in case of 'any' trigger */
5362306a36Sopenharmony_ci	if (!(wowlan && wowlan->any))
5462306a36Sopenharmony_ci		ieee80211_sched_scan_cancel(local);
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	ieee80211_stop_queues_by_reason(hw,
5762306a36Sopenharmony_ci					IEEE80211_MAX_QUEUE_MAP,
5862306a36Sopenharmony_ci					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
5962306a36Sopenharmony_ci					false);
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	/* flush out all packets */
6262306a36Sopenharmony_ci	synchronize_net();
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	ieee80211_flush_queues(local, NULL, true);
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	local->quiescing = true;
6762306a36Sopenharmony_ci	/* make quiescing visible to timers everywhere */
6862306a36Sopenharmony_ci	mb();
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	flush_workqueue(local->workqueue);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	/* Don't try to run timers while suspended. */
7362306a36Sopenharmony_ci	del_timer_sync(&local->sta_cleanup);
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	 /*
7662306a36Sopenharmony_ci	 * Note that this particular timer doesn't need to be
7762306a36Sopenharmony_ci	 * restarted at resume.
7862306a36Sopenharmony_ci	 */
7962306a36Sopenharmony_ci	cancel_work_sync(&local->dynamic_ps_enable_work);
8062306a36Sopenharmony_ci	del_timer_sync(&local->dynamic_ps_timer);
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	local->wowlan = wowlan;
8362306a36Sopenharmony_ci	if (local->wowlan) {
8462306a36Sopenharmony_ci		int err;
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci		/* Drivers don't expect to suspend while some operations like
8762306a36Sopenharmony_ci		 * authenticating or associating are in progress. It doesn't
8862306a36Sopenharmony_ci		 * make sense anyway to accept that, since the authentication
8962306a36Sopenharmony_ci		 * or association would never finish since the driver can't do
9062306a36Sopenharmony_ci		 * that on its own.
9162306a36Sopenharmony_ci		 * Thus, clean up in-progress auth/assoc first.
9262306a36Sopenharmony_ci		 */
9362306a36Sopenharmony_ci		list_for_each_entry(sdata, &local->interfaces, list) {
9462306a36Sopenharmony_ci			if (!ieee80211_sdata_running(sdata))
9562306a36Sopenharmony_ci				continue;
9662306a36Sopenharmony_ci			if (sdata->vif.type != NL80211_IFTYPE_STATION)
9762306a36Sopenharmony_ci				continue;
9862306a36Sopenharmony_ci			ieee80211_mgd_quiesce(sdata);
9962306a36Sopenharmony_ci			/* If suspended during TX in progress, and wowlan
10062306a36Sopenharmony_ci			 * is enabled (connection will be active) there
10162306a36Sopenharmony_ci			 * can be a race where the driver is put out
10262306a36Sopenharmony_ci			 * of power-save due to TX and during suspend
10362306a36Sopenharmony_ci			 * dynamic_ps_timer is cancelled and TX packet
10462306a36Sopenharmony_ci			 * is flushed, leaving the driver in ACTIVE even
10562306a36Sopenharmony_ci			 * after resuming until dynamic_ps_timer puts
10662306a36Sopenharmony_ci			 * driver back in DOZE.
10762306a36Sopenharmony_ci			 */
10862306a36Sopenharmony_ci			if (sdata->u.mgd.associated &&
10962306a36Sopenharmony_ci			    sdata->u.mgd.powersave &&
11062306a36Sopenharmony_ci			     !(local->hw.conf.flags & IEEE80211_CONF_PS)) {
11162306a36Sopenharmony_ci				local->hw.conf.flags |= IEEE80211_CONF_PS;
11262306a36Sopenharmony_ci				ieee80211_hw_config(local,
11362306a36Sopenharmony_ci						    IEEE80211_CONF_CHANGE_PS);
11462306a36Sopenharmony_ci			}
11562306a36Sopenharmony_ci		}
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci		err = drv_suspend(local, wowlan);
11862306a36Sopenharmony_ci		if (err < 0) {
11962306a36Sopenharmony_ci			local->quiescing = false;
12062306a36Sopenharmony_ci			local->wowlan = false;
12162306a36Sopenharmony_ci			if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
12262306a36Sopenharmony_ci				mutex_lock(&local->sta_mtx);
12362306a36Sopenharmony_ci				list_for_each_entry(sta,
12462306a36Sopenharmony_ci						    &local->sta_list, list) {
12562306a36Sopenharmony_ci					clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
12662306a36Sopenharmony_ci				}
12762306a36Sopenharmony_ci				mutex_unlock(&local->sta_mtx);
12862306a36Sopenharmony_ci			}
12962306a36Sopenharmony_ci			ieee80211_wake_queues_by_reason(hw,
13062306a36Sopenharmony_ci					IEEE80211_MAX_QUEUE_MAP,
13162306a36Sopenharmony_ci					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
13262306a36Sopenharmony_ci					false);
13362306a36Sopenharmony_ci			return err;
13462306a36Sopenharmony_ci		} else if (err > 0) {
13562306a36Sopenharmony_ci			WARN_ON(err != 1);
13662306a36Sopenharmony_ci			/* cfg80211 will call back into mac80211 to disconnect
13762306a36Sopenharmony_ci			 * all interfaces, allow that to proceed properly
13862306a36Sopenharmony_ci			 */
13962306a36Sopenharmony_ci			ieee80211_wake_queues_by_reason(hw,
14062306a36Sopenharmony_ci					IEEE80211_MAX_QUEUE_MAP,
14162306a36Sopenharmony_ci					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
14262306a36Sopenharmony_ci					false);
14362306a36Sopenharmony_ci			return err;
14462306a36Sopenharmony_ci		} else {
14562306a36Sopenharmony_ci			goto suspend;
14662306a36Sopenharmony_ci		}
14762306a36Sopenharmony_ci	}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	/* remove all interfaces that were created in the driver */
15062306a36Sopenharmony_ci	list_for_each_entry(sdata, &local->interfaces, list) {
15162306a36Sopenharmony_ci		if (!ieee80211_sdata_running(sdata))
15262306a36Sopenharmony_ci			continue;
15362306a36Sopenharmony_ci		switch (sdata->vif.type) {
15462306a36Sopenharmony_ci		case NL80211_IFTYPE_AP_VLAN:
15562306a36Sopenharmony_ci		case NL80211_IFTYPE_MONITOR:
15662306a36Sopenharmony_ci			continue;
15762306a36Sopenharmony_ci		case NL80211_IFTYPE_STATION:
15862306a36Sopenharmony_ci			ieee80211_mgd_quiesce(sdata);
15962306a36Sopenharmony_ci			break;
16062306a36Sopenharmony_ci		default:
16162306a36Sopenharmony_ci			break;
16262306a36Sopenharmony_ci		}
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci		flush_delayed_work(&sdata->dec_tailroom_needed_wk);
16562306a36Sopenharmony_ci		drv_remove_interface(local, sdata);
16662306a36Sopenharmony_ci	}
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	/*
16962306a36Sopenharmony_ci	 * We disconnected on all interfaces before suspend, all channel
17062306a36Sopenharmony_ci	 * contexts should be released.
17162306a36Sopenharmony_ci	 */
17262306a36Sopenharmony_ci	WARN_ON(!list_empty(&local->chanctx_list));
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	/* stop hardware - this must stop RX */
17562306a36Sopenharmony_ci	ieee80211_stop_device(local);
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci suspend:
17862306a36Sopenharmony_ci	local->suspended = true;
17962306a36Sopenharmony_ci	/* need suspended to be visible before quiescing is false */
18062306a36Sopenharmony_ci	barrier();
18162306a36Sopenharmony_ci	local->quiescing = false;
18262306a36Sopenharmony_ci	local->suspending = false;
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	return 0;
18562306a36Sopenharmony_ci}
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci/*
18862306a36Sopenharmony_ci * __ieee80211_resume() is a static inline which just calls
18962306a36Sopenharmony_ci * ieee80211_reconfig(), which is also needed for hardware
19062306a36Sopenharmony_ci * hang/firmware failure/etc. recovery.
19162306a36Sopenharmony_ci */
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_civoid ieee80211_report_wowlan_wakeup(struct ieee80211_vif *vif,
19462306a36Sopenharmony_ci				    struct cfg80211_wowlan_wakeup *wakeup,
19562306a36Sopenharmony_ci				    gfp_t gfp)
19662306a36Sopenharmony_ci{
19762306a36Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	cfg80211_report_wowlan_wakeup(&sdata->wdev, wakeup, gfp);
20062306a36Sopenharmony_ci}
20162306a36Sopenharmony_ciEXPORT_SYMBOL(ieee80211_report_wowlan_wakeup);
202