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