xref: /openbmc/linux/net/mac80211/pm.c (revision 5c7587f6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Portions
4  * Copyright (C) 2020-2021 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 {
23 	struct ieee80211_local *local = hw_to_local(hw);
24 	struct ieee80211_sub_if_data *sdata;
25 	struct sta_info *sta;
26 
27 	if (!local->open_count)
28 		goto suspend;
29 
30 	local->suspending = true;
31 	mb(); /* make suspending visible before any cancellation */
32 
33 	ieee80211_scan_cancel(local);
34 
35 	ieee80211_dfs_cac_cancel(local);
36 
37 	ieee80211_roc_purge(local, NULL);
38 
39 	ieee80211_del_virtual_monitor(local);
40 
41 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION) &&
42 	    !(wowlan && wowlan->any)) {
43 		mutex_lock(&local->sta_mtx);
44 		list_for_each_entry(sta, &local->sta_list, list) {
45 			set_sta_flag(sta, WLAN_STA_BLOCK_BA);
46 			ieee80211_sta_tear_down_BA_sessions(
47 					sta, AGG_STOP_LOCAL_REQUEST);
48 		}
49 		mutex_unlock(&local->sta_mtx);
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 	del_timer_sync(&local->sta_cleanup);
74 
75 	 /*
76 	 * Note that this particular timer doesn't need to be
77 	 * restarted at resume.
78 	 */
79 	cancel_work_sync(&local->dynamic_ps_enable_work);
80 	del_timer_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,
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 				mutex_lock(&local->sta_mtx);
123 				list_for_each_entry(sta,
124 						    &local->sta_list, list) {
125 					clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
126 				}
127 				mutex_unlock(&local->sta_mtx);
128 			}
129 			ieee80211_wake_queues_by_reason(hw,
130 					IEEE80211_MAX_QUEUE_MAP,
131 					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
132 					false);
133 			return err;
134 		} else if (err > 0) {
135 			WARN_ON(err != 1);
136 			/* cfg80211 will call back into mac80211 to disconnect
137 			 * all interfaces, allow that to proceed properly
138 			 */
139 			ieee80211_wake_queues_by_reason(hw,
140 					IEEE80211_MAX_QUEUE_MAP,
141 					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
142 					false);
143 			return err;
144 		} else {
145 			goto suspend;
146 		}
147 	}
148 
149 	/* remove all interfaces that were created in the driver */
150 	list_for_each_entry(sdata, &local->interfaces, list) {
151 		if (!ieee80211_sdata_running(sdata))
152 			continue;
153 		switch (sdata->vif.type) {
154 		case NL80211_IFTYPE_AP_VLAN:
155 		case NL80211_IFTYPE_MONITOR:
156 			continue;
157 		case NL80211_IFTYPE_STATION:
158 			ieee80211_mgd_quiesce(sdata);
159 			break;
160 		default:
161 			break;
162 		}
163 
164 		flush_delayed_work(&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.
171 	 */
172 	WARN_ON(!list_empty(&local->chanctx_list));
173 
174 	/* stop hardware - this must stop RX */
175 	ieee80211_stop_device(local);
176 
177  suspend:
178 	local->suspended = true;
179 	/* need suspended to be visible before quiescing is false */
180 	barrier();
181 	local->quiescing = false;
182 	local->suspending = false;
183 
184 	return 0;
185 }
186 
187 /*
188  * __ieee80211_resume() is a static inline which just calls
189  * ieee80211_reconfig(), which is also needed for hardware
190  * hang/firmware failure/etc. recovery.
191  */
192 
193 void ieee80211_report_wowlan_wakeup(struct ieee80211_vif *vif,
194 				    struct cfg80211_wowlan_wakeup *wakeup,
195 				    gfp_t gfp)
196 {
197 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
198 
199 	cfg80211_report_wowlan_wakeup(&sdata->wdev, wakeup, gfp);
200 }
201 EXPORT_SYMBOL(ieee80211_report_wowlan_wakeup);
202