xref: /openbmc/linux/net/mac80211/util.c (revision 2fb51c35)
1c2d1560aSJohannes Berg /*
2c2d1560aSJohannes Berg  * Copyright 2002-2005, Instant802 Networks, Inc.
3c2d1560aSJohannes Berg  * Copyright 2005-2006, Devicescape Software, Inc.
4c2d1560aSJohannes Berg  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
5c2d1560aSJohannes Berg  * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
6d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
7f4a0f0c5SJohannes Berg  * Copyright (C) 2015-2016	Intel Deutschland GmbH
8c2d1560aSJohannes Berg  *
9c2d1560aSJohannes Berg  * This program is free software; you can redistribute it and/or modify
10c2d1560aSJohannes Berg  * it under the terms of the GNU General Public License version 2 as
11c2d1560aSJohannes Berg  * published by the Free Software Foundation.
12c2d1560aSJohannes Berg  *
13c2d1560aSJohannes Berg  * utilities for mac80211
14c2d1560aSJohannes Berg  */
15c2d1560aSJohannes Berg 
16c2d1560aSJohannes Berg #include <net/mac80211.h>
17c2d1560aSJohannes Berg #include <linux/netdevice.h>
18bc3b2d7fSPaul Gortmaker #include <linux/export.h>
19c2d1560aSJohannes Berg #include <linux/types.h>
20c2d1560aSJohannes Berg #include <linux/slab.h>
21c2d1560aSJohannes Berg #include <linux/skbuff.h>
22c2d1560aSJohannes Berg #include <linux/etherdevice.h>
23c2d1560aSJohannes Berg #include <linux/if_arp.h>
24c2d1560aSJohannes Berg #include <linux/bitmap.h>
25dd76986bSJohannes Berg #include <linux/crc32.h>
26881d966bSEric W. Biederman #include <net/net_namespace.h>
27c2d1560aSJohannes Berg #include <net/cfg80211.h>
28dabeb344SJohannes Berg #include <net/rtnetlink.h>
29c2d1560aSJohannes Berg 
30c2d1560aSJohannes Berg #include "ieee80211_i.h"
3124487981SJohannes Berg #include "driver-ops.h"
322c8dccc7SJohannes Berg #include "rate.h"
33ee385855SLuis Carlos Cobo #include "mesh.h"
34c2d1560aSJohannes Berg #include "wme.h"
35f2753ddbSJohannes Berg #include "led.h"
36fffd0934SJohannes Berg #include "wep.h"
37c2d1560aSJohannes Berg 
38c2d1560aSJohannes Berg /* privid for wiphys to determine whether they belong to us or not */
398a47cea7SJohannes Berg const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
40c2d1560aSJohannes Berg 
419a95371aSLuis R. Rodriguez struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
429a95371aSLuis R. Rodriguez {
439a95371aSLuis R. Rodriguez 	struct ieee80211_local *local;
449a95371aSLuis R. Rodriguez 	BUG_ON(!wiphy);
459a95371aSLuis R. Rodriguez 
469a95371aSLuis R. Rodriguez 	local = wiphy_priv(wiphy);
479a95371aSLuis R. Rodriguez 	return &local->hw;
489a95371aSLuis R. Rodriguez }
499a95371aSLuis R. Rodriguez EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
50c2d1560aSJohannes Berg 
515cf121c3SJohannes Berg void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
52c2d1560aSJohannes Berg {
53252b86c4SJohannes Berg 	struct sk_buff *skb;
542de8e0d9SJohannes Berg 	struct ieee80211_hdr *hdr;
55c2d1560aSJohannes Berg 
56252b86c4SJohannes Berg 	skb_queue_walk(&tx->skbs, skb) {
572de8e0d9SJohannes Berg 		hdr = (struct ieee80211_hdr *) skb->data;
58c2d1560aSJohannes Berg 		hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
59252b86c4SJohannes Berg 	}
60c2d1560aSJohannes Berg }
61c2d1560aSJohannes Berg 
6257fbcce3SJohannes Berg int ieee80211_frame_duration(enum nl80211_band band, size_t len,
63438b61b7SSimon Wunderlich 			     int rate, int erp, int short_preamble,
64438b61b7SSimon Wunderlich 			     int shift)
65c2d1560aSJohannes Berg {
66c2d1560aSJohannes Berg 	int dur;
67c2d1560aSJohannes Berg 
68c2d1560aSJohannes Berg 	/* calculate duration (in microseconds, rounded up to next higher
69c2d1560aSJohannes Berg 	 * integer if it includes a fractional microsecond) to send frame of
70c2d1560aSJohannes Berg 	 * len bytes (does not include FCS) at the given rate. Duration will
71c2d1560aSJohannes Berg 	 * also include SIFS.
72c2d1560aSJohannes Berg 	 *
73c2d1560aSJohannes Berg 	 * rate is in 100 kbps, so divident is multiplied by 10 in the
74c2d1560aSJohannes Berg 	 * DIV_ROUND_UP() operations.
75438b61b7SSimon Wunderlich 	 *
76438b61b7SSimon Wunderlich 	 * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
77438b61b7SSimon Wunderlich 	 * is assumed to be 0 otherwise.
78c2d1560aSJohannes Berg 	 */
79c2d1560aSJohannes Berg 
8057fbcce3SJohannes Berg 	if (band == NL80211_BAND_5GHZ || erp) {
81c2d1560aSJohannes Berg 		/*
82c2d1560aSJohannes Berg 		 * OFDM:
83c2d1560aSJohannes Berg 		 *
84c2d1560aSJohannes Berg 		 * N_DBPS = DATARATE x 4
85c2d1560aSJohannes Berg 		 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
86c2d1560aSJohannes Berg 		 *	(16 = SIGNAL time, 6 = tail bits)
87c2d1560aSJohannes Berg 		 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
88c2d1560aSJohannes Berg 		 *
89c2d1560aSJohannes Berg 		 * T_SYM = 4 usec
90438b61b7SSimon Wunderlich 		 * 802.11a - 18.5.2: aSIFSTime = 16 usec
91c2d1560aSJohannes Berg 		 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
92c2d1560aSJohannes Berg 		 *	signal ext = 6 usec
93c2d1560aSJohannes Berg 		 */
94c2d1560aSJohannes Berg 		dur = 16; /* SIFS + signal ext */
95438b61b7SSimon Wunderlich 		dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
96438b61b7SSimon Wunderlich 		dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
97438b61b7SSimon Wunderlich 
98438b61b7SSimon Wunderlich 		/* IEEE 802.11-2012 18.3.2.4: all values above are:
99438b61b7SSimon Wunderlich 		 *  * times 4 for 5 MHz
100438b61b7SSimon Wunderlich 		 *  * times 2 for 10 MHz
101438b61b7SSimon Wunderlich 		 */
102438b61b7SSimon Wunderlich 		dur *= 1 << shift;
1032103dec1SSimon Wunderlich 
1042103dec1SSimon Wunderlich 		/* rates should already consider the channel bandwidth,
1052103dec1SSimon Wunderlich 		 * don't apply divisor again.
1062103dec1SSimon Wunderlich 		 */
1072103dec1SSimon Wunderlich 		dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
1082103dec1SSimon Wunderlich 					4 * rate); /* T_SYM x N_SYM */
109c2d1560aSJohannes Berg 	} else {
110c2d1560aSJohannes Berg 		/*
111c2d1560aSJohannes Berg 		 * 802.11b or 802.11g with 802.11b compatibility:
112c2d1560aSJohannes Berg 		 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
113c2d1560aSJohannes Berg 		 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
114c2d1560aSJohannes Berg 		 *
115c2d1560aSJohannes Berg 		 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
116c2d1560aSJohannes Berg 		 * aSIFSTime = 10 usec
117c2d1560aSJohannes Berg 		 * aPreambleLength = 144 usec or 72 usec with short preamble
118c2d1560aSJohannes Berg 		 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
119c2d1560aSJohannes Berg 		 */
120c2d1560aSJohannes Berg 		dur = 10; /* aSIFSTime = 10 usec */
121c2d1560aSJohannes Berg 		dur += short_preamble ? (72 + 24) : (144 + 48);
122c2d1560aSJohannes Berg 
123c2d1560aSJohannes Berg 		dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
124c2d1560aSJohannes Berg 	}
125c2d1560aSJohannes Berg 
126c2d1560aSJohannes Berg 	return dur;
127c2d1560aSJohannes Berg }
128c2d1560aSJohannes Berg 
129c2d1560aSJohannes Berg /* Exported duration function for driver use */
13032bfd35dSJohannes Berg __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
13132bfd35dSJohannes Berg 					struct ieee80211_vif *vif,
13257fbcce3SJohannes Berg 					enum nl80211_band band,
1338318d78aSJohannes Berg 					size_t frame_len,
1348318d78aSJohannes Berg 					struct ieee80211_rate *rate)
135c2d1560aSJohannes Berg {
13625d834e1SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
137c2d1560aSJohannes Berg 	u16 dur;
138438b61b7SSimon Wunderlich 	int erp, shift = 0;
13925d834e1SJohannes Berg 	bool short_preamble = false;
140c2d1560aSJohannes Berg 
1418318d78aSJohannes Berg 	erp = 0;
14225d834e1SJohannes Berg 	if (vif) {
14325d834e1SJohannes Berg 		sdata = vif_to_sdata(vif);
144bda3933aSJohannes Berg 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
1458318d78aSJohannes Berg 		if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
1468318d78aSJohannes Berg 			erp = rate->flags & IEEE80211_RATE_ERP_G;
147438b61b7SSimon Wunderlich 		shift = ieee80211_vif_get_shift(vif);
14825d834e1SJohannes Berg 	}
1498318d78aSJohannes Berg 
1504ee73f33SMichal Kazior 	dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
151438b61b7SSimon Wunderlich 				       short_preamble, shift);
152c2d1560aSJohannes Berg 
153c2d1560aSJohannes Berg 	return cpu_to_le16(dur);
154c2d1560aSJohannes Berg }
155c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_generic_frame_duration);
156c2d1560aSJohannes Berg 
15732bfd35dSJohannes Berg __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
15832bfd35dSJohannes Berg 			      struct ieee80211_vif *vif, size_t frame_len,
159e039fa4aSJohannes Berg 			      const struct ieee80211_tx_info *frame_txctl)
160c2d1560aSJohannes Berg {
161c2d1560aSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
162c2d1560aSJohannes Berg 	struct ieee80211_rate *rate;
16325d834e1SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
164471b3efdSJohannes Berg 	bool short_preamble;
1652103dec1SSimon Wunderlich 	int erp, shift = 0, bitrate;
166c2d1560aSJohannes Berg 	u16 dur;
1672e92e6f2SJohannes Berg 	struct ieee80211_supported_band *sband;
1682e92e6f2SJohannes Berg 
1694ee73f33SMichal Kazior 	sband = local->hw.wiphy->bands[frame_txctl->band];
170c2d1560aSJohannes Berg 
17125d834e1SJohannes Berg 	short_preamble = false;
1727e9ed188SDaniel Drake 
173e039fa4aSJohannes Berg 	rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
1748318d78aSJohannes Berg 
1758318d78aSJohannes Berg 	erp = 0;
17625d834e1SJohannes Berg 	if (vif) {
17725d834e1SJohannes Berg 		sdata = vif_to_sdata(vif);
178bda3933aSJohannes Berg 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
1798318d78aSJohannes Berg 		if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
1808318d78aSJohannes Berg 			erp = rate->flags & IEEE80211_RATE_ERP_G;
181438b61b7SSimon Wunderlich 		shift = ieee80211_vif_get_shift(vif);
18225d834e1SJohannes Berg 	}
183c2d1560aSJohannes Berg 
1842103dec1SSimon Wunderlich 	bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
1852103dec1SSimon Wunderlich 
186c2d1560aSJohannes Berg 	/* CTS duration */
1872103dec1SSimon Wunderlich 	dur = ieee80211_frame_duration(sband->band, 10, bitrate,
188438b61b7SSimon Wunderlich 				       erp, short_preamble, shift);
189c2d1560aSJohannes Berg 	/* Data frame duration */
1902103dec1SSimon Wunderlich 	dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
191438b61b7SSimon Wunderlich 					erp, short_preamble, shift);
192c2d1560aSJohannes Berg 	/* ACK duration */
1932103dec1SSimon Wunderlich 	dur += ieee80211_frame_duration(sband->band, 10, bitrate,
194438b61b7SSimon Wunderlich 					erp, short_preamble, shift);
195c2d1560aSJohannes Berg 
196c2d1560aSJohannes Berg 	return cpu_to_le16(dur);
197c2d1560aSJohannes Berg }
198c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_rts_duration);
199c2d1560aSJohannes Berg 
20032bfd35dSJohannes Berg __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
20132bfd35dSJohannes Berg 				    struct ieee80211_vif *vif,
202c2d1560aSJohannes Berg 				    size_t frame_len,
203e039fa4aSJohannes Berg 				    const struct ieee80211_tx_info *frame_txctl)
204c2d1560aSJohannes Berg {
205c2d1560aSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
206c2d1560aSJohannes Berg 	struct ieee80211_rate *rate;
20725d834e1SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
208471b3efdSJohannes Berg 	bool short_preamble;
2092103dec1SSimon Wunderlich 	int erp, shift = 0, bitrate;
210c2d1560aSJohannes Berg 	u16 dur;
2112e92e6f2SJohannes Berg 	struct ieee80211_supported_band *sband;
2122e92e6f2SJohannes Berg 
2134ee73f33SMichal Kazior 	sband = local->hw.wiphy->bands[frame_txctl->band];
214c2d1560aSJohannes Berg 
21525d834e1SJohannes Berg 	short_preamble = false;
2167e9ed188SDaniel Drake 
217e039fa4aSJohannes Berg 	rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
2188318d78aSJohannes Berg 	erp = 0;
21925d834e1SJohannes Berg 	if (vif) {
22025d834e1SJohannes Berg 		sdata = vif_to_sdata(vif);
221bda3933aSJohannes Berg 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
2228318d78aSJohannes Berg 		if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
2238318d78aSJohannes Berg 			erp = rate->flags & IEEE80211_RATE_ERP_G;
224438b61b7SSimon Wunderlich 		shift = ieee80211_vif_get_shift(vif);
22525d834e1SJohannes Berg 	}
226c2d1560aSJohannes Berg 
2272103dec1SSimon Wunderlich 	bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
2282103dec1SSimon Wunderlich 
229c2d1560aSJohannes Berg 	/* Data frame duration */
2302103dec1SSimon Wunderlich 	dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
231438b61b7SSimon Wunderlich 				       erp, short_preamble, shift);
232e039fa4aSJohannes Berg 	if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
233c2d1560aSJohannes Berg 		/* ACK duration */
2342103dec1SSimon Wunderlich 		dur += ieee80211_frame_duration(sband->band, 10, bitrate,
235438b61b7SSimon Wunderlich 						erp, short_preamble, shift);
236c2d1560aSJohannes Berg 	}
237c2d1560aSJohannes Berg 
238c2d1560aSJohannes Berg 	return cpu_to_le16(dur);
239c2d1560aSJohannes Berg }
240c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_ctstoself_duration);
241c2d1560aSJohannes Berg 
2423a25a8c8SJohannes Berg void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
2433a25a8c8SJohannes Berg {
2443a25a8c8SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
245a6f38ac3SJohannes Berg 	int n_acs = IEEE80211_NUM_ACS;
246a6f38ac3SJohannes Berg 
24780a83cfcSMichal Kazior 	if (local->ops->wake_tx_queue)
24880a83cfcSMichal Kazior 		return;
24980a83cfcSMichal Kazior 
250a6f38ac3SJohannes Berg 	if (local->hw.queues < IEEE80211_NUM_ACS)
251a6f38ac3SJohannes Berg 		n_acs = 1;
2523a25a8c8SJohannes Berg 
2533a25a8c8SJohannes Berg 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2543a25a8c8SJohannes Berg 		int ac;
2553a25a8c8SJohannes Berg 
256f142c6b9SJohannes Berg 		if (!sdata->dev)
257f142c6b9SJohannes Berg 			continue;
258f142c6b9SJohannes Berg 
2593a25a8c8SJohannes Berg 		if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
2603a25a8c8SJohannes Berg 		    local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
2613a25a8c8SJohannes Berg 			continue;
2623a25a8c8SJohannes Berg 
263a6f38ac3SJohannes Berg 		for (ac = 0; ac < n_acs; ac++) {
2643a25a8c8SJohannes Berg 			int ac_queue = sdata->vif.hw_queue[ac];
2653a25a8c8SJohannes Berg 
2663a25a8c8SJohannes Berg 			if (ac_queue == queue ||
2673a25a8c8SJohannes Berg 			    (sdata->vif.cab_queue == queue &&
2683a25a8c8SJohannes Berg 			     local->queue_stop_reasons[ac_queue] == 0 &&
2693a25a8c8SJohannes Berg 			     skb_queue_empty(&local->pending[ac_queue])))
2703a25a8c8SJohannes Berg 				netif_wake_subqueue(sdata->dev, ac);
2713a25a8c8SJohannes Berg 		}
2723a25a8c8SJohannes Berg 	}
2733a25a8c8SJohannes Berg }
2743a25a8c8SJohannes Berg 
275ce7c9111SKalle Valo static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
276cca07b00SLuciano Coelho 				   enum queue_stop_reason reason,
277cca07b00SLuciano Coelho 				   bool refcounted)
278c2d1560aSJohannes Berg {
279c2d1560aSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
280c2d1560aSJohannes Berg 
281b5878a2dSJohannes Berg 	trace_wake_queue(local, queue, reason);
282b5878a2dSJohannes Berg 
283e4e72fb4SJohannes Berg 	if (WARN_ON(queue >= hw->queues))
28496f5e66eSJohannes Berg 		return;
28596f5e66eSJohannes Berg 
286ada15125SJohannes Berg 	if (!test_bit(reason, &local->queue_stop_reasons[queue]))
287ada15125SJohannes Berg 		return;
288ada15125SJohannes Berg 
289856142cdSJohannes Berg 	if (!refcounted) {
290cca07b00SLuciano Coelho 		local->q_stop_reasons[queue][reason] = 0;
291856142cdSJohannes Berg 	} else {
292cca07b00SLuciano Coelho 		local->q_stop_reasons[queue][reason]--;
293856142cdSJohannes Berg 		if (WARN_ON(local->q_stop_reasons[queue][reason] < 0))
294856142cdSJohannes Berg 			local->q_stop_reasons[queue][reason] = 0;
295856142cdSJohannes Berg 	}
296cca07b00SLuciano Coelho 
297cca07b00SLuciano Coelho 	if (local->q_stop_reasons[queue][reason] == 0)
298ce7c9111SKalle Valo 		__clear_bit(reason, &local->queue_stop_reasons[queue]);
299ce7c9111SKalle Valo 
300ce7c9111SKalle Valo 	if (local->queue_stop_reasons[queue] != 0)
301ce7c9111SKalle Valo 		/* someone still has this queue stopped */
302ce7c9111SKalle Valo 		return;
303ce7c9111SKalle Valo 
3047236fe29SJohannes Berg 	if (skb_queue_empty(&local->pending[queue])) {
305cf0277e7SJohannes Berg 		rcu_read_lock();
3063a25a8c8SJohannes Berg 		ieee80211_propagate_queue_wake(local, queue);
307cf0277e7SJohannes Berg 		rcu_read_unlock();
3087236fe29SJohannes Berg 	} else
3097236fe29SJohannes Berg 		tasklet_schedule(&local->tx_pending_tasklet);
310c2d1560aSJohannes Berg }
311ce7c9111SKalle Valo 
31296f5e66eSJohannes Berg void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
313cca07b00SLuciano Coelho 				    enum queue_stop_reason reason,
314cca07b00SLuciano Coelho 				    bool refcounted)
315ce7c9111SKalle Valo {
316ce7c9111SKalle Valo 	struct ieee80211_local *local = hw_to_local(hw);
317ce7c9111SKalle Valo 	unsigned long flags;
318ce7c9111SKalle Valo 
319ce7c9111SKalle Valo 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
320cca07b00SLuciano Coelho 	__ieee80211_wake_queue(hw, queue, reason, refcounted);
321ce7c9111SKalle Valo 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
322ce7c9111SKalle Valo }
323ce7c9111SKalle Valo 
324ce7c9111SKalle Valo void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
325ce7c9111SKalle Valo {
326ce7c9111SKalle Valo 	ieee80211_wake_queue_by_reason(hw, queue,
327cca07b00SLuciano Coelho 				       IEEE80211_QUEUE_STOP_REASON_DRIVER,
328cca07b00SLuciano Coelho 				       false);
329ce7c9111SKalle Valo }
330c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_wake_queue);
331c2d1560aSJohannes Berg 
332ce7c9111SKalle Valo static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
333cca07b00SLuciano Coelho 				   enum queue_stop_reason reason,
334cca07b00SLuciano Coelho 				   bool refcounted)
335c2d1560aSJohannes Berg {
336c2d1560aSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
337cf0277e7SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
338a6f38ac3SJohannes Berg 	int n_acs = IEEE80211_NUM_ACS;
339c2d1560aSJohannes Berg 
340b5878a2dSJohannes Berg 	trace_stop_queue(local, queue, reason);
341b5878a2dSJohannes Berg 
342e4e72fb4SJohannes Berg 	if (WARN_ON(queue >= hw->queues))
34396f5e66eSJohannes Berg 		return;
34496f5e66eSJohannes Berg 
345cca07b00SLuciano Coelho 	if (!refcounted)
346cca07b00SLuciano Coelho 		local->q_stop_reasons[queue][reason] = 1;
347cca07b00SLuciano Coelho 	else
348cca07b00SLuciano Coelho 		local->q_stop_reasons[queue][reason]++;
349ada15125SJohannes Berg 
350cca07b00SLuciano Coelho 	if (__test_and_set_bit(reason, &local->queue_stop_reasons[queue]))
351cca07b00SLuciano Coelho 		return;
352cf0277e7SJohannes Berg 
35380a83cfcSMichal Kazior 	if (local->ops->wake_tx_queue)
35480a83cfcSMichal Kazior 		return;
35580a83cfcSMichal Kazior 
356a6f38ac3SJohannes Berg 	if (local->hw.queues < IEEE80211_NUM_ACS)
357a6f38ac3SJohannes Berg 		n_acs = 1;
358a6f38ac3SJohannes Berg 
359cf0277e7SJohannes Berg 	rcu_read_lock();
3603a25a8c8SJohannes Berg 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3613a25a8c8SJohannes Berg 		int ac;
3623a25a8c8SJohannes Berg 
363f142c6b9SJohannes Berg 		if (!sdata->dev)
364f142c6b9SJohannes Berg 			continue;
365f142c6b9SJohannes Berg 
366a6f38ac3SJohannes Berg 		for (ac = 0; ac < n_acs; ac++) {
3673a25a8c8SJohannes Berg 			if (sdata->vif.hw_queue[ac] == queue ||
3683a25a8c8SJohannes Berg 			    sdata->vif.cab_queue == queue)
3693a25a8c8SJohannes Berg 				netif_stop_subqueue(sdata->dev, ac);
3703a25a8c8SJohannes Berg 		}
3713a25a8c8SJohannes Berg 	}
372cf0277e7SJohannes Berg 	rcu_read_unlock();
373c2d1560aSJohannes Berg }
374ce7c9111SKalle Valo 
37596f5e66eSJohannes Berg void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
376cca07b00SLuciano Coelho 				    enum queue_stop_reason reason,
377cca07b00SLuciano Coelho 				    bool refcounted)
378ce7c9111SKalle Valo {
379ce7c9111SKalle Valo 	struct ieee80211_local *local = hw_to_local(hw);
380ce7c9111SKalle Valo 	unsigned long flags;
381ce7c9111SKalle Valo 
382ce7c9111SKalle Valo 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
383cca07b00SLuciano Coelho 	__ieee80211_stop_queue(hw, queue, reason, refcounted);
384ce7c9111SKalle Valo 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
385ce7c9111SKalle Valo }
386ce7c9111SKalle Valo 
387ce7c9111SKalle Valo void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
388ce7c9111SKalle Valo {
389ce7c9111SKalle Valo 	ieee80211_stop_queue_by_reason(hw, queue,
390cca07b00SLuciano Coelho 				       IEEE80211_QUEUE_STOP_REASON_DRIVER,
391cca07b00SLuciano Coelho 				       false);
392ce7c9111SKalle Valo }
393c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_stop_queue);
394c2d1560aSJohannes Berg 
3958f77f384SJohannes Berg void ieee80211_add_pending_skb(struct ieee80211_local *local,
3968f77f384SJohannes Berg 			       struct sk_buff *skb)
3978f77f384SJohannes Berg {
3988f77f384SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
3998f77f384SJohannes Berg 	unsigned long flags;
400a7bc376cSJohannes Berg 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4013a25a8c8SJohannes Berg 	int queue = info->hw_queue;
402a7bc376cSJohannes Berg 
403a7bc376cSJohannes Berg 	if (WARN_ON(!info->control.vif)) {
404d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
405a7bc376cSJohannes Berg 		return;
406a7bc376cSJohannes Berg 	}
4078f77f384SJohannes Berg 
4088f77f384SJohannes Berg 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
409cca07b00SLuciano Coelho 	__ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
410cca07b00SLuciano Coelho 			       false);
4113b8d81e0SJohannes Berg 	__skb_queue_tail(&local->pending[queue], skb);
412cca07b00SLuciano Coelho 	__ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
413cca07b00SLuciano Coelho 			       false);
4148f77f384SJohannes Berg 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4158f77f384SJohannes Berg }
4168f77f384SJohannes Berg 
417e3685e03SJohannes Berg void ieee80211_add_pending_skbs(struct ieee80211_local *local,
418e3685e03SJohannes Berg 				struct sk_buff_head *skbs)
4198f77f384SJohannes Berg {
4208f77f384SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
4218f77f384SJohannes Berg 	struct sk_buff *skb;
4228f77f384SJohannes Berg 	unsigned long flags;
423b0b97a8aSJohannes Berg 	int queue, i;
4248f77f384SJohannes Berg 
4258f77f384SJohannes Berg 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4268f77f384SJohannes Berg 	while ((skb = skb_dequeue(skbs))) {
427a7bc376cSJohannes Berg 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
428a7bc376cSJohannes Berg 
429a7bc376cSJohannes Berg 		if (WARN_ON(!info->control.vif)) {
430d4fa14cdSFelix Fietkau 			ieee80211_free_txskb(&local->hw, skb);
431a7bc376cSJohannes Berg 			continue;
432a7bc376cSJohannes Berg 		}
433a7bc376cSJohannes Berg 
4343a25a8c8SJohannes Berg 		queue = info->hw_queue;
4354644ae89SJohannes Berg 
4364644ae89SJohannes Berg 		__ieee80211_stop_queue(hw, queue,
437cca07b00SLuciano Coelho 				IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
438cca07b00SLuciano Coelho 				false);
4394644ae89SJohannes Berg 
4403b8d81e0SJohannes Berg 		__skb_queue_tail(&local->pending[queue], skb);
4418f77f384SJohannes Berg 	}
4428f77f384SJohannes Berg 
4433b8d81e0SJohannes Berg 	for (i = 0; i < hw->queues; i++)
4448f77f384SJohannes Berg 		__ieee80211_wake_queue(hw, i,
445cca07b00SLuciano Coelho 			IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
446cca07b00SLuciano Coelho 			false);
4478f77f384SJohannes Berg 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4488f77f384SJohannes Berg }
4498f77f384SJohannes Berg 
450ce7c9111SKalle Valo void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
451445ea4e8SJohannes Berg 				     unsigned long queues,
452cca07b00SLuciano Coelho 				     enum queue_stop_reason reason,
453cca07b00SLuciano Coelho 				     bool refcounted)
454c2d1560aSJohannes Berg {
455ce7c9111SKalle Valo 	struct ieee80211_local *local = hw_to_local(hw);
456ce7c9111SKalle Valo 	unsigned long flags;
457c2d1560aSJohannes Berg 	int i;
458c2d1560aSJohannes Berg 
459ce7c9111SKalle Valo 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
460ce7c9111SKalle Valo 
461445ea4e8SJohannes Berg 	for_each_set_bit(i, &queues, hw->queues)
462cca07b00SLuciano Coelho 		__ieee80211_stop_queue(hw, i, reason, refcounted);
463ce7c9111SKalle Valo 
464ce7c9111SKalle Valo 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
465ce7c9111SKalle Valo }
466ce7c9111SKalle Valo 
467ce7c9111SKalle Valo void ieee80211_stop_queues(struct ieee80211_hw *hw)
468ce7c9111SKalle Valo {
469445ea4e8SJohannes Berg 	ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
470cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_DRIVER,
471cca07b00SLuciano Coelho 					false);
472c2d1560aSJohannes Berg }
473c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_stop_queues);
474c2d1560aSJohannes Berg 
47592ab8535STomas Winkler int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
47692ab8535STomas Winkler {
47792ab8535STomas Winkler 	struct ieee80211_local *local = hw_to_local(hw);
4783b8d81e0SJohannes Berg 	unsigned long flags;
4793b8d81e0SJohannes Berg 	int ret;
48096f5e66eSJohannes Berg 
481e4e72fb4SJohannes Berg 	if (WARN_ON(queue >= hw->queues))
48296f5e66eSJohannes Berg 		return true;
48396f5e66eSJohannes Berg 
4843b8d81e0SJohannes Berg 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4852419ea14SThomas Pedersen 	ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
4862419ea14SThomas Pedersen 		       &local->queue_stop_reasons[queue]);
4873b8d81e0SJohannes Berg 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4883b8d81e0SJohannes Berg 	return ret;
48992ab8535STomas Winkler }
49092ab8535STomas Winkler EXPORT_SYMBOL(ieee80211_queue_stopped);
49192ab8535STomas Winkler 
492ce7c9111SKalle Valo void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
493445ea4e8SJohannes Berg 				     unsigned long queues,
494cca07b00SLuciano Coelho 				     enum queue_stop_reason reason,
495cca07b00SLuciano Coelho 				     bool refcounted)
496c2d1560aSJohannes Berg {
497ce7c9111SKalle Valo 	struct ieee80211_local *local = hw_to_local(hw);
498ce7c9111SKalle Valo 	unsigned long flags;
499c2d1560aSJohannes Berg 	int i;
500c2d1560aSJohannes Berg 
501ce7c9111SKalle Valo 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
502ce7c9111SKalle Valo 
503445ea4e8SJohannes Berg 	for_each_set_bit(i, &queues, hw->queues)
504cca07b00SLuciano Coelho 		__ieee80211_wake_queue(hw, i, reason, refcounted);
505ce7c9111SKalle Valo 
506ce7c9111SKalle Valo 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
507ce7c9111SKalle Valo }
508ce7c9111SKalle Valo 
509ce7c9111SKalle Valo void ieee80211_wake_queues(struct ieee80211_hw *hw)
510ce7c9111SKalle Valo {
511445ea4e8SJohannes Berg 	ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
512cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_DRIVER,
513cca07b00SLuciano Coelho 					false);
514c2d1560aSJohannes Berg }
515c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_wake_queues);
516dabeb344SJohannes Berg 
51726da23b6SLuciano Coelho static unsigned int
51826da23b6SLuciano Coelho ieee80211_get_vif_queues(struct ieee80211_local *local,
51939ecc01dSJohannes Berg 			 struct ieee80211_sub_if_data *sdata)
52039ecc01dSJohannes Berg {
52126da23b6SLuciano Coelho 	unsigned int queues;
52239ecc01dSJohannes Berg 
52330686bf7SJohannes Berg 	if (sdata && ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
52439ecc01dSJohannes Berg 		int ac;
52539ecc01dSJohannes Berg 
52639ecc01dSJohannes Berg 		queues = 0;
52739ecc01dSJohannes Berg 
52839ecc01dSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
52939ecc01dSJohannes Berg 			queues |= BIT(sdata->vif.hw_queue[ac]);
53039ecc01dSJohannes Berg 		if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
53139ecc01dSJohannes Berg 			queues |= BIT(sdata->vif.cab_queue);
53239ecc01dSJohannes Berg 	} else {
53339ecc01dSJohannes Berg 		/* all queues */
53439ecc01dSJohannes Berg 		queues = BIT(local->hw.queues) - 1;
53539ecc01dSJohannes Berg 	}
53639ecc01dSJohannes Berg 
53726da23b6SLuciano Coelho 	return queues;
53826da23b6SLuciano Coelho }
53926da23b6SLuciano Coelho 
5404f9610d5SLiad Kaufman void __ieee80211_flush_queues(struct ieee80211_local *local,
5414f9610d5SLiad Kaufman 			      struct ieee80211_sub_if_data *sdata,
5423b24f4c6SEmmanuel Grumbach 			      unsigned int queues, bool drop)
54326da23b6SLuciano Coelho {
54426da23b6SLuciano Coelho 	if (!local->ops->flush)
54526da23b6SLuciano Coelho 		return;
54626da23b6SLuciano Coelho 
5474f9610d5SLiad Kaufman 	/*
5484f9610d5SLiad Kaufman 	 * If no queue was set, or if the HW doesn't support
5494f9610d5SLiad Kaufman 	 * IEEE80211_HW_QUEUE_CONTROL - flush all queues
5504f9610d5SLiad Kaufman 	 */
55130686bf7SJohannes Berg 	if (!queues || !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
55226da23b6SLuciano Coelho 		queues = ieee80211_get_vif_queues(local, sdata);
55326da23b6SLuciano Coelho 
55459f48fe2SLuciano Coelho 	ieee80211_stop_queues_by_reason(&local->hw, queues,
555cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_FLUSH,
556cca07b00SLuciano Coelho 					false);
557445ea4e8SJohannes Berg 
5583b24f4c6SEmmanuel Grumbach 	drv_flush(local, sdata, queues, drop);
559445ea4e8SJohannes Berg 
56059f48fe2SLuciano Coelho 	ieee80211_wake_queues_by_reason(&local->hw, queues,
561cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_FLUSH,
562cca07b00SLuciano Coelho 					false);
56339ecc01dSJohannes Berg }
56439ecc01dSJohannes Berg 
5654f9610d5SLiad Kaufman void ieee80211_flush_queues(struct ieee80211_local *local,
5663b24f4c6SEmmanuel Grumbach 			    struct ieee80211_sub_if_data *sdata, bool drop)
5674f9610d5SLiad Kaufman {
5683b24f4c6SEmmanuel Grumbach 	__ieee80211_flush_queues(local, sdata, 0, drop);
5694f9610d5SLiad Kaufman }
5704f9610d5SLiad Kaufman 
57126da23b6SLuciano Coelho void ieee80211_stop_vif_queues(struct ieee80211_local *local,
57226da23b6SLuciano Coelho 			       struct ieee80211_sub_if_data *sdata,
57326da23b6SLuciano Coelho 			       enum queue_stop_reason reason)
57426da23b6SLuciano Coelho {
57526da23b6SLuciano Coelho 	ieee80211_stop_queues_by_reason(&local->hw,
57626da23b6SLuciano Coelho 					ieee80211_get_vif_queues(local, sdata),
57726da23b6SLuciano Coelho 					reason, true);
57826da23b6SLuciano Coelho }
57926da23b6SLuciano Coelho 
58026da23b6SLuciano Coelho void ieee80211_wake_vif_queues(struct ieee80211_local *local,
58126da23b6SLuciano Coelho 			       struct ieee80211_sub_if_data *sdata,
58226da23b6SLuciano Coelho 			       enum queue_stop_reason reason)
58326da23b6SLuciano Coelho {
58426da23b6SLuciano Coelho 	ieee80211_wake_queues_by_reason(&local->hw,
58526da23b6SLuciano Coelho 					ieee80211_get_vif_queues(local, sdata),
58626da23b6SLuciano Coelho 					reason, true);
58726da23b6SLuciano Coelho }
58826da23b6SLuciano Coelho 
5893384d757SArik Nemtsov static void __iterate_interfaces(struct ieee80211_local *local,
590c7c71066SJohannes Berg 				 u32 iter_flags,
591dabeb344SJohannes Berg 				 void (*iterator)(void *data, u8 *mac,
59232bfd35dSJohannes Berg 						  struct ieee80211_vif *vif),
593dabeb344SJohannes Berg 				 void *data)
594dabeb344SJohannes Berg {
595dabeb344SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
5963384d757SArik Nemtsov 	bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE;
597dabeb344SJohannes Berg 
598e38bad47SJohannes Berg 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
59951fb61e7SJohannes Berg 		switch (sdata->vif.type) {
60005c914feSJohannes Berg 		case NL80211_IFTYPE_MONITOR:
601d8212184SAviya Erenfeld 			if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
60231eba5bcSFelix Fietkau 				continue;
60331eba5bcSFelix Fietkau 			break;
60405c914feSJohannes Berg 		case NL80211_IFTYPE_AP_VLAN:
605dabeb344SJohannes Berg 			continue;
6062ca27bcfSJohannes Berg 		default:
607dabeb344SJohannes Berg 			break;
608dabeb344SJohannes Berg 		}
6098b2c9824SJohannes Berg 		if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
6103384d757SArik Nemtsov 		    active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
6118b2c9824SJohannes Berg 			continue;
6123384d757SArik Nemtsov 		if (ieee80211_sdata_running(sdata) || !active_only)
61347846c9bSJohannes Berg 			iterator(data, sdata->vif.addr,
61432bfd35dSJohannes Berg 				 &sdata->vif);
615dabeb344SJohannes Berg 	}
616e38bad47SJohannes Berg 
617c7c71066SJohannes Berg 	sdata = rcu_dereference_check(local->monitor_sdata,
618c7c71066SJohannes Berg 				      lockdep_is_held(&local->iflist_mtx) ||
619c7c71066SJohannes Berg 				      lockdep_rtnl_is_held());
6208b2c9824SJohannes Berg 	if (sdata &&
6213384d757SArik Nemtsov 	    (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only ||
6228b2c9824SJohannes Berg 	     sdata->flags & IEEE80211_SDATA_IN_DRIVER))
623685fb72bSJohannes Berg 		iterator(data, sdata->vif.addr, &sdata->vif);
624c7c71066SJohannes Berg }
625685fb72bSJohannes Berg 
6263384d757SArik Nemtsov void ieee80211_iterate_interfaces(
627c7c71066SJohannes Berg 	struct ieee80211_hw *hw, u32 iter_flags,
628c7c71066SJohannes Berg 	void (*iterator)(void *data, u8 *mac,
629c7c71066SJohannes Berg 			 struct ieee80211_vif *vif),
630c7c71066SJohannes Berg 	void *data)
631c7c71066SJohannes Berg {
632c7c71066SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
633c7c71066SJohannes Berg 
634c7c71066SJohannes Berg 	mutex_lock(&local->iflist_mtx);
6353384d757SArik Nemtsov 	__iterate_interfaces(local, iter_flags, iterator, data);
636c7c71066SJohannes Berg 	mutex_unlock(&local->iflist_mtx);
637c7c71066SJohannes Berg }
6383384d757SArik Nemtsov EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces);
639c7c71066SJohannes Berg 
640c7c71066SJohannes Berg void ieee80211_iterate_active_interfaces_atomic(
641c7c71066SJohannes Berg 	struct ieee80211_hw *hw, u32 iter_flags,
642c7c71066SJohannes Berg 	void (*iterator)(void *data, u8 *mac,
643c7c71066SJohannes Berg 			 struct ieee80211_vif *vif),
644c7c71066SJohannes Berg 	void *data)
645c7c71066SJohannes Berg {
646c7c71066SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
647c7c71066SJohannes Berg 
648c7c71066SJohannes Berg 	rcu_read_lock();
6493384d757SArik Nemtsov 	__iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
6503384d757SArik Nemtsov 			     iterator, data);
651e38bad47SJohannes Berg 	rcu_read_unlock();
652dabeb344SJohannes Berg }
6532f561febSIvo van Doorn EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
65437ffc8daSJohannes Berg 
655c7c71066SJohannes Berg void ieee80211_iterate_active_interfaces_rtnl(
656c7c71066SJohannes Berg 	struct ieee80211_hw *hw, u32 iter_flags,
657c7c71066SJohannes Berg 	void (*iterator)(void *data, u8 *mac,
658c7c71066SJohannes Berg 			 struct ieee80211_vif *vif),
659c7c71066SJohannes Berg 	void *data)
660c7c71066SJohannes Berg {
661c7c71066SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
662c7c71066SJohannes Berg 
663c7c71066SJohannes Berg 	ASSERT_RTNL();
664c7c71066SJohannes Berg 
6653384d757SArik Nemtsov 	__iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
6663384d757SArik Nemtsov 			     iterator, data);
667c7c71066SJohannes Berg }
668c7c71066SJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_rtnl);
669c7c71066SJohannes Berg 
6700fc1e049SArik Nemtsov static void __iterate_stations(struct ieee80211_local *local,
6710fc1e049SArik Nemtsov 			       void (*iterator)(void *data,
6720fc1e049SArik Nemtsov 						struct ieee80211_sta *sta),
6730fc1e049SArik Nemtsov 			       void *data)
6740fc1e049SArik Nemtsov {
6750fc1e049SArik Nemtsov 	struct sta_info *sta;
6760fc1e049SArik Nemtsov 
6770fc1e049SArik Nemtsov 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
6780fc1e049SArik Nemtsov 		if (!sta->uploaded)
6790fc1e049SArik Nemtsov 			continue;
6800fc1e049SArik Nemtsov 
6810fc1e049SArik Nemtsov 		iterator(data, &sta->sta);
6820fc1e049SArik Nemtsov 	}
6830fc1e049SArik Nemtsov }
6840fc1e049SArik Nemtsov 
6850fc1e049SArik Nemtsov void ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
6860fc1e049SArik Nemtsov 			void (*iterator)(void *data,
6870fc1e049SArik Nemtsov 					 struct ieee80211_sta *sta),
6880fc1e049SArik Nemtsov 			void *data)
6890fc1e049SArik Nemtsov {
6900fc1e049SArik Nemtsov 	struct ieee80211_local *local = hw_to_local(hw);
6910fc1e049SArik Nemtsov 
6920fc1e049SArik Nemtsov 	rcu_read_lock();
6930fc1e049SArik Nemtsov 	__iterate_stations(local, iterator, data);
6940fc1e049SArik Nemtsov 	rcu_read_unlock();
6950fc1e049SArik Nemtsov }
6960fc1e049SArik Nemtsov EXPORT_SYMBOL_GPL(ieee80211_iterate_stations_atomic);
6970fc1e049SArik Nemtsov 
698ad7e718cSJohannes Berg struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
699ad7e718cSJohannes Berg {
700ad7e718cSJohannes Berg 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
701ad7e718cSJohannes Berg 
702ad7e718cSJohannes Berg 	if (!ieee80211_sdata_running(sdata) ||
703ad7e718cSJohannes Berg 	    !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
704ad7e718cSJohannes Berg 		return NULL;
705ad7e718cSJohannes Berg 	return &sdata->vif;
706ad7e718cSJohannes Berg }
707ad7e718cSJohannes Berg EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
708ad7e718cSJohannes Berg 
709dc5a1ad7SEmmanuel Grumbach struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
710dc5a1ad7SEmmanuel Grumbach {
7116513e98eSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
7126513e98eSJohannes Berg 
7136513e98eSJohannes Berg 	if (!vif)
7146513e98eSJohannes Berg 		return NULL;
7156513e98eSJohannes Berg 
7166513e98eSJohannes Berg 	sdata = vif_to_sdata(vif);
717dc5a1ad7SEmmanuel Grumbach 
718dc5a1ad7SEmmanuel Grumbach 	if (!ieee80211_sdata_running(sdata) ||
719dc5a1ad7SEmmanuel Grumbach 	    !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
720dc5a1ad7SEmmanuel Grumbach 		return NULL;
721dc5a1ad7SEmmanuel Grumbach 
722dc5a1ad7SEmmanuel Grumbach 	return &sdata->wdev;
723dc5a1ad7SEmmanuel Grumbach }
724dc5a1ad7SEmmanuel Grumbach EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
725dc5a1ad7SEmmanuel Grumbach 
72642935ecaSLuis R. Rodriguez /*
72742935ecaSLuis R. Rodriguez  * Nothing should have been stuffed into the workqueue during
7284afaff17SEmmanuel Grumbach  * the suspend->resume cycle. Since we can't check each caller
7294afaff17SEmmanuel Grumbach  * of this function if we are already quiescing / suspended,
7304afaff17SEmmanuel Grumbach  * check here and don't WARN since this can actually happen when
7314afaff17SEmmanuel Grumbach  * the rx path (for example) is racing against __ieee80211_suspend
7324afaff17SEmmanuel Grumbach  * and suspending / quiescing was set after the rx path checked
7334afaff17SEmmanuel Grumbach  * them.
73442935ecaSLuis R. Rodriguez  */
73542935ecaSLuis R. Rodriguez static bool ieee80211_can_queue_work(struct ieee80211_local *local)
73642935ecaSLuis R. Rodriguez {
7374afaff17SEmmanuel Grumbach 	if (local->quiescing || (local->suspended && !local->resuming)) {
7384afaff17SEmmanuel Grumbach 		pr_warn("queueing ieee80211 work while going to suspend\n");
73942935ecaSLuis R. Rodriguez 		return false;
7404afaff17SEmmanuel Grumbach 	}
74142935ecaSLuis R. Rodriguez 
74242935ecaSLuis R. Rodriguez 	return true;
74342935ecaSLuis R. Rodriguez }
74442935ecaSLuis R. Rodriguez 
74542935ecaSLuis R. Rodriguez void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
74642935ecaSLuis R. Rodriguez {
74742935ecaSLuis R. Rodriguez 	struct ieee80211_local *local = hw_to_local(hw);
74842935ecaSLuis R. Rodriguez 
74942935ecaSLuis R. Rodriguez 	if (!ieee80211_can_queue_work(local))
75042935ecaSLuis R. Rodriguez 		return;
75142935ecaSLuis R. Rodriguez 
75242935ecaSLuis R. Rodriguez 	queue_work(local->workqueue, work);
75342935ecaSLuis R. Rodriguez }
75442935ecaSLuis R. Rodriguez EXPORT_SYMBOL(ieee80211_queue_work);
75542935ecaSLuis R. Rodriguez 
75642935ecaSLuis R. Rodriguez void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
75742935ecaSLuis R. Rodriguez 				  struct delayed_work *dwork,
75842935ecaSLuis R. Rodriguez 				  unsigned long delay)
75942935ecaSLuis R. Rodriguez {
76042935ecaSLuis R. Rodriguez 	struct ieee80211_local *local = hw_to_local(hw);
76142935ecaSLuis R. Rodriguez 
76242935ecaSLuis R. Rodriguez 	if (!ieee80211_can_queue_work(local))
76342935ecaSLuis R. Rodriguez 		return;
76442935ecaSLuis R. Rodriguez 
76542935ecaSLuis R. Rodriguez 	queue_delayed_work(local->workqueue, dwork, delay);
76642935ecaSLuis R. Rodriguez }
76742935ecaSLuis R. Rodriguez EXPORT_SYMBOL(ieee80211_queue_delayed_work);
76842935ecaSLuis R. Rodriguez 
76935d865afSJohannes Berg u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
770dd76986bSJohannes Berg 			       struct ieee802_11_elems *elems,
771dd76986bSJohannes Berg 			       u64 filter, u32 crc)
772dd76986bSJohannes Berg {
773dd76986bSJohannes Berg 	size_t left = len;
77435d865afSJohannes Berg 	const u8 *pos = start;
775dd76986bSJohannes Berg 	bool calc_crc = filter != 0;
776fcff4f10SPaul Stewart 	DECLARE_BITMAP(seen_elems, 256);
777b2e506bfSJohannes Berg 	const u8 *ie;
778dd76986bSJohannes Berg 
779fcff4f10SPaul Stewart 	bitmap_zero(seen_elems, 256);
780dd76986bSJohannes Berg 	memset(elems, 0, sizeof(*elems));
781dd76986bSJohannes Berg 	elems->ie_start = start;
782dd76986bSJohannes Berg 	elems->total_len = len;
783dd76986bSJohannes Berg 
784dd76986bSJohannes Berg 	while (left >= 2) {
785dd76986bSJohannes Berg 		u8 id, elen;
786fcff4f10SPaul Stewart 		bool elem_parse_failed;
787dd76986bSJohannes Berg 
788dd76986bSJohannes Berg 		id = *pos++;
789dd76986bSJohannes Berg 		elen = *pos++;
790dd76986bSJohannes Berg 		left -= 2;
791dd76986bSJohannes Berg 
792fcff4f10SPaul Stewart 		if (elen > left) {
793fcff4f10SPaul Stewart 			elems->parse_error = true;
794dd76986bSJohannes Berg 			break;
795fcff4f10SPaul Stewart 		}
796fcff4f10SPaul Stewart 
7979690fb16SJohannes Berg 		switch (id) {
7989690fb16SJohannes Berg 		case WLAN_EID_SSID:
7999690fb16SJohannes Berg 		case WLAN_EID_SUPP_RATES:
8009690fb16SJohannes Berg 		case WLAN_EID_FH_PARAMS:
8019690fb16SJohannes Berg 		case WLAN_EID_DS_PARAMS:
8029690fb16SJohannes Berg 		case WLAN_EID_CF_PARAMS:
8039690fb16SJohannes Berg 		case WLAN_EID_TIM:
8049690fb16SJohannes Berg 		case WLAN_EID_IBSS_PARAMS:
8059690fb16SJohannes Berg 		case WLAN_EID_CHALLENGE:
8069690fb16SJohannes Berg 		case WLAN_EID_RSN:
8079690fb16SJohannes Berg 		case WLAN_EID_ERP_INFO:
8089690fb16SJohannes Berg 		case WLAN_EID_EXT_SUPP_RATES:
8099690fb16SJohannes Berg 		case WLAN_EID_HT_CAPABILITY:
8109690fb16SJohannes Berg 		case WLAN_EID_HT_OPERATION:
8119690fb16SJohannes Berg 		case WLAN_EID_VHT_CAPABILITY:
8129690fb16SJohannes Berg 		case WLAN_EID_VHT_OPERATION:
8139690fb16SJohannes Berg 		case WLAN_EID_MESH_ID:
8149690fb16SJohannes Berg 		case WLAN_EID_MESH_CONFIG:
8159690fb16SJohannes Berg 		case WLAN_EID_PEER_MGMT:
8169690fb16SJohannes Berg 		case WLAN_EID_PREQ:
8179690fb16SJohannes Berg 		case WLAN_EID_PREP:
8189690fb16SJohannes Berg 		case WLAN_EID_PERR:
8199690fb16SJohannes Berg 		case WLAN_EID_RANN:
8209690fb16SJohannes Berg 		case WLAN_EID_CHANNEL_SWITCH:
8219690fb16SJohannes Berg 		case WLAN_EID_EXT_CHANSWITCH_ANN:
8229690fb16SJohannes Berg 		case WLAN_EID_COUNTRY:
8239690fb16SJohannes Berg 		case WLAN_EID_PWR_CONSTRAINT:
8249690fb16SJohannes Berg 		case WLAN_EID_TIMEOUT_INTERVAL:
82585220d71SJohannes Berg 		case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
826b2e506bfSJohannes Berg 		case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
8278f2535b9SChun-Yeow Yeoh 		case WLAN_EID_CHAN_SWITCH_PARAM:
8289041c1faSArik Nemtsov 		case WLAN_EID_EXT_CAPABILITY:
82953837584SArik Nemtsov 		case WLAN_EID_CHAN_SWITCH_TIMING:
83053837584SArik Nemtsov 		case WLAN_EID_LINK_ID:
831b2e506bfSJohannes Berg 		/*
832b2e506bfSJohannes Berg 		 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
833b2e506bfSJohannes Berg 		 * that if the content gets bigger it might be needed more than once
834b2e506bfSJohannes Berg 		 */
8359690fb16SJohannes Berg 			if (test_bit(id, seen_elems)) {
836fcff4f10SPaul Stewart 				elems->parse_error = true;
837fcff4f10SPaul Stewart 				left -= elen;
838fcff4f10SPaul Stewart 				pos += elen;
839fcff4f10SPaul Stewart 				continue;
840fcff4f10SPaul Stewart 			}
8419690fb16SJohannes Berg 			break;
8429690fb16SJohannes Berg 		}
843dd76986bSJohannes Berg 
844dd76986bSJohannes Berg 		if (calc_crc && id < 64 && (filter & (1ULL << id)))
845dd76986bSJohannes Berg 			crc = crc32_be(crc, pos - 2, elen + 2);
846dd76986bSJohannes Berg 
847fcff4f10SPaul Stewart 		elem_parse_failed = false;
848fcff4f10SPaul Stewart 
849dd76986bSJohannes Berg 		switch (id) {
85053837584SArik Nemtsov 		case WLAN_EID_LINK_ID:
85153837584SArik Nemtsov 			if (elen + 2 != sizeof(struct ieee80211_tdls_lnkie)) {
85253837584SArik Nemtsov 				elem_parse_failed = true;
85353837584SArik Nemtsov 				break;
85453837584SArik Nemtsov 			}
85553837584SArik Nemtsov 			elems->lnk_id = (void *)(pos - 2);
85653837584SArik Nemtsov 			break;
85753837584SArik Nemtsov 		case WLAN_EID_CHAN_SWITCH_TIMING:
85853837584SArik Nemtsov 			if (elen != sizeof(struct ieee80211_ch_switch_timing)) {
85953837584SArik Nemtsov 				elem_parse_failed = true;
86053837584SArik Nemtsov 				break;
86153837584SArik Nemtsov 			}
86253837584SArik Nemtsov 			elems->ch_sw_timing = (void *)pos;
86353837584SArik Nemtsov 			break;
8649041c1faSArik Nemtsov 		case WLAN_EID_EXT_CAPABILITY:
8659041c1faSArik Nemtsov 			elems->ext_capab = pos;
8669041c1faSArik Nemtsov 			elems->ext_capab_len = elen;
8679041c1faSArik Nemtsov 			break;
868dd76986bSJohannes Berg 		case WLAN_EID_SSID:
869dd76986bSJohannes Berg 			elems->ssid = pos;
870dd76986bSJohannes Berg 			elems->ssid_len = elen;
871dd76986bSJohannes Berg 			break;
872dd76986bSJohannes Berg 		case WLAN_EID_SUPP_RATES:
873dd76986bSJohannes Berg 			elems->supp_rates = pos;
874dd76986bSJohannes Berg 			elems->supp_rates_len = elen;
875dd76986bSJohannes Berg 			break;
876dd76986bSJohannes Berg 		case WLAN_EID_DS_PARAMS:
8771cd8e88eSJohannes Berg 			if (elen >= 1)
878dd76986bSJohannes Berg 				elems->ds_params = pos;
8791cd8e88eSJohannes Berg 			else
8801cd8e88eSJohannes Berg 				elem_parse_failed = true;
881dd76986bSJohannes Berg 			break;
882dd76986bSJohannes Berg 		case WLAN_EID_TIM:
883dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_tim_ie)) {
884dd76986bSJohannes Berg 				elems->tim = (void *)pos;
885dd76986bSJohannes Berg 				elems->tim_len = elen;
886fcff4f10SPaul Stewart 			} else
887fcff4f10SPaul Stewart 				elem_parse_failed = true;
888dd76986bSJohannes Berg 			break;
889dd76986bSJohannes Berg 		case WLAN_EID_CHALLENGE:
890dd76986bSJohannes Berg 			elems->challenge = pos;
891dd76986bSJohannes Berg 			elems->challenge_len = elen;
892dd76986bSJohannes Berg 			break;
893dd76986bSJohannes Berg 		case WLAN_EID_VENDOR_SPECIFIC:
894dd76986bSJohannes Berg 			if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
895dd76986bSJohannes Berg 			    pos[2] == 0xf2) {
896dd76986bSJohannes Berg 				/* Microsoft OUI (00:50:F2) */
897dd76986bSJohannes Berg 
898dd76986bSJohannes Berg 				if (calc_crc)
899dd76986bSJohannes Berg 					crc = crc32_be(crc, pos - 2, elen + 2);
900dd76986bSJohannes Berg 
901441a33baSJohannes Berg 				if (elen >= 5 && pos[3] == 2) {
902dd76986bSJohannes Berg 					/* OUI Type 2 - WMM IE */
903dd76986bSJohannes Berg 					if (pos[4] == 0) {
904dd76986bSJohannes Berg 						elems->wmm_info = pos;
905dd76986bSJohannes Berg 						elems->wmm_info_len = elen;
906dd76986bSJohannes Berg 					} else if (pos[4] == 1) {
907dd76986bSJohannes Berg 						elems->wmm_param = pos;
908dd76986bSJohannes Berg 						elems->wmm_param_len = elen;
909dd76986bSJohannes Berg 					}
910dd76986bSJohannes Berg 				}
911dd76986bSJohannes Berg 			}
912dd76986bSJohannes Berg 			break;
913dd76986bSJohannes Berg 		case WLAN_EID_RSN:
914dd76986bSJohannes Berg 			elems->rsn = pos;
915dd76986bSJohannes Berg 			elems->rsn_len = elen;
916dd76986bSJohannes Berg 			break;
917dd76986bSJohannes Berg 		case WLAN_EID_ERP_INFO:
9181946bed9SJohannes Berg 			if (elen >= 1)
919dd76986bSJohannes Berg 				elems->erp_info = pos;
9201946bed9SJohannes Berg 			else
9211946bed9SJohannes Berg 				elem_parse_failed = true;
922dd76986bSJohannes Berg 			break;
923dd76986bSJohannes Berg 		case WLAN_EID_EXT_SUPP_RATES:
924dd76986bSJohannes Berg 			elems->ext_supp_rates = pos;
925dd76986bSJohannes Berg 			elems->ext_supp_rates_len = elen;
926dd76986bSJohannes Berg 			break;
927dd76986bSJohannes Berg 		case WLAN_EID_HT_CAPABILITY:
928dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_ht_cap))
929dd76986bSJohannes Berg 				elems->ht_cap_elem = (void *)pos;
930fcff4f10SPaul Stewart 			else
931fcff4f10SPaul Stewart 				elem_parse_failed = true;
932dd76986bSJohannes Berg 			break;
933074d46d1SJohannes Berg 		case WLAN_EID_HT_OPERATION:
934074d46d1SJohannes Berg 			if (elen >= sizeof(struct ieee80211_ht_operation))
935074d46d1SJohannes Berg 				elems->ht_operation = (void *)pos;
936fcff4f10SPaul Stewart 			else
937fcff4f10SPaul Stewart 				elem_parse_failed = true;
938dd76986bSJohannes Berg 			break;
939818255eaSMahesh Palivela 		case WLAN_EID_VHT_CAPABILITY:
940818255eaSMahesh Palivela 			if (elen >= sizeof(struct ieee80211_vht_cap))
941818255eaSMahesh Palivela 				elems->vht_cap_elem = (void *)pos;
942818255eaSMahesh Palivela 			else
943818255eaSMahesh Palivela 				elem_parse_failed = true;
944818255eaSMahesh Palivela 			break;
945818255eaSMahesh Palivela 		case WLAN_EID_VHT_OPERATION:
946818255eaSMahesh Palivela 			if (elen >= sizeof(struct ieee80211_vht_operation))
947818255eaSMahesh Palivela 				elems->vht_operation = (void *)pos;
948818255eaSMahesh Palivela 			else
949818255eaSMahesh Palivela 				elem_parse_failed = true;
950818255eaSMahesh Palivela 			break;
951bee7f586SJohannes Berg 		case WLAN_EID_OPMODE_NOTIF:
952bee7f586SJohannes Berg 			if (elen > 0)
953bee7f586SJohannes Berg 				elems->opmode_notif = pos;
954bee7f586SJohannes Berg 			else
955bee7f586SJohannes Berg 				elem_parse_failed = true;
956bee7f586SJohannes Berg 			break;
957dd76986bSJohannes Berg 		case WLAN_EID_MESH_ID:
958dd76986bSJohannes Berg 			elems->mesh_id = pos;
959dd76986bSJohannes Berg 			elems->mesh_id_len = elen;
960dd76986bSJohannes Berg 			break;
961dd76986bSJohannes Berg 		case WLAN_EID_MESH_CONFIG:
962dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_meshconf_ie))
963dd76986bSJohannes Berg 				elems->mesh_config = (void *)pos;
964fcff4f10SPaul Stewart 			else
965fcff4f10SPaul Stewart 				elem_parse_failed = true;
966dd76986bSJohannes Berg 			break;
967dd76986bSJohannes Berg 		case WLAN_EID_PEER_MGMT:
968dd76986bSJohannes Berg 			elems->peering = pos;
969dd76986bSJohannes Berg 			elems->peering_len = elen;
970dd76986bSJohannes Berg 			break;
9713f52b7e3SMarco Porsch 		case WLAN_EID_MESH_AWAKE_WINDOW:
9723f52b7e3SMarco Porsch 			if (elen >= 2)
9733f52b7e3SMarco Porsch 				elems->awake_window = (void *)pos;
9743f52b7e3SMarco Porsch 			break;
975dd76986bSJohannes Berg 		case WLAN_EID_PREQ:
976dd76986bSJohannes Berg 			elems->preq = pos;
977dd76986bSJohannes Berg 			elems->preq_len = elen;
978dd76986bSJohannes Berg 			break;
979dd76986bSJohannes Berg 		case WLAN_EID_PREP:
980dd76986bSJohannes Berg 			elems->prep = pos;
981dd76986bSJohannes Berg 			elems->prep_len = elen;
982dd76986bSJohannes Berg 			break;
983dd76986bSJohannes Berg 		case WLAN_EID_PERR:
984dd76986bSJohannes Berg 			elems->perr = pos;
985dd76986bSJohannes Berg 			elems->perr_len = elen;
986dd76986bSJohannes Berg 			break;
987dd76986bSJohannes Berg 		case WLAN_EID_RANN:
988dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_rann_ie))
989dd76986bSJohannes Berg 				elems->rann = (void *)pos;
990fcff4f10SPaul Stewart 			else
991fcff4f10SPaul Stewart 				elem_parse_failed = true;
992dd76986bSJohannes Berg 			break;
993dd76986bSJohannes Berg 		case WLAN_EID_CHANNEL_SWITCH:
9945bc1420bSJohannes Berg 			if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
9955bc1420bSJohannes Berg 				elem_parse_failed = true;
9965bc1420bSJohannes Berg 				break;
9975bc1420bSJohannes Berg 			}
9985bc1420bSJohannes Berg 			elems->ch_switch_ie = (void *)pos;
999dd76986bSJohannes Berg 			break;
1000b4f286a1SJohannes Berg 		case WLAN_EID_EXT_CHANSWITCH_ANN:
1001b4f286a1SJohannes Berg 			if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
1002b4f286a1SJohannes Berg 				elem_parse_failed = true;
1003b4f286a1SJohannes Berg 				break;
1004b4f286a1SJohannes Berg 			}
1005b4f286a1SJohannes Berg 			elems->ext_chansw_ie = (void *)pos;
1006b4f286a1SJohannes Berg 			break;
100785220d71SJohannes Berg 		case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
100885220d71SJohannes Berg 			if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
100985220d71SJohannes Berg 				elem_parse_failed = true;
101085220d71SJohannes Berg 				break;
101185220d71SJohannes Berg 			}
101285220d71SJohannes Berg 			elems->sec_chan_offs = (void *)pos;
101385220d71SJohannes Berg 			break;
10148f2535b9SChun-Yeow Yeoh 		case WLAN_EID_CHAN_SWITCH_PARAM:
10158f2535b9SChun-Yeow Yeoh 			if (elen !=
10168f2535b9SChun-Yeow Yeoh 			    sizeof(*elems->mesh_chansw_params_ie)) {
10178f2535b9SChun-Yeow Yeoh 				elem_parse_failed = true;
10188f2535b9SChun-Yeow Yeoh 				break;
10198f2535b9SChun-Yeow Yeoh 			}
10208f2535b9SChun-Yeow Yeoh 			elems->mesh_chansw_params_ie = (void *)pos;
10218f2535b9SChun-Yeow Yeoh 			break;
1022b2e506bfSJohannes Berg 		case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1023b2e506bfSJohannes Berg 			if (!action ||
1024b2e506bfSJohannes Berg 			    elen != sizeof(*elems->wide_bw_chansw_ie)) {
1025b2e506bfSJohannes Berg 				elem_parse_failed = true;
1026b2e506bfSJohannes Berg 				break;
1027b2e506bfSJohannes Berg 			}
1028b2e506bfSJohannes Berg 			elems->wide_bw_chansw_ie = (void *)pos;
1029b2e506bfSJohannes Berg 			break;
1030b2e506bfSJohannes Berg 		case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
1031b2e506bfSJohannes Berg 			if (action) {
1032b2e506bfSJohannes Berg 				elem_parse_failed = true;
1033b2e506bfSJohannes Berg 				break;
1034b2e506bfSJohannes Berg 			}
1035b2e506bfSJohannes Berg 			/*
1036b2e506bfSJohannes Berg 			 * This is a bit tricky, but as we only care about
1037b2e506bfSJohannes Berg 			 * the wide bandwidth channel switch element, so
1038b2e506bfSJohannes Berg 			 * just parse it out manually.
1039b2e506bfSJohannes Berg 			 */
1040b2e506bfSJohannes Berg 			ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
1041b2e506bfSJohannes Berg 					      pos, elen);
1042b2e506bfSJohannes Berg 			if (ie) {
1043b2e506bfSJohannes Berg 				if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
1044b2e506bfSJohannes Berg 					elems->wide_bw_chansw_ie =
1045b2e506bfSJohannes Berg 						(void *)(ie + 2);
1046b2e506bfSJohannes Berg 				else
1047b2e506bfSJohannes Berg 					elem_parse_failed = true;
1048b2e506bfSJohannes Berg 			}
1049b2e506bfSJohannes Berg 			break;
1050dd76986bSJohannes Berg 		case WLAN_EID_COUNTRY:
1051dd76986bSJohannes Berg 			elems->country_elem = pos;
1052dd76986bSJohannes Berg 			elems->country_elem_len = elen;
1053dd76986bSJohannes Berg 			break;
1054dd76986bSJohannes Berg 		case WLAN_EID_PWR_CONSTRAINT:
1055761a48d2SJohannes Berg 			if (elen != 1) {
1056761a48d2SJohannes Berg 				elem_parse_failed = true;
1057761a48d2SJohannes Berg 				break;
1058761a48d2SJohannes Berg 			}
1059dd76986bSJohannes Berg 			elems->pwr_constr_elem = pos;
1060dd76986bSJohannes Berg 			break;
1061c8d65917SSteinar H. Gunderson 		case WLAN_EID_CISCO_VENDOR_SPECIFIC:
1062c8d65917SSteinar H. Gunderson 			/* Lots of different options exist, but we only care
1063c8d65917SSteinar H. Gunderson 			 * about the Dynamic Transmit Power Control element.
1064c8d65917SSteinar H. Gunderson 			 * First check for the Cisco OUI, then for the DTPC
1065c8d65917SSteinar H. Gunderson 			 * tag (0x00).
1066c8d65917SSteinar H. Gunderson 			 */
1067c8d65917SSteinar H. Gunderson 			if (elen < 4) {
1068c8d65917SSteinar H. Gunderson 				elem_parse_failed = true;
1069c8d65917SSteinar H. Gunderson 				break;
1070c8d65917SSteinar H. Gunderson 			}
1071c8d65917SSteinar H. Gunderson 
1072c8d65917SSteinar H. Gunderson 			if (pos[0] != 0x00 || pos[1] != 0x40 ||
1073c8d65917SSteinar H. Gunderson 			    pos[2] != 0x96 || pos[3] != 0x00)
1074c8d65917SSteinar H. Gunderson 				break;
1075c8d65917SSteinar H. Gunderson 
1076c8d65917SSteinar H. Gunderson 			if (elen != 6) {
1077c8d65917SSteinar H. Gunderson 				elem_parse_failed = true;
1078c8d65917SSteinar H. Gunderson 				break;
1079c8d65917SSteinar H. Gunderson 			}
1080c8d65917SSteinar H. Gunderson 
1081c8d65917SSteinar H. Gunderson 			if (calc_crc)
1082c8d65917SSteinar H. Gunderson 				crc = crc32_be(crc, pos - 2, elen + 2);
1083c8d65917SSteinar H. Gunderson 
1084c8d65917SSteinar H. Gunderson 			elems->cisco_dtpc_elem = pos;
1085c8d65917SSteinar H. Gunderson 			break;
1086dd76986bSJohannes Berg 		case WLAN_EID_TIMEOUT_INTERVAL:
108779ba1d89SJohannes Berg 			if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
108879ba1d89SJohannes Berg 				elems->timeout_int = (void *)pos;
108979ba1d89SJohannes Berg 			else
109079ba1d89SJohannes Berg 				elem_parse_failed = true;
1091dd76986bSJohannes Berg 			break;
1092dd76986bSJohannes Berg 		default:
1093dd76986bSJohannes Berg 			break;
1094dd76986bSJohannes Berg 		}
1095dd76986bSJohannes Berg 
1096fcff4f10SPaul Stewart 		if (elem_parse_failed)
1097fcff4f10SPaul Stewart 			elems->parse_error = true;
1098fcff4f10SPaul Stewart 		else
10995df45690SJohannes Berg 			__set_bit(id, seen_elems);
1100fcff4f10SPaul Stewart 
1101dd76986bSJohannes Berg 		left -= elen;
1102dd76986bSJohannes Berg 		pos += elen;
1103dd76986bSJohannes Berg 	}
1104dd76986bSJohannes Berg 
1105fcff4f10SPaul Stewart 	if (left != 0)
1106fcff4f10SPaul Stewart 		elems->parse_error = true;
1107fcff4f10SPaul Stewart 
1108dd76986bSJohannes Berg 	return crc;
1109dd76986bSJohannes Berg }
1110dd76986bSJohannes Berg 
11113abead59SJohannes Berg void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
1112cec66283SJohannes Berg 			       bool bss_notify, bool enable_qos)
11135825fe10SJohannes Berg {
11145825fe10SJohannes Berg 	struct ieee80211_local *local = sdata->local;
11155825fe10SJohannes Berg 	struct ieee80211_tx_queue_params qparam;
111655de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
111754bcbc69SJohannes Berg 	int ac;
1118cec66283SJohannes Berg 	bool use_11b;
1119239281f8SRostislav Lisovy 	bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
1120aa837e1dSJohannes Berg 	int aCWmin, aCWmax;
11215825fe10SJohannes Berg 
11225825fe10SJohannes Berg 	if (!local->ops->conf_tx)
11235825fe10SJohannes Berg 		return;
11245825fe10SJohannes Berg 
112554bcbc69SJohannes Berg 	if (local->hw.queues < IEEE80211_NUM_ACS)
112654bcbc69SJohannes Berg 		return;
112754bcbc69SJohannes Berg 
11285825fe10SJohannes Berg 	memset(&qparam, 0, sizeof(qparam));
11295825fe10SJohannes Berg 
113055de908aSJohannes Berg 	rcu_read_lock();
113155de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
113255de908aSJohannes Berg 	use_11b = (chanctx_conf &&
113357fbcce3SJohannes Berg 		   chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
1134aa837e1dSJohannes Berg 		 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
113555de908aSJohannes Berg 	rcu_read_unlock();
11365825fe10SJohannes Berg 
1137239281f8SRostislav Lisovy 	is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
1138239281f8SRostislav Lisovy 
1139aa837e1dSJohannes Berg 	/* Set defaults according to 802.11-2007 Table 7-37 */
1140aa837e1dSJohannes Berg 	aCWmax = 1023;
1141aa837e1dSJohannes Berg 	if (use_11b)
1142aa837e1dSJohannes Berg 		aCWmin = 31;
11435825fe10SJohannes Berg 	else
1144aa837e1dSJohannes Berg 		aCWmin = 15;
11455825fe10SJohannes Berg 
11461f4ffde8SFred Zhou 	/* Confiure old 802.11b/g medium access rules. */
11471f4ffde8SFred Zhou 	qparam.cw_max = aCWmax;
11481f4ffde8SFred Zhou 	qparam.cw_min = aCWmin;
11491f4ffde8SFred Zhou 	qparam.txop = 0;
11501f4ffde8SFred Zhou 	qparam.aifs = 2;
11511f4ffde8SFred Zhou 
11521f4ffde8SFred Zhou 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
11531f4ffde8SFred Zhou 		/* Update if QoS is enabled. */
1154a8ce8544SStanislaw Gruszka 		if (enable_qos) {
115554bcbc69SJohannes Berg 			switch (ac) {
11561d98fb12SJohannes Berg 			case IEEE80211_AC_BK:
11577ba10a8eSJohannes Berg 				qparam.cw_max = aCWmax;
11587ba10a8eSJohannes Berg 				qparam.cw_min = aCWmin;
11595825fe10SJohannes Berg 				qparam.txop = 0;
1160239281f8SRostislav Lisovy 				if (is_ocb)
1161239281f8SRostislav Lisovy 					qparam.aifs = 9;
1162239281f8SRostislav Lisovy 				else
1163aa837e1dSJohannes Berg 					qparam.aifs = 7;
1164aa837e1dSJohannes Berg 				break;
1165a8ce8544SStanislaw Gruszka 			/* never happens but let's not leave undefined */
1166a8ce8544SStanislaw Gruszka 			default:
11671d98fb12SJohannes Berg 			case IEEE80211_AC_BE:
11687ba10a8eSJohannes Berg 				qparam.cw_max = aCWmax;
11697ba10a8eSJohannes Berg 				qparam.cw_min = aCWmin;
1170aa837e1dSJohannes Berg 				qparam.txop = 0;
1171239281f8SRostislav Lisovy 				if (is_ocb)
1172239281f8SRostislav Lisovy 					qparam.aifs = 6;
1173239281f8SRostislav Lisovy 				else
1174aa837e1dSJohannes Berg 					qparam.aifs = 3;
1175aa837e1dSJohannes Berg 				break;
11761d98fb12SJohannes Berg 			case IEEE80211_AC_VI:
1177aa837e1dSJohannes Berg 				qparam.cw_max = aCWmin;
1178aa837e1dSJohannes Berg 				qparam.cw_min = (aCWmin + 1) / 2 - 1;
1179239281f8SRostislav Lisovy 				if (is_ocb)
1180239281f8SRostislav Lisovy 					qparam.txop = 0;
1181239281f8SRostislav Lisovy 				else if (use_11b)
1182aa837e1dSJohannes Berg 					qparam.txop = 6016/32;
1183aa837e1dSJohannes Berg 				else
1184aa837e1dSJohannes Berg 					qparam.txop = 3008/32;
1185239281f8SRostislav Lisovy 
1186239281f8SRostislav Lisovy 				if (is_ocb)
1187239281f8SRostislav Lisovy 					qparam.aifs = 3;
1188239281f8SRostislav Lisovy 				else
1189aa837e1dSJohannes Berg 					qparam.aifs = 2;
1190aa837e1dSJohannes Berg 				break;
11911d98fb12SJohannes Berg 			case IEEE80211_AC_VO:
1192aa837e1dSJohannes Berg 				qparam.cw_max = (aCWmin + 1) / 2 - 1;
1193aa837e1dSJohannes Berg 				qparam.cw_min = (aCWmin + 1) / 4 - 1;
1194239281f8SRostislav Lisovy 				if (is_ocb)
1195239281f8SRostislav Lisovy 					qparam.txop = 0;
1196239281f8SRostislav Lisovy 				else if (use_11b)
1197aa837e1dSJohannes Berg 					qparam.txop = 3264/32;
1198aa837e1dSJohannes Berg 				else
1199aa837e1dSJohannes Berg 					qparam.txop = 1504/32;
1200aa837e1dSJohannes Berg 				qparam.aifs = 2;
1201aa837e1dSJohannes Berg 				break;
1202aa837e1dSJohannes Berg 			}
1203a8ce8544SStanislaw Gruszka 		}
12045825fe10SJohannes Berg 
1205ab13315aSKalle Valo 		qparam.uapsd = false;
1206ab13315aSKalle Valo 
120754bcbc69SJohannes Berg 		sdata->tx_conf[ac] = qparam;
120854bcbc69SJohannes Berg 		drv_conf_tx(local, sdata, ac, &qparam);
1209aa837e1dSJohannes Berg 	}
1210e1b3ec1aSStanislaw Gruszka 
1211f142c6b9SJohannes Berg 	if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1212708d50edSAyala Beker 	    sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1213708d50edSAyala Beker 	    sdata->vif.type != NL80211_IFTYPE_NAN) {
1214a8ce8544SStanislaw Gruszka 		sdata->vif.bss_conf.qos = enable_qos;
12153abead59SJohannes Berg 		if (bss_notify)
12163abead59SJohannes Berg 			ieee80211_bss_info_change_notify(sdata,
12173abead59SJohannes Berg 							 BSS_CHANGED_QOS);
12185825fe10SJohannes Berg 	}
1219d9734979SSujith }
1220e50db65cSJohannes Berg 
122146900298SJohannes Berg void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1222700e8ea6SJouni Malinen 			 u16 transaction, u16 auth_alg, u16 status,
12234a3cb702SJohannes Berg 			 const u8 *extra, size_t extra_len, const u8 *da,
12241672c0e3SJohannes Berg 			 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
12251672c0e3SJohannes Berg 			 u32 tx_flags)
122646900298SJohannes Berg {
122746900298SJohannes Berg 	struct ieee80211_local *local = sdata->local;
122846900298SJohannes Berg 	struct sk_buff *skb;
122946900298SJohannes Berg 	struct ieee80211_mgmt *mgmt;
1230fffd0934SJohannes Berg 	int err;
123146900298SJohannes Berg 
123215e230abSFred Zhou 	/* 24 + 6 = header + auth_algo + auth_transaction + status_code */
1233744462a9SMax Stepanov 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
1234744462a9SMax Stepanov 			    24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN);
1235d15b8459SJoe Perches 	if (!skb)
123646900298SJohannes Berg 		return;
1237d15b8459SJoe Perches 
1238744462a9SMax Stepanov 	skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
123946900298SJohannes Berg 
124046900298SJohannes Berg 	mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
124146900298SJohannes Berg 	memset(mgmt, 0, 24 + 6);
124246900298SJohannes Berg 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
124346900298SJohannes Berg 					  IEEE80211_STYPE_AUTH);
1244efa6a09dSAntonio Quartulli 	memcpy(mgmt->da, da, ETH_ALEN);
124547846c9bSJohannes Berg 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
124646900298SJohannes Berg 	memcpy(mgmt->bssid, bssid, ETH_ALEN);
124746900298SJohannes Berg 	mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
124846900298SJohannes Berg 	mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
1249700e8ea6SJouni Malinen 	mgmt->u.auth.status_code = cpu_to_le16(status);
125046900298SJohannes Berg 	if (extra)
125146900298SJohannes Berg 		memcpy(skb_put(skb, extra_len), extra, extra_len);
125246900298SJohannes Berg 
1253fffd0934SJohannes Berg 	if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1254fffd0934SJohannes Berg 		mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1255fffd0934SJohannes Berg 		err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1256fffd0934SJohannes Berg 		WARN_ON(err);
1257fffd0934SJohannes Berg 	}
1258fffd0934SJohannes Berg 
12591672c0e3SJohannes Berg 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
12601672c0e3SJohannes Berg 					tx_flags;
126162ae67beSJohannes Berg 	ieee80211_tx_skb(sdata, skb);
126246900298SJohannes Berg }
126346900298SJohannes Berg 
12646ae16775SAntonio Quartulli void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
12656ae16775SAntonio Quartulli 				    const u8 *bssid, u16 stype, u16 reason,
12666ae16775SAntonio Quartulli 				    bool send_frame, u8 *frame_buf)
12676ae16775SAntonio Quartulli {
12686ae16775SAntonio Quartulli 	struct ieee80211_local *local = sdata->local;
12696ae16775SAntonio Quartulli 	struct sk_buff *skb;
12706ae16775SAntonio Quartulli 	struct ieee80211_mgmt *mgmt = (void *)frame_buf;
12716ae16775SAntonio Quartulli 
12726ae16775SAntonio Quartulli 	/* build frame */
12736ae16775SAntonio Quartulli 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
12746ae16775SAntonio Quartulli 	mgmt->duration = 0; /* initialize only */
12756ae16775SAntonio Quartulli 	mgmt->seq_ctrl = 0; /* initialize only */
12766ae16775SAntonio Quartulli 	memcpy(mgmt->da, bssid, ETH_ALEN);
12776ae16775SAntonio Quartulli 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
12786ae16775SAntonio Quartulli 	memcpy(mgmt->bssid, bssid, ETH_ALEN);
12796ae16775SAntonio Quartulli 	/* u.deauth.reason_code == u.disassoc.reason_code */
12806ae16775SAntonio Quartulli 	mgmt->u.deauth.reason_code = cpu_to_le16(reason);
12816ae16775SAntonio Quartulli 
12826ae16775SAntonio Quartulli 	if (send_frame) {
12836ae16775SAntonio Quartulli 		skb = dev_alloc_skb(local->hw.extra_tx_headroom +
12846ae16775SAntonio Quartulli 				    IEEE80211_DEAUTH_FRAME_LEN);
12856ae16775SAntonio Quartulli 		if (!skb)
12866ae16775SAntonio Quartulli 			return;
12876ae16775SAntonio Quartulli 
12886ae16775SAntonio Quartulli 		skb_reserve(skb, local->hw.extra_tx_headroom);
12896ae16775SAntonio Quartulli 
12906ae16775SAntonio Quartulli 		/* copy in frame */
12916ae16775SAntonio Quartulli 		memcpy(skb_put(skb, IEEE80211_DEAUTH_FRAME_LEN),
12926ae16775SAntonio Quartulli 		       mgmt, IEEE80211_DEAUTH_FRAME_LEN);
12936ae16775SAntonio Quartulli 
12946ae16775SAntonio Quartulli 		if (sdata->vif.type != NL80211_IFTYPE_STATION ||
12956ae16775SAntonio Quartulli 		    !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
12966ae16775SAntonio Quartulli 			IEEE80211_SKB_CB(skb)->flags |=
12976ae16775SAntonio Quartulli 				IEEE80211_TX_INTFL_DONT_ENCRYPT;
12986ae16775SAntonio Quartulli 
12996ae16775SAntonio Quartulli 		ieee80211_tx_skb(sdata, skb);
13006ae16775SAntonio Quartulli 	}
13016ae16775SAntonio Quartulli }
13026ae16775SAntonio Quartulli 
1303c56ef672SDavid Spinadel static int ieee80211_build_preq_ies_band(struct ieee80211_local *local,
1304c56ef672SDavid Spinadel 					 u8 *buffer, size_t buffer_len,
1305c56ef672SDavid Spinadel 					 const u8 *ie, size_t ie_len,
130657fbcce3SJohannes Berg 					 enum nl80211_band band,
1307c56ef672SDavid Spinadel 					 u32 rate_mask,
1308c56ef672SDavid Spinadel 					 struct cfg80211_chan_def *chandef,
1309c56ef672SDavid Spinadel 					 size_t *offset)
1310de95a54bSJohannes Berg {
1311de95a54bSJohannes Berg 	struct ieee80211_supported_band *sband;
1312c604b9f2SJohannes Berg 	u8 *pos = buffer, *end = buffer + buffer_len;
1313c56ef672SDavid Spinadel 	size_t noffset;
13148e664fb3SJohannes Berg 	int supp_rates_len, i;
13158dcb2003SJouni Malinen 	u8 rates[32];
13168dcb2003SJouni Malinen 	int num_rates;
13178dcb2003SJouni Malinen 	int ext_rates_len;
13182103dec1SSimon Wunderlich 	int shift;
13192103dec1SSimon Wunderlich 	u32 rate_flags;
132040a11ca8SJohannes Berg 	bool have_80mhz = false;
1321de95a54bSJohannes Berg 
1322c56ef672SDavid Spinadel 	*offset = 0;
1323c56ef672SDavid Spinadel 
13244d36ec58SJohannes Berg 	sband = local->hw.wiphy->bands[band];
1325d811b3d5SArik Nemtsov 	if (WARN_ON_ONCE(!sband))
1326d811b3d5SArik Nemtsov 		return 0;
1327de95a54bSJohannes Berg 
13282103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(chandef);
13292103dec1SSimon Wunderlich 	shift = ieee80211_chandef_get_shift(chandef);
13302103dec1SSimon Wunderlich 
13318dcb2003SJouni Malinen 	num_rates = 0;
13328dcb2003SJouni Malinen 	for (i = 0; i < sband->n_bitrates; i++) {
13338dcb2003SJouni Malinen 		if ((BIT(i) & rate_mask) == 0)
13348dcb2003SJouni Malinen 			continue; /* skip rate */
13352103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
13362103dec1SSimon Wunderlich 			continue;
13372103dec1SSimon Wunderlich 
13382103dec1SSimon Wunderlich 		rates[num_rates++] =
13392103dec1SSimon Wunderlich 			(u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
13402103dec1SSimon Wunderlich 					  (1 << shift) * 5);
13418dcb2003SJouni Malinen 	}
13428dcb2003SJouni Malinen 
13438dcb2003SJouni Malinen 	supp_rates_len = min_t(int, num_rates, 8);
13448e664fb3SJohannes Berg 
1345c604b9f2SJohannes Berg 	if (end - pos < 2 + supp_rates_len)
1346c604b9f2SJohannes Berg 		goto out_err;
1347de95a54bSJohannes Berg 	*pos++ = WLAN_EID_SUPP_RATES;
13488e664fb3SJohannes Berg 	*pos++ = supp_rates_len;
13498dcb2003SJouni Malinen 	memcpy(pos, rates, supp_rates_len);
13508dcb2003SJouni Malinen 	pos += supp_rates_len;
1351de95a54bSJohannes Berg 
13528e664fb3SJohannes Berg 	/* insert "request information" if in custom IEs */
13538e664fb3SJohannes Berg 	if (ie && ie_len) {
13548e664fb3SJohannes Berg 		static const u8 before_extrates[] = {
13558e664fb3SJohannes Berg 			WLAN_EID_SSID,
13568e664fb3SJohannes Berg 			WLAN_EID_SUPP_RATES,
13578e664fb3SJohannes Berg 			WLAN_EID_REQUEST,
13588e664fb3SJohannes Berg 		};
13598e664fb3SJohannes Berg 		noffset = ieee80211_ie_split(ie, ie_len,
13608e664fb3SJohannes Berg 					     before_extrates,
13618e664fb3SJohannes Berg 					     ARRAY_SIZE(before_extrates),
1362c56ef672SDavid Spinadel 					     *offset);
1363c56ef672SDavid Spinadel 		if (end - pos < noffset - *offset)
1364c604b9f2SJohannes Berg 			goto out_err;
1365c56ef672SDavid Spinadel 		memcpy(pos, ie + *offset, noffset - *offset);
1366c56ef672SDavid Spinadel 		pos += noffset - *offset;
1367c56ef672SDavid Spinadel 		*offset = noffset;
13688e664fb3SJohannes Berg 	}
13698e664fb3SJohannes Berg 
13708dcb2003SJouni Malinen 	ext_rates_len = num_rates - supp_rates_len;
13718dcb2003SJouni Malinen 	if (ext_rates_len > 0) {
1372c604b9f2SJohannes Berg 		if (end - pos < 2 + ext_rates_len)
1373c604b9f2SJohannes Berg 			goto out_err;
1374de95a54bSJohannes Berg 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
13758dcb2003SJouni Malinen 		*pos++ = ext_rates_len;
13768dcb2003SJouni Malinen 		memcpy(pos, rates + supp_rates_len, ext_rates_len);
13778dcb2003SJouni Malinen 		pos += ext_rates_len;
13788e664fb3SJohannes Berg 	}
13798e664fb3SJohannes Berg 
138057fbcce3SJohannes Berg 	if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
1381c604b9f2SJohannes Berg 		if (end - pos < 3)
1382c604b9f2SJohannes Berg 			goto out_err;
1383651b5225SJouni Malinen 		*pos++ = WLAN_EID_DS_PARAMS;
1384651b5225SJouni Malinen 		*pos++ = 1;
13852103dec1SSimon Wunderlich 		*pos++ = ieee80211_frequency_to_channel(
13862103dec1SSimon Wunderlich 				chandef->chan->center_freq);
1387651b5225SJouni Malinen 	}
1388651b5225SJouni Malinen 
13898e664fb3SJohannes Berg 	/* insert custom IEs that go before HT */
13908e664fb3SJohannes Berg 	if (ie && ie_len) {
13918e664fb3SJohannes Berg 		static const u8 before_ht[] = {
13928e664fb3SJohannes Berg 			WLAN_EID_SSID,
13938e664fb3SJohannes Berg 			WLAN_EID_SUPP_RATES,
13948e664fb3SJohannes Berg 			WLAN_EID_REQUEST,
13958e664fb3SJohannes Berg 			WLAN_EID_EXT_SUPP_RATES,
13968e664fb3SJohannes Berg 			WLAN_EID_DS_PARAMS,
13978e664fb3SJohannes Berg 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
13988e664fb3SJohannes Berg 		};
13998e664fb3SJohannes Berg 		noffset = ieee80211_ie_split(ie, ie_len,
14008e664fb3SJohannes Berg 					     before_ht, ARRAY_SIZE(before_ht),
1401c56ef672SDavid Spinadel 					     *offset);
1402c56ef672SDavid Spinadel 		if (end - pos < noffset - *offset)
1403c604b9f2SJohannes Berg 			goto out_err;
1404c56ef672SDavid Spinadel 		memcpy(pos, ie + *offset, noffset - *offset);
1405c56ef672SDavid Spinadel 		pos += noffset - *offset;
1406c56ef672SDavid Spinadel 		*offset = noffset;
1407de95a54bSJohannes Berg 	}
1408de95a54bSJohannes Berg 
1409c604b9f2SJohannes Berg 	if (sband->ht_cap.ht_supported) {
1410c604b9f2SJohannes Berg 		if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
1411c604b9f2SJohannes Berg 			goto out_err;
1412ef96a842SBen Greear 		pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
1413ef96a842SBen Greear 						sband->ht_cap.cap);
1414c604b9f2SJohannes Berg 	}
14155ef2d41aSJohannes Berg 
1416de95a54bSJohannes Berg 	/*
1417de95a54bSJohannes Berg 	 * If adding more here, adjust code in main.c
1418de95a54bSJohannes Berg 	 * that calculates local->scan_ies_len.
1419de95a54bSJohannes Berg 	 */
1420de95a54bSJohannes Berg 
14214d952300SJohannes Berg 	/* insert custom IEs that go before VHT */
14228e664fb3SJohannes Berg 	if (ie && ie_len) {
14234d952300SJohannes Berg 		static const u8 before_vht[] = {
14244d952300SJohannes Berg 			WLAN_EID_SSID,
14254d952300SJohannes Berg 			WLAN_EID_SUPP_RATES,
14264d952300SJohannes Berg 			WLAN_EID_REQUEST,
14274d952300SJohannes Berg 			WLAN_EID_EXT_SUPP_RATES,
14284d952300SJohannes Berg 			WLAN_EID_DS_PARAMS,
14294d952300SJohannes Berg 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
14304d952300SJohannes Berg 			WLAN_EID_HT_CAPABILITY,
14314d952300SJohannes Berg 			WLAN_EID_BSS_COEX_2040,
14324d952300SJohannes Berg 			WLAN_EID_EXT_CAPABILITY,
14334d952300SJohannes Berg 			WLAN_EID_SSID_LIST,
14344d952300SJohannes Berg 			WLAN_EID_CHANNEL_USAGE,
14354d952300SJohannes Berg 			WLAN_EID_INTERWORKING,
14364d952300SJohannes Berg 			/* mesh ID can't happen here */
14374d952300SJohannes Berg 			/* 60 GHz can't happen here right now */
14384d952300SJohannes Berg 		};
14394d952300SJohannes Berg 		noffset = ieee80211_ie_split(ie, ie_len,
14404d952300SJohannes Berg 					     before_vht, ARRAY_SIZE(before_vht),
1441c56ef672SDavid Spinadel 					     *offset);
1442c56ef672SDavid Spinadel 		if (end - pos < noffset - *offset)
1443c604b9f2SJohannes Berg 			goto out_err;
1444c56ef672SDavid Spinadel 		memcpy(pos, ie + *offset, noffset - *offset);
1445c56ef672SDavid Spinadel 		pos += noffset - *offset;
1446c56ef672SDavid Spinadel 		*offset = noffset;
1447de95a54bSJohannes Berg 	}
1448de95a54bSJohannes Berg 
144940a11ca8SJohannes Berg 	/* Check if any channel in this sband supports at least 80 MHz */
145040a11ca8SJohannes Berg 	for (i = 0; i < sband->n_channels; i++) {
1451fa44b988SArik Nemtsov 		if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
1452fa44b988SArik Nemtsov 						IEEE80211_CHAN_NO_80MHZ))
1453fa44b988SArik Nemtsov 			continue;
1454fa44b988SArik Nemtsov 
145540a11ca8SJohannes Berg 		have_80mhz = true;
145640a11ca8SJohannes Berg 		break;
145740a11ca8SJohannes Berg 	}
145840a11ca8SJohannes Berg 
145940a11ca8SJohannes Berg 	if (sband->vht_cap.vht_supported && have_80mhz) {
1460c604b9f2SJohannes Berg 		if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
1461c604b9f2SJohannes Berg 			goto out_err;
1462ba0afa2fSMahesh Palivela 		pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
1463ba0afa2fSMahesh Palivela 						 sband->vht_cap.cap);
1464c604b9f2SJohannes Berg 	}
1465ba0afa2fSMahesh Palivela 
1466de95a54bSJohannes Berg 	return pos - buffer;
1467c604b9f2SJohannes Berg  out_err:
1468c604b9f2SJohannes Berg 	WARN_ONCE(1, "not enough space for preq IEs\n");
1469c604b9f2SJohannes Berg 	return pos - buffer;
1470de95a54bSJohannes Berg }
1471de95a54bSJohannes Berg 
1472c56ef672SDavid Spinadel int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
1473c56ef672SDavid Spinadel 			     size_t buffer_len,
1474c56ef672SDavid Spinadel 			     struct ieee80211_scan_ies *ie_desc,
1475c56ef672SDavid Spinadel 			     const u8 *ie, size_t ie_len,
1476c56ef672SDavid Spinadel 			     u8 bands_used, u32 *rate_masks,
1477c56ef672SDavid Spinadel 			     struct cfg80211_chan_def *chandef)
1478c56ef672SDavid Spinadel {
1479c56ef672SDavid Spinadel 	size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
1480c56ef672SDavid Spinadel 	int i;
1481c56ef672SDavid Spinadel 
1482c56ef672SDavid Spinadel 	memset(ie_desc, 0, sizeof(*ie_desc));
1483c56ef672SDavid Spinadel 
148457fbcce3SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
1485c56ef672SDavid Spinadel 		if (bands_used & BIT(i)) {
1486c56ef672SDavid Spinadel 			pos += ieee80211_build_preq_ies_band(local,
1487c56ef672SDavid Spinadel 							     buffer + pos,
1488c56ef672SDavid Spinadel 							     buffer_len - pos,
1489c56ef672SDavid Spinadel 							     ie, ie_len, i,
1490c56ef672SDavid Spinadel 							     rate_masks[i],
1491c56ef672SDavid Spinadel 							     chandef,
1492c56ef672SDavid Spinadel 							     &custom_ie_offset);
1493c56ef672SDavid Spinadel 			ie_desc->ies[i] = buffer + old_pos;
1494c56ef672SDavid Spinadel 			ie_desc->len[i] = pos - old_pos;
1495c56ef672SDavid Spinadel 			old_pos = pos;
1496c56ef672SDavid Spinadel 		}
1497c56ef672SDavid Spinadel 	}
1498c56ef672SDavid Spinadel 
1499c56ef672SDavid Spinadel 	/* add any remaining custom IEs */
1500c56ef672SDavid Spinadel 	if (ie && ie_len) {
1501c56ef672SDavid Spinadel 		if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
1502c56ef672SDavid Spinadel 			      "not enough space for preq custom IEs\n"))
1503c56ef672SDavid Spinadel 			return pos;
1504c56ef672SDavid Spinadel 		memcpy(buffer + pos, ie + custom_ie_offset,
1505c56ef672SDavid Spinadel 		       ie_len - custom_ie_offset);
1506c56ef672SDavid Spinadel 		ie_desc->common_ies = buffer + pos;
1507c56ef672SDavid Spinadel 		ie_desc->common_ie_len = ie_len - custom_ie_offset;
1508c56ef672SDavid Spinadel 		pos += ie_len - custom_ie_offset;
1509c56ef672SDavid Spinadel 	}
1510c56ef672SDavid Spinadel 
1511c56ef672SDavid Spinadel 	return pos;
1512c56ef672SDavid Spinadel };
1513c56ef672SDavid Spinadel 
1514a619a4c0SJuuso Oikarinen struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
1515a344d677SJohannes Berg 					  const u8 *src, const u8 *dst,
1516a344d677SJohannes Berg 					  u32 ratemask,
15176b77863bSJohannes Berg 					  struct ieee80211_channel *chan,
1518de95a54bSJohannes Berg 					  const u8 *ssid, size_t ssid_len,
1519a806c558SPaul Stewart 					  const u8 *ie, size_t ie_len,
1520a806c558SPaul Stewart 					  bool directed)
152146900298SJohannes Berg {
152246900298SJohannes Berg 	struct ieee80211_local *local = sdata->local;
15232103dec1SSimon Wunderlich 	struct cfg80211_chan_def chandef;
152446900298SJohannes Berg 	struct sk_buff *skb;
152546900298SJohannes Berg 	struct ieee80211_mgmt *mgmt;
1526b9a9ada1SJohannes Berg 	int ies_len;
152757fbcce3SJohannes Berg 	u32 rate_masks[NUM_NL80211_BANDS] = {};
1528c56ef672SDavid Spinadel 	struct ieee80211_scan_ies dummy_ie_desc;
152946900298SJohannes Berg 
1530a806c558SPaul Stewart 	/*
1531a806c558SPaul Stewart 	 * Do not send DS Channel parameter for directed probe requests
1532a806c558SPaul Stewart 	 * in order to maximize the chance that we get a response.  Some
1533a806c558SPaul Stewart 	 * badly-behaved APs don't respond when this parameter is included.
1534a806c558SPaul Stewart 	 */
15352103dec1SSimon Wunderlich 	chandef.width = sdata->vif.bss_conf.chandef.width;
1536a806c558SPaul Stewart 	if (directed)
15372103dec1SSimon Wunderlich 		chandef.chan = NULL;
1538a806c558SPaul Stewart 	else
15392103dec1SSimon Wunderlich 		chandef.chan = chan;
1540651b5225SJouni Malinen 
1541a344d677SJohannes Berg 	skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
1542a344d677SJohannes Berg 				     100 + ie_len);
15435b2bbf75SJohannes Berg 	if (!skb)
1544b9a9ada1SJohannes Berg 		return NULL;
1545b9a9ada1SJohannes Berg 
1546c56ef672SDavid Spinadel 	rate_masks[chan->band] = ratemask;
1547b9a9ada1SJohannes Berg 	ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
1548c56ef672SDavid Spinadel 					   skb_tailroom(skb), &dummy_ie_desc,
1549c56ef672SDavid Spinadel 					   ie, ie_len, BIT(chan->band),
1550c56ef672SDavid Spinadel 					   rate_masks, &chandef);
1551b9a9ada1SJohannes Berg 	skb_put(skb, ies_len);
15527c12ce8bSKalle Valo 
155346900298SJohannes Berg 	if (dst) {
15547c12ce8bSKalle Valo 		mgmt = (struct ieee80211_mgmt *) skb->data;
155546900298SJohannes Berg 		memcpy(mgmt->da, dst, ETH_ALEN);
155646900298SJohannes Berg 		memcpy(mgmt->bssid, dst, ETH_ALEN);
155746900298SJohannes Berg 	}
155846900298SJohannes Berg 
155962ae67beSJohannes Berg 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
15605b2bbf75SJohannes Berg 
1561a619a4c0SJuuso Oikarinen 	return skb;
1562a619a4c0SJuuso Oikarinen }
1563a619a4c0SJuuso Oikarinen 
1564a344d677SJohannes Berg void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata,
1565a344d677SJohannes Berg 			      const u8 *src, const u8 *dst,
1566a619a4c0SJuuso Oikarinen 			      const u8 *ssid, size_t ssid_len,
1567a806c558SPaul Stewart 			      const u8 *ie, size_t ie_len,
15681672c0e3SJohannes Berg 			      u32 ratemask, bool directed, u32 tx_flags,
156955de908aSJohannes Berg 			      struct ieee80211_channel *channel, bool scan)
1570a619a4c0SJuuso Oikarinen {
1571a619a4c0SJuuso Oikarinen 	struct sk_buff *skb;
1572a619a4c0SJuuso Oikarinen 
1573a344d677SJohannes Berg 	skb = ieee80211_build_probe_req(sdata, src, dst, ratemask, channel,
15746b77863bSJohannes Berg 					ssid, ssid_len,
157585a237feSJohannes Berg 					ie, ie_len, directed);
1576aad14cebSRajkumar Manoharan 	if (skb) {
15771672c0e3SJohannes Berg 		IEEE80211_SKB_CB(skb)->flags |= tx_flags;
157855de908aSJohannes Berg 		if (scan)
157955de908aSJohannes Berg 			ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
158055de908aSJohannes Berg 		else
1581a619a4c0SJuuso Oikarinen 			ieee80211_tx_skb(sdata, skb);
158246900298SJohannes Berg 	}
1583aad14cebSRajkumar Manoharan }
158446900298SJohannes Berg 
15852103dec1SSimon Wunderlich u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
158646900298SJohannes Berg 			    struct ieee802_11_elems *elems,
158757fbcce3SJohannes Berg 			    enum nl80211_band band, u32 *basic_rates)
158846900298SJohannes Berg {
158946900298SJohannes Berg 	struct ieee80211_supported_band *sband;
159046900298SJohannes Berg 	size_t num_rates;
15912103dec1SSimon Wunderlich 	u32 supp_rates, rate_flags;
15922103dec1SSimon Wunderlich 	int i, j, shift;
15932103dec1SSimon Wunderlich 	sband = sdata->local->hw.wiphy->bands[band];
15942103dec1SSimon Wunderlich 
15952103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
15962103dec1SSimon Wunderlich 	shift = ieee80211_vif_get_shift(&sdata->vif);
159746900298SJohannes Berg 
15984ee73f33SMichal Kazior 	if (WARN_ON(!sband))
15994ee73f33SMichal Kazior 		return 1;
160046900298SJohannes Berg 
160146900298SJohannes Berg 	num_rates = sband->n_bitrates;
160246900298SJohannes Berg 	supp_rates = 0;
160346900298SJohannes Berg 	for (i = 0; i < elems->supp_rates_len +
160446900298SJohannes Berg 		     elems->ext_supp_rates_len; i++) {
160546900298SJohannes Berg 		u8 rate = 0;
160646900298SJohannes Berg 		int own_rate;
16079ebb61a2SAshok Nagarajan 		bool is_basic;
160846900298SJohannes Berg 		if (i < elems->supp_rates_len)
160946900298SJohannes Berg 			rate = elems->supp_rates[i];
161046900298SJohannes Berg 		else if (elems->ext_supp_rates)
161146900298SJohannes Berg 			rate = elems->ext_supp_rates
161246900298SJohannes Berg 				[i - elems->supp_rates_len];
161346900298SJohannes Berg 		own_rate = 5 * (rate & 0x7f);
16149ebb61a2SAshok Nagarajan 		is_basic = !!(rate & 0x80);
16159ebb61a2SAshok Nagarajan 
16169ebb61a2SAshok Nagarajan 		if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
16179ebb61a2SAshok Nagarajan 			continue;
16189ebb61a2SAshok Nagarajan 
16199ebb61a2SAshok Nagarajan 		for (j = 0; j < num_rates; j++) {
16202103dec1SSimon Wunderlich 			int brate;
16212103dec1SSimon Wunderlich 			if ((rate_flags & sband->bitrates[j].flags)
16222103dec1SSimon Wunderlich 			    != rate_flags)
16232103dec1SSimon Wunderlich 				continue;
16242103dec1SSimon Wunderlich 
16252103dec1SSimon Wunderlich 			brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
16262103dec1SSimon Wunderlich 					     1 << shift);
16272103dec1SSimon Wunderlich 
16282103dec1SSimon Wunderlich 			if (brate == own_rate) {
162946900298SJohannes Berg 				supp_rates |= BIT(j);
16309ebb61a2SAshok Nagarajan 				if (basic_rates && is_basic)
16319ebb61a2SAshok Nagarajan 					*basic_rates |= BIT(j);
16329ebb61a2SAshok Nagarajan 			}
16339ebb61a2SAshok Nagarajan 		}
163446900298SJohannes Berg 	}
163546900298SJohannes Berg 	return supp_rates;
163646900298SJohannes Berg }
1637f2753ddbSJohannes Berg 
163884f6a01cSJohannes Berg void ieee80211_stop_device(struct ieee80211_local *local)
163984f6a01cSJohannes Berg {
164084f6a01cSJohannes Berg 	ieee80211_led_radio(local, false);
164167408c8cSJohannes Berg 	ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
164284f6a01cSJohannes Berg 
164384f6a01cSJohannes Berg 	cancel_work_sync(&local->reconfig_filter);
164484f6a01cSJohannes Berg 
164584f6a01cSJohannes Berg 	flush_workqueue(local->workqueue);
1646678f415fSLennert Buytenhek 	drv_stop(local);
164784f6a01cSJohannes Berg }
164884f6a01cSJohannes Berg 
164974430f94SJohannes Berg static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
165074430f94SJohannes Berg 					   bool aborted)
165174430f94SJohannes Berg {
165274430f94SJohannes Berg 	/* It's possible that we don't handle the scan completion in
165374430f94SJohannes Berg 	 * time during suspend, so if it's still marked as completed
165474430f94SJohannes Berg 	 * here, queue the work and flush it to clean things up.
165574430f94SJohannes Berg 	 * Instead of calling the worker function directly here, we
165674430f94SJohannes Berg 	 * really queue it to avoid potential races with other flows
165774430f94SJohannes Berg 	 * scheduling the same work.
165874430f94SJohannes Berg 	 */
165974430f94SJohannes Berg 	if (test_bit(SCAN_COMPLETED, &local->scanning)) {
166074430f94SJohannes Berg 		/* If coming from reconfiguration failure, abort the scan so
166174430f94SJohannes Berg 		 * we don't attempt to continue a partial HW scan - which is
166274430f94SJohannes Berg 		 * possible otherwise if (e.g.) the 2.4 GHz portion was the
166374430f94SJohannes Berg 		 * completed scan, and a 5 GHz portion is still pending.
166474430f94SJohannes Berg 		 */
166574430f94SJohannes Berg 		if (aborted)
166674430f94SJohannes Berg 			set_bit(SCAN_ABORTED, &local->scanning);
166774430f94SJohannes Berg 		ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
166874430f94SJohannes Berg 		flush_delayed_work(&local->scan_work);
166974430f94SJohannes Berg 	}
167074430f94SJohannes Berg }
167174430f94SJohannes Berg 
1672f6837ba8SJohannes Berg static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
1673f6837ba8SJohannes Berg {
1674f6837ba8SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
1675f6837ba8SJohannes Berg 	struct ieee80211_chanctx *ctx;
1676f6837ba8SJohannes Berg 
1677f6837ba8SJohannes Berg 	/*
1678f6837ba8SJohannes Berg 	 * We get here if during resume the device can't be restarted properly.
1679f6837ba8SJohannes Berg 	 * We might also get here if this happens during HW reset, which is a
1680f6837ba8SJohannes Berg 	 * slightly different situation and we need to drop all connections in
1681f6837ba8SJohannes Berg 	 * the latter case.
1682f6837ba8SJohannes Berg 	 *
1683f6837ba8SJohannes Berg 	 * Ask cfg80211 to turn off all interfaces, this will result in more
1684f6837ba8SJohannes Berg 	 * warnings but at least we'll then get into a clean stopped state.
1685f6837ba8SJohannes Berg 	 */
1686f6837ba8SJohannes Berg 
1687f6837ba8SJohannes Berg 	local->resuming = false;
1688f6837ba8SJohannes Berg 	local->suspended = false;
16897584f88fSEliad Peller 	local->in_reconfig = false;
1690f6837ba8SJohannes Berg 
169174430f94SJohannes Berg 	ieee80211_flush_completed_scan(local, true);
169274430f94SJohannes Berg 
1693f6837ba8SJohannes Berg 	/* scheduled scan clearly can't be running any more, but tell
1694f6837ba8SJohannes Berg 	 * cfg80211 and clear local state
1695f6837ba8SJohannes Berg 	 */
1696f6837ba8SJohannes Berg 	ieee80211_sched_scan_end(local);
1697f6837ba8SJohannes Berg 
1698f6837ba8SJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list)
1699f6837ba8SJohannes Berg 		sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
1700f6837ba8SJohannes Berg 
1701f6837ba8SJohannes Berg 	/* Mark channel contexts as not being in the driver any more to avoid
1702f6837ba8SJohannes Berg 	 * removing them from the driver during the shutdown process...
1703f6837ba8SJohannes Berg 	 */
1704f6837ba8SJohannes Berg 	mutex_lock(&local->chanctx_mtx);
1705f6837ba8SJohannes Berg 	list_for_each_entry(ctx, &local->chanctx_list, list)
1706f6837ba8SJohannes Berg 		ctx->driver_present = false;
1707f6837ba8SJohannes Berg 	mutex_unlock(&local->chanctx_mtx);
1708f6837ba8SJohannes Berg 
1709f6837ba8SJohannes Berg 	cfg80211_shutdown_all_interfaces(local->hw.wiphy);
1710f6837ba8SJohannes Berg }
1711f6837ba8SJohannes Berg 
1712153a5fc4SStanislaw Gruszka static void ieee80211_assign_chanctx(struct ieee80211_local *local,
1713153a5fc4SStanislaw Gruszka 				     struct ieee80211_sub_if_data *sdata)
1714153a5fc4SStanislaw Gruszka {
1715153a5fc4SStanislaw Gruszka 	struct ieee80211_chanctx_conf *conf;
1716153a5fc4SStanislaw Gruszka 	struct ieee80211_chanctx *ctx;
1717153a5fc4SStanislaw Gruszka 
1718153a5fc4SStanislaw Gruszka 	if (!local->use_chanctx)
1719153a5fc4SStanislaw Gruszka 		return;
1720153a5fc4SStanislaw Gruszka 
1721153a5fc4SStanislaw Gruszka 	mutex_lock(&local->chanctx_mtx);
1722153a5fc4SStanislaw Gruszka 	conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1723153a5fc4SStanislaw Gruszka 					 lockdep_is_held(&local->chanctx_mtx));
1724153a5fc4SStanislaw Gruszka 	if (conf) {
1725153a5fc4SStanislaw Gruszka 		ctx = container_of(conf, struct ieee80211_chanctx, conf);
1726153a5fc4SStanislaw Gruszka 		drv_assign_vif_chanctx(local, sdata, ctx);
1727153a5fc4SStanislaw Gruszka 	}
1728153a5fc4SStanislaw Gruszka 	mutex_unlock(&local->chanctx_mtx);
1729153a5fc4SStanislaw Gruszka }
1730153a5fc4SStanislaw Gruszka 
17311ea2c864SJohannes Berg static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
17321ea2c864SJohannes Berg {
17331ea2c864SJohannes Berg 	struct ieee80211_local *local = sdata->local;
17341ea2c864SJohannes Berg 	struct sta_info *sta;
17351ea2c864SJohannes Berg 
17361ea2c864SJohannes Berg 	/* add STAs back */
17371ea2c864SJohannes Berg 	mutex_lock(&local->sta_mtx);
17381ea2c864SJohannes Berg 	list_for_each_entry(sta, &local->sta_list, list) {
17391ea2c864SJohannes Berg 		enum ieee80211_sta_state state;
17401ea2c864SJohannes Berg 
17411ea2c864SJohannes Berg 		if (!sta->uploaded || sta->sdata != sdata)
17421ea2c864SJohannes Berg 			continue;
17431ea2c864SJohannes Berg 
17441ea2c864SJohannes Berg 		for (state = IEEE80211_STA_NOTEXIST;
17451ea2c864SJohannes Berg 		     state < sta->sta_state; state++)
17461ea2c864SJohannes Berg 			WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
17471ea2c864SJohannes Berg 					      state + 1));
17481ea2c864SJohannes Berg 	}
17491ea2c864SJohannes Berg 	mutex_unlock(&local->sta_mtx);
17501ea2c864SJohannes Berg }
17511ea2c864SJohannes Berg 
1752167e33f4SAyala Beker static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
1753167e33f4SAyala Beker {
1754167e33f4SAyala Beker 	struct cfg80211_nan_func *func, **funcs;
1755167e33f4SAyala Beker 	int res, id, i = 0;
1756167e33f4SAyala Beker 
1757167e33f4SAyala Beker 	res = drv_start_nan(sdata->local, sdata,
1758167e33f4SAyala Beker 			    &sdata->u.nan.conf);
1759167e33f4SAyala Beker 	if (WARN_ON(res))
1760167e33f4SAyala Beker 		return res;
1761167e33f4SAyala Beker 
1762167e33f4SAyala Beker 	funcs = kzalloc((sdata->local->hw.max_nan_de_entries + 1) *
1763167e33f4SAyala Beker 			sizeof(*funcs), GFP_KERNEL);
1764167e33f4SAyala Beker 	if (!funcs)
1765167e33f4SAyala Beker 		return -ENOMEM;
1766167e33f4SAyala Beker 
1767167e33f4SAyala Beker 	/* Add all the functions:
1768167e33f4SAyala Beker 	 * This is a little bit ugly. We need to call a potentially sleeping
1769167e33f4SAyala Beker 	 * callback for each NAN function, so we can't hold the spinlock.
1770167e33f4SAyala Beker 	 */
1771167e33f4SAyala Beker 	spin_lock_bh(&sdata->u.nan.func_lock);
1772167e33f4SAyala Beker 
1773167e33f4SAyala Beker 	idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
1774167e33f4SAyala Beker 		funcs[i++] = func;
1775167e33f4SAyala Beker 
1776167e33f4SAyala Beker 	spin_unlock_bh(&sdata->u.nan.func_lock);
1777167e33f4SAyala Beker 
1778167e33f4SAyala Beker 	for (i = 0; funcs[i]; i++) {
1779167e33f4SAyala Beker 		res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
1780167e33f4SAyala Beker 		if (WARN_ON(res))
1781167e33f4SAyala Beker 			ieee80211_nan_func_terminated(&sdata->vif,
1782167e33f4SAyala Beker 						      funcs[i]->instance_id,
1783167e33f4SAyala Beker 						      NL80211_NAN_FUNC_TERM_REASON_ERROR,
1784167e33f4SAyala Beker 						      GFP_KERNEL);
1785167e33f4SAyala Beker 	}
1786167e33f4SAyala Beker 
1787167e33f4SAyala Beker 	kfree(funcs);
1788167e33f4SAyala Beker 
1789167e33f4SAyala Beker 	return 0;
1790167e33f4SAyala Beker }
1791167e33f4SAyala Beker 
1792f2753ddbSJohannes Berg int ieee80211_reconfig(struct ieee80211_local *local)
1793f2753ddbSJohannes Berg {
1794f2753ddbSJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
1795f2753ddbSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
179655de908aSJohannes Berg 	struct ieee80211_chanctx *ctx;
1797f2753ddbSJohannes Berg 	struct sta_info *sta;
17982683d65bSEliad Peller 	int res, i;
1799d888130aSJohannes Berg 	bool reconfig_due_to_wowlan = false;
1800d43c6b6eSDavid Spinadel 	struct ieee80211_sub_if_data *sched_scan_sdata;
18016ea0a69cSJohannes Berg 	struct cfg80211_sched_scan_request *sched_scan_req;
1802d43c6b6eSDavid Spinadel 	bool sched_scan_stopped = false;
1803b0485e9fSEliad Peller 	bool suspended = local->suspended;
1804d888130aSJohannes Berg 
18050f8b8245SEliad Peller 	/* nothing to do if HW shouldn't run */
18060f8b8245SEliad Peller 	if (!local->open_count)
18070f8b8245SEliad Peller 		goto wake_up;
18080f8b8245SEliad Peller 
18098f21b0adSJohannes Berg #ifdef CONFIG_PM
1810b0485e9fSEliad Peller 	if (suspended)
1811ceb99fe0SJohannes Berg 		local->resuming = true;
1812f2753ddbSJohannes Berg 
1813eecc4800SJohannes Berg 	if (local->wowlan) {
1814b0485e9fSEliad Peller 		/*
1815b0485e9fSEliad Peller 		 * In the wowlan case, both mac80211 and the device
1816b0485e9fSEliad Peller 		 * are functional when the resume op is called, so
1817b0485e9fSEliad Peller 		 * clear local->suspended so the device could operate
1818b0485e9fSEliad Peller 		 * normally (e.g. pass rx frames).
1819b0485e9fSEliad Peller 		 */
1820b0485e9fSEliad Peller 		local->suspended = false;
1821eecc4800SJohannes Berg 		res = drv_resume(local);
182227b3eb9cSJohannes Berg 		local->wowlan = false;
1823eecc4800SJohannes Berg 		if (res < 0) {
1824eecc4800SJohannes Berg 			local->resuming = false;
1825eecc4800SJohannes Berg 			return res;
1826eecc4800SJohannes Berg 		}
1827eecc4800SJohannes Berg 		if (res == 0)
1828eecc4800SJohannes Berg 			goto wake_up;
1829eecc4800SJohannes Berg 		WARN_ON(res > 1);
1830eecc4800SJohannes Berg 		/*
1831eecc4800SJohannes Berg 		 * res is 1, which means the driver requested
1832eecc4800SJohannes Berg 		 * to go through a regular reset on wakeup.
1833b0485e9fSEliad Peller 		 * restore local->suspended in this case.
1834eecc4800SJohannes Berg 		 */
1835d888130aSJohannes Berg 		reconfig_due_to_wowlan = true;
1836b0485e9fSEliad Peller 		local->suspended = true;
1837eecc4800SJohannes Berg 	}
1838eecc4800SJohannes Berg #endif
183994f9b97bSJohannes Berg 
184024feda00SLuis R. Rodriguez 	/*
184143d6df00SEliad Peller 	 * In case of hw_restart during suspend (without wowlan),
184243d6df00SEliad Peller 	 * cancel restart work, as we are reconfiguring the device
184343d6df00SEliad Peller 	 * anyway.
184443d6df00SEliad Peller 	 * Note that restart_work is scheduled on a frozen workqueue,
184543d6df00SEliad Peller 	 * so we can't deadlock in this case.
184643d6df00SEliad Peller 	 */
184743d6df00SEliad Peller 	if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
184843d6df00SEliad Peller 		cancel_work_sync(&local->restart_work);
184943d6df00SEliad Peller 
1850968a76ceSEliad Peller 	local->started = false;
1851968a76ceSEliad Peller 
185243d6df00SEliad Peller 	/*
185324feda00SLuis R. Rodriguez 	 * Upon resume hardware can sometimes be goofy due to
185424feda00SLuis R. Rodriguez 	 * various platform / driver / bus issues, so restarting
185524feda00SLuis R. Rodriguez 	 * the device may at times not work immediately. Propagate
185624feda00SLuis R. Rodriguez 	 * the error.
185724feda00SLuis R. Rodriguez 	 */
185824487981SJohannes Berg 	res = drv_start(local);
185924feda00SLuis R. Rodriguez 	if (res) {
1860b0485e9fSEliad Peller 		if (suspended)
1861f6837ba8SJohannes Berg 			WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
1862f6837ba8SJohannes Berg 		else
1863f6837ba8SJohannes Berg 			WARN(1, "Hardware became unavailable during restart.\n");
1864f6837ba8SJohannes Berg 		ieee80211_handle_reconfig_failure(local);
186524feda00SLuis R. Rodriguez 		return res;
186624feda00SLuis R. Rodriguez 	}
1867f2753ddbSJohannes Berg 
18687f281975SYogesh Ashok Powar 	/* setup fragmentation threshold */
18697f281975SYogesh Ashok Powar 	drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
18707f281975SYogesh Ashok Powar 
18717f281975SYogesh Ashok Powar 	/* setup RTS threshold */
18727f281975SYogesh Ashok Powar 	drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
18737f281975SYogesh Ashok Powar 
18747f281975SYogesh Ashok Powar 	/* reset coverage class */
18757f281975SYogesh Ashok Powar 	drv_set_coverage_class(local, hw->wiphy->coverage_class);
18767f281975SYogesh Ashok Powar 
18771f87f7d3SJohannes Berg 	ieee80211_led_radio(local, true);
187867408c8cSJohannes Berg 	ieee80211_mod_tpt_led_trig(local,
187967408c8cSJohannes Berg 				   IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1880f2753ddbSJohannes Berg 
1881f2753ddbSJohannes Berg 	/* add interfaces */
18824b6f1dd6SJohannes Berg 	sdata = rtnl_dereference(local->monitor_sdata);
18834b6f1dd6SJohannes Berg 	if (sdata) {
18843c3e21e7SJohannes Berg 		/* in HW restart it exists already */
18853c3e21e7SJohannes Berg 		WARN_ON(local->resuming);
18864b6f1dd6SJohannes Berg 		res = drv_add_interface(local, sdata);
18874b6f1dd6SJohannes Berg 		if (WARN_ON(res)) {
18880c2bef46SMonam Agarwal 			RCU_INIT_POINTER(local->monitor_sdata, NULL);
18894b6f1dd6SJohannes Berg 			synchronize_net();
18904b6f1dd6SJohannes Berg 			kfree(sdata);
18914b6f1dd6SJohannes Berg 		}
18924b6f1dd6SJohannes Berg 	}
18934b6f1dd6SJohannes Berg 
1894f2753ddbSJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
1895f2753ddbSJohannes Berg 		if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1896f2753ddbSJohannes Berg 		    sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1897c8fff3dcSLuciano Coelho 		    ieee80211_sdata_running(sdata)) {
18987b7eab6fSJohannes Berg 			res = drv_add_interface(local, sdata);
1899c8fff3dcSLuciano Coelho 			if (WARN_ON(res))
1900c8fff3dcSLuciano Coelho 				break;
1901c8fff3dcSLuciano Coelho 		}
1902c8fff3dcSLuciano Coelho 	}
1903c8fff3dcSLuciano Coelho 
1904c8fff3dcSLuciano Coelho 	/* If adding any of the interfaces failed above, roll back and
1905c8fff3dcSLuciano Coelho 	 * report failure.
1906c8fff3dcSLuciano Coelho 	 */
1907c8fff3dcSLuciano Coelho 	if (res) {
1908c8fff3dcSLuciano Coelho 		list_for_each_entry_continue_reverse(sdata, &local->interfaces,
1909c8fff3dcSLuciano Coelho 						     list)
1910c8fff3dcSLuciano Coelho 			if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1911c8fff3dcSLuciano Coelho 			    sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1912c8fff3dcSLuciano Coelho 			    ieee80211_sdata_running(sdata))
1913c8fff3dcSLuciano Coelho 				drv_remove_interface(local, sdata);
1914c8fff3dcSLuciano Coelho 		ieee80211_handle_reconfig_failure(local);
1915c8fff3dcSLuciano Coelho 		return res;
1916f2753ddbSJohannes Berg 	}
1917f2753ddbSJohannes Berg 
191855de908aSJohannes Berg 	/* add channel contexts */
1919f0dea9c7SArend van Spriel 	if (local->use_chanctx) {
192055de908aSJohannes Berg 		mutex_lock(&local->chanctx_mtx);
192155de908aSJohannes Berg 		list_for_each_entry(ctx, &local->chanctx_list, list)
19225bcae31dSMichal Kazior 			if (ctx->replace_state !=
19235bcae31dSMichal Kazior 			    IEEE80211_CHANCTX_REPLACES_OTHER)
192455de908aSJohannes Berg 				WARN_ON(drv_add_chanctx(local, ctx));
192555de908aSJohannes Berg 		mutex_unlock(&local->chanctx_mtx);
192655de908aSJohannes Berg 
1927fe5f2559SJohannes Berg 		sdata = rtnl_dereference(local->monitor_sdata);
1928153a5fc4SStanislaw Gruszka 		if (sdata && ieee80211_sdata_running(sdata))
1929153a5fc4SStanislaw Gruszka 			ieee80211_assign_chanctx(local, sdata);
19307df180f7SZhao, Gang 	}
1931fe5f2559SJohannes Berg 
1932f2753ddbSJohannes Berg 	/* reconfigure hardware */
1933f2753ddbSJohannes Berg 	ieee80211_hw_config(local, ~0);
1934f2753ddbSJohannes Berg 
1935f2753ddbSJohannes Berg 	ieee80211_configure_filter(local);
1936f2753ddbSJohannes Berg 
1937f2753ddbSJohannes Berg 	/* Finally also reconfigure all the BSS information */
1938f2753ddbSJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
1939ac8dd506SJohannes Berg 		u32 changed;
1940ac8dd506SJohannes Berg 
19419607e6b6SJohannes Berg 		if (!ieee80211_sdata_running(sdata))
1942f2753ddbSJohannes Berg 			continue;
1943ac8dd506SJohannes Berg 
19441ea2c864SJohannes Berg 		ieee80211_assign_chanctx(local, sdata);
19451ea2c864SJohannes Berg 
19461ea2c864SJohannes Berg 		switch (sdata->vif.type) {
19471ea2c864SJohannes Berg 		case NL80211_IFTYPE_AP_VLAN:
19481ea2c864SJohannes Berg 		case NL80211_IFTYPE_MONITOR:
19491ea2c864SJohannes Berg 			break;
19501ea2c864SJohannes Berg 		default:
19511ea2c864SJohannes Berg 			ieee80211_reconfig_stations(sdata);
19521ea2c864SJohannes Berg 			/* fall through */
19531ea2c864SJohannes Berg 		case NL80211_IFTYPE_AP: /* AP stations are handled later */
19541ea2c864SJohannes Berg 			for (i = 0; i < IEEE80211_NUM_ACS; i++)
19551ea2c864SJohannes Berg 				drv_conf_tx(local, sdata, i,
19561ea2c864SJohannes Berg 					    &sdata->tx_conf[i]);
19571ea2c864SJohannes Berg 			break;
19581ea2c864SJohannes Berg 		}
19591ea2c864SJohannes Berg 
1960ac8dd506SJohannes Berg 		/* common change flags for all interface types */
1961ac8dd506SJohannes Berg 		changed = BSS_CHANGED_ERP_CTS_PROT |
1962ac8dd506SJohannes Berg 			  BSS_CHANGED_ERP_PREAMBLE |
1963ac8dd506SJohannes Berg 			  BSS_CHANGED_ERP_SLOT |
1964ac8dd506SJohannes Berg 			  BSS_CHANGED_HT |
1965ac8dd506SJohannes Berg 			  BSS_CHANGED_BASIC_RATES |
1966ac8dd506SJohannes Berg 			  BSS_CHANGED_BEACON_INT |
1967ac8dd506SJohannes Berg 			  BSS_CHANGED_BSSID |
19684ced3f74SJohannes Berg 			  BSS_CHANGED_CQM |
196955de47f6SEliad Peller 			  BSS_CHANGED_QOS |
19701ea6f9c0SJohannes Berg 			  BSS_CHANGED_IDLE |
19711ea6f9c0SJohannes Berg 			  BSS_CHANGED_TXPOWER;
1972ac8dd506SJohannes Berg 
1973b5a33d52SSara Sharon 		if (sdata->vif.mu_mimo_owner)
197423a1f8d4SSara Sharon 			changed |= BSS_CHANGED_MU_GROUPS;
197523a1f8d4SSara Sharon 
1976f2753ddbSJohannes Berg 		switch (sdata->vif.type) {
1977f2753ddbSJohannes Berg 		case NL80211_IFTYPE_STATION:
19780d392e93SEliad Peller 			changed |= BSS_CHANGED_ASSOC |
1979ab095877SEliad Peller 				   BSS_CHANGED_ARP_FILTER |
1980ab095877SEliad Peller 				   BSS_CHANGED_PS;
1981c65dd147SEmmanuel Grumbach 
1982989c6505SAlexander Bondar 			/* Re-send beacon info report to the driver */
1983989c6505SAlexander Bondar 			if (sdata->u.mgd.have_beacon)
1984989c6505SAlexander Bondar 				changed |= BSS_CHANGED_BEACON_INFO;
1985c65dd147SEmmanuel Grumbach 
19868d61ffa5SJohannes Berg 			sdata_lock(sdata);
1987ac8dd506SJohannes Berg 			ieee80211_bss_info_change_notify(sdata, changed);
19888d61ffa5SJohannes Berg 			sdata_unlock(sdata);
1989ac8dd506SJohannes Berg 			break;
19906e0bd6c3SRostislav Lisovy 		case NL80211_IFTYPE_OCB:
1991239281f8SRostislav Lisovy 			changed |= BSS_CHANGED_OCB;
1992239281f8SRostislav Lisovy 			ieee80211_bss_info_change_notify(sdata, changed);
19936e0bd6c3SRostislav Lisovy 			break;
1994f2753ddbSJohannes Berg 		case NL80211_IFTYPE_ADHOC:
1995ac8dd506SJohannes Berg 			changed |= BSS_CHANGED_IBSS;
1996ac8dd506SJohannes Berg 			/* fall through */
1997f2753ddbSJohannes Berg 		case NL80211_IFTYPE_AP:
1998339afbf4SJohannes Berg 			changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
1999e7979ac7SArik Nemtsov 
20001041638fSJohannes Berg 			if (sdata->vif.type == NL80211_IFTYPE_AP) {
2001e7979ac7SArik Nemtsov 				changed |= BSS_CHANGED_AP_PROBE_RESP;
2002e7979ac7SArik Nemtsov 
20031041638fSJohannes Berg 				if (rcu_access_pointer(sdata->u.ap.beacon))
20041041638fSJohannes Berg 					drv_start_ap(local, sdata);
20051041638fSJohannes Berg 			}
20061041638fSJohannes Berg 
20077827493bSArik Nemtsov 			/* fall through */
2008f2753ddbSJohannes Berg 		case NL80211_IFTYPE_MESH_POINT:
20098da34932SJohannes Berg 			if (sdata->vif.bss_conf.enable_beacon) {
2010ac8dd506SJohannes Berg 				changed |= BSS_CHANGED_BEACON |
2011ac8dd506SJohannes Berg 					   BSS_CHANGED_BEACON_ENABLED;
20122d0ddec5SJohannes Berg 				ieee80211_bss_info_change_notify(sdata, changed);
20138da34932SJohannes Berg 			}
2014f2753ddbSJohannes Berg 			break;
2015167e33f4SAyala Beker 		case NL80211_IFTYPE_NAN:
2016167e33f4SAyala Beker 			res = ieee80211_reconfig_nan(sdata);
2017167e33f4SAyala Beker 			if (res < 0) {
2018167e33f4SAyala Beker 				ieee80211_handle_reconfig_failure(local);
2019167e33f4SAyala Beker 				return res;
2020167e33f4SAyala Beker 			}
2021167e33f4SAyala Beker 			break;
2022f2753ddbSJohannes Berg 		case NL80211_IFTYPE_WDS:
2023f2753ddbSJohannes Berg 		case NL80211_IFTYPE_AP_VLAN:
2024f2753ddbSJohannes Berg 		case NL80211_IFTYPE_MONITOR:
202598104fdeSJohannes Berg 		case NL80211_IFTYPE_P2P_DEVICE:
2026b205786eSZhao, Gang 			/* nothing to do */
2027f142c6b9SJohannes Berg 			break;
2028f2753ddbSJohannes Berg 		case NL80211_IFTYPE_UNSPECIFIED:
20292e161f78SJohannes Berg 		case NUM_NL80211_IFTYPES:
20302ca27bcfSJohannes Berg 		case NL80211_IFTYPE_P2P_CLIENT:
20312ca27bcfSJohannes Berg 		case NL80211_IFTYPE_P2P_GO:
2032f2753ddbSJohannes Berg 			WARN_ON(1);
2033f2753ddbSJohannes Berg 			break;
2034f2753ddbSJohannes Berg 		}
2035f2753ddbSJohannes Berg 	}
2036f2753ddbSJohannes Berg 
20374a733ef1SJohannes Berg 	ieee80211_recalc_ps(local);
20388e1b23b9SEyal Shapira 
20392a419056SJohannes Berg 	/*
20406e1b1b24SEliad Peller 	 * The sta might be in psm against the ap (e.g. because
20416e1b1b24SEliad Peller 	 * this was the state before a hw restart), so we
20426e1b1b24SEliad Peller 	 * explicitly send a null packet in order to make sure
20436e1b1b24SEliad Peller 	 * it'll sync against the ap (and get out of psm).
20446e1b1b24SEliad Peller 	 */
20456e1b1b24SEliad Peller 	if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
20466e1b1b24SEliad Peller 		list_for_each_entry(sdata, &local->interfaces, list) {
20476e1b1b24SEliad Peller 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
20486e1b1b24SEliad Peller 				continue;
204920f544eeSJohannes Berg 			if (!sdata->u.mgd.associated)
205020f544eeSJohannes Berg 				continue;
20516e1b1b24SEliad Peller 
2052076cdcb1SJohannes Berg 			ieee80211_send_nullfunc(local, sdata, false);
20536e1b1b24SEliad Peller 		}
20546e1b1b24SEliad Peller 	}
20556e1b1b24SEliad Peller 
20562e8d397eSArik Nemtsov 	/* APs are now beaconing, add back stations */
20572e8d397eSArik Nemtsov 	mutex_lock(&local->sta_mtx);
20582e8d397eSArik Nemtsov 	list_for_each_entry(sta, &local->sta_list, list) {
20592e8d397eSArik Nemtsov 		enum ieee80211_sta_state state;
20602e8d397eSArik Nemtsov 
20612e8d397eSArik Nemtsov 		if (!sta->uploaded)
20622e8d397eSArik Nemtsov 			continue;
20632e8d397eSArik Nemtsov 
20642e8d397eSArik Nemtsov 		if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
20652e8d397eSArik Nemtsov 			continue;
20662e8d397eSArik Nemtsov 
20672e8d397eSArik Nemtsov 		for (state = IEEE80211_STA_NOTEXIST;
20682e8d397eSArik Nemtsov 		     state < sta->sta_state; state++)
20692e8d397eSArik Nemtsov 			WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
20702e8d397eSArik Nemtsov 					      state + 1));
20712e8d397eSArik Nemtsov 	}
20722e8d397eSArik Nemtsov 	mutex_unlock(&local->sta_mtx);
20732e8d397eSArik Nemtsov 
20747b21aea0SEyal Shapira 	/* add back keys */
20757b21aea0SEyal Shapira 	list_for_each_entry(sdata, &local->interfaces, list)
2076f9dca80bSMichal Kazior 		ieee80211_reset_crypto_tx_tailroom(sdata);
2077f9dca80bSMichal Kazior 
2078f9dca80bSMichal Kazior 	list_for_each_entry(sdata, &local->interfaces, list)
20797b21aea0SEyal Shapira 		if (ieee80211_sdata_running(sdata))
20807b21aea0SEyal Shapira 			ieee80211_enable_keys(sdata);
20817b21aea0SEyal Shapira 
20820d440ea2SEliad Peller 	/* Reconfigure sched scan if it was interrupted by FW restart */
20830d440ea2SEliad Peller 	mutex_lock(&local->mtx);
20840d440ea2SEliad Peller 	sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
20850d440ea2SEliad Peller 						lockdep_is_held(&local->mtx));
20860d440ea2SEliad Peller 	sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
20870d440ea2SEliad Peller 						lockdep_is_held(&local->mtx));
20880d440ea2SEliad Peller 	if (sched_scan_sdata && sched_scan_req)
20890d440ea2SEliad Peller 		/*
20900d440ea2SEliad Peller 		 * Sched scan stopped, but we don't want to report it. Instead,
20910d440ea2SEliad Peller 		 * we're trying to reschedule. However, if more than one scan
20920d440ea2SEliad Peller 		 * plan was set, we cannot reschedule since we don't know which
20930d440ea2SEliad Peller 		 * scan plan was currently running (and some scan plans may have
20940d440ea2SEliad Peller 		 * already finished).
20950d440ea2SEliad Peller 		 */
20960d440ea2SEliad Peller 		if (sched_scan_req->n_scan_plans > 1 ||
20970d440ea2SEliad Peller 		    __ieee80211_request_sched_scan_start(sched_scan_sdata,
2098b9f628fcSEliad Peller 							 sched_scan_req)) {
2099b9f628fcSEliad Peller 			RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
2100b9f628fcSEliad Peller 			RCU_INIT_POINTER(local->sched_scan_req, NULL);
21010d440ea2SEliad Peller 			sched_scan_stopped = true;
2102b9f628fcSEliad Peller 		}
21030d440ea2SEliad Peller 	mutex_unlock(&local->mtx);
21040d440ea2SEliad Peller 
21050d440ea2SEliad Peller 	if (sched_scan_stopped)
21060d440ea2SEliad Peller 		cfg80211_sched_scan_stopped_rtnl(local->hw.wiphy);
21070d440ea2SEliad Peller 
2108c6209488SEliad Peller  wake_up:
2109470f4d61SEliad Peller 	if (local->in_reconfig) {
211004800adaSArik Nemtsov 		local->in_reconfig = false;
211104800adaSArik Nemtsov 		barrier();
211204800adaSArik Nemtsov 
2113470f4d61SEliad Peller 		/* Restart deferred ROCs */
2114470f4d61SEliad Peller 		mutex_lock(&local->mtx);
2115470f4d61SEliad Peller 		ieee80211_start_next_roc(local);
2116470f4d61SEliad Peller 		mutex_unlock(&local->mtx);
2117470f4d61SEliad Peller 	}
2118470f4d61SEliad Peller 
21193c3e21e7SJohannes Berg 	if (local->monitors == local->open_count && local->monitors > 0)
21203c3e21e7SJohannes Berg 		ieee80211_add_virtual_monitor(local);
21213c3e21e7SJohannes Berg 
21226e1b1b24SEliad Peller 	/*
21232a419056SJohannes Berg 	 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
21242a419056SJohannes Berg 	 * sessions can be established after a resume.
21252a419056SJohannes Berg 	 *
21262a419056SJohannes Berg 	 * Also tear down aggregation sessions since reconfiguring
21272a419056SJohannes Berg 	 * them in a hardware restart scenario is not easily done
21282a419056SJohannes Berg 	 * right now, and the hardware will have lost information
21292a419056SJohannes Berg 	 * about the sessions, but we and the AP still think they
21302a419056SJohannes Berg 	 * are active. This is really a workaround though.
21312a419056SJohannes Berg 	 */
213230686bf7SJohannes Berg 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
21332a419056SJohannes Berg 		mutex_lock(&local->sta_mtx);
21342a419056SJohannes Berg 
21352a419056SJohannes Berg 		list_for_each_entry(sta, &local->sta_list, list) {
213627392719SEliad Peller 			if (!local->resuming)
2137c82c4a80SJohannes Berg 				ieee80211_sta_tear_down_BA_sessions(
2138c82c4a80SJohannes Berg 						sta, AGG_STOP_LOCAL_REQUEST);
2139c2c98fdeSJohannes Berg 			clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
214074e2bd1fSWey-Yi Guy 		}
21412a419056SJohannes Berg 
21422a419056SJohannes Berg 		mutex_unlock(&local->sta_mtx);
214374e2bd1fSWey-Yi Guy 	}
214474e2bd1fSWey-Yi Guy 
2145445ea4e8SJohannes Berg 	ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
2146cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
2147cca07b00SLuciano Coelho 					false);
2148f2753ddbSJohannes Berg 
21495bb644a0SJohannes Berg 	/*
21505bb644a0SJohannes Berg 	 * If this is for hw restart things are still running.
21515bb644a0SJohannes Berg 	 * We may want to change that later, however.
21525bb644a0SJohannes Berg 	 */
2153b0485e9fSEliad Peller 	if (local->open_count && (!suspended || reconfig_due_to_wowlan))
2154cf2c92d8SEliad Peller 		drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
21558f21b0adSJohannes Berg 
2156b0485e9fSEliad Peller 	if (!suspended)
21575bb644a0SJohannes Berg 		return 0;
21585bb644a0SJohannes Berg 
21595bb644a0SJohannes Berg #ifdef CONFIG_PM
2160ceb99fe0SJohannes Berg 	/* first set suspended false, then resuming */
21615bb644a0SJohannes Berg 	local->suspended = false;
2162ceb99fe0SJohannes Berg 	mb();
2163ceb99fe0SJohannes Berg 	local->resuming = false;
21645bb644a0SJohannes Berg 
216574430f94SJohannes Berg 	ieee80211_flush_completed_scan(local, false);
21669120d94eSLuciano Coelho 
21670f8b8245SEliad Peller 	if (local->open_count && !reconfig_due_to_wowlan)
2168cf2c92d8SEliad Peller 		drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
2169cf2c92d8SEliad Peller 
2170b8360ab8SJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
2171b8360ab8SJohannes Berg 		if (!ieee80211_sdata_running(sdata))
2172b8360ab8SJohannes Berg 			continue;
2173b8360ab8SJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_STATION)
2174b8360ab8SJohannes Berg 			ieee80211_sta_restart(sdata);
2175b8360ab8SJohannes Berg 	}
2176b8360ab8SJohannes Berg 
217726d59535SJohannes Berg 	mod_timer(&local->sta_cleanup, jiffies + 1);
21785bb644a0SJohannes Berg #else
21795bb644a0SJohannes Berg 	WARN_ON(1);
21805bb644a0SJohannes Berg #endif
2181d43c6b6eSDavid Spinadel 
2182f2753ddbSJohannes Berg 	return 0;
2183f2753ddbSJohannes Berg }
218442935ecaSLuis R. Rodriguez 
218595acac61SJohannes Berg void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
218695acac61SJohannes Berg {
218795acac61SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
218895acac61SJohannes Berg 	struct ieee80211_local *local;
218995acac61SJohannes Berg 	struct ieee80211_key *key;
219095acac61SJohannes Berg 
219195acac61SJohannes Berg 	if (WARN_ON(!vif))
219295acac61SJohannes Berg 		return;
219395acac61SJohannes Berg 
219495acac61SJohannes Berg 	sdata = vif_to_sdata(vif);
219595acac61SJohannes Berg 	local = sdata->local;
219695acac61SJohannes Berg 
219795acac61SJohannes Berg 	if (WARN_ON(!local->resuming))
219895acac61SJohannes Berg 		return;
219995acac61SJohannes Berg 
220095acac61SJohannes Berg 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
220195acac61SJohannes Berg 		return;
220295acac61SJohannes Berg 
220395acac61SJohannes Berg 	sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
220495acac61SJohannes Berg 
220595acac61SJohannes Berg 	mutex_lock(&local->key_mtx);
220695acac61SJohannes Berg 	list_for_each_entry(key, &sdata->key_list, list)
220795acac61SJohannes Berg 		key->flags |= KEY_FLAG_TAINTED;
220895acac61SJohannes Berg 	mutex_unlock(&local->key_mtx);
220995acac61SJohannes Berg }
221095acac61SJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
221195acac61SJohannes Berg 
221204ecd257SJohannes Berg void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
22130f78231bSJohannes Berg {
221404ecd257SJohannes Berg 	struct ieee80211_local *local = sdata->local;
221504ecd257SJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
221604ecd257SJohannes Berg 	struct ieee80211_chanctx *chanctx;
22170f78231bSJohannes Berg 
221804ecd257SJohannes Berg 	mutex_lock(&local->chanctx_mtx);
22190f78231bSJohannes Berg 
222004ecd257SJohannes Berg 	chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
222104ecd257SJohannes Berg 					lockdep_is_held(&local->chanctx_mtx));
22220f78231bSJohannes Berg 
2223c189a685SAndrei Otcheretianski 	/*
2224c189a685SAndrei Otcheretianski 	 * This function can be called from a work, thus it may be possible
2225c189a685SAndrei Otcheretianski 	 * that the chanctx_conf is removed (due to a disconnection, for
2226c189a685SAndrei Otcheretianski 	 * example).
2227c189a685SAndrei Otcheretianski 	 * So nothing should be done in such case.
2228c189a685SAndrei Otcheretianski 	 */
2229c189a685SAndrei Otcheretianski 	if (!chanctx_conf)
22305d8e4237SJohannes Berg 		goto unlock;
22310f78231bSJohannes Berg 
223204ecd257SJohannes Berg 	chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
223304ecd257SJohannes Berg 	ieee80211_recalc_smps_chanctx(local, chanctx);
22345d8e4237SJohannes Berg  unlock:
223504ecd257SJohannes Berg 	mutex_unlock(&local->chanctx_mtx);
22360f78231bSJohannes Berg }
22378e664fb3SJohannes Berg 
223821f659bfSEliad Peller void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata)
223921f659bfSEliad Peller {
224021f659bfSEliad Peller 	struct ieee80211_local *local = sdata->local;
224121f659bfSEliad Peller 	struct ieee80211_chanctx_conf *chanctx_conf;
224221f659bfSEliad Peller 	struct ieee80211_chanctx *chanctx;
224321f659bfSEliad Peller 
224421f659bfSEliad Peller 	mutex_lock(&local->chanctx_mtx);
224521f659bfSEliad Peller 
224621f659bfSEliad Peller 	chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
224721f659bfSEliad Peller 					lockdep_is_held(&local->chanctx_mtx));
224821f659bfSEliad Peller 
224921f659bfSEliad Peller 	if (WARN_ON_ONCE(!chanctx_conf))
225021f659bfSEliad Peller 		goto unlock;
225121f659bfSEliad Peller 
225221f659bfSEliad Peller 	chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
225321f659bfSEliad Peller 	ieee80211_recalc_chanctx_min_def(local, chanctx);
225421f659bfSEliad Peller  unlock:
225521f659bfSEliad Peller 	mutex_unlock(&local->chanctx_mtx);
225621f659bfSEliad Peller }
225721f659bfSEliad Peller 
22588e664fb3SJohannes Berg size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
22598e664fb3SJohannes Berg {
22608e664fb3SJohannes Berg 	size_t pos = offset;
22618e664fb3SJohannes Berg 
22628e664fb3SJohannes Berg 	while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
22638e664fb3SJohannes Berg 		pos += 2 + ies[pos + 1];
22648e664fb3SJohannes Berg 
22658e664fb3SJohannes Berg 	return pos;
22668e664fb3SJohannes Berg }
2267615f7b9bSMeenakshi Venkataraman 
2268615f7b9bSMeenakshi Venkataraman static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
2269615f7b9bSMeenakshi Venkataraman 					    int rssi_min_thold,
2270615f7b9bSMeenakshi Venkataraman 					    int rssi_max_thold)
2271615f7b9bSMeenakshi Venkataraman {
2272615f7b9bSMeenakshi Venkataraman 	trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
2273615f7b9bSMeenakshi Venkataraman 
2274615f7b9bSMeenakshi Venkataraman 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2275615f7b9bSMeenakshi Venkataraman 		return;
2276615f7b9bSMeenakshi Venkataraman 
2277615f7b9bSMeenakshi Venkataraman 	/*
2278615f7b9bSMeenakshi Venkataraman 	 * Scale up threshold values before storing it, as the RSSI averaging
2279615f7b9bSMeenakshi Venkataraman 	 * algorithm uses a scaled up value as well. Change this scaling
2280615f7b9bSMeenakshi Venkataraman 	 * factor if the RSSI averaging algorithm changes.
2281615f7b9bSMeenakshi Venkataraman 	 */
2282615f7b9bSMeenakshi Venkataraman 	sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
2283615f7b9bSMeenakshi Venkataraman 	sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
2284615f7b9bSMeenakshi Venkataraman }
2285615f7b9bSMeenakshi Venkataraman 
2286615f7b9bSMeenakshi Venkataraman void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
2287615f7b9bSMeenakshi Venkataraman 				    int rssi_min_thold,
2288615f7b9bSMeenakshi Venkataraman 				    int rssi_max_thold)
2289615f7b9bSMeenakshi Venkataraman {
2290615f7b9bSMeenakshi Venkataraman 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2291615f7b9bSMeenakshi Venkataraman 
2292615f7b9bSMeenakshi Venkataraman 	WARN_ON(rssi_min_thold == rssi_max_thold ||
2293615f7b9bSMeenakshi Venkataraman 		rssi_min_thold > rssi_max_thold);
2294615f7b9bSMeenakshi Venkataraman 
2295615f7b9bSMeenakshi Venkataraman 	_ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
2296615f7b9bSMeenakshi Venkataraman 				       rssi_max_thold);
2297615f7b9bSMeenakshi Venkataraman }
2298615f7b9bSMeenakshi Venkataraman EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
2299615f7b9bSMeenakshi Venkataraman 
2300615f7b9bSMeenakshi Venkataraman void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
2301615f7b9bSMeenakshi Venkataraman {
2302615f7b9bSMeenakshi Venkataraman 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2303615f7b9bSMeenakshi Venkataraman 
2304615f7b9bSMeenakshi Venkataraman 	_ieee80211_enable_rssi_reports(sdata, 0, 0);
2305615f7b9bSMeenakshi Venkataraman }
2306615f7b9bSMeenakshi Venkataraman EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
2307768db343SArik Nemtsov 
2308ef96a842SBen Greear u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
230942e7aa77SAlexander Simon 			      u16 cap)
231042e7aa77SAlexander Simon {
231142e7aa77SAlexander Simon 	__le16 tmp;
231242e7aa77SAlexander Simon 
231342e7aa77SAlexander Simon 	*pos++ = WLAN_EID_HT_CAPABILITY;
231442e7aa77SAlexander Simon 	*pos++ = sizeof(struct ieee80211_ht_cap);
231542e7aa77SAlexander Simon 	memset(pos, 0, sizeof(struct ieee80211_ht_cap));
231642e7aa77SAlexander Simon 
231742e7aa77SAlexander Simon 	/* capability flags */
231842e7aa77SAlexander Simon 	tmp = cpu_to_le16(cap);
231942e7aa77SAlexander Simon 	memcpy(pos, &tmp, sizeof(u16));
232042e7aa77SAlexander Simon 	pos += sizeof(u16);
232142e7aa77SAlexander Simon 
232242e7aa77SAlexander Simon 	/* AMPDU parameters */
2323ef96a842SBen Greear 	*pos++ = ht_cap->ampdu_factor |
2324ef96a842SBen Greear 		 (ht_cap->ampdu_density <<
232542e7aa77SAlexander Simon 			IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
232642e7aa77SAlexander Simon 
232742e7aa77SAlexander Simon 	/* MCS set */
2328ef96a842SBen Greear 	memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
2329ef96a842SBen Greear 	pos += sizeof(ht_cap->mcs);
233042e7aa77SAlexander Simon 
233142e7aa77SAlexander Simon 	/* extended capabilities */
233242e7aa77SAlexander Simon 	pos += sizeof(__le16);
233342e7aa77SAlexander Simon 
233442e7aa77SAlexander Simon 	/* BF capabilities */
233542e7aa77SAlexander Simon 	pos += sizeof(__le32);
233642e7aa77SAlexander Simon 
233742e7aa77SAlexander Simon 	/* antenna selection */
233842e7aa77SAlexander Simon 	pos += sizeof(u8);
233942e7aa77SAlexander Simon 
234042e7aa77SAlexander Simon 	return pos;
234142e7aa77SAlexander Simon }
234242e7aa77SAlexander Simon 
2343ba0afa2fSMahesh Palivela u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2344ba0afa2fSMahesh Palivela 			       u32 cap)
2345ba0afa2fSMahesh Palivela {
2346ba0afa2fSMahesh Palivela 	__le32 tmp;
2347ba0afa2fSMahesh Palivela 
2348ba0afa2fSMahesh Palivela 	*pos++ = WLAN_EID_VHT_CAPABILITY;
2349d4950281SMahesh Palivela 	*pos++ = sizeof(struct ieee80211_vht_cap);
2350d4950281SMahesh Palivela 	memset(pos, 0, sizeof(struct ieee80211_vht_cap));
2351ba0afa2fSMahesh Palivela 
2352ba0afa2fSMahesh Palivela 	/* capability flags */
2353ba0afa2fSMahesh Palivela 	tmp = cpu_to_le32(cap);
2354ba0afa2fSMahesh Palivela 	memcpy(pos, &tmp, sizeof(u32));
2355ba0afa2fSMahesh Palivela 	pos += sizeof(u32);
2356ba0afa2fSMahesh Palivela 
2357ba0afa2fSMahesh Palivela 	/* VHT MCS set */
2358ba0afa2fSMahesh Palivela 	memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
2359ba0afa2fSMahesh Palivela 	pos += sizeof(vht_cap->vht_mcs);
2360ba0afa2fSMahesh Palivela 
2361ba0afa2fSMahesh Palivela 	return pos;
2362ba0afa2fSMahesh Palivela }
2363ba0afa2fSMahesh Palivela 
2364074d46d1SJohannes Berg u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
23654bf88530SJohannes Berg 			       const struct cfg80211_chan_def *chandef,
236657f255f5SArik Nemtsov 			       u16 prot_mode, bool rifs_mode)
236742e7aa77SAlexander Simon {
2368074d46d1SJohannes Berg 	struct ieee80211_ht_operation *ht_oper;
236942e7aa77SAlexander Simon 	/* Build HT Information */
2370074d46d1SJohannes Berg 	*pos++ = WLAN_EID_HT_OPERATION;
2371074d46d1SJohannes Berg 	*pos++ = sizeof(struct ieee80211_ht_operation);
2372074d46d1SJohannes Berg 	ht_oper = (struct ieee80211_ht_operation *)pos;
23734bf88530SJohannes Berg 	ht_oper->primary_chan = ieee80211_frequency_to_channel(
23744bf88530SJohannes Berg 					chandef->chan->center_freq);
23754bf88530SJohannes Berg 	switch (chandef->width) {
23764bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_160:
23774bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_80P80:
23784bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_80:
23794bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_40:
23804bf88530SJohannes Berg 		if (chandef->center_freq1 > chandef->chan->center_freq)
23814bf88530SJohannes Berg 			ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
23824bf88530SJohannes Berg 		else
2383074d46d1SJohannes Berg 			ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
238442e7aa77SAlexander Simon 		break;
238542e7aa77SAlexander Simon 	default:
2386074d46d1SJohannes Berg 		ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
238742e7aa77SAlexander Simon 		break;
238842e7aa77SAlexander Simon 	}
2389aee286c2SThomas Pedersen 	if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
23904bf88530SJohannes Berg 	    chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
23914bf88530SJohannes Berg 	    chandef->width != NL80211_CHAN_WIDTH_20)
2392074d46d1SJohannes Berg 		ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
2393ff3cc5f4SSimon Wunderlich 
239457f255f5SArik Nemtsov 	if (rifs_mode)
239557f255f5SArik Nemtsov 		ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
239657f255f5SArik Nemtsov 
2397431e3154SAshok Nagarajan 	ht_oper->operation_mode = cpu_to_le16(prot_mode);
2398074d46d1SJohannes Berg 	ht_oper->stbc_param = 0x0000;
239942e7aa77SAlexander Simon 
240042e7aa77SAlexander Simon 	/* It seems that Basic MCS set and Supported MCS set
240142e7aa77SAlexander Simon 	   are identical for the first 10 bytes */
2402074d46d1SJohannes Berg 	memset(&ht_oper->basic_set, 0, 16);
2403074d46d1SJohannes Berg 	memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
240442e7aa77SAlexander Simon 
2405074d46d1SJohannes Berg 	return pos + sizeof(struct ieee80211_ht_operation);
240642e7aa77SAlexander Simon }
240742e7aa77SAlexander Simon 
2408fb28ec0cSArik Nemtsov u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2409fb28ec0cSArik Nemtsov 				const struct cfg80211_chan_def *chandef)
2410fb28ec0cSArik Nemtsov {
2411fb28ec0cSArik Nemtsov 	struct ieee80211_vht_operation *vht_oper;
2412fb28ec0cSArik Nemtsov 
2413fb28ec0cSArik Nemtsov 	*pos++ = WLAN_EID_VHT_OPERATION;
2414fb28ec0cSArik Nemtsov 	*pos++ = sizeof(struct ieee80211_vht_operation);
2415fb28ec0cSArik Nemtsov 	vht_oper = (struct ieee80211_vht_operation *)pos;
24162fb51c35SJohannes Berg 	vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
2417fb28ec0cSArik Nemtsov 							chandef->center_freq1);
2418fb28ec0cSArik Nemtsov 	if (chandef->center_freq2)
24192fb51c35SJohannes Berg 		vht_oper->center_freq_seg1_idx =
2420fb28ec0cSArik Nemtsov 			ieee80211_frequency_to_channel(chandef->center_freq2);
2421f020ae40SChun-Yeow Yeoh 	else
24222fb51c35SJohannes Berg 		vht_oper->center_freq_seg1_idx = 0x00;
2423fb28ec0cSArik Nemtsov 
2424fb28ec0cSArik Nemtsov 	switch (chandef->width) {
2425fb28ec0cSArik Nemtsov 	case NL80211_CHAN_WIDTH_160:
242623665aafSJouni Malinen 		/*
242723665aafSJouni Malinen 		 * Convert 160 MHz channel width to new style as interop
242823665aafSJouni Malinen 		 * workaround.
242923665aafSJouni Malinen 		 */
243023665aafSJouni Malinen 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
24312fb51c35SJohannes Berg 		vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
243223665aafSJouni Malinen 		if (chandef->chan->center_freq < chandef->center_freq1)
24332fb51c35SJohannes Berg 			vht_oper->center_freq_seg0_idx -= 8;
243423665aafSJouni Malinen 		else
24352fb51c35SJohannes Berg 			vht_oper->center_freq_seg0_idx += 8;
2436fb28ec0cSArik Nemtsov 		break;
2437fb28ec0cSArik Nemtsov 	case NL80211_CHAN_WIDTH_80P80:
243823665aafSJouni Malinen 		/*
243923665aafSJouni Malinen 		 * Convert 80+80 MHz channel width to new style as interop
244023665aafSJouni Malinen 		 * workaround.
244123665aafSJouni Malinen 		 */
244223665aafSJouni Malinen 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
2443fb28ec0cSArik Nemtsov 		break;
2444fb28ec0cSArik Nemtsov 	case NL80211_CHAN_WIDTH_80:
2445fb28ec0cSArik Nemtsov 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
2446fb28ec0cSArik Nemtsov 		break;
2447fb28ec0cSArik Nemtsov 	default:
2448fb28ec0cSArik Nemtsov 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
2449fb28ec0cSArik Nemtsov 		break;
2450fb28ec0cSArik Nemtsov 	}
2451fb28ec0cSArik Nemtsov 
2452fb28ec0cSArik Nemtsov 	/* don't require special VHT peer rates */
2453fb28ec0cSArik Nemtsov 	vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
2454fb28ec0cSArik Nemtsov 
2455fb28ec0cSArik Nemtsov 	return pos + sizeof(struct ieee80211_vht_operation);
2456fb28ec0cSArik Nemtsov }
2457fb28ec0cSArik Nemtsov 
24588ac3c704SJohannes Berg bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
24594bf88530SJohannes Berg 			       struct cfg80211_chan_def *chandef)
246042e7aa77SAlexander Simon {
246142e7aa77SAlexander Simon 	enum nl80211_channel_type channel_type;
246242e7aa77SAlexander Simon 
24638ac3c704SJohannes Berg 	if (!ht_oper)
24648ac3c704SJohannes Berg 		return false;
246542e7aa77SAlexander Simon 
2466074d46d1SJohannes Berg 	switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
246742e7aa77SAlexander Simon 	case IEEE80211_HT_PARAM_CHA_SEC_NONE:
246842e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_HT20;
246942e7aa77SAlexander Simon 		break;
247042e7aa77SAlexander Simon 	case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
247142e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_HT40PLUS;
247242e7aa77SAlexander Simon 		break;
247342e7aa77SAlexander Simon 	case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
247442e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_HT40MINUS;
247542e7aa77SAlexander Simon 		break;
247642e7aa77SAlexander Simon 	default:
247742e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_NO_HT;
24788ac3c704SJohannes Berg 		return false;
247942e7aa77SAlexander Simon 	}
248042e7aa77SAlexander Simon 
24818ac3c704SJohannes Berg 	cfg80211_chandef_create(chandef, chandef->chan, channel_type);
24828ac3c704SJohannes Berg 	return true;
248342e7aa77SAlexander Simon }
248442e7aa77SAlexander Simon 
24858ac3c704SJohannes Berg bool ieee80211_chandef_vht_oper(const struct ieee80211_vht_operation *oper,
2486abcff6efSJanusz.Dziedzic@tieto.com 				struct cfg80211_chan_def *chandef)
2487abcff6efSJanusz.Dziedzic@tieto.com {
24888ac3c704SJohannes Berg 	struct cfg80211_chan_def new = *chandef;
24898ac3c704SJohannes Berg 	int cf1, cf2;
2490abcff6efSJanusz.Dziedzic@tieto.com 
24918ac3c704SJohannes Berg 	if (!oper)
24928ac3c704SJohannes Berg 		return false;
24938ac3c704SJohannes Berg 
24942fb51c35SJohannes Berg 	cf1 = ieee80211_channel_to_frequency(oper->center_freq_seg0_idx,
24958ac3c704SJohannes Berg 					     chandef->chan->band);
24962fb51c35SJohannes Berg 	cf2 = ieee80211_channel_to_frequency(oper->center_freq_seg1_idx,
24978ac3c704SJohannes Berg 					     chandef->chan->band);
2498abcff6efSJanusz.Dziedzic@tieto.com 
2499abcff6efSJanusz.Dziedzic@tieto.com 	switch (oper->chan_width) {
2500abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_USE_HT:
2501abcff6efSJanusz.Dziedzic@tieto.com 		break;
2502abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_80MHZ:
25038ac3c704SJohannes Berg 		new.width = NL80211_CHAN_WIDTH_80;
25048ac3c704SJohannes Berg 		new.center_freq1 = cf1;
250523665aafSJouni Malinen 		/* If needed, adjust based on the newer interop workaround. */
25062fb51c35SJohannes Berg 		if (oper->center_freq_seg1_idx) {
250723665aafSJouni Malinen 			unsigned int diff;
250823665aafSJouni Malinen 
25092fb51c35SJohannes Berg 			diff = abs(oper->center_freq_seg1_idx -
25102fb51c35SJohannes Berg 				   oper->center_freq_seg0_idx);
251123665aafSJouni Malinen 			if (diff == 8) {
251223665aafSJouni Malinen 				new.width = NL80211_CHAN_WIDTH_160;
251323665aafSJouni Malinen 				new.center_freq1 = cf2;
251423665aafSJouni Malinen 			} else if (diff > 8) {
251523665aafSJouni Malinen 				new.width = NL80211_CHAN_WIDTH_80P80;
251623665aafSJouni Malinen 				new.center_freq2 = cf2;
251723665aafSJouni Malinen 			}
251823665aafSJouni Malinen 		}
2519abcff6efSJanusz.Dziedzic@tieto.com 		break;
2520abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_160MHZ:
25218ac3c704SJohannes Berg 		new.width = NL80211_CHAN_WIDTH_160;
25228ac3c704SJohannes Berg 		new.center_freq1 = cf1;
2523abcff6efSJanusz.Dziedzic@tieto.com 		break;
2524abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
25258ac3c704SJohannes Berg 		new.width = NL80211_CHAN_WIDTH_80P80;
25268ac3c704SJohannes Berg 		new.center_freq1 = cf1;
25278ac3c704SJohannes Berg 		new.center_freq2 = cf2;
2528abcff6efSJanusz.Dziedzic@tieto.com 		break;
2529abcff6efSJanusz.Dziedzic@tieto.com 	default:
25308ac3c704SJohannes Berg 		return false;
2531abcff6efSJanusz.Dziedzic@tieto.com 	}
2532abcff6efSJanusz.Dziedzic@tieto.com 
25338ac3c704SJohannes Berg 	if (!cfg80211_chandef_valid(&new))
25348ac3c704SJohannes Berg 		return false;
25358ac3c704SJohannes Berg 
25368ac3c704SJohannes Berg 	*chandef = new;
25378ac3c704SJohannes Berg 	return true;
2538abcff6efSJanusz.Dziedzic@tieto.com }
2539abcff6efSJanusz.Dziedzic@tieto.com 
25402103dec1SSimon Wunderlich int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
25412103dec1SSimon Wunderlich 			     const struct ieee80211_supported_band *sband,
25422103dec1SSimon Wunderlich 			     const u8 *srates, int srates_len, u32 *rates)
25432103dec1SSimon Wunderlich {
25442103dec1SSimon Wunderlich 	u32 rate_flags = ieee80211_chandef_rate_flags(chandef);
25452103dec1SSimon Wunderlich 	int shift = ieee80211_chandef_get_shift(chandef);
25462103dec1SSimon Wunderlich 	struct ieee80211_rate *br;
25472103dec1SSimon Wunderlich 	int brate, rate, i, j, count = 0;
25482103dec1SSimon Wunderlich 
25492103dec1SSimon Wunderlich 	*rates = 0;
25502103dec1SSimon Wunderlich 
25512103dec1SSimon Wunderlich 	for (i = 0; i < srates_len; i++) {
25522103dec1SSimon Wunderlich 		rate = srates[i] & 0x7f;
25532103dec1SSimon Wunderlich 
25542103dec1SSimon Wunderlich 		for (j = 0; j < sband->n_bitrates; j++) {
25552103dec1SSimon Wunderlich 			br = &sband->bitrates[j];
25562103dec1SSimon Wunderlich 			if ((rate_flags & br->flags) != rate_flags)
25572103dec1SSimon Wunderlich 				continue;
25582103dec1SSimon Wunderlich 
25592103dec1SSimon Wunderlich 			brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
25602103dec1SSimon Wunderlich 			if (brate == rate) {
25612103dec1SSimon Wunderlich 				*rates |= BIT(j);
25622103dec1SSimon Wunderlich 				count++;
25632103dec1SSimon Wunderlich 				break;
25642103dec1SSimon Wunderlich 			}
25652103dec1SSimon Wunderlich 		}
25662103dec1SSimon Wunderlich 	}
25672103dec1SSimon Wunderlich 	return count;
25682103dec1SSimon Wunderlich }
25692103dec1SSimon Wunderlich 
2570fc8a7321SJohannes Berg int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
25716b77863bSJohannes Berg 			    struct sk_buff *skb, bool need_basic,
257257fbcce3SJohannes Berg 			    enum nl80211_band band)
2573768db343SArik Nemtsov {
2574768db343SArik Nemtsov 	struct ieee80211_local *local = sdata->local;
2575768db343SArik Nemtsov 	struct ieee80211_supported_band *sband;
25762103dec1SSimon Wunderlich 	int rate, shift;
2577768db343SArik Nemtsov 	u8 i, rates, *pos;
2578fc8a7321SJohannes Berg 	u32 basic_rates = sdata->vif.bss_conf.basic_rates;
25792103dec1SSimon Wunderlich 	u32 rate_flags;
2580768db343SArik Nemtsov 
25812103dec1SSimon Wunderlich 	shift = ieee80211_vif_get_shift(&sdata->vif);
25822103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
25836b77863bSJohannes Berg 	sband = local->hw.wiphy->bands[band];
25842103dec1SSimon Wunderlich 	rates = 0;
25852103dec1SSimon Wunderlich 	for (i = 0; i < sband->n_bitrates; i++) {
25862103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
25872103dec1SSimon Wunderlich 			continue;
25882103dec1SSimon Wunderlich 		rates++;
25892103dec1SSimon Wunderlich 	}
2590768db343SArik Nemtsov 	if (rates > 8)
2591768db343SArik Nemtsov 		rates = 8;
2592768db343SArik Nemtsov 
2593768db343SArik Nemtsov 	if (skb_tailroom(skb) < rates + 2)
2594768db343SArik Nemtsov 		return -ENOMEM;
2595768db343SArik Nemtsov 
2596768db343SArik Nemtsov 	pos = skb_put(skb, rates + 2);
2597768db343SArik Nemtsov 	*pos++ = WLAN_EID_SUPP_RATES;
2598768db343SArik Nemtsov 	*pos++ = rates;
2599768db343SArik Nemtsov 	for (i = 0; i < rates; i++) {
2600657c3e0cSAshok Nagarajan 		u8 basic = 0;
26012103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
26022103dec1SSimon Wunderlich 			continue;
26032103dec1SSimon Wunderlich 
2604657c3e0cSAshok Nagarajan 		if (need_basic && basic_rates & BIT(i))
2605657c3e0cSAshok Nagarajan 			basic = 0x80;
26062103dec1SSimon Wunderlich 		rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
26072103dec1SSimon Wunderlich 				    5 * (1 << shift));
26082103dec1SSimon Wunderlich 		*pos++ = basic | (u8) rate;
2609768db343SArik Nemtsov 	}
2610768db343SArik Nemtsov 
2611768db343SArik Nemtsov 	return 0;
2612768db343SArik Nemtsov }
2613768db343SArik Nemtsov 
2614fc8a7321SJohannes Berg int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
26156b77863bSJohannes Berg 				struct sk_buff *skb, bool need_basic,
261657fbcce3SJohannes Berg 				enum nl80211_band band)
2617768db343SArik Nemtsov {
2618768db343SArik Nemtsov 	struct ieee80211_local *local = sdata->local;
2619768db343SArik Nemtsov 	struct ieee80211_supported_band *sband;
2620cc63ec76SChun-Yeow Yeoh 	int rate, shift;
2621768db343SArik Nemtsov 	u8 i, exrates, *pos;
2622fc8a7321SJohannes Berg 	u32 basic_rates = sdata->vif.bss_conf.basic_rates;
26232103dec1SSimon Wunderlich 	u32 rate_flags;
26242103dec1SSimon Wunderlich 
26252103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
26262103dec1SSimon Wunderlich 	shift = ieee80211_vif_get_shift(&sdata->vif);
2627768db343SArik Nemtsov 
26286b77863bSJohannes Berg 	sband = local->hw.wiphy->bands[band];
26292103dec1SSimon Wunderlich 	exrates = 0;
26302103dec1SSimon Wunderlich 	for (i = 0; i < sband->n_bitrates; i++) {
26312103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
26322103dec1SSimon Wunderlich 			continue;
26332103dec1SSimon Wunderlich 		exrates++;
26342103dec1SSimon Wunderlich 	}
26352103dec1SSimon Wunderlich 
2636768db343SArik Nemtsov 	if (exrates > 8)
2637768db343SArik Nemtsov 		exrates -= 8;
2638768db343SArik Nemtsov 	else
2639768db343SArik Nemtsov 		exrates = 0;
2640768db343SArik Nemtsov 
2641768db343SArik Nemtsov 	if (skb_tailroom(skb) < exrates + 2)
2642768db343SArik Nemtsov 		return -ENOMEM;
2643768db343SArik Nemtsov 
2644768db343SArik Nemtsov 	if (exrates) {
2645768db343SArik Nemtsov 		pos = skb_put(skb, exrates + 2);
2646768db343SArik Nemtsov 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
2647768db343SArik Nemtsov 		*pos++ = exrates;
2648768db343SArik Nemtsov 		for (i = 8; i < sband->n_bitrates; i++) {
2649657c3e0cSAshok Nagarajan 			u8 basic = 0;
26502103dec1SSimon Wunderlich 			if ((rate_flags & sband->bitrates[i].flags)
26512103dec1SSimon Wunderlich 			    != rate_flags)
26522103dec1SSimon Wunderlich 				continue;
2653657c3e0cSAshok Nagarajan 			if (need_basic && basic_rates & BIT(i))
2654657c3e0cSAshok Nagarajan 				basic = 0x80;
26552103dec1SSimon Wunderlich 			rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
26562103dec1SSimon Wunderlich 					    5 * (1 << shift));
26572103dec1SSimon Wunderlich 			*pos++ = basic | (u8) rate;
2658768db343SArik Nemtsov 		}
2659768db343SArik Nemtsov 	}
2660768db343SArik Nemtsov 	return 0;
2661768db343SArik Nemtsov }
26621dae27f8SWey-Yi Guy 
26631dae27f8SWey-Yi Guy int ieee80211_ave_rssi(struct ieee80211_vif *vif)
26641dae27f8SWey-Yi Guy {
26651dae27f8SWey-Yi Guy 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
26661dae27f8SWey-Yi Guy 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
26671dae27f8SWey-Yi Guy 
2668be6bcabcSWey-Yi Guy 	if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
2669be6bcabcSWey-Yi Guy 		/* non-managed type inferfaces */
2670be6bcabcSWey-Yi Guy 		return 0;
2671be6bcabcSWey-Yi Guy 	}
2672338c17aeSJohannes Berg 	return -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
26731dae27f8SWey-Yi Guy }
26740d8a0a17SWey-Yi Guy EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
267504ecd257SJohannes Berg 
267604ecd257SJohannes Berg u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
267704ecd257SJohannes Berg {
267804ecd257SJohannes Berg 	if (!mcs)
267904ecd257SJohannes Berg 		return 1;
268004ecd257SJohannes Berg 
268104ecd257SJohannes Berg 	/* TODO: consider rx_highest */
268204ecd257SJohannes Berg 
268304ecd257SJohannes Berg 	if (mcs->rx_mask[3])
268404ecd257SJohannes Berg 		return 4;
268504ecd257SJohannes Berg 	if (mcs->rx_mask[2])
268604ecd257SJohannes Berg 		return 3;
268704ecd257SJohannes Berg 	if (mcs->rx_mask[1])
268804ecd257SJohannes Berg 		return 2;
268904ecd257SJohannes Berg 	return 1;
269004ecd257SJohannes Berg }
2691f4bda337SThomas Pedersen 
2692f4bda337SThomas Pedersen /**
2693f4bda337SThomas Pedersen  * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
2694f4bda337SThomas Pedersen  * @local: mac80211 hw info struct
2695f4bda337SThomas Pedersen  * @status: RX status
2696f4bda337SThomas Pedersen  * @mpdu_len: total MPDU length (including FCS)
2697f4bda337SThomas Pedersen  * @mpdu_offset: offset into MPDU to calculate timestamp at
2698f4bda337SThomas Pedersen  *
2699f4bda337SThomas Pedersen  * This function calculates the RX timestamp at the given MPDU offset, taking
2700f4bda337SThomas Pedersen  * into account what the RX timestamp was. An offset of 0 will just normalize
2701f4bda337SThomas Pedersen  * the timestamp to TSF at beginning of MPDU reception.
2702f4bda337SThomas Pedersen  */
2703f4bda337SThomas Pedersen u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
2704f4bda337SThomas Pedersen 				     struct ieee80211_rx_status *status,
2705f4bda337SThomas Pedersen 				     unsigned int mpdu_len,
2706f4bda337SThomas Pedersen 				     unsigned int mpdu_offset)
2707f4bda337SThomas Pedersen {
2708f4bda337SThomas Pedersen 	u64 ts = status->mactime;
2709f4bda337SThomas Pedersen 	struct rate_info ri;
2710f4bda337SThomas Pedersen 	u16 rate;
2711f4bda337SThomas Pedersen 
2712f4bda337SThomas Pedersen 	if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
2713f4bda337SThomas Pedersen 		return 0;
2714f4bda337SThomas Pedersen 
2715f4bda337SThomas Pedersen 	memset(&ri, 0, sizeof(ri));
2716f4bda337SThomas Pedersen 
2717f4bda337SThomas Pedersen 	/* Fill cfg80211 rate info */
2718f4bda337SThomas Pedersen 	if (status->flag & RX_FLAG_HT) {
2719f4bda337SThomas Pedersen 		ri.mcs = status->rate_idx;
2720f4bda337SThomas Pedersen 		ri.flags |= RATE_INFO_FLAGS_MCS;
2721f4bda337SThomas Pedersen 		if (status->flag & RX_FLAG_40MHZ)
2722b51f3beeSJohannes Berg 			ri.bw = RATE_INFO_BW_40;
2723b51f3beeSJohannes Berg 		else
2724b51f3beeSJohannes Berg 			ri.bw = RATE_INFO_BW_20;
2725f4bda337SThomas Pedersen 		if (status->flag & RX_FLAG_SHORT_GI)
2726f4bda337SThomas Pedersen 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
27275614618eSJohannes Berg 	} else if (status->flag & RX_FLAG_VHT) {
27285614618eSJohannes Berg 		ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
27295614618eSJohannes Berg 		ri.mcs = status->rate_idx;
27305614618eSJohannes Berg 		ri.nss = status->vht_nss;
27315614618eSJohannes Berg 		if (status->flag & RX_FLAG_40MHZ)
2732b51f3beeSJohannes Berg 			ri.bw = RATE_INFO_BW_40;
2733b51f3beeSJohannes Berg 		else if (status->vht_flag & RX_VHT_FLAG_80MHZ)
2734b51f3beeSJohannes Berg 			ri.bw = RATE_INFO_BW_80;
2735b51f3beeSJohannes Berg 		else if (status->vht_flag & RX_VHT_FLAG_160MHZ)
2736b51f3beeSJohannes Berg 			ri.bw = RATE_INFO_BW_160;
2737b51f3beeSJohannes Berg 		else
2738b51f3beeSJohannes Berg 			ri.bw = RATE_INFO_BW_20;
27395614618eSJohannes Berg 		if (status->flag & RX_FLAG_SHORT_GI)
27405614618eSJohannes Berg 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
2741f4bda337SThomas Pedersen 	} else {
2742f4bda337SThomas Pedersen 		struct ieee80211_supported_band *sband;
27432103dec1SSimon Wunderlich 		int shift = 0;
27442103dec1SSimon Wunderlich 		int bitrate;
27452103dec1SSimon Wunderlich 
2746b51f3beeSJohannes Berg 		if (status->flag & RX_FLAG_10MHZ) {
27472103dec1SSimon Wunderlich 			shift = 1;
2748b51f3beeSJohannes Berg 			ri.bw = RATE_INFO_BW_10;
2749b51f3beeSJohannes Berg 		} else if (status->flag & RX_FLAG_5MHZ) {
27502103dec1SSimon Wunderlich 			shift = 2;
2751b51f3beeSJohannes Berg 			ri.bw = RATE_INFO_BW_5;
2752b51f3beeSJohannes Berg 		} else {
2753b51f3beeSJohannes Berg 			ri.bw = RATE_INFO_BW_20;
2754b51f3beeSJohannes Berg 		}
2755f4bda337SThomas Pedersen 
2756f4bda337SThomas Pedersen 		sband = local->hw.wiphy->bands[status->band];
27572103dec1SSimon Wunderlich 		bitrate = sband->bitrates[status->rate_idx].bitrate;
27582103dec1SSimon Wunderlich 		ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
2759f4a0f0c5SJohannes Berg 
2760f4a0f0c5SJohannes Berg 		if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
2761f4a0f0c5SJohannes Berg 			/* TODO: handle HT/VHT preambles */
276257fbcce3SJohannes Berg 			if (status->band == NL80211_BAND_5GHZ) {
2763f4a0f0c5SJohannes Berg 				ts += 20 << shift;
2764f4a0f0c5SJohannes Berg 				mpdu_offset += 2;
2765f4a0f0c5SJohannes Berg 			} else if (status->flag & RX_FLAG_SHORTPRE) {
2766f4a0f0c5SJohannes Berg 				ts += 96;
2767f4a0f0c5SJohannes Berg 			} else {
2768f4a0f0c5SJohannes Berg 				ts += 192;
2769f4a0f0c5SJohannes Berg 			}
2770f4a0f0c5SJohannes Berg 		}
2771f4bda337SThomas Pedersen 	}
2772f4bda337SThomas Pedersen 
2773f4bda337SThomas Pedersen 	rate = cfg80211_calculate_bitrate(&ri);
2774d86aa4f8SJohannes Berg 	if (WARN_ONCE(!rate,
2775f980ebc0SSara Sharon 		      "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
2776f980ebc0SSara Sharon 		      (unsigned long long)status->flag, status->rate_idx,
2777f980ebc0SSara Sharon 		      status->vht_nss))
2778d86aa4f8SJohannes Berg 		return 0;
2779f4bda337SThomas Pedersen 
2780f4bda337SThomas Pedersen 	/* rewind from end of MPDU */
2781f4bda337SThomas Pedersen 	if (status->flag & RX_FLAG_MACTIME_END)
2782f4bda337SThomas Pedersen 		ts -= mpdu_len * 8 * 10 / rate;
2783f4bda337SThomas Pedersen 
2784f4bda337SThomas Pedersen 	ts += mpdu_offset * 8 * 10 / rate;
2785f4bda337SThomas Pedersen 
2786f4bda337SThomas Pedersen 	return ts;
2787f4bda337SThomas Pedersen }
2788164eb02dSSimon Wunderlich 
2789164eb02dSSimon Wunderlich void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
2790164eb02dSSimon Wunderlich {
2791164eb02dSSimon Wunderlich 	struct ieee80211_sub_if_data *sdata;
2792d2859df5SJanusz Dziedzic 	struct cfg80211_chan_def chandef;
2793164eb02dSSimon Wunderlich 
279434a3740dSJohannes Berg 	mutex_lock(&local->mtx);
2795164eb02dSSimon Wunderlich 	mutex_lock(&local->iflist_mtx);
2796164eb02dSSimon Wunderlich 	list_for_each_entry(sdata, &local->interfaces, list) {
279734a3740dSJohannes Berg 		/* it might be waiting for the local->mtx, but then
279834a3740dSJohannes Berg 		 * by the time it gets it, sdata->wdev.cac_started
279934a3740dSJohannes Berg 		 * will no longer be true
280034a3740dSJohannes Berg 		 */
280134a3740dSJohannes Berg 		cancel_delayed_work(&sdata->dfs_cac_timer_work);
2802164eb02dSSimon Wunderlich 
2803164eb02dSSimon Wunderlich 		if (sdata->wdev.cac_started) {
2804d2859df5SJanusz Dziedzic 			chandef = sdata->vif.bss_conf.chandef;
2805164eb02dSSimon Wunderlich 			ieee80211_vif_release_channel(sdata);
2806164eb02dSSimon Wunderlich 			cfg80211_cac_event(sdata->dev,
2807d2859df5SJanusz Dziedzic 					   &chandef,
2808164eb02dSSimon Wunderlich 					   NL80211_RADAR_CAC_ABORTED,
2809164eb02dSSimon Wunderlich 					   GFP_KERNEL);
2810164eb02dSSimon Wunderlich 		}
2811164eb02dSSimon Wunderlich 	}
2812164eb02dSSimon Wunderlich 	mutex_unlock(&local->iflist_mtx);
281334a3740dSJohannes Berg 	mutex_unlock(&local->mtx);
2814164eb02dSSimon Wunderlich }
2815164eb02dSSimon Wunderlich 
2816164eb02dSSimon Wunderlich void ieee80211_dfs_radar_detected_work(struct work_struct *work)
2817164eb02dSSimon Wunderlich {
2818164eb02dSSimon Wunderlich 	struct ieee80211_local *local =
2819164eb02dSSimon Wunderlich 		container_of(work, struct ieee80211_local, radar_detected_work);
282084a3d1c9SJanusz Dziedzic 	struct cfg80211_chan_def chandef = local->hw.conf.chandef;
2821486cf4c0SMichal Kazior 	struct ieee80211_chanctx *ctx;
2822486cf4c0SMichal Kazior 	int num_chanctx = 0;
2823486cf4c0SMichal Kazior 
2824486cf4c0SMichal Kazior 	mutex_lock(&local->chanctx_mtx);
2825486cf4c0SMichal Kazior 	list_for_each_entry(ctx, &local->chanctx_list, list) {
2826486cf4c0SMichal Kazior 		if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
2827486cf4c0SMichal Kazior 			continue;
2828486cf4c0SMichal Kazior 
2829486cf4c0SMichal Kazior 		num_chanctx++;
2830486cf4c0SMichal Kazior 		chandef = ctx->conf.def;
2831486cf4c0SMichal Kazior 	}
2832486cf4c0SMichal Kazior 	mutex_unlock(&local->chanctx_mtx);
2833164eb02dSSimon Wunderlich 
2834164eb02dSSimon Wunderlich 	ieee80211_dfs_cac_cancel(local);
2835164eb02dSSimon Wunderlich 
2836486cf4c0SMichal Kazior 	if (num_chanctx > 1)
2837486cf4c0SMichal Kazior 		/* XXX: multi-channel is not supported yet */
2838164eb02dSSimon Wunderlich 		WARN_ON(1);
283984a3d1c9SJanusz Dziedzic 	else
2840164eb02dSSimon Wunderlich 		cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
2841164eb02dSSimon Wunderlich }
2842164eb02dSSimon Wunderlich 
2843164eb02dSSimon Wunderlich void ieee80211_radar_detected(struct ieee80211_hw *hw)
2844164eb02dSSimon Wunderlich {
2845164eb02dSSimon Wunderlich 	struct ieee80211_local *local = hw_to_local(hw);
2846164eb02dSSimon Wunderlich 
2847164eb02dSSimon Wunderlich 	trace_api_radar_detected(local);
2848164eb02dSSimon Wunderlich 
2849164eb02dSSimon Wunderlich 	ieee80211_queue_work(hw, &local->radar_detected_work);
2850164eb02dSSimon Wunderlich }
2851164eb02dSSimon Wunderlich EXPORT_SYMBOL(ieee80211_radar_detected);
2852e6b7cde4SSimon Wunderlich 
2853e6b7cde4SSimon Wunderlich u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
2854e6b7cde4SSimon Wunderlich {
2855e6b7cde4SSimon Wunderlich 	u32 ret;
2856e6b7cde4SSimon Wunderlich 	int tmp;
2857e6b7cde4SSimon Wunderlich 
2858e6b7cde4SSimon Wunderlich 	switch (c->width) {
2859e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_20:
2860e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_20_NOHT;
2861e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2862e6b7cde4SSimon Wunderlich 		break;
2863e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_40:
2864e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_20;
2865e6b7cde4SSimon Wunderlich 		c->center_freq1 = c->chan->center_freq;
2866e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_40MHZ |
2867e6b7cde4SSimon Wunderlich 		      IEEE80211_STA_DISABLE_VHT;
2868e6b7cde4SSimon Wunderlich 		break;
2869e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_80:
2870e6b7cde4SSimon Wunderlich 		tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
2871e6b7cde4SSimon Wunderlich 		/* n_P40 */
2872e6b7cde4SSimon Wunderlich 		tmp /= 2;
2873e6b7cde4SSimon Wunderlich 		/* freq_P40 */
2874e6b7cde4SSimon Wunderlich 		c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
2875e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_40;
2876e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_VHT;
2877e6b7cde4SSimon Wunderlich 		break;
2878e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_80P80:
2879e6b7cde4SSimon Wunderlich 		c->center_freq2 = 0;
2880e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_80;
2881e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_80P80MHZ |
2882e6b7cde4SSimon Wunderlich 		      IEEE80211_STA_DISABLE_160MHZ;
2883e6b7cde4SSimon Wunderlich 		break;
2884e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_160:
2885e6b7cde4SSimon Wunderlich 		/* n_P20 */
2886e6b7cde4SSimon Wunderlich 		tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
2887e6b7cde4SSimon Wunderlich 		/* n_P80 */
2888e6b7cde4SSimon Wunderlich 		tmp /= 4;
2889e6b7cde4SSimon Wunderlich 		c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
2890e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_80;
2891e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_80P80MHZ |
2892e6b7cde4SSimon Wunderlich 		      IEEE80211_STA_DISABLE_160MHZ;
2893e6b7cde4SSimon Wunderlich 		break;
2894e6b7cde4SSimon Wunderlich 	default:
2895e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_20_NOHT:
2896e6b7cde4SSimon Wunderlich 		WARN_ON_ONCE(1);
2897e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_20_NOHT;
2898e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2899e6b7cde4SSimon Wunderlich 		break;
2900e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_5:
2901e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_10:
2902e6b7cde4SSimon Wunderlich 		WARN_ON_ONCE(1);
2903e6b7cde4SSimon Wunderlich 		/* keep c->width */
2904e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2905e6b7cde4SSimon Wunderlich 		break;
2906e6b7cde4SSimon Wunderlich 	}
2907e6b7cde4SSimon Wunderlich 
2908e6b7cde4SSimon Wunderlich 	WARN_ON_ONCE(!cfg80211_chandef_valid(c));
2909e6b7cde4SSimon Wunderlich 
2910e6b7cde4SSimon Wunderlich 	return ret;
2911e6b7cde4SSimon Wunderlich }
2912687da132SEmmanuel Grumbach 
2913687da132SEmmanuel Grumbach /*
2914687da132SEmmanuel Grumbach  * Returns true if smps_mode_new is strictly more restrictive than
2915687da132SEmmanuel Grumbach  * smps_mode_old.
2916687da132SEmmanuel Grumbach  */
2917687da132SEmmanuel Grumbach bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
2918687da132SEmmanuel Grumbach 				   enum ieee80211_smps_mode smps_mode_new)
2919687da132SEmmanuel Grumbach {
2920687da132SEmmanuel Grumbach 	if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
2921687da132SEmmanuel Grumbach 			 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
2922687da132SEmmanuel Grumbach 		return false;
2923687da132SEmmanuel Grumbach 
2924687da132SEmmanuel Grumbach 	switch (smps_mode_old) {
2925687da132SEmmanuel Grumbach 	case IEEE80211_SMPS_STATIC:
2926687da132SEmmanuel Grumbach 		return false;
2927687da132SEmmanuel Grumbach 	case IEEE80211_SMPS_DYNAMIC:
2928687da132SEmmanuel Grumbach 		return smps_mode_new == IEEE80211_SMPS_STATIC;
2929687da132SEmmanuel Grumbach 	case IEEE80211_SMPS_OFF:
2930687da132SEmmanuel Grumbach 		return smps_mode_new != IEEE80211_SMPS_OFF;
2931687da132SEmmanuel Grumbach 	default:
2932687da132SEmmanuel Grumbach 		WARN_ON(1);
2933687da132SEmmanuel Grumbach 	}
2934687da132SEmmanuel Grumbach 
2935687da132SEmmanuel Grumbach 	return false;
2936687da132SEmmanuel Grumbach }
2937c6da674aSChun-Yeow Yeoh 
2938c6da674aSChun-Yeow Yeoh int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
2939c6da674aSChun-Yeow Yeoh 			      struct cfg80211_csa_settings *csa_settings)
2940c6da674aSChun-Yeow Yeoh {
2941c6da674aSChun-Yeow Yeoh 	struct sk_buff *skb;
2942c6da674aSChun-Yeow Yeoh 	struct ieee80211_mgmt *mgmt;
2943c6da674aSChun-Yeow Yeoh 	struct ieee80211_local *local = sdata->local;
2944c6da674aSChun-Yeow Yeoh 	int freq;
2945c6da674aSChun-Yeow Yeoh 	int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.chan_switch) +
2946c6da674aSChun-Yeow Yeoh 			       sizeof(mgmt->u.action.u.chan_switch);
2947c6da674aSChun-Yeow Yeoh 	u8 *pos;
2948c6da674aSChun-Yeow Yeoh 
2949c6da674aSChun-Yeow Yeoh 	if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2950c6da674aSChun-Yeow Yeoh 	    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
2951c6da674aSChun-Yeow Yeoh 		return -EOPNOTSUPP;
2952c6da674aSChun-Yeow Yeoh 
2953c6da674aSChun-Yeow Yeoh 	skb = dev_alloc_skb(local->tx_headroom + hdr_len +
2954c6da674aSChun-Yeow Yeoh 			    5 + /* channel switch announcement element */
2955c6da674aSChun-Yeow Yeoh 			    3 + /* secondary channel offset element */
2956c6da674aSChun-Yeow Yeoh 			    8); /* mesh channel switch parameters element */
2957c6da674aSChun-Yeow Yeoh 	if (!skb)
2958c6da674aSChun-Yeow Yeoh 		return -ENOMEM;
2959c6da674aSChun-Yeow Yeoh 
2960c6da674aSChun-Yeow Yeoh 	skb_reserve(skb, local->tx_headroom);
2961c6da674aSChun-Yeow Yeoh 	mgmt = (struct ieee80211_mgmt *)skb_put(skb, hdr_len);
2962c6da674aSChun-Yeow Yeoh 	memset(mgmt, 0, hdr_len);
2963c6da674aSChun-Yeow Yeoh 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2964c6da674aSChun-Yeow Yeoh 					  IEEE80211_STYPE_ACTION);
2965c6da674aSChun-Yeow Yeoh 
2966c6da674aSChun-Yeow Yeoh 	eth_broadcast_addr(mgmt->da);
2967c6da674aSChun-Yeow Yeoh 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
2968c6da674aSChun-Yeow Yeoh 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2969c6da674aSChun-Yeow Yeoh 		memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
2970c6da674aSChun-Yeow Yeoh 	} else {
2971c6da674aSChun-Yeow Yeoh 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2972c6da674aSChun-Yeow Yeoh 		memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
2973c6da674aSChun-Yeow Yeoh 	}
2974c6da674aSChun-Yeow Yeoh 	mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
2975c6da674aSChun-Yeow Yeoh 	mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
2976c6da674aSChun-Yeow Yeoh 	pos = skb_put(skb, 5);
2977c6da674aSChun-Yeow Yeoh 	*pos++ = WLAN_EID_CHANNEL_SWITCH;			/* EID */
2978c6da674aSChun-Yeow Yeoh 	*pos++ = 3;						/* IE length */
2979c6da674aSChun-Yeow Yeoh 	*pos++ = csa_settings->block_tx ? 1 : 0;		/* CSA mode */
2980c6da674aSChun-Yeow Yeoh 	freq = csa_settings->chandef.chan->center_freq;
2981c6da674aSChun-Yeow Yeoh 	*pos++ = ieee80211_frequency_to_channel(freq);		/* channel */
2982c6da674aSChun-Yeow Yeoh 	*pos++ = csa_settings->count;				/* count */
2983c6da674aSChun-Yeow Yeoh 
2984c6da674aSChun-Yeow Yeoh 	if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
2985c6da674aSChun-Yeow Yeoh 		enum nl80211_channel_type ch_type;
2986c6da674aSChun-Yeow Yeoh 
2987c6da674aSChun-Yeow Yeoh 		skb_put(skb, 3);
2988c6da674aSChun-Yeow Yeoh 		*pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;	/* EID */
2989c6da674aSChun-Yeow Yeoh 		*pos++ = 1;					/* IE length */
2990c6da674aSChun-Yeow Yeoh 		ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
2991c6da674aSChun-Yeow Yeoh 		if (ch_type == NL80211_CHAN_HT40PLUS)
2992c6da674aSChun-Yeow Yeoh 			*pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2993c6da674aSChun-Yeow Yeoh 		else
2994c6da674aSChun-Yeow Yeoh 			*pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
2995c6da674aSChun-Yeow Yeoh 	}
2996c6da674aSChun-Yeow Yeoh 
2997c6da674aSChun-Yeow Yeoh 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2998c6da674aSChun-Yeow Yeoh 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2999c6da674aSChun-Yeow Yeoh 
3000c6da674aSChun-Yeow Yeoh 		skb_put(skb, 8);
3001c6da674aSChun-Yeow Yeoh 		*pos++ = WLAN_EID_CHAN_SWITCH_PARAM;		/* EID */
3002c6da674aSChun-Yeow Yeoh 		*pos++ = 6;					/* IE length */
3003c6da674aSChun-Yeow Yeoh 		*pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL;	/* Mesh TTL */
3004c6da674aSChun-Yeow Yeoh 		*pos = 0x00;	/* Mesh Flag: Tx Restrict, Initiator, Reason */
3005c6da674aSChun-Yeow Yeoh 		*pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
3006c6da674aSChun-Yeow Yeoh 		*pos++ |= csa_settings->block_tx ?
3007c6da674aSChun-Yeow Yeoh 			  WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
3008c6da674aSChun-Yeow Yeoh 		put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
3009c6da674aSChun-Yeow Yeoh 		pos += 2;
3010ca91dc97SChun-Yeow Yeoh 		put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
3011c6da674aSChun-Yeow Yeoh 		pos += 2;
3012c6da674aSChun-Yeow Yeoh 	}
3013c6da674aSChun-Yeow Yeoh 
3014c6da674aSChun-Yeow Yeoh 	ieee80211_tx_skb(sdata, skb);
3015c6da674aSChun-Yeow Yeoh 	return 0;
3016c6da674aSChun-Yeow Yeoh }
30172475b1ccSMax Stepanov 
30182475b1ccSMax Stepanov bool ieee80211_cs_valid(const struct ieee80211_cipher_scheme *cs)
30192475b1ccSMax Stepanov {
30202475b1ccSMax Stepanov 	return !(cs == NULL || cs->cipher == 0 ||
30212475b1ccSMax Stepanov 		 cs->hdr_len < cs->pn_len + cs->pn_off ||
30222475b1ccSMax Stepanov 		 cs->hdr_len <= cs->key_idx_off ||
30232475b1ccSMax Stepanov 		 cs->key_idx_shift > 7 ||
30242475b1ccSMax Stepanov 		 cs->key_idx_mask == 0);
30252475b1ccSMax Stepanov }
30262475b1ccSMax Stepanov 
30272475b1ccSMax Stepanov bool ieee80211_cs_list_valid(const struct ieee80211_cipher_scheme *cs, int n)
30282475b1ccSMax Stepanov {
30292475b1ccSMax Stepanov 	int i;
30302475b1ccSMax Stepanov 
30312475b1ccSMax Stepanov 	/* Ensure we have enough iftype bitmap space for all iftype values */
30322475b1ccSMax Stepanov 	WARN_ON((NUM_NL80211_IFTYPES / 8 + 1) > sizeof(cs[0].iftype));
30332475b1ccSMax Stepanov 
30342475b1ccSMax Stepanov 	for (i = 0; i < n; i++)
30352475b1ccSMax Stepanov 		if (!ieee80211_cs_valid(&cs[i]))
30362475b1ccSMax Stepanov 			return false;
30372475b1ccSMax Stepanov 
30382475b1ccSMax Stepanov 	return true;
30392475b1ccSMax Stepanov }
30402475b1ccSMax Stepanov 
30412475b1ccSMax Stepanov const struct ieee80211_cipher_scheme *
30422475b1ccSMax Stepanov ieee80211_cs_get(struct ieee80211_local *local, u32 cipher,
30432475b1ccSMax Stepanov 		 enum nl80211_iftype iftype)
30442475b1ccSMax Stepanov {
30452475b1ccSMax Stepanov 	const struct ieee80211_cipher_scheme *l = local->hw.cipher_schemes;
30462475b1ccSMax Stepanov 	int n = local->hw.n_cipher_schemes;
30472475b1ccSMax Stepanov 	int i;
30482475b1ccSMax Stepanov 	const struct ieee80211_cipher_scheme *cs = NULL;
30492475b1ccSMax Stepanov 
30502475b1ccSMax Stepanov 	for (i = 0; i < n; i++) {
30512475b1ccSMax Stepanov 		if (l[i].cipher == cipher) {
30522475b1ccSMax Stepanov 			cs = &l[i];
30532475b1ccSMax Stepanov 			break;
30542475b1ccSMax Stepanov 		}
30552475b1ccSMax Stepanov 	}
30562475b1ccSMax Stepanov 
30572475b1ccSMax Stepanov 	if (!cs || !(cs->iftype & BIT(iftype)))
30582475b1ccSMax Stepanov 		return NULL;
30592475b1ccSMax Stepanov 
30602475b1ccSMax Stepanov 	return cs;
30612475b1ccSMax Stepanov }
30622475b1ccSMax Stepanov 
30632475b1ccSMax Stepanov int ieee80211_cs_headroom(struct ieee80211_local *local,
30642475b1ccSMax Stepanov 			  struct cfg80211_crypto_settings *crypto,
30652475b1ccSMax Stepanov 			  enum nl80211_iftype iftype)
30662475b1ccSMax Stepanov {
30672475b1ccSMax Stepanov 	const struct ieee80211_cipher_scheme *cs;
30682475b1ccSMax Stepanov 	int headroom = IEEE80211_ENCRYPT_HEADROOM;
30692475b1ccSMax Stepanov 	int i;
30702475b1ccSMax Stepanov 
30712475b1ccSMax Stepanov 	for (i = 0; i < crypto->n_ciphers_pairwise; i++) {
30722475b1ccSMax Stepanov 		cs = ieee80211_cs_get(local, crypto->ciphers_pairwise[i],
30732475b1ccSMax Stepanov 				      iftype);
30742475b1ccSMax Stepanov 
30752475b1ccSMax Stepanov 		if (cs && headroom < cs->hdr_len)
30762475b1ccSMax Stepanov 			headroom = cs->hdr_len;
30772475b1ccSMax Stepanov 	}
30782475b1ccSMax Stepanov 
30792475b1ccSMax Stepanov 	cs = ieee80211_cs_get(local, crypto->cipher_group, iftype);
30802475b1ccSMax Stepanov 	if (cs && headroom < cs->hdr_len)
30812475b1ccSMax Stepanov 		headroom = cs->hdr_len;
30822475b1ccSMax Stepanov 
30832475b1ccSMax Stepanov 	return headroom;
30842475b1ccSMax Stepanov }
3085a7022e65SFelix Fietkau 
3086a7022e65SFelix Fietkau static bool
3087a7022e65SFelix Fietkau ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
3088a7022e65SFelix Fietkau {
3089a7022e65SFelix Fietkau 	s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
3090a7022e65SFelix Fietkau 	int skip;
3091a7022e65SFelix Fietkau 
3092a7022e65SFelix Fietkau 	if (end > 0)
3093a7022e65SFelix Fietkau 		return false;
3094a7022e65SFelix Fietkau 
3095519ee691SJanusz.Dziedzic@tieto.com 	/* One shot NOA  */
3096519ee691SJanusz.Dziedzic@tieto.com 	if (data->count[i] == 1)
3097519ee691SJanusz.Dziedzic@tieto.com 		return false;
3098519ee691SJanusz.Dziedzic@tieto.com 
3099519ee691SJanusz.Dziedzic@tieto.com 	if (data->desc[i].interval == 0)
3100519ee691SJanusz.Dziedzic@tieto.com 		return false;
3101519ee691SJanusz.Dziedzic@tieto.com 
3102a7022e65SFelix Fietkau 	/* End time is in the past, check for repetitions */
3103a7022e65SFelix Fietkau 	skip = DIV_ROUND_UP(-end, data->desc[i].interval);
3104a7022e65SFelix Fietkau 	if (data->count[i] < 255) {
3105a7022e65SFelix Fietkau 		if (data->count[i] <= skip) {
3106a7022e65SFelix Fietkau 			data->count[i] = 0;
3107a7022e65SFelix Fietkau 			return false;
3108a7022e65SFelix Fietkau 		}
3109a7022e65SFelix Fietkau 
3110a7022e65SFelix Fietkau 		data->count[i] -= skip;
3111a7022e65SFelix Fietkau 	}
3112a7022e65SFelix Fietkau 
3113a7022e65SFelix Fietkau 	data->desc[i].start += skip * data->desc[i].interval;
3114a7022e65SFelix Fietkau 
3115a7022e65SFelix Fietkau 	return true;
3116a7022e65SFelix Fietkau }
3117a7022e65SFelix Fietkau 
3118a7022e65SFelix Fietkau static bool
3119a7022e65SFelix Fietkau ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
3120a7022e65SFelix Fietkau 			     s32 *offset)
3121a7022e65SFelix Fietkau {
3122a7022e65SFelix Fietkau 	bool ret = false;
3123a7022e65SFelix Fietkau 	int i;
3124a7022e65SFelix Fietkau 
3125a7022e65SFelix Fietkau 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3126a7022e65SFelix Fietkau 		s32 cur;
3127a7022e65SFelix Fietkau 
3128a7022e65SFelix Fietkau 		if (!data->count[i])
3129a7022e65SFelix Fietkau 			continue;
3130a7022e65SFelix Fietkau 
3131a7022e65SFelix Fietkau 		if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
3132a7022e65SFelix Fietkau 			ret = true;
3133a7022e65SFelix Fietkau 
3134a7022e65SFelix Fietkau 		cur = data->desc[i].start - tsf;
3135a7022e65SFelix Fietkau 		if (cur > *offset)
3136a7022e65SFelix Fietkau 			continue;
3137a7022e65SFelix Fietkau 
3138a7022e65SFelix Fietkau 		cur = data->desc[i].start + data->desc[i].duration - tsf;
3139a7022e65SFelix Fietkau 		if (cur > *offset)
3140a7022e65SFelix Fietkau 			*offset = cur;
3141a7022e65SFelix Fietkau 	}
3142a7022e65SFelix Fietkau 
3143a7022e65SFelix Fietkau 	return ret;
3144a7022e65SFelix Fietkau }
3145a7022e65SFelix Fietkau 
3146a7022e65SFelix Fietkau static u32
3147a7022e65SFelix Fietkau ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
3148a7022e65SFelix Fietkau {
3149a7022e65SFelix Fietkau 	s32 offset = 0;
3150a7022e65SFelix Fietkau 	int tries = 0;
3151a7022e65SFelix Fietkau 	/*
3152a7022e65SFelix Fietkau 	 * arbitrary limit, used to avoid infinite loops when combined NoA
3153a7022e65SFelix Fietkau 	 * descriptors cover the full time period.
3154a7022e65SFelix Fietkau 	 */
3155a7022e65SFelix Fietkau 	int max_tries = 5;
3156a7022e65SFelix Fietkau 
3157a7022e65SFelix Fietkau 	ieee80211_extend_absent_time(data, tsf, &offset);
3158a7022e65SFelix Fietkau 	do {
3159a7022e65SFelix Fietkau 		if (!ieee80211_extend_absent_time(data, tsf, &offset))
3160a7022e65SFelix Fietkau 			break;
3161a7022e65SFelix Fietkau 
3162a7022e65SFelix Fietkau 		tries++;
3163a7022e65SFelix Fietkau 	} while (tries < max_tries);
3164a7022e65SFelix Fietkau 
3165a7022e65SFelix Fietkau 	return offset;
3166a7022e65SFelix Fietkau }
3167a7022e65SFelix Fietkau 
3168a7022e65SFelix Fietkau void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
3169a7022e65SFelix Fietkau {
3170a7022e65SFelix Fietkau 	u32 next_offset = BIT(31) - 1;
3171a7022e65SFelix Fietkau 	int i;
3172a7022e65SFelix Fietkau 
3173a7022e65SFelix Fietkau 	data->absent = 0;
3174a7022e65SFelix Fietkau 	data->has_next_tsf = false;
3175a7022e65SFelix Fietkau 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3176a7022e65SFelix Fietkau 		s32 start;
3177a7022e65SFelix Fietkau 
3178a7022e65SFelix Fietkau 		if (!data->count[i])
3179a7022e65SFelix Fietkau 			continue;
3180a7022e65SFelix Fietkau 
3181a7022e65SFelix Fietkau 		ieee80211_extend_noa_desc(data, tsf, i);
3182a7022e65SFelix Fietkau 		start = data->desc[i].start - tsf;
3183a7022e65SFelix Fietkau 		if (start <= 0)
3184a7022e65SFelix Fietkau 			data->absent |= BIT(i);
3185a7022e65SFelix Fietkau 
3186a7022e65SFelix Fietkau 		if (next_offset > start)
3187a7022e65SFelix Fietkau 			next_offset = start;
3188a7022e65SFelix Fietkau 
3189a7022e65SFelix Fietkau 		data->has_next_tsf = true;
3190a7022e65SFelix Fietkau 	}
3191a7022e65SFelix Fietkau 
3192a7022e65SFelix Fietkau 	if (data->absent)
3193a7022e65SFelix Fietkau 		next_offset = ieee80211_get_noa_absent_time(data, tsf);
3194a7022e65SFelix Fietkau 
3195a7022e65SFelix Fietkau 	data->next_tsf = tsf + next_offset;
3196a7022e65SFelix Fietkau }
3197a7022e65SFelix Fietkau EXPORT_SYMBOL(ieee80211_update_p2p_noa);
3198a7022e65SFelix Fietkau 
3199a7022e65SFelix Fietkau int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
3200a7022e65SFelix Fietkau 			    struct ieee80211_noa_data *data, u32 tsf)
3201a7022e65SFelix Fietkau {
3202a7022e65SFelix Fietkau 	int ret = 0;
3203a7022e65SFelix Fietkau 	int i;
3204a7022e65SFelix Fietkau 
3205a7022e65SFelix Fietkau 	memset(data, 0, sizeof(*data));
3206a7022e65SFelix Fietkau 
3207a7022e65SFelix Fietkau 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3208a7022e65SFelix Fietkau 		const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
3209a7022e65SFelix Fietkau 
3210a7022e65SFelix Fietkau 		if (!desc->count || !desc->duration)
3211a7022e65SFelix Fietkau 			continue;
3212a7022e65SFelix Fietkau 
3213a7022e65SFelix Fietkau 		data->count[i] = desc->count;
3214a7022e65SFelix Fietkau 		data->desc[i].start = le32_to_cpu(desc->start_time);
3215a7022e65SFelix Fietkau 		data->desc[i].duration = le32_to_cpu(desc->duration);
3216a7022e65SFelix Fietkau 		data->desc[i].interval = le32_to_cpu(desc->interval);
3217a7022e65SFelix Fietkau 
3218a7022e65SFelix Fietkau 		if (data->count[i] > 1 &&
3219a7022e65SFelix Fietkau 		    data->desc[i].interval < data->desc[i].duration)
3220a7022e65SFelix Fietkau 			continue;
3221a7022e65SFelix Fietkau 
3222a7022e65SFelix Fietkau 		ieee80211_extend_noa_desc(data, tsf, i);
3223a7022e65SFelix Fietkau 		ret++;
3224a7022e65SFelix Fietkau 	}
3225a7022e65SFelix Fietkau 
3226a7022e65SFelix Fietkau 	if (ret)
3227a7022e65SFelix Fietkau 		ieee80211_update_p2p_noa(data, tsf);
3228a7022e65SFelix Fietkau 
3229a7022e65SFelix Fietkau 	return ret;
3230a7022e65SFelix Fietkau }
3231a7022e65SFelix Fietkau EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
3232057d5f4bSThomas Pedersen 
3233057d5f4bSThomas Pedersen void ieee80211_recalc_dtim(struct ieee80211_local *local,
3234057d5f4bSThomas Pedersen 			   struct ieee80211_sub_if_data *sdata)
3235057d5f4bSThomas Pedersen {
3236057d5f4bSThomas Pedersen 	u64 tsf = drv_get_tsf(local, sdata);
3237057d5f4bSThomas Pedersen 	u64 dtim_count = 0;
3238057d5f4bSThomas Pedersen 	u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
3239057d5f4bSThomas Pedersen 	u8 dtim_period = sdata->vif.bss_conf.dtim_period;
3240057d5f4bSThomas Pedersen 	struct ps_data *ps;
3241057d5f4bSThomas Pedersen 	u8 bcns_from_dtim;
3242057d5f4bSThomas Pedersen 
3243057d5f4bSThomas Pedersen 	if (tsf == -1ULL || !beacon_int || !dtim_period)
3244057d5f4bSThomas Pedersen 		return;
3245057d5f4bSThomas Pedersen 
3246057d5f4bSThomas Pedersen 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
3247057d5f4bSThomas Pedersen 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
3248057d5f4bSThomas Pedersen 		if (!sdata->bss)
3249057d5f4bSThomas Pedersen 			return;
3250057d5f4bSThomas Pedersen 
3251057d5f4bSThomas Pedersen 		ps = &sdata->bss->ps;
3252057d5f4bSThomas Pedersen 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
3253057d5f4bSThomas Pedersen 		ps = &sdata->u.mesh.ps;
3254057d5f4bSThomas Pedersen 	} else {
3255057d5f4bSThomas Pedersen 		return;
3256057d5f4bSThomas Pedersen 	}
3257057d5f4bSThomas Pedersen 
3258057d5f4bSThomas Pedersen 	/*
3259057d5f4bSThomas Pedersen 	 * actually finds last dtim_count, mac80211 will update in
3260057d5f4bSThomas Pedersen 	 * __beacon_add_tim().
3261057d5f4bSThomas Pedersen 	 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
3262057d5f4bSThomas Pedersen 	 */
3263057d5f4bSThomas Pedersen 	do_div(tsf, beacon_int);
3264057d5f4bSThomas Pedersen 	bcns_from_dtim = do_div(tsf, dtim_period);
3265057d5f4bSThomas Pedersen 	/* just had a DTIM */
3266057d5f4bSThomas Pedersen 	if (!bcns_from_dtim)
3267057d5f4bSThomas Pedersen 		dtim_count = 0;
3268057d5f4bSThomas Pedersen 	else
3269057d5f4bSThomas Pedersen 		dtim_count = dtim_period - bcns_from_dtim;
3270057d5f4bSThomas Pedersen 
3271057d5f4bSThomas Pedersen 	ps->dtim_count = dtim_count;
3272057d5f4bSThomas Pedersen }
327373de86a3SLuciano Coelho 
327471e6195eSMichal Kazior static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
327571e6195eSMichal Kazior 					 struct ieee80211_chanctx *ctx)
327671e6195eSMichal Kazior {
327771e6195eSMichal Kazior 	struct ieee80211_sub_if_data *sdata;
327871e6195eSMichal Kazior 	u8 radar_detect = 0;
327971e6195eSMichal Kazior 
328071e6195eSMichal Kazior 	lockdep_assert_held(&local->chanctx_mtx);
328171e6195eSMichal Kazior 
328271e6195eSMichal Kazior 	if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
328371e6195eSMichal Kazior 		return 0;
328471e6195eSMichal Kazior 
328571e6195eSMichal Kazior 	list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
328671e6195eSMichal Kazior 		if (sdata->reserved_radar_required)
328771e6195eSMichal Kazior 			radar_detect |= BIT(sdata->reserved_chandef.width);
328871e6195eSMichal Kazior 
328971e6195eSMichal Kazior 	/*
329071e6195eSMichal Kazior 	 * An in-place reservation context should not have any assigned vifs
329171e6195eSMichal Kazior 	 * until it replaces the other context.
329271e6195eSMichal Kazior 	 */
329371e6195eSMichal Kazior 	WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
329471e6195eSMichal Kazior 		!list_empty(&ctx->assigned_vifs));
329571e6195eSMichal Kazior 
329671e6195eSMichal Kazior 	list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
329771e6195eSMichal Kazior 		if (sdata->radar_required)
329871e6195eSMichal Kazior 			radar_detect |= BIT(sdata->vif.bss_conf.chandef.width);
329971e6195eSMichal Kazior 
330071e6195eSMichal Kazior 	return radar_detect;
330171e6195eSMichal Kazior }
330271e6195eSMichal Kazior 
330373de86a3SLuciano Coelho int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
330473de86a3SLuciano Coelho 				 const struct cfg80211_chan_def *chandef,
330573de86a3SLuciano Coelho 				 enum ieee80211_chanctx_mode chanmode,
330673de86a3SLuciano Coelho 				 u8 radar_detect)
330773de86a3SLuciano Coelho {
330873de86a3SLuciano Coelho 	struct ieee80211_local *local = sdata->local;
330973de86a3SLuciano Coelho 	struct ieee80211_sub_if_data *sdata_iter;
331073de86a3SLuciano Coelho 	enum nl80211_iftype iftype = sdata->wdev.iftype;
331173de86a3SLuciano Coelho 	struct ieee80211_chanctx *ctx;
331273de86a3SLuciano Coelho 	int total = 1;
3313e227300cSPurushottam Kushwaha 	struct iface_combination_params params = {
3314e227300cSPurushottam Kushwaha 		.radar_detect = radar_detect,
3315e227300cSPurushottam Kushwaha 	};
331673de86a3SLuciano Coelho 
331773de86a3SLuciano Coelho 	lockdep_assert_held(&local->chanctx_mtx);
331873de86a3SLuciano Coelho 
331973de86a3SLuciano Coelho 	if (WARN_ON(hweight32(radar_detect) > 1))
332073de86a3SLuciano Coelho 		return -EINVAL;
332173de86a3SLuciano Coelho 
3322b6a55015SLuciano Coelho 	if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
3323b6a55015SLuciano Coelho 		    !chandef->chan))
332473de86a3SLuciano Coelho 		return -EINVAL;
332573de86a3SLuciano Coelho 
332673de86a3SLuciano Coelho 	if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
332773de86a3SLuciano Coelho 		return -EINVAL;
332873de86a3SLuciano Coelho 
3329ac668afeSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
3330ac668afeSJohannes Berg 	    sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
3331ac668afeSJohannes Berg 		/*
3332ac668afeSJohannes Berg 		 * always passing this is harmless, since it'll be the
3333ac668afeSJohannes Berg 		 * same value that cfg80211 finds if it finds the same
3334ac668afeSJohannes Berg 		 * interface ... and that's always allowed
3335ac668afeSJohannes Berg 		 */
3336ac668afeSJohannes Berg 		params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
3337ac668afeSJohannes Berg 	}
3338ac668afeSJohannes Berg 
333973de86a3SLuciano Coelho 	/* Always allow software iftypes */
334073de86a3SLuciano Coelho 	if (local->hw.wiphy->software_iftypes & BIT(iftype)) {
334173de86a3SLuciano Coelho 		if (radar_detect)
334273de86a3SLuciano Coelho 			return -EINVAL;
334373de86a3SLuciano Coelho 		return 0;
334473de86a3SLuciano Coelho 	}
334573de86a3SLuciano Coelho 
3346e227300cSPurushottam Kushwaha 	if (chandef)
3347e227300cSPurushottam Kushwaha 		params.num_different_channels = 1;
334873de86a3SLuciano Coelho 
334973de86a3SLuciano Coelho 	if (iftype != NL80211_IFTYPE_UNSPECIFIED)
3350e227300cSPurushottam Kushwaha 		params.iftype_num[iftype] = 1;
335173de86a3SLuciano Coelho 
335273de86a3SLuciano Coelho 	list_for_each_entry(ctx, &local->chanctx_list, list) {
33535bcae31dSMichal Kazior 		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
33545bcae31dSMichal Kazior 			continue;
3355e227300cSPurushottam Kushwaha 		params.radar_detect |=
3356e227300cSPurushottam Kushwaha 			ieee80211_chanctx_radar_detect(local, ctx);
335773de86a3SLuciano Coelho 		if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
3358e227300cSPurushottam Kushwaha 			params.num_different_channels++;
335973de86a3SLuciano Coelho 			continue;
336073de86a3SLuciano Coelho 		}
3361b6a55015SLuciano Coelho 		if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
336273de86a3SLuciano Coelho 		    cfg80211_chandef_compatible(chandef,
336373de86a3SLuciano Coelho 						&ctx->conf.def))
336473de86a3SLuciano Coelho 			continue;
3365e227300cSPurushottam Kushwaha 		params.num_different_channels++;
336673de86a3SLuciano Coelho 	}
336773de86a3SLuciano Coelho 
336873de86a3SLuciano Coelho 	list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
336973de86a3SLuciano Coelho 		struct wireless_dev *wdev_iter;
337073de86a3SLuciano Coelho 
337173de86a3SLuciano Coelho 		wdev_iter = &sdata_iter->wdev;
337273de86a3SLuciano Coelho 
337373de86a3SLuciano Coelho 		if (sdata_iter == sdata ||
33740f611d28SAndrei Otcheretianski 		    !ieee80211_sdata_running(sdata_iter) ||
337573de86a3SLuciano Coelho 		    local->hw.wiphy->software_iftypes & BIT(wdev_iter->iftype))
337673de86a3SLuciano Coelho 			continue;
337773de86a3SLuciano Coelho 
3378e227300cSPurushottam Kushwaha 		params.iftype_num[wdev_iter->iftype]++;
337973de86a3SLuciano Coelho 		total++;
338073de86a3SLuciano Coelho 	}
338173de86a3SLuciano Coelho 
3382e227300cSPurushottam Kushwaha 	if (total == 1 && !params.radar_detect)
338373de86a3SLuciano Coelho 		return 0;
338473de86a3SLuciano Coelho 
3385e227300cSPurushottam Kushwaha 	return cfg80211_check_combinations(local->hw.wiphy, &params);
338673de86a3SLuciano Coelho }
33876fa001bcSMichal Kazior 
33886fa001bcSMichal Kazior static void
33896fa001bcSMichal Kazior ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
33906fa001bcSMichal Kazior 			 void *data)
33916fa001bcSMichal Kazior {
33926fa001bcSMichal Kazior 	u32 *max_num_different_channels = data;
33936fa001bcSMichal Kazior 
33946fa001bcSMichal Kazior 	*max_num_different_channels = max(*max_num_different_channels,
33956fa001bcSMichal Kazior 					  c->num_different_channels);
33966fa001bcSMichal Kazior }
33976fa001bcSMichal Kazior 
33986fa001bcSMichal Kazior int ieee80211_max_num_channels(struct ieee80211_local *local)
33996fa001bcSMichal Kazior {
34006fa001bcSMichal Kazior 	struct ieee80211_sub_if_data *sdata;
34016fa001bcSMichal Kazior 	struct ieee80211_chanctx *ctx;
34026fa001bcSMichal Kazior 	u32 max_num_different_channels = 1;
34036fa001bcSMichal Kazior 	int err;
3404e227300cSPurushottam Kushwaha 	struct iface_combination_params params = {0};
34056fa001bcSMichal Kazior 
34066fa001bcSMichal Kazior 	lockdep_assert_held(&local->chanctx_mtx);
34076fa001bcSMichal Kazior 
34086fa001bcSMichal Kazior 	list_for_each_entry(ctx, &local->chanctx_list, list) {
34095bcae31dSMichal Kazior 		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
34105bcae31dSMichal Kazior 			continue;
34115bcae31dSMichal Kazior 
3412e227300cSPurushottam Kushwaha 		params.num_different_channels++;
34136fa001bcSMichal Kazior 
3414e227300cSPurushottam Kushwaha 		params.radar_detect |=
3415e227300cSPurushottam Kushwaha 			ieee80211_chanctx_radar_detect(local, ctx);
34166fa001bcSMichal Kazior 	}
34176fa001bcSMichal Kazior 
34186fa001bcSMichal Kazior 	list_for_each_entry_rcu(sdata, &local->interfaces, list)
3419e227300cSPurushottam Kushwaha 		params.iftype_num[sdata->wdev.iftype]++;
34206fa001bcSMichal Kazior 
3421e227300cSPurushottam Kushwaha 	err = cfg80211_iter_combinations(local->hw.wiphy, &params,
3422e227300cSPurushottam Kushwaha 					 ieee80211_iter_max_chans,
34236fa001bcSMichal Kazior 					 &max_num_different_channels);
34246fa001bcSMichal Kazior 	if (err < 0)
34256fa001bcSMichal Kazior 		return err;
34266fa001bcSMichal Kazior 
34276fa001bcSMichal Kazior 	return max_num_different_channels;
34286fa001bcSMichal Kazior }
342940b861a0SArik Nemtsov 
343040b861a0SArik Nemtsov u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
343140b861a0SArik Nemtsov {
343240b861a0SArik Nemtsov 	*buf++ = WLAN_EID_VENDOR_SPECIFIC;
343340b861a0SArik Nemtsov 	*buf++ = 7; /* len */
343440b861a0SArik Nemtsov 	*buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
343540b861a0SArik Nemtsov 	*buf++ = 0x50;
343640b861a0SArik Nemtsov 	*buf++ = 0xf2;
343740b861a0SArik Nemtsov 	*buf++ = 2; /* WME */
343840b861a0SArik Nemtsov 	*buf++ = 0; /* WME info */
343940b861a0SArik Nemtsov 	*buf++ = 1; /* WME ver */
344040b861a0SArik Nemtsov 	*buf++ = qosinfo; /* U-APSD no in use */
344140b861a0SArik Nemtsov 
344240b861a0SArik Nemtsov 	return buf;
344340b861a0SArik Nemtsov }
3444ba8c3d6fSFelix Fietkau 
3445f2ac7e30SMichal Kazior void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
3446f2ac7e30SMichal Kazior 			     unsigned long *frame_cnt,
3447f2ac7e30SMichal Kazior 			     unsigned long *byte_cnt)
3448f2ac7e30SMichal Kazior {
3449f2ac7e30SMichal Kazior 	struct txq_info *txqi = to_txq_info(txq);
3450bb42f2d1SToke Høiland-Jørgensen 	u32 frag_cnt = 0, frag_bytes = 0;
3451bb42f2d1SToke Høiland-Jørgensen 	struct sk_buff *skb;
3452bb42f2d1SToke Høiland-Jørgensen 
3453bb42f2d1SToke Høiland-Jørgensen 	skb_queue_walk(&txqi->frags, skb) {
3454bb42f2d1SToke Høiland-Jørgensen 		frag_cnt++;
3455bb42f2d1SToke Høiland-Jørgensen 		frag_bytes += skb->len;
3456bb42f2d1SToke Høiland-Jørgensen 	}
3457f2ac7e30SMichal Kazior 
3458f2ac7e30SMichal Kazior 	if (frame_cnt)
3459bb42f2d1SToke Høiland-Jørgensen 		*frame_cnt = txqi->tin.backlog_packets + frag_cnt;
3460f2ac7e30SMichal Kazior 
3461f2ac7e30SMichal Kazior 	if (byte_cnt)
3462bb42f2d1SToke Høiland-Jørgensen 		*byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
3463f2ac7e30SMichal Kazior }
3464f2ac7e30SMichal Kazior EXPORT_SYMBOL(ieee80211_txq_get_depth);
3465f438ceb8SEmmanuel Grumbach 
3466f438ceb8SEmmanuel Grumbach const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
3467f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
3468f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
3469f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
3470f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
3471f438ceb8SEmmanuel Grumbach };
3472