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