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