xref: /openbmc/linux/net/mac80211/util.c (revision f8891461)
1c2d1560aSJohannes Berg /*
2c2d1560aSJohannes Berg  * Copyright 2002-2005, Instant802 Networks, Inc.
3c2d1560aSJohannes Berg  * Copyright 2005-2006, Devicescape Software, Inc.
4c2d1560aSJohannes Berg  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
5c2d1560aSJohannes Berg  * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
6d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
7da6a4352SJohannes Berg  * Copyright (C) 2015-2017	Intel Deutschland GmbH
84926b51bSJohannes Berg  * Copyright (C) 2018-2019 Intel Corporation
9c2d1560aSJohannes Berg  *
10c2d1560aSJohannes Berg  * This program is free software; you can redistribute it and/or modify
11c2d1560aSJohannes Berg  * it under the terms of the GNU General Public License version 2 as
12c2d1560aSJohannes Berg  * published by the Free Software Foundation.
13c2d1560aSJohannes Berg  *
14c2d1560aSJohannes Berg  * utilities for mac80211
15c2d1560aSJohannes Berg  */
16c2d1560aSJohannes Berg 
17c2d1560aSJohannes Berg #include <net/mac80211.h>
18c2d1560aSJohannes Berg #include <linux/netdevice.h>
19bc3b2d7fSPaul Gortmaker #include <linux/export.h>
20c2d1560aSJohannes Berg #include <linux/types.h>
21c2d1560aSJohannes Berg #include <linux/slab.h>
22c2d1560aSJohannes Berg #include <linux/skbuff.h>
23c2d1560aSJohannes Berg #include <linux/etherdevice.h>
24c2d1560aSJohannes Berg #include <linux/if_arp.h>
25c2d1560aSJohannes Berg #include <linux/bitmap.h>
26dd76986bSJohannes Berg #include <linux/crc32.h>
27881d966bSEric W. Biederman #include <net/net_namespace.h>
28c2d1560aSJohannes Berg #include <net/cfg80211.h>
29dabeb344SJohannes Berg #include <net/rtnetlink.h>
30c2d1560aSJohannes Berg 
31c2d1560aSJohannes Berg #include "ieee80211_i.h"
3224487981SJohannes Berg #include "driver-ops.h"
332c8dccc7SJohannes Berg #include "rate.h"
34ee385855SLuis Carlos Cobo #include "mesh.h"
35c2d1560aSJohannes Berg #include "wme.h"
36f2753ddbSJohannes Berg #include "led.h"
37fffd0934SJohannes Berg #include "wep.h"
38c2d1560aSJohannes Berg 
39c2d1560aSJohannes Berg /* privid for wiphys to determine whether they belong to us or not */
408a47cea7SJohannes Berg const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
41c2d1560aSJohannes Berg 
429a95371aSLuis R. Rodriguez struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
439a95371aSLuis R. Rodriguez {
449a95371aSLuis R. Rodriguez 	struct ieee80211_local *local;
459a95371aSLuis R. Rodriguez 	BUG_ON(!wiphy);
469a95371aSLuis R. Rodriguez 
479a95371aSLuis R. Rodriguez 	local = wiphy_priv(wiphy);
489a95371aSLuis R. Rodriguez 	return &local->hw;
499a95371aSLuis R. Rodriguez }
509a95371aSLuis R. Rodriguez EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
51c2d1560aSJohannes Berg 
525cf121c3SJohannes Berg void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
53c2d1560aSJohannes Berg {
54252b86c4SJohannes Berg 	struct sk_buff *skb;
552de8e0d9SJohannes Berg 	struct ieee80211_hdr *hdr;
56c2d1560aSJohannes Berg 
57252b86c4SJohannes Berg 	skb_queue_walk(&tx->skbs, skb) {
582de8e0d9SJohannes Berg 		hdr = (struct ieee80211_hdr *) skb->data;
59c2d1560aSJohannes Berg 		hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
60252b86c4SJohannes Berg 	}
61c2d1560aSJohannes Berg }
62c2d1560aSJohannes Berg 
6357fbcce3SJohannes Berg int ieee80211_frame_duration(enum nl80211_band band, size_t len,
64438b61b7SSimon Wunderlich 			     int rate, int erp, int short_preamble,
65438b61b7SSimon Wunderlich 			     int shift)
66c2d1560aSJohannes Berg {
67c2d1560aSJohannes Berg 	int dur;
68c2d1560aSJohannes Berg 
69c2d1560aSJohannes Berg 	/* calculate duration (in microseconds, rounded up to next higher
70c2d1560aSJohannes Berg 	 * integer if it includes a fractional microsecond) to send frame of
71c2d1560aSJohannes Berg 	 * len bytes (does not include FCS) at the given rate. Duration will
72c2d1560aSJohannes Berg 	 * also include SIFS.
73c2d1560aSJohannes Berg 	 *
74c2d1560aSJohannes Berg 	 * rate is in 100 kbps, so divident is multiplied by 10 in the
75c2d1560aSJohannes Berg 	 * DIV_ROUND_UP() operations.
76438b61b7SSimon Wunderlich 	 *
77438b61b7SSimon Wunderlich 	 * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
78438b61b7SSimon Wunderlich 	 * is assumed to be 0 otherwise.
79c2d1560aSJohannes Berg 	 */
80c2d1560aSJohannes Berg 
8157fbcce3SJohannes Berg 	if (band == NL80211_BAND_5GHZ || erp) {
82c2d1560aSJohannes Berg 		/*
83c2d1560aSJohannes Berg 		 * OFDM:
84c2d1560aSJohannes Berg 		 *
85c2d1560aSJohannes Berg 		 * N_DBPS = DATARATE x 4
86c2d1560aSJohannes Berg 		 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
87c2d1560aSJohannes Berg 		 *	(16 = SIGNAL time, 6 = tail bits)
88c2d1560aSJohannes Berg 		 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
89c2d1560aSJohannes Berg 		 *
90c2d1560aSJohannes Berg 		 * T_SYM = 4 usec
91438b61b7SSimon Wunderlich 		 * 802.11a - 18.5.2: aSIFSTime = 16 usec
92c2d1560aSJohannes Berg 		 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
93c2d1560aSJohannes Berg 		 *	signal ext = 6 usec
94c2d1560aSJohannes Berg 		 */
95c2d1560aSJohannes Berg 		dur = 16; /* SIFS + signal ext */
96438b61b7SSimon Wunderlich 		dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
97438b61b7SSimon Wunderlich 		dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
98438b61b7SSimon Wunderlich 
99438b61b7SSimon Wunderlich 		/* IEEE 802.11-2012 18.3.2.4: all values above are:
100438b61b7SSimon Wunderlich 		 *  * times 4 for 5 MHz
101438b61b7SSimon Wunderlich 		 *  * times 2 for 10 MHz
102438b61b7SSimon Wunderlich 		 */
103438b61b7SSimon Wunderlich 		dur *= 1 << shift;
1042103dec1SSimon Wunderlich 
1052103dec1SSimon Wunderlich 		/* rates should already consider the channel bandwidth,
1062103dec1SSimon Wunderlich 		 * don't apply divisor again.
1072103dec1SSimon Wunderlich 		 */
1082103dec1SSimon Wunderlich 		dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
1092103dec1SSimon Wunderlich 					4 * rate); /* T_SYM x N_SYM */
110c2d1560aSJohannes Berg 	} else {
111c2d1560aSJohannes Berg 		/*
112c2d1560aSJohannes Berg 		 * 802.11b or 802.11g with 802.11b compatibility:
113c2d1560aSJohannes Berg 		 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
114c2d1560aSJohannes Berg 		 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
115c2d1560aSJohannes Berg 		 *
116c2d1560aSJohannes Berg 		 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
117c2d1560aSJohannes Berg 		 * aSIFSTime = 10 usec
118c2d1560aSJohannes Berg 		 * aPreambleLength = 144 usec or 72 usec with short preamble
119c2d1560aSJohannes Berg 		 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
120c2d1560aSJohannes Berg 		 */
121c2d1560aSJohannes Berg 		dur = 10; /* aSIFSTime = 10 usec */
122c2d1560aSJohannes Berg 		dur += short_preamble ? (72 + 24) : (144 + 48);
123c2d1560aSJohannes Berg 
124c2d1560aSJohannes Berg 		dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
125c2d1560aSJohannes Berg 	}
126c2d1560aSJohannes Berg 
127c2d1560aSJohannes Berg 	return dur;
128c2d1560aSJohannes Berg }
129c2d1560aSJohannes Berg 
130c2d1560aSJohannes Berg /* Exported duration function for driver use */
13132bfd35dSJohannes Berg __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
13232bfd35dSJohannes Berg 					struct ieee80211_vif *vif,
13357fbcce3SJohannes Berg 					enum nl80211_band band,
1348318d78aSJohannes Berg 					size_t frame_len,
1358318d78aSJohannes Berg 					struct ieee80211_rate *rate)
136c2d1560aSJohannes Berg {
13725d834e1SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
138c2d1560aSJohannes Berg 	u16 dur;
139438b61b7SSimon Wunderlich 	int erp, shift = 0;
14025d834e1SJohannes Berg 	bool short_preamble = false;
141c2d1560aSJohannes Berg 
1428318d78aSJohannes Berg 	erp = 0;
14325d834e1SJohannes Berg 	if (vif) {
14425d834e1SJohannes Berg 		sdata = vif_to_sdata(vif);
145bda3933aSJohannes Berg 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
1468318d78aSJohannes Berg 		if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
1478318d78aSJohannes Berg 			erp = rate->flags & IEEE80211_RATE_ERP_G;
148438b61b7SSimon Wunderlich 		shift = ieee80211_vif_get_shift(vif);
14925d834e1SJohannes Berg 	}
1508318d78aSJohannes Berg 
1514ee73f33SMichal Kazior 	dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
152438b61b7SSimon Wunderlich 				       short_preamble, shift);
153c2d1560aSJohannes Berg 
154c2d1560aSJohannes Berg 	return cpu_to_le16(dur);
155c2d1560aSJohannes Berg }
156c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_generic_frame_duration);
157c2d1560aSJohannes Berg 
15832bfd35dSJohannes Berg __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
15932bfd35dSJohannes Berg 			      struct ieee80211_vif *vif, size_t frame_len,
160e039fa4aSJohannes Berg 			      const struct ieee80211_tx_info *frame_txctl)
161c2d1560aSJohannes Berg {
162c2d1560aSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
163c2d1560aSJohannes Berg 	struct ieee80211_rate *rate;
16425d834e1SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
165471b3efdSJohannes Berg 	bool short_preamble;
1662103dec1SSimon Wunderlich 	int erp, shift = 0, bitrate;
167c2d1560aSJohannes Berg 	u16 dur;
1682e92e6f2SJohannes Berg 	struct ieee80211_supported_band *sband;
1692e92e6f2SJohannes Berg 
1704ee73f33SMichal Kazior 	sband = local->hw.wiphy->bands[frame_txctl->band];
171c2d1560aSJohannes Berg 
17225d834e1SJohannes Berg 	short_preamble = false;
1737e9ed188SDaniel Drake 
174e039fa4aSJohannes Berg 	rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
1758318d78aSJohannes Berg 
1768318d78aSJohannes Berg 	erp = 0;
17725d834e1SJohannes Berg 	if (vif) {
17825d834e1SJohannes Berg 		sdata = vif_to_sdata(vif);
179bda3933aSJohannes Berg 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
1808318d78aSJohannes Berg 		if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
1818318d78aSJohannes Berg 			erp = rate->flags & IEEE80211_RATE_ERP_G;
182438b61b7SSimon Wunderlich 		shift = ieee80211_vif_get_shift(vif);
18325d834e1SJohannes Berg 	}
184c2d1560aSJohannes Berg 
1852103dec1SSimon Wunderlich 	bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
1862103dec1SSimon Wunderlich 
187c2d1560aSJohannes Berg 	/* CTS duration */
1882103dec1SSimon Wunderlich 	dur = ieee80211_frame_duration(sband->band, 10, bitrate,
189438b61b7SSimon Wunderlich 				       erp, short_preamble, shift);
190c2d1560aSJohannes Berg 	/* Data frame duration */
1912103dec1SSimon Wunderlich 	dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
192438b61b7SSimon Wunderlich 					erp, short_preamble, shift);
193c2d1560aSJohannes Berg 	/* ACK duration */
1942103dec1SSimon Wunderlich 	dur += ieee80211_frame_duration(sband->band, 10, bitrate,
195438b61b7SSimon Wunderlich 					erp, short_preamble, shift);
196c2d1560aSJohannes Berg 
197c2d1560aSJohannes Berg 	return cpu_to_le16(dur);
198c2d1560aSJohannes Berg }
199c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_rts_duration);
200c2d1560aSJohannes Berg 
20132bfd35dSJohannes Berg __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
20232bfd35dSJohannes Berg 				    struct ieee80211_vif *vif,
203c2d1560aSJohannes Berg 				    size_t frame_len,
204e039fa4aSJohannes Berg 				    const struct ieee80211_tx_info *frame_txctl)
205c2d1560aSJohannes Berg {
206c2d1560aSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
207c2d1560aSJohannes Berg 	struct ieee80211_rate *rate;
20825d834e1SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
209471b3efdSJohannes Berg 	bool short_preamble;
2102103dec1SSimon Wunderlich 	int erp, shift = 0, bitrate;
211c2d1560aSJohannes Berg 	u16 dur;
2122e92e6f2SJohannes Berg 	struct ieee80211_supported_band *sband;
2132e92e6f2SJohannes Berg 
2144ee73f33SMichal Kazior 	sband = local->hw.wiphy->bands[frame_txctl->band];
215c2d1560aSJohannes Berg 
21625d834e1SJohannes Berg 	short_preamble = false;
2177e9ed188SDaniel Drake 
218e039fa4aSJohannes Berg 	rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
2198318d78aSJohannes Berg 	erp = 0;
22025d834e1SJohannes Berg 	if (vif) {
22125d834e1SJohannes Berg 		sdata = vif_to_sdata(vif);
222bda3933aSJohannes Berg 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
2238318d78aSJohannes Berg 		if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
2248318d78aSJohannes Berg 			erp = rate->flags & IEEE80211_RATE_ERP_G;
225438b61b7SSimon Wunderlich 		shift = ieee80211_vif_get_shift(vif);
22625d834e1SJohannes Berg 	}
227c2d1560aSJohannes Berg 
2282103dec1SSimon Wunderlich 	bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
2292103dec1SSimon Wunderlich 
230c2d1560aSJohannes Berg 	/* Data frame duration */
2312103dec1SSimon Wunderlich 	dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
232438b61b7SSimon Wunderlich 				       erp, short_preamble, shift);
233e039fa4aSJohannes Berg 	if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
234c2d1560aSJohannes Berg 		/* ACK duration */
2352103dec1SSimon Wunderlich 		dur += ieee80211_frame_duration(sband->band, 10, bitrate,
236438b61b7SSimon Wunderlich 						erp, short_preamble, shift);
237c2d1560aSJohannes Berg 	}
238c2d1560aSJohannes Berg 
239c2d1560aSJohannes Berg 	return cpu_to_le16(dur);
240c2d1560aSJohannes Berg }
241c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_ctstoself_duration);
242c2d1560aSJohannes Berg 
24321a5d4c3SManikanta Pubbisetty static void __ieee80211_wake_txqs(struct ieee80211_sub_if_data *sdata, int ac)
24421a5d4c3SManikanta Pubbisetty {
24521a5d4c3SManikanta Pubbisetty 	struct ieee80211_local *local = sdata->local;
24621a5d4c3SManikanta Pubbisetty 	struct ieee80211_vif *vif = &sdata->vif;
24721a5d4c3SManikanta Pubbisetty 	struct fq *fq = &local->fq;
24821a5d4c3SManikanta Pubbisetty 	struct ps_data *ps = NULL;
24921a5d4c3SManikanta Pubbisetty 	struct txq_info *txqi;
25021a5d4c3SManikanta Pubbisetty 	struct sta_info *sta;
25121a5d4c3SManikanta Pubbisetty 	int i;
25221a5d4c3SManikanta Pubbisetty 
25321a5d4c3SManikanta Pubbisetty 	spin_lock_bh(&fq->lock);
25421a5d4c3SManikanta Pubbisetty 
25521a5d4c3SManikanta Pubbisetty 	if (sdata->vif.type == NL80211_IFTYPE_AP)
25621a5d4c3SManikanta Pubbisetty 		ps = &sdata->bss->ps;
25721a5d4c3SManikanta Pubbisetty 
25821a5d4c3SManikanta Pubbisetty 	sdata->vif.txqs_stopped[ac] = false;
25921a5d4c3SManikanta Pubbisetty 
26021a5d4c3SManikanta Pubbisetty 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
26121a5d4c3SManikanta Pubbisetty 		if (sdata != sta->sdata)
26221a5d4c3SManikanta Pubbisetty 			continue;
26321a5d4c3SManikanta Pubbisetty 
26421a5d4c3SManikanta Pubbisetty 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
26521a5d4c3SManikanta Pubbisetty 			struct ieee80211_txq *txq = sta->sta.txq[i];
26621a5d4c3SManikanta Pubbisetty 
267a5ae3264SErik Stromdahl 			if (!txq)
268a5ae3264SErik Stromdahl 				continue;
269a5ae3264SErik Stromdahl 
27021a5d4c3SManikanta Pubbisetty 			txqi = to_txq_info(txq);
27121a5d4c3SManikanta Pubbisetty 
27221a5d4c3SManikanta Pubbisetty 			if (ac != txq->ac)
27321a5d4c3SManikanta Pubbisetty 				continue;
27421a5d4c3SManikanta Pubbisetty 
27521a5d4c3SManikanta Pubbisetty 			if (!test_and_clear_bit(IEEE80211_TXQ_STOP_NETIF_TX,
27621a5d4c3SManikanta Pubbisetty 						&txqi->flags))
27721a5d4c3SManikanta Pubbisetty 				continue;
27821a5d4c3SManikanta Pubbisetty 
27921a5d4c3SManikanta Pubbisetty 			spin_unlock_bh(&fq->lock);
28021a5d4c3SManikanta Pubbisetty 			drv_wake_tx_queue(local, txqi);
28121a5d4c3SManikanta Pubbisetty 			spin_lock_bh(&fq->lock);
28221a5d4c3SManikanta Pubbisetty 		}
28321a5d4c3SManikanta Pubbisetty 	}
28421a5d4c3SManikanta Pubbisetty 
28521a5d4c3SManikanta Pubbisetty 	if (!vif->txq)
28621a5d4c3SManikanta Pubbisetty 		goto out;
28721a5d4c3SManikanta Pubbisetty 
28821a5d4c3SManikanta Pubbisetty 	txqi = to_txq_info(vif->txq);
28921a5d4c3SManikanta Pubbisetty 
29021a5d4c3SManikanta Pubbisetty 	if (!test_and_clear_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags) ||
29121a5d4c3SManikanta Pubbisetty 	    (ps && atomic_read(&ps->num_sta_ps)) || ac != vif->txq->ac)
29221a5d4c3SManikanta Pubbisetty 		goto out;
29321a5d4c3SManikanta Pubbisetty 
29421a5d4c3SManikanta Pubbisetty 	spin_unlock_bh(&fq->lock);
29521a5d4c3SManikanta Pubbisetty 
29621a5d4c3SManikanta Pubbisetty 	drv_wake_tx_queue(local, txqi);
29721a5d4c3SManikanta Pubbisetty 	return;
29821a5d4c3SManikanta Pubbisetty out:
29921a5d4c3SManikanta Pubbisetty 	spin_unlock_bh(&fq->lock);
30021a5d4c3SManikanta Pubbisetty }
30121a5d4c3SManikanta Pubbisetty 
302f6c7f03fSEmmanuel Grumbach static void
303f6c7f03fSEmmanuel Grumbach __releases(&local->queue_stop_reason_lock)
304f6c7f03fSEmmanuel Grumbach __acquires(&local->queue_stop_reason_lock)
305f6c7f03fSEmmanuel Grumbach _ieee80211_wake_txqs(struct ieee80211_local *local, unsigned long *flags)
30621a5d4c3SManikanta Pubbisetty {
30721a5d4c3SManikanta Pubbisetty 	struct ieee80211_sub_if_data *sdata;
30821a5d4c3SManikanta Pubbisetty 	int n_acs = IEEE80211_NUM_ACS;
30921a5d4c3SManikanta Pubbisetty 	int i;
31021a5d4c3SManikanta Pubbisetty 
31121a5d4c3SManikanta Pubbisetty 	rcu_read_lock();
31221a5d4c3SManikanta Pubbisetty 
31321a5d4c3SManikanta Pubbisetty 	if (local->hw.queues < IEEE80211_NUM_ACS)
31421a5d4c3SManikanta Pubbisetty 		n_acs = 1;
31521a5d4c3SManikanta Pubbisetty 
31621a5d4c3SManikanta Pubbisetty 	for (i = 0; i < local->hw.queues; i++) {
31721a5d4c3SManikanta Pubbisetty 		if (local->queue_stop_reasons[i])
31821a5d4c3SManikanta Pubbisetty 			continue;
31921a5d4c3SManikanta Pubbisetty 
320f6c7f03fSEmmanuel Grumbach 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, *flags);
32121a5d4c3SManikanta Pubbisetty 		list_for_each_entry_rcu(sdata, &local->interfaces, list) {
32221a5d4c3SManikanta Pubbisetty 			int ac;
32321a5d4c3SManikanta Pubbisetty 
32421a5d4c3SManikanta Pubbisetty 			for (ac = 0; ac < n_acs; ac++) {
32521a5d4c3SManikanta Pubbisetty 				int ac_queue = sdata->vif.hw_queue[ac];
32621a5d4c3SManikanta Pubbisetty 
32721a5d4c3SManikanta Pubbisetty 				if (ac_queue == i ||
32821a5d4c3SManikanta Pubbisetty 				    sdata->vif.cab_queue == i)
32921a5d4c3SManikanta Pubbisetty 					__ieee80211_wake_txqs(sdata, ac);
33021a5d4c3SManikanta Pubbisetty 			}
33121a5d4c3SManikanta Pubbisetty 		}
332f6c7f03fSEmmanuel Grumbach 		spin_lock_irqsave(&local->queue_stop_reason_lock, *flags);
33321a5d4c3SManikanta Pubbisetty 	}
33421a5d4c3SManikanta Pubbisetty 
33521a5d4c3SManikanta Pubbisetty 	rcu_read_unlock();
33621a5d4c3SManikanta Pubbisetty }
33721a5d4c3SManikanta Pubbisetty 
338f6c7f03fSEmmanuel Grumbach void ieee80211_wake_txqs(unsigned long data)
339f6c7f03fSEmmanuel Grumbach {
340f6c7f03fSEmmanuel Grumbach 	struct ieee80211_local *local = (struct ieee80211_local *)data;
341f6c7f03fSEmmanuel Grumbach 	unsigned long flags;
342f6c7f03fSEmmanuel Grumbach 
343f6c7f03fSEmmanuel Grumbach 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
344f6c7f03fSEmmanuel Grumbach 	_ieee80211_wake_txqs(local, &flags);
345f6c7f03fSEmmanuel Grumbach 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
346f6c7f03fSEmmanuel Grumbach }
347f6c7f03fSEmmanuel Grumbach 
3483a25a8c8SJohannes Berg void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
3493a25a8c8SJohannes Berg {
3503a25a8c8SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
351a6f38ac3SJohannes Berg 	int n_acs = IEEE80211_NUM_ACS;
352a6f38ac3SJohannes Berg 
35380a83cfcSMichal Kazior 	if (local->ops->wake_tx_queue)
35480a83cfcSMichal Kazior 		return;
35580a83cfcSMichal Kazior 
356a6f38ac3SJohannes Berg 	if (local->hw.queues < IEEE80211_NUM_ACS)
357a6f38ac3SJohannes Berg 		n_acs = 1;
3583a25a8c8SJohannes Berg 
3593a25a8c8SJohannes Berg 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3603a25a8c8SJohannes Berg 		int ac;
3613a25a8c8SJohannes Berg 
362f142c6b9SJohannes Berg 		if (!sdata->dev)
363f142c6b9SJohannes Berg 			continue;
364f142c6b9SJohannes Berg 
3653a25a8c8SJohannes Berg 		if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
3663a25a8c8SJohannes Berg 		    local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
3673a25a8c8SJohannes Berg 			continue;
3683a25a8c8SJohannes Berg 
369a6f38ac3SJohannes Berg 		for (ac = 0; ac < n_acs; ac++) {
3703a25a8c8SJohannes Berg 			int ac_queue = sdata->vif.hw_queue[ac];
3713a25a8c8SJohannes Berg 
3723a25a8c8SJohannes Berg 			if (ac_queue == queue ||
3733a25a8c8SJohannes Berg 			    (sdata->vif.cab_queue == queue &&
3743a25a8c8SJohannes Berg 			     local->queue_stop_reasons[ac_queue] == 0 &&
3753a25a8c8SJohannes Berg 			     skb_queue_empty(&local->pending[ac_queue])))
3763a25a8c8SJohannes Berg 				netif_wake_subqueue(sdata->dev, ac);
3773a25a8c8SJohannes Berg 		}
3783a25a8c8SJohannes Berg 	}
3793a25a8c8SJohannes Berg }
3803a25a8c8SJohannes Berg 
381ce7c9111SKalle Valo static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
382cca07b00SLuciano Coelho 				   enum queue_stop_reason reason,
383f6c7f03fSEmmanuel Grumbach 				   bool refcounted,
384f6c7f03fSEmmanuel Grumbach 				   unsigned long *flags)
385c2d1560aSJohannes Berg {
386c2d1560aSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
387c2d1560aSJohannes Berg 
388b5878a2dSJohannes Berg 	trace_wake_queue(local, queue, reason);
389b5878a2dSJohannes Berg 
390e4e72fb4SJohannes Berg 	if (WARN_ON(queue >= hw->queues))
39196f5e66eSJohannes Berg 		return;
39296f5e66eSJohannes Berg 
393ada15125SJohannes Berg 	if (!test_bit(reason, &local->queue_stop_reasons[queue]))
394ada15125SJohannes Berg 		return;
395ada15125SJohannes Berg 
396856142cdSJohannes Berg 	if (!refcounted) {
397cca07b00SLuciano Coelho 		local->q_stop_reasons[queue][reason] = 0;
398856142cdSJohannes Berg 	} else {
399cca07b00SLuciano Coelho 		local->q_stop_reasons[queue][reason]--;
400856142cdSJohannes Berg 		if (WARN_ON(local->q_stop_reasons[queue][reason] < 0))
401856142cdSJohannes Berg 			local->q_stop_reasons[queue][reason] = 0;
402856142cdSJohannes Berg 	}
403cca07b00SLuciano Coelho 
404cca07b00SLuciano Coelho 	if (local->q_stop_reasons[queue][reason] == 0)
405ce7c9111SKalle Valo 		__clear_bit(reason, &local->queue_stop_reasons[queue]);
406ce7c9111SKalle Valo 
407ce7c9111SKalle Valo 	if (local->queue_stop_reasons[queue] != 0)
408ce7c9111SKalle Valo 		/* someone still has this queue stopped */
409ce7c9111SKalle Valo 		return;
410ce7c9111SKalle Valo 
4117236fe29SJohannes Berg 	if (skb_queue_empty(&local->pending[queue])) {
412cf0277e7SJohannes Berg 		rcu_read_lock();
4133a25a8c8SJohannes Berg 		ieee80211_propagate_queue_wake(local, queue);
414cf0277e7SJohannes Berg 		rcu_read_unlock();
4157236fe29SJohannes Berg 	} else
4167236fe29SJohannes Berg 		tasklet_schedule(&local->tx_pending_tasklet);
41721a5d4c3SManikanta Pubbisetty 
418f6c7f03fSEmmanuel Grumbach 	/*
419f6c7f03fSEmmanuel Grumbach 	 * Calling _ieee80211_wake_txqs here can be a problem because it may
420f6c7f03fSEmmanuel Grumbach 	 * release queue_stop_reason_lock which has been taken by
421f6c7f03fSEmmanuel Grumbach 	 * __ieee80211_wake_queue's caller. It is certainly not very nice to
422f6c7f03fSEmmanuel Grumbach 	 * release someone's lock, but it is fine because all the callers of
423f6c7f03fSEmmanuel Grumbach 	 * __ieee80211_wake_queue call it right before releasing the lock.
424f6c7f03fSEmmanuel Grumbach 	 */
425f6c7f03fSEmmanuel Grumbach 	if (local->ops->wake_tx_queue) {
426f6c7f03fSEmmanuel Grumbach 		if (reason == IEEE80211_QUEUE_STOP_REASON_DRIVER)
42721a5d4c3SManikanta Pubbisetty 			tasklet_schedule(&local->wake_txqs_tasklet);
428f6c7f03fSEmmanuel Grumbach 		else
429f6c7f03fSEmmanuel Grumbach 			_ieee80211_wake_txqs(local, flags);
430f6c7f03fSEmmanuel Grumbach 	}
431c2d1560aSJohannes Berg }
432ce7c9111SKalle Valo 
43396f5e66eSJohannes Berg void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
434cca07b00SLuciano Coelho 				    enum queue_stop_reason reason,
435cca07b00SLuciano Coelho 				    bool refcounted)
436ce7c9111SKalle Valo {
437ce7c9111SKalle Valo 	struct ieee80211_local *local = hw_to_local(hw);
438ce7c9111SKalle Valo 	unsigned long flags;
439ce7c9111SKalle Valo 
440ce7c9111SKalle Valo 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
441f6c7f03fSEmmanuel Grumbach 	__ieee80211_wake_queue(hw, queue, reason, refcounted, &flags);
442ce7c9111SKalle Valo 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
443ce7c9111SKalle Valo }
444ce7c9111SKalle Valo 
445ce7c9111SKalle Valo void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
446ce7c9111SKalle Valo {
447ce7c9111SKalle Valo 	ieee80211_wake_queue_by_reason(hw, queue,
448cca07b00SLuciano Coelho 				       IEEE80211_QUEUE_STOP_REASON_DRIVER,
449cca07b00SLuciano Coelho 				       false);
450ce7c9111SKalle Valo }
451c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_wake_queue);
452c2d1560aSJohannes Berg 
453ce7c9111SKalle Valo static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
454cca07b00SLuciano Coelho 				   enum queue_stop_reason reason,
455cca07b00SLuciano Coelho 				   bool refcounted)
456c2d1560aSJohannes Berg {
457c2d1560aSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
458cf0277e7SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
459a6f38ac3SJohannes Berg 	int n_acs = IEEE80211_NUM_ACS;
460c2d1560aSJohannes Berg 
461b5878a2dSJohannes Berg 	trace_stop_queue(local, queue, reason);
462b5878a2dSJohannes Berg 
463e4e72fb4SJohannes Berg 	if (WARN_ON(queue >= hw->queues))
46496f5e66eSJohannes Berg 		return;
46596f5e66eSJohannes Berg 
466cca07b00SLuciano Coelho 	if (!refcounted)
467cca07b00SLuciano Coelho 		local->q_stop_reasons[queue][reason] = 1;
468cca07b00SLuciano Coelho 	else
469cca07b00SLuciano Coelho 		local->q_stop_reasons[queue][reason]++;
470ada15125SJohannes Berg 
471cca07b00SLuciano Coelho 	if (__test_and_set_bit(reason, &local->queue_stop_reasons[queue]))
472cca07b00SLuciano Coelho 		return;
473cf0277e7SJohannes Berg 
474a6f38ac3SJohannes Berg 	if (local->hw.queues < IEEE80211_NUM_ACS)
475a6f38ac3SJohannes Berg 		n_acs = 1;
476a6f38ac3SJohannes Berg 
477cf0277e7SJohannes Berg 	rcu_read_lock();
4783a25a8c8SJohannes Berg 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4793a25a8c8SJohannes Berg 		int ac;
4803a25a8c8SJohannes Berg 
481f142c6b9SJohannes Berg 		if (!sdata->dev)
482f142c6b9SJohannes Berg 			continue;
483f142c6b9SJohannes Berg 
484a6f38ac3SJohannes Berg 		for (ac = 0; ac < n_acs; ac++) {
4853a25a8c8SJohannes Berg 			if (sdata->vif.hw_queue[ac] == queue ||
48621a5d4c3SManikanta Pubbisetty 			    sdata->vif.cab_queue == queue) {
48721a5d4c3SManikanta Pubbisetty 				if (!local->ops->wake_tx_queue) {
4883a25a8c8SJohannes Berg 					netif_stop_subqueue(sdata->dev, ac);
48921a5d4c3SManikanta Pubbisetty 					continue;
49021a5d4c3SManikanta Pubbisetty 				}
49121a5d4c3SManikanta Pubbisetty 				spin_lock(&local->fq.lock);
49221a5d4c3SManikanta Pubbisetty 				sdata->vif.txqs_stopped[ac] = true;
49321a5d4c3SManikanta Pubbisetty 				spin_unlock(&local->fq.lock);
49421a5d4c3SManikanta Pubbisetty 			}
4953a25a8c8SJohannes Berg 		}
4963a25a8c8SJohannes Berg 	}
497cf0277e7SJohannes Berg 	rcu_read_unlock();
498c2d1560aSJohannes Berg }
499ce7c9111SKalle Valo 
50096f5e66eSJohannes Berg void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
501cca07b00SLuciano Coelho 				    enum queue_stop_reason reason,
502cca07b00SLuciano Coelho 				    bool refcounted)
503ce7c9111SKalle Valo {
504ce7c9111SKalle Valo 	struct ieee80211_local *local = hw_to_local(hw);
505ce7c9111SKalle Valo 	unsigned long flags;
506ce7c9111SKalle Valo 
507ce7c9111SKalle Valo 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
508cca07b00SLuciano Coelho 	__ieee80211_stop_queue(hw, queue, reason, refcounted);
509ce7c9111SKalle Valo 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
510ce7c9111SKalle Valo }
511ce7c9111SKalle Valo 
512ce7c9111SKalle Valo void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
513ce7c9111SKalle Valo {
514ce7c9111SKalle Valo 	ieee80211_stop_queue_by_reason(hw, queue,
515cca07b00SLuciano Coelho 				       IEEE80211_QUEUE_STOP_REASON_DRIVER,
516cca07b00SLuciano Coelho 				       false);
517ce7c9111SKalle Valo }
518c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_stop_queue);
519c2d1560aSJohannes Berg 
5208f77f384SJohannes Berg void ieee80211_add_pending_skb(struct ieee80211_local *local,
5218f77f384SJohannes Berg 			       struct sk_buff *skb)
5228f77f384SJohannes Berg {
5238f77f384SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
5248f77f384SJohannes Berg 	unsigned long flags;
525a7bc376cSJohannes Berg 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
5263a25a8c8SJohannes Berg 	int queue = info->hw_queue;
527a7bc376cSJohannes Berg 
528a7bc376cSJohannes Berg 	if (WARN_ON(!info->control.vif)) {
529d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
530a7bc376cSJohannes Berg 		return;
531a7bc376cSJohannes Berg 	}
5328f77f384SJohannes Berg 
5338f77f384SJohannes Berg 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
534cca07b00SLuciano Coelho 	__ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
535cca07b00SLuciano Coelho 			       false);
5363b8d81e0SJohannes Berg 	__skb_queue_tail(&local->pending[queue], skb);
537cca07b00SLuciano Coelho 	__ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
538f6c7f03fSEmmanuel Grumbach 			       false, &flags);
5398f77f384SJohannes Berg 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
5408f77f384SJohannes Berg }
5418f77f384SJohannes Berg 
542e3685e03SJohannes Berg void ieee80211_add_pending_skbs(struct ieee80211_local *local,
543e3685e03SJohannes Berg 				struct sk_buff_head *skbs)
5448f77f384SJohannes Berg {
5458f77f384SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
5468f77f384SJohannes Berg 	struct sk_buff *skb;
5478f77f384SJohannes Berg 	unsigned long flags;
548b0b97a8aSJohannes Berg 	int queue, i;
5498f77f384SJohannes Berg 
5508f77f384SJohannes Berg 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
5518f77f384SJohannes Berg 	while ((skb = skb_dequeue(skbs))) {
552a7bc376cSJohannes Berg 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
553a7bc376cSJohannes Berg 
554a7bc376cSJohannes Berg 		if (WARN_ON(!info->control.vif)) {
555d4fa14cdSFelix Fietkau 			ieee80211_free_txskb(&local->hw, skb);
556a7bc376cSJohannes Berg 			continue;
557a7bc376cSJohannes Berg 		}
558a7bc376cSJohannes Berg 
5593a25a8c8SJohannes Berg 		queue = info->hw_queue;
5604644ae89SJohannes Berg 
5614644ae89SJohannes Berg 		__ieee80211_stop_queue(hw, queue,
562cca07b00SLuciano Coelho 				IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
563cca07b00SLuciano Coelho 				false);
5644644ae89SJohannes Berg 
5653b8d81e0SJohannes Berg 		__skb_queue_tail(&local->pending[queue], skb);
5668f77f384SJohannes Berg 	}
5678f77f384SJohannes Berg 
5683b8d81e0SJohannes Berg 	for (i = 0; i < hw->queues; i++)
5698f77f384SJohannes Berg 		__ieee80211_wake_queue(hw, i,
570cca07b00SLuciano Coelho 			IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
571f6c7f03fSEmmanuel Grumbach 			false, &flags);
5728f77f384SJohannes Berg 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
5738f77f384SJohannes Berg }
5748f77f384SJohannes Berg 
575ce7c9111SKalle Valo void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
576445ea4e8SJohannes Berg 				     unsigned long queues,
577cca07b00SLuciano Coelho 				     enum queue_stop_reason reason,
578cca07b00SLuciano Coelho 				     bool refcounted)
579c2d1560aSJohannes Berg {
580ce7c9111SKalle Valo 	struct ieee80211_local *local = hw_to_local(hw);
581ce7c9111SKalle Valo 	unsigned long flags;
582c2d1560aSJohannes Berg 	int i;
583c2d1560aSJohannes Berg 
584ce7c9111SKalle Valo 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
585ce7c9111SKalle Valo 
586445ea4e8SJohannes Berg 	for_each_set_bit(i, &queues, hw->queues)
587cca07b00SLuciano Coelho 		__ieee80211_stop_queue(hw, i, reason, refcounted);
588ce7c9111SKalle Valo 
589ce7c9111SKalle Valo 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
590ce7c9111SKalle Valo }
591ce7c9111SKalle Valo 
592ce7c9111SKalle Valo void ieee80211_stop_queues(struct ieee80211_hw *hw)
593ce7c9111SKalle Valo {
594445ea4e8SJohannes Berg 	ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
595cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_DRIVER,
596cca07b00SLuciano Coelho 					false);
597c2d1560aSJohannes Berg }
598c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_stop_queues);
599c2d1560aSJohannes Berg 
60092ab8535STomas Winkler int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
60192ab8535STomas Winkler {
60292ab8535STomas Winkler 	struct ieee80211_local *local = hw_to_local(hw);
6033b8d81e0SJohannes Berg 	unsigned long flags;
6043b8d81e0SJohannes Berg 	int ret;
60596f5e66eSJohannes Berg 
606e4e72fb4SJohannes Berg 	if (WARN_ON(queue >= hw->queues))
60796f5e66eSJohannes Berg 		return true;
60896f5e66eSJohannes Berg 
6093b8d81e0SJohannes Berg 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
6102419ea14SThomas Pedersen 	ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
6112419ea14SThomas Pedersen 		       &local->queue_stop_reasons[queue]);
6123b8d81e0SJohannes Berg 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
6133b8d81e0SJohannes Berg 	return ret;
61492ab8535STomas Winkler }
61592ab8535STomas Winkler EXPORT_SYMBOL(ieee80211_queue_stopped);
61692ab8535STomas Winkler 
617ce7c9111SKalle Valo void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
618445ea4e8SJohannes Berg 				     unsigned long queues,
619cca07b00SLuciano Coelho 				     enum queue_stop_reason reason,
620cca07b00SLuciano Coelho 				     bool refcounted)
621c2d1560aSJohannes Berg {
622ce7c9111SKalle Valo 	struct ieee80211_local *local = hw_to_local(hw);
623ce7c9111SKalle Valo 	unsigned long flags;
624c2d1560aSJohannes Berg 	int i;
625c2d1560aSJohannes Berg 
626ce7c9111SKalle Valo 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
627ce7c9111SKalle Valo 
628445ea4e8SJohannes Berg 	for_each_set_bit(i, &queues, hw->queues)
629f6c7f03fSEmmanuel Grumbach 		__ieee80211_wake_queue(hw, i, reason, refcounted, &flags);
630ce7c9111SKalle Valo 
631ce7c9111SKalle Valo 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
632ce7c9111SKalle Valo }
633ce7c9111SKalle Valo 
634ce7c9111SKalle Valo void ieee80211_wake_queues(struct ieee80211_hw *hw)
635ce7c9111SKalle Valo {
636445ea4e8SJohannes Berg 	ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
637cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_DRIVER,
638cca07b00SLuciano Coelho 					false);
639c2d1560aSJohannes Berg }
640c2d1560aSJohannes Berg EXPORT_SYMBOL(ieee80211_wake_queues);
641dabeb344SJohannes Berg 
64226da23b6SLuciano Coelho static unsigned int
64326da23b6SLuciano Coelho ieee80211_get_vif_queues(struct ieee80211_local *local,
64439ecc01dSJohannes Berg 			 struct ieee80211_sub_if_data *sdata)
64539ecc01dSJohannes Berg {
64626da23b6SLuciano Coelho 	unsigned int queues;
64739ecc01dSJohannes Berg 
64830686bf7SJohannes Berg 	if (sdata && ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
64939ecc01dSJohannes Berg 		int ac;
65039ecc01dSJohannes Berg 
65139ecc01dSJohannes Berg 		queues = 0;
65239ecc01dSJohannes Berg 
65339ecc01dSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
65439ecc01dSJohannes Berg 			queues |= BIT(sdata->vif.hw_queue[ac]);
65539ecc01dSJohannes Berg 		if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
65639ecc01dSJohannes Berg 			queues |= BIT(sdata->vif.cab_queue);
65739ecc01dSJohannes Berg 	} else {
65839ecc01dSJohannes Berg 		/* all queues */
65939ecc01dSJohannes Berg 		queues = BIT(local->hw.queues) - 1;
66039ecc01dSJohannes Berg 	}
66139ecc01dSJohannes Berg 
66226da23b6SLuciano Coelho 	return queues;
66326da23b6SLuciano Coelho }
66426da23b6SLuciano Coelho 
6654f9610d5SLiad Kaufman void __ieee80211_flush_queues(struct ieee80211_local *local,
6664f9610d5SLiad Kaufman 			      struct ieee80211_sub_if_data *sdata,
6673b24f4c6SEmmanuel Grumbach 			      unsigned int queues, bool drop)
66826da23b6SLuciano Coelho {
66926da23b6SLuciano Coelho 	if (!local->ops->flush)
67026da23b6SLuciano Coelho 		return;
67126da23b6SLuciano Coelho 
6724f9610d5SLiad Kaufman 	/*
6734f9610d5SLiad Kaufman 	 * If no queue was set, or if the HW doesn't support
6744f9610d5SLiad Kaufman 	 * IEEE80211_HW_QUEUE_CONTROL - flush all queues
6754f9610d5SLiad Kaufman 	 */
67630686bf7SJohannes Berg 	if (!queues || !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
67726da23b6SLuciano Coelho 		queues = ieee80211_get_vif_queues(local, sdata);
67826da23b6SLuciano Coelho 
67959f48fe2SLuciano Coelho 	ieee80211_stop_queues_by_reason(&local->hw, queues,
680cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_FLUSH,
681cca07b00SLuciano Coelho 					false);
682445ea4e8SJohannes Berg 
6833b24f4c6SEmmanuel Grumbach 	drv_flush(local, sdata, queues, drop);
684445ea4e8SJohannes Berg 
68559f48fe2SLuciano Coelho 	ieee80211_wake_queues_by_reason(&local->hw, queues,
686cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_FLUSH,
687cca07b00SLuciano Coelho 					false);
68839ecc01dSJohannes Berg }
68939ecc01dSJohannes Berg 
6904f9610d5SLiad Kaufman void ieee80211_flush_queues(struct ieee80211_local *local,
6913b24f4c6SEmmanuel Grumbach 			    struct ieee80211_sub_if_data *sdata, bool drop)
6924f9610d5SLiad Kaufman {
6933b24f4c6SEmmanuel Grumbach 	__ieee80211_flush_queues(local, sdata, 0, drop);
6944f9610d5SLiad Kaufman }
6954f9610d5SLiad Kaufman 
69626da23b6SLuciano Coelho void ieee80211_stop_vif_queues(struct ieee80211_local *local,
69726da23b6SLuciano Coelho 			       struct ieee80211_sub_if_data *sdata,
69826da23b6SLuciano Coelho 			       enum queue_stop_reason reason)
69926da23b6SLuciano Coelho {
70026da23b6SLuciano Coelho 	ieee80211_stop_queues_by_reason(&local->hw,
70126da23b6SLuciano Coelho 					ieee80211_get_vif_queues(local, sdata),
70226da23b6SLuciano Coelho 					reason, true);
70326da23b6SLuciano Coelho }
70426da23b6SLuciano Coelho 
70526da23b6SLuciano Coelho void ieee80211_wake_vif_queues(struct ieee80211_local *local,
70626da23b6SLuciano Coelho 			       struct ieee80211_sub_if_data *sdata,
70726da23b6SLuciano Coelho 			       enum queue_stop_reason reason)
70826da23b6SLuciano Coelho {
70926da23b6SLuciano Coelho 	ieee80211_wake_queues_by_reason(&local->hw,
71026da23b6SLuciano Coelho 					ieee80211_get_vif_queues(local, sdata),
71126da23b6SLuciano Coelho 					reason, true);
71226da23b6SLuciano Coelho }
71326da23b6SLuciano Coelho 
7143384d757SArik Nemtsov static void __iterate_interfaces(struct ieee80211_local *local,
715c7c71066SJohannes Berg 				 u32 iter_flags,
716dabeb344SJohannes Berg 				 void (*iterator)(void *data, u8 *mac,
71732bfd35dSJohannes Berg 						  struct ieee80211_vif *vif),
718dabeb344SJohannes Berg 				 void *data)
719dabeb344SJohannes Berg {
720dabeb344SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
7213384d757SArik Nemtsov 	bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE;
722dabeb344SJohannes Berg 
723e38bad47SJohannes Berg 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
72451fb61e7SJohannes Berg 		switch (sdata->vif.type) {
72505c914feSJohannes Berg 		case NL80211_IFTYPE_MONITOR:
726d8212184SAviya Erenfeld 			if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
72731eba5bcSFelix Fietkau 				continue;
72831eba5bcSFelix Fietkau 			break;
72905c914feSJohannes Berg 		case NL80211_IFTYPE_AP_VLAN:
730dabeb344SJohannes Berg 			continue;
7312ca27bcfSJohannes Berg 		default:
732dabeb344SJohannes Berg 			break;
733dabeb344SJohannes Berg 		}
7348b2c9824SJohannes Berg 		if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
7353384d757SArik Nemtsov 		    active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
7368b2c9824SJohannes Berg 			continue;
7373384d757SArik Nemtsov 		if (ieee80211_sdata_running(sdata) || !active_only)
73847846c9bSJohannes Berg 			iterator(data, sdata->vif.addr,
73932bfd35dSJohannes Berg 				 &sdata->vif);
740dabeb344SJohannes Berg 	}
741e38bad47SJohannes Berg 
742c7c71066SJohannes Berg 	sdata = rcu_dereference_check(local->monitor_sdata,
743c7c71066SJohannes Berg 				      lockdep_is_held(&local->iflist_mtx) ||
744c7c71066SJohannes Berg 				      lockdep_rtnl_is_held());
7458b2c9824SJohannes Berg 	if (sdata &&
7463384d757SArik Nemtsov 	    (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only ||
7478b2c9824SJohannes Berg 	     sdata->flags & IEEE80211_SDATA_IN_DRIVER))
748685fb72bSJohannes Berg 		iterator(data, sdata->vif.addr, &sdata->vif);
749c7c71066SJohannes Berg }
750685fb72bSJohannes Berg 
7513384d757SArik Nemtsov void ieee80211_iterate_interfaces(
752c7c71066SJohannes Berg 	struct ieee80211_hw *hw, u32 iter_flags,
753c7c71066SJohannes Berg 	void (*iterator)(void *data, u8 *mac,
754c7c71066SJohannes Berg 			 struct ieee80211_vif *vif),
755c7c71066SJohannes Berg 	void *data)
756c7c71066SJohannes Berg {
757c7c71066SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
758c7c71066SJohannes Berg 
759c7c71066SJohannes Berg 	mutex_lock(&local->iflist_mtx);
7603384d757SArik Nemtsov 	__iterate_interfaces(local, iter_flags, iterator, data);
761c7c71066SJohannes Berg 	mutex_unlock(&local->iflist_mtx);
762c7c71066SJohannes Berg }
7633384d757SArik Nemtsov EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces);
764c7c71066SJohannes Berg 
765c7c71066SJohannes Berg void ieee80211_iterate_active_interfaces_atomic(
766c7c71066SJohannes Berg 	struct ieee80211_hw *hw, u32 iter_flags,
767c7c71066SJohannes Berg 	void (*iterator)(void *data, u8 *mac,
768c7c71066SJohannes Berg 			 struct ieee80211_vif *vif),
769c7c71066SJohannes Berg 	void *data)
770c7c71066SJohannes Berg {
771c7c71066SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
772c7c71066SJohannes Berg 
773c7c71066SJohannes Berg 	rcu_read_lock();
7743384d757SArik Nemtsov 	__iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
7753384d757SArik Nemtsov 			     iterator, data);
776e38bad47SJohannes Berg 	rcu_read_unlock();
777dabeb344SJohannes Berg }
7782f561febSIvo van Doorn EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
77937ffc8daSJohannes Berg 
780c7c71066SJohannes Berg void ieee80211_iterate_active_interfaces_rtnl(
781c7c71066SJohannes Berg 	struct ieee80211_hw *hw, u32 iter_flags,
782c7c71066SJohannes Berg 	void (*iterator)(void *data, u8 *mac,
783c7c71066SJohannes Berg 			 struct ieee80211_vif *vif),
784c7c71066SJohannes Berg 	void *data)
785c7c71066SJohannes Berg {
786c7c71066SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
787c7c71066SJohannes Berg 
788c7c71066SJohannes Berg 	ASSERT_RTNL();
789c7c71066SJohannes Berg 
7903384d757SArik Nemtsov 	__iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
7913384d757SArik Nemtsov 			     iterator, data);
792c7c71066SJohannes Berg }
793c7c71066SJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_rtnl);
794c7c71066SJohannes Berg 
7950fc1e049SArik Nemtsov static void __iterate_stations(struct ieee80211_local *local,
7960fc1e049SArik Nemtsov 			       void (*iterator)(void *data,
7970fc1e049SArik Nemtsov 						struct ieee80211_sta *sta),
7980fc1e049SArik Nemtsov 			       void *data)
7990fc1e049SArik Nemtsov {
8000fc1e049SArik Nemtsov 	struct sta_info *sta;
8010fc1e049SArik Nemtsov 
8020fc1e049SArik Nemtsov 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
8030fc1e049SArik Nemtsov 		if (!sta->uploaded)
8040fc1e049SArik Nemtsov 			continue;
8050fc1e049SArik Nemtsov 
8060fc1e049SArik Nemtsov 		iterator(data, &sta->sta);
8070fc1e049SArik Nemtsov 	}
8080fc1e049SArik Nemtsov }
8090fc1e049SArik Nemtsov 
8100fc1e049SArik Nemtsov void ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
8110fc1e049SArik Nemtsov 			void (*iterator)(void *data,
8120fc1e049SArik Nemtsov 					 struct ieee80211_sta *sta),
8130fc1e049SArik Nemtsov 			void *data)
8140fc1e049SArik Nemtsov {
8150fc1e049SArik Nemtsov 	struct ieee80211_local *local = hw_to_local(hw);
8160fc1e049SArik Nemtsov 
8170fc1e049SArik Nemtsov 	rcu_read_lock();
8180fc1e049SArik Nemtsov 	__iterate_stations(local, iterator, data);
8190fc1e049SArik Nemtsov 	rcu_read_unlock();
8200fc1e049SArik Nemtsov }
8210fc1e049SArik Nemtsov EXPORT_SYMBOL_GPL(ieee80211_iterate_stations_atomic);
8220fc1e049SArik Nemtsov 
823ad7e718cSJohannes Berg struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
824ad7e718cSJohannes Berg {
825ad7e718cSJohannes Berg 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
826ad7e718cSJohannes Berg 
827ad7e718cSJohannes Berg 	if (!ieee80211_sdata_running(sdata) ||
828ad7e718cSJohannes Berg 	    !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
829ad7e718cSJohannes Berg 		return NULL;
830ad7e718cSJohannes Berg 	return &sdata->vif;
831ad7e718cSJohannes Berg }
832ad7e718cSJohannes Berg EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
833ad7e718cSJohannes Berg 
834dc5a1ad7SEmmanuel Grumbach struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
835dc5a1ad7SEmmanuel Grumbach {
8366513e98eSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8376513e98eSJohannes Berg 
8386513e98eSJohannes Berg 	if (!vif)
8396513e98eSJohannes Berg 		return NULL;
8406513e98eSJohannes Berg 
8416513e98eSJohannes Berg 	sdata = vif_to_sdata(vif);
842dc5a1ad7SEmmanuel Grumbach 
843dc5a1ad7SEmmanuel Grumbach 	if (!ieee80211_sdata_running(sdata) ||
844dc5a1ad7SEmmanuel Grumbach 	    !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
845dc5a1ad7SEmmanuel Grumbach 		return NULL;
846dc5a1ad7SEmmanuel Grumbach 
847dc5a1ad7SEmmanuel Grumbach 	return &sdata->wdev;
848dc5a1ad7SEmmanuel Grumbach }
849dc5a1ad7SEmmanuel Grumbach EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
850dc5a1ad7SEmmanuel Grumbach 
85142935ecaSLuis R. Rodriguez /*
85242935ecaSLuis R. Rodriguez  * Nothing should have been stuffed into the workqueue during
8534afaff17SEmmanuel Grumbach  * the suspend->resume cycle. Since we can't check each caller
8544afaff17SEmmanuel Grumbach  * of this function if we are already quiescing / suspended,
8554afaff17SEmmanuel Grumbach  * check here and don't WARN since this can actually happen when
8564afaff17SEmmanuel Grumbach  * the rx path (for example) is racing against __ieee80211_suspend
8574afaff17SEmmanuel Grumbach  * and suspending / quiescing was set after the rx path checked
8584afaff17SEmmanuel Grumbach  * them.
85942935ecaSLuis R. Rodriguez  */
86042935ecaSLuis R. Rodriguez static bool ieee80211_can_queue_work(struct ieee80211_local *local)
86142935ecaSLuis R. Rodriguez {
8624afaff17SEmmanuel Grumbach 	if (local->quiescing || (local->suspended && !local->resuming)) {
8634afaff17SEmmanuel Grumbach 		pr_warn("queueing ieee80211 work while going to suspend\n");
86442935ecaSLuis R. Rodriguez 		return false;
8654afaff17SEmmanuel Grumbach 	}
86642935ecaSLuis R. Rodriguez 
86742935ecaSLuis R. Rodriguez 	return true;
86842935ecaSLuis R. Rodriguez }
86942935ecaSLuis R. Rodriguez 
87042935ecaSLuis R. Rodriguez void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
87142935ecaSLuis R. Rodriguez {
87242935ecaSLuis R. Rodriguez 	struct ieee80211_local *local = hw_to_local(hw);
87342935ecaSLuis R. Rodriguez 
87442935ecaSLuis R. Rodriguez 	if (!ieee80211_can_queue_work(local))
87542935ecaSLuis R. Rodriguez 		return;
87642935ecaSLuis R. Rodriguez 
87742935ecaSLuis R. Rodriguez 	queue_work(local->workqueue, work);
87842935ecaSLuis R. Rodriguez }
87942935ecaSLuis R. Rodriguez EXPORT_SYMBOL(ieee80211_queue_work);
88042935ecaSLuis R. Rodriguez 
88142935ecaSLuis R. Rodriguez void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
88242935ecaSLuis R. Rodriguez 				  struct delayed_work *dwork,
88342935ecaSLuis R. Rodriguez 				  unsigned long delay)
88442935ecaSLuis R. Rodriguez {
88542935ecaSLuis R. Rodriguez 	struct ieee80211_local *local = hw_to_local(hw);
88642935ecaSLuis R. Rodriguez 
88742935ecaSLuis R. Rodriguez 	if (!ieee80211_can_queue_work(local))
88842935ecaSLuis R. Rodriguez 		return;
88942935ecaSLuis R. Rodriguez 
89042935ecaSLuis R. Rodriguez 	queue_delayed_work(local->workqueue, dwork, delay);
89142935ecaSLuis R. Rodriguez }
89242935ecaSLuis R. Rodriguez EXPORT_SYMBOL(ieee80211_queue_delayed_work);
89342935ecaSLuis R. Rodriguez 
89478ac51f8SSara Sharon static u32
89578ac51f8SSara Sharon _ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
896dd76986bSJohannes Berg 			    struct ieee802_11_elems *elems,
897671042a4SSara Sharon 			    u64 filter, u32 crc,
898671042a4SSara Sharon 			    const struct element *check_inherit)
899dd76986bSJohannes Berg {
900671042a4SSara Sharon 	const struct element *elem;
901dd76986bSJohannes Berg 	bool calc_crc = filter != 0;
902fcff4f10SPaul Stewart 	DECLARE_BITMAP(seen_elems, 256);
903b2e506bfSJohannes Berg 	const u8 *ie;
904dd76986bSJohannes Berg 
905fcff4f10SPaul Stewart 	bitmap_zero(seen_elems, 256);
906dd76986bSJohannes Berg 
907c17e28d1SJohannes Berg 	for_each_element(elem, start, len) {
908fcff4f10SPaul Stewart 		bool elem_parse_failed;
909c17e28d1SJohannes Berg 		u8 id = elem->id;
910c17e28d1SJohannes Berg 		u8 elen = elem->datalen;
911c17e28d1SJohannes Berg 		const u8 *pos = elem->data;
912fcff4f10SPaul Stewart 
913671042a4SSara Sharon 		if (check_inherit &&
914671042a4SSara Sharon 		    !cfg80211_is_element_inherited(elem,
915671042a4SSara Sharon 						   check_inherit))
916671042a4SSara Sharon 			continue;
917671042a4SSara Sharon 
9189690fb16SJohannes Berg 		switch (id) {
9199690fb16SJohannes Berg 		case WLAN_EID_SSID:
9209690fb16SJohannes Berg 		case WLAN_EID_SUPP_RATES:
9219690fb16SJohannes Berg 		case WLAN_EID_FH_PARAMS:
9229690fb16SJohannes Berg 		case WLAN_EID_DS_PARAMS:
9239690fb16SJohannes Berg 		case WLAN_EID_CF_PARAMS:
9249690fb16SJohannes Berg 		case WLAN_EID_TIM:
9259690fb16SJohannes Berg 		case WLAN_EID_IBSS_PARAMS:
9269690fb16SJohannes Berg 		case WLAN_EID_CHALLENGE:
9279690fb16SJohannes Berg 		case WLAN_EID_RSN:
9289690fb16SJohannes Berg 		case WLAN_EID_ERP_INFO:
9299690fb16SJohannes Berg 		case WLAN_EID_EXT_SUPP_RATES:
9309690fb16SJohannes Berg 		case WLAN_EID_HT_CAPABILITY:
9319690fb16SJohannes Berg 		case WLAN_EID_HT_OPERATION:
9329690fb16SJohannes Berg 		case WLAN_EID_VHT_CAPABILITY:
9339690fb16SJohannes Berg 		case WLAN_EID_VHT_OPERATION:
9349690fb16SJohannes Berg 		case WLAN_EID_MESH_ID:
9359690fb16SJohannes Berg 		case WLAN_EID_MESH_CONFIG:
9369690fb16SJohannes Berg 		case WLAN_EID_PEER_MGMT:
9379690fb16SJohannes Berg 		case WLAN_EID_PREQ:
9389690fb16SJohannes Berg 		case WLAN_EID_PREP:
9399690fb16SJohannes Berg 		case WLAN_EID_PERR:
9409690fb16SJohannes Berg 		case WLAN_EID_RANN:
9419690fb16SJohannes Berg 		case WLAN_EID_CHANNEL_SWITCH:
9429690fb16SJohannes Berg 		case WLAN_EID_EXT_CHANSWITCH_ANN:
9439690fb16SJohannes Berg 		case WLAN_EID_COUNTRY:
9449690fb16SJohannes Berg 		case WLAN_EID_PWR_CONSTRAINT:
9459690fb16SJohannes Berg 		case WLAN_EID_TIMEOUT_INTERVAL:
94685220d71SJohannes Berg 		case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
947b2e506bfSJohannes Berg 		case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
9488f2535b9SChun-Yeow Yeoh 		case WLAN_EID_CHAN_SWITCH_PARAM:
9499041c1faSArik Nemtsov 		case WLAN_EID_EXT_CAPABILITY:
95053837584SArik Nemtsov 		case WLAN_EID_CHAN_SWITCH_TIMING:
95153837584SArik Nemtsov 		case WLAN_EID_LINK_ID:
952e38a017bSAvraham Stern 		case WLAN_EID_BSS_MAX_IDLE_PERIOD:
953b2e506bfSJohannes Berg 		/*
954b2e506bfSJohannes Berg 		 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
955b2e506bfSJohannes Berg 		 * that if the content gets bigger it might be needed more than once
956b2e506bfSJohannes Berg 		 */
9579690fb16SJohannes Berg 			if (test_bit(id, seen_elems)) {
958fcff4f10SPaul Stewart 				elems->parse_error = true;
959fcff4f10SPaul Stewart 				continue;
960fcff4f10SPaul Stewart 			}
9619690fb16SJohannes Berg 			break;
9629690fb16SJohannes Berg 		}
963dd76986bSJohannes Berg 
964dd76986bSJohannes Berg 		if (calc_crc && id < 64 && (filter & (1ULL << id)))
965dd76986bSJohannes Berg 			crc = crc32_be(crc, pos - 2, elen + 2);
966dd76986bSJohannes Berg 
967fcff4f10SPaul Stewart 		elem_parse_failed = false;
968fcff4f10SPaul Stewart 
969dd76986bSJohannes Berg 		switch (id) {
97053837584SArik Nemtsov 		case WLAN_EID_LINK_ID:
97153837584SArik Nemtsov 			if (elen + 2 != sizeof(struct ieee80211_tdls_lnkie)) {
97253837584SArik Nemtsov 				elem_parse_failed = true;
97353837584SArik Nemtsov 				break;
97453837584SArik Nemtsov 			}
97553837584SArik Nemtsov 			elems->lnk_id = (void *)(pos - 2);
97653837584SArik Nemtsov 			break;
97753837584SArik Nemtsov 		case WLAN_EID_CHAN_SWITCH_TIMING:
97853837584SArik Nemtsov 			if (elen != sizeof(struct ieee80211_ch_switch_timing)) {
97953837584SArik Nemtsov 				elem_parse_failed = true;
98053837584SArik Nemtsov 				break;
98153837584SArik Nemtsov 			}
98253837584SArik Nemtsov 			elems->ch_sw_timing = (void *)pos;
98353837584SArik Nemtsov 			break;
9849041c1faSArik Nemtsov 		case WLAN_EID_EXT_CAPABILITY:
9859041c1faSArik Nemtsov 			elems->ext_capab = pos;
9869041c1faSArik Nemtsov 			elems->ext_capab_len = elen;
9879041c1faSArik Nemtsov 			break;
988dd76986bSJohannes Berg 		case WLAN_EID_SSID:
989dd76986bSJohannes Berg 			elems->ssid = pos;
990dd76986bSJohannes Berg 			elems->ssid_len = elen;
991dd76986bSJohannes Berg 			break;
992dd76986bSJohannes Berg 		case WLAN_EID_SUPP_RATES:
993dd76986bSJohannes Berg 			elems->supp_rates = pos;
994dd76986bSJohannes Berg 			elems->supp_rates_len = elen;
995dd76986bSJohannes Berg 			break;
996dd76986bSJohannes Berg 		case WLAN_EID_DS_PARAMS:
9971cd8e88eSJohannes Berg 			if (elen >= 1)
998dd76986bSJohannes Berg 				elems->ds_params = pos;
9991cd8e88eSJohannes Berg 			else
10001cd8e88eSJohannes Berg 				elem_parse_failed = true;
1001dd76986bSJohannes Berg 			break;
1002dd76986bSJohannes Berg 		case WLAN_EID_TIM:
1003dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_tim_ie)) {
1004dd76986bSJohannes Berg 				elems->tim = (void *)pos;
1005dd76986bSJohannes Berg 				elems->tim_len = elen;
1006fcff4f10SPaul Stewart 			} else
1007fcff4f10SPaul Stewart 				elem_parse_failed = true;
1008dd76986bSJohannes Berg 			break;
1009dd76986bSJohannes Berg 		case WLAN_EID_CHALLENGE:
1010dd76986bSJohannes Berg 			elems->challenge = pos;
1011dd76986bSJohannes Berg 			elems->challenge_len = elen;
1012dd76986bSJohannes Berg 			break;
1013dd76986bSJohannes Berg 		case WLAN_EID_VENDOR_SPECIFIC:
1014dd76986bSJohannes Berg 			if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
1015dd76986bSJohannes Berg 			    pos[2] == 0xf2) {
1016dd76986bSJohannes Berg 				/* Microsoft OUI (00:50:F2) */
1017dd76986bSJohannes Berg 
1018dd76986bSJohannes Berg 				if (calc_crc)
1019dd76986bSJohannes Berg 					crc = crc32_be(crc, pos - 2, elen + 2);
1020dd76986bSJohannes Berg 
1021441a33baSJohannes Berg 				if (elen >= 5 && pos[3] == 2) {
1022dd76986bSJohannes Berg 					/* OUI Type 2 - WMM IE */
1023dd76986bSJohannes Berg 					if (pos[4] == 0) {
1024dd76986bSJohannes Berg 						elems->wmm_info = pos;
1025dd76986bSJohannes Berg 						elems->wmm_info_len = elen;
1026dd76986bSJohannes Berg 					} else if (pos[4] == 1) {
1027dd76986bSJohannes Berg 						elems->wmm_param = pos;
1028dd76986bSJohannes Berg 						elems->wmm_param_len = elen;
1029dd76986bSJohannes Berg 					}
1030dd76986bSJohannes Berg 				}
1031dd76986bSJohannes Berg 			}
1032dd76986bSJohannes Berg 			break;
1033dd76986bSJohannes Berg 		case WLAN_EID_RSN:
1034dd76986bSJohannes Berg 			elems->rsn = pos;
1035dd76986bSJohannes Berg 			elems->rsn_len = elen;
1036dd76986bSJohannes Berg 			break;
1037dd76986bSJohannes Berg 		case WLAN_EID_ERP_INFO:
10381946bed9SJohannes Berg 			if (elen >= 1)
1039dd76986bSJohannes Berg 				elems->erp_info = pos;
10401946bed9SJohannes Berg 			else
10411946bed9SJohannes Berg 				elem_parse_failed = true;
1042dd76986bSJohannes Berg 			break;
1043dd76986bSJohannes Berg 		case WLAN_EID_EXT_SUPP_RATES:
1044dd76986bSJohannes Berg 			elems->ext_supp_rates = pos;
1045dd76986bSJohannes Berg 			elems->ext_supp_rates_len = elen;
1046dd76986bSJohannes Berg 			break;
1047dd76986bSJohannes Berg 		case WLAN_EID_HT_CAPABILITY:
1048dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_ht_cap))
1049dd76986bSJohannes Berg 				elems->ht_cap_elem = (void *)pos;
1050fcff4f10SPaul Stewart 			else
1051fcff4f10SPaul Stewart 				elem_parse_failed = true;
1052dd76986bSJohannes Berg 			break;
1053074d46d1SJohannes Berg 		case WLAN_EID_HT_OPERATION:
1054074d46d1SJohannes Berg 			if (elen >= sizeof(struct ieee80211_ht_operation))
1055074d46d1SJohannes Berg 				elems->ht_operation = (void *)pos;
1056fcff4f10SPaul Stewart 			else
1057fcff4f10SPaul Stewart 				elem_parse_failed = true;
1058dd76986bSJohannes Berg 			break;
1059818255eaSMahesh Palivela 		case WLAN_EID_VHT_CAPABILITY:
1060818255eaSMahesh Palivela 			if (elen >= sizeof(struct ieee80211_vht_cap))
1061818255eaSMahesh Palivela 				elems->vht_cap_elem = (void *)pos;
1062818255eaSMahesh Palivela 			else
1063818255eaSMahesh Palivela 				elem_parse_failed = true;
1064818255eaSMahesh Palivela 			break;
1065818255eaSMahesh Palivela 		case WLAN_EID_VHT_OPERATION:
1066818255eaSMahesh Palivela 			if (elen >= sizeof(struct ieee80211_vht_operation))
1067818255eaSMahesh Palivela 				elems->vht_operation = (void *)pos;
1068818255eaSMahesh Palivela 			else
1069818255eaSMahesh Palivela 				elem_parse_failed = true;
1070818255eaSMahesh Palivela 			break;
1071bee7f586SJohannes Berg 		case WLAN_EID_OPMODE_NOTIF:
1072bee7f586SJohannes Berg 			if (elen > 0)
1073bee7f586SJohannes Berg 				elems->opmode_notif = pos;
1074bee7f586SJohannes Berg 			else
1075bee7f586SJohannes Berg 				elem_parse_failed = true;
1076bee7f586SJohannes Berg 			break;
1077dd76986bSJohannes Berg 		case WLAN_EID_MESH_ID:
1078dd76986bSJohannes Berg 			elems->mesh_id = pos;
1079dd76986bSJohannes Berg 			elems->mesh_id_len = elen;
1080dd76986bSJohannes Berg 			break;
1081dd76986bSJohannes Berg 		case WLAN_EID_MESH_CONFIG:
1082dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_meshconf_ie))
1083dd76986bSJohannes Berg 				elems->mesh_config = (void *)pos;
1084fcff4f10SPaul Stewart 			else
1085fcff4f10SPaul Stewart 				elem_parse_failed = true;
1086dd76986bSJohannes Berg 			break;
1087dd76986bSJohannes Berg 		case WLAN_EID_PEER_MGMT:
1088dd76986bSJohannes Berg 			elems->peering = pos;
1089dd76986bSJohannes Berg 			elems->peering_len = elen;
1090dd76986bSJohannes Berg 			break;
10913f52b7e3SMarco Porsch 		case WLAN_EID_MESH_AWAKE_WINDOW:
10923f52b7e3SMarco Porsch 			if (elen >= 2)
10933f52b7e3SMarco Porsch 				elems->awake_window = (void *)pos;
10943f52b7e3SMarco Porsch 			break;
1095dd76986bSJohannes Berg 		case WLAN_EID_PREQ:
1096dd76986bSJohannes Berg 			elems->preq = pos;
1097dd76986bSJohannes Berg 			elems->preq_len = elen;
1098dd76986bSJohannes Berg 			break;
1099dd76986bSJohannes Berg 		case WLAN_EID_PREP:
1100dd76986bSJohannes Berg 			elems->prep = pos;
1101dd76986bSJohannes Berg 			elems->prep_len = elen;
1102dd76986bSJohannes Berg 			break;
1103dd76986bSJohannes Berg 		case WLAN_EID_PERR:
1104dd76986bSJohannes Berg 			elems->perr = pos;
1105dd76986bSJohannes Berg 			elems->perr_len = elen;
1106dd76986bSJohannes Berg 			break;
1107dd76986bSJohannes Berg 		case WLAN_EID_RANN:
1108dd76986bSJohannes Berg 			if (elen >= sizeof(struct ieee80211_rann_ie))
1109dd76986bSJohannes Berg 				elems->rann = (void *)pos;
1110fcff4f10SPaul Stewart 			else
1111fcff4f10SPaul Stewart 				elem_parse_failed = true;
1112dd76986bSJohannes Berg 			break;
1113dd76986bSJohannes Berg 		case WLAN_EID_CHANNEL_SWITCH:
11145bc1420bSJohannes Berg 			if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
11155bc1420bSJohannes Berg 				elem_parse_failed = true;
11165bc1420bSJohannes Berg 				break;
11175bc1420bSJohannes Berg 			}
11185bc1420bSJohannes Berg 			elems->ch_switch_ie = (void *)pos;
1119dd76986bSJohannes Berg 			break;
1120b4f286a1SJohannes Berg 		case WLAN_EID_EXT_CHANSWITCH_ANN:
1121b4f286a1SJohannes Berg 			if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
1122b4f286a1SJohannes Berg 				elem_parse_failed = true;
1123b4f286a1SJohannes Berg 				break;
1124b4f286a1SJohannes Berg 			}
1125b4f286a1SJohannes Berg 			elems->ext_chansw_ie = (void *)pos;
1126b4f286a1SJohannes Berg 			break;
112785220d71SJohannes Berg 		case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
112885220d71SJohannes Berg 			if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
112985220d71SJohannes Berg 				elem_parse_failed = true;
113085220d71SJohannes Berg 				break;
113185220d71SJohannes Berg 			}
113285220d71SJohannes Berg 			elems->sec_chan_offs = (void *)pos;
113385220d71SJohannes Berg 			break;
11348f2535b9SChun-Yeow Yeoh 		case WLAN_EID_CHAN_SWITCH_PARAM:
11358f2535b9SChun-Yeow Yeoh 			if (elen !=
11368f2535b9SChun-Yeow Yeoh 			    sizeof(*elems->mesh_chansw_params_ie)) {
11378f2535b9SChun-Yeow Yeoh 				elem_parse_failed = true;
11388f2535b9SChun-Yeow Yeoh 				break;
11398f2535b9SChun-Yeow Yeoh 			}
11408f2535b9SChun-Yeow Yeoh 			elems->mesh_chansw_params_ie = (void *)pos;
11418f2535b9SChun-Yeow Yeoh 			break;
1142b2e506bfSJohannes Berg 		case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1143b2e506bfSJohannes Berg 			if (!action ||
1144b2e506bfSJohannes Berg 			    elen != sizeof(*elems->wide_bw_chansw_ie)) {
1145b2e506bfSJohannes Berg 				elem_parse_failed = true;
1146b2e506bfSJohannes Berg 				break;
1147b2e506bfSJohannes Berg 			}
1148b2e506bfSJohannes Berg 			elems->wide_bw_chansw_ie = (void *)pos;
1149b2e506bfSJohannes Berg 			break;
1150b2e506bfSJohannes Berg 		case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
1151b2e506bfSJohannes Berg 			if (action) {
1152b2e506bfSJohannes Berg 				elem_parse_failed = true;
1153b2e506bfSJohannes Berg 				break;
1154b2e506bfSJohannes Berg 			}
1155b2e506bfSJohannes Berg 			/*
1156b2e506bfSJohannes Berg 			 * This is a bit tricky, but as we only care about
1157b2e506bfSJohannes Berg 			 * the wide bandwidth channel switch element, so
1158b2e506bfSJohannes Berg 			 * just parse it out manually.
1159b2e506bfSJohannes Berg 			 */
1160b2e506bfSJohannes Berg 			ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
1161b2e506bfSJohannes Berg 					      pos, elen);
1162b2e506bfSJohannes Berg 			if (ie) {
1163b2e506bfSJohannes Berg 				if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
1164b2e506bfSJohannes Berg 					elems->wide_bw_chansw_ie =
1165b2e506bfSJohannes Berg 						(void *)(ie + 2);
1166b2e506bfSJohannes Berg 				else
1167b2e506bfSJohannes Berg 					elem_parse_failed = true;
1168b2e506bfSJohannes Berg 			}
1169b2e506bfSJohannes Berg 			break;
1170dd76986bSJohannes Berg 		case WLAN_EID_COUNTRY:
1171dd76986bSJohannes Berg 			elems->country_elem = pos;
1172dd76986bSJohannes Berg 			elems->country_elem_len = elen;
1173dd76986bSJohannes Berg 			break;
1174dd76986bSJohannes Berg 		case WLAN_EID_PWR_CONSTRAINT:
1175761a48d2SJohannes Berg 			if (elen != 1) {
1176761a48d2SJohannes Berg 				elem_parse_failed = true;
1177761a48d2SJohannes Berg 				break;
1178761a48d2SJohannes Berg 			}
1179dd76986bSJohannes Berg 			elems->pwr_constr_elem = pos;
1180dd76986bSJohannes Berg 			break;
1181c8d65917SSteinar H. Gunderson 		case WLAN_EID_CISCO_VENDOR_SPECIFIC:
1182c8d65917SSteinar H. Gunderson 			/* Lots of different options exist, but we only care
1183c8d65917SSteinar H. Gunderson 			 * about the Dynamic Transmit Power Control element.
1184c8d65917SSteinar H. Gunderson 			 * First check for the Cisco OUI, then for the DTPC
1185c8d65917SSteinar H. Gunderson 			 * tag (0x00).
1186c8d65917SSteinar H. Gunderson 			 */
1187c8d65917SSteinar H. Gunderson 			if (elen < 4) {
1188c8d65917SSteinar H. Gunderson 				elem_parse_failed = true;
1189c8d65917SSteinar H. Gunderson 				break;
1190c8d65917SSteinar H. Gunderson 			}
1191c8d65917SSteinar H. Gunderson 
1192c8d65917SSteinar H. Gunderson 			if (pos[0] != 0x00 || pos[1] != 0x40 ||
1193c8d65917SSteinar H. Gunderson 			    pos[2] != 0x96 || pos[3] != 0x00)
1194c8d65917SSteinar H. Gunderson 				break;
1195c8d65917SSteinar H. Gunderson 
1196c8d65917SSteinar H. Gunderson 			if (elen != 6) {
1197c8d65917SSteinar H. Gunderson 				elem_parse_failed = true;
1198c8d65917SSteinar H. Gunderson 				break;
1199c8d65917SSteinar H. Gunderson 			}
1200c8d65917SSteinar H. Gunderson 
1201c8d65917SSteinar H. Gunderson 			if (calc_crc)
1202c8d65917SSteinar H. Gunderson 				crc = crc32_be(crc, pos - 2, elen + 2);
1203c8d65917SSteinar H. Gunderson 
1204c8d65917SSteinar H. Gunderson 			elems->cisco_dtpc_elem = pos;
1205c8d65917SSteinar H. Gunderson 			break;
1206dd76986bSJohannes Berg 		case WLAN_EID_TIMEOUT_INTERVAL:
120779ba1d89SJohannes Berg 			if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
120879ba1d89SJohannes Berg 				elems->timeout_int = (void *)pos;
120979ba1d89SJohannes Berg 			else
121079ba1d89SJohannes Berg 				elem_parse_failed = true;
1211dd76986bSJohannes Berg 			break;
1212e38a017bSAvraham Stern 		case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1213e38a017bSAvraham Stern 			if (elen >= sizeof(*elems->max_idle_period_ie))
1214e38a017bSAvraham Stern 				elems->max_idle_period_ie = (void *)pos;
1215e38a017bSAvraham Stern 			break;
121641cbb0f5SLuca Coelho 		case WLAN_EID_EXTENSION:
121741cbb0f5SLuca Coelho 			if (pos[0] == WLAN_EID_EXT_HE_MU_EDCA &&
121841cbb0f5SLuca Coelho 			    elen >= (sizeof(*elems->mu_edca_param_set) + 1)) {
121941cbb0f5SLuca Coelho 				elems->mu_edca_param_set = (void *)&pos[1];
12202e249fc3SShaul Triebitz 				if (calc_crc)
12212e249fc3SShaul Triebitz 					crc = crc32_be(crc, pos - 2, elen + 2);
122241cbb0f5SLuca Coelho 			} else if (pos[0] == WLAN_EID_EXT_HE_CAPABILITY) {
122341cbb0f5SLuca Coelho 				elems->he_cap = (void *)&pos[1];
122441cbb0f5SLuca Coelho 				elems->he_cap_len = elen - 1;
122541cbb0f5SLuca Coelho 			} else if (pos[0] == WLAN_EID_EXT_HE_OPERATION &&
122641cbb0f5SLuca Coelho 				   elen >= sizeof(*elems->he_operation) &&
122741cbb0f5SLuca Coelho 				   elen >= ieee80211_he_oper_size(&pos[1])) {
122841cbb0f5SLuca Coelho 				elems->he_operation = (void *)&pos[1];
122941cbb0f5SLuca Coelho 			} else if (pos[0] == WLAN_EID_EXT_UORA && elen >= 1) {
123041cbb0f5SLuca Coelho 				elems->uora_element = (void *)&pos[1];
123178ac51f8SSara Sharon 			} else if (pos[0] ==
1232ee145775SSara Sharon 				   WLAN_EID_EXT_MAX_CHANNEL_SWITCH_TIME &&
1233ee145775SSara Sharon 				   elen == 4) {
1234ee145775SSara Sharon 				elems->max_channel_switch_time = pos + 1;
1235ee145775SSara Sharon 			} else if (pos[0] ==
123678ac51f8SSara Sharon 				   WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION &&
123778ac51f8SSara Sharon 				   elen == 3) {
123878ac51f8SSara Sharon 				elems->mbssid_config_ie = (void *)&pos[1];
123941cbb0f5SLuca Coelho 			}
124041cbb0f5SLuca Coelho 			break;
1241dd76986bSJohannes Berg 		default:
1242dd76986bSJohannes Berg 			break;
1243dd76986bSJohannes Berg 		}
1244dd76986bSJohannes Berg 
1245fcff4f10SPaul Stewart 		if (elem_parse_failed)
1246fcff4f10SPaul Stewart 			elems->parse_error = true;
1247fcff4f10SPaul Stewart 		else
12485df45690SJohannes Berg 			__set_bit(id, seen_elems);
1249dd76986bSJohannes Berg 	}
1250dd76986bSJohannes Berg 
1251c17e28d1SJohannes Berg 	if (!for_each_element_completed(elem, start, len))
1252fcff4f10SPaul Stewart 		elems->parse_error = true;
1253fcff4f10SPaul Stewart 
1254dd76986bSJohannes Berg 	return crc;
1255dd76986bSJohannes Berg }
1256dd76986bSJohannes Berg 
12575023b14cSSara Sharon static size_t ieee802_11_find_bssid_profile(const u8 *start, size_t len,
1258671042a4SSara Sharon 					    struct ieee802_11_elems *elems,
1259671042a4SSara Sharon 					    u8 *transmitter_bssid,
12605023b14cSSara Sharon 					    u8 *bss_bssid,
12615809a5d5SDan Carpenter 					    u8 *nontransmitted_profile)
1262671042a4SSara Sharon {
1263671042a4SSara Sharon 	const struct element *elem, *sub;
12645023b14cSSara Sharon 	size_t profile_len = 0;
12655023b14cSSara Sharon 	bool found = false;
1266671042a4SSara Sharon 
1267671042a4SSara Sharon 	if (!bss_bssid || !transmitter_bssid)
12685023b14cSSara Sharon 		return profile_len;
1269671042a4SSara Sharon 
1270671042a4SSara Sharon 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, len) {
1271671042a4SSara Sharon 		if (elem->datalen < 2)
1272671042a4SSara Sharon 			continue;
1273671042a4SSara Sharon 
1274671042a4SSara Sharon 		for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1275671042a4SSara Sharon 			u8 new_bssid[ETH_ALEN];
1276671042a4SSara Sharon 			const u8 *index;
1277671042a4SSara Sharon 
1278671042a4SSara Sharon 			if (sub->id != 0 || sub->datalen < 4) {
1279671042a4SSara Sharon 				/* not a valid BSS profile */
1280671042a4SSara Sharon 				continue;
1281671042a4SSara Sharon 			}
1282671042a4SSara Sharon 
1283671042a4SSara Sharon 			if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1284671042a4SSara Sharon 			    sub->data[1] != 2) {
1285671042a4SSara Sharon 				/* The first element of the
1286671042a4SSara Sharon 				 * Nontransmitted BSSID Profile is not
1287671042a4SSara Sharon 				 * the Nontransmitted BSSID Capability
1288671042a4SSara Sharon 				 * element.
1289671042a4SSara Sharon 				 */
1290671042a4SSara Sharon 				continue;
1291671042a4SSara Sharon 			}
1292671042a4SSara Sharon 
12935809a5d5SDan Carpenter 			memset(nontransmitted_profile, 0, len);
12945023b14cSSara Sharon 			profile_len = cfg80211_merge_profile(start, len,
12955023b14cSSara Sharon 							     elem,
12965023b14cSSara Sharon 							     sub,
12975023b14cSSara Sharon 							     nontransmitted_profile,
12985023b14cSSara Sharon 							     len);
12995023b14cSSara Sharon 
1300671042a4SSara Sharon 			/* found a Nontransmitted BSSID Profile */
1301671042a4SSara Sharon 			index = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX,
13025809a5d5SDan Carpenter 						 nontransmitted_profile,
13035023b14cSSara Sharon 						 profile_len);
1304671042a4SSara Sharon 			if (!index || index[1] < 1 || index[2] == 0) {
1305671042a4SSara Sharon 				/* Invalid MBSSID Index element */
1306671042a4SSara Sharon 				continue;
1307671042a4SSara Sharon 			}
1308671042a4SSara Sharon 
1309671042a4SSara Sharon 			cfg80211_gen_new_bssid(transmitter_bssid,
1310671042a4SSara Sharon 					       elem->data[0],
1311671042a4SSara Sharon 					       index[2],
1312671042a4SSara Sharon 					       new_bssid);
1313671042a4SSara Sharon 			if (ether_addr_equal(new_bssid, bss_bssid)) {
13145023b14cSSara Sharon 				found = true;
1315671042a4SSara Sharon 				elems->bssid_index_len = index[1];
1316671042a4SSara Sharon 				elems->bssid_index = (void *)&index[2];
1317671042a4SSara Sharon 				break;
1318671042a4SSara Sharon 			}
1319671042a4SSara Sharon 		}
1320671042a4SSara Sharon 	}
13215023b14cSSara Sharon 
13225023b14cSSara Sharon 	return found ? profile_len : 0;
1323671042a4SSara Sharon }
1324671042a4SSara Sharon 
132578ac51f8SSara Sharon u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
132678ac51f8SSara Sharon 			       struct ieee802_11_elems *elems,
132778ac51f8SSara Sharon 			       u64 filter, u32 crc, u8 *transmitter_bssid,
132878ac51f8SSara Sharon 			       u8 *bss_bssid)
132978ac51f8SSara Sharon {
1330671042a4SSara Sharon 	const struct element *non_inherit = NULL;
13315023b14cSSara Sharon 	u8 *nontransmitted_profile;
13325023b14cSSara Sharon 	int nontransmitted_profile_len = 0;
1333671042a4SSara Sharon 
133478ac51f8SSara Sharon 	memset(elems, 0, sizeof(*elems));
133578ac51f8SSara Sharon 	elems->ie_start = start;
133678ac51f8SSara Sharon 	elems->total_len = len;
133778ac51f8SSara Sharon 
13385023b14cSSara Sharon 	nontransmitted_profile = kmalloc(len, GFP_ATOMIC);
13395023b14cSSara Sharon 	if (nontransmitted_profile) {
13405023b14cSSara Sharon 		nontransmitted_profile_len =
13415023b14cSSara Sharon 			ieee802_11_find_bssid_profile(start, len, elems,
13425023b14cSSara Sharon 						      transmitter_bssid,
13435023b14cSSara Sharon 						      bss_bssid,
13445809a5d5SDan Carpenter 						      nontransmitted_profile);
1345671042a4SSara Sharon 		non_inherit =
1346671042a4SSara Sharon 			cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
13475023b14cSSara Sharon 					       nontransmitted_profile,
13485023b14cSSara Sharon 					       nontransmitted_profile_len);
13495023b14cSSara Sharon 	}
1350671042a4SSara Sharon 
135178ac51f8SSara Sharon 	crc = _ieee802_11_parse_elems_crc(start, len, action, elems, filter,
1352671042a4SSara Sharon 					  crc, non_inherit);
135378ac51f8SSara Sharon 
135478ac51f8SSara Sharon 	/* Override with nontransmitted profile, if found */
13555023b14cSSara Sharon 	if (nontransmitted_profile_len)
13565023b14cSSara Sharon 		_ieee802_11_parse_elems_crc(nontransmitted_profile,
13575023b14cSSara Sharon 					    nontransmitted_profile_len,
1358671042a4SSara Sharon 					    action, elems, 0, 0, NULL);
135978ac51f8SSara Sharon 
136078ac51f8SSara Sharon 	if (elems->tim && !elems->parse_error) {
136178ac51f8SSara Sharon 		const struct ieee80211_tim_ie *tim_ie = elems->tim;
136278ac51f8SSara Sharon 
136378ac51f8SSara Sharon 		elems->dtim_period = tim_ie->dtim_period;
136478ac51f8SSara Sharon 		elems->dtim_count = tim_ie->dtim_count;
136578ac51f8SSara Sharon 	}
136678ac51f8SSara Sharon 
136778ac51f8SSara Sharon 	/* Override DTIM period and count if needed */
136878ac51f8SSara Sharon 	if (elems->bssid_index &&
136978ac51f8SSara Sharon 	    elems->bssid_index_len >=
137078ac51f8SSara Sharon 	    offsetofend(struct ieee80211_bssid_index, dtim_period))
137178ac51f8SSara Sharon 		elems->dtim_period = elems->bssid_index->dtim_period;
137278ac51f8SSara Sharon 
137378ac51f8SSara Sharon 	if (elems->bssid_index &&
137478ac51f8SSara Sharon 	    elems->bssid_index_len >=
137578ac51f8SSara Sharon 	    offsetofend(struct ieee80211_bssid_index, dtim_count))
137678ac51f8SSara Sharon 		elems->dtim_count = elems->bssid_index->dtim_count;
137778ac51f8SSara Sharon 
13785023b14cSSara Sharon 	kfree(nontransmitted_profile);
13795023b14cSSara Sharon 
138078ac51f8SSara Sharon 	return crc;
138178ac51f8SSara Sharon }
138278ac51f8SSara Sharon 
1383e552af05SHaim Dreyfuss void ieee80211_regulatory_limit_wmm_params(struct ieee80211_sub_if_data *sdata,
1384e552af05SHaim Dreyfuss 					   struct ieee80211_tx_queue_params
1385e552af05SHaim Dreyfuss 					   *qparam, int ac)
1386e552af05SHaim Dreyfuss {
1387e552af05SHaim Dreyfuss 	struct ieee80211_chanctx_conf *chanctx_conf;
1388e552af05SHaim Dreyfuss 	const struct ieee80211_reg_rule *rrule;
138938cb87eeSStanislaw Gruszka 	const struct ieee80211_wmm_ac *wmm_ac;
1390e552af05SHaim Dreyfuss 	u16 center_freq = 0;
1391e552af05SHaim Dreyfuss 
1392e552af05SHaim Dreyfuss 	if (sdata->vif.type != NL80211_IFTYPE_AP &&
1393e552af05SHaim Dreyfuss 	    sdata->vif.type != NL80211_IFTYPE_STATION)
1394e552af05SHaim Dreyfuss 		return;
1395e552af05SHaim Dreyfuss 
1396e552af05SHaim Dreyfuss 	rcu_read_lock();
1397e552af05SHaim Dreyfuss 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1398e552af05SHaim Dreyfuss 	if (chanctx_conf)
1399e552af05SHaim Dreyfuss 		center_freq = chanctx_conf->def.chan->center_freq;
1400e552af05SHaim Dreyfuss 
1401e552af05SHaim Dreyfuss 	if (!center_freq) {
1402e552af05SHaim Dreyfuss 		rcu_read_unlock();
1403e552af05SHaim Dreyfuss 		return;
1404e552af05SHaim Dreyfuss 	}
1405e552af05SHaim Dreyfuss 
1406e552af05SHaim Dreyfuss 	rrule = freq_reg_info(sdata->wdev.wiphy, MHZ_TO_KHZ(center_freq));
1407e552af05SHaim Dreyfuss 
140838cb87eeSStanislaw Gruszka 	if (IS_ERR_OR_NULL(rrule) || !rrule->has_wmm) {
1409e552af05SHaim Dreyfuss 		rcu_read_unlock();
1410e552af05SHaim Dreyfuss 		return;
1411e552af05SHaim Dreyfuss 	}
1412e552af05SHaim Dreyfuss 
1413e552af05SHaim Dreyfuss 	if (sdata->vif.type == NL80211_IFTYPE_AP)
141438cb87eeSStanislaw Gruszka 		wmm_ac = &rrule->wmm_rule.ap[ac];
1415e552af05SHaim Dreyfuss 	else
141638cb87eeSStanislaw Gruszka 		wmm_ac = &rrule->wmm_rule.client[ac];
1417e552af05SHaim Dreyfuss 	qparam->cw_min = max_t(u16, qparam->cw_min, wmm_ac->cw_min);
1418e552af05SHaim Dreyfuss 	qparam->cw_max = max_t(u16, qparam->cw_max, wmm_ac->cw_max);
1419e552af05SHaim Dreyfuss 	qparam->aifs = max_t(u8, qparam->aifs, wmm_ac->aifsn);
1420abd76d25SDreyfuss, Haim 	qparam->txop = min_t(u16, qparam->txop, wmm_ac->cot / 32);
1421e552af05SHaim Dreyfuss 	rcu_read_unlock();
1422e552af05SHaim Dreyfuss }
1423e552af05SHaim Dreyfuss 
14243abead59SJohannes Berg void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
1425cec66283SJohannes Berg 			       bool bss_notify, bool enable_qos)
14265825fe10SJohannes Berg {
14275825fe10SJohannes Berg 	struct ieee80211_local *local = sdata->local;
14285825fe10SJohannes Berg 	struct ieee80211_tx_queue_params qparam;
142955de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
143054bcbc69SJohannes Berg 	int ac;
1431cec66283SJohannes Berg 	bool use_11b;
1432239281f8SRostislav Lisovy 	bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
1433aa837e1dSJohannes Berg 	int aCWmin, aCWmax;
14345825fe10SJohannes Berg 
14355825fe10SJohannes Berg 	if (!local->ops->conf_tx)
14365825fe10SJohannes Berg 		return;
14375825fe10SJohannes Berg 
143854bcbc69SJohannes Berg 	if (local->hw.queues < IEEE80211_NUM_ACS)
143954bcbc69SJohannes Berg 		return;
144054bcbc69SJohannes Berg 
14415825fe10SJohannes Berg 	memset(&qparam, 0, sizeof(qparam));
14425825fe10SJohannes Berg 
144355de908aSJohannes Berg 	rcu_read_lock();
144455de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
144555de908aSJohannes Berg 	use_11b = (chanctx_conf &&
144657fbcce3SJohannes Berg 		   chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
1447aa837e1dSJohannes Berg 		 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
144855de908aSJohannes Berg 	rcu_read_unlock();
14495825fe10SJohannes Berg 
1450239281f8SRostislav Lisovy 	is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
1451239281f8SRostislav Lisovy 
1452aa837e1dSJohannes Berg 	/* Set defaults according to 802.11-2007 Table 7-37 */
1453aa837e1dSJohannes Berg 	aCWmax = 1023;
1454aa837e1dSJohannes Berg 	if (use_11b)
1455aa837e1dSJohannes Berg 		aCWmin = 31;
14565825fe10SJohannes Berg 	else
1457aa837e1dSJohannes Berg 		aCWmin = 15;
14585825fe10SJohannes Berg 
14591f4ffde8SFred Zhou 	/* Confiure old 802.11b/g medium access rules. */
14601f4ffde8SFred Zhou 	qparam.cw_max = aCWmax;
14611f4ffde8SFred Zhou 	qparam.cw_min = aCWmin;
14621f4ffde8SFred Zhou 	qparam.txop = 0;
14631f4ffde8SFred Zhou 	qparam.aifs = 2;
14641f4ffde8SFred Zhou 
14651f4ffde8SFred Zhou 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
14661f4ffde8SFred Zhou 		/* Update if QoS is enabled. */
1467a8ce8544SStanislaw Gruszka 		if (enable_qos) {
146854bcbc69SJohannes Berg 			switch (ac) {
14691d98fb12SJohannes Berg 			case IEEE80211_AC_BK:
14707ba10a8eSJohannes Berg 				qparam.cw_max = aCWmax;
14717ba10a8eSJohannes Berg 				qparam.cw_min = aCWmin;
14725825fe10SJohannes Berg 				qparam.txop = 0;
1473239281f8SRostislav Lisovy 				if (is_ocb)
1474239281f8SRostislav Lisovy 					qparam.aifs = 9;
1475239281f8SRostislav Lisovy 				else
1476aa837e1dSJohannes Berg 					qparam.aifs = 7;
1477aa837e1dSJohannes Berg 				break;
1478a8ce8544SStanislaw Gruszka 			/* never happens but let's not leave undefined */
1479a8ce8544SStanislaw Gruszka 			default:
14801d98fb12SJohannes Berg 			case IEEE80211_AC_BE:
14817ba10a8eSJohannes Berg 				qparam.cw_max = aCWmax;
14827ba10a8eSJohannes Berg 				qparam.cw_min = aCWmin;
1483aa837e1dSJohannes Berg 				qparam.txop = 0;
1484239281f8SRostislav Lisovy 				if (is_ocb)
1485239281f8SRostislav Lisovy 					qparam.aifs = 6;
1486239281f8SRostislav Lisovy 				else
1487aa837e1dSJohannes Berg 					qparam.aifs = 3;
1488aa837e1dSJohannes Berg 				break;
14891d98fb12SJohannes Berg 			case IEEE80211_AC_VI:
1490aa837e1dSJohannes Berg 				qparam.cw_max = aCWmin;
1491aa837e1dSJohannes Berg 				qparam.cw_min = (aCWmin + 1) / 2 - 1;
1492239281f8SRostislav Lisovy 				if (is_ocb)
1493239281f8SRostislav Lisovy 					qparam.txop = 0;
1494239281f8SRostislav Lisovy 				else if (use_11b)
1495aa837e1dSJohannes Berg 					qparam.txop = 6016/32;
1496aa837e1dSJohannes Berg 				else
1497aa837e1dSJohannes Berg 					qparam.txop = 3008/32;
1498239281f8SRostislav Lisovy 
1499239281f8SRostislav Lisovy 				if (is_ocb)
1500239281f8SRostislav Lisovy 					qparam.aifs = 3;
1501239281f8SRostislav Lisovy 				else
1502aa837e1dSJohannes Berg 					qparam.aifs = 2;
1503aa837e1dSJohannes Berg 				break;
15041d98fb12SJohannes Berg 			case IEEE80211_AC_VO:
1505aa837e1dSJohannes Berg 				qparam.cw_max = (aCWmin + 1) / 2 - 1;
1506aa837e1dSJohannes Berg 				qparam.cw_min = (aCWmin + 1) / 4 - 1;
1507239281f8SRostislav Lisovy 				if (is_ocb)
1508239281f8SRostislav Lisovy 					qparam.txop = 0;
1509239281f8SRostislav Lisovy 				else if (use_11b)
1510aa837e1dSJohannes Berg 					qparam.txop = 3264/32;
1511aa837e1dSJohannes Berg 				else
1512aa837e1dSJohannes Berg 					qparam.txop = 1504/32;
1513aa837e1dSJohannes Berg 				qparam.aifs = 2;
1514aa837e1dSJohannes Berg 				break;
1515aa837e1dSJohannes Berg 			}
1516a8ce8544SStanislaw Gruszka 		}
1517e552af05SHaim Dreyfuss 		ieee80211_regulatory_limit_wmm_params(sdata, &qparam, ac);
15185825fe10SJohannes Berg 
1519ab13315aSKalle Valo 		qparam.uapsd = false;
1520ab13315aSKalle Valo 
152154bcbc69SJohannes Berg 		sdata->tx_conf[ac] = qparam;
152254bcbc69SJohannes Berg 		drv_conf_tx(local, sdata, ac, &qparam);
1523aa837e1dSJohannes Berg 	}
1524e1b3ec1aSStanislaw Gruszka 
1525f142c6b9SJohannes Berg 	if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1526708d50edSAyala Beker 	    sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1527708d50edSAyala Beker 	    sdata->vif.type != NL80211_IFTYPE_NAN) {
1528a8ce8544SStanislaw Gruszka 		sdata->vif.bss_conf.qos = enable_qos;
15293abead59SJohannes Berg 		if (bss_notify)
15303abead59SJohannes Berg 			ieee80211_bss_info_change_notify(sdata,
15313abead59SJohannes Berg 							 BSS_CHANGED_QOS);
15325825fe10SJohannes Berg 	}
1533d9734979SSujith }
1534e50db65cSJohannes Berg 
153546900298SJohannes Berg void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1536700e8ea6SJouni Malinen 			 u16 transaction, u16 auth_alg, u16 status,
15374a3cb702SJohannes Berg 			 const u8 *extra, size_t extra_len, const u8 *da,
15381672c0e3SJohannes Berg 			 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
15391672c0e3SJohannes Berg 			 u32 tx_flags)
154046900298SJohannes Berg {
154146900298SJohannes Berg 	struct ieee80211_local *local = sdata->local;
154246900298SJohannes Berg 	struct sk_buff *skb;
154346900298SJohannes Berg 	struct ieee80211_mgmt *mgmt;
1544fffd0934SJohannes Berg 	int err;
154546900298SJohannes Berg 
154615e230abSFred Zhou 	/* 24 + 6 = header + auth_algo + auth_transaction + status_code */
1547744462a9SMax Stepanov 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
1548744462a9SMax Stepanov 			    24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN);
1549d15b8459SJoe Perches 	if (!skb)
155046900298SJohannes Berg 		return;
1551d15b8459SJoe Perches 
1552744462a9SMax Stepanov 	skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
155346900298SJohannes Berg 
1554b080db58SJohannes Berg 	mgmt = skb_put_zero(skb, 24 + 6);
155546900298SJohannes Berg 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
155646900298SJohannes Berg 					  IEEE80211_STYPE_AUTH);
1557efa6a09dSAntonio Quartulli 	memcpy(mgmt->da, da, ETH_ALEN);
155847846c9bSJohannes Berg 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
155946900298SJohannes Berg 	memcpy(mgmt->bssid, bssid, ETH_ALEN);
156046900298SJohannes Berg 	mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
156146900298SJohannes Berg 	mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
1562700e8ea6SJouni Malinen 	mgmt->u.auth.status_code = cpu_to_le16(status);
156346900298SJohannes Berg 	if (extra)
156459ae1d12SJohannes Berg 		skb_put_data(skb, extra, extra_len);
156546900298SJohannes Berg 
1566fffd0934SJohannes Berg 	if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1567fffd0934SJohannes Berg 		mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1568fffd0934SJohannes Berg 		err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1569fffd0934SJohannes Berg 		WARN_ON(err);
1570fffd0934SJohannes Berg 	}
1571fffd0934SJohannes Berg 
15721672c0e3SJohannes Berg 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
15731672c0e3SJohannes Berg 					tx_flags;
157462ae67beSJohannes Berg 	ieee80211_tx_skb(sdata, skb);
157546900298SJohannes Berg }
157646900298SJohannes Berg 
15776ae16775SAntonio Quartulli void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
15786ae16775SAntonio Quartulli 				    const u8 *bssid, u16 stype, u16 reason,
15796ae16775SAntonio Quartulli 				    bool send_frame, u8 *frame_buf)
15806ae16775SAntonio Quartulli {
15816ae16775SAntonio Quartulli 	struct ieee80211_local *local = sdata->local;
15826ae16775SAntonio Quartulli 	struct sk_buff *skb;
15836ae16775SAntonio Quartulli 	struct ieee80211_mgmt *mgmt = (void *)frame_buf;
15846ae16775SAntonio Quartulli 
15856ae16775SAntonio Quartulli 	/* build frame */
15866ae16775SAntonio Quartulli 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
15876ae16775SAntonio Quartulli 	mgmt->duration = 0; /* initialize only */
15886ae16775SAntonio Quartulli 	mgmt->seq_ctrl = 0; /* initialize only */
15896ae16775SAntonio Quartulli 	memcpy(mgmt->da, bssid, ETH_ALEN);
15906ae16775SAntonio Quartulli 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
15916ae16775SAntonio Quartulli 	memcpy(mgmt->bssid, bssid, ETH_ALEN);
15926ae16775SAntonio Quartulli 	/* u.deauth.reason_code == u.disassoc.reason_code */
15936ae16775SAntonio Quartulli 	mgmt->u.deauth.reason_code = cpu_to_le16(reason);
15946ae16775SAntonio Quartulli 
15956ae16775SAntonio Quartulli 	if (send_frame) {
15966ae16775SAntonio Quartulli 		skb = dev_alloc_skb(local->hw.extra_tx_headroom +
15976ae16775SAntonio Quartulli 				    IEEE80211_DEAUTH_FRAME_LEN);
15986ae16775SAntonio Quartulli 		if (!skb)
15996ae16775SAntonio Quartulli 			return;
16006ae16775SAntonio Quartulli 
16016ae16775SAntonio Quartulli 		skb_reserve(skb, local->hw.extra_tx_headroom);
16026ae16775SAntonio Quartulli 
16036ae16775SAntonio Quartulli 		/* copy in frame */
160459ae1d12SJohannes Berg 		skb_put_data(skb, mgmt, IEEE80211_DEAUTH_FRAME_LEN);
16056ae16775SAntonio Quartulli 
16066ae16775SAntonio Quartulli 		if (sdata->vif.type != NL80211_IFTYPE_STATION ||
16076ae16775SAntonio Quartulli 		    !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
16086ae16775SAntonio Quartulli 			IEEE80211_SKB_CB(skb)->flags |=
16096ae16775SAntonio Quartulli 				IEEE80211_TX_INTFL_DONT_ENCRYPT;
16106ae16775SAntonio Quartulli 
16116ae16775SAntonio Quartulli 		ieee80211_tx_skb(sdata, skb);
16126ae16775SAntonio Quartulli 	}
16136ae16775SAntonio Quartulli }
16146ae16775SAntonio Quartulli 
1615c56ef672SDavid Spinadel static int ieee80211_build_preq_ies_band(struct ieee80211_local *local,
1616c56ef672SDavid Spinadel 					 u8 *buffer, size_t buffer_len,
1617c56ef672SDavid Spinadel 					 const u8 *ie, size_t ie_len,
161857fbcce3SJohannes Berg 					 enum nl80211_band band,
1619c56ef672SDavid Spinadel 					 u32 rate_mask,
1620c56ef672SDavid Spinadel 					 struct cfg80211_chan_def *chandef,
162100387f32SJohannes Berg 					 size_t *offset, u32 flags)
1622de95a54bSJohannes Berg {
1623de95a54bSJohannes Berg 	struct ieee80211_supported_band *sband;
162441cbb0f5SLuca Coelho 	const struct ieee80211_sta_he_cap *he_cap;
1625c604b9f2SJohannes Berg 	u8 *pos = buffer, *end = buffer + buffer_len;
1626c56ef672SDavid Spinadel 	size_t noffset;
16278e664fb3SJohannes Berg 	int supp_rates_len, i;
16288dcb2003SJouni Malinen 	u8 rates[32];
16298dcb2003SJouni Malinen 	int num_rates;
16308dcb2003SJouni Malinen 	int ext_rates_len;
16312103dec1SSimon Wunderlich 	int shift;
16322103dec1SSimon Wunderlich 	u32 rate_flags;
163340a11ca8SJohannes Berg 	bool have_80mhz = false;
1634de95a54bSJohannes Berg 
1635c56ef672SDavid Spinadel 	*offset = 0;
1636c56ef672SDavid Spinadel 
16374d36ec58SJohannes Berg 	sband = local->hw.wiphy->bands[band];
1638d811b3d5SArik Nemtsov 	if (WARN_ON_ONCE(!sband))
1639d811b3d5SArik Nemtsov 		return 0;
1640de95a54bSJohannes Berg 
16412103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(chandef);
16422103dec1SSimon Wunderlich 	shift = ieee80211_chandef_get_shift(chandef);
16432103dec1SSimon Wunderlich 
16448dcb2003SJouni Malinen 	num_rates = 0;
16458dcb2003SJouni Malinen 	for (i = 0; i < sband->n_bitrates; i++) {
16468dcb2003SJouni Malinen 		if ((BIT(i) & rate_mask) == 0)
16478dcb2003SJouni Malinen 			continue; /* skip rate */
16482103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
16492103dec1SSimon Wunderlich 			continue;
16502103dec1SSimon Wunderlich 
16512103dec1SSimon Wunderlich 		rates[num_rates++] =
16522103dec1SSimon Wunderlich 			(u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
16532103dec1SSimon Wunderlich 					  (1 << shift) * 5);
16548dcb2003SJouni Malinen 	}
16558dcb2003SJouni Malinen 
16568dcb2003SJouni Malinen 	supp_rates_len = min_t(int, num_rates, 8);
16578e664fb3SJohannes Berg 
1658c604b9f2SJohannes Berg 	if (end - pos < 2 + supp_rates_len)
1659c604b9f2SJohannes Berg 		goto out_err;
1660de95a54bSJohannes Berg 	*pos++ = WLAN_EID_SUPP_RATES;
16618e664fb3SJohannes Berg 	*pos++ = supp_rates_len;
16628dcb2003SJouni Malinen 	memcpy(pos, rates, supp_rates_len);
16638dcb2003SJouni Malinen 	pos += supp_rates_len;
1664de95a54bSJohannes Berg 
16658e664fb3SJohannes Berg 	/* insert "request information" if in custom IEs */
16668e664fb3SJohannes Berg 	if (ie && ie_len) {
16678e664fb3SJohannes Berg 		static const u8 before_extrates[] = {
16688e664fb3SJohannes Berg 			WLAN_EID_SSID,
16698e664fb3SJohannes Berg 			WLAN_EID_SUPP_RATES,
16708e664fb3SJohannes Berg 			WLAN_EID_REQUEST,
16718e664fb3SJohannes Berg 		};
16728e664fb3SJohannes Berg 		noffset = ieee80211_ie_split(ie, ie_len,
16738e664fb3SJohannes Berg 					     before_extrates,
16748e664fb3SJohannes Berg 					     ARRAY_SIZE(before_extrates),
1675c56ef672SDavid Spinadel 					     *offset);
1676c56ef672SDavid Spinadel 		if (end - pos < noffset - *offset)
1677c604b9f2SJohannes Berg 			goto out_err;
1678c56ef672SDavid Spinadel 		memcpy(pos, ie + *offset, noffset - *offset);
1679c56ef672SDavid Spinadel 		pos += noffset - *offset;
1680c56ef672SDavid Spinadel 		*offset = noffset;
16818e664fb3SJohannes Berg 	}
16828e664fb3SJohannes Berg 
16838dcb2003SJouni Malinen 	ext_rates_len = num_rates - supp_rates_len;
16848dcb2003SJouni Malinen 	if (ext_rates_len > 0) {
1685c604b9f2SJohannes Berg 		if (end - pos < 2 + ext_rates_len)
1686c604b9f2SJohannes Berg 			goto out_err;
1687de95a54bSJohannes Berg 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
16888dcb2003SJouni Malinen 		*pos++ = ext_rates_len;
16898dcb2003SJouni Malinen 		memcpy(pos, rates + supp_rates_len, ext_rates_len);
16908dcb2003SJouni Malinen 		pos += ext_rates_len;
16918e664fb3SJohannes Berg 	}
16928e664fb3SJohannes Berg 
169357fbcce3SJohannes Berg 	if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
1694c604b9f2SJohannes Berg 		if (end - pos < 3)
1695c604b9f2SJohannes Berg 			goto out_err;
1696651b5225SJouni Malinen 		*pos++ = WLAN_EID_DS_PARAMS;
1697651b5225SJouni Malinen 		*pos++ = 1;
16982103dec1SSimon Wunderlich 		*pos++ = ieee80211_frequency_to_channel(
16992103dec1SSimon Wunderlich 				chandef->chan->center_freq);
1700651b5225SJouni Malinen 	}
1701651b5225SJouni Malinen 
1702b9771d41SJohannes Berg 	if (flags & IEEE80211_PROBE_FLAG_MIN_CONTENT)
1703b9771d41SJohannes Berg 		goto done;
1704b9771d41SJohannes Berg 
17058e664fb3SJohannes Berg 	/* insert custom IEs that go before HT */
17068e664fb3SJohannes Berg 	if (ie && ie_len) {
17078e664fb3SJohannes Berg 		static const u8 before_ht[] = {
1708a7f26d80SJohannes Berg 			/*
1709a7f26d80SJohannes Berg 			 * no need to list the ones split off already
1710a7f26d80SJohannes Berg 			 * (or generated here)
1711a7f26d80SJohannes Berg 			 */
17128e664fb3SJohannes Berg 			WLAN_EID_DS_PARAMS,
17138e664fb3SJohannes Berg 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
17148e664fb3SJohannes Berg 		};
17158e664fb3SJohannes Berg 		noffset = ieee80211_ie_split(ie, ie_len,
17168e664fb3SJohannes Berg 					     before_ht, ARRAY_SIZE(before_ht),
1717c56ef672SDavid Spinadel 					     *offset);
1718c56ef672SDavid Spinadel 		if (end - pos < noffset - *offset)
1719c604b9f2SJohannes Berg 			goto out_err;
1720c56ef672SDavid Spinadel 		memcpy(pos, ie + *offset, noffset - *offset);
1721c56ef672SDavid Spinadel 		pos += noffset - *offset;
1722c56ef672SDavid Spinadel 		*offset = noffset;
1723de95a54bSJohannes Berg 	}
1724de95a54bSJohannes Berg 
1725c604b9f2SJohannes Berg 	if (sband->ht_cap.ht_supported) {
1726c604b9f2SJohannes Berg 		if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
1727c604b9f2SJohannes Berg 			goto out_err;
1728ef96a842SBen Greear 		pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
1729ef96a842SBen Greear 						sband->ht_cap.cap);
1730c604b9f2SJohannes Berg 	}
17315ef2d41aSJohannes Berg 
17324d952300SJohannes Berg 	/* insert custom IEs that go before VHT */
17338e664fb3SJohannes Berg 	if (ie && ie_len) {
17344d952300SJohannes Berg 		static const u8 before_vht[] = {
1735a7f26d80SJohannes Berg 			/*
1736a7f26d80SJohannes Berg 			 * no need to list the ones split off already
1737a7f26d80SJohannes Berg 			 * (or generated here)
1738a7f26d80SJohannes Berg 			 */
17394d952300SJohannes Berg 			WLAN_EID_BSS_COEX_2040,
17404d952300SJohannes Berg 			WLAN_EID_EXT_CAPABILITY,
17414d952300SJohannes Berg 			WLAN_EID_SSID_LIST,
17424d952300SJohannes Berg 			WLAN_EID_CHANNEL_USAGE,
17434d952300SJohannes Berg 			WLAN_EID_INTERWORKING,
1744b44eebeaSLiad Kaufman 			WLAN_EID_MESH_ID,
1745a7f26d80SJohannes Berg 			/* 60 GHz (Multi-band, DMG, MMS) can't happen */
17464d952300SJohannes Berg 		};
17474d952300SJohannes Berg 		noffset = ieee80211_ie_split(ie, ie_len,
17484d952300SJohannes Berg 					     before_vht, ARRAY_SIZE(before_vht),
1749c56ef672SDavid Spinadel 					     *offset);
1750c56ef672SDavid Spinadel 		if (end - pos < noffset - *offset)
1751c604b9f2SJohannes Berg 			goto out_err;
1752c56ef672SDavid Spinadel 		memcpy(pos, ie + *offset, noffset - *offset);
1753c56ef672SDavid Spinadel 		pos += noffset - *offset;
1754c56ef672SDavid Spinadel 		*offset = noffset;
1755de95a54bSJohannes Berg 	}
1756de95a54bSJohannes Berg 
175740a11ca8SJohannes Berg 	/* Check if any channel in this sband supports at least 80 MHz */
175840a11ca8SJohannes Berg 	for (i = 0; i < sband->n_channels; i++) {
1759fa44b988SArik Nemtsov 		if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
1760fa44b988SArik Nemtsov 						IEEE80211_CHAN_NO_80MHZ))
1761fa44b988SArik Nemtsov 			continue;
1762fa44b988SArik Nemtsov 
176340a11ca8SJohannes Berg 		have_80mhz = true;
176440a11ca8SJohannes Berg 		break;
176540a11ca8SJohannes Berg 	}
176640a11ca8SJohannes Berg 
176740a11ca8SJohannes Berg 	if (sband->vht_cap.vht_supported && have_80mhz) {
1768c604b9f2SJohannes Berg 		if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
1769c604b9f2SJohannes Berg 			goto out_err;
1770ba0afa2fSMahesh Palivela 		pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
1771ba0afa2fSMahesh Palivela 						 sband->vht_cap.cap);
1772c604b9f2SJohannes Berg 	}
1773ba0afa2fSMahesh Palivela 
177441cbb0f5SLuca Coelho 	/* insert custom IEs that go before HE */
177541cbb0f5SLuca Coelho 	if (ie && ie_len) {
177641cbb0f5SLuca Coelho 		static const u8 before_he[] = {
177741cbb0f5SLuca Coelho 			/*
177841cbb0f5SLuca Coelho 			 * no need to list the ones split off before VHT
177941cbb0f5SLuca Coelho 			 * or generated here
178041cbb0f5SLuca Coelho 			 */
178141cbb0f5SLuca Coelho 			WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_REQ_PARAMS,
178241cbb0f5SLuca Coelho 			WLAN_EID_AP_CSN,
178341cbb0f5SLuca Coelho 			/* TODO: add 11ah/11aj/11ak elements */
178441cbb0f5SLuca Coelho 		};
178541cbb0f5SLuca Coelho 		noffset = ieee80211_ie_split(ie, ie_len,
178641cbb0f5SLuca Coelho 					     before_he, ARRAY_SIZE(before_he),
178741cbb0f5SLuca Coelho 					     *offset);
178841cbb0f5SLuca Coelho 		if (end - pos < noffset - *offset)
178941cbb0f5SLuca Coelho 			goto out_err;
179041cbb0f5SLuca Coelho 		memcpy(pos, ie + *offset, noffset - *offset);
179141cbb0f5SLuca Coelho 		pos += noffset - *offset;
179241cbb0f5SLuca Coelho 		*offset = noffset;
179341cbb0f5SLuca Coelho 	}
179441cbb0f5SLuca Coelho 
179541cbb0f5SLuca Coelho 	he_cap = ieee80211_get_he_sta_cap(sband);
179641cbb0f5SLuca Coelho 	if (he_cap) {
179741cbb0f5SLuca Coelho 		pos = ieee80211_ie_build_he_cap(pos, he_cap, end);
179841cbb0f5SLuca Coelho 		if (!pos)
179941cbb0f5SLuca Coelho 			goto out_err;
180041cbb0f5SLuca Coelho 	}
180141cbb0f5SLuca Coelho 
180241cbb0f5SLuca Coelho 	/*
180341cbb0f5SLuca Coelho 	 * If adding more here, adjust code in main.c
180441cbb0f5SLuca Coelho 	 * that calculates local->scan_ies_len.
180541cbb0f5SLuca Coelho 	 */
180641cbb0f5SLuca Coelho 
1807de95a54bSJohannes Berg 	return pos - buffer;
1808c604b9f2SJohannes Berg  out_err:
1809c604b9f2SJohannes Berg 	WARN_ONCE(1, "not enough space for preq IEs\n");
1810b9771d41SJohannes Berg  done:
1811c604b9f2SJohannes Berg 	return pos - buffer;
1812de95a54bSJohannes Berg }
1813de95a54bSJohannes Berg 
1814c56ef672SDavid Spinadel int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
1815c56ef672SDavid Spinadel 			     size_t buffer_len,
1816c56ef672SDavid Spinadel 			     struct ieee80211_scan_ies *ie_desc,
1817c56ef672SDavid Spinadel 			     const u8 *ie, size_t ie_len,
1818c56ef672SDavid Spinadel 			     u8 bands_used, u32 *rate_masks,
181900387f32SJohannes Berg 			     struct cfg80211_chan_def *chandef,
182000387f32SJohannes Berg 			     u32 flags)
1821c56ef672SDavid Spinadel {
1822c56ef672SDavid Spinadel 	size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
1823c56ef672SDavid Spinadel 	int i;
1824c56ef672SDavid Spinadel 
1825c56ef672SDavid Spinadel 	memset(ie_desc, 0, sizeof(*ie_desc));
1826c56ef672SDavid Spinadel 
182757fbcce3SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
1828c56ef672SDavid Spinadel 		if (bands_used & BIT(i)) {
1829c56ef672SDavid Spinadel 			pos += ieee80211_build_preq_ies_band(local,
1830c56ef672SDavid Spinadel 							     buffer + pos,
1831c56ef672SDavid Spinadel 							     buffer_len - pos,
1832c56ef672SDavid Spinadel 							     ie, ie_len, i,
1833c56ef672SDavid Spinadel 							     rate_masks[i],
1834c56ef672SDavid Spinadel 							     chandef,
183500387f32SJohannes Berg 							     &custom_ie_offset,
183600387f32SJohannes Berg 							     flags);
1837c56ef672SDavid Spinadel 			ie_desc->ies[i] = buffer + old_pos;
1838c56ef672SDavid Spinadel 			ie_desc->len[i] = pos - old_pos;
1839c56ef672SDavid Spinadel 			old_pos = pos;
1840c56ef672SDavid Spinadel 		}
1841c56ef672SDavid Spinadel 	}
1842c56ef672SDavid Spinadel 
1843c56ef672SDavid Spinadel 	/* add any remaining custom IEs */
1844c56ef672SDavid Spinadel 	if (ie && ie_len) {
1845c56ef672SDavid Spinadel 		if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
1846c56ef672SDavid Spinadel 			      "not enough space for preq custom IEs\n"))
1847c56ef672SDavid Spinadel 			return pos;
1848c56ef672SDavid Spinadel 		memcpy(buffer + pos, ie + custom_ie_offset,
1849c56ef672SDavid Spinadel 		       ie_len - custom_ie_offset);
1850c56ef672SDavid Spinadel 		ie_desc->common_ies = buffer + pos;
1851c56ef672SDavid Spinadel 		ie_desc->common_ie_len = ie_len - custom_ie_offset;
1852c56ef672SDavid Spinadel 		pos += ie_len - custom_ie_offset;
1853c56ef672SDavid Spinadel 	}
1854c56ef672SDavid Spinadel 
1855c56ef672SDavid Spinadel 	return pos;
1856c56ef672SDavid Spinadel };
1857c56ef672SDavid Spinadel 
1858a619a4c0SJuuso Oikarinen struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
1859a344d677SJohannes Berg 					  const u8 *src, const u8 *dst,
1860a344d677SJohannes Berg 					  u32 ratemask,
18616b77863bSJohannes Berg 					  struct ieee80211_channel *chan,
1862de95a54bSJohannes Berg 					  const u8 *ssid, size_t ssid_len,
1863a806c558SPaul Stewart 					  const u8 *ie, size_t ie_len,
186400387f32SJohannes Berg 					  u32 flags)
186546900298SJohannes Berg {
186646900298SJohannes Berg 	struct ieee80211_local *local = sdata->local;
18672103dec1SSimon Wunderlich 	struct cfg80211_chan_def chandef;
186846900298SJohannes Berg 	struct sk_buff *skb;
186946900298SJohannes Berg 	struct ieee80211_mgmt *mgmt;
1870b9a9ada1SJohannes Berg 	int ies_len;
187157fbcce3SJohannes Berg 	u32 rate_masks[NUM_NL80211_BANDS] = {};
1872c56ef672SDavid Spinadel 	struct ieee80211_scan_ies dummy_ie_desc;
187346900298SJohannes Berg 
1874a806c558SPaul Stewart 	/*
1875a806c558SPaul Stewart 	 * Do not send DS Channel parameter for directed probe requests
1876a806c558SPaul Stewart 	 * in order to maximize the chance that we get a response.  Some
1877a806c558SPaul Stewart 	 * badly-behaved APs don't respond when this parameter is included.
1878a806c558SPaul Stewart 	 */
18792103dec1SSimon Wunderlich 	chandef.width = sdata->vif.bss_conf.chandef.width;
188000387f32SJohannes Berg 	if (flags & IEEE80211_PROBE_FLAG_DIRECTED)
18812103dec1SSimon Wunderlich 		chandef.chan = NULL;
1882a806c558SPaul Stewart 	else
18832103dec1SSimon Wunderlich 		chandef.chan = chan;
1884651b5225SJouni Malinen 
1885a344d677SJohannes Berg 	skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
1886a344d677SJohannes Berg 				     100 + ie_len);
18875b2bbf75SJohannes Berg 	if (!skb)
1888b9a9ada1SJohannes Berg 		return NULL;
1889b9a9ada1SJohannes Berg 
1890c56ef672SDavid Spinadel 	rate_masks[chan->band] = ratemask;
1891b9a9ada1SJohannes Berg 	ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
1892c56ef672SDavid Spinadel 					   skb_tailroom(skb), &dummy_ie_desc,
1893c56ef672SDavid Spinadel 					   ie, ie_len, BIT(chan->band),
189400387f32SJohannes Berg 					   rate_masks, &chandef, flags);
1895b9a9ada1SJohannes Berg 	skb_put(skb, ies_len);
18967c12ce8bSKalle Valo 
189746900298SJohannes Berg 	if (dst) {
18987c12ce8bSKalle Valo 		mgmt = (struct ieee80211_mgmt *) skb->data;
189946900298SJohannes Berg 		memcpy(mgmt->da, dst, ETH_ALEN);
190046900298SJohannes Berg 		memcpy(mgmt->bssid, dst, ETH_ALEN);
190146900298SJohannes Berg 	}
190246900298SJohannes Berg 
190362ae67beSJohannes Berg 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
19045b2bbf75SJohannes Berg 
1905a619a4c0SJuuso Oikarinen 	return skb;
1906a619a4c0SJuuso Oikarinen }
1907a619a4c0SJuuso Oikarinen 
19082103dec1SSimon Wunderlich u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
190946900298SJohannes Berg 			    struct ieee802_11_elems *elems,
191057fbcce3SJohannes Berg 			    enum nl80211_band band, u32 *basic_rates)
191146900298SJohannes Berg {
191246900298SJohannes Berg 	struct ieee80211_supported_band *sband;
191346900298SJohannes Berg 	size_t num_rates;
19142103dec1SSimon Wunderlich 	u32 supp_rates, rate_flags;
19152103dec1SSimon Wunderlich 	int i, j, shift;
191621a8e9ddSMohammed Shafi Shajakhan 
19172103dec1SSimon Wunderlich 	sband = sdata->local->hw.wiphy->bands[band];
191821a8e9ddSMohammed Shafi Shajakhan 	if (WARN_ON(!sband))
191921a8e9ddSMohammed Shafi Shajakhan 		return 1;
19202103dec1SSimon Wunderlich 
19212103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
19222103dec1SSimon Wunderlich 	shift = ieee80211_vif_get_shift(&sdata->vif);
192346900298SJohannes Berg 
192446900298SJohannes Berg 	num_rates = sband->n_bitrates;
192546900298SJohannes Berg 	supp_rates = 0;
192646900298SJohannes Berg 	for (i = 0; i < elems->supp_rates_len +
192746900298SJohannes Berg 		     elems->ext_supp_rates_len; i++) {
192846900298SJohannes Berg 		u8 rate = 0;
192946900298SJohannes Berg 		int own_rate;
19309ebb61a2SAshok Nagarajan 		bool is_basic;
193146900298SJohannes Berg 		if (i < elems->supp_rates_len)
193246900298SJohannes Berg 			rate = elems->supp_rates[i];
193346900298SJohannes Berg 		else if (elems->ext_supp_rates)
193446900298SJohannes Berg 			rate = elems->ext_supp_rates
193546900298SJohannes Berg 				[i - elems->supp_rates_len];
193646900298SJohannes Berg 		own_rate = 5 * (rate & 0x7f);
19379ebb61a2SAshok Nagarajan 		is_basic = !!(rate & 0x80);
19389ebb61a2SAshok Nagarajan 
19399ebb61a2SAshok Nagarajan 		if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
19409ebb61a2SAshok Nagarajan 			continue;
19419ebb61a2SAshok Nagarajan 
19429ebb61a2SAshok Nagarajan 		for (j = 0; j < num_rates; j++) {
19432103dec1SSimon Wunderlich 			int brate;
19442103dec1SSimon Wunderlich 			if ((rate_flags & sband->bitrates[j].flags)
19452103dec1SSimon Wunderlich 			    != rate_flags)
19462103dec1SSimon Wunderlich 				continue;
19472103dec1SSimon Wunderlich 
19482103dec1SSimon Wunderlich 			brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
19492103dec1SSimon Wunderlich 					     1 << shift);
19502103dec1SSimon Wunderlich 
19512103dec1SSimon Wunderlich 			if (brate == own_rate) {
195246900298SJohannes Berg 				supp_rates |= BIT(j);
19539ebb61a2SAshok Nagarajan 				if (basic_rates && is_basic)
19549ebb61a2SAshok Nagarajan 					*basic_rates |= BIT(j);
19559ebb61a2SAshok Nagarajan 			}
19569ebb61a2SAshok Nagarajan 		}
195746900298SJohannes Berg 	}
195846900298SJohannes Berg 	return supp_rates;
195946900298SJohannes Berg }
1960f2753ddbSJohannes Berg 
196184f6a01cSJohannes Berg void ieee80211_stop_device(struct ieee80211_local *local)
196284f6a01cSJohannes Berg {
196384f6a01cSJohannes Berg 	ieee80211_led_radio(local, false);
196467408c8cSJohannes Berg 	ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
196584f6a01cSJohannes Berg 
196684f6a01cSJohannes Berg 	cancel_work_sync(&local->reconfig_filter);
196784f6a01cSJohannes Berg 
196884f6a01cSJohannes Berg 	flush_workqueue(local->workqueue);
1969678f415fSLennert Buytenhek 	drv_stop(local);
197084f6a01cSJohannes Berg }
197184f6a01cSJohannes Berg 
197274430f94SJohannes Berg static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
197374430f94SJohannes Berg 					   bool aborted)
197474430f94SJohannes Berg {
197574430f94SJohannes Berg 	/* It's possible that we don't handle the scan completion in
197674430f94SJohannes Berg 	 * time during suspend, so if it's still marked as completed
197774430f94SJohannes Berg 	 * here, queue the work and flush it to clean things up.
197874430f94SJohannes Berg 	 * Instead of calling the worker function directly here, we
197974430f94SJohannes Berg 	 * really queue it to avoid potential races with other flows
198074430f94SJohannes Berg 	 * scheduling the same work.
198174430f94SJohannes Berg 	 */
198274430f94SJohannes Berg 	if (test_bit(SCAN_COMPLETED, &local->scanning)) {
198374430f94SJohannes Berg 		/* If coming from reconfiguration failure, abort the scan so
198474430f94SJohannes Berg 		 * we don't attempt to continue a partial HW scan - which is
198574430f94SJohannes Berg 		 * possible otherwise if (e.g.) the 2.4 GHz portion was the
198674430f94SJohannes Berg 		 * completed scan, and a 5 GHz portion is still pending.
198774430f94SJohannes Berg 		 */
198874430f94SJohannes Berg 		if (aborted)
198974430f94SJohannes Berg 			set_bit(SCAN_ABORTED, &local->scanning);
199074430f94SJohannes Berg 		ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
199174430f94SJohannes Berg 		flush_delayed_work(&local->scan_work);
199274430f94SJohannes Berg 	}
199374430f94SJohannes Berg }
199474430f94SJohannes Berg 
1995f6837ba8SJohannes Berg static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
1996f6837ba8SJohannes Berg {
1997f6837ba8SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
1998f6837ba8SJohannes Berg 	struct ieee80211_chanctx *ctx;
1999f6837ba8SJohannes Berg 
2000f6837ba8SJohannes Berg 	/*
2001f6837ba8SJohannes Berg 	 * We get here if during resume the device can't be restarted properly.
2002f6837ba8SJohannes Berg 	 * We might also get here if this happens during HW reset, which is a
2003f6837ba8SJohannes Berg 	 * slightly different situation and we need to drop all connections in
2004f6837ba8SJohannes Berg 	 * the latter case.
2005f6837ba8SJohannes Berg 	 *
2006f6837ba8SJohannes Berg 	 * Ask cfg80211 to turn off all interfaces, this will result in more
2007f6837ba8SJohannes Berg 	 * warnings but at least we'll then get into a clean stopped state.
2008f6837ba8SJohannes Berg 	 */
2009f6837ba8SJohannes Berg 
2010f6837ba8SJohannes Berg 	local->resuming = false;
2011f6837ba8SJohannes Berg 	local->suspended = false;
20127584f88fSEliad Peller 	local->in_reconfig = false;
2013f6837ba8SJohannes Berg 
201474430f94SJohannes Berg 	ieee80211_flush_completed_scan(local, true);
201574430f94SJohannes Berg 
2016f6837ba8SJohannes Berg 	/* scheduled scan clearly can't be running any more, but tell
2017f6837ba8SJohannes Berg 	 * cfg80211 and clear local state
2018f6837ba8SJohannes Berg 	 */
2019f6837ba8SJohannes Berg 	ieee80211_sched_scan_end(local);
2020f6837ba8SJohannes Berg 
2021f6837ba8SJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list)
2022f6837ba8SJohannes Berg 		sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
2023f6837ba8SJohannes Berg 
2024f6837ba8SJohannes Berg 	/* Mark channel contexts as not being in the driver any more to avoid
2025f6837ba8SJohannes Berg 	 * removing them from the driver during the shutdown process...
2026f6837ba8SJohannes Berg 	 */
2027f6837ba8SJohannes Berg 	mutex_lock(&local->chanctx_mtx);
2028f6837ba8SJohannes Berg 	list_for_each_entry(ctx, &local->chanctx_list, list)
2029f6837ba8SJohannes Berg 		ctx->driver_present = false;
2030f6837ba8SJohannes Berg 	mutex_unlock(&local->chanctx_mtx);
2031f6837ba8SJohannes Berg 
2032f6837ba8SJohannes Berg 	cfg80211_shutdown_all_interfaces(local->hw.wiphy);
2033f6837ba8SJohannes Berg }
2034f6837ba8SJohannes Berg 
2035153a5fc4SStanislaw Gruszka static void ieee80211_assign_chanctx(struct ieee80211_local *local,
2036153a5fc4SStanislaw Gruszka 				     struct ieee80211_sub_if_data *sdata)
2037153a5fc4SStanislaw Gruszka {
2038153a5fc4SStanislaw Gruszka 	struct ieee80211_chanctx_conf *conf;
2039153a5fc4SStanislaw Gruszka 	struct ieee80211_chanctx *ctx;
2040153a5fc4SStanislaw Gruszka 
2041153a5fc4SStanislaw Gruszka 	if (!local->use_chanctx)
2042153a5fc4SStanislaw Gruszka 		return;
2043153a5fc4SStanislaw Gruszka 
2044153a5fc4SStanislaw Gruszka 	mutex_lock(&local->chanctx_mtx);
2045153a5fc4SStanislaw Gruszka 	conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
2046153a5fc4SStanislaw Gruszka 					 lockdep_is_held(&local->chanctx_mtx));
2047153a5fc4SStanislaw Gruszka 	if (conf) {
2048153a5fc4SStanislaw Gruszka 		ctx = container_of(conf, struct ieee80211_chanctx, conf);
2049153a5fc4SStanislaw Gruszka 		drv_assign_vif_chanctx(local, sdata, ctx);
2050153a5fc4SStanislaw Gruszka 	}
2051153a5fc4SStanislaw Gruszka 	mutex_unlock(&local->chanctx_mtx);
2052153a5fc4SStanislaw Gruszka }
2053153a5fc4SStanislaw Gruszka 
20541ea2c864SJohannes Berg static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
20551ea2c864SJohannes Berg {
20561ea2c864SJohannes Berg 	struct ieee80211_local *local = sdata->local;
20571ea2c864SJohannes Berg 	struct sta_info *sta;
20581ea2c864SJohannes Berg 
20591ea2c864SJohannes Berg 	/* add STAs back */
20601ea2c864SJohannes Berg 	mutex_lock(&local->sta_mtx);
20611ea2c864SJohannes Berg 	list_for_each_entry(sta, &local->sta_list, list) {
20621ea2c864SJohannes Berg 		enum ieee80211_sta_state state;
20631ea2c864SJohannes Berg 
20641ea2c864SJohannes Berg 		if (!sta->uploaded || sta->sdata != sdata)
20651ea2c864SJohannes Berg 			continue;
20661ea2c864SJohannes Berg 
20671ea2c864SJohannes Berg 		for (state = IEEE80211_STA_NOTEXIST;
20681ea2c864SJohannes Berg 		     state < sta->sta_state; state++)
20691ea2c864SJohannes Berg 			WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
20701ea2c864SJohannes Berg 					      state + 1));
20711ea2c864SJohannes Berg 	}
20721ea2c864SJohannes Berg 	mutex_unlock(&local->sta_mtx);
20731ea2c864SJohannes Berg }
20741ea2c864SJohannes Berg 
2075167e33f4SAyala Beker static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
2076167e33f4SAyala Beker {
2077167e33f4SAyala Beker 	struct cfg80211_nan_func *func, **funcs;
2078167e33f4SAyala Beker 	int res, id, i = 0;
2079167e33f4SAyala Beker 
2080167e33f4SAyala Beker 	res = drv_start_nan(sdata->local, sdata,
2081167e33f4SAyala Beker 			    &sdata->u.nan.conf);
2082167e33f4SAyala Beker 	if (WARN_ON(res))
2083167e33f4SAyala Beker 		return res;
2084167e33f4SAyala Beker 
20856396bb22SKees Cook 	funcs = kcalloc(sdata->local->hw.max_nan_de_entries + 1,
20866396bb22SKees Cook 			sizeof(*funcs),
20876396bb22SKees Cook 			GFP_KERNEL);
2088167e33f4SAyala Beker 	if (!funcs)
2089167e33f4SAyala Beker 		return -ENOMEM;
2090167e33f4SAyala Beker 
2091167e33f4SAyala Beker 	/* Add all the functions:
2092167e33f4SAyala Beker 	 * This is a little bit ugly. We need to call a potentially sleeping
2093167e33f4SAyala Beker 	 * callback for each NAN function, so we can't hold the spinlock.
2094167e33f4SAyala Beker 	 */
2095167e33f4SAyala Beker 	spin_lock_bh(&sdata->u.nan.func_lock);
2096167e33f4SAyala Beker 
2097167e33f4SAyala Beker 	idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
2098167e33f4SAyala Beker 		funcs[i++] = func;
2099167e33f4SAyala Beker 
2100167e33f4SAyala Beker 	spin_unlock_bh(&sdata->u.nan.func_lock);
2101167e33f4SAyala Beker 
2102167e33f4SAyala Beker 	for (i = 0; funcs[i]; i++) {
2103167e33f4SAyala Beker 		res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
2104167e33f4SAyala Beker 		if (WARN_ON(res))
2105167e33f4SAyala Beker 			ieee80211_nan_func_terminated(&sdata->vif,
2106167e33f4SAyala Beker 						      funcs[i]->instance_id,
2107167e33f4SAyala Beker 						      NL80211_NAN_FUNC_TERM_REASON_ERROR,
2108167e33f4SAyala Beker 						      GFP_KERNEL);
2109167e33f4SAyala Beker 	}
2110167e33f4SAyala Beker 
2111167e33f4SAyala Beker 	kfree(funcs);
2112167e33f4SAyala Beker 
2113167e33f4SAyala Beker 	return 0;
2114167e33f4SAyala Beker }
2115167e33f4SAyala Beker 
2116f2753ddbSJohannes Berg int ieee80211_reconfig(struct ieee80211_local *local)
2117f2753ddbSJohannes Berg {
2118f2753ddbSJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
2119f2753ddbSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
212055de908aSJohannes Berg 	struct ieee80211_chanctx *ctx;
2121f2753ddbSJohannes Berg 	struct sta_info *sta;
21222683d65bSEliad Peller 	int res, i;
2123d888130aSJohannes Berg 	bool reconfig_due_to_wowlan = false;
2124d43c6b6eSDavid Spinadel 	struct ieee80211_sub_if_data *sched_scan_sdata;
21256ea0a69cSJohannes Berg 	struct cfg80211_sched_scan_request *sched_scan_req;
2126d43c6b6eSDavid Spinadel 	bool sched_scan_stopped = false;
2127b0485e9fSEliad Peller 	bool suspended = local->suspended;
2128d888130aSJohannes Berg 
21290f8b8245SEliad Peller 	/* nothing to do if HW shouldn't run */
21300f8b8245SEliad Peller 	if (!local->open_count)
21310f8b8245SEliad Peller 		goto wake_up;
21320f8b8245SEliad Peller 
21338f21b0adSJohannes Berg #ifdef CONFIG_PM
2134b0485e9fSEliad Peller 	if (suspended)
2135ceb99fe0SJohannes Berg 		local->resuming = true;
2136f2753ddbSJohannes Berg 
2137eecc4800SJohannes Berg 	if (local->wowlan) {
2138b0485e9fSEliad Peller 		/*
2139b0485e9fSEliad Peller 		 * In the wowlan case, both mac80211 and the device
2140b0485e9fSEliad Peller 		 * are functional when the resume op is called, so
2141b0485e9fSEliad Peller 		 * clear local->suspended so the device could operate
2142b0485e9fSEliad Peller 		 * normally (e.g. pass rx frames).
2143b0485e9fSEliad Peller 		 */
2144b0485e9fSEliad Peller 		local->suspended = false;
2145eecc4800SJohannes Berg 		res = drv_resume(local);
214627b3eb9cSJohannes Berg 		local->wowlan = false;
2147eecc4800SJohannes Berg 		if (res < 0) {
2148eecc4800SJohannes Berg 			local->resuming = false;
2149eecc4800SJohannes Berg 			return res;
2150eecc4800SJohannes Berg 		}
2151eecc4800SJohannes Berg 		if (res == 0)
2152eecc4800SJohannes Berg 			goto wake_up;
2153eecc4800SJohannes Berg 		WARN_ON(res > 1);
2154eecc4800SJohannes Berg 		/*
2155eecc4800SJohannes Berg 		 * res is 1, which means the driver requested
2156eecc4800SJohannes Berg 		 * to go through a regular reset on wakeup.
2157b0485e9fSEliad Peller 		 * restore local->suspended in this case.
2158eecc4800SJohannes Berg 		 */
2159d888130aSJohannes Berg 		reconfig_due_to_wowlan = true;
2160b0485e9fSEliad Peller 		local->suspended = true;
2161eecc4800SJohannes Berg 	}
2162eecc4800SJohannes Berg #endif
216394f9b97bSJohannes Berg 
216424feda00SLuis R. Rodriguez 	/*
216543d6df00SEliad Peller 	 * In case of hw_restart during suspend (without wowlan),
216643d6df00SEliad Peller 	 * cancel restart work, as we are reconfiguring the device
216743d6df00SEliad Peller 	 * anyway.
216843d6df00SEliad Peller 	 * Note that restart_work is scheduled on a frozen workqueue,
216943d6df00SEliad Peller 	 * so we can't deadlock in this case.
217043d6df00SEliad Peller 	 */
217143d6df00SEliad Peller 	if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
217243d6df00SEliad Peller 		cancel_work_sync(&local->restart_work);
217343d6df00SEliad Peller 
2174968a76ceSEliad Peller 	local->started = false;
2175968a76ceSEliad Peller 
217643d6df00SEliad Peller 	/*
217724feda00SLuis R. Rodriguez 	 * Upon resume hardware can sometimes be goofy due to
217824feda00SLuis R. Rodriguez 	 * various platform / driver / bus issues, so restarting
217924feda00SLuis R. Rodriguez 	 * the device may at times not work immediately. Propagate
218024feda00SLuis R. Rodriguez 	 * the error.
218124feda00SLuis R. Rodriguez 	 */
218224487981SJohannes Berg 	res = drv_start(local);
218324feda00SLuis R. Rodriguez 	if (res) {
2184b0485e9fSEliad Peller 		if (suspended)
2185f6837ba8SJohannes Berg 			WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
2186f6837ba8SJohannes Berg 		else
2187f6837ba8SJohannes Berg 			WARN(1, "Hardware became unavailable during restart.\n");
2188f6837ba8SJohannes Berg 		ieee80211_handle_reconfig_failure(local);
218924feda00SLuis R. Rodriguez 		return res;
219024feda00SLuis R. Rodriguez 	}
2191f2753ddbSJohannes Berg 
21927f281975SYogesh Ashok Powar 	/* setup fragmentation threshold */
21937f281975SYogesh Ashok Powar 	drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
21947f281975SYogesh Ashok Powar 
21957f281975SYogesh Ashok Powar 	/* setup RTS threshold */
21967f281975SYogesh Ashok Powar 	drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
21977f281975SYogesh Ashok Powar 
21987f281975SYogesh Ashok Powar 	/* reset coverage class */
21997f281975SYogesh Ashok Powar 	drv_set_coverage_class(local, hw->wiphy->coverage_class);
22007f281975SYogesh Ashok Powar 
22011f87f7d3SJohannes Berg 	ieee80211_led_radio(local, true);
220267408c8cSJohannes Berg 	ieee80211_mod_tpt_led_trig(local,
220367408c8cSJohannes Berg 				   IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
2204f2753ddbSJohannes Berg 
2205f2753ddbSJohannes Berg 	/* add interfaces */
22064b6f1dd6SJohannes Berg 	sdata = rtnl_dereference(local->monitor_sdata);
22074b6f1dd6SJohannes Berg 	if (sdata) {
22083c3e21e7SJohannes Berg 		/* in HW restart it exists already */
22093c3e21e7SJohannes Berg 		WARN_ON(local->resuming);
22104b6f1dd6SJohannes Berg 		res = drv_add_interface(local, sdata);
22114b6f1dd6SJohannes Berg 		if (WARN_ON(res)) {
22120c2bef46SMonam Agarwal 			RCU_INIT_POINTER(local->monitor_sdata, NULL);
22134b6f1dd6SJohannes Berg 			synchronize_net();
22144b6f1dd6SJohannes Berg 			kfree(sdata);
22154b6f1dd6SJohannes Berg 		}
22164b6f1dd6SJohannes Berg 	}
22174b6f1dd6SJohannes Berg 
2218f2753ddbSJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
2219f2753ddbSJohannes Berg 		if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2220f2753ddbSJohannes Berg 		    sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2221c8fff3dcSLuciano Coelho 		    ieee80211_sdata_running(sdata)) {
22227b7eab6fSJohannes Berg 			res = drv_add_interface(local, sdata);
2223c8fff3dcSLuciano Coelho 			if (WARN_ON(res))
2224c8fff3dcSLuciano Coelho 				break;
2225c8fff3dcSLuciano Coelho 		}
2226c8fff3dcSLuciano Coelho 	}
2227c8fff3dcSLuciano Coelho 
2228c8fff3dcSLuciano Coelho 	/* If adding any of the interfaces failed above, roll back and
2229c8fff3dcSLuciano Coelho 	 * report failure.
2230c8fff3dcSLuciano Coelho 	 */
2231c8fff3dcSLuciano Coelho 	if (res) {
2232c8fff3dcSLuciano Coelho 		list_for_each_entry_continue_reverse(sdata, &local->interfaces,
2233c8fff3dcSLuciano Coelho 						     list)
2234c8fff3dcSLuciano Coelho 			if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2235c8fff3dcSLuciano Coelho 			    sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2236c8fff3dcSLuciano Coelho 			    ieee80211_sdata_running(sdata))
2237c8fff3dcSLuciano Coelho 				drv_remove_interface(local, sdata);
2238c8fff3dcSLuciano Coelho 		ieee80211_handle_reconfig_failure(local);
2239c8fff3dcSLuciano Coelho 		return res;
2240f2753ddbSJohannes Berg 	}
2241f2753ddbSJohannes Berg 
224255de908aSJohannes Berg 	/* add channel contexts */
2243f0dea9c7SArend van Spriel 	if (local->use_chanctx) {
224455de908aSJohannes Berg 		mutex_lock(&local->chanctx_mtx);
224555de908aSJohannes Berg 		list_for_each_entry(ctx, &local->chanctx_list, list)
22465bcae31dSMichal Kazior 			if (ctx->replace_state !=
22475bcae31dSMichal Kazior 			    IEEE80211_CHANCTX_REPLACES_OTHER)
224855de908aSJohannes Berg 				WARN_ON(drv_add_chanctx(local, ctx));
224955de908aSJohannes Berg 		mutex_unlock(&local->chanctx_mtx);
225055de908aSJohannes Berg 
2251fe5f2559SJohannes Berg 		sdata = rtnl_dereference(local->monitor_sdata);
2252153a5fc4SStanislaw Gruszka 		if (sdata && ieee80211_sdata_running(sdata))
2253153a5fc4SStanislaw Gruszka 			ieee80211_assign_chanctx(local, sdata);
22547df180f7SZhao, Gang 	}
2255fe5f2559SJohannes Berg 
2256f2753ddbSJohannes Berg 	/* reconfigure hardware */
2257f2753ddbSJohannes Berg 	ieee80211_hw_config(local, ~0);
2258f2753ddbSJohannes Berg 
2259f2753ddbSJohannes Berg 	ieee80211_configure_filter(local);
2260f2753ddbSJohannes Berg 
2261f2753ddbSJohannes Berg 	/* Finally also reconfigure all the BSS information */
2262f2753ddbSJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
2263ac8dd506SJohannes Berg 		u32 changed;
2264ac8dd506SJohannes Berg 
22659607e6b6SJohannes Berg 		if (!ieee80211_sdata_running(sdata))
2266f2753ddbSJohannes Berg 			continue;
2267ac8dd506SJohannes Berg 
22681ea2c864SJohannes Berg 		ieee80211_assign_chanctx(local, sdata);
22691ea2c864SJohannes Berg 
22701ea2c864SJohannes Berg 		switch (sdata->vif.type) {
22711ea2c864SJohannes Berg 		case NL80211_IFTYPE_AP_VLAN:
22721ea2c864SJohannes Berg 		case NL80211_IFTYPE_MONITOR:
22731ea2c864SJohannes Berg 			break;
22744926b51bSJohannes Berg 		case NL80211_IFTYPE_ADHOC:
22754926b51bSJohannes Berg 			if (sdata->vif.bss_conf.ibss_joined)
22764926b51bSJohannes Berg 				WARN_ON(drv_join_ibss(local, sdata));
22774926b51bSJohannes Berg 			/* fall through */
22781ea2c864SJohannes Berg 		default:
22791ea2c864SJohannes Berg 			ieee80211_reconfig_stations(sdata);
22801ea2c864SJohannes Berg 			/* fall through */
22811ea2c864SJohannes Berg 		case NL80211_IFTYPE_AP: /* AP stations are handled later */
22821ea2c864SJohannes Berg 			for (i = 0; i < IEEE80211_NUM_ACS; i++)
22831ea2c864SJohannes Berg 				drv_conf_tx(local, sdata, i,
22841ea2c864SJohannes Berg 					    &sdata->tx_conf[i]);
22851ea2c864SJohannes Berg 			break;
22861ea2c864SJohannes Berg 		}
22871ea2c864SJohannes Berg 
2288ac8dd506SJohannes Berg 		/* common change flags for all interface types */
2289ac8dd506SJohannes Berg 		changed = BSS_CHANGED_ERP_CTS_PROT |
2290ac8dd506SJohannes Berg 			  BSS_CHANGED_ERP_PREAMBLE |
2291ac8dd506SJohannes Berg 			  BSS_CHANGED_ERP_SLOT |
2292ac8dd506SJohannes Berg 			  BSS_CHANGED_HT |
2293ac8dd506SJohannes Berg 			  BSS_CHANGED_BASIC_RATES |
2294ac8dd506SJohannes Berg 			  BSS_CHANGED_BEACON_INT |
2295ac8dd506SJohannes Berg 			  BSS_CHANGED_BSSID |
22964ced3f74SJohannes Berg 			  BSS_CHANGED_CQM |
229755de47f6SEliad Peller 			  BSS_CHANGED_QOS |
22981ea6f9c0SJohannes Berg 			  BSS_CHANGED_IDLE |
2299dcbe73caSPradeep Kumar Chitrapu 			  BSS_CHANGED_TXPOWER |
2300dcbe73caSPradeep Kumar Chitrapu 			  BSS_CHANGED_MCAST_RATE;
2301ac8dd506SJohannes Berg 
2302b5a33d52SSara Sharon 		if (sdata->vif.mu_mimo_owner)
230323a1f8d4SSara Sharon 			changed |= BSS_CHANGED_MU_GROUPS;
230423a1f8d4SSara Sharon 
2305f2753ddbSJohannes Berg 		switch (sdata->vif.type) {
2306f2753ddbSJohannes Berg 		case NL80211_IFTYPE_STATION:
23070d392e93SEliad Peller 			changed |= BSS_CHANGED_ASSOC |
2308ab095877SEliad Peller 				   BSS_CHANGED_ARP_FILTER |
2309ab095877SEliad Peller 				   BSS_CHANGED_PS;
2310c65dd147SEmmanuel Grumbach 
2311989c6505SAlexander Bondar 			/* Re-send beacon info report to the driver */
2312989c6505SAlexander Bondar 			if (sdata->u.mgd.have_beacon)
2313989c6505SAlexander Bondar 				changed |= BSS_CHANGED_BEACON_INFO;
2314c65dd147SEmmanuel Grumbach 
2315e38a017bSAvraham Stern 			if (sdata->vif.bss_conf.max_idle_period ||
2316e38a017bSAvraham Stern 			    sdata->vif.bss_conf.protected_keep_alive)
2317e38a017bSAvraham Stern 				changed |= BSS_CHANGED_KEEP_ALIVE;
2318e38a017bSAvraham Stern 
23198d61ffa5SJohannes Berg 			sdata_lock(sdata);
2320ac8dd506SJohannes Berg 			ieee80211_bss_info_change_notify(sdata, changed);
23218d61ffa5SJohannes Berg 			sdata_unlock(sdata);
2322ac8dd506SJohannes Berg 			break;
23236e0bd6c3SRostislav Lisovy 		case NL80211_IFTYPE_OCB:
2324239281f8SRostislav Lisovy 			changed |= BSS_CHANGED_OCB;
2325239281f8SRostislav Lisovy 			ieee80211_bss_info_change_notify(sdata, changed);
23266e0bd6c3SRostislav Lisovy 			break;
2327f2753ddbSJohannes Berg 		case NL80211_IFTYPE_ADHOC:
2328ac8dd506SJohannes Berg 			changed |= BSS_CHANGED_IBSS;
2329ac8dd506SJohannes Berg 			/* fall through */
2330f2753ddbSJohannes Berg 		case NL80211_IFTYPE_AP:
2331339afbf4SJohannes Berg 			changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
2332e7979ac7SArik Nemtsov 
2333bc847970SPradeep Kumar Chitrapu 			if (sdata->vif.bss_conf.ftm_responder == 1 &&
2334bc847970SPradeep Kumar Chitrapu 			    wiphy_ext_feature_isset(sdata->local->hw.wiphy,
2335bc847970SPradeep Kumar Chitrapu 					NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
2336bc847970SPradeep Kumar Chitrapu 				changed |= BSS_CHANGED_FTM_RESPONDER;
2337bc847970SPradeep Kumar Chitrapu 
23381041638fSJohannes Berg 			if (sdata->vif.type == NL80211_IFTYPE_AP) {
2339e7979ac7SArik Nemtsov 				changed |= BSS_CHANGED_AP_PROBE_RESP;
2340e7979ac7SArik Nemtsov 
23411041638fSJohannes Berg 				if (rcu_access_pointer(sdata->u.ap.beacon))
23421041638fSJohannes Berg 					drv_start_ap(local, sdata);
23431041638fSJohannes Berg 			}
23441041638fSJohannes Berg 
23457827493bSArik Nemtsov 			/* fall through */
2346f2753ddbSJohannes Berg 		case NL80211_IFTYPE_MESH_POINT:
23478da34932SJohannes Berg 			if (sdata->vif.bss_conf.enable_beacon) {
2348ac8dd506SJohannes Berg 				changed |= BSS_CHANGED_BEACON |
2349ac8dd506SJohannes Berg 					   BSS_CHANGED_BEACON_ENABLED;
23502d0ddec5SJohannes Berg 				ieee80211_bss_info_change_notify(sdata, changed);
23518da34932SJohannes Berg 			}
2352f2753ddbSJohannes Berg 			break;
2353167e33f4SAyala Beker 		case NL80211_IFTYPE_NAN:
2354167e33f4SAyala Beker 			res = ieee80211_reconfig_nan(sdata);
2355167e33f4SAyala Beker 			if (res < 0) {
2356167e33f4SAyala Beker 				ieee80211_handle_reconfig_failure(local);
2357167e33f4SAyala Beker 				return res;
2358167e33f4SAyala Beker 			}
2359167e33f4SAyala Beker 			break;
2360f2753ddbSJohannes Berg 		case NL80211_IFTYPE_WDS:
2361f2753ddbSJohannes Berg 		case NL80211_IFTYPE_AP_VLAN:
2362f2753ddbSJohannes Berg 		case NL80211_IFTYPE_MONITOR:
236398104fdeSJohannes Berg 		case NL80211_IFTYPE_P2P_DEVICE:
2364b205786eSZhao, Gang 			/* nothing to do */
2365f142c6b9SJohannes Berg 			break;
2366f2753ddbSJohannes Berg 		case NL80211_IFTYPE_UNSPECIFIED:
23672e161f78SJohannes Berg 		case NUM_NL80211_IFTYPES:
23682ca27bcfSJohannes Berg 		case NL80211_IFTYPE_P2P_CLIENT:
23692ca27bcfSJohannes Berg 		case NL80211_IFTYPE_P2P_GO:
2370f2753ddbSJohannes Berg 			WARN_ON(1);
2371f2753ddbSJohannes Berg 			break;
2372f2753ddbSJohannes Berg 		}
2373f2753ddbSJohannes Berg 	}
2374f2753ddbSJohannes Berg 
23754a733ef1SJohannes Berg 	ieee80211_recalc_ps(local);
23768e1b23b9SEyal Shapira 
23772a419056SJohannes Berg 	/*
23786e1b1b24SEliad Peller 	 * The sta might be in psm against the ap (e.g. because
23796e1b1b24SEliad Peller 	 * this was the state before a hw restart), so we
23806e1b1b24SEliad Peller 	 * explicitly send a null packet in order to make sure
23816e1b1b24SEliad Peller 	 * it'll sync against the ap (and get out of psm).
23826e1b1b24SEliad Peller 	 */
23836e1b1b24SEliad Peller 	if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
23846e1b1b24SEliad Peller 		list_for_each_entry(sdata, &local->interfaces, list) {
23856e1b1b24SEliad Peller 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
23866e1b1b24SEliad Peller 				continue;
238720f544eeSJohannes Berg 			if (!sdata->u.mgd.associated)
238820f544eeSJohannes Berg 				continue;
23896e1b1b24SEliad Peller 
2390076cdcb1SJohannes Berg 			ieee80211_send_nullfunc(local, sdata, false);
23916e1b1b24SEliad Peller 		}
23926e1b1b24SEliad Peller 	}
23936e1b1b24SEliad Peller 
23942e8d397eSArik Nemtsov 	/* APs are now beaconing, add back stations */
23952e8d397eSArik Nemtsov 	mutex_lock(&local->sta_mtx);
23962e8d397eSArik Nemtsov 	list_for_each_entry(sta, &local->sta_list, list) {
23972e8d397eSArik Nemtsov 		enum ieee80211_sta_state state;
23982e8d397eSArik Nemtsov 
23992e8d397eSArik Nemtsov 		if (!sta->uploaded)
24002e8d397eSArik Nemtsov 			continue;
24012e8d397eSArik Nemtsov 
240219103a4bSmpubbise@codeaurora.org 		if (sta->sdata->vif.type != NL80211_IFTYPE_AP &&
240319103a4bSmpubbise@codeaurora.org 		    sta->sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
24042e8d397eSArik Nemtsov 			continue;
24052e8d397eSArik Nemtsov 
24062e8d397eSArik Nemtsov 		for (state = IEEE80211_STA_NOTEXIST;
24072e8d397eSArik Nemtsov 		     state < sta->sta_state; state++)
24082e8d397eSArik Nemtsov 			WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
24092e8d397eSArik Nemtsov 					      state + 1));
24102e8d397eSArik Nemtsov 	}
24112e8d397eSArik Nemtsov 	mutex_unlock(&local->sta_mtx);
24122e8d397eSArik Nemtsov 
24137b21aea0SEyal Shapira 	/* add back keys */
24147b21aea0SEyal Shapira 	list_for_each_entry(sdata, &local->interfaces, list)
2415f9dca80bSMichal Kazior 		ieee80211_reset_crypto_tx_tailroom(sdata);
2416f9dca80bSMichal Kazior 
2417f9dca80bSMichal Kazior 	list_for_each_entry(sdata, &local->interfaces, list)
24187b21aea0SEyal Shapira 		if (ieee80211_sdata_running(sdata))
24197b21aea0SEyal Shapira 			ieee80211_enable_keys(sdata);
24207b21aea0SEyal Shapira 
24210d440ea2SEliad Peller 	/* Reconfigure sched scan if it was interrupted by FW restart */
24220d440ea2SEliad Peller 	mutex_lock(&local->mtx);
24230d440ea2SEliad Peller 	sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
24240d440ea2SEliad Peller 						lockdep_is_held(&local->mtx));
24250d440ea2SEliad Peller 	sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
24260d440ea2SEliad Peller 						lockdep_is_held(&local->mtx));
24270d440ea2SEliad Peller 	if (sched_scan_sdata && sched_scan_req)
24280d440ea2SEliad Peller 		/*
24290d440ea2SEliad Peller 		 * Sched scan stopped, but we don't want to report it. Instead,
24300d440ea2SEliad Peller 		 * we're trying to reschedule. However, if more than one scan
24310d440ea2SEliad Peller 		 * plan was set, we cannot reschedule since we don't know which
24320d440ea2SEliad Peller 		 * scan plan was currently running (and some scan plans may have
24330d440ea2SEliad Peller 		 * already finished).
24340d440ea2SEliad Peller 		 */
24350d440ea2SEliad Peller 		if (sched_scan_req->n_scan_plans > 1 ||
24360d440ea2SEliad Peller 		    __ieee80211_request_sched_scan_start(sched_scan_sdata,
2437b9f628fcSEliad Peller 							 sched_scan_req)) {
2438b9f628fcSEliad Peller 			RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
2439b9f628fcSEliad Peller 			RCU_INIT_POINTER(local->sched_scan_req, NULL);
24400d440ea2SEliad Peller 			sched_scan_stopped = true;
2441b9f628fcSEliad Peller 		}
24420d440ea2SEliad Peller 	mutex_unlock(&local->mtx);
24430d440ea2SEliad Peller 
24440d440ea2SEliad Peller 	if (sched_scan_stopped)
2445b34939b9SArend Van Spriel 		cfg80211_sched_scan_stopped_rtnl(local->hw.wiphy, 0);
24460d440ea2SEliad Peller 
2447c6209488SEliad Peller  wake_up:
2448470f4d61SEliad Peller 
24493c3e21e7SJohannes Berg 	if (local->monitors == local->open_count && local->monitors > 0)
24503c3e21e7SJohannes Berg 		ieee80211_add_virtual_monitor(local);
24513c3e21e7SJohannes Berg 
24526e1b1b24SEliad Peller 	/*
24532a419056SJohannes Berg 	 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
24542a419056SJohannes Berg 	 * sessions can be established after a resume.
24552a419056SJohannes Berg 	 *
24562a419056SJohannes Berg 	 * Also tear down aggregation sessions since reconfiguring
24572a419056SJohannes Berg 	 * them in a hardware restart scenario is not easily done
24582a419056SJohannes Berg 	 * right now, and the hardware will have lost information
24592a419056SJohannes Berg 	 * about the sessions, but we and the AP still think they
24602a419056SJohannes Berg 	 * are active. This is really a workaround though.
24612a419056SJohannes Berg 	 */
246230686bf7SJohannes Berg 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
24632a419056SJohannes Berg 		mutex_lock(&local->sta_mtx);
24642a419056SJohannes Berg 
24652a419056SJohannes Berg 		list_for_each_entry(sta, &local->sta_list, list) {
246627392719SEliad Peller 			if (!local->resuming)
2467c82c4a80SJohannes Berg 				ieee80211_sta_tear_down_BA_sessions(
2468c82c4a80SJohannes Berg 						sta, AGG_STOP_LOCAL_REQUEST);
2469c2c98fdeSJohannes Berg 			clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
247074e2bd1fSWey-Yi Guy 		}
24712a419056SJohannes Berg 
24722a419056SJohannes Berg 		mutex_unlock(&local->sta_mtx);
247374e2bd1fSWey-Yi Guy 	}
247474e2bd1fSWey-Yi Guy 
24752316380fSSara Sharon 	if (local->in_reconfig) {
24762316380fSSara Sharon 		local->in_reconfig = false;
24772316380fSSara Sharon 		barrier();
24782316380fSSara Sharon 
24792316380fSSara Sharon 		/* Restart deferred ROCs */
24802316380fSSara Sharon 		mutex_lock(&local->mtx);
24812316380fSSara Sharon 		ieee80211_start_next_roc(local);
24822316380fSSara Sharon 		mutex_unlock(&local->mtx);
2483f8891461SNaftali Goldstein 
2484f8891461SNaftali Goldstein 		/* Requeue all works */
2485f8891461SNaftali Goldstein 		list_for_each_entry(sdata, &local->interfaces, list)
2486f8891461SNaftali Goldstein 			ieee80211_queue_work(&local->hw, &sdata->work);
24872316380fSSara Sharon 	}
24882316380fSSara Sharon 
2489445ea4e8SJohannes Berg 	ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
2490cca07b00SLuciano Coelho 					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
2491cca07b00SLuciano Coelho 					false);
2492f2753ddbSJohannes Berg 
24935bb644a0SJohannes Berg 	/*
24945bb644a0SJohannes Berg 	 * If this is for hw restart things are still running.
24955bb644a0SJohannes Berg 	 * We may want to change that later, however.
24965bb644a0SJohannes Berg 	 */
2497b0485e9fSEliad Peller 	if (local->open_count && (!suspended || reconfig_due_to_wowlan))
2498cf2c92d8SEliad Peller 		drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
24998f21b0adSJohannes Berg 
2500b0485e9fSEliad Peller 	if (!suspended)
25015bb644a0SJohannes Berg 		return 0;
25025bb644a0SJohannes Berg 
25035bb644a0SJohannes Berg #ifdef CONFIG_PM
2504ceb99fe0SJohannes Berg 	/* first set suspended false, then resuming */
25055bb644a0SJohannes Berg 	local->suspended = false;
2506ceb99fe0SJohannes Berg 	mb();
2507ceb99fe0SJohannes Berg 	local->resuming = false;
25085bb644a0SJohannes Berg 
250974430f94SJohannes Berg 	ieee80211_flush_completed_scan(local, false);
25109120d94eSLuciano Coelho 
25110f8b8245SEliad Peller 	if (local->open_count && !reconfig_due_to_wowlan)
2512cf2c92d8SEliad Peller 		drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
2513cf2c92d8SEliad Peller 
2514b8360ab8SJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
2515b8360ab8SJohannes Berg 		if (!ieee80211_sdata_running(sdata))
2516b8360ab8SJohannes Berg 			continue;
2517b8360ab8SJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_STATION)
2518b8360ab8SJohannes Berg 			ieee80211_sta_restart(sdata);
2519b8360ab8SJohannes Berg 	}
2520b8360ab8SJohannes Berg 
252126d59535SJohannes Berg 	mod_timer(&local->sta_cleanup, jiffies + 1);
25225bb644a0SJohannes Berg #else
25235bb644a0SJohannes Berg 	WARN_ON(1);
25245bb644a0SJohannes Berg #endif
2525d43c6b6eSDavid Spinadel 
2526f2753ddbSJohannes Berg 	return 0;
2527f2753ddbSJohannes Berg }
252842935ecaSLuis R. Rodriguez 
252995acac61SJohannes Berg void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
253095acac61SJohannes Berg {
253195acac61SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
253295acac61SJohannes Berg 	struct ieee80211_local *local;
253395acac61SJohannes Berg 	struct ieee80211_key *key;
253495acac61SJohannes Berg 
253595acac61SJohannes Berg 	if (WARN_ON(!vif))
253695acac61SJohannes Berg 		return;
253795acac61SJohannes Berg 
253895acac61SJohannes Berg 	sdata = vif_to_sdata(vif);
253995acac61SJohannes Berg 	local = sdata->local;
254095acac61SJohannes Berg 
254195acac61SJohannes Berg 	if (WARN_ON(!local->resuming))
254295acac61SJohannes Berg 		return;
254395acac61SJohannes Berg 
254495acac61SJohannes Berg 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
254595acac61SJohannes Berg 		return;
254695acac61SJohannes Berg 
254795acac61SJohannes Berg 	sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
254895acac61SJohannes Berg 
254995acac61SJohannes Berg 	mutex_lock(&local->key_mtx);
255095acac61SJohannes Berg 	list_for_each_entry(key, &sdata->key_list, list)
255195acac61SJohannes Berg 		key->flags |= KEY_FLAG_TAINTED;
255295acac61SJohannes Berg 	mutex_unlock(&local->key_mtx);
255395acac61SJohannes Berg }
255495acac61SJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
255595acac61SJohannes Berg 
255604ecd257SJohannes Berg void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
25570f78231bSJohannes Berg {
255804ecd257SJohannes Berg 	struct ieee80211_local *local = sdata->local;
255904ecd257SJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
256004ecd257SJohannes Berg 	struct ieee80211_chanctx *chanctx;
25610f78231bSJohannes Berg 
256204ecd257SJohannes Berg 	mutex_lock(&local->chanctx_mtx);
25630f78231bSJohannes Berg 
256404ecd257SJohannes Berg 	chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
256504ecd257SJohannes Berg 					lockdep_is_held(&local->chanctx_mtx));
25660f78231bSJohannes Berg 
2567c189a685SAndrei Otcheretianski 	/*
2568c189a685SAndrei Otcheretianski 	 * This function can be called from a work, thus it may be possible
2569c189a685SAndrei Otcheretianski 	 * that the chanctx_conf is removed (due to a disconnection, for
2570c189a685SAndrei Otcheretianski 	 * example).
2571c189a685SAndrei Otcheretianski 	 * So nothing should be done in such case.
2572c189a685SAndrei Otcheretianski 	 */
2573c189a685SAndrei Otcheretianski 	if (!chanctx_conf)
25745d8e4237SJohannes Berg 		goto unlock;
25750f78231bSJohannes Berg 
257604ecd257SJohannes Berg 	chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
257704ecd257SJohannes Berg 	ieee80211_recalc_smps_chanctx(local, chanctx);
25785d8e4237SJohannes Berg  unlock:
257904ecd257SJohannes Berg 	mutex_unlock(&local->chanctx_mtx);
25800f78231bSJohannes Berg }
25818e664fb3SJohannes Berg 
258221f659bfSEliad Peller void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata)
258321f659bfSEliad Peller {
258421f659bfSEliad Peller 	struct ieee80211_local *local = sdata->local;
258521f659bfSEliad Peller 	struct ieee80211_chanctx_conf *chanctx_conf;
258621f659bfSEliad Peller 	struct ieee80211_chanctx *chanctx;
258721f659bfSEliad Peller 
258821f659bfSEliad Peller 	mutex_lock(&local->chanctx_mtx);
258921f659bfSEliad Peller 
259021f659bfSEliad Peller 	chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
259121f659bfSEliad Peller 					lockdep_is_held(&local->chanctx_mtx));
259221f659bfSEliad Peller 
259321f659bfSEliad Peller 	if (WARN_ON_ONCE(!chanctx_conf))
259421f659bfSEliad Peller 		goto unlock;
259521f659bfSEliad Peller 
259621f659bfSEliad Peller 	chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
259721f659bfSEliad Peller 	ieee80211_recalc_chanctx_min_def(local, chanctx);
259821f659bfSEliad Peller  unlock:
259921f659bfSEliad Peller 	mutex_unlock(&local->chanctx_mtx);
260021f659bfSEliad Peller }
260121f659bfSEliad Peller 
26028e664fb3SJohannes Berg size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
26038e664fb3SJohannes Berg {
26048e664fb3SJohannes Berg 	size_t pos = offset;
26058e664fb3SJohannes Berg 
26068e664fb3SJohannes Berg 	while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
26078e664fb3SJohannes Berg 		pos += 2 + ies[pos + 1];
26088e664fb3SJohannes Berg 
26098e664fb3SJohannes Berg 	return pos;
26108e664fb3SJohannes Berg }
2611615f7b9bSMeenakshi Venkataraman 
2612615f7b9bSMeenakshi Venkataraman static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
2613615f7b9bSMeenakshi Venkataraman 					    int rssi_min_thold,
2614615f7b9bSMeenakshi Venkataraman 					    int rssi_max_thold)
2615615f7b9bSMeenakshi Venkataraman {
2616615f7b9bSMeenakshi Venkataraman 	trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
2617615f7b9bSMeenakshi Venkataraman 
2618615f7b9bSMeenakshi Venkataraman 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2619615f7b9bSMeenakshi Venkataraman 		return;
2620615f7b9bSMeenakshi Venkataraman 
2621615f7b9bSMeenakshi Venkataraman 	/*
2622615f7b9bSMeenakshi Venkataraman 	 * Scale up threshold values before storing it, as the RSSI averaging
2623615f7b9bSMeenakshi Venkataraman 	 * algorithm uses a scaled up value as well. Change this scaling
2624615f7b9bSMeenakshi Venkataraman 	 * factor if the RSSI averaging algorithm changes.
2625615f7b9bSMeenakshi Venkataraman 	 */
2626615f7b9bSMeenakshi Venkataraman 	sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
2627615f7b9bSMeenakshi Venkataraman 	sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
2628615f7b9bSMeenakshi Venkataraman }
2629615f7b9bSMeenakshi Venkataraman 
2630615f7b9bSMeenakshi Venkataraman void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
2631615f7b9bSMeenakshi Venkataraman 				    int rssi_min_thold,
2632615f7b9bSMeenakshi Venkataraman 				    int rssi_max_thold)
2633615f7b9bSMeenakshi Venkataraman {
2634615f7b9bSMeenakshi Venkataraman 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2635615f7b9bSMeenakshi Venkataraman 
2636615f7b9bSMeenakshi Venkataraman 	WARN_ON(rssi_min_thold == rssi_max_thold ||
2637615f7b9bSMeenakshi Venkataraman 		rssi_min_thold > rssi_max_thold);
2638615f7b9bSMeenakshi Venkataraman 
2639615f7b9bSMeenakshi Venkataraman 	_ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
2640615f7b9bSMeenakshi Venkataraman 				       rssi_max_thold);
2641615f7b9bSMeenakshi Venkataraman }
2642615f7b9bSMeenakshi Venkataraman EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
2643615f7b9bSMeenakshi Venkataraman 
2644615f7b9bSMeenakshi Venkataraman void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
2645615f7b9bSMeenakshi Venkataraman {
2646615f7b9bSMeenakshi Venkataraman 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2647615f7b9bSMeenakshi Venkataraman 
2648615f7b9bSMeenakshi Venkataraman 	_ieee80211_enable_rssi_reports(sdata, 0, 0);
2649615f7b9bSMeenakshi Venkataraman }
2650615f7b9bSMeenakshi Venkataraman EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
2651768db343SArik Nemtsov 
2652ef96a842SBen Greear u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
265342e7aa77SAlexander Simon 			      u16 cap)
265442e7aa77SAlexander Simon {
265542e7aa77SAlexander Simon 	__le16 tmp;
265642e7aa77SAlexander Simon 
265742e7aa77SAlexander Simon 	*pos++ = WLAN_EID_HT_CAPABILITY;
265842e7aa77SAlexander Simon 	*pos++ = sizeof(struct ieee80211_ht_cap);
265942e7aa77SAlexander Simon 	memset(pos, 0, sizeof(struct ieee80211_ht_cap));
266042e7aa77SAlexander Simon 
266142e7aa77SAlexander Simon 	/* capability flags */
266242e7aa77SAlexander Simon 	tmp = cpu_to_le16(cap);
266342e7aa77SAlexander Simon 	memcpy(pos, &tmp, sizeof(u16));
266442e7aa77SAlexander Simon 	pos += sizeof(u16);
266542e7aa77SAlexander Simon 
266642e7aa77SAlexander Simon 	/* AMPDU parameters */
2667ef96a842SBen Greear 	*pos++ = ht_cap->ampdu_factor |
2668ef96a842SBen Greear 		 (ht_cap->ampdu_density <<
266942e7aa77SAlexander Simon 			IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
267042e7aa77SAlexander Simon 
267142e7aa77SAlexander Simon 	/* MCS set */
2672ef96a842SBen Greear 	memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
2673ef96a842SBen Greear 	pos += sizeof(ht_cap->mcs);
267442e7aa77SAlexander Simon 
267542e7aa77SAlexander Simon 	/* extended capabilities */
267642e7aa77SAlexander Simon 	pos += sizeof(__le16);
267742e7aa77SAlexander Simon 
267842e7aa77SAlexander Simon 	/* BF capabilities */
267942e7aa77SAlexander Simon 	pos += sizeof(__le32);
268042e7aa77SAlexander Simon 
268142e7aa77SAlexander Simon 	/* antenna selection */
268242e7aa77SAlexander Simon 	pos += sizeof(u8);
268342e7aa77SAlexander Simon 
268442e7aa77SAlexander Simon 	return pos;
268542e7aa77SAlexander Simon }
268642e7aa77SAlexander Simon 
2687ba0afa2fSMahesh Palivela u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2688ba0afa2fSMahesh Palivela 			       u32 cap)
2689ba0afa2fSMahesh Palivela {
2690ba0afa2fSMahesh Palivela 	__le32 tmp;
2691ba0afa2fSMahesh Palivela 
2692ba0afa2fSMahesh Palivela 	*pos++ = WLAN_EID_VHT_CAPABILITY;
2693d4950281SMahesh Palivela 	*pos++ = sizeof(struct ieee80211_vht_cap);
2694d4950281SMahesh Palivela 	memset(pos, 0, sizeof(struct ieee80211_vht_cap));
2695ba0afa2fSMahesh Palivela 
2696ba0afa2fSMahesh Palivela 	/* capability flags */
2697ba0afa2fSMahesh Palivela 	tmp = cpu_to_le32(cap);
2698ba0afa2fSMahesh Palivela 	memcpy(pos, &tmp, sizeof(u32));
2699ba0afa2fSMahesh Palivela 	pos += sizeof(u32);
2700ba0afa2fSMahesh Palivela 
2701ba0afa2fSMahesh Palivela 	/* VHT MCS set */
2702ba0afa2fSMahesh Palivela 	memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
2703ba0afa2fSMahesh Palivela 	pos += sizeof(vht_cap->vht_mcs);
2704ba0afa2fSMahesh Palivela 
2705ba0afa2fSMahesh Palivela 	return pos;
2706ba0afa2fSMahesh Palivela }
2707ba0afa2fSMahesh Palivela 
270841cbb0f5SLuca Coelho u8 *ieee80211_ie_build_he_cap(u8 *pos,
270941cbb0f5SLuca Coelho 			      const struct ieee80211_sta_he_cap *he_cap,
271041cbb0f5SLuca Coelho 			      u8 *end)
271141cbb0f5SLuca Coelho {
271241cbb0f5SLuca Coelho 	u8 n;
271341cbb0f5SLuca Coelho 	u8 ie_len;
271441cbb0f5SLuca Coelho 	u8 *orig_pos = pos;
271541cbb0f5SLuca Coelho 
271641cbb0f5SLuca Coelho 	/* Make sure we have place for the IE */
271741cbb0f5SLuca Coelho 	/*
271841cbb0f5SLuca Coelho 	 * TODO: the 1 added is because this temporarily is under the EXTENSION
271941cbb0f5SLuca Coelho 	 * IE. Get rid of it when it moves.
272041cbb0f5SLuca Coelho 	 */
272141cbb0f5SLuca Coelho 	if (!he_cap)
272241cbb0f5SLuca Coelho 		return orig_pos;
272341cbb0f5SLuca Coelho 
272441cbb0f5SLuca Coelho 	n = ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem);
272541cbb0f5SLuca Coelho 	ie_len = 2 + 1 +
272641cbb0f5SLuca Coelho 		 sizeof(he_cap->he_cap_elem) + n +
272741cbb0f5SLuca Coelho 		 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
272841cbb0f5SLuca Coelho 				       he_cap->he_cap_elem.phy_cap_info);
272941cbb0f5SLuca Coelho 
273041cbb0f5SLuca Coelho 	if ((end - pos) < ie_len)
273141cbb0f5SLuca Coelho 		return orig_pos;
273241cbb0f5SLuca Coelho 
273341cbb0f5SLuca Coelho 	*pos++ = WLAN_EID_EXTENSION;
273441cbb0f5SLuca Coelho 	pos++; /* We'll set the size later below */
273541cbb0f5SLuca Coelho 	*pos++ = WLAN_EID_EXT_HE_CAPABILITY;
273641cbb0f5SLuca Coelho 
273741cbb0f5SLuca Coelho 	/* Fixed data */
273841cbb0f5SLuca Coelho 	memcpy(pos, &he_cap->he_cap_elem, sizeof(he_cap->he_cap_elem));
273941cbb0f5SLuca Coelho 	pos += sizeof(he_cap->he_cap_elem);
274041cbb0f5SLuca Coelho 
274141cbb0f5SLuca Coelho 	memcpy(pos, &he_cap->he_mcs_nss_supp, n);
274241cbb0f5SLuca Coelho 	pos += n;
274341cbb0f5SLuca Coelho 
274441cbb0f5SLuca Coelho 	/* Check if PPE Threshold should be present */
274541cbb0f5SLuca Coelho 	if ((he_cap->he_cap_elem.phy_cap_info[6] &
274641cbb0f5SLuca Coelho 	     IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) == 0)
274741cbb0f5SLuca Coelho 		goto end;
274841cbb0f5SLuca Coelho 
274941cbb0f5SLuca Coelho 	/*
275041cbb0f5SLuca Coelho 	 * Calculate how many PPET16/PPET8 pairs are to come. Algorithm:
275141cbb0f5SLuca Coelho 	 * (NSS_M1 + 1) x (num of 1 bits in RU_INDEX_BITMASK)
275241cbb0f5SLuca Coelho 	 */
275341cbb0f5SLuca Coelho 	n = hweight8(he_cap->ppe_thres[0] &
275441cbb0f5SLuca Coelho 		     IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
275541cbb0f5SLuca Coelho 	n *= (1 + ((he_cap->ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) >>
275641cbb0f5SLuca Coelho 		   IEEE80211_PPE_THRES_NSS_POS));
275741cbb0f5SLuca Coelho 
275841cbb0f5SLuca Coelho 	/*
275941cbb0f5SLuca Coelho 	 * Each pair is 6 bits, and we need to add the 7 "header" bits to the
276041cbb0f5SLuca Coelho 	 * total size.
276141cbb0f5SLuca Coelho 	 */
276241cbb0f5SLuca Coelho 	n = (n * IEEE80211_PPE_THRES_INFO_PPET_SIZE * 2) + 7;
276341cbb0f5SLuca Coelho 	n = DIV_ROUND_UP(n, 8);
276441cbb0f5SLuca Coelho 
276541cbb0f5SLuca Coelho 	/* Copy PPE Thresholds */
276641cbb0f5SLuca Coelho 	memcpy(pos, &he_cap->ppe_thres, n);
276741cbb0f5SLuca Coelho 	pos += n;
276841cbb0f5SLuca Coelho 
276941cbb0f5SLuca Coelho end:
277041cbb0f5SLuca Coelho 	orig_pos[1] = (pos - orig_pos) - 2;
277141cbb0f5SLuca Coelho 	return pos;
277241cbb0f5SLuca Coelho }
277341cbb0f5SLuca Coelho 
2774074d46d1SJohannes Berg u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
27754bf88530SJohannes Berg 			       const struct cfg80211_chan_def *chandef,
277657f255f5SArik Nemtsov 			       u16 prot_mode, bool rifs_mode)
277742e7aa77SAlexander Simon {
2778074d46d1SJohannes Berg 	struct ieee80211_ht_operation *ht_oper;
277942e7aa77SAlexander Simon 	/* Build HT Information */
2780074d46d1SJohannes Berg 	*pos++ = WLAN_EID_HT_OPERATION;
2781074d46d1SJohannes Berg 	*pos++ = sizeof(struct ieee80211_ht_operation);
2782074d46d1SJohannes Berg 	ht_oper = (struct ieee80211_ht_operation *)pos;
27834bf88530SJohannes Berg 	ht_oper->primary_chan = ieee80211_frequency_to_channel(
27844bf88530SJohannes Berg 					chandef->chan->center_freq);
27854bf88530SJohannes Berg 	switch (chandef->width) {
27864bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_160:
27874bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_80P80:
27884bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_80:
27894bf88530SJohannes Berg 	case NL80211_CHAN_WIDTH_40:
27904bf88530SJohannes Berg 		if (chandef->center_freq1 > chandef->chan->center_freq)
27914bf88530SJohannes Berg 			ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
27924bf88530SJohannes Berg 		else
2793074d46d1SJohannes Berg 			ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
279442e7aa77SAlexander Simon 		break;
279542e7aa77SAlexander Simon 	default:
2796074d46d1SJohannes Berg 		ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
279742e7aa77SAlexander Simon 		break;
279842e7aa77SAlexander Simon 	}
2799aee286c2SThomas Pedersen 	if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
28004bf88530SJohannes Berg 	    chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
28014bf88530SJohannes Berg 	    chandef->width != NL80211_CHAN_WIDTH_20)
2802074d46d1SJohannes Berg 		ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
2803ff3cc5f4SSimon Wunderlich 
280457f255f5SArik Nemtsov 	if (rifs_mode)
280557f255f5SArik Nemtsov 		ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
280657f255f5SArik Nemtsov 
2807431e3154SAshok Nagarajan 	ht_oper->operation_mode = cpu_to_le16(prot_mode);
2808074d46d1SJohannes Berg 	ht_oper->stbc_param = 0x0000;
280942e7aa77SAlexander Simon 
281042e7aa77SAlexander Simon 	/* It seems that Basic MCS set and Supported MCS set
281142e7aa77SAlexander Simon 	   are identical for the first 10 bytes */
2812074d46d1SJohannes Berg 	memset(&ht_oper->basic_set, 0, 16);
2813074d46d1SJohannes Berg 	memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
281442e7aa77SAlexander Simon 
2815074d46d1SJohannes Berg 	return pos + sizeof(struct ieee80211_ht_operation);
281642e7aa77SAlexander Simon }
281742e7aa77SAlexander Simon 
281875d627d5SSimon Wunderlich void ieee80211_ie_build_wide_bw_cs(u8 *pos,
281975d627d5SSimon Wunderlich 				   const struct cfg80211_chan_def *chandef)
282075d627d5SSimon Wunderlich {
282175d627d5SSimon Wunderlich 	*pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH;	/* EID */
282275d627d5SSimon Wunderlich 	*pos++ = 3;					/* IE length */
282375d627d5SSimon Wunderlich 	/* New channel width */
282475d627d5SSimon Wunderlich 	switch (chandef->width) {
282575d627d5SSimon Wunderlich 	case NL80211_CHAN_WIDTH_80:
282675d627d5SSimon Wunderlich 		*pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ;
282775d627d5SSimon Wunderlich 		break;
282875d627d5SSimon Wunderlich 	case NL80211_CHAN_WIDTH_160:
282975d627d5SSimon Wunderlich 		*pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ;
283075d627d5SSimon Wunderlich 		break;
283175d627d5SSimon Wunderlich 	case NL80211_CHAN_WIDTH_80P80:
283275d627d5SSimon Wunderlich 		*pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
283375d627d5SSimon Wunderlich 		break;
283475d627d5SSimon Wunderlich 	default:
283575d627d5SSimon Wunderlich 		*pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT;
283675d627d5SSimon Wunderlich 	}
283775d627d5SSimon Wunderlich 
283875d627d5SSimon Wunderlich 	/* new center frequency segment 0 */
283975d627d5SSimon Wunderlich 	*pos++ = ieee80211_frequency_to_channel(chandef->center_freq1);
284075d627d5SSimon Wunderlich 	/* new center frequency segment 1 */
284175d627d5SSimon Wunderlich 	if (chandef->center_freq2)
284275d627d5SSimon Wunderlich 		*pos++ = ieee80211_frequency_to_channel(chandef->center_freq2);
284375d627d5SSimon Wunderlich 	else
284475d627d5SSimon Wunderlich 		*pos++ = 0;
284575d627d5SSimon Wunderlich }
284675d627d5SSimon Wunderlich 
2847fb28ec0cSArik Nemtsov u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2848fb28ec0cSArik Nemtsov 				const struct cfg80211_chan_def *chandef)
2849fb28ec0cSArik Nemtsov {
2850fb28ec0cSArik Nemtsov 	struct ieee80211_vht_operation *vht_oper;
2851fb28ec0cSArik Nemtsov 
2852fb28ec0cSArik Nemtsov 	*pos++ = WLAN_EID_VHT_OPERATION;
2853fb28ec0cSArik Nemtsov 	*pos++ = sizeof(struct ieee80211_vht_operation);
2854fb28ec0cSArik Nemtsov 	vht_oper = (struct ieee80211_vht_operation *)pos;
28552fb51c35SJohannes Berg 	vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
2856fb28ec0cSArik Nemtsov 							chandef->center_freq1);
2857fb28ec0cSArik Nemtsov 	if (chandef->center_freq2)
28582fb51c35SJohannes Berg 		vht_oper->center_freq_seg1_idx =
2859fb28ec0cSArik Nemtsov 			ieee80211_frequency_to_channel(chandef->center_freq2);
2860f020ae40SChun-Yeow Yeoh 	else
28612fb51c35SJohannes Berg 		vht_oper->center_freq_seg1_idx = 0x00;
2862fb28ec0cSArik Nemtsov 
2863fb28ec0cSArik Nemtsov 	switch (chandef->width) {
2864fb28ec0cSArik Nemtsov 	case NL80211_CHAN_WIDTH_160:
286523665aafSJouni Malinen 		/*
286623665aafSJouni Malinen 		 * Convert 160 MHz channel width to new style as interop
286723665aafSJouni Malinen 		 * workaround.
286823665aafSJouni Malinen 		 */
286923665aafSJouni Malinen 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
28702fb51c35SJohannes Berg 		vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
287123665aafSJouni Malinen 		if (chandef->chan->center_freq < chandef->center_freq1)
28722fb51c35SJohannes Berg 			vht_oper->center_freq_seg0_idx -= 8;
287323665aafSJouni Malinen 		else
28742fb51c35SJohannes Berg 			vht_oper->center_freq_seg0_idx += 8;
2875fb28ec0cSArik Nemtsov 		break;
2876fb28ec0cSArik Nemtsov 	case NL80211_CHAN_WIDTH_80P80:
287723665aafSJouni Malinen 		/*
287823665aafSJouni Malinen 		 * Convert 80+80 MHz channel width to new style as interop
287923665aafSJouni Malinen 		 * workaround.
288023665aafSJouni Malinen 		 */
288123665aafSJouni Malinen 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
2882fb28ec0cSArik Nemtsov 		break;
2883fb28ec0cSArik Nemtsov 	case NL80211_CHAN_WIDTH_80:
2884fb28ec0cSArik Nemtsov 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
2885fb28ec0cSArik Nemtsov 		break;
2886fb28ec0cSArik Nemtsov 	default:
2887fb28ec0cSArik Nemtsov 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
2888fb28ec0cSArik Nemtsov 		break;
2889fb28ec0cSArik Nemtsov 	}
2890fb28ec0cSArik Nemtsov 
2891fb28ec0cSArik Nemtsov 	/* don't require special VHT peer rates */
2892fb28ec0cSArik Nemtsov 	vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
2893fb28ec0cSArik Nemtsov 
2894fb28ec0cSArik Nemtsov 	return pos + sizeof(struct ieee80211_vht_operation);
2895fb28ec0cSArik Nemtsov }
2896fb28ec0cSArik Nemtsov 
28978ac3c704SJohannes Berg bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
28984bf88530SJohannes Berg 			       struct cfg80211_chan_def *chandef)
289942e7aa77SAlexander Simon {
290042e7aa77SAlexander Simon 	enum nl80211_channel_type channel_type;
290142e7aa77SAlexander Simon 
29028ac3c704SJohannes Berg 	if (!ht_oper)
29038ac3c704SJohannes Berg 		return false;
290442e7aa77SAlexander Simon 
2905074d46d1SJohannes Berg 	switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
290642e7aa77SAlexander Simon 	case IEEE80211_HT_PARAM_CHA_SEC_NONE:
290742e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_HT20;
290842e7aa77SAlexander Simon 		break;
290942e7aa77SAlexander Simon 	case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
291042e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_HT40PLUS;
291142e7aa77SAlexander Simon 		break;
291242e7aa77SAlexander Simon 	case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
291342e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_HT40MINUS;
291442e7aa77SAlexander Simon 		break;
291542e7aa77SAlexander Simon 	default:
291642e7aa77SAlexander Simon 		channel_type = NL80211_CHAN_NO_HT;
29178ac3c704SJohannes Berg 		return false;
291842e7aa77SAlexander Simon 	}
291942e7aa77SAlexander Simon 
29208ac3c704SJohannes Berg 	cfg80211_chandef_create(chandef, chandef->chan, channel_type);
29218ac3c704SJohannes Berg 	return true;
292242e7aa77SAlexander Simon }
292342e7aa77SAlexander Simon 
29247eb26df2SJohannes Berg bool ieee80211_chandef_vht_oper(struct ieee80211_hw *hw,
29257eb26df2SJohannes Berg 				const struct ieee80211_vht_operation *oper,
29267eb26df2SJohannes Berg 				const struct ieee80211_ht_operation *htop,
2927abcff6efSJanusz.Dziedzic@tieto.com 				struct cfg80211_chan_def *chandef)
2928abcff6efSJanusz.Dziedzic@tieto.com {
29298ac3c704SJohannes Berg 	struct cfg80211_chan_def new = *chandef;
29307eb26df2SJohannes Berg 	int cf0, cf1;
29317eb26df2SJohannes Berg 	int ccfs0, ccfs1, ccfs2;
29327eb26df2SJohannes Berg 	int ccf0, ccf1;
2933abcff6efSJanusz.Dziedzic@tieto.com 
29347eb26df2SJohannes Berg 	if (!oper || !htop)
29358ac3c704SJohannes Berg 		return false;
29368ac3c704SJohannes Berg 
29377eb26df2SJohannes Berg 	ccfs0 = oper->center_freq_seg0_idx;
29387eb26df2SJohannes Berg 	ccfs1 = oper->center_freq_seg1_idx;
29397eb26df2SJohannes Berg 	ccfs2 = (le16_to_cpu(htop->operation_mode) &
29407eb26df2SJohannes Berg 				IEEE80211_HT_OP_MODE_CCFS2_MASK)
29417eb26df2SJohannes Berg 			>> IEEE80211_HT_OP_MODE_CCFS2_SHIFT;
29427eb26df2SJohannes Berg 
29437eb26df2SJohannes Berg 	/* when parsing (and we know how to) CCFS1 and CCFS2 are equivalent */
29447eb26df2SJohannes Berg 	ccf0 = ccfs0;
29457eb26df2SJohannes Berg 	ccf1 = ccfs1;
29467eb26df2SJohannes Berg 	if (!ccfs1 && ieee80211_hw_check(hw, SUPPORTS_VHT_EXT_NSS_BW))
29477eb26df2SJohannes Berg 		ccf1 = ccfs2;
29487eb26df2SJohannes Berg 
29497eb26df2SJohannes Berg 	cf0 = ieee80211_channel_to_frequency(ccf0, chandef->chan->band);
29507eb26df2SJohannes Berg 	cf1 = ieee80211_channel_to_frequency(ccf1, chandef->chan->band);
2951abcff6efSJanusz.Dziedzic@tieto.com 
2952abcff6efSJanusz.Dziedzic@tieto.com 	switch (oper->chan_width) {
2953abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_USE_HT:
29547eb26df2SJohannes Berg 		/* just use HT information directly */
2955abcff6efSJanusz.Dziedzic@tieto.com 		break;
2956abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_80MHZ:
29578ac3c704SJohannes Berg 		new.width = NL80211_CHAN_WIDTH_80;
29587eb26df2SJohannes Berg 		new.center_freq1 = cf0;
295923665aafSJouni Malinen 		/* If needed, adjust based on the newer interop workaround. */
29607eb26df2SJohannes Berg 		if (ccf1) {
296123665aafSJouni Malinen 			unsigned int diff;
296223665aafSJouni Malinen 
29637eb26df2SJohannes Berg 			diff = abs(ccf1 - ccf0);
296423665aafSJouni Malinen 			if (diff == 8) {
296523665aafSJouni Malinen 				new.width = NL80211_CHAN_WIDTH_160;
29667eb26df2SJohannes Berg 				new.center_freq1 = cf1;
296723665aafSJouni Malinen 			} else if (diff > 8) {
296823665aafSJouni Malinen 				new.width = NL80211_CHAN_WIDTH_80P80;
29697eb26df2SJohannes Berg 				new.center_freq2 = cf1;
297023665aafSJouni Malinen 			}
297123665aafSJouni Malinen 		}
2972abcff6efSJanusz.Dziedzic@tieto.com 		break;
2973abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_160MHZ:
29747eb26df2SJohannes Berg 		/* deprecated encoding */
29758ac3c704SJohannes Berg 		new.width = NL80211_CHAN_WIDTH_160;
29767eb26df2SJohannes Berg 		new.center_freq1 = cf0;
2977abcff6efSJanusz.Dziedzic@tieto.com 		break;
2978abcff6efSJanusz.Dziedzic@tieto.com 	case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
29797eb26df2SJohannes Berg 		/* deprecated encoding */
29808ac3c704SJohannes Berg 		new.width = NL80211_CHAN_WIDTH_80P80;
29817eb26df2SJohannes Berg 		new.center_freq1 = cf0;
29827eb26df2SJohannes Berg 		new.center_freq2 = cf1;
2983abcff6efSJanusz.Dziedzic@tieto.com 		break;
2984abcff6efSJanusz.Dziedzic@tieto.com 	default:
29858ac3c704SJohannes Berg 		return false;
2986abcff6efSJanusz.Dziedzic@tieto.com 	}
2987abcff6efSJanusz.Dziedzic@tieto.com 
29888ac3c704SJohannes Berg 	if (!cfg80211_chandef_valid(&new))
29898ac3c704SJohannes Berg 		return false;
29908ac3c704SJohannes Berg 
29918ac3c704SJohannes Berg 	*chandef = new;
29928ac3c704SJohannes Berg 	return true;
2993abcff6efSJanusz.Dziedzic@tieto.com }
2994abcff6efSJanusz.Dziedzic@tieto.com 
29952103dec1SSimon Wunderlich int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
29962103dec1SSimon Wunderlich 			     const struct ieee80211_supported_band *sband,
29972103dec1SSimon Wunderlich 			     const u8 *srates, int srates_len, u32 *rates)
29982103dec1SSimon Wunderlich {
29992103dec1SSimon Wunderlich 	u32 rate_flags = ieee80211_chandef_rate_flags(chandef);
30002103dec1SSimon Wunderlich 	int shift = ieee80211_chandef_get_shift(chandef);
30012103dec1SSimon Wunderlich 	struct ieee80211_rate *br;
30022103dec1SSimon Wunderlich 	int brate, rate, i, j, count = 0;
30032103dec1SSimon Wunderlich 
30042103dec1SSimon Wunderlich 	*rates = 0;
30052103dec1SSimon Wunderlich 
30062103dec1SSimon Wunderlich 	for (i = 0; i < srates_len; i++) {
30072103dec1SSimon Wunderlich 		rate = srates[i] & 0x7f;
30082103dec1SSimon Wunderlich 
30092103dec1SSimon Wunderlich 		for (j = 0; j < sband->n_bitrates; j++) {
30102103dec1SSimon Wunderlich 			br = &sband->bitrates[j];
30112103dec1SSimon Wunderlich 			if ((rate_flags & br->flags) != rate_flags)
30122103dec1SSimon Wunderlich 				continue;
30132103dec1SSimon Wunderlich 
30142103dec1SSimon Wunderlich 			brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
30152103dec1SSimon Wunderlich 			if (brate == rate) {
30162103dec1SSimon Wunderlich 				*rates |= BIT(j);
30172103dec1SSimon Wunderlich 				count++;
30182103dec1SSimon Wunderlich 				break;
30192103dec1SSimon Wunderlich 			}
30202103dec1SSimon Wunderlich 		}
30212103dec1SSimon Wunderlich 	}
30222103dec1SSimon Wunderlich 	return count;
30232103dec1SSimon Wunderlich }
30242103dec1SSimon Wunderlich 
3025fc8a7321SJohannes Berg int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
30266b77863bSJohannes Berg 			    struct sk_buff *skb, bool need_basic,
302757fbcce3SJohannes Berg 			    enum nl80211_band band)
3028768db343SArik Nemtsov {
3029768db343SArik Nemtsov 	struct ieee80211_local *local = sdata->local;
3030768db343SArik Nemtsov 	struct ieee80211_supported_band *sband;
30312103dec1SSimon Wunderlich 	int rate, shift;
3032768db343SArik Nemtsov 	u8 i, rates, *pos;
3033fc8a7321SJohannes Berg 	u32 basic_rates = sdata->vif.bss_conf.basic_rates;
30342103dec1SSimon Wunderlich 	u32 rate_flags;
3035768db343SArik Nemtsov 
30362103dec1SSimon Wunderlich 	shift = ieee80211_vif_get_shift(&sdata->vif);
30372103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
30386b77863bSJohannes Berg 	sband = local->hw.wiphy->bands[band];
30392103dec1SSimon Wunderlich 	rates = 0;
30402103dec1SSimon Wunderlich 	for (i = 0; i < sband->n_bitrates; i++) {
30412103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
30422103dec1SSimon Wunderlich 			continue;
30432103dec1SSimon Wunderlich 		rates++;
30442103dec1SSimon Wunderlich 	}
3045768db343SArik Nemtsov 	if (rates > 8)
3046768db343SArik Nemtsov 		rates = 8;
3047768db343SArik Nemtsov 
3048768db343SArik Nemtsov 	if (skb_tailroom(skb) < rates + 2)
3049768db343SArik Nemtsov 		return -ENOMEM;
3050768db343SArik Nemtsov 
3051768db343SArik Nemtsov 	pos = skb_put(skb, rates + 2);
3052768db343SArik Nemtsov 	*pos++ = WLAN_EID_SUPP_RATES;
3053768db343SArik Nemtsov 	*pos++ = rates;
3054768db343SArik Nemtsov 	for (i = 0; i < rates; i++) {
3055657c3e0cSAshok Nagarajan 		u8 basic = 0;
30562103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
30572103dec1SSimon Wunderlich 			continue;
30582103dec1SSimon Wunderlich 
3059657c3e0cSAshok Nagarajan 		if (need_basic && basic_rates & BIT(i))
3060657c3e0cSAshok Nagarajan 			basic = 0x80;
30612103dec1SSimon Wunderlich 		rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
30622103dec1SSimon Wunderlich 				    5 * (1 << shift));
30632103dec1SSimon Wunderlich 		*pos++ = basic | (u8) rate;
3064768db343SArik Nemtsov 	}
3065768db343SArik Nemtsov 
3066768db343SArik Nemtsov 	return 0;
3067768db343SArik Nemtsov }
3068768db343SArik Nemtsov 
3069fc8a7321SJohannes Berg int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
30706b77863bSJohannes Berg 				struct sk_buff *skb, bool need_basic,
307157fbcce3SJohannes Berg 				enum nl80211_band band)
3072768db343SArik Nemtsov {
3073768db343SArik Nemtsov 	struct ieee80211_local *local = sdata->local;
3074768db343SArik Nemtsov 	struct ieee80211_supported_band *sband;
3075cc63ec76SChun-Yeow Yeoh 	int rate, shift;
3076768db343SArik Nemtsov 	u8 i, exrates, *pos;
3077fc8a7321SJohannes Berg 	u32 basic_rates = sdata->vif.bss_conf.basic_rates;
30782103dec1SSimon Wunderlich 	u32 rate_flags;
30792103dec1SSimon Wunderlich 
30802103dec1SSimon Wunderlich 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
30812103dec1SSimon Wunderlich 	shift = ieee80211_vif_get_shift(&sdata->vif);
3082768db343SArik Nemtsov 
30836b77863bSJohannes Berg 	sband = local->hw.wiphy->bands[band];
30842103dec1SSimon Wunderlich 	exrates = 0;
30852103dec1SSimon Wunderlich 	for (i = 0; i < sband->n_bitrates; i++) {
30862103dec1SSimon Wunderlich 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
30872103dec1SSimon Wunderlich 			continue;
30882103dec1SSimon Wunderlich 		exrates++;
30892103dec1SSimon Wunderlich 	}
30902103dec1SSimon Wunderlich 
3091768db343SArik Nemtsov 	if (exrates > 8)
3092768db343SArik Nemtsov 		exrates -= 8;
3093768db343SArik Nemtsov 	else
3094768db343SArik Nemtsov 		exrates = 0;
3095768db343SArik Nemtsov 
3096768db343SArik Nemtsov 	if (skb_tailroom(skb) < exrates + 2)
3097768db343SArik Nemtsov 		return -ENOMEM;
3098768db343SArik Nemtsov 
3099768db343SArik Nemtsov 	if (exrates) {
3100768db343SArik Nemtsov 		pos = skb_put(skb, exrates + 2);
3101768db343SArik Nemtsov 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
3102768db343SArik Nemtsov 		*pos++ = exrates;
3103768db343SArik Nemtsov 		for (i = 8; i < sband->n_bitrates; i++) {
3104657c3e0cSAshok Nagarajan 			u8 basic = 0;
31052103dec1SSimon Wunderlich 			if ((rate_flags & sband->bitrates[i].flags)
31062103dec1SSimon Wunderlich 			    != rate_flags)
31072103dec1SSimon Wunderlich 				continue;
3108657c3e0cSAshok Nagarajan 			if (need_basic && basic_rates & BIT(i))
3109657c3e0cSAshok Nagarajan 				basic = 0x80;
31102103dec1SSimon Wunderlich 			rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
31112103dec1SSimon Wunderlich 					    5 * (1 << shift));
31122103dec1SSimon Wunderlich 			*pos++ = basic | (u8) rate;
3113768db343SArik Nemtsov 		}
3114768db343SArik Nemtsov 	}
3115768db343SArik Nemtsov 	return 0;
3116768db343SArik Nemtsov }
31171dae27f8SWey-Yi Guy 
31181dae27f8SWey-Yi Guy int ieee80211_ave_rssi(struct ieee80211_vif *vif)
31191dae27f8SWey-Yi Guy {
31201dae27f8SWey-Yi Guy 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
31211dae27f8SWey-Yi Guy 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
31221dae27f8SWey-Yi Guy 
3123be6bcabcSWey-Yi Guy 	if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
3124be6bcabcSWey-Yi Guy 		/* non-managed type inferfaces */
3125be6bcabcSWey-Yi Guy 		return 0;
3126be6bcabcSWey-Yi Guy 	}
3127338c17aeSJohannes Berg 	return -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
31281dae27f8SWey-Yi Guy }
31290d8a0a17SWey-Yi Guy EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
313004ecd257SJohannes Berg 
313104ecd257SJohannes Berg u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
313204ecd257SJohannes Berg {
313304ecd257SJohannes Berg 	if (!mcs)
313404ecd257SJohannes Berg 		return 1;
313504ecd257SJohannes Berg 
313604ecd257SJohannes Berg 	/* TODO: consider rx_highest */
313704ecd257SJohannes Berg 
313804ecd257SJohannes Berg 	if (mcs->rx_mask[3])
313904ecd257SJohannes Berg 		return 4;
314004ecd257SJohannes Berg 	if (mcs->rx_mask[2])
314104ecd257SJohannes Berg 		return 3;
314204ecd257SJohannes Berg 	if (mcs->rx_mask[1])
314304ecd257SJohannes Berg 		return 2;
314404ecd257SJohannes Berg 	return 1;
314504ecd257SJohannes Berg }
3146f4bda337SThomas Pedersen 
3147f4bda337SThomas Pedersen /**
3148f4bda337SThomas Pedersen  * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
3149f4bda337SThomas Pedersen  * @local: mac80211 hw info struct
3150f4bda337SThomas Pedersen  * @status: RX status
3151f4bda337SThomas Pedersen  * @mpdu_len: total MPDU length (including FCS)
3152f4bda337SThomas Pedersen  * @mpdu_offset: offset into MPDU to calculate timestamp at
3153f4bda337SThomas Pedersen  *
3154f4bda337SThomas Pedersen  * This function calculates the RX timestamp at the given MPDU offset, taking
3155f4bda337SThomas Pedersen  * into account what the RX timestamp was. An offset of 0 will just normalize
3156f4bda337SThomas Pedersen  * the timestamp to TSF at beginning of MPDU reception.
3157f4bda337SThomas Pedersen  */
3158f4bda337SThomas Pedersen u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
3159f4bda337SThomas Pedersen 				     struct ieee80211_rx_status *status,
3160f4bda337SThomas Pedersen 				     unsigned int mpdu_len,
3161f4bda337SThomas Pedersen 				     unsigned int mpdu_offset)
3162f4bda337SThomas Pedersen {
3163f4bda337SThomas Pedersen 	u64 ts = status->mactime;
3164f4bda337SThomas Pedersen 	struct rate_info ri;
3165f4bda337SThomas Pedersen 	u16 rate;
3166f4bda337SThomas Pedersen 
3167f4bda337SThomas Pedersen 	if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
3168f4bda337SThomas Pedersen 		return 0;
3169f4bda337SThomas Pedersen 
3170f4bda337SThomas Pedersen 	memset(&ri, 0, sizeof(ri));
3171f4bda337SThomas Pedersen 
317235f4962cSJohannes Berg 	ri.bw = status->bw;
317335f4962cSJohannes Berg 
3174f4bda337SThomas Pedersen 	/* Fill cfg80211 rate info */
3175da6a4352SJohannes Berg 	switch (status->encoding) {
3176da6a4352SJohannes Berg 	case RX_ENC_HT:
3177f4bda337SThomas Pedersen 		ri.mcs = status->rate_idx;
3178f4bda337SThomas Pedersen 		ri.flags |= RATE_INFO_FLAGS_MCS;
31797fdd69c5SJohannes Berg 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
3180f4bda337SThomas Pedersen 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
3181da6a4352SJohannes Berg 		break;
3182da6a4352SJohannes Berg 	case RX_ENC_VHT:
31835614618eSJohannes Berg 		ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
31845614618eSJohannes Berg 		ri.mcs = status->rate_idx;
31858613c948SJohannes Berg 		ri.nss = status->nss;
31867fdd69c5SJohannes Berg 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
31875614618eSJohannes Berg 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
3188da6a4352SJohannes Berg 		break;
3189da6a4352SJohannes Berg 	default:
3190da6a4352SJohannes Berg 		WARN_ON(1);
3191da6a4352SJohannes Berg 		/* fall through */
3192da6a4352SJohannes Berg 	case RX_ENC_LEGACY: {
3193f4bda337SThomas Pedersen 		struct ieee80211_supported_band *sband;
31942103dec1SSimon Wunderlich 		int shift = 0;
31952103dec1SSimon Wunderlich 		int bitrate;
31962103dec1SSimon Wunderlich 
3197da6a4352SJohannes Berg 		switch (status->bw) {
3198da6a4352SJohannes Berg 		case RATE_INFO_BW_10:
31992103dec1SSimon Wunderlich 			shift = 1;
3200da6a4352SJohannes Berg 			break;
3201da6a4352SJohannes Berg 		case RATE_INFO_BW_5:
32022103dec1SSimon Wunderlich 			shift = 2;
3203da6a4352SJohannes Berg 			break;
3204b51f3beeSJohannes Berg 		}
3205f4bda337SThomas Pedersen 
3206f4bda337SThomas Pedersen 		sband = local->hw.wiphy->bands[status->band];
32072103dec1SSimon Wunderlich 		bitrate = sband->bitrates[status->rate_idx].bitrate;
32082103dec1SSimon Wunderlich 		ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
3209f4a0f0c5SJohannes Berg 
3210f4a0f0c5SJohannes Berg 		if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
3211f4a0f0c5SJohannes Berg 			/* TODO: handle HT/VHT preambles */
321257fbcce3SJohannes Berg 			if (status->band == NL80211_BAND_5GHZ) {
3213f4a0f0c5SJohannes Berg 				ts += 20 << shift;
3214f4a0f0c5SJohannes Berg 				mpdu_offset += 2;
32157fdd69c5SJohannes Berg 			} else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
3216f4a0f0c5SJohannes Berg 				ts += 96;
3217f4a0f0c5SJohannes Berg 			} else {
3218f4a0f0c5SJohannes Berg 				ts += 192;
3219f4a0f0c5SJohannes Berg 			}
3220f4a0f0c5SJohannes Berg 		}
3221da6a4352SJohannes Berg 		break;
3222da6a4352SJohannes Berg 		}
3223f4bda337SThomas Pedersen 	}
3224f4bda337SThomas Pedersen 
3225f4bda337SThomas Pedersen 	rate = cfg80211_calculate_bitrate(&ri);
3226d86aa4f8SJohannes Berg 	if (WARN_ONCE(!rate,
3227f980ebc0SSara Sharon 		      "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
3228f980ebc0SSara Sharon 		      (unsigned long long)status->flag, status->rate_idx,
32298613c948SJohannes Berg 		      status->nss))
3230d86aa4f8SJohannes Berg 		return 0;
3231f4bda337SThomas Pedersen 
3232f4bda337SThomas Pedersen 	/* rewind from end of MPDU */
3233f4bda337SThomas Pedersen 	if (status->flag & RX_FLAG_MACTIME_END)
3234f4bda337SThomas Pedersen 		ts -= mpdu_len * 8 * 10 / rate;
3235f4bda337SThomas Pedersen 
3236f4bda337SThomas Pedersen 	ts += mpdu_offset * 8 * 10 / rate;
3237f4bda337SThomas Pedersen 
3238f4bda337SThomas Pedersen 	return ts;
3239f4bda337SThomas Pedersen }
3240164eb02dSSimon Wunderlich 
3241164eb02dSSimon Wunderlich void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
3242164eb02dSSimon Wunderlich {
3243164eb02dSSimon Wunderlich 	struct ieee80211_sub_if_data *sdata;
3244d2859df5SJanusz Dziedzic 	struct cfg80211_chan_def chandef;
3245164eb02dSSimon Wunderlich 
32464a199068SJohannes Berg 	/* for interface list, to avoid linking iflist_mtx and chanctx_mtx */
32474a199068SJohannes Berg 	ASSERT_RTNL();
32484a199068SJohannes Berg 
324934a3740dSJohannes Berg 	mutex_lock(&local->mtx);
3250164eb02dSSimon Wunderlich 	list_for_each_entry(sdata, &local->interfaces, list) {
325134a3740dSJohannes Berg 		/* it might be waiting for the local->mtx, but then
325234a3740dSJohannes Berg 		 * by the time it gets it, sdata->wdev.cac_started
325334a3740dSJohannes Berg 		 * will no longer be true
325434a3740dSJohannes Berg 		 */
325534a3740dSJohannes Berg 		cancel_delayed_work(&sdata->dfs_cac_timer_work);
3256164eb02dSSimon Wunderlich 
3257164eb02dSSimon Wunderlich 		if (sdata->wdev.cac_started) {
3258d2859df5SJanusz Dziedzic 			chandef = sdata->vif.bss_conf.chandef;
3259164eb02dSSimon Wunderlich 			ieee80211_vif_release_channel(sdata);
3260164eb02dSSimon Wunderlich 			cfg80211_cac_event(sdata->dev,
3261d2859df5SJanusz Dziedzic 					   &chandef,
3262164eb02dSSimon Wunderlich 					   NL80211_RADAR_CAC_ABORTED,
3263164eb02dSSimon Wunderlich 					   GFP_KERNEL);
3264164eb02dSSimon Wunderlich 		}
3265164eb02dSSimon Wunderlich 	}
326634a3740dSJohannes Berg 	mutex_unlock(&local->mtx);
3267164eb02dSSimon Wunderlich }
3268164eb02dSSimon Wunderlich 
3269164eb02dSSimon Wunderlich void ieee80211_dfs_radar_detected_work(struct work_struct *work)
3270164eb02dSSimon Wunderlich {
3271164eb02dSSimon Wunderlich 	struct ieee80211_local *local =
3272164eb02dSSimon Wunderlich 		container_of(work, struct ieee80211_local, radar_detected_work);
327384a3d1c9SJanusz Dziedzic 	struct cfg80211_chan_def chandef = local->hw.conf.chandef;
3274486cf4c0SMichal Kazior 	struct ieee80211_chanctx *ctx;
3275486cf4c0SMichal Kazior 	int num_chanctx = 0;
3276486cf4c0SMichal Kazior 
3277486cf4c0SMichal Kazior 	mutex_lock(&local->chanctx_mtx);
3278486cf4c0SMichal Kazior 	list_for_each_entry(ctx, &local->chanctx_list, list) {
3279486cf4c0SMichal Kazior 		if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
3280486cf4c0SMichal Kazior 			continue;
3281486cf4c0SMichal Kazior 
3282486cf4c0SMichal Kazior 		num_chanctx++;
3283486cf4c0SMichal Kazior 		chandef = ctx->conf.def;
3284486cf4c0SMichal Kazior 	}
3285486cf4c0SMichal Kazior 	mutex_unlock(&local->chanctx_mtx);
3286164eb02dSSimon Wunderlich 
32874a199068SJohannes Berg 	rtnl_lock();
3288164eb02dSSimon Wunderlich 	ieee80211_dfs_cac_cancel(local);
32894a199068SJohannes Berg 	rtnl_unlock();
3290164eb02dSSimon Wunderlich 
3291486cf4c0SMichal Kazior 	if (num_chanctx > 1)
3292486cf4c0SMichal Kazior 		/* XXX: multi-channel is not supported yet */
3293164eb02dSSimon Wunderlich 		WARN_ON(1);
329484a3d1c9SJanusz Dziedzic 	else
3295164eb02dSSimon Wunderlich 		cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
3296164eb02dSSimon Wunderlich }
3297164eb02dSSimon Wunderlich 
3298164eb02dSSimon Wunderlich void ieee80211_radar_detected(struct ieee80211_hw *hw)
3299164eb02dSSimon Wunderlich {
3300164eb02dSSimon Wunderlich 	struct ieee80211_local *local = hw_to_local(hw);
3301164eb02dSSimon Wunderlich 
3302164eb02dSSimon Wunderlich 	trace_api_radar_detected(local);
3303164eb02dSSimon Wunderlich 
33044a199068SJohannes Berg 	schedule_work(&local->radar_detected_work);
3305164eb02dSSimon Wunderlich }
3306164eb02dSSimon Wunderlich EXPORT_SYMBOL(ieee80211_radar_detected);
3307e6b7cde4SSimon Wunderlich 
3308e6b7cde4SSimon Wunderlich u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
3309e6b7cde4SSimon Wunderlich {
3310e6b7cde4SSimon Wunderlich 	u32 ret;
3311e6b7cde4SSimon Wunderlich 	int tmp;
3312e6b7cde4SSimon Wunderlich 
3313e6b7cde4SSimon Wunderlich 	switch (c->width) {
3314e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_20:
3315e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_20_NOHT;
3316e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3317e6b7cde4SSimon Wunderlich 		break;
3318e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_40:
3319e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_20;
3320e6b7cde4SSimon Wunderlich 		c->center_freq1 = c->chan->center_freq;
3321e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_40MHZ |
3322e6b7cde4SSimon Wunderlich 		      IEEE80211_STA_DISABLE_VHT;
3323e6b7cde4SSimon Wunderlich 		break;
3324e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_80:
3325e6b7cde4SSimon Wunderlich 		tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
3326e6b7cde4SSimon Wunderlich 		/* n_P40 */
3327e6b7cde4SSimon Wunderlich 		tmp /= 2;
3328e6b7cde4SSimon Wunderlich 		/* freq_P40 */
3329e6b7cde4SSimon Wunderlich 		c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
3330e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_40;
3331e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_VHT;
3332e6b7cde4SSimon Wunderlich 		break;
3333e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_80P80:
3334e6b7cde4SSimon Wunderlich 		c->center_freq2 = 0;
3335e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_80;
3336e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_80P80MHZ |
3337e6b7cde4SSimon Wunderlich 		      IEEE80211_STA_DISABLE_160MHZ;
3338e6b7cde4SSimon Wunderlich 		break;
3339e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_160:
3340e6b7cde4SSimon Wunderlich 		/* n_P20 */
3341e6b7cde4SSimon Wunderlich 		tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
3342e6b7cde4SSimon Wunderlich 		/* n_P80 */
3343e6b7cde4SSimon Wunderlich 		tmp /= 4;
3344e6b7cde4SSimon Wunderlich 		c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
3345e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_80;
3346e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_80P80MHZ |
3347e6b7cde4SSimon Wunderlich 		      IEEE80211_STA_DISABLE_160MHZ;
3348e6b7cde4SSimon Wunderlich 		break;
3349e6b7cde4SSimon Wunderlich 	default:
3350e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_20_NOHT:
3351e6b7cde4SSimon Wunderlich 		WARN_ON_ONCE(1);
3352e6b7cde4SSimon Wunderlich 		c->width = NL80211_CHAN_WIDTH_20_NOHT;
3353e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3354e6b7cde4SSimon Wunderlich 		break;
3355e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_5:
3356e6b7cde4SSimon Wunderlich 	case NL80211_CHAN_WIDTH_10:
3357e6b7cde4SSimon Wunderlich 		WARN_ON_ONCE(1);
3358e6b7cde4SSimon Wunderlich 		/* keep c->width */
3359e6b7cde4SSimon Wunderlich 		ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3360e6b7cde4SSimon Wunderlich 		break;
3361e6b7cde4SSimon Wunderlich 	}
3362e6b7cde4SSimon Wunderlich 
3363e6b7cde4SSimon Wunderlich 	WARN_ON_ONCE(!cfg80211_chandef_valid(c));
3364e6b7cde4SSimon Wunderlich 
3365e6b7cde4SSimon Wunderlich 	return ret;
3366e6b7cde4SSimon Wunderlich }
3367687da132SEmmanuel Grumbach 
3368687da132SEmmanuel Grumbach /*
3369687da132SEmmanuel Grumbach  * Returns true if smps_mode_new is strictly more restrictive than
3370687da132SEmmanuel Grumbach  * smps_mode_old.
3371687da132SEmmanuel Grumbach  */
3372687da132SEmmanuel Grumbach bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
3373687da132SEmmanuel Grumbach 				   enum ieee80211_smps_mode smps_mode_new)
3374687da132SEmmanuel Grumbach {
3375687da132SEmmanuel Grumbach 	if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
3376687da132SEmmanuel Grumbach 			 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
3377687da132SEmmanuel Grumbach 		return false;
3378687da132SEmmanuel Grumbach 
3379687da132SEmmanuel Grumbach 	switch (smps_mode_old) {
3380687da132SEmmanuel Grumbach 	case IEEE80211_SMPS_STATIC:
3381687da132SEmmanuel Grumbach 		return false;
3382687da132SEmmanuel Grumbach 	case IEEE80211_SMPS_DYNAMIC:
3383687da132SEmmanuel Grumbach 		return smps_mode_new == IEEE80211_SMPS_STATIC;
3384687da132SEmmanuel Grumbach 	case IEEE80211_SMPS_OFF:
3385687da132SEmmanuel Grumbach 		return smps_mode_new != IEEE80211_SMPS_OFF;
3386687da132SEmmanuel Grumbach 	default:
3387687da132SEmmanuel Grumbach 		WARN_ON(1);
3388687da132SEmmanuel Grumbach 	}
3389687da132SEmmanuel Grumbach 
3390687da132SEmmanuel Grumbach 	return false;
3391687da132SEmmanuel Grumbach }
3392c6da674aSChun-Yeow Yeoh 
3393c6da674aSChun-Yeow Yeoh int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
3394c6da674aSChun-Yeow Yeoh 			      struct cfg80211_csa_settings *csa_settings)
3395c6da674aSChun-Yeow Yeoh {
3396c6da674aSChun-Yeow Yeoh 	struct sk_buff *skb;
3397c6da674aSChun-Yeow Yeoh 	struct ieee80211_mgmt *mgmt;
3398c6da674aSChun-Yeow Yeoh 	struct ieee80211_local *local = sdata->local;
3399c6da674aSChun-Yeow Yeoh 	int freq;
34004c121fd6SJohannes Berg 	int hdr_len = offsetofend(struct ieee80211_mgmt,
34014c121fd6SJohannes Berg 				  u.action.u.chan_switch);
3402c6da674aSChun-Yeow Yeoh 	u8 *pos;
3403c6da674aSChun-Yeow Yeoh 
3404c6da674aSChun-Yeow Yeoh 	if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3405c6da674aSChun-Yeow Yeoh 	    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3406c6da674aSChun-Yeow Yeoh 		return -EOPNOTSUPP;
3407c6da674aSChun-Yeow Yeoh 
3408c6da674aSChun-Yeow Yeoh 	skb = dev_alloc_skb(local->tx_headroom + hdr_len +
3409c6da674aSChun-Yeow Yeoh 			    5 + /* channel switch announcement element */
3410c6da674aSChun-Yeow Yeoh 			    3 + /* secondary channel offset element */
341175d627d5SSimon Wunderlich 			    5 + /* wide bandwidth channel switch announcement */
3412c6da674aSChun-Yeow Yeoh 			    8); /* mesh channel switch parameters element */
3413c6da674aSChun-Yeow Yeoh 	if (!skb)
3414c6da674aSChun-Yeow Yeoh 		return -ENOMEM;
3415c6da674aSChun-Yeow Yeoh 
3416c6da674aSChun-Yeow Yeoh 	skb_reserve(skb, local->tx_headroom);
3417b080db58SJohannes Berg 	mgmt = skb_put_zero(skb, hdr_len);
3418c6da674aSChun-Yeow Yeoh 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3419c6da674aSChun-Yeow Yeoh 					  IEEE80211_STYPE_ACTION);
3420c6da674aSChun-Yeow Yeoh 
3421c6da674aSChun-Yeow Yeoh 	eth_broadcast_addr(mgmt->da);
3422c6da674aSChun-Yeow Yeoh 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
3423c6da674aSChun-Yeow Yeoh 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
3424c6da674aSChun-Yeow Yeoh 		memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
3425c6da674aSChun-Yeow Yeoh 	} else {
3426c6da674aSChun-Yeow Yeoh 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
3427c6da674aSChun-Yeow Yeoh 		memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
3428c6da674aSChun-Yeow Yeoh 	}
3429c6da674aSChun-Yeow Yeoh 	mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
3430c6da674aSChun-Yeow Yeoh 	mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
3431c6da674aSChun-Yeow Yeoh 	pos = skb_put(skb, 5);
3432c6da674aSChun-Yeow Yeoh 	*pos++ = WLAN_EID_CHANNEL_SWITCH;			/* EID */
3433c6da674aSChun-Yeow Yeoh 	*pos++ = 3;						/* IE length */
3434c6da674aSChun-Yeow Yeoh 	*pos++ = csa_settings->block_tx ? 1 : 0;		/* CSA mode */
3435c6da674aSChun-Yeow Yeoh 	freq = csa_settings->chandef.chan->center_freq;
3436c6da674aSChun-Yeow Yeoh 	*pos++ = ieee80211_frequency_to_channel(freq);		/* channel */
3437c6da674aSChun-Yeow Yeoh 	*pos++ = csa_settings->count;				/* count */
3438c6da674aSChun-Yeow Yeoh 
3439c6da674aSChun-Yeow Yeoh 	if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
3440c6da674aSChun-Yeow Yeoh 		enum nl80211_channel_type ch_type;
3441c6da674aSChun-Yeow Yeoh 
3442c6da674aSChun-Yeow Yeoh 		skb_put(skb, 3);
3443c6da674aSChun-Yeow Yeoh 		*pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;	/* EID */
3444c6da674aSChun-Yeow Yeoh 		*pos++ = 1;					/* IE length */
3445c6da674aSChun-Yeow Yeoh 		ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
3446c6da674aSChun-Yeow Yeoh 		if (ch_type == NL80211_CHAN_HT40PLUS)
3447c6da674aSChun-Yeow Yeoh 			*pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
3448c6da674aSChun-Yeow Yeoh 		else
3449c6da674aSChun-Yeow Yeoh 			*pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
3450c6da674aSChun-Yeow Yeoh 	}
3451c6da674aSChun-Yeow Yeoh 
3452c6da674aSChun-Yeow Yeoh 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
3453c6da674aSChun-Yeow Yeoh 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
3454c6da674aSChun-Yeow Yeoh 
3455c6da674aSChun-Yeow Yeoh 		skb_put(skb, 8);
3456c6da674aSChun-Yeow Yeoh 		*pos++ = WLAN_EID_CHAN_SWITCH_PARAM;		/* EID */
3457c6da674aSChun-Yeow Yeoh 		*pos++ = 6;					/* IE length */
3458c6da674aSChun-Yeow Yeoh 		*pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL;	/* Mesh TTL */
3459c6da674aSChun-Yeow Yeoh 		*pos = 0x00;	/* Mesh Flag: Tx Restrict, Initiator, Reason */
3460c6da674aSChun-Yeow Yeoh 		*pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
3461c6da674aSChun-Yeow Yeoh 		*pos++ |= csa_settings->block_tx ?
3462c6da674aSChun-Yeow Yeoh 			  WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
3463c6da674aSChun-Yeow Yeoh 		put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
3464c6da674aSChun-Yeow Yeoh 		pos += 2;
3465ca91dc97SChun-Yeow Yeoh 		put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
3466c6da674aSChun-Yeow Yeoh 		pos += 2;
3467c6da674aSChun-Yeow Yeoh 	}
3468c6da674aSChun-Yeow Yeoh 
346975d627d5SSimon Wunderlich 	if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 ||
347075d627d5SSimon Wunderlich 	    csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 ||
347175d627d5SSimon Wunderlich 	    csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) {
347275d627d5SSimon Wunderlich 		skb_put(skb, 5);
347375d627d5SSimon Wunderlich 		ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef);
347475d627d5SSimon Wunderlich 	}
347575d627d5SSimon Wunderlich 
3476c6da674aSChun-Yeow Yeoh 	ieee80211_tx_skb(sdata, skb);
3477c6da674aSChun-Yeow Yeoh 	return 0;
3478c6da674aSChun-Yeow Yeoh }
34792475b1ccSMax Stepanov 
34802475b1ccSMax Stepanov bool ieee80211_cs_valid(const struct ieee80211_cipher_scheme *cs)
34812475b1ccSMax Stepanov {
34822475b1ccSMax Stepanov 	return !(cs == NULL || cs->cipher == 0 ||
34832475b1ccSMax Stepanov 		 cs->hdr_len < cs->pn_len + cs->pn_off ||
34842475b1ccSMax Stepanov 		 cs->hdr_len <= cs->key_idx_off ||
34852475b1ccSMax Stepanov 		 cs->key_idx_shift > 7 ||
34862475b1ccSMax Stepanov 		 cs->key_idx_mask == 0);
34872475b1ccSMax Stepanov }
34882475b1ccSMax Stepanov 
34892475b1ccSMax Stepanov bool ieee80211_cs_list_valid(const struct ieee80211_cipher_scheme *cs, int n)
34902475b1ccSMax Stepanov {
34912475b1ccSMax Stepanov 	int i;
34922475b1ccSMax Stepanov 
34932475b1ccSMax Stepanov 	/* Ensure we have enough iftype bitmap space for all iftype values */
34942475b1ccSMax Stepanov 	WARN_ON((NUM_NL80211_IFTYPES / 8 + 1) > sizeof(cs[0].iftype));
34952475b1ccSMax Stepanov 
34962475b1ccSMax Stepanov 	for (i = 0; i < n; i++)
34972475b1ccSMax Stepanov 		if (!ieee80211_cs_valid(&cs[i]))
34982475b1ccSMax Stepanov 			return false;
34992475b1ccSMax Stepanov 
35002475b1ccSMax Stepanov 	return true;
35012475b1ccSMax Stepanov }
35022475b1ccSMax Stepanov 
35032475b1ccSMax Stepanov const struct ieee80211_cipher_scheme *
35042475b1ccSMax Stepanov ieee80211_cs_get(struct ieee80211_local *local, u32 cipher,
35052475b1ccSMax Stepanov 		 enum nl80211_iftype iftype)
35062475b1ccSMax Stepanov {
35072475b1ccSMax Stepanov 	const struct ieee80211_cipher_scheme *l = local->hw.cipher_schemes;
35082475b1ccSMax Stepanov 	int n = local->hw.n_cipher_schemes;
35092475b1ccSMax Stepanov 	int i;
35102475b1ccSMax Stepanov 	const struct ieee80211_cipher_scheme *cs = NULL;
35112475b1ccSMax Stepanov 
35122475b1ccSMax Stepanov 	for (i = 0; i < n; i++) {
35132475b1ccSMax Stepanov 		if (l[i].cipher == cipher) {
35142475b1ccSMax Stepanov 			cs = &l[i];
35152475b1ccSMax Stepanov 			break;
35162475b1ccSMax Stepanov 		}
35172475b1ccSMax Stepanov 	}
35182475b1ccSMax Stepanov 
35192475b1ccSMax Stepanov 	if (!cs || !(cs->iftype & BIT(iftype)))
35202475b1ccSMax Stepanov 		return NULL;
35212475b1ccSMax Stepanov 
35222475b1ccSMax Stepanov 	return cs;
35232475b1ccSMax Stepanov }
35242475b1ccSMax Stepanov 
35252475b1ccSMax Stepanov int ieee80211_cs_headroom(struct ieee80211_local *local,
35262475b1ccSMax Stepanov 			  struct cfg80211_crypto_settings *crypto,
35272475b1ccSMax Stepanov 			  enum nl80211_iftype iftype)
35282475b1ccSMax Stepanov {
35292475b1ccSMax Stepanov 	const struct ieee80211_cipher_scheme *cs;
35302475b1ccSMax Stepanov 	int headroom = IEEE80211_ENCRYPT_HEADROOM;
35312475b1ccSMax Stepanov 	int i;
35322475b1ccSMax Stepanov 
35332475b1ccSMax Stepanov 	for (i = 0; i < crypto->n_ciphers_pairwise; i++) {
35342475b1ccSMax Stepanov 		cs = ieee80211_cs_get(local, crypto->ciphers_pairwise[i],
35352475b1ccSMax Stepanov 				      iftype);
35362475b1ccSMax Stepanov 
35372475b1ccSMax Stepanov 		if (cs && headroom < cs->hdr_len)
35382475b1ccSMax Stepanov 			headroom = cs->hdr_len;
35392475b1ccSMax Stepanov 	}
35402475b1ccSMax Stepanov 
35412475b1ccSMax Stepanov 	cs = ieee80211_cs_get(local, crypto->cipher_group, iftype);
35422475b1ccSMax Stepanov 	if (cs && headroom < cs->hdr_len)
35432475b1ccSMax Stepanov 		headroom = cs->hdr_len;
35442475b1ccSMax Stepanov 
35452475b1ccSMax Stepanov 	return headroom;
35462475b1ccSMax Stepanov }
3547a7022e65SFelix Fietkau 
3548a7022e65SFelix Fietkau static bool
3549a7022e65SFelix Fietkau ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
3550a7022e65SFelix Fietkau {
3551a7022e65SFelix Fietkau 	s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
3552a7022e65SFelix Fietkau 	int skip;
3553a7022e65SFelix Fietkau 
3554a7022e65SFelix Fietkau 	if (end > 0)
3555a7022e65SFelix Fietkau 		return false;
3556a7022e65SFelix Fietkau 
3557519ee691SJanusz.Dziedzic@tieto.com 	/* One shot NOA  */
3558519ee691SJanusz.Dziedzic@tieto.com 	if (data->count[i] == 1)
3559519ee691SJanusz.Dziedzic@tieto.com 		return false;
3560519ee691SJanusz.Dziedzic@tieto.com 
3561519ee691SJanusz.Dziedzic@tieto.com 	if (data->desc[i].interval == 0)
3562519ee691SJanusz.Dziedzic@tieto.com 		return false;
3563519ee691SJanusz.Dziedzic@tieto.com 
3564a7022e65SFelix Fietkau 	/* End time is in the past, check for repetitions */
3565a7022e65SFelix Fietkau 	skip = DIV_ROUND_UP(-end, data->desc[i].interval);
3566a7022e65SFelix Fietkau 	if (data->count[i] < 255) {
3567a7022e65SFelix Fietkau 		if (data->count[i] <= skip) {
3568a7022e65SFelix Fietkau 			data->count[i] = 0;
3569a7022e65SFelix Fietkau 			return false;
3570a7022e65SFelix Fietkau 		}
3571a7022e65SFelix Fietkau 
3572a7022e65SFelix Fietkau 		data->count[i] -= skip;
3573a7022e65SFelix Fietkau 	}
3574a7022e65SFelix Fietkau 
3575a7022e65SFelix Fietkau 	data->desc[i].start += skip * data->desc[i].interval;
3576a7022e65SFelix Fietkau 
3577a7022e65SFelix Fietkau 	return true;
3578a7022e65SFelix Fietkau }
3579a7022e65SFelix Fietkau 
3580a7022e65SFelix Fietkau static bool
3581a7022e65SFelix Fietkau ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
3582a7022e65SFelix Fietkau 			     s32 *offset)
3583a7022e65SFelix Fietkau {
3584a7022e65SFelix Fietkau 	bool ret = false;
3585a7022e65SFelix Fietkau 	int i;
3586a7022e65SFelix Fietkau 
3587a7022e65SFelix Fietkau 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3588a7022e65SFelix Fietkau 		s32 cur;
3589a7022e65SFelix Fietkau 
3590a7022e65SFelix Fietkau 		if (!data->count[i])
3591a7022e65SFelix Fietkau 			continue;
3592a7022e65SFelix Fietkau 
3593a7022e65SFelix Fietkau 		if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
3594a7022e65SFelix Fietkau 			ret = true;
3595a7022e65SFelix Fietkau 
3596a7022e65SFelix Fietkau 		cur = data->desc[i].start - tsf;
3597a7022e65SFelix Fietkau 		if (cur > *offset)
3598a7022e65SFelix Fietkau 			continue;
3599a7022e65SFelix Fietkau 
3600a7022e65SFelix Fietkau 		cur = data->desc[i].start + data->desc[i].duration - tsf;
3601a7022e65SFelix Fietkau 		if (cur > *offset)
3602a7022e65SFelix Fietkau 			*offset = cur;
3603a7022e65SFelix Fietkau 	}
3604a7022e65SFelix Fietkau 
3605a7022e65SFelix Fietkau 	return ret;
3606a7022e65SFelix Fietkau }
3607a7022e65SFelix Fietkau 
3608a7022e65SFelix Fietkau static u32
3609a7022e65SFelix Fietkau ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
3610a7022e65SFelix Fietkau {
3611a7022e65SFelix Fietkau 	s32 offset = 0;
3612a7022e65SFelix Fietkau 	int tries = 0;
3613a7022e65SFelix Fietkau 	/*
3614a7022e65SFelix Fietkau 	 * arbitrary limit, used to avoid infinite loops when combined NoA
3615a7022e65SFelix Fietkau 	 * descriptors cover the full time period.
3616a7022e65SFelix Fietkau 	 */
3617a7022e65SFelix Fietkau 	int max_tries = 5;
3618a7022e65SFelix Fietkau 
3619a7022e65SFelix Fietkau 	ieee80211_extend_absent_time(data, tsf, &offset);
3620a7022e65SFelix Fietkau 	do {
3621a7022e65SFelix Fietkau 		if (!ieee80211_extend_absent_time(data, tsf, &offset))
3622a7022e65SFelix Fietkau 			break;
3623a7022e65SFelix Fietkau 
3624a7022e65SFelix Fietkau 		tries++;
3625a7022e65SFelix Fietkau 	} while (tries < max_tries);
3626a7022e65SFelix Fietkau 
3627a7022e65SFelix Fietkau 	return offset;
3628a7022e65SFelix Fietkau }
3629a7022e65SFelix Fietkau 
3630a7022e65SFelix Fietkau void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
3631a7022e65SFelix Fietkau {
3632a7022e65SFelix Fietkau 	u32 next_offset = BIT(31) - 1;
3633a7022e65SFelix Fietkau 	int i;
3634a7022e65SFelix Fietkau 
3635a7022e65SFelix Fietkau 	data->absent = 0;
3636a7022e65SFelix Fietkau 	data->has_next_tsf = false;
3637a7022e65SFelix Fietkau 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3638a7022e65SFelix Fietkau 		s32 start;
3639a7022e65SFelix Fietkau 
3640a7022e65SFelix Fietkau 		if (!data->count[i])
3641a7022e65SFelix Fietkau 			continue;
3642a7022e65SFelix Fietkau 
3643a7022e65SFelix Fietkau 		ieee80211_extend_noa_desc(data, tsf, i);
3644a7022e65SFelix Fietkau 		start = data->desc[i].start - tsf;
3645a7022e65SFelix Fietkau 		if (start <= 0)
3646a7022e65SFelix Fietkau 			data->absent |= BIT(i);
3647a7022e65SFelix Fietkau 
3648a7022e65SFelix Fietkau 		if (next_offset > start)
3649a7022e65SFelix Fietkau 			next_offset = start;
3650a7022e65SFelix Fietkau 
3651a7022e65SFelix Fietkau 		data->has_next_tsf = true;
3652a7022e65SFelix Fietkau 	}
3653a7022e65SFelix Fietkau 
3654a7022e65SFelix Fietkau 	if (data->absent)
3655a7022e65SFelix Fietkau 		next_offset = ieee80211_get_noa_absent_time(data, tsf);
3656a7022e65SFelix Fietkau 
3657a7022e65SFelix Fietkau 	data->next_tsf = tsf + next_offset;
3658a7022e65SFelix Fietkau }
3659a7022e65SFelix Fietkau EXPORT_SYMBOL(ieee80211_update_p2p_noa);
3660a7022e65SFelix Fietkau 
3661a7022e65SFelix Fietkau int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
3662a7022e65SFelix Fietkau 			    struct ieee80211_noa_data *data, u32 tsf)
3663a7022e65SFelix Fietkau {
3664a7022e65SFelix Fietkau 	int ret = 0;
3665a7022e65SFelix Fietkau 	int i;
3666a7022e65SFelix Fietkau 
3667a7022e65SFelix Fietkau 	memset(data, 0, sizeof(*data));
3668a7022e65SFelix Fietkau 
3669a7022e65SFelix Fietkau 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3670a7022e65SFelix Fietkau 		const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
3671a7022e65SFelix Fietkau 
3672a7022e65SFelix Fietkau 		if (!desc->count || !desc->duration)
3673a7022e65SFelix Fietkau 			continue;
3674a7022e65SFelix Fietkau 
3675a7022e65SFelix Fietkau 		data->count[i] = desc->count;
3676a7022e65SFelix Fietkau 		data->desc[i].start = le32_to_cpu(desc->start_time);
3677a7022e65SFelix Fietkau 		data->desc[i].duration = le32_to_cpu(desc->duration);
3678a7022e65SFelix Fietkau 		data->desc[i].interval = le32_to_cpu(desc->interval);
3679a7022e65SFelix Fietkau 
3680a7022e65SFelix Fietkau 		if (data->count[i] > 1 &&
3681a7022e65SFelix Fietkau 		    data->desc[i].interval < data->desc[i].duration)
3682a7022e65SFelix Fietkau 			continue;
3683a7022e65SFelix Fietkau 
3684a7022e65SFelix Fietkau 		ieee80211_extend_noa_desc(data, tsf, i);
3685a7022e65SFelix Fietkau 		ret++;
3686a7022e65SFelix Fietkau 	}
3687a7022e65SFelix Fietkau 
3688a7022e65SFelix Fietkau 	if (ret)
3689a7022e65SFelix Fietkau 		ieee80211_update_p2p_noa(data, tsf);
3690a7022e65SFelix Fietkau 
3691a7022e65SFelix Fietkau 	return ret;
3692a7022e65SFelix Fietkau }
3693a7022e65SFelix Fietkau EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
3694057d5f4bSThomas Pedersen 
3695057d5f4bSThomas Pedersen void ieee80211_recalc_dtim(struct ieee80211_local *local,
3696057d5f4bSThomas Pedersen 			   struct ieee80211_sub_if_data *sdata)
3697057d5f4bSThomas Pedersen {
3698057d5f4bSThomas Pedersen 	u64 tsf = drv_get_tsf(local, sdata);
3699057d5f4bSThomas Pedersen 	u64 dtim_count = 0;
3700057d5f4bSThomas Pedersen 	u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
3701057d5f4bSThomas Pedersen 	u8 dtim_period = sdata->vif.bss_conf.dtim_period;
3702057d5f4bSThomas Pedersen 	struct ps_data *ps;
3703057d5f4bSThomas Pedersen 	u8 bcns_from_dtim;
3704057d5f4bSThomas Pedersen 
3705057d5f4bSThomas Pedersen 	if (tsf == -1ULL || !beacon_int || !dtim_period)
3706057d5f4bSThomas Pedersen 		return;
3707057d5f4bSThomas Pedersen 
3708057d5f4bSThomas Pedersen 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
3709057d5f4bSThomas Pedersen 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
3710057d5f4bSThomas Pedersen 		if (!sdata->bss)
3711057d5f4bSThomas Pedersen 			return;
3712057d5f4bSThomas Pedersen 
3713057d5f4bSThomas Pedersen 		ps = &sdata->bss->ps;
3714057d5f4bSThomas Pedersen 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
3715057d5f4bSThomas Pedersen 		ps = &sdata->u.mesh.ps;
3716057d5f4bSThomas Pedersen 	} else {
3717057d5f4bSThomas Pedersen 		return;
3718057d5f4bSThomas Pedersen 	}
3719057d5f4bSThomas Pedersen 
3720057d5f4bSThomas Pedersen 	/*
3721057d5f4bSThomas Pedersen 	 * actually finds last dtim_count, mac80211 will update in
3722057d5f4bSThomas Pedersen 	 * __beacon_add_tim().
3723057d5f4bSThomas Pedersen 	 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
3724057d5f4bSThomas Pedersen 	 */
3725057d5f4bSThomas Pedersen 	do_div(tsf, beacon_int);
3726057d5f4bSThomas Pedersen 	bcns_from_dtim = do_div(tsf, dtim_period);
3727057d5f4bSThomas Pedersen 	/* just had a DTIM */
3728057d5f4bSThomas Pedersen 	if (!bcns_from_dtim)
3729057d5f4bSThomas Pedersen 		dtim_count = 0;
3730057d5f4bSThomas Pedersen 	else
3731057d5f4bSThomas Pedersen 		dtim_count = dtim_period - bcns_from_dtim;
3732057d5f4bSThomas Pedersen 
3733057d5f4bSThomas Pedersen 	ps->dtim_count = dtim_count;
3734057d5f4bSThomas Pedersen }
373573de86a3SLuciano Coelho 
373671e6195eSMichal Kazior static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
373771e6195eSMichal Kazior 					 struct ieee80211_chanctx *ctx)
373871e6195eSMichal Kazior {
373971e6195eSMichal Kazior 	struct ieee80211_sub_if_data *sdata;
374071e6195eSMichal Kazior 	u8 radar_detect = 0;
374171e6195eSMichal Kazior 
374271e6195eSMichal Kazior 	lockdep_assert_held(&local->chanctx_mtx);
374371e6195eSMichal Kazior 
374471e6195eSMichal Kazior 	if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
374571e6195eSMichal Kazior 		return 0;
374671e6195eSMichal Kazior 
374771e6195eSMichal Kazior 	list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
374871e6195eSMichal Kazior 		if (sdata->reserved_radar_required)
374971e6195eSMichal Kazior 			radar_detect |= BIT(sdata->reserved_chandef.width);
375071e6195eSMichal Kazior 
375171e6195eSMichal Kazior 	/*
375271e6195eSMichal Kazior 	 * An in-place reservation context should not have any assigned vifs
375371e6195eSMichal Kazior 	 * until it replaces the other context.
375471e6195eSMichal Kazior 	 */
375571e6195eSMichal Kazior 	WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
375671e6195eSMichal Kazior 		!list_empty(&ctx->assigned_vifs));
375771e6195eSMichal Kazior 
375871e6195eSMichal Kazior 	list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
375971e6195eSMichal Kazior 		if (sdata->radar_required)
376071e6195eSMichal Kazior 			radar_detect |= BIT(sdata->vif.bss_conf.chandef.width);
376171e6195eSMichal Kazior 
376271e6195eSMichal Kazior 	return radar_detect;
376371e6195eSMichal Kazior }
376471e6195eSMichal Kazior 
376573de86a3SLuciano Coelho int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
376673de86a3SLuciano Coelho 				 const struct cfg80211_chan_def *chandef,
376773de86a3SLuciano Coelho 				 enum ieee80211_chanctx_mode chanmode,
376873de86a3SLuciano Coelho 				 u8 radar_detect)
376973de86a3SLuciano Coelho {
377073de86a3SLuciano Coelho 	struct ieee80211_local *local = sdata->local;
377173de86a3SLuciano Coelho 	struct ieee80211_sub_if_data *sdata_iter;
377273de86a3SLuciano Coelho 	enum nl80211_iftype iftype = sdata->wdev.iftype;
377373de86a3SLuciano Coelho 	struct ieee80211_chanctx *ctx;
377473de86a3SLuciano Coelho 	int total = 1;
3775e227300cSPurushottam Kushwaha 	struct iface_combination_params params = {
3776e227300cSPurushottam Kushwaha 		.radar_detect = radar_detect,
3777e227300cSPurushottam Kushwaha 	};
377873de86a3SLuciano Coelho 
377973de86a3SLuciano Coelho 	lockdep_assert_held(&local->chanctx_mtx);
378073de86a3SLuciano Coelho 
378173de86a3SLuciano Coelho 	if (WARN_ON(hweight32(radar_detect) > 1))
378273de86a3SLuciano Coelho 		return -EINVAL;
378373de86a3SLuciano Coelho 
3784b6a55015SLuciano Coelho 	if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
3785b6a55015SLuciano Coelho 		    !chandef->chan))
378673de86a3SLuciano Coelho 		return -EINVAL;
378773de86a3SLuciano Coelho 
378873de86a3SLuciano Coelho 	if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
378973de86a3SLuciano Coelho 		return -EINVAL;
379073de86a3SLuciano Coelho 
3791ac668afeSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
3792ac668afeSJohannes Berg 	    sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
3793ac668afeSJohannes Berg 		/*
3794ac668afeSJohannes Berg 		 * always passing this is harmless, since it'll be the
3795ac668afeSJohannes Berg 		 * same value that cfg80211 finds if it finds the same
3796ac668afeSJohannes Berg 		 * interface ... and that's always allowed
3797ac668afeSJohannes Berg 		 */
3798ac668afeSJohannes Berg 		params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
3799ac668afeSJohannes Berg 	}
3800ac668afeSJohannes Berg 
380173de86a3SLuciano Coelho 	/* Always allow software iftypes */
380233d915d9SManikanta Pubbisetty 	if (local->hw.wiphy->software_iftypes & BIT(iftype) ||
380333d915d9SManikanta Pubbisetty 	    (iftype == NL80211_IFTYPE_AP_VLAN &&
380433d915d9SManikanta Pubbisetty 	     local->hw.wiphy->flags & WIPHY_FLAG_4ADDR_AP)) {
380573de86a3SLuciano Coelho 		if (radar_detect)
380673de86a3SLuciano Coelho 			return -EINVAL;
380773de86a3SLuciano Coelho 		return 0;
380873de86a3SLuciano Coelho 	}
380973de86a3SLuciano Coelho 
3810e227300cSPurushottam Kushwaha 	if (chandef)
3811e227300cSPurushottam Kushwaha 		params.num_different_channels = 1;
381273de86a3SLuciano Coelho 
381373de86a3SLuciano Coelho 	if (iftype != NL80211_IFTYPE_UNSPECIFIED)
3814e227300cSPurushottam Kushwaha 		params.iftype_num[iftype] = 1;
381573de86a3SLuciano Coelho 
381673de86a3SLuciano Coelho 	list_for_each_entry(ctx, &local->chanctx_list, list) {
38175bcae31dSMichal Kazior 		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
38185bcae31dSMichal Kazior 			continue;
3819e227300cSPurushottam Kushwaha 		params.radar_detect |=
3820e227300cSPurushottam Kushwaha 			ieee80211_chanctx_radar_detect(local, ctx);
382173de86a3SLuciano Coelho 		if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
3822e227300cSPurushottam Kushwaha 			params.num_different_channels++;
382373de86a3SLuciano Coelho 			continue;
382473de86a3SLuciano Coelho 		}
3825b6a55015SLuciano Coelho 		if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
382673de86a3SLuciano Coelho 		    cfg80211_chandef_compatible(chandef,
382773de86a3SLuciano Coelho 						&ctx->conf.def))
382873de86a3SLuciano Coelho 			continue;
3829e227300cSPurushottam Kushwaha 		params.num_different_channels++;
383073de86a3SLuciano Coelho 	}
383173de86a3SLuciano Coelho 
383273de86a3SLuciano Coelho 	list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
383373de86a3SLuciano Coelho 		struct wireless_dev *wdev_iter;
383473de86a3SLuciano Coelho 
383573de86a3SLuciano Coelho 		wdev_iter = &sdata_iter->wdev;
383673de86a3SLuciano Coelho 
383773de86a3SLuciano Coelho 		if (sdata_iter == sdata ||
38380f611d28SAndrei Otcheretianski 		    !ieee80211_sdata_running(sdata_iter) ||
383973de86a3SLuciano Coelho 		    local->hw.wiphy->software_iftypes & BIT(wdev_iter->iftype))
384073de86a3SLuciano Coelho 			continue;
384173de86a3SLuciano Coelho 
3842e227300cSPurushottam Kushwaha 		params.iftype_num[wdev_iter->iftype]++;
384373de86a3SLuciano Coelho 		total++;
384473de86a3SLuciano Coelho 	}
384573de86a3SLuciano Coelho 
3846e227300cSPurushottam Kushwaha 	if (total == 1 && !params.radar_detect)
384773de86a3SLuciano Coelho 		return 0;
384873de86a3SLuciano Coelho 
3849e227300cSPurushottam Kushwaha 	return cfg80211_check_combinations(local->hw.wiphy, &params);
385073de86a3SLuciano Coelho }
38516fa001bcSMichal Kazior 
38526fa001bcSMichal Kazior static void
38536fa001bcSMichal Kazior ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
38546fa001bcSMichal Kazior 			 void *data)
38556fa001bcSMichal Kazior {
38566fa001bcSMichal Kazior 	u32 *max_num_different_channels = data;
38576fa001bcSMichal Kazior 
38586fa001bcSMichal Kazior 	*max_num_different_channels = max(*max_num_different_channels,
38596fa001bcSMichal Kazior 					  c->num_different_channels);
38606fa001bcSMichal Kazior }
38616fa001bcSMichal Kazior 
38626fa001bcSMichal Kazior int ieee80211_max_num_channels(struct ieee80211_local *local)
38636fa001bcSMichal Kazior {
38646fa001bcSMichal Kazior 	struct ieee80211_sub_if_data *sdata;
38656fa001bcSMichal Kazior 	struct ieee80211_chanctx *ctx;
38666fa001bcSMichal Kazior 	u32 max_num_different_channels = 1;
38676fa001bcSMichal Kazior 	int err;
3868e227300cSPurushottam Kushwaha 	struct iface_combination_params params = {0};
38696fa001bcSMichal Kazior 
38706fa001bcSMichal Kazior 	lockdep_assert_held(&local->chanctx_mtx);
38716fa001bcSMichal Kazior 
38726fa001bcSMichal Kazior 	list_for_each_entry(ctx, &local->chanctx_list, list) {
38735bcae31dSMichal Kazior 		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
38745bcae31dSMichal Kazior 			continue;
38755bcae31dSMichal Kazior 
3876e227300cSPurushottam Kushwaha 		params.num_different_channels++;
38776fa001bcSMichal Kazior 
3878e227300cSPurushottam Kushwaha 		params.radar_detect |=
3879e227300cSPurushottam Kushwaha 			ieee80211_chanctx_radar_detect(local, ctx);
38806fa001bcSMichal Kazior 	}
38816fa001bcSMichal Kazior 
38826fa001bcSMichal Kazior 	list_for_each_entry_rcu(sdata, &local->interfaces, list)
3883e227300cSPurushottam Kushwaha 		params.iftype_num[sdata->wdev.iftype]++;
38846fa001bcSMichal Kazior 
3885e227300cSPurushottam Kushwaha 	err = cfg80211_iter_combinations(local->hw.wiphy, &params,
3886e227300cSPurushottam Kushwaha 					 ieee80211_iter_max_chans,
38876fa001bcSMichal Kazior 					 &max_num_different_channels);
38886fa001bcSMichal Kazior 	if (err < 0)
38896fa001bcSMichal Kazior 		return err;
38906fa001bcSMichal Kazior 
38916fa001bcSMichal Kazior 	return max_num_different_channels;
38926fa001bcSMichal Kazior }
389340b861a0SArik Nemtsov 
389440b861a0SArik Nemtsov u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
389540b861a0SArik Nemtsov {
389640b861a0SArik Nemtsov 	*buf++ = WLAN_EID_VENDOR_SPECIFIC;
389740b861a0SArik Nemtsov 	*buf++ = 7; /* len */
389840b861a0SArik Nemtsov 	*buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
389940b861a0SArik Nemtsov 	*buf++ = 0x50;
390040b861a0SArik Nemtsov 	*buf++ = 0xf2;
390140b861a0SArik Nemtsov 	*buf++ = 2; /* WME */
390240b861a0SArik Nemtsov 	*buf++ = 0; /* WME info */
390340b861a0SArik Nemtsov 	*buf++ = 1; /* WME ver */
390440b861a0SArik Nemtsov 	*buf++ = qosinfo; /* U-APSD no in use */
390540b861a0SArik Nemtsov 
390640b861a0SArik Nemtsov 	return buf;
390740b861a0SArik Nemtsov }
3908ba8c3d6fSFelix Fietkau 
3909f2ac7e30SMichal Kazior void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
3910f2ac7e30SMichal Kazior 			     unsigned long *frame_cnt,
3911f2ac7e30SMichal Kazior 			     unsigned long *byte_cnt)
3912f2ac7e30SMichal Kazior {
3913f2ac7e30SMichal Kazior 	struct txq_info *txqi = to_txq_info(txq);
3914bb42f2d1SToke Høiland-Jørgensen 	u32 frag_cnt = 0, frag_bytes = 0;
3915bb42f2d1SToke Høiland-Jørgensen 	struct sk_buff *skb;
3916bb42f2d1SToke Høiland-Jørgensen 
3917bb42f2d1SToke Høiland-Jørgensen 	skb_queue_walk(&txqi->frags, skb) {
3918bb42f2d1SToke Høiland-Jørgensen 		frag_cnt++;
3919bb42f2d1SToke Høiland-Jørgensen 		frag_bytes += skb->len;
3920bb42f2d1SToke Høiland-Jørgensen 	}
3921f2ac7e30SMichal Kazior 
3922f2ac7e30SMichal Kazior 	if (frame_cnt)
3923bb42f2d1SToke Høiland-Jørgensen 		*frame_cnt = txqi->tin.backlog_packets + frag_cnt;
3924f2ac7e30SMichal Kazior 
3925f2ac7e30SMichal Kazior 	if (byte_cnt)
3926bb42f2d1SToke Høiland-Jørgensen 		*byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
3927f2ac7e30SMichal Kazior }
3928f2ac7e30SMichal Kazior EXPORT_SYMBOL(ieee80211_txq_get_depth);
3929f438ceb8SEmmanuel Grumbach 
3930f438ceb8SEmmanuel Grumbach const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
3931f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
3932f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
3933f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
3934f438ceb8SEmmanuel Grumbach 	IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
3935f438ceb8SEmmanuel Grumbach };
3936