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