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