xref: /linux/net/mac80211/pm.c (revision 9a44b828dd7c0be1d0d6d6cf178d9727ceb6da6e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Portions
4  * Copyright (C) 2020-2021, 2023-2024 Intel Corporation
5  */
6 #include <net/mac80211.h>
7 #include <net/rtnetlink.h>
8 
9 #include "ieee80211_i.h"
10 #include "mesh.h"
11 #include "driver-ops.h"
12 #include "led.h"
13 
14 static void ieee80211_sched_scan_cancel(struct ieee80211_local *local)
15 {
16 	if (ieee80211_request_sched_scan_stop(local))
17 		return;
18 	cfg80211_sched_scan_stopped_locked(local->hw.wiphy, 0);
19 }
20 
21 int __ieee80211_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan,
22 			bool reset)
23 {
24 	struct ieee80211_local *local = hw_to_local(hw);
25 	struct ieee80211_sub_if_data *sdata;
26 	struct sta_info *sta;
27 
28 	if (!local->open_count)
29 		goto suspend;
30 
31 	local->suspending = true;
32 	mb(); /* make suspending visible before any cancellation */
33 
34 	ieee80211_scan_cancel(local);
35 
36 	ieee80211_dfs_cac_cancel(local, NULL);
37 
38 	ieee80211_roc_purge(local, NULL);
39 
40 	ieee80211_del_virtual_monitor(local);
41 
42 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION) &&
43 	    !(wowlan && wowlan->any)) {
44 		lockdep_assert_wiphy(local->hw.wiphy);
45 		list_for_each_entry(sta, &local->sta_list, list) {
46 			set_sta_flag(sta, WLAN_STA_BLOCK_BA);
47 			ieee80211_sta_tear_down_BA_sessions(
48 					sta, AGG_STOP_LOCAL_REQUEST);
49 		}
50 	}
51 
52 	/* keep sched_scan only in case of 'any' trigger */
53 	if (!(wowlan && wowlan->any))
54 		ieee80211_sched_scan_cancel(local);
55 
56 	ieee80211_stop_queues_by_reason(hw,
57 					IEEE80211_MAX_QUEUE_MAP,
58 					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
59 					false);
60 
61 	/* flush out all packets */
62 	synchronize_net();
63 
64 	ieee80211_flush_queues(local, NULL, true);
65 
66 	local->quiescing = true;
67 	/* make quiescing visible to timers everywhere */
68 	mb();
69 
70 	flush_workqueue(local->workqueue);
71 
72 	/* Don't try to run timers while suspended. */
73 	timer_delete_sync(&local->sta_cleanup);
74 
75 	 /*
76 	 * Note that this particular timer doesn't need to be
77 	 * restarted at resume.
78 	 */
79 	wiphy_work_cancel(local->hw.wiphy, &local->dynamic_ps_enable_work);
80 	timer_delete_sync(&local->dynamic_ps_timer);
81 
82 	local->wowlan = wowlan;
83 	if (local->wowlan) {
84 		int err;
85 
86 		/* Drivers don't expect to suspend while some operations like
87 		 * authenticating or associating are in progress. It doesn't
88 		 * make sense anyway to accept that, since the authentication
89 		 * or association would never finish since the driver can't do
90 		 * that on its own.
91 		 * Thus, clean up in-progress auth/assoc first.
92 		 */
93 		list_for_each_entry(sdata, &local->interfaces, list) {
94 			if (!ieee80211_sdata_running(sdata))
95 				continue;
96 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
97 				continue;
98 			ieee80211_mgd_quiesce(sdata);
99 			/* If suspended during TX in progress, and wowlan
100 			 * is enabled (connection will be active) there
101 			 * can be a race where the driver is put out
102 			 * of power-save due to TX and during suspend
103 			 * dynamic_ps_timer is cancelled and TX packet
104 			 * is flushed, leaving the driver in ACTIVE even
105 			 * after resuming until dynamic_ps_timer puts
106 			 * driver back in DOZE.
107 			 */
108 			if (sdata->u.mgd.associated &&
109 			    sdata->u.mgd.powersave &&
110 			     !(local->hw.conf.flags & IEEE80211_CONF_PS)) {
111 				local->hw.conf.flags |= IEEE80211_CONF_PS;
112 				ieee80211_hw_config(local, -1,
113 						    IEEE80211_CONF_CHANGE_PS);
114 			}
115 		}
116 
117 		err = drv_suspend(local, wowlan);
118 		if (err < 0) {
119 			local->quiescing = false;
120 			local->wowlan = false;
121 			if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
122 				lockdep_assert_wiphy(local->hw.wiphy);
123 				list_for_each_entry(sta,
124 						    &local->sta_list, list) {
125 					clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
126 				}
127 			}
128 			ieee80211_wake_queues_by_reason(hw,
129 					IEEE80211_MAX_QUEUE_MAP,
130 					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
131 					false);
132 			return err;
133 		} else if (err > 0) {
134 			WARN_ON(err != 1);
135 			/* cfg80211 will call back into mac80211 to disconnect
136 			 * all interfaces, allow that to proceed properly
137 			 */
138 			ieee80211_wake_queues_by_reason(hw,
139 					IEEE80211_MAX_QUEUE_MAP,
140 					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
141 					false);
142 			return err;
143 		} else {
144 			goto suspend;
145 		}
146 	}
147 
148 	/* remove all interfaces that were created in the driver */
149 	list_for_each_entry(sdata, &local->interfaces, list) {
150 		if (!ieee80211_sdata_running(sdata))
151 			continue;
152 		switch (sdata->vif.type) {
153 		case NL80211_IFTYPE_AP_VLAN:
154 		case NL80211_IFTYPE_MONITOR:
155 			continue;
156 		case NL80211_IFTYPE_STATION:
157 			ieee80211_mgd_quiesce(sdata);
158 			break;
159 		default:
160 			break;
161 		}
162 
163 		wiphy_delayed_work_flush(local->hw.wiphy,
164 					 &sdata->dec_tailroom_needed_wk);
165 		drv_remove_interface(local, sdata);
166 	}
167 
168 	/*
169 	 * We disconnected on all interfaces before suspend, all channel
170 	 * contexts should be released, but on 'reset' debugfs that's
171 	 * not true so don't check there.
172 	 */
173 	WARN_ON(!reset && !list_empty(&local->chanctx_list));
174 
175 	/* stop hardware - this must stop RX */
176 	ieee80211_stop_device(local, true);
177 
178  suspend:
179 	local->suspended = true;
180 	/* need suspended to be visible before quiescing is false */
181 	barrier();
182 	local->quiescing = false;
183 	local->suspending = false;
184 
185 	return 0;
186 }
187 
188 /*
189  * __ieee80211_resume() is a static inline which just calls
190  * ieee80211_reconfig(), which is also needed for hardware
191  * hang/firmware failure/etc. recovery.
192  */
193 
194 void ieee80211_report_wowlan_wakeup(struct ieee80211_vif *vif,
195 				    struct cfg80211_wowlan_wakeup *wakeup,
196 				    gfp_t gfp)
197 {
198 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
199 
200 	cfg80211_report_wowlan_wakeup(&sdata->wdev, wakeup, gfp);
201 }
202 EXPORT_SYMBOL(ieee80211_report_wowlan_wakeup);
203