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