xref: /linux/net/mac80211/util.c (revision 40286d6379aacfcc053253ef78dc78b09addffda)
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 				ieee80211_link_info_change_notify(sdata, link,
2151 								  changed);
2152 				changed = BSS_CHANGED_ASSOC |
2153 					  BSS_CHANGED_IDLE |
2154 					  BSS_CHANGED_PS |
2155 					  BSS_CHANGED_ARP_FILTER;
2156 				ieee80211_vif_cfg_change_notify(sdata, changed);
2157 			}
2158 			break;
2159 		case NL80211_IFTYPE_OCB:
2160 			changed |= BSS_CHANGED_OCB;
2161 			ieee80211_bss_info_change_notify(sdata, changed);
2162 			break;
2163 		case NL80211_IFTYPE_ADHOC:
2164 			changed |= BSS_CHANGED_IBSS;
2165 			fallthrough;
2166 		case NL80211_IFTYPE_AP:
2167 			changed |= BSS_CHANGED_P2P_PS;
2168 
2169 			if (ieee80211_vif_is_mld(&sdata->vif))
2170 				ieee80211_vif_cfg_change_notify(sdata,
2171 								BSS_CHANGED_SSID);
2172 			else
2173 				changed |= BSS_CHANGED_SSID;
2174 
2175 			if (sdata->vif.bss_conf.ftm_responder == 1 &&
2176 			    wiphy_ext_feature_isset(sdata->local->hw.wiphy,
2177 					NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
2178 				changed |= BSS_CHANGED_FTM_RESPONDER;
2179 
2180 			if (sdata->vif.type == NL80211_IFTYPE_AP) {
2181 				changed |= BSS_CHANGED_AP_PROBE_RESP;
2182 
2183 				if (ieee80211_vif_is_mld(&sdata->vif)) {
2184 					ieee80211_reconfig_ap_links(local,
2185 								    sdata,
2186 								    changed);
2187 					break;
2188 				}
2189 
2190 				if (rcu_access_pointer(sdata->deflink.u.ap.beacon))
2191 					drv_start_ap(local, sdata,
2192 						     sdata->deflink.conf);
2193 			}
2194 			fallthrough;
2195 		case NL80211_IFTYPE_MESH_POINT:
2196 			if (sdata->vif.bss_conf.enable_beacon) {
2197 				changed |= BSS_CHANGED_BEACON |
2198 					   BSS_CHANGED_BEACON_ENABLED;
2199 				ieee80211_bss_info_change_notify(sdata, changed);
2200 			}
2201 			break;
2202 		case NL80211_IFTYPE_NAN:
2203 			res = ieee80211_reconfig_nan(sdata);
2204 			if (res < 0) {
2205 				ieee80211_handle_reconfig_failure(local);
2206 				return res;
2207 			}
2208 			break;
2209 		case NL80211_IFTYPE_NAN_DATA:
2210 		case NL80211_IFTYPE_AP_VLAN:
2211 		case NL80211_IFTYPE_MONITOR:
2212 		case NL80211_IFTYPE_P2P_DEVICE:
2213 			/* nothing to do */
2214 			break;
2215 		case NL80211_IFTYPE_UNSPECIFIED:
2216 		case NUM_NL80211_IFTYPES:
2217 		case NL80211_IFTYPE_P2P_CLIENT:
2218 		case NL80211_IFTYPE_P2P_GO:
2219 		case NL80211_IFTYPE_WDS:
2220 			WARN_ON(1);
2221 			break;
2222 		}
2223 	}
2224 
2225 	ieee80211_recalc_ps(local);
2226 
2227 	/*
2228 	 * The sta might be in psm against the ap (e.g. because
2229 	 * this was the state before a hw restart), so we
2230 	 * explicitly send a null packet in order to make sure
2231 	 * it'll sync against the ap (and get out of psm).
2232 	 */
2233 	if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
2234 		list_for_each_entry(sdata, &local->interfaces, list) {
2235 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
2236 				continue;
2237 			if (!sdata->u.mgd.associated)
2238 				continue;
2239 
2240 			ieee80211_send_nullfunc(local, sdata, false);
2241 		}
2242 	}
2243 
2244 	/* APs are now beaconing, add back stations */
2245 	list_for_each_entry(sdata, &local->interfaces, list) {
2246 		if (!ieee80211_sdata_running(sdata))
2247 			continue;
2248 
2249 		switch (sdata->vif.type) {
2250 		case NL80211_IFTYPE_AP_VLAN:
2251 		case NL80211_IFTYPE_AP:
2252 			ieee80211_reconfig_stations(sdata);
2253 			break;
2254 		default:
2255 			break;
2256 		}
2257 	}
2258 
2259 	/* add back keys */
2260 	list_for_each_entry(sdata, &local->interfaces, list)
2261 		ieee80211_reenable_keys(sdata);
2262 
2263 	/* re-enable multi-link for client interfaces */
2264 	list_for_each_entry(sdata, &local->interfaces, list) {
2265 		if (sdata->restart_active_links)
2266 			ieee80211_set_active_links(&sdata->vif,
2267 						   sdata->restart_active_links);
2268 		/*
2269 		 * If a link switch was scheduled before the restart, and ran
2270 		 * before reconfig, it will do nothing, so re-schedule.
2271 		 */
2272 		if (sdata->desired_active_links)
2273 			wiphy_work_queue(sdata->local->hw.wiphy,
2274 					 &sdata->activate_links_work);
2275 	}
2276 
2277 	/* Reconfigure sched scan if it was interrupted by FW restart */
2278 	sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
2279 						lockdep_is_held(&local->hw.wiphy->mtx));
2280 	sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
2281 						lockdep_is_held(&local->hw.wiphy->mtx));
2282 	if (sched_scan_sdata && sched_scan_req)
2283 		/*
2284 		 * Sched scan stopped, but we don't want to report it. Instead,
2285 		 * we're trying to reschedule. However, if more than one scan
2286 		 * plan was set, we cannot reschedule since we don't know which
2287 		 * scan plan was currently running (and some scan plans may have
2288 		 * already finished).
2289 		 */
2290 		if (sched_scan_req->n_scan_plans > 1 ||
2291 		    __ieee80211_request_sched_scan_start(sched_scan_sdata,
2292 							 sched_scan_req)) {
2293 			RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
2294 			RCU_INIT_POINTER(local->sched_scan_req, NULL);
2295 			sched_scan_stopped = true;
2296 		}
2297 
2298 	if (sched_scan_stopped)
2299 		cfg80211_sched_scan_stopped_locked(local->hw.wiphy, 0);
2300 
2301  wake_up:
2302 	/*
2303 	 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
2304 	 * sessions can be established after a resume.
2305 	 *
2306 	 * Also tear down aggregation sessions since reconfiguring
2307 	 * them in a hardware restart scenario is not easily done
2308 	 * right now, and the hardware will have lost information
2309 	 * about the sessions, but we and the AP still think they
2310 	 * are active. This is really a workaround though.
2311 	 */
2312 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
2313 		list_for_each_entry(sta, &local->sta_list, list) {
2314 			if (!local->resuming)
2315 				ieee80211_sta_tear_down_BA_sessions(
2316 						sta, AGG_STOP_LOCAL_REQUEST);
2317 			clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
2318 		}
2319 	}
2320 
2321 	/*
2322 	 * If this is for hw restart things are still running.
2323 	 * We may want to change that later, however.
2324 	 */
2325 	if (local->open_count && (!suspended || reconfig_due_to_wowlan))
2326 		drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
2327 
2328 	if (local->in_reconfig) {
2329 		in_reconfig = local->in_reconfig;
2330 		local->in_reconfig = false;
2331 		barrier();
2332 
2333 		ieee80211_reconfig_roc(local);
2334 
2335 		/* Requeue all works */
2336 		list_for_each_entry(sdata, &local->interfaces, list) {
2337 			if (ieee80211_sdata_running(sdata))
2338 				wiphy_work_queue(local->hw.wiphy, &sdata->work);
2339 		}
2340 	}
2341 
2342 	ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
2343 					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
2344 					false);
2345 
2346 	if (in_reconfig) {
2347 		list_for_each_entry(sdata, &local->interfaces, list) {
2348 			if (!ieee80211_sdata_running(sdata))
2349 				continue;
2350 			if (sdata->vif.type == NL80211_IFTYPE_STATION)
2351 				ieee80211_sta_restart(sdata);
2352 		}
2353 	}
2354 
2355 	/* Passing NULL means an interface is picked for configuration */
2356 	if (local->virt_monitors > 0 &&
2357 	    local->virt_monitors == local->open_count)
2358 		ieee80211_add_virtual_monitor(local, NULL);
2359 
2360 	if (!suspended)
2361 		return 0;
2362 
2363 #ifdef CONFIG_PM
2364 	/* first set suspended false, then resuming */
2365 	local->suspended = false;
2366 	mb();
2367 	local->resuming = false;
2368 
2369 	ieee80211_flush_completed_scan(local, false);
2370 
2371 	if (local->open_count && !reconfig_due_to_wowlan)
2372 		drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
2373 
2374 	list_for_each_entry(sdata, &local->interfaces, list) {
2375 		if (!ieee80211_sdata_running(sdata))
2376 			continue;
2377 		if (sdata->vif.type == NL80211_IFTYPE_STATION)
2378 			ieee80211_sta_restart(sdata);
2379 	}
2380 
2381 	mod_timer(&local->sta_cleanup, jiffies + 1);
2382 #else
2383 	WARN_ON(1);
2384 #endif
2385 
2386 	return 0;
2387 }
2388 
2389 static void ieee80211_reconfig_disconnect(struct ieee80211_vif *vif, u8 flag)
2390 {
2391 	struct ieee80211_sub_if_data *sdata;
2392 	struct ieee80211_local *local;
2393 	struct ieee80211_key *key;
2394 
2395 	if (WARN_ON(!vif))
2396 		return;
2397 
2398 	sdata = vif_to_sdata(vif);
2399 	local = sdata->local;
2400 
2401 	lockdep_assert_wiphy(local->hw.wiphy);
2402 
2403 	if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_RESUME &&
2404 		    !local->resuming))
2405 		return;
2406 
2407 	if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_HW_RESTART &&
2408 		    !local->in_reconfig))
2409 		return;
2410 
2411 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2412 		return;
2413 
2414 	sdata->flags |= flag;
2415 
2416 	list_for_each_entry(key, &sdata->key_list, list)
2417 		key->flags |= KEY_FLAG_TAINTED;
2418 }
2419 
2420 void ieee80211_hw_restart_disconnect(struct ieee80211_vif *vif)
2421 {
2422 	ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_HW_RESTART);
2423 }
2424 EXPORT_SYMBOL_GPL(ieee80211_hw_restart_disconnect);
2425 
2426 void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
2427 {
2428 	ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_RESUME);
2429 }
2430 EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
2431 
2432 void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata,
2433 			   struct ieee80211_link_data *link)
2434 {
2435 	struct ieee80211_local *local = sdata->local;
2436 	struct ieee80211_chanctx_conf *chanctx_conf;
2437 	struct ieee80211_chanctx *chanctx;
2438 
2439 	lockdep_assert_wiphy(local->hw.wiphy);
2440 
2441 	chanctx_conf = rcu_dereference_protected(link->conf->chanctx_conf,
2442 						 lockdep_is_held(&local->hw.wiphy->mtx));
2443 
2444 	/*
2445 	 * This function can be called from a work, thus it may be possible
2446 	 * that the chanctx_conf is removed (due to a disconnection, for
2447 	 * example).
2448 	 * So nothing should be done in such case.
2449 	 */
2450 	if (!chanctx_conf)
2451 		return;
2452 
2453 	chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
2454 	ieee80211_recalc_smps_chanctx(local, chanctx);
2455 }
2456 
2457 void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata,
2458 				  int link_id)
2459 {
2460 	struct ieee80211_local *local = sdata->local;
2461 	struct ieee80211_chanctx_conf *chanctx_conf;
2462 	struct ieee80211_chanctx *chanctx;
2463 	int i;
2464 
2465 	lockdep_assert_wiphy(local->hw.wiphy);
2466 
2467 	for (i = 0; i < ARRAY_SIZE(sdata->vif.link_conf); i++) {
2468 		struct ieee80211_bss_conf *bss_conf;
2469 
2470 		if (link_id >= 0 && link_id != i)
2471 			continue;
2472 
2473 		rcu_read_lock();
2474 		bss_conf = rcu_dereference(sdata->vif.link_conf[i]);
2475 		if (!bss_conf) {
2476 			rcu_read_unlock();
2477 			continue;
2478 		}
2479 
2480 		chanctx_conf = rcu_dereference_protected(bss_conf->chanctx_conf,
2481 							 lockdep_is_held(&local->hw.wiphy->mtx));
2482 		/*
2483 		 * Since we hold the wiphy mutex (checked above)
2484 		 * we can take the chanctx_conf pointer out of the
2485 		 * RCU critical section, it cannot go away without
2486 		 * the mutex. Just the way we reached it could - in
2487 		 * theory - go away, but we don't really care and
2488 		 * it really shouldn't happen anyway.
2489 		 */
2490 		rcu_read_unlock();
2491 
2492 		if (!chanctx_conf)
2493 			return;
2494 
2495 		chanctx = container_of(chanctx_conf, struct ieee80211_chanctx,
2496 				       conf);
2497 		ieee80211_recalc_chanctx_min_def(local, chanctx);
2498 	}
2499 }
2500 
2501 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
2502 {
2503 	size_t pos = offset;
2504 
2505 	while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
2506 		pos += 2 + ies[pos + 1];
2507 
2508 	return pos;
2509 }
2510 
2511 u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
2512 			      u16 cap)
2513 {
2514 	__le16 tmp;
2515 
2516 	*pos++ = WLAN_EID_HT_CAPABILITY;
2517 	*pos++ = sizeof(struct ieee80211_ht_cap);
2518 	memset(pos, 0, sizeof(struct ieee80211_ht_cap));
2519 
2520 	/* capability flags */
2521 	tmp = cpu_to_le16(cap);
2522 	memcpy(pos, &tmp, sizeof(u16));
2523 	pos += sizeof(u16);
2524 
2525 	/* AMPDU parameters */
2526 	*pos++ = ht_cap->ampdu_factor |
2527 		 (ht_cap->ampdu_density <<
2528 			IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
2529 
2530 	/* MCS set */
2531 	memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
2532 	pos += sizeof(ht_cap->mcs);
2533 
2534 	/* extended capabilities */
2535 	pos += sizeof(__le16);
2536 
2537 	/* BF capabilities */
2538 	pos += sizeof(__le32);
2539 
2540 	/* antenna selection */
2541 	pos += sizeof(u8);
2542 
2543 	return pos;
2544 }
2545 
2546 u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2547 			       u32 cap)
2548 {
2549 	__le32 tmp;
2550 
2551 	*pos++ = WLAN_EID_VHT_CAPABILITY;
2552 	*pos++ = sizeof(struct ieee80211_vht_cap);
2553 	memset(pos, 0, sizeof(struct ieee80211_vht_cap));
2554 
2555 	/* capability flags */
2556 	tmp = cpu_to_le32(cap);
2557 	memcpy(pos, &tmp, sizeof(u32));
2558 	pos += sizeof(u32);
2559 
2560 	/* VHT MCS set */
2561 	memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
2562 	pos += sizeof(vht_cap->vht_mcs);
2563 
2564 	return pos;
2565 }
2566 
2567 /* this may return more than ieee80211_put_he_6ghz_cap() will need */
2568 u8 ieee80211_ie_len_he_cap(struct ieee80211_sub_if_data *sdata)
2569 {
2570 	const struct ieee80211_sta_he_cap *he_cap;
2571 	struct ieee80211_supported_band *sband;
2572 	u8 n;
2573 
2574 	sband = ieee80211_get_sband(sdata);
2575 	if (!sband)
2576 		return 0;
2577 
2578 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
2579 	if (!he_cap)
2580 		return 0;
2581 
2582 	n = ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem);
2583 	return 2 + 1 +
2584 	       sizeof(he_cap->he_cap_elem) + n +
2585 	       ieee80211_he_ppe_size(he_cap->ppe_thres[0],
2586 				     he_cap->he_cap_elem.phy_cap_info);
2587 }
2588 
2589 static void
2590 ieee80211_get_adjusted_he_cap(const struct ieee80211_conn_settings *conn,
2591 			      const struct ieee80211_sta_he_cap *he_cap,
2592 			      struct ieee80211_he_cap_elem *elem)
2593 {
2594 	u8 ru_limit, max_ru;
2595 
2596 	*elem = he_cap->he_cap_elem;
2597 
2598 	switch (conn->bw_limit) {
2599 	case IEEE80211_CONN_BW_LIMIT_20:
2600 		ru_limit = IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_242;
2601 		break;
2602 	case IEEE80211_CONN_BW_LIMIT_40:
2603 		ru_limit = IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484;
2604 		break;
2605 	case IEEE80211_CONN_BW_LIMIT_80:
2606 		ru_limit = IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_996;
2607 		break;
2608 	default:
2609 		ru_limit = IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_2x996;
2610 		break;
2611 	}
2612 
2613 	max_ru = elem->phy_cap_info[8] & IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_MASK;
2614 	max_ru = min(max_ru, ru_limit);
2615 	elem->phy_cap_info[8] &= ~IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_MASK;
2616 	elem->phy_cap_info[8] |= max_ru;
2617 
2618 	if (conn->bw_limit < IEEE80211_CONN_BW_LIMIT_40) {
2619 		elem->phy_cap_info[0] &=
2620 			~(IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2621 			  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G);
2622 		elem->phy_cap_info[9] &=
2623 			~IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM;
2624 	}
2625 
2626 	if (conn->bw_limit < IEEE80211_CONN_BW_LIMIT_160) {
2627 		elem->phy_cap_info[0] &=
2628 			~(IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2629 			  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G);
2630 		elem->phy_cap_info[5] &=
2631 			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_ABOVE_80MHZ_MASK;
2632 		elem->phy_cap_info[7] &=
2633 			~(IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
2634 			  IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ);
2635 	}
2636 }
2637 
2638 int ieee80211_put_he_cap(struct sk_buff *skb,
2639 			 struct ieee80211_sub_if_data *sdata,
2640 			 const struct ieee80211_supported_band *sband,
2641 			 const struct ieee80211_conn_settings *conn)
2642 {
2643 	const struct ieee80211_sta_he_cap *he_cap;
2644 	struct ieee80211_he_cap_elem elem;
2645 	u8 *len;
2646 	u8 n;
2647 	u8 ie_len;
2648 
2649 	if (!conn)
2650 		conn = &ieee80211_conn_settings_unlimited;
2651 
2652 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
2653 	if (!he_cap)
2654 		return 0;
2655 
2656 	/* modify on stack first to calculate 'n' and 'ie_len' correctly */
2657 	ieee80211_get_adjusted_he_cap(conn, he_cap, &elem);
2658 
2659 	n = ieee80211_he_mcs_nss_size(&elem);
2660 	ie_len = 2 + 1 +
2661 		 sizeof(he_cap->he_cap_elem) + n +
2662 		 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
2663 				       he_cap->he_cap_elem.phy_cap_info);
2664 
2665 	if (skb_tailroom(skb) < ie_len)
2666 		return -ENOBUFS;
2667 
2668 	skb_put_u8(skb, WLAN_EID_EXTENSION);
2669 	len = skb_put(skb, 1); /* We'll set the size later below */
2670 	skb_put_u8(skb, WLAN_EID_EXT_HE_CAPABILITY);
2671 
2672 	/* Fixed data */
2673 	skb_put_data(skb, &elem, sizeof(elem));
2674 
2675 	skb_put_data(skb, &he_cap->he_mcs_nss_supp, n);
2676 
2677 	/* Check if PPE Threshold should be present */
2678 	if ((he_cap->he_cap_elem.phy_cap_info[6] &
2679 	     IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) == 0)
2680 		goto end;
2681 
2682 	/*
2683 	 * Calculate how many PPET16/PPET8 pairs are to come. Algorithm:
2684 	 * (NSS_M1 + 1) x (num of 1 bits in RU_INDEX_BITMASK)
2685 	 */
2686 	n = hweight8(he_cap->ppe_thres[0] &
2687 		     IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
2688 	n *= (1 + ((he_cap->ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) >>
2689 		   IEEE80211_PPE_THRES_NSS_POS));
2690 
2691 	/*
2692 	 * Each pair is 6 bits, and we need to add the 7 "header" bits to the
2693 	 * total size.
2694 	 */
2695 	n = (n * IEEE80211_PPE_THRES_INFO_PPET_SIZE * 2) + 7;
2696 	n = DIV_ROUND_UP(n, 8);
2697 
2698 	/* Copy PPE Thresholds */
2699 	skb_put_data(skb, &he_cap->ppe_thres, n);
2700 
2701 end:
2702 	*len = skb_tail_pointer(skb) - len - 1;
2703 	return 0;
2704 }
2705 
2706 int ieee80211_put_reg_conn(struct sk_buff *skb,
2707 			   enum ieee80211_channel_flags flags)
2708 {
2709 	u8 reg_conn = IEEE80211_REG_CONN_LPI_VALID |
2710 		      IEEE80211_REG_CONN_LPI_VALUE |
2711 		      IEEE80211_REG_CONN_SP_VALID;
2712 
2713 	if (!(flags & IEEE80211_CHAN_NO_6GHZ_AFC_CLIENT))
2714 		reg_conn |= IEEE80211_REG_CONN_SP_VALUE;
2715 
2716 	skb_put_u8(skb, WLAN_EID_EXTENSION);
2717 	skb_put_u8(skb, 1 + sizeof(reg_conn));
2718 	skb_put_u8(skb, WLAN_EID_EXT_NON_AP_STA_REG_CON);
2719 	skb_put_u8(skb, reg_conn);
2720 	return 0;
2721 }
2722 
2723 int ieee80211_put_he_6ghz_cap(struct sk_buff *skb,
2724 			      struct ieee80211_sub_if_data *sdata,
2725 			      enum ieee80211_smps_mode smps_mode)
2726 {
2727 	struct ieee80211_supported_band *sband;
2728 	const struct ieee80211_sband_iftype_data *iftd;
2729 	enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
2730 	__le16 cap;
2731 
2732 	if (!cfg80211_any_usable_channels(sdata->local->hw.wiphy,
2733 					  BIT(NL80211_BAND_6GHZ),
2734 					  IEEE80211_CHAN_NO_HE))
2735 		return 0;
2736 
2737 	sband = sdata->local->hw.wiphy->bands[NL80211_BAND_6GHZ];
2738 
2739 	iftd = ieee80211_get_sband_iftype_data(sband, iftype);
2740 	if (!iftd)
2741 		return 0;
2742 
2743 	/* Check for device HE 6 GHz capability before adding element */
2744 	if (!iftd->he_6ghz_capa.capa)
2745 		return 0;
2746 
2747 	cap = iftd->he_6ghz_capa.capa;
2748 	cap &= cpu_to_le16(~IEEE80211_HE_6GHZ_CAP_SM_PS);
2749 
2750 	switch (smps_mode) {
2751 	case IEEE80211_SMPS_AUTOMATIC:
2752 	case IEEE80211_SMPS_NUM_MODES:
2753 		WARN_ON(1);
2754 		fallthrough;
2755 	case IEEE80211_SMPS_OFF:
2756 		cap |= le16_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
2757 					IEEE80211_HE_6GHZ_CAP_SM_PS);
2758 		break;
2759 	case IEEE80211_SMPS_STATIC:
2760 		cap |= le16_encode_bits(WLAN_HT_CAP_SM_PS_STATIC,
2761 					IEEE80211_HE_6GHZ_CAP_SM_PS);
2762 		break;
2763 	case IEEE80211_SMPS_DYNAMIC:
2764 		cap |= le16_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
2765 					IEEE80211_HE_6GHZ_CAP_SM_PS);
2766 		break;
2767 	}
2768 
2769 	if (skb_tailroom(skb) < 2 + 1 + sizeof(cap))
2770 		return -ENOBUFS;
2771 
2772 	skb_put_u8(skb, WLAN_EID_EXTENSION);
2773 	skb_put_u8(skb, 1 + sizeof(cap));
2774 	skb_put_u8(skb, WLAN_EID_EXT_HE_6GHZ_CAPA);
2775 	skb_put_data(skb, &cap, sizeof(cap));
2776 	return 0;
2777 }
2778 
2779 u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
2780 			       const struct cfg80211_chan_def *chandef,
2781 			       u16 prot_mode, bool rifs_mode)
2782 {
2783 	struct ieee80211_ht_operation *ht_oper;
2784 	/* Build HT Information */
2785 	*pos++ = WLAN_EID_HT_OPERATION;
2786 	*pos++ = sizeof(struct ieee80211_ht_operation);
2787 	ht_oper = (struct ieee80211_ht_operation *)pos;
2788 	ht_oper->primary_chan = ieee80211_frequency_to_channel(
2789 					chandef->chan->center_freq);
2790 	switch (chandef->width) {
2791 	case NL80211_CHAN_WIDTH_160:
2792 	case NL80211_CHAN_WIDTH_80P80:
2793 	case NL80211_CHAN_WIDTH_80:
2794 	case NL80211_CHAN_WIDTH_40:
2795 		if (chandef->center_freq1 > chandef->chan->center_freq)
2796 			ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2797 		else
2798 			ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
2799 		break;
2800 	case NL80211_CHAN_WIDTH_320:
2801 		/* HT information element should not be included on 6GHz */
2802 		WARN_ON(1);
2803 		return pos;
2804 	default:
2805 		ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
2806 		break;
2807 	}
2808 	if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
2809 	    chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
2810 	    chandef->width != NL80211_CHAN_WIDTH_20)
2811 		ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
2812 
2813 	if (rifs_mode)
2814 		ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
2815 
2816 	ht_oper->operation_mode = cpu_to_le16(prot_mode);
2817 	ht_oper->stbc_param = 0x0000;
2818 
2819 	/* It seems that Basic MCS set and Supported MCS set
2820 	   are identical for the first 10 bytes */
2821 	memset(&ht_oper->basic_set, 0, 16);
2822 	memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
2823 
2824 	return pos + sizeof(struct ieee80211_ht_operation);
2825 }
2826 
2827 void ieee80211_ie_build_wide_bw_cs(u8 *pos,
2828 				   const struct cfg80211_chan_def *chandef)
2829 {
2830 	*pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH;	/* EID */
2831 	*pos++ = 3;					/* IE length */
2832 	/* New channel width */
2833 	switch (chandef->width) {
2834 	case NL80211_CHAN_WIDTH_80:
2835 		*pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ;
2836 		break;
2837 	case NL80211_CHAN_WIDTH_160:
2838 		*pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ;
2839 		break;
2840 	case NL80211_CHAN_WIDTH_80P80:
2841 		*pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
2842 		break;
2843 	case NL80211_CHAN_WIDTH_320:
2844 		/* The behavior is not defined for 320 MHz channels */
2845 		WARN_ON(1);
2846 		fallthrough;
2847 	default:
2848 		*pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT;
2849 	}
2850 
2851 	/* new center frequency segment 0 */
2852 	*pos++ = ieee80211_frequency_to_channel(chandef->center_freq1);
2853 	/* new center frequency segment 1 */
2854 	if (chandef->center_freq2)
2855 		*pos++ = ieee80211_frequency_to_channel(chandef->center_freq2);
2856 	else
2857 		*pos++ = 0;
2858 }
2859 
2860 u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2861 				const struct cfg80211_chan_def *chandef)
2862 {
2863 	struct ieee80211_vht_operation *vht_oper;
2864 
2865 	*pos++ = WLAN_EID_VHT_OPERATION;
2866 	*pos++ = sizeof(struct ieee80211_vht_operation);
2867 	vht_oper = (struct ieee80211_vht_operation *)pos;
2868 	vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
2869 							chandef->center_freq1);
2870 	if (chandef->center_freq2)
2871 		vht_oper->center_freq_seg1_idx =
2872 			ieee80211_frequency_to_channel(chandef->center_freq2);
2873 	else
2874 		vht_oper->center_freq_seg1_idx = 0x00;
2875 
2876 	switch (chandef->width) {
2877 	case NL80211_CHAN_WIDTH_160:
2878 		/*
2879 		 * Convert 160 MHz channel width to new style as interop
2880 		 * workaround.
2881 		 */
2882 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
2883 		vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
2884 		if (chandef->chan->center_freq < chandef->center_freq1)
2885 			vht_oper->center_freq_seg0_idx -= 8;
2886 		else
2887 			vht_oper->center_freq_seg0_idx += 8;
2888 		break;
2889 	case NL80211_CHAN_WIDTH_80P80:
2890 		/*
2891 		 * Convert 80+80 MHz channel width to new style as interop
2892 		 * workaround.
2893 		 */
2894 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
2895 		break;
2896 	case NL80211_CHAN_WIDTH_80:
2897 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
2898 		break;
2899 	case NL80211_CHAN_WIDTH_320:
2900 		/* VHT information element should not be included on 6GHz */
2901 		WARN_ON(1);
2902 		return pos;
2903 	default:
2904 		vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
2905 		break;
2906 	}
2907 
2908 	/* don't require special VHT peer rates */
2909 	vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
2910 
2911 	return pos + sizeof(struct ieee80211_vht_operation);
2912 }
2913 
2914 u8 *ieee80211_ie_build_he_oper(u8 *pos, const struct cfg80211_chan_def *chandef)
2915 {
2916 	struct ieee80211_he_operation *he_oper;
2917 	struct ieee80211_he_6ghz_oper *he_6ghz_op;
2918 	struct cfg80211_chan_def he_chandef;
2919 	u32 he_oper_params;
2920 	u8 ie_len = 1 + sizeof(struct ieee80211_he_operation);
2921 
2922 	if (chandef->chan->band == NL80211_BAND_6GHZ)
2923 		ie_len += sizeof(struct ieee80211_he_6ghz_oper);
2924 
2925 	*pos++ = WLAN_EID_EXTENSION;
2926 	*pos++ = ie_len;
2927 	*pos++ = WLAN_EID_EXT_HE_OPERATION;
2928 
2929 	he_oper_params = 0;
2930 	he_oper_params |= u32_encode_bits(1023, /* disabled */
2931 				IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
2932 	he_oper_params |= u32_encode_bits(1,
2933 				IEEE80211_HE_OPERATION_ER_SU_DISABLE);
2934 	he_oper_params |= u32_encode_bits(1,
2935 				IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
2936 	if (chandef->chan->band == NL80211_BAND_6GHZ)
2937 		he_oper_params |= u32_encode_bits(1,
2938 				IEEE80211_HE_OPERATION_6GHZ_OP_INFO);
2939 
2940 	he_oper = (struct ieee80211_he_operation *)pos;
2941 	he_oper->he_oper_params = cpu_to_le32(he_oper_params);
2942 
2943 	/* don't require special HE peer rates */
2944 	he_oper->he_mcs_nss_set = cpu_to_le16(0xffff);
2945 	pos += sizeof(struct ieee80211_he_operation);
2946 
2947 	if (chandef->chan->band != NL80211_BAND_6GHZ)
2948 		goto out;
2949 
2950 	cfg80211_chandef_create(&he_chandef, chandef->chan, NL80211_CHAN_NO_HT);
2951 	he_chandef.center_freq1 = chandef->center_freq1;
2952 	he_chandef.center_freq2 = chandef->center_freq2;
2953 	he_chandef.width = chandef->width;
2954 
2955 	/* TODO add VHT operational */
2956 	he_6ghz_op = (struct ieee80211_he_6ghz_oper *)pos;
2957 	he_6ghz_op->minrate = 6; /* 6 Mbps */
2958 	he_6ghz_op->primary =
2959 		ieee80211_frequency_to_channel(he_chandef.chan->center_freq);
2960 	he_6ghz_op->ccfs0 =
2961 		ieee80211_frequency_to_channel(he_chandef.center_freq1);
2962 	if (he_chandef.center_freq2)
2963 		he_6ghz_op->ccfs1 =
2964 			ieee80211_frequency_to_channel(he_chandef.center_freq2);
2965 	else
2966 		he_6ghz_op->ccfs1 = 0;
2967 
2968 	switch (he_chandef.width) {
2969 	case NL80211_CHAN_WIDTH_320:
2970 		/* Downgrade EHT 320 MHz BW to 160 MHz for HE and set new
2971 		 * center_freq1
2972 		 */
2973 		ieee80211_chandef_downgrade(&he_chandef, NULL);
2974 		he_6ghz_op->ccfs0 =
2975 			ieee80211_frequency_to_channel(he_chandef.center_freq1);
2976 		fallthrough;
2977 	case NL80211_CHAN_WIDTH_160:
2978 		/* Convert 160 MHz channel width to new style as interop
2979 		 * workaround.
2980 		 */
2981 		he_6ghz_op->control =
2982 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
2983 		he_6ghz_op->ccfs1 = he_6ghz_op->ccfs0;
2984 		if (he_chandef.chan->center_freq < he_chandef.center_freq1)
2985 			he_6ghz_op->ccfs0 -= 8;
2986 		else
2987 			he_6ghz_op->ccfs0 += 8;
2988 		fallthrough;
2989 	case NL80211_CHAN_WIDTH_80P80:
2990 		he_6ghz_op->control =
2991 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
2992 		break;
2993 	case NL80211_CHAN_WIDTH_80:
2994 		he_6ghz_op->control =
2995 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ;
2996 		break;
2997 	case NL80211_CHAN_WIDTH_40:
2998 		he_6ghz_op->control =
2999 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ;
3000 		break;
3001 	default:
3002 		he_6ghz_op->control =
3003 			IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ;
3004 		break;
3005 	}
3006 
3007 	pos += sizeof(struct ieee80211_he_6ghz_oper);
3008 
3009 out:
3010 	return pos;
3011 }
3012 
3013 u8 *ieee80211_ie_build_eht_oper(u8 *pos, const struct cfg80211_chan_def *chandef,
3014 				const struct ieee80211_sta_eht_cap *eht_cap)
3015 
3016 {
3017 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
3018 					&eht_cap->eht_mcs_nss_supp.only_20mhz;
3019 	struct ieee80211_eht_operation *eht_oper;
3020 	struct ieee80211_eht_operation_info *eht_oper_info;
3021 	u8 eht_oper_len = offsetof(struct ieee80211_eht_operation, optional);
3022 	u8 eht_oper_info_len =
3023 		offsetof(struct ieee80211_eht_operation_info, optional);
3024 	u8 chan_width = 0;
3025 
3026 	*pos++ = WLAN_EID_EXTENSION;
3027 	*pos++ = 1 + eht_oper_len + eht_oper_info_len;
3028 	*pos++ = WLAN_EID_EXT_EHT_OPERATION;
3029 
3030 	eht_oper = (struct ieee80211_eht_operation *)pos;
3031 
3032 	memcpy(&eht_oper->basic_mcs_nss, eht_mcs_nss, sizeof(*eht_mcs_nss));
3033 	eht_oper->params |= IEEE80211_EHT_OPER_INFO_PRESENT;
3034 	pos += eht_oper_len;
3035 
3036 	eht_oper_info =
3037 		(struct ieee80211_eht_operation_info *)eht_oper->optional;
3038 
3039 	eht_oper_info->ccfs0 =
3040 		ieee80211_frequency_to_channel(chandef->center_freq1);
3041 	if (chandef->center_freq2)
3042 		eht_oper_info->ccfs1 =
3043 			ieee80211_frequency_to_channel(chandef->center_freq2);
3044 	else
3045 		eht_oper_info->ccfs1 = 0;
3046 
3047 	switch (chandef->width) {
3048 	case NL80211_CHAN_WIDTH_320:
3049 		chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ;
3050 		eht_oper_info->ccfs1 = eht_oper_info->ccfs0;
3051 		if (chandef->chan->center_freq < chandef->center_freq1)
3052 			eht_oper_info->ccfs0 -= 16;
3053 		else
3054 			eht_oper_info->ccfs0 += 16;
3055 		break;
3056 	case NL80211_CHAN_WIDTH_160:
3057 		eht_oper_info->ccfs1 = eht_oper_info->ccfs0;
3058 		if (chandef->chan->center_freq < chandef->center_freq1)
3059 			eht_oper_info->ccfs0 -= 8;
3060 		else
3061 			eht_oper_info->ccfs0 += 8;
3062 		fallthrough;
3063 	case NL80211_CHAN_WIDTH_80P80:
3064 		chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ;
3065 		break;
3066 	case NL80211_CHAN_WIDTH_80:
3067 		chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ;
3068 		break;
3069 	case NL80211_CHAN_WIDTH_40:
3070 		chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ;
3071 		break;
3072 	default:
3073 		chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ;
3074 		break;
3075 	}
3076 	eht_oper_info->control = chan_width;
3077 	pos += eht_oper_info_len;
3078 
3079 	/* TODO: eht_oper_info->optional */
3080 
3081 	return pos;
3082 }
3083 
3084 bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
3085 			       struct cfg80211_chan_def *chandef)
3086 {
3087 	enum nl80211_channel_type channel_type;
3088 
3089 	if (!ht_oper)
3090 		return false;
3091 
3092 	switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3093 	case IEEE80211_HT_PARAM_CHA_SEC_NONE:
3094 		channel_type = NL80211_CHAN_HT20;
3095 		break;
3096 	case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3097 		channel_type = NL80211_CHAN_HT40PLUS;
3098 		break;
3099 	case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3100 		channel_type = NL80211_CHAN_HT40MINUS;
3101 		break;
3102 	default:
3103 		return false;
3104 	}
3105 
3106 	cfg80211_chandef_create(chandef, chandef->chan, channel_type);
3107 	return true;
3108 }
3109 
3110 bool ieee80211_chandef_vht_oper(struct ieee80211_hw *hw, u32 vht_cap_info,
3111 				const struct ieee80211_vht_operation *oper,
3112 				const struct ieee80211_ht_operation *htop,
3113 				struct cfg80211_chan_def *chandef)
3114 {
3115 	struct cfg80211_chan_def new = *chandef;
3116 	int cf0, cf1;
3117 	int ccfs0, ccfs1, ccfs2;
3118 	int ccf0, ccf1;
3119 	u32 vht_cap;
3120 	bool support_80_80 = false;
3121 	bool support_160 = false;
3122 	u8 ext_nss_bw_supp = u32_get_bits(vht_cap_info,
3123 					  IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
3124 	u8 supp_chwidth = u32_get_bits(vht_cap_info,
3125 				       IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
3126 
3127 	if (!oper || !htop)
3128 		return false;
3129 
3130 	vht_cap = hw->wiphy->bands[chandef->chan->band]->vht_cap.cap;
3131 	support_160 = (vht_cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK |
3132 				  IEEE80211_VHT_CAP_EXT_NSS_BW_MASK));
3133 	support_80_80 = ((vht_cap &
3134 			 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
3135 			(vht_cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
3136 			 vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
3137 			((vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) >>
3138 				    IEEE80211_VHT_CAP_EXT_NSS_BW_SHIFT > 1));
3139 	ccfs0 = oper->center_freq_seg0_idx;
3140 	ccfs1 = oper->center_freq_seg1_idx;
3141 	ccfs2 = (le16_to_cpu(htop->operation_mode) &
3142 				IEEE80211_HT_OP_MODE_CCFS2_MASK)
3143 			>> IEEE80211_HT_OP_MODE_CCFS2_SHIFT;
3144 
3145 	ccf0 = ccfs0;
3146 
3147 	/* if not supported, parse as though we didn't understand it */
3148 	if (!ieee80211_hw_check(hw, SUPPORTS_VHT_EXT_NSS_BW))
3149 		ext_nss_bw_supp = 0;
3150 
3151 	/*
3152 	 * Cf. IEEE 802.11 Table 9-250
3153 	 *
3154 	 * We really just consider that because it's inefficient to connect
3155 	 * at a higher bandwidth than we'll actually be able to use.
3156 	 */
3157 	switch ((supp_chwidth << 4) | ext_nss_bw_supp) {
3158 	default:
3159 	case 0x00:
3160 		ccf1 = 0;
3161 		support_160 = false;
3162 		support_80_80 = false;
3163 		break;
3164 	case 0x01:
3165 		support_80_80 = false;
3166 		fallthrough;
3167 	case 0x02:
3168 	case 0x03:
3169 		ccf1 = ccfs2;
3170 		break;
3171 	case 0x10:
3172 		ccf1 = ccfs1;
3173 		break;
3174 	case 0x11:
3175 	case 0x12:
3176 		if (!ccfs1)
3177 			ccf1 = ccfs2;
3178 		else
3179 			ccf1 = ccfs1;
3180 		break;
3181 	case 0x13:
3182 	case 0x20:
3183 	case 0x23:
3184 		ccf1 = ccfs1;
3185 		break;
3186 	}
3187 
3188 	cf0 = ieee80211_channel_to_frequency(ccf0, chandef->chan->band);
3189 	cf1 = ieee80211_channel_to_frequency(ccf1, chandef->chan->band);
3190 
3191 	switch (oper->chan_width) {
3192 	case IEEE80211_VHT_CHANWIDTH_USE_HT:
3193 		/* just use HT information directly */
3194 		break;
3195 	case IEEE80211_VHT_CHANWIDTH_80MHZ:
3196 		new.width = NL80211_CHAN_WIDTH_80;
3197 		new.center_freq1 = cf0;
3198 		/* If needed, adjust based on the newer interop workaround. */
3199 		if (ccf1) {
3200 			unsigned int diff;
3201 
3202 			diff = abs(ccf1 - ccf0);
3203 			if ((diff == 8) && support_160) {
3204 				new.width = NL80211_CHAN_WIDTH_160;
3205 				new.center_freq1 = cf1;
3206 			} else if ((diff > 8) && support_80_80) {
3207 				new.width = NL80211_CHAN_WIDTH_80P80;
3208 				new.center_freq2 = cf1;
3209 			}
3210 		}
3211 		break;
3212 	case IEEE80211_VHT_CHANWIDTH_160MHZ:
3213 		/* deprecated encoding */
3214 		new.width = NL80211_CHAN_WIDTH_160;
3215 		new.center_freq1 = cf0;
3216 		break;
3217 	case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
3218 		/* deprecated encoding */
3219 		new.width = NL80211_CHAN_WIDTH_80P80;
3220 		new.center_freq1 = cf0;
3221 		new.center_freq2 = cf1;
3222 		break;
3223 	default:
3224 		return false;
3225 	}
3226 
3227 	if (!cfg80211_chandef_valid(&new))
3228 		return false;
3229 
3230 	*chandef = new;
3231 	return true;
3232 }
3233 
3234 void ieee80211_chandef_eht_oper(const struct ieee80211_eht_operation_info *info,
3235 				struct cfg80211_chan_def *chandef)
3236 {
3237 	chandef->center_freq1 =
3238 		ieee80211_channel_to_frequency(info->ccfs0,
3239 					       chandef->chan->band);
3240 
3241 	switch (u8_get_bits(info->control,
3242 			    IEEE80211_EHT_OPER_CHAN_WIDTH)) {
3243 	case IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ:
3244 		chandef->width = NL80211_CHAN_WIDTH_20;
3245 		break;
3246 	case IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ:
3247 		chandef->width = NL80211_CHAN_WIDTH_40;
3248 		break;
3249 	case IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ:
3250 		chandef->width = NL80211_CHAN_WIDTH_80;
3251 		break;
3252 	case IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ:
3253 		chandef->width = NL80211_CHAN_WIDTH_160;
3254 		chandef->center_freq1 =
3255 			ieee80211_channel_to_frequency(info->ccfs1,
3256 						       chandef->chan->band);
3257 		break;
3258 	case IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ:
3259 		chandef->width = NL80211_CHAN_WIDTH_320;
3260 		chandef->center_freq1 =
3261 			ieee80211_channel_to_frequency(info->ccfs1,
3262 						       chandef->chan->band);
3263 		break;
3264 	}
3265 }
3266 
3267 bool ieee80211_chandef_he_6ghz_oper(struct ieee80211_local *local,
3268 				    const struct ieee80211_he_operation *he_oper,
3269 				    const struct ieee80211_eht_operation *eht_oper,
3270 				    struct cfg80211_chan_def *chandef)
3271 {
3272 	struct cfg80211_chan_def he_chandef = *chandef;
3273 	const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
3274 	u32 freq;
3275 
3276 	if (chandef->chan->band != NL80211_BAND_6GHZ)
3277 		return true;
3278 
3279 	if (!he_oper)
3280 		return false;
3281 
3282 	he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
3283 	if (!he_6ghz_oper)
3284 		return false;
3285 
3286 	/*
3287 	 * The EHT operation IE does not contain the primary channel so the
3288 	 * primary channel frequency should be taken from the 6 GHz operation
3289 	 * information.
3290 	 */
3291 	freq = ieee80211_channel_to_frequency(he_6ghz_oper->primary,
3292 					      NL80211_BAND_6GHZ);
3293 	he_chandef.chan = ieee80211_get_channel(local->hw.wiphy, freq);
3294 
3295 	if (!he_chandef.chan)
3296 		return false;
3297 
3298 	if (!eht_oper ||
3299 	    !(eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT)) {
3300 		switch (u8_get_bits(he_6ghz_oper->control,
3301 				    IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH)) {
3302 		case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ:
3303 			he_chandef.width = NL80211_CHAN_WIDTH_20;
3304 			break;
3305 		case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ:
3306 			he_chandef.width = NL80211_CHAN_WIDTH_40;
3307 			break;
3308 		case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ:
3309 			he_chandef.width = NL80211_CHAN_WIDTH_80;
3310 			break;
3311 		case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ:
3312 			he_chandef.width = NL80211_CHAN_WIDTH_80;
3313 			if (!he_6ghz_oper->ccfs1)
3314 				break;
3315 			if (abs(he_6ghz_oper->ccfs1 - he_6ghz_oper->ccfs0) == 8)
3316 				he_chandef.width = NL80211_CHAN_WIDTH_160;
3317 			else
3318 				he_chandef.width = NL80211_CHAN_WIDTH_80P80;
3319 			break;
3320 		}
3321 
3322 		if (he_chandef.width == NL80211_CHAN_WIDTH_160) {
3323 			he_chandef.center_freq1 =
3324 				ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
3325 							       NL80211_BAND_6GHZ);
3326 		} else {
3327 			he_chandef.center_freq1 =
3328 				ieee80211_channel_to_frequency(he_6ghz_oper->ccfs0,
3329 							       NL80211_BAND_6GHZ);
3330 			he_chandef.center_freq2 =
3331 				ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1,
3332 							       NL80211_BAND_6GHZ);
3333 		}
3334 	} else {
3335 		ieee80211_chandef_eht_oper((const void *)eht_oper->optional,
3336 					   &he_chandef);
3337 		he_chandef.punctured =
3338 			ieee80211_eht_oper_dis_subchan_bitmap(eht_oper);
3339 	}
3340 
3341 	if (!cfg80211_chandef_valid(&he_chandef))
3342 		return false;
3343 
3344 	*chandef = he_chandef;
3345 
3346 	return true;
3347 }
3348 
3349 bool ieee80211_chandef_s1g_oper(struct ieee80211_local *local,
3350 				const struct ieee80211_s1g_oper_ie *oper,
3351 				struct cfg80211_chan_def *chandef)
3352 {
3353 	u32 oper_khz, pri_1mhz_khz, pri_2mhz_khz;
3354 
3355 	if (!oper)
3356 		return false;
3357 
3358 	switch (FIELD_GET(S1G_OPER_CH_WIDTH_OPER, oper->ch_width)) {
3359 	case IEEE80211_S1G_CHANWIDTH_1MHZ:
3360 		chandef->width = NL80211_CHAN_WIDTH_1;
3361 		break;
3362 	case IEEE80211_S1G_CHANWIDTH_2MHZ:
3363 		chandef->width = NL80211_CHAN_WIDTH_2;
3364 		break;
3365 	case IEEE80211_S1G_CHANWIDTH_4MHZ:
3366 		chandef->width = NL80211_CHAN_WIDTH_4;
3367 		break;
3368 	case IEEE80211_S1G_CHANWIDTH_8MHZ:
3369 		chandef->width = NL80211_CHAN_WIDTH_8;
3370 		break;
3371 	case IEEE80211_S1G_CHANWIDTH_16MHZ:
3372 		chandef->width = NL80211_CHAN_WIDTH_16;
3373 		break;
3374 	default:
3375 		return false;
3376 	}
3377 
3378 	chandef->s1g_primary_2mhz = false;
3379 
3380 	switch (u8_get_bits(oper->ch_width, S1G_OPER_CH_WIDTH_PRIMARY)) {
3381 	case IEEE80211_S1G_PRI_CHANWIDTH_1MHZ:
3382 		pri_1mhz_khz = ieee80211_channel_to_freq_khz(
3383 			oper->primary_ch, NL80211_BAND_S1GHZ);
3384 		break;
3385 	case IEEE80211_S1G_PRI_CHANWIDTH_2MHZ:
3386 		chandef->s1g_primary_2mhz = true;
3387 		pri_2mhz_khz = ieee80211_channel_to_freq_khz(
3388 			oper->primary_ch, NL80211_BAND_S1GHZ);
3389 
3390 		if (u8_get_bits(oper->ch_width, S1G_OPER_CH_PRIMARY_LOCATION) ==
3391 		    S1G_2M_PRIMARY_LOCATION_LOWER)
3392 			pri_1mhz_khz = pri_2mhz_khz - 500;
3393 		else
3394 			pri_1mhz_khz = pri_2mhz_khz + 500;
3395 		break;
3396 	default:
3397 		return false;
3398 	}
3399 
3400 	oper_khz = ieee80211_channel_to_freq_khz(oper->oper_ch,
3401 						 NL80211_BAND_S1GHZ);
3402 	chandef->center_freq1 = KHZ_TO_MHZ(oper_khz);
3403 	chandef->freq1_offset = oper_khz % 1000;
3404 	chandef->chan =
3405 		ieee80211_get_channel_khz(local->hw.wiphy, pri_1mhz_khz);
3406 
3407 	return chandef->chan;
3408 }
3409 
3410 int ieee80211_put_srates_elem(struct sk_buff *skb,
3411 			      const struct ieee80211_supported_band *sband,
3412 			      u32 basic_rates, u32 masked_rates,
3413 			      u8 element_id)
3414 {
3415 	u8 i, rates, skip;
3416 
3417 	rates = 0;
3418 	for (i = 0; i < sband->n_bitrates; i++) {
3419 		if (masked_rates & BIT(i))
3420 			continue;
3421 		rates++;
3422 	}
3423 
3424 	if (element_id == WLAN_EID_SUPP_RATES) {
3425 		rates = min_t(u8, rates, 8);
3426 		skip = 0;
3427 	} else {
3428 		skip = 8;
3429 		if (rates <= skip)
3430 			return 0;
3431 		rates -= skip;
3432 	}
3433 
3434 	if (skb_tailroom(skb) < rates + 2)
3435 		return -ENOBUFS;
3436 
3437 	skb_put_u8(skb, element_id);
3438 	skb_put_u8(skb, rates);
3439 
3440 	for (i = 0; i < sband->n_bitrates && rates; i++) {
3441 		int rate;
3442 		u8 basic;
3443 
3444 		if (masked_rates & BIT(i))
3445 			continue;
3446 
3447 		if (skip > 0) {
3448 			skip--;
3449 			continue;
3450 		}
3451 
3452 		basic = basic_rates & BIT(i) ? 0x80 : 0;
3453 
3454 		rate = DIV_ROUND_UP(sband->bitrates[i].bitrate, 5);
3455 		skb_put_u8(skb, basic | (u8)rate);
3456 		rates--;
3457 	}
3458 
3459 	WARN(rates > 0, "rates confused: rates:%d, element:%d\n",
3460 	     rates, element_id);
3461 
3462 	return 0;
3463 }
3464 
3465 int ieee80211_ave_rssi(struct ieee80211_vif *vif, int link_id)
3466 {
3467 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3468 	struct ieee80211_link_data *link_data;
3469 
3470 	if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION))
3471 		return 0;
3472 
3473 	if (link_id < 0)
3474 		link_data = &sdata->deflink;
3475 	else
3476 		link_data = wiphy_dereference(sdata->local->hw.wiphy,
3477 					      sdata->link[link_id]);
3478 
3479 	if (WARN_ON_ONCE(!link_data))
3480 		return -99;
3481 
3482 	return -ewma_beacon_signal_read(&link_data->u.mgd.ave_beacon_signal);
3483 }
3484 EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
3485 
3486 u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
3487 {
3488 	if (!mcs)
3489 		return 1;
3490 
3491 	/* TODO: consider rx_highest */
3492 
3493 	if (mcs->rx_mask[3])
3494 		return 4;
3495 	if (mcs->rx_mask[2])
3496 		return 3;
3497 	if (mcs->rx_mask[1])
3498 		return 2;
3499 	return 1;
3500 }
3501 
3502 u64 ieee80211_calculate_rx_timestamp(struct ieee80211_hw *hw,
3503 				     struct ieee80211_rx_status *status,
3504 				     unsigned int mpdu_len,
3505 				     unsigned int mpdu_offset)
3506 {
3507 	u64 ts = status->mactime;
3508 	bool mactime_plcp_start;
3509 	struct rate_info ri;
3510 	u16 rate;
3511 	u8 n_ltf;
3512 
3513 	if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
3514 		return 0;
3515 
3516 	mactime_plcp_start = (status->flag & RX_FLAG_MACTIME) ==
3517 				RX_FLAG_MACTIME_PLCP_START;
3518 
3519 	memset(&ri, 0, sizeof(ri));
3520 
3521 	ri.bw = status->bw;
3522 
3523 	/* Fill cfg80211 rate info */
3524 	switch (status->encoding) {
3525 	case RX_ENC_EHT:
3526 		ri.flags |= RATE_INFO_FLAGS_EHT_MCS;
3527 		ri.mcs = status->rate_idx;
3528 		ri.nss = status->nss;
3529 		ri.eht_ru_alloc = status->eht.ru;
3530 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
3531 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
3532 		/* TODO/FIXME: is this right? handle other PPDUs */
3533 		if (mactime_plcp_start) {
3534 			mpdu_offset += 2;
3535 			ts += 36;
3536 		}
3537 		break;
3538 	case RX_ENC_HE:
3539 		ri.flags |= RATE_INFO_FLAGS_HE_MCS;
3540 		ri.mcs = status->rate_idx;
3541 		ri.nss = status->nss;
3542 		ri.he_ru_alloc = status->he_ru;
3543 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
3544 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
3545 
3546 		/*
3547 		 * See P802.11ax_D6.0, section 27.3.4 for
3548 		 * VHT PPDU format.
3549 		 */
3550 		if (mactime_plcp_start) {
3551 			mpdu_offset += 2;
3552 			ts += 36;
3553 
3554 			/*
3555 			 * TODO:
3556 			 * For HE MU PPDU, add the HE-SIG-B.
3557 			 * For HE ER PPDU, add 8us for the HE-SIG-A.
3558 			 * For HE TB PPDU, add 4us for the HE-STF.
3559 			 * Add the HE-LTF durations - variable.
3560 			 */
3561 		}
3562 
3563 		break;
3564 	case RX_ENC_HT:
3565 		ri.mcs = status->rate_idx;
3566 		ri.flags |= RATE_INFO_FLAGS_MCS;
3567 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
3568 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
3569 
3570 		/*
3571 		 * See P802.11REVmd_D3.0, section 19.3.2 for
3572 		 * HT PPDU format.
3573 		 */
3574 		if (mactime_plcp_start) {
3575 			mpdu_offset += 2;
3576 			if (status->enc_flags & RX_ENC_FLAG_HT_GF)
3577 				ts += 24;
3578 			else
3579 				ts += 32;
3580 
3581 			/*
3582 			 * Add Data HT-LTFs per streams
3583 			 * TODO: add Extension HT-LTFs, 4us per LTF
3584 			 */
3585 			n_ltf = ((ri.mcs >> 3) & 3) + 1;
3586 			n_ltf = n_ltf == 3 ? 4 : n_ltf;
3587 			ts += n_ltf * 4;
3588 		}
3589 
3590 		break;
3591 	case RX_ENC_VHT:
3592 		ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
3593 		ri.mcs = status->rate_idx;
3594 		ri.nss = status->nss;
3595 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
3596 			ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
3597 
3598 		/*
3599 		 * See P802.11REVmd_D3.0, section 21.3.2 for
3600 		 * VHT PPDU format.
3601 		 */
3602 		if (mactime_plcp_start) {
3603 			mpdu_offset += 2;
3604 			ts += 36;
3605 
3606 			/*
3607 			 * Add VHT-LTFs per streams
3608 			 */
3609 			n_ltf = (ri.nss != 1) && (ri.nss % 2) ?
3610 				ri.nss + 1 : ri.nss;
3611 			ts += 4 * n_ltf;
3612 		}
3613 
3614 		break;
3615 	default:
3616 		WARN_ON(1);
3617 		fallthrough;
3618 	case RX_ENC_LEGACY: {
3619 		struct ieee80211_supported_band *sband;
3620 
3621 		sband = hw->wiphy->bands[status->band];
3622 		ri.legacy = sband->bitrates[status->rate_idx].bitrate;
3623 
3624 		if (mactime_plcp_start) {
3625 			if (status->band == NL80211_BAND_5GHZ) {
3626 				ts += 20;
3627 				mpdu_offset += 2;
3628 			} else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
3629 				ts += 96;
3630 			} else {
3631 				ts += 192;
3632 			}
3633 		}
3634 		break;
3635 		}
3636 	}
3637 
3638 	rate = cfg80211_calculate_bitrate(&ri);
3639 	if (WARN_ONCE(!rate,
3640 		      "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
3641 		      (unsigned long long)status->flag, status->rate_idx,
3642 		      status->nss))
3643 		return 0;
3644 
3645 	/* rewind from end of MPDU */
3646 	if ((status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
3647 		ts -= mpdu_len * 8 * 10 / rate;
3648 
3649 	ts += mpdu_offset * 8 * 10 / rate;
3650 
3651 	return ts;
3652 }
3653 EXPORT_SYMBOL_GPL(ieee80211_calculate_rx_timestamp);
3654 
3655 /* Cancel CAC for the interfaces under the specified @local. If @ctx is
3656  * also provided, only the interfaces using that ctx will be canceled.
3657  */
3658 void ieee80211_dfs_cac_cancel(struct ieee80211_local *local,
3659 			      struct ieee80211_chanctx *ctx)
3660 {
3661 	struct ieee80211_sub_if_data *sdata;
3662 	struct cfg80211_chan_def chandef;
3663 	struct ieee80211_link_data *link;
3664 	struct ieee80211_chanctx_conf *chanctx_conf;
3665 	unsigned int link_id;
3666 
3667 	lockdep_assert_wiphy(local->hw.wiphy);
3668 
3669 	list_for_each_entry(sdata, &local->interfaces, list) {
3670 		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS;
3671 		     link_id++) {
3672 			link = sdata_dereference(sdata->link[link_id],
3673 						 sdata);
3674 			if (!link)
3675 				continue;
3676 
3677 			chanctx_conf = sdata_dereference(link->conf->chanctx_conf,
3678 							 sdata);
3679 			if (ctx && &ctx->conf != chanctx_conf)
3680 				continue;
3681 
3682 			wiphy_hrtimer_work_cancel(local->hw.wiphy,
3683 						  &link->dfs_cac_timer_work);
3684 
3685 			if (!sdata->wdev.links[link_id].cac_started)
3686 				continue;
3687 
3688 			chandef = link->conf->chanreq.oper;
3689 			ieee80211_link_release_channel(link);
3690 			cfg80211_cac_event(sdata->dev, &chandef,
3691 					   NL80211_RADAR_CAC_ABORTED,
3692 					   GFP_KERNEL, link_id);
3693 		}
3694 	}
3695 }
3696 
3697 void ieee80211_dfs_radar_detected_work(struct wiphy *wiphy,
3698 				       struct wiphy_work *work)
3699 {
3700 	struct ieee80211_local *local =
3701 		container_of(work, struct ieee80211_local, radar_detected_work);
3702 	struct cfg80211_chan_def chandef;
3703 	struct ieee80211_chanctx *ctx;
3704 
3705 	lockdep_assert_wiphy(local->hw.wiphy);
3706 
3707 	list_for_each_entry(ctx, &local->chanctx_list, list) {
3708 		if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
3709 			continue;
3710 
3711 		if (!ctx->radar_detected)
3712 			continue;
3713 
3714 		ctx->radar_detected = false;
3715 
3716 		chandef = ctx->conf.def;
3717 
3718 		ieee80211_dfs_cac_cancel(local, ctx);
3719 		cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
3720 	}
3721 }
3722 
3723 static void
3724 ieee80211_radar_mark_chan_ctx_iterator(struct ieee80211_hw *hw,
3725 				       struct ieee80211_chanctx_conf *chanctx_conf,
3726 				       void *data)
3727 {
3728 	struct ieee80211_chanctx *ctx =
3729 		container_of(chanctx_conf, struct ieee80211_chanctx,
3730 			     conf);
3731 
3732 	if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
3733 		return;
3734 
3735 	if (data && data != chanctx_conf)
3736 		return;
3737 
3738 	ctx->radar_detected = true;
3739 }
3740 
3741 void ieee80211_radar_detected(struct ieee80211_hw *hw,
3742 			      struct ieee80211_chanctx_conf *chanctx_conf)
3743 {
3744 	struct ieee80211_local *local = hw_to_local(hw);
3745 
3746 	trace_api_radar_detected(local);
3747 
3748 	ieee80211_iter_chan_contexts_atomic(hw, ieee80211_radar_mark_chan_ctx_iterator,
3749 					    chanctx_conf);
3750 
3751 	wiphy_work_queue(hw->wiphy, &local->radar_detected_work);
3752 }
3753 EXPORT_SYMBOL(ieee80211_radar_detected);
3754 
3755 void ieee80211_chandef_downgrade(struct cfg80211_chan_def *c,
3756 				 struct ieee80211_conn_settings *conn)
3757 {
3758 	enum nl80211_chan_width new_primary_width;
3759 	struct ieee80211_conn_settings _ignored = {};
3760 
3761 	/* allow passing NULL if caller doesn't care */
3762 	if (!conn)
3763 		conn = &_ignored;
3764 
3765 again:
3766 	/* no-HT indicates nothing to do */
3767 	new_primary_width = NL80211_CHAN_WIDTH_20_NOHT;
3768 
3769 	switch (c->width) {
3770 	default:
3771 	case NL80211_CHAN_WIDTH_20_NOHT:
3772 		WARN_ON_ONCE(1);
3773 		fallthrough;
3774 	case NL80211_CHAN_WIDTH_20:
3775 		c->width = NL80211_CHAN_WIDTH_20_NOHT;
3776 		conn->mode = IEEE80211_CONN_MODE_LEGACY;
3777 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
3778 		c->punctured = 0;
3779 		break;
3780 	case NL80211_CHAN_WIDTH_40:
3781 		c->width = NL80211_CHAN_WIDTH_20;
3782 		c->center_freq1 = c->chan->center_freq;
3783 		if (conn->mode == IEEE80211_CONN_MODE_VHT)
3784 			conn->mode = IEEE80211_CONN_MODE_HT;
3785 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
3786 		c->punctured = 0;
3787 		break;
3788 	case NL80211_CHAN_WIDTH_80:
3789 		new_primary_width = NL80211_CHAN_WIDTH_40;
3790 		if (conn->mode == IEEE80211_CONN_MODE_VHT)
3791 			conn->mode = IEEE80211_CONN_MODE_HT;
3792 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_40;
3793 		break;
3794 	case NL80211_CHAN_WIDTH_80P80:
3795 		c->center_freq2 = 0;
3796 		c->width = NL80211_CHAN_WIDTH_80;
3797 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_80;
3798 		break;
3799 	case NL80211_CHAN_WIDTH_160:
3800 		new_primary_width = NL80211_CHAN_WIDTH_80;
3801 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_80;
3802 		break;
3803 	case NL80211_CHAN_WIDTH_320:
3804 		new_primary_width = NL80211_CHAN_WIDTH_160;
3805 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_160;
3806 		break;
3807 	case NL80211_CHAN_WIDTH_1:
3808 	case NL80211_CHAN_WIDTH_2:
3809 	case NL80211_CHAN_WIDTH_4:
3810 	case NL80211_CHAN_WIDTH_8:
3811 	case NL80211_CHAN_WIDTH_16:
3812 		WARN_ON_ONCE(1);
3813 		/* keep c->width */
3814 		conn->mode = IEEE80211_CONN_MODE_S1G;
3815 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
3816 		break;
3817 	case NL80211_CHAN_WIDTH_5:
3818 	case NL80211_CHAN_WIDTH_10:
3819 		WARN_ON_ONCE(1);
3820 		/* keep c->width */
3821 		conn->mode = IEEE80211_CONN_MODE_LEGACY;
3822 		conn->bw_limit = IEEE80211_CONN_BW_LIMIT_20;
3823 		break;
3824 	}
3825 
3826 	if (new_primary_width != NL80211_CHAN_WIDTH_20_NOHT) {
3827 		c->center_freq1 = cfg80211_chandef_primary(c, new_primary_width,
3828 							   &c->punctured);
3829 		c->width = new_primary_width;
3830 	}
3831 
3832 	/*
3833 	 * With an 80 MHz channel, we might have the puncturing in the primary
3834 	 * 40 Mhz channel, but that's not valid when downgraded to 40 MHz width.
3835 	 * In that case, downgrade again.
3836 	 */
3837 	if (!cfg80211_chandef_valid(c) && c->punctured)
3838 		goto again;
3839 
3840 	WARN_ON_ONCE(!cfg80211_chandef_valid(c));
3841 }
3842 
3843 int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
3844 			      struct cfg80211_csa_settings *csa_settings)
3845 {
3846 	int hdr_len = IEEE80211_MIN_ACTION_SIZE(chan_switch);
3847 	struct sk_buff *skb;
3848 	struct ieee80211_mgmt *mgmt;
3849 	struct ieee80211_local *local = sdata->local;
3850 	int freq;
3851 	u8 *pos;
3852 
3853 	if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3854 	    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3855 		return -EOPNOTSUPP;
3856 
3857 	skb = dev_alloc_skb(local->tx_headroom + hdr_len +
3858 			    5 + /* channel switch announcement element */
3859 			    3 + /* secondary channel offset element */
3860 			    5 + /* wide bandwidth channel switch announcement */
3861 			    8); /* mesh channel switch parameters element */
3862 	if (!skb)
3863 		return -ENOMEM;
3864 
3865 	skb_reserve(skb, local->tx_headroom);
3866 	mgmt = skb_put_zero(skb, hdr_len);
3867 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3868 					  IEEE80211_STYPE_ACTION);
3869 
3870 	eth_broadcast_addr(mgmt->da);
3871 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
3872 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
3873 		memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
3874 	} else {
3875 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
3876 		memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
3877 	}
3878 	mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
3879 	mgmt->u.action.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
3880 	pos = skb_put(skb, 5);
3881 	*pos++ = WLAN_EID_CHANNEL_SWITCH;			/* EID */
3882 	*pos++ = 3;						/* IE length */
3883 	*pos++ = csa_settings->block_tx ? 1 : 0;		/* CSA mode */
3884 	freq = csa_settings->chandef.chan->center_freq;
3885 	*pos++ = ieee80211_frequency_to_channel(freq);		/* channel */
3886 	*pos++ = csa_settings->count;				/* count */
3887 
3888 	if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
3889 		enum nl80211_channel_type ch_type;
3890 
3891 		skb_put(skb, 3);
3892 		*pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;	/* EID */
3893 		*pos++ = 1;					/* IE length */
3894 		ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
3895 		if (ch_type == NL80211_CHAN_HT40PLUS)
3896 			*pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
3897 		else
3898 			*pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
3899 	}
3900 
3901 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
3902 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
3903 
3904 		skb_put(skb, 8);
3905 		*pos++ = WLAN_EID_CHAN_SWITCH_PARAM;		/* EID */
3906 		*pos++ = 6;					/* IE length */
3907 		*pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL;	/* Mesh TTL */
3908 		*pos = 0x00;	/* Mesh Flag: Tx Restrict, Initiator, Reason */
3909 		*pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
3910 		*pos++ |= csa_settings->block_tx ?
3911 			  WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
3912 		put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
3913 		pos += 2;
3914 		put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
3915 		pos += 2;
3916 	}
3917 
3918 	if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 ||
3919 	    csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 ||
3920 	    csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) {
3921 		skb_put(skb, 5);
3922 		ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef);
3923 	}
3924 
3925 	ieee80211_tx_skb(sdata, skb);
3926 	return 0;
3927 }
3928 
3929 static bool
3930 ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
3931 {
3932 	s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
3933 	int skip;
3934 
3935 	if (end > 0)
3936 		return false;
3937 
3938 	/* One shot NOA  */
3939 	if (data->count[i] == 1)
3940 		return false;
3941 
3942 	if (data->desc[i].interval == 0)
3943 		return false;
3944 
3945 	/* End time is in the past, check for repetitions */
3946 	skip = DIV_ROUND_UP(-end, data->desc[i].interval);
3947 	if (data->count[i] < 255) {
3948 		if (data->count[i] <= skip) {
3949 			data->count[i] = 0;
3950 			return false;
3951 		}
3952 
3953 		data->count[i] -= skip;
3954 	}
3955 
3956 	data->desc[i].start += skip * data->desc[i].interval;
3957 
3958 	return true;
3959 }
3960 
3961 static bool
3962 ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
3963 			     s32 *offset)
3964 {
3965 	bool ret = false;
3966 	int i;
3967 
3968 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3969 		s32 cur;
3970 
3971 		if (!data->count[i])
3972 			continue;
3973 
3974 		if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
3975 			ret = true;
3976 
3977 		cur = data->desc[i].start - tsf;
3978 		if (cur > *offset)
3979 			continue;
3980 
3981 		cur = data->desc[i].start + data->desc[i].duration - tsf;
3982 		if (cur > *offset)
3983 			*offset = cur;
3984 	}
3985 
3986 	return ret;
3987 }
3988 
3989 static u32
3990 ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
3991 {
3992 	s32 offset = 0;
3993 	int tries = 0;
3994 	/*
3995 	 * arbitrary limit, used to avoid infinite loops when combined NoA
3996 	 * descriptors cover the full time period.
3997 	 */
3998 	int max_tries = 5;
3999 
4000 	ieee80211_extend_absent_time(data, tsf, &offset);
4001 	do {
4002 		if (!ieee80211_extend_absent_time(data, tsf, &offset))
4003 			break;
4004 
4005 		tries++;
4006 	} while (tries < max_tries);
4007 
4008 	return offset;
4009 }
4010 
4011 void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
4012 {
4013 	u32 next_offset = BIT(31) - 1;
4014 	int i;
4015 
4016 	data->absent = 0;
4017 	data->has_next_tsf = false;
4018 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4019 		s32 start;
4020 
4021 		if (!data->count[i])
4022 			continue;
4023 
4024 		ieee80211_extend_noa_desc(data, tsf, i);
4025 		start = data->desc[i].start - tsf;
4026 		if (start <= 0)
4027 			data->absent |= BIT(i);
4028 
4029 		if (next_offset > start)
4030 			next_offset = start;
4031 
4032 		data->has_next_tsf = true;
4033 	}
4034 
4035 	if (data->absent)
4036 		next_offset = ieee80211_get_noa_absent_time(data, tsf);
4037 
4038 	data->next_tsf = tsf + next_offset;
4039 }
4040 EXPORT_SYMBOL(ieee80211_update_p2p_noa);
4041 
4042 int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
4043 			    struct ieee80211_noa_data *data, u32 tsf)
4044 {
4045 	int ret = 0;
4046 	int i;
4047 
4048 	memset(data, 0, sizeof(*data));
4049 
4050 	for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
4051 		const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
4052 
4053 		if (!desc->count || !desc->duration)
4054 			continue;
4055 
4056 		data->count[i] = desc->count;
4057 		data->desc[i].start = le32_to_cpu(desc->start_time);
4058 		data->desc[i].duration = le32_to_cpu(desc->duration);
4059 		data->desc[i].interval = le32_to_cpu(desc->interval);
4060 
4061 		if (data->count[i] > 1 &&
4062 		    data->desc[i].interval < data->desc[i].duration)
4063 			continue;
4064 
4065 		ieee80211_extend_noa_desc(data, tsf, i);
4066 		ret++;
4067 	}
4068 
4069 	if (ret)
4070 		ieee80211_update_p2p_noa(data, tsf);
4071 
4072 	return ret;
4073 }
4074 EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
4075 
4076 void ieee80211_recalc_dtim(struct ieee80211_sub_if_data *sdata, u64 tsf)
4077 {
4078 	u64 dtim_count = 0;
4079 	u32 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
4080 	u8 dtim_period = sdata->vif.bss_conf.dtim_period;
4081 	struct ps_data *ps;
4082 	u8 bcns_from_dtim;
4083 
4084 	if (tsf == -1ULL || !beacon_int || !dtim_period)
4085 		return;
4086 
4087 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
4088 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
4089 		if (!sdata->bss)
4090 			return;
4091 
4092 		ps = &sdata->bss->ps;
4093 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4094 		ps = &sdata->u.mesh.ps;
4095 	} else {
4096 		return;
4097 	}
4098 
4099 	/*
4100 	 * actually finds last dtim_count, mac80211 will update in
4101 	 * __beacon_add_tim().
4102 	 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
4103 	 */
4104 	do_div(tsf, beacon_int);
4105 	bcns_from_dtim = do_div(tsf, dtim_period);
4106 	/* just had a DTIM */
4107 	if (!bcns_from_dtim)
4108 		dtim_count = 0;
4109 	else
4110 		dtim_count = dtim_period - bcns_from_dtim;
4111 
4112 	ps->dtim_count = dtim_count;
4113 }
4114 
4115 /*
4116  * Given a long beacon period, calculate the current index into
4117  * that period to determine the number of TSBTTs until the next TBTT.
4118  * It is completely valid to have a short beacon period that differs
4119  * from the dtim period (i.e a TBTT thats not a DTIM).
4120  */
4121 void ieee80211_recalc_sb_count(struct ieee80211_sub_if_data *sdata, u64 tsf)
4122 {
4123 	u32 sb_idx;
4124 	struct ps_data *ps = &sdata->bss->ps;
4125 	u8 lb_period = sdata->vif.bss_conf.s1g_long_beacon_period;
4126 	u32 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
4127 
4128 	/* No mesh / IBSS support for short beaconing */
4129 	if (tsf == -1ULL || !lb_period ||
4130 	    (sdata->vif.type != NL80211_IFTYPE_AP &&
4131 	     sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
4132 		return;
4133 
4134 	/* find the current TSBTT index in our lb_period */
4135 	do_div(tsf, beacon_int);
4136 	sb_idx = do_div(tsf, lb_period);
4137 
4138 	/* num TSBTTs until the next TBTT */
4139 	ps->sb_count = sb_idx ? lb_period - sb_idx : 0;
4140 }
4141 
4142 static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
4143 					 struct ieee80211_chanctx *ctx)
4144 {
4145 	struct ieee80211_link_data *link;
4146 	u8 radar_detect = 0;
4147 
4148 	lockdep_assert_wiphy(local->hw.wiphy);
4149 
4150 	if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
4151 		return 0;
4152 
4153 	for_each_sdata_link(local, link) {
4154 		if (rcu_access_pointer(link->conf->chanctx_conf) == &ctx->conf) {
4155 			/*
4156 			 * An in-place reservation context should not have any
4157 			 * assigned links until it replaces the other context.
4158 			 */
4159 			WARN_ON(ctx->replace_state ==
4160 				IEEE80211_CHANCTX_REPLACES_OTHER);
4161 
4162 			if (link->radar_required)
4163 				radar_detect |=
4164 					BIT(link->conf->chanreq.oper.width);
4165 		}
4166 
4167 		if (link->reserved_chanctx == ctx &&
4168 		    link->reserved_radar_required)
4169 			radar_detect |= BIT(link->reserved.oper.width);
4170 	}
4171 
4172 	return radar_detect;
4173 }
4174 
4175 bool ieee80211_is_radio_idx_in_scan_req(struct wiphy *wiphy,
4176 					struct cfg80211_scan_request *scan_req,
4177 					int radio_idx)
4178 {
4179 	struct ieee80211_channel *chan;
4180 	int i, chan_radio_idx;
4181 
4182 	for (i = 0; i < scan_req->n_channels; i++) {
4183 		chan = scan_req->channels[i];
4184 		chan_radio_idx = cfg80211_get_radio_idx_by_chan(wiphy, chan);
4185 
4186 		/* The radio index either matched successfully, or an error
4187 		 * occurred. For example, if radio-level information is
4188 		 * missing, the same error value is returned. This
4189 		 * typically implies a single-radio setup, in which case
4190 		 * the operation should not be allowed.
4191 		 */
4192 		if (chan_radio_idx == radio_idx)
4193 			return true;
4194 	}
4195 
4196 	return false;
4197 }
4198 
4199 static u32
4200 __ieee80211_get_radio_mask(struct ieee80211_sub_if_data *sdata)
4201 {
4202 	struct ieee80211_bss_conf *link_conf;
4203 	struct ieee80211_chanctx_conf *conf;
4204 	unsigned int link_id;
4205 	u32 mask = 0;
4206 
4207 	for_each_vif_active_link(&sdata->vif, link_conf, link_id) {
4208 		conf = sdata_dereference(link_conf->chanctx_conf, sdata);
4209 		if (!conf || conf->radio_idx < 0)
4210 			continue;
4211 
4212 		mask |= BIT(conf->radio_idx);
4213 	}
4214 
4215 	return mask;
4216 }
4217 
4218 u32 ieee80211_get_radio_mask(struct wiphy *wiphy, struct net_device *dev)
4219 {
4220 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4221 
4222 	return __ieee80211_get_radio_mask(sdata);
4223 }
4224 
4225 static bool
4226 ieee80211_sdata_uses_radio(struct ieee80211_sub_if_data *sdata, int radio_idx)
4227 {
4228 	if (radio_idx < 0)
4229 		return true;
4230 
4231 	return __ieee80211_get_radio_mask(sdata) & BIT(radio_idx);
4232 }
4233 
4234 static int
4235 ieee80211_fill_ifcomb_params(struct ieee80211_local *local,
4236 			     struct iface_combination_params *params,
4237 			     const struct cfg80211_chan_def *chandef,
4238 			     struct ieee80211_sub_if_data *sdata)
4239 {
4240 	struct ieee80211_sub_if_data *sdata_iter;
4241 	struct ieee80211_chanctx *ctx;
4242 	int total = !!sdata;
4243 
4244 	list_for_each_entry(ctx, &local->chanctx_list, list) {
4245 		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
4246 			continue;
4247 
4248 		if (params->radio_idx >= 0 &&
4249 		    ctx->conf.radio_idx != params->radio_idx)
4250 			continue;
4251 
4252 		params->radar_detect |=
4253 			ieee80211_chanctx_radar_detect(local, ctx);
4254 
4255 		if (chandef && ctx->mode != IEEE80211_CHANCTX_EXCLUSIVE &&
4256 		    cfg80211_chandef_compatible(chandef, &ctx->conf.def))
4257 			continue;
4258 
4259 		params->num_different_channels++;
4260 	}
4261 
4262 	list_for_each_entry(sdata_iter, &local->interfaces, list) {
4263 		struct wireless_dev *wdev_iter;
4264 
4265 		wdev_iter = &sdata_iter->wdev;
4266 
4267 		if (sdata_iter == sdata ||
4268 		    !ieee80211_sdata_running(sdata_iter) ||
4269 		    cfg80211_iftype_allowed(local->hw.wiphy,
4270 					    wdev_iter->iftype, 0, 1))
4271 			continue;
4272 
4273 		if (!ieee80211_sdata_uses_radio(sdata_iter, params->radio_idx))
4274 			continue;
4275 
4276 		params->iftype_num[wdev_iter->iftype]++;
4277 		total++;
4278 	}
4279 
4280 	return total;
4281 }
4282 
4283 int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
4284 				 const struct cfg80211_chan_def *chandef,
4285 				 enum ieee80211_chanctx_mode chanmode,
4286 				 u8 radar_detect, int radio_idx)
4287 {
4288 	bool shared = chanmode == IEEE80211_CHANCTX_SHARED;
4289 	struct ieee80211_local *local = sdata->local;
4290 	enum nl80211_iftype iftype = sdata->wdev.iftype;
4291 	struct iface_combination_params params = {
4292 		.radar_detect = radar_detect,
4293 		.radio_idx = radio_idx,
4294 	};
4295 	int total;
4296 
4297 	lockdep_assert_wiphy(local->hw.wiphy);
4298 
4299 	if (WARN_ON(hweight32(radar_detect) > 1))
4300 		return -EINVAL;
4301 
4302 	if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
4303 		    !chandef->chan))
4304 		return -EINVAL;
4305 
4306 	if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
4307 		return -EINVAL;
4308 
4309 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
4310 	    sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
4311 		/*
4312 		 * always passing this is harmless, since it'll be the
4313 		 * same value that cfg80211 finds if it finds the same
4314 		 * interface ... and that's always allowed
4315 		 */
4316 		params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
4317 	}
4318 
4319 	/* Always allow software iftypes */
4320 	if (cfg80211_iftype_allowed(local->hw.wiphy, iftype, 0, 1)) {
4321 		if (radar_detect)
4322 			return -EINVAL;
4323 		return 0;
4324 	}
4325 
4326 	if (chandef)
4327 		params.num_different_channels = 1;
4328 
4329 	if (iftype != NL80211_IFTYPE_UNSPECIFIED)
4330 		params.iftype_num[iftype] = 1;
4331 
4332 	total = ieee80211_fill_ifcomb_params(local, &params,
4333 					     shared ? chandef : NULL,
4334 					     sdata);
4335 	if (total == 1 && !params.radar_detect)
4336 		return 0;
4337 
4338 	return cfg80211_check_combinations(local->hw.wiphy, &params);
4339 }
4340 
4341 static void
4342 ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
4343 			 void *data)
4344 {
4345 	u32 *max_num_different_channels = data;
4346 
4347 	*max_num_different_channels = max(*max_num_different_channels,
4348 					  c->num_different_channels);
4349 }
4350 
4351 int ieee80211_max_num_channels(struct ieee80211_local *local, int radio_idx)
4352 {
4353 	u32 max_num_different_channels = 1;
4354 	int err;
4355 	struct iface_combination_params params = {
4356 		.radio_idx = radio_idx,
4357 	};
4358 
4359 	lockdep_assert_wiphy(local->hw.wiphy);
4360 
4361 	ieee80211_fill_ifcomb_params(local, &params, NULL, NULL);
4362 
4363 	err = cfg80211_iter_combinations(local->hw.wiphy, &params,
4364 					 ieee80211_iter_max_chans,
4365 					 &max_num_different_channels);
4366 	if (err < 0)
4367 		return err;
4368 
4369 	return max_num_different_channels;
4370 }
4371 
4372 void ieee80211_add_s1g_capab_ie(struct ieee80211_sub_if_data *sdata,
4373 				struct ieee80211_sta_s1g_cap *caps,
4374 				struct sk_buff *skb)
4375 {
4376 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4377 	struct ieee80211_s1g_cap s1g_capab;
4378 	u8 *pos;
4379 	int i;
4380 
4381 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
4382 		return;
4383 
4384 	if (!caps->s1g)
4385 		return;
4386 
4387 	memcpy(s1g_capab.capab_info, caps->cap, sizeof(caps->cap));
4388 	memcpy(s1g_capab.supp_mcs_nss, caps->nss_mcs, sizeof(caps->nss_mcs));
4389 
4390 	/* override the capability info */
4391 	for (i = 0; i < sizeof(ifmgd->s1g_capa.capab_info); i++) {
4392 		u8 mask = ifmgd->s1g_capa_mask.capab_info[i];
4393 
4394 		s1g_capab.capab_info[i] &= ~mask;
4395 		s1g_capab.capab_info[i] |= ifmgd->s1g_capa.capab_info[i] & mask;
4396 	}
4397 
4398 	/* then MCS and NSS set */
4399 	for (i = 0; i < sizeof(ifmgd->s1g_capa.supp_mcs_nss); i++) {
4400 		u8 mask = ifmgd->s1g_capa_mask.supp_mcs_nss[i];
4401 
4402 		s1g_capab.supp_mcs_nss[i] &= ~mask;
4403 		s1g_capab.supp_mcs_nss[i] |=
4404 			ifmgd->s1g_capa.supp_mcs_nss[i] & mask;
4405 	}
4406 
4407 	pos = skb_put(skb, 2 + sizeof(s1g_capab));
4408 	*pos++ = WLAN_EID_S1G_CAPABILITIES;
4409 	*pos++ = sizeof(s1g_capab);
4410 
4411 	memcpy(pos, &s1g_capab, sizeof(s1g_capab));
4412 }
4413 
4414 void ieee80211_add_aid_request_ie(struct ieee80211_sub_if_data *sdata,
4415 				  struct sk_buff *skb)
4416 {
4417 	u8 *pos = skb_put(skb, 3);
4418 
4419 	*pos++ = WLAN_EID_AID_REQUEST;
4420 	*pos++ = 1;
4421 	*pos++ = 0;
4422 }
4423 
4424 u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
4425 {
4426 	*buf++ = WLAN_EID_VENDOR_SPECIFIC;
4427 	*buf++ = 7; /* len */
4428 	*buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
4429 	*buf++ = 0x50;
4430 	*buf++ = 0xf2;
4431 	*buf++ = 2; /* WME */
4432 	*buf++ = 0; /* WME info */
4433 	*buf++ = 1; /* WME ver */
4434 	*buf++ = qosinfo; /* U-APSD no in use */
4435 
4436 	return buf;
4437 }
4438 
4439 void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
4440 			     unsigned long *frame_cnt,
4441 			     unsigned long *byte_cnt)
4442 {
4443 	struct txq_info *txqi = to_txq_info(txq);
4444 	u32 frag_cnt = 0, frag_bytes = 0;
4445 	struct sk_buff *skb;
4446 
4447 	skb_queue_walk(&txqi->frags, skb) {
4448 		frag_cnt++;
4449 		frag_bytes += skb->len;
4450 	}
4451 
4452 	if (frame_cnt)
4453 		*frame_cnt = txqi->tin.backlog_packets + frag_cnt;
4454 
4455 	if (byte_cnt)
4456 		*byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
4457 }
4458 EXPORT_SYMBOL(ieee80211_txq_get_depth);
4459 
4460 const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
4461 	IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
4462 	IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
4463 	IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
4464 	IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
4465 };
4466 
4467 u16 ieee80211_encode_usf(int listen_interval)
4468 {
4469 	static const int listen_int_usf[] = { 1, 10, 1000, 10000 };
4470 	u16 ui, usf = 0;
4471 
4472 	/* find greatest USF */
4473 	while (usf < IEEE80211_MAX_USF) {
4474 		if (listen_interval % listen_int_usf[usf + 1])
4475 			break;
4476 		usf += 1;
4477 	}
4478 	ui = listen_interval / listen_int_usf[usf];
4479 
4480 	/* error if there is a remainder. Should've been checked by user */
4481 	WARN_ON_ONCE(ui > IEEE80211_MAX_UI);
4482 	listen_interval = FIELD_PREP(LISTEN_INT_USF, usf) |
4483 			  FIELD_PREP(LISTEN_INT_UI, ui);
4484 
4485 	return (u16) listen_interval;
4486 }
4487 
4488 /* this may return more than ieee80211_put_eht_cap() will need */
4489 u8 ieee80211_ie_len_eht_cap(struct ieee80211_sub_if_data *sdata)
4490 {
4491 	const struct ieee80211_sta_he_cap *he_cap;
4492 	const struct ieee80211_sta_eht_cap *eht_cap;
4493 	struct ieee80211_supported_band *sband;
4494 	bool is_ap;
4495 	u8 n;
4496 
4497 	sband = ieee80211_get_sband(sdata);
4498 	if (!sband)
4499 		return 0;
4500 
4501 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
4502 	eht_cap = ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif);
4503 	if (!he_cap || !eht_cap)
4504 		return 0;
4505 
4506 	is_ap = sdata->vif.type == NL80211_IFTYPE_AP;
4507 
4508 	n = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
4509 				       &eht_cap->eht_cap_elem,
4510 				       is_ap);
4511 	return 2 + 1 +
4512 	       sizeof(eht_cap->eht_cap_elem) + n +
4513 	       ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
4514 				      eht_cap->eht_cap_elem.phy_cap_info);
4515 	return 0;
4516 }
4517 
4518 int ieee80211_put_eht_cap(struct sk_buff *skb,
4519 			  struct ieee80211_sub_if_data *sdata,
4520 			  const struct ieee80211_supported_band *sband,
4521 			  const struct ieee80211_conn_settings *conn)
4522 {
4523 	const struct ieee80211_sta_he_cap *he_cap =
4524 		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
4525 	const struct ieee80211_sta_eht_cap *eht_cap =
4526 		ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif);
4527 	bool for_ap = sdata->vif.type == NL80211_IFTYPE_AP;
4528 	struct ieee80211_eht_cap_elem_fixed fixed;
4529 	struct ieee80211_he_cap_elem he;
4530 	u8 mcs_nss_len, ppet_len;
4531 	u8 orig_mcs_nss_len;
4532 	u8 ie_len;
4533 
4534 	if (!conn)
4535 		conn = &ieee80211_conn_settings_unlimited;
4536 
4537 	/* Make sure we have place for the IE */
4538 	if (!he_cap || !eht_cap)
4539 		return 0;
4540 
4541 	orig_mcs_nss_len = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
4542 						      &eht_cap->eht_cap_elem,
4543 						      for_ap);
4544 
4545 	ieee80211_get_adjusted_he_cap(conn, he_cap, &he);
4546 
4547 	fixed = eht_cap->eht_cap_elem;
4548 
4549 	if (conn->bw_limit < IEEE80211_CONN_BW_LIMIT_80)
4550 		fixed.phy_cap_info[6] &=
4551 			~IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_80MHZ;
4552 
4553 	if (conn->bw_limit < IEEE80211_CONN_BW_LIMIT_160) {
4554 		fixed.phy_cap_info[1] &=
4555 			~IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK;
4556 		fixed.phy_cap_info[2] &=
4557 			~IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK;
4558 		fixed.phy_cap_info[6] &=
4559 			~IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_160MHZ;
4560 	}
4561 
4562 	if (conn->bw_limit < IEEE80211_CONN_BW_LIMIT_320) {
4563 		fixed.phy_cap_info[0] &=
4564 			~IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ;
4565 		fixed.phy_cap_info[1] &=
4566 			~IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_320MHZ_MASK;
4567 		fixed.phy_cap_info[2] &=
4568 			~IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_320MHZ_MASK;
4569 		fixed.phy_cap_info[6] &=
4570 			~IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_320MHZ;
4571 	}
4572 
4573 	if (conn->bw_limit == IEEE80211_CONN_BW_LIMIT_20)
4574 		fixed.phy_cap_info[0] &=
4575 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
4576 
4577 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&he, &fixed, for_ap);
4578 	ppet_len = ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
4579 					  fixed.phy_cap_info);
4580 
4581 	ie_len = 2 + 1 + sizeof(eht_cap->eht_cap_elem) + mcs_nss_len + ppet_len;
4582 	if (skb_tailroom(skb) < ie_len)
4583 		return -ENOBUFS;
4584 
4585 	skb_put_u8(skb, WLAN_EID_EXTENSION);
4586 	skb_put_u8(skb, ie_len - 2);
4587 	skb_put_u8(skb, WLAN_EID_EXT_EHT_CAPABILITY);
4588 	skb_put_data(skb, &fixed, sizeof(fixed));
4589 
4590 	if (mcs_nss_len == 4 && orig_mcs_nss_len != 4) {
4591 		/*
4592 		 * If the (non-AP) STA became 20 MHz only, then convert from
4593 		 * <=80 to 20-MHz-only format, where MCSes are indicated in
4594 		 * the groups 0-7, 8-9, 10-11, 12-13 rather than just 0-9,
4595 		 * 10-11, 12-13. Thus, use 0-9 for 0-7 and 8-9.
4596 		 */
4597 		skb_put_u8(skb, eht_cap->eht_mcs_nss_supp.bw._80.rx_tx_mcs9_max_nss);
4598 		skb_put_u8(skb, eht_cap->eht_mcs_nss_supp.bw._80.rx_tx_mcs9_max_nss);
4599 		skb_put_u8(skb, eht_cap->eht_mcs_nss_supp.bw._80.rx_tx_mcs11_max_nss);
4600 		skb_put_u8(skb, eht_cap->eht_mcs_nss_supp.bw._80.rx_tx_mcs13_max_nss);
4601 	} else {
4602 		skb_put_data(skb, &eht_cap->eht_mcs_nss_supp, mcs_nss_len);
4603 	}
4604 
4605 	if (ppet_len)
4606 		skb_put_data(skb, &eht_cap->eht_ppe_thres, ppet_len);
4607 
4608 	return 0;
4609 }
4610 
4611 int ieee80211_put_uhr_cap(struct sk_buff *skb,
4612 			  struct ieee80211_sub_if_data *sdata,
4613 			  const struct ieee80211_supported_band *sband)
4614 {
4615 	const struct ieee80211_sta_uhr_cap *uhr_cap =
4616 		ieee80211_get_uhr_iftype_cap_vif(sband, &sdata->vif);
4617 	int len;
4618 
4619 	if (!uhr_cap)
4620 		return 0;
4621 
4622 	len = 2 + 1 + sizeof(struct ieee80211_uhr_cap) +
4623 	      sizeof(struct ieee80211_uhr_cap_phy);
4624 
4625 	if (skb_tailroom(skb) < len)
4626 		return -ENOBUFS;
4627 
4628 	skb_put_u8(skb, WLAN_EID_EXTENSION);
4629 	skb_put_u8(skb, len - 2);
4630 	skb_put_u8(skb, WLAN_EID_EXT_UHR_CAPA);
4631 	skb_put_data(skb, &uhr_cap->mac, sizeof(uhr_cap->mac));
4632 	skb_put_data(skb, &uhr_cap->phy, sizeof(uhr_cap->phy));
4633 
4634 	return 0;
4635 }
4636 
4637 const char *ieee80211_conn_mode_str(enum ieee80211_conn_mode mode)
4638 {
4639 	static const char * const modes[] = {
4640 		[IEEE80211_CONN_MODE_S1G] = "S1G",
4641 		[IEEE80211_CONN_MODE_LEGACY] = "legacy",
4642 		[IEEE80211_CONN_MODE_HT] = "HT",
4643 		[IEEE80211_CONN_MODE_VHT] = "VHT",
4644 		[IEEE80211_CONN_MODE_HE] = "HE",
4645 		[IEEE80211_CONN_MODE_EHT] = "EHT",
4646 		[IEEE80211_CONN_MODE_UHR] = "UHR",
4647 	};
4648 
4649 	if (WARN_ON(mode >= ARRAY_SIZE(modes)))
4650 		return "<out of range>";
4651 
4652 	return modes[mode] ?: "<missing string>";
4653 }
4654 
4655 enum ieee80211_conn_bw_limit
4656 ieee80211_min_bw_limit_from_chandef(struct cfg80211_chan_def *chandef)
4657 {
4658 	switch (chandef->width) {
4659 	case NL80211_CHAN_WIDTH_20_NOHT:
4660 	case NL80211_CHAN_WIDTH_20:
4661 		return IEEE80211_CONN_BW_LIMIT_20;
4662 	case NL80211_CHAN_WIDTH_40:
4663 		return IEEE80211_CONN_BW_LIMIT_40;
4664 	case NL80211_CHAN_WIDTH_80:
4665 		return IEEE80211_CONN_BW_LIMIT_80;
4666 	case NL80211_CHAN_WIDTH_80P80:
4667 	case NL80211_CHAN_WIDTH_160:
4668 		return IEEE80211_CONN_BW_LIMIT_160;
4669 	case NL80211_CHAN_WIDTH_320:
4670 		return IEEE80211_CONN_BW_LIMIT_320;
4671 	default:
4672 		WARN(1, "unhandled chandef width %d\n", chandef->width);
4673 		return IEEE80211_CONN_BW_LIMIT_20;
4674 	}
4675 }
4676 
4677 void ieee80211_clear_tpe(struct ieee80211_parsed_tpe *tpe)
4678 {
4679 	for (int i = 0; i < 2; i++) {
4680 		tpe->max_local[i].valid = false;
4681 		memset(tpe->max_local[i].power,
4682 		       IEEE80211_TPE_MAX_TX_PWR_NO_CONSTRAINT,
4683 		       sizeof(tpe->max_local[i].power));
4684 
4685 		tpe->max_reg_client[i].valid = false;
4686 		memset(tpe->max_reg_client[i].power,
4687 		       IEEE80211_TPE_MAX_TX_PWR_NO_CONSTRAINT,
4688 		       sizeof(tpe->max_reg_client[i].power));
4689 
4690 		tpe->psd_local[i].valid = false;
4691 		memset(tpe->psd_local[i].power,
4692 		       IEEE80211_TPE_PSD_NO_LIMIT,
4693 		       sizeof(tpe->psd_local[i].power));
4694 
4695 		tpe->psd_reg_client[i].valid = false;
4696 		memset(tpe->psd_reg_client[i].power,
4697 		       IEEE80211_TPE_PSD_NO_LIMIT,
4698 		       sizeof(tpe->psd_reg_client[i].power));
4699 	}
4700 }
4701 
4702 bool ieee80211_vif_nan_started(struct ieee80211_vif *vif)
4703 {
4704 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4705 
4706 	return vif->type == NL80211_IFTYPE_NAN && sdata->u.nan.started;
4707 }
4708 EXPORT_SYMBOL_GPL(ieee80211_vif_nan_started);
4709