xref: /openbmc/linux/net/mac80211/pm.c (revision 5f32c314)
1 #include <net/mac80211.h>
2 #include <net/rtnetlink.h>
3 
4 #include "ieee80211_i.h"
5 #include "mesh.h"
6 #include "driver-ops.h"
7 #include "led.h"
8 
9 int __ieee80211_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
10 {
11 	struct ieee80211_local *local = hw_to_local(hw);
12 	struct ieee80211_sub_if_data *sdata;
13 	struct sta_info *sta;
14 
15 	if (!local->open_count)
16 		goto suspend;
17 
18 	ieee80211_scan_cancel(local);
19 
20 	ieee80211_dfs_cac_cancel(local);
21 
22 	ieee80211_roc_purge(local, NULL);
23 
24 	ieee80211_del_virtual_monitor(local);
25 
26 	if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
27 		mutex_lock(&local->sta_mtx);
28 		list_for_each_entry(sta, &local->sta_list, list) {
29 			set_sta_flag(sta, WLAN_STA_BLOCK_BA);
30 			ieee80211_sta_tear_down_BA_sessions(
31 					sta, AGG_STOP_LOCAL_REQUEST);
32 		}
33 		mutex_unlock(&local->sta_mtx);
34 	}
35 
36 	ieee80211_stop_queues_by_reason(hw,
37 					IEEE80211_MAX_QUEUE_MAP,
38 					IEEE80211_QUEUE_STOP_REASON_SUSPEND);
39 
40 	/* flush out all packets */
41 	synchronize_net();
42 
43 	ieee80211_flush_queues(local, NULL);
44 
45 	local->quiescing = true;
46 	/* make quiescing visible to timers everywhere */
47 	mb();
48 
49 	flush_workqueue(local->workqueue);
50 
51 	/* Don't try to run timers while suspended. */
52 	del_timer_sync(&local->sta_cleanup);
53 
54 	 /*
55 	 * Note that this particular timer doesn't need to be
56 	 * restarted at resume.
57 	 */
58 	cancel_work_sync(&local->dynamic_ps_enable_work);
59 	del_timer_sync(&local->dynamic_ps_timer);
60 
61 	local->wowlan = wowlan && local->open_count;
62 	if (local->wowlan) {
63 		int err = drv_suspend(local, wowlan);
64 		if (err < 0) {
65 			local->quiescing = false;
66 			local->wowlan = false;
67 			if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
68 				mutex_lock(&local->sta_mtx);
69 				list_for_each_entry(sta,
70 						    &local->sta_list, list) {
71 					clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
72 				}
73 				mutex_unlock(&local->sta_mtx);
74 			}
75 			ieee80211_wake_queues_by_reason(hw,
76 					IEEE80211_MAX_QUEUE_MAP,
77 					IEEE80211_QUEUE_STOP_REASON_SUSPEND);
78 			return err;
79 		} else if (err > 0) {
80 			WARN_ON(err != 1);
81 			return err;
82 		} else {
83 			goto suspend;
84 		}
85 	}
86 
87 	/* tear down aggregation sessions and remove STAs */
88 	mutex_lock(&local->sta_mtx);
89 	list_for_each_entry(sta, &local->sta_list, list) {
90 		if (sta->uploaded) {
91 			enum ieee80211_sta_state state;
92 
93 			state = sta->sta_state;
94 			for (; state > IEEE80211_STA_NOTEXIST; state--)
95 				WARN_ON(drv_sta_state(local, sta->sdata, sta,
96 						      state, state - 1));
97 		}
98 	}
99 	mutex_unlock(&local->sta_mtx);
100 
101 	/* remove all interfaces that were created in the driver */
102 	list_for_each_entry(sdata, &local->interfaces, list) {
103 		if (!ieee80211_sdata_running(sdata) ||
104 		    sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
105 		    sdata->vif.type == NL80211_IFTYPE_MONITOR)
106 			continue;
107 
108 		drv_remove_interface(local, sdata);
109 	}
110 
111 	/*
112 	 * We disconnected on all interfaces before suspend, all channel
113 	 * contexts should be released.
114 	 */
115 	WARN_ON(!list_empty(&local->chanctx_list));
116 
117 	/* stop hardware - this must stop RX */
118 	if (local->open_count)
119 		ieee80211_stop_device(local);
120 
121  suspend:
122 	local->suspended = true;
123 	/* need suspended to be visible before quiescing is false */
124 	barrier();
125 	local->quiescing = false;
126 
127 	return 0;
128 }
129 
130 /*
131  * __ieee80211_resume() is a static inline which just calls
132  * ieee80211_reconfig(), which is also needed for hardware
133  * hang/firmware failure/etc. recovery.
134  */
135 
136 void ieee80211_report_wowlan_wakeup(struct ieee80211_vif *vif,
137 				    struct cfg80211_wowlan_wakeup *wakeup,
138 				    gfp_t gfp)
139 {
140 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
141 
142 	cfg80211_report_wowlan_wakeup(&sdata->wdev, wakeup, gfp);
143 }
144 EXPORT_SYMBOL(ieee80211_report_wowlan_wakeup);
145