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