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