xref: /openbmc/linux/net/mac80211/util.c (revision 2a12187d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2002-2005, Instant802 Networks, Inc.
4  * Copyright 2005-2006, Devicescape Software, Inc.
5  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
6  * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
7  * Copyright 2013-2014  Intel Mobile Communications GmbH
8  * Copyright (C) 2015-2017	Intel Deutschland GmbH
9  * Copyright (C) 2018-2022 Intel Corporation
10  *
11  * utilities for mac80211
12  */
13 
14 #include <net/mac80211.h>
15 #include <linux/netdevice.h>
16 #include <linux/export.h>
17 #include <linux/types.h>
18 #include <linux/slab.h>
19 #include <linux/skbuff.h>
20 #include <linux/etherdevice.h>
21 #include <linux/if_arp.h>
22 #include <linux/bitmap.h>
23 #include <linux/crc32.h>
24 #include <net/net_namespace.h>
25 #include <net/cfg80211.h>
26 #include <net/rtnetlink.h>
27 
28 #include "ieee80211_i.h"
29 #include "driver-ops.h"
30 #include "rate.h"
31 #include "mesh.h"
32 #include "wme.h"
33 #include "led.h"
34 #include "wep.h"
35 
36 /* privid for wiphys to determine whether they belong to us or not */
37 const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
38 
39 struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
40 {
41 	struct ieee80211_local *local;
42 
43 	local = wiphy_priv(wiphy);
44 	return &local->hw;
45 }
46 EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
47 
48 u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
49 			enum nl80211_iftype type)
50 {
51 	__le16 fc = hdr->frame_control;
52 
53 	if (ieee80211_is_data(fc)) {
54 		if (len < 24) /* drop incorrect hdr len (data) */
55 			return NULL;
56 
57 		if (ieee80211_has_a4(fc))
58 			return NULL;
59 		if (ieee80211_has_tods(fc))
60 			return hdr->addr1;
61 		if (ieee80211_has_fromds(fc))
62 			return hdr->addr2;
63 
64 		return hdr->addr3;
65 	}
66 
67 	if (ieee80211_is_s1g_beacon(fc)) {
68 		struct ieee80211_ext *ext = (void *) hdr;
69 
70 		return ext->u.s1g_beacon.sa;
71 	}
72 
73 	if (ieee80211_is_mgmt(fc)) {
74 		if (len < 24) /* drop incorrect hdr len (mgmt) */
75 			return NULL;
76 		return hdr->addr3;
77 	}
78 
79 	if (ieee80211_is_ctl(fc)) {
80 		if (ieee80211_is_pspoll(fc))
81 			return hdr->addr1;
82 
83 		if (ieee80211_is_back_req(fc)) {
84 			switch (type) {
85 			case NL80211_IFTYPE_STATION:
86 				return hdr->addr2;
87 			case NL80211_IFTYPE_AP:
88 			case NL80211_IFTYPE_AP_VLAN:
89 				return hdr->addr1;
90 			default:
91 				break; /* fall through to the return */
92 			}
93 		}
94 	}
95 
96 	return NULL;
97 }
98 EXPORT_SYMBOL(ieee80211_get_bssid);
99 
100 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
101 {
102 	struct sk_buff *skb;
103 	struct ieee80211_hdr *hdr;
104 
105 	skb_queue_walk(&tx->skbs, skb) {
106 		hdr = (struct ieee80211_hdr *) skb->data;
107 		hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
108 	}
109 }
110 
111 int ieee80211_frame_duration(enum nl80211_band band, size_t len,
112 			     int rate, int erp, int short_preamble,
113 			     int shift)
114 {
115 	int dur;
116 
117 	/* calculate duration (in microseconds, rounded up to next higher
118 	 * integer if it includes a fractional microsecond) to send frame of
119 	 * len bytes (does not include FCS) at the given rate. Duration will
120 	 * also include SIFS.
121 	 *
122 	 * rate is in 100 kbps, so divident is multiplied by 10 in the
123 	 * DIV_ROUND_UP() operations.
124 	 *
125 	 * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
126 	 * is assumed to be 0 otherwise.
127 	 */
128 
129 	if (band == NL80211_BAND_5GHZ || erp) {
130 		/*
131 		 * OFDM:
132 		 *
133 		 * N_DBPS = DATARATE x 4
134 		 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
135 		 *	(16 = SIGNAL time, 6 = tail bits)
136 		 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
137 		 *
138 		 * T_SYM = 4 usec
139 		 * 802.11a - 18.5.2: aSIFSTime = 16 usec
140 		 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
141 		 *	signal ext = 6 usec
142 		 */
143 		dur = 16; /* SIFS + signal ext */
144 		dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
145 		dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
146 
147 		/* IEEE 802.11-2012 18.3.2.4: all values above are:
148 		 *  * times 4 for 5 MHz
149 		 *  * times 2 for 10 MHz
150 		 */
151 		dur *= 1 << shift;
152 
153 		/* rates should already consider the channel bandwidth,
154 		 * don't apply divisor again.
155 		 */
156 		dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
157 					4 * rate); /* T_SYM x N_SYM */
158 	} else {
159 		/*
160 		 * 802.11b or 802.11g with 802.11b compatibility:
161 		 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
162 		 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
163 		 *
164 		 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
165 		 * aSIFSTime = 10 usec
166 		 * aPreambleLength = 144 usec or 72 usec with short preamble
167 		 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
168 		 */
169 		dur = 10; /* aSIFSTime = 10 usec */
170 		dur += short_preamble ? (72 + 24) : (144 + 48);
171 
172 		dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
173 	}
174 
175 	return dur;
176 }
177 
178 /* Exported duration function for driver use */
179 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
180 					struct ieee80211_vif *vif,
181 					enum nl80211_band band,
182 					size_t frame_len,
183 					struct ieee80211_rate *rate)
184 {
185 	struct ieee80211_sub_if_data *sdata;
186 	u16 dur;
187 	int erp, shift = 0;
188 	bool short_preamble = false;
189 
190 	erp = 0;
191 	if (vif) {
192 		sdata = vif_to_sdata(vif);
193 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
194 		if (sdata->deflink.operating_11g_mode)
195 			erp = rate->flags & IEEE80211_RATE_ERP_G;
196 		shift = ieee80211_vif_get_shift(vif);
197 	}
198 
199 	dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
200 				       short_preamble, shift);
201 
202 	return cpu_to_le16(dur);
203 }
204 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
205 
206 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
207 			      struct ieee80211_vif *vif, size_t frame_len,
208 			      const struct ieee80211_tx_info *frame_txctl)
209 {
210 	struct ieee80211_local *local = hw_to_local(hw);
211 	struct ieee80211_rate *rate;
212 	struct ieee80211_sub_if_data *sdata;
213 	bool short_preamble;
214 	int erp, shift = 0, bitrate;
215 	u16 dur;
216 	struct ieee80211_supported_band *sband;
217 
218 	sband = local->hw.wiphy->bands[frame_txctl->band];
219 
220 	short_preamble = false;
221 
222 	rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
223 
224 	erp = 0;
225 	if (vif) {
226 		sdata = vif_to_sdata(vif);
227 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
228 		if (sdata->deflink.operating_11g_mode)
229 			erp = rate->flags & IEEE80211_RATE_ERP_G;
230 		shift = ieee80211_vif_get_shift(vif);
231 	}
232 
233 	bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
234 
235 	/* CTS duration */
236 	dur = ieee80211_frame_duration(sband->band, 10, bitrate,
237 				       erp, short_preamble, shift);
238 	/* Data frame duration */
239 	dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
240 					erp, short_preamble, shift);
241 	/* ACK duration */
242 	dur += ieee80211_frame_duration(sband->band, 10, bitrate,
243 					erp, short_preamble, shift);
244 
245 	return cpu_to_le16(dur);
246 }
247 EXPORT_SYMBOL(ieee80211_rts_duration);
248 
249 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
250 				    struct ieee80211_vif *vif,
251 				    size_t frame_len,
252 				    const struct ieee80211_tx_info *frame_txctl)
253 {
254 	struct ieee80211_local *local = hw_to_local(hw);
255 	struct ieee80211_rate *rate;
256 	struct ieee80211_sub_if_data *sdata;
257 	bool short_preamble;
258 	int erp, shift = 0, bitrate;
259 	u16 dur;
260 	struct ieee80211_supported_band *sband;
261 
262 	sband = local->hw.wiphy->bands[frame_txctl->band];
263 
264 	short_preamble = false;
265 
266 	rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
267 	erp = 0;
268 	if (vif) {
269 		sdata = vif_to_sdata(vif);
270 		short_preamble = sdata->vif.bss_conf.use_short_preamble;
271 		if (sdata->deflink.operating_11g_mode)
272 			erp = rate->flags & IEEE80211_RATE_ERP_G;
273 		shift = ieee80211_vif_get_shift(vif);
274 	}
275 
276 	bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
277 
278 	/* Data frame duration */
279 	dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
280 				       erp, short_preamble, shift);
281 	if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
282 		/* ACK duration */
283 		dur += ieee80211_frame_duration(sband->band, 10, bitrate,
284 						erp, short_preamble, shift);
285 	}
286 
287 	return cpu_to_le16(dur);
288 }
289 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
290 
291 static void wake_tx_push_queue(struct ieee80211_local *local,
292 			       struct ieee80211_sub_if_data *sdata,
293 			       struct ieee80211_txq *queue)
294 {
295 	int q = sdata->vif.hw_queue[queue->ac];
296 	struct ieee80211_tx_control control = {
297 		.sta = queue->sta,
298 	};
299 	struct sk_buff *skb;
300 	unsigned long flags;
301 	bool q_stopped;
302 
303 	while (1) {
304 		spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
305 		q_stopped = local->queue_stop_reasons[q];
306 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
307 
308 		if (q_stopped)
309 			break;
310 
311 		skb = ieee80211_tx_dequeue(&local->hw, queue);
312 		if (!skb)
313 			break;
314 
315 		drv_tx(local, &control, skb);
316 	}
317 }
318 
319 /* wake_tx_queue handler for driver not implementing a custom one*/
320 void ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
321 				    struct ieee80211_txq *txq)
322 {
323 	struct ieee80211_local *local = hw_to_local(hw);
324 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
325 	struct ieee80211_txq *queue;
326 
327 	/* Use ieee80211_next_txq() for airtime fairness accounting */
328 	ieee80211_txq_schedule_start(hw, txq->ac);
329 	while ((queue = ieee80211_next_txq(hw, txq->ac))) {
330 		wake_tx_push_queue(local, sdata, queue);
331 		ieee80211_return_txq(hw, queue, false);
332 	}
333 	ieee80211_txq_schedule_end(hw, txq->ac);
334 }
335 EXPORT_SYMBOL(ieee80211_handle_wake_tx_queue);
336 
337 static void __ieee80211_wake_txqs(struct ieee80211_sub_if_data *sdata, int ac)
338 {
339 	struct ieee80211_local *local = sdata->local;
340 	struct ieee80211_vif *vif = &sdata->vif;
341 	struct fq *fq = &local->fq;
342 	struct ps_data *ps = NULL;
343 	struct txq_info *txqi;
344 	struct sta_info *sta;
345 	int i;
346 
347 	local_bh_disable();
348 	spin_lock(&fq->lock);
349 
350 	sdata->vif.txqs_stopped[ac] = false;
351 
352 	if (!test_bit(SDATA_STATE_RUNNING, &sdata->state))
353 		goto out;
354 
355 	if (sdata->vif.type == NL80211_IFTYPE_AP)
356 		ps = &sdata->bss->ps;
357 
358 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
359 		if (sdata != sta->sdata)
360 			continue;
361 
362 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
363 			struct ieee80211_txq *txq = sta->sta.txq[i];
364 
365 			if (!txq)
366 				continue;
367 
368 			txqi = to_txq_info(txq);
369 
370 			if (ac != txq->ac)
371 				continue;
372 
373 			if (!test_and_clear_bit(IEEE80211_TXQ_STOP_NETIF_TX,
374 						&txqi->flags))
375 				continue;
376 
377 			spin_unlock(&fq->lock);
378 			drv_wake_tx_queue(local, txqi);
379 			spin_lock(&fq->lock);
380 		}
381 	}
382 
383 	if (!vif->txq)
384 		goto out;
385 
386 	txqi = to_txq_info(vif->txq);
387 
388 	if (!test_and_clear_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags) ||
389 	    (ps && atomic_read(&ps->num_sta_ps)) || ac != vif->txq->ac)
390 		goto out;
391 
392 	spin_unlock(&fq->lock);
393 
394 	drv_wake_tx_queue(local, txqi);
395 	local_bh_enable();
396 	return;
397 out:
398 	spin_unlock(&fq->lock);
399 	local_bh_enable();
400 }
401 
402 static void
403 __releases(&local->queue_stop_reason_lock)
404 __acquires(&local->queue_stop_reason_lock)
405 _ieee80211_wake_txqs(struct ieee80211_local *local, unsigned long *flags)
406 {
407 	struct ieee80211_sub_if_data *sdata;
408 	int n_acs = IEEE80211_NUM_ACS;
409 	int i;
410 
411 	rcu_read_lock();
412 
413 	if (local->hw.queues < IEEE80211_NUM_ACS)
414 		n_acs = 1;
415 
416 	for (i = 0; i < local->hw.queues; i++) {
417 		if (local->queue_stop_reasons[i])
418 			continue;
419 
420 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, *flags);
421 		list_for_each_entry_rcu(sdata, &local->interfaces, list) {
422 			int ac;
423 
424 			for (ac = 0; ac < n_acs; ac++) {
425 				int ac_queue = sdata->vif.hw_queue[ac];
426 
427 				if (ac_queue == i ||
428 				    sdata->vif.cab_queue == i)
429 					__ieee80211_wake_txqs(sdata, ac);
430 			}
431 		}
432 		spin_lock_irqsave(&local->queue_stop_reason_lock, *flags);
433 	}
434 
435 	rcu_read_unlock();
436 }
437 
438 void ieee80211_wake_txqs(struct tasklet_struct *t)
439 {
440 	struct ieee80211_local *local = from_tasklet(local, t,
441 						     wake_txqs_tasklet);
442 	unsigned long flags;
443 
444 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
445 	_ieee80211_wake_txqs(local, &flags);
446 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
447 }
448 
449 static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
450 				   enum queue_stop_reason reason,
451 				   bool refcounted,
452 				   unsigned long *flags)
453 {
454 	struct ieee80211_local *local = hw_to_local(hw);
455 
456 	trace_wake_queue(local, queue, reason);
457 
458 	if (WARN_ON(queue >= hw->queues))
459 		return;
460 
461 	if (!test_bit(reason, &local->queue_stop_reasons[queue]))
462 		return;
463 
464 	if (!refcounted) {
465 		local->q_stop_reasons[queue][reason] = 0;
466 	} else {
467 		local->q_stop_reasons[queue][reason]--;
468 		if (WARN_ON(local->q_stop_reasons[queue][reason] < 0))
469 			local->q_stop_reasons[queue][reason] = 0;
470 	}
471 
472 	if (local->q_stop_reasons[queue][reason] == 0)
473 		__clear_bit(reason, &local->queue_stop_reasons[queue]);
474 
475 	if (local->queue_stop_reasons[queue] != 0)
476 		/* someone still has this queue stopped */
477 		return;
478 
479 	if (!skb_queue_empty(&local->pending[queue]))
480 		tasklet_schedule(&local->tx_pending_tasklet);
481 
482 	/*
483 	 * Calling _ieee80211_wake_txqs here can be a problem because it may
484 	 * release queue_stop_reason_lock which has been taken by
485 	 * __ieee80211_wake_queue's caller. It is certainly not very nice to
486 	 * release someone's lock, but it is fine because all the callers of
487 	 * __ieee80211_wake_queue call it right before releasing the lock.
488 	 */
489 	if (reason == IEEE80211_QUEUE_STOP_REASON_DRIVER)
490 		tasklet_schedule(&local->wake_txqs_tasklet);
491 	else
492 		_ieee80211_wake_txqs(local, flags);
493 }
494 
495 void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
496 				    enum queue_stop_reason reason,
497 				    bool refcounted)
498 {
499 	struct ieee80211_local *local = hw_to_local(hw);
500 	unsigned long flags;
501 
502 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
503 	__ieee80211_wake_queue(hw, queue, reason, refcounted, &flags);
504 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
505 }
506 
507 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
508 {
509 	ieee80211_wake_queue_by_reason(hw, queue,
510 				       IEEE80211_QUEUE_STOP_REASON_DRIVER,
511 				       false);
512 }
513 EXPORT_SYMBOL(ieee80211_wake_queue);
514 
515 static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
516 				   enum queue_stop_reason reason,
517 				   bool refcounted)
518 {
519 	struct ieee80211_local *local = hw_to_local(hw);
520 	struct ieee80211_sub_if_data *sdata;
521 	int n_acs = IEEE80211_NUM_ACS;
522 
523 	trace_stop_queue(local, queue, reason);
524 
525 	if (WARN_ON(queue >= hw->queues))
526 		return;
527 
528 	if (!refcounted)
529 		local->q_stop_reasons[queue][reason] = 1;
530 	else
531 		local->q_stop_reasons[queue][reason]++;
532 
533 	if (__test_and_set_bit(reason, &local->queue_stop_reasons[queue]))
534 		return;
535 
536 	if (local->hw.queues < IEEE80211_NUM_ACS)
537 		n_acs = 1;
538 
539 	rcu_read_lock();
540 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
541 		int ac;
542 
543 		if (!sdata->dev)
544 			continue;
545 
546 		for (ac = 0; ac < n_acs; ac++) {
547 			if (sdata->vif.hw_queue[ac] == queue ||
548 			    sdata->vif.cab_queue == queue) {
549 				spin_lock(&local->fq.lock);
550 				sdata->vif.txqs_stopped[ac] = true;
551 				spin_unlock(&local->fq.lock);
552 			}
553 		}
554 	}
555 	rcu_read_unlock();
556 }
557 
558 void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
559 				    enum queue_stop_reason reason,
560 				    bool refcounted)
561 {
562 	struct ieee80211_local *local = hw_to_local(hw);
563 	unsigned long flags;
564 
565 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
566 	__ieee80211_stop_queue(hw, queue, reason, refcounted);
567 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
568 }
569 
570 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
571 {
572 	ieee80211_stop_queue_by_reason(hw, queue,
573 				       IEEE80211_QUEUE_STOP_REASON_DRIVER,
574 				       false);
575 }
576 EXPORT_SYMBOL(ieee80211_stop_queue);
577 
578 void ieee80211_add_pending_skb(struct ieee80211_local *local,
579 			       struct sk_buff *skb)
580 {
581 	struct ieee80211_hw *hw = &local->hw;
582 	unsigned long flags;
583 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
584 	int queue = info->hw_queue;
585 
586 	if (WARN_ON(!info->control.vif)) {
587 		ieee80211_free_txskb(&local->hw, skb);
588 		return;
589 	}
590 
591 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
592 	__ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
593 			       false);
594 	__skb_queue_tail(&local->pending[queue], skb);
595 	__ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
596 			       false, &flags);
597 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
598 }
599 
600 void ieee80211_add_pending_skbs(struct ieee80211_local *local,
601 				struct sk_buff_head *skbs)
602 {
603 	struct ieee80211_hw *hw = &local->hw;
604 	struct sk_buff *skb;
605 	unsigned long flags;
606 	int queue, i;
607 
608 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
609 	while ((skb = skb_dequeue(skbs))) {
610 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
611 
612 		if (WARN_ON(!info->control.vif)) {
613 			ieee80211_free_txskb(&local->hw, skb);
614 			continue;
615 		}
616 
617 		queue = info->hw_queue;
618 
619 		__ieee80211_stop_queue(hw, queue,
620 				IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
621 				false);
622 
623 		__skb_queue_tail(&local->pending[queue], skb);
624 	}
625 
626 	for (i = 0; i < hw->queues; i++)
627 		__ieee80211_wake_queue(hw, i,
628 			IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
629 			false, &flags);
630 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
631 }
632 
633 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
634 				     unsigned long queues,
635 				     enum queue_stop_reason reason,
636 				     bool refcounted)
637 {
638 	struct ieee80211_local *local = hw_to_local(hw);
639 	unsigned long flags;
640 	int i;
641 
642 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
643 
644 	for_each_set_bit(i, &queues, hw->queues)
645 		__ieee80211_stop_queue(hw, i, reason, refcounted);
646 
647 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
648 }
649 
650 void ieee80211_stop_queues(struct ieee80211_hw *hw)
651 {
652 	ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
653 					IEEE80211_QUEUE_STOP_REASON_DRIVER,
654 					false);
655 }
656 EXPORT_SYMBOL(ieee80211_stop_queues);
657 
658 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
659 {
660 	struct ieee80211_local *local = hw_to_local(hw);
661 	unsigned long flags;
662 	int ret;
663 
664 	if (WARN_ON(queue >= hw->queues))
665 		return true;
666 
667 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
668 	ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
669 		       &local->queue_stop_reasons[queue]);
670 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
671 	return ret;
672 }
673 EXPORT_SYMBOL(ieee80211_queue_stopped);
674 
675 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
676 				     unsigned long queues,
677 				     enum queue_stop_reason reason,
678 				     bool refcounted)
679 {
680 	struct ieee80211_local *local = hw_to_local(hw);
681 	unsigned long flags;
682 	int i;
683 
684 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
685 
686 	for_each_set_bit(i, &queues, hw->queues)
687 		__ieee80211_wake_queue(hw, i, reason, refcounted, &flags);
688 
689 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
690 }
691 
692 void ieee80211_wake_queues(struct ieee80211_hw *hw)
693 {
694 	ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
695 					IEEE80211_QUEUE_STOP_REASON_DRIVER,
696 					false);
697 }
698 EXPORT_SYMBOL(ieee80211_wake_queues);
699 
700 static unsigned int
701 ieee80211_get_vif_queues(struct ieee80211_local *local,
702 			 struct ieee80211_sub_if_data *sdata)
703 {
704 	unsigned int queues;
705 
706 	if (sdata && ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
707 		int ac;
708 
709 		queues = 0;
710 
711 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
712 			queues |= BIT(sdata->vif.hw_queue[ac]);
713 		if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
714 			queues |= BIT(sdata->vif.cab_queue);
715 	} else {
716 		/* all queues */
717 		queues = BIT(local->hw.queues) - 1;
718 	}
719 
720 	return queues;
721 }
722 
723 void __ieee80211_flush_queues(struct ieee80211_local *local,
724 			      struct ieee80211_sub_if_data *sdata,
725 			      unsigned int queues, bool drop)
726 {
727 	if (!local->ops->flush)
728 		return;
729 
730 	/*
731 	 * If no queue was set, or if the HW doesn't support
732 	 * IEEE80211_HW_QUEUE_CONTROL - flush all queues
733 	 */
734 	if (!queues || !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
735 		queues = ieee80211_get_vif_queues(local, sdata);
736 
737 	ieee80211_stop_queues_by_reason(&local->hw, queues,
738 					IEEE80211_QUEUE_STOP_REASON_FLUSH,
739 					false);
740 
741 	drv_flush(local, sdata, queues, drop);
742 
743 	ieee80211_wake_queues_by_reason(&local->hw, queues,
744 					IEEE80211_QUEUE_STOP_REASON_FLUSH,
745 					false);
746 }
747 
748 void ieee80211_flush_queues(struct ieee80211_local *local,
749 			    struct ieee80211_sub_if_data *sdata, bool drop)
750 {
751 	__ieee80211_flush_queues(local, sdata, 0, drop);
752 }
753 
754 void ieee80211_stop_vif_queues(struct ieee80211_local *local,
755 			       struct ieee80211_sub_if_data *sdata,
756 			       enum queue_stop_reason reason)
757 {
758 	ieee80211_stop_queues_by_reason(&local->hw,
759 					ieee80211_get_vif_queues(local, sdata),
760 					reason, true);
761 }
762 
763 void ieee80211_wake_vif_queues(struct ieee80211_local *local,
764 			       struct ieee80211_sub_if_data *sdata,
765 			       enum queue_stop_reason reason)
766 {
767 	ieee80211_wake_queues_by_reason(&local->hw,
768 					ieee80211_get_vif_queues(local, sdata),
769 					reason, true);
770 }
771 
772 static void __iterate_interfaces(struct ieee80211_local *local,
773 				 u32 iter_flags,
774 				 void (*iterator)(void *data, u8 *mac,
775 						  struct ieee80211_vif *vif),
776 				 void *data)
777 {
778 	struct ieee80211_sub_if_data *sdata;
779 	bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE;
780 
781 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
782 		switch (sdata->vif.type) {
783 		case NL80211_IFTYPE_MONITOR:
784 			if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
785 				continue;
786 			break;
787 		case NL80211_IFTYPE_AP_VLAN:
788 			continue;
789 		default:
790 			break;
791 		}
792 		if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
793 		    active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
794 			continue;
795 		if ((iter_flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) &&
796 		    !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
797 			continue;
798 		if (ieee80211_sdata_running(sdata) || !active_only)
799 			iterator(data, sdata->vif.addr,
800 				 &sdata->vif);
801 	}
802 
803 	sdata = rcu_dereference_check(local->monitor_sdata,
804 				      lockdep_is_held(&local->iflist_mtx) ||
805 				      lockdep_is_held(&local->hw.wiphy->mtx));
806 	if (sdata &&
807 	    (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only ||
808 	     sdata->flags & IEEE80211_SDATA_IN_DRIVER))
809 		iterator(data, sdata->vif.addr, &sdata->vif);
810 }
811 
812 void ieee80211_iterate_interfaces(
813 	struct ieee80211_hw *hw, u32 iter_flags,
814 	void (*iterator)(void *data, u8 *mac,
815 			 struct ieee80211_vif *vif),
816 	void *data)
817 {
818 	struct ieee80211_local *local = hw_to_local(hw);
819 
820 	mutex_lock(&local->iflist_mtx);
821 	__iterate_interfaces(local, iter_flags, iterator, data);
822 	mutex_unlock(&local->iflist_mtx);
823 }
824 EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces);
825 
826 void ieee80211_iterate_active_interfaces_atomic(
827 	struct ieee80211_hw *hw, u32 iter_flags,
828 	void (*iterator)(void *data, u8 *mac,
829 			 struct ieee80211_vif *vif),
830 	void *data)
831 {
832 	struct ieee80211_local *local = hw_to_local(hw);
833 
834 	rcu_read_lock();
835 	__iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
836 			     iterator, data);
837 	rcu_read_unlock();
838 }
839 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
840 
841 void ieee80211_iterate_active_interfaces_mtx(
842 	struct ieee80211_hw *hw, u32 iter_flags,
843 	void (*iterator)(void *data, u8 *mac,
844 			 struct ieee80211_vif *vif),
845 	void *data)
846 {
847 	struct ieee80211_local *local = hw_to_local(hw);
848 
849 	lockdep_assert_wiphy(hw->wiphy);
850 
851 	__iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
852 			     iterator, data);
853 }
854 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_mtx);
855 
856 static void __iterate_stations(struct ieee80211_local *local,
857 			       void (*iterator)(void *data,
858 						struct ieee80211_sta *sta),
859 			       void *data)
860 {
861 	struct sta_info *sta;
862 
863 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
864 		if (!sta->uploaded)
865 			continue;
866 
867 		iterator(data, &sta->sta);
868 	}
869 }
870 
871 void ieee80211_iterate_stations(struct ieee80211_hw *hw,
872 				void (*iterator)(void *data,
873 						 struct ieee80211_sta *sta),
874 				void *data)
875 {
876 	struct ieee80211_local *local = hw_to_local(hw);
877 
878 	mutex_lock(&local->sta_mtx);
879 	__iterate_stations(local, iterator, data);
880 	mutex_unlock(&local->sta_mtx);
881 }
882 EXPORT_SYMBOL_GPL(ieee80211_iterate_stations);
883 
884 void ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
885 			void (*iterator)(void *data,
886 					 struct ieee80211_sta *sta),
887 			void *data)
888 {
889 	struct ieee80211_local *local = hw_to_local(hw);
890 
891 	rcu_read_lock();
892 	__iterate_stations(local, iterator, data);
893 	rcu_read_unlock();
894 }
895 EXPORT_SYMBOL_GPL(ieee80211_iterate_stations_atomic);
896 
897 struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
898 {
899 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
900 
901 	if (!ieee80211_sdata_running(sdata) ||
902 	    !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
903 		return NULL;
904 	return &sdata->vif;
905 }
906 EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
907 
908 struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
909 {
910 	if (!vif)
911 		return NULL;
912 
913 	return &vif_to_sdata(vif)->wdev;
914 }
915 EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
916 
917 /*
918  * Nothing should have been stuffed into the workqueue during
919  * the suspend->resume cycle. Since we can't check each caller
920  * of this function if we are already quiescing / suspended,
921  * check here and don't WARN since this can actually happen when
922  * the rx path (for example) is racing against __ieee80211_suspend
923  * and suspending / quiescing was set after the rx path checked
924  * them.
925  */
926 static bool ieee80211_can_queue_work(struct ieee80211_local *local)
927 {
928 	if (local->quiescing || (local->suspended && !local->resuming)) {
929 		pr_warn("queueing ieee80211 work while going to suspend\n");
930 		return false;
931 	}
932 
933 	return true;
934 }
935 
936 void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
937 {
938 	struct ieee80211_local *local = hw_to_local(hw);
939 
940 	if (!ieee80211_can_queue_work(local))
941 		return;
942 
943 	queue_work(local->workqueue, work);
944 }
945 EXPORT_SYMBOL(ieee80211_queue_work);
946 
947 void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
948 				  struct delayed_work *dwork,
949 				  unsigned long delay)
950 {
951 	struct ieee80211_local *local = hw_to_local(hw);
952 
953 	if (!ieee80211_can_queue_work(local))
954 		return;
955 
956 	queue_delayed_work(local->workqueue, dwork, delay);
957 }
958 EXPORT_SYMBOL(ieee80211_queue_delayed_work);
959 
960 static void
961 ieee80211_parse_extension_element(u32 *crc,
962 				  const struct element *elem,
963 				  struct ieee802_11_elems *elems,
964 				  struct ieee80211_elems_parse_params *params)
965 {
966 	const void *data = elem->data + 1;
967 	u8 len;
968 
969 	if (!elem->datalen)
970 		return;
971 
972 	len = elem->datalen - 1;
973 
974 	switch (elem->data[0]) {
975 	case WLAN_EID_EXT_HE_MU_EDCA:
976 		if (len >= sizeof(*elems->mu_edca_param_set)) {
977 			elems->mu_edca_param_set = data;
978 			if (crc)
979 				*crc = crc32_be(*crc, (void *)elem,
980 						elem->datalen + 2);
981 		}
982 		break;
983 	case WLAN_EID_EXT_HE_CAPABILITY:
984 		if (ieee80211_he_capa_size_ok(data, len)) {
985 			elems->he_cap = data;
986 			elems->he_cap_len = len;
987 		}
988 		break;
989 	case WLAN_EID_EXT_HE_OPERATION:
990 		if (len >= sizeof(*elems->he_operation) &&
991 		    len >= ieee80211_he_oper_size(data) - 1) {
992 			if (crc)
993 				*crc = crc32_be(*crc, (void *)elem,
994 						elem->datalen + 2);
995 			elems->he_operation = data;
996 		}
997 		break;
998 	case WLAN_EID_EXT_UORA:
999 		if (len >= 1)
1000 			elems->uora_element = data;
1001 		break;
1002 	case WLAN_EID_EXT_MAX_CHANNEL_SWITCH_TIME:
1003 		if (len == 3)
1004 			elems->max_channel_switch_time = data;
1005 		break;
1006 	case WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION:
1007 		if (len >= sizeof(*elems->mbssid_config_ie))
1008 			elems->mbssid_config_ie = data;
1009 		break;
1010 	case WLAN_EID_EXT_HE_SPR:
1011 		if (len >= sizeof(*elems->he_spr) &&
1012 		    len >= ieee80211_he_spr_size(data))
1013 			elems->he_spr = data;
1014 		break;
1015 	case WLAN_EID_EXT_HE_6GHZ_CAPA:
1016 		if (len >= sizeof(*elems->he_6ghz_capa))
1017 			elems->he_6ghz_capa = data;
1018 		break;
1019 	case WLAN_EID_EXT_EHT_CAPABILITY:
1020 		if (ieee80211_eht_capa_size_ok(elems->he_cap,
1021 					       data, len,
1022 					       params->from_ap)) {
1023 			elems->eht_cap = data;
1024 			elems->eht_cap_len = len;
1025 		}
1026 		break;
1027 	case WLAN_EID_EXT_EHT_OPERATION:
1028 		if (ieee80211_eht_oper_size_ok(data, len))
1029 			elems->eht_operation = data;
1030 		break;
1031 	case WLAN_EID_EXT_EHT_MULTI_LINK:
1032 		if (ieee80211_mle_size_ok(data, len)) {
1033 			elems->multi_link = (void *)data;
1034 			elems->multi_link_len = len;
1035 		}
1036 		break;
1037 	}
1038 }
1039 
1040 static u32
1041 _ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params,
1042 			     struct ieee802_11_elems *elems,
1043 			     const struct element *check_inherit)
1044 {
1045 	const struct element *elem;
1046 	bool calc_crc = params->filter != 0;
1047 	DECLARE_BITMAP(seen_elems, 256);
1048 	u32 crc = params->crc;
1049 	const u8 *ie;
1050 
1051 	bitmap_zero(seen_elems, 256);
1052 
1053 	for_each_element(elem, params->start, params->len) {
1054 		bool elem_parse_failed;
1055 		u8 id = elem->id;
1056 		u8 elen = elem->datalen;
1057 		const u8 *pos = elem->data;
1058 
1059 		if (check_inherit &&
1060 		    !cfg80211_is_element_inherited(elem,
1061 						   check_inherit))
1062 			continue;
1063 
1064 		switch (id) {
1065 		case WLAN_EID_SSID:
1066 		case WLAN_EID_SUPP_RATES:
1067 		case WLAN_EID_FH_PARAMS:
1068 		case WLAN_EID_DS_PARAMS:
1069 		case WLAN_EID_CF_PARAMS:
1070 		case WLAN_EID_TIM:
1071 		case WLAN_EID_IBSS_PARAMS:
1072 		case WLAN_EID_CHALLENGE:
1073 		case WLAN_EID_RSN:
1074 		case WLAN_EID_ERP_INFO:
1075 		case WLAN_EID_EXT_SUPP_RATES:
1076 		case WLAN_EID_HT_CAPABILITY:
1077 		case WLAN_EID_HT_OPERATION:
1078 		case WLAN_EID_VHT_CAPABILITY:
1079 		case WLAN_EID_VHT_OPERATION:
1080 		case WLAN_EID_MESH_ID:
1081 		case WLAN_EID_MESH_CONFIG:
1082 		case WLAN_EID_PEER_MGMT:
1083 		case WLAN_EID_PREQ:
1084 		case WLAN_EID_PREP:
1085 		case WLAN_EID_PERR:
1086 		case WLAN_EID_RANN:
1087 		case WLAN_EID_CHANNEL_SWITCH:
1088 		case WLAN_EID_EXT_CHANSWITCH_ANN:
1089 		case WLAN_EID_COUNTRY:
1090 		case WLAN_EID_PWR_CONSTRAINT:
1091 		case WLAN_EID_TIMEOUT_INTERVAL:
1092 		case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
1093 		case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1094 		case WLAN_EID_CHAN_SWITCH_PARAM:
1095 		case WLAN_EID_EXT_CAPABILITY:
1096 		case WLAN_EID_CHAN_SWITCH_TIMING:
1097 		case WLAN_EID_LINK_ID:
1098 		case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1099 		case WLAN_EID_RSNX:
1100 		case WLAN_EID_S1G_BCN_COMPAT:
1101 		case WLAN_EID_S1G_CAPABILITIES:
1102 		case WLAN_EID_S1G_OPERATION:
1103 		case WLAN_EID_AID_RESPONSE:
1104 		case WLAN_EID_S1G_SHORT_BCN_INTERVAL:
1105 		/*
1106 		 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
1107 		 * that if the content gets bigger it might be needed more than once
1108 		 */
1109 			if (test_bit(id, seen_elems)) {
1110 				elems->parse_error = true;
1111 				continue;
1112 			}
1113 			break;
1114 		}
1115 
1116 		if (calc_crc && id < 64 && (params->filter & (1ULL << id)))
1117 			crc = crc32_be(crc, pos - 2, elen + 2);
1118 
1119 		elem_parse_failed = false;
1120 
1121 		switch (id) {
1122 		case WLAN_EID_LINK_ID:
1123 			if (elen + 2 < sizeof(struct ieee80211_tdls_lnkie)) {
1124 				elem_parse_failed = true;
1125 				break;
1126 			}
1127 			elems->lnk_id = (void *)(pos - 2);
1128 			break;
1129 		case WLAN_EID_CHAN_SWITCH_TIMING:
1130 			if (elen < sizeof(struct ieee80211_ch_switch_timing)) {
1131 				elem_parse_failed = true;
1132 				break;
1133 			}
1134 			elems->ch_sw_timing = (void *)pos;
1135 			break;
1136 		case WLAN_EID_EXT_CAPABILITY:
1137 			elems->ext_capab = pos;
1138 			elems->ext_capab_len = elen;
1139 			break;
1140 		case WLAN_EID_SSID:
1141 			elems->ssid = pos;
1142 			elems->ssid_len = elen;
1143 			break;
1144 		case WLAN_EID_SUPP_RATES:
1145 			elems->supp_rates = pos;
1146 			elems->supp_rates_len = elen;
1147 			break;
1148 		case WLAN_EID_DS_PARAMS:
1149 			if (elen >= 1)
1150 				elems->ds_params = pos;
1151 			else
1152 				elem_parse_failed = true;
1153 			break;
1154 		case WLAN_EID_TIM:
1155 			if (elen >= sizeof(struct ieee80211_tim_ie)) {
1156 				elems->tim = (void *)pos;
1157 				elems->tim_len = elen;
1158 			} else
1159 				elem_parse_failed = true;
1160 			break;
1161 		case WLAN_EID_VENDOR_SPECIFIC:
1162 			if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
1163 			    pos[2] == 0xf2) {
1164 				/* Microsoft OUI (00:50:F2) */
1165 
1166 				if (calc_crc)
1167 					crc = crc32_be(crc, pos - 2, elen + 2);
1168 
1169 				if (elen >= 5 && pos[3] == 2) {
1170 					/* OUI Type 2 - WMM IE */
1171 					if (pos[4] == 0) {
1172 						elems->wmm_info = pos;
1173 						elems->wmm_info_len = elen;
1174 					} else if (pos[4] == 1) {
1175 						elems->wmm_param = pos;
1176 						elems->wmm_param_len = elen;
1177 					}
1178 				}
1179 			}
1180 			break;
1181 		case WLAN_EID_RSN:
1182 			elems->rsn = pos;
1183 			elems->rsn_len = elen;
1184 			break;
1185 		case WLAN_EID_ERP_INFO:
1186 			if (elen >= 1)
1187 				elems->erp_info = pos;
1188 			else
1189 				elem_parse_failed = true;
1190 			break;
1191 		case WLAN_EID_EXT_SUPP_RATES:
1192 			elems->ext_supp_rates = pos;
1193 			elems->ext_supp_rates_len = elen;
1194 			break;
1195 		case WLAN_EID_HT_CAPABILITY:
1196 			if (elen >= sizeof(struct ieee80211_ht_cap))
1197 				elems->ht_cap_elem = (void *)pos;
1198 			else
1199 				elem_parse_failed = true;
1200 			break;
1201 		case WLAN_EID_HT_OPERATION:
1202 			if (elen >= sizeof(struct ieee80211_ht_operation))
1203 				elems->ht_operation = (void *)pos;
1204 			else
1205 				elem_parse_failed = true;
1206 			break;
1207 		case WLAN_EID_VHT_CAPABILITY:
1208 			if (elen >= sizeof(struct ieee80211_vht_cap))
1209 				elems->vht_cap_elem = (void *)pos;
1210 			else
1211 				elem_parse_failed = true;
1212 			break;
1213 		case WLAN_EID_VHT_OPERATION:
1214 			if (elen >= sizeof(struct ieee80211_vht_operation)) {
1215 				elems->vht_operation = (void *)pos;
1216 				if (calc_crc)
1217 					crc = crc32_be(crc, pos - 2, elen + 2);
1218 				break;
1219 			}
1220 			elem_parse_failed = true;
1221 			break;
1222 		case WLAN_EID_OPMODE_NOTIF:
1223 			if (elen > 0) {
1224 				elems->opmode_notif = pos;
1225 				if (calc_crc)
1226 					crc = crc32_be(crc, pos - 2, elen + 2);
1227 				break;
1228 			}
1229 			elem_parse_failed = true;
1230 			break;
1231 		case WLAN_EID_MESH_ID:
1232 			elems->mesh_id = pos;
1233 			elems->mesh_id_len = elen;
1234 			break;
1235 		case WLAN_EID_MESH_CONFIG:
1236 			if (elen >= sizeof(struct ieee80211_meshconf_ie))
1237 				elems->mesh_config = (void *)pos;
1238 			else
1239 				elem_parse_failed = true;
1240 			break;
1241 		case WLAN_EID_PEER_MGMT:
1242 			elems->peering = pos;
1243 			elems->peering_len = elen;
1244 			break;
1245 		case WLAN_EID_MESH_AWAKE_WINDOW:
1246 			if (elen >= 2)
1247 				elems->awake_window = (void *)pos;
1248 			break;
1249 		case WLAN_EID_PREQ:
1250 			elems->preq = pos;
1251 			elems->preq_len = elen;
1252 			break;
1253 		case WLAN_EID_PREP:
1254 			elems->prep = pos;
1255 			elems->prep_len = elen;
1256 			break;
1257 		case WLAN_EID_PERR:
1258 			elems->perr = pos;
1259 			elems->perr_len = elen;
1260 			break;
1261 		case WLAN_EID_RANN:
1262 			if (elen >= sizeof(struct ieee80211_rann_ie))
1263 				elems->rann = (void *)pos;
1264 			else
1265 				elem_parse_failed = true;
1266 			break;
1267 		case WLAN_EID_CHANNEL_SWITCH:
1268 			if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
1269 				elem_parse_failed = true;
1270 				break;
1271 			}
1272 			elems->ch_switch_ie = (void *)pos;
1273 			break;
1274 		case WLAN_EID_EXT_CHANSWITCH_ANN:
1275 			if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
1276 				elem_parse_failed = true;
1277 				break;
1278 			}
1279 			elems->ext_chansw_ie = (void *)pos;
1280 			break;
1281 		case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
1282 			if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
1283 				elem_parse_failed = true;
1284 				break;
1285 			}
1286 			elems->sec_chan_offs = (void *)pos;
1287 			break;
1288 		case WLAN_EID_CHAN_SWITCH_PARAM:
1289 			if (elen <
1290 			    sizeof(*elems->mesh_chansw_params_ie)) {
1291 				elem_parse_failed = true;
1292 				break;
1293 			}
1294 			elems->mesh_chansw_params_ie = (void *)pos;
1295 			break;
1296 		case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1297 			if (!params->action ||
1298 			    elen < sizeof(*elems->wide_bw_chansw_ie)) {
1299 				elem_parse_failed = true;
1300 				break;
1301 			}
1302 			elems->wide_bw_chansw_ie = (void *)pos;
1303 			break;
1304 		case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
1305 			if (params->action) {
1306 				elem_parse_failed = true;
1307 				break;
1308 			}
1309 			/*
1310 			 * This is a bit tricky, but as we only care about
1311 			 * the wide bandwidth channel switch element, so
1312 			 * just parse it out manually.
1313 			 */
1314 			ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
1315 					      pos, elen);
1316 			if (ie) {
1317 				if (ie[1] >= sizeof(*elems->wide_bw_chansw_ie))
1318 					elems->wide_bw_chansw_ie =
1319 						(void *)(ie + 2);
1320 				else
1321 					elem_parse_failed = true;
1322 			}
1323 			break;
1324 		case WLAN_EID_COUNTRY:
1325 			elems->country_elem = pos;
1326 			elems->country_elem_len = elen;
1327 			break;
1328 		case WLAN_EID_PWR_CONSTRAINT:
1329 			if (elen != 1) {
1330 				elem_parse_failed = true;
1331 				break;
1332 			}
1333 			elems->pwr_constr_elem = pos;
1334 			break;
1335 		case WLAN_EID_CISCO_VENDOR_SPECIFIC:
1336 			/* Lots of different options exist, but we only care
1337 			 * about the Dynamic Transmit Power Control element.
1338 			 * First check for the Cisco OUI, then for the DTPC
1339 			 * tag (0x00).
1340 			 */
1341 			if (elen < 4) {
1342 				elem_parse_failed = true;
1343 				break;
1344 			}
1345 
1346 			if (pos[0] != 0x00 || pos[1] != 0x40 ||
1347 			    pos[2] != 0x96 || pos[3] != 0x00)
1348 				break;
1349 
1350 			if (elen != 6) {
1351 				elem_parse_failed = true;
1352 				break;
1353 			}
1354 
1355 			if (calc_crc)
1356 				crc = crc32_be(crc, pos - 2, elen + 2);
1357 
1358 			elems->cisco_dtpc_elem = pos;
1359 			break;
1360 		case WLAN_EID_ADDBA_EXT:
1361 			if (elen < sizeof(struct ieee80211_addba_ext_ie)) {
1362 				elem_parse_failed = true;
1363 				break;
1364 			}
1365 			elems->addba_ext_ie = (void *)pos;
1366 			break;
1367 		case WLAN_EID_TIMEOUT_INTERVAL:
1368 			if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
1369 				elems->timeout_int = (void *)pos;
1370 			else
1371 				elem_parse_failed = true;
1372 			break;
1373 		case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1374 			if (elen >= sizeof(*elems->max_idle_period_ie))
1375 				elems->max_idle_period_ie = (void *)pos;
1376 			break;
1377 		case WLAN_EID_RSNX:
1378 			elems->rsnx = pos;
1379 			elems->rsnx_len = elen;
1380 			break;
1381 		case WLAN_EID_TX_POWER_ENVELOPE:
1382 			if (elen < 1 ||
1383 			    elen > sizeof(struct ieee80211_tx_pwr_env))
1384 				break;
1385 
1386 			if (elems->tx_pwr_env_num >= ARRAY_SIZE(elems->tx_pwr_env))
1387 				break;
1388 
1389 			elems->tx_pwr_env[elems->tx_pwr_env_num] = (void *)pos;
1390 			elems->tx_pwr_env_len[elems->tx_pwr_env_num] = elen;
1391 			elems->tx_pwr_env_num++;
1392 			break;
1393 		case WLAN_EID_EXTENSION:
1394 			ieee80211_parse_extension_element(calc_crc ?
1395 								&crc : NULL,
1396 							  elem, elems, params);
1397 			break;
1398 		case WLAN_EID_S1G_CAPABILITIES:
1399 			if (elen >= sizeof(*elems->s1g_capab))
1400 				elems->s1g_capab = (void *)pos;
1401 			else
1402 				elem_parse_failed = true;
1403 			break;
1404 		case WLAN_EID_S1G_OPERATION:
1405 			if (elen == sizeof(*elems->s1g_oper))
1406 				elems->s1g_oper = (void *)pos;
1407 			else
1408 				elem_parse_failed = true;
1409 			break;
1410 		case WLAN_EID_S1G_BCN_COMPAT:
1411 			if (elen == sizeof(*elems->s1g_bcn_compat))
1412 				elems->s1g_bcn_compat = (void *)pos;
1413 			else
1414 				elem_parse_failed = true;
1415 			break;
1416 		case WLAN_EID_AID_RESPONSE:
1417 			if (elen == sizeof(struct ieee80211_aid_response_ie))
1418 				elems->aid_resp = (void *)pos;
1419 			else
1420 				elem_parse_failed = true;
1421 			break;
1422 		default:
1423 			break;
1424 		}
1425 
1426 		if (elem_parse_failed)
1427 			elems->parse_error = true;
1428 		else
1429 			__set_bit(id, seen_elems);
1430 	}
1431 
1432 	if (!for_each_element_completed(elem, params->start, params->len))
1433 		elems->parse_error = true;
1434 
1435 	return crc;
1436 }
1437 
1438 static size_t ieee802_11_find_bssid_profile(const u8 *start, size_t len,
1439 					    struct ieee802_11_elems *elems,
1440 					    struct cfg80211_bss *bss,
1441 					    u8 *nontransmitted_profile)
1442 {
1443 	const struct element *elem, *sub;
1444 	size_t profile_len = 0;
1445 	bool found = false;
1446 
1447 	if (!bss || !bss->transmitted_bss)
1448 		return profile_len;
1449 
1450 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, len) {
1451 		if (elem->datalen < 2)
1452 			continue;
1453 		if (elem->data[0] < 1 || elem->data[0] > 8)
1454 			continue;
1455 
1456 		for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1457 			u8 new_bssid[ETH_ALEN];
1458 			const u8 *index;
1459 
1460 			if (sub->id != 0 || sub->datalen < 4) {
1461 				/* not a valid BSS profile */
1462 				continue;
1463 			}
1464 
1465 			if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1466 			    sub->data[1] != 2) {
1467 				/* The first element of the
1468 				 * Nontransmitted BSSID Profile is not
1469 				 * the Nontransmitted BSSID Capability
1470 				 * element.
1471 				 */
1472 				continue;
1473 			}
1474 
1475 			memset(nontransmitted_profile, 0, len);
1476 			profile_len = cfg80211_merge_profile(start, len,
1477 							     elem,
1478 							     sub,
1479 							     nontransmitted_profile,
1480 							     len);
1481 
1482 			/* found a Nontransmitted BSSID Profile */
1483 			index = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX,
1484 						 nontransmitted_profile,
1485 						 profile_len);
1486 			if (!index || index[1] < 1 || index[2] == 0) {
1487 				/* Invalid MBSSID Index element */
1488 				continue;
1489 			}
1490 
1491 			cfg80211_gen_new_bssid(bss->transmitted_bss->bssid,
1492 					       elem->data[0],
1493 					       index[2],
1494 					       new_bssid);
1495 			if (ether_addr_equal(new_bssid, bss->bssid)) {
1496 				found = true;
1497 				elems->bssid_index_len = index[1];
1498 				elems->bssid_index = (void *)&index[2];
1499 				break;
1500 			}
1501 		}
1502 	}
1503 
1504 	return found ? profile_len : 0;
1505 }
1506 
1507 static void ieee80211_defragment_element(struct ieee802_11_elems *elems,
1508 					 void **elem_ptr, size_t *len,
1509 					 size_t total_len, u8 frag_id)
1510 {
1511 	u8 *data = *elem_ptr, *pos, *start;
1512 	const struct element *elem;
1513 
1514 	/*
1515 	 * Since 'data' points to the data of the element, not the element
1516 	 * itself, allow 254 in case it was an extended element where the
1517 	 * extended ID isn't part of the data we see here and thus not part of
1518 	 * 'len' either.
1519 	 */
1520 	if (!data || (*len != 254 && *len != 255))
1521 		return;
1522 
1523 	start = elems->scratch_pos;
1524 
1525 	if (WARN_ON(*len > (elems->scratch + elems->scratch_len -
1526 			    elems->scratch_pos)))
1527 		return;
1528 
1529 	memcpy(elems->scratch_pos, data, *len);
1530 	elems->scratch_pos += *len;
1531 
1532 	pos = data + *len;
1533 	total_len -= *len;
1534 	for_each_element(elem, pos, total_len) {
1535 		if (elem->id != frag_id)
1536 			break;
1537 
1538 		if (WARN_ON(elem->datalen >
1539 			    (elems->scratch + elems->scratch_len -
1540 			     elems->scratch_pos)))
1541 			return;
1542 
1543 		memcpy(elems->scratch_pos, elem->data, elem->datalen);
1544 		elems->scratch_pos += elem->datalen;
1545 
1546 		*len += elem->datalen;
1547 	}
1548 
1549 	*elem_ptr = start;
1550 }
1551 
1552 static void ieee80211_mle_get_sta_prof(struct ieee802_11_elems *elems,
1553 				       u8 link_id)
1554 {
1555 	const struct ieee80211_multi_link_elem *ml = elems->multi_link;
1556 	size_t ml_len = elems->multi_link_len;
1557 	const struct element *sub;
1558 
1559 	if (!ml || !ml_len)
1560 		return;
1561 
1562 	if (le16_get_bits(ml->control, IEEE80211_ML_CONTROL_TYPE) !=
1563 	    IEEE80211_ML_CONTROL_TYPE_BASIC)
1564 		return;
1565 
1566 	for_each_mle_subelement(sub, (u8 *)ml, ml_len) {
1567 		struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data;
1568 		u16 control;
1569 
1570 		if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE)
1571 			continue;
1572 
1573 		if (!ieee80211_mle_sta_prof_size_ok(sub->data, sub->datalen))
1574 			return;
1575 
1576 		control = le16_to_cpu(prof->control);
1577 
1578 		if (link_id != u16_get_bits(control,
1579 					    IEEE80211_MLE_STA_CONTROL_LINK_ID))
1580 			continue;
1581 
1582 		if (!(control & IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE))
1583 			return;
1584 
1585 		elems->prof = prof;
1586 		elems->sta_prof_len = sub->datalen;
1587 
1588 		/* the sub element can be fragmented */
1589 		ieee80211_defragment_element(elems, (void **)&elems->prof,
1590 					     &elems->sta_prof_len,
1591 					     ml_len - (sub->data - (u8 *)ml),
1592 					     IEEE80211_MLE_SUBELEM_FRAGMENT);
1593 		return;
1594 	}
1595 }
1596 
1597 static void ieee80211_mle_parse_link(struct ieee802_11_elems *elems,
1598 				     struct ieee80211_elems_parse_params *params)
1599 {
1600 	struct ieee80211_mle_per_sta_profile *prof;
1601 	struct ieee80211_elems_parse_params sub = {
1602 		.action = params->action,
1603 		.from_ap = params->from_ap,
1604 		.link_id = -1,
1605 	};
1606 	const struct element *non_inherit = NULL;
1607 	const u8 *end;
1608 
1609 	if (params->link_id == -1)
1610 		return;
1611 
1612 	ieee80211_defragment_element(elems, (void **)&elems->multi_link,
1613 				     &elems->multi_link_len,
1614 				     elems->total_len - ((u8 *)elems->multi_link -
1615 							 elems->ie_start),
1616 				     WLAN_EID_FRAGMENT);
1617 
1618 	ieee80211_mle_get_sta_prof(elems, params->link_id);
1619 	prof = elems->prof;
1620 
1621 	if (!prof)
1622 		return;
1623 
1624 	/* check if we have the 4 bytes for the fixed part in assoc response */
1625 	if (elems->sta_prof_len < sizeof(*prof) + prof->sta_info_len - 1 + 4) {
1626 		elems->prof = NULL;
1627 		elems->sta_prof_len = 0;
1628 		return;
1629 	}
1630 
1631 	/*
1632 	 * Skip the capability information and the status code that are expected
1633 	 * as part of the station profile in association response frames. Note
1634 	 * the -1 is because the 'sta_info_len' is accounted to as part of the
1635 	 * per-STA profile, but not part of the 'u8 variable[]' portion.
1636 	 */
1637 	sub.start = prof->variable + prof->sta_info_len - 1 + 4;
1638 	end = (const u8 *)prof + elems->sta_prof_len;
1639 	sub.len = end - sub.start;
1640 
1641 	non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1642 					     sub.start, sub.len);
1643 	_ieee802_11_parse_elems_full(&sub, elems, non_inherit);
1644 }
1645 
1646 struct ieee802_11_elems *
1647 ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params)
1648 {
1649 	struct ieee802_11_elems *elems;
1650 	const struct element *non_inherit = NULL;
1651 	u8 *nontransmitted_profile;
1652 	int nontransmitted_profile_len = 0;
1653 	size_t scratch_len = params->scratch_len ?: 3 * params->len;
1654 
1655 	elems = kzalloc(sizeof(*elems) + scratch_len, GFP_ATOMIC);
1656 	if (!elems)
1657 		return NULL;
1658 	elems->ie_start = params->start;
1659 	elems->total_len = params->len;
1660 	elems->scratch_len = scratch_len;
1661 	elems->scratch_pos = elems->scratch;
1662 
1663 	nontransmitted_profile = elems->scratch_pos;
1664 	nontransmitted_profile_len =
1665 		ieee802_11_find_bssid_profile(params->start, params->len,
1666 					      elems, params->bss,
1667 					      nontransmitted_profile);
1668 	elems->scratch_pos += nontransmitted_profile_len;
1669 	elems->scratch_len -= nontransmitted_profile_len;
1670 	non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1671 					     nontransmitted_profile,
1672 					     nontransmitted_profile_len);
1673 
1674 	elems->crc = _ieee802_11_parse_elems_full(params, elems, non_inherit);
1675 
1676 	/* Override with nontransmitted profile, if found */
1677 	if (nontransmitted_profile_len) {
1678 		struct ieee80211_elems_parse_params sub = {
1679 			.start = nontransmitted_profile,
1680 			.len = nontransmitted_profile_len,
1681 			.action = params->action,
1682 			.link_id = params->link_id,
1683 		};
1684 
1685 		_ieee802_11_parse_elems_full(&sub, elems, NULL);
1686 	}
1687 
1688 	ieee80211_mle_parse_link(elems, params);
1689 
1690 	if (elems->tim && !elems->parse_error) {
1691 		const struct ieee80211_tim_ie *tim_ie = elems->tim;
1692 
1693 		elems->dtim_period = tim_ie->dtim_period;
1694 		elems->dtim_count = tim_ie->dtim_count;
1695 	}
1696 
1697 	/* Override DTIM period and count if needed */
1698 	if (elems->bssid_index &&
1699 	    elems->bssid_index_len >=
1700 	    offsetofend(struct ieee80211_bssid_index, dtim_period))
1701 		elems->dtim_period = elems->bssid_index->dtim_period;
1702 
1703 	if (elems->bssid_index &&
1704 	    elems->bssid_index_len >=
1705 	    offsetofend(struct ieee80211_bssid_index, dtim_count))
1706 		elems->dtim_count = elems->bssid_index->dtim_count;
1707 
1708 	return elems;
1709 }
1710 
1711 void ieee80211_regulatory_limit_wmm_params(struct ieee80211_sub_if_data *sdata,
1712 					   struct ieee80211_tx_queue_params
1713 					   *qparam, int ac)
1714 {
1715 	struct ieee80211_chanctx_conf *chanctx_conf;
1716 	const struct ieee80211_reg_rule *rrule;
1717 	const struct ieee80211_wmm_ac *wmm_ac;
1718 	u16 center_freq = 0;
1719 
1720 	if (sdata->vif.type != NL80211_IFTYPE_AP &&
1721 	    sdata->vif.type != NL80211_IFTYPE_STATION)
1722 		return;
1723 
1724 	rcu_read_lock();
1725 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1726 	if (chanctx_conf)
1727 		center_freq = chanctx_conf->def.chan->center_freq;
1728 
1729 	if (!center_freq) {
1730 		rcu_read_unlock();
1731 		return;
1732 	}
1733 
1734 	rrule = freq_reg_info(sdata->wdev.wiphy, MHZ_TO_KHZ(center_freq));
1735 
1736 	if (IS_ERR_OR_NULL(rrule) || !rrule->has_wmm) {
1737 		rcu_read_unlock();
1738 		return;
1739 	}
1740 
1741 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1742 		wmm_ac = &rrule->wmm_rule.ap[ac];
1743 	else
1744 		wmm_ac = &rrule->wmm_rule.client[ac];
1745 	qparam->cw_min = max_t(u16, qparam->cw_min, wmm_ac->cw_min);
1746 	qparam->cw_max = max_t(u16, qparam->cw_max, wmm_ac->cw_max);
1747 	qparam->aifs = max_t(u8, qparam->aifs, wmm_ac->aifsn);
1748 	qparam->txop = min_t(u16, qparam->txop, wmm_ac->cot / 32);
1749 	rcu_read_unlock();
1750 }
1751 
1752 void ieee80211_set_wmm_default(struct ieee80211_link_data *link,
1753 			       bool bss_notify, bool enable_qos)
1754 {
1755 	struct ieee80211_sub_if_data *sdata = link->sdata;
1756 	struct ieee80211_local *local = sdata->local;
1757 	struct ieee80211_tx_queue_params qparam;
1758 	struct ieee80211_chanctx_conf *chanctx_conf;
1759 	int ac;
1760 	bool use_11b;
1761 	bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
1762 	int aCWmin, aCWmax;
1763 
1764 	if (!local->ops->conf_tx)
1765 		return;
1766 
1767 	if (local->hw.queues < IEEE80211_NUM_ACS)
1768 		return;
1769 
1770 	memset(&qparam, 0, sizeof(qparam));
1771 
1772 	rcu_read_lock();
1773 	chanctx_conf = rcu_dereference(link->conf->chanctx_conf);
1774 	use_11b = (chanctx_conf &&
1775 		   chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
1776 		 !link->operating_11g_mode;
1777 	rcu_read_unlock();
1778 
1779 	is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
1780 
1781 	/* Set defaults according to 802.11-2007 Table 7-37 */
1782 	aCWmax = 1023;
1783 	if (use_11b)
1784 		aCWmin = 31;
1785 	else
1786 		aCWmin = 15;
1787 
1788 	/* Confiure old 802.11b/g medium access rules. */
1789 	qparam.cw_max = aCWmax;
1790 	qparam.cw_min = aCWmin;
1791 	qparam.txop = 0;
1792 	qparam.aifs = 2;
1793 
1794 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1795 		/* Update if QoS is enabled. */
1796 		if (enable_qos) {
1797 			switch (ac) {
1798 			case IEEE80211_AC_BK:
1799 				qparam.cw_max = aCWmax;
1800 				qparam.cw_min = aCWmin;
1801 				qparam.txop = 0;
1802 				if (is_ocb)
1803 					qparam.aifs = 9;
1804 				else
1805 					qparam.aifs = 7;
1806 				break;
1807 			/* never happens but let's not leave undefined */
1808 			default:
1809 			case IEEE80211_AC_BE:
1810 				qparam.cw_max = aCWmax;
1811 				qparam.cw_min = aCWmin;
1812 				qparam.txop = 0;
1813 				if (is_ocb)
1814 					qparam.aifs = 6;
1815 				else
1816 					qparam.aifs = 3;
1817 				break;
1818 			case IEEE80211_AC_VI:
1819 				qparam.cw_max = aCWmin;
1820 				qparam.cw_min = (aCWmin + 1) / 2 - 1;
1821 				if (is_ocb)
1822 					qparam.txop = 0;
1823 				else if (use_11b)
1824 					qparam.txop = 6016/32;
1825 				else
1826 					qparam.txop = 3008/32;
1827 
1828 				if (is_ocb)
1829 					qparam.aifs = 3;
1830 				else
1831 					qparam.aifs = 2;
1832 				break;
1833 			case IEEE80211_AC_VO:
1834 				qparam.cw_max = (aCWmin + 1) / 2 - 1;
1835 				qparam.cw_min = (aCWmin + 1) / 4 - 1;
1836 				if (is_ocb)
1837 					qparam.txop = 0;
1838 				else if (use_11b)
1839 					qparam.txop = 3264/32;
1840 				else
1841 					qparam.txop = 1504/32;
1842 				qparam.aifs = 2;
1843 				break;
1844 			}
1845 		}
1846 		ieee80211_regulatory_limit_wmm_params(sdata, &qparam, ac);
1847 
1848 		qparam.uapsd = false;
1849 
1850 		link->tx_conf[ac] = qparam;
1851 		drv_conf_tx(local, link, ac, &qparam);
1852 	}
1853 
1854 	if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1855 	    sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1856 	    sdata->vif.type != NL80211_IFTYPE_NAN) {
1857 		link->conf->qos = enable_qos;
1858 		if (bss_notify)
1859 			ieee80211_link_info_change_notify(sdata, link,
1860 							  BSS_CHANGED_QOS);
1861 	}
1862 }
1863 
1864 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1865 			 u16 transaction, u16 auth_alg, u16 status,
1866 			 const u8 *extra, size_t extra_len, const u8 *da,
1867 			 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
1868 			 u32 tx_flags)
1869 {
1870 	struct ieee80211_local *local = sdata->local;
1871 	struct sk_buff *skb;
1872 	struct ieee80211_mgmt *mgmt;
1873 	bool multi_link = sdata->vif.valid_links;
1874 	struct {
1875 		u8 id;
1876 		u8 len;
1877 		u8 ext_id;
1878 		struct ieee80211_multi_link_elem ml;
1879 		struct ieee80211_mle_basic_common_info basic;
1880 	} __packed mle = {
1881 		.id = WLAN_EID_EXTENSION,
1882 		.len = sizeof(mle) - 2,
1883 		.ext_id = WLAN_EID_EXT_EHT_MULTI_LINK,
1884 		.ml.control = cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_BASIC),
1885 		.basic.len = sizeof(mle.basic),
1886 	};
1887 	int err;
1888 
1889 	memcpy(mle.basic.mld_mac_addr, sdata->vif.addr, ETH_ALEN);
1890 
1891 	/* 24 + 6 = header + auth_algo + auth_transaction + status_code */
1892 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
1893 			    24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN +
1894 			    multi_link * sizeof(mle));
1895 	if (!skb)
1896 		return;
1897 
1898 	skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
1899 
1900 	mgmt = skb_put_zero(skb, 24 + 6);
1901 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1902 					  IEEE80211_STYPE_AUTH);
1903 	memcpy(mgmt->da, da, ETH_ALEN);
1904 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1905 	memcpy(mgmt->bssid, bssid, ETH_ALEN);
1906 	mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
1907 	mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
1908 	mgmt->u.auth.status_code = cpu_to_le16(status);
1909 	if (extra)
1910 		skb_put_data(skb, extra, extra_len);
1911 	if (multi_link)
1912 		skb_put_data(skb, &mle, sizeof(mle));
1913 
1914 	if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1915 		mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1916 		err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1917 		if (WARN_ON(err)) {
1918 			kfree_skb(skb);
1919 			return;
1920 		}
1921 	}
1922 
1923 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1924 					tx_flags;
1925 	ieee80211_tx_skb(sdata, skb);
1926 }
1927 
1928 void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
1929 				    const u8 *da, const u8 *bssid,
1930 				    u16 stype, u16 reason,
1931 				    bool send_frame, u8 *frame_buf)
1932 {
1933 	struct ieee80211_local *local = sdata->local;
1934 	struct sk_buff *skb;
1935 	struct ieee80211_mgmt *mgmt = (void *)frame_buf;
1936 
1937 	/* build frame */
1938 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1939 	mgmt->duration = 0; /* initialize only */
1940 	mgmt->seq_ctrl = 0; /* initialize only */
1941 	memcpy(mgmt->da, da, ETH_ALEN);
1942 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1943 	memcpy(mgmt->bssid, bssid, ETH_ALEN);
1944 	/* u.deauth.reason_code == u.disassoc.reason_code */
1945 	mgmt->u.deauth.reason_code = cpu_to_le16(reason);
1946 
1947 	if (send_frame) {
1948 		skb = dev_alloc_skb(local->hw.extra_tx_headroom +
1949 				    IEEE80211_DEAUTH_FRAME_LEN);
1950 		if (!skb)
1951 			return;
1952 
1953 		skb_reserve(skb, local->hw.extra_tx_headroom);
1954 
1955 		/* copy in frame */
1956 		skb_put_data(skb, mgmt, IEEE80211_DEAUTH_FRAME_LEN);
1957 
1958 		if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1959 		    !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
1960 			IEEE80211_SKB_CB(skb)->flags |=
1961 				IEEE80211_TX_INTFL_DONT_ENCRYPT;
1962 
1963 		ieee80211_tx_skb(sdata, skb);
1964 	}
1965 }
1966 
1967 static u8 *ieee80211_write_he_6ghz_cap(u8 *pos, __le16 cap, u8 *end)
1968 {
1969 	if ((end - pos) < 5)
1970 		return pos;
1971 
1972 	*pos++ = WLAN_EID_EXTENSION;
1973 	*pos++ = 1 + sizeof(cap);
1974 	*pos++ = WLAN_EID_EXT_HE_6GHZ_CAPA;
1975 	memcpy(pos, &cap, sizeof(cap));
1976 
1977 	return pos + 2;
1978 }
1979 
1980 static int ieee80211_build_preq_ies_band(struct ieee80211_sub_if_data *sdata,
1981 					 u8 *buffer, size_t buffer_len,
1982 					 const u8 *ie, size_t ie_len,
1983 					 enum nl80211_band band,
1984 					 u32 rate_mask,
1985 					 struct cfg80211_chan_def *chandef,
1986 					 size_t *offset, u32 flags)
1987 {
1988 	struct ieee80211_local *local = sdata->local;
1989 	struct ieee80211_supported_band *sband;
1990 	const struct ieee80211_sta_he_cap *he_cap;
1991 	const struct ieee80211_sta_eht_cap *eht_cap;
1992 	u8 *pos = buffer, *end = buffer + buffer_len;
1993 	size_t noffset;
1994 	int supp_rates_len, i;
1995 	u8 rates[32];
1996 	int num_rates;
1997 	int ext_rates_len;
1998 	int shift;
1999 	u32 rate_flags;
2000 	bool have_80mhz = false;
2001 
2002 	*offset = 0;
2003 
2004 	sband = local->hw.wiphy->bands[band];
2005 	if (WARN_ON_ONCE(!sband))
2006 		return 0;
2007 
2008 	rate_flags = ieee80211_chandef_rate_flags(chandef);
2009 	shift = ieee80211_chandef_get_shift(chandef);
2010 
2011 	num_rates = 0;
2012 	for (i = 0; i < sband->n_bitrates; i++) {
2013 		if ((BIT(i) & rate_mask) == 0)
2014 			continue; /* skip rate */
2015 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
2016 			continue;
2017 
2018 		rates[num_rates++] =
2019 			(u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
2020 					  (1 << shift) * 5);
2021 	}
2022 
2023 	supp_rates_len = min_t(int, num_rates, 8);
2024 
2025 	if (end - pos < 2 + supp_rates_len)
2026 		goto out_err;
2027 	*pos++ = WLAN_EID_SUPP_RATES;
2028 	*pos++ = supp_rates_len;
2029 	memcpy(pos, rates, supp_rates_len);
2030 	pos += supp_rates_len;
2031 
2032 	/* insert "request information" if in custom IEs */
2033 	if (ie && ie_len) {
2034 		static const u8 before_extrates[] = {
2035 			WLAN_EID_SSID,
2036 			WLAN_EID_SUPP_RATES,
2037 			WLAN_EID_REQUEST,
2038 		};
2039 		noffset = ieee80211_ie_split(ie, ie_len,
2040 					     before_extrates,
2041 					     ARRAY_SIZE(before_extrates),
2042 					     *offset);
2043 		if (end - pos < noffset - *offset)
2044 			goto out_err;
2045 		memcpy(pos, ie + *offset, noffset - *offset);
2046 		pos += noffset - *offset;
2047 		*offset = noffset;
2048 	}
2049 
2050 	ext_rates_len = num_rates - supp_rates_len;
2051 	if (ext_rates_len > 0) {
2052 		if (end - pos < 2 + ext_rates_len)
2053 			goto out_err;
2054 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
2055 		*pos++ = ext_rates_len;
2056 		memcpy(pos, rates + supp_rates_len, ext_rates_len);
2057 		pos += ext_rates_len;
2058 	}
2059 
2060 	if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
2061 		if (end - pos < 3)
2062 			goto out_err;
2063 		*pos++ = WLAN_EID_DS_PARAMS;
2064 		*pos++ = 1;
2065 		*pos++ = ieee80211_frequency_to_channel(
2066 				chandef->chan->center_freq);
2067 	}
2068 
2069 	if (flags & IEEE80211_PROBE_FLAG_MIN_CONTENT)
2070 		goto done;
2071 
2072 	/* insert custom IEs that go before HT */
2073 	if (ie && ie_len) {
2074 		static const u8 before_ht[] = {
2075 			/*
2076 			 * no need to list the ones split off already
2077 			 * (or generated here)
2078 			 */
2079 			WLAN_EID_DS_PARAMS,
2080 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
2081 		};
2082 		noffset = ieee80211_ie_split(ie, ie_len,
2083 					     before_ht, ARRAY_SIZE(before_ht),
2084 					     *offset);
2085 		if (end - pos < noffset - *offset)
2086 			goto out_err;
2087 		memcpy(pos, ie + *offset, noffset - *offset);
2088 		pos += noffset - *offset;
2089 		*offset = noffset;
2090 	}
2091 
2092 	if (sband->ht_cap.ht_supported) {
2093 		if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
2094 			goto out_err;
2095 		pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
2096 						sband->ht_cap.cap);
2097 	}
2098 
2099 	/* insert custom IEs that go before VHT */
2100 	if (ie && ie_len) {
2101 		static const u8 before_vht[] = {
2102 			/*
2103 			 * no need to list the ones split off already
2104 			 * (or generated here)
2105 			 */
2106 			WLAN_EID_BSS_COEX_2040,
2107 			WLAN_EID_EXT_CAPABILITY,
2108 			WLAN_EID_SSID_LIST,
2109 			WLAN_EID_CHANNEL_USAGE,
2110 			WLAN_EID_INTERWORKING,
2111 			WLAN_EID_MESH_ID,
2112 			/* 60 GHz (Multi-band, DMG, MMS) can't happen */
2113 		};
2114 		noffset = ieee80211_ie_split(ie, ie_len,
2115 					     before_vht, ARRAY_SIZE(before_vht),
2116 					     *offset);
2117 		if (end - pos < noffset - *offset)
2118 			goto out_err;
2119 		memcpy(pos, ie + *offset, noffset - *offset);
2120 		pos += noffset - *offset;
2121 		*offset = noffset;
2122 	}
2123 
2124 	/* Check if any channel in this sband supports at least 80 MHz */
2125 	for (i = 0; i < sband->n_channels; i++) {
2126 		if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
2127 						IEEE80211_CHAN_NO_80MHZ))
2128 			continue;
2129 
2130 		have_80mhz = true;
2131 		break;
2132 	}
2133 
2134 	if (sband->vht_cap.vht_supported && have_80mhz) {
2135 		if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
2136 			goto out_err;
2137 		pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
2138 						 sband->vht_cap.cap);
2139 	}
2140 
2141 	/* insert custom IEs that go before HE */
2142 	if (ie && ie_len) {
2143 		static const u8 before_he[] = {
2144 			/*
2145 			 * no need to list the ones split off before VHT
2146 			 * or generated here
2147 			 */
2148 			WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_REQ_PARAMS,
2149 			WLAN_EID_AP_CSN,
2150 			/* TODO: add 11ah/11aj/11ak elements */
2151 		};
2152 		noffset = ieee80211_ie_split(ie, ie_len,
2153 					     before_he, ARRAY_SIZE(before_he),
2154 					     *offset);
2155 		if (end - pos < noffset - *offset)
2156 			goto out_err;
2157 		memcpy(pos, ie + *offset, noffset - *offset);
2158 		pos += noffset - *offset;
2159 		*offset = noffset;
2160 	}
2161 
2162 	he_cap = ieee80211_get_he_iftype_cap(sband,
2163 					     ieee80211_vif_type_p2p(&sdata->vif));
2164 	if (he_cap &&
2165 	    cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band),
2166 					 IEEE80211_CHAN_NO_HE)) {
2167 		pos = ieee80211_ie_build_he_cap(0, pos, he_cap, end);
2168 		if (!pos)
2169 			goto out_err;
2170 	}
2171 
2172 	eht_cap = ieee80211_get_eht_iftype_cap(sband,
2173 					       ieee80211_vif_type_p2p(&sdata->vif));
2174 
2175 	if (eht_cap &&
2176 	    cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band),
2177 					 IEEE80211_CHAN_NO_HE |
2178 					 IEEE80211_CHAN_NO_EHT)) {
2179 		pos = ieee80211_ie_build_eht_cap(pos, he_cap, eht_cap, end,
2180 						 sdata->vif.type == NL80211_IFTYPE_AP);
2181 		if (!pos)
2182 			goto out_err;
2183 	}
2184 
2185 	if (cfg80211_any_usable_channels(local->hw.wiphy,
2186 					 BIT(NL80211_BAND_6GHZ),
2187 					 IEEE80211_CHAN_NO_HE)) {
2188 		struct ieee80211_supported_band *sband6;
2189 
2190 		sband6 = local->hw.wiphy->bands[NL80211_BAND_6GHZ];
2191 		he_cap = ieee80211_get_he_iftype_cap(sband6,
2192 				ieee80211_vif_type_p2p(&sdata->vif));
2193 
2194 		if (he_cap) {
2195 			enum nl80211_iftype iftype =
2196 				ieee80211_vif_type_p2p(&sdata->vif);
2197 			__le16 cap = ieee80211_get_he_6ghz_capa(sband6, iftype);
2198 
2199 			pos = ieee80211_write_he_6ghz_cap(pos, cap, end);
2200 		}
2201 	}
2202 
2203 	/*
2204 	 * If adding more here, adjust code in main.c
2205 	 * that calculates local->scan_ies_len.
2206 	 */
2207 
2208 	return pos - buffer;
2209  out_err:
2210 	WARN_ONCE(1, "not enough space for preq IEs\n");
2211  done:
2212 	return pos - buffer;
2213 }
2214 
2215 int ieee80211_build_preq_ies(struct ieee80211_sub_if_data *sdata, u8 *buffer,
2216 			     size_t buffer_len,
2217 			     struct ieee80211_scan_ies *ie_desc,
2218 			     const u8 *ie, size_t ie_len,
2219 			     u8 bands_used, u32 *rate_masks,
2220 			     struct cfg80211_chan_def *chandef,
2221 			     u32 flags)
2222 {
2223 	size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
2224 	int i;
2225 
2226 	memset(ie_desc, 0, sizeof(*ie_desc));
2227 
2228 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
2229 		if (bands_used & BIT(i)) {
2230 			pos += ieee80211_build_preq_ies_band(sdata,
2231 							     buffer + pos,
2232 							     buffer_len - pos,
2233 							     ie, ie_len, i,
2234 							     rate_masks[i],
2235 							     chandef,
2236 							     &custom_ie_offset,
2237 							     flags);
2238 			ie_desc->ies[i] = buffer + old_pos;
2239 			ie_desc->len[i] = pos - old_pos;
2240 			old_pos = pos;
2241 		}
2242 	}
2243 
2244 	/* add any remaining custom IEs */
2245 	if (ie && ie_len) {
2246 		if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
2247 			      "not enough space for preq custom IEs\n"))
2248 			return pos;
2249 		memcpy(buffer + pos, ie + custom_ie_offset,
2250 		       ie_len - custom_ie_offset);
2251 		ie_desc->common_ies = buffer + pos;
2252 		ie_desc->common_ie_len = ie_len - custom_ie_offset;
2253 		pos += ie_len - custom_ie_offset;
2254 	}
2255 
2256 	return pos;
2257 };
2258 
2259 struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
2260 					  const u8 *src, const u8 *dst,
2261 					  u32 ratemask,
2262 					  struct ieee80211_channel *chan,
2263 					  const u8 *ssid, size_t ssid_len,
2264 					  const u8 *ie, size_t ie_len,
2265 					  u32 flags)
2266 {
2267 	struct ieee80211_local *local = sdata->local;
2268 	struct cfg80211_chan_def chandef;
2269 	struct sk_buff *skb;
2270 	struct ieee80211_mgmt *mgmt;
2271 	int ies_len;
2272 	u32 rate_masks[NUM_NL80211_BANDS] = {};
2273 	struct ieee80211_scan_ies dummy_ie_desc;
2274 
2275 	/*
2276 	 * Do not send DS Channel parameter for directed probe requests
2277 	 * in order to maximize the chance that we get a response.  Some
2278 	 * badly-behaved APs don't respond when this parameter is included.
2279 	 */
2280 	chandef.width = sdata->vif.bss_conf.chandef.width;
2281 	if (flags & IEEE80211_PROBE_FLAG_DIRECTED)
2282 		chandef.chan = NULL;
2283 	else
2284 		chandef.chan = chan;
2285 
2286 	skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
2287 				     local->scan_ies_len + ie_len);
2288 	if (!skb)
2289 		return NULL;
2290 
2291 	rate_masks[chan->band] = ratemask;
2292 	ies_len = ieee80211_build_preq_ies(sdata, skb_tail_pointer(skb),
2293 					   skb_tailroom(skb), &dummy_ie_desc,
2294 					   ie, ie_len, BIT(chan->band),
2295 					   rate_masks, &chandef, flags);
2296 	skb_put(skb, ies_len);
2297 
2298 	if (dst) {
2299 		mgmt = (struct ieee80211_mgmt *) skb->data;
2300 		memcpy(mgmt->da, dst, ETH_ALEN);
2301 		memcpy(mgmt->bssid, dst, ETH_ALEN);
2302 	}
2303 
2304 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
2305 
2306 	return skb;
2307 }
2308 
2309 u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
2310 			    struct ieee802_11_elems *elems,
2311 			    enum nl80211_band band, u32 *basic_rates)
2312 {
2313 	struct ieee80211_supported_band *sband;
2314 	size_t num_rates;
2315 	u32 supp_rates, rate_flags;
2316 	int i, j, shift;
2317 
2318 	sband = sdata->local->hw.wiphy->bands[band];
2319 	if (WARN_ON(!sband))
2320 		return 1;
2321 
2322 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
2323 	shift = ieee80211_vif_get_shift(&sdata->vif);
2324 
2325 	num_rates = sband->n_bitrates;
2326 	supp_rates = 0;
2327 	for (i = 0; i < elems->supp_rates_len +
2328 		     elems->ext_supp_rates_len; i++) {
2329 		u8 rate = 0;
2330 		int own_rate;
2331 		bool is_basic;
2332 		if (i < elems->supp_rates_len)
2333 			rate = elems->supp_rates[i];
2334 		else if (elems->ext_supp_rates)
2335 			rate = elems->ext_supp_rates
2336 				[i - elems->supp_rates_len];
2337 		own_rate = 5 * (rate & 0x7f);
2338 		is_basic = !!(rate & 0x80);
2339 
2340 		if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2341 			continue;
2342 
2343 		for (j = 0; j < num_rates; j++) {
2344 			int brate;
2345 			if ((rate_flags & sband->bitrates[j].flags)
2346 			    != rate_flags)
2347 				continue;
2348 
2349 			brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
2350 					     1 << shift);
2351 
2352 			if (brate == own_rate) {
2353 				supp_rates |= BIT(j);
2354 				if (basic_rates && is_basic)
2355 					*basic_rates |= BIT(j);
2356 			}
2357 		}
2358 	}
2359 	return supp_rates;
2360 }
2361 
2362 void ieee80211_stop_device(struct ieee80211_local *local)
2363 {
2364 	ieee80211_led_radio(local, false);
2365 	ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
2366 
2367 	cancel_work_sync(&local->reconfig_filter);
2368 
2369 	flush_workqueue(local->workqueue);
2370 	drv_stop(local);
2371 }
2372 
2373 static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
2374 					   bool aborted)
2375 {
2376 	/* It's possible that we don't handle the scan completion in
2377 	 * time during suspend, so if it's still marked as completed
2378 	 * here, queue the work and flush it to clean things up.
2379 	 * Instead of calling the worker function directly here, we
2380 	 * really queue it to avoid potential races with other flows
2381 	 * scheduling the same work.
2382 	 */
2383 	if (test_bit(SCAN_COMPLETED, &local->scanning)) {
2384 		/* If coming from reconfiguration failure, abort the scan so
2385 		 * we don't attempt to continue a partial HW scan - which is
2386 		 * possible otherwise if (e.g.) the 2.4 GHz portion was the
2387 		 * completed scan, and a 5 GHz portion is still pending.
2388 		 */
2389 		if (aborted)
2390 			set_bit(SCAN_ABORTED, &local->scanning);
2391 		ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
2392 		flush_delayed_work(&local->scan_work);
2393 	}
2394 }
2395 
2396 static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
2397 {
2398 	struct ieee80211_sub_if_data *sdata;
2399 	struct ieee80211_chanctx *ctx;
2400 
2401 	/*
2402 	 * We get here if during resume the device can't be restarted properly.
2403 	 * We might also get here if this happens during HW reset, which is a
2404 	 * slightly different situation and we need to drop all connections in
2405 	 * the latter case.
2406 	 *
2407 	 * Ask cfg80211 to turn off all interfaces, this will result in more
2408 	 * warnings but at least we'll then get into a clean stopped state.
2409 	 */
2410 
2411 	local->resuming = false;
2412 	local->suspended = false;
2413 	local->in_reconfig = false;
2414 
2415 	ieee80211_flush_completed_scan(local, true);
2416 
2417 	/* scheduled scan clearly can't be running any more, but tell
2418 	 * cfg80211 and clear local state
2419 	 */
2420 	ieee80211_sched_scan_end(local);
2421 
2422 	list_for_each_entry(sdata, &local->interfaces, list)
2423 		sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
2424 
2425 	/* Mark channel contexts as not being in the driver any more to avoid
2426 	 * removing them from the driver during the shutdown process...
2427 	 */
2428 	mutex_lock(&local->chanctx_mtx);
2429 	list_for_each_entry(ctx, &local->chanctx_list, list)
2430 		ctx->driver_present = false;
2431 	mutex_unlock(&local->chanctx_mtx);
2432 }
2433 
2434 static void ieee80211_assign_chanctx(struct ieee80211_local *local,
2435 				     struct ieee80211_sub_if_data *sdata,
2436 				     struct ieee80211_link_data *link)
2437 {
2438 	struct ieee80211_chanctx_conf *conf;
2439 	struct ieee80211_chanctx *ctx;
2440 
2441 	if (!local->use_chanctx)
2442 		return;
2443 
2444 	mutex_lock(&local->chanctx_mtx);
2445 	conf = rcu_dereference_protected(link->conf->chanctx_conf,
2446 					 lockdep_is_held(&local->chanctx_mtx));
2447 	if (conf) {
2448 		ctx = container_of(conf, struct ieee80211_chanctx, conf);
2449 		drv_assign_vif_chanctx(local, sdata, link->conf, ctx);
2450 	}
2451 	mutex_unlock(&local->chanctx_mtx);
2452 }
2453 
2454 static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
2455 {
2456 	struct ieee80211_local *local = sdata->local;
2457 	struct sta_info *sta;
2458 
2459 	/* add STAs back */
2460 	mutex_lock(&local->sta_mtx);
2461 	list_for_each_entry(sta, &local->sta_list, list) {
2462 		enum ieee80211_sta_state state;
2463 
2464 		if (!sta->uploaded || sta->sdata != sdata)
2465 			continue;
2466 
2467 		for (state = IEEE80211_STA_NOTEXIST;
2468 		     state < sta->sta_state; state++)
2469 			WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
2470 					      state + 1));
2471 	}
2472 	mutex_unlock(&local->sta_mtx);
2473 }
2474 
2475 static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
2476 {
2477 	struct cfg80211_nan_func *func, **funcs;
2478 	int res, id, i = 0;
2479 
2480 	res = drv_start_nan(sdata->local, sdata,
2481 			    &sdata->u.nan.conf);
2482 	if (WARN_ON(res))
2483 		return res;
2484 
2485 	funcs = kcalloc(sdata->local->hw.max_nan_de_entries + 1,
2486 			sizeof(*funcs),
2487 			GFP_KERNEL);
2488 	if (!funcs)
2489 		return -ENOMEM;
2490 
2491 	/* Add all the functions:
2492 	 * This is a little bit ugly. We need to call a potentially sleeping
2493 	 * callback for each NAN function, so we can't hold the spinlock.
2494 	 */
2495 	spin_lock_bh(&sdata->u.nan.func_lock);
2496 
2497 	idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
2498 		funcs[i++] = func;
2499 
2500 	spin_unlock_bh(&sdata->u.nan.func_lock);
2501 
2502 	for (i = 0; funcs[i]; i++) {
2503 		res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
2504 		if (WARN_ON(res))
2505 			ieee80211_nan_func_terminated(&sdata->vif,
2506 						      funcs[i]->instance_id,
2507 						      NL80211_NAN_FUNC_TERM_REASON_ERROR,
2508 						      GFP_KERNEL);
2509 	}
2510 
2511 	kfree(funcs);
2512 
2513 	return 0;
2514 }
2515 
2516 int ieee80211_reconfig(struct ieee80211_local *local)
2517 {
2518 	struct ieee80211_hw *hw = &local->hw;
2519 	struct ieee80211_sub_if_data *sdata;
2520 	struct ieee80211_chanctx *ctx;
2521 	struct sta_info *sta;
2522 	int res, i;
2523 	bool reconfig_due_to_wowlan = false;
2524 	struct ieee80211_sub_if_data *sched_scan_sdata;
2525 	struct cfg80211_sched_scan_request *sched_scan_req;
2526 	bool sched_scan_stopped = false;
2527 	bool suspended = local->suspended;
2528 	bool in_reconfig = false;
2529 
2530 	/* nothing to do if HW shouldn't run */
2531 	if (!local->open_count)
2532 		goto wake_up;
2533 
2534 #ifdef CONFIG_PM
2535 	if (suspended)
2536 		local->resuming = true;
2537 
2538 	if (local->wowlan) {
2539 		/*
2540 		 * In the wowlan case, both mac80211 and the device
2541 		 * are functional when the resume op is called, so
2542 		 * clear local->suspended so the device could operate
2543 		 * normally (e.g. pass rx frames).
2544 		 */
2545 		local->suspended = false;
2546 		res = drv_resume(local);
2547 		local->wowlan = false;
2548 		if (res < 0) {
2549 			local->resuming = false;
2550 			return res;
2551 		}
2552 		if (res == 0)
2553 			goto wake_up;
2554 		WARN_ON(res > 1);
2555 		/*
2556 		 * res is 1, which means the driver requested
2557 		 * to go through a regular reset on wakeup.
2558 		 * restore local->suspended in this case.
2559 		 */
2560 		reconfig_due_to_wowlan = true;
2561 		local->suspended = true;
2562 	}
2563 #endif
2564 
2565 	/*
2566 	 * In case of hw_restart during suspend (without wowlan),
2567 	 * cancel restart work, as we are reconfiguring the device
2568 	 * anyway.
2569 	 * Note that restart_work is scheduled on a frozen workqueue,
2570 	 * so we can't deadlock in this case.
2571 	 */
2572 	if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
2573 		cancel_work_sync(&local->restart_work);
2574 
2575 	local->started = false;
2576 
2577 	/*
2578 	 * Upon resume hardware can sometimes be goofy due to
2579 	 * various platform / driver / bus issues, so restarting
2580 	 * the device may at times not work immediately. Propagate
2581 	 * the error.
2582 	 */
2583 	res = drv_start(local);
2584 	if (res) {
2585 		if (suspended)
2586 			WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
2587 		else
2588 			WARN(1, "Hardware became unavailable during restart.\n");
2589 		ieee80211_handle_reconfig_failure(local);
2590 		return res;
2591 	}
2592 
2593 	/* setup fragmentation threshold */
2594 	drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
2595 
2596 	/* setup RTS threshold */
2597 	drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
2598 
2599 	/* reset coverage class */
2600 	drv_set_coverage_class(local, hw->wiphy->coverage_class);
2601 
2602 	ieee80211_led_radio(local, true);
2603 	ieee80211_mod_tpt_led_trig(local,
2604 				   IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
2605 
2606 	/* add interfaces */
2607 	sdata = wiphy_dereference(local->hw.wiphy, local->monitor_sdata);
2608 	if (sdata) {
2609 		/* in HW restart it exists already */
2610 		WARN_ON(local->resuming);
2611 		res = drv_add_interface(local, sdata);
2612 		if (WARN_ON(res)) {
2613 			RCU_INIT_POINTER(local->monitor_sdata, NULL);
2614 			synchronize_net();
2615 			kfree(sdata);
2616 		}
2617 	}
2618 
2619 	list_for_each_entry(sdata, &local->interfaces, list) {
2620 		if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2621 		    sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2622 		    ieee80211_sdata_running(sdata)) {
2623 			res = drv_add_interface(local, sdata);
2624 			if (WARN_ON(res))
2625 				break;
2626 		}
2627 	}
2628 
2629 	/* If adding any of the interfaces failed above, roll back and
2630 	 * report failure.
2631 	 */
2632 	if (res) {
2633 		list_for_each_entry_continue_reverse(sdata, &local->interfaces,
2634 						     list)
2635 			if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2636 			    sdata->vif.type != NL80211_IFTYPE_MONITOR &&
2637 			    ieee80211_sdata_running(sdata))
2638 				drv_remove_interface(local, sdata);
2639 		ieee80211_handle_reconfig_failure(local);
2640 		return res;
2641 	}
2642 
2643 	/* add channel contexts */
2644 	if (local->use_chanctx) {
2645 		mutex_lock(&local->chanctx_mtx);
2646 		list_for_each_entry(ctx, &local->chanctx_list, list)
2647 			if (ctx->replace_state !=
2648 			    IEEE80211_CHANCTX_REPLACES_OTHER)
2649 				WARN_ON(drv_add_chanctx(local, ctx));
2650 		mutex_unlock(&local->chanctx_mtx);
2651 
2652 		sdata = wiphy_dereference(local->hw.wiphy,
2653 					  local->monitor_sdata);
2654 		if (sdata && ieee80211_sdata_running(sdata))
2655 			ieee80211_assign_chanctx(local, sdata, &sdata->deflink);
2656 	}
2657 
2658 	/* reconfigure hardware */
2659 	ieee80211_hw_config(local, ~0);
2660 
2661 	ieee80211_configure_filter(local);
2662 
2663 	/* Finally also reconfigure all the BSS information */
2664 	list_for_each_entry(sdata, &local->interfaces, list) {
2665 		unsigned int link_id;
2666 		u32 changed;
2667 
2668 		if (!ieee80211_sdata_running(sdata))
2669 			continue;
2670 
2671 		sdata_lock(sdata);
2672 		for (link_id = 0;
2673 		     link_id < ARRAY_SIZE(sdata->vif.link_conf);
2674 		     link_id++) {
2675 			struct ieee80211_link_data *link;
2676 
2677 			link = sdata_dereference(sdata->link[link_id], sdata);
2678 			if (link)
2679 				ieee80211_assign_chanctx(local, sdata, link);
2680 		}
2681 
2682 		switch (sdata->vif.type) {
2683 		case NL80211_IFTYPE_AP_VLAN:
2684 		case NL80211_IFTYPE_MONITOR:
2685 			break;
2686 		case NL80211_IFTYPE_ADHOC:
2687 			if (sdata->vif.cfg.ibss_joined)
2688 				WARN_ON(drv_join_ibss(local, sdata));
2689 			fallthrough;
2690 		default:
2691 			ieee80211_reconfig_stations(sdata);
2692 			fallthrough;
2693 		case NL80211_IFTYPE_AP: /* AP stations are handled later */
2694 			for (i = 0; i < IEEE80211_NUM_ACS; i++)
2695 				drv_conf_tx(local, &sdata->deflink, i,
2696 					    &sdata->deflink.tx_conf[i]);
2697 			break;
2698 		}
2699 		sdata_unlock(sdata);
2700 
2701 		/* common change flags for all interface types */
2702 		changed = BSS_CHANGED_ERP_CTS_PROT |
2703 			  BSS_CHANGED_ERP_PREAMBLE |
2704 			  BSS_CHANGED_ERP_SLOT |
2705 			  BSS_CHANGED_HT |
2706 			  BSS_CHANGED_BASIC_RATES |
2707 			  BSS_CHANGED_BEACON_INT |
2708 			  BSS_CHANGED_BSSID |
2709 			  BSS_CHANGED_CQM |
2710 			  BSS_CHANGED_QOS |
2711 			  BSS_CHANGED_IDLE |
2712 			  BSS_CHANGED_TXPOWER |
2713 			  BSS_CHANGED_MCAST_RATE;
2714 
2715 		if (sdata->vif.bss_conf.mu_mimo_owner)
2716 			changed |= BSS_CHANGED_MU_GROUPS;
2717 
2718 		switch (sdata->vif.type) {
2719 		case NL80211_IFTYPE_STATION:
2720 			changed |= BSS_CHANGED_ASSOC |
2721 				   BSS_CHANGED_ARP_FILTER |
2722 				   BSS_CHANGED_PS;
2723 
2724 			/* Re-send beacon info report to the driver */
2725 			if (sdata->deflink.u.mgd.have_beacon)
2726 				changed |= BSS_CHANGED_BEACON_INFO;
2727 
2728 			if (sdata->vif.bss_conf.max_idle_period ||
2729 			    sdata->vif.bss_conf.protected_keep_alive)
2730 				changed |= BSS_CHANGED_KEEP_ALIVE;
2731 
2732 			sdata_lock(sdata);
2733 			ieee80211_bss_info_change_notify(sdata, changed);
2734 			sdata_unlock(sdata);
2735 			break;
2736 		case NL80211_IFTYPE_OCB:
2737 			changed |= BSS_CHANGED_OCB;
2738 			ieee80211_bss_info_change_notify(sdata, changed);
2739 			break;
2740 		case NL80211_IFTYPE_ADHOC:
2741 			changed |= BSS_CHANGED_IBSS;
2742 			fallthrough;
2743 		case NL80211_IFTYPE_AP:
2744 			changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
2745 
2746 			if (sdata->vif.bss_conf.ftm_responder == 1 &&
2747 			    wiphy_ext_feature_isset(sdata->local->hw.wiphy,
2748 					NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
2749 				changed |= BSS_CHANGED_FTM_RESPONDER;
2750 
2751 			if (sdata->vif.type == NL80211_IFTYPE_AP) {
2752 				changed |= BSS_CHANGED_AP_PROBE_RESP;
2753 
2754 				if (rcu_access_pointer(sdata->deflink.u.ap.beacon))
2755 					drv_start_ap(local, sdata,
2756 						     sdata->deflink.conf);
2757 			}
2758 			fallthrough;
2759 		case NL80211_IFTYPE_MESH_POINT:
2760 			if (sdata->vif.bss_conf.enable_beacon) {
2761 				changed |= BSS_CHANGED_BEACON |
2762 					   BSS_CHANGED_BEACON_ENABLED;
2763 				ieee80211_bss_info_change_notify(sdata, changed);
2764 			}
2765 			break;
2766 		case NL80211_IFTYPE_NAN:
2767 			res = ieee80211_reconfig_nan(sdata);
2768 			if (res < 0) {
2769 				ieee80211_handle_reconfig_failure(local);
2770 				return res;
2771 			}
2772 			break;
2773 		case NL80211_IFTYPE_AP_VLAN:
2774 		case NL80211_IFTYPE_MONITOR:
2775 		case NL80211_IFTYPE_P2P_DEVICE:
2776 			/* nothing to do */
2777 			break;
2778 		case NL80211_IFTYPE_UNSPECIFIED:
2779 		case NUM_NL80211_IFTYPES:
2780 		case NL80211_IFTYPE_P2P_CLIENT:
2781 		case NL80211_IFTYPE_P2P_GO:
2782 		case NL80211_IFTYPE_WDS:
2783 			WARN_ON(1);
2784 			break;
2785 		}
2786 	}
2787 
2788 	ieee80211_recalc_ps(local);
2789 
2790 	/*
2791 	 * The sta might be in psm against the ap (e.g. because
2792 	 * this was the state before a hw restart), so we
2793 	 * explicitly send a null packet in order to make sure
2794 	 * it'll sync against the ap (and get out of psm).
2795 	 */
2796 	if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
2797 		list_for_each_entry(sdata, &local->interfaces, list) {
2798 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
2799 				continue;
2800 			if (!sdata->u.mgd.associated)
2801 				continue;
2802 
2803 			ieee80211_send_nullfunc(local, sdata, false);
2804 		}
2805 	}
2806 
2807 	/* APs are now beaconing, add back stations */
2808 	list_for_each_entry(sdata, &local->interfaces, list) {
2809 		if (!ieee80211_sdata_running(sdata))
2810 			continue;
2811 
2812 		sdata_lock(sdata);
2813 		switch (sdata->vif.type) {
2814 		case NL80211_IFTYPE_AP_VLAN:
2815 		case NL80211_IFTYPE_AP:
2816 			ieee80211_reconfig_stations(sdata);
2817 			break;
2818 		default:
2819 			break;
2820 		}
2821 		sdata_unlock(sdata);
2822 	}
2823 
2824 	/* add back keys */
2825 	list_for_each_entry(sdata, &local->interfaces, list)
2826 		ieee80211_reenable_keys(sdata);
2827 
2828 	/* Reconfigure sched scan if it was interrupted by FW restart */
2829 	mutex_lock(&local->mtx);
2830 	sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
2831 						lockdep_is_held(&local->mtx));
2832 	sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
2833 						lockdep_is_held(&local->mtx));
2834 	if (sched_scan_sdata && sched_scan_req)
2835 		/*
2836 		 * Sched scan stopped, but we don't want to report it. Instead,
2837 		 * we're trying to reschedule. However, if more than one scan
2838 		 * plan was set, we cannot reschedule since we don't know which
2839 		 * scan plan was currently running (and some scan plans may have
2840 		 * already finished).
2841 		 */
2842 		if (sched_scan_req->n_scan_plans > 1 ||
2843 		    __ieee80211_request_sched_scan_start(sched_scan_sdata,
2844 							 sched_scan_req)) {
2845 			RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
2846 			RCU_INIT_POINTER(local->sched_scan_req, NULL);
2847 			sched_scan_stopped = true;
2848 		}
2849 	mutex_unlock(&local->mtx);
2850 
2851 	if (sched_scan_stopped)
2852 		cfg80211_sched_scan_stopped_locked(local->hw.wiphy, 0);
2853 
2854  wake_up:
2855 
2856 	if (local->monitors == local->open_count && local->monitors > 0)
2857 		ieee80211_add_virtual_monitor(local);
2858 
2859 	/*
2860 	 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
2861 	 * sessions can be established after a resume.
2862 	 *
2863 	 * Also tear down aggregation sessions since reconfiguring
2864 	 * them in a hardware restart scenario is not easily done
2865 	 * right now, and the hardware will have lost information
2866 	 * about the sessions, but we and the AP still think they
2867 	 * are active. This is really a workaround though.
2868 	 */
2869 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
2870 		mutex_lock(&local->sta_mtx);
2871 
2872 		list_for_each_entry(sta, &local->sta_list, list) {
2873 			if (!local->resuming)
2874 				ieee80211_sta_tear_down_BA_sessions(
2875 						sta, AGG_STOP_LOCAL_REQUEST);
2876 			clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
2877 		}
2878 
2879 		mutex_unlock(&local->sta_mtx);
2880 	}
2881 
2882 	/*
2883 	 * If this is for hw restart things are still running.
2884 	 * We may want to change that later, however.
2885 	 */
2886 	if (local->open_count && (!suspended || reconfig_due_to_wowlan))
2887 		drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
2888 
2889 	if (local->in_reconfig) {
2890 		in_reconfig = local->in_reconfig;
2891 		local->in_reconfig = false;
2892 		barrier();
2893 
2894 		/* Restart deferred ROCs */
2895 		mutex_lock(&local->mtx);
2896 		ieee80211_start_next_roc(local);
2897 		mutex_unlock(&local->mtx);
2898 
2899 		/* Requeue all works */
2900 		list_for_each_entry(sdata, &local->interfaces, list)
2901 			ieee80211_queue_work(&local->hw, &sdata->work);
2902 	}
2903 
2904 	ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
2905 					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
2906 					false);
2907 
2908 	if (in_reconfig) {
2909 		list_for_each_entry(sdata, &local->interfaces, list) {
2910 			if (!ieee80211_sdata_running(sdata))
2911 				continue;
2912 			if (sdata->vif.type == NL80211_IFTYPE_STATION)
2913 				ieee80211_sta_restart(sdata);
2914 		}
2915 	}
2916 
2917 	if (!suspended)
2918 		return 0;
2919 
2920 #ifdef CONFIG_PM
2921 	/* first set suspended false, then resuming */
2922 	local->suspended = false;
2923 	mb();
2924 	local->resuming = false;
2925 
2926 	ieee80211_flush_completed_scan(local, false);
2927 
2928 	if (local->open_count && !reconfig_due_to_wowlan)
2929 		drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
2930 
2931 	list_for_each_entry(sdata, &local->interfaces, list) {
2932 		if (!ieee80211_sdata_running(sdata))
2933 			continue;
2934 		if (sdata->vif.type == NL80211_IFTYPE_STATION)
2935 			ieee80211_sta_restart(sdata);
2936 	}
2937 
2938 	mod_timer(&local->sta_cleanup, jiffies + 1);
2939 #else
2940 	WARN_ON(1);
2941 #endif
2942 
2943 	return 0;
2944 }
2945 
2946 static void ieee80211_reconfig_disconnect(struct ieee80211_vif *vif, u8 flag)
2947 {
2948 	struct ieee80211_sub_if_data *sdata;
2949 	struct ieee80211_local *local;
2950 	struct ieee80211_key *key;
2951 
2952 	if (WARN_ON(!vif))
2953 		return;
2954 
2955 	sdata = vif_to_sdata(vif);
2956 	local = sdata->local;
2957 
2958 	if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_RESUME &&
2959 		    !local->resuming))
2960 		return;
2961 
2962 	if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_HW_RESTART &&
2963 		    !local->in_reconfig))
2964 		return;
2965 
2966 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2967 		return;
2968 
2969 	sdata->flags |= flag;
2970 
2971 	mutex_lock(&local->key_mtx);
2972 	list_for_each_entry(key, &sdata->key_list, list)
2973 		key->flags |= KEY_FLAG_TAINTED;
2974 	mutex_unlock(&local->key_mtx);
2975 }
2976 
2977 void ieee80211_hw_restart_disconnect(struct ieee80211_vif *vif)
2978 {
2979 	ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_HW_RESTART);
2980 }
2981 EXPORT_SYMBOL_GPL(ieee80211_hw_restart_disconnect);
2982 
2983 void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
2984 {
2985 	ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_RESUME);
2986 }
2987 EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
2988 
2989 void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata,
2990 			   struct ieee80211_link_data *link)
2991 {
2992 	struct ieee80211_local *local = sdata->local;
2993 	struct ieee80211_chanctx_conf *chanctx_conf;
2994 	struct ieee80211_chanctx *chanctx;
2995 
2996 	mutex_lock(&local->chanctx_mtx);
2997 
2998 	chanctx_conf = rcu_dereference_protected(link->conf->chanctx_conf,
2999 						 lockdep_is_held(&local->chanctx_mtx));
3000 
3001 	/*
3002 	 * This function can be called from a work, thus it may be possible
3003 	 * that the chanctx_conf is removed (due to a disconnection, for
3004 	 * example).
3005 	 * So nothing should be done in such case.
3006 	 */
3007 	if (!chanctx_conf)
3008 		goto unlock;
3009 
3010 	chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
3011 	ieee80211_recalc_smps_chanctx(local, chanctx);
3012  unlock:
3013 	mutex_unlock(&local->chanctx_mtx);
3014 }
3015 
3016 void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata,
3017 				  int link_id)
3018 {
3019 	struct ieee80211_local *local = sdata->local;
3020 	struct ieee80211_chanctx_conf *chanctx_conf;
3021 	struct ieee80211_chanctx *chanctx;
3022 	int i;
3023 
3024 	mutex_lock(&local->chanctx_mtx);
3025 
3026 	for (i = 0; i < ARRAY_SIZE(sdata->vif.link_conf); i++) {
3027 		struct ieee80211_bss_conf *bss_conf;
3028 
3029 		if (link_id >= 0 && link_id != i)
3030 			continue;
3031 
3032 		rcu_read_lock();
3033 		bss_conf = rcu_dereference(sdata->vif.link_conf[i]);
3034 		if (!bss_conf) {
3035 			rcu_read_unlock();
3036 			continue;
3037 		}
3038 
3039 		chanctx_conf = rcu_dereference_protected(bss_conf->chanctx_conf,
3040 							 lockdep_is_held(&local->chanctx_mtx));
3041 		/*
3042 		 * Since we hold the chanctx_mtx (checked above)
3043 		 * we can take the chanctx_conf pointer out of the
3044 		 * RCU critical section, it cannot go away without
3045 		 * the mutex. Just the way we reached it could - in
3046 		 * theory - go away, but we don't really care and
3047 		 * it really shouldn't happen anyway.
3048 		 */
3049 		rcu_read_unlock();
3050 
3051 		if (!chanctx_conf)
3052 			goto unlock;
3053 
3054 		chanctx = container_of(chanctx_conf, struct ieee80211_chanctx,
3055 				       conf);
3056 		ieee80211_recalc_chanctx_min_def(local, chanctx);
3057 	}
3058  unlock:
3059 	mutex_unlock(&local->chanctx_mtx);
3060 }
3061 
3062 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
3063 {
3064 	size_t pos = offset;
3065 
3066 	while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
3067 		pos += 2 + ies[pos + 1];
3068 
3069 	return pos;
3070 }
3071 
3072 u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
3073 			      u16 cap)
3074 {
3075 	__le16 tmp;
3076 
3077 	*pos++ = WLAN_EID_HT_CAPABILITY;
3078 	*pos++ = sizeof(struct ieee80211_ht_cap);
3079 	memset(pos, 0, sizeof(struct ieee80211_ht_cap));
3080 
3081 	/* capability flags */
3082 	tmp = cpu_to_le16(cap);
3083 	memcpy(pos, &tmp, sizeof(u16));
3084 	pos += sizeof(u16);
3085 
3086 	/* AMPDU parameters */
3087 	*pos++ = ht_cap->ampdu_factor |
3088 		 (ht_cap->ampdu_density <<
3089 			IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
3090 
3091 	/* MCS set */
3092 	memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
3093 	pos += sizeof(ht_cap->mcs);
3094 
3095 	/* extended capabilities */
3096 	pos += sizeof(__le16);
3097 
3098 	/* BF capabilities */
3099 	pos += sizeof(__le32);
3100 
3101 	/* antenna selection */
3102 	pos += sizeof(u8);
3103 
3104 	return pos;
3105 }
3106 
3107 u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
3108 			       u32 cap)
3109 {
3110 	__le32 tmp;
3111 
3112 	*pos++ = WLAN_EID_VHT_CAPABILITY;
3113 	*pos++ = sizeof(struct ieee80211_vht_cap);
3114 	memset(pos, 0, sizeof(struct ieee80211_vht_cap));
3115 
3116 	/* capability flags */
3117 	tmp = cpu_to_le32(cap);
3118 	memcpy(pos, &tmp, sizeof(u32));
3119 	pos += sizeof(u32);
3120 
3121 	/* VHT MCS set */
3122 	memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
3123 	pos += sizeof(vht_cap->vht_mcs);
3124 
3125 	return pos;
3126 }
3127 
3128 u8 ieee80211_ie_len_he_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
3129 {
3130 	const struct ieee80211_sta_he_cap *he_cap;
3131 	struct ieee80211_supported_band *sband;
3132 	u8 n;
3133 
3134 	sband = ieee80211_get_sband(sdata);
3135 	if (!sband)
3136 		return 0;
3137 
3138 	he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
3139 	if (!he_cap)
3140 		return 0;
3141 
3142 	n = ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem);
3143 	return 2 + 1 +
3144 	       sizeof(he_cap->he_cap_elem) + n +
3145 	       ieee80211_he_ppe_size(he_cap->ppe_thres[0],
3146 				     he_cap->he_cap_elem.phy_cap_info);
3147 }
3148 
3149 u8 *ieee80211_ie_build_he_cap(ieee80211_conn_flags_t disable_flags, u8 *pos,
3150 			      const struct ieee80211_sta_he_cap *he_cap,
3151 			      u8 *end)
3152 {
3153 	struct ieee80211_he_cap_elem elem;
3154 	u8 n;
3155 	u8 ie_len;
3156 	u8 *orig_pos = pos;
3157 
3158 	/* Make sure we have place for the IE */
3159 	/*
3160 	 * TODO: the 1 added is because this temporarily is under the EXTENSION
3161 	 * IE. Get rid of it when it moves.
3162 	 */
3163 	if (!he_cap)
3164 		return orig_pos;
3165 
3166 	/* modify on stack first to calculate 'n' and 'ie_len' correctly */
3167 	elem = he_cap->he_cap_elem;
3168 
3169 	if (disable_flags & IEEE80211_CONN_DISABLE_40MHZ)
3170 		elem.phy_cap_info[0] &=
3171 			~(IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3172 			  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G);
3173 
3174 	if (disable_flags & IEEE80211_CONN_DISABLE_160MHZ)
3175 		elem.phy_cap_info[0] &=
3176 			~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3177 
3178 	if (disable_flags & IEEE80211_CONN_DISABLE_80P80MHZ)
3179 		elem.phy_cap_info[0] &=
3180 			~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
3181 
3182 	n = ieee80211_he_mcs_nss_size(&elem);
3183 	ie_len = 2 + 1 +
3184 		 sizeof(he_cap->he_cap_elem) + n +
3185 		 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
3186 				       he_cap->he_cap_elem.phy_cap_info);
3187 
3188 	if ((end - pos) < ie_len)
3189 		return orig_pos;
3190 
3191 	*pos++ = WLAN_EID_EXTENSION;
3192 	pos++; /* We'll set the size later below */
3193 	*pos++ = WLAN_EID_EXT_HE_CAPABILITY;
3194 
3195 	/* Fixed data */
3196 	memcpy(pos, &elem, sizeof(elem));
3197 	pos += sizeof(elem);
3198 
3199 	memcpy(pos, &he_cap->he_mcs_nss_supp, n);
3200 	pos += n;
3201 
3202 	/* Check if PPE Threshold should be present */
3203 	if ((he_cap->he_cap_elem.phy_cap_info[6] &
3204 	     IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) == 0)
3205 		goto end;
3206 
3207 	/*
3208 	 * Calculate how many PPET16/PPET8 pairs are to come. Algorithm:
3209 	 * (NSS_M1 + 1) x (num of 1 bits in RU_INDEX_BITMASK)
3210 	 */
3211 	n = hweight8(he_cap->ppe_thres[0] &
3212 		     IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
3213 	n *= (1 + ((he_cap->ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) >>
3214 		   IEEE80211_PPE_THRES_NSS_POS));
3215 
3216 	/*
3217 	 * Each pair is 6 bits, and we need to add the 7 "header" bits to the
3218 	 * total size.
3219 	 */
3220 	n = (n * IEEE80211_PPE_THRES_INFO_PPET_SIZE * 2) + 7;
3221 	n = DIV_ROUND_UP(n, 8);
3222 
3223 	/* Copy PPE Thresholds */
3224 	memcpy(pos, &he_cap->ppe_thres, n);
3225 	pos += n;
3226 
3227 end:
3228 	orig_pos[1] = (pos - orig_pos) - 2;
3229 	return pos;
3230 }
3231 
3232 void ieee80211_ie_build_he_6ghz_cap(struct ieee80211_sub_if_data *sdata,
3233 				    enum ieee80211_smps_mode smps_mode,
3234 				    struct sk_buff *skb)
3235 {
3236 	struct ieee80211_supported_band *sband;
3237 	const struct ieee80211_sband_iftype_data *iftd;
3238 	enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
3239 	u8 *pos;
3240 	u16 cap;
3241 
3242 	if (!cfg80211_any_usable_channels(sdata->local->hw.wiphy,
3243 					  BIT(NL80211_BAND_6GHZ),
3244 					  IEEE80211_CHAN_NO_HE))
3245 		return;
3246 
3247 	sband = sdata->local->hw.wiphy->bands[NL80211_BAND_6GHZ];
3248 
3249 	iftd = ieee80211_get_sband_iftype_data(sband, iftype);
3250 	if (!iftd)
3251 		return;
3252 
3253 	/* Check for device HE 6 GHz capability before adding element */
3254 	if (!iftd->he_6ghz_capa.capa)
3255 		return;
3256 
3257 	cap = le16_to_cpu(iftd->he_6ghz_capa.capa);
3258 	cap &= ~IEEE80211_HE_6GHZ_CAP_SM_PS;
3259 
3260 	switch (smps_mode) {
3261 	case IEEE80211_SMPS_AUTOMATIC:
3262 	case IEEE80211_SMPS_NUM_MODES:
3263 		WARN_ON(1);
3264 		fallthrough;
3265 	case IEEE80211_SMPS_OFF:
3266 		cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
3267 				       IEEE80211_HE_6GHZ_CAP_SM_PS);
3268 		break;
3269 	case IEEE80211_SMPS_STATIC:
3270 		cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_STATIC,
3271 				       IEEE80211_HE_6GHZ_CAP_SM_PS);
3272 		break;
3273 	case IEEE80211_SMPS_DYNAMIC:
3274 		cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
3275 				       IEEE80211_HE_6GHZ_CAP_SM_PS);
3276 		break;
3277 	}
3278 
3279 	pos = skb_put(skb, 2 + 1 + sizeof(cap));
3280 	ieee80211_write_he_6ghz_cap(pos, cpu_to_le16(cap),
3281 				    pos + 2 + 1 + sizeof(cap));
3282 }
3283 
3284 u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
3285 			       const struct cfg80211_chan_def *chandef,
3286 			       u16 prot_mode, bool rifs_mode)
3287 {
3288 	struct ieee80211_ht_operation *ht_oper;
3289 	/* Build HT Information */
3290 	*pos++ = WLAN_EID_HT_OPERATION;
3291 	*pos++ = sizeof(struct ieee80211_ht_operation);
3292 	ht_oper = (struct ieee80211_ht_operation *)pos;
3293 	ht_oper->primary_chan = ieee80211_frequency_to_channel(
3294 					chandef->chan->center_freq);
3295 	switch (chandef->width) {
3296 	case NL80211_CHAN_WIDTH_160:
3297 	case NL80211_CHAN_WIDTH_80P80:
3298 	case NL80211_CHAN_WIDTH_80:
3299 	case NL80211_CHAN_WIDTH_40:
3300 		if (chandef->center_freq1 > chandef->chan->center_freq)
3301 			ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
3302 		else
3303 			ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
3304 		break;
3305 	case NL80211_CHAN_WIDTH_320:
3306 		/* HT information element should not be included on 6GHz */
3307 		WARN_ON(1);
3308 		return pos;
3309 	default:
3310 		ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
3311 		break;
3312 	}
3313 	if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
3314 	    chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
3315 	    chandef->width != NL80211_CHAN_WIDTH_20)
3316 		ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
3317 
3318 	if (rifs_mode)
3319 		ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
3320 
3321 	ht_oper->operation_mode = cpu_to_le16(prot_mode);
3322 	ht_oper->stbc_param = 0x0000;
3323 
3324 	/* It seems that Basic MCS set and Supported MCS set
3325 	   are identical for the first 10 bytes */
3326 	memset(&ht_oper->basic_set, 0, 16);
3327 	memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
3328 
3329 	return pos + sizeof(struct ieee80211_ht_operation);
3330 }
3331 
3332 void ieee80211_ie_build_wide_bw_cs(u8 *pos,
3333 				   const struct cfg80211_chan_def *chandef)
3334 {
3335 	*pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH;	/* EID */
3336 	*pos++ = 3;					/* IE length */
3337 	/* New channel width */
3338 	switch (chandef->width) {
3339 	case NL80211_CHAN_WIDTH_80:
3340 		*pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ;
3341 		break;
3342 	case NL80211_CHAN_WIDTH_160:
3343 		*pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ;
3344 		break;
3345 	case NL80211_CHAN_WIDTH_80P80:
3346 		*pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
3347 		break;
3348 	case NL80211_CHAN_WIDTH_320:
3349 		/* The behavior is not defined for 320 MHz channels */
3350 		WARN_ON(1);
3351 		fallthrough;
3352 	default:
3353 		*pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT;
3354 	}
3355 
3356 	/* new center frequency segment 0 */
3357 	*pos++ = ieee80211_frequency_to_channel(chandef->center_freq1);
3358 	/* new center frequency segment 1 */
3359 	if (chandef->center_freq2)
3360 		*pos++ = ieee80211_frequency_to_channel(chandef->center_freq2);
3361 	else
3362 		*pos++ = 0;
3363 }
3364 
3365 u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
3366 				const struct cfg80211_chan_def *chandef)
3367 {
3368 	struct ieee80211_vht_operation *vht_oper;
3369 
3370 	*pos++ = WLAN_EID_VHT_OPERATION;
3371 	*pos++ = sizeof(struct ieee80211_vht_operation);
3372 	vht_oper = (struct ieee80211_vht_operation *)pos;
3373 	vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
3374 							chandef->center_freq1);
3375 	if (chandef->center_freq2)
3376 		vht_oper->center_freq_seg1_idx =
3377 			ieee80211_frequency_to_channel(chandef->center_freq2);
3378 	else
3379 		vht_oper->center_freq_seg1_idx = 0x00;
3380 
3381 	switch (chandef->width) {
3382 	case NL80211_CHAN_WIDTH_160:
3383 		/*
3384 		 * Convert 160 MHz channel width to new style as interop
3385 		 * workaround.
3386 		 */
3387 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3388 		vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
3389 		if (chandef->chan->center_freq < chandef->center_freq1)
3390 			vht_oper->center_freq_seg0_idx -= 8;
3391 		else
3392 			vht_oper->center_freq_seg0_idx += 8;
3393 		break;
3394 	case NL80211_CHAN_WIDTH_80P80:
3395 		/*
3396 		 * Convert 80+80 MHz channel width to new style as interop
3397 		 * workaround.
3398 		 */
3399 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3400 		break;
3401 	case NL80211_CHAN_WIDTH_80:
3402 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
3403 		break;
3404 	case NL80211_CHAN_WIDTH_320:
3405 		/* VHT information element should not be included on 6GHz */
3406 		WARN_ON(1);
3407 		return pos;
3408 	default:
3409 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
3410 		break;
3411 	}
3412 
3413 	/* don't require special VHT peer rates */
3414 	vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
3415 
3416 	return pos + sizeof(struct ieee80211_vht_operation);
3417 }
3418 
3419 u8 *ieee80211_ie_build_he_oper(u8 *pos, struct cfg80211_chan_def *chandef)
3420 {
3421 	struct ieee80211_he_operation *he_oper;
3422 	struct ieee80211_he_6ghz_oper *he_6ghz_op;
3423 	u32 he_oper_params;
3424 	u8 ie_len = 1 + sizeof(struct ieee80211_he_operation);
3425 
3426 	if (chandef->chan->band == NL80211_BAND_6GHZ)
3427 		ie_len += sizeof(struct ieee80211_he_6ghz_oper);
3428 
3429 	*pos++ = WLAN_EID_EXTENSION;
3430 	*pos++ = ie_len;
3431 	*pos++ = WLAN_EID_EXT_HE_OPERATION;
3432 
3433 	he_oper_params = 0;
3434 	he_oper_params |= u32_encode_bits(1023, /* disabled */
3435 				IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
3436 	he_oper_params |= u32_encode_bits(1,
3437 				IEEE80211_HE_OPERATION_ER_SU_DISABLE);
3438 	he_oper_params |= u32_encode_bits(1,
3439 				IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
3440 	if (chandef->chan->band == NL80211_BAND_6GHZ)
3441 		he_oper_params |= u32_encode_bits(1,
3442 				IEEE80211_HE_OPERATION_6GHZ_OP_INFO);
3443 
3444 	he_oper = (struct ieee80211_he_operation *)pos;
3445 	he_oper->he_oper_params = cpu_to_le32(he_oper_params);
3446 
3447 	/* don't require special HE peer rates */
3448 	he_oper->he_mcs_nss_set = cpu_to_le16(0xffff);
3449 	pos += sizeof(struct ieee80211_he_operation);
3450 
3451 	if (chandef->chan->band != NL80211_BAND_6GHZ)
3452 		goto out;
3453 
3454 	/* TODO add VHT operational */
3455 	he_6ghz_op = (struct ieee80211_he_6ghz_oper *)pos;
3456 	he_6ghz_op->minrate = 6; /* 6 Mbps */
3457 	he_6ghz_op->primary =
3458 		ieee80211_frequency_to_channel(chandef->chan->center_freq);
3459 	he_6ghz_op->ccfs0 =
3460 		ieee80211_frequency_to_channel(chandef->center_freq1);
3461 	if (chandef->center_freq2)
3462 		he_6ghz_op->ccfs1 =
3463 			ieee80211_frequency_to_channel(chandef->center_freq2);
3464 	else
3465 		he_6ghz_op->ccfs1 = 0;
3466 
3467 	switch (chandef->width) {
3468 	case NL80211_CHAN_WIDTH_320:
3469 		/*
3470 		 * TODO: mesh operation is not defined over 6GHz 320 MHz
3471 		 * channels.
3472 		 */
3473 		WARN_ON(1);
3474 		break;
3475 	case NL80211_CHAN_WIDTH_160:
3476 		/* Convert 160 MHz channel width to new style as interop
3477 		 * workaround.
3478 		 */
3479 		he_6ghz_op->control =
3480 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
3481 		he_6ghz_op->ccfs1 = he_6ghz_op->ccfs0;
3482 		if (chandef->chan->center_freq < chandef->center_freq1)
3483 			he_6ghz_op->ccfs0 -= 8;
3484 		else
3485 			he_6ghz_op->ccfs0 += 8;
3486 		fallthrough;
3487 	case NL80211_CHAN_WIDTH_80P80:
3488 		he_6ghz_op->control =
3489 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
3490 		break;
3491 	case NL80211_CHAN_WIDTH_80:
3492 		he_6ghz_op->control =
3493 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ;
3494 		break;
3495 	case NL80211_CHAN_WIDTH_40:
3496 		he_6ghz_op->control =
3497 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ;
3498 		break;
3499 	default:
3500 		he_6ghz_op->control =
3501 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ;
3502 		break;
3503 	}
3504 
3505 	pos += sizeof(struct ieee80211_he_6ghz_oper);
3506 
3507 out:
3508 	return pos;
3509 }
3510 
3511 bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
3512 			       struct cfg80211_chan_def *chandef)
3513 {
3514 	enum nl80211_channel_type channel_type;
3515 
3516 	if (!ht_oper)
3517 		return false;
3518 
3519 	switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3520 	case IEEE80211_HT_PARAM_CHA_SEC_NONE:
3521 		channel_type = NL80211_CHAN_HT20;
3522 		break;
3523 	case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3524 		channel_type = NL80211_CHAN_HT40PLUS;
3525 		break;
3526 	case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3527 		channel_type = NL80211_CHAN_HT40MINUS;
3528 		break;
3529 	default:
3530 		return false;
3531 	}
3532 
3533 	cfg80211_chandef_create(chandef, chandef->chan, channel_type);
3534 	return true;
3535 }
3536 
3537 bool ieee80211_chandef_vht_oper(struct ieee80211_hw *hw, u32 vht_cap_info,
3538 				const struct ieee80211_vht_operation *oper,
3539 				const struct ieee80211_ht_operation *htop,
3540 				struct cfg80211_chan_def *chandef)
3541 {
3542 	struct cfg80211_chan_def new = *chandef;
3543 	int cf0, cf1;
3544 	int ccfs0, ccfs1, ccfs2;
3545 	int ccf0, ccf1;
3546 	u32 vht_cap;
3547 	bool support_80_80 = false;
3548 	bool support_160 = false;
3549 	u8 ext_nss_bw_supp = u32_get_bits(vht_cap_info,
3550 					  IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
3551 	u8 supp_chwidth = u32_get_bits(vht_cap_info,
3552 				       IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
3553 
3554 	if (!oper || !htop)
3555 		return false;
3556 
3557 	vht_cap = hw->wiphy->bands[chandef->chan->band]->vht_cap.cap;
3558 	support_160 = (vht_cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK |
3559 				  IEEE80211_VHT_CAP_EXT_NSS_BW_MASK));
3560 	support_80_80 = ((vht_cap &
3561 			 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
3562 			(vht_cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
3563 			 vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
3564 			((vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) >>
3565 				    IEEE80211_VHT_CAP_EXT_NSS_BW_SHIFT > 1));
3566 	ccfs0 = oper->center_freq_seg0_idx;
3567 	ccfs1 = oper->center_freq_seg1_idx;
3568 	ccfs2 = (le16_to_cpu(htop->operation_mode) &
3569 				IEEE80211_HT_OP_MODE_CCFS2_MASK)
3570 			>> IEEE80211_HT_OP_MODE_CCFS2_SHIFT;
3571 
3572 	ccf0 = ccfs0;
3573 
3574 	/* if not supported, parse as though we didn't understand it */
3575 	if (!ieee80211_hw_check(hw, SUPPORTS_VHT_EXT_NSS_BW))
3576 		ext_nss_bw_supp = 0;
3577 
3578 	/*
3579 	 * Cf. IEEE 802.11 Table 9-250
3580 	 *
3581 	 * We really just consider that because it's inefficient to connect
3582 	 * at a higher bandwidth than we'll actually be able to use.
3583 	 */
3584 	switch ((supp_chwidth << 4) | ext_nss_bw_supp) {
3585 	default:
3586 	case 0x00:
3587 		ccf1 = 0;
3588 		support_160 = false;
3589 		support_80_80 = false;
3590 		break;
3591 	case 0x01:
3592 		support_80_80 = false;
3593 		fallthrough;
3594 	case 0x02:
3595 	case 0x03:
3596 		ccf1 = ccfs2;
3597 		break;
3598 	case 0x10:
3599 		ccf1 = ccfs1;
3600 		break;
3601 	case 0x11:
3602 	case 0x12:
3603 		if (!ccfs1)
3604 			ccf1 = ccfs2;
3605 		else
3606 			ccf1 = ccfs1;
3607 		break;
3608 	case 0x13:
3609 	case 0x20:
3610 	case 0x23:
3611 		ccf1 = ccfs1;
3612 		break;
3613 	}
3614 
3615 	cf0 = ieee80211_channel_to_frequency(ccf0, chandef->chan->band);
3616 	cf1 = ieee80211_channel_to_frequency(ccf1, chandef->chan->band);
3617 
3618 	switch (oper->chan_width) {
3619 	case IEEE80211_VHT_CHANWIDTH_USE_HT:
3620 		/* just use HT information directly */
3621 		break;
3622 	case IEEE80211_VHT_CHANWIDTH_80MHZ:
3623 		new.width = NL80211_CHAN_WIDTH_80;
3624 		new.center_freq1 = cf0;
3625 		/* If needed, adjust based on the newer interop workaround. */
3626 		if (ccf1) {
3627 			unsigned int diff;
3628 
3629 			diff = abs(ccf1 - ccf0);
3630 			if ((diff == 8) && support_160) {
3631 				new.width = NL80211_CHAN_WIDTH_160;
3632 				new.center_freq1 = cf1;
3633 			} else if ((diff > 8) && support_80_80) {
3634 				new.width = NL80211_CHAN_WIDTH_80P80;
3635 				new.center_freq2 = cf1;
3636 			}
3637 		}
3638 		break;
3639 	case IEEE80211_VHT_CHANWIDTH_160MHZ:
3640 		/* deprecated encoding */
3641 		new.width = NL80211_CHAN_WIDTH_160;
3642 		new.center_freq1 = cf0;
3643 		break;
3644 	case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
3645 		/* deprecated encoding */
3646 		new.width = NL80211_CHAN_WIDTH_80P80;
3647 		new.center_freq1 = cf0;
3648 		new.center_freq2 = cf1;
3649 		break;
3650 	default:
3651 		return false;
3652 	}
3653 
3654 	if (!cfg80211_chandef_valid(&new))
3655 		return false;
3656 
3657 	*chandef = new;
3658 	return true;
3659 }
3660 
3661 void ieee80211_chandef_eht_oper(const struct ieee80211_eht_operation *eht_oper,
3662 				bool support_160, bool support_320,
3663 				struct cfg80211_chan_def *chandef)
3664 {
3665 	struct ieee80211_eht_operation_info *info = (void *)eht_oper->optional;
3666 
3667 	chandef->center_freq1 =
3668 		ieee80211_channel_to_frequency(info->ccfs0,
3669 					       chandef->chan->band);
3670 
3671 	switch (u8_get_bits(info->control,
3672 			    IEEE80211_EHT_OPER_CHAN_WIDTH)) {
3673 	case IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ:
3674 		chandef->width = NL80211_CHAN_WIDTH_20;
3675 		break;
3676 	case IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ:
3677 		chandef->width = NL80211_CHAN_WIDTH_40;
3678 		break;
3679 	case IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ:
3680 		chandef->width = NL80211_CHAN_WIDTH_80;
3681 		break;
3682 	case IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ:
3683 		if (support_160) {
3684 			chandef->width = NL80211_CHAN_WIDTH_160;
3685 			chandef->center_freq1 =
3686 				ieee80211_channel_to_frequency(info->ccfs1,
3687 							       chandef->chan->band);
3688 		} else {
3689 			chandef->width = NL80211_CHAN_WIDTH_80;
3690 		}
3691 		break;
3692 	case IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ:
3693 		if (support_320) {
3694 			chandef->width = NL80211_CHAN_WIDTH_320;
3695 			chandef->center_freq1 =
3696 				ieee80211_channel_to_frequency(info->ccfs1,
3697 							       chandef->chan->band);
3698 		} else if (support_160) {
3699 			chandef->width = NL80211_CHAN_WIDTH_160;
3700 		} else {
3701 			chandef->width = NL80211_CHAN_WIDTH_80;
3702 
3703 			if (chandef->center_freq1 > chandef->chan->center_freq)
3704 				chandef->center_freq1 -= 40;
3705 			else
3706 				chandef->center_freq1 += 40;
3707 		}
3708 		break;
3709 	}
3710 }
3711 
3712 bool ieee80211_chandef_he_6ghz_oper(struct ieee80211_sub_if_data *sdata,
3713 				    const struct ieee80211_he_operation *he_oper,
3714 				    const struct ieee80211_eht_operation *eht_oper,
3715 				    struct cfg80211_chan_def *chandef)
3716 {
3717 	struct ieee80211_local *local = sdata->local;
3718 	struct ieee80211_supported_band *sband;
3719 	enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
3720 	const struct ieee80211_sta_he_cap *he_cap;
3721 	const struct ieee80211_sta_eht_cap *eht_cap;
3722 	struct cfg80211_chan_def he_chandef = *chandef;
3723 	const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
3724 	struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3725 	bool support_80_80, support_160, support_320;
3726 	u8 he_phy_cap, eht_phy_cap;
3727 	u32 freq;
3728 
3729 	if (chandef->chan->band != NL80211_BAND_6GHZ)
3730 		return true;
3731 
3732 	sband = local->hw.wiphy->bands[NL80211_BAND_6GHZ];
3733 
3734 	he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
3735 	if (!he_cap) {
3736 		sdata_info(sdata, "Missing iftype sband data/HE cap");
3737 		return false;
3738 	}
3739 
3740 	he_phy_cap = he_cap->he_cap_elem.phy_cap_info[0];
3741 	support_160 =
3742 		he_phy_cap &
3743 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3744 	support_80_80 =
3745 		he_phy_cap &
3746 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
3747 
3748 	if (!he_oper) {
3749 		sdata_info(sdata,
3750 			   "HE is not advertised on (on %d MHz), expect issues\n",
3751 			   chandef->chan->center_freq);
3752 		return false;
3753 	}
3754 
3755 	eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype);
3756 	if (!eht_cap) {
3757 		sdata_info(sdata, "Missing iftype sband data/EHT cap");
3758 		eht_oper = NULL;
3759 	}
3760 
3761 	he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
3762 
3763 	if (!he_6ghz_oper) {
3764 		sdata_info(sdata,
3765 			   "HE 6GHz operation missing (on %d MHz), expect issues\n",
3766 			   chandef->chan->center_freq);
3767 		return false;
3768 	}
3769 
3770 	/*
3771 	 * The EHT operation IE does not contain the primary channel so the
3772 	 * primary channel frequency should be taken from the 6 GHz operation
3773 	 * information.
3774 	 */
3775 	freq = ieee80211_channel_to_frequency(he_6ghz_oper->primary,
3776 					      NL80211_BAND_6GHZ);
3777 	he_chandef.chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
3778 
3779 	switch (u8_get_bits(he_6ghz_oper->control,
3780 			    IEEE80211_HE_6GHZ_OPER_CTRL_REG_INFO)) {
3781 	case IEEE80211_6GHZ_CTRL_REG_LPI_AP:
3782 		bss_conf->power_type = IEEE80211_REG_LPI_AP;
3783 		break;
3784 	case IEEE80211_6GHZ_CTRL_REG_SP_AP:
3785 		bss_conf->power_type = IEEE80211_REG_SP_AP;
3786 		break;
3787 	default:
3788 		bss_conf->power_type = IEEE80211_REG_UNSET_AP;
3789 		break;
3790 	}
3791 
3792 	if (!eht_oper ||
3793 	    !(eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT)) {
3794 		switch (u8_get_bits(he_6ghz_oper->control,
3795 				    IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH)) {
3796 		case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ:
3797 			he_chandef.width = NL80211_CHAN_WIDTH_20;
3798 			break;
3799 		case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ:
3800 			he_chandef.width = NL80211_CHAN_WIDTH_40;
3801 			break;
3802 		case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ:
3803 			he_chandef.width = NL80211_CHAN_WIDTH_80;
3804 			break;
3805 		case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ:
3806 			he_chandef.width = NL80211_CHAN_WIDTH_80;
3807 			if (!he_6ghz_oper->ccfs1)
3808 				break;
3809 			if (abs(he_6ghz_oper->ccfs1 - he_6ghz_oper->ccfs0) == 8) {
3810 				if (support_160)
3811 					he_chandef.width = NL80211_CHAN_WIDTH_160;
3812 			} else {
3813 				if (support_80_80)
3814 					he_chandef.width = NL80211_CHAN_WIDTH_80P80;
3815 			}
3816 			break;
3817 		}
3818 
3819 		if (he_chandef.width == NL80211_CHAN_WIDTH_160) {
3820 			he_chandef.center_freq1 =
3821 				ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
3822 							       NL80211_BAND_6GHZ);
3823 		} else {
3824 			he_chandef.center_freq1 =
3825 				ieee80211_channel_to_frequency(he_6ghz_oper->ccfs0,
3826 							       NL80211_BAND_6GHZ);
3827 			if (support_80_80 || support_160)
3828 				he_chandef.center_freq2 =
3829 					ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
3830 								       NL80211_BAND_6GHZ);
3831 		}
3832 	} else {
3833 		eht_phy_cap = eht_cap->eht_cap_elem.phy_cap_info[0];
3834 		support_320 =
3835 			eht_phy_cap & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ;
3836 
3837 		ieee80211_chandef_eht_oper(eht_oper, support_160,
3838 					   support_320, &he_chandef);
3839 	}
3840 
3841 	if (!cfg80211_chandef_valid(&he_chandef)) {
3842 		sdata_info(sdata,
3843 			   "HE 6GHz operation resulted in invalid chandef: %d MHz/%d/%d MHz/%d MHz\n",
3844 			   he_chandef.chan ? he_chandef.chan->center_freq : 0,
3845 			   he_chandef.width,
3846 			   he_chandef.center_freq1,
3847 			   he_chandef.center_freq2);
3848 		return false;
3849 	}
3850 
3851 	*chandef = he_chandef;
3852 
3853 	return true;
3854 }
3855 
3856 bool ieee80211_chandef_s1g_oper(const struct ieee80211_s1g_oper_ie *oper,
3857 				struct cfg80211_chan_def *chandef)
3858 {
3859 	u32 oper_freq;
3860 
3861 	if (!oper)
3862 		return false;
3863 
3864 	switch (FIELD_GET(S1G_OPER_CH_WIDTH_OPER, oper->ch_width)) {
3865 	case IEEE80211_S1G_CHANWIDTH_1MHZ:
3866 		chandef->width = NL80211_CHAN_WIDTH_1;
3867 		break;
3868 	case IEEE80211_S1G_CHANWIDTH_2MHZ:
3869 		chandef->width = NL80211_CHAN_WIDTH_2;
3870 		break;
3871 	case IEEE80211_S1G_CHANWIDTH_4MHZ:
3872 		chandef->width = NL80211_CHAN_WIDTH_4;
3873 		break;
3874 	case IEEE80211_S1G_CHANWIDTH_8MHZ:
3875 		chandef->width = NL80211_CHAN_WIDTH_8;
3876 		break;
3877 	case IEEE80211_S1G_CHANWIDTH_16MHZ:
3878 		chandef->width = NL80211_CHAN_WIDTH_16;
3879 		break;
3880 	default:
3881 		return false;
3882 	}
3883 
3884 	oper_freq = ieee80211_channel_to_freq_khz(oper->oper_ch,
3885 						  NL80211_BAND_S1GHZ);
3886 	chandef->center_freq1 = KHZ_TO_MHZ(oper_freq);
3887 	chandef->freq1_offset = oper_freq % 1000;
3888 
3889 	return true;
3890 }
3891 
3892 int ieee80211_parse_bitrates(enum nl80211_chan_width width,
3893 			     const struct ieee80211_supported_band *sband,
3894 			     const u8 *srates, int srates_len, u32 *rates)
3895 {
3896 	u32 rate_flags = ieee80211_chanwidth_rate_flags(width);
3897 	int shift = ieee80211_chanwidth_get_shift(width);
3898 	struct ieee80211_rate *br;
3899 	int brate, rate, i, j, count = 0;
3900 
3901 	*rates = 0;
3902 
3903 	for (i = 0; i < srates_len; i++) {
3904 		rate = srates[i] & 0x7f;
3905 
3906 		for (j = 0; j < sband->n_bitrates; j++) {
3907 			br = &sband->bitrates[j];
3908 			if ((rate_flags & br->flags) != rate_flags)
3909 				continue;
3910 
3911 			brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3912 			if (brate == rate) {
3913 				*rates |= BIT(j);
3914 				count++;
3915 				break;
3916 			}
3917 		}
3918 	}
3919 	return count;
3920 }
3921 
3922 int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
3923 			    struct sk_buff *skb, bool need_basic,
3924 			    enum nl80211_band band)
3925 {
3926 	struct ieee80211_local *local = sdata->local;
3927 	struct ieee80211_supported_band *sband;
3928 	int rate, shift;
3929 	u8 i, rates, *pos;
3930 	u32 basic_rates = sdata->vif.bss_conf.basic_rates;
3931 	u32 rate_flags;
3932 
3933 	shift = ieee80211_vif_get_shift(&sdata->vif);
3934 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
3935 	sband = local->hw.wiphy->bands[band];
3936 	rates = 0;
3937 	for (i = 0; i < sband->n_bitrates; i++) {
3938 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
3939 			continue;
3940 		rates++;
3941 	}
3942 	if (rates > 8)
3943 		rates = 8;
3944 
3945 	if (skb_tailroom(skb) < rates + 2)
3946 		return -ENOMEM;
3947 
3948 	pos = skb_put(skb, rates + 2);
3949 	*pos++ = WLAN_EID_SUPP_RATES;
3950 	*pos++ = rates;
3951 	for (i = 0; i < rates; i++) {
3952 		u8 basic = 0;
3953 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
3954 			continue;
3955 
3956 		if (need_basic && basic_rates & BIT(i))
3957 			basic = 0x80;
3958 		rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
3959 				    5 * (1 << shift));
3960 		*pos++ = basic | (u8) rate;
3961 	}
3962 
3963 	return 0;
3964 }
3965 
3966 int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
3967 				struct sk_buff *skb, bool need_basic,
3968 				enum nl80211_band band)
3969 {
3970 	struct ieee80211_local *local = sdata->local;
3971 	struct ieee80211_supported_band *sband;
3972 	int rate, shift;
3973 	u8 i, exrates, *pos;
3974 	u32 basic_rates = sdata->vif.bss_conf.basic_rates;
3975 	u32 rate_flags;
3976 
3977 	rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
3978 	shift = ieee80211_vif_get_shift(&sdata->vif);
3979 
3980 	sband = local->hw.wiphy->bands[band];
3981 	exrates = 0;
3982 	for (i = 0; i < sband->n_bitrates; i++) {
3983 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
3984 			continue;
3985 		exrates++;
3986 	}
3987 
3988 	if (exrates > 8)
3989 		exrates -= 8;
3990 	else
3991 		exrates = 0;
3992 
3993 	if (skb_tailroom(skb) < exrates + 2)
3994 		return -ENOMEM;
3995 
3996 	if (exrates) {
3997 		pos = skb_put(skb, exrates + 2);
3998 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
3999 		*pos++ = exrates;
4000 		for (i = 8; i < sband->n_bitrates; i++) {
4001 			u8 basic = 0;
4002 			if ((rate_flags & sband->bitrates[i].flags)
4003 			    != rate_flags)
4004 				continue;
4005 			if (need_basic && basic_rates & BIT(i))
4006 				basic = 0x80;
4007 			rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
4008 					    5 * (1 << shift));
4009 			*pos++ = basic | (u8) rate;
4010 		}
4011 	}
4012 	return 0;
4013 }
4014 
4015 int ieee80211_ave_rssi(struct ieee80211_vif *vif)
4016 {
4017 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4018 
4019 	if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION))
4020 		return 0;
4021 
4022 	return -ewma_beacon_signal_read(&sdata->deflink.u.mgd.ave_beacon_signal);
4023 }
4024 EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
4025 
4026 u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
4027 {
4028 	if (!mcs)
4029 		return 1;
4030 
4031 	/* TODO: consider rx_highest */
4032 
4033 	if (mcs->rx_mask[3])
4034 		return 4;
4035 	if (mcs->rx_mask[2])
4036 		return 3;
4037 	if (mcs->rx_mask[1])
4038 		return 2;
4039 	return 1;
4040 }
4041 
4042 /**
4043  * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
4044  * @local: mac80211 hw info struct
4045  * @status: RX status
4046  * @mpdu_len: total MPDU length (including FCS)
4047  * @mpdu_offset: offset into MPDU to calculate timestamp at
4048  *
4049  * This function calculates the RX timestamp at the given MPDU offset, taking
4050  * into account what the RX timestamp was. An offset of 0 will just normalize
4051  * the timestamp to TSF at beginning of MPDU reception.
4052  */
4053 u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
4054 				     struct ieee80211_rx_status *status,
4055 				     unsigned int mpdu_len,
4056 				     unsigned int mpdu_offset)
4057 {
4058 	u64 ts = status->mactime;
4059 	struct rate_info ri;
4060 	u16 rate;
4061 	u8 n_ltf;
4062 
4063 	if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
4064 		return 0;
4065 
4066 	memset(&ri, 0, sizeof(ri));
4067 
4068 	ri.bw = status->bw;
4069 
4070 	/* Fill cfg80211 rate info */
4071 	switch (status->encoding) {
4072 	case RX_ENC_HE:
4073 		ri.flags |= RATE_INFO_FLAGS_HE_MCS;
4074 		ri.mcs = status->rate_idx;
4075 		ri.nss = status->nss;
4076 		ri.he_ru_alloc = status->he_ru;
4077 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4078 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4079 
4080 		/*
4081 		 * See P802.11ax_D6.0, section 27.3.4 for
4082 		 * VHT PPDU format.
4083 		 */
4084 		if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4085 			mpdu_offset += 2;
4086 			ts += 36;
4087 
4088 			/*
4089 			 * TODO:
4090 			 * For HE MU PPDU, add the HE-SIG-B.
4091 			 * For HE ER PPDU, add 8us for the HE-SIG-A.
4092 			 * For HE TB PPDU, add 4us for the HE-STF.
4093 			 * Add the HE-LTF durations - variable.
4094 			 */
4095 		}
4096 
4097 		break;
4098 	case RX_ENC_HT:
4099 		ri.mcs = status->rate_idx;
4100 		ri.flags |= RATE_INFO_FLAGS_MCS;
4101 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4102 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4103 
4104 		/*
4105 		 * See P802.11REVmd_D3.0, section 19.3.2 for
4106 		 * HT PPDU format.
4107 		 */
4108 		if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4109 			mpdu_offset += 2;
4110 			if (status->enc_flags & RX_ENC_FLAG_HT_GF)
4111 				ts += 24;
4112 			else
4113 				ts += 32;
4114 
4115 			/*
4116 			 * Add Data HT-LTFs per streams
4117 			 * TODO: add Extension HT-LTFs, 4us per LTF
4118 			 */
4119 			n_ltf = ((ri.mcs >> 3) & 3) + 1;
4120 			n_ltf = n_ltf == 3 ? 4 : n_ltf;
4121 			ts += n_ltf * 4;
4122 		}
4123 
4124 		break;
4125 	case RX_ENC_VHT:
4126 		ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
4127 		ri.mcs = status->rate_idx;
4128 		ri.nss = status->nss;
4129 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
4130 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
4131 
4132 		/*
4133 		 * See P802.11REVmd_D3.0, section 21.3.2 for
4134 		 * VHT PPDU format.
4135 		 */
4136 		if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4137 			mpdu_offset += 2;
4138 			ts += 36;
4139 
4140 			/*
4141 			 * Add VHT-LTFs per streams
4142 			 */
4143 			n_ltf = (ri.nss != 1) && (ri.nss % 2) ?
4144 				ri.nss + 1 : ri.nss;
4145 			ts += 4 * n_ltf;
4146 		}
4147 
4148 		break;
4149 	default:
4150 		WARN_ON(1);
4151 		fallthrough;
4152 	case RX_ENC_LEGACY: {
4153 		struct ieee80211_supported_band *sband;
4154 		int shift = 0;
4155 		int bitrate;
4156 
4157 		switch (status->bw) {
4158 		case RATE_INFO_BW_10:
4159 			shift = 1;
4160 			break;
4161 		case RATE_INFO_BW_5:
4162 			shift = 2;
4163 			break;
4164 		}
4165 
4166 		sband = local->hw.wiphy->bands[status->band];
4167 		bitrate = sband->bitrates[status->rate_idx].bitrate;
4168 		ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
4169 
4170 		if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
4171 			if (status->band == NL80211_BAND_5GHZ) {
4172 				ts += 20 << shift;
4173 				mpdu_offset += 2;
4174 			} else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
4175 				ts += 96;
4176 			} else {
4177 				ts += 192;
4178 			}
4179 		}
4180 		break;
4181 		}
4182 	}
4183 
4184 	rate = cfg80211_calculate_bitrate(&ri);
4185 	if (WARN_ONCE(!rate,
4186 		      "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
4187 		      (unsigned long long)status->flag, status->rate_idx,
4188 		      status->nss))
4189 		return 0;
4190 
4191 	/* rewind from end of MPDU */
4192 	if (status->flag & RX_FLAG_MACTIME_END)
4193 		ts -= mpdu_len * 8 * 10 / rate;
4194 
4195 	ts += mpdu_offset * 8 * 10 / rate;
4196 
4197 	return ts;
4198 }
4199 
4200 void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
4201 {
4202 	struct ieee80211_sub_if_data *sdata;
4203 	struct cfg80211_chan_def chandef;
4204 
4205 	/* for interface list, to avoid linking iflist_mtx and chanctx_mtx */
4206 	lockdep_assert_wiphy(local->hw.wiphy);
4207 
4208 	mutex_lock(&local->mtx);
4209 	list_for_each_entry(sdata, &local->interfaces, list) {
4210 		/* it might be waiting for the local->mtx, but then
4211 		 * by the time it gets it, sdata->wdev.cac_started
4212 		 * will no longer be true
4213 		 */
4214 		cancel_delayed_work(&sdata->deflink.dfs_cac_timer_work);
4215 
4216 		if (sdata->wdev.cac_started) {
4217 			chandef = sdata->vif.bss_conf.chandef;
4218 			ieee80211_link_release_channel(&sdata->deflink);
4219 			cfg80211_cac_event(sdata->dev,
4220 					   &chandef,
4221 					   NL80211_RADAR_CAC_ABORTED,
4222 					   GFP_KERNEL);
4223 		}
4224 	}
4225 	mutex_unlock(&local->mtx);
4226 }
4227 
4228 void ieee80211_dfs_radar_detected_work(struct work_struct *work)
4229 {
4230 	struct ieee80211_local *local =
4231 		container_of(work, struct ieee80211_local, radar_detected_work);
4232 	struct cfg80211_chan_def chandef = local->hw.conf.chandef;
4233 	struct ieee80211_chanctx *ctx;
4234 	int num_chanctx = 0;
4235 
4236 	mutex_lock(&local->chanctx_mtx);
4237 	list_for_each_entry(ctx, &local->chanctx_list, list) {
4238 		if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
4239 			continue;
4240 
4241 		num_chanctx++;
4242 		chandef = ctx->conf.def;
4243 	}
4244 	mutex_unlock(&local->chanctx_mtx);
4245 
4246 	wiphy_lock(local->hw.wiphy);
4247 	ieee80211_dfs_cac_cancel(local);
4248 	wiphy_unlock(local->hw.wiphy);
4249 
4250 	if (num_chanctx > 1)
4251 		/* XXX: multi-channel is not supported yet */
4252 		WARN_ON(1);
4253 	else
4254 		cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
4255 }
4256 
4257 void ieee80211_radar_detected(struct ieee80211_hw *hw)
4258 {
4259 	struct ieee80211_local *local = hw_to_local(hw);
4260 
4261 	trace_api_radar_detected(local);
4262 
4263 	schedule_work(&local->radar_detected_work);
4264 }
4265 EXPORT_SYMBOL(ieee80211_radar_detected);
4266 
4267 ieee80211_conn_flags_t ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
4268 {
4269 	ieee80211_conn_flags_t ret;
4270 	int tmp;
4271 
4272 	switch (c->width) {
4273 	case NL80211_CHAN_WIDTH_20:
4274 		c->width = NL80211_CHAN_WIDTH_20_NOHT;
4275 		ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4276 		break;
4277 	case NL80211_CHAN_WIDTH_40:
4278 		c->width = NL80211_CHAN_WIDTH_20;
4279 		c->center_freq1 = c->chan->center_freq;
4280 		ret = IEEE80211_CONN_DISABLE_40MHZ |
4281 		      IEEE80211_CONN_DISABLE_VHT;
4282 		break;
4283 	case NL80211_CHAN_WIDTH_80:
4284 		tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
4285 		/* n_P40 */
4286 		tmp /= 2;
4287 		/* freq_P40 */
4288 		c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
4289 		c->width = NL80211_CHAN_WIDTH_40;
4290 		ret = IEEE80211_CONN_DISABLE_VHT;
4291 		break;
4292 	case NL80211_CHAN_WIDTH_80P80:
4293 		c->center_freq2 = 0;
4294 		c->width = NL80211_CHAN_WIDTH_80;
4295 		ret = IEEE80211_CONN_DISABLE_80P80MHZ |
4296 		      IEEE80211_CONN_DISABLE_160MHZ;
4297 		break;
4298 	case NL80211_CHAN_WIDTH_160:
4299 		/* n_P20 */
4300 		tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
4301 		/* n_P80 */
4302 		tmp /= 4;
4303 		c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
4304 		c->width = NL80211_CHAN_WIDTH_80;
4305 		ret = IEEE80211_CONN_DISABLE_80P80MHZ |
4306 		      IEEE80211_CONN_DISABLE_160MHZ;
4307 		break;
4308 	case NL80211_CHAN_WIDTH_320:
4309 		/* n_P20 */
4310 		tmp = (150 + c->chan->center_freq - c->center_freq1) / 20;
4311 		/* n_P160 */
4312 		tmp /= 8;
4313 		c->center_freq1 = c->center_freq1 - 80 + 160 * tmp;
4314 		c->width = NL80211_CHAN_WIDTH_160;
4315 		ret = IEEE80211_CONN_DISABLE_320MHZ;
4316 		break;
4317 	default:
4318 	case NL80211_CHAN_WIDTH_20_NOHT:
4319 		WARN_ON_ONCE(1);
4320 		c->width = NL80211_CHAN_WIDTH_20_NOHT;
4321 		ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4322 		break;
4323 	case NL80211_CHAN_WIDTH_1:
4324 	case NL80211_CHAN_WIDTH_2:
4325 	case NL80211_CHAN_WIDTH_4:
4326 	case NL80211_CHAN_WIDTH_8:
4327 	case NL80211_CHAN_WIDTH_16:
4328 	case NL80211_CHAN_WIDTH_5:
4329 	case NL80211_CHAN_WIDTH_10:
4330 		WARN_ON_ONCE(1);
4331 		/* keep c->width */
4332 		ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT;
4333 		break;
4334 	}
4335 
4336 	WARN_ON_ONCE(!cfg80211_chandef_valid(c));
4337 
4338 	return ret;
4339 }
4340 
4341 /*
4342  * Returns true if smps_mode_new is strictly more restrictive than
4343  * smps_mode_old.
4344  */
4345 bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
4346 				   enum ieee80211_smps_mode smps_mode_new)
4347 {
4348 	if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
4349 			 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
4350 		return false;
4351 
4352 	switch (smps_mode_old) {
4353 	case IEEE80211_SMPS_STATIC:
4354 		return false;
4355 	case IEEE80211_SMPS_DYNAMIC:
4356 		return smps_mode_new == IEEE80211_SMPS_STATIC;
4357 	case IEEE80211_SMPS_OFF:
4358 		return smps_mode_new != IEEE80211_SMPS_OFF;
4359 	default:
4360 		WARN_ON(1);
4361 	}
4362 
4363 	return false;
4364 }
4365 
4366 int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
4367 			      struct cfg80211_csa_settings *csa_settings)
4368 {
4369 	struct sk_buff *skb;
4370 	struct ieee80211_mgmt *mgmt;
4371 	struct ieee80211_local *local = sdata->local;
4372 	int freq;
4373 	int hdr_len = offsetofend(struct ieee80211_mgmt,
4374 				  u.action.u.chan_switch);
4375 	u8 *pos;
4376 
4377 	if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
4378 	    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
4379 		return -EOPNOTSUPP;
4380 
4381 	skb = dev_alloc_skb(local->tx_headroom + hdr_len +
4382 			    5 + /* channel switch announcement element */
4383 			    3 + /* secondary channel offset element */
4384 			    5 + /* wide bandwidth channel switch announcement */
4385 			    8); /* mesh channel switch parameters element */
4386 	if (!skb)
4387 		return -ENOMEM;
4388 
4389 	skb_reserve(skb, local->tx_headroom);
4390 	mgmt = skb_put_zero(skb, hdr_len);
4391 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4392 					  IEEE80211_STYPE_ACTION);
4393 
4394 	eth_broadcast_addr(mgmt->da);
4395 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
4396 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
4397 		memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
4398 	} else {
4399 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4400 		memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
4401 	}
4402 	mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
4403 	mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
4404 	pos = skb_put(skb, 5);
4405 	*pos++ = WLAN_EID_CHANNEL_SWITCH;			/* EID */
4406 	*pos++ = 3;						/* IE length */
4407 	*pos++ = csa_settings->block_tx ? 1 : 0;		/* CSA mode */
4408 	freq = csa_settings->chandef.chan->center_freq;
4409 	*pos++ = ieee80211_frequency_to_channel(freq);		/* channel */
4410 	*pos++ = csa_settings->count;				/* count */
4411 
4412 	if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
4413 		enum nl80211_channel_type ch_type;
4414 
4415 		skb_put(skb, 3);
4416 		*pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;	/* EID */
4417 		*pos++ = 1;					/* IE length */
4418 		ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
4419 		if (ch_type == NL80211_CHAN_HT40PLUS)
4420 			*pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
4421 		else
4422 			*pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
4423 	}
4424 
4425 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
4426 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4427 
4428 		skb_put(skb, 8);
4429 		*pos++ = WLAN_EID_CHAN_SWITCH_PARAM;		/* EID */
4430 		*pos++ = 6;					/* IE length */
4431 		*pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL;	/* Mesh TTL */
4432 		*pos = 0x00;	/* Mesh Flag: Tx Restrict, Initiator, Reason */
4433 		*pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
4434 		*pos++ |= csa_settings->block_tx ?
4435 			  WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
4436 		put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
4437 		pos += 2;
4438 		put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
4439 		pos += 2;
4440 	}
4441 
4442 	if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 ||
4443 	    csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 ||
4444 	    csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) {
4445 		skb_put(skb, 5);
4446 		ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef);
4447 	}
4448 
4449 	ieee80211_tx_skb(sdata, skb);
4450 	return 0;
4451 }
4452 
4453 static bool
4454 ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
4455 {
4456 	s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
4457 	int skip;
4458 
4459 	if (end > 0)
4460 		return false;
4461 
4462 	/* One shot NOA  */
4463 	if (data->count[i] == 1)
4464 		return false;
4465 
4466 	if (data->desc[i].interval == 0)
4467 		return false;
4468 
4469 	/* End time is in the past, check for repetitions */
4470 	skip = DIV_ROUND_UP(-end, data->desc[i].interval);
4471 	if (data->count[i] < 255) {
4472 		if (data->count[i] <= skip) {
4473 			data->count[i] = 0;
4474 			return false;
4475 		}
4476 
4477 		data->count[i] -= skip;
4478 	}
4479 
4480 	data->desc[i].start += skip * data->desc[i].interval;
4481 
4482 	return true;
4483 }
4484 
4485 static bool
4486 ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
4487 			     s32 *offset)
4488 {
4489 	bool ret = false;
4490 	int i;
4491 
4492 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4493 		s32 cur;
4494 
4495 		if (!data->count[i])
4496 			continue;
4497 
4498 		if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
4499 			ret = true;
4500 
4501 		cur = data->desc[i].start - tsf;
4502 		if (cur > *offset)
4503 			continue;
4504 
4505 		cur = data->desc[i].start + data->desc[i].duration - tsf;
4506 		if (cur > *offset)
4507 			*offset = cur;
4508 	}
4509 
4510 	return ret;
4511 }
4512 
4513 static u32
4514 ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
4515 {
4516 	s32 offset = 0;
4517 	int tries = 0;
4518 	/*
4519 	 * arbitrary limit, used to avoid infinite loops when combined NoA
4520 	 * descriptors cover the full time period.
4521 	 */
4522 	int max_tries = 5;
4523 
4524 	ieee80211_extend_absent_time(data, tsf, &offset);
4525 	do {
4526 		if (!ieee80211_extend_absent_time(data, tsf, &offset))
4527 			break;
4528 
4529 		tries++;
4530 	} while (tries < max_tries);
4531 
4532 	return offset;
4533 }
4534 
4535 void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
4536 {
4537 	u32 next_offset = BIT(31) - 1;
4538 	int i;
4539 
4540 	data->absent = 0;
4541 	data->has_next_tsf = false;
4542 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4543 		s32 start;
4544 
4545 		if (!data->count[i])
4546 			continue;
4547 
4548 		ieee80211_extend_noa_desc(data, tsf, i);
4549 		start = data->desc[i].start - tsf;
4550 		if (start <= 0)
4551 			data->absent |= BIT(i);
4552 
4553 		if (next_offset > start)
4554 			next_offset = start;
4555 
4556 		data->has_next_tsf = true;
4557 	}
4558 
4559 	if (data->absent)
4560 		next_offset = ieee80211_get_noa_absent_time(data, tsf);
4561 
4562 	data->next_tsf = tsf + next_offset;
4563 }
4564 EXPORT_SYMBOL(ieee80211_update_p2p_noa);
4565 
4566 int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
4567 			    struct ieee80211_noa_data *data, u32 tsf)
4568 {
4569 	int ret = 0;
4570 	int i;
4571 
4572 	memset(data, 0, sizeof(*data));
4573 
4574 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4575 		const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
4576 
4577 		if (!desc->count || !desc->duration)
4578 			continue;
4579 
4580 		data->count[i] = desc->count;
4581 		data->desc[i].start = le32_to_cpu(desc->start_time);
4582 		data->desc[i].duration = le32_to_cpu(desc->duration);
4583 		data->desc[i].interval = le32_to_cpu(desc->interval);
4584 
4585 		if (data->count[i] > 1 &&
4586 		    data->desc[i].interval < data->desc[i].duration)
4587 			continue;
4588 
4589 		ieee80211_extend_noa_desc(data, tsf, i);
4590 		ret++;
4591 	}
4592 
4593 	if (ret)
4594 		ieee80211_update_p2p_noa(data, tsf);
4595 
4596 	return ret;
4597 }
4598 EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
4599 
4600 void ieee80211_recalc_dtim(struct ieee80211_local *local,
4601 			   struct ieee80211_sub_if_data *sdata)
4602 {
4603 	u64 tsf = drv_get_tsf(local, sdata);
4604 	u64 dtim_count = 0;
4605 	u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
4606 	u8 dtim_period = sdata->vif.bss_conf.dtim_period;
4607 	struct ps_data *ps;
4608 	u8 bcns_from_dtim;
4609 
4610 	if (tsf == -1ULL || !beacon_int || !dtim_period)
4611 		return;
4612 
4613 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
4614 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
4615 		if (!sdata->bss)
4616 			return;
4617 
4618 		ps = &sdata->bss->ps;
4619 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4620 		ps = &sdata->u.mesh.ps;
4621 	} else {
4622 		return;
4623 	}
4624 
4625 	/*
4626 	 * actually finds last dtim_count, mac80211 will update in
4627 	 * __beacon_add_tim().
4628 	 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
4629 	 */
4630 	do_div(tsf, beacon_int);
4631 	bcns_from_dtim = do_div(tsf, dtim_period);
4632 	/* just had a DTIM */
4633 	if (!bcns_from_dtim)
4634 		dtim_count = 0;
4635 	else
4636 		dtim_count = dtim_period - bcns_from_dtim;
4637 
4638 	ps->dtim_count = dtim_count;
4639 }
4640 
4641 static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
4642 					 struct ieee80211_chanctx *ctx)
4643 {
4644 	struct ieee80211_link_data *link;
4645 	u8 radar_detect = 0;
4646 
4647 	lockdep_assert_held(&local->chanctx_mtx);
4648 
4649 	if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
4650 		return 0;
4651 
4652 	list_for_each_entry(link, &ctx->reserved_links, reserved_chanctx_list)
4653 		if (link->reserved_radar_required)
4654 			radar_detect |= BIT(link->reserved_chandef.width);
4655 
4656 	/*
4657 	 * An in-place reservation context should not have any assigned vifs
4658 	 * until it replaces the other context.
4659 	 */
4660 	WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
4661 		!list_empty(&ctx->assigned_links));
4662 
4663 	list_for_each_entry(link, &ctx->assigned_links, assigned_chanctx_list) {
4664 		if (!link->radar_required)
4665 			continue;
4666 
4667 		radar_detect |=
4668 			BIT(link->conf->chandef.width);
4669 	}
4670 
4671 	return radar_detect;
4672 }
4673 
4674 int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
4675 				 const struct cfg80211_chan_def *chandef,
4676 				 enum ieee80211_chanctx_mode chanmode,
4677 				 u8 radar_detect)
4678 {
4679 	struct ieee80211_local *local = sdata->local;
4680 	struct ieee80211_sub_if_data *sdata_iter;
4681 	enum nl80211_iftype iftype = sdata->wdev.iftype;
4682 	struct ieee80211_chanctx *ctx;
4683 	int total = 1;
4684 	struct iface_combination_params params = {
4685 		.radar_detect = radar_detect,
4686 	};
4687 
4688 	lockdep_assert_held(&local->chanctx_mtx);
4689 
4690 	if (WARN_ON(hweight32(radar_detect) > 1))
4691 		return -EINVAL;
4692 
4693 	if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
4694 		    !chandef->chan))
4695 		return -EINVAL;
4696 
4697 	if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
4698 		return -EINVAL;
4699 
4700 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
4701 	    sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
4702 		/*
4703 		 * always passing this is harmless, since it'll be the
4704 		 * same value that cfg80211 finds if it finds the same
4705 		 * interface ... and that's always allowed
4706 		 */
4707 		params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
4708 	}
4709 
4710 	/* Always allow software iftypes */
4711 	if (cfg80211_iftype_allowed(local->hw.wiphy, iftype, 0, 1)) {
4712 		if (radar_detect)
4713 			return -EINVAL;
4714 		return 0;
4715 	}
4716 
4717 	if (chandef)
4718 		params.num_different_channels = 1;
4719 
4720 	if (iftype != NL80211_IFTYPE_UNSPECIFIED)
4721 		params.iftype_num[iftype] = 1;
4722 
4723 	list_for_each_entry(ctx, &local->chanctx_list, list) {
4724 		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
4725 			continue;
4726 		params.radar_detect |=
4727 			ieee80211_chanctx_radar_detect(local, ctx);
4728 		if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
4729 			params.num_different_channels++;
4730 			continue;
4731 		}
4732 		if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
4733 		    cfg80211_chandef_compatible(chandef,
4734 						&ctx->conf.def))
4735 			continue;
4736 		params.num_different_channels++;
4737 	}
4738 
4739 	list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
4740 		struct wireless_dev *wdev_iter;
4741 
4742 		wdev_iter = &sdata_iter->wdev;
4743 
4744 		if (sdata_iter == sdata ||
4745 		    !ieee80211_sdata_running(sdata_iter) ||
4746 		    cfg80211_iftype_allowed(local->hw.wiphy,
4747 					    wdev_iter->iftype, 0, 1))
4748 			continue;
4749 
4750 		params.iftype_num[wdev_iter->iftype]++;
4751 		total++;
4752 	}
4753 
4754 	if (total == 1 && !params.radar_detect)
4755 		return 0;
4756 
4757 	return cfg80211_check_combinations(local->hw.wiphy, &params);
4758 }
4759 
4760 static void
4761 ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
4762 			 void *data)
4763 {
4764 	u32 *max_num_different_channels = data;
4765 
4766 	*max_num_different_channels = max(*max_num_different_channels,
4767 					  c->num_different_channels);
4768 }
4769 
4770 int ieee80211_max_num_channels(struct ieee80211_local *local)
4771 {
4772 	struct ieee80211_sub_if_data *sdata;
4773 	struct ieee80211_chanctx *ctx;
4774 	u32 max_num_different_channels = 1;
4775 	int err;
4776 	struct iface_combination_params params = {0};
4777 
4778 	lockdep_assert_held(&local->chanctx_mtx);
4779 
4780 	list_for_each_entry(ctx, &local->chanctx_list, list) {
4781 		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
4782 			continue;
4783 
4784 		params.num_different_channels++;
4785 
4786 		params.radar_detect |=
4787 			ieee80211_chanctx_radar_detect(local, ctx);
4788 	}
4789 
4790 	list_for_each_entry_rcu(sdata, &local->interfaces, list)
4791 		params.iftype_num[sdata->wdev.iftype]++;
4792 
4793 	err = cfg80211_iter_combinations(local->hw.wiphy, &params,
4794 					 ieee80211_iter_max_chans,
4795 					 &max_num_different_channels);
4796 	if (err < 0)
4797 		return err;
4798 
4799 	return max_num_different_channels;
4800 }
4801 
4802 void ieee80211_add_s1g_capab_ie(struct ieee80211_sub_if_data *sdata,
4803 				struct ieee80211_sta_s1g_cap *caps,
4804 				struct sk_buff *skb)
4805 {
4806 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4807 	struct ieee80211_s1g_cap s1g_capab;
4808 	u8 *pos;
4809 	int i;
4810 
4811 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
4812 		return;
4813 
4814 	if (!caps->s1g)
4815 		return;
4816 
4817 	memcpy(s1g_capab.capab_info, caps->cap, sizeof(caps->cap));
4818 	memcpy(s1g_capab.supp_mcs_nss, caps->nss_mcs, sizeof(caps->nss_mcs));
4819 
4820 	/* override the capability info */
4821 	for (i = 0; i < sizeof(ifmgd->s1g_capa.capab_info); i++) {
4822 		u8 mask = ifmgd->s1g_capa_mask.capab_info[i];
4823 
4824 		s1g_capab.capab_info[i] &= ~mask;
4825 		s1g_capab.capab_info[i] |= ifmgd->s1g_capa.capab_info[i] & mask;
4826 	}
4827 
4828 	/* then MCS and NSS set */
4829 	for (i = 0; i < sizeof(ifmgd->s1g_capa.supp_mcs_nss); i++) {
4830 		u8 mask = ifmgd->s1g_capa_mask.supp_mcs_nss[i];
4831 
4832 		s1g_capab.supp_mcs_nss[i] &= ~mask;
4833 		s1g_capab.supp_mcs_nss[i] |=
4834 			ifmgd->s1g_capa.supp_mcs_nss[i] & mask;
4835 	}
4836 
4837 	pos = skb_put(skb, 2 + sizeof(s1g_capab));
4838 	*pos++ = WLAN_EID_S1G_CAPABILITIES;
4839 	*pos++ = sizeof(s1g_capab);
4840 
4841 	memcpy(pos, &s1g_capab, sizeof(s1g_capab));
4842 }
4843 
4844 void ieee80211_add_aid_request_ie(struct ieee80211_sub_if_data *sdata,
4845 				  struct sk_buff *skb)
4846 {
4847 	u8 *pos = skb_put(skb, 3);
4848 
4849 	*pos++ = WLAN_EID_AID_REQUEST;
4850 	*pos++ = 1;
4851 	*pos++ = 0;
4852 }
4853 
4854 u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
4855 {
4856 	*buf++ = WLAN_EID_VENDOR_SPECIFIC;
4857 	*buf++ = 7; /* len */
4858 	*buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
4859 	*buf++ = 0x50;
4860 	*buf++ = 0xf2;
4861 	*buf++ = 2; /* WME */
4862 	*buf++ = 0; /* WME info */
4863 	*buf++ = 1; /* WME ver */
4864 	*buf++ = qosinfo; /* U-APSD no in use */
4865 
4866 	return buf;
4867 }
4868 
4869 void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
4870 			     unsigned long *frame_cnt,
4871 			     unsigned long *byte_cnt)
4872 {
4873 	struct txq_info *txqi = to_txq_info(txq);
4874 	u32 frag_cnt = 0, frag_bytes = 0;
4875 	struct sk_buff *skb;
4876 
4877 	skb_queue_walk(&txqi->frags, skb) {
4878 		frag_cnt++;
4879 		frag_bytes += skb->len;
4880 	}
4881 
4882 	if (frame_cnt)
4883 		*frame_cnt = txqi->tin.backlog_packets + frag_cnt;
4884 
4885 	if (byte_cnt)
4886 		*byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
4887 }
4888 EXPORT_SYMBOL(ieee80211_txq_get_depth);
4889 
4890 const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
4891 	IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
4892 	IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
4893 	IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
4894 	IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
4895 };
4896 
4897 u16 ieee80211_encode_usf(int listen_interval)
4898 {
4899 	static const int listen_int_usf[] = { 1, 10, 1000, 10000 };
4900 	u16 ui, usf = 0;
4901 
4902 	/* find greatest USF */
4903 	while (usf < IEEE80211_MAX_USF) {
4904 		if (listen_interval % listen_int_usf[usf + 1])
4905 			break;
4906 		usf += 1;
4907 	}
4908 	ui = listen_interval / listen_int_usf[usf];
4909 
4910 	/* error if there is a remainder. Should've been checked by user */
4911 	WARN_ON_ONCE(ui > IEEE80211_MAX_UI);
4912 	listen_interval = FIELD_PREP(LISTEN_INT_USF, usf) |
4913 			  FIELD_PREP(LISTEN_INT_UI, ui);
4914 
4915 	return (u16) listen_interval;
4916 }
4917 
4918 u8 ieee80211_ie_len_eht_cap(struct ieee80211_sub_if_data *sdata, u8 iftype)
4919 {
4920 	const struct ieee80211_sta_he_cap *he_cap;
4921 	const struct ieee80211_sta_eht_cap *eht_cap;
4922 	struct ieee80211_supported_band *sband;
4923 	bool is_ap;
4924 	u8 n;
4925 
4926 	sband = ieee80211_get_sband(sdata);
4927 	if (!sband)
4928 		return 0;
4929 
4930 	he_cap = ieee80211_get_he_iftype_cap(sband, iftype);
4931 	eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype);
4932 	if (!he_cap || !eht_cap)
4933 		return 0;
4934 
4935 	is_ap = iftype == NL80211_IFTYPE_AP ||
4936 		iftype == NL80211_IFTYPE_P2P_GO;
4937 
4938 	n = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
4939 				       &eht_cap->eht_cap_elem,
4940 				       is_ap);
4941 	return 2 + 1 +
4942 	       sizeof(he_cap->he_cap_elem) + n +
4943 	       ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
4944 				      eht_cap->eht_cap_elem.phy_cap_info);
4945 	return 0;
4946 }
4947 
4948 u8 *ieee80211_ie_build_eht_cap(u8 *pos,
4949 			       const struct ieee80211_sta_he_cap *he_cap,
4950 			       const struct ieee80211_sta_eht_cap *eht_cap,
4951 			       u8 *end,
4952 			       bool for_ap)
4953 {
4954 	u8 mcs_nss_len, ppet_len;
4955 	u8 ie_len;
4956 	u8 *orig_pos = pos;
4957 
4958 	/* Make sure we have place for the IE */
4959 	if (!he_cap || !eht_cap)
4960 		return orig_pos;
4961 
4962 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
4963 						 &eht_cap->eht_cap_elem,
4964 						 for_ap);
4965 	ppet_len = ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
4966 					  eht_cap->eht_cap_elem.phy_cap_info);
4967 
4968 	ie_len = 2 + 1 + sizeof(eht_cap->eht_cap_elem) + mcs_nss_len + ppet_len;
4969 	if ((end - pos) < ie_len)
4970 		return orig_pos;
4971 
4972 	*pos++ = WLAN_EID_EXTENSION;
4973 	*pos++ = ie_len - 2;
4974 	*pos++ = WLAN_EID_EXT_EHT_CAPABILITY;
4975 
4976 	/* Fixed data */
4977 	memcpy(pos, &eht_cap->eht_cap_elem, sizeof(eht_cap->eht_cap_elem));
4978 	pos += sizeof(eht_cap->eht_cap_elem);
4979 
4980 	memcpy(pos, &eht_cap->eht_mcs_nss_supp, mcs_nss_len);
4981 	pos += mcs_nss_len;
4982 
4983 	if (ppet_len) {
4984 		memcpy(pos, &eht_cap->eht_ppe_thres, ppet_len);
4985 		pos += ppet_len;
4986 	}
4987 
4988 	return pos;
4989 }
4990 
4991 void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos)
4992 {
4993 	unsigned int elem_len;
4994 
4995 	if (!len_pos)
4996 		return;
4997 
4998 	elem_len = skb->data + skb->len - len_pos - 1;
4999 
5000 	while (elem_len > 255) {
5001 		/* this one is 255 */
5002 		*len_pos = 255;
5003 		/* remaining data gets smaller */
5004 		elem_len -= 255;
5005 		/* make space for the fragment ID/len in SKB */
5006 		skb_put(skb, 2);
5007 		/* shift back the remaining data to place fragment ID/len */
5008 		memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len);
5009 		/* place the fragment ID */
5010 		len_pos += 255 + 1;
5011 		*len_pos = WLAN_EID_FRAGMENT;
5012 		/* and point to fragment length to update later */
5013 		len_pos++;
5014 	}
5015 
5016 	*len_pos = elem_len;
5017 }
5018