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