xref: /openbmc/linux/net/mac80211/util.c (revision a6cf28e0)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c2d1560aSJohannes Berg /*
3c2d1560aSJohannes Berg  * Copyright 2002-2005, Instant802 Networks, Inc.
4c2d1560aSJohannes Berg  * Copyright 2005-2006, Devicescape Software, Inc.
5c2d1560aSJohannes Berg  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
6c2d1560aSJohannes Berg  * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
7d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
8da6a4352SJohannes Berg  * Copyright (C) 2015-2017	Intel Deutschland GmbH
9c0058df7SShaul Triebitz  * Copyright (C) 2018-2020 Intel Corporation
10c2d1560aSJohannes Berg  *
11c2d1560aSJohannes Berg  * utilities for mac80211
12c2d1560aSJohannes Berg  */
13c2d1560aSJohannes Berg 
14c2d1560aSJohannes Berg #include <net/mac80211.h>
15c2d1560aSJohannes Berg #include <linux/netdevice.h>
16bc3b2d7fSPaul Gortmaker #include <linux/export.h>
17c2d1560aSJohannes Berg #include <linux/types.h>
18c2d1560aSJohannes Berg #include <linux/slab.h>
19c2d1560aSJohannes Berg #include <linux/skbuff.h>
20c2d1560aSJohannes Berg #include <linux/etherdevice.h>
21c2d1560aSJohannes Berg #include <linux/if_arp.h>
22c2d1560aSJohannes Berg #include <linux/bitmap.h>
23dd76986bSJohannes Berg #include <linux/crc32.h>
24881d966bSEric W. Biederman #include <net/net_namespace.h>
25c2d1560aSJohannes Berg #include <net/cfg80211.h>
26dabeb344SJohannes Berg #include <net/rtnetlink.h>
27c2d1560aSJohannes Berg 
28c2d1560aSJohannes Berg #include "ieee80211_i.h"
2924487981SJohannes Berg #include "driver-ops.h"
302c8dccc7SJohannes Berg #include "rate.h"
31ee385855SLuis Carlos Cobo #include "mesh.h"
32c2d1560aSJohannes Berg #include "wme.h"
33f2753ddbSJohannes Berg #include "led.h"
34fffd0934SJohannes Berg #include "wep.h"
35c2d1560aSJohannes Berg 
36c2d1560aSJohannes Berg /* privid for wiphys to determine whether they belong to us or not */
378a47cea7SJohannes Berg const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
38c2d1560aSJohannes Berg 
399a95371aSLuis R. Rodriguez struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
409a95371aSLuis R. Rodriguez {
419a95371aSLuis R. Rodriguez 	struct ieee80211_local *local;
429a95371aSLuis R. Rodriguez 
439a95371aSLuis R. Rodriguez 	local = wiphy_priv(wiphy);
449a95371aSLuis R. Rodriguez 	return &local->hw;
459a95371aSLuis R. Rodriguez }
469a95371aSLuis R. Rodriguez EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
47c2d1560aSJohannes Berg 
485cf121c3SJohannes Berg void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
49c2d1560aSJohannes Berg {
50252b86c4SJohannes Berg 	struct sk_buff *skb;
512de8e0d9SJohannes Berg 	struct ieee80211_hdr *hdr;
52c2d1560aSJohannes Berg 
53252b86c4SJohannes Berg 	skb_queue_walk(&tx->skbs, skb) {
542de8e0d9SJohannes Berg 		hdr = (struct ieee80211_hdr *) skb->data;
55c2d1560aSJohannes Berg 		hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
56252b86c4SJohannes Berg 	}
57c2d1560aSJohannes Berg }
58c2d1560aSJohannes Berg 
5957fbcce3SJohannes Berg int ieee80211_frame_duration(enum nl80211_band band, size_t len,
60438b61b7SSimon Wunderlich 			     int rate, int erp, int short_preamble,
61438b61b7SSimon Wunderlich 			     int shift)
62c2d1560aSJohannes Berg {
63c2d1560aSJohannes Berg 	int dur;
64c2d1560aSJohannes Berg 
65c2d1560aSJohannes Berg 	/* calculate duration (in microseconds, rounded up to next higher
66c2d1560aSJohannes Berg 	 * integer if it includes a fractional microsecond) to send frame of
67c2d1560aSJohannes Berg 	 * len bytes (does not include FCS) at the given rate. Duration will
68c2d1560aSJohannes Berg 	 * also include SIFS.
69c2d1560aSJohannes Berg 	 *
70c2d1560aSJohannes Berg 	 * rate is in 100 kbps, so divident is multiplied by 10 in the
71c2d1560aSJohannes Berg 	 * DIV_ROUND_UP() operations.
72438b61b7SSimon Wunderlich 	 *
73438b61b7SSimon Wunderlich 	 * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
74438b61b7SSimon Wunderlich 	 * is assumed to be 0 otherwise.
75c2d1560aSJohannes Berg 	 */
76c2d1560aSJohannes Berg 
7757fbcce3SJohannes Berg 	if (band == NL80211_BAND_5GHZ || erp) {
78c2d1560aSJohannes Berg 		/*
79c2d1560aSJohannes Berg 		 * OFDM:
80c2d1560aSJohannes Berg 		 *
81c2d1560aSJohannes Berg 		 * N_DBPS = DATARATE x 4
82c2d1560aSJohannes Berg 		 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
83c2d1560aSJohannes Berg 		 *	(16 = SIGNAL time, 6 = tail bits)
84c2d1560aSJohannes Berg 		 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
85c2d1560aSJohannes Berg 		 *
86c2d1560aSJohannes Berg 		 * T_SYM = 4 usec
87438b61b7SSimon Wunderlich 		 * 802.11a - 18.5.2: aSIFSTime = 16 usec
88c2d1560aSJohannes Berg 		 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
89c2d1560aSJohannes Berg 		 *	signal ext = 6 usec
90c2d1560aSJohannes Berg 		 */
91c2d1560aSJohannes Berg 		dur = 16; /* SIFS + signal ext */
92438b61b7SSimon Wunderlich 		dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
93438b61b7SSimon Wunderlich 		dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
94438b61b7SSimon Wunderlich 
95438b61b7SSimon Wunderlich 		/* IEEE 802.11-2012 18.3.2.4: all values above are:
96438b61b7SSimon Wunderlich 		 *  * times 4 for 5 MHz
97438b61b7SSimon Wunderlich 		 *  * times 2 for 10 MHz
98438b61b7SSimon Wunderlich 		 */
99438b61b7SSimon Wunderlich 		dur *= 1 << shift;
1002103dec1SSimon Wunderlich 
1012103dec1SSimon Wunderlich 		/* rates should already consider the channel bandwidth,
1022103dec1SSimon Wunderlich 		 * don't apply divisor again.
1032103dec1SSimon Wunderlich 		 */
1042103dec1SSimon Wunderlich 		dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
1052103dec1SSimon Wunderlich 					4 * rate); /* T_SYM x N_SYM */
106c2d1560aSJohannes Berg 	} else {
107c2d1560aSJohannes Berg 		/*
108c2d1560aSJohannes Berg 		 * 802.11b or 802.11g with 802.11b compatibility:
109c2d1560aSJohannes Berg 		 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
110c2d1560aSJohannes Berg 		 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
111c2d1560aSJohannes Berg 		 *
112c2d1560aSJohannes Berg 		 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
113c2d1560aSJohannes Berg 		 * aSIFSTime = 10 usec
114c2d1560aSJohannes Berg 		 * aPreambleLength = 144 usec or 72 usec with short preamble
115c2d1560aSJohannes Berg 		 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
116c2d1560aSJohannes Berg 		 */
117c2d1560aSJohannes Berg 		dur = 10; /* aSIFSTime = 10 usec */
118c2d1560aSJohannes Berg 		dur += short_preamble ? (72 + 24) : (144 + 48);
119c2d1560aSJohannes Berg 
120c2d1560aSJohannes Berg 		dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
121c2d1560aSJohannes Berg 	}
122c2d1560aSJohannes Berg 
123c2d1560aSJohannes Berg 	return dur;
124c2d1560aSJohannes Berg }
125c2d1560aSJohannes Berg 
126c2d1560aSJohannes Berg /* Exported duration function for driver use */
12732bfd35dSJohannes Berg __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
12832bfd35dSJohannes Berg 					struct ieee80211_vif *vif,
12957fbcce3SJohannes Berg 					enum nl80211_band band,
1308318d78aSJohannes Berg 					size_t frame_len,
1318318d78aSJohannes Berg 					struct ieee80211_rate *rate)
132c2d1560aSJohannes Berg {
13325d834e1SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
134c2d1560aSJohannes Berg 	u16 dur;
135438b61b7SSimon Wunderlich 	int erp, shift = 0;
13625d834e1SJohannes Berg 	bool short_preamble = false;
137c2d1560aSJohannes Berg 
1388318d78aSJohannes Berg 	erp = 0;
13925d834e1SJohannes Berg 	if (vif) {
14025d834e1SJohannes Berg 		sdata = vif_to_sdata(vif);
141bda3933aSJohannes Berg 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
1428318d78aSJohannes Berg 		if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
1438318d78aSJohannes Berg 			erp = rate->flags & IEEE80211_RATE_ERP_G;
144438b61b7SSimon Wunderlich 		shift = ieee80211_vif_get_shift(vif);
14525d834e1SJohannes Berg 	}
1468318d78aSJohannes Berg 
1474ee73f33SMichal Kazior 	dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
148438b61b7SSimon Wunderlich 				       short_preamble, shift);
149c2d1560aSJohannes Berg 
150c2d1560aSJohannes Berg 	return cpu_to_le16(dur);
151c2d1560aSJohannes Berg }
152c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_generic_frame_duration);
153c2d1560aSJohannes Berg 
15432bfd35dSJohannes Berg __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
15532bfd35dSJohannes Berg 			      struct ieee80211_vif *vif, size_t frame_len,
156e039fa4aSJohannes Berg 			      const struct ieee80211_tx_info *frame_txctl)
157c2d1560aSJohannes Berg {
158c2d1560aSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
159c2d1560aSJohannes Berg 	struct ieee80211_rate *rate;
16025d834e1SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
161471b3efdSJohannes Berg 	bool short_preamble;
1622103dec1SSimon Wunderlich 	int erp, shift = 0, bitrate;
163c2d1560aSJohannes Berg 	u16 dur;
1642e92e6f2SJohannes Berg 	struct ieee80211_supported_band *sband;
1652e92e6f2SJohannes Berg 
1664ee73f33SMichal Kazior 	sband = local->hw.wiphy->bands[frame_txctl->band];
167c2d1560aSJohannes Berg 
16825d834e1SJohannes Berg 	short_preamble = false;
1697e9ed188SDaniel Drake 
170e039fa4aSJohannes Berg 	rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
1718318d78aSJohannes Berg 
1728318d78aSJohannes Berg 	erp = 0;
17325d834e1SJohannes Berg 	if (vif) {
17425d834e1SJohannes Berg 		sdata = vif_to_sdata(vif);
175bda3933aSJohannes Berg 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
1768318d78aSJohannes Berg 		if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
1778318d78aSJohannes Berg 			erp = rate->flags & IEEE80211_RATE_ERP_G;
178438b61b7SSimon Wunderlich 		shift = ieee80211_vif_get_shift(vif);
17925d834e1SJohannes Berg 	}
180c2d1560aSJohannes Berg 
1812103dec1SSimon Wunderlich 	bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
1822103dec1SSimon Wunderlich 
183c2d1560aSJohannes Berg 	/* CTS duration */
1842103dec1SSimon Wunderlich 	dur = ieee80211_frame_duration(sband->band, 10, bitrate,
185438b61b7SSimon Wunderlich 				       erp, short_preamble, shift);
186c2d1560aSJohannes Berg 	/* Data frame duration */
1872103dec1SSimon Wunderlich 	dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
188438b61b7SSimon Wunderlich 					erp, short_preamble, shift);
189c2d1560aSJohannes Berg 	/* ACK duration */
1902103dec1SSimon Wunderlich 	dur += ieee80211_frame_duration(sband->band, 10, bitrate,
191438b61b7SSimon Wunderlich 					erp, short_preamble, shift);
192c2d1560aSJohannes Berg 
193c2d1560aSJohannes Berg 	return cpu_to_le16(dur);
194c2d1560aSJohannes Berg }
195c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_rts_duration);
196c2d1560aSJohannes Berg 
19732bfd35dSJohannes Berg __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
19832bfd35dSJohannes Berg 				    struct ieee80211_vif *vif,
199c2d1560aSJohannes Berg 				    size_t frame_len,
200e039fa4aSJohannes Berg 				    const struct ieee80211_tx_info *frame_txctl)
201c2d1560aSJohannes Berg {
202c2d1560aSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
203c2d1560aSJohannes Berg 	struct ieee80211_rate *rate;
20425d834e1SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
205471b3efdSJohannes Berg 	bool short_preamble;
2062103dec1SSimon Wunderlich 	int erp, shift = 0, bitrate;
207c2d1560aSJohannes Berg 	u16 dur;
2082e92e6f2SJohannes Berg 	struct ieee80211_supported_band *sband;
2092e92e6f2SJohannes Berg 
2104ee73f33SMichal Kazior 	sband = local->hw.wiphy->bands[frame_txctl->band];
211c2d1560aSJohannes Berg 
21225d834e1SJohannes Berg 	short_preamble = false;
2137e9ed188SDaniel Drake 
214e039fa4aSJohannes Berg 	rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
2158318d78aSJohannes Berg 	erp = 0;
21625d834e1SJohannes Berg 	if (vif) {
21725d834e1SJohannes Berg 		sdata = vif_to_sdata(vif);
218bda3933aSJohannes Berg 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
2198318d78aSJohannes Berg 		if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
2208318d78aSJohannes Berg 			erp = rate->flags & IEEE80211_RATE_ERP_G;
221438b61b7SSimon Wunderlich 		shift = ieee80211_vif_get_shift(vif);
22225d834e1SJohannes Berg 	}
223c2d1560aSJohannes Berg 
2242103dec1SSimon Wunderlich 	bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
2252103dec1SSimon Wunderlich 
226c2d1560aSJohannes Berg 	/* Data frame duration */
2272103dec1SSimon Wunderlich 	dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
228438b61b7SSimon Wunderlich 				       erp, short_preamble, shift);
229e039fa4aSJohannes Berg 	if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
230c2d1560aSJohannes Berg 		/* ACK duration */
2312103dec1SSimon Wunderlich 		dur += ieee80211_frame_duration(sband->band, 10, bitrate,
232438b61b7SSimon Wunderlich 						erp, short_preamble, shift);
233c2d1560aSJohannes Berg 	}
234c2d1560aSJohannes Berg 
235c2d1560aSJohannes Berg 	return cpu_to_le16(dur);
236c2d1560aSJohannes Berg }
237c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_ctstoself_duration);
238c2d1560aSJohannes Berg 
23921a5d4c3SManikanta Pubbisetty static void __ieee80211_wake_txqs(struct ieee80211_sub_if_data *sdata, int ac)
24021a5d4c3SManikanta Pubbisetty {
24121a5d4c3SManikanta Pubbisetty 	struct ieee80211_local *local = sdata->local;
24221a5d4c3SManikanta Pubbisetty 	struct ieee80211_vif *vif = &sdata->vif;
24321a5d4c3SManikanta Pubbisetty 	struct fq *fq = &local->fq;
24421a5d4c3SManikanta Pubbisetty 	struct ps_data *ps = NULL;
24521a5d4c3SManikanta Pubbisetty 	struct txq_info *txqi;
24621a5d4c3SManikanta Pubbisetty 	struct sta_info *sta;
24721a5d4c3SManikanta Pubbisetty 	int i;
24821a5d4c3SManikanta Pubbisetty 
249d8dec42bSJohannes Berg 	local_bh_disable();
250d8dec42bSJohannes Berg 	spin_lock(&fq->lock);
25121a5d4c3SManikanta Pubbisetty 
25221a5d4c3SManikanta Pubbisetty 	if (sdata->vif.type == NL80211_IFTYPE_AP)
25321a5d4c3SManikanta Pubbisetty 		ps = &sdata->bss->ps;
25421a5d4c3SManikanta Pubbisetty 
25521a5d4c3SManikanta Pubbisetty 	sdata->vif.txqs_stopped[ac] = false;
25621a5d4c3SManikanta Pubbisetty 
25721a5d4c3SManikanta Pubbisetty 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
25821a5d4c3SManikanta Pubbisetty 		if (sdata != sta->sdata)
25921a5d4c3SManikanta Pubbisetty 			continue;
26021a5d4c3SManikanta Pubbisetty 
26121a5d4c3SManikanta Pubbisetty 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
26221a5d4c3SManikanta Pubbisetty 			struct ieee80211_txq *txq = sta->sta.txq[i];
26321a5d4c3SManikanta Pubbisetty 
264a5ae3264SErik Stromdahl 			if (!txq)
265a5ae3264SErik Stromdahl 				continue;
266a5ae3264SErik Stromdahl 
26721a5d4c3SManikanta Pubbisetty 			txqi = to_txq_info(txq);
26821a5d4c3SManikanta Pubbisetty 
26921a5d4c3SManikanta Pubbisetty 			if (ac != txq->ac)
27021a5d4c3SManikanta Pubbisetty 				continue;
27121a5d4c3SManikanta Pubbisetty 
27221a5d4c3SManikanta Pubbisetty 			if (!test_and_clear_bit(IEEE80211_TXQ_STOP_NETIF_TX,
27321a5d4c3SManikanta Pubbisetty 						&txqi->flags))
27421a5d4c3SManikanta Pubbisetty 				continue;
27521a5d4c3SManikanta Pubbisetty 
276d8dec42bSJohannes Berg 			spin_unlock(&fq->lock);
27721a5d4c3SManikanta Pubbisetty 			drv_wake_tx_queue(local, txqi);
278d8dec42bSJohannes Berg 			spin_lock(&fq->lock);
27921a5d4c3SManikanta Pubbisetty 		}
28021a5d4c3SManikanta Pubbisetty 	}
28121a5d4c3SManikanta Pubbisetty 
28221a5d4c3SManikanta Pubbisetty 	if (!vif->txq)
28321a5d4c3SManikanta Pubbisetty 		goto out;
28421a5d4c3SManikanta Pubbisetty 
28521a5d4c3SManikanta Pubbisetty 	txqi = to_txq_info(vif->txq);
28621a5d4c3SManikanta Pubbisetty 
28721a5d4c3SManikanta Pubbisetty 	if (!test_and_clear_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags) ||
28821a5d4c3SManikanta Pubbisetty 	    (ps && atomic_read(&ps->num_sta_ps)) || ac != vif->txq->ac)
28921a5d4c3SManikanta Pubbisetty 		goto out;
29021a5d4c3SManikanta Pubbisetty 
291d8dec42bSJohannes Berg 	spin_unlock(&fq->lock);
29221a5d4c3SManikanta Pubbisetty 
29321a5d4c3SManikanta Pubbisetty 	drv_wake_tx_queue(local, txqi);
294d8dec42bSJohannes Berg 	local_bh_enable();
29521a5d4c3SManikanta Pubbisetty 	return;
29621a5d4c3SManikanta Pubbisetty out:
297d8dec42bSJohannes Berg 	spin_unlock(&fq->lock);
298d8dec42bSJohannes Berg 	local_bh_enable();
29921a5d4c3SManikanta Pubbisetty }
30021a5d4c3SManikanta Pubbisetty 
301f6c7f03fSEmmanuel Grumbach static void
302f6c7f03fSEmmanuel Grumbach __releases(&local->queue_stop_reason_lock)
303f6c7f03fSEmmanuel Grumbach __acquires(&local->queue_stop_reason_lock)
304f6c7f03fSEmmanuel Grumbach _ieee80211_wake_txqs(struct ieee80211_local *local, unsigned long *flags)
30521a5d4c3SManikanta Pubbisetty {
30621a5d4c3SManikanta Pubbisetty 	struct ieee80211_sub_if_data *sdata;
30721a5d4c3SManikanta Pubbisetty 	int n_acs = IEEE80211_NUM_ACS;
30821a5d4c3SManikanta Pubbisetty 	int i;
30921a5d4c3SManikanta Pubbisetty 
31021a5d4c3SManikanta Pubbisetty 	rcu_read_lock();
31121a5d4c3SManikanta Pubbisetty 
31221a5d4c3SManikanta Pubbisetty 	if (local->hw.queues < IEEE80211_NUM_ACS)
31321a5d4c3SManikanta Pubbisetty 		n_acs = 1;
31421a5d4c3SManikanta Pubbisetty 
31521a5d4c3SManikanta Pubbisetty 	for (i = 0; i < local->hw.queues; i++) {
31621a5d4c3SManikanta Pubbisetty 		if (local->queue_stop_reasons[i])
31721a5d4c3SManikanta Pubbisetty 			continue;
31821a5d4c3SManikanta Pubbisetty 
319f6c7f03fSEmmanuel Grumbach 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, *flags);
32021a5d4c3SManikanta Pubbisetty 		list_for_each_entry_rcu(sdata, &local->interfaces, list) {
32121a5d4c3SManikanta Pubbisetty 			int ac;
32221a5d4c3SManikanta Pubbisetty 
32321a5d4c3SManikanta Pubbisetty 			for (ac = 0; ac < n_acs; ac++) {
32421a5d4c3SManikanta Pubbisetty 				int ac_queue = sdata->vif.hw_queue[ac];
32521a5d4c3SManikanta Pubbisetty 
32621a5d4c3SManikanta Pubbisetty 				if (ac_queue == i ||
32721a5d4c3SManikanta Pubbisetty 				    sdata->vif.cab_queue == i)
32821a5d4c3SManikanta Pubbisetty 					__ieee80211_wake_txqs(sdata, ac);
32921a5d4c3SManikanta Pubbisetty 			}
33021a5d4c3SManikanta Pubbisetty 		}
331f6c7f03fSEmmanuel Grumbach 		spin_lock_irqsave(&local->queue_stop_reason_lock, *flags);
33221a5d4c3SManikanta Pubbisetty 	}
33321a5d4c3SManikanta Pubbisetty 
33421a5d4c3SManikanta Pubbisetty 	rcu_read_unlock();
33521a5d4c3SManikanta Pubbisetty }
33621a5d4c3SManikanta Pubbisetty 
337f6c7f03fSEmmanuel Grumbach void ieee80211_wake_txqs(unsigned long data)
338f6c7f03fSEmmanuel Grumbach {
339f6c7f03fSEmmanuel Grumbach 	struct ieee80211_local *local = (struct ieee80211_local *)data;
340f6c7f03fSEmmanuel Grumbach 	unsigned long flags;
341f6c7f03fSEmmanuel Grumbach 
342f6c7f03fSEmmanuel Grumbach 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
343f6c7f03fSEmmanuel Grumbach 	_ieee80211_wake_txqs(local, &flags);
344f6c7f03fSEmmanuel Grumbach 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
345f6c7f03fSEmmanuel Grumbach }
346f6c7f03fSEmmanuel Grumbach 
3473a25a8c8SJohannes Berg void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
3483a25a8c8SJohannes Berg {
3493a25a8c8SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
350a6f38ac3SJohannes Berg 	int n_acs = IEEE80211_NUM_ACS;
351a6f38ac3SJohannes Berg 
35280a83cfcSMichal Kazior 	if (local->ops->wake_tx_queue)
35380a83cfcSMichal Kazior 		return;
35480a83cfcSMichal Kazior 
355a6f38ac3SJohannes Berg 	if (local->hw.queues < IEEE80211_NUM_ACS)
356a6f38ac3SJohannes Berg 		n_acs = 1;
3573a25a8c8SJohannes Berg 
3583a25a8c8SJohannes Berg 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3593a25a8c8SJohannes Berg 		int ac;
3603a25a8c8SJohannes Berg 
361f142c6b9SJohannes Berg 		if (!sdata->dev)
362f142c6b9SJohannes Berg 			continue;
363f142c6b9SJohannes Berg 
3643a25a8c8SJohannes Berg 		if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
3653a25a8c8SJohannes Berg 		    local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
3663a25a8c8SJohannes Berg 			continue;
3673a25a8c8SJohannes Berg 
368a6f38ac3SJohannes Berg 		for (ac = 0; ac < n_acs; ac++) {
3693a25a8c8SJohannes Berg 			int ac_queue = sdata->vif.hw_queue[ac];
3703a25a8c8SJohannes Berg 
3713a25a8c8SJohannes Berg 			if (ac_queue == queue ||
3723a25a8c8SJohannes Berg 			    (sdata->vif.cab_queue == queue &&
3733a25a8c8SJohannes Berg 			     local->queue_stop_reasons[ac_queue] == 0 &&
3743a25a8c8SJohannes Berg 			     skb_queue_empty(&local->pending[ac_queue])))
3753a25a8c8SJohannes Berg 				netif_wake_subqueue(sdata->dev, ac);
3763a25a8c8SJohannes Berg 		}
3773a25a8c8SJohannes Berg 	}
3783a25a8c8SJohannes Berg }
3793a25a8c8SJohannes Berg 
380ce7c9111SKalle Valo static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
381cca07b00SLuciano Coelho 				   enum queue_stop_reason reason,
382f6c7f03fSEmmanuel Grumbach 				   bool refcounted,
383f6c7f03fSEmmanuel Grumbach 				   unsigned long *flags)
384c2d1560aSJohannes Berg {
385c2d1560aSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
386c2d1560aSJohannes Berg 
387b5878a2dSJohannes Berg 	trace_wake_queue(local, queue, reason);
388b5878a2dSJohannes Berg 
389e4e72fb4SJohannes Berg 	if (WARN_ON(queue >= hw->queues))
39096f5e66eSJohannes Berg 		return;
39196f5e66eSJohannes Berg 
392ada15125SJohannes Berg 	if (!test_bit(reason, &local->queue_stop_reasons[queue]))
393ada15125SJohannes Berg 		return;
394ada15125SJohannes Berg 
395856142cdSJohannes Berg 	if (!refcounted) {
396cca07b00SLuciano Coelho 		local->q_stop_reasons[queue][reason] = 0;
397856142cdSJohannes Berg 	} else {
398cca07b00SLuciano Coelho 		local->q_stop_reasons[queue][reason]--;
399856142cdSJohannes Berg 		if (WARN_ON(local->q_stop_reasons[queue][reason] < 0))
400856142cdSJohannes Berg 			local->q_stop_reasons[queue][reason] = 0;
401856142cdSJohannes Berg 	}
402cca07b00SLuciano Coelho 
403cca07b00SLuciano Coelho 	if (local->q_stop_reasons[queue][reason] == 0)
404ce7c9111SKalle Valo 		__clear_bit(reason, &local->queue_stop_reasons[queue]);
405ce7c9111SKalle Valo 
406ce7c9111SKalle Valo 	if (local->queue_stop_reasons[queue] != 0)
407ce7c9111SKalle Valo 		/* someone still has this queue stopped */
408ce7c9111SKalle Valo 		return;
409ce7c9111SKalle Valo 
4107236fe29SJohannes Berg 	if (skb_queue_empty(&local->pending[queue])) {
411cf0277e7SJohannes Berg 		rcu_read_lock();
4123a25a8c8SJohannes Berg 		ieee80211_propagate_queue_wake(local, queue);
413cf0277e7SJohannes Berg 		rcu_read_unlock();
4147236fe29SJohannes Berg 	} else
4157236fe29SJohannes Berg 		tasklet_schedule(&local->tx_pending_tasklet);
41621a5d4c3SManikanta Pubbisetty 
417f6c7f03fSEmmanuel Grumbach 	/*
418f6c7f03fSEmmanuel Grumbach 	 * Calling _ieee80211_wake_txqs here can be a problem because it may
419f6c7f03fSEmmanuel Grumbach 	 * release queue_stop_reason_lock which has been taken by
420f6c7f03fSEmmanuel Grumbach 	 * __ieee80211_wake_queue's caller. It is certainly not very nice to
421f6c7f03fSEmmanuel Grumbach 	 * release someone's lock, but it is fine because all the callers of
422f6c7f03fSEmmanuel Grumbach 	 * __ieee80211_wake_queue call it right before releasing the lock.
423f6c7f03fSEmmanuel Grumbach 	 */
424f6c7f03fSEmmanuel Grumbach 	if (local->ops->wake_tx_queue) {
425f6c7f03fSEmmanuel Grumbach 		if (reason == IEEE80211_QUEUE_STOP_REASON_DRIVER)
42621a5d4c3SManikanta Pubbisetty 			tasklet_schedule(&local->wake_txqs_tasklet);
427f6c7f03fSEmmanuel Grumbach 		else
428f6c7f03fSEmmanuel Grumbach 			_ieee80211_wake_txqs(local, flags);
429f6c7f03fSEmmanuel Grumbach 	}
430c2d1560aSJohannes Berg }
431ce7c9111SKalle Valo 
43296f5e66eSJohannes Berg void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
433cca07b00SLuciano Coelho 				    enum queue_stop_reason reason,
434cca07b00SLuciano Coelho 				    bool refcounted)
435ce7c9111SKalle Valo {
436ce7c9111SKalle Valo 	struct ieee80211_local *local = hw_to_local(hw);
437ce7c9111SKalle Valo 	unsigned long flags;
438ce7c9111SKalle Valo 
439ce7c9111SKalle Valo 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
440f6c7f03fSEmmanuel Grumbach 	__ieee80211_wake_queue(hw, queue, reason, refcounted, &flags);
441ce7c9111SKalle Valo 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
442ce7c9111SKalle Valo }
443ce7c9111SKalle Valo 
444ce7c9111SKalle Valo void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
445ce7c9111SKalle Valo {
446ce7c9111SKalle Valo 	ieee80211_wake_queue_by_reason(hw, queue,
447cca07b00SLuciano Coelho 				       IEEE80211_QUEUE_STOP_REASON_DRIVER,
448cca07b00SLuciano Coelho 				       false);
449ce7c9111SKalle Valo }
450c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_wake_queue);
451c2d1560aSJohannes Berg 
452ce7c9111SKalle Valo static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
453cca07b00SLuciano Coelho 				   enum queue_stop_reason reason,
454cca07b00SLuciano Coelho 				   bool refcounted)
455c2d1560aSJohannes Berg {
456c2d1560aSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
457cf0277e7SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
458a6f38ac3SJohannes Berg 	int n_acs = IEEE80211_NUM_ACS;
459c2d1560aSJohannes Berg 
460b5878a2dSJohannes Berg 	trace_stop_queue(local, queue, reason);
461b5878a2dSJohannes Berg 
462e4e72fb4SJohannes Berg 	if (WARN_ON(queue >= hw->queues))
46396f5e66eSJohannes Berg 		return;
46496f5e66eSJohannes Berg 
465cca07b00SLuciano Coelho 	if (!refcounted)
466cca07b00SLuciano Coelho 		local->q_stop_reasons[queue][reason] = 1;
467cca07b00SLuciano Coelho 	else
468cca07b00SLuciano Coelho 		local->q_stop_reasons[queue][reason]++;
469ada15125SJohannes Berg 
470cca07b00SLuciano Coelho 	if (__test_and_set_bit(reason, &local->queue_stop_reasons[queue]))
471cca07b00SLuciano Coelho 		return;
472cf0277e7SJohannes Berg 
473a6f38ac3SJohannes Berg 	if (local->hw.queues < IEEE80211_NUM_ACS)
474a6f38ac3SJohannes Berg 		n_acs = 1;
475a6f38ac3SJohannes Berg 
476cf0277e7SJohannes Berg 	rcu_read_lock();
4773a25a8c8SJohannes Berg 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4783a25a8c8SJohannes Berg 		int ac;
4793a25a8c8SJohannes Berg 
480f142c6b9SJohannes Berg 		if (!sdata->dev)
481f142c6b9SJohannes Berg 			continue;
482f142c6b9SJohannes Berg 
483a6f38ac3SJohannes Berg 		for (ac = 0; ac < n_acs; ac++) {
4843a25a8c8SJohannes Berg 			if (sdata->vif.hw_queue[ac] == queue ||
48521a5d4c3SManikanta Pubbisetty 			    sdata->vif.cab_queue == queue) {
48621a5d4c3SManikanta Pubbisetty 				if (!local->ops->wake_tx_queue) {
4873a25a8c8SJohannes Berg 					netif_stop_subqueue(sdata->dev, ac);
48821a5d4c3SManikanta Pubbisetty 					continue;
48921a5d4c3SManikanta Pubbisetty 				}
49021a5d4c3SManikanta Pubbisetty 				spin_lock(&local->fq.lock);
49121a5d4c3SManikanta Pubbisetty 				sdata->vif.txqs_stopped[ac] = true;
49221a5d4c3SManikanta Pubbisetty 				spin_unlock(&local->fq.lock);
49321a5d4c3SManikanta Pubbisetty 			}
4943a25a8c8SJohannes Berg 		}
4953a25a8c8SJohannes Berg 	}
496cf0277e7SJohannes Berg 	rcu_read_unlock();
497c2d1560aSJohannes Berg }
498ce7c9111SKalle Valo 
49996f5e66eSJohannes Berg void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
500cca07b00SLuciano Coelho 				    enum queue_stop_reason reason,
501cca07b00SLuciano Coelho 				    bool refcounted)
502ce7c9111SKalle Valo {
503ce7c9111SKalle Valo 	struct ieee80211_local *local = hw_to_local(hw);
504ce7c9111SKalle Valo 	unsigned long flags;
505ce7c9111SKalle Valo 
506ce7c9111SKalle Valo 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
507cca07b00SLuciano Coelho 	__ieee80211_stop_queue(hw, queue, reason, refcounted);
508ce7c9111SKalle Valo 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
509ce7c9111SKalle Valo }
510ce7c9111SKalle Valo 
511ce7c9111SKalle Valo void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
512ce7c9111SKalle Valo {
513ce7c9111SKalle Valo 	ieee80211_stop_queue_by_reason(hw, queue,
514cca07b00SLuciano Coelho 				       IEEE80211_QUEUE_STOP_REASON_DRIVER,
515cca07b00SLuciano Coelho 				       false);
516ce7c9111SKalle Valo }
517c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_stop_queue);
518c2d1560aSJohannes Berg 
5198f77f384SJohannes Berg void ieee80211_add_pending_skb(struct ieee80211_local *local,
5208f77f384SJohannes Berg 			       struct sk_buff *skb)
5218f77f384SJohannes Berg {
5228f77f384SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
5238f77f384SJohannes Berg 	unsigned long flags;
524a7bc376cSJohannes Berg 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
5253a25a8c8SJohannes Berg 	int queue = info->hw_queue;
526a7bc376cSJohannes Berg 
527a7bc376cSJohannes Berg 	if (WARN_ON(!info->control.vif)) {
528d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
529a7bc376cSJohannes Berg 		return;
530a7bc376cSJohannes Berg 	}
5318f77f384SJohannes Berg 
5328f77f384SJohannes Berg 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
533cca07b00SLuciano Coelho 	__ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
534cca07b00SLuciano Coelho 			       false);
5353b8d81e0SJohannes Berg 	__skb_queue_tail(&local->pending[queue], skb);
536cca07b00SLuciano Coelho 	__ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
537f6c7f03fSEmmanuel Grumbach 			       false, &flags);
5388f77f384SJohannes Berg 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
5398f77f384SJohannes Berg }
5408f77f384SJohannes Berg 
541e3685e03SJohannes Berg void ieee80211_add_pending_skbs(struct ieee80211_local *local,
542e3685e03SJohannes Berg 				struct sk_buff_head *skbs)
5438f77f384SJohannes Berg {
5448f77f384SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
5458f77f384SJohannes Berg 	struct sk_buff *skb;
5468f77f384SJohannes Berg 	unsigned long flags;
547b0b97a8aSJohannes Berg 	int queue, i;
5488f77f384SJohannes Berg 
5498f77f384SJohannes Berg 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
5508f77f384SJohannes Berg 	while ((skb = skb_dequeue(skbs))) {
551a7bc376cSJohannes Berg 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
552a7bc376cSJohannes Berg 
553a7bc376cSJohannes Berg 		if (WARN_ON(!info->control.vif)) {
554d4fa14cdSFelix Fietkau 			ieee80211_free_txskb(&local->hw, skb);
555a7bc376cSJohannes Berg 			continue;
556a7bc376cSJohannes Berg 		}
557a7bc376cSJohannes Berg 
5583a25a8c8SJohannes Berg 		queue = info->hw_queue;
5594644ae89SJohannes Berg 
5604644ae89SJohannes Berg 		__ieee80211_stop_queue(hw, queue,
561cca07b00SLuciano Coelho 				IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
562cca07b00SLuciano Coelho 				false);
5634644ae89SJohannes Berg 
5643b8d81e0SJohannes Berg 		__skb_queue_tail(&local->pending[queue], skb);
5658f77f384SJohannes Berg 	}
5668f77f384SJohannes Berg 
5673b8d81e0SJohannes Berg 	for (i = 0; i < hw->queues; i++)
5688f77f384SJohannes Berg 		__ieee80211_wake_queue(hw, i,
569cca07b00SLuciano Coelho 			IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
570f6c7f03fSEmmanuel Grumbach 			false, &flags);
5718f77f384SJohannes Berg 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
5728f77f384SJohannes Berg }
5738f77f384SJohannes Berg 
574ce7c9111SKalle Valo void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
575445ea4e8SJohannes Berg 				     unsigned long queues,
576cca07b00SLuciano Coelho 				     enum queue_stop_reason reason,
577cca07b00SLuciano Coelho 				     bool refcounted)
578c2d1560aSJohannes Berg {
579ce7c9111SKalle Valo 	struct ieee80211_local *local = hw_to_local(hw);
580ce7c9111SKalle Valo 	unsigned long flags;
581c2d1560aSJohannes Berg 	int i;
582c2d1560aSJohannes Berg 
583ce7c9111SKalle Valo 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
584ce7c9111SKalle Valo 
585445ea4e8SJohannes Berg 	for_each_set_bit(i, &queues, hw->queues)
586cca07b00SLuciano Coelho 		__ieee80211_stop_queue(hw, i, reason, refcounted);
587ce7c9111SKalle Valo 
588ce7c9111SKalle Valo 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
589ce7c9111SKalle Valo }
590ce7c9111SKalle Valo 
591ce7c9111SKalle Valo void ieee80211_stop_queues(struct ieee80211_hw *hw)
592ce7c9111SKalle Valo {
593445ea4e8SJohannes Berg 	ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
594cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_DRIVER,
595cca07b00SLuciano Coelho 					false);
596c2d1560aSJohannes Berg }
597c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_stop_queues);
598c2d1560aSJohannes Berg 
59992ab8535STomas Winkler int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
60092ab8535STomas Winkler {
60192ab8535STomas Winkler 	struct ieee80211_local *local = hw_to_local(hw);
6023b8d81e0SJohannes Berg 	unsigned long flags;
6033b8d81e0SJohannes Berg 	int ret;
60496f5e66eSJohannes Berg 
605e4e72fb4SJohannes Berg 	if (WARN_ON(queue >= hw->queues))
60696f5e66eSJohannes Berg 		return true;
60796f5e66eSJohannes Berg 
6083b8d81e0SJohannes Berg 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
6092419ea14SThomas Pedersen 	ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
6102419ea14SThomas Pedersen 		       &local->queue_stop_reasons[queue]);
6113b8d81e0SJohannes Berg 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
6123b8d81e0SJohannes Berg 	return ret;
61392ab8535STomas Winkler }
61492ab8535STomas Winkler EXPORT_SYMBOL(ieee80211_queue_stopped);
61592ab8535STomas Winkler 
616ce7c9111SKalle Valo void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
617445ea4e8SJohannes Berg 				     unsigned long queues,
618cca07b00SLuciano Coelho 				     enum queue_stop_reason reason,
619cca07b00SLuciano Coelho 				     bool refcounted)
620c2d1560aSJohannes Berg {
621ce7c9111SKalle Valo 	struct ieee80211_local *local = hw_to_local(hw);
622ce7c9111SKalle Valo 	unsigned long flags;
623c2d1560aSJohannes Berg 	int i;
624c2d1560aSJohannes Berg 
625ce7c9111SKalle Valo 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
626ce7c9111SKalle Valo 
627445ea4e8SJohannes Berg 	for_each_set_bit(i, &queues, hw->queues)
628f6c7f03fSEmmanuel Grumbach 		__ieee80211_wake_queue(hw, i, reason, refcounted, &flags);
629ce7c9111SKalle Valo 
630ce7c9111SKalle Valo 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
631ce7c9111SKalle Valo }
632ce7c9111SKalle Valo 
633ce7c9111SKalle Valo void ieee80211_wake_queues(struct ieee80211_hw *hw)
634ce7c9111SKalle Valo {
635445ea4e8SJohannes Berg 	ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
636cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_DRIVER,
637cca07b00SLuciano Coelho 					false);
638c2d1560aSJohannes Berg }
639c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_wake_queues);
640dabeb344SJohannes Berg 
64126da23b6SLuciano Coelho static unsigned int
64226da23b6SLuciano Coelho ieee80211_get_vif_queues(struct ieee80211_local *local,
64339ecc01dSJohannes Berg 			 struct ieee80211_sub_if_data *sdata)
64439ecc01dSJohannes Berg {
64526da23b6SLuciano Coelho 	unsigned int queues;
64639ecc01dSJohannes Berg 
64730686bf7SJohannes Berg 	if (sdata && ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
64839ecc01dSJohannes Berg 		int ac;
64939ecc01dSJohannes Berg 
65039ecc01dSJohannes Berg 		queues = 0;
65139ecc01dSJohannes Berg 
65239ecc01dSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
65339ecc01dSJohannes Berg 			queues |= BIT(sdata->vif.hw_queue[ac]);
65439ecc01dSJohannes Berg 		if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
65539ecc01dSJohannes Berg 			queues |= BIT(sdata->vif.cab_queue);
65639ecc01dSJohannes Berg 	} else {
65739ecc01dSJohannes Berg 		/* all queues */
65839ecc01dSJohannes Berg 		queues = BIT(local->hw.queues) - 1;
65939ecc01dSJohannes Berg 	}
66039ecc01dSJohannes Berg 
66126da23b6SLuciano Coelho 	return queues;
66226da23b6SLuciano Coelho }
66326da23b6SLuciano Coelho 
6644f9610d5SLiad Kaufman void __ieee80211_flush_queues(struct ieee80211_local *local,
6654f9610d5SLiad Kaufman 			      struct ieee80211_sub_if_data *sdata,
6663b24f4c6SEmmanuel Grumbach 			      unsigned int queues, bool drop)
66726da23b6SLuciano Coelho {
66826da23b6SLuciano Coelho 	if (!local->ops->flush)
66926da23b6SLuciano Coelho 		return;
67026da23b6SLuciano Coelho 
6714f9610d5SLiad Kaufman 	/*
6724f9610d5SLiad Kaufman 	 * If no queue was set, or if the HW doesn't support
6734f9610d5SLiad Kaufman 	 * IEEE80211_HW_QUEUE_CONTROL - flush all queues
6744f9610d5SLiad Kaufman 	 */
67530686bf7SJohannes Berg 	if (!queues || !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
67626da23b6SLuciano Coelho 		queues = ieee80211_get_vif_queues(local, sdata);
67726da23b6SLuciano Coelho 
67859f48fe2SLuciano Coelho 	ieee80211_stop_queues_by_reason(&local->hw, queues,
679cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_FLUSH,
680cca07b00SLuciano Coelho 					false);
681445ea4e8SJohannes Berg 
6823b24f4c6SEmmanuel Grumbach 	drv_flush(local, sdata, queues, drop);
683445ea4e8SJohannes Berg 
68459f48fe2SLuciano Coelho 	ieee80211_wake_queues_by_reason(&local->hw, queues,
685cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_FLUSH,
686cca07b00SLuciano Coelho 					false);
68739ecc01dSJohannes Berg }
68839ecc01dSJohannes Berg 
6894f9610d5SLiad Kaufman void ieee80211_flush_queues(struct ieee80211_local *local,
6903b24f4c6SEmmanuel Grumbach 			    struct ieee80211_sub_if_data *sdata, bool drop)
6914f9610d5SLiad Kaufman {
6923b24f4c6SEmmanuel Grumbach 	__ieee80211_flush_queues(local, sdata, 0, drop);
6934f9610d5SLiad Kaufman }
6944f9610d5SLiad Kaufman 
69526da23b6SLuciano Coelho void ieee80211_stop_vif_queues(struct ieee80211_local *local,
69626da23b6SLuciano Coelho 			       struct ieee80211_sub_if_data *sdata,
69726da23b6SLuciano Coelho 			       enum queue_stop_reason reason)
69826da23b6SLuciano Coelho {
69926da23b6SLuciano Coelho 	ieee80211_stop_queues_by_reason(&local->hw,
70026da23b6SLuciano Coelho 					ieee80211_get_vif_queues(local, sdata),
70126da23b6SLuciano Coelho 					reason, true);
70226da23b6SLuciano Coelho }
70326da23b6SLuciano Coelho 
70426da23b6SLuciano Coelho void ieee80211_wake_vif_queues(struct ieee80211_local *local,
70526da23b6SLuciano Coelho 			       struct ieee80211_sub_if_data *sdata,
70626da23b6SLuciano Coelho 			       enum queue_stop_reason reason)
70726da23b6SLuciano Coelho {
70826da23b6SLuciano Coelho 	ieee80211_wake_queues_by_reason(&local->hw,
70926da23b6SLuciano Coelho 					ieee80211_get_vif_queues(local, sdata),
71026da23b6SLuciano Coelho 					reason, true);
71126da23b6SLuciano Coelho }
71226da23b6SLuciano Coelho 
7133384d757SArik Nemtsov static void __iterate_interfaces(struct ieee80211_local *local,
714c7c71066SJohannes Berg 				 u32 iter_flags,
715dabeb344SJohannes Berg 				 void (*iterator)(void *data, u8 *mac,
71632bfd35dSJohannes Berg 						  struct ieee80211_vif *vif),
717dabeb344SJohannes Berg 				 void *data)
718dabeb344SJohannes Berg {
719dabeb344SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
7203384d757SArik Nemtsov 	bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE;
721dabeb344SJohannes Berg 
722e38bad47SJohannes Berg 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
72351fb61e7SJohannes Berg 		switch (sdata->vif.type) {
72405c914feSJohannes Berg 		case NL80211_IFTYPE_MONITOR:
725d8212184SAviya Erenfeld 			if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
72631eba5bcSFelix Fietkau 				continue;
72731eba5bcSFelix Fietkau 			break;
72805c914feSJohannes Berg 		case NL80211_IFTYPE_AP_VLAN:
729dabeb344SJohannes Berg 			continue;
7302ca27bcfSJohannes Berg 		default:
731dabeb344SJohannes Berg 			break;
732dabeb344SJohannes Berg 		}
7338b2c9824SJohannes Berg 		if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
7343384d757SArik Nemtsov 		    active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
7358b2c9824SJohannes Berg 			continue;
7363384d757SArik Nemtsov 		if (ieee80211_sdata_running(sdata) || !active_only)
73747846c9bSJohannes Berg 			iterator(data, sdata->vif.addr,
73832bfd35dSJohannes Berg 				 &sdata->vif);
739dabeb344SJohannes Berg 	}
740e38bad47SJohannes Berg 
741c7c71066SJohannes Berg 	sdata = rcu_dereference_check(local->monitor_sdata,
742c7c71066SJohannes Berg 				      lockdep_is_held(&local->iflist_mtx) ||
743c7c71066SJohannes Berg 				      lockdep_rtnl_is_held());
7448b2c9824SJohannes Berg 	if (sdata &&
7453384d757SArik Nemtsov 	    (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only ||
7468b2c9824SJohannes Berg 	     sdata->flags & IEEE80211_SDATA_IN_DRIVER))
747685fb72bSJohannes Berg 		iterator(data, sdata->vif.addr, &sdata->vif);
748c7c71066SJohannes Berg }
749685fb72bSJohannes Berg 
7503384d757SArik Nemtsov void ieee80211_iterate_interfaces(
751c7c71066SJohannes Berg 	struct ieee80211_hw *hw, u32 iter_flags,
752c7c71066SJohannes Berg 	void (*iterator)(void *data, u8 *mac,
753c7c71066SJohannes Berg 			 struct ieee80211_vif *vif),
754c7c71066SJohannes Berg 	void *data)
755c7c71066SJohannes Berg {
756c7c71066SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
757c7c71066SJohannes Berg 
758c7c71066SJohannes Berg 	mutex_lock(&local->iflist_mtx);
7593384d757SArik Nemtsov 	__iterate_interfaces(local, iter_flags, iterator, data);
760c7c71066SJohannes Berg 	mutex_unlock(&local->iflist_mtx);
761c7c71066SJohannes Berg }
7623384d757SArik Nemtsov EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces);
763c7c71066SJohannes Berg 
764c7c71066SJohannes Berg void ieee80211_iterate_active_interfaces_atomic(
765c7c71066SJohannes Berg 	struct ieee80211_hw *hw, u32 iter_flags,
766c7c71066SJohannes Berg 	void (*iterator)(void *data, u8 *mac,
767c7c71066SJohannes Berg 			 struct ieee80211_vif *vif),
768c7c71066SJohannes Berg 	void *data)
769c7c71066SJohannes Berg {
770c7c71066SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
771c7c71066SJohannes Berg 
772c7c71066SJohannes Berg 	rcu_read_lock();
7733384d757SArik Nemtsov 	__iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
7743384d757SArik Nemtsov 			     iterator, data);
775e38bad47SJohannes Berg 	rcu_read_unlock();
776dabeb344SJohannes Berg }
7772f561febSIvo van Doorn EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
77837ffc8daSJohannes Berg 
779c7c71066SJohannes Berg void ieee80211_iterate_active_interfaces_rtnl(
780c7c71066SJohannes Berg 	struct ieee80211_hw *hw, u32 iter_flags,
781c7c71066SJohannes Berg 	void (*iterator)(void *data, u8 *mac,
782c7c71066SJohannes Berg 			 struct ieee80211_vif *vif),
783c7c71066SJohannes Berg 	void *data)
784c7c71066SJohannes Berg {
785c7c71066SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
786c7c71066SJohannes Berg 
787c7c71066SJohannes Berg 	ASSERT_RTNL();
788c7c71066SJohannes Berg 
7893384d757SArik Nemtsov 	__iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
7903384d757SArik Nemtsov 			     iterator, data);
791c7c71066SJohannes Berg }
792c7c71066SJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_rtnl);
793c7c71066SJohannes Berg 
7940fc1e049SArik Nemtsov static void __iterate_stations(struct ieee80211_local *local,
7950fc1e049SArik Nemtsov 			       void (*iterator)(void *data,
7960fc1e049SArik Nemtsov 						struct ieee80211_sta *sta),
7970fc1e049SArik Nemtsov 			       void *data)
7980fc1e049SArik Nemtsov {
7990fc1e049SArik Nemtsov 	struct sta_info *sta;
8000fc1e049SArik Nemtsov 
8010fc1e049SArik Nemtsov 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
8020fc1e049SArik Nemtsov 		if (!sta->uploaded)
8030fc1e049SArik Nemtsov 			continue;
8040fc1e049SArik Nemtsov 
8050fc1e049SArik Nemtsov 		iterator(data, &sta->sta);
8060fc1e049SArik Nemtsov 	}
8070fc1e049SArik Nemtsov }
8080fc1e049SArik Nemtsov 
8090fc1e049SArik Nemtsov void ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
8100fc1e049SArik Nemtsov 			void (*iterator)(void *data,
8110fc1e049SArik Nemtsov 					 struct ieee80211_sta *sta),
8120fc1e049SArik Nemtsov 			void *data)
8130fc1e049SArik Nemtsov {
8140fc1e049SArik Nemtsov 	struct ieee80211_local *local = hw_to_local(hw);
8150fc1e049SArik Nemtsov 
8160fc1e049SArik Nemtsov 	rcu_read_lock();
8170fc1e049SArik Nemtsov 	__iterate_stations(local, iterator, data);
8180fc1e049SArik Nemtsov 	rcu_read_unlock();
8190fc1e049SArik Nemtsov }
8200fc1e049SArik Nemtsov EXPORT_SYMBOL_GPL(ieee80211_iterate_stations_atomic);
8210fc1e049SArik Nemtsov 
822ad7e718cSJohannes Berg struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
823ad7e718cSJohannes Berg {
824ad7e718cSJohannes Berg 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
825ad7e718cSJohannes Berg 
826ad7e718cSJohannes Berg 	if (!ieee80211_sdata_running(sdata) ||
827ad7e718cSJohannes Berg 	    !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
828ad7e718cSJohannes Berg 		return NULL;
829ad7e718cSJohannes Berg 	return &sdata->vif;
830ad7e718cSJohannes Berg }
831ad7e718cSJohannes Berg EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
832ad7e718cSJohannes Berg 
833dc5a1ad7SEmmanuel Grumbach struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
834dc5a1ad7SEmmanuel Grumbach {
8356513e98eSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8366513e98eSJohannes Berg 
8376513e98eSJohannes Berg 	if (!vif)
8386513e98eSJohannes Berg 		return NULL;
8396513e98eSJohannes Berg 
8406513e98eSJohannes Berg 	sdata = vif_to_sdata(vif);
841dc5a1ad7SEmmanuel Grumbach 
842dc5a1ad7SEmmanuel Grumbach 	if (!ieee80211_sdata_running(sdata) ||
843dc5a1ad7SEmmanuel Grumbach 	    !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
844dc5a1ad7SEmmanuel Grumbach 		return NULL;
845dc5a1ad7SEmmanuel Grumbach 
846dc5a1ad7SEmmanuel Grumbach 	return &sdata->wdev;
847dc5a1ad7SEmmanuel Grumbach }
848dc5a1ad7SEmmanuel Grumbach EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
849dc5a1ad7SEmmanuel Grumbach 
85042935ecaSLuis R. Rodriguez /*
85142935ecaSLuis R. Rodriguez  * Nothing should have been stuffed into the workqueue during
8524afaff17SEmmanuel Grumbach  * the suspend->resume cycle. Since we can't check each caller
8534afaff17SEmmanuel Grumbach  * of this function if we are already quiescing / suspended,
8544afaff17SEmmanuel Grumbach  * check here and don't WARN since this can actually happen when
8554afaff17SEmmanuel Grumbach  * the rx path (for example) is racing against __ieee80211_suspend
8564afaff17SEmmanuel Grumbach  * and suspending / quiescing was set after the rx path checked
8574afaff17SEmmanuel Grumbach  * them.
85842935ecaSLuis R. Rodriguez  */
85942935ecaSLuis R. Rodriguez static bool ieee80211_can_queue_work(struct ieee80211_local *local)
86042935ecaSLuis R. Rodriguez {
8614afaff17SEmmanuel Grumbach 	if (local->quiescing || (local->suspended && !local->resuming)) {
8624afaff17SEmmanuel Grumbach 		pr_warn("queueing ieee80211 work while going to suspend\n");
86342935ecaSLuis R. Rodriguez 		return false;
8644afaff17SEmmanuel Grumbach 	}
86542935ecaSLuis R. Rodriguez 
86642935ecaSLuis R. Rodriguez 	return true;
86742935ecaSLuis R. Rodriguez }
86842935ecaSLuis R. Rodriguez 
86942935ecaSLuis R. Rodriguez void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
87042935ecaSLuis R. Rodriguez {
87142935ecaSLuis R. Rodriguez 	struct ieee80211_local *local = hw_to_local(hw);
87242935ecaSLuis R. Rodriguez 
87342935ecaSLuis R. Rodriguez 	if (!ieee80211_can_queue_work(local))
87442935ecaSLuis R. Rodriguez 		return;
87542935ecaSLuis R. Rodriguez 
87642935ecaSLuis R. Rodriguez 	queue_work(local->workqueue, work);
87742935ecaSLuis R. Rodriguez }
87842935ecaSLuis R. Rodriguez EXPORT_SYMBOL(ieee80211_queue_work);
87942935ecaSLuis R. Rodriguez 
88042935ecaSLuis R. Rodriguez void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
88142935ecaSLuis R. Rodriguez 				  struct delayed_work *dwork,
88242935ecaSLuis R. Rodriguez 				  unsigned long delay)
88342935ecaSLuis R. Rodriguez {
88442935ecaSLuis R. Rodriguez 	struct ieee80211_local *local = hw_to_local(hw);
88542935ecaSLuis R. Rodriguez 
88642935ecaSLuis R. Rodriguez 	if (!ieee80211_can_queue_work(local))
88742935ecaSLuis R. Rodriguez 		return;
88842935ecaSLuis R. Rodriguez 
88942935ecaSLuis R. Rodriguez 	queue_delayed_work(local->workqueue, dwork, delay);
89042935ecaSLuis R. Rodriguez }
89142935ecaSLuis R. Rodriguez EXPORT_SYMBOL(ieee80211_queue_delayed_work);
89242935ecaSLuis R. Rodriguez 
893e4d005b8SJohannes Berg static void ieee80211_parse_extension_element(u32 *crc,
894e4d005b8SJohannes Berg 					      const struct element *elem,
895e4d005b8SJohannes Berg 					      struct ieee802_11_elems *elems)
896e4d005b8SJohannes Berg {
897e4d005b8SJohannes Berg 	const void *data = elem->data + 1;
898e4d005b8SJohannes Berg 	u8 len = elem->datalen - 1;
899e4d005b8SJohannes Berg 
900e4d005b8SJohannes Berg 	switch (elem->data[0]) {
901e4d005b8SJohannes Berg 	case WLAN_EID_EXT_HE_MU_EDCA:
902e4d005b8SJohannes Berg 		if (len == sizeof(*elems->mu_edca_param_set)) {
903e4d005b8SJohannes Berg 			elems->mu_edca_param_set = data;
904e4d005b8SJohannes Berg 			if (crc)
905e4d005b8SJohannes Berg 				*crc = crc32_be(*crc, (void *)elem,
906e4d005b8SJohannes Berg 						elem->datalen + 2);
907e4d005b8SJohannes Berg 		}
908e4d005b8SJohannes Berg 		break;
909e4d005b8SJohannes Berg 	case WLAN_EID_EXT_HE_CAPABILITY:
910e4d005b8SJohannes Berg 		elems->he_cap = data;
911e4d005b8SJohannes Berg 		elems->he_cap_len = len;
912e4d005b8SJohannes Berg 		break;
913e4d005b8SJohannes Berg 	case WLAN_EID_EXT_HE_OPERATION:
914e4d005b8SJohannes Berg 		if (len >= sizeof(*elems->he_operation) &&
915660d81daSJohannes Berg 		    len == ieee80211_he_oper_size(data) - 1) {
916660d81daSJohannes Berg 			if (crc)
917660d81daSJohannes Berg 				*crc = crc32_be(*crc, (void *)elem,
918660d81daSJohannes Berg 						elem->datalen + 2);
919e4d005b8SJohannes Berg 			elems->he_operation = data;
920660d81daSJohannes Berg 		}
921e4d005b8SJohannes Berg 		break;
922e4d005b8SJohannes Berg 	case WLAN_EID_EXT_UORA:
923e4d005b8SJohannes Berg 		if (len == 1)
924e4d005b8SJohannes Berg 			elems->uora_element = data;
925e4d005b8SJohannes Berg 		break;
926e4d005b8SJohannes Berg 	case WLAN_EID_EXT_MAX_CHANNEL_SWITCH_TIME:
927e4d005b8SJohannes Berg 		if (len == 3)
928e4d005b8SJohannes Berg 			elems->max_channel_switch_time = data;
929e4d005b8SJohannes Berg 		break;
930e4d005b8SJohannes Berg 	case WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION:
931e4d005b8SJohannes Berg 		if (len == sizeof(*elems->mbssid_config_ie))
932e4d005b8SJohannes Berg 			elems->mbssid_config_ie = data;
933e4d005b8SJohannes Berg 		break;
934e4d005b8SJohannes Berg 	case WLAN_EID_EXT_HE_SPR:
935e4d005b8SJohannes Berg 		if (len >= sizeof(*elems->he_spr) &&
936e4d005b8SJohannes Berg 		    len >= ieee80211_he_spr_size(data))
937e4d005b8SJohannes Berg 			elems->he_spr = data;
938e4d005b8SJohannes Berg 		break;
939a6cf28e0SRajkumar Manoharan 	case WLAN_EID_EXT_HE_6GHZ_CAPA:
940a6cf28e0SRajkumar Manoharan 		if (len == sizeof(*elems->he_6ghz_capa))
941a6cf28e0SRajkumar Manoharan 			elems->he_6ghz_capa = data;
942a6cf28e0SRajkumar Manoharan 		break;
943e4d005b8SJohannes Berg 	}
944e4d005b8SJohannes Berg }
945e4d005b8SJohannes Berg 
94678ac51f8SSara Sharon static u32
94778ac51f8SSara Sharon _ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
948dd76986bSJohannes Berg 			    struct ieee802_11_elems *elems,
949671042a4SSara Sharon 			    u64 filter, u32 crc,
950671042a4SSara Sharon 			    const struct element *check_inherit)
951dd76986bSJohannes Berg {
952671042a4SSara Sharon 	const struct element *elem;
953dd76986bSJohannes Berg 	bool calc_crc = filter != 0;
954fcff4f10SPaul Stewart 	DECLARE_BITMAP(seen_elems, 256);
955b2e506bfSJohannes Berg 	const u8 *ie;
956dd76986bSJohannes Berg 
957fcff4f10SPaul Stewart 	bitmap_zero(seen_elems, 256);
958dd76986bSJohannes Berg 
959c17e28d1SJohannes Berg 	for_each_element(elem, start, len) {
960fcff4f10SPaul Stewart 		bool elem_parse_failed;
961c17e28d1SJohannes Berg 		u8 id = elem->id;
962c17e28d1SJohannes Berg 		u8 elen = elem->datalen;
963c17e28d1SJohannes Berg 		const u8 *pos = elem->data;
964fcff4f10SPaul Stewart 
965671042a4SSara Sharon 		if (check_inherit &&
966671042a4SSara Sharon 		    !cfg80211_is_element_inherited(elem,
967671042a4SSara Sharon 						   check_inherit))
968671042a4SSara Sharon 			continue;
969671042a4SSara Sharon 
9709690fb16SJohannes Berg 		switch (id) {
9719690fb16SJohannes Berg 		case WLAN_EID_SSID:
9729690fb16SJohannes Berg 		case WLAN_EID_SUPP_RATES:
9739690fb16SJohannes Berg 		case WLAN_EID_FH_PARAMS:
9749690fb16SJohannes Berg 		case WLAN_EID_DS_PARAMS:
9759690fb16SJohannes Berg 		case WLAN_EID_CF_PARAMS:
9769690fb16SJohannes Berg 		case WLAN_EID_TIM:
9779690fb16SJohannes Berg 		case WLAN_EID_IBSS_PARAMS:
9789690fb16SJohannes Berg 		case WLAN_EID_CHALLENGE:
9799690fb16SJohannes Berg 		case WLAN_EID_RSN:
9809690fb16SJohannes Berg 		case WLAN_EID_ERP_INFO:
9819690fb16SJohannes Berg 		case WLAN_EID_EXT_SUPP_RATES:
9829690fb16SJohannes Berg 		case WLAN_EID_HT_CAPABILITY:
9839690fb16SJohannes Berg 		case WLAN_EID_HT_OPERATION:
9849690fb16SJohannes Berg 		case WLAN_EID_VHT_CAPABILITY:
9859690fb16SJohannes Berg 		case WLAN_EID_VHT_OPERATION:
9869690fb16SJohannes Berg 		case WLAN_EID_MESH_ID:
9879690fb16SJohannes Berg 		case WLAN_EID_MESH_CONFIG:
9889690fb16SJohannes Berg 		case WLAN_EID_PEER_MGMT:
9899690fb16SJohannes Berg 		case WLAN_EID_PREQ:
9909690fb16SJohannes Berg 		case WLAN_EID_PREP:
9919690fb16SJohannes Berg 		case WLAN_EID_PERR:
9929690fb16SJohannes Berg 		case WLAN_EID_RANN:
9939690fb16SJohannes Berg 		case WLAN_EID_CHANNEL_SWITCH:
9949690fb16SJohannes Berg 		case WLAN_EID_EXT_CHANSWITCH_ANN:
9959690fb16SJohannes Berg 		case WLAN_EID_COUNTRY:
9969690fb16SJohannes Berg 		case WLAN_EID_PWR_CONSTRAINT:
9979690fb16SJohannes Berg 		case WLAN_EID_TIMEOUT_INTERVAL:
99885220d71SJohannes Berg 		case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
999b2e506bfSJohannes Berg 		case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
10008f2535b9SChun-Yeow Yeoh 		case WLAN_EID_CHAN_SWITCH_PARAM:
10019041c1faSArik Nemtsov 		case WLAN_EID_EXT_CAPABILITY:
100253837584SArik Nemtsov 		case WLAN_EID_CHAN_SWITCH_TIMING:
100353837584SArik Nemtsov 		case WLAN_EID_LINK_ID:
1004e38a017bSAvraham Stern 		case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1005c0058df7SShaul Triebitz 		case WLAN_EID_RSNX:
1006b2e506bfSJohannes Berg 		/*
1007b2e506bfSJohannes Berg 		 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
1008b2e506bfSJohannes Berg 		 * that if the content gets bigger it might be needed more than once
1009b2e506bfSJohannes Berg 		 */
10109690fb16SJohannes Berg 			if (test_bit(id, seen_elems)) {
1011fcff4f10SPaul Stewart 				elems->parse_error = true;
1012fcff4f10SPaul Stewart 				continue;
1013fcff4f10SPaul Stewart 			}
10149690fb16SJohannes Berg 			break;
10159690fb16SJohannes Berg 		}
1016dd76986bSJohannes Berg 
1017dd76986bSJohannes Berg 		if (calc_crc && id < 64 && (filter & (1ULL << id)))
1018dd76986bSJohannes Berg 			crc = crc32_be(crc, pos - 2, elen + 2);
1019dd76986bSJohannes Berg 
1020fcff4f10SPaul Stewart 		elem_parse_failed = false;
1021fcff4f10SPaul Stewart 
1022dd76986bSJohannes Berg 		switch (id) {
102353837584SArik Nemtsov 		case WLAN_EID_LINK_ID:
102453837584SArik Nemtsov 			if (elen + 2 != sizeof(struct ieee80211_tdls_lnkie)) {
102553837584SArik Nemtsov 				elem_parse_failed = true;
102653837584SArik Nemtsov 				break;
102753837584SArik Nemtsov 			}
102853837584SArik Nemtsov 			elems->lnk_id = (void *)(pos - 2);
102953837584SArik Nemtsov 			break;
103053837584SArik Nemtsov 		case WLAN_EID_CHAN_SWITCH_TIMING:
103153837584SArik Nemtsov 			if (elen != sizeof(struct ieee80211_ch_switch_timing)) {
103253837584SArik Nemtsov 				elem_parse_failed = true;
103353837584SArik Nemtsov 				break;
103453837584SArik Nemtsov 			}
103553837584SArik Nemtsov 			elems->ch_sw_timing = (void *)pos;
103653837584SArik Nemtsov 			break;
10379041c1faSArik Nemtsov 		case WLAN_EID_EXT_CAPABILITY:
10389041c1faSArik Nemtsov 			elems->ext_capab = pos;
10399041c1faSArik Nemtsov 			elems->ext_capab_len = elen;
10409041c1faSArik Nemtsov 			break;
1041dd76986bSJohannes Berg 		case WLAN_EID_SSID:
1042dd76986bSJohannes Berg 			elems->ssid = pos;
1043dd76986bSJohannes Berg 			elems->ssid_len = elen;
1044dd76986bSJohannes Berg 			break;
1045dd76986bSJohannes Berg 		case WLAN_EID_SUPP_RATES:
1046dd76986bSJohannes Berg 			elems->supp_rates = pos;
1047dd76986bSJohannes Berg 			elems->supp_rates_len = elen;
1048dd76986bSJohannes Berg 			break;
1049dd76986bSJohannes Berg 		case WLAN_EID_DS_PARAMS:
10501cd8e88eSJohannes Berg 			if (elen >= 1)
1051dd76986bSJohannes Berg 				elems->ds_params = pos;
10521cd8e88eSJohannes Berg 			else
10531cd8e88eSJohannes Berg 				elem_parse_failed = true;
1054dd76986bSJohannes Berg 			break;
1055dd76986bSJohannes Berg 		case WLAN_EID_TIM:
1056dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_tim_ie)) {
1057dd76986bSJohannes Berg 				elems->tim = (void *)pos;
1058dd76986bSJohannes Berg 				elems->tim_len = elen;
1059fcff4f10SPaul Stewart 			} else
1060fcff4f10SPaul Stewart 				elem_parse_failed = true;
1061dd76986bSJohannes Berg 			break;
1062dd76986bSJohannes Berg 		case WLAN_EID_CHALLENGE:
1063dd76986bSJohannes Berg 			elems->challenge = pos;
1064dd76986bSJohannes Berg 			elems->challenge_len = elen;
1065dd76986bSJohannes Berg 			break;
1066dd76986bSJohannes Berg 		case WLAN_EID_VENDOR_SPECIFIC:
1067dd76986bSJohannes Berg 			if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
1068dd76986bSJohannes Berg 			    pos[2] == 0xf2) {
1069dd76986bSJohannes Berg 				/* Microsoft OUI (00:50:F2) */
1070dd76986bSJohannes Berg 
1071dd76986bSJohannes Berg 				if (calc_crc)
1072dd76986bSJohannes Berg 					crc = crc32_be(crc, pos - 2, elen + 2);
1073dd76986bSJohannes Berg 
1074441a33baSJohannes Berg 				if (elen >= 5 && pos[3] == 2) {
1075dd76986bSJohannes Berg 					/* OUI Type 2 - WMM IE */
1076dd76986bSJohannes Berg 					if (pos[4] == 0) {
1077dd76986bSJohannes Berg 						elems->wmm_info = pos;
1078dd76986bSJohannes Berg 						elems->wmm_info_len = elen;
1079dd76986bSJohannes Berg 					} else if (pos[4] == 1) {
1080dd76986bSJohannes Berg 						elems->wmm_param = pos;
1081dd76986bSJohannes Berg 						elems->wmm_param_len = elen;
1082dd76986bSJohannes Berg 					}
1083dd76986bSJohannes Berg 				}
1084dd76986bSJohannes Berg 			}
1085dd76986bSJohannes Berg 			break;
1086dd76986bSJohannes Berg 		case WLAN_EID_RSN:
1087dd76986bSJohannes Berg 			elems->rsn = pos;
1088dd76986bSJohannes Berg 			elems->rsn_len = elen;
1089dd76986bSJohannes Berg 			break;
1090dd76986bSJohannes Berg 		case WLAN_EID_ERP_INFO:
10911946bed9SJohannes Berg 			if (elen >= 1)
1092dd76986bSJohannes Berg 				elems->erp_info = pos;
10931946bed9SJohannes Berg 			else
10941946bed9SJohannes Berg 				elem_parse_failed = true;
1095dd76986bSJohannes Berg 			break;
1096dd76986bSJohannes Berg 		case WLAN_EID_EXT_SUPP_RATES:
1097dd76986bSJohannes Berg 			elems->ext_supp_rates = pos;
1098dd76986bSJohannes Berg 			elems->ext_supp_rates_len = elen;
1099dd76986bSJohannes Berg 			break;
1100dd76986bSJohannes Berg 		case WLAN_EID_HT_CAPABILITY:
1101dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_ht_cap))
1102dd76986bSJohannes Berg 				elems->ht_cap_elem = (void *)pos;
1103fcff4f10SPaul Stewart 			else
1104fcff4f10SPaul Stewart 				elem_parse_failed = true;
1105dd76986bSJohannes Berg 			break;
1106074d46d1SJohannes Berg 		case WLAN_EID_HT_OPERATION:
1107074d46d1SJohannes Berg 			if (elen >= sizeof(struct ieee80211_ht_operation))
1108074d46d1SJohannes Berg 				elems->ht_operation = (void *)pos;
1109fcff4f10SPaul Stewart 			else
1110fcff4f10SPaul Stewart 				elem_parse_failed = true;
1111dd76986bSJohannes Berg 			break;
1112818255eaSMahesh Palivela 		case WLAN_EID_VHT_CAPABILITY:
1113818255eaSMahesh Palivela 			if (elen >= sizeof(struct ieee80211_vht_cap))
1114818255eaSMahesh Palivela 				elems->vht_cap_elem = (void *)pos;
1115818255eaSMahesh Palivela 			else
1116818255eaSMahesh Palivela 				elem_parse_failed = true;
1117818255eaSMahesh Palivela 			break;
1118818255eaSMahesh Palivela 		case WLAN_EID_VHT_OPERATION:
1119a04564c9SJohannes Berg 			if (elen >= sizeof(struct ieee80211_vht_operation)) {
1120818255eaSMahesh Palivela 				elems->vht_operation = (void *)pos;
1121a04564c9SJohannes Berg 				if (calc_crc)
1122a04564c9SJohannes Berg 					crc = crc32_be(crc, pos - 2, elen + 2);
1123a04564c9SJohannes Berg 				break;
1124a04564c9SJohannes Berg 			}
1125818255eaSMahesh Palivela 			elem_parse_failed = true;
1126818255eaSMahesh Palivela 			break;
1127bee7f586SJohannes Berg 		case WLAN_EID_OPMODE_NOTIF:
1128a04564c9SJohannes Berg 			if (elen > 0) {
1129bee7f586SJohannes Berg 				elems->opmode_notif = pos;
1130a04564c9SJohannes Berg 				if (calc_crc)
1131a04564c9SJohannes Berg 					crc = crc32_be(crc, pos - 2, elen + 2);
1132a04564c9SJohannes Berg 				break;
1133a04564c9SJohannes Berg 			}
1134bee7f586SJohannes Berg 			elem_parse_failed = true;
1135bee7f586SJohannes Berg 			break;
1136dd76986bSJohannes Berg 		case WLAN_EID_MESH_ID:
1137dd76986bSJohannes Berg 			elems->mesh_id = pos;
1138dd76986bSJohannes Berg 			elems->mesh_id_len = elen;
1139dd76986bSJohannes Berg 			break;
1140dd76986bSJohannes Berg 		case WLAN_EID_MESH_CONFIG:
1141dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_meshconf_ie))
1142dd76986bSJohannes Berg 				elems->mesh_config = (void *)pos;
1143fcff4f10SPaul Stewart 			else
1144fcff4f10SPaul Stewart 				elem_parse_failed = true;
1145dd76986bSJohannes Berg 			break;
1146dd76986bSJohannes Berg 		case WLAN_EID_PEER_MGMT:
1147dd76986bSJohannes Berg 			elems->peering = pos;
1148dd76986bSJohannes Berg 			elems->peering_len = elen;
1149dd76986bSJohannes Berg 			break;
11503f52b7e3SMarco Porsch 		case WLAN_EID_MESH_AWAKE_WINDOW:
11513f52b7e3SMarco Porsch 			if (elen >= 2)
11523f52b7e3SMarco Porsch 				elems->awake_window = (void *)pos;
11533f52b7e3SMarco Porsch 			break;
1154dd76986bSJohannes Berg 		case WLAN_EID_PREQ:
1155dd76986bSJohannes Berg 			elems->preq = pos;
1156dd76986bSJohannes Berg 			elems->preq_len = elen;
1157dd76986bSJohannes Berg 			break;
1158dd76986bSJohannes Berg 		case WLAN_EID_PREP:
1159dd76986bSJohannes Berg 			elems->prep = pos;
1160dd76986bSJohannes Berg 			elems->prep_len = elen;
1161dd76986bSJohannes Berg 			break;
1162dd76986bSJohannes Berg 		case WLAN_EID_PERR:
1163dd76986bSJohannes Berg 			elems->perr = pos;
1164dd76986bSJohannes Berg 			elems->perr_len = elen;
1165dd76986bSJohannes Berg 			break;
1166dd76986bSJohannes Berg 		case WLAN_EID_RANN:
1167dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_rann_ie))
1168dd76986bSJohannes Berg 				elems->rann = (void *)pos;
1169fcff4f10SPaul Stewart 			else
1170fcff4f10SPaul Stewart 				elem_parse_failed = true;
1171dd76986bSJohannes Berg 			break;
1172dd76986bSJohannes Berg 		case WLAN_EID_CHANNEL_SWITCH:
11735bc1420bSJohannes Berg 			if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
11745bc1420bSJohannes Berg 				elem_parse_failed = true;
11755bc1420bSJohannes Berg 				break;
11765bc1420bSJohannes Berg 			}
11775bc1420bSJohannes Berg 			elems->ch_switch_ie = (void *)pos;
1178dd76986bSJohannes Berg 			break;
1179b4f286a1SJohannes Berg 		case WLAN_EID_EXT_CHANSWITCH_ANN:
1180b4f286a1SJohannes Berg 			if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
1181b4f286a1SJohannes Berg 				elem_parse_failed = true;
1182b4f286a1SJohannes Berg 				break;
1183b4f286a1SJohannes Berg 			}
1184b4f286a1SJohannes Berg 			elems->ext_chansw_ie = (void *)pos;
1185b4f286a1SJohannes Berg 			break;
118685220d71SJohannes Berg 		case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
118785220d71SJohannes Berg 			if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
118885220d71SJohannes Berg 				elem_parse_failed = true;
118985220d71SJohannes Berg 				break;
119085220d71SJohannes Berg 			}
119185220d71SJohannes Berg 			elems->sec_chan_offs = (void *)pos;
119285220d71SJohannes Berg 			break;
11938f2535b9SChun-Yeow Yeoh 		case WLAN_EID_CHAN_SWITCH_PARAM:
11948f2535b9SChun-Yeow Yeoh 			if (elen !=
11958f2535b9SChun-Yeow Yeoh 			    sizeof(*elems->mesh_chansw_params_ie)) {
11968f2535b9SChun-Yeow Yeoh 				elem_parse_failed = true;
11978f2535b9SChun-Yeow Yeoh 				break;
11988f2535b9SChun-Yeow Yeoh 			}
11998f2535b9SChun-Yeow Yeoh 			elems->mesh_chansw_params_ie = (void *)pos;
12008f2535b9SChun-Yeow Yeoh 			break;
1201b2e506bfSJohannes Berg 		case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1202b2e506bfSJohannes Berg 			if (!action ||
1203b2e506bfSJohannes Berg 			    elen != sizeof(*elems->wide_bw_chansw_ie)) {
1204b2e506bfSJohannes Berg 				elem_parse_failed = true;
1205b2e506bfSJohannes Berg 				break;
1206b2e506bfSJohannes Berg 			}
1207b2e506bfSJohannes Berg 			elems->wide_bw_chansw_ie = (void *)pos;
1208b2e506bfSJohannes Berg 			break;
1209b2e506bfSJohannes Berg 		case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
1210b2e506bfSJohannes Berg 			if (action) {
1211b2e506bfSJohannes Berg 				elem_parse_failed = true;
1212b2e506bfSJohannes Berg 				break;
1213b2e506bfSJohannes Berg 			}
1214b2e506bfSJohannes Berg 			/*
1215b2e506bfSJohannes Berg 			 * This is a bit tricky, but as we only care about
1216b2e506bfSJohannes Berg 			 * the wide bandwidth channel switch element, so
1217b2e506bfSJohannes Berg 			 * just parse it out manually.
1218b2e506bfSJohannes Berg 			 */
1219b2e506bfSJohannes Berg 			ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
1220b2e506bfSJohannes Berg 					      pos, elen);
1221b2e506bfSJohannes Berg 			if (ie) {
1222b2e506bfSJohannes Berg 				if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
1223b2e506bfSJohannes Berg 					elems->wide_bw_chansw_ie =
1224b2e506bfSJohannes Berg 						(void *)(ie + 2);
1225b2e506bfSJohannes Berg 				else
1226b2e506bfSJohannes Berg 					elem_parse_failed = true;
1227b2e506bfSJohannes Berg 			}
1228b2e506bfSJohannes Berg 			break;
1229dd76986bSJohannes Berg 		case WLAN_EID_COUNTRY:
1230dd76986bSJohannes Berg 			elems->country_elem = pos;
1231dd76986bSJohannes Berg 			elems->country_elem_len = elen;
1232dd76986bSJohannes Berg 			break;
1233dd76986bSJohannes Berg 		case WLAN_EID_PWR_CONSTRAINT:
1234761a48d2SJohannes Berg 			if (elen != 1) {
1235761a48d2SJohannes Berg 				elem_parse_failed = true;
1236761a48d2SJohannes Berg 				break;
1237761a48d2SJohannes Berg 			}
1238dd76986bSJohannes Berg 			elems->pwr_constr_elem = pos;
1239dd76986bSJohannes Berg 			break;
1240c8d65917SSteinar H. Gunderson 		case WLAN_EID_CISCO_VENDOR_SPECIFIC:
1241c8d65917SSteinar H. Gunderson 			/* Lots of different options exist, but we only care
1242c8d65917SSteinar H. Gunderson 			 * about the Dynamic Transmit Power Control element.
1243c8d65917SSteinar H. Gunderson 			 * First check for the Cisco OUI, then for the DTPC
1244c8d65917SSteinar H. Gunderson 			 * tag (0x00).
1245c8d65917SSteinar H. Gunderson 			 */
1246c8d65917SSteinar H. Gunderson 			if (elen < 4) {
1247c8d65917SSteinar H. Gunderson 				elem_parse_failed = true;
1248c8d65917SSteinar H. Gunderson 				break;
1249c8d65917SSteinar H. Gunderson 			}
1250c8d65917SSteinar H. Gunderson 
1251c8d65917SSteinar H. Gunderson 			if (pos[0] != 0x00 || pos[1] != 0x40 ||
1252c8d65917SSteinar H. Gunderson 			    pos[2] != 0x96 || pos[3] != 0x00)
1253c8d65917SSteinar H. Gunderson 				break;
1254c8d65917SSteinar H. Gunderson 
1255c8d65917SSteinar H. Gunderson 			if (elen != 6) {
1256c8d65917SSteinar H. Gunderson 				elem_parse_failed = true;
1257c8d65917SSteinar H. Gunderson 				break;
1258c8d65917SSteinar H. Gunderson 			}
1259c8d65917SSteinar H. Gunderson 
1260c8d65917SSteinar H. Gunderson 			if (calc_crc)
1261c8d65917SSteinar H. Gunderson 				crc = crc32_be(crc, pos - 2, elen + 2);
1262c8d65917SSteinar H. Gunderson 
1263c8d65917SSteinar H. Gunderson 			elems->cisco_dtpc_elem = pos;
1264c8d65917SSteinar H. Gunderson 			break;
12652aa485e1SJohn Crispin 		case WLAN_EID_ADDBA_EXT:
12662aa485e1SJohn Crispin 			if (elen != sizeof(struct ieee80211_addba_ext_ie)) {
12672aa485e1SJohn Crispin 				elem_parse_failed = true;
12682aa485e1SJohn Crispin 				break;
12692aa485e1SJohn Crispin 			}
12702aa485e1SJohn Crispin 			elems->addba_ext_ie = (void *)pos;
12712aa485e1SJohn Crispin 			break;
1272dd76986bSJohannes Berg 		case WLAN_EID_TIMEOUT_INTERVAL:
127379ba1d89SJohannes Berg 			if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
127479ba1d89SJohannes Berg 				elems->timeout_int = (void *)pos;
127579ba1d89SJohannes Berg 			else
127679ba1d89SJohannes Berg 				elem_parse_failed = true;
1277dd76986bSJohannes Berg 			break;
1278e38a017bSAvraham Stern 		case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1279e38a017bSAvraham Stern 			if (elen >= sizeof(*elems->max_idle_period_ie))
1280e38a017bSAvraham Stern 				elems->max_idle_period_ie = (void *)pos;
1281e38a017bSAvraham Stern 			break;
1282c0058df7SShaul Triebitz 		case WLAN_EID_RSNX:
1283c0058df7SShaul Triebitz 			elems->rsnx = pos;
1284c0058df7SShaul Triebitz 			elems->rsnx_len = elen;
1285c0058df7SShaul Triebitz 			break;
128641cbb0f5SLuca Coelho 		case WLAN_EID_EXTENSION:
1287e4d005b8SJohannes Berg 			ieee80211_parse_extension_element(calc_crc ?
1288e4d005b8SJohannes Berg 								&crc : NULL,
1289e4d005b8SJohannes Berg 							  elem, elems);
129041cbb0f5SLuca Coelho 			break;
1291dd76986bSJohannes Berg 		default:
1292dd76986bSJohannes Berg 			break;
1293dd76986bSJohannes Berg 		}
1294dd76986bSJohannes Berg 
1295fcff4f10SPaul Stewart 		if (elem_parse_failed)
1296fcff4f10SPaul Stewart 			elems->parse_error = true;
1297fcff4f10SPaul Stewart 		else
12985df45690SJohannes Berg 			__set_bit(id, seen_elems);
1299dd76986bSJohannes Berg 	}
1300dd76986bSJohannes Berg 
1301c17e28d1SJohannes Berg 	if (!for_each_element_completed(elem, start, len))
1302fcff4f10SPaul Stewart 		elems->parse_error = true;
1303fcff4f10SPaul Stewart 
1304dd76986bSJohannes Berg 	return crc;
1305dd76986bSJohannes Berg }
1306dd76986bSJohannes Berg 
13075023b14cSSara Sharon static size_t ieee802_11_find_bssid_profile(const u8 *start, size_t len,
1308671042a4SSara Sharon 					    struct ieee802_11_elems *elems,
1309671042a4SSara Sharon 					    u8 *transmitter_bssid,
13105023b14cSSara Sharon 					    u8 *bss_bssid,
13115809a5d5SDan Carpenter 					    u8 *nontransmitted_profile)
1312671042a4SSara Sharon {
1313671042a4SSara Sharon 	const struct element *elem, *sub;
13145023b14cSSara Sharon 	size_t profile_len = 0;
13155023b14cSSara Sharon 	bool found = false;
1316671042a4SSara Sharon 
1317671042a4SSara Sharon 	if (!bss_bssid || !transmitter_bssid)
13185023b14cSSara Sharon 		return profile_len;
1319671042a4SSara Sharon 
1320671042a4SSara Sharon 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, len) {
1321671042a4SSara Sharon 		if (elem->datalen < 2)
1322671042a4SSara Sharon 			continue;
1323671042a4SSara Sharon 
1324671042a4SSara Sharon 		for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1325671042a4SSara Sharon 			u8 new_bssid[ETH_ALEN];
1326671042a4SSara Sharon 			const u8 *index;
1327671042a4SSara Sharon 
1328671042a4SSara Sharon 			if (sub->id != 0 || sub->datalen < 4) {
1329671042a4SSara Sharon 				/* not a valid BSS profile */
1330671042a4SSara Sharon 				continue;
1331671042a4SSara Sharon 			}
1332671042a4SSara Sharon 
1333671042a4SSara Sharon 			if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1334671042a4SSara Sharon 			    sub->data[1] != 2) {
1335671042a4SSara Sharon 				/* The first element of the
1336671042a4SSara Sharon 				 * Nontransmitted BSSID Profile is not
1337671042a4SSara Sharon 				 * the Nontransmitted BSSID Capability
1338671042a4SSara Sharon 				 * element.
1339671042a4SSara Sharon 				 */
1340671042a4SSara Sharon 				continue;
1341671042a4SSara Sharon 			}
1342671042a4SSara Sharon 
13435809a5d5SDan Carpenter 			memset(nontransmitted_profile, 0, len);
13445023b14cSSara Sharon 			profile_len = cfg80211_merge_profile(start, len,
13455023b14cSSara Sharon 							     elem,
13465023b14cSSara Sharon 							     sub,
13475023b14cSSara Sharon 							     nontransmitted_profile,
13485023b14cSSara Sharon 							     len);
13495023b14cSSara Sharon 
1350671042a4SSara Sharon 			/* found a Nontransmitted BSSID Profile */
1351671042a4SSara Sharon 			index = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX,
13525809a5d5SDan Carpenter 						 nontransmitted_profile,
13535023b14cSSara Sharon 						 profile_len);
1354671042a4SSara Sharon 			if (!index || index[1] < 1 || index[2] == 0) {
1355671042a4SSara Sharon 				/* Invalid MBSSID Index element */
1356671042a4SSara Sharon 				continue;
1357671042a4SSara Sharon 			}
1358671042a4SSara Sharon 
1359671042a4SSara Sharon 			cfg80211_gen_new_bssid(transmitter_bssid,
1360671042a4SSara Sharon 					       elem->data[0],
1361671042a4SSara Sharon 					       index[2],
1362671042a4SSara Sharon 					       new_bssid);
1363671042a4SSara Sharon 			if (ether_addr_equal(new_bssid, bss_bssid)) {
13645023b14cSSara Sharon 				found = true;
1365671042a4SSara Sharon 				elems->bssid_index_len = index[1];
1366671042a4SSara Sharon 				elems->bssid_index = (void *)&index[2];
1367671042a4SSara Sharon 				break;
1368671042a4SSara Sharon 			}
1369671042a4SSara Sharon 		}
1370671042a4SSara Sharon 	}
13715023b14cSSara Sharon 
13725023b14cSSara Sharon 	return found ? profile_len : 0;
1373671042a4SSara Sharon }
1374671042a4SSara Sharon 
137578ac51f8SSara Sharon u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
137678ac51f8SSara Sharon 			       struct ieee802_11_elems *elems,
137778ac51f8SSara Sharon 			       u64 filter, u32 crc, u8 *transmitter_bssid,
137878ac51f8SSara Sharon 			       u8 *bss_bssid)
137978ac51f8SSara Sharon {
1380671042a4SSara Sharon 	const struct element *non_inherit = NULL;
13815023b14cSSara Sharon 	u8 *nontransmitted_profile;
13825023b14cSSara Sharon 	int nontransmitted_profile_len = 0;
1383671042a4SSara Sharon 
138478ac51f8SSara Sharon 	memset(elems, 0, sizeof(*elems));
138578ac51f8SSara Sharon 	elems->ie_start = start;
138678ac51f8SSara Sharon 	elems->total_len = len;
138778ac51f8SSara Sharon 
13885023b14cSSara Sharon 	nontransmitted_profile = kmalloc(len, GFP_ATOMIC);
13895023b14cSSara Sharon 	if (nontransmitted_profile) {
13905023b14cSSara Sharon 		nontransmitted_profile_len =
13915023b14cSSara Sharon 			ieee802_11_find_bssid_profile(start, len, elems,
13925023b14cSSara Sharon 						      transmitter_bssid,
13935023b14cSSara Sharon 						      bss_bssid,
13945809a5d5SDan Carpenter 						      nontransmitted_profile);
1395671042a4SSara Sharon 		non_inherit =
1396671042a4SSara Sharon 			cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
13975023b14cSSara Sharon 					       nontransmitted_profile,
13985023b14cSSara Sharon 					       nontransmitted_profile_len);
13995023b14cSSara Sharon 	}
1400671042a4SSara Sharon 
140178ac51f8SSara Sharon 	crc = _ieee802_11_parse_elems_crc(start, len, action, elems, filter,
1402671042a4SSara Sharon 					  crc, non_inherit);
140378ac51f8SSara Sharon 
140478ac51f8SSara Sharon 	/* Override with nontransmitted profile, if found */
14055023b14cSSara Sharon 	if (nontransmitted_profile_len)
14065023b14cSSara Sharon 		_ieee802_11_parse_elems_crc(nontransmitted_profile,
14075023b14cSSara Sharon 					    nontransmitted_profile_len,
1408671042a4SSara Sharon 					    action, elems, 0, 0, NULL);
140978ac51f8SSara Sharon 
141078ac51f8SSara Sharon 	if (elems->tim && !elems->parse_error) {
141178ac51f8SSara Sharon 		const struct ieee80211_tim_ie *tim_ie = elems->tim;
141278ac51f8SSara Sharon 
141378ac51f8SSara Sharon 		elems->dtim_period = tim_ie->dtim_period;
141478ac51f8SSara Sharon 		elems->dtim_count = tim_ie->dtim_count;
141578ac51f8SSara Sharon 	}
141678ac51f8SSara Sharon 
141778ac51f8SSara Sharon 	/* Override DTIM period and count if needed */
141878ac51f8SSara Sharon 	if (elems->bssid_index &&
141978ac51f8SSara Sharon 	    elems->bssid_index_len >=
142078ac51f8SSara Sharon 	    offsetofend(struct ieee80211_bssid_index, dtim_period))
142178ac51f8SSara Sharon 		elems->dtim_period = elems->bssid_index->dtim_period;
142278ac51f8SSara Sharon 
142378ac51f8SSara Sharon 	if (elems->bssid_index &&
142478ac51f8SSara Sharon 	    elems->bssid_index_len >=
142578ac51f8SSara Sharon 	    offsetofend(struct ieee80211_bssid_index, dtim_count))
142678ac51f8SSara Sharon 		elems->dtim_count = elems->bssid_index->dtim_count;
142778ac51f8SSara Sharon 
14285023b14cSSara Sharon 	kfree(nontransmitted_profile);
14295023b14cSSara Sharon 
143078ac51f8SSara Sharon 	return crc;
143178ac51f8SSara Sharon }
143278ac51f8SSara Sharon 
1433e552af05SHaim Dreyfuss void ieee80211_regulatory_limit_wmm_params(struct ieee80211_sub_if_data *sdata,
1434e552af05SHaim Dreyfuss 					   struct ieee80211_tx_queue_params
1435e552af05SHaim Dreyfuss 					   *qparam, int ac)
1436e552af05SHaim Dreyfuss {
1437e552af05SHaim Dreyfuss 	struct ieee80211_chanctx_conf *chanctx_conf;
1438e552af05SHaim Dreyfuss 	const struct ieee80211_reg_rule *rrule;
143938cb87eeSStanislaw Gruszka 	const struct ieee80211_wmm_ac *wmm_ac;
1440e552af05SHaim Dreyfuss 	u16 center_freq = 0;
1441e552af05SHaim Dreyfuss 
1442e552af05SHaim Dreyfuss 	if (sdata->vif.type != NL80211_IFTYPE_AP &&
1443e552af05SHaim Dreyfuss 	    sdata->vif.type != NL80211_IFTYPE_STATION)
1444e552af05SHaim Dreyfuss 		return;
1445e552af05SHaim Dreyfuss 
1446e552af05SHaim Dreyfuss 	rcu_read_lock();
1447e552af05SHaim Dreyfuss 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1448e552af05SHaim Dreyfuss 	if (chanctx_conf)
1449e552af05SHaim Dreyfuss 		center_freq = chanctx_conf->def.chan->center_freq;
1450e552af05SHaim Dreyfuss 
1451e552af05SHaim Dreyfuss 	if (!center_freq) {
1452e552af05SHaim Dreyfuss 		rcu_read_unlock();
1453e552af05SHaim Dreyfuss 		return;
1454e552af05SHaim Dreyfuss 	}
1455e552af05SHaim Dreyfuss 
1456e552af05SHaim Dreyfuss 	rrule = freq_reg_info(sdata->wdev.wiphy, MHZ_TO_KHZ(center_freq));
1457e552af05SHaim Dreyfuss 
145838cb87eeSStanislaw Gruszka 	if (IS_ERR_OR_NULL(rrule) || !rrule->has_wmm) {
1459e552af05SHaim Dreyfuss 		rcu_read_unlock();
1460e552af05SHaim Dreyfuss 		return;
1461e552af05SHaim Dreyfuss 	}
1462e552af05SHaim Dreyfuss 
1463e552af05SHaim Dreyfuss 	if (sdata->vif.type == NL80211_IFTYPE_AP)
146438cb87eeSStanislaw Gruszka 		wmm_ac = &rrule->wmm_rule.ap[ac];
1465e552af05SHaim Dreyfuss 	else
146638cb87eeSStanislaw Gruszka 		wmm_ac = &rrule->wmm_rule.client[ac];
1467e552af05SHaim Dreyfuss 	qparam->cw_min = max_t(u16, qparam->cw_min, wmm_ac->cw_min);
1468e552af05SHaim Dreyfuss 	qparam->cw_max = max_t(u16, qparam->cw_max, wmm_ac->cw_max);
1469e552af05SHaim Dreyfuss 	qparam->aifs = max_t(u8, qparam->aifs, wmm_ac->aifsn);
1470abd76d25SDreyfuss, Haim 	qparam->txop = min_t(u16, qparam->txop, wmm_ac->cot / 32);
1471e552af05SHaim Dreyfuss 	rcu_read_unlock();
1472e552af05SHaim Dreyfuss }
1473e552af05SHaim Dreyfuss 
14743abead59SJohannes Berg void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
1475cec66283SJohannes Berg 			       bool bss_notify, bool enable_qos)
14765825fe10SJohannes Berg {
14775825fe10SJohannes Berg 	struct ieee80211_local *local = sdata->local;
14785825fe10SJohannes Berg 	struct ieee80211_tx_queue_params qparam;
147955de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
148054bcbc69SJohannes Berg 	int ac;
1481cec66283SJohannes Berg 	bool use_11b;
1482239281f8SRostislav Lisovy 	bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
1483aa837e1dSJohannes Berg 	int aCWmin, aCWmax;
14845825fe10SJohannes Berg 
14855825fe10SJohannes Berg 	if (!local->ops->conf_tx)
14865825fe10SJohannes Berg 		return;
14875825fe10SJohannes Berg 
148854bcbc69SJohannes Berg 	if (local->hw.queues < IEEE80211_NUM_ACS)
148954bcbc69SJohannes Berg 		return;
149054bcbc69SJohannes Berg 
14915825fe10SJohannes Berg 	memset(&qparam, 0, sizeof(qparam));
14925825fe10SJohannes Berg 
149355de908aSJohannes Berg 	rcu_read_lock();
149455de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
149555de908aSJohannes Berg 	use_11b = (chanctx_conf &&
149657fbcce3SJohannes Berg 		   chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
1497aa837e1dSJohannes Berg 		 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
149855de908aSJohannes Berg 	rcu_read_unlock();
14995825fe10SJohannes Berg 
1500239281f8SRostislav Lisovy 	is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
1501239281f8SRostislav Lisovy 
1502aa837e1dSJohannes Berg 	/* Set defaults according to 802.11-2007 Table 7-37 */
1503aa837e1dSJohannes Berg 	aCWmax = 1023;
1504aa837e1dSJohannes Berg 	if (use_11b)
1505aa837e1dSJohannes Berg 		aCWmin = 31;
15065825fe10SJohannes Berg 	else
1507aa837e1dSJohannes Berg 		aCWmin = 15;
15085825fe10SJohannes Berg 
15091f4ffde8SFred Zhou 	/* Confiure old 802.11b/g medium access rules. */
15101f4ffde8SFred Zhou 	qparam.cw_max = aCWmax;
15111f4ffde8SFred Zhou 	qparam.cw_min = aCWmin;
15121f4ffde8SFred Zhou 	qparam.txop = 0;
15131f4ffde8SFred Zhou 	qparam.aifs = 2;
15141f4ffde8SFred Zhou 
15151f4ffde8SFred Zhou 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
15161f4ffde8SFred Zhou 		/* Update if QoS is enabled. */
1517a8ce8544SStanislaw Gruszka 		if (enable_qos) {
151854bcbc69SJohannes Berg 			switch (ac) {
15191d98fb12SJohannes Berg 			case IEEE80211_AC_BK:
15207ba10a8eSJohannes Berg 				qparam.cw_max = aCWmax;
15217ba10a8eSJohannes Berg 				qparam.cw_min = aCWmin;
15225825fe10SJohannes Berg 				qparam.txop = 0;
1523239281f8SRostislav Lisovy 				if (is_ocb)
1524239281f8SRostislav Lisovy 					qparam.aifs = 9;
1525239281f8SRostislav Lisovy 				else
1526aa837e1dSJohannes Berg 					qparam.aifs = 7;
1527aa837e1dSJohannes Berg 				break;
1528a8ce8544SStanislaw Gruszka 			/* never happens but let's not leave undefined */
1529a8ce8544SStanislaw Gruszka 			default:
15301d98fb12SJohannes Berg 			case IEEE80211_AC_BE:
15317ba10a8eSJohannes Berg 				qparam.cw_max = aCWmax;
15327ba10a8eSJohannes Berg 				qparam.cw_min = aCWmin;
1533aa837e1dSJohannes Berg 				qparam.txop = 0;
1534239281f8SRostislav Lisovy 				if (is_ocb)
1535239281f8SRostislav Lisovy 					qparam.aifs = 6;
1536239281f8SRostislav Lisovy 				else
1537aa837e1dSJohannes Berg 					qparam.aifs = 3;
1538aa837e1dSJohannes Berg 				break;
15391d98fb12SJohannes Berg 			case IEEE80211_AC_VI:
1540aa837e1dSJohannes Berg 				qparam.cw_max = aCWmin;
1541aa837e1dSJohannes Berg 				qparam.cw_min = (aCWmin + 1) / 2 - 1;
1542239281f8SRostislav Lisovy 				if (is_ocb)
1543239281f8SRostislav Lisovy 					qparam.txop = 0;
1544239281f8SRostislav Lisovy 				else if (use_11b)
1545aa837e1dSJohannes Berg 					qparam.txop = 6016/32;
1546aa837e1dSJohannes Berg 				else
1547aa837e1dSJohannes Berg 					qparam.txop = 3008/32;
1548239281f8SRostislav Lisovy 
1549239281f8SRostislav Lisovy 				if (is_ocb)
1550239281f8SRostislav Lisovy 					qparam.aifs = 3;
1551239281f8SRostislav Lisovy 				else
1552aa837e1dSJohannes Berg 					qparam.aifs = 2;
1553aa837e1dSJohannes Berg 				break;
15541d98fb12SJohannes Berg 			case IEEE80211_AC_VO:
1555aa837e1dSJohannes Berg 				qparam.cw_max = (aCWmin + 1) / 2 - 1;
1556aa837e1dSJohannes Berg 				qparam.cw_min = (aCWmin + 1) / 4 - 1;
1557239281f8SRostislav Lisovy 				if (is_ocb)
1558239281f8SRostislav Lisovy 					qparam.txop = 0;
1559239281f8SRostislav Lisovy 				else if (use_11b)
1560aa837e1dSJohannes Berg 					qparam.txop = 3264/32;
1561aa837e1dSJohannes Berg 				else
1562aa837e1dSJohannes Berg 					qparam.txop = 1504/32;
1563aa837e1dSJohannes Berg 				qparam.aifs = 2;
1564aa837e1dSJohannes Berg 				break;
1565aa837e1dSJohannes Berg 			}
1566a8ce8544SStanislaw Gruszka 		}
1567e552af05SHaim Dreyfuss 		ieee80211_regulatory_limit_wmm_params(sdata, &qparam, ac);
15685825fe10SJohannes Berg 
1569ab13315aSKalle Valo 		qparam.uapsd = false;
1570ab13315aSKalle Valo 
157154bcbc69SJohannes Berg 		sdata->tx_conf[ac] = qparam;
157254bcbc69SJohannes Berg 		drv_conf_tx(local, sdata, ac, &qparam);
1573aa837e1dSJohannes Berg 	}
1574e1b3ec1aSStanislaw Gruszka 
1575f142c6b9SJohannes Berg 	if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1576708d50edSAyala Beker 	    sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1577708d50edSAyala Beker 	    sdata->vif.type != NL80211_IFTYPE_NAN) {
1578a8ce8544SStanislaw Gruszka 		sdata->vif.bss_conf.qos = enable_qos;
15793abead59SJohannes Berg 		if (bss_notify)
15803abead59SJohannes Berg 			ieee80211_bss_info_change_notify(sdata,
15813abead59SJohannes Berg 							 BSS_CHANGED_QOS);
15825825fe10SJohannes Berg 	}
1583d9734979SSujith }
1584e50db65cSJohannes Berg 
158546900298SJohannes Berg void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1586700e8ea6SJouni Malinen 			 u16 transaction, u16 auth_alg, u16 status,
15874a3cb702SJohannes Berg 			 const u8 *extra, size_t extra_len, const u8 *da,
15881672c0e3SJohannes Berg 			 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
15891672c0e3SJohannes Berg 			 u32 tx_flags)
159046900298SJohannes Berg {
159146900298SJohannes Berg 	struct ieee80211_local *local = sdata->local;
159246900298SJohannes Berg 	struct sk_buff *skb;
159346900298SJohannes Berg 	struct ieee80211_mgmt *mgmt;
1594fffd0934SJohannes Berg 	int err;
159546900298SJohannes Berg 
159615e230abSFred Zhou 	/* 24 + 6 = header + auth_algo + auth_transaction + status_code */
1597744462a9SMax Stepanov 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
1598744462a9SMax Stepanov 			    24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN);
1599d15b8459SJoe Perches 	if (!skb)
160046900298SJohannes Berg 		return;
1601d15b8459SJoe Perches 
1602744462a9SMax Stepanov 	skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
160346900298SJohannes Berg 
1604b080db58SJohannes Berg 	mgmt = skb_put_zero(skb, 24 + 6);
160546900298SJohannes Berg 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
160646900298SJohannes Berg 					  IEEE80211_STYPE_AUTH);
1607efa6a09dSAntonio Quartulli 	memcpy(mgmt->da, da, ETH_ALEN);
160847846c9bSJohannes Berg 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
160946900298SJohannes Berg 	memcpy(mgmt->bssid, bssid, ETH_ALEN);
161046900298SJohannes Berg 	mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
161146900298SJohannes Berg 	mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
1612700e8ea6SJouni Malinen 	mgmt->u.auth.status_code = cpu_to_le16(status);
161346900298SJohannes Berg 	if (extra)
161459ae1d12SJohannes Berg 		skb_put_data(skb, extra, extra_len);
161546900298SJohannes Berg 
1616fffd0934SJohannes Berg 	if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1617fffd0934SJohannes Berg 		mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1618fffd0934SJohannes Berg 		err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1619fffd0934SJohannes Berg 		WARN_ON(err);
1620fffd0934SJohannes Berg 	}
1621fffd0934SJohannes Berg 
16221672c0e3SJohannes Berg 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
16231672c0e3SJohannes Berg 					tx_flags;
162462ae67beSJohannes Berg 	ieee80211_tx_skb(sdata, skb);
162546900298SJohannes Berg }
162646900298SJohannes Berg 
16276ae16775SAntonio Quartulli void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
16284b08d1b6SJohannes Berg 				    const u8 *da, const u8 *bssid,
16294b08d1b6SJohannes Berg 				    u16 stype, u16 reason,
16306ae16775SAntonio Quartulli 				    bool send_frame, u8 *frame_buf)
16316ae16775SAntonio Quartulli {
16326ae16775SAntonio Quartulli 	struct ieee80211_local *local = sdata->local;
16336ae16775SAntonio Quartulli 	struct sk_buff *skb;
16346ae16775SAntonio Quartulli 	struct ieee80211_mgmt *mgmt = (void *)frame_buf;
16356ae16775SAntonio Quartulli 
16366ae16775SAntonio Quartulli 	/* build frame */
16376ae16775SAntonio Quartulli 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
16386ae16775SAntonio Quartulli 	mgmt->duration = 0; /* initialize only */
16396ae16775SAntonio Quartulli 	mgmt->seq_ctrl = 0; /* initialize only */
16404b08d1b6SJohannes Berg 	memcpy(mgmt->da, da, ETH_ALEN);
16416ae16775SAntonio Quartulli 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
16426ae16775SAntonio Quartulli 	memcpy(mgmt->bssid, bssid, ETH_ALEN);
16436ae16775SAntonio Quartulli 	/* u.deauth.reason_code == u.disassoc.reason_code */
16446ae16775SAntonio Quartulli 	mgmt->u.deauth.reason_code = cpu_to_le16(reason);
16456ae16775SAntonio Quartulli 
16466ae16775SAntonio Quartulli 	if (send_frame) {
16476ae16775SAntonio Quartulli 		skb = dev_alloc_skb(local->hw.extra_tx_headroom +
16486ae16775SAntonio Quartulli 				    IEEE80211_DEAUTH_FRAME_LEN);
16496ae16775SAntonio Quartulli 		if (!skb)
16506ae16775SAntonio Quartulli 			return;
16516ae16775SAntonio Quartulli 
16526ae16775SAntonio Quartulli 		skb_reserve(skb, local->hw.extra_tx_headroom);
16536ae16775SAntonio Quartulli 
16546ae16775SAntonio Quartulli 		/* copy in frame */
165559ae1d12SJohannes Berg 		skb_put_data(skb, mgmt, IEEE80211_DEAUTH_FRAME_LEN);
16566ae16775SAntonio Quartulli 
16576ae16775SAntonio Quartulli 		if (sdata->vif.type != NL80211_IFTYPE_STATION ||
16586ae16775SAntonio Quartulli 		    !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
16596ae16775SAntonio Quartulli 			IEEE80211_SKB_CB(skb)->flags |=
16606ae16775SAntonio Quartulli 				IEEE80211_TX_INTFL_DONT_ENCRYPT;
16616ae16775SAntonio Quartulli 
16626ae16775SAntonio Quartulli 		ieee80211_tx_skb(sdata, skb);
16636ae16775SAntonio Quartulli 	}
16646ae16775SAntonio Quartulli }
16656ae16775SAntonio Quartulli 
1666c56ef672SDavid Spinadel static int ieee80211_build_preq_ies_band(struct ieee80211_local *local,
1667c56ef672SDavid Spinadel 					 u8 *buffer, size_t buffer_len,
1668c56ef672SDavid Spinadel 					 const u8 *ie, size_t ie_len,
166957fbcce3SJohannes Berg 					 enum nl80211_band band,
1670c56ef672SDavid Spinadel 					 u32 rate_mask,
1671c56ef672SDavid Spinadel 					 struct cfg80211_chan_def *chandef,
167200387f32SJohannes Berg 					 size_t *offset, u32 flags)
1673de95a54bSJohannes Berg {
1674de95a54bSJohannes Berg 	struct ieee80211_supported_band *sband;
167541cbb0f5SLuca Coelho 	const struct ieee80211_sta_he_cap *he_cap;
1676c604b9f2SJohannes Berg 	u8 *pos = buffer, *end = buffer + buffer_len;
1677c56ef672SDavid Spinadel 	size_t noffset;
16788e664fb3SJohannes Berg 	int supp_rates_len, i;
16798dcb2003SJouni Malinen 	u8 rates[32];
16808dcb2003SJouni Malinen 	int num_rates;
16818dcb2003SJouni Malinen 	int ext_rates_len;
16822103dec1SSimon Wunderlich 	int shift;
16832103dec1SSimon Wunderlich 	u32 rate_flags;
168440a11ca8SJohannes Berg 	bool have_80mhz = false;
1685de95a54bSJohannes Berg 
1686c56ef672SDavid Spinadel 	*offset = 0;
1687c56ef672SDavid Spinadel 
16884d36ec58SJohannes Berg 	sband = local->hw.wiphy->bands[band];
1689d811b3d5SArik Nemtsov 	if (WARN_ON_ONCE(!sband))
1690d811b3d5SArik Nemtsov 		return 0;
1691de95a54bSJohannes Berg 
16922103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(chandef);
16932103dec1SSimon Wunderlich 	shift = ieee80211_chandef_get_shift(chandef);
16942103dec1SSimon Wunderlich 
16958dcb2003SJouni Malinen 	num_rates = 0;
16968dcb2003SJouni Malinen 	for (i = 0; i < sband->n_bitrates; i++) {
16978dcb2003SJouni Malinen 		if ((BIT(i) & rate_mask) == 0)
16988dcb2003SJouni Malinen 			continue; /* skip rate */
16992103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
17002103dec1SSimon Wunderlich 			continue;
17012103dec1SSimon Wunderlich 
17022103dec1SSimon Wunderlich 		rates[num_rates++] =
17032103dec1SSimon Wunderlich 			(u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
17042103dec1SSimon Wunderlich 					  (1 << shift) * 5);
17058dcb2003SJouni Malinen 	}
17068dcb2003SJouni Malinen 
17078dcb2003SJouni Malinen 	supp_rates_len = min_t(int, num_rates, 8);
17088e664fb3SJohannes Berg 
1709c604b9f2SJohannes Berg 	if (end - pos < 2 + supp_rates_len)
1710c604b9f2SJohannes Berg 		goto out_err;
1711de95a54bSJohannes Berg 	*pos++ = WLAN_EID_SUPP_RATES;
17128e664fb3SJohannes Berg 	*pos++ = supp_rates_len;
17138dcb2003SJouni Malinen 	memcpy(pos, rates, supp_rates_len);
17148dcb2003SJouni Malinen 	pos += supp_rates_len;
1715de95a54bSJohannes Berg 
17168e664fb3SJohannes Berg 	/* insert "request information" if in custom IEs */
17178e664fb3SJohannes Berg 	if (ie && ie_len) {
17188e664fb3SJohannes Berg 		static const u8 before_extrates[] = {
17198e664fb3SJohannes Berg 			WLAN_EID_SSID,
17208e664fb3SJohannes Berg 			WLAN_EID_SUPP_RATES,
17218e664fb3SJohannes Berg 			WLAN_EID_REQUEST,
17228e664fb3SJohannes Berg 		};
17238e664fb3SJohannes Berg 		noffset = ieee80211_ie_split(ie, ie_len,
17248e664fb3SJohannes Berg 					     before_extrates,
17258e664fb3SJohannes Berg 					     ARRAY_SIZE(before_extrates),
1726c56ef672SDavid Spinadel 					     *offset);
1727c56ef672SDavid Spinadel 		if (end - pos < noffset - *offset)
1728c604b9f2SJohannes Berg 			goto out_err;
1729c56ef672SDavid Spinadel 		memcpy(pos, ie + *offset, noffset - *offset);
1730c56ef672SDavid Spinadel 		pos += noffset - *offset;
1731c56ef672SDavid Spinadel 		*offset = noffset;
17328e664fb3SJohannes Berg 	}
17338e664fb3SJohannes Berg 
17348dcb2003SJouni Malinen 	ext_rates_len = num_rates - supp_rates_len;
17358dcb2003SJouni Malinen 	if (ext_rates_len > 0) {
1736c604b9f2SJohannes Berg 		if (end - pos < 2 + ext_rates_len)
1737c604b9f2SJohannes Berg 			goto out_err;
1738de95a54bSJohannes Berg 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
17398dcb2003SJouni Malinen 		*pos++ = ext_rates_len;
17408dcb2003SJouni Malinen 		memcpy(pos, rates + supp_rates_len, ext_rates_len);
17418dcb2003SJouni Malinen 		pos += ext_rates_len;
17428e664fb3SJohannes Berg 	}
17438e664fb3SJohannes Berg 
174457fbcce3SJohannes Berg 	if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
1745c604b9f2SJohannes Berg 		if (end - pos < 3)
1746c604b9f2SJohannes Berg 			goto out_err;
1747651b5225SJouni Malinen 		*pos++ = WLAN_EID_DS_PARAMS;
1748651b5225SJouni Malinen 		*pos++ = 1;
17492103dec1SSimon Wunderlich 		*pos++ = ieee80211_frequency_to_channel(
17502103dec1SSimon Wunderlich 				chandef->chan->center_freq);
1751651b5225SJouni Malinen 	}
1752651b5225SJouni Malinen 
1753b9771d41SJohannes Berg 	if (flags & IEEE80211_PROBE_FLAG_MIN_CONTENT)
1754b9771d41SJohannes Berg 		goto done;
1755b9771d41SJohannes Berg 
17568e664fb3SJohannes Berg 	/* insert custom IEs that go before HT */
17578e664fb3SJohannes Berg 	if (ie && ie_len) {
17588e664fb3SJohannes Berg 		static const u8 before_ht[] = {
1759a7f26d80SJohannes Berg 			/*
1760a7f26d80SJohannes Berg 			 * no need to list the ones split off already
1761a7f26d80SJohannes Berg 			 * (or generated here)
1762a7f26d80SJohannes Berg 			 */
17638e664fb3SJohannes Berg 			WLAN_EID_DS_PARAMS,
17648e664fb3SJohannes Berg 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
17658e664fb3SJohannes Berg 		};
17668e664fb3SJohannes Berg 		noffset = ieee80211_ie_split(ie, ie_len,
17678e664fb3SJohannes Berg 					     before_ht, ARRAY_SIZE(before_ht),
1768c56ef672SDavid Spinadel 					     *offset);
1769c56ef672SDavid Spinadel 		if (end - pos < noffset - *offset)
1770c604b9f2SJohannes Berg 			goto out_err;
1771c56ef672SDavid Spinadel 		memcpy(pos, ie + *offset, noffset - *offset);
1772c56ef672SDavid Spinadel 		pos += noffset - *offset;
1773c56ef672SDavid Spinadel 		*offset = noffset;
1774de95a54bSJohannes Berg 	}
1775de95a54bSJohannes Berg 
1776c604b9f2SJohannes Berg 	if (sband->ht_cap.ht_supported) {
1777c604b9f2SJohannes Berg 		if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
1778c604b9f2SJohannes Berg 			goto out_err;
1779ef96a842SBen Greear 		pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
1780ef96a842SBen Greear 						sband->ht_cap.cap);
1781c604b9f2SJohannes Berg 	}
17825ef2d41aSJohannes Berg 
17834d952300SJohannes Berg 	/* insert custom IEs that go before VHT */
17848e664fb3SJohannes Berg 	if (ie && ie_len) {
17854d952300SJohannes Berg 		static const u8 before_vht[] = {
1786a7f26d80SJohannes Berg 			/*
1787a7f26d80SJohannes Berg 			 * no need to list the ones split off already
1788a7f26d80SJohannes Berg 			 * (or generated here)
1789a7f26d80SJohannes Berg 			 */
17904d952300SJohannes Berg 			WLAN_EID_BSS_COEX_2040,
17914d952300SJohannes Berg 			WLAN_EID_EXT_CAPABILITY,
17924d952300SJohannes Berg 			WLAN_EID_SSID_LIST,
17934d952300SJohannes Berg 			WLAN_EID_CHANNEL_USAGE,
17944d952300SJohannes Berg 			WLAN_EID_INTERWORKING,
1795b44eebeaSLiad Kaufman 			WLAN_EID_MESH_ID,
1796a7f26d80SJohannes Berg 			/* 60 GHz (Multi-band, DMG, MMS) can't happen */
17974d952300SJohannes Berg 		};
17984d952300SJohannes Berg 		noffset = ieee80211_ie_split(ie, ie_len,
17994d952300SJohannes Berg 					     before_vht, ARRAY_SIZE(before_vht),
1800c56ef672SDavid Spinadel 					     *offset);
1801c56ef672SDavid Spinadel 		if (end - pos < noffset - *offset)
1802c604b9f2SJohannes Berg 			goto out_err;
1803c56ef672SDavid Spinadel 		memcpy(pos, ie + *offset, noffset - *offset);
1804c56ef672SDavid Spinadel 		pos += noffset - *offset;
1805c56ef672SDavid Spinadel 		*offset = noffset;
1806de95a54bSJohannes Berg 	}
1807de95a54bSJohannes Berg 
180840a11ca8SJohannes Berg 	/* Check if any channel in this sband supports at least 80 MHz */
180940a11ca8SJohannes Berg 	for (i = 0; i < sband->n_channels; i++) {
1810fa44b988SArik Nemtsov 		if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
1811fa44b988SArik Nemtsov 						IEEE80211_CHAN_NO_80MHZ))
1812fa44b988SArik Nemtsov 			continue;
1813fa44b988SArik Nemtsov 
181440a11ca8SJohannes Berg 		have_80mhz = true;
181540a11ca8SJohannes Berg 		break;
181640a11ca8SJohannes Berg 	}
181740a11ca8SJohannes Berg 
181840a11ca8SJohannes Berg 	if (sband->vht_cap.vht_supported && have_80mhz) {
1819c604b9f2SJohannes Berg 		if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
1820c604b9f2SJohannes Berg 			goto out_err;
1821ba0afa2fSMahesh Palivela 		pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
1822ba0afa2fSMahesh Palivela 						 sband->vht_cap.cap);
1823c604b9f2SJohannes Berg 	}
1824ba0afa2fSMahesh Palivela 
182541cbb0f5SLuca Coelho 	/* insert custom IEs that go before HE */
182641cbb0f5SLuca Coelho 	if (ie && ie_len) {
182741cbb0f5SLuca Coelho 		static const u8 before_he[] = {
182841cbb0f5SLuca Coelho 			/*
182941cbb0f5SLuca Coelho 			 * no need to list the ones split off before VHT
183041cbb0f5SLuca Coelho 			 * or generated here
183141cbb0f5SLuca Coelho 			 */
183241cbb0f5SLuca Coelho 			WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_REQ_PARAMS,
183341cbb0f5SLuca Coelho 			WLAN_EID_AP_CSN,
183441cbb0f5SLuca Coelho 			/* TODO: add 11ah/11aj/11ak elements */
183541cbb0f5SLuca Coelho 		};
183641cbb0f5SLuca Coelho 		noffset = ieee80211_ie_split(ie, ie_len,
183741cbb0f5SLuca Coelho 					     before_he, ARRAY_SIZE(before_he),
183841cbb0f5SLuca Coelho 					     *offset);
183941cbb0f5SLuca Coelho 		if (end - pos < noffset - *offset)
184041cbb0f5SLuca Coelho 			goto out_err;
184141cbb0f5SLuca Coelho 		memcpy(pos, ie + *offset, noffset - *offset);
184241cbb0f5SLuca Coelho 		pos += noffset - *offset;
184341cbb0f5SLuca Coelho 		*offset = noffset;
184441cbb0f5SLuca Coelho 	}
184541cbb0f5SLuca Coelho 
184641cbb0f5SLuca Coelho 	he_cap = ieee80211_get_he_sta_cap(sband);
184741cbb0f5SLuca Coelho 	if (he_cap) {
184841cbb0f5SLuca Coelho 		pos = ieee80211_ie_build_he_cap(pos, he_cap, end);
184941cbb0f5SLuca Coelho 		if (!pos)
185041cbb0f5SLuca Coelho 			goto out_err;
185141cbb0f5SLuca Coelho 	}
185241cbb0f5SLuca Coelho 
185341cbb0f5SLuca Coelho 	/*
185441cbb0f5SLuca Coelho 	 * If adding more here, adjust code in main.c
185541cbb0f5SLuca Coelho 	 * that calculates local->scan_ies_len.
185641cbb0f5SLuca Coelho 	 */
185741cbb0f5SLuca Coelho 
1858de95a54bSJohannes Berg 	return pos - buffer;
1859c604b9f2SJohannes Berg  out_err:
1860c604b9f2SJohannes Berg 	WARN_ONCE(1, "not enough space for preq IEs\n");
1861b9771d41SJohannes Berg  done:
1862c604b9f2SJohannes Berg 	return pos - buffer;
1863de95a54bSJohannes Berg }
1864de95a54bSJohannes Berg 
1865c56ef672SDavid Spinadel int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
1866c56ef672SDavid Spinadel 			     size_t buffer_len,
1867c56ef672SDavid Spinadel 			     struct ieee80211_scan_ies *ie_desc,
1868c56ef672SDavid Spinadel 			     const u8 *ie, size_t ie_len,
1869c56ef672SDavid Spinadel 			     u8 bands_used, u32 *rate_masks,
187000387f32SJohannes Berg 			     struct cfg80211_chan_def *chandef,
187100387f32SJohannes Berg 			     u32 flags)
1872c56ef672SDavid Spinadel {
1873c56ef672SDavid Spinadel 	size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
1874c56ef672SDavid Spinadel 	int i;
1875c56ef672SDavid Spinadel 
1876c56ef672SDavid Spinadel 	memset(ie_desc, 0, sizeof(*ie_desc));
1877c56ef672SDavid Spinadel 
187857fbcce3SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
1879c56ef672SDavid Spinadel 		if (bands_used & BIT(i)) {
1880c56ef672SDavid Spinadel 			pos += ieee80211_build_preq_ies_band(local,
1881c56ef672SDavid Spinadel 							     buffer + pos,
1882c56ef672SDavid Spinadel 							     buffer_len - pos,
1883c56ef672SDavid Spinadel 							     ie, ie_len, i,
1884c56ef672SDavid Spinadel 							     rate_masks[i],
1885c56ef672SDavid Spinadel 							     chandef,
188600387f32SJohannes Berg 							     &custom_ie_offset,
188700387f32SJohannes Berg 							     flags);
1888c56ef672SDavid Spinadel 			ie_desc->ies[i] = buffer + old_pos;
1889c56ef672SDavid Spinadel 			ie_desc->len[i] = pos - old_pos;
1890c56ef672SDavid Spinadel 			old_pos = pos;
1891c56ef672SDavid Spinadel 		}
1892c56ef672SDavid Spinadel 	}
1893c56ef672SDavid Spinadel 
1894c56ef672SDavid Spinadel 	/* add any remaining custom IEs */
1895c56ef672SDavid Spinadel 	if (ie && ie_len) {
1896c56ef672SDavid Spinadel 		if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
1897c56ef672SDavid Spinadel 			      "not enough space for preq custom IEs\n"))
1898c56ef672SDavid Spinadel 			return pos;
1899c56ef672SDavid Spinadel 		memcpy(buffer + pos, ie + custom_ie_offset,
1900c56ef672SDavid Spinadel 		       ie_len - custom_ie_offset);
1901c56ef672SDavid Spinadel 		ie_desc->common_ies = buffer + pos;
1902c56ef672SDavid Spinadel 		ie_desc->common_ie_len = ie_len - custom_ie_offset;
1903c56ef672SDavid Spinadel 		pos += ie_len - custom_ie_offset;
1904c56ef672SDavid Spinadel 	}
1905c56ef672SDavid Spinadel 
1906c56ef672SDavid Spinadel 	return pos;
1907c56ef672SDavid Spinadel };
1908c56ef672SDavid Spinadel 
1909a619a4c0SJuuso Oikarinen struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
1910a344d677SJohannes Berg 					  const u8 *src, const u8 *dst,
1911a344d677SJohannes Berg 					  u32 ratemask,
19126b77863bSJohannes Berg 					  struct ieee80211_channel *chan,
1913de95a54bSJohannes Berg 					  const u8 *ssid, size_t ssid_len,
1914a806c558SPaul Stewart 					  const u8 *ie, size_t ie_len,
191500387f32SJohannes Berg 					  u32 flags)
191646900298SJohannes Berg {
191746900298SJohannes Berg 	struct ieee80211_local *local = sdata->local;
19182103dec1SSimon Wunderlich 	struct cfg80211_chan_def chandef;
191946900298SJohannes Berg 	struct sk_buff *skb;
192046900298SJohannes Berg 	struct ieee80211_mgmt *mgmt;
1921b9a9ada1SJohannes Berg 	int ies_len;
192257fbcce3SJohannes Berg 	u32 rate_masks[NUM_NL80211_BANDS] = {};
1923c56ef672SDavid Spinadel 	struct ieee80211_scan_ies dummy_ie_desc;
192446900298SJohannes Berg 
1925a806c558SPaul Stewart 	/*
1926a806c558SPaul Stewart 	 * Do not send DS Channel parameter for directed probe requests
1927a806c558SPaul Stewart 	 * in order to maximize the chance that we get a response.  Some
1928a806c558SPaul Stewart 	 * badly-behaved APs don't respond when this parameter is included.
1929a806c558SPaul Stewart 	 */
19302103dec1SSimon Wunderlich 	chandef.width = sdata->vif.bss_conf.chandef.width;
193100387f32SJohannes Berg 	if (flags & IEEE80211_PROBE_FLAG_DIRECTED)
19322103dec1SSimon Wunderlich 		chandef.chan = NULL;
1933a806c558SPaul Stewart 	else
19342103dec1SSimon Wunderlich 		chandef.chan = chan;
1935651b5225SJouni Malinen 
1936a344d677SJohannes Berg 	skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
1937a344d677SJohannes Berg 				     100 + ie_len);
19385b2bbf75SJohannes Berg 	if (!skb)
1939b9a9ada1SJohannes Berg 		return NULL;
1940b9a9ada1SJohannes Berg 
1941c56ef672SDavid Spinadel 	rate_masks[chan->band] = ratemask;
1942b9a9ada1SJohannes Berg 	ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
1943c56ef672SDavid Spinadel 					   skb_tailroom(skb), &dummy_ie_desc,
1944c56ef672SDavid Spinadel 					   ie, ie_len, BIT(chan->band),
194500387f32SJohannes Berg 					   rate_masks, &chandef, flags);
1946b9a9ada1SJohannes Berg 	skb_put(skb, ies_len);
19477c12ce8bSKalle Valo 
194846900298SJohannes Berg 	if (dst) {
19497c12ce8bSKalle Valo 		mgmt = (struct ieee80211_mgmt *) skb->data;
195046900298SJohannes Berg 		memcpy(mgmt->da, dst, ETH_ALEN);
195146900298SJohannes Berg 		memcpy(mgmt->bssid, dst, ETH_ALEN);
195246900298SJohannes Berg 	}
195346900298SJohannes Berg 
195462ae67beSJohannes Berg 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
19555b2bbf75SJohannes Berg 
1956a619a4c0SJuuso Oikarinen 	return skb;
1957a619a4c0SJuuso Oikarinen }
1958a619a4c0SJuuso Oikarinen 
19592103dec1SSimon Wunderlich u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
196046900298SJohannes Berg 			    struct ieee802_11_elems *elems,
196157fbcce3SJohannes Berg 			    enum nl80211_band band, u32 *basic_rates)
196246900298SJohannes Berg {
196346900298SJohannes Berg 	struct ieee80211_supported_band *sband;
196446900298SJohannes Berg 	size_t num_rates;
19652103dec1SSimon Wunderlich 	u32 supp_rates, rate_flags;
19662103dec1SSimon Wunderlich 	int i, j, shift;
196721a8e9ddSMohammed Shafi Shajakhan 
19682103dec1SSimon Wunderlich 	sband = sdata->local->hw.wiphy->bands[band];
196921a8e9ddSMohammed Shafi Shajakhan 	if (WARN_ON(!sband))
197021a8e9ddSMohammed Shafi Shajakhan 		return 1;
19712103dec1SSimon Wunderlich 
19722103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
19732103dec1SSimon Wunderlich 	shift = ieee80211_vif_get_shift(&sdata->vif);
197446900298SJohannes Berg 
197546900298SJohannes Berg 	num_rates = sband->n_bitrates;
197646900298SJohannes Berg 	supp_rates = 0;
197746900298SJohannes Berg 	for (i = 0; i < elems->supp_rates_len +
197846900298SJohannes Berg 		     elems->ext_supp_rates_len; i++) {
197946900298SJohannes Berg 		u8 rate = 0;
198046900298SJohannes Berg 		int own_rate;
19819ebb61a2SAshok Nagarajan 		bool is_basic;
198246900298SJohannes Berg 		if (i < elems->supp_rates_len)
198346900298SJohannes Berg 			rate = elems->supp_rates[i];
198446900298SJohannes Berg 		else if (elems->ext_supp_rates)
198546900298SJohannes Berg 			rate = elems->ext_supp_rates
198646900298SJohannes Berg 				[i - elems->supp_rates_len];
198746900298SJohannes Berg 		own_rate = 5 * (rate & 0x7f);
19889ebb61a2SAshok Nagarajan 		is_basic = !!(rate & 0x80);
19899ebb61a2SAshok Nagarajan 
19909ebb61a2SAshok Nagarajan 		if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
19919ebb61a2SAshok Nagarajan 			continue;
19929ebb61a2SAshok Nagarajan 
19939ebb61a2SAshok Nagarajan 		for (j = 0; j < num_rates; j++) {
19942103dec1SSimon Wunderlich 			int brate;
19952103dec1SSimon Wunderlich 			if ((rate_flags & sband->bitrates[j].flags)
19962103dec1SSimon Wunderlich 			    != rate_flags)
19972103dec1SSimon Wunderlich 				continue;
19982103dec1SSimon Wunderlich 
19992103dec1SSimon Wunderlich 			brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
20002103dec1SSimon Wunderlich 					     1 << shift);
20012103dec1SSimon Wunderlich 
20022103dec1SSimon Wunderlich 			if (brate == own_rate) {
200346900298SJohannes Berg 				supp_rates |= BIT(j);
20049ebb61a2SAshok Nagarajan 				if (basic_rates && is_basic)
20059ebb61a2SAshok Nagarajan 					*basic_rates |= BIT(j);
20069ebb61a2SAshok Nagarajan 			}
20079ebb61a2SAshok Nagarajan 		}
200846900298SJohannes Berg 	}
200946900298SJohannes Berg 	return supp_rates;
201046900298SJohannes Berg }
2011f2753ddbSJohannes Berg 
201284f6a01cSJohannes Berg void ieee80211_stop_device(struct ieee80211_local *local)
201384f6a01cSJohannes Berg {
201484f6a01cSJohannes Berg 	ieee80211_led_radio(local, false);
201567408c8cSJohannes Berg 	ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
201684f6a01cSJohannes Berg 
201784f6a01cSJohannes Berg 	cancel_work_sync(&local->reconfig_filter);
201884f6a01cSJohannes Berg 
201984f6a01cSJohannes Berg 	flush_workqueue(local->workqueue);
2020678f415fSLennert Buytenhek 	drv_stop(local);
202184f6a01cSJohannes Berg }
202284f6a01cSJohannes Berg 
202374430f94SJohannes Berg static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
202474430f94SJohannes Berg 					   bool aborted)
202574430f94SJohannes Berg {
202674430f94SJohannes Berg 	/* It's possible that we don't handle the scan completion in
202774430f94SJohannes Berg 	 * time during suspend, so if it's still marked as completed
202874430f94SJohannes Berg 	 * here, queue the work and flush it to clean things up.
202974430f94SJohannes Berg 	 * Instead of calling the worker function directly here, we
203074430f94SJohannes Berg 	 * really queue it to avoid potential races with other flows
203174430f94SJohannes Berg 	 * scheduling the same work.
203274430f94SJohannes Berg 	 */
203374430f94SJohannes Berg 	if (test_bit(SCAN_COMPLETED, &local->scanning)) {
203474430f94SJohannes Berg 		/* If coming from reconfiguration failure, abort the scan so
203574430f94SJohannes Berg 		 * we don't attempt to continue a partial HW scan - which is
203674430f94SJohannes Berg 		 * possible otherwise if (e.g.) the 2.4 GHz portion was the
203774430f94SJohannes Berg 		 * completed scan, and a 5 GHz portion is still pending.
203874430f94SJohannes Berg 		 */
203974430f94SJohannes Berg 		if (aborted)
204074430f94SJohannes Berg 			set_bit(SCAN_ABORTED, &local->scanning);
204174430f94SJohannes Berg 		ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
204274430f94SJohannes Berg 		flush_delayed_work(&local->scan_work);
204374430f94SJohannes Berg 	}
204474430f94SJohannes Berg }
204574430f94SJohannes Berg 
2046f6837ba8SJohannes Berg static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
2047f6837ba8SJohannes Berg {
2048f6837ba8SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
2049f6837ba8SJohannes Berg 	struct ieee80211_chanctx *ctx;
2050f6837ba8SJohannes Berg 
2051f6837ba8SJohannes Berg 	/*
2052f6837ba8SJohannes Berg 	 * We get here if during resume the device can't be restarted properly.
2053f6837ba8SJohannes Berg 	 * We might also get here if this happens during HW reset, which is a
2054f6837ba8SJohannes Berg 	 * slightly different situation and we need to drop all connections in
2055f6837ba8SJohannes Berg 	 * the latter case.
2056f6837ba8SJohannes Berg 	 *
2057f6837ba8SJohannes Berg 	 * Ask cfg80211 to turn off all interfaces, this will result in more
2058f6837ba8SJohannes Berg 	 * warnings but at least we'll then get into a clean stopped state.
2059f6837ba8SJohannes Berg 	 */
2060f6837ba8SJohannes Berg 
2061f6837ba8SJohannes Berg 	local->resuming = false;
2062f6837ba8SJohannes Berg 	local->suspended = false;
20637584f88fSEliad Peller 	local->in_reconfig = false;
2064f6837ba8SJohannes Berg 
206574430f94SJohannes Berg 	ieee80211_flush_completed_scan(local, true);
206674430f94SJohannes Berg 
2067f6837ba8SJohannes Berg 	/* scheduled scan clearly can't be running any more, but tell
2068f6837ba8SJohannes Berg 	 * cfg80211 and clear local state
2069f6837ba8SJohannes Berg 	 */
2070f6837ba8SJohannes Berg 	ieee80211_sched_scan_end(local);
2071f6837ba8SJohannes Berg 
2072f6837ba8SJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list)
2073f6837ba8SJohannes Berg 		sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
2074f6837ba8SJohannes Berg 
2075f6837ba8SJohannes Berg 	/* Mark channel contexts as not being in the driver any more to avoid
2076f6837ba8SJohannes Berg 	 * removing them from the driver during the shutdown process...
2077f6837ba8SJohannes Berg 	 */
2078f6837ba8SJohannes Berg 	mutex_lock(&local->chanctx_mtx);
2079f6837ba8SJohannes Berg 	list_for_each_entry(ctx, &local->chanctx_list, list)
2080f6837ba8SJohannes Berg 		ctx->driver_present = false;
2081f6837ba8SJohannes Berg 	mutex_unlock(&local->chanctx_mtx);
2082f6837ba8SJohannes Berg 
2083f6837ba8SJohannes Berg 	cfg80211_shutdown_all_interfaces(local->hw.wiphy);
2084f6837ba8SJohannes Berg }
2085f6837ba8SJohannes Berg 
2086153a5fc4SStanislaw Gruszka static void ieee80211_assign_chanctx(struct ieee80211_local *local,
2087153a5fc4SStanislaw Gruszka 				     struct ieee80211_sub_if_data *sdata)
2088153a5fc4SStanislaw Gruszka {
2089153a5fc4SStanislaw Gruszka 	struct ieee80211_chanctx_conf *conf;
2090153a5fc4SStanislaw Gruszka 	struct ieee80211_chanctx *ctx;
2091153a5fc4SStanislaw Gruszka 
2092153a5fc4SStanislaw Gruszka 	if (!local->use_chanctx)
2093153a5fc4SStanislaw Gruszka 		return;
2094153a5fc4SStanislaw Gruszka 
2095153a5fc4SStanislaw Gruszka 	mutex_lock(&local->chanctx_mtx);
2096153a5fc4SStanislaw Gruszka 	conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
2097153a5fc4SStanislaw Gruszka 					 lockdep_is_held(&local->chanctx_mtx));
2098153a5fc4SStanislaw Gruszka 	if (conf) {
2099153a5fc4SStanislaw Gruszka 		ctx = container_of(conf, struct ieee80211_chanctx, conf);
2100153a5fc4SStanislaw Gruszka 		drv_assign_vif_chanctx(local, sdata, ctx);
2101153a5fc4SStanislaw Gruszka 	}
2102153a5fc4SStanislaw Gruszka 	mutex_unlock(&local->chanctx_mtx);
2103153a5fc4SStanislaw Gruszka }
2104153a5fc4SStanislaw Gruszka 
21051ea2c864SJohannes Berg static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
21061ea2c864SJohannes Berg {
21071ea2c864SJohannes Berg 	struct ieee80211_local *local = sdata->local;
21081ea2c864SJohannes Berg 	struct sta_info *sta;
21091ea2c864SJohannes Berg 
21101ea2c864SJohannes Berg 	/* add STAs back */
21111ea2c864SJohannes Berg 	mutex_lock(&local->sta_mtx);
21121ea2c864SJohannes Berg 	list_for_each_entry(sta, &local->sta_list, list) {
21131ea2c864SJohannes Berg 		enum ieee80211_sta_state state;
21141ea2c864SJohannes Berg 
21151ea2c864SJohannes Berg 		if (!sta->uploaded || sta->sdata != sdata)
21161ea2c864SJohannes Berg 			continue;
21171ea2c864SJohannes Berg 
21181ea2c864SJohannes Berg 		for (state = IEEE80211_STA_NOTEXIST;
21191ea2c864SJohannes Berg 		     state < sta->sta_state; state++)
21201ea2c864SJohannes Berg 			WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
21211ea2c864SJohannes Berg 					      state + 1));
21221ea2c864SJohannes Berg 	}
21231ea2c864SJohannes Berg 	mutex_unlock(&local->sta_mtx);
21241ea2c864SJohannes Berg }
21251ea2c864SJohannes Berg 
2126167e33f4SAyala Beker static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
2127167e33f4SAyala Beker {
2128167e33f4SAyala Beker 	struct cfg80211_nan_func *func, **funcs;
2129167e33f4SAyala Beker 	int res, id, i = 0;
2130167e33f4SAyala Beker 
2131167e33f4SAyala Beker 	res = drv_start_nan(sdata->local, sdata,
2132167e33f4SAyala Beker 			    &sdata->u.nan.conf);
2133167e33f4SAyala Beker 	if (WARN_ON(res))
2134167e33f4SAyala Beker 		return res;
2135167e33f4SAyala Beker 
21366396bb22SKees Cook 	funcs = kcalloc(sdata->local->hw.max_nan_de_entries + 1,
21376396bb22SKees Cook 			sizeof(*funcs),
21386396bb22SKees Cook 			GFP_KERNEL);
2139167e33f4SAyala Beker 	if (!funcs)
2140167e33f4SAyala Beker 		return -ENOMEM;
2141167e33f4SAyala Beker 
2142167e33f4SAyala Beker 	/* Add all the functions:
2143167e33f4SAyala Beker 	 * This is a little bit ugly. We need to call a potentially sleeping
2144167e33f4SAyala Beker 	 * callback for each NAN function, so we can't hold the spinlock.
2145167e33f4SAyala Beker 	 */
2146167e33f4SAyala Beker 	spin_lock_bh(&sdata->u.nan.func_lock);
2147167e33f4SAyala Beker 
2148167e33f4SAyala Beker 	idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
2149167e33f4SAyala Beker 		funcs[i++] = func;
2150167e33f4SAyala Beker 
2151167e33f4SAyala Beker 	spin_unlock_bh(&sdata->u.nan.func_lock);
2152167e33f4SAyala Beker 
2153167e33f4SAyala Beker 	for (i = 0; funcs[i]; i++) {
2154167e33f4SAyala Beker 		res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
2155167e33f4SAyala Beker 		if (WARN_ON(res))
2156167e33f4SAyala Beker 			ieee80211_nan_func_terminated(&sdata->vif,
2157167e33f4SAyala Beker 						      funcs[i]->instance_id,
2158167e33f4SAyala Beker 						      NL80211_NAN_FUNC_TERM_REASON_ERROR,
2159167e33f4SAyala Beker 						      GFP_KERNEL);
2160167e33f4SAyala Beker 	}
2161167e33f4SAyala Beker 
2162167e33f4SAyala Beker 	kfree(funcs);
2163167e33f4SAyala Beker 
2164167e33f4SAyala Beker 	return 0;
2165167e33f4SAyala Beker }
2166167e33f4SAyala Beker 
2167f2753ddbSJohannes Berg int ieee80211_reconfig(struct ieee80211_local *local)
2168f2753ddbSJohannes Berg {
2169f2753ddbSJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
2170f2753ddbSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
217155de908aSJohannes Berg 	struct ieee80211_chanctx *ctx;
2172f2753ddbSJohannes Berg 	struct sta_info *sta;
21732683d65bSEliad Peller 	int res, i;
2174d888130aSJohannes Berg 	bool reconfig_due_to_wowlan = false;
2175d43c6b6eSDavid Spinadel 	struct ieee80211_sub_if_data *sched_scan_sdata;
21766ea0a69cSJohannes Berg 	struct cfg80211_sched_scan_request *sched_scan_req;
2177d43c6b6eSDavid Spinadel 	bool sched_scan_stopped = false;
2178b0485e9fSEliad Peller 	bool suspended = local->suspended;
2179d888130aSJohannes Berg 
21800f8b8245SEliad Peller 	/* nothing to do if HW shouldn't run */
21810f8b8245SEliad Peller 	if (!local->open_count)
21820f8b8245SEliad Peller 		goto wake_up;
21830f8b8245SEliad Peller 
21848f21b0adSJohannes Berg #ifdef CONFIG_PM
2185b0485e9fSEliad Peller 	if (suspended)
2186ceb99fe0SJohannes Berg 		local->resuming = true;
2187f2753ddbSJohannes Berg 
2188eecc4800SJohannes Berg 	if (local->wowlan) {
2189b0485e9fSEliad Peller 		/*
2190b0485e9fSEliad Peller 		 * In the wowlan case, both mac80211 and the device
2191b0485e9fSEliad Peller 		 * are functional when the resume op is called, so
2192b0485e9fSEliad Peller 		 * clear local->suspended so the device could operate
2193b0485e9fSEliad Peller 		 * normally (e.g. pass rx frames).
2194b0485e9fSEliad Peller 		 */
2195b0485e9fSEliad Peller 		local->suspended = false;
2196eecc4800SJohannes Berg 		res = drv_resume(local);
219727b3eb9cSJohannes Berg 		local->wowlan = false;
2198eecc4800SJohannes Berg 		if (res < 0) {
2199eecc4800SJohannes Berg 			local->resuming = false;
2200eecc4800SJohannes Berg 			return res;
2201eecc4800SJohannes Berg 		}
2202eecc4800SJohannes Berg 		if (res == 0)
2203eecc4800SJohannes Berg 			goto wake_up;
2204eecc4800SJohannes Berg 		WARN_ON(res > 1);
2205eecc4800SJohannes Berg 		/*
2206eecc4800SJohannes Berg 		 * res is 1, which means the driver requested
2207eecc4800SJohannes Berg 		 * to go through a regular reset on wakeup.
2208b0485e9fSEliad Peller 		 * restore local->suspended in this case.
2209eecc4800SJohannes Berg 		 */
2210d888130aSJohannes Berg 		reconfig_due_to_wowlan = true;
2211b0485e9fSEliad Peller 		local->suspended = true;
2212eecc4800SJohannes Berg 	}
2213eecc4800SJohannes Berg #endif
221494f9b97bSJohannes Berg 
221524feda00SLuis R. Rodriguez 	/*
221643d6df00SEliad Peller 	 * In case of hw_restart during suspend (without wowlan),
221743d6df00SEliad Peller 	 * cancel restart work, as we are reconfiguring the device
221843d6df00SEliad Peller 	 * anyway.
221943d6df00SEliad Peller 	 * Note that restart_work is scheduled on a frozen workqueue,
222043d6df00SEliad Peller 	 * so we can't deadlock in this case.
222143d6df00SEliad Peller 	 */
222243d6df00SEliad Peller 	if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
222343d6df00SEliad Peller 		cancel_work_sync(&local->restart_work);
222443d6df00SEliad Peller 
2225968a76ceSEliad Peller 	local->started = false;
2226968a76ceSEliad Peller 
222743d6df00SEliad Peller 	/*
222824feda00SLuis R. Rodriguez 	 * Upon resume hardware can sometimes be goofy due to
222924feda00SLuis R. Rodriguez 	 * various platform / driver / bus issues, so restarting
223024feda00SLuis R. Rodriguez 	 * the device may at times not work immediately. Propagate
223124feda00SLuis R. Rodriguez 	 * the error.
223224feda00SLuis R. Rodriguez 	 */
223324487981SJohannes Berg 	res = drv_start(local);
223424feda00SLuis R. Rodriguez 	if (res) {
2235b0485e9fSEliad Peller 		if (suspended)
2236f6837ba8SJohannes Berg 			WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
2237f6837ba8SJohannes Berg 		else
2238f6837ba8SJohannes Berg 			WARN(1, "Hardware became unavailable during restart.\n");
2239f6837ba8SJohannes Berg 		ieee80211_handle_reconfig_failure(local);
224024feda00SLuis R. Rodriguez 		return res;
224124feda00SLuis R. Rodriguez 	}
2242f2753ddbSJohannes Berg 
22437f281975SYogesh Ashok Powar 	/* setup fragmentation threshold */
22447f281975SYogesh Ashok Powar 	drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
22457f281975SYogesh Ashok Powar 
22467f281975SYogesh Ashok Powar 	/* setup RTS threshold */
22477f281975SYogesh Ashok Powar 	drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
22487f281975SYogesh Ashok Powar 
22497f281975SYogesh Ashok Powar 	/* reset coverage class */
22507f281975SYogesh Ashok Powar 	drv_set_coverage_class(local, hw->wiphy->coverage_class);
22517f281975SYogesh Ashok Powar 
22521f87f7d3SJohannes Berg 	ieee80211_led_radio(local, true);
225367408c8cSJohannes Berg 	ieee80211_mod_tpt_led_trig(local,
225467408c8cSJohannes Berg 				   IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
2255f2753ddbSJohannes Berg 
2256f2753ddbSJohannes Berg 	/* add interfaces */
22574b6f1dd6SJohannes Berg 	sdata = rtnl_dereference(local->monitor_sdata);
22584b6f1dd6SJohannes Berg 	if (sdata) {
22593c3e21e7SJohannes Berg 		/* in HW restart it exists already */
22603c3e21e7SJohannes Berg 		WARN_ON(local->resuming);
22614b6f1dd6SJohannes Berg 		res = drv_add_interface(local, sdata);
22624b6f1dd6SJohannes Berg 		if (WARN_ON(res)) {
22630c2bef46SMonam Agarwal 			RCU_INIT_POINTER(local->monitor_sdata, NULL);
22644b6f1dd6SJohannes Berg 			synchronize_net();
22654b6f1dd6SJohannes Berg 			kfree(sdata);
22664b6f1dd6SJohannes Berg 		}
22674b6f1dd6SJohannes Berg 	}
22684b6f1dd6SJohannes Berg 
2269f2753ddbSJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
2270f2753ddbSJohannes Berg 		if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2271f2753ddbSJohannes Berg 		    sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2272c8fff3dcSLuciano Coelho 		    ieee80211_sdata_running(sdata)) {
22737b7eab6fSJohannes Berg 			res = drv_add_interface(local, sdata);
2274c8fff3dcSLuciano Coelho 			if (WARN_ON(res))
2275c8fff3dcSLuciano Coelho 				break;
2276c8fff3dcSLuciano Coelho 		}
2277c8fff3dcSLuciano Coelho 	}
2278c8fff3dcSLuciano Coelho 
2279c8fff3dcSLuciano Coelho 	/* If adding any of the interfaces failed above, roll back and
2280c8fff3dcSLuciano Coelho 	 * report failure.
2281c8fff3dcSLuciano Coelho 	 */
2282c8fff3dcSLuciano Coelho 	if (res) {
2283c8fff3dcSLuciano Coelho 		list_for_each_entry_continue_reverse(sdata, &local->interfaces,
2284c8fff3dcSLuciano Coelho 						     list)
2285c8fff3dcSLuciano Coelho 			if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2286c8fff3dcSLuciano Coelho 			    sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2287c8fff3dcSLuciano Coelho 			    ieee80211_sdata_running(sdata))
2288c8fff3dcSLuciano Coelho 				drv_remove_interface(local, sdata);
2289c8fff3dcSLuciano Coelho 		ieee80211_handle_reconfig_failure(local);
2290c8fff3dcSLuciano Coelho 		return res;
2291f2753ddbSJohannes Berg 	}
2292f2753ddbSJohannes Berg 
229355de908aSJohannes Berg 	/* add channel contexts */
2294f0dea9c7SArend van Spriel 	if (local->use_chanctx) {
229555de908aSJohannes Berg 		mutex_lock(&local->chanctx_mtx);
229655de908aSJohannes Berg 		list_for_each_entry(ctx, &local->chanctx_list, list)
22975bcae31dSMichal Kazior 			if (ctx->replace_state !=
22985bcae31dSMichal Kazior 			    IEEE80211_CHANCTX_REPLACES_OTHER)
229955de908aSJohannes Berg 				WARN_ON(drv_add_chanctx(local, ctx));
230055de908aSJohannes Berg 		mutex_unlock(&local->chanctx_mtx);
230155de908aSJohannes Berg 
2302fe5f2559SJohannes Berg 		sdata = rtnl_dereference(local->monitor_sdata);
2303153a5fc4SStanislaw Gruszka 		if (sdata && ieee80211_sdata_running(sdata))
2304153a5fc4SStanislaw Gruszka 			ieee80211_assign_chanctx(local, sdata);
23057df180f7SZhao, Gang 	}
2306fe5f2559SJohannes Berg 
2307f2753ddbSJohannes Berg 	/* reconfigure hardware */
2308f2753ddbSJohannes Berg 	ieee80211_hw_config(local, ~0);
2309f2753ddbSJohannes Berg 
2310f2753ddbSJohannes Berg 	ieee80211_configure_filter(local);
2311f2753ddbSJohannes Berg 
2312f2753ddbSJohannes Berg 	/* Finally also reconfigure all the BSS information */
2313f2753ddbSJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
2314ac8dd506SJohannes Berg 		u32 changed;
2315ac8dd506SJohannes Berg 
23169607e6b6SJohannes Berg 		if (!ieee80211_sdata_running(sdata))
2317f2753ddbSJohannes Berg 			continue;
2318ac8dd506SJohannes Berg 
23191ea2c864SJohannes Berg 		ieee80211_assign_chanctx(local, sdata);
23201ea2c864SJohannes Berg 
23211ea2c864SJohannes Berg 		switch (sdata->vif.type) {
23221ea2c864SJohannes Berg 		case NL80211_IFTYPE_AP_VLAN:
23231ea2c864SJohannes Berg 		case NL80211_IFTYPE_MONITOR:
23241ea2c864SJohannes Berg 			break;
23254926b51bSJohannes Berg 		case NL80211_IFTYPE_ADHOC:
23264926b51bSJohannes Berg 			if (sdata->vif.bss_conf.ibss_joined)
23274926b51bSJohannes Berg 				WARN_ON(drv_join_ibss(local, sdata));
23284926b51bSJohannes Berg 			/* fall through */
23291ea2c864SJohannes Berg 		default:
23301ea2c864SJohannes Berg 			ieee80211_reconfig_stations(sdata);
23311ea2c864SJohannes Berg 			/* fall through */
23321ea2c864SJohannes Berg 		case NL80211_IFTYPE_AP: /* AP stations are handled later */
23331ea2c864SJohannes Berg 			for (i = 0; i < IEEE80211_NUM_ACS; i++)
23341ea2c864SJohannes Berg 				drv_conf_tx(local, sdata, i,
23351ea2c864SJohannes Berg 					    &sdata->tx_conf[i]);
23361ea2c864SJohannes Berg 			break;
23371ea2c864SJohannes Berg 		}
23381ea2c864SJohannes Berg 
2339ac8dd506SJohannes Berg 		/* common change flags for all interface types */
2340ac8dd506SJohannes Berg 		changed = BSS_CHANGED_ERP_CTS_PROT |
2341ac8dd506SJohannes Berg 			  BSS_CHANGED_ERP_PREAMBLE |
2342ac8dd506SJohannes Berg 			  BSS_CHANGED_ERP_SLOT |
2343ac8dd506SJohannes Berg 			  BSS_CHANGED_HT |
2344ac8dd506SJohannes Berg 			  BSS_CHANGED_BASIC_RATES |
2345ac8dd506SJohannes Berg 			  BSS_CHANGED_BEACON_INT |
2346ac8dd506SJohannes Berg 			  BSS_CHANGED_BSSID |
23474ced3f74SJohannes Berg 			  BSS_CHANGED_CQM |
234855de47f6SEliad Peller 			  BSS_CHANGED_QOS |
23491ea6f9c0SJohannes Berg 			  BSS_CHANGED_IDLE |
2350dcbe73caSPradeep Kumar Chitrapu 			  BSS_CHANGED_TXPOWER |
2351dcbe73caSPradeep Kumar Chitrapu 			  BSS_CHANGED_MCAST_RATE;
2352ac8dd506SJohannes Berg 
2353b5a33d52SSara Sharon 		if (sdata->vif.mu_mimo_owner)
235423a1f8d4SSara Sharon 			changed |= BSS_CHANGED_MU_GROUPS;
235523a1f8d4SSara Sharon 
2356f2753ddbSJohannes Berg 		switch (sdata->vif.type) {
2357f2753ddbSJohannes Berg 		case NL80211_IFTYPE_STATION:
23580d392e93SEliad Peller 			changed |= BSS_CHANGED_ASSOC |
2359ab095877SEliad Peller 				   BSS_CHANGED_ARP_FILTER |
2360ab095877SEliad Peller 				   BSS_CHANGED_PS;
2361c65dd147SEmmanuel Grumbach 
2362989c6505SAlexander Bondar 			/* Re-send beacon info report to the driver */
2363989c6505SAlexander Bondar 			if (sdata->u.mgd.have_beacon)
2364989c6505SAlexander Bondar 				changed |= BSS_CHANGED_BEACON_INFO;
2365c65dd147SEmmanuel Grumbach 
2366e38a017bSAvraham Stern 			if (sdata->vif.bss_conf.max_idle_period ||
2367e38a017bSAvraham Stern 			    sdata->vif.bss_conf.protected_keep_alive)
2368e38a017bSAvraham Stern 				changed |= BSS_CHANGED_KEEP_ALIVE;
2369e38a017bSAvraham Stern 
23708d61ffa5SJohannes Berg 			sdata_lock(sdata);
2371ac8dd506SJohannes Berg 			ieee80211_bss_info_change_notify(sdata, changed);
23728d61ffa5SJohannes Berg 			sdata_unlock(sdata);
2373ac8dd506SJohannes Berg 			break;
23746e0bd6c3SRostislav Lisovy 		case NL80211_IFTYPE_OCB:
2375239281f8SRostislav Lisovy 			changed |= BSS_CHANGED_OCB;
2376239281f8SRostislav Lisovy 			ieee80211_bss_info_change_notify(sdata, changed);
23776e0bd6c3SRostislav Lisovy 			break;
2378f2753ddbSJohannes Berg 		case NL80211_IFTYPE_ADHOC:
2379ac8dd506SJohannes Berg 			changed |= BSS_CHANGED_IBSS;
2380ac8dd506SJohannes Berg 			/* fall through */
2381f2753ddbSJohannes Berg 		case NL80211_IFTYPE_AP:
2382339afbf4SJohannes Berg 			changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
2383e7979ac7SArik Nemtsov 
2384bc847970SPradeep Kumar Chitrapu 			if (sdata->vif.bss_conf.ftm_responder == 1 &&
2385bc847970SPradeep Kumar Chitrapu 			    wiphy_ext_feature_isset(sdata->local->hw.wiphy,
2386bc847970SPradeep Kumar Chitrapu 					NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
2387bc847970SPradeep Kumar Chitrapu 				changed |= BSS_CHANGED_FTM_RESPONDER;
2388bc847970SPradeep Kumar Chitrapu 
23891041638fSJohannes Berg 			if (sdata->vif.type == NL80211_IFTYPE_AP) {
2390e7979ac7SArik Nemtsov 				changed |= BSS_CHANGED_AP_PROBE_RESP;
2391e7979ac7SArik Nemtsov 
23921041638fSJohannes Berg 				if (rcu_access_pointer(sdata->u.ap.beacon))
23931041638fSJohannes Berg 					drv_start_ap(local, sdata);
23941041638fSJohannes Berg 			}
23951041638fSJohannes Berg 
23967827493bSArik Nemtsov 			/* fall through */
2397f2753ddbSJohannes Berg 		case NL80211_IFTYPE_MESH_POINT:
23988da34932SJohannes Berg 			if (sdata->vif.bss_conf.enable_beacon) {
2399ac8dd506SJohannes Berg 				changed |= BSS_CHANGED_BEACON |
2400ac8dd506SJohannes Berg 					   BSS_CHANGED_BEACON_ENABLED;
24012d0ddec5SJohannes Berg 				ieee80211_bss_info_change_notify(sdata, changed);
24028da34932SJohannes Berg 			}
2403f2753ddbSJohannes Berg 			break;
2404167e33f4SAyala Beker 		case NL80211_IFTYPE_NAN:
2405167e33f4SAyala Beker 			res = ieee80211_reconfig_nan(sdata);
2406167e33f4SAyala Beker 			if (res < 0) {
2407167e33f4SAyala Beker 				ieee80211_handle_reconfig_failure(local);
2408167e33f4SAyala Beker 				return res;
2409167e33f4SAyala Beker 			}
2410167e33f4SAyala Beker 			break;
2411f2753ddbSJohannes Berg 		case NL80211_IFTYPE_WDS:
2412f2753ddbSJohannes Berg 		case NL80211_IFTYPE_AP_VLAN:
2413f2753ddbSJohannes Berg 		case NL80211_IFTYPE_MONITOR:
241498104fdeSJohannes Berg 		case NL80211_IFTYPE_P2P_DEVICE:
2415b205786eSZhao, Gang 			/* nothing to do */
2416f142c6b9SJohannes Berg 			break;
2417f2753ddbSJohannes Berg 		case NL80211_IFTYPE_UNSPECIFIED:
24182e161f78SJohannes Berg 		case NUM_NL80211_IFTYPES:
24192ca27bcfSJohannes Berg 		case NL80211_IFTYPE_P2P_CLIENT:
24202ca27bcfSJohannes Berg 		case NL80211_IFTYPE_P2P_GO:
2421f2753ddbSJohannes Berg 			WARN_ON(1);
2422f2753ddbSJohannes Berg 			break;
2423f2753ddbSJohannes Berg 		}
2424f2753ddbSJohannes Berg 	}
2425f2753ddbSJohannes Berg 
24264a733ef1SJohannes Berg 	ieee80211_recalc_ps(local);
24278e1b23b9SEyal Shapira 
24282a419056SJohannes Berg 	/*
24296e1b1b24SEliad Peller 	 * The sta might be in psm against the ap (e.g. because
24306e1b1b24SEliad Peller 	 * this was the state before a hw restart), so we
24316e1b1b24SEliad Peller 	 * explicitly send a null packet in order to make sure
24326e1b1b24SEliad Peller 	 * it'll sync against the ap (and get out of psm).
24336e1b1b24SEliad Peller 	 */
24346e1b1b24SEliad Peller 	if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
24356e1b1b24SEliad Peller 		list_for_each_entry(sdata, &local->interfaces, list) {
24366e1b1b24SEliad Peller 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
24376e1b1b24SEliad Peller 				continue;
243820f544eeSJohannes Berg 			if (!sdata->u.mgd.associated)
243920f544eeSJohannes Berg 				continue;
24406e1b1b24SEliad Peller 
2441076cdcb1SJohannes Berg 			ieee80211_send_nullfunc(local, sdata, false);
24426e1b1b24SEliad Peller 		}
24436e1b1b24SEliad Peller 	}
24446e1b1b24SEliad Peller 
24452e8d397eSArik Nemtsov 	/* APs are now beaconing, add back stations */
24462e8d397eSArik Nemtsov 	mutex_lock(&local->sta_mtx);
24472e8d397eSArik Nemtsov 	list_for_each_entry(sta, &local->sta_list, list) {
24482e8d397eSArik Nemtsov 		enum ieee80211_sta_state state;
24492e8d397eSArik Nemtsov 
24502e8d397eSArik Nemtsov 		if (!sta->uploaded)
24512e8d397eSArik Nemtsov 			continue;
24522e8d397eSArik Nemtsov 
245319103a4bSmpubbise@codeaurora.org 		if (sta->sdata->vif.type != NL80211_IFTYPE_AP &&
245419103a4bSmpubbise@codeaurora.org 		    sta->sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
24552e8d397eSArik Nemtsov 			continue;
24562e8d397eSArik Nemtsov 
24572e8d397eSArik Nemtsov 		for (state = IEEE80211_STA_NOTEXIST;
24582e8d397eSArik Nemtsov 		     state < sta->sta_state; state++)
24592e8d397eSArik Nemtsov 			WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
24602e8d397eSArik Nemtsov 					      state + 1));
24612e8d397eSArik Nemtsov 	}
24622e8d397eSArik Nemtsov 	mutex_unlock(&local->sta_mtx);
24632e8d397eSArik Nemtsov 
24647b21aea0SEyal Shapira 	/* add back keys */
24657b21aea0SEyal Shapira 	list_for_each_entry(sdata, &local->interfaces, list)
2466624ff4b2SLior Cohen 		ieee80211_reenable_keys(sdata);
24677b21aea0SEyal Shapira 
24680d440ea2SEliad Peller 	/* Reconfigure sched scan if it was interrupted by FW restart */
24690d440ea2SEliad Peller 	mutex_lock(&local->mtx);
24700d440ea2SEliad Peller 	sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
24710d440ea2SEliad Peller 						lockdep_is_held(&local->mtx));
24720d440ea2SEliad Peller 	sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
24730d440ea2SEliad Peller 						lockdep_is_held(&local->mtx));
24740d440ea2SEliad Peller 	if (sched_scan_sdata && sched_scan_req)
24750d440ea2SEliad Peller 		/*
24760d440ea2SEliad Peller 		 * Sched scan stopped, but we don't want to report it. Instead,
24770d440ea2SEliad Peller 		 * we're trying to reschedule. However, if more than one scan
24780d440ea2SEliad Peller 		 * plan was set, we cannot reschedule since we don't know which
24790d440ea2SEliad Peller 		 * scan plan was currently running (and some scan plans may have
24800d440ea2SEliad Peller 		 * already finished).
24810d440ea2SEliad Peller 		 */
24820d440ea2SEliad Peller 		if (sched_scan_req->n_scan_plans > 1 ||
24830d440ea2SEliad Peller 		    __ieee80211_request_sched_scan_start(sched_scan_sdata,
2484b9f628fcSEliad Peller 							 sched_scan_req)) {
2485b9f628fcSEliad Peller 			RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
2486b9f628fcSEliad Peller 			RCU_INIT_POINTER(local->sched_scan_req, NULL);
24870d440ea2SEliad Peller 			sched_scan_stopped = true;
2488b9f628fcSEliad Peller 		}
24890d440ea2SEliad Peller 	mutex_unlock(&local->mtx);
24900d440ea2SEliad Peller 
24910d440ea2SEliad Peller 	if (sched_scan_stopped)
2492b34939b9SArend Van Spriel 		cfg80211_sched_scan_stopped_rtnl(local->hw.wiphy, 0);
24930d440ea2SEliad Peller 
2494c6209488SEliad Peller  wake_up:
2495470f4d61SEliad Peller 
24963c3e21e7SJohannes Berg 	if (local->monitors == local->open_count && local->monitors > 0)
24973c3e21e7SJohannes Berg 		ieee80211_add_virtual_monitor(local);
24983c3e21e7SJohannes Berg 
24996e1b1b24SEliad Peller 	/*
25002a419056SJohannes Berg 	 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
25012a419056SJohannes Berg 	 * sessions can be established after a resume.
25022a419056SJohannes Berg 	 *
25032a419056SJohannes Berg 	 * Also tear down aggregation sessions since reconfiguring
25042a419056SJohannes Berg 	 * them in a hardware restart scenario is not easily done
25052a419056SJohannes Berg 	 * right now, and the hardware will have lost information
25062a419056SJohannes Berg 	 * about the sessions, but we and the AP still think they
25072a419056SJohannes Berg 	 * are active. This is really a workaround though.
25082a419056SJohannes Berg 	 */
250930686bf7SJohannes Berg 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
25102a419056SJohannes Berg 		mutex_lock(&local->sta_mtx);
25112a419056SJohannes Berg 
25122a419056SJohannes Berg 		list_for_each_entry(sta, &local->sta_list, list) {
251327392719SEliad Peller 			if (!local->resuming)
2514c82c4a80SJohannes Berg 				ieee80211_sta_tear_down_BA_sessions(
2515c82c4a80SJohannes Berg 						sta, AGG_STOP_LOCAL_REQUEST);
2516c2c98fdeSJohannes Berg 			clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
251774e2bd1fSWey-Yi Guy 		}
25182a419056SJohannes Berg 
25192a419056SJohannes Berg 		mutex_unlock(&local->sta_mtx);
252074e2bd1fSWey-Yi Guy 	}
252174e2bd1fSWey-Yi Guy 
25222316380fSSara Sharon 	if (local->in_reconfig) {
25232316380fSSara Sharon 		local->in_reconfig = false;
25242316380fSSara Sharon 		barrier();
25252316380fSSara Sharon 
25262316380fSSara Sharon 		/* Restart deferred ROCs */
25272316380fSSara Sharon 		mutex_lock(&local->mtx);
25282316380fSSara Sharon 		ieee80211_start_next_roc(local);
25292316380fSSara Sharon 		mutex_unlock(&local->mtx);
2530f8891461SNaftali Goldstein 
2531f8891461SNaftali Goldstein 		/* Requeue all works */
2532f8891461SNaftali Goldstein 		list_for_each_entry(sdata, &local->interfaces, list)
2533f8891461SNaftali Goldstein 			ieee80211_queue_work(&local->hw, &sdata->work);
25342316380fSSara Sharon 	}
25352316380fSSara Sharon 
2536445ea4e8SJohannes Berg 	ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
2537cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
2538cca07b00SLuciano Coelho 					false);
2539f2753ddbSJohannes Berg 
25405bb644a0SJohannes Berg 	/*
25415bb644a0SJohannes Berg 	 * If this is for hw restart things are still running.
25425bb644a0SJohannes Berg 	 * We may want to change that later, however.
25435bb644a0SJohannes Berg 	 */
2544b0485e9fSEliad Peller 	if (local->open_count && (!suspended || reconfig_due_to_wowlan))
2545cf2c92d8SEliad Peller 		drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
25468f21b0adSJohannes Berg 
2547b0485e9fSEliad Peller 	if (!suspended)
25485bb644a0SJohannes Berg 		return 0;
25495bb644a0SJohannes Berg 
25505bb644a0SJohannes Berg #ifdef CONFIG_PM
2551ceb99fe0SJohannes Berg 	/* first set suspended false, then resuming */
25525bb644a0SJohannes Berg 	local->suspended = false;
2553ceb99fe0SJohannes Berg 	mb();
2554ceb99fe0SJohannes Berg 	local->resuming = false;
25555bb644a0SJohannes Berg 
255674430f94SJohannes Berg 	ieee80211_flush_completed_scan(local, false);
25579120d94eSLuciano Coelho 
25580f8b8245SEliad Peller 	if (local->open_count && !reconfig_due_to_wowlan)
2559cf2c92d8SEliad Peller 		drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
2560cf2c92d8SEliad Peller 
2561b8360ab8SJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
2562b8360ab8SJohannes Berg 		if (!ieee80211_sdata_running(sdata))
2563b8360ab8SJohannes Berg 			continue;
2564b8360ab8SJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_STATION)
2565b8360ab8SJohannes Berg 			ieee80211_sta_restart(sdata);
2566b8360ab8SJohannes Berg 	}
2567b8360ab8SJohannes Berg 
256826d59535SJohannes Berg 	mod_timer(&local->sta_cleanup, jiffies + 1);
25695bb644a0SJohannes Berg #else
25705bb644a0SJohannes Berg 	WARN_ON(1);
25715bb644a0SJohannes Berg #endif
2572d43c6b6eSDavid Spinadel 
2573f2753ddbSJohannes Berg 	return 0;
2574f2753ddbSJohannes Berg }
257542935ecaSLuis R. Rodriguez 
257695acac61SJohannes Berg void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
257795acac61SJohannes Berg {
257895acac61SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
257995acac61SJohannes Berg 	struct ieee80211_local *local;
258095acac61SJohannes Berg 	struct ieee80211_key *key;
258195acac61SJohannes Berg 
258295acac61SJohannes Berg 	if (WARN_ON(!vif))
258395acac61SJohannes Berg 		return;
258495acac61SJohannes Berg 
258595acac61SJohannes Berg 	sdata = vif_to_sdata(vif);
258695acac61SJohannes Berg 	local = sdata->local;
258795acac61SJohannes Berg 
258895acac61SJohannes Berg 	if (WARN_ON(!local->resuming))
258995acac61SJohannes Berg 		return;
259095acac61SJohannes Berg 
259195acac61SJohannes Berg 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
259295acac61SJohannes Berg 		return;
259395acac61SJohannes Berg 
259495acac61SJohannes Berg 	sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
259595acac61SJohannes Berg 
259695acac61SJohannes Berg 	mutex_lock(&local->key_mtx);
259795acac61SJohannes Berg 	list_for_each_entry(key, &sdata->key_list, list)
259895acac61SJohannes Berg 		key->flags |= KEY_FLAG_TAINTED;
259995acac61SJohannes Berg 	mutex_unlock(&local->key_mtx);
260095acac61SJohannes Berg }
260195acac61SJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
260295acac61SJohannes Berg 
260304ecd257SJohannes Berg void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
26040f78231bSJohannes Berg {
260504ecd257SJohannes Berg 	struct ieee80211_local *local = sdata->local;
260604ecd257SJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
260704ecd257SJohannes Berg 	struct ieee80211_chanctx *chanctx;
26080f78231bSJohannes Berg 
260904ecd257SJohannes Berg 	mutex_lock(&local->chanctx_mtx);
26100f78231bSJohannes Berg 
261104ecd257SJohannes Berg 	chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
261204ecd257SJohannes Berg 					lockdep_is_held(&local->chanctx_mtx));
26130f78231bSJohannes Berg 
2614c189a685SAndrei Otcheretianski 	/*
2615c189a685SAndrei Otcheretianski 	 * This function can be called from a work, thus it may be possible
2616c189a685SAndrei Otcheretianski 	 * that the chanctx_conf is removed (due to a disconnection, for
2617c189a685SAndrei Otcheretianski 	 * example).
2618c189a685SAndrei Otcheretianski 	 * So nothing should be done in such case.
2619c189a685SAndrei Otcheretianski 	 */
2620c189a685SAndrei Otcheretianski 	if (!chanctx_conf)
26215d8e4237SJohannes Berg 		goto unlock;
26220f78231bSJohannes Berg 
262304ecd257SJohannes Berg 	chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
262404ecd257SJohannes Berg 	ieee80211_recalc_smps_chanctx(local, chanctx);
26255d8e4237SJohannes Berg  unlock:
262604ecd257SJohannes Berg 	mutex_unlock(&local->chanctx_mtx);
26270f78231bSJohannes Berg }
26288e664fb3SJohannes Berg 
262921f659bfSEliad Peller void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata)
263021f659bfSEliad Peller {
263121f659bfSEliad Peller 	struct ieee80211_local *local = sdata->local;
263221f659bfSEliad Peller 	struct ieee80211_chanctx_conf *chanctx_conf;
263321f659bfSEliad Peller 	struct ieee80211_chanctx *chanctx;
263421f659bfSEliad Peller 
263521f659bfSEliad Peller 	mutex_lock(&local->chanctx_mtx);
263621f659bfSEliad Peller 
263721f659bfSEliad Peller 	chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
263821f659bfSEliad Peller 					lockdep_is_held(&local->chanctx_mtx));
263921f659bfSEliad Peller 
264021f659bfSEliad Peller 	if (WARN_ON_ONCE(!chanctx_conf))
264121f659bfSEliad Peller 		goto unlock;
264221f659bfSEliad Peller 
264321f659bfSEliad Peller 	chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
264421f659bfSEliad Peller 	ieee80211_recalc_chanctx_min_def(local, chanctx);
264521f659bfSEliad Peller  unlock:
264621f659bfSEliad Peller 	mutex_unlock(&local->chanctx_mtx);
264721f659bfSEliad Peller }
264821f659bfSEliad Peller 
26498e664fb3SJohannes Berg size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
26508e664fb3SJohannes Berg {
26518e664fb3SJohannes Berg 	size_t pos = offset;
26528e664fb3SJohannes Berg 
26538e664fb3SJohannes Berg 	while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
26548e664fb3SJohannes Berg 		pos += 2 + ies[pos + 1];
26558e664fb3SJohannes Berg 
26568e664fb3SJohannes Berg 	return pos;
26578e664fb3SJohannes Berg }
2658615f7b9bSMeenakshi Venkataraman 
2659615f7b9bSMeenakshi Venkataraman static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
2660615f7b9bSMeenakshi Venkataraman 					    int rssi_min_thold,
2661615f7b9bSMeenakshi Venkataraman 					    int rssi_max_thold)
2662615f7b9bSMeenakshi Venkataraman {
2663615f7b9bSMeenakshi Venkataraman 	trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
2664615f7b9bSMeenakshi Venkataraman 
2665615f7b9bSMeenakshi Venkataraman 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2666615f7b9bSMeenakshi Venkataraman 		return;
2667615f7b9bSMeenakshi Venkataraman 
2668615f7b9bSMeenakshi Venkataraman 	/*
2669615f7b9bSMeenakshi Venkataraman 	 * Scale up threshold values before storing it, as the RSSI averaging
2670615f7b9bSMeenakshi Venkataraman 	 * algorithm uses a scaled up value as well. Change this scaling
2671615f7b9bSMeenakshi Venkataraman 	 * factor if the RSSI averaging algorithm changes.
2672615f7b9bSMeenakshi Venkataraman 	 */
2673615f7b9bSMeenakshi Venkataraman 	sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
2674615f7b9bSMeenakshi Venkataraman 	sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
2675615f7b9bSMeenakshi Venkataraman }
2676615f7b9bSMeenakshi Venkataraman 
2677615f7b9bSMeenakshi Venkataraman void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
2678615f7b9bSMeenakshi Venkataraman 				    int rssi_min_thold,
2679615f7b9bSMeenakshi Venkataraman 				    int rssi_max_thold)
2680615f7b9bSMeenakshi Venkataraman {
2681615f7b9bSMeenakshi Venkataraman 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2682615f7b9bSMeenakshi Venkataraman 
2683615f7b9bSMeenakshi Venkataraman 	WARN_ON(rssi_min_thold == rssi_max_thold ||
2684615f7b9bSMeenakshi Venkataraman 		rssi_min_thold > rssi_max_thold);
2685615f7b9bSMeenakshi Venkataraman 
2686615f7b9bSMeenakshi Venkataraman 	_ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
2687615f7b9bSMeenakshi Venkataraman 				       rssi_max_thold);
2688615f7b9bSMeenakshi Venkataraman }
2689615f7b9bSMeenakshi Venkataraman EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
2690615f7b9bSMeenakshi Venkataraman 
2691615f7b9bSMeenakshi Venkataraman void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
2692615f7b9bSMeenakshi Venkataraman {
2693615f7b9bSMeenakshi Venkataraman 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2694615f7b9bSMeenakshi Venkataraman 
2695615f7b9bSMeenakshi Venkataraman 	_ieee80211_enable_rssi_reports(sdata, 0, 0);
2696615f7b9bSMeenakshi Venkataraman }
2697615f7b9bSMeenakshi Venkataraman EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
2698768db343SArik Nemtsov 
2699ef96a842SBen Greear u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
270042e7aa77SAlexander Simon 			      u16 cap)
270142e7aa77SAlexander Simon {
270242e7aa77SAlexander Simon 	__le16 tmp;
270342e7aa77SAlexander Simon 
270442e7aa77SAlexander Simon 	*pos++ = WLAN_EID_HT_CAPABILITY;
270542e7aa77SAlexander Simon 	*pos++ = sizeof(struct ieee80211_ht_cap);
270642e7aa77SAlexander Simon 	memset(pos, 0, sizeof(struct ieee80211_ht_cap));
270742e7aa77SAlexander Simon 
270842e7aa77SAlexander Simon 	/* capability flags */
270942e7aa77SAlexander Simon 	tmp = cpu_to_le16(cap);
271042e7aa77SAlexander Simon 	memcpy(pos, &tmp, sizeof(u16));
271142e7aa77SAlexander Simon 	pos += sizeof(u16);
271242e7aa77SAlexander Simon 
271342e7aa77SAlexander Simon 	/* AMPDU parameters */
2714ef96a842SBen Greear 	*pos++ = ht_cap->ampdu_factor |
2715ef96a842SBen Greear 		 (ht_cap->ampdu_density <<
271642e7aa77SAlexander Simon 			IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
271742e7aa77SAlexander Simon 
271842e7aa77SAlexander Simon 	/* MCS set */
2719ef96a842SBen Greear 	memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
2720ef96a842SBen Greear 	pos += sizeof(ht_cap->mcs);
272142e7aa77SAlexander Simon 
272242e7aa77SAlexander Simon 	/* extended capabilities */
272342e7aa77SAlexander Simon 	pos += sizeof(__le16);
272442e7aa77SAlexander Simon 
272542e7aa77SAlexander Simon 	/* BF capabilities */
272642e7aa77SAlexander Simon 	pos += sizeof(__le32);
272742e7aa77SAlexander Simon 
272842e7aa77SAlexander Simon 	/* antenna selection */
272942e7aa77SAlexander Simon 	pos += sizeof(u8);
273042e7aa77SAlexander Simon 
273142e7aa77SAlexander Simon 	return pos;
273242e7aa77SAlexander Simon }
273342e7aa77SAlexander Simon 
2734ba0afa2fSMahesh Palivela u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2735ba0afa2fSMahesh Palivela 			       u32 cap)
2736ba0afa2fSMahesh Palivela {
2737ba0afa2fSMahesh Palivela 	__le32 tmp;
2738ba0afa2fSMahesh Palivela 
2739ba0afa2fSMahesh Palivela 	*pos++ = WLAN_EID_VHT_CAPABILITY;
2740d4950281SMahesh Palivela 	*pos++ = sizeof(struct ieee80211_vht_cap);
2741d4950281SMahesh Palivela 	memset(pos, 0, sizeof(struct ieee80211_vht_cap));
2742ba0afa2fSMahesh Palivela 
2743ba0afa2fSMahesh Palivela 	/* capability flags */
2744ba0afa2fSMahesh Palivela 	tmp = cpu_to_le32(cap);
2745ba0afa2fSMahesh Palivela 	memcpy(pos, &tmp, sizeof(u32));
2746ba0afa2fSMahesh Palivela 	pos += sizeof(u32);
2747ba0afa2fSMahesh Palivela 
2748ba0afa2fSMahesh Palivela 	/* VHT MCS set */
2749ba0afa2fSMahesh Palivela 	memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
2750ba0afa2fSMahesh Palivela 	pos += sizeof(vht_cap->vht_mcs);
2751ba0afa2fSMahesh Palivela 
2752ba0afa2fSMahesh Palivela 	return pos;
2753ba0afa2fSMahesh Palivela }
2754ba0afa2fSMahesh Palivela 
275560ad72daSSven Eckelmann u8 ieee80211_ie_len_he_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
275660ad72daSSven Eckelmann {
275760ad72daSSven Eckelmann 	const struct ieee80211_sta_he_cap *he_cap;
275860ad72daSSven Eckelmann 	struct ieee80211_supported_band *sband;
275960ad72daSSven Eckelmann 	u8 n;
276060ad72daSSven Eckelmann 
276160ad72daSSven Eckelmann 	sband = ieee80211_get_sband(sdata);
276260ad72daSSven Eckelmann 	if (!sband)
276360ad72daSSven Eckelmann 		return 0;
276460ad72daSSven Eckelmann 
276560ad72daSSven Eckelmann 	he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
276660ad72daSSven Eckelmann 	if (!he_cap)
276760ad72daSSven Eckelmann 		return 0;
276860ad72daSSven Eckelmann 
276960ad72daSSven Eckelmann 	n = ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem);
277060ad72daSSven Eckelmann 	return 2 + 1 +
277160ad72daSSven Eckelmann 	       sizeof(he_cap->he_cap_elem) + n +
277260ad72daSSven Eckelmann 	       ieee80211_he_ppe_size(he_cap->ppe_thres[0],
277360ad72daSSven Eckelmann 				     he_cap->he_cap_elem.phy_cap_info);
277460ad72daSSven Eckelmann }
277560ad72daSSven Eckelmann 
277641cbb0f5SLuca Coelho u8 *ieee80211_ie_build_he_cap(u8 *pos,
277741cbb0f5SLuca Coelho 			      const struct ieee80211_sta_he_cap *he_cap,
277841cbb0f5SLuca Coelho 			      u8 *end)
277941cbb0f5SLuca Coelho {
278041cbb0f5SLuca Coelho 	u8 n;
278141cbb0f5SLuca Coelho 	u8 ie_len;
278241cbb0f5SLuca Coelho 	u8 *orig_pos = pos;
278341cbb0f5SLuca Coelho 
278441cbb0f5SLuca Coelho 	/* Make sure we have place for the IE */
278541cbb0f5SLuca Coelho 	/*
278641cbb0f5SLuca Coelho 	 * TODO: the 1 added is because this temporarily is under the EXTENSION
278741cbb0f5SLuca Coelho 	 * IE. Get rid of it when it moves.
278841cbb0f5SLuca Coelho 	 */
278941cbb0f5SLuca Coelho 	if (!he_cap)
279041cbb0f5SLuca Coelho 		return orig_pos;
279141cbb0f5SLuca Coelho 
279241cbb0f5SLuca Coelho 	n = ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem);
279341cbb0f5SLuca Coelho 	ie_len = 2 + 1 +
279441cbb0f5SLuca Coelho 		 sizeof(he_cap->he_cap_elem) + n +
279541cbb0f5SLuca Coelho 		 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
279641cbb0f5SLuca Coelho 				       he_cap->he_cap_elem.phy_cap_info);
279741cbb0f5SLuca Coelho 
279841cbb0f5SLuca Coelho 	if ((end - pos) < ie_len)
279941cbb0f5SLuca Coelho 		return orig_pos;
280041cbb0f5SLuca Coelho 
280141cbb0f5SLuca Coelho 	*pos++ = WLAN_EID_EXTENSION;
280241cbb0f5SLuca Coelho 	pos++; /* We'll set the size later below */
280341cbb0f5SLuca Coelho 	*pos++ = WLAN_EID_EXT_HE_CAPABILITY;
280441cbb0f5SLuca Coelho 
280541cbb0f5SLuca Coelho 	/* Fixed data */
280641cbb0f5SLuca Coelho 	memcpy(pos, &he_cap->he_cap_elem, sizeof(he_cap->he_cap_elem));
280741cbb0f5SLuca Coelho 	pos += sizeof(he_cap->he_cap_elem);
280841cbb0f5SLuca Coelho 
280941cbb0f5SLuca Coelho 	memcpy(pos, &he_cap->he_mcs_nss_supp, n);
281041cbb0f5SLuca Coelho 	pos += n;
281141cbb0f5SLuca Coelho 
281241cbb0f5SLuca Coelho 	/* Check if PPE Threshold should be present */
281341cbb0f5SLuca Coelho 	if ((he_cap->he_cap_elem.phy_cap_info[6] &
281441cbb0f5SLuca Coelho 	     IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) == 0)
281541cbb0f5SLuca Coelho 		goto end;
281641cbb0f5SLuca Coelho 
281741cbb0f5SLuca Coelho 	/*
281841cbb0f5SLuca Coelho 	 * Calculate how many PPET16/PPET8 pairs are to come. Algorithm:
281941cbb0f5SLuca Coelho 	 * (NSS_M1 + 1) x (num of 1 bits in RU_INDEX_BITMASK)
282041cbb0f5SLuca Coelho 	 */
282141cbb0f5SLuca Coelho 	n = hweight8(he_cap->ppe_thres[0] &
282241cbb0f5SLuca Coelho 		     IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
282341cbb0f5SLuca Coelho 	n *= (1 + ((he_cap->ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) >>
282441cbb0f5SLuca Coelho 		   IEEE80211_PPE_THRES_NSS_POS));
282541cbb0f5SLuca Coelho 
282641cbb0f5SLuca Coelho 	/*
282741cbb0f5SLuca Coelho 	 * Each pair is 6 bits, and we need to add the 7 "header" bits to the
282841cbb0f5SLuca Coelho 	 * total size.
282941cbb0f5SLuca Coelho 	 */
283041cbb0f5SLuca Coelho 	n = (n * IEEE80211_PPE_THRES_INFO_PPET_SIZE * 2) + 7;
283141cbb0f5SLuca Coelho 	n = DIV_ROUND_UP(n, 8);
283241cbb0f5SLuca Coelho 
283341cbb0f5SLuca Coelho 	/* Copy PPE Thresholds */
283441cbb0f5SLuca Coelho 	memcpy(pos, &he_cap->ppe_thres, n);
283541cbb0f5SLuca Coelho 	pos += n;
283641cbb0f5SLuca Coelho 
283741cbb0f5SLuca Coelho end:
283841cbb0f5SLuca Coelho 	orig_pos[1] = (pos - orig_pos) - 2;
283941cbb0f5SLuca Coelho 	return pos;
284041cbb0f5SLuca Coelho }
284141cbb0f5SLuca Coelho 
2842074d46d1SJohannes Berg u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
28434bf88530SJohannes Berg 			       const struct cfg80211_chan_def *chandef,
284457f255f5SArik Nemtsov 			       u16 prot_mode, bool rifs_mode)
284542e7aa77SAlexander Simon {
2846074d46d1SJohannes Berg 	struct ieee80211_ht_operation *ht_oper;
284742e7aa77SAlexander Simon 	/* Build HT Information */
2848074d46d1SJohannes Berg 	*pos++ = WLAN_EID_HT_OPERATION;
2849074d46d1SJohannes Berg 	*pos++ = sizeof(struct ieee80211_ht_operation);
2850074d46d1SJohannes Berg 	ht_oper = (struct ieee80211_ht_operation *)pos;
28514bf88530SJohannes Berg 	ht_oper->primary_chan = ieee80211_frequency_to_channel(
28524bf88530SJohannes Berg 					chandef->chan->center_freq);
28534bf88530SJohannes Berg 	switch (chandef->width) {
28544bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_160:
28554bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_80P80:
28564bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_80:
28574bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_40:
28584bf88530SJohannes Berg 		if (chandef->center_freq1 > chandef->chan->center_freq)
28594bf88530SJohannes Berg 			ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
28604bf88530SJohannes Berg 		else
2861074d46d1SJohannes Berg 			ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
286242e7aa77SAlexander Simon 		break;
286342e7aa77SAlexander Simon 	default:
2864074d46d1SJohannes Berg 		ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
286542e7aa77SAlexander Simon 		break;
286642e7aa77SAlexander Simon 	}
2867aee286c2SThomas Pedersen 	if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
28684bf88530SJohannes Berg 	    chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
28694bf88530SJohannes Berg 	    chandef->width != NL80211_CHAN_WIDTH_20)
2870074d46d1SJohannes Berg 		ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
2871ff3cc5f4SSimon Wunderlich 
287257f255f5SArik Nemtsov 	if (rifs_mode)
287357f255f5SArik Nemtsov 		ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
287457f255f5SArik Nemtsov 
2875431e3154SAshok Nagarajan 	ht_oper->operation_mode = cpu_to_le16(prot_mode);
2876074d46d1SJohannes Berg 	ht_oper->stbc_param = 0x0000;
287742e7aa77SAlexander Simon 
287842e7aa77SAlexander Simon 	/* It seems that Basic MCS set and Supported MCS set
287942e7aa77SAlexander Simon 	   are identical for the first 10 bytes */
2880074d46d1SJohannes Berg 	memset(&ht_oper->basic_set, 0, 16);
2881074d46d1SJohannes Berg 	memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
288242e7aa77SAlexander Simon 
2883074d46d1SJohannes Berg 	return pos + sizeof(struct ieee80211_ht_operation);
288442e7aa77SAlexander Simon }
288542e7aa77SAlexander Simon 
288675d627d5SSimon Wunderlich void ieee80211_ie_build_wide_bw_cs(u8 *pos,
288775d627d5SSimon Wunderlich 				   const struct cfg80211_chan_def *chandef)
288875d627d5SSimon Wunderlich {
288975d627d5SSimon Wunderlich 	*pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH;	/* EID */
289075d627d5SSimon Wunderlich 	*pos++ = 3;					/* IE length */
289175d627d5SSimon Wunderlich 	/* New channel width */
289275d627d5SSimon Wunderlich 	switch (chandef->width) {
289375d627d5SSimon Wunderlich 	case NL80211_CHAN_WIDTH_80:
289475d627d5SSimon Wunderlich 		*pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ;
289575d627d5SSimon Wunderlich 		break;
289675d627d5SSimon Wunderlich 	case NL80211_CHAN_WIDTH_160:
289775d627d5SSimon Wunderlich 		*pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ;
289875d627d5SSimon Wunderlich 		break;
289975d627d5SSimon Wunderlich 	case NL80211_CHAN_WIDTH_80P80:
290075d627d5SSimon Wunderlich 		*pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
290175d627d5SSimon Wunderlich 		break;
290275d627d5SSimon Wunderlich 	default:
290375d627d5SSimon Wunderlich 		*pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT;
290475d627d5SSimon Wunderlich 	}
290575d627d5SSimon Wunderlich 
290675d627d5SSimon Wunderlich 	/* new center frequency segment 0 */
290775d627d5SSimon Wunderlich 	*pos++ = ieee80211_frequency_to_channel(chandef->center_freq1);
290875d627d5SSimon Wunderlich 	/* new center frequency segment 1 */
290975d627d5SSimon Wunderlich 	if (chandef->center_freq2)
291075d627d5SSimon Wunderlich 		*pos++ = ieee80211_frequency_to_channel(chandef->center_freq2);
291175d627d5SSimon Wunderlich 	else
291275d627d5SSimon Wunderlich 		*pos++ = 0;
291375d627d5SSimon Wunderlich }
291475d627d5SSimon Wunderlich 
2915fb28ec0cSArik Nemtsov u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2916fb28ec0cSArik Nemtsov 				const struct cfg80211_chan_def *chandef)
2917fb28ec0cSArik Nemtsov {
2918fb28ec0cSArik Nemtsov 	struct ieee80211_vht_operation *vht_oper;
2919fb28ec0cSArik Nemtsov 
2920fb28ec0cSArik Nemtsov 	*pos++ = WLAN_EID_VHT_OPERATION;
2921fb28ec0cSArik Nemtsov 	*pos++ = sizeof(struct ieee80211_vht_operation);
2922fb28ec0cSArik Nemtsov 	vht_oper = (struct ieee80211_vht_operation *)pos;
29232fb51c35SJohannes Berg 	vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
2924fb28ec0cSArik Nemtsov 							chandef->center_freq1);
2925fb28ec0cSArik Nemtsov 	if (chandef->center_freq2)
29262fb51c35SJohannes Berg 		vht_oper->center_freq_seg1_idx =
2927fb28ec0cSArik Nemtsov 			ieee80211_frequency_to_channel(chandef->center_freq2);
2928f020ae40SChun-Yeow Yeoh 	else
29292fb51c35SJohannes Berg 		vht_oper->center_freq_seg1_idx = 0x00;
2930fb28ec0cSArik Nemtsov 
2931fb28ec0cSArik Nemtsov 	switch (chandef->width) {
2932fb28ec0cSArik Nemtsov 	case NL80211_CHAN_WIDTH_160:
293323665aafSJouni Malinen 		/*
293423665aafSJouni Malinen 		 * Convert 160 MHz channel width to new style as interop
293523665aafSJouni Malinen 		 * workaround.
293623665aafSJouni Malinen 		 */
293723665aafSJouni Malinen 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
29382fb51c35SJohannes Berg 		vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
293923665aafSJouni Malinen 		if (chandef->chan->center_freq < chandef->center_freq1)
29402fb51c35SJohannes Berg 			vht_oper->center_freq_seg0_idx -= 8;
294123665aafSJouni Malinen 		else
29422fb51c35SJohannes Berg 			vht_oper->center_freq_seg0_idx += 8;
2943fb28ec0cSArik Nemtsov 		break;
2944fb28ec0cSArik Nemtsov 	case NL80211_CHAN_WIDTH_80P80:
294523665aafSJouni Malinen 		/*
294623665aafSJouni Malinen 		 * Convert 80+80 MHz channel width to new style as interop
294723665aafSJouni Malinen 		 * workaround.
294823665aafSJouni Malinen 		 */
294923665aafSJouni Malinen 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
2950fb28ec0cSArik Nemtsov 		break;
2951fb28ec0cSArik Nemtsov 	case NL80211_CHAN_WIDTH_80:
2952fb28ec0cSArik Nemtsov 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
2953fb28ec0cSArik Nemtsov 		break;
2954fb28ec0cSArik Nemtsov 	default:
2955fb28ec0cSArik Nemtsov 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
2956fb28ec0cSArik Nemtsov 		break;
2957fb28ec0cSArik Nemtsov 	}
2958fb28ec0cSArik Nemtsov 
2959fb28ec0cSArik Nemtsov 	/* don't require special VHT peer rates */
2960fb28ec0cSArik Nemtsov 	vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
2961fb28ec0cSArik Nemtsov 
2962fb28ec0cSArik Nemtsov 	return pos + sizeof(struct ieee80211_vht_operation);
2963fb28ec0cSArik Nemtsov }
2964fb28ec0cSArik Nemtsov 
296560ad72daSSven Eckelmann u8 *ieee80211_ie_build_he_oper(u8 *pos)
296660ad72daSSven Eckelmann {
296760ad72daSSven Eckelmann 	struct ieee80211_he_operation *he_oper;
296860ad72daSSven Eckelmann 	u32 he_oper_params;
296960ad72daSSven Eckelmann 
297060ad72daSSven Eckelmann 	*pos++ = WLAN_EID_EXTENSION;
297160ad72daSSven Eckelmann 	*pos++ = 1 + sizeof(struct ieee80211_he_operation);
297260ad72daSSven Eckelmann 	*pos++ = WLAN_EID_EXT_HE_OPERATION;
297360ad72daSSven Eckelmann 
297460ad72daSSven Eckelmann 	he_oper_params = 0;
297560ad72daSSven Eckelmann 	he_oper_params |= u32_encode_bits(1023, /* disabled */
297660ad72daSSven Eckelmann 				IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
297760ad72daSSven Eckelmann 	he_oper_params |= u32_encode_bits(1,
297860ad72daSSven Eckelmann 				IEEE80211_HE_OPERATION_ER_SU_DISABLE);
297960ad72daSSven Eckelmann 	he_oper_params |= u32_encode_bits(1,
298060ad72daSSven Eckelmann 				IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
298160ad72daSSven Eckelmann 
298260ad72daSSven Eckelmann 	he_oper = (struct ieee80211_he_operation *)pos;
298360ad72daSSven Eckelmann 	he_oper->he_oper_params = cpu_to_le32(he_oper_params);
298460ad72daSSven Eckelmann 
298560ad72daSSven Eckelmann 	/* don't require special HE peer rates */
298660ad72daSSven Eckelmann 	he_oper->he_mcs_nss_set = cpu_to_le16(0xffff);
298760ad72daSSven Eckelmann 
298860ad72daSSven Eckelmann 	/* TODO add VHT operational and 6GHz operational subelement? */
298960ad72daSSven Eckelmann 
299060ad72daSSven Eckelmann 	return pos + sizeof(struct ieee80211_vht_operation);
299160ad72daSSven Eckelmann }
299260ad72daSSven Eckelmann 
29938ac3c704SJohannes Berg bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
29944bf88530SJohannes Berg 			       struct cfg80211_chan_def *chandef)
299542e7aa77SAlexander Simon {
299642e7aa77SAlexander Simon 	enum nl80211_channel_type channel_type;
299742e7aa77SAlexander Simon 
29988ac3c704SJohannes Berg 	if (!ht_oper)
29998ac3c704SJohannes Berg 		return false;
300042e7aa77SAlexander Simon 
3001074d46d1SJohannes Berg 	switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
300242e7aa77SAlexander Simon 	case IEEE80211_HT_PARAM_CHA_SEC_NONE:
300342e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_HT20;
300442e7aa77SAlexander Simon 		break;
300542e7aa77SAlexander Simon 	case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
300642e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_HT40PLUS;
300742e7aa77SAlexander Simon 		break;
300842e7aa77SAlexander Simon 	case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
300942e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_HT40MINUS;
301042e7aa77SAlexander Simon 		break;
301142e7aa77SAlexander Simon 	default:
301242e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_NO_HT;
30138ac3c704SJohannes Berg 		return false;
301442e7aa77SAlexander Simon 	}
301542e7aa77SAlexander Simon 
30168ac3c704SJohannes Berg 	cfg80211_chandef_create(chandef, chandef->chan, channel_type);
30178ac3c704SJohannes Berg 	return true;
301842e7aa77SAlexander Simon }
301942e7aa77SAlexander Simon 
30207eb26df2SJohannes Berg bool ieee80211_chandef_vht_oper(struct ieee80211_hw *hw,
30217eb26df2SJohannes Berg 				const struct ieee80211_vht_operation *oper,
30227eb26df2SJohannes Berg 				const struct ieee80211_ht_operation *htop,
3023abcff6efSJanusz.Dziedzic@tieto.com 				struct cfg80211_chan_def *chandef)
3024abcff6efSJanusz.Dziedzic@tieto.com {
30258ac3c704SJohannes Berg 	struct cfg80211_chan_def new = *chandef;
30267eb26df2SJohannes Berg 	int cf0, cf1;
30277eb26df2SJohannes Berg 	int ccfs0, ccfs1, ccfs2;
30287eb26df2SJohannes Berg 	int ccf0, ccf1;
302933181ea7SShay Bar 	u32 vht_cap;
303033181ea7SShay Bar 	bool support_80_80 = false;
303133181ea7SShay Bar 	bool support_160 = false;
3032abcff6efSJanusz.Dziedzic@tieto.com 
30337eb26df2SJohannes Berg 	if (!oper || !htop)
30348ac3c704SJohannes Berg 		return false;
30358ac3c704SJohannes Berg 
303633181ea7SShay Bar 	vht_cap = hw->wiphy->bands[chandef->chan->band]->vht_cap.cap;
303733181ea7SShay Bar 	support_160 = (vht_cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK |
303833181ea7SShay Bar 				  IEEE80211_VHT_CAP_EXT_NSS_BW_MASK));
303933181ea7SShay Bar 	support_80_80 = ((vht_cap &
304033181ea7SShay Bar 			 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
304133181ea7SShay Bar 			(vht_cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
304233181ea7SShay Bar 			 vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
304333181ea7SShay Bar 			((vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) >>
304433181ea7SShay Bar 				    IEEE80211_VHT_CAP_EXT_NSS_BW_SHIFT > 1));
30457eb26df2SJohannes Berg 	ccfs0 = oper->center_freq_seg0_idx;
30467eb26df2SJohannes Berg 	ccfs1 = oper->center_freq_seg1_idx;
30477eb26df2SJohannes Berg 	ccfs2 = (le16_to_cpu(htop->operation_mode) &
30487eb26df2SJohannes Berg 				IEEE80211_HT_OP_MODE_CCFS2_MASK)
30497eb26df2SJohannes Berg 			>> IEEE80211_HT_OP_MODE_CCFS2_SHIFT;
30507eb26df2SJohannes Berg 
30517eb26df2SJohannes Berg 	/* when parsing (and we know how to) CCFS1 and CCFS2 are equivalent */
30527eb26df2SJohannes Berg 	ccf0 = ccfs0;
30537eb26df2SJohannes Berg 	ccf1 = ccfs1;
30547eb26df2SJohannes Berg 	if (!ccfs1 && ieee80211_hw_check(hw, SUPPORTS_VHT_EXT_NSS_BW))
30557eb26df2SJohannes Berg 		ccf1 = ccfs2;
30567eb26df2SJohannes Berg 
30577eb26df2SJohannes Berg 	cf0 = ieee80211_channel_to_frequency(ccf0, chandef->chan->band);
30587eb26df2SJohannes Berg 	cf1 = ieee80211_channel_to_frequency(ccf1, chandef->chan->band);
3059abcff6efSJanusz.Dziedzic@tieto.com 
3060abcff6efSJanusz.Dziedzic@tieto.com 	switch (oper->chan_width) {
3061abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_USE_HT:
30627eb26df2SJohannes Berg 		/* just use HT information directly */
3063abcff6efSJanusz.Dziedzic@tieto.com 		break;
3064abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_80MHZ:
30658ac3c704SJohannes Berg 		new.width = NL80211_CHAN_WIDTH_80;
30667eb26df2SJohannes Berg 		new.center_freq1 = cf0;
306723665aafSJouni Malinen 		/* If needed, adjust based on the newer interop workaround. */
30687eb26df2SJohannes Berg 		if (ccf1) {
306923665aafSJouni Malinen 			unsigned int diff;
307023665aafSJouni Malinen 
30717eb26df2SJohannes Berg 			diff = abs(ccf1 - ccf0);
307233181ea7SShay Bar 			if ((diff == 8) && support_160) {
307323665aafSJouni Malinen 				new.width = NL80211_CHAN_WIDTH_160;
30747eb26df2SJohannes Berg 				new.center_freq1 = cf1;
307533181ea7SShay Bar 			} else if ((diff > 8) && support_80_80) {
307623665aafSJouni Malinen 				new.width = NL80211_CHAN_WIDTH_80P80;
30777eb26df2SJohannes Berg 				new.center_freq2 = cf1;
307823665aafSJouni Malinen 			}
307923665aafSJouni Malinen 		}
3080abcff6efSJanusz.Dziedzic@tieto.com 		break;
3081abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_160MHZ:
30827eb26df2SJohannes Berg 		/* deprecated encoding */
30838ac3c704SJohannes Berg 		new.width = NL80211_CHAN_WIDTH_160;
30847eb26df2SJohannes Berg 		new.center_freq1 = cf0;
3085abcff6efSJanusz.Dziedzic@tieto.com 		break;
3086abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
30877eb26df2SJohannes Berg 		/* deprecated encoding */
30888ac3c704SJohannes Berg 		new.width = NL80211_CHAN_WIDTH_80P80;
30897eb26df2SJohannes Berg 		new.center_freq1 = cf0;
30907eb26df2SJohannes Berg 		new.center_freq2 = cf1;
3091abcff6efSJanusz.Dziedzic@tieto.com 		break;
3092abcff6efSJanusz.Dziedzic@tieto.com 	default:
30938ac3c704SJohannes Berg 		return false;
3094abcff6efSJanusz.Dziedzic@tieto.com 	}
3095abcff6efSJanusz.Dziedzic@tieto.com 
30968ac3c704SJohannes Berg 	if (!cfg80211_chandef_valid(&new))
30978ac3c704SJohannes Berg 		return false;
30988ac3c704SJohannes Berg 
30998ac3c704SJohannes Berg 	*chandef = new;
31008ac3c704SJohannes Berg 	return true;
3101abcff6efSJanusz.Dziedzic@tieto.com }
3102abcff6efSJanusz.Dziedzic@tieto.com 
31032103dec1SSimon Wunderlich int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
31042103dec1SSimon Wunderlich 			     const struct ieee80211_supported_band *sband,
31052103dec1SSimon Wunderlich 			     const u8 *srates, int srates_len, u32 *rates)
31062103dec1SSimon Wunderlich {
31072103dec1SSimon Wunderlich 	u32 rate_flags = ieee80211_chandef_rate_flags(chandef);
31082103dec1SSimon Wunderlich 	int shift = ieee80211_chandef_get_shift(chandef);
31092103dec1SSimon Wunderlich 	struct ieee80211_rate *br;
31102103dec1SSimon Wunderlich 	int brate, rate, i, j, count = 0;
31112103dec1SSimon Wunderlich 
31122103dec1SSimon Wunderlich 	*rates = 0;
31132103dec1SSimon Wunderlich 
31142103dec1SSimon Wunderlich 	for (i = 0; i < srates_len; i++) {
31152103dec1SSimon Wunderlich 		rate = srates[i] & 0x7f;
31162103dec1SSimon Wunderlich 
31172103dec1SSimon Wunderlich 		for (j = 0; j < sband->n_bitrates; j++) {
31182103dec1SSimon Wunderlich 			br = &sband->bitrates[j];
31192103dec1SSimon Wunderlich 			if ((rate_flags & br->flags) != rate_flags)
31202103dec1SSimon Wunderlich 				continue;
31212103dec1SSimon Wunderlich 
31222103dec1SSimon Wunderlich 			brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
31232103dec1SSimon Wunderlich 			if (brate == rate) {
31242103dec1SSimon Wunderlich 				*rates |= BIT(j);
31252103dec1SSimon Wunderlich 				count++;
31262103dec1SSimon Wunderlich 				break;
31272103dec1SSimon Wunderlich 			}
31282103dec1SSimon Wunderlich 		}
31292103dec1SSimon Wunderlich 	}
31302103dec1SSimon Wunderlich 	return count;
31312103dec1SSimon Wunderlich }
31322103dec1SSimon Wunderlich 
3133fc8a7321SJohannes Berg int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
31346b77863bSJohannes Berg 			    struct sk_buff *skb, bool need_basic,
313557fbcce3SJohannes Berg 			    enum nl80211_band band)
3136768db343SArik Nemtsov {
3137768db343SArik Nemtsov 	struct ieee80211_local *local = sdata->local;
3138768db343SArik Nemtsov 	struct ieee80211_supported_band *sband;
31392103dec1SSimon Wunderlich 	int rate, shift;
3140768db343SArik Nemtsov 	u8 i, rates, *pos;
3141fc8a7321SJohannes Berg 	u32 basic_rates = sdata->vif.bss_conf.basic_rates;
31422103dec1SSimon Wunderlich 	u32 rate_flags;
3143768db343SArik Nemtsov 
31442103dec1SSimon Wunderlich 	shift = ieee80211_vif_get_shift(&sdata->vif);
31452103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
31466b77863bSJohannes Berg 	sband = local->hw.wiphy->bands[band];
31472103dec1SSimon Wunderlich 	rates = 0;
31482103dec1SSimon Wunderlich 	for (i = 0; i < sband->n_bitrates; i++) {
31492103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
31502103dec1SSimon Wunderlich 			continue;
31512103dec1SSimon Wunderlich 		rates++;
31522103dec1SSimon Wunderlich 	}
3153768db343SArik Nemtsov 	if (rates > 8)
3154768db343SArik Nemtsov 		rates = 8;
3155768db343SArik Nemtsov 
3156768db343SArik Nemtsov 	if (skb_tailroom(skb) < rates + 2)
3157768db343SArik Nemtsov 		return -ENOMEM;
3158768db343SArik Nemtsov 
3159768db343SArik Nemtsov 	pos = skb_put(skb, rates + 2);
3160768db343SArik Nemtsov 	*pos++ = WLAN_EID_SUPP_RATES;
3161768db343SArik Nemtsov 	*pos++ = rates;
3162768db343SArik Nemtsov 	for (i = 0; i < rates; i++) {
3163657c3e0cSAshok Nagarajan 		u8 basic = 0;
31642103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
31652103dec1SSimon Wunderlich 			continue;
31662103dec1SSimon Wunderlich 
3167657c3e0cSAshok Nagarajan 		if (need_basic && basic_rates & BIT(i))
3168657c3e0cSAshok Nagarajan 			basic = 0x80;
31692103dec1SSimon Wunderlich 		rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
31702103dec1SSimon Wunderlich 				    5 * (1 << shift));
31712103dec1SSimon Wunderlich 		*pos++ = basic | (u8) rate;
3172768db343SArik Nemtsov 	}
3173768db343SArik Nemtsov 
3174768db343SArik Nemtsov 	return 0;
3175768db343SArik Nemtsov }
3176768db343SArik Nemtsov 
3177fc8a7321SJohannes Berg int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
31786b77863bSJohannes Berg 				struct sk_buff *skb, bool need_basic,
317957fbcce3SJohannes Berg 				enum nl80211_band band)
3180768db343SArik Nemtsov {
3181768db343SArik Nemtsov 	struct ieee80211_local *local = sdata->local;
3182768db343SArik Nemtsov 	struct ieee80211_supported_band *sband;
3183cc63ec76SChun-Yeow Yeoh 	int rate, shift;
3184768db343SArik Nemtsov 	u8 i, exrates, *pos;
3185fc8a7321SJohannes Berg 	u32 basic_rates = sdata->vif.bss_conf.basic_rates;
31862103dec1SSimon Wunderlich 	u32 rate_flags;
31872103dec1SSimon Wunderlich 
31882103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
31892103dec1SSimon Wunderlich 	shift = ieee80211_vif_get_shift(&sdata->vif);
3190768db343SArik Nemtsov 
31916b77863bSJohannes Berg 	sband = local->hw.wiphy->bands[band];
31922103dec1SSimon Wunderlich 	exrates = 0;
31932103dec1SSimon Wunderlich 	for (i = 0; i < sband->n_bitrates; i++) {
31942103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
31952103dec1SSimon Wunderlich 			continue;
31962103dec1SSimon Wunderlich 		exrates++;
31972103dec1SSimon Wunderlich 	}
31982103dec1SSimon Wunderlich 
3199768db343SArik Nemtsov 	if (exrates > 8)
3200768db343SArik Nemtsov 		exrates -= 8;
3201768db343SArik Nemtsov 	else
3202768db343SArik Nemtsov 		exrates = 0;
3203768db343SArik Nemtsov 
3204768db343SArik Nemtsov 	if (skb_tailroom(skb) < exrates + 2)
3205768db343SArik Nemtsov 		return -ENOMEM;
3206768db343SArik Nemtsov 
3207768db343SArik Nemtsov 	if (exrates) {
3208768db343SArik Nemtsov 		pos = skb_put(skb, exrates + 2);
3209768db343SArik Nemtsov 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
3210768db343SArik Nemtsov 		*pos++ = exrates;
3211768db343SArik Nemtsov 		for (i = 8; i < sband->n_bitrates; i++) {
3212657c3e0cSAshok Nagarajan 			u8 basic = 0;
32132103dec1SSimon Wunderlich 			if ((rate_flags & sband->bitrates[i].flags)
32142103dec1SSimon Wunderlich 			    != rate_flags)
32152103dec1SSimon Wunderlich 				continue;
3216657c3e0cSAshok Nagarajan 			if (need_basic && basic_rates & BIT(i))
3217657c3e0cSAshok Nagarajan 				basic = 0x80;
32182103dec1SSimon Wunderlich 			rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
32192103dec1SSimon Wunderlich 					    5 * (1 << shift));
32202103dec1SSimon Wunderlich 			*pos++ = basic | (u8) rate;
3221768db343SArik Nemtsov 		}
3222768db343SArik Nemtsov 	}
3223768db343SArik Nemtsov 	return 0;
3224768db343SArik Nemtsov }
32251dae27f8SWey-Yi Guy 
32261dae27f8SWey-Yi Guy int ieee80211_ave_rssi(struct ieee80211_vif *vif)
32271dae27f8SWey-Yi Guy {
32281dae27f8SWey-Yi Guy 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
32291dae27f8SWey-Yi Guy 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
32301dae27f8SWey-Yi Guy 
3231be6bcabcSWey-Yi Guy 	if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
3232be6bcabcSWey-Yi Guy 		/* non-managed type inferfaces */
3233be6bcabcSWey-Yi Guy 		return 0;
3234be6bcabcSWey-Yi Guy 	}
3235338c17aeSJohannes Berg 	return -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
32361dae27f8SWey-Yi Guy }
32370d8a0a17SWey-Yi Guy EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
323804ecd257SJohannes Berg 
323904ecd257SJohannes Berg u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
324004ecd257SJohannes Berg {
324104ecd257SJohannes Berg 	if (!mcs)
324204ecd257SJohannes Berg 		return 1;
324304ecd257SJohannes Berg 
324404ecd257SJohannes Berg 	/* TODO: consider rx_highest */
324504ecd257SJohannes Berg 
324604ecd257SJohannes Berg 	if (mcs->rx_mask[3])
324704ecd257SJohannes Berg 		return 4;
324804ecd257SJohannes Berg 	if (mcs->rx_mask[2])
324904ecd257SJohannes Berg 		return 3;
325004ecd257SJohannes Berg 	if (mcs->rx_mask[1])
325104ecd257SJohannes Berg 		return 2;
325204ecd257SJohannes Berg 	return 1;
325304ecd257SJohannes Berg }
3254f4bda337SThomas Pedersen 
3255f4bda337SThomas Pedersen /**
3256f4bda337SThomas Pedersen  * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
3257f4bda337SThomas Pedersen  * @local: mac80211 hw info struct
3258f4bda337SThomas Pedersen  * @status: RX status
3259f4bda337SThomas Pedersen  * @mpdu_len: total MPDU length (including FCS)
3260f4bda337SThomas Pedersen  * @mpdu_offset: offset into MPDU to calculate timestamp at
3261f4bda337SThomas Pedersen  *
3262f4bda337SThomas Pedersen  * This function calculates the RX timestamp at the given MPDU offset, taking
3263f4bda337SThomas Pedersen  * into account what the RX timestamp was. An offset of 0 will just normalize
3264f4bda337SThomas Pedersen  * the timestamp to TSF at beginning of MPDU reception.
3265f4bda337SThomas Pedersen  */
3266f4bda337SThomas Pedersen u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
3267f4bda337SThomas Pedersen 				     struct ieee80211_rx_status *status,
3268f4bda337SThomas Pedersen 				     unsigned int mpdu_len,
3269f4bda337SThomas Pedersen 				     unsigned int mpdu_offset)
3270f4bda337SThomas Pedersen {
3271f4bda337SThomas Pedersen 	u64 ts = status->mactime;
3272f4bda337SThomas Pedersen 	struct rate_info ri;
3273f4bda337SThomas Pedersen 	u16 rate;
3274f4bda337SThomas Pedersen 
3275f4bda337SThomas Pedersen 	if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
3276f4bda337SThomas Pedersen 		return 0;
3277f4bda337SThomas Pedersen 
3278f4bda337SThomas Pedersen 	memset(&ri, 0, sizeof(ri));
3279f4bda337SThomas Pedersen 
328035f4962cSJohannes Berg 	ri.bw = status->bw;
328135f4962cSJohannes Berg 
3282f4bda337SThomas Pedersen 	/* Fill cfg80211 rate info */
3283da6a4352SJohannes Berg 	switch (status->encoding) {
3284da6a4352SJohannes Berg 	case RX_ENC_HT:
3285f4bda337SThomas Pedersen 		ri.mcs = status->rate_idx;
3286f4bda337SThomas Pedersen 		ri.flags |= RATE_INFO_FLAGS_MCS;
32877fdd69c5SJohannes Berg 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
3288f4bda337SThomas Pedersen 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
3289da6a4352SJohannes Berg 		break;
3290da6a4352SJohannes Berg 	case RX_ENC_VHT:
32915614618eSJohannes Berg 		ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
32925614618eSJohannes Berg 		ri.mcs = status->rate_idx;
32938613c948SJohannes Berg 		ri.nss = status->nss;
32947fdd69c5SJohannes Berg 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
32955614618eSJohannes Berg 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
3296da6a4352SJohannes Berg 		break;
3297da6a4352SJohannes Berg 	default:
3298da6a4352SJohannes Berg 		WARN_ON(1);
3299da6a4352SJohannes Berg 		/* fall through */
3300da6a4352SJohannes Berg 	case RX_ENC_LEGACY: {
3301f4bda337SThomas Pedersen 		struct ieee80211_supported_band *sband;
33022103dec1SSimon Wunderlich 		int shift = 0;
33032103dec1SSimon Wunderlich 		int bitrate;
33042103dec1SSimon Wunderlich 
3305da6a4352SJohannes Berg 		switch (status->bw) {
3306da6a4352SJohannes Berg 		case RATE_INFO_BW_10:
33072103dec1SSimon Wunderlich 			shift = 1;
3308da6a4352SJohannes Berg 			break;
3309da6a4352SJohannes Berg 		case RATE_INFO_BW_5:
33102103dec1SSimon Wunderlich 			shift = 2;
3311da6a4352SJohannes Berg 			break;
3312b51f3beeSJohannes Berg 		}
3313f4bda337SThomas Pedersen 
3314f4bda337SThomas Pedersen 		sband = local->hw.wiphy->bands[status->band];
33152103dec1SSimon Wunderlich 		bitrate = sband->bitrates[status->rate_idx].bitrate;
33162103dec1SSimon Wunderlich 		ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
3317f4a0f0c5SJohannes Berg 
3318f4a0f0c5SJohannes Berg 		if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
3319f4a0f0c5SJohannes Berg 			/* TODO: handle HT/VHT preambles */
332057fbcce3SJohannes Berg 			if (status->band == NL80211_BAND_5GHZ) {
3321f4a0f0c5SJohannes Berg 				ts += 20 << shift;
3322f4a0f0c5SJohannes Berg 				mpdu_offset += 2;
33237fdd69c5SJohannes Berg 			} else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
3324f4a0f0c5SJohannes Berg 				ts += 96;
3325f4a0f0c5SJohannes Berg 			} else {
3326f4a0f0c5SJohannes Berg 				ts += 192;
3327f4a0f0c5SJohannes Berg 			}
3328f4a0f0c5SJohannes Berg 		}
3329da6a4352SJohannes Berg 		break;
3330da6a4352SJohannes Berg 		}
3331f4bda337SThomas Pedersen 	}
3332f4bda337SThomas Pedersen 
3333f4bda337SThomas Pedersen 	rate = cfg80211_calculate_bitrate(&ri);
3334d86aa4f8SJohannes Berg 	if (WARN_ONCE(!rate,
3335f980ebc0SSara Sharon 		      "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
3336f980ebc0SSara Sharon 		      (unsigned long long)status->flag, status->rate_idx,
33378613c948SJohannes Berg 		      status->nss))
3338d86aa4f8SJohannes Berg 		return 0;
3339f4bda337SThomas Pedersen 
3340f4bda337SThomas Pedersen 	/* rewind from end of MPDU */
3341f4bda337SThomas Pedersen 	if (status->flag & RX_FLAG_MACTIME_END)
3342f4bda337SThomas Pedersen 		ts -= mpdu_len * 8 * 10 / rate;
3343f4bda337SThomas Pedersen 
3344f4bda337SThomas Pedersen 	ts += mpdu_offset * 8 * 10 / rate;
3345f4bda337SThomas Pedersen 
3346f4bda337SThomas Pedersen 	return ts;
3347f4bda337SThomas Pedersen }
3348164eb02dSSimon Wunderlich 
3349164eb02dSSimon Wunderlich void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
3350164eb02dSSimon Wunderlich {
3351164eb02dSSimon Wunderlich 	struct ieee80211_sub_if_data *sdata;
3352d2859df5SJanusz Dziedzic 	struct cfg80211_chan_def chandef;
3353164eb02dSSimon Wunderlich 
33544a199068SJohannes Berg 	/* for interface list, to avoid linking iflist_mtx and chanctx_mtx */
33554a199068SJohannes Berg 	ASSERT_RTNL();
33564a199068SJohannes Berg 
335734a3740dSJohannes Berg 	mutex_lock(&local->mtx);
3358164eb02dSSimon Wunderlich 	list_for_each_entry(sdata, &local->interfaces, list) {
335934a3740dSJohannes Berg 		/* it might be waiting for the local->mtx, but then
336034a3740dSJohannes Berg 		 * by the time it gets it, sdata->wdev.cac_started
336134a3740dSJohannes Berg 		 * will no longer be true
336234a3740dSJohannes Berg 		 */
336334a3740dSJohannes Berg 		cancel_delayed_work(&sdata->dfs_cac_timer_work);
3364164eb02dSSimon Wunderlich 
3365164eb02dSSimon Wunderlich 		if (sdata->wdev.cac_started) {
3366d2859df5SJanusz Dziedzic 			chandef = sdata->vif.bss_conf.chandef;
3367164eb02dSSimon Wunderlich 			ieee80211_vif_release_channel(sdata);
3368164eb02dSSimon Wunderlich 			cfg80211_cac_event(sdata->dev,
3369d2859df5SJanusz Dziedzic 					   &chandef,
3370164eb02dSSimon Wunderlich 					   NL80211_RADAR_CAC_ABORTED,
3371164eb02dSSimon Wunderlich 					   GFP_KERNEL);
3372164eb02dSSimon Wunderlich 		}
3373164eb02dSSimon Wunderlich 	}
337434a3740dSJohannes Berg 	mutex_unlock(&local->mtx);
3375164eb02dSSimon Wunderlich }
3376164eb02dSSimon Wunderlich 
3377164eb02dSSimon Wunderlich void ieee80211_dfs_radar_detected_work(struct work_struct *work)
3378164eb02dSSimon Wunderlich {
3379164eb02dSSimon Wunderlich 	struct ieee80211_local *local =
3380164eb02dSSimon Wunderlich 		container_of(work, struct ieee80211_local, radar_detected_work);
338184a3d1c9SJanusz Dziedzic 	struct cfg80211_chan_def chandef = local->hw.conf.chandef;
3382486cf4c0SMichal Kazior 	struct ieee80211_chanctx *ctx;
3383486cf4c0SMichal Kazior 	int num_chanctx = 0;
3384486cf4c0SMichal Kazior 
3385486cf4c0SMichal Kazior 	mutex_lock(&local->chanctx_mtx);
3386486cf4c0SMichal Kazior 	list_for_each_entry(ctx, &local->chanctx_list, list) {
3387486cf4c0SMichal Kazior 		if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
3388486cf4c0SMichal Kazior 			continue;
3389486cf4c0SMichal Kazior 
3390486cf4c0SMichal Kazior 		num_chanctx++;
3391486cf4c0SMichal Kazior 		chandef = ctx->conf.def;
3392486cf4c0SMichal Kazior 	}
3393486cf4c0SMichal Kazior 	mutex_unlock(&local->chanctx_mtx);
3394164eb02dSSimon Wunderlich 
33954a199068SJohannes Berg 	rtnl_lock();
3396164eb02dSSimon Wunderlich 	ieee80211_dfs_cac_cancel(local);
33974a199068SJohannes Berg 	rtnl_unlock();
3398164eb02dSSimon Wunderlich 
3399486cf4c0SMichal Kazior 	if (num_chanctx > 1)
3400486cf4c0SMichal Kazior 		/* XXX: multi-channel is not supported yet */
3401164eb02dSSimon Wunderlich 		WARN_ON(1);
340284a3d1c9SJanusz Dziedzic 	else
3403164eb02dSSimon Wunderlich 		cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
3404164eb02dSSimon Wunderlich }
3405164eb02dSSimon Wunderlich 
3406164eb02dSSimon Wunderlich void ieee80211_radar_detected(struct ieee80211_hw *hw)
3407164eb02dSSimon Wunderlich {
3408164eb02dSSimon Wunderlich 	struct ieee80211_local *local = hw_to_local(hw);
3409164eb02dSSimon Wunderlich 
3410164eb02dSSimon Wunderlich 	trace_api_radar_detected(local);
3411164eb02dSSimon Wunderlich 
34124a199068SJohannes Berg 	schedule_work(&local->radar_detected_work);
3413164eb02dSSimon Wunderlich }
3414164eb02dSSimon Wunderlich EXPORT_SYMBOL(ieee80211_radar_detected);
3415e6b7cde4SSimon Wunderlich 
3416e6b7cde4SSimon Wunderlich u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
3417e6b7cde4SSimon Wunderlich {
3418e6b7cde4SSimon Wunderlich 	u32 ret;
3419e6b7cde4SSimon Wunderlich 	int tmp;
3420e6b7cde4SSimon Wunderlich 
3421e6b7cde4SSimon Wunderlich 	switch (c->width) {
3422e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_20:
3423e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_20_NOHT;
3424e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3425e6b7cde4SSimon Wunderlich 		break;
3426e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_40:
3427e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_20;
3428e6b7cde4SSimon Wunderlich 		c->center_freq1 = c->chan->center_freq;
3429e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_40MHZ |
3430e6b7cde4SSimon Wunderlich 		      IEEE80211_STA_DISABLE_VHT;
3431e6b7cde4SSimon Wunderlich 		break;
3432e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_80:
3433e6b7cde4SSimon Wunderlich 		tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
3434e6b7cde4SSimon Wunderlich 		/* n_P40 */
3435e6b7cde4SSimon Wunderlich 		tmp /= 2;
3436e6b7cde4SSimon Wunderlich 		/* freq_P40 */
3437e6b7cde4SSimon Wunderlich 		c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
3438e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_40;
3439e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_VHT;
3440e6b7cde4SSimon Wunderlich 		break;
3441e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_80P80:
3442e6b7cde4SSimon Wunderlich 		c->center_freq2 = 0;
3443e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_80;
3444e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_80P80MHZ |
3445e6b7cde4SSimon Wunderlich 		      IEEE80211_STA_DISABLE_160MHZ;
3446e6b7cde4SSimon Wunderlich 		break;
3447e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_160:
3448e6b7cde4SSimon Wunderlich 		/* n_P20 */
3449e6b7cde4SSimon Wunderlich 		tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
3450e6b7cde4SSimon Wunderlich 		/* n_P80 */
3451e6b7cde4SSimon Wunderlich 		tmp /= 4;
3452e6b7cde4SSimon Wunderlich 		c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
3453e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_80;
3454e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_80P80MHZ |
3455e6b7cde4SSimon Wunderlich 		      IEEE80211_STA_DISABLE_160MHZ;
3456e6b7cde4SSimon Wunderlich 		break;
3457e6b7cde4SSimon Wunderlich 	default:
3458e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_20_NOHT:
3459e6b7cde4SSimon Wunderlich 		WARN_ON_ONCE(1);
3460e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_20_NOHT;
3461e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3462e6b7cde4SSimon Wunderlich 		break;
3463e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_5:
3464e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_10:
3465e6b7cde4SSimon Wunderlich 		WARN_ON_ONCE(1);
3466e6b7cde4SSimon Wunderlich 		/* keep c->width */
3467e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3468e6b7cde4SSimon Wunderlich 		break;
3469e6b7cde4SSimon Wunderlich 	}
3470e6b7cde4SSimon Wunderlich 
3471e6b7cde4SSimon Wunderlich 	WARN_ON_ONCE(!cfg80211_chandef_valid(c));
3472e6b7cde4SSimon Wunderlich 
3473e6b7cde4SSimon Wunderlich 	return ret;
3474e6b7cde4SSimon Wunderlich }
3475687da132SEmmanuel Grumbach 
3476687da132SEmmanuel Grumbach /*
3477687da132SEmmanuel Grumbach  * Returns true if smps_mode_new is strictly more restrictive than
3478687da132SEmmanuel Grumbach  * smps_mode_old.
3479687da132SEmmanuel Grumbach  */
3480687da132SEmmanuel Grumbach bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
3481687da132SEmmanuel Grumbach 				   enum ieee80211_smps_mode smps_mode_new)
3482687da132SEmmanuel Grumbach {
3483687da132SEmmanuel Grumbach 	if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
3484687da132SEmmanuel Grumbach 			 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
3485687da132SEmmanuel Grumbach 		return false;
3486687da132SEmmanuel Grumbach 
3487687da132SEmmanuel Grumbach 	switch (smps_mode_old) {
3488687da132SEmmanuel Grumbach 	case IEEE80211_SMPS_STATIC:
3489687da132SEmmanuel Grumbach 		return false;
3490687da132SEmmanuel Grumbach 	case IEEE80211_SMPS_DYNAMIC:
3491687da132SEmmanuel Grumbach 		return smps_mode_new == IEEE80211_SMPS_STATIC;
3492687da132SEmmanuel Grumbach 	case IEEE80211_SMPS_OFF:
3493687da132SEmmanuel Grumbach 		return smps_mode_new != IEEE80211_SMPS_OFF;
3494687da132SEmmanuel Grumbach 	default:
3495687da132SEmmanuel Grumbach 		WARN_ON(1);
3496687da132SEmmanuel Grumbach 	}
3497687da132SEmmanuel Grumbach 
3498687da132SEmmanuel Grumbach 	return false;
3499687da132SEmmanuel Grumbach }
3500c6da674aSChun-Yeow Yeoh 
3501c6da674aSChun-Yeow Yeoh int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
3502c6da674aSChun-Yeow Yeoh 			      struct cfg80211_csa_settings *csa_settings)
3503c6da674aSChun-Yeow Yeoh {
3504c6da674aSChun-Yeow Yeoh 	struct sk_buff *skb;
3505c6da674aSChun-Yeow Yeoh 	struct ieee80211_mgmt *mgmt;
3506c6da674aSChun-Yeow Yeoh 	struct ieee80211_local *local = sdata->local;
3507c6da674aSChun-Yeow Yeoh 	int freq;
35084c121fd6SJohannes Berg 	int hdr_len = offsetofend(struct ieee80211_mgmt,
35094c121fd6SJohannes Berg 				  u.action.u.chan_switch);
3510c6da674aSChun-Yeow Yeoh 	u8 *pos;
3511c6da674aSChun-Yeow Yeoh 
3512c6da674aSChun-Yeow Yeoh 	if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3513c6da674aSChun-Yeow Yeoh 	    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3514c6da674aSChun-Yeow Yeoh 		return -EOPNOTSUPP;
3515c6da674aSChun-Yeow Yeoh 
3516c6da674aSChun-Yeow Yeoh 	skb = dev_alloc_skb(local->tx_headroom + hdr_len +
3517c6da674aSChun-Yeow Yeoh 			    5 + /* channel switch announcement element */
3518c6da674aSChun-Yeow Yeoh 			    3 + /* secondary channel offset element */
351975d627d5SSimon Wunderlich 			    5 + /* wide bandwidth channel switch announcement */
3520c6da674aSChun-Yeow Yeoh 			    8); /* mesh channel switch parameters element */
3521c6da674aSChun-Yeow Yeoh 	if (!skb)
3522c6da674aSChun-Yeow Yeoh 		return -ENOMEM;
3523c6da674aSChun-Yeow Yeoh 
3524c6da674aSChun-Yeow Yeoh 	skb_reserve(skb, local->tx_headroom);
3525b080db58SJohannes Berg 	mgmt = skb_put_zero(skb, hdr_len);
3526c6da674aSChun-Yeow Yeoh 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3527c6da674aSChun-Yeow Yeoh 					  IEEE80211_STYPE_ACTION);
3528c6da674aSChun-Yeow Yeoh 
3529c6da674aSChun-Yeow Yeoh 	eth_broadcast_addr(mgmt->da);
3530c6da674aSChun-Yeow Yeoh 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
3531c6da674aSChun-Yeow Yeoh 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
3532c6da674aSChun-Yeow Yeoh 		memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
3533c6da674aSChun-Yeow Yeoh 	} else {
3534c6da674aSChun-Yeow Yeoh 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
3535c6da674aSChun-Yeow Yeoh 		memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
3536c6da674aSChun-Yeow Yeoh 	}
3537c6da674aSChun-Yeow Yeoh 	mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
3538c6da674aSChun-Yeow Yeoh 	mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
3539c6da674aSChun-Yeow Yeoh 	pos = skb_put(skb, 5);
3540c6da674aSChun-Yeow Yeoh 	*pos++ = WLAN_EID_CHANNEL_SWITCH;			/* EID */
3541c6da674aSChun-Yeow Yeoh 	*pos++ = 3;						/* IE length */
3542c6da674aSChun-Yeow Yeoh 	*pos++ = csa_settings->block_tx ? 1 : 0;		/* CSA mode */
3543c6da674aSChun-Yeow Yeoh 	freq = csa_settings->chandef.chan->center_freq;
3544c6da674aSChun-Yeow Yeoh 	*pos++ = ieee80211_frequency_to_channel(freq);		/* channel */
3545c6da674aSChun-Yeow Yeoh 	*pos++ = csa_settings->count;				/* count */
3546c6da674aSChun-Yeow Yeoh 
3547c6da674aSChun-Yeow Yeoh 	if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
3548c6da674aSChun-Yeow Yeoh 		enum nl80211_channel_type ch_type;
3549c6da674aSChun-Yeow Yeoh 
3550c6da674aSChun-Yeow Yeoh 		skb_put(skb, 3);
3551c6da674aSChun-Yeow Yeoh 		*pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;	/* EID */
3552c6da674aSChun-Yeow Yeoh 		*pos++ = 1;					/* IE length */
3553c6da674aSChun-Yeow Yeoh 		ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
3554c6da674aSChun-Yeow Yeoh 		if (ch_type == NL80211_CHAN_HT40PLUS)
3555c6da674aSChun-Yeow Yeoh 			*pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
3556c6da674aSChun-Yeow Yeoh 		else
3557c6da674aSChun-Yeow Yeoh 			*pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
3558c6da674aSChun-Yeow Yeoh 	}
3559c6da674aSChun-Yeow Yeoh 
3560c6da674aSChun-Yeow Yeoh 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
3561c6da674aSChun-Yeow Yeoh 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
3562c6da674aSChun-Yeow Yeoh 
3563c6da674aSChun-Yeow Yeoh 		skb_put(skb, 8);
3564c6da674aSChun-Yeow Yeoh 		*pos++ = WLAN_EID_CHAN_SWITCH_PARAM;		/* EID */
3565c6da674aSChun-Yeow Yeoh 		*pos++ = 6;					/* IE length */
3566c6da674aSChun-Yeow Yeoh 		*pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL;	/* Mesh TTL */
3567c6da674aSChun-Yeow Yeoh 		*pos = 0x00;	/* Mesh Flag: Tx Restrict, Initiator, Reason */
3568c6da674aSChun-Yeow Yeoh 		*pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
3569c6da674aSChun-Yeow Yeoh 		*pos++ |= csa_settings->block_tx ?
3570c6da674aSChun-Yeow Yeoh 			  WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
3571c6da674aSChun-Yeow Yeoh 		put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
3572c6da674aSChun-Yeow Yeoh 		pos += 2;
3573ca91dc97SChun-Yeow Yeoh 		put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
3574c6da674aSChun-Yeow Yeoh 		pos += 2;
3575c6da674aSChun-Yeow Yeoh 	}
3576c6da674aSChun-Yeow Yeoh 
357775d627d5SSimon Wunderlich 	if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 ||
357875d627d5SSimon Wunderlich 	    csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 ||
357975d627d5SSimon Wunderlich 	    csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) {
358075d627d5SSimon Wunderlich 		skb_put(skb, 5);
358175d627d5SSimon Wunderlich 		ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef);
358275d627d5SSimon Wunderlich 	}
358375d627d5SSimon Wunderlich 
3584c6da674aSChun-Yeow Yeoh 	ieee80211_tx_skb(sdata, skb);
3585c6da674aSChun-Yeow Yeoh 	return 0;
3586c6da674aSChun-Yeow Yeoh }
35872475b1ccSMax Stepanov 
35882475b1ccSMax Stepanov bool ieee80211_cs_valid(const struct ieee80211_cipher_scheme *cs)
35892475b1ccSMax Stepanov {
35902475b1ccSMax Stepanov 	return !(cs == NULL || cs->cipher == 0 ||
35912475b1ccSMax Stepanov 		 cs->hdr_len < cs->pn_len + cs->pn_off ||
35922475b1ccSMax Stepanov 		 cs->hdr_len <= cs->key_idx_off ||
35932475b1ccSMax Stepanov 		 cs->key_idx_shift > 7 ||
35942475b1ccSMax Stepanov 		 cs->key_idx_mask == 0);
35952475b1ccSMax Stepanov }
35962475b1ccSMax Stepanov 
35972475b1ccSMax Stepanov bool ieee80211_cs_list_valid(const struct ieee80211_cipher_scheme *cs, int n)
35982475b1ccSMax Stepanov {
35992475b1ccSMax Stepanov 	int i;
36002475b1ccSMax Stepanov 
36012475b1ccSMax Stepanov 	/* Ensure we have enough iftype bitmap space for all iftype values */
36022475b1ccSMax Stepanov 	WARN_ON((NUM_NL80211_IFTYPES / 8 + 1) > sizeof(cs[0].iftype));
36032475b1ccSMax Stepanov 
36042475b1ccSMax Stepanov 	for (i = 0; i < n; i++)
36052475b1ccSMax Stepanov 		if (!ieee80211_cs_valid(&cs[i]))
36062475b1ccSMax Stepanov 			return false;
36072475b1ccSMax Stepanov 
36082475b1ccSMax Stepanov 	return true;
36092475b1ccSMax Stepanov }
36102475b1ccSMax Stepanov 
36112475b1ccSMax Stepanov const struct ieee80211_cipher_scheme *
36122475b1ccSMax Stepanov ieee80211_cs_get(struct ieee80211_local *local, u32 cipher,
36132475b1ccSMax Stepanov 		 enum nl80211_iftype iftype)
36142475b1ccSMax Stepanov {
36152475b1ccSMax Stepanov 	const struct ieee80211_cipher_scheme *l = local->hw.cipher_schemes;
36162475b1ccSMax Stepanov 	int n = local->hw.n_cipher_schemes;
36172475b1ccSMax Stepanov 	int i;
36182475b1ccSMax Stepanov 	const struct ieee80211_cipher_scheme *cs = NULL;
36192475b1ccSMax Stepanov 
36202475b1ccSMax Stepanov 	for (i = 0; i < n; i++) {
36212475b1ccSMax Stepanov 		if (l[i].cipher == cipher) {
36222475b1ccSMax Stepanov 			cs = &l[i];
36232475b1ccSMax Stepanov 			break;
36242475b1ccSMax Stepanov 		}
36252475b1ccSMax Stepanov 	}
36262475b1ccSMax Stepanov 
36272475b1ccSMax Stepanov 	if (!cs || !(cs->iftype & BIT(iftype)))
36282475b1ccSMax Stepanov 		return NULL;
36292475b1ccSMax Stepanov 
36302475b1ccSMax Stepanov 	return cs;
36312475b1ccSMax Stepanov }
36322475b1ccSMax Stepanov 
36332475b1ccSMax Stepanov int ieee80211_cs_headroom(struct ieee80211_local *local,
36342475b1ccSMax Stepanov 			  struct cfg80211_crypto_settings *crypto,
36352475b1ccSMax Stepanov 			  enum nl80211_iftype iftype)
36362475b1ccSMax Stepanov {
36372475b1ccSMax Stepanov 	const struct ieee80211_cipher_scheme *cs;
36382475b1ccSMax Stepanov 	int headroom = IEEE80211_ENCRYPT_HEADROOM;
36392475b1ccSMax Stepanov 	int i;
36402475b1ccSMax Stepanov 
36412475b1ccSMax Stepanov 	for (i = 0; i < crypto->n_ciphers_pairwise; i++) {
36422475b1ccSMax Stepanov 		cs = ieee80211_cs_get(local, crypto->ciphers_pairwise[i],
36432475b1ccSMax Stepanov 				      iftype);
36442475b1ccSMax Stepanov 
36452475b1ccSMax Stepanov 		if (cs && headroom < cs->hdr_len)
36462475b1ccSMax Stepanov 			headroom = cs->hdr_len;
36472475b1ccSMax Stepanov 	}
36482475b1ccSMax Stepanov 
36492475b1ccSMax Stepanov 	cs = ieee80211_cs_get(local, crypto->cipher_group, iftype);
36502475b1ccSMax Stepanov 	if (cs && headroom < cs->hdr_len)
36512475b1ccSMax Stepanov 		headroom = cs->hdr_len;
36522475b1ccSMax Stepanov 
36532475b1ccSMax Stepanov 	return headroom;
36542475b1ccSMax Stepanov }
3655a7022e65SFelix Fietkau 
3656a7022e65SFelix Fietkau static bool
3657a7022e65SFelix Fietkau ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
3658a7022e65SFelix Fietkau {
3659a7022e65SFelix Fietkau 	s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
3660a7022e65SFelix Fietkau 	int skip;
3661a7022e65SFelix Fietkau 
3662a7022e65SFelix Fietkau 	if (end > 0)
3663a7022e65SFelix Fietkau 		return false;
3664a7022e65SFelix Fietkau 
3665519ee691SJanusz.Dziedzic@tieto.com 	/* One shot NOA  */
3666519ee691SJanusz.Dziedzic@tieto.com 	if (data->count[i] == 1)
3667519ee691SJanusz.Dziedzic@tieto.com 		return false;
3668519ee691SJanusz.Dziedzic@tieto.com 
3669519ee691SJanusz.Dziedzic@tieto.com 	if (data->desc[i].interval == 0)
3670519ee691SJanusz.Dziedzic@tieto.com 		return false;
3671519ee691SJanusz.Dziedzic@tieto.com 
3672a7022e65SFelix Fietkau 	/* End time is in the past, check for repetitions */
3673a7022e65SFelix Fietkau 	skip = DIV_ROUND_UP(-end, data->desc[i].interval);
3674a7022e65SFelix Fietkau 	if (data->count[i] < 255) {
3675a7022e65SFelix Fietkau 		if (data->count[i] <= skip) {
3676a7022e65SFelix Fietkau 			data->count[i] = 0;
3677a7022e65SFelix Fietkau 			return false;
3678a7022e65SFelix Fietkau 		}
3679a7022e65SFelix Fietkau 
3680a7022e65SFelix Fietkau 		data->count[i] -= skip;
3681a7022e65SFelix Fietkau 	}
3682a7022e65SFelix Fietkau 
3683a7022e65SFelix Fietkau 	data->desc[i].start += skip * data->desc[i].interval;
3684a7022e65SFelix Fietkau 
3685a7022e65SFelix Fietkau 	return true;
3686a7022e65SFelix Fietkau }
3687a7022e65SFelix Fietkau 
3688a7022e65SFelix Fietkau static bool
3689a7022e65SFelix Fietkau ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
3690a7022e65SFelix Fietkau 			     s32 *offset)
3691a7022e65SFelix Fietkau {
3692a7022e65SFelix Fietkau 	bool ret = false;
3693a7022e65SFelix Fietkau 	int i;
3694a7022e65SFelix Fietkau 
3695a7022e65SFelix Fietkau 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3696a7022e65SFelix Fietkau 		s32 cur;
3697a7022e65SFelix Fietkau 
3698a7022e65SFelix Fietkau 		if (!data->count[i])
3699a7022e65SFelix Fietkau 			continue;
3700a7022e65SFelix Fietkau 
3701a7022e65SFelix Fietkau 		if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
3702a7022e65SFelix Fietkau 			ret = true;
3703a7022e65SFelix Fietkau 
3704a7022e65SFelix Fietkau 		cur = data->desc[i].start - tsf;
3705a7022e65SFelix Fietkau 		if (cur > *offset)
3706a7022e65SFelix Fietkau 			continue;
3707a7022e65SFelix Fietkau 
3708a7022e65SFelix Fietkau 		cur = data->desc[i].start + data->desc[i].duration - tsf;
3709a7022e65SFelix Fietkau 		if (cur > *offset)
3710a7022e65SFelix Fietkau 			*offset = cur;
3711a7022e65SFelix Fietkau 	}
3712a7022e65SFelix Fietkau 
3713a7022e65SFelix Fietkau 	return ret;
3714a7022e65SFelix Fietkau }
3715a7022e65SFelix Fietkau 
3716a7022e65SFelix Fietkau static u32
3717a7022e65SFelix Fietkau ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
3718a7022e65SFelix Fietkau {
3719a7022e65SFelix Fietkau 	s32 offset = 0;
3720a7022e65SFelix Fietkau 	int tries = 0;
3721a7022e65SFelix Fietkau 	/*
3722a7022e65SFelix Fietkau 	 * arbitrary limit, used to avoid infinite loops when combined NoA
3723a7022e65SFelix Fietkau 	 * descriptors cover the full time period.
3724a7022e65SFelix Fietkau 	 */
3725a7022e65SFelix Fietkau 	int max_tries = 5;
3726a7022e65SFelix Fietkau 
3727a7022e65SFelix Fietkau 	ieee80211_extend_absent_time(data, tsf, &offset);
3728a7022e65SFelix Fietkau 	do {
3729a7022e65SFelix Fietkau 		if (!ieee80211_extend_absent_time(data, tsf, &offset))
3730a7022e65SFelix Fietkau 			break;
3731a7022e65SFelix Fietkau 
3732a7022e65SFelix Fietkau 		tries++;
3733a7022e65SFelix Fietkau 	} while (tries < max_tries);
3734a7022e65SFelix Fietkau 
3735a7022e65SFelix Fietkau 	return offset;
3736a7022e65SFelix Fietkau }
3737a7022e65SFelix Fietkau 
3738a7022e65SFelix Fietkau void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
3739a7022e65SFelix Fietkau {
3740a7022e65SFelix Fietkau 	u32 next_offset = BIT(31) - 1;
3741a7022e65SFelix Fietkau 	int i;
3742a7022e65SFelix Fietkau 
3743a7022e65SFelix Fietkau 	data->absent = 0;
3744a7022e65SFelix Fietkau 	data->has_next_tsf = false;
3745a7022e65SFelix Fietkau 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3746a7022e65SFelix Fietkau 		s32 start;
3747a7022e65SFelix Fietkau 
3748a7022e65SFelix Fietkau 		if (!data->count[i])
3749a7022e65SFelix Fietkau 			continue;
3750a7022e65SFelix Fietkau 
3751a7022e65SFelix Fietkau 		ieee80211_extend_noa_desc(data, tsf, i);
3752a7022e65SFelix Fietkau 		start = data->desc[i].start - tsf;
3753a7022e65SFelix Fietkau 		if (start <= 0)
3754a7022e65SFelix Fietkau 			data->absent |= BIT(i);
3755a7022e65SFelix Fietkau 
3756a7022e65SFelix Fietkau 		if (next_offset > start)
3757a7022e65SFelix Fietkau 			next_offset = start;
3758a7022e65SFelix Fietkau 
3759a7022e65SFelix Fietkau 		data->has_next_tsf = true;
3760a7022e65SFelix Fietkau 	}
3761a7022e65SFelix Fietkau 
3762a7022e65SFelix Fietkau 	if (data->absent)
3763a7022e65SFelix Fietkau 		next_offset = ieee80211_get_noa_absent_time(data, tsf);
3764a7022e65SFelix Fietkau 
3765a7022e65SFelix Fietkau 	data->next_tsf = tsf + next_offset;
3766a7022e65SFelix Fietkau }
3767a7022e65SFelix Fietkau EXPORT_SYMBOL(ieee80211_update_p2p_noa);
3768a7022e65SFelix Fietkau 
3769a7022e65SFelix Fietkau int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
3770a7022e65SFelix Fietkau 			    struct ieee80211_noa_data *data, u32 tsf)
3771a7022e65SFelix Fietkau {
3772a7022e65SFelix Fietkau 	int ret = 0;
3773a7022e65SFelix Fietkau 	int i;
3774a7022e65SFelix Fietkau 
3775a7022e65SFelix Fietkau 	memset(data, 0, sizeof(*data));
3776a7022e65SFelix Fietkau 
3777a7022e65SFelix Fietkau 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3778a7022e65SFelix Fietkau 		const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
3779a7022e65SFelix Fietkau 
3780a7022e65SFelix Fietkau 		if (!desc->count || !desc->duration)
3781a7022e65SFelix Fietkau 			continue;
3782a7022e65SFelix Fietkau 
3783a7022e65SFelix Fietkau 		data->count[i] = desc->count;
3784a7022e65SFelix Fietkau 		data->desc[i].start = le32_to_cpu(desc->start_time);
3785a7022e65SFelix Fietkau 		data->desc[i].duration = le32_to_cpu(desc->duration);
3786a7022e65SFelix Fietkau 		data->desc[i].interval = le32_to_cpu(desc->interval);
3787a7022e65SFelix Fietkau 
3788a7022e65SFelix Fietkau 		if (data->count[i] > 1 &&
3789a7022e65SFelix Fietkau 		    data->desc[i].interval < data->desc[i].duration)
3790a7022e65SFelix Fietkau 			continue;
3791a7022e65SFelix Fietkau 
3792a7022e65SFelix Fietkau 		ieee80211_extend_noa_desc(data, tsf, i);
3793a7022e65SFelix Fietkau 		ret++;
3794a7022e65SFelix Fietkau 	}
3795a7022e65SFelix Fietkau 
3796a7022e65SFelix Fietkau 	if (ret)
3797a7022e65SFelix Fietkau 		ieee80211_update_p2p_noa(data, tsf);
3798a7022e65SFelix Fietkau 
3799a7022e65SFelix Fietkau 	return ret;
3800a7022e65SFelix Fietkau }
3801a7022e65SFelix Fietkau EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
3802057d5f4bSThomas Pedersen 
3803057d5f4bSThomas Pedersen void ieee80211_recalc_dtim(struct ieee80211_local *local,
3804057d5f4bSThomas Pedersen 			   struct ieee80211_sub_if_data *sdata)
3805057d5f4bSThomas Pedersen {
3806057d5f4bSThomas Pedersen 	u64 tsf = drv_get_tsf(local, sdata);
3807057d5f4bSThomas Pedersen 	u64 dtim_count = 0;
3808057d5f4bSThomas Pedersen 	u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
3809057d5f4bSThomas Pedersen 	u8 dtim_period = sdata->vif.bss_conf.dtim_period;
3810057d5f4bSThomas Pedersen 	struct ps_data *ps;
3811057d5f4bSThomas Pedersen 	u8 bcns_from_dtim;
3812057d5f4bSThomas Pedersen 
3813057d5f4bSThomas Pedersen 	if (tsf == -1ULL || !beacon_int || !dtim_period)
3814057d5f4bSThomas Pedersen 		return;
3815057d5f4bSThomas Pedersen 
3816057d5f4bSThomas Pedersen 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
3817057d5f4bSThomas Pedersen 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
3818057d5f4bSThomas Pedersen 		if (!sdata->bss)
3819057d5f4bSThomas Pedersen 			return;
3820057d5f4bSThomas Pedersen 
3821057d5f4bSThomas Pedersen 		ps = &sdata->bss->ps;
3822057d5f4bSThomas Pedersen 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
3823057d5f4bSThomas Pedersen 		ps = &sdata->u.mesh.ps;
3824057d5f4bSThomas Pedersen 	} else {
3825057d5f4bSThomas Pedersen 		return;
3826057d5f4bSThomas Pedersen 	}
3827057d5f4bSThomas Pedersen 
3828057d5f4bSThomas Pedersen 	/*
3829057d5f4bSThomas Pedersen 	 * actually finds last dtim_count, mac80211 will update in
3830057d5f4bSThomas Pedersen 	 * __beacon_add_tim().
3831057d5f4bSThomas Pedersen 	 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
3832057d5f4bSThomas Pedersen 	 */
3833057d5f4bSThomas Pedersen 	do_div(tsf, beacon_int);
3834057d5f4bSThomas Pedersen 	bcns_from_dtim = do_div(tsf, dtim_period);
3835057d5f4bSThomas Pedersen 	/* just had a DTIM */
3836057d5f4bSThomas Pedersen 	if (!bcns_from_dtim)
3837057d5f4bSThomas Pedersen 		dtim_count = 0;
3838057d5f4bSThomas Pedersen 	else
3839057d5f4bSThomas Pedersen 		dtim_count = dtim_period - bcns_from_dtim;
3840057d5f4bSThomas Pedersen 
3841057d5f4bSThomas Pedersen 	ps->dtim_count = dtim_count;
3842057d5f4bSThomas Pedersen }
384373de86a3SLuciano Coelho 
384471e6195eSMichal Kazior static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
384571e6195eSMichal Kazior 					 struct ieee80211_chanctx *ctx)
384671e6195eSMichal Kazior {
384771e6195eSMichal Kazior 	struct ieee80211_sub_if_data *sdata;
384871e6195eSMichal Kazior 	u8 radar_detect = 0;
384971e6195eSMichal Kazior 
385071e6195eSMichal Kazior 	lockdep_assert_held(&local->chanctx_mtx);
385171e6195eSMichal Kazior 
385271e6195eSMichal Kazior 	if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
385371e6195eSMichal Kazior 		return 0;
385471e6195eSMichal Kazior 
385571e6195eSMichal Kazior 	list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
385671e6195eSMichal Kazior 		if (sdata->reserved_radar_required)
385771e6195eSMichal Kazior 			radar_detect |= BIT(sdata->reserved_chandef.width);
385871e6195eSMichal Kazior 
385971e6195eSMichal Kazior 	/*
386071e6195eSMichal Kazior 	 * An in-place reservation context should not have any assigned vifs
386171e6195eSMichal Kazior 	 * until it replaces the other context.
386271e6195eSMichal Kazior 	 */
386371e6195eSMichal Kazior 	WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
386471e6195eSMichal Kazior 		!list_empty(&ctx->assigned_vifs));
386571e6195eSMichal Kazior 
386671e6195eSMichal Kazior 	list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
386771e6195eSMichal Kazior 		if (sdata->radar_required)
386871e6195eSMichal Kazior 			radar_detect |= BIT(sdata->vif.bss_conf.chandef.width);
386971e6195eSMichal Kazior 
387071e6195eSMichal Kazior 	return radar_detect;
387171e6195eSMichal Kazior }
387271e6195eSMichal Kazior 
387373de86a3SLuciano Coelho int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
387473de86a3SLuciano Coelho 				 const struct cfg80211_chan_def *chandef,
387573de86a3SLuciano Coelho 				 enum ieee80211_chanctx_mode chanmode,
387673de86a3SLuciano Coelho 				 u8 radar_detect)
387773de86a3SLuciano Coelho {
387873de86a3SLuciano Coelho 	struct ieee80211_local *local = sdata->local;
387973de86a3SLuciano Coelho 	struct ieee80211_sub_if_data *sdata_iter;
388073de86a3SLuciano Coelho 	enum nl80211_iftype iftype = sdata->wdev.iftype;
388173de86a3SLuciano Coelho 	struct ieee80211_chanctx *ctx;
388273de86a3SLuciano Coelho 	int total = 1;
3883e227300cSPurushottam Kushwaha 	struct iface_combination_params params = {
3884e227300cSPurushottam Kushwaha 		.radar_detect = radar_detect,
3885e227300cSPurushottam Kushwaha 	};
388673de86a3SLuciano Coelho 
388773de86a3SLuciano Coelho 	lockdep_assert_held(&local->chanctx_mtx);
388873de86a3SLuciano Coelho 
388973de86a3SLuciano Coelho 	if (WARN_ON(hweight32(radar_detect) > 1))
389073de86a3SLuciano Coelho 		return -EINVAL;
389173de86a3SLuciano Coelho 
3892b6a55015SLuciano Coelho 	if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
3893b6a55015SLuciano Coelho 		    !chandef->chan))
389473de86a3SLuciano Coelho 		return -EINVAL;
389573de86a3SLuciano Coelho 
389673de86a3SLuciano Coelho 	if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
389773de86a3SLuciano Coelho 		return -EINVAL;
389873de86a3SLuciano Coelho 
3899ac668afeSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
3900ac668afeSJohannes Berg 	    sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
3901ac668afeSJohannes Berg 		/*
3902ac668afeSJohannes Berg 		 * always passing this is harmless, since it'll be the
3903ac668afeSJohannes Berg 		 * same value that cfg80211 finds if it finds the same
3904ac668afeSJohannes Berg 		 * interface ... and that's always allowed
3905ac668afeSJohannes Berg 		 */
3906ac668afeSJohannes Berg 		params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
3907ac668afeSJohannes Berg 	}
3908ac668afeSJohannes Berg 
390973de86a3SLuciano Coelho 	/* Always allow software iftypes */
3910e6f40511SManikanta Pubbisetty 	if (cfg80211_iftype_allowed(local->hw.wiphy, iftype, 0, 1)) {
391173de86a3SLuciano Coelho 		if (radar_detect)
391273de86a3SLuciano Coelho 			return -EINVAL;
391373de86a3SLuciano Coelho 		return 0;
391473de86a3SLuciano Coelho 	}
391573de86a3SLuciano Coelho 
3916e227300cSPurushottam Kushwaha 	if (chandef)
3917e227300cSPurushottam Kushwaha 		params.num_different_channels = 1;
391873de86a3SLuciano Coelho 
391973de86a3SLuciano Coelho 	if (iftype != NL80211_IFTYPE_UNSPECIFIED)
3920e227300cSPurushottam Kushwaha 		params.iftype_num[iftype] = 1;
392173de86a3SLuciano Coelho 
392273de86a3SLuciano Coelho 	list_for_each_entry(ctx, &local->chanctx_list, list) {
39235bcae31dSMichal Kazior 		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
39245bcae31dSMichal Kazior 			continue;
3925e227300cSPurushottam Kushwaha 		params.radar_detect |=
3926e227300cSPurushottam Kushwaha 			ieee80211_chanctx_radar_detect(local, ctx);
392773de86a3SLuciano Coelho 		if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
3928e227300cSPurushottam Kushwaha 			params.num_different_channels++;
392973de86a3SLuciano Coelho 			continue;
393073de86a3SLuciano Coelho 		}
3931b6a55015SLuciano Coelho 		if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
393273de86a3SLuciano Coelho 		    cfg80211_chandef_compatible(chandef,
393373de86a3SLuciano Coelho 						&ctx->conf.def))
393473de86a3SLuciano Coelho 			continue;
3935e227300cSPurushottam Kushwaha 		params.num_different_channels++;
393673de86a3SLuciano Coelho 	}
393773de86a3SLuciano Coelho 
393873de86a3SLuciano Coelho 	list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
393973de86a3SLuciano Coelho 		struct wireless_dev *wdev_iter;
394073de86a3SLuciano Coelho 
394173de86a3SLuciano Coelho 		wdev_iter = &sdata_iter->wdev;
394273de86a3SLuciano Coelho 
394373de86a3SLuciano Coelho 		if (sdata_iter == sdata ||
39440f611d28SAndrei Otcheretianski 		    !ieee80211_sdata_running(sdata_iter) ||
3945e6f40511SManikanta Pubbisetty 		    cfg80211_iftype_allowed(local->hw.wiphy,
3946e6f40511SManikanta Pubbisetty 					    wdev_iter->iftype, 0, 1))
394773de86a3SLuciano Coelho 			continue;
394873de86a3SLuciano Coelho 
3949e227300cSPurushottam Kushwaha 		params.iftype_num[wdev_iter->iftype]++;
395073de86a3SLuciano Coelho 		total++;
395173de86a3SLuciano Coelho 	}
395273de86a3SLuciano Coelho 
3953e227300cSPurushottam Kushwaha 	if (total == 1 && !params.radar_detect)
395473de86a3SLuciano Coelho 		return 0;
395573de86a3SLuciano Coelho 
3956e227300cSPurushottam Kushwaha 	return cfg80211_check_combinations(local->hw.wiphy, &params);
395773de86a3SLuciano Coelho }
39586fa001bcSMichal Kazior 
39596fa001bcSMichal Kazior static void
39606fa001bcSMichal Kazior ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
39616fa001bcSMichal Kazior 			 void *data)
39626fa001bcSMichal Kazior {
39636fa001bcSMichal Kazior 	u32 *max_num_different_channels = data;
39646fa001bcSMichal Kazior 
39656fa001bcSMichal Kazior 	*max_num_different_channels = max(*max_num_different_channels,
39666fa001bcSMichal Kazior 					  c->num_different_channels);
39676fa001bcSMichal Kazior }
39686fa001bcSMichal Kazior 
39696fa001bcSMichal Kazior int ieee80211_max_num_channels(struct ieee80211_local *local)
39706fa001bcSMichal Kazior {
39716fa001bcSMichal Kazior 	struct ieee80211_sub_if_data *sdata;
39726fa001bcSMichal Kazior 	struct ieee80211_chanctx *ctx;
39736fa001bcSMichal Kazior 	u32 max_num_different_channels = 1;
39746fa001bcSMichal Kazior 	int err;
3975e227300cSPurushottam Kushwaha 	struct iface_combination_params params = {0};
39766fa001bcSMichal Kazior 
39776fa001bcSMichal Kazior 	lockdep_assert_held(&local->chanctx_mtx);
39786fa001bcSMichal Kazior 
39796fa001bcSMichal Kazior 	list_for_each_entry(ctx, &local->chanctx_list, list) {
39805bcae31dSMichal Kazior 		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
39815bcae31dSMichal Kazior 			continue;
39825bcae31dSMichal Kazior 
3983e227300cSPurushottam Kushwaha 		params.num_different_channels++;
39846fa001bcSMichal Kazior 
3985e227300cSPurushottam Kushwaha 		params.radar_detect |=
3986e227300cSPurushottam Kushwaha 			ieee80211_chanctx_radar_detect(local, ctx);
39876fa001bcSMichal Kazior 	}
39886fa001bcSMichal Kazior 
39896fa001bcSMichal Kazior 	list_for_each_entry_rcu(sdata, &local->interfaces, list)
3990e227300cSPurushottam Kushwaha 		params.iftype_num[sdata->wdev.iftype]++;
39916fa001bcSMichal Kazior 
3992e227300cSPurushottam Kushwaha 	err = cfg80211_iter_combinations(local->hw.wiphy, &params,
3993e227300cSPurushottam Kushwaha 					 ieee80211_iter_max_chans,
39946fa001bcSMichal Kazior 					 &max_num_different_channels);
39956fa001bcSMichal Kazior 	if (err < 0)
39966fa001bcSMichal Kazior 		return err;
39976fa001bcSMichal Kazior 
39986fa001bcSMichal Kazior 	return max_num_different_channels;
39996fa001bcSMichal Kazior }
400040b861a0SArik Nemtsov 
400140b861a0SArik Nemtsov u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
400240b861a0SArik Nemtsov {
400340b861a0SArik Nemtsov 	*buf++ = WLAN_EID_VENDOR_SPECIFIC;
400440b861a0SArik Nemtsov 	*buf++ = 7; /* len */
400540b861a0SArik Nemtsov 	*buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
400640b861a0SArik Nemtsov 	*buf++ = 0x50;
400740b861a0SArik Nemtsov 	*buf++ = 0xf2;
400840b861a0SArik Nemtsov 	*buf++ = 2; /* WME */
400940b861a0SArik Nemtsov 	*buf++ = 0; /* WME info */
401040b861a0SArik Nemtsov 	*buf++ = 1; /* WME ver */
401140b861a0SArik Nemtsov 	*buf++ = qosinfo; /* U-APSD no in use */
401240b861a0SArik Nemtsov 
401340b861a0SArik Nemtsov 	return buf;
401440b861a0SArik Nemtsov }
4015ba8c3d6fSFelix Fietkau 
4016f2ac7e30SMichal Kazior void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
4017f2ac7e30SMichal Kazior 			     unsigned long *frame_cnt,
4018f2ac7e30SMichal Kazior 			     unsigned long *byte_cnt)
4019f2ac7e30SMichal Kazior {
4020f2ac7e30SMichal Kazior 	struct txq_info *txqi = to_txq_info(txq);
4021bb42f2d1SToke Høiland-Jørgensen 	u32 frag_cnt = 0, frag_bytes = 0;
4022bb42f2d1SToke Høiland-Jørgensen 	struct sk_buff *skb;
4023bb42f2d1SToke Høiland-Jørgensen 
4024bb42f2d1SToke Høiland-Jørgensen 	skb_queue_walk(&txqi->frags, skb) {
4025bb42f2d1SToke Høiland-Jørgensen 		frag_cnt++;
4026bb42f2d1SToke Høiland-Jørgensen 		frag_bytes += skb->len;
4027bb42f2d1SToke Høiland-Jørgensen 	}
4028f2ac7e30SMichal Kazior 
4029f2ac7e30SMichal Kazior 	if (frame_cnt)
4030bb42f2d1SToke Høiland-Jørgensen 		*frame_cnt = txqi->tin.backlog_packets + frag_cnt;
4031f2ac7e30SMichal Kazior 
4032f2ac7e30SMichal Kazior 	if (byte_cnt)
4033bb42f2d1SToke Høiland-Jørgensen 		*byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
4034f2ac7e30SMichal Kazior }
4035f2ac7e30SMichal Kazior EXPORT_SYMBOL(ieee80211_txq_get_depth);
4036f438ceb8SEmmanuel Grumbach 
4037f438ceb8SEmmanuel Grumbach const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
4038f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
4039f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
4040f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
4041f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
4042f438ceb8SEmmanuel Grumbach };
4043