xref: /openbmc/linux/net/mac80211/util.c (revision e38a017b)
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
7da6a4352SJohannes Berg  * Copyright (C) 2015-2017	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:
831e38a017bSAvraham Stern 		case WLAN_EID_BSS_MAX_IDLE_PERIOD:
832b2e506bfSJohannes Berg 		/*
833b2e506bfSJohannes Berg 		 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
834b2e506bfSJohannes Berg 		 * that if the content gets bigger it might be needed more than once
835b2e506bfSJohannes Berg 		 */
8369690fb16SJohannes Berg 			if (test_bit(id, seen_elems)) {
837fcff4f10SPaul Stewart 				elems->parse_error = true;
838fcff4f10SPaul Stewart 				left -= elen;
839fcff4f10SPaul Stewart 				pos += elen;
840fcff4f10SPaul Stewart 				continue;
841fcff4f10SPaul Stewart 			}
8429690fb16SJohannes Berg 			break;
8439690fb16SJohannes Berg 		}
844dd76986bSJohannes Berg 
845dd76986bSJohannes Berg 		if (calc_crc && id < 64 && (filter & (1ULL << id)))
846dd76986bSJohannes Berg 			crc = crc32_be(crc, pos - 2, elen + 2);
847dd76986bSJohannes Berg 
848fcff4f10SPaul Stewart 		elem_parse_failed = false;
849fcff4f10SPaul Stewart 
850dd76986bSJohannes Berg 		switch (id) {
85153837584SArik Nemtsov 		case WLAN_EID_LINK_ID:
85253837584SArik Nemtsov 			if (elen + 2 != sizeof(struct ieee80211_tdls_lnkie)) {
85353837584SArik Nemtsov 				elem_parse_failed = true;
85453837584SArik Nemtsov 				break;
85553837584SArik Nemtsov 			}
85653837584SArik Nemtsov 			elems->lnk_id = (void *)(pos - 2);
85753837584SArik Nemtsov 			break;
85853837584SArik Nemtsov 		case WLAN_EID_CHAN_SWITCH_TIMING:
85953837584SArik Nemtsov 			if (elen != sizeof(struct ieee80211_ch_switch_timing)) {
86053837584SArik Nemtsov 				elem_parse_failed = true;
86153837584SArik Nemtsov 				break;
86253837584SArik Nemtsov 			}
86353837584SArik Nemtsov 			elems->ch_sw_timing = (void *)pos;
86453837584SArik Nemtsov 			break;
8659041c1faSArik Nemtsov 		case WLAN_EID_EXT_CAPABILITY:
8669041c1faSArik Nemtsov 			elems->ext_capab = pos;
8679041c1faSArik Nemtsov 			elems->ext_capab_len = elen;
8689041c1faSArik Nemtsov 			break;
869dd76986bSJohannes Berg 		case WLAN_EID_SSID:
870dd76986bSJohannes Berg 			elems->ssid = pos;
871dd76986bSJohannes Berg 			elems->ssid_len = elen;
872dd76986bSJohannes Berg 			break;
873dd76986bSJohannes Berg 		case WLAN_EID_SUPP_RATES:
874dd76986bSJohannes Berg 			elems->supp_rates = pos;
875dd76986bSJohannes Berg 			elems->supp_rates_len = elen;
876dd76986bSJohannes Berg 			break;
877dd76986bSJohannes Berg 		case WLAN_EID_DS_PARAMS:
8781cd8e88eSJohannes Berg 			if (elen >= 1)
879dd76986bSJohannes Berg 				elems->ds_params = pos;
8801cd8e88eSJohannes Berg 			else
8811cd8e88eSJohannes Berg 				elem_parse_failed = true;
882dd76986bSJohannes Berg 			break;
883dd76986bSJohannes Berg 		case WLAN_EID_TIM:
884dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_tim_ie)) {
885dd76986bSJohannes Berg 				elems->tim = (void *)pos;
886dd76986bSJohannes Berg 				elems->tim_len = elen;
887fcff4f10SPaul Stewart 			} else
888fcff4f10SPaul Stewart 				elem_parse_failed = true;
889dd76986bSJohannes Berg 			break;
890dd76986bSJohannes Berg 		case WLAN_EID_CHALLENGE:
891dd76986bSJohannes Berg 			elems->challenge = pos;
892dd76986bSJohannes Berg 			elems->challenge_len = elen;
893dd76986bSJohannes Berg 			break;
894dd76986bSJohannes Berg 		case WLAN_EID_VENDOR_SPECIFIC:
895dd76986bSJohannes Berg 			if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
896dd76986bSJohannes Berg 			    pos[2] == 0xf2) {
897dd76986bSJohannes Berg 				/* Microsoft OUI (00:50:F2) */
898dd76986bSJohannes Berg 
899dd76986bSJohannes Berg 				if (calc_crc)
900dd76986bSJohannes Berg 					crc = crc32_be(crc, pos - 2, elen + 2);
901dd76986bSJohannes Berg 
902441a33baSJohannes Berg 				if (elen >= 5 && pos[3] == 2) {
903dd76986bSJohannes Berg 					/* OUI Type 2 - WMM IE */
904dd76986bSJohannes Berg 					if (pos[4] == 0) {
905dd76986bSJohannes Berg 						elems->wmm_info = pos;
906dd76986bSJohannes Berg 						elems->wmm_info_len = elen;
907dd76986bSJohannes Berg 					} else if (pos[4] == 1) {
908dd76986bSJohannes Berg 						elems->wmm_param = pos;
909dd76986bSJohannes Berg 						elems->wmm_param_len = elen;
910dd76986bSJohannes Berg 					}
911dd76986bSJohannes Berg 				}
912dd76986bSJohannes Berg 			}
913dd76986bSJohannes Berg 			break;
914dd76986bSJohannes Berg 		case WLAN_EID_RSN:
915dd76986bSJohannes Berg 			elems->rsn = pos;
916dd76986bSJohannes Berg 			elems->rsn_len = elen;
917dd76986bSJohannes Berg 			break;
918dd76986bSJohannes Berg 		case WLAN_EID_ERP_INFO:
9191946bed9SJohannes Berg 			if (elen >= 1)
920dd76986bSJohannes Berg 				elems->erp_info = pos;
9211946bed9SJohannes Berg 			else
9221946bed9SJohannes Berg 				elem_parse_failed = true;
923dd76986bSJohannes Berg 			break;
924dd76986bSJohannes Berg 		case WLAN_EID_EXT_SUPP_RATES:
925dd76986bSJohannes Berg 			elems->ext_supp_rates = pos;
926dd76986bSJohannes Berg 			elems->ext_supp_rates_len = elen;
927dd76986bSJohannes Berg 			break;
928dd76986bSJohannes Berg 		case WLAN_EID_HT_CAPABILITY:
929dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_ht_cap))
930dd76986bSJohannes Berg 				elems->ht_cap_elem = (void *)pos;
931fcff4f10SPaul Stewart 			else
932fcff4f10SPaul Stewart 				elem_parse_failed = true;
933dd76986bSJohannes Berg 			break;
934074d46d1SJohannes Berg 		case WLAN_EID_HT_OPERATION:
935074d46d1SJohannes Berg 			if (elen >= sizeof(struct ieee80211_ht_operation))
936074d46d1SJohannes Berg 				elems->ht_operation = (void *)pos;
937fcff4f10SPaul Stewart 			else
938fcff4f10SPaul Stewart 				elem_parse_failed = true;
939dd76986bSJohannes Berg 			break;
940818255eaSMahesh Palivela 		case WLAN_EID_VHT_CAPABILITY:
941818255eaSMahesh Palivela 			if (elen >= sizeof(struct ieee80211_vht_cap))
942818255eaSMahesh Palivela 				elems->vht_cap_elem = (void *)pos;
943818255eaSMahesh Palivela 			else
944818255eaSMahesh Palivela 				elem_parse_failed = true;
945818255eaSMahesh Palivela 			break;
946818255eaSMahesh Palivela 		case WLAN_EID_VHT_OPERATION:
947818255eaSMahesh Palivela 			if (elen >= sizeof(struct ieee80211_vht_operation))
948818255eaSMahesh Palivela 				elems->vht_operation = (void *)pos;
949818255eaSMahesh Palivela 			else
950818255eaSMahesh Palivela 				elem_parse_failed = true;
951818255eaSMahesh Palivela 			break;
952bee7f586SJohannes Berg 		case WLAN_EID_OPMODE_NOTIF:
953bee7f586SJohannes Berg 			if (elen > 0)
954bee7f586SJohannes Berg 				elems->opmode_notif = pos;
955bee7f586SJohannes Berg 			else
956bee7f586SJohannes Berg 				elem_parse_failed = true;
957bee7f586SJohannes Berg 			break;
958dd76986bSJohannes Berg 		case WLAN_EID_MESH_ID:
959dd76986bSJohannes Berg 			elems->mesh_id = pos;
960dd76986bSJohannes Berg 			elems->mesh_id_len = elen;
961dd76986bSJohannes Berg 			break;
962dd76986bSJohannes Berg 		case WLAN_EID_MESH_CONFIG:
963dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_meshconf_ie))
964dd76986bSJohannes Berg 				elems->mesh_config = (void *)pos;
965fcff4f10SPaul Stewart 			else
966fcff4f10SPaul Stewart 				elem_parse_failed = true;
967dd76986bSJohannes Berg 			break;
968dd76986bSJohannes Berg 		case WLAN_EID_PEER_MGMT:
969dd76986bSJohannes Berg 			elems->peering = pos;
970dd76986bSJohannes Berg 			elems->peering_len = elen;
971dd76986bSJohannes Berg 			break;
9723f52b7e3SMarco Porsch 		case WLAN_EID_MESH_AWAKE_WINDOW:
9733f52b7e3SMarco Porsch 			if (elen >= 2)
9743f52b7e3SMarco Porsch 				elems->awake_window = (void *)pos;
9753f52b7e3SMarco Porsch 			break;
976dd76986bSJohannes Berg 		case WLAN_EID_PREQ:
977dd76986bSJohannes Berg 			elems->preq = pos;
978dd76986bSJohannes Berg 			elems->preq_len = elen;
979dd76986bSJohannes Berg 			break;
980dd76986bSJohannes Berg 		case WLAN_EID_PREP:
981dd76986bSJohannes Berg 			elems->prep = pos;
982dd76986bSJohannes Berg 			elems->prep_len = elen;
983dd76986bSJohannes Berg 			break;
984dd76986bSJohannes Berg 		case WLAN_EID_PERR:
985dd76986bSJohannes Berg 			elems->perr = pos;
986dd76986bSJohannes Berg 			elems->perr_len = elen;
987dd76986bSJohannes Berg 			break;
988dd76986bSJohannes Berg 		case WLAN_EID_RANN:
989dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_rann_ie))
990dd76986bSJohannes Berg 				elems->rann = (void *)pos;
991fcff4f10SPaul Stewart 			else
992fcff4f10SPaul Stewart 				elem_parse_failed = true;
993dd76986bSJohannes Berg 			break;
994dd76986bSJohannes Berg 		case WLAN_EID_CHANNEL_SWITCH:
9955bc1420bSJohannes Berg 			if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
9965bc1420bSJohannes Berg 				elem_parse_failed = true;
9975bc1420bSJohannes Berg 				break;
9985bc1420bSJohannes Berg 			}
9995bc1420bSJohannes Berg 			elems->ch_switch_ie = (void *)pos;
1000dd76986bSJohannes Berg 			break;
1001b4f286a1SJohannes Berg 		case WLAN_EID_EXT_CHANSWITCH_ANN:
1002b4f286a1SJohannes Berg 			if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
1003b4f286a1SJohannes Berg 				elem_parse_failed = true;
1004b4f286a1SJohannes Berg 				break;
1005b4f286a1SJohannes Berg 			}
1006b4f286a1SJohannes Berg 			elems->ext_chansw_ie = (void *)pos;
1007b4f286a1SJohannes Berg 			break;
100885220d71SJohannes Berg 		case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
100985220d71SJohannes Berg 			if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
101085220d71SJohannes Berg 				elem_parse_failed = true;
101185220d71SJohannes Berg 				break;
101285220d71SJohannes Berg 			}
101385220d71SJohannes Berg 			elems->sec_chan_offs = (void *)pos;
101485220d71SJohannes Berg 			break;
10158f2535b9SChun-Yeow Yeoh 		case WLAN_EID_CHAN_SWITCH_PARAM:
10168f2535b9SChun-Yeow Yeoh 			if (elen !=
10178f2535b9SChun-Yeow Yeoh 			    sizeof(*elems->mesh_chansw_params_ie)) {
10188f2535b9SChun-Yeow Yeoh 				elem_parse_failed = true;
10198f2535b9SChun-Yeow Yeoh 				break;
10208f2535b9SChun-Yeow Yeoh 			}
10218f2535b9SChun-Yeow Yeoh 			elems->mesh_chansw_params_ie = (void *)pos;
10228f2535b9SChun-Yeow Yeoh 			break;
1023b2e506bfSJohannes Berg 		case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1024b2e506bfSJohannes Berg 			if (!action ||
1025b2e506bfSJohannes Berg 			    elen != sizeof(*elems->wide_bw_chansw_ie)) {
1026b2e506bfSJohannes Berg 				elem_parse_failed = true;
1027b2e506bfSJohannes Berg 				break;
1028b2e506bfSJohannes Berg 			}
1029b2e506bfSJohannes Berg 			elems->wide_bw_chansw_ie = (void *)pos;
1030b2e506bfSJohannes Berg 			break;
1031b2e506bfSJohannes Berg 		case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
1032b2e506bfSJohannes Berg 			if (action) {
1033b2e506bfSJohannes Berg 				elem_parse_failed = true;
1034b2e506bfSJohannes Berg 				break;
1035b2e506bfSJohannes Berg 			}
1036b2e506bfSJohannes Berg 			/*
1037b2e506bfSJohannes Berg 			 * This is a bit tricky, but as we only care about
1038b2e506bfSJohannes Berg 			 * the wide bandwidth channel switch element, so
1039b2e506bfSJohannes Berg 			 * just parse it out manually.
1040b2e506bfSJohannes Berg 			 */
1041b2e506bfSJohannes Berg 			ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
1042b2e506bfSJohannes Berg 					      pos, elen);
1043b2e506bfSJohannes Berg 			if (ie) {
1044b2e506bfSJohannes Berg 				if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
1045b2e506bfSJohannes Berg 					elems->wide_bw_chansw_ie =
1046b2e506bfSJohannes Berg 						(void *)(ie + 2);
1047b2e506bfSJohannes Berg 				else
1048b2e506bfSJohannes Berg 					elem_parse_failed = true;
1049b2e506bfSJohannes Berg 			}
1050b2e506bfSJohannes Berg 			break;
1051dd76986bSJohannes Berg 		case WLAN_EID_COUNTRY:
1052dd76986bSJohannes Berg 			elems->country_elem = pos;
1053dd76986bSJohannes Berg 			elems->country_elem_len = elen;
1054dd76986bSJohannes Berg 			break;
1055dd76986bSJohannes Berg 		case WLAN_EID_PWR_CONSTRAINT:
1056761a48d2SJohannes Berg 			if (elen != 1) {
1057761a48d2SJohannes Berg 				elem_parse_failed = true;
1058761a48d2SJohannes Berg 				break;
1059761a48d2SJohannes Berg 			}
1060dd76986bSJohannes Berg 			elems->pwr_constr_elem = pos;
1061dd76986bSJohannes Berg 			break;
1062c8d65917SSteinar H. Gunderson 		case WLAN_EID_CISCO_VENDOR_SPECIFIC:
1063c8d65917SSteinar H. Gunderson 			/* Lots of different options exist, but we only care
1064c8d65917SSteinar H. Gunderson 			 * about the Dynamic Transmit Power Control element.
1065c8d65917SSteinar H. Gunderson 			 * First check for the Cisco OUI, then for the DTPC
1066c8d65917SSteinar H. Gunderson 			 * tag (0x00).
1067c8d65917SSteinar H. Gunderson 			 */
1068c8d65917SSteinar H. Gunderson 			if (elen < 4) {
1069c8d65917SSteinar H. Gunderson 				elem_parse_failed = true;
1070c8d65917SSteinar H. Gunderson 				break;
1071c8d65917SSteinar H. Gunderson 			}
1072c8d65917SSteinar H. Gunderson 
1073c8d65917SSteinar H. Gunderson 			if (pos[0] != 0x00 || pos[1] != 0x40 ||
1074c8d65917SSteinar H. Gunderson 			    pos[2] != 0x96 || pos[3] != 0x00)
1075c8d65917SSteinar H. Gunderson 				break;
1076c8d65917SSteinar H. Gunderson 
1077c8d65917SSteinar H. Gunderson 			if (elen != 6) {
1078c8d65917SSteinar H. Gunderson 				elem_parse_failed = true;
1079c8d65917SSteinar H. Gunderson 				break;
1080c8d65917SSteinar H. Gunderson 			}
1081c8d65917SSteinar H. Gunderson 
1082c8d65917SSteinar H. Gunderson 			if (calc_crc)
1083c8d65917SSteinar H. Gunderson 				crc = crc32_be(crc, pos - 2, elen + 2);
1084c8d65917SSteinar H. Gunderson 
1085c8d65917SSteinar H. Gunderson 			elems->cisco_dtpc_elem = pos;
1086c8d65917SSteinar H. Gunderson 			break;
1087dd76986bSJohannes Berg 		case WLAN_EID_TIMEOUT_INTERVAL:
108879ba1d89SJohannes Berg 			if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
108979ba1d89SJohannes Berg 				elems->timeout_int = (void *)pos;
109079ba1d89SJohannes Berg 			else
109179ba1d89SJohannes Berg 				elem_parse_failed = true;
1092dd76986bSJohannes Berg 			break;
1093e38a017bSAvraham Stern 		case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1094e38a017bSAvraham Stern 			if (elen >= sizeof(*elems->max_idle_period_ie))
1095e38a017bSAvraham Stern 				elems->max_idle_period_ie = (void *)pos;
1096e38a017bSAvraham Stern 			break;
1097dd76986bSJohannes Berg 		default:
1098dd76986bSJohannes Berg 			break;
1099dd76986bSJohannes Berg 		}
1100dd76986bSJohannes Berg 
1101fcff4f10SPaul Stewart 		if (elem_parse_failed)
1102fcff4f10SPaul Stewart 			elems->parse_error = true;
1103fcff4f10SPaul Stewart 		else
11045df45690SJohannes Berg 			__set_bit(id, seen_elems);
1105fcff4f10SPaul Stewart 
1106dd76986bSJohannes Berg 		left -= elen;
1107dd76986bSJohannes Berg 		pos += elen;
1108dd76986bSJohannes Berg 	}
1109dd76986bSJohannes Berg 
1110fcff4f10SPaul Stewart 	if (left != 0)
1111fcff4f10SPaul Stewart 		elems->parse_error = true;
1112fcff4f10SPaul Stewart 
1113dd76986bSJohannes Berg 	return crc;
1114dd76986bSJohannes Berg }
1115dd76986bSJohannes Berg 
11163abead59SJohannes Berg void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
1117cec66283SJohannes Berg 			       bool bss_notify, bool enable_qos)
11185825fe10SJohannes Berg {
11195825fe10SJohannes Berg 	struct ieee80211_local *local = sdata->local;
11205825fe10SJohannes Berg 	struct ieee80211_tx_queue_params qparam;
112155de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
112254bcbc69SJohannes Berg 	int ac;
1123cec66283SJohannes Berg 	bool use_11b;
1124239281f8SRostislav Lisovy 	bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
1125aa837e1dSJohannes Berg 	int aCWmin, aCWmax;
11265825fe10SJohannes Berg 
11275825fe10SJohannes Berg 	if (!local->ops->conf_tx)
11285825fe10SJohannes Berg 		return;
11295825fe10SJohannes Berg 
113054bcbc69SJohannes Berg 	if (local->hw.queues < IEEE80211_NUM_ACS)
113154bcbc69SJohannes Berg 		return;
113254bcbc69SJohannes Berg 
11335825fe10SJohannes Berg 	memset(&qparam, 0, sizeof(qparam));
11345825fe10SJohannes Berg 
113555de908aSJohannes Berg 	rcu_read_lock();
113655de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
113755de908aSJohannes Berg 	use_11b = (chanctx_conf &&
113857fbcce3SJohannes Berg 		   chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
1139aa837e1dSJohannes Berg 		 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
114055de908aSJohannes Berg 	rcu_read_unlock();
11415825fe10SJohannes Berg 
1142239281f8SRostislav Lisovy 	is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
1143239281f8SRostislav Lisovy 
1144aa837e1dSJohannes Berg 	/* Set defaults according to 802.11-2007 Table 7-37 */
1145aa837e1dSJohannes Berg 	aCWmax = 1023;
1146aa837e1dSJohannes Berg 	if (use_11b)
1147aa837e1dSJohannes Berg 		aCWmin = 31;
11485825fe10SJohannes Berg 	else
1149aa837e1dSJohannes Berg 		aCWmin = 15;
11505825fe10SJohannes Berg 
11511f4ffde8SFred Zhou 	/* Confiure old 802.11b/g medium access rules. */
11521f4ffde8SFred Zhou 	qparam.cw_max = aCWmax;
11531f4ffde8SFred Zhou 	qparam.cw_min = aCWmin;
11541f4ffde8SFred Zhou 	qparam.txop = 0;
11551f4ffde8SFred Zhou 	qparam.aifs = 2;
11561f4ffde8SFred Zhou 
11571f4ffde8SFred Zhou 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
11581f4ffde8SFred Zhou 		/* Update if QoS is enabled. */
1159a8ce8544SStanislaw Gruszka 		if (enable_qos) {
116054bcbc69SJohannes Berg 			switch (ac) {
11611d98fb12SJohannes Berg 			case IEEE80211_AC_BK:
11627ba10a8eSJohannes Berg 				qparam.cw_max = aCWmax;
11637ba10a8eSJohannes Berg 				qparam.cw_min = aCWmin;
11645825fe10SJohannes Berg 				qparam.txop = 0;
1165239281f8SRostislav Lisovy 				if (is_ocb)
1166239281f8SRostislav Lisovy 					qparam.aifs = 9;
1167239281f8SRostislav Lisovy 				else
1168aa837e1dSJohannes Berg 					qparam.aifs = 7;
1169aa837e1dSJohannes Berg 				break;
1170a8ce8544SStanislaw Gruszka 			/* never happens but let's not leave undefined */
1171a8ce8544SStanislaw Gruszka 			default:
11721d98fb12SJohannes Berg 			case IEEE80211_AC_BE:
11737ba10a8eSJohannes Berg 				qparam.cw_max = aCWmax;
11747ba10a8eSJohannes Berg 				qparam.cw_min = aCWmin;
1175aa837e1dSJohannes Berg 				qparam.txop = 0;
1176239281f8SRostislav Lisovy 				if (is_ocb)
1177239281f8SRostislav Lisovy 					qparam.aifs = 6;
1178239281f8SRostislav Lisovy 				else
1179aa837e1dSJohannes Berg 					qparam.aifs = 3;
1180aa837e1dSJohannes Berg 				break;
11811d98fb12SJohannes Berg 			case IEEE80211_AC_VI:
1182aa837e1dSJohannes Berg 				qparam.cw_max = aCWmin;
1183aa837e1dSJohannes Berg 				qparam.cw_min = (aCWmin + 1) / 2 - 1;
1184239281f8SRostislav Lisovy 				if (is_ocb)
1185239281f8SRostislav Lisovy 					qparam.txop = 0;
1186239281f8SRostislav Lisovy 				else if (use_11b)
1187aa837e1dSJohannes Berg 					qparam.txop = 6016/32;
1188aa837e1dSJohannes Berg 				else
1189aa837e1dSJohannes Berg 					qparam.txop = 3008/32;
1190239281f8SRostislav Lisovy 
1191239281f8SRostislav Lisovy 				if (is_ocb)
1192239281f8SRostislav Lisovy 					qparam.aifs = 3;
1193239281f8SRostislav Lisovy 				else
1194aa837e1dSJohannes Berg 					qparam.aifs = 2;
1195aa837e1dSJohannes Berg 				break;
11961d98fb12SJohannes Berg 			case IEEE80211_AC_VO:
1197aa837e1dSJohannes Berg 				qparam.cw_max = (aCWmin + 1) / 2 - 1;
1198aa837e1dSJohannes Berg 				qparam.cw_min = (aCWmin + 1) / 4 - 1;
1199239281f8SRostislav Lisovy 				if (is_ocb)
1200239281f8SRostislav Lisovy 					qparam.txop = 0;
1201239281f8SRostislav Lisovy 				else if (use_11b)
1202aa837e1dSJohannes Berg 					qparam.txop = 3264/32;
1203aa837e1dSJohannes Berg 				else
1204aa837e1dSJohannes Berg 					qparam.txop = 1504/32;
1205aa837e1dSJohannes Berg 				qparam.aifs = 2;
1206aa837e1dSJohannes Berg 				break;
1207aa837e1dSJohannes Berg 			}
1208a8ce8544SStanislaw Gruszka 		}
12095825fe10SJohannes Berg 
1210ab13315aSKalle Valo 		qparam.uapsd = false;
1211ab13315aSKalle Valo 
121254bcbc69SJohannes Berg 		sdata->tx_conf[ac] = qparam;
121354bcbc69SJohannes Berg 		drv_conf_tx(local, sdata, ac, &qparam);
1214aa837e1dSJohannes Berg 	}
1215e1b3ec1aSStanislaw Gruszka 
1216f142c6b9SJohannes Berg 	if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1217708d50edSAyala Beker 	    sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1218708d50edSAyala Beker 	    sdata->vif.type != NL80211_IFTYPE_NAN) {
1219a8ce8544SStanislaw Gruszka 		sdata->vif.bss_conf.qos = enable_qos;
12203abead59SJohannes Berg 		if (bss_notify)
12213abead59SJohannes Berg 			ieee80211_bss_info_change_notify(sdata,
12223abead59SJohannes Berg 							 BSS_CHANGED_QOS);
12235825fe10SJohannes Berg 	}
1224d9734979SSujith }
1225e50db65cSJohannes Berg 
122646900298SJohannes Berg void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1227700e8ea6SJouni Malinen 			 u16 transaction, u16 auth_alg, u16 status,
12284a3cb702SJohannes Berg 			 const u8 *extra, size_t extra_len, const u8 *da,
12291672c0e3SJohannes Berg 			 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
12301672c0e3SJohannes Berg 			 u32 tx_flags)
123146900298SJohannes Berg {
123246900298SJohannes Berg 	struct ieee80211_local *local = sdata->local;
123346900298SJohannes Berg 	struct sk_buff *skb;
123446900298SJohannes Berg 	struct ieee80211_mgmt *mgmt;
1235fffd0934SJohannes Berg 	int err;
123646900298SJohannes Berg 
123715e230abSFred Zhou 	/* 24 + 6 = header + auth_algo + auth_transaction + status_code */
1238744462a9SMax Stepanov 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
1239744462a9SMax Stepanov 			    24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN);
1240d15b8459SJoe Perches 	if (!skb)
124146900298SJohannes Berg 		return;
1242d15b8459SJoe Perches 
1243744462a9SMax Stepanov 	skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
124446900298SJohannes Berg 
124546900298SJohannes Berg 	mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
124646900298SJohannes Berg 	memset(mgmt, 0, 24 + 6);
124746900298SJohannes Berg 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
124846900298SJohannes Berg 					  IEEE80211_STYPE_AUTH);
1249efa6a09dSAntonio Quartulli 	memcpy(mgmt->da, da, ETH_ALEN);
125047846c9bSJohannes Berg 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
125146900298SJohannes Berg 	memcpy(mgmt->bssid, bssid, ETH_ALEN);
125246900298SJohannes Berg 	mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
125346900298SJohannes Berg 	mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
1254700e8ea6SJouni Malinen 	mgmt->u.auth.status_code = cpu_to_le16(status);
125546900298SJohannes Berg 	if (extra)
125646900298SJohannes Berg 		memcpy(skb_put(skb, extra_len), extra, extra_len);
125746900298SJohannes Berg 
1258fffd0934SJohannes Berg 	if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1259fffd0934SJohannes Berg 		mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1260fffd0934SJohannes Berg 		err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1261fffd0934SJohannes Berg 		WARN_ON(err);
1262fffd0934SJohannes Berg 	}
1263fffd0934SJohannes Berg 
12641672c0e3SJohannes Berg 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
12651672c0e3SJohannes Berg 					tx_flags;
126662ae67beSJohannes Berg 	ieee80211_tx_skb(sdata, skb);
126746900298SJohannes Berg }
126846900298SJohannes Berg 
12696ae16775SAntonio Quartulli void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
12706ae16775SAntonio Quartulli 				    const u8 *bssid, u16 stype, u16 reason,
12716ae16775SAntonio Quartulli 				    bool send_frame, u8 *frame_buf)
12726ae16775SAntonio Quartulli {
12736ae16775SAntonio Quartulli 	struct ieee80211_local *local = sdata->local;
12746ae16775SAntonio Quartulli 	struct sk_buff *skb;
12756ae16775SAntonio Quartulli 	struct ieee80211_mgmt *mgmt = (void *)frame_buf;
12766ae16775SAntonio Quartulli 
12776ae16775SAntonio Quartulli 	/* build frame */
12786ae16775SAntonio Quartulli 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
12796ae16775SAntonio Quartulli 	mgmt->duration = 0; /* initialize only */
12806ae16775SAntonio Quartulli 	mgmt->seq_ctrl = 0; /* initialize only */
12816ae16775SAntonio Quartulli 	memcpy(mgmt->da, bssid, ETH_ALEN);
12826ae16775SAntonio Quartulli 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
12836ae16775SAntonio Quartulli 	memcpy(mgmt->bssid, bssid, ETH_ALEN);
12846ae16775SAntonio Quartulli 	/* u.deauth.reason_code == u.disassoc.reason_code */
12856ae16775SAntonio Quartulli 	mgmt->u.deauth.reason_code = cpu_to_le16(reason);
12866ae16775SAntonio Quartulli 
12876ae16775SAntonio Quartulli 	if (send_frame) {
12886ae16775SAntonio Quartulli 		skb = dev_alloc_skb(local->hw.extra_tx_headroom +
12896ae16775SAntonio Quartulli 				    IEEE80211_DEAUTH_FRAME_LEN);
12906ae16775SAntonio Quartulli 		if (!skb)
12916ae16775SAntonio Quartulli 			return;
12926ae16775SAntonio Quartulli 
12936ae16775SAntonio Quartulli 		skb_reserve(skb, local->hw.extra_tx_headroom);
12946ae16775SAntonio Quartulli 
12956ae16775SAntonio Quartulli 		/* copy in frame */
12966ae16775SAntonio Quartulli 		memcpy(skb_put(skb, IEEE80211_DEAUTH_FRAME_LEN),
12976ae16775SAntonio Quartulli 		       mgmt, IEEE80211_DEAUTH_FRAME_LEN);
12986ae16775SAntonio Quartulli 
12996ae16775SAntonio Quartulli 		if (sdata->vif.type != NL80211_IFTYPE_STATION ||
13006ae16775SAntonio Quartulli 		    !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
13016ae16775SAntonio Quartulli 			IEEE80211_SKB_CB(skb)->flags |=
13026ae16775SAntonio Quartulli 				IEEE80211_TX_INTFL_DONT_ENCRYPT;
13036ae16775SAntonio Quartulli 
13046ae16775SAntonio Quartulli 		ieee80211_tx_skb(sdata, skb);
13056ae16775SAntonio Quartulli 	}
13066ae16775SAntonio Quartulli }
13076ae16775SAntonio Quartulli 
1308c56ef672SDavid Spinadel static int ieee80211_build_preq_ies_band(struct ieee80211_local *local,
1309c56ef672SDavid Spinadel 					 u8 *buffer, size_t buffer_len,
1310c56ef672SDavid Spinadel 					 const u8 *ie, size_t ie_len,
131157fbcce3SJohannes Berg 					 enum nl80211_band band,
1312c56ef672SDavid Spinadel 					 u32 rate_mask,
1313c56ef672SDavid Spinadel 					 struct cfg80211_chan_def *chandef,
1314c56ef672SDavid Spinadel 					 size_t *offset)
1315de95a54bSJohannes Berg {
1316de95a54bSJohannes Berg 	struct ieee80211_supported_band *sband;
1317c604b9f2SJohannes Berg 	u8 *pos = buffer, *end = buffer + buffer_len;
1318c56ef672SDavid Spinadel 	size_t noffset;
13198e664fb3SJohannes Berg 	int supp_rates_len, i;
13208dcb2003SJouni Malinen 	u8 rates[32];
13218dcb2003SJouni Malinen 	int num_rates;
13228dcb2003SJouni Malinen 	int ext_rates_len;
13232103dec1SSimon Wunderlich 	int shift;
13242103dec1SSimon Wunderlich 	u32 rate_flags;
132540a11ca8SJohannes Berg 	bool have_80mhz = false;
1326de95a54bSJohannes Berg 
1327c56ef672SDavid Spinadel 	*offset = 0;
1328c56ef672SDavid Spinadel 
13294d36ec58SJohannes Berg 	sband = local->hw.wiphy->bands[band];
1330d811b3d5SArik Nemtsov 	if (WARN_ON_ONCE(!sband))
1331d811b3d5SArik Nemtsov 		return 0;
1332de95a54bSJohannes Berg 
13332103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(chandef);
13342103dec1SSimon Wunderlich 	shift = ieee80211_chandef_get_shift(chandef);
13352103dec1SSimon Wunderlich 
13368dcb2003SJouni Malinen 	num_rates = 0;
13378dcb2003SJouni Malinen 	for (i = 0; i < sband->n_bitrates; i++) {
13388dcb2003SJouni Malinen 		if ((BIT(i) & rate_mask) == 0)
13398dcb2003SJouni Malinen 			continue; /* skip rate */
13402103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
13412103dec1SSimon Wunderlich 			continue;
13422103dec1SSimon Wunderlich 
13432103dec1SSimon Wunderlich 		rates[num_rates++] =
13442103dec1SSimon Wunderlich 			(u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
13452103dec1SSimon Wunderlich 					  (1 << shift) * 5);
13468dcb2003SJouni Malinen 	}
13478dcb2003SJouni Malinen 
13488dcb2003SJouni Malinen 	supp_rates_len = min_t(int, num_rates, 8);
13498e664fb3SJohannes Berg 
1350c604b9f2SJohannes Berg 	if (end - pos < 2 + supp_rates_len)
1351c604b9f2SJohannes Berg 		goto out_err;
1352de95a54bSJohannes Berg 	*pos++ = WLAN_EID_SUPP_RATES;
13538e664fb3SJohannes Berg 	*pos++ = supp_rates_len;
13548dcb2003SJouni Malinen 	memcpy(pos, rates, supp_rates_len);
13558dcb2003SJouni Malinen 	pos += supp_rates_len;
1356de95a54bSJohannes Berg 
13578e664fb3SJohannes Berg 	/* insert "request information" if in custom IEs */
13588e664fb3SJohannes Berg 	if (ie && ie_len) {
13598e664fb3SJohannes Berg 		static const u8 before_extrates[] = {
13608e664fb3SJohannes Berg 			WLAN_EID_SSID,
13618e664fb3SJohannes Berg 			WLAN_EID_SUPP_RATES,
13628e664fb3SJohannes Berg 			WLAN_EID_REQUEST,
13638e664fb3SJohannes Berg 		};
13648e664fb3SJohannes Berg 		noffset = ieee80211_ie_split(ie, ie_len,
13658e664fb3SJohannes Berg 					     before_extrates,
13668e664fb3SJohannes Berg 					     ARRAY_SIZE(before_extrates),
1367c56ef672SDavid Spinadel 					     *offset);
1368c56ef672SDavid Spinadel 		if (end - pos < noffset - *offset)
1369c604b9f2SJohannes Berg 			goto out_err;
1370c56ef672SDavid Spinadel 		memcpy(pos, ie + *offset, noffset - *offset);
1371c56ef672SDavid Spinadel 		pos += noffset - *offset;
1372c56ef672SDavid Spinadel 		*offset = noffset;
13738e664fb3SJohannes Berg 	}
13748e664fb3SJohannes Berg 
13758dcb2003SJouni Malinen 	ext_rates_len = num_rates - supp_rates_len;
13768dcb2003SJouni Malinen 	if (ext_rates_len > 0) {
1377c604b9f2SJohannes Berg 		if (end - pos < 2 + ext_rates_len)
1378c604b9f2SJohannes Berg 			goto out_err;
1379de95a54bSJohannes Berg 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
13808dcb2003SJouni Malinen 		*pos++ = ext_rates_len;
13818dcb2003SJouni Malinen 		memcpy(pos, rates + supp_rates_len, ext_rates_len);
13828dcb2003SJouni Malinen 		pos += ext_rates_len;
13838e664fb3SJohannes Berg 	}
13848e664fb3SJohannes Berg 
138557fbcce3SJohannes Berg 	if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
1386c604b9f2SJohannes Berg 		if (end - pos < 3)
1387c604b9f2SJohannes Berg 			goto out_err;
1388651b5225SJouni Malinen 		*pos++ = WLAN_EID_DS_PARAMS;
1389651b5225SJouni Malinen 		*pos++ = 1;
13902103dec1SSimon Wunderlich 		*pos++ = ieee80211_frequency_to_channel(
13912103dec1SSimon Wunderlich 				chandef->chan->center_freq);
1392651b5225SJouni Malinen 	}
1393651b5225SJouni Malinen 
13948e664fb3SJohannes Berg 	/* insert custom IEs that go before HT */
13958e664fb3SJohannes Berg 	if (ie && ie_len) {
13968e664fb3SJohannes Berg 		static const u8 before_ht[] = {
13978e664fb3SJohannes Berg 			WLAN_EID_SSID,
13988e664fb3SJohannes Berg 			WLAN_EID_SUPP_RATES,
13998e664fb3SJohannes Berg 			WLAN_EID_REQUEST,
14008e664fb3SJohannes Berg 			WLAN_EID_EXT_SUPP_RATES,
14018e664fb3SJohannes Berg 			WLAN_EID_DS_PARAMS,
14028e664fb3SJohannes Berg 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
14038e664fb3SJohannes Berg 		};
14048e664fb3SJohannes Berg 		noffset = ieee80211_ie_split(ie, ie_len,
14058e664fb3SJohannes Berg 					     before_ht, ARRAY_SIZE(before_ht),
1406c56ef672SDavid Spinadel 					     *offset);
1407c56ef672SDavid Spinadel 		if (end - pos < noffset - *offset)
1408c604b9f2SJohannes Berg 			goto out_err;
1409c56ef672SDavid Spinadel 		memcpy(pos, ie + *offset, noffset - *offset);
1410c56ef672SDavid Spinadel 		pos += noffset - *offset;
1411c56ef672SDavid Spinadel 		*offset = noffset;
1412de95a54bSJohannes Berg 	}
1413de95a54bSJohannes Berg 
1414c604b9f2SJohannes Berg 	if (sband->ht_cap.ht_supported) {
1415c604b9f2SJohannes Berg 		if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
1416c604b9f2SJohannes Berg 			goto out_err;
1417ef96a842SBen Greear 		pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
1418ef96a842SBen Greear 						sband->ht_cap.cap);
1419c604b9f2SJohannes Berg 	}
14205ef2d41aSJohannes Berg 
1421de95a54bSJohannes Berg 	/*
1422de95a54bSJohannes Berg 	 * If adding more here, adjust code in main.c
1423de95a54bSJohannes Berg 	 * that calculates local->scan_ies_len.
1424de95a54bSJohannes Berg 	 */
1425de95a54bSJohannes Berg 
14264d952300SJohannes Berg 	/* insert custom IEs that go before VHT */
14278e664fb3SJohannes Berg 	if (ie && ie_len) {
14284d952300SJohannes Berg 		static const u8 before_vht[] = {
14294d952300SJohannes Berg 			WLAN_EID_SSID,
14304d952300SJohannes Berg 			WLAN_EID_SUPP_RATES,
14314d952300SJohannes Berg 			WLAN_EID_REQUEST,
14324d952300SJohannes Berg 			WLAN_EID_EXT_SUPP_RATES,
14334d952300SJohannes Berg 			WLAN_EID_DS_PARAMS,
14344d952300SJohannes Berg 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
14354d952300SJohannes Berg 			WLAN_EID_HT_CAPABILITY,
14364d952300SJohannes Berg 			WLAN_EID_BSS_COEX_2040,
14374d952300SJohannes Berg 			WLAN_EID_EXT_CAPABILITY,
14384d952300SJohannes Berg 			WLAN_EID_SSID_LIST,
14394d952300SJohannes Berg 			WLAN_EID_CHANNEL_USAGE,
14404d952300SJohannes Berg 			WLAN_EID_INTERWORKING,
14414d952300SJohannes Berg 			/* mesh ID can't happen here */
14424d952300SJohannes Berg 			/* 60 GHz can't happen here right now */
14434d952300SJohannes Berg 		};
14444d952300SJohannes Berg 		noffset = ieee80211_ie_split(ie, ie_len,
14454d952300SJohannes Berg 					     before_vht, ARRAY_SIZE(before_vht),
1446c56ef672SDavid Spinadel 					     *offset);
1447c56ef672SDavid Spinadel 		if (end - pos < noffset - *offset)
1448c604b9f2SJohannes Berg 			goto out_err;
1449c56ef672SDavid Spinadel 		memcpy(pos, ie + *offset, noffset - *offset);
1450c56ef672SDavid Spinadel 		pos += noffset - *offset;
1451c56ef672SDavid Spinadel 		*offset = noffset;
1452de95a54bSJohannes Berg 	}
1453de95a54bSJohannes Berg 
145440a11ca8SJohannes Berg 	/* Check if any channel in this sband supports at least 80 MHz */
145540a11ca8SJohannes Berg 	for (i = 0; i < sband->n_channels; i++) {
1456fa44b988SArik Nemtsov 		if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
1457fa44b988SArik Nemtsov 						IEEE80211_CHAN_NO_80MHZ))
1458fa44b988SArik Nemtsov 			continue;
1459fa44b988SArik Nemtsov 
146040a11ca8SJohannes Berg 		have_80mhz = true;
146140a11ca8SJohannes Berg 		break;
146240a11ca8SJohannes Berg 	}
146340a11ca8SJohannes Berg 
146440a11ca8SJohannes Berg 	if (sband->vht_cap.vht_supported && have_80mhz) {
1465c604b9f2SJohannes Berg 		if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
1466c604b9f2SJohannes Berg 			goto out_err;
1467ba0afa2fSMahesh Palivela 		pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
1468ba0afa2fSMahesh Palivela 						 sband->vht_cap.cap);
1469c604b9f2SJohannes Berg 	}
1470ba0afa2fSMahesh Palivela 
1471de95a54bSJohannes Berg 	return pos - buffer;
1472c604b9f2SJohannes Berg  out_err:
1473c604b9f2SJohannes Berg 	WARN_ONCE(1, "not enough space for preq IEs\n");
1474c604b9f2SJohannes Berg 	return pos - buffer;
1475de95a54bSJohannes Berg }
1476de95a54bSJohannes Berg 
1477c56ef672SDavid Spinadel int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
1478c56ef672SDavid Spinadel 			     size_t buffer_len,
1479c56ef672SDavid Spinadel 			     struct ieee80211_scan_ies *ie_desc,
1480c56ef672SDavid Spinadel 			     const u8 *ie, size_t ie_len,
1481c56ef672SDavid Spinadel 			     u8 bands_used, u32 *rate_masks,
1482c56ef672SDavid Spinadel 			     struct cfg80211_chan_def *chandef)
1483c56ef672SDavid Spinadel {
1484c56ef672SDavid Spinadel 	size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
1485c56ef672SDavid Spinadel 	int i;
1486c56ef672SDavid Spinadel 
1487c56ef672SDavid Spinadel 	memset(ie_desc, 0, sizeof(*ie_desc));
1488c56ef672SDavid Spinadel 
148957fbcce3SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
1490c56ef672SDavid Spinadel 		if (bands_used & BIT(i)) {
1491c56ef672SDavid Spinadel 			pos += ieee80211_build_preq_ies_band(local,
1492c56ef672SDavid Spinadel 							     buffer + pos,
1493c56ef672SDavid Spinadel 							     buffer_len - pos,
1494c56ef672SDavid Spinadel 							     ie, ie_len, i,
1495c56ef672SDavid Spinadel 							     rate_masks[i],
1496c56ef672SDavid Spinadel 							     chandef,
1497c56ef672SDavid Spinadel 							     &custom_ie_offset);
1498c56ef672SDavid Spinadel 			ie_desc->ies[i] = buffer + old_pos;
1499c56ef672SDavid Spinadel 			ie_desc->len[i] = pos - old_pos;
1500c56ef672SDavid Spinadel 			old_pos = pos;
1501c56ef672SDavid Spinadel 		}
1502c56ef672SDavid Spinadel 	}
1503c56ef672SDavid Spinadel 
1504c56ef672SDavid Spinadel 	/* add any remaining custom IEs */
1505c56ef672SDavid Spinadel 	if (ie && ie_len) {
1506c56ef672SDavid Spinadel 		if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
1507c56ef672SDavid Spinadel 			      "not enough space for preq custom IEs\n"))
1508c56ef672SDavid Spinadel 			return pos;
1509c56ef672SDavid Spinadel 		memcpy(buffer + pos, ie + custom_ie_offset,
1510c56ef672SDavid Spinadel 		       ie_len - custom_ie_offset);
1511c56ef672SDavid Spinadel 		ie_desc->common_ies = buffer + pos;
1512c56ef672SDavid Spinadel 		ie_desc->common_ie_len = ie_len - custom_ie_offset;
1513c56ef672SDavid Spinadel 		pos += ie_len - custom_ie_offset;
1514c56ef672SDavid Spinadel 	}
1515c56ef672SDavid Spinadel 
1516c56ef672SDavid Spinadel 	return pos;
1517c56ef672SDavid Spinadel };
1518c56ef672SDavid Spinadel 
1519a619a4c0SJuuso Oikarinen struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
1520a344d677SJohannes Berg 					  const u8 *src, const u8 *dst,
1521a344d677SJohannes Berg 					  u32 ratemask,
15226b77863bSJohannes Berg 					  struct ieee80211_channel *chan,
1523de95a54bSJohannes Berg 					  const u8 *ssid, size_t ssid_len,
1524a806c558SPaul Stewart 					  const u8 *ie, size_t ie_len,
1525a806c558SPaul Stewart 					  bool directed)
152646900298SJohannes Berg {
152746900298SJohannes Berg 	struct ieee80211_local *local = sdata->local;
15282103dec1SSimon Wunderlich 	struct cfg80211_chan_def chandef;
152946900298SJohannes Berg 	struct sk_buff *skb;
153046900298SJohannes Berg 	struct ieee80211_mgmt *mgmt;
1531b9a9ada1SJohannes Berg 	int ies_len;
153257fbcce3SJohannes Berg 	u32 rate_masks[NUM_NL80211_BANDS] = {};
1533c56ef672SDavid Spinadel 	struct ieee80211_scan_ies dummy_ie_desc;
153446900298SJohannes Berg 
1535a806c558SPaul Stewart 	/*
1536a806c558SPaul Stewart 	 * Do not send DS Channel parameter for directed probe requests
1537a806c558SPaul Stewart 	 * in order to maximize the chance that we get a response.  Some
1538a806c558SPaul Stewart 	 * badly-behaved APs don't respond when this parameter is included.
1539a806c558SPaul Stewart 	 */
15402103dec1SSimon Wunderlich 	chandef.width = sdata->vif.bss_conf.chandef.width;
1541a806c558SPaul Stewart 	if (directed)
15422103dec1SSimon Wunderlich 		chandef.chan = NULL;
1543a806c558SPaul Stewart 	else
15442103dec1SSimon Wunderlich 		chandef.chan = chan;
1545651b5225SJouni Malinen 
1546a344d677SJohannes Berg 	skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
1547a344d677SJohannes Berg 				     100 + ie_len);
15485b2bbf75SJohannes Berg 	if (!skb)
1549b9a9ada1SJohannes Berg 		return NULL;
1550b9a9ada1SJohannes Berg 
1551c56ef672SDavid Spinadel 	rate_masks[chan->band] = ratemask;
1552b9a9ada1SJohannes Berg 	ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
1553c56ef672SDavid Spinadel 					   skb_tailroom(skb), &dummy_ie_desc,
1554c56ef672SDavid Spinadel 					   ie, ie_len, BIT(chan->band),
1555c56ef672SDavid Spinadel 					   rate_masks, &chandef);
1556b9a9ada1SJohannes Berg 	skb_put(skb, ies_len);
15577c12ce8bSKalle Valo 
155846900298SJohannes Berg 	if (dst) {
15597c12ce8bSKalle Valo 		mgmt = (struct ieee80211_mgmt *) skb->data;
156046900298SJohannes Berg 		memcpy(mgmt->da, dst, ETH_ALEN);
156146900298SJohannes Berg 		memcpy(mgmt->bssid, dst, ETH_ALEN);
156246900298SJohannes Berg 	}
156346900298SJohannes Berg 
156462ae67beSJohannes Berg 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
15655b2bbf75SJohannes Berg 
1566a619a4c0SJuuso Oikarinen 	return skb;
1567a619a4c0SJuuso Oikarinen }
1568a619a4c0SJuuso Oikarinen 
1569a344d677SJohannes Berg void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata,
1570a344d677SJohannes Berg 			      const u8 *src, const u8 *dst,
1571a619a4c0SJuuso Oikarinen 			      const u8 *ssid, size_t ssid_len,
1572a806c558SPaul Stewart 			      const u8 *ie, size_t ie_len,
15731672c0e3SJohannes Berg 			      u32 ratemask, bool directed, u32 tx_flags,
157455de908aSJohannes Berg 			      struct ieee80211_channel *channel, bool scan)
1575a619a4c0SJuuso Oikarinen {
1576a619a4c0SJuuso Oikarinen 	struct sk_buff *skb;
1577a619a4c0SJuuso Oikarinen 
1578a344d677SJohannes Berg 	skb = ieee80211_build_probe_req(sdata, src, dst, ratemask, channel,
15796b77863bSJohannes Berg 					ssid, ssid_len,
158085a237feSJohannes Berg 					ie, ie_len, directed);
1581aad14cebSRajkumar Manoharan 	if (skb) {
15821672c0e3SJohannes Berg 		IEEE80211_SKB_CB(skb)->flags |= tx_flags;
158355de908aSJohannes Berg 		if (scan)
158455de908aSJohannes Berg 			ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
158555de908aSJohannes Berg 		else
1586a619a4c0SJuuso Oikarinen 			ieee80211_tx_skb(sdata, skb);
158746900298SJohannes Berg 	}
1588aad14cebSRajkumar Manoharan }
158946900298SJohannes Berg 
15902103dec1SSimon Wunderlich u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
159146900298SJohannes Berg 			    struct ieee802_11_elems *elems,
159257fbcce3SJohannes Berg 			    enum nl80211_band band, u32 *basic_rates)
159346900298SJohannes Berg {
159446900298SJohannes Berg 	struct ieee80211_supported_band *sband;
159546900298SJohannes Berg 	size_t num_rates;
15962103dec1SSimon Wunderlich 	u32 supp_rates, rate_flags;
15972103dec1SSimon Wunderlich 	int i, j, shift;
159821a8e9ddSMohammed Shafi Shajakhan 
15992103dec1SSimon Wunderlich 	sband = sdata->local->hw.wiphy->bands[band];
160021a8e9ddSMohammed Shafi Shajakhan 	if (WARN_ON(!sband))
160121a8e9ddSMohammed Shafi Shajakhan 		return 1;
16022103dec1SSimon Wunderlich 
16032103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
16042103dec1SSimon Wunderlich 	shift = ieee80211_vif_get_shift(&sdata->vif);
160546900298SJohannes Berg 
160646900298SJohannes Berg 	num_rates = sband->n_bitrates;
160746900298SJohannes Berg 	supp_rates = 0;
160846900298SJohannes Berg 	for (i = 0; i < elems->supp_rates_len +
160946900298SJohannes Berg 		     elems->ext_supp_rates_len; i++) {
161046900298SJohannes Berg 		u8 rate = 0;
161146900298SJohannes Berg 		int own_rate;
16129ebb61a2SAshok Nagarajan 		bool is_basic;
161346900298SJohannes Berg 		if (i < elems->supp_rates_len)
161446900298SJohannes Berg 			rate = elems->supp_rates[i];
161546900298SJohannes Berg 		else if (elems->ext_supp_rates)
161646900298SJohannes Berg 			rate = elems->ext_supp_rates
161746900298SJohannes Berg 				[i - elems->supp_rates_len];
161846900298SJohannes Berg 		own_rate = 5 * (rate & 0x7f);
16199ebb61a2SAshok Nagarajan 		is_basic = !!(rate & 0x80);
16209ebb61a2SAshok Nagarajan 
16219ebb61a2SAshok Nagarajan 		if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
16229ebb61a2SAshok Nagarajan 			continue;
16239ebb61a2SAshok Nagarajan 
16249ebb61a2SAshok Nagarajan 		for (j = 0; j < num_rates; j++) {
16252103dec1SSimon Wunderlich 			int brate;
16262103dec1SSimon Wunderlich 			if ((rate_flags & sband->bitrates[j].flags)
16272103dec1SSimon Wunderlich 			    != rate_flags)
16282103dec1SSimon Wunderlich 				continue;
16292103dec1SSimon Wunderlich 
16302103dec1SSimon Wunderlich 			brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
16312103dec1SSimon Wunderlich 					     1 << shift);
16322103dec1SSimon Wunderlich 
16332103dec1SSimon Wunderlich 			if (brate == own_rate) {
163446900298SJohannes Berg 				supp_rates |= BIT(j);
16359ebb61a2SAshok Nagarajan 				if (basic_rates && is_basic)
16369ebb61a2SAshok Nagarajan 					*basic_rates |= BIT(j);
16379ebb61a2SAshok Nagarajan 			}
16389ebb61a2SAshok Nagarajan 		}
163946900298SJohannes Berg 	}
164046900298SJohannes Berg 	return supp_rates;
164146900298SJohannes Berg }
1642f2753ddbSJohannes Berg 
164384f6a01cSJohannes Berg void ieee80211_stop_device(struct ieee80211_local *local)
164484f6a01cSJohannes Berg {
164584f6a01cSJohannes Berg 	ieee80211_led_radio(local, false);
164667408c8cSJohannes Berg 	ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
164784f6a01cSJohannes Berg 
164884f6a01cSJohannes Berg 	cancel_work_sync(&local->reconfig_filter);
164984f6a01cSJohannes Berg 
165084f6a01cSJohannes Berg 	flush_workqueue(local->workqueue);
1651678f415fSLennert Buytenhek 	drv_stop(local);
165284f6a01cSJohannes Berg }
165384f6a01cSJohannes Berg 
165474430f94SJohannes Berg static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
165574430f94SJohannes Berg 					   bool aborted)
165674430f94SJohannes Berg {
165774430f94SJohannes Berg 	/* It's possible that we don't handle the scan completion in
165874430f94SJohannes Berg 	 * time during suspend, so if it's still marked as completed
165974430f94SJohannes Berg 	 * here, queue the work and flush it to clean things up.
166074430f94SJohannes Berg 	 * Instead of calling the worker function directly here, we
166174430f94SJohannes Berg 	 * really queue it to avoid potential races with other flows
166274430f94SJohannes Berg 	 * scheduling the same work.
166374430f94SJohannes Berg 	 */
166474430f94SJohannes Berg 	if (test_bit(SCAN_COMPLETED, &local->scanning)) {
166574430f94SJohannes Berg 		/* If coming from reconfiguration failure, abort the scan so
166674430f94SJohannes Berg 		 * we don't attempt to continue a partial HW scan - which is
166774430f94SJohannes Berg 		 * possible otherwise if (e.g.) the 2.4 GHz portion was the
166874430f94SJohannes Berg 		 * completed scan, and a 5 GHz portion is still pending.
166974430f94SJohannes Berg 		 */
167074430f94SJohannes Berg 		if (aborted)
167174430f94SJohannes Berg 			set_bit(SCAN_ABORTED, &local->scanning);
167274430f94SJohannes Berg 		ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
167374430f94SJohannes Berg 		flush_delayed_work(&local->scan_work);
167474430f94SJohannes Berg 	}
167574430f94SJohannes Berg }
167674430f94SJohannes Berg 
1677f6837ba8SJohannes Berg static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
1678f6837ba8SJohannes Berg {
1679f6837ba8SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
1680f6837ba8SJohannes Berg 	struct ieee80211_chanctx *ctx;
1681f6837ba8SJohannes Berg 
1682f6837ba8SJohannes Berg 	/*
1683f6837ba8SJohannes Berg 	 * We get here if during resume the device can't be restarted properly.
1684f6837ba8SJohannes Berg 	 * We might also get here if this happens during HW reset, which is a
1685f6837ba8SJohannes Berg 	 * slightly different situation and we need to drop all connections in
1686f6837ba8SJohannes Berg 	 * the latter case.
1687f6837ba8SJohannes Berg 	 *
1688f6837ba8SJohannes Berg 	 * Ask cfg80211 to turn off all interfaces, this will result in more
1689f6837ba8SJohannes Berg 	 * warnings but at least we'll then get into a clean stopped state.
1690f6837ba8SJohannes Berg 	 */
1691f6837ba8SJohannes Berg 
1692f6837ba8SJohannes Berg 	local->resuming = false;
1693f6837ba8SJohannes Berg 	local->suspended = false;
16947584f88fSEliad Peller 	local->in_reconfig = false;
1695f6837ba8SJohannes Berg 
169674430f94SJohannes Berg 	ieee80211_flush_completed_scan(local, true);
169774430f94SJohannes Berg 
1698f6837ba8SJohannes Berg 	/* scheduled scan clearly can't be running any more, but tell
1699f6837ba8SJohannes Berg 	 * cfg80211 and clear local state
1700f6837ba8SJohannes Berg 	 */
1701f6837ba8SJohannes Berg 	ieee80211_sched_scan_end(local);
1702f6837ba8SJohannes Berg 
1703f6837ba8SJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list)
1704f6837ba8SJohannes Berg 		sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
1705f6837ba8SJohannes Berg 
1706f6837ba8SJohannes Berg 	/* Mark channel contexts as not being in the driver any more to avoid
1707f6837ba8SJohannes Berg 	 * removing them from the driver during the shutdown process...
1708f6837ba8SJohannes Berg 	 */
1709f6837ba8SJohannes Berg 	mutex_lock(&local->chanctx_mtx);
1710f6837ba8SJohannes Berg 	list_for_each_entry(ctx, &local->chanctx_list, list)
1711f6837ba8SJohannes Berg 		ctx->driver_present = false;
1712f6837ba8SJohannes Berg 	mutex_unlock(&local->chanctx_mtx);
1713f6837ba8SJohannes Berg 
1714f6837ba8SJohannes Berg 	cfg80211_shutdown_all_interfaces(local->hw.wiphy);
1715f6837ba8SJohannes Berg }
1716f6837ba8SJohannes Berg 
1717153a5fc4SStanislaw Gruszka static void ieee80211_assign_chanctx(struct ieee80211_local *local,
1718153a5fc4SStanislaw Gruszka 				     struct ieee80211_sub_if_data *sdata)
1719153a5fc4SStanislaw Gruszka {
1720153a5fc4SStanislaw Gruszka 	struct ieee80211_chanctx_conf *conf;
1721153a5fc4SStanislaw Gruszka 	struct ieee80211_chanctx *ctx;
1722153a5fc4SStanislaw Gruszka 
1723153a5fc4SStanislaw Gruszka 	if (!local->use_chanctx)
1724153a5fc4SStanislaw Gruszka 		return;
1725153a5fc4SStanislaw Gruszka 
1726153a5fc4SStanislaw Gruszka 	mutex_lock(&local->chanctx_mtx);
1727153a5fc4SStanislaw Gruszka 	conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1728153a5fc4SStanislaw Gruszka 					 lockdep_is_held(&local->chanctx_mtx));
1729153a5fc4SStanislaw Gruszka 	if (conf) {
1730153a5fc4SStanislaw Gruszka 		ctx = container_of(conf, struct ieee80211_chanctx, conf);
1731153a5fc4SStanislaw Gruszka 		drv_assign_vif_chanctx(local, sdata, ctx);
1732153a5fc4SStanislaw Gruszka 	}
1733153a5fc4SStanislaw Gruszka 	mutex_unlock(&local->chanctx_mtx);
1734153a5fc4SStanislaw Gruszka }
1735153a5fc4SStanislaw Gruszka 
17361ea2c864SJohannes Berg static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
17371ea2c864SJohannes Berg {
17381ea2c864SJohannes Berg 	struct ieee80211_local *local = sdata->local;
17391ea2c864SJohannes Berg 	struct sta_info *sta;
17401ea2c864SJohannes Berg 
17411ea2c864SJohannes Berg 	/* add STAs back */
17421ea2c864SJohannes Berg 	mutex_lock(&local->sta_mtx);
17431ea2c864SJohannes Berg 	list_for_each_entry(sta, &local->sta_list, list) {
17441ea2c864SJohannes Berg 		enum ieee80211_sta_state state;
17451ea2c864SJohannes Berg 
17461ea2c864SJohannes Berg 		if (!sta->uploaded || sta->sdata != sdata)
17471ea2c864SJohannes Berg 			continue;
17481ea2c864SJohannes Berg 
17491ea2c864SJohannes Berg 		for (state = IEEE80211_STA_NOTEXIST;
17501ea2c864SJohannes Berg 		     state < sta->sta_state; state++)
17511ea2c864SJohannes Berg 			WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
17521ea2c864SJohannes Berg 					      state + 1));
17531ea2c864SJohannes Berg 	}
17541ea2c864SJohannes Berg 	mutex_unlock(&local->sta_mtx);
17551ea2c864SJohannes Berg }
17561ea2c864SJohannes Berg 
1757167e33f4SAyala Beker static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
1758167e33f4SAyala Beker {
1759167e33f4SAyala Beker 	struct cfg80211_nan_func *func, **funcs;
1760167e33f4SAyala Beker 	int res, id, i = 0;
1761167e33f4SAyala Beker 
1762167e33f4SAyala Beker 	res = drv_start_nan(sdata->local, sdata,
1763167e33f4SAyala Beker 			    &sdata->u.nan.conf);
1764167e33f4SAyala Beker 	if (WARN_ON(res))
1765167e33f4SAyala Beker 		return res;
1766167e33f4SAyala Beker 
1767167e33f4SAyala Beker 	funcs = kzalloc((sdata->local->hw.max_nan_de_entries + 1) *
1768167e33f4SAyala Beker 			sizeof(*funcs), GFP_KERNEL);
1769167e33f4SAyala Beker 	if (!funcs)
1770167e33f4SAyala Beker 		return -ENOMEM;
1771167e33f4SAyala Beker 
1772167e33f4SAyala Beker 	/* Add all the functions:
1773167e33f4SAyala Beker 	 * This is a little bit ugly. We need to call a potentially sleeping
1774167e33f4SAyala Beker 	 * callback for each NAN function, so we can't hold the spinlock.
1775167e33f4SAyala Beker 	 */
1776167e33f4SAyala Beker 	spin_lock_bh(&sdata->u.nan.func_lock);
1777167e33f4SAyala Beker 
1778167e33f4SAyala Beker 	idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
1779167e33f4SAyala Beker 		funcs[i++] = func;
1780167e33f4SAyala Beker 
1781167e33f4SAyala Beker 	spin_unlock_bh(&sdata->u.nan.func_lock);
1782167e33f4SAyala Beker 
1783167e33f4SAyala Beker 	for (i = 0; funcs[i]; i++) {
1784167e33f4SAyala Beker 		res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
1785167e33f4SAyala Beker 		if (WARN_ON(res))
1786167e33f4SAyala Beker 			ieee80211_nan_func_terminated(&sdata->vif,
1787167e33f4SAyala Beker 						      funcs[i]->instance_id,
1788167e33f4SAyala Beker 						      NL80211_NAN_FUNC_TERM_REASON_ERROR,
1789167e33f4SAyala Beker 						      GFP_KERNEL);
1790167e33f4SAyala Beker 	}
1791167e33f4SAyala Beker 
1792167e33f4SAyala Beker 	kfree(funcs);
1793167e33f4SAyala Beker 
1794167e33f4SAyala Beker 	return 0;
1795167e33f4SAyala Beker }
1796167e33f4SAyala Beker 
1797f2753ddbSJohannes Berg int ieee80211_reconfig(struct ieee80211_local *local)
1798f2753ddbSJohannes Berg {
1799f2753ddbSJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
1800f2753ddbSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
180155de908aSJohannes Berg 	struct ieee80211_chanctx *ctx;
1802f2753ddbSJohannes Berg 	struct sta_info *sta;
18032683d65bSEliad Peller 	int res, i;
1804d888130aSJohannes Berg 	bool reconfig_due_to_wowlan = false;
1805d43c6b6eSDavid Spinadel 	struct ieee80211_sub_if_data *sched_scan_sdata;
18066ea0a69cSJohannes Berg 	struct cfg80211_sched_scan_request *sched_scan_req;
1807d43c6b6eSDavid Spinadel 	bool sched_scan_stopped = false;
1808b0485e9fSEliad Peller 	bool suspended = local->suspended;
1809d888130aSJohannes Berg 
18100f8b8245SEliad Peller 	/* nothing to do if HW shouldn't run */
18110f8b8245SEliad Peller 	if (!local->open_count)
18120f8b8245SEliad Peller 		goto wake_up;
18130f8b8245SEliad Peller 
18148f21b0adSJohannes Berg #ifdef CONFIG_PM
1815b0485e9fSEliad Peller 	if (suspended)
1816ceb99fe0SJohannes Berg 		local->resuming = true;
1817f2753ddbSJohannes Berg 
1818eecc4800SJohannes Berg 	if (local->wowlan) {
1819b0485e9fSEliad Peller 		/*
1820b0485e9fSEliad Peller 		 * In the wowlan case, both mac80211 and the device
1821b0485e9fSEliad Peller 		 * are functional when the resume op is called, so
1822b0485e9fSEliad Peller 		 * clear local->suspended so the device could operate
1823b0485e9fSEliad Peller 		 * normally (e.g. pass rx frames).
1824b0485e9fSEliad Peller 		 */
1825b0485e9fSEliad Peller 		local->suspended = false;
1826eecc4800SJohannes Berg 		res = drv_resume(local);
182727b3eb9cSJohannes Berg 		local->wowlan = false;
1828eecc4800SJohannes Berg 		if (res < 0) {
1829eecc4800SJohannes Berg 			local->resuming = false;
1830eecc4800SJohannes Berg 			return res;
1831eecc4800SJohannes Berg 		}
1832eecc4800SJohannes Berg 		if (res == 0)
1833eecc4800SJohannes Berg 			goto wake_up;
1834eecc4800SJohannes Berg 		WARN_ON(res > 1);
1835eecc4800SJohannes Berg 		/*
1836eecc4800SJohannes Berg 		 * res is 1, which means the driver requested
1837eecc4800SJohannes Berg 		 * to go through a regular reset on wakeup.
1838b0485e9fSEliad Peller 		 * restore local->suspended in this case.
1839eecc4800SJohannes Berg 		 */
1840d888130aSJohannes Berg 		reconfig_due_to_wowlan = true;
1841b0485e9fSEliad Peller 		local->suspended = true;
1842eecc4800SJohannes Berg 	}
1843eecc4800SJohannes Berg #endif
184494f9b97bSJohannes Berg 
184524feda00SLuis R. Rodriguez 	/*
184643d6df00SEliad Peller 	 * In case of hw_restart during suspend (without wowlan),
184743d6df00SEliad Peller 	 * cancel restart work, as we are reconfiguring the device
184843d6df00SEliad Peller 	 * anyway.
184943d6df00SEliad Peller 	 * Note that restart_work is scheduled on a frozen workqueue,
185043d6df00SEliad Peller 	 * so we can't deadlock in this case.
185143d6df00SEliad Peller 	 */
185243d6df00SEliad Peller 	if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
185343d6df00SEliad Peller 		cancel_work_sync(&local->restart_work);
185443d6df00SEliad Peller 
1855968a76ceSEliad Peller 	local->started = false;
1856968a76ceSEliad Peller 
185743d6df00SEliad Peller 	/*
185824feda00SLuis R. Rodriguez 	 * Upon resume hardware can sometimes be goofy due to
185924feda00SLuis R. Rodriguez 	 * various platform / driver / bus issues, so restarting
186024feda00SLuis R. Rodriguez 	 * the device may at times not work immediately. Propagate
186124feda00SLuis R. Rodriguez 	 * the error.
186224feda00SLuis R. Rodriguez 	 */
186324487981SJohannes Berg 	res = drv_start(local);
186424feda00SLuis R. Rodriguez 	if (res) {
1865b0485e9fSEliad Peller 		if (suspended)
1866f6837ba8SJohannes Berg 			WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
1867f6837ba8SJohannes Berg 		else
1868f6837ba8SJohannes Berg 			WARN(1, "Hardware became unavailable during restart.\n");
1869f6837ba8SJohannes Berg 		ieee80211_handle_reconfig_failure(local);
187024feda00SLuis R. Rodriguez 		return res;
187124feda00SLuis R. Rodriguez 	}
1872f2753ddbSJohannes Berg 
18737f281975SYogesh Ashok Powar 	/* setup fragmentation threshold */
18747f281975SYogesh Ashok Powar 	drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
18757f281975SYogesh Ashok Powar 
18767f281975SYogesh Ashok Powar 	/* setup RTS threshold */
18777f281975SYogesh Ashok Powar 	drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
18787f281975SYogesh Ashok Powar 
18797f281975SYogesh Ashok Powar 	/* reset coverage class */
18807f281975SYogesh Ashok Powar 	drv_set_coverage_class(local, hw->wiphy->coverage_class);
18817f281975SYogesh Ashok Powar 
18821f87f7d3SJohannes Berg 	ieee80211_led_radio(local, true);
188367408c8cSJohannes Berg 	ieee80211_mod_tpt_led_trig(local,
188467408c8cSJohannes Berg 				   IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1885f2753ddbSJohannes Berg 
1886f2753ddbSJohannes Berg 	/* add interfaces */
18874b6f1dd6SJohannes Berg 	sdata = rtnl_dereference(local->monitor_sdata);
18884b6f1dd6SJohannes Berg 	if (sdata) {
18893c3e21e7SJohannes Berg 		/* in HW restart it exists already */
18903c3e21e7SJohannes Berg 		WARN_ON(local->resuming);
18914b6f1dd6SJohannes Berg 		res = drv_add_interface(local, sdata);
18924b6f1dd6SJohannes Berg 		if (WARN_ON(res)) {
18930c2bef46SMonam Agarwal 			RCU_INIT_POINTER(local->monitor_sdata, NULL);
18944b6f1dd6SJohannes Berg 			synchronize_net();
18954b6f1dd6SJohannes Berg 			kfree(sdata);
18964b6f1dd6SJohannes Berg 		}
18974b6f1dd6SJohannes Berg 	}
18984b6f1dd6SJohannes Berg 
1899f2753ddbSJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
1900f2753ddbSJohannes Berg 		if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1901f2753ddbSJohannes Berg 		    sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1902c8fff3dcSLuciano Coelho 		    ieee80211_sdata_running(sdata)) {
19037b7eab6fSJohannes Berg 			res = drv_add_interface(local, sdata);
1904c8fff3dcSLuciano Coelho 			if (WARN_ON(res))
1905c8fff3dcSLuciano Coelho 				break;
1906c8fff3dcSLuciano Coelho 		}
1907c8fff3dcSLuciano Coelho 	}
1908c8fff3dcSLuciano Coelho 
1909c8fff3dcSLuciano Coelho 	/* If adding any of the interfaces failed above, roll back and
1910c8fff3dcSLuciano Coelho 	 * report failure.
1911c8fff3dcSLuciano Coelho 	 */
1912c8fff3dcSLuciano Coelho 	if (res) {
1913c8fff3dcSLuciano Coelho 		list_for_each_entry_continue_reverse(sdata, &local->interfaces,
1914c8fff3dcSLuciano Coelho 						     list)
1915c8fff3dcSLuciano Coelho 			if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1916c8fff3dcSLuciano Coelho 			    sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1917c8fff3dcSLuciano Coelho 			    ieee80211_sdata_running(sdata))
1918c8fff3dcSLuciano Coelho 				drv_remove_interface(local, sdata);
1919c8fff3dcSLuciano Coelho 		ieee80211_handle_reconfig_failure(local);
1920c8fff3dcSLuciano Coelho 		return res;
1921f2753ddbSJohannes Berg 	}
1922f2753ddbSJohannes Berg 
192355de908aSJohannes Berg 	/* add channel contexts */
1924f0dea9c7SArend van Spriel 	if (local->use_chanctx) {
192555de908aSJohannes Berg 		mutex_lock(&local->chanctx_mtx);
192655de908aSJohannes Berg 		list_for_each_entry(ctx, &local->chanctx_list, list)
19275bcae31dSMichal Kazior 			if (ctx->replace_state !=
19285bcae31dSMichal Kazior 			    IEEE80211_CHANCTX_REPLACES_OTHER)
192955de908aSJohannes Berg 				WARN_ON(drv_add_chanctx(local, ctx));
193055de908aSJohannes Berg 		mutex_unlock(&local->chanctx_mtx);
193155de908aSJohannes Berg 
1932fe5f2559SJohannes Berg 		sdata = rtnl_dereference(local->monitor_sdata);
1933153a5fc4SStanislaw Gruszka 		if (sdata && ieee80211_sdata_running(sdata))
1934153a5fc4SStanislaw Gruszka 			ieee80211_assign_chanctx(local, sdata);
19357df180f7SZhao, Gang 	}
1936fe5f2559SJohannes Berg 
1937f2753ddbSJohannes Berg 	/* reconfigure hardware */
1938f2753ddbSJohannes Berg 	ieee80211_hw_config(local, ~0);
1939f2753ddbSJohannes Berg 
1940f2753ddbSJohannes Berg 	ieee80211_configure_filter(local);
1941f2753ddbSJohannes Berg 
1942f2753ddbSJohannes Berg 	/* Finally also reconfigure all the BSS information */
1943f2753ddbSJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
1944ac8dd506SJohannes Berg 		u32 changed;
1945ac8dd506SJohannes Berg 
19469607e6b6SJohannes Berg 		if (!ieee80211_sdata_running(sdata))
1947f2753ddbSJohannes Berg 			continue;
1948ac8dd506SJohannes Berg 
19491ea2c864SJohannes Berg 		ieee80211_assign_chanctx(local, sdata);
19501ea2c864SJohannes Berg 
19511ea2c864SJohannes Berg 		switch (sdata->vif.type) {
19521ea2c864SJohannes Berg 		case NL80211_IFTYPE_AP_VLAN:
19531ea2c864SJohannes Berg 		case NL80211_IFTYPE_MONITOR:
19541ea2c864SJohannes Berg 			break;
19551ea2c864SJohannes Berg 		default:
19561ea2c864SJohannes Berg 			ieee80211_reconfig_stations(sdata);
19571ea2c864SJohannes Berg 			/* fall through */
19581ea2c864SJohannes Berg 		case NL80211_IFTYPE_AP: /* AP stations are handled later */
19591ea2c864SJohannes Berg 			for (i = 0; i < IEEE80211_NUM_ACS; i++)
19601ea2c864SJohannes Berg 				drv_conf_tx(local, sdata, i,
19611ea2c864SJohannes Berg 					    &sdata->tx_conf[i]);
19621ea2c864SJohannes Berg 			break;
19631ea2c864SJohannes Berg 		}
19641ea2c864SJohannes Berg 
1965ac8dd506SJohannes Berg 		/* common change flags for all interface types */
1966ac8dd506SJohannes Berg 		changed = BSS_CHANGED_ERP_CTS_PROT |
1967ac8dd506SJohannes Berg 			  BSS_CHANGED_ERP_PREAMBLE |
1968ac8dd506SJohannes Berg 			  BSS_CHANGED_ERP_SLOT |
1969ac8dd506SJohannes Berg 			  BSS_CHANGED_HT |
1970ac8dd506SJohannes Berg 			  BSS_CHANGED_BASIC_RATES |
1971ac8dd506SJohannes Berg 			  BSS_CHANGED_BEACON_INT |
1972ac8dd506SJohannes Berg 			  BSS_CHANGED_BSSID |
19734ced3f74SJohannes Berg 			  BSS_CHANGED_CQM |
197455de47f6SEliad Peller 			  BSS_CHANGED_QOS |
19751ea6f9c0SJohannes Berg 			  BSS_CHANGED_IDLE |
19761ea6f9c0SJohannes Berg 			  BSS_CHANGED_TXPOWER;
1977ac8dd506SJohannes Berg 
1978b5a33d52SSara Sharon 		if (sdata->vif.mu_mimo_owner)
197923a1f8d4SSara Sharon 			changed |= BSS_CHANGED_MU_GROUPS;
198023a1f8d4SSara Sharon 
1981f2753ddbSJohannes Berg 		switch (sdata->vif.type) {
1982f2753ddbSJohannes Berg 		case NL80211_IFTYPE_STATION:
19830d392e93SEliad Peller 			changed |= BSS_CHANGED_ASSOC |
1984ab095877SEliad Peller 				   BSS_CHANGED_ARP_FILTER |
1985ab095877SEliad Peller 				   BSS_CHANGED_PS;
1986c65dd147SEmmanuel Grumbach 
1987989c6505SAlexander Bondar 			/* Re-send beacon info report to the driver */
1988989c6505SAlexander Bondar 			if (sdata->u.mgd.have_beacon)
1989989c6505SAlexander Bondar 				changed |= BSS_CHANGED_BEACON_INFO;
1990c65dd147SEmmanuel Grumbach 
1991e38a017bSAvraham Stern 			if (sdata->vif.bss_conf.max_idle_period ||
1992e38a017bSAvraham Stern 			    sdata->vif.bss_conf.protected_keep_alive)
1993e38a017bSAvraham Stern 				changed |= BSS_CHANGED_KEEP_ALIVE;
1994e38a017bSAvraham Stern 
19958d61ffa5SJohannes Berg 			sdata_lock(sdata);
1996ac8dd506SJohannes Berg 			ieee80211_bss_info_change_notify(sdata, changed);
19978d61ffa5SJohannes Berg 			sdata_unlock(sdata);
1998ac8dd506SJohannes Berg 			break;
19996e0bd6c3SRostislav Lisovy 		case NL80211_IFTYPE_OCB:
2000239281f8SRostislav Lisovy 			changed |= BSS_CHANGED_OCB;
2001239281f8SRostislav Lisovy 			ieee80211_bss_info_change_notify(sdata, changed);
20026e0bd6c3SRostislav Lisovy 			break;
2003f2753ddbSJohannes Berg 		case NL80211_IFTYPE_ADHOC:
2004ac8dd506SJohannes Berg 			changed |= BSS_CHANGED_IBSS;
2005ac8dd506SJohannes Berg 			/* fall through */
2006f2753ddbSJohannes Berg 		case NL80211_IFTYPE_AP:
2007339afbf4SJohannes Berg 			changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
2008e7979ac7SArik Nemtsov 
20091041638fSJohannes Berg 			if (sdata->vif.type == NL80211_IFTYPE_AP) {
2010e7979ac7SArik Nemtsov 				changed |= BSS_CHANGED_AP_PROBE_RESP;
2011e7979ac7SArik Nemtsov 
20121041638fSJohannes Berg 				if (rcu_access_pointer(sdata->u.ap.beacon))
20131041638fSJohannes Berg 					drv_start_ap(local, sdata);
20141041638fSJohannes Berg 			}
20151041638fSJohannes Berg 
20167827493bSArik Nemtsov 			/* fall through */
2017f2753ddbSJohannes Berg 		case NL80211_IFTYPE_MESH_POINT:
20188da34932SJohannes Berg 			if (sdata->vif.bss_conf.enable_beacon) {
2019ac8dd506SJohannes Berg 				changed |= BSS_CHANGED_BEACON |
2020ac8dd506SJohannes Berg 					   BSS_CHANGED_BEACON_ENABLED;
20212d0ddec5SJohannes Berg 				ieee80211_bss_info_change_notify(sdata, changed);
20228da34932SJohannes Berg 			}
2023f2753ddbSJohannes Berg 			break;
2024167e33f4SAyala Beker 		case NL80211_IFTYPE_NAN:
2025167e33f4SAyala Beker 			res = ieee80211_reconfig_nan(sdata);
2026167e33f4SAyala Beker 			if (res < 0) {
2027167e33f4SAyala Beker 				ieee80211_handle_reconfig_failure(local);
2028167e33f4SAyala Beker 				return res;
2029167e33f4SAyala Beker 			}
2030167e33f4SAyala Beker 			break;
2031f2753ddbSJohannes Berg 		case NL80211_IFTYPE_WDS:
2032f2753ddbSJohannes Berg 		case NL80211_IFTYPE_AP_VLAN:
2033f2753ddbSJohannes Berg 		case NL80211_IFTYPE_MONITOR:
203498104fdeSJohannes Berg 		case NL80211_IFTYPE_P2P_DEVICE:
2035b205786eSZhao, Gang 			/* nothing to do */
2036f142c6b9SJohannes Berg 			break;
2037f2753ddbSJohannes Berg 		case NL80211_IFTYPE_UNSPECIFIED:
20382e161f78SJohannes Berg 		case NUM_NL80211_IFTYPES:
20392ca27bcfSJohannes Berg 		case NL80211_IFTYPE_P2P_CLIENT:
20402ca27bcfSJohannes Berg 		case NL80211_IFTYPE_P2P_GO:
2041f2753ddbSJohannes Berg 			WARN_ON(1);
2042f2753ddbSJohannes Berg 			break;
2043f2753ddbSJohannes Berg 		}
2044f2753ddbSJohannes Berg 	}
2045f2753ddbSJohannes Berg 
20464a733ef1SJohannes Berg 	ieee80211_recalc_ps(local);
20478e1b23b9SEyal Shapira 
20482a419056SJohannes Berg 	/*
20496e1b1b24SEliad Peller 	 * The sta might be in psm against the ap (e.g. because
20506e1b1b24SEliad Peller 	 * this was the state before a hw restart), so we
20516e1b1b24SEliad Peller 	 * explicitly send a null packet in order to make sure
20526e1b1b24SEliad Peller 	 * it'll sync against the ap (and get out of psm).
20536e1b1b24SEliad Peller 	 */
20546e1b1b24SEliad Peller 	if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
20556e1b1b24SEliad Peller 		list_for_each_entry(sdata, &local->interfaces, list) {
20566e1b1b24SEliad Peller 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
20576e1b1b24SEliad Peller 				continue;
205820f544eeSJohannes Berg 			if (!sdata->u.mgd.associated)
205920f544eeSJohannes Berg 				continue;
20606e1b1b24SEliad Peller 
2061076cdcb1SJohannes Berg 			ieee80211_send_nullfunc(local, sdata, false);
20626e1b1b24SEliad Peller 		}
20636e1b1b24SEliad Peller 	}
20646e1b1b24SEliad Peller 
20652e8d397eSArik Nemtsov 	/* APs are now beaconing, add back stations */
20662e8d397eSArik Nemtsov 	mutex_lock(&local->sta_mtx);
20672e8d397eSArik Nemtsov 	list_for_each_entry(sta, &local->sta_list, list) {
20682e8d397eSArik Nemtsov 		enum ieee80211_sta_state state;
20692e8d397eSArik Nemtsov 
20702e8d397eSArik Nemtsov 		if (!sta->uploaded)
20712e8d397eSArik Nemtsov 			continue;
20722e8d397eSArik Nemtsov 
20732e8d397eSArik Nemtsov 		if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
20742e8d397eSArik Nemtsov 			continue;
20752e8d397eSArik Nemtsov 
20762e8d397eSArik Nemtsov 		for (state = IEEE80211_STA_NOTEXIST;
20772e8d397eSArik Nemtsov 		     state < sta->sta_state; state++)
20782e8d397eSArik Nemtsov 			WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
20792e8d397eSArik Nemtsov 					      state + 1));
20802e8d397eSArik Nemtsov 	}
20812e8d397eSArik Nemtsov 	mutex_unlock(&local->sta_mtx);
20822e8d397eSArik Nemtsov 
20837b21aea0SEyal Shapira 	/* add back keys */
20847b21aea0SEyal Shapira 	list_for_each_entry(sdata, &local->interfaces, list)
2085f9dca80bSMichal Kazior 		ieee80211_reset_crypto_tx_tailroom(sdata);
2086f9dca80bSMichal Kazior 
2087f9dca80bSMichal Kazior 	list_for_each_entry(sdata, &local->interfaces, list)
20887b21aea0SEyal Shapira 		if (ieee80211_sdata_running(sdata))
20897b21aea0SEyal Shapira 			ieee80211_enable_keys(sdata);
20907b21aea0SEyal Shapira 
20910d440ea2SEliad Peller 	/* Reconfigure sched scan if it was interrupted by FW restart */
20920d440ea2SEliad Peller 	mutex_lock(&local->mtx);
20930d440ea2SEliad Peller 	sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
20940d440ea2SEliad Peller 						lockdep_is_held(&local->mtx));
20950d440ea2SEliad Peller 	sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
20960d440ea2SEliad Peller 						lockdep_is_held(&local->mtx));
20970d440ea2SEliad Peller 	if (sched_scan_sdata && sched_scan_req)
20980d440ea2SEliad Peller 		/*
20990d440ea2SEliad Peller 		 * Sched scan stopped, but we don't want to report it. Instead,
21000d440ea2SEliad Peller 		 * we're trying to reschedule. However, if more than one scan
21010d440ea2SEliad Peller 		 * plan was set, we cannot reschedule since we don't know which
21020d440ea2SEliad Peller 		 * scan plan was currently running (and some scan plans may have
21030d440ea2SEliad Peller 		 * already finished).
21040d440ea2SEliad Peller 		 */
21050d440ea2SEliad Peller 		if (sched_scan_req->n_scan_plans > 1 ||
21060d440ea2SEliad Peller 		    __ieee80211_request_sched_scan_start(sched_scan_sdata,
2107b9f628fcSEliad Peller 							 sched_scan_req)) {
2108b9f628fcSEliad Peller 			RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
2109b9f628fcSEliad Peller 			RCU_INIT_POINTER(local->sched_scan_req, NULL);
21100d440ea2SEliad Peller 			sched_scan_stopped = true;
2111b9f628fcSEliad Peller 		}
21120d440ea2SEliad Peller 	mutex_unlock(&local->mtx);
21130d440ea2SEliad Peller 
21140d440ea2SEliad Peller 	if (sched_scan_stopped)
21150d440ea2SEliad Peller 		cfg80211_sched_scan_stopped_rtnl(local->hw.wiphy);
21160d440ea2SEliad Peller 
2117c6209488SEliad Peller  wake_up:
2118470f4d61SEliad Peller 	if (local->in_reconfig) {
211904800adaSArik Nemtsov 		local->in_reconfig = false;
212004800adaSArik Nemtsov 		barrier();
212104800adaSArik Nemtsov 
2122470f4d61SEliad Peller 		/* Restart deferred ROCs */
2123470f4d61SEliad Peller 		mutex_lock(&local->mtx);
2124470f4d61SEliad Peller 		ieee80211_start_next_roc(local);
2125470f4d61SEliad Peller 		mutex_unlock(&local->mtx);
2126470f4d61SEliad Peller 	}
2127470f4d61SEliad Peller 
21283c3e21e7SJohannes Berg 	if (local->monitors == local->open_count && local->monitors > 0)
21293c3e21e7SJohannes Berg 		ieee80211_add_virtual_monitor(local);
21303c3e21e7SJohannes Berg 
21316e1b1b24SEliad Peller 	/*
21322a419056SJohannes Berg 	 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
21332a419056SJohannes Berg 	 * sessions can be established after a resume.
21342a419056SJohannes Berg 	 *
21352a419056SJohannes Berg 	 * Also tear down aggregation sessions since reconfiguring
21362a419056SJohannes Berg 	 * them in a hardware restart scenario is not easily done
21372a419056SJohannes Berg 	 * right now, and the hardware will have lost information
21382a419056SJohannes Berg 	 * about the sessions, but we and the AP still think they
21392a419056SJohannes Berg 	 * are active. This is really a workaround though.
21402a419056SJohannes Berg 	 */
214130686bf7SJohannes Berg 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
21422a419056SJohannes Berg 		mutex_lock(&local->sta_mtx);
21432a419056SJohannes Berg 
21442a419056SJohannes Berg 		list_for_each_entry(sta, &local->sta_list, list) {
214527392719SEliad Peller 			if (!local->resuming)
2146c82c4a80SJohannes Berg 				ieee80211_sta_tear_down_BA_sessions(
2147c82c4a80SJohannes Berg 						sta, AGG_STOP_LOCAL_REQUEST);
2148c2c98fdeSJohannes Berg 			clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
214974e2bd1fSWey-Yi Guy 		}
21502a419056SJohannes Berg 
21512a419056SJohannes Berg 		mutex_unlock(&local->sta_mtx);
215274e2bd1fSWey-Yi Guy 	}
215374e2bd1fSWey-Yi Guy 
2154445ea4e8SJohannes Berg 	ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
2155cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
2156cca07b00SLuciano Coelho 					false);
2157f2753ddbSJohannes Berg 
21585bb644a0SJohannes Berg 	/*
21595bb644a0SJohannes Berg 	 * If this is for hw restart things are still running.
21605bb644a0SJohannes Berg 	 * We may want to change that later, however.
21615bb644a0SJohannes Berg 	 */
2162b0485e9fSEliad Peller 	if (local->open_count && (!suspended || reconfig_due_to_wowlan))
2163cf2c92d8SEliad Peller 		drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
21648f21b0adSJohannes Berg 
2165b0485e9fSEliad Peller 	if (!suspended)
21665bb644a0SJohannes Berg 		return 0;
21675bb644a0SJohannes Berg 
21685bb644a0SJohannes Berg #ifdef CONFIG_PM
2169ceb99fe0SJohannes Berg 	/* first set suspended false, then resuming */
21705bb644a0SJohannes Berg 	local->suspended = false;
2171ceb99fe0SJohannes Berg 	mb();
2172ceb99fe0SJohannes Berg 	local->resuming = false;
21735bb644a0SJohannes Berg 
217474430f94SJohannes Berg 	ieee80211_flush_completed_scan(local, false);
21759120d94eSLuciano Coelho 
21760f8b8245SEliad Peller 	if (local->open_count && !reconfig_due_to_wowlan)
2177cf2c92d8SEliad Peller 		drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
2178cf2c92d8SEliad Peller 
2179b8360ab8SJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
2180b8360ab8SJohannes Berg 		if (!ieee80211_sdata_running(sdata))
2181b8360ab8SJohannes Berg 			continue;
2182b8360ab8SJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_STATION)
2183b8360ab8SJohannes Berg 			ieee80211_sta_restart(sdata);
2184b8360ab8SJohannes Berg 	}
2185b8360ab8SJohannes Berg 
218626d59535SJohannes Berg 	mod_timer(&local->sta_cleanup, jiffies + 1);
21875bb644a0SJohannes Berg #else
21885bb644a0SJohannes Berg 	WARN_ON(1);
21895bb644a0SJohannes Berg #endif
2190d43c6b6eSDavid Spinadel 
2191f2753ddbSJohannes Berg 	return 0;
2192f2753ddbSJohannes Berg }
219342935ecaSLuis R. Rodriguez 
219495acac61SJohannes Berg void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
219595acac61SJohannes Berg {
219695acac61SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
219795acac61SJohannes Berg 	struct ieee80211_local *local;
219895acac61SJohannes Berg 	struct ieee80211_key *key;
219995acac61SJohannes Berg 
220095acac61SJohannes Berg 	if (WARN_ON(!vif))
220195acac61SJohannes Berg 		return;
220295acac61SJohannes Berg 
220395acac61SJohannes Berg 	sdata = vif_to_sdata(vif);
220495acac61SJohannes Berg 	local = sdata->local;
220595acac61SJohannes Berg 
220695acac61SJohannes Berg 	if (WARN_ON(!local->resuming))
220795acac61SJohannes Berg 		return;
220895acac61SJohannes Berg 
220995acac61SJohannes Berg 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
221095acac61SJohannes Berg 		return;
221195acac61SJohannes Berg 
221295acac61SJohannes Berg 	sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
221395acac61SJohannes Berg 
221495acac61SJohannes Berg 	mutex_lock(&local->key_mtx);
221595acac61SJohannes Berg 	list_for_each_entry(key, &sdata->key_list, list)
221695acac61SJohannes Berg 		key->flags |= KEY_FLAG_TAINTED;
221795acac61SJohannes Berg 	mutex_unlock(&local->key_mtx);
221895acac61SJohannes Berg }
221995acac61SJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
222095acac61SJohannes Berg 
222104ecd257SJohannes Berg void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
22220f78231bSJohannes Berg {
222304ecd257SJohannes Berg 	struct ieee80211_local *local = sdata->local;
222404ecd257SJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
222504ecd257SJohannes Berg 	struct ieee80211_chanctx *chanctx;
22260f78231bSJohannes Berg 
222704ecd257SJohannes Berg 	mutex_lock(&local->chanctx_mtx);
22280f78231bSJohannes Berg 
222904ecd257SJohannes Berg 	chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
223004ecd257SJohannes Berg 					lockdep_is_held(&local->chanctx_mtx));
22310f78231bSJohannes Berg 
2232c189a685SAndrei Otcheretianski 	/*
2233c189a685SAndrei Otcheretianski 	 * This function can be called from a work, thus it may be possible
2234c189a685SAndrei Otcheretianski 	 * that the chanctx_conf is removed (due to a disconnection, for
2235c189a685SAndrei Otcheretianski 	 * example).
2236c189a685SAndrei Otcheretianski 	 * So nothing should be done in such case.
2237c189a685SAndrei Otcheretianski 	 */
2238c189a685SAndrei Otcheretianski 	if (!chanctx_conf)
22395d8e4237SJohannes Berg 		goto unlock;
22400f78231bSJohannes Berg 
224104ecd257SJohannes Berg 	chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
224204ecd257SJohannes Berg 	ieee80211_recalc_smps_chanctx(local, chanctx);
22435d8e4237SJohannes Berg  unlock:
224404ecd257SJohannes Berg 	mutex_unlock(&local->chanctx_mtx);
22450f78231bSJohannes Berg }
22468e664fb3SJohannes Berg 
224721f659bfSEliad Peller void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata)
224821f659bfSEliad Peller {
224921f659bfSEliad Peller 	struct ieee80211_local *local = sdata->local;
225021f659bfSEliad Peller 	struct ieee80211_chanctx_conf *chanctx_conf;
225121f659bfSEliad Peller 	struct ieee80211_chanctx *chanctx;
225221f659bfSEliad Peller 
225321f659bfSEliad Peller 	mutex_lock(&local->chanctx_mtx);
225421f659bfSEliad Peller 
225521f659bfSEliad Peller 	chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
225621f659bfSEliad Peller 					lockdep_is_held(&local->chanctx_mtx));
225721f659bfSEliad Peller 
225821f659bfSEliad Peller 	if (WARN_ON_ONCE(!chanctx_conf))
225921f659bfSEliad Peller 		goto unlock;
226021f659bfSEliad Peller 
226121f659bfSEliad Peller 	chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
226221f659bfSEliad Peller 	ieee80211_recalc_chanctx_min_def(local, chanctx);
226321f659bfSEliad Peller  unlock:
226421f659bfSEliad Peller 	mutex_unlock(&local->chanctx_mtx);
226521f659bfSEliad Peller }
226621f659bfSEliad Peller 
22678e664fb3SJohannes Berg size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
22688e664fb3SJohannes Berg {
22698e664fb3SJohannes Berg 	size_t pos = offset;
22708e664fb3SJohannes Berg 
22718e664fb3SJohannes Berg 	while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
22728e664fb3SJohannes Berg 		pos += 2 + ies[pos + 1];
22738e664fb3SJohannes Berg 
22748e664fb3SJohannes Berg 	return pos;
22758e664fb3SJohannes Berg }
2276615f7b9bSMeenakshi Venkataraman 
2277615f7b9bSMeenakshi Venkataraman static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
2278615f7b9bSMeenakshi Venkataraman 					    int rssi_min_thold,
2279615f7b9bSMeenakshi Venkataraman 					    int rssi_max_thold)
2280615f7b9bSMeenakshi Venkataraman {
2281615f7b9bSMeenakshi Venkataraman 	trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
2282615f7b9bSMeenakshi Venkataraman 
2283615f7b9bSMeenakshi Venkataraman 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2284615f7b9bSMeenakshi Venkataraman 		return;
2285615f7b9bSMeenakshi Venkataraman 
2286615f7b9bSMeenakshi Venkataraman 	/*
2287615f7b9bSMeenakshi Venkataraman 	 * Scale up threshold values before storing it, as the RSSI averaging
2288615f7b9bSMeenakshi Venkataraman 	 * algorithm uses a scaled up value as well. Change this scaling
2289615f7b9bSMeenakshi Venkataraman 	 * factor if the RSSI averaging algorithm changes.
2290615f7b9bSMeenakshi Venkataraman 	 */
2291615f7b9bSMeenakshi Venkataraman 	sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
2292615f7b9bSMeenakshi Venkataraman 	sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
2293615f7b9bSMeenakshi Venkataraman }
2294615f7b9bSMeenakshi Venkataraman 
2295615f7b9bSMeenakshi Venkataraman void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
2296615f7b9bSMeenakshi Venkataraman 				    int rssi_min_thold,
2297615f7b9bSMeenakshi Venkataraman 				    int rssi_max_thold)
2298615f7b9bSMeenakshi Venkataraman {
2299615f7b9bSMeenakshi Venkataraman 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2300615f7b9bSMeenakshi Venkataraman 
2301615f7b9bSMeenakshi Venkataraman 	WARN_ON(rssi_min_thold == rssi_max_thold ||
2302615f7b9bSMeenakshi Venkataraman 		rssi_min_thold > rssi_max_thold);
2303615f7b9bSMeenakshi Venkataraman 
2304615f7b9bSMeenakshi Venkataraman 	_ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
2305615f7b9bSMeenakshi Venkataraman 				       rssi_max_thold);
2306615f7b9bSMeenakshi Venkataraman }
2307615f7b9bSMeenakshi Venkataraman EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
2308615f7b9bSMeenakshi Venkataraman 
2309615f7b9bSMeenakshi Venkataraman void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
2310615f7b9bSMeenakshi Venkataraman {
2311615f7b9bSMeenakshi Venkataraman 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2312615f7b9bSMeenakshi Venkataraman 
2313615f7b9bSMeenakshi Venkataraman 	_ieee80211_enable_rssi_reports(sdata, 0, 0);
2314615f7b9bSMeenakshi Venkataraman }
2315615f7b9bSMeenakshi Venkataraman EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
2316768db343SArik Nemtsov 
2317ef96a842SBen Greear u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
231842e7aa77SAlexander Simon 			      u16 cap)
231942e7aa77SAlexander Simon {
232042e7aa77SAlexander Simon 	__le16 tmp;
232142e7aa77SAlexander Simon 
232242e7aa77SAlexander Simon 	*pos++ = WLAN_EID_HT_CAPABILITY;
232342e7aa77SAlexander Simon 	*pos++ = sizeof(struct ieee80211_ht_cap);
232442e7aa77SAlexander Simon 	memset(pos, 0, sizeof(struct ieee80211_ht_cap));
232542e7aa77SAlexander Simon 
232642e7aa77SAlexander Simon 	/* capability flags */
232742e7aa77SAlexander Simon 	tmp = cpu_to_le16(cap);
232842e7aa77SAlexander Simon 	memcpy(pos, &tmp, sizeof(u16));
232942e7aa77SAlexander Simon 	pos += sizeof(u16);
233042e7aa77SAlexander Simon 
233142e7aa77SAlexander Simon 	/* AMPDU parameters */
2332ef96a842SBen Greear 	*pos++ = ht_cap->ampdu_factor |
2333ef96a842SBen Greear 		 (ht_cap->ampdu_density <<
233442e7aa77SAlexander Simon 			IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
233542e7aa77SAlexander Simon 
233642e7aa77SAlexander Simon 	/* MCS set */
2337ef96a842SBen Greear 	memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
2338ef96a842SBen Greear 	pos += sizeof(ht_cap->mcs);
233942e7aa77SAlexander Simon 
234042e7aa77SAlexander Simon 	/* extended capabilities */
234142e7aa77SAlexander Simon 	pos += sizeof(__le16);
234242e7aa77SAlexander Simon 
234342e7aa77SAlexander Simon 	/* BF capabilities */
234442e7aa77SAlexander Simon 	pos += sizeof(__le32);
234542e7aa77SAlexander Simon 
234642e7aa77SAlexander Simon 	/* antenna selection */
234742e7aa77SAlexander Simon 	pos += sizeof(u8);
234842e7aa77SAlexander Simon 
234942e7aa77SAlexander Simon 	return pos;
235042e7aa77SAlexander Simon }
235142e7aa77SAlexander Simon 
2352ba0afa2fSMahesh Palivela u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2353ba0afa2fSMahesh Palivela 			       u32 cap)
2354ba0afa2fSMahesh Palivela {
2355ba0afa2fSMahesh Palivela 	__le32 tmp;
2356ba0afa2fSMahesh Palivela 
2357ba0afa2fSMahesh Palivela 	*pos++ = WLAN_EID_VHT_CAPABILITY;
2358d4950281SMahesh Palivela 	*pos++ = sizeof(struct ieee80211_vht_cap);
2359d4950281SMahesh Palivela 	memset(pos, 0, sizeof(struct ieee80211_vht_cap));
2360ba0afa2fSMahesh Palivela 
2361ba0afa2fSMahesh Palivela 	/* capability flags */
2362ba0afa2fSMahesh Palivela 	tmp = cpu_to_le32(cap);
2363ba0afa2fSMahesh Palivela 	memcpy(pos, &tmp, sizeof(u32));
2364ba0afa2fSMahesh Palivela 	pos += sizeof(u32);
2365ba0afa2fSMahesh Palivela 
2366ba0afa2fSMahesh Palivela 	/* VHT MCS set */
2367ba0afa2fSMahesh Palivela 	memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
2368ba0afa2fSMahesh Palivela 	pos += sizeof(vht_cap->vht_mcs);
2369ba0afa2fSMahesh Palivela 
2370ba0afa2fSMahesh Palivela 	return pos;
2371ba0afa2fSMahesh Palivela }
2372ba0afa2fSMahesh Palivela 
2373074d46d1SJohannes Berg u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
23744bf88530SJohannes Berg 			       const struct cfg80211_chan_def *chandef,
237557f255f5SArik Nemtsov 			       u16 prot_mode, bool rifs_mode)
237642e7aa77SAlexander Simon {
2377074d46d1SJohannes Berg 	struct ieee80211_ht_operation *ht_oper;
237842e7aa77SAlexander Simon 	/* Build HT Information */
2379074d46d1SJohannes Berg 	*pos++ = WLAN_EID_HT_OPERATION;
2380074d46d1SJohannes Berg 	*pos++ = sizeof(struct ieee80211_ht_operation);
2381074d46d1SJohannes Berg 	ht_oper = (struct ieee80211_ht_operation *)pos;
23824bf88530SJohannes Berg 	ht_oper->primary_chan = ieee80211_frequency_to_channel(
23834bf88530SJohannes Berg 					chandef->chan->center_freq);
23844bf88530SJohannes Berg 	switch (chandef->width) {
23854bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_160:
23864bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_80P80:
23874bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_80:
23884bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_40:
23894bf88530SJohannes Berg 		if (chandef->center_freq1 > chandef->chan->center_freq)
23904bf88530SJohannes Berg 			ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
23914bf88530SJohannes Berg 		else
2392074d46d1SJohannes Berg 			ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
239342e7aa77SAlexander Simon 		break;
239442e7aa77SAlexander Simon 	default:
2395074d46d1SJohannes Berg 		ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
239642e7aa77SAlexander Simon 		break;
239742e7aa77SAlexander Simon 	}
2398aee286c2SThomas Pedersen 	if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
23994bf88530SJohannes Berg 	    chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
24004bf88530SJohannes Berg 	    chandef->width != NL80211_CHAN_WIDTH_20)
2401074d46d1SJohannes Berg 		ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
2402ff3cc5f4SSimon Wunderlich 
240357f255f5SArik Nemtsov 	if (rifs_mode)
240457f255f5SArik Nemtsov 		ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
240557f255f5SArik Nemtsov 
2406431e3154SAshok Nagarajan 	ht_oper->operation_mode = cpu_to_le16(prot_mode);
2407074d46d1SJohannes Berg 	ht_oper->stbc_param = 0x0000;
240842e7aa77SAlexander Simon 
240942e7aa77SAlexander Simon 	/* It seems that Basic MCS set and Supported MCS set
241042e7aa77SAlexander Simon 	   are identical for the first 10 bytes */
2411074d46d1SJohannes Berg 	memset(&ht_oper->basic_set, 0, 16);
2412074d46d1SJohannes Berg 	memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
241342e7aa77SAlexander Simon 
2414074d46d1SJohannes Berg 	return pos + sizeof(struct ieee80211_ht_operation);
241542e7aa77SAlexander Simon }
241642e7aa77SAlexander Simon 
2417fb28ec0cSArik Nemtsov u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2418fb28ec0cSArik Nemtsov 				const struct cfg80211_chan_def *chandef)
2419fb28ec0cSArik Nemtsov {
2420fb28ec0cSArik Nemtsov 	struct ieee80211_vht_operation *vht_oper;
2421fb28ec0cSArik Nemtsov 
2422fb28ec0cSArik Nemtsov 	*pos++ = WLAN_EID_VHT_OPERATION;
2423fb28ec0cSArik Nemtsov 	*pos++ = sizeof(struct ieee80211_vht_operation);
2424fb28ec0cSArik Nemtsov 	vht_oper = (struct ieee80211_vht_operation *)pos;
24252fb51c35SJohannes Berg 	vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
2426fb28ec0cSArik Nemtsov 							chandef->center_freq1);
2427fb28ec0cSArik Nemtsov 	if (chandef->center_freq2)
24282fb51c35SJohannes Berg 		vht_oper->center_freq_seg1_idx =
2429fb28ec0cSArik Nemtsov 			ieee80211_frequency_to_channel(chandef->center_freq2);
2430f020ae40SChun-Yeow Yeoh 	else
24312fb51c35SJohannes Berg 		vht_oper->center_freq_seg1_idx = 0x00;
2432fb28ec0cSArik Nemtsov 
2433fb28ec0cSArik Nemtsov 	switch (chandef->width) {
2434fb28ec0cSArik Nemtsov 	case NL80211_CHAN_WIDTH_160:
243523665aafSJouni Malinen 		/*
243623665aafSJouni Malinen 		 * Convert 160 MHz channel width to new style as interop
243723665aafSJouni Malinen 		 * workaround.
243823665aafSJouni Malinen 		 */
243923665aafSJouni Malinen 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
24402fb51c35SJohannes Berg 		vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
244123665aafSJouni Malinen 		if (chandef->chan->center_freq < chandef->center_freq1)
24422fb51c35SJohannes Berg 			vht_oper->center_freq_seg0_idx -= 8;
244323665aafSJouni Malinen 		else
24442fb51c35SJohannes Berg 			vht_oper->center_freq_seg0_idx += 8;
2445fb28ec0cSArik Nemtsov 		break;
2446fb28ec0cSArik Nemtsov 	case NL80211_CHAN_WIDTH_80P80:
244723665aafSJouni Malinen 		/*
244823665aafSJouni Malinen 		 * Convert 80+80 MHz channel width to new style as interop
244923665aafSJouni Malinen 		 * workaround.
245023665aafSJouni Malinen 		 */
245123665aafSJouni Malinen 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
2452fb28ec0cSArik Nemtsov 		break;
2453fb28ec0cSArik Nemtsov 	case NL80211_CHAN_WIDTH_80:
2454fb28ec0cSArik Nemtsov 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
2455fb28ec0cSArik Nemtsov 		break;
2456fb28ec0cSArik Nemtsov 	default:
2457fb28ec0cSArik Nemtsov 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
2458fb28ec0cSArik Nemtsov 		break;
2459fb28ec0cSArik Nemtsov 	}
2460fb28ec0cSArik Nemtsov 
2461fb28ec0cSArik Nemtsov 	/* don't require special VHT peer rates */
2462fb28ec0cSArik Nemtsov 	vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
2463fb28ec0cSArik Nemtsov 
2464fb28ec0cSArik Nemtsov 	return pos + sizeof(struct ieee80211_vht_operation);
2465fb28ec0cSArik Nemtsov }
2466fb28ec0cSArik Nemtsov 
24678ac3c704SJohannes Berg bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
24684bf88530SJohannes Berg 			       struct cfg80211_chan_def *chandef)
246942e7aa77SAlexander Simon {
247042e7aa77SAlexander Simon 	enum nl80211_channel_type channel_type;
247142e7aa77SAlexander Simon 
24728ac3c704SJohannes Berg 	if (!ht_oper)
24738ac3c704SJohannes Berg 		return false;
247442e7aa77SAlexander Simon 
2475074d46d1SJohannes Berg 	switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
247642e7aa77SAlexander Simon 	case IEEE80211_HT_PARAM_CHA_SEC_NONE:
247742e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_HT20;
247842e7aa77SAlexander Simon 		break;
247942e7aa77SAlexander Simon 	case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
248042e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_HT40PLUS;
248142e7aa77SAlexander Simon 		break;
248242e7aa77SAlexander Simon 	case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
248342e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_HT40MINUS;
248442e7aa77SAlexander Simon 		break;
248542e7aa77SAlexander Simon 	default:
248642e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_NO_HT;
24878ac3c704SJohannes Berg 		return false;
248842e7aa77SAlexander Simon 	}
248942e7aa77SAlexander Simon 
24908ac3c704SJohannes Berg 	cfg80211_chandef_create(chandef, chandef->chan, channel_type);
24918ac3c704SJohannes Berg 	return true;
249242e7aa77SAlexander Simon }
249342e7aa77SAlexander Simon 
24948ac3c704SJohannes Berg bool ieee80211_chandef_vht_oper(const struct ieee80211_vht_operation *oper,
2495abcff6efSJanusz.Dziedzic@tieto.com 				struct cfg80211_chan_def *chandef)
2496abcff6efSJanusz.Dziedzic@tieto.com {
24978ac3c704SJohannes Berg 	struct cfg80211_chan_def new = *chandef;
24988ac3c704SJohannes Berg 	int cf1, cf2;
2499abcff6efSJanusz.Dziedzic@tieto.com 
25008ac3c704SJohannes Berg 	if (!oper)
25018ac3c704SJohannes Berg 		return false;
25028ac3c704SJohannes Berg 
25032fb51c35SJohannes Berg 	cf1 = ieee80211_channel_to_frequency(oper->center_freq_seg0_idx,
25048ac3c704SJohannes Berg 					     chandef->chan->band);
25052fb51c35SJohannes Berg 	cf2 = ieee80211_channel_to_frequency(oper->center_freq_seg1_idx,
25068ac3c704SJohannes Berg 					     chandef->chan->band);
2507abcff6efSJanusz.Dziedzic@tieto.com 
2508abcff6efSJanusz.Dziedzic@tieto.com 	switch (oper->chan_width) {
2509abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_USE_HT:
2510abcff6efSJanusz.Dziedzic@tieto.com 		break;
2511abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_80MHZ:
25128ac3c704SJohannes Berg 		new.width = NL80211_CHAN_WIDTH_80;
25138ac3c704SJohannes Berg 		new.center_freq1 = cf1;
251423665aafSJouni Malinen 		/* If needed, adjust based on the newer interop workaround. */
25152fb51c35SJohannes Berg 		if (oper->center_freq_seg1_idx) {
251623665aafSJouni Malinen 			unsigned int diff;
251723665aafSJouni Malinen 
25182fb51c35SJohannes Berg 			diff = abs(oper->center_freq_seg1_idx -
25192fb51c35SJohannes Berg 				   oper->center_freq_seg0_idx);
252023665aafSJouni Malinen 			if (diff == 8) {
252123665aafSJouni Malinen 				new.width = NL80211_CHAN_WIDTH_160;
252223665aafSJouni Malinen 				new.center_freq1 = cf2;
252323665aafSJouni Malinen 			} else if (diff > 8) {
252423665aafSJouni Malinen 				new.width = NL80211_CHAN_WIDTH_80P80;
252523665aafSJouni Malinen 				new.center_freq2 = cf2;
252623665aafSJouni Malinen 			}
252723665aafSJouni Malinen 		}
2528abcff6efSJanusz.Dziedzic@tieto.com 		break;
2529abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_160MHZ:
25308ac3c704SJohannes Berg 		new.width = NL80211_CHAN_WIDTH_160;
25318ac3c704SJohannes Berg 		new.center_freq1 = cf1;
2532abcff6efSJanusz.Dziedzic@tieto.com 		break;
2533abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
25348ac3c704SJohannes Berg 		new.width = NL80211_CHAN_WIDTH_80P80;
25358ac3c704SJohannes Berg 		new.center_freq1 = cf1;
25368ac3c704SJohannes Berg 		new.center_freq2 = cf2;
2537abcff6efSJanusz.Dziedzic@tieto.com 		break;
2538abcff6efSJanusz.Dziedzic@tieto.com 	default:
25398ac3c704SJohannes Berg 		return false;
2540abcff6efSJanusz.Dziedzic@tieto.com 	}
2541abcff6efSJanusz.Dziedzic@tieto.com 
25428ac3c704SJohannes Berg 	if (!cfg80211_chandef_valid(&new))
25438ac3c704SJohannes Berg 		return false;
25448ac3c704SJohannes Berg 
25458ac3c704SJohannes Berg 	*chandef = new;
25468ac3c704SJohannes Berg 	return true;
2547abcff6efSJanusz.Dziedzic@tieto.com }
2548abcff6efSJanusz.Dziedzic@tieto.com 
25492103dec1SSimon Wunderlich int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
25502103dec1SSimon Wunderlich 			     const struct ieee80211_supported_band *sband,
25512103dec1SSimon Wunderlich 			     const u8 *srates, int srates_len, u32 *rates)
25522103dec1SSimon Wunderlich {
25532103dec1SSimon Wunderlich 	u32 rate_flags = ieee80211_chandef_rate_flags(chandef);
25542103dec1SSimon Wunderlich 	int shift = ieee80211_chandef_get_shift(chandef);
25552103dec1SSimon Wunderlich 	struct ieee80211_rate *br;
25562103dec1SSimon Wunderlich 	int brate, rate, i, j, count = 0;
25572103dec1SSimon Wunderlich 
25582103dec1SSimon Wunderlich 	*rates = 0;
25592103dec1SSimon Wunderlich 
25602103dec1SSimon Wunderlich 	for (i = 0; i < srates_len; i++) {
25612103dec1SSimon Wunderlich 		rate = srates[i] & 0x7f;
25622103dec1SSimon Wunderlich 
25632103dec1SSimon Wunderlich 		for (j = 0; j < sband->n_bitrates; j++) {
25642103dec1SSimon Wunderlich 			br = &sband->bitrates[j];
25652103dec1SSimon Wunderlich 			if ((rate_flags & br->flags) != rate_flags)
25662103dec1SSimon Wunderlich 				continue;
25672103dec1SSimon Wunderlich 
25682103dec1SSimon Wunderlich 			brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
25692103dec1SSimon Wunderlich 			if (brate == rate) {
25702103dec1SSimon Wunderlich 				*rates |= BIT(j);
25712103dec1SSimon Wunderlich 				count++;
25722103dec1SSimon Wunderlich 				break;
25732103dec1SSimon Wunderlich 			}
25742103dec1SSimon Wunderlich 		}
25752103dec1SSimon Wunderlich 	}
25762103dec1SSimon Wunderlich 	return count;
25772103dec1SSimon Wunderlich }
25782103dec1SSimon Wunderlich 
2579fc8a7321SJohannes Berg int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
25806b77863bSJohannes Berg 			    struct sk_buff *skb, bool need_basic,
258157fbcce3SJohannes Berg 			    enum nl80211_band band)
2582768db343SArik Nemtsov {
2583768db343SArik Nemtsov 	struct ieee80211_local *local = sdata->local;
2584768db343SArik Nemtsov 	struct ieee80211_supported_band *sband;
25852103dec1SSimon Wunderlich 	int rate, shift;
2586768db343SArik Nemtsov 	u8 i, rates, *pos;
2587fc8a7321SJohannes Berg 	u32 basic_rates = sdata->vif.bss_conf.basic_rates;
25882103dec1SSimon Wunderlich 	u32 rate_flags;
2589768db343SArik Nemtsov 
25902103dec1SSimon Wunderlich 	shift = ieee80211_vif_get_shift(&sdata->vif);
25912103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
25926b77863bSJohannes Berg 	sband = local->hw.wiphy->bands[band];
25932103dec1SSimon Wunderlich 	rates = 0;
25942103dec1SSimon Wunderlich 	for (i = 0; i < sband->n_bitrates; i++) {
25952103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
25962103dec1SSimon Wunderlich 			continue;
25972103dec1SSimon Wunderlich 		rates++;
25982103dec1SSimon Wunderlich 	}
2599768db343SArik Nemtsov 	if (rates > 8)
2600768db343SArik Nemtsov 		rates = 8;
2601768db343SArik Nemtsov 
2602768db343SArik Nemtsov 	if (skb_tailroom(skb) < rates + 2)
2603768db343SArik Nemtsov 		return -ENOMEM;
2604768db343SArik Nemtsov 
2605768db343SArik Nemtsov 	pos = skb_put(skb, rates + 2);
2606768db343SArik Nemtsov 	*pos++ = WLAN_EID_SUPP_RATES;
2607768db343SArik Nemtsov 	*pos++ = rates;
2608768db343SArik Nemtsov 	for (i = 0; i < rates; i++) {
2609657c3e0cSAshok Nagarajan 		u8 basic = 0;
26102103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
26112103dec1SSimon Wunderlich 			continue;
26122103dec1SSimon Wunderlich 
2613657c3e0cSAshok Nagarajan 		if (need_basic && basic_rates & BIT(i))
2614657c3e0cSAshok Nagarajan 			basic = 0x80;
26152103dec1SSimon Wunderlich 		rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
26162103dec1SSimon Wunderlich 				    5 * (1 << shift));
26172103dec1SSimon Wunderlich 		*pos++ = basic | (u8) rate;
2618768db343SArik Nemtsov 	}
2619768db343SArik Nemtsov 
2620768db343SArik Nemtsov 	return 0;
2621768db343SArik Nemtsov }
2622768db343SArik Nemtsov 
2623fc8a7321SJohannes Berg int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
26246b77863bSJohannes Berg 				struct sk_buff *skb, bool need_basic,
262557fbcce3SJohannes Berg 				enum nl80211_band band)
2626768db343SArik Nemtsov {
2627768db343SArik Nemtsov 	struct ieee80211_local *local = sdata->local;
2628768db343SArik Nemtsov 	struct ieee80211_supported_band *sband;
2629cc63ec76SChun-Yeow Yeoh 	int rate, shift;
2630768db343SArik Nemtsov 	u8 i, exrates, *pos;
2631fc8a7321SJohannes Berg 	u32 basic_rates = sdata->vif.bss_conf.basic_rates;
26322103dec1SSimon Wunderlich 	u32 rate_flags;
26332103dec1SSimon Wunderlich 
26342103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
26352103dec1SSimon Wunderlich 	shift = ieee80211_vif_get_shift(&sdata->vif);
2636768db343SArik Nemtsov 
26376b77863bSJohannes Berg 	sband = local->hw.wiphy->bands[band];
26382103dec1SSimon Wunderlich 	exrates = 0;
26392103dec1SSimon Wunderlich 	for (i = 0; i < sband->n_bitrates; i++) {
26402103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
26412103dec1SSimon Wunderlich 			continue;
26422103dec1SSimon Wunderlich 		exrates++;
26432103dec1SSimon Wunderlich 	}
26442103dec1SSimon Wunderlich 
2645768db343SArik Nemtsov 	if (exrates > 8)
2646768db343SArik Nemtsov 		exrates -= 8;
2647768db343SArik Nemtsov 	else
2648768db343SArik Nemtsov 		exrates = 0;
2649768db343SArik Nemtsov 
2650768db343SArik Nemtsov 	if (skb_tailroom(skb) < exrates + 2)
2651768db343SArik Nemtsov 		return -ENOMEM;
2652768db343SArik Nemtsov 
2653768db343SArik Nemtsov 	if (exrates) {
2654768db343SArik Nemtsov 		pos = skb_put(skb, exrates + 2);
2655768db343SArik Nemtsov 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
2656768db343SArik Nemtsov 		*pos++ = exrates;
2657768db343SArik Nemtsov 		for (i = 8; i < sband->n_bitrates; i++) {
2658657c3e0cSAshok Nagarajan 			u8 basic = 0;
26592103dec1SSimon Wunderlich 			if ((rate_flags & sband->bitrates[i].flags)
26602103dec1SSimon Wunderlich 			    != rate_flags)
26612103dec1SSimon Wunderlich 				continue;
2662657c3e0cSAshok Nagarajan 			if (need_basic && basic_rates & BIT(i))
2663657c3e0cSAshok Nagarajan 				basic = 0x80;
26642103dec1SSimon Wunderlich 			rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
26652103dec1SSimon Wunderlich 					    5 * (1 << shift));
26662103dec1SSimon Wunderlich 			*pos++ = basic | (u8) rate;
2667768db343SArik Nemtsov 		}
2668768db343SArik Nemtsov 	}
2669768db343SArik Nemtsov 	return 0;
2670768db343SArik Nemtsov }
26711dae27f8SWey-Yi Guy 
26721dae27f8SWey-Yi Guy int ieee80211_ave_rssi(struct ieee80211_vif *vif)
26731dae27f8SWey-Yi Guy {
26741dae27f8SWey-Yi Guy 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
26751dae27f8SWey-Yi Guy 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
26761dae27f8SWey-Yi Guy 
2677be6bcabcSWey-Yi Guy 	if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
2678be6bcabcSWey-Yi Guy 		/* non-managed type inferfaces */
2679be6bcabcSWey-Yi Guy 		return 0;
2680be6bcabcSWey-Yi Guy 	}
2681338c17aeSJohannes Berg 	return -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
26821dae27f8SWey-Yi Guy }
26830d8a0a17SWey-Yi Guy EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
268404ecd257SJohannes Berg 
268504ecd257SJohannes Berg u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
268604ecd257SJohannes Berg {
268704ecd257SJohannes Berg 	if (!mcs)
268804ecd257SJohannes Berg 		return 1;
268904ecd257SJohannes Berg 
269004ecd257SJohannes Berg 	/* TODO: consider rx_highest */
269104ecd257SJohannes Berg 
269204ecd257SJohannes Berg 	if (mcs->rx_mask[3])
269304ecd257SJohannes Berg 		return 4;
269404ecd257SJohannes Berg 	if (mcs->rx_mask[2])
269504ecd257SJohannes Berg 		return 3;
269604ecd257SJohannes Berg 	if (mcs->rx_mask[1])
269704ecd257SJohannes Berg 		return 2;
269804ecd257SJohannes Berg 	return 1;
269904ecd257SJohannes Berg }
2700f4bda337SThomas Pedersen 
2701f4bda337SThomas Pedersen /**
2702f4bda337SThomas Pedersen  * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
2703f4bda337SThomas Pedersen  * @local: mac80211 hw info struct
2704f4bda337SThomas Pedersen  * @status: RX status
2705f4bda337SThomas Pedersen  * @mpdu_len: total MPDU length (including FCS)
2706f4bda337SThomas Pedersen  * @mpdu_offset: offset into MPDU to calculate timestamp at
2707f4bda337SThomas Pedersen  *
2708f4bda337SThomas Pedersen  * This function calculates the RX timestamp at the given MPDU offset, taking
2709f4bda337SThomas Pedersen  * into account what the RX timestamp was. An offset of 0 will just normalize
2710f4bda337SThomas Pedersen  * the timestamp to TSF at beginning of MPDU reception.
2711f4bda337SThomas Pedersen  */
2712f4bda337SThomas Pedersen u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
2713f4bda337SThomas Pedersen 				     struct ieee80211_rx_status *status,
2714f4bda337SThomas Pedersen 				     unsigned int mpdu_len,
2715f4bda337SThomas Pedersen 				     unsigned int mpdu_offset)
2716f4bda337SThomas Pedersen {
2717f4bda337SThomas Pedersen 	u64 ts = status->mactime;
2718f4bda337SThomas Pedersen 	struct rate_info ri;
2719f4bda337SThomas Pedersen 	u16 rate;
2720f4bda337SThomas Pedersen 
2721f4bda337SThomas Pedersen 	if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
2722f4bda337SThomas Pedersen 		return 0;
2723f4bda337SThomas Pedersen 
2724f4bda337SThomas Pedersen 	memset(&ri, 0, sizeof(ri));
2725f4bda337SThomas Pedersen 
2726f4bda337SThomas Pedersen 	/* Fill cfg80211 rate info */
2727da6a4352SJohannes Berg 	switch (status->encoding) {
2728da6a4352SJohannes Berg 	case RX_ENC_HT:
2729f4bda337SThomas Pedersen 		ri.mcs = status->rate_idx;
2730f4bda337SThomas Pedersen 		ri.flags |= RATE_INFO_FLAGS_MCS;
2731da6a4352SJohannes Berg 		ri.bw = status->bw;
27327fdd69c5SJohannes Berg 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
2733f4bda337SThomas Pedersen 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
2734da6a4352SJohannes Berg 		break;
2735da6a4352SJohannes Berg 	case RX_ENC_VHT:
27365614618eSJohannes Berg 		ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
27375614618eSJohannes Berg 		ri.mcs = status->rate_idx;
27388613c948SJohannes Berg 		ri.nss = status->nss;
2739da6a4352SJohannes Berg 		ri.bw = status->bw;
27407fdd69c5SJohannes Berg 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
27415614618eSJohannes Berg 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
2742da6a4352SJohannes Berg 		break;
2743da6a4352SJohannes Berg 	default:
2744da6a4352SJohannes Berg 		WARN_ON(1);
2745da6a4352SJohannes Berg 		/* fall through */
2746da6a4352SJohannes Berg 	case RX_ENC_LEGACY: {
2747f4bda337SThomas Pedersen 		struct ieee80211_supported_band *sband;
27482103dec1SSimon Wunderlich 		int shift = 0;
27492103dec1SSimon Wunderlich 		int bitrate;
27502103dec1SSimon Wunderlich 
2751da6a4352SJohannes Berg 		ri.bw = status->bw;
2752da6a4352SJohannes Berg 
2753da6a4352SJohannes Berg 		switch (status->bw) {
2754da6a4352SJohannes Berg 		case RATE_INFO_BW_10:
27552103dec1SSimon Wunderlich 			shift = 1;
2756da6a4352SJohannes Berg 			break;
2757da6a4352SJohannes Berg 		case RATE_INFO_BW_5:
27582103dec1SSimon Wunderlich 			shift = 2;
2759da6a4352SJohannes Berg 			break;
2760b51f3beeSJohannes Berg 		}
2761f4bda337SThomas Pedersen 
2762f4bda337SThomas Pedersen 		sband = local->hw.wiphy->bands[status->band];
27632103dec1SSimon Wunderlich 		bitrate = sband->bitrates[status->rate_idx].bitrate;
27642103dec1SSimon Wunderlich 		ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
2765f4a0f0c5SJohannes Berg 
2766f4a0f0c5SJohannes Berg 		if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
2767f4a0f0c5SJohannes Berg 			/* TODO: handle HT/VHT preambles */
276857fbcce3SJohannes Berg 			if (status->band == NL80211_BAND_5GHZ) {
2769f4a0f0c5SJohannes Berg 				ts += 20 << shift;
2770f4a0f0c5SJohannes Berg 				mpdu_offset += 2;
27717fdd69c5SJohannes Berg 			} else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
2772f4a0f0c5SJohannes Berg 				ts += 96;
2773f4a0f0c5SJohannes Berg 			} else {
2774f4a0f0c5SJohannes Berg 				ts += 192;
2775f4a0f0c5SJohannes Berg 			}
2776f4a0f0c5SJohannes Berg 		}
2777da6a4352SJohannes Berg 		break;
2778da6a4352SJohannes Berg 		}
2779f4bda337SThomas Pedersen 	}
2780f4bda337SThomas Pedersen 
2781f4bda337SThomas Pedersen 	rate = cfg80211_calculate_bitrate(&ri);
2782d86aa4f8SJohannes Berg 	if (WARN_ONCE(!rate,
2783f980ebc0SSara Sharon 		      "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
2784f980ebc0SSara Sharon 		      (unsigned long long)status->flag, status->rate_idx,
27858613c948SJohannes Berg 		      status->nss))
2786d86aa4f8SJohannes Berg 		return 0;
2787f4bda337SThomas Pedersen 
2788f4bda337SThomas Pedersen 	/* rewind from end of MPDU */
2789f4bda337SThomas Pedersen 	if (status->flag & RX_FLAG_MACTIME_END)
2790f4bda337SThomas Pedersen 		ts -= mpdu_len * 8 * 10 / rate;
2791f4bda337SThomas Pedersen 
2792f4bda337SThomas Pedersen 	ts += mpdu_offset * 8 * 10 / rate;
2793f4bda337SThomas Pedersen 
2794f4bda337SThomas Pedersen 	return ts;
2795f4bda337SThomas Pedersen }
2796164eb02dSSimon Wunderlich 
2797164eb02dSSimon Wunderlich void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
2798164eb02dSSimon Wunderlich {
2799164eb02dSSimon Wunderlich 	struct ieee80211_sub_if_data *sdata;
2800d2859df5SJanusz Dziedzic 	struct cfg80211_chan_def chandef;
2801164eb02dSSimon Wunderlich 
28024a199068SJohannes Berg 	/* for interface list, to avoid linking iflist_mtx and chanctx_mtx */
28034a199068SJohannes Berg 	ASSERT_RTNL();
28044a199068SJohannes Berg 
280534a3740dSJohannes Berg 	mutex_lock(&local->mtx);
2806164eb02dSSimon Wunderlich 	list_for_each_entry(sdata, &local->interfaces, list) {
280734a3740dSJohannes Berg 		/* it might be waiting for the local->mtx, but then
280834a3740dSJohannes Berg 		 * by the time it gets it, sdata->wdev.cac_started
280934a3740dSJohannes Berg 		 * will no longer be true
281034a3740dSJohannes Berg 		 */
281134a3740dSJohannes Berg 		cancel_delayed_work(&sdata->dfs_cac_timer_work);
2812164eb02dSSimon Wunderlich 
2813164eb02dSSimon Wunderlich 		if (sdata->wdev.cac_started) {
2814d2859df5SJanusz Dziedzic 			chandef = sdata->vif.bss_conf.chandef;
2815164eb02dSSimon Wunderlich 			ieee80211_vif_release_channel(sdata);
2816164eb02dSSimon Wunderlich 			cfg80211_cac_event(sdata->dev,
2817d2859df5SJanusz Dziedzic 					   &chandef,
2818164eb02dSSimon Wunderlich 					   NL80211_RADAR_CAC_ABORTED,
2819164eb02dSSimon Wunderlich 					   GFP_KERNEL);
2820164eb02dSSimon Wunderlich 		}
2821164eb02dSSimon Wunderlich 	}
282234a3740dSJohannes Berg 	mutex_unlock(&local->mtx);
2823164eb02dSSimon Wunderlich }
2824164eb02dSSimon Wunderlich 
2825164eb02dSSimon Wunderlich void ieee80211_dfs_radar_detected_work(struct work_struct *work)
2826164eb02dSSimon Wunderlich {
2827164eb02dSSimon Wunderlich 	struct ieee80211_local *local =
2828164eb02dSSimon Wunderlich 		container_of(work, struct ieee80211_local, radar_detected_work);
282984a3d1c9SJanusz Dziedzic 	struct cfg80211_chan_def chandef = local->hw.conf.chandef;
2830486cf4c0SMichal Kazior 	struct ieee80211_chanctx *ctx;
2831486cf4c0SMichal Kazior 	int num_chanctx = 0;
2832486cf4c0SMichal Kazior 
2833486cf4c0SMichal Kazior 	mutex_lock(&local->chanctx_mtx);
2834486cf4c0SMichal Kazior 	list_for_each_entry(ctx, &local->chanctx_list, list) {
2835486cf4c0SMichal Kazior 		if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
2836486cf4c0SMichal Kazior 			continue;
2837486cf4c0SMichal Kazior 
2838486cf4c0SMichal Kazior 		num_chanctx++;
2839486cf4c0SMichal Kazior 		chandef = ctx->conf.def;
2840486cf4c0SMichal Kazior 	}
2841486cf4c0SMichal Kazior 	mutex_unlock(&local->chanctx_mtx);
2842164eb02dSSimon Wunderlich 
28434a199068SJohannes Berg 	rtnl_lock();
2844164eb02dSSimon Wunderlich 	ieee80211_dfs_cac_cancel(local);
28454a199068SJohannes Berg 	rtnl_unlock();
2846164eb02dSSimon Wunderlich 
2847486cf4c0SMichal Kazior 	if (num_chanctx > 1)
2848486cf4c0SMichal Kazior 		/* XXX: multi-channel is not supported yet */
2849164eb02dSSimon Wunderlich 		WARN_ON(1);
285084a3d1c9SJanusz Dziedzic 	else
2851164eb02dSSimon Wunderlich 		cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
2852164eb02dSSimon Wunderlich }
2853164eb02dSSimon Wunderlich 
2854164eb02dSSimon Wunderlich void ieee80211_radar_detected(struct ieee80211_hw *hw)
2855164eb02dSSimon Wunderlich {
2856164eb02dSSimon Wunderlich 	struct ieee80211_local *local = hw_to_local(hw);
2857164eb02dSSimon Wunderlich 
2858164eb02dSSimon Wunderlich 	trace_api_radar_detected(local);
2859164eb02dSSimon Wunderlich 
28604a199068SJohannes Berg 	schedule_work(&local->radar_detected_work);
2861164eb02dSSimon Wunderlich }
2862164eb02dSSimon Wunderlich EXPORT_SYMBOL(ieee80211_radar_detected);
2863e6b7cde4SSimon Wunderlich 
2864e6b7cde4SSimon Wunderlich u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
2865e6b7cde4SSimon Wunderlich {
2866e6b7cde4SSimon Wunderlich 	u32 ret;
2867e6b7cde4SSimon Wunderlich 	int tmp;
2868e6b7cde4SSimon Wunderlich 
2869e6b7cde4SSimon Wunderlich 	switch (c->width) {
2870e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_20:
2871e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_20_NOHT;
2872e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2873e6b7cde4SSimon Wunderlich 		break;
2874e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_40:
2875e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_20;
2876e6b7cde4SSimon Wunderlich 		c->center_freq1 = c->chan->center_freq;
2877e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_40MHZ |
2878e6b7cde4SSimon Wunderlich 		      IEEE80211_STA_DISABLE_VHT;
2879e6b7cde4SSimon Wunderlich 		break;
2880e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_80:
2881e6b7cde4SSimon Wunderlich 		tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
2882e6b7cde4SSimon Wunderlich 		/* n_P40 */
2883e6b7cde4SSimon Wunderlich 		tmp /= 2;
2884e6b7cde4SSimon Wunderlich 		/* freq_P40 */
2885e6b7cde4SSimon Wunderlich 		c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
2886e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_40;
2887e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_VHT;
2888e6b7cde4SSimon Wunderlich 		break;
2889e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_80P80:
2890e6b7cde4SSimon Wunderlich 		c->center_freq2 = 0;
2891e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_80;
2892e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_80P80MHZ |
2893e6b7cde4SSimon Wunderlich 		      IEEE80211_STA_DISABLE_160MHZ;
2894e6b7cde4SSimon Wunderlich 		break;
2895e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_160:
2896e6b7cde4SSimon Wunderlich 		/* n_P20 */
2897e6b7cde4SSimon Wunderlich 		tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
2898e6b7cde4SSimon Wunderlich 		/* n_P80 */
2899e6b7cde4SSimon Wunderlich 		tmp /= 4;
2900e6b7cde4SSimon Wunderlich 		c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
2901e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_80;
2902e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_80P80MHZ |
2903e6b7cde4SSimon Wunderlich 		      IEEE80211_STA_DISABLE_160MHZ;
2904e6b7cde4SSimon Wunderlich 		break;
2905e6b7cde4SSimon Wunderlich 	default:
2906e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_20_NOHT:
2907e6b7cde4SSimon Wunderlich 		WARN_ON_ONCE(1);
2908e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_20_NOHT;
2909e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2910e6b7cde4SSimon Wunderlich 		break;
2911e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_5:
2912e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_10:
2913e6b7cde4SSimon Wunderlich 		WARN_ON_ONCE(1);
2914e6b7cde4SSimon Wunderlich 		/* keep c->width */
2915e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2916e6b7cde4SSimon Wunderlich 		break;
2917e6b7cde4SSimon Wunderlich 	}
2918e6b7cde4SSimon Wunderlich 
2919e6b7cde4SSimon Wunderlich 	WARN_ON_ONCE(!cfg80211_chandef_valid(c));
2920e6b7cde4SSimon Wunderlich 
2921e6b7cde4SSimon Wunderlich 	return ret;
2922e6b7cde4SSimon Wunderlich }
2923687da132SEmmanuel Grumbach 
2924687da132SEmmanuel Grumbach /*
2925687da132SEmmanuel Grumbach  * Returns true if smps_mode_new is strictly more restrictive than
2926687da132SEmmanuel Grumbach  * smps_mode_old.
2927687da132SEmmanuel Grumbach  */
2928687da132SEmmanuel Grumbach bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
2929687da132SEmmanuel Grumbach 				   enum ieee80211_smps_mode smps_mode_new)
2930687da132SEmmanuel Grumbach {
2931687da132SEmmanuel Grumbach 	if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
2932687da132SEmmanuel Grumbach 			 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
2933687da132SEmmanuel Grumbach 		return false;
2934687da132SEmmanuel Grumbach 
2935687da132SEmmanuel Grumbach 	switch (smps_mode_old) {
2936687da132SEmmanuel Grumbach 	case IEEE80211_SMPS_STATIC:
2937687da132SEmmanuel Grumbach 		return false;
2938687da132SEmmanuel Grumbach 	case IEEE80211_SMPS_DYNAMIC:
2939687da132SEmmanuel Grumbach 		return smps_mode_new == IEEE80211_SMPS_STATIC;
2940687da132SEmmanuel Grumbach 	case IEEE80211_SMPS_OFF:
2941687da132SEmmanuel Grumbach 		return smps_mode_new != IEEE80211_SMPS_OFF;
2942687da132SEmmanuel Grumbach 	default:
2943687da132SEmmanuel Grumbach 		WARN_ON(1);
2944687da132SEmmanuel Grumbach 	}
2945687da132SEmmanuel Grumbach 
2946687da132SEmmanuel Grumbach 	return false;
2947687da132SEmmanuel Grumbach }
2948c6da674aSChun-Yeow Yeoh 
2949c6da674aSChun-Yeow Yeoh int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
2950c6da674aSChun-Yeow Yeoh 			      struct cfg80211_csa_settings *csa_settings)
2951c6da674aSChun-Yeow Yeoh {
2952c6da674aSChun-Yeow Yeoh 	struct sk_buff *skb;
2953c6da674aSChun-Yeow Yeoh 	struct ieee80211_mgmt *mgmt;
2954c6da674aSChun-Yeow Yeoh 	struct ieee80211_local *local = sdata->local;
2955c6da674aSChun-Yeow Yeoh 	int freq;
2956c6da674aSChun-Yeow Yeoh 	int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.chan_switch) +
2957c6da674aSChun-Yeow Yeoh 			       sizeof(mgmt->u.action.u.chan_switch);
2958c6da674aSChun-Yeow Yeoh 	u8 *pos;
2959c6da674aSChun-Yeow Yeoh 
2960c6da674aSChun-Yeow Yeoh 	if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2961c6da674aSChun-Yeow Yeoh 	    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
2962c6da674aSChun-Yeow Yeoh 		return -EOPNOTSUPP;
2963c6da674aSChun-Yeow Yeoh 
2964c6da674aSChun-Yeow Yeoh 	skb = dev_alloc_skb(local->tx_headroom + hdr_len +
2965c6da674aSChun-Yeow Yeoh 			    5 + /* channel switch announcement element */
2966c6da674aSChun-Yeow Yeoh 			    3 + /* secondary channel offset element */
2967c6da674aSChun-Yeow Yeoh 			    8); /* mesh channel switch parameters element */
2968c6da674aSChun-Yeow Yeoh 	if (!skb)
2969c6da674aSChun-Yeow Yeoh 		return -ENOMEM;
2970c6da674aSChun-Yeow Yeoh 
2971c6da674aSChun-Yeow Yeoh 	skb_reserve(skb, local->tx_headroom);
2972c6da674aSChun-Yeow Yeoh 	mgmt = (struct ieee80211_mgmt *)skb_put(skb, hdr_len);
2973c6da674aSChun-Yeow Yeoh 	memset(mgmt, 0, hdr_len);
2974c6da674aSChun-Yeow Yeoh 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2975c6da674aSChun-Yeow Yeoh 					  IEEE80211_STYPE_ACTION);
2976c6da674aSChun-Yeow Yeoh 
2977c6da674aSChun-Yeow Yeoh 	eth_broadcast_addr(mgmt->da);
2978c6da674aSChun-Yeow Yeoh 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
2979c6da674aSChun-Yeow Yeoh 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2980c6da674aSChun-Yeow Yeoh 		memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
2981c6da674aSChun-Yeow Yeoh 	} else {
2982c6da674aSChun-Yeow Yeoh 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2983c6da674aSChun-Yeow Yeoh 		memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
2984c6da674aSChun-Yeow Yeoh 	}
2985c6da674aSChun-Yeow Yeoh 	mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
2986c6da674aSChun-Yeow Yeoh 	mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
2987c6da674aSChun-Yeow Yeoh 	pos = skb_put(skb, 5);
2988c6da674aSChun-Yeow Yeoh 	*pos++ = WLAN_EID_CHANNEL_SWITCH;			/* EID */
2989c6da674aSChun-Yeow Yeoh 	*pos++ = 3;						/* IE length */
2990c6da674aSChun-Yeow Yeoh 	*pos++ = csa_settings->block_tx ? 1 : 0;		/* CSA mode */
2991c6da674aSChun-Yeow Yeoh 	freq = csa_settings->chandef.chan->center_freq;
2992c6da674aSChun-Yeow Yeoh 	*pos++ = ieee80211_frequency_to_channel(freq);		/* channel */
2993c6da674aSChun-Yeow Yeoh 	*pos++ = csa_settings->count;				/* count */
2994c6da674aSChun-Yeow Yeoh 
2995c6da674aSChun-Yeow Yeoh 	if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
2996c6da674aSChun-Yeow Yeoh 		enum nl80211_channel_type ch_type;
2997c6da674aSChun-Yeow Yeoh 
2998c6da674aSChun-Yeow Yeoh 		skb_put(skb, 3);
2999c6da674aSChun-Yeow Yeoh 		*pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;	/* EID */
3000c6da674aSChun-Yeow Yeoh 		*pos++ = 1;					/* IE length */
3001c6da674aSChun-Yeow Yeoh 		ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
3002c6da674aSChun-Yeow Yeoh 		if (ch_type == NL80211_CHAN_HT40PLUS)
3003c6da674aSChun-Yeow Yeoh 			*pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
3004c6da674aSChun-Yeow Yeoh 		else
3005c6da674aSChun-Yeow Yeoh 			*pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
3006c6da674aSChun-Yeow Yeoh 	}
3007c6da674aSChun-Yeow Yeoh 
3008c6da674aSChun-Yeow Yeoh 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
3009c6da674aSChun-Yeow Yeoh 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
3010c6da674aSChun-Yeow Yeoh 
3011c6da674aSChun-Yeow Yeoh 		skb_put(skb, 8);
3012c6da674aSChun-Yeow Yeoh 		*pos++ = WLAN_EID_CHAN_SWITCH_PARAM;		/* EID */
3013c6da674aSChun-Yeow Yeoh 		*pos++ = 6;					/* IE length */
3014c6da674aSChun-Yeow Yeoh 		*pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL;	/* Mesh TTL */
3015c6da674aSChun-Yeow Yeoh 		*pos = 0x00;	/* Mesh Flag: Tx Restrict, Initiator, Reason */
3016c6da674aSChun-Yeow Yeoh 		*pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
3017c6da674aSChun-Yeow Yeoh 		*pos++ |= csa_settings->block_tx ?
3018c6da674aSChun-Yeow Yeoh 			  WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
3019c6da674aSChun-Yeow Yeoh 		put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
3020c6da674aSChun-Yeow Yeoh 		pos += 2;
3021ca91dc97SChun-Yeow Yeoh 		put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
3022c6da674aSChun-Yeow Yeoh 		pos += 2;
3023c6da674aSChun-Yeow Yeoh 	}
3024c6da674aSChun-Yeow Yeoh 
3025c6da674aSChun-Yeow Yeoh 	ieee80211_tx_skb(sdata, skb);
3026c6da674aSChun-Yeow Yeoh 	return 0;
3027c6da674aSChun-Yeow Yeoh }
30282475b1ccSMax Stepanov 
30292475b1ccSMax Stepanov bool ieee80211_cs_valid(const struct ieee80211_cipher_scheme *cs)
30302475b1ccSMax Stepanov {
30312475b1ccSMax Stepanov 	return !(cs == NULL || cs->cipher == 0 ||
30322475b1ccSMax Stepanov 		 cs->hdr_len < cs->pn_len + cs->pn_off ||
30332475b1ccSMax Stepanov 		 cs->hdr_len <= cs->key_idx_off ||
30342475b1ccSMax Stepanov 		 cs->key_idx_shift > 7 ||
30352475b1ccSMax Stepanov 		 cs->key_idx_mask == 0);
30362475b1ccSMax Stepanov }
30372475b1ccSMax Stepanov 
30382475b1ccSMax Stepanov bool ieee80211_cs_list_valid(const struct ieee80211_cipher_scheme *cs, int n)
30392475b1ccSMax Stepanov {
30402475b1ccSMax Stepanov 	int i;
30412475b1ccSMax Stepanov 
30422475b1ccSMax Stepanov 	/* Ensure we have enough iftype bitmap space for all iftype values */
30432475b1ccSMax Stepanov 	WARN_ON((NUM_NL80211_IFTYPES / 8 + 1) > sizeof(cs[0].iftype));
30442475b1ccSMax Stepanov 
30452475b1ccSMax Stepanov 	for (i = 0; i < n; i++)
30462475b1ccSMax Stepanov 		if (!ieee80211_cs_valid(&cs[i]))
30472475b1ccSMax Stepanov 			return false;
30482475b1ccSMax Stepanov 
30492475b1ccSMax Stepanov 	return true;
30502475b1ccSMax Stepanov }
30512475b1ccSMax Stepanov 
30522475b1ccSMax Stepanov const struct ieee80211_cipher_scheme *
30532475b1ccSMax Stepanov ieee80211_cs_get(struct ieee80211_local *local, u32 cipher,
30542475b1ccSMax Stepanov 		 enum nl80211_iftype iftype)
30552475b1ccSMax Stepanov {
30562475b1ccSMax Stepanov 	const struct ieee80211_cipher_scheme *l = local->hw.cipher_schemes;
30572475b1ccSMax Stepanov 	int n = local->hw.n_cipher_schemes;
30582475b1ccSMax Stepanov 	int i;
30592475b1ccSMax Stepanov 	const struct ieee80211_cipher_scheme *cs = NULL;
30602475b1ccSMax Stepanov 
30612475b1ccSMax Stepanov 	for (i = 0; i < n; i++) {
30622475b1ccSMax Stepanov 		if (l[i].cipher == cipher) {
30632475b1ccSMax Stepanov 			cs = &l[i];
30642475b1ccSMax Stepanov 			break;
30652475b1ccSMax Stepanov 		}
30662475b1ccSMax Stepanov 	}
30672475b1ccSMax Stepanov 
30682475b1ccSMax Stepanov 	if (!cs || !(cs->iftype & BIT(iftype)))
30692475b1ccSMax Stepanov 		return NULL;
30702475b1ccSMax Stepanov 
30712475b1ccSMax Stepanov 	return cs;
30722475b1ccSMax Stepanov }
30732475b1ccSMax Stepanov 
30742475b1ccSMax Stepanov int ieee80211_cs_headroom(struct ieee80211_local *local,
30752475b1ccSMax Stepanov 			  struct cfg80211_crypto_settings *crypto,
30762475b1ccSMax Stepanov 			  enum nl80211_iftype iftype)
30772475b1ccSMax Stepanov {
30782475b1ccSMax Stepanov 	const struct ieee80211_cipher_scheme *cs;
30792475b1ccSMax Stepanov 	int headroom = IEEE80211_ENCRYPT_HEADROOM;
30802475b1ccSMax Stepanov 	int i;
30812475b1ccSMax Stepanov 
30822475b1ccSMax Stepanov 	for (i = 0; i < crypto->n_ciphers_pairwise; i++) {
30832475b1ccSMax Stepanov 		cs = ieee80211_cs_get(local, crypto->ciphers_pairwise[i],
30842475b1ccSMax Stepanov 				      iftype);
30852475b1ccSMax Stepanov 
30862475b1ccSMax Stepanov 		if (cs && headroom < cs->hdr_len)
30872475b1ccSMax Stepanov 			headroom = cs->hdr_len;
30882475b1ccSMax Stepanov 	}
30892475b1ccSMax Stepanov 
30902475b1ccSMax Stepanov 	cs = ieee80211_cs_get(local, crypto->cipher_group, iftype);
30912475b1ccSMax Stepanov 	if (cs && headroom < cs->hdr_len)
30922475b1ccSMax Stepanov 		headroom = cs->hdr_len;
30932475b1ccSMax Stepanov 
30942475b1ccSMax Stepanov 	return headroom;
30952475b1ccSMax Stepanov }
3096a7022e65SFelix Fietkau 
3097a7022e65SFelix Fietkau static bool
3098a7022e65SFelix Fietkau ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
3099a7022e65SFelix Fietkau {
3100a7022e65SFelix Fietkau 	s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
3101a7022e65SFelix Fietkau 	int skip;
3102a7022e65SFelix Fietkau 
3103a7022e65SFelix Fietkau 	if (end > 0)
3104a7022e65SFelix Fietkau 		return false;
3105a7022e65SFelix Fietkau 
3106519ee691SJanusz.Dziedzic@tieto.com 	/* One shot NOA  */
3107519ee691SJanusz.Dziedzic@tieto.com 	if (data->count[i] == 1)
3108519ee691SJanusz.Dziedzic@tieto.com 		return false;
3109519ee691SJanusz.Dziedzic@tieto.com 
3110519ee691SJanusz.Dziedzic@tieto.com 	if (data->desc[i].interval == 0)
3111519ee691SJanusz.Dziedzic@tieto.com 		return false;
3112519ee691SJanusz.Dziedzic@tieto.com 
3113a7022e65SFelix Fietkau 	/* End time is in the past, check for repetitions */
3114a7022e65SFelix Fietkau 	skip = DIV_ROUND_UP(-end, data->desc[i].interval);
3115a7022e65SFelix Fietkau 	if (data->count[i] < 255) {
3116a7022e65SFelix Fietkau 		if (data->count[i] <= skip) {
3117a7022e65SFelix Fietkau 			data->count[i] = 0;
3118a7022e65SFelix Fietkau 			return false;
3119a7022e65SFelix Fietkau 		}
3120a7022e65SFelix Fietkau 
3121a7022e65SFelix Fietkau 		data->count[i] -= skip;
3122a7022e65SFelix Fietkau 	}
3123a7022e65SFelix Fietkau 
3124a7022e65SFelix Fietkau 	data->desc[i].start += skip * data->desc[i].interval;
3125a7022e65SFelix Fietkau 
3126a7022e65SFelix Fietkau 	return true;
3127a7022e65SFelix Fietkau }
3128a7022e65SFelix Fietkau 
3129a7022e65SFelix Fietkau static bool
3130a7022e65SFelix Fietkau ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
3131a7022e65SFelix Fietkau 			     s32 *offset)
3132a7022e65SFelix Fietkau {
3133a7022e65SFelix Fietkau 	bool ret = false;
3134a7022e65SFelix Fietkau 	int i;
3135a7022e65SFelix Fietkau 
3136a7022e65SFelix Fietkau 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3137a7022e65SFelix Fietkau 		s32 cur;
3138a7022e65SFelix Fietkau 
3139a7022e65SFelix Fietkau 		if (!data->count[i])
3140a7022e65SFelix Fietkau 			continue;
3141a7022e65SFelix Fietkau 
3142a7022e65SFelix Fietkau 		if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
3143a7022e65SFelix Fietkau 			ret = true;
3144a7022e65SFelix Fietkau 
3145a7022e65SFelix Fietkau 		cur = data->desc[i].start - tsf;
3146a7022e65SFelix Fietkau 		if (cur > *offset)
3147a7022e65SFelix Fietkau 			continue;
3148a7022e65SFelix Fietkau 
3149a7022e65SFelix Fietkau 		cur = data->desc[i].start + data->desc[i].duration - tsf;
3150a7022e65SFelix Fietkau 		if (cur > *offset)
3151a7022e65SFelix Fietkau 			*offset = cur;
3152a7022e65SFelix Fietkau 	}
3153a7022e65SFelix Fietkau 
3154a7022e65SFelix Fietkau 	return ret;
3155a7022e65SFelix Fietkau }
3156a7022e65SFelix Fietkau 
3157a7022e65SFelix Fietkau static u32
3158a7022e65SFelix Fietkau ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
3159a7022e65SFelix Fietkau {
3160a7022e65SFelix Fietkau 	s32 offset = 0;
3161a7022e65SFelix Fietkau 	int tries = 0;
3162a7022e65SFelix Fietkau 	/*
3163a7022e65SFelix Fietkau 	 * arbitrary limit, used to avoid infinite loops when combined NoA
3164a7022e65SFelix Fietkau 	 * descriptors cover the full time period.
3165a7022e65SFelix Fietkau 	 */
3166a7022e65SFelix Fietkau 	int max_tries = 5;
3167a7022e65SFelix Fietkau 
3168a7022e65SFelix Fietkau 	ieee80211_extend_absent_time(data, tsf, &offset);
3169a7022e65SFelix Fietkau 	do {
3170a7022e65SFelix Fietkau 		if (!ieee80211_extend_absent_time(data, tsf, &offset))
3171a7022e65SFelix Fietkau 			break;
3172a7022e65SFelix Fietkau 
3173a7022e65SFelix Fietkau 		tries++;
3174a7022e65SFelix Fietkau 	} while (tries < max_tries);
3175a7022e65SFelix Fietkau 
3176a7022e65SFelix Fietkau 	return offset;
3177a7022e65SFelix Fietkau }
3178a7022e65SFelix Fietkau 
3179a7022e65SFelix Fietkau void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
3180a7022e65SFelix Fietkau {
3181a7022e65SFelix Fietkau 	u32 next_offset = BIT(31) - 1;
3182a7022e65SFelix Fietkau 	int i;
3183a7022e65SFelix Fietkau 
3184a7022e65SFelix Fietkau 	data->absent = 0;
3185a7022e65SFelix Fietkau 	data->has_next_tsf = false;
3186a7022e65SFelix Fietkau 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3187a7022e65SFelix Fietkau 		s32 start;
3188a7022e65SFelix Fietkau 
3189a7022e65SFelix Fietkau 		if (!data->count[i])
3190a7022e65SFelix Fietkau 			continue;
3191a7022e65SFelix Fietkau 
3192a7022e65SFelix Fietkau 		ieee80211_extend_noa_desc(data, tsf, i);
3193a7022e65SFelix Fietkau 		start = data->desc[i].start - tsf;
3194a7022e65SFelix Fietkau 		if (start <= 0)
3195a7022e65SFelix Fietkau 			data->absent |= BIT(i);
3196a7022e65SFelix Fietkau 
3197a7022e65SFelix Fietkau 		if (next_offset > start)
3198a7022e65SFelix Fietkau 			next_offset = start;
3199a7022e65SFelix Fietkau 
3200a7022e65SFelix Fietkau 		data->has_next_tsf = true;
3201a7022e65SFelix Fietkau 	}
3202a7022e65SFelix Fietkau 
3203a7022e65SFelix Fietkau 	if (data->absent)
3204a7022e65SFelix Fietkau 		next_offset = ieee80211_get_noa_absent_time(data, tsf);
3205a7022e65SFelix Fietkau 
3206a7022e65SFelix Fietkau 	data->next_tsf = tsf + next_offset;
3207a7022e65SFelix Fietkau }
3208a7022e65SFelix Fietkau EXPORT_SYMBOL(ieee80211_update_p2p_noa);
3209a7022e65SFelix Fietkau 
3210a7022e65SFelix Fietkau int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
3211a7022e65SFelix Fietkau 			    struct ieee80211_noa_data *data, u32 tsf)
3212a7022e65SFelix Fietkau {
3213a7022e65SFelix Fietkau 	int ret = 0;
3214a7022e65SFelix Fietkau 	int i;
3215a7022e65SFelix Fietkau 
3216a7022e65SFelix Fietkau 	memset(data, 0, sizeof(*data));
3217a7022e65SFelix Fietkau 
3218a7022e65SFelix Fietkau 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3219a7022e65SFelix Fietkau 		const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
3220a7022e65SFelix Fietkau 
3221a7022e65SFelix Fietkau 		if (!desc->count || !desc->duration)
3222a7022e65SFelix Fietkau 			continue;
3223a7022e65SFelix Fietkau 
3224a7022e65SFelix Fietkau 		data->count[i] = desc->count;
3225a7022e65SFelix Fietkau 		data->desc[i].start = le32_to_cpu(desc->start_time);
3226a7022e65SFelix Fietkau 		data->desc[i].duration = le32_to_cpu(desc->duration);
3227a7022e65SFelix Fietkau 		data->desc[i].interval = le32_to_cpu(desc->interval);
3228a7022e65SFelix Fietkau 
3229a7022e65SFelix Fietkau 		if (data->count[i] > 1 &&
3230a7022e65SFelix Fietkau 		    data->desc[i].interval < data->desc[i].duration)
3231a7022e65SFelix Fietkau 			continue;
3232a7022e65SFelix Fietkau 
3233a7022e65SFelix Fietkau 		ieee80211_extend_noa_desc(data, tsf, i);
3234a7022e65SFelix Fietkau 		ret++;
3235a7022e65SFelix Fietkau 	}
3236a7022e65SFelix Fietkau 
3237a7022e65SFelix Fietkau 	if (ret)
3238a7022e65SFelix Fietkau 		ieee80211_update_p2p_noa(data, tsf);
3239a7022e65SFelix Fietkau 
3240a7022e65SFelix Fietkau 	return ret;
3241a7022e65SFelix Fietkau }
3242a7022e65SFelix Fietkau EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
3243057d5f4bSThomas Pedersen 
3244057d5f4bSThomas Pedersen void ieee80211_recalc_dtim(struct ieee80211_local *local,
3245057d5f4bSThomas Pedersen 			   struct ieee80211_sub_if_data *sdata)
3246057d5f4bSThomas Pedersen {
3247057d5f4bSThomas Pedersen 	u64 tsf = drv_get_tsf(local, sdata);
3248057d5f4bSThomas Pedersen 	u64 dtim_count = 0;
3249057d5f4bSThomas Pedersen 	u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
3250057d5f4bSThomas Pedersen 	u8 dtim_period = sdata->vif.bss_conf.dtim_period;
3251057d5f4bSThomas Pedersen 	struct ps_data *ps;
3252057d5f4bSThomas Pedersen 	u8 bcns_from_dtim;
3253057d5f4bSThomas Pedersen 
3254057d5f4bSThomas Pedersen 	if (tsf == -1ULL || !beacon_int || !dtim_period)
3255057d5f4bSThomas Pedersen 		return;
3256057d5f4bSThomas Pedersen 
3257057d5f4bSThomas Pedersen 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
3258057d5f4bSThomas Pedersen 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
3259057d5f4bSThomas Pedersen 		if (!sdata->bss)
3260057d5f4bSThomas Pedersen 			return;
3261057d5f4bSThomas Pedersen 
3262057d5f4bSThomas Pedersen 		ps = &sdata->bss->ps;
3263057d5f4bSThomas Pedersen 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
3264057d5f4bSThomas Pedersen 		ps = &sdata->u.mesh.ps;
3265057d5f4bSThomas Pedersen 	} else {
3266057d5f4bSThomas Pedersen 		return;
3267057d5f4bSThomas Pedersen 	}
3268057d5f4bSThomas Pedersen 
3269057d5f4bSThomas Pedersen 	/*
3270057d5f4bSThomas Pedersen 	 * actually finds last dtim_count, mac80211 will update in
3271057d5f4bSThomas Pedersen 	 * __beacon_add_tim().
3272057d5f4bSThomas Pedersen 	 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
3273057d5f4bSThomas Pedersen 	 */
3274057d5f4bSThomas Pedersen 	do_div(tsf, beacon_int);
3275057d5f4bSThomas Pedersen 	bcns_from_dtim = do_div(tsf, dtim_period);
3276057d5f4bSThomas Pedersen 	/* just had a DTIM */
3277057d5f4bSThomas Pedersen 	if (!bcns_from_dtim)
3278057d5f4bSThomas Pedersen 		dtim_count = 0;
3279057d5f4bSThomas Pedersen 	else
3280057d5f4bSThomas Pedersen 		dtim_count = dtim_period - bcns_from_dtim;
3281057d5f4bSThomas Pedersen 
3282057d5f4bSThomas Pedersen 	ps->dtim_count = dtim_count;
3283057d5f4bSThomas Pedersen }
328473de86a3SLuciano Coelho 
328571e6195eSMichal Kazior static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
328671e6195eSMichal Kazior 					 struct ieee80211_chanctx *ctx)
328771e6195eSMichal Kazior {
328871e6195eSMichal Kazior 	struct ieee80211_sub_if_data *sdata;
328971e6195eSMichal Kazior 	u8 radar_detect = 0;
329071e6195eSMichal Kazior 
329171e6195eSMichal Kazior 	lockdep_assert_held(&local->chanctx_mtx);
329271e6195eSMichal Kazior 
329371e6195eSMichal Kazior 	if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
329471e6195eSMichal Kazior 		return 0;
329571e6195eSMichal Kazior 
329671e6195eSMichal Kazior 	list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
329771e6195eSMichal Kazior 		if (sdata->reserved_radar_required)
329871e6195eSMichal Kazior 			radar_detect |= BIT(sdata->reserved_chandef.width);
329971e6195eSMichal Kazior 
330071e6195eSMichal Kazior 	/*
330171e6195eSMichal Kazior 	 * An in-place reservation context should not have any assigned vifs
330271e6195eSMichal Kazior 	 * until it replaces the other context.
330371e6195eSMichal Kazior 	 */
330471e6195eSMichal Kazior 	WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
330571e6195eSMichal Kazior 		!list_empty(&ctx->assigned_vifs));
330671e6195eSMichal Kazior 
330771e6195eSMichal Kazior 	list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
330871e6195eSMichal Kazior 		if (sdata->radar_required)
330971e6195eSMichal Kazior 			radar_detect |= BIT(sdata->vif.bss_conf.chandef.width);
331071e6195eSMichal Kazior 
331171e6195eSMichal Kazior 	return radar_detect;
331271e6195eSMichal Kazior }
331371e6195eSMichal Kazior 
331473de86a3SLuciano Coelho int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
331573de86a3SLuciano Coelho 				 const struct cfg80211_chan_def *chandef,
331673de86a3SLuciano Coelho 				 enum ieee80211_chanctx_mode chanmode,
331773de86a3SLuciano Coelho 				 u8 radar_detect)
331873de86a3SLuciano Coelho {
331973de86a3SLuciano Coelho 	struct ieee80211_local *local = sdata->local;
332073de86a3SLuciano Coelho 	struct ieee80211_sub_if_data *sdata_iter;
332173de86a3SLuciano Coelho 	enum nl80211_iftype iftype = sdata->wdev.iftype;
332273de86a3SLuciano Coelho 	struct ieee80211_chanctx *ctx;
332373de86a3SLuciano Coelho 	int total = 1;
3324e227300cSPurushottam Kushwaha 	struct iface_combination_params params = {
3325e227300cSPurushottam Kushwaha 		.radar_detect = radar_detect,
3326e227300cSPurushottam Kushwaha 	};
332773de86a3SLuciano Coelho 
332873de86a3SLuciano Coelho 	lockdep_assert_held(&local->chanctx_mtx);
332973de86a3SLuciano Coelho 
333073de86a3SLuciano Coelho 	if (WARN_ON(hweight32(radar_detect) > 1))
333173de86a3SLuciano Coelho 		return -EINVAL;
333273de86a3SLuciano Coelho 
3333b6a55015SLuciano Coelho 	if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
3334b6a55015SLuciano Coelho 		    !chandef->chan))
333573de86a3SLuciano Coelho 		return -EINVAL;
333673de86a3SLuciano Coelho 
333773de86a3SLuciano Coelho 	if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
333873de86a3SLuciano Coelho 		return -EINVAL;
333973de86a3SLuciano Coelho 
3340ac668afeSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
3341ac668afeSJohannes Berg 	    sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
3342ac668afeSJohannes Berg 		/*
3343ac668afeSJohannes Berg 		 * always passing this is harmless, since it'll be the
3344ac668afeSJohannes Berg 		 * same value that cfg80211 finds if it finds the same
3345ac668afeSJohannes Berg 		 * interface ... and that's always allowed
3346ac668afeSJohannes Berg 		 */
3347ac668afeSJohannes Berg 		params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
3348ac668afeSJohannes Berg 	}
3349ac668afeSJohannes Berg 
335073de86a3SLuciano Coelho 	/* Always allow software iftypes */
335173de86a3SLuciano Coelho 	if (local->hw.wiphy->software_iftypes & BIT(iftype)) {
335273de86a3SLuciano Coelho 		if (radar_detect)
335373de86a3SLuciano Coelho 			return -EINVAL;
335473de86a3SLuciano Coelho 		return 0;
335573de86a3SLuciano Coelho 	}
335673de86a3SLuciano Coelho 
3357e227300cSPurushottam Kushwaha 	if (chandef)
3358e227300cSPurushottam Kushwaha 		params.num_different_channels = 1;
335973de86a3SLuciano Coelho 
336073de86a3SLuciano Coelho 	if (iftype != NL80211_IFTYPE_UNSPECIFIED)
3361e227300cSPurushottam Kushwaha 		params.iftype_num[iftype] = 1;
336273de86a3SLuciano Coelho 
336373de86a3SLuciano Coelho 	list_for_each_entry(ctx, &local->chanctx_list, list) {
33645bcae31dSMichal Kazior 		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
33655bcae31dSMichal Kazior 			continue;
3366e227300cSPurushottam Kushwaha 		params.radar_detect |=
3367e227300cSPurushottam Kushwaha 			ieee80211_chanctx_radar_detect(local, ctx);
336873de86a3SLuciano Coelho 		if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
3369e227300cSPurushottam Kushwaha 			params.num_different_channels++;
337073de86a3SLuciano Coelho 			continue;
337173de86a3SLuciano Coelho 		}
3372b6a55015SLuciano Coelho 		if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
337373de86a3SLuciano Coelho 		    cfg80211_chandef_compatible(chandef,
337473de86a3SLuciano Coelho 						&ctx->conf.def))
337573de86a3SLuciano Coelho 			continue;
3376e227300cSPurushottam Kushwaha 		params.num_different_channels++;
337773de86a3SLuciano Coelho 	}
337873de86a3SLuciano Coelho 
337973de86a3SLuciano Coelho 	list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
338073de86a3SLuciano Coelho 		struct wireless_dev *wdev_iter;
338173de86a3SLuciano Coelho 
338273de86a3SLuciano Coelho 		wdev_iter = &sdata_iter->wdev;
338373de86a3SLuciano Coelho 
338473de86a3SLuciano Coelho 		if (sdata_iter == sdata ||
33850f611d28SAndrei Otcheretianski 		    !ieee80211_sdata_running(sdata_iter) ||
338673de86a3SLuciano Coelho 		    local->hw.wiphy->software_iftypes & BIT(wdev_iter->iftype))
338773de86a3SLuciano Coelho 			continue;
338873de86a3SLuciano Coelho 
3389e227300cSPurushottam Kushwaha 		params.iftype_num[wdev_iter->iftype]++;
339073de86a3SLuciano Coelho 		total++;
339173de86a3SLuciano Coelho 	}
339273de86a3SLuciano Coelho 
3393e227300cSPurushottam Kushwaha 	if (total == 1 && !params.radar_detect)
339473de86a3SLuciano Coelho 		return 0;
339573de86a3SLuciano Coelho 
3396e227300cSPurushottam Kushwaha 	return cfg80211_check_combinations(local->hw.wiphy, &params);
339773de86a3SLuciano Coelho }
33986fa001bcSMichal Kazior 
33996fa001bcSMichal Kazior static void
34006fa001bcSMichal Kazior ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
34016fa001bcSMichal Kazior 			 void *data)
34026fa001bcSMichal Kazior {
34036fa001bcSMichal Kazior 	u32 *max_num_different_channels = data;
34046fa001bcSMichal Kazior 
34056fa001bcSMichal Kazior 	*max_num_different_channels = max(*max_num_different_channels,
34066fa001bcSMichal Kazior 					  c->num_different_channels);
34076fa001bcSMichal Kazior }
34086fa001bcSMichal Kazior 
34096fa001bcSMichal Kazior int ieee80211_max_num_channels(struct ieee80211_local *local)
34106fa001bcSMichal Kazior {
34116fa001bcSMichal Kazior 	struct ieee80211_sub_if_data *sdata;
34126fa001bcSMichal Kazior 	struct ieee80211_chanctx *ctx;
34136fa001bcSMichal Kazior 	u32 max_num_different_channels = 1;
34146fa001bcSMichal Kazior 	int err;
3415e227300cSPurushottam Kushwaha 	struct iface_combination_params params = {0};
34166fa001bcSMichal Kazior 
34176fa001bcSMichal Kazior 	lockdep_assert_held(&local->chanctx_mtx);
34186fa001bcSMichal Kazior 
34196fa001bcSMichal Kazior 	list_for_each_entry(ctx, &local->chanctx_list, list) {
34205bcae31dSMichal Kazior 		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
34215bcae31dSMichal Kazior 			continue;
34225bcae31dSMichal Kazior 
3423e227300cSPurushottam Kushwaha 		params.num_different_channels++;
34246fa001bcSMichal Kazior 
3425e227300cSPurushottam Kushwaha 		params.radar_detect |=
3426e227300cSPurushottam Kushwaha 			ieee80211_chanctx_radar_detect(local, ctx);
34276fa001bcSMichal Kazior 	}
34286fa001bcSMichal Kazior 
34296fa001bcSMichal Kazior 	list_for_each_entry_rcu(sdata, &local->interfaces, list)
3430e227300cSPurushottam Kushwaha 		params.iftype_num[sdata->wdev.iftype]++;
34316fa001bcSMichal Kazior 
3432e227300cSPurushottam Kushwaha 	err = cfg80211_iter_combinations(local->hw.wiphy, &params,
3433e227300cSPurushottam Kushwaha 					 ieee80211_iter_max_chans,
34346fa001bcSMichal Kazior 					 &max_num_different_channels);
34356fa001bcSMichal Kazior 	if (err < 0)
34366fa001bcSMichal Kazior 		return err;
34376fa001bcSMichal Kazior 
34386fa001bcSMichal Kazior 	return max_num_different_channels;
34396fa001bcSMichal Kazior }
344040b861a0SArik Nemtsov 
344140b861a0SArik Nemtsov u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
344240b861a0SArik Nemtsov {
344340b861a0SArik Nemtsov 	*buf++ = WLAN_EID_VENDOR_SPECIFIC;
344440b861a0SArik Nemtsov 	*buf++ = 7; /* len */
344540b861a0SArik Nemtsov 	*buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
344640b861a0SArik Nemtsov 	*buf++ = 0x50;
344740b861a0SArik Nemtsov 	*buf++ = 0xf2;
344840b861a0SArik Nemtsov 	*buf++ = 2; /* WME */
344940b861a0SArik Nemtsov 	*buf++ = 0; /* WME info */
345040b861a0SArik Nemtsov 	*buf++ = 1; /* WME ver */
345140b861a0SArik Nemtsov 	*buf++ = qosinfo; /* U-APSD no in use */
345240b861a0SArik Nemtsov 
345340b861a0SArik Nemtsov 	return buf;
345440b861a0SArik Nemtsov }
3455ba8c3d6fSFelix Fietkau 
3456f2ac7e30SMichal Kazior void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
3457f2ac7e30SMichal Kazior 			     unsigned long *frame_cnt,
3458f2ac7e30SMichal Kazior 			     unsigned long *byte_cnt)
3459f2ac7e30SMichal Kazior {
3460f2ac7e30SMichal Kazior 	struct txq_info *txqi = to_txq_info(txq);
3461bb42f2d1SToke Høiland-Jørgensen 	u32 frag_cnt = 0, frag_bytes = 0;
3462bb42f2d1SToke Høiland-Jørgensen 	struct sk_buff *skb;
3463bb42f2d1SToke Høiland-Jørgensen 
3464bb42f2d1SToke Høiland-Jørgensen 	skb_queue_walk(&txqi->frags, skb) {
3465bb42f2d1SToke Høiland-Jørgensen 		frag_cnt++;
3466bb42f2d1SToke Høiland-Jørgensen 		frag_bytes += skb->len;
3467bb42f2d1SToke Høiland-Jørgensen 	}
3468f2ac7e30SMichal Kazior 
3469f2ac7e30SMichal Kazior 	if (frame_cnt)
3470bb42f2d1SToke Høiland-Jørgensen 		*frame_cnt = txqi->tin.backlog_packets + frag_cnt;
3471f2ac7e30SMichal Kazior 
3472f2ac7e30SMichal Kazior 	if (byte_cnt)
3473bb42f2d1SToke Høiland-Jørgensen 		*byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
3474f2ac7e30SMichal Kazior }
3475f2ac7e30SMichal Kazior EXPORT_SYMBOL(ieee80211_txq_get_depth);
3476f438ceb8SEmmanuel Grumbach 
3477f438ceb8SEmmanuel Grumbach const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
3478f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
3479f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
3480f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
3481f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
3482f438ceb8SEmmanuel Grumbach };
3483