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