1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4  */
5 #include <linux/sched.h>
6 #include <linux/of.h>
7 #include "mt76.h"
8 
9 #define CHAN2G(_idx, _freq) {			\
10 	.band = NL80211_BAND_2GHZ,		\
11 	.center_freq = (_freq),			\
12 	.hw_value = (_idx),			\
13 	.max_power = 30,			\
14 }
15 
16 #define CHAN5G(_idx, _freq) {			\
17 	.band = NL80211_BAND_5GHZ,		\
18 	.center_freq = (_freq),			\
19 	.hw_value = (_idx),			\
20 	.max_power = 30,			\
21 }
22 
23 static const struct ieee80211_channel mt76_channels_2ghz[] = {
24 	CHAN2G(1, 2412),
25 	CHAN2G(2, 2417),
26 	CHAN2G(3, 2422),
27 	CHAN2G(4, 2427),
28 	CHAN2G(5, 2432),
29 	CHAN2G(6, 2437),
30 	CHAN2G(7, 2442),
31 	CHAN2G(8, 2447),
32 	CHAN2G(9, 2452),
33 	CHAN2G(10, 2457),
34 	CHAN2G(11, 2462),
35 	CHAN2G(12, 2467),
36 	CHAN2G(13, 2472),
37 	CHAN2G(14, 2484),
38 };
39 
40 static const struct ieee80211_channel mt76_channels_5ghz[] = {
41 	CHAN5G(36, 5180),
42 	CHAN5G(40, 5200),
43 	CHAN5G(44, 5220),
44 	CHAN5G(48, 5240),
45 
46 	CHAN5G(52, 5260),
47 	CHAN5G(56, 5280),
48 	CHAN5G(60, 5300),
49 	CHAN5G(64, 5320),
50 
51 	CHAN5G(100, 5500),
52 	CHAN5G(104, 5520),
53 	CHAN5G(108, 5540),
54 	CHAN5G(112, 5560),
55 	CHAN5G(116, 5580),
56 	CHAN5G(120, 5600),
57 	CHAN5G(124, 5620),
58 	CHAN5G(128, 5640),
59 	CHAN5G(132, 5660),
60 	CHAN5G(136, 5680),
61 	CHAN5G(140, 5700),
62 	CHAN5G(144, 5720),
63 
64 	CHAN5G(149, 5745),
65 	CHAN5G(153, 5765),
66 	CHAN5G(157, 5785),
67 	CHAN5G(161, 5805),
68 	CHAN5G(165, 5825),
69 	CHAN5G(169, 5845),
70 	CHAN5G(173, 5865),
71 };
72 
73 static const struct ieee80211_tpt_blink mt76_tpt_blink[] = {
74 	{ .throughput =   0 * 1024, .blink_time = 334 },
75 	{ .throughput =   1 * 1024, .blink_time = 260 },
76 	{ .throughput =   5 * 1024, .blink_time = 220 },
77 	{ .throughput =  10 * 1024, .blink_time = 190 },
78 	{ .throughput =  20 * 1024, .blink_time = 170 },
79 	{ .throughput =  50 * 1024, .blink_time = 150 },
80 	{ .throughput =  70 * 1024, .blink_time = 130 },
81 	{ .throughput = 100 * 1024, .blink_time = 110 },
82 	{ .throughput = 200 * 1024, .blink_time =  80 },
83 	{ .throughput = 300 * 1024, .blink_time =  50 },
84 };
85 
86 static int mt76_led_init(struct mt76_dev *dev)
87 {
88 	struct device_node *np = dev->dev->of_node;
89 	struct ieee80211_hw *hw = dev->hw;
90 	int led_pin;
91 
92 	if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
93 		return 0;
94 
95 	snprintf(dev->led_name, sizeof(dev->led_name),
96 		 "mt76-%s", wiphy_name(hw->wiphy));
97 
98 	dev->led_cdev.name = dev->led_name;
99 	dev->led_cdev.default_trigger =
100 		ieee80211_create_tpt_led_trigger(hw,
101 					IEEE80211_TPT_LEDTRIG_FL_RADIO,
102 					mt76_tpt_blink,
103 					ARRAY_SIZE(mt76_tpt_blink));
104 
105 	np = of_get_child_by_name(np, "led");
106 	if (np) {
107 		if (!of_property_read_u32(np, "led-sources", &led_pin))
108 			dev->led_pin = led_pin;
109 		dev->led_al = of_property_read_bool(np, "led-active-low");
110 	}
111 
112 	return led_classdev_register(dev->dev, &dev->led_cdev);
113 }
114 
115 static void mt76_led_cleanup(struct mt76_dev *dev)
116 {
117 	if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
118 		return;
119 
120 	led_classdev_unregister(&dev->led_cdev);
121 }
122 
123 static void mt76_init_stream_cap(struct mt76_phy *phy,
124 				 struct ieee80211_supported_band *sband,
125 				 bool vht)
126 {
127 	struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
128 	int i, nstream = hweight8(phy->antenna_mask);
129 	struct ieee80211_sta_vht_cap *vht_cap;
130 	u16 mcs_map = 0;
131 
132 	if (nstream > 1)
133 		ht_cap->cap |= IEEE80211_HT_CAP_TX_STBC;
134 	else
135 		ht_cap->cap &= ~IEEE80211_HT_CAP_TX_STBC;
136 
137 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
138 		ht_cap->mcs.rx_mask[i] = i < nstream ? 0xff : 0;
139 
140 	if (!vht)
141 		return;
142 
143 	vht_cap = &sband->vht_cap;
144 	if (nstream > 1)
145 		vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
146 	else
147 		vht_cap->cap &= ~IEEE80211_VHT_CAP_TXSTBC;
148 
149 	for (i = 0; i < 8; i++) {
150 		if (i < nstream)
151 			mcs_map |= (IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2));
152 		else
153 			mcs_map |=
154 				(IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2));
155 	}
156 	vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
157 	vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
158 }
159 
160 void mt76_set_stream_caps(struct mt76_phy *phy, bool vht)
161 {
162 	if (phy->cap.has_2ghz)
163 		mt76_init_stream_cap(phy, &phy->sband_2g.sband, false);
164 	if (phy->cap.has_5ghz)
165 		mt76_init_stream_cap(phy, &phy->sband_5g.sband, vht);
166 }
167 EXPORT_SYMBOL_GPL(mt76_set_stream_caps);
168 
169 static int
170 mt76_init_sband(struct mt76_phy *phy, struct mt76_sband *msband,
171 		const struct ieee80211_channel *chan, int n_chan,
172 		struct ieee80211_rate *rates, int n_rates, bool vht)
173 {
174 	struct ieee80211_supported_band *sband = &msband->sband;
175 	struct ieee80211_sta_vht_cap *vht_cap;
176 	struct ieee80211_sta_ht_cap *ht_cap;
177 	struct mt76_dev *dev = phy->dev;
178 	void *chanlist;
179 	int size;
180 
181 	size = n_chan * sizeof(*chan);
182 	chanlist = devm_kmemdup(dev->dev, chan, size, GFP_KERNEL);
183 	if (!chanlist)
184 		return -ENOMEM;
185 
186 	msband->chan = devm_kcalloc(dev->dev, n_chan, sizeof(*msband->chan),
187 				    GFP_KERNEL);
188 	if (!msband->chan)
189 		return -ENOMEM;
190 
191 	sband->channels = chanlist;
192 	sband->n_channels = n_chan;
193 	sband->bitrates = rates;
194 	sband->n_bitrates = n_rates;
195 
196 	ht_cap = &sband->ht_cap;
197 	ht_cap->ht_supported = true;
198 	ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
199 		       IEEE80211_HT_CAP_GRN_FLD |
200 		       IEEE80211_HT_CAP_SGI_20 |
201 		       IEEE80211_HT_CAP_SGI_40 |
202 		       (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
203 
204 	ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
205 	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
206 
207 	mt76_init_stream_cap(phy, sband, vht);
208 
209 	if (!vht)
210 		return 0;
211 
212 	vht_cap = &sband->vht_cap;
213 	vht_cap->vht_supported = true;
214 	vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC |
215 			IEEE80211_VHT_CAP_RXSTBC_1 |
216 			IEEE80211_VHT_CAP_SHORT_GI_80 |
217 			IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
218 			IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
219 			(3 << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT);
220 
221 	return 0;
222 }
223 
224 static int
225 mt76_init_sband_2g(struct mt76_phy *phy, struct ieee80211_rate *rates,
226 		   int n_rates)
227 {
228 	phy->hw->wiphy->bands[NL80211_BAND_2GHZ] = &phy->sband_2g.sband;
229 
230 	return mt76_init_sband(phy, &phy->sband_2g, mt76_channels_2ghz,
231 			       ARRAY_SIZE(mt76_channels_2ghz), rates,
232 			       n_rates, false);
233 }
234 
235 static int
236 mt76_init_sband_5g(struct mt76_phy *phy, struct ieee80211_rate *rates,
237 		   int n_rates, bool vht)
238 {
239 	phy->hw->wiphy->bands[NL80211_BAND_5GHZ] = &phy->sband_5g.sband;
240 
241 	return mt76_init_sband(phy, &phy->sband_5g, mt76_channels_5ghz,
242 			       ARRAY_SIZE(mt76_channels_5ghz), rates,
243 			       n_rates, vht);
244 }
245 
246 static void
247 mt76_check_sband(struct mt76_phy *phy, struct mt76_sband *msband,
248 		 enum nl80211_band band)
249 {
250 	struct ieee80211_supported_band *sband = &msband->sband;
251 	bool found = false;
252 	int i;
253 
254 	if (!sband)
255 		return;
256 
257 	for (i = 0; i < sband->n_channels; i++) {
258 		if (sband->channels[i].flags & IEEE80211_CHAN_DISABLED)
259 			continue;
260 
261 		found = true;
262 		break;
263 	}
264 
265 	if (found) {
266 		phy->chandef.chan = &sband->channels[0];
267 		phy->chan_state = &msband->chan[0];
268 		return;
269 	}
270 
271 	sband->n_channels = 0;
272 	phy->hw->wiphy->bands[band] = NULL;
273 }
274 
275 static void
276 mt76_phy_init(struct mt76_phy *phy, struct ieee80211_hw *hw)
277 {
278 	struct mt76_dev *dev = phy->dev;
279 	struct wiphy *wiphy = hw->wiphy;
280 
281 	SET_IEEE80211_DEV(hw, dev->dev);
282 	SET_IEEE80211_PERM_ADDR(hw, phy->macaddr);
283 
284 	wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
285 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH |
286 			WIPHY_FLAG_SUPPORTS_TDLS |
287 			WIPHY_FLAG_AP_UAPSD;
288 
289 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
290 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
291 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AQL);
292 
293 	wiphy->available_antennas_tx = dev->phy.antenna_mask;
294 	wiphy->available_antennas_rx = dev->phy.antenna_mask;
295 
296 	hw->txq_data_size = sizeof(struct mt76_txq);
297 	hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
298 
299 	if (!hw->max_tx_fragments)
300 		hw->max_tx_fragments = 16;
301 
302 	ieee80211_hw_set(hw, SIGNAL_DBM);
303 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
304 	ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
305 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
306 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
307 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
308 	ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
309 
310 	if (!(dev->drv->drv_flags & MT_DRV_AMSDU_OFFLOAD)) {
311 		ieee80211_hw_set(hw, TX_AMSDU);
312 		ieee80211_hw_set(hw, TX_FRAG_LIST);
313 	}
314 
315 	ieee80211_hw_set(hw, MFP_CAPABLE);
316 	ieee80211_hw_set(hw, AP_LINK_PS);
317 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
318 
319 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
320 	wiphy->interface_modes =
321 		BIT(NL80211_IFTYPE_STATION) |
322 		BIT(NL80211_IFTYPE_AP) |
323 #ifdef CONFIG_MAC80211_MESH
324 		BIT(NL80211_IFTYPE_MESH_POINT) |
325 #endif
326 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
327 		BIT(NL80211_IFTYPE_P2P_GO) |
328 		BIT(NL80211_IFTYPE_ADHOC);
329 }
330 
331 struct mt76_phy *
332 mt76_alloc_phy(struct mt76_dev *dev, unsigned int size,
333 	       const struct ieee80211_ops *ops)
334 {
335 	struct ieee80211_hw *hw;
336 	unsigned int phy_size;
337 	struct mt76_phy *phy;
338 
339 	phy_size = ALIGN(sizeof(*phy), 8);
340 	hw = ieee80211_alloc_hw(size + phy_size, ops);
341 	if (!hw)
342 		return NULL;
343 
344 	phy = hw->priv;
345 	phy->dev = dev;
346 	phy->hw = hw;
347 	phy->priv = hw->priv + phy_size;
348 
349 	return phy;
350 }
351 EXPORT_SYMBOL_GPL(mt76_alloc_phy);
352 
353 int mt76_register_phy(struct mt76_phy *phy, bool vht,
354 		      struct ieee80211_rate *rates, int n_rates)
355 {
356 	int ret;
357 
358 	mt76_phy_init(phy, phy->hw);
359 
360 	if (phy->cap.has_2ghz) {
361 		ret = mt76_init_sband_2g(phy, rates, n_rates);
362 		if (ret)
363 			return ret;
364 	}
365 
366 	if (phy->cap.has_5ghz) {
367 		ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht);
368 		if (ret)
369 			return ret;
370 	}
371 
372 	wiphy_read_of_freq_limits(phy->hw->wiphy);
373 	mt76_check_sband(phy, &phy->sband_2g, NL80211_BAND_2GHZ);
374 	mt76_check_sband(phy, &phy->sband_5g, NL80211_BAND_5GHZ);
375 
376 	ret = ieee80211_register_hw(phy->hw);
377 	if (ret)
378 		return ret;
379 
380 	phy->dev->phy2 = phy;
381 
382 	return 0;
383 }
384 EXPORT_SYMBOL_GPL(mt76_register_phy);
385 
386 void mt76_unregister_phy(struct mt76_phy *phy)
387 {
388 	struct mt76_dev *dev = phy->dev;
389 
390 	mt76_tx_status_check(dev, NULL, true);
391 	ieee80211_unregister_hw(phy->hw);
392 	dev->phy2 = NULL;
393 }
394 EXPORT_SYMBOL_GPL(mt76_unregister_phy);
395 
396 struct mt76_dev *
397 mt76_alloc_device(struct device *pdev, unsigned int size,
398 		  const struct ieee80211_ops *ops,
399 		  const struct mt76_driver_ops *drv_ops)
400 {
401 	struct ieee80211_hw *hw;
402 	struct mt76_phy *phy;
403 	struct mt76_dev *dev;
404 	int i;
405 
406 	hw = ieee80211_alloc_hw(size, ops);
407 	if (!hw)
408 		return NULL;
409 
410 	dev = hw->priv;
411 	dev->hw = hw;
412 	dev->dev = pdev;
413 	dev->drv = drv_ops;
414 
415 	phy = &dev->phy;
416 	phy->dev = dev;
417 	phy->hw = hw;
418 
419 	spin_lock_init(&dev->rx_lock);
420 	spin_lock_init(&dev->lock);
421 	spin_lock_init(&dev->cc_lock);
422 	mutex_init(&dev->mutex);
423 	init_waitqueue_head(&dev->tx_wait);
424 	skb_queue_head_init(&dev->status_list);
425 
426 	skb_queue_head_init(&dev->mcu.res_q);
427 	init_waitqueue_head(&dev->mcu.wait);
428 	mutex_init(&dev->mcu.mutex);
429 	dev->tx_worker.fn = mt76_tx_worker;
430 
431 	spin_lock_init(&dev->token_lock);
432 	idr_init(&dev->token);
433 
434 	INIT_LIST_HEAD(&dev->txwi_cache);
435 
436 	for (i = 0; i < ARRAY_SIZE(dev->q_rx); i++)
437 		skb_queue_head_init(&dev->rx_skb[i]);
438 
439 	dev->wq = alloc_ordered_workqueue("mt76", 0);
440 	if (!dev->wq) {
441 		ieee80211_free_hw(hw);
442 		return NULL;
443 	}
444 
445 	return dev;
446 }
447 EXPORT_SYMBOL_GPL(mt76_alloc_device);
448 
449 int mt76_register_device(struct mt76_dev *dev, bool vht,
450 			 struct ieee80211_rate *rates, int n_rates)
451 {
452 	struct ieee80211_hw *hw = dev->hw;
453 	struct mt76_phy *phy = &dev->phy;
454 	int ret;
455 
456 	dev_set_drvdata(dev->dev, dev);
457 	mt76_phy_init(phy, hw);
458 
459 	if (phy->cap.has_2ghz) {
460 		ret = mt76_init_sband_2g(phy, rates, n_rates);
461 		if (ret)
462 			return ret;
463 	}
464 
465 	if (phy->cap.has_5ghz) {
466 		ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht);
467 		if (ret)
468 			return ret;
469 	}
470 
471 	wiphy_read_of_freq_limits(hw->wiphy);
472 	mt76_check_sband(&dev->phy, &phy->sband_2g, NL80211_BAND_2GHZ);
473 	mt76_check_sband(&dev->phy, &phy->sband_5g, NL80211_BAND_5GHZ);
474 
475 	if (IS_ENABLED(CONFIG_MT76_LEDS)) {
476 		ret = mt76_led_init(dev);
477 		if (ret)
478 			return ret;
479 	}
480 
481 	ret = ieee80211_register_hw(hw);
482 	if (ret)
483 		return ret;
484 
485 	WARN_ON(mt76_worker_setup(hw, &dev->tx_worker, NULL, "tx"));
486 	sched_set_fifo_low(dev->tx_worker.task);
487 
488 	return 0;
489 }
490 EXPORT_SYMBOL_GPL(mt76_register_device);
491 
492 void mt76_unregister_device(struct mt76_dev *dev)
493 {
494 	struct ieee80211_hw *hw = dev->hw;
495 
496 	if (IS_ENABLED(CONFIG_MT76_LEDS))
497 		mt76_led_cleanup(dev);
498 	mt76_tx_status_check(dev, NULL, true);
499 	ieee80211_unregister_hw(hw);
500 }
501 EXPORT_SYMBOL_GPL(mt76_unregister_device);
502 
503 void mt76_free_device(struct mt76_dev *dev)
504 {
505 	mt76_worker_teardown(&dev->tx_worker);
506 	if (dev->wq) {
507 		destroy_workqueue(dev->wq);
508 		dev->wq = NULL;
509 	}
510 	ieee80211_free_hw(dev->hw);
511 }
512 EXPORT_SYMBOL_GPL(mt76_free_device);
513 
514 static void mt76_rx_release_amsdu(struct mt76_phy *phy, enum mt76_rxq_id q)
515 {
516 	struct sk_buff *skb = phy->rx_amsdu[q].head;
517 	struct mt76_dev *dev = phy->dev;
518 
519 	phy->rx_amsdu[q].head = NULL;
520 	phy->rx_amsdu[q].tail = NULL;
521 	__skb_queue_tail(&dev->rx_skb[q], skb);
522 }
523 
524 static void mt76_rx_release_burst(struct mt76_phy *phy, enum mt76_rxq_id q,
525 				  struct sk_buff *skb)
526 {
527 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
528 
529 	if (phy->rx_amsdu[q].head &&
530 	    (!status->amsdu || status->first_amsdu ||
531 	     status->seqno != phy->rx_amsdu[q].seqno))
532 		mt76_rx_release_amsdu(phy, q);
533 
534 	if (!phy->rx_amsdu[q].head) {
535 		phy->rx_amsdu[q].tail = &skb_shinfo(skb)->frag_list;
536 		phy->rx_amsdu[q].seqno = status->seqno;
537 		phy->rx_amsdu[q].head = skb;
538 	} else {
539 		*phy->rx_amsdu[q].tail = skb;
540 		phy->rx_amsdu[q].tail = &skb->next;
541 	}
542 
543 	if (!status->amsdu || status->last_amsdu)
544 		mt76_rx_release_amsdu(phy, q);
545 }
546 
547 void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb)
548 {
549 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
550 	struct mt76_phy *phy = mt76_dev_phy(dev, status->ext_phy);
551 
552 	if (!test_bit(MT76_STATE_RUNNING, &phy->state)) {
553 		dev_kfree_skb(skb);
554 		return;
555 	}
556 
557 #ifdef CONFIG_NL80211_TESTMODE
558 	if (phy->test.state == MT76_TM_STATE_RX_FRAMES) {
559 		phy->test.rx_stats.packets[q]++;
560 		if (status->flag & RX_FLAG_FAILED_FCS_CRC)
561 			phy->test.rx_stats.fcs_error[q]++;
562 	}
563 #endif
564 
565 	mt76_rx_release_burst(phy, q, skb);
566 }
567 EXPORT_SYMBOL_GPL(mt76_rx);
568 
569 bool mt76_has_tx_pending(struct mt76_phy *phy)
570 {
571 	struct mt76_queue *q;
572 	int i;
573 
574 	for (i = 0; i < __MT_TXQ_MAX; i++) {
575 		q = phy->q_tx[i];
576 		if (q && q->queued)
577 			return true;
578 	}
579 
580 	return false;
581 }
582 EXPORT_SYMBOL_GPL(mt76_has_tx_pending);
583 
584 static struct mt76_channel_state *
585 mt76_channel_state(struct mt76_phy *phy, struct ieee80211_channel *c)
586 {
587 	struct mt76_sband *msband;
588 	int idx;
589 
590 	if (c->band == NL80211_BAND_2GHZ)
591 		msband = &phy->sband_2g;
592 	else
593 		msband = &phy->sband_5g;
594 
595 	idx = c - &msband->sband.channels[0];
596 	return &msband->chan[idx];
597 }
598 
599 void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time)
600 {
601 	struct mt76_channel_state *state = phy->chan_state;
602 
603 	state->cc_active += ktime_to_us(ktime_sub(time,
604 						  phy->survey_time));
605 	phy->survey_time = time;
606 }
607 EXPORT_SYMBOL_GPL(mt76_update_survey_active_time);
608 
609 void mt76_update_survey(struct mt76_dev *dev)
610 {
611 	ktime_t cur_time;
612 
613 	if (dev->drv->update_survey)
614 		dev->drv->update_survey(dev);
615 
616 	cur_time = ktime_get_boottime();
617 	mt76_update_survey_active_time(&dev->phy, cur_time);
618 	if (dev->phy2)
619 		mt76_update_survey_active_time(dev->phy2, cur_time);
620 
621 	if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME) {
622 		struct mt76_channel_state *state = dev->phy.chan_state;
623 
624 		spin_lock_bh(&dev->cc_lock);
625 		state->cc_bss_rx += dev->cur_cc_bss_rx;
626 		dev->cur_cc_bss_rx = 0;
627 		spin_unlock_bh(&dev->cc_lock);
628 	}
629 }
630 EXPORT_SYMBOL_GPL(mt76_update_survey);
631 
632 void mt76_set_channel(struct mt76_phy *phy)
633 {
634 	struct mt76_dev *dev = phy->dev;
635 	struct ieee80211_hw *hw = phy->hw;
636 	struct cfg80211_chan_def *chandef = &hw->conf.chandef;
637 	bool offchannel = hw->conf.flags & IEEE80211_CONF_OFFCHANNEL;
638 	int timeout = HZ / 5;
639 
640 	wait_event_timeout(dev->tx_wait, !mt76_has_tx_pending(phy), timeout);
641 	mt76_update_survey(dev);
642 
643 	phy->chandef = *chandef;
644 	phy->chan_state = mt76_channel_state(phy, chandef->chan);
645 
646 	if (!offchannel)
647 		phy->main_chan = chandef->chan;
648 
649 	if (chandef->chan != phy->main_chan)
650 		memset(phy->chan_state, 0, sizeof(*phy->chan_state));
651 }
652 EXPORT_SYMBOL_GPL(mt76_set_channel);
653 
654 int mt76_get_survey(struct ieee80211_hw *hw, int idx,
655 		    struct survey_info *survey)
656 {
657 	struct mt76_phy *phy = hw->priv;
658 	struct mt76_dev *dev = phy->dev;
659 	struct mt76_sband *sband;
660 	struct ieee80211_channel *chan;
661 	struct mt76_channel_state *state;
662 	int ret = 0;
663 
664 	mutex_lock(&dev->mutex);
665 	if (idx == 0 && dev->drv->update_survey)
666 		mt76_update_survey(dev);
667 
668 	sband = &phy->sband_2g;
669 	if (idx >= sband->sband.n_channels) {
670 		idx -= sband->sband.n_channels;
671 		sband = &phy->sband_5g;
672 	}
673 
674 	if (idx >= sband->sband.n_channels) {
675 		ret = -ENOENT;
676 		goto out;
677 	}
678 
679 	chan = &sband->sband.channels[idx];
680 	state = mt76_channel_state(phy, chan);
681 
682 	memset(survey, 0, sizeof(*survey));
683 	survey->channel = chan;
684 	survey->filled = SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY;
685 	survey->filled |= dev->drv->survey_flags;
686 	if (state->noise)
687 		survey->filled |= SURVEY_INFO_NOISE_DBM;
688 
689 	if (chan == phy->main_chan) {
690 		survey->filled |= SURVEY_INFO_IN_USE;
691 
692 		if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME)
693 			survey->filled |= SURVEY_INFO_TIME_BSS_RX;
694 	}
695 
696 	survey->time_busy = div_u64(state->cc_busy, 1000);
697 	survey->time_rx = div_u64(state->cc_rx, 1000);
698 	survey->time = div_u64(state->cc_active, 1000);
699 	survey->noise = state->noise;
700 
701 	spin_lock_bh(&dev->cc_lock);
702 	survey->time_bss_rx = div_u64(state->cc_bss_rx, 1000);
703 	survey->time_tx = div_u64(state->cc_tx, 1000);
704 	spin_unlock_bh(&dev->cc_lock);
705 
706 out:
707 	mutex_unlock(&dev->mutex);
708 
709 	return ret;
710 }
711 EXPORT_SYMBOL_GPL(mt76_get_survey);
712 
713 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
714 			 struct ieee80211_key_conf *key)
715 {
716 	struct ieee80211_key_seq seq;
717 	int i;
718 
719 	wcid->rx_check_pn = false;
720 
721 	if (!key)
722 		return;
723 
724 	if (key->cipher != WLAN_CIPHER_SUITE_CCMP)
725 		return;
726 
727 	wcid->rx_check_pn = true;
728 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
729 		ieee80211_get_key_rx_seq(key, i, &seq);
730 		memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
731 	}
732 }
733 EXPORT_SYMBOL(mt76_wcid_key_setup);
734 
735 static void
736 mt76_rx_convert(struct mt76_dev *dev, struct sk_buff *skb,
737 		struct ieee80211_hw **hw,
738 		struct ieee80211_sta **sta)
739 {
740 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
741 	struct mt76_rx_status mstat;
742 
743 	mstat = *((struct mt76_rx_status *)skb->cb);
744 	memset(status, 0, sizeof(*status));
745 
746 	status->flag = mstat.flag;
747 	status->freq = mstat.freq;
748 	status->enc_flags = mstat.enc_flags;
749 	status->encoding = mstat.encoding;
750 	status->bw = mstat.bw;
751 	status->he_ru = mstat.he_ru;
752 	status->he_gi = mstat.he_gi;
753 	status->he_dcm = mstat.he_dcm;
754 	status->rate_idx = mstat.rate_idx;
755 	status->nss = mstat.nss;
756 	status->band = mstat.band;
757 	status->signal = mstat.signal;
758 	status->chains = mstat.chains;
759 	status->ampdu_reference = mstat.ampdu_ref;
760 	status->device_timestamp = mstat.timestamp;
761 	status->mactime = mstat.timestamp;
762 
763 	BUILD_BUG_ON(sizeof(mstat) > sizeof(skb->cb));
764 	BUILD_BUG_ON(sizeof(status->chain_signal) !=
765 		     sizeof(mstat.chain_signal));
766 	memcpy(status->chain_signal, mstat.chain_signal,
767 	       sizeof(mstat.chain_signal));
768 
769 	*sta = wcid_to_sta(mstat.wcid);
770 	*hw = mt76_phy_hw(dev, mstat.ext_phy);
771 }
772 
773 static int
774 mt76_check_ccmp_pn(struct sk_buff *skb)
775 {
776 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
777 	struct mt76_wcid *wcid = status->wcid;
778 	struct ieee80211_hdr *hdr;
779 	u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
780 	int ret;
781 
782 	if (!(status->flag & RX_FLAG_DECRYPTED))
783 		return 0;
784 
785 	if (!wcid || !wcid->rx_check_pn)
786 		return 0;
787 
788 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
789 		/*
790 		 * Validate the first fragment both here and in mac80211
791 		 * All further fragments will be validated by mac80211 only.
792 		 */
793 		hdr = mt76_skb_get_hdr(skb);
794 		if (ieee80211_is_frag(hdr) &&
795 		    !ieee80211_is_first_frag(hdr->frame_control))
796 			return 0;
797 	}
798 
799 	BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0]));
800 	ret = memcmp(status->iv, wcid->rx_key_pn[tidno],
801 		     sizeof(status->iv));
802 	if (ret <= 0)
803 		return -EINVAL; /* replay */
804 
805 	memcpy(wcid->rx_key_pn[tidno], status->iv, sizeof(status->iv));
806 
807 	if (status->flag & RX_FLAG_IV_STRIPPED)
808 		status->flag |= RX_FLAG_PN_VALIDATED;
809 
810 	return 0;
811 }
812 
813 static void
814 mt76_airtime_report(struct mt76_dev *dev, struct mt76_rx_status *status,
815 		    int len)
816 {
817 	struct mt76_wcid *wcid = status->wcid;
818 	struct ieee80211_rx_status info = {
819 		.enc_flags = status->enc_flags,
820 		.rate_idx = status->rate_idx,
821 		.encoding = status->encoding,
822 		.band = status->band,
823 		.nss = status->nss,
824 		.bw = status->bw,
825 	};
826 	struct ieee80211_sta *sta;
827 	u32 airtime;
828 	u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
829 
830 	airtime = ieee80211_calc_rx_airtime(dev->hw, &info, len);
831 	spin_lock(&dev->cc_lock);
832 	dev->cur_cc_bss_rx += airtime;
833 	spin_unlock(&dev->cc_lock);
834 
835 	if (!wcid || !wcid->sta)
836 		return;
837 
838 	sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
839 	ieee80211_sta_register_airtime(sta, tidno, 0, airtime);
840 }
841 
842 static void
843 mt76_airtime_flush_ampdu(struct mt76_dev *dev)
844 {
845 	struct mt76_wcid *wcid;
846 	int wcid_idx;
847 
848 	if (!dev->rx_ampdu_len)
849 		return;
850 
851 	wcid_idx = dev->rx_ampdu_status.wcid_idx;
852 	if (wcid_idx < ARRAY_SIZE(dev->wcid))
853 		wcid = rcu_dereference(dev->wcid[wcid_idx]);
854 	else
855 		wcid = NULL;
856 	dev->rx_ampdu_status.wcid = wcid;
857 
858 	mt76_airtime_report(dev, &dev->rx_ampdu_status, dev->rx_ampdu_len);
859 
860 	dev->rx_ampdu_len = 0;
861 	dev->rx_ampdu_ref = 0;
862 }
863 
864 static void
865 mt76_airtime_check(struct mt76_dev *dev, struct sk_buff *skb)
866 {
867 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
868 	struct mt76_wcid *wcid = status->wcid;
869 
870 	if (!(dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME))
871 		return;
872 
873 	if (!wcid || !wcid->sta) {
874 		struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
875 
876 		if (status->flag & RX_FLAG_8023)
877 			return;
878 
879 		if (!ether_addr_equal(hdr->addr1, dev->phy.macaddr))
880 			return;
881 
882 		wcid = NULL;
883 	}
884 
885 	if (!(status->flag & RX_FLAG_AMPDU_DETAILS) ||
886 	    status->ampdu_ref != dev->rx_ampdu_ref)
887 		mt76_airtime_flush_ampdu(dev);
888 
889 	if (status->flag & RX_FLAG_AMPDU_DETAILS) {
890 		if (!dev->rx_ampdu_len ||
891 		    status->ampdu_ref != dev->rx_ampdu_ref) {
892 			dev->rx_ampdu_status = *status;
893 			dev->rx_ampdu_status.wcid_idx = wcid ? wcid->idx : 0xff;
894 			dev->rx_ampdu_ref = status->ampdu_ref;
895 		}
896 
897 		dev->rx_ampdu_len += skb->len;
898 		return;
899 	}
900 
901 	mt76_airtime_report(dev, status, skb->len);
902 }
903 
904 static void
905 mt76_check_sta(struct mt76_dev *dev, struct sk_buff *skb)
906 {
907 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
908 	struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
909 	struct ieee80211_sta *sta;
910 	struct ieee80211_hw *hw;
911 	struct mt76_wcid *wcid = status->wcid;
912 	u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
913 	bool ps;
914 
915 	hw = mt76_phy_hw(dev, status->ext_phy);
916 	if (ieee80211_is_pspoll(hdr->frame_control) && !wcid &&
917 	    !(status->flag & RX_FLAG_8023)) {
918 		sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr2, NULL);
919 		if (sta)
920 			wcid = status->wcid = (struct mt76_wcid *)sta->drv_priv;
921 	}
922 
923 	mt76_airtime_check(dev, skb);
924 
925 	if (!wcid || !wcid->sta)
926 		return;
927 
928 	sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
929 
930 	if (status->signal <= 0)
931 		ewma_signal_add(&wcid->rssi, -status->signal);
932 
933 	wcid->inactive_count = 0;
934 
935 	if (status->flag & RX_FLAG_8023)
936 		return;
937 
938 	if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags))
939 		return;
940 
941 	if (ieee80211_is_pspoll(hdr->frame_control)) {
942 		ieee80211_sta_pspoll(sta);
943 		return;
944 	}
945 
946 	if (ieee80211_has_morefrags(hdr->frame_control) ||
947 	    !(ieee80211_is_mgmt(hdr->frame_control) ||
948 	      ieee80211_is_data(hdr->frame_control)))
949 		return;
950 
951 	ps = ieee80211_has_pm(hdr->frame_control);
952 
953 	if (ps && (ieee80211_is_data_qos(hdr->frame_control) ||
954 		   ieee80211_is_qos_nullfunc(hdr->frame_control)))
955 		ieee80211_sta_uapsd_trigger(sta, tidno);
956 
957 	if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps)
958 		return;
959 
960 	if (ps)
961 		set_bit(MT_WCID_FLAG_PS, &wcid->flags);
962 	else
963 		clear_bit(MT_WCID_FLAG_PS, &wcid->flags);
964 
965 	dev->drv->sta_ps(dev, sta, ps);
966 	ieee80211_sta_ps_transition(sta, ps);
967 }
968 
969 void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
970 		      struct napi_struct *napi)
971 {
972 	struct ieee80211_sta *sta;
973 	struct ieee80211_hw *hw;
974 	struct sk_buff *skb, *tmp;
975 	LIST_HEAD(list);
976 
977 	spin_lock(&dev->rx_lock);
978 	while ((skb = __skb_dequeue(frames)) != NULL) {
979 		struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
980 
981 		if (mt76_check_ccmp_pn(skb)) {
982 			dev_kfree_skb(skb);
983 			continue;
984 		}
985 
986 		skb_shinfo(skb)->frag_list = NULL;
987 		mt76_rx_convert(dev, skb, &hw, &sta);
988 		ieee80211_rx_list(hw, sta, skb, &list);
989 
990 		/* subsequent amsdu frames */
991 		while (nskb) {
992 			skb = nskb;
993 			nskb = nskb->next;
994 			skb->next = NULL;
995 
996 			mt76_rx_convert(dev, skb, &hw, &sta);
997 			ieee80211_rx_list(hw, sta, skb, &list);
998 		}
999 	}
1000 	spin_unlock(&dev->rx_lock);
1001 
1002 	if (!napi) {
1003 		netif_receive_skb_list(&list);
1004 		return;
1005 	}
1006 
1007 	list_for_each_entry_safe(skb, tmp, &list, list) {
1008 		skb_list_del_init(skb);
1009 		napi_gro_receive(napi, skb);
1010 	}
1011 }
1012 
1013 void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
1014 			   struct napi_struct *napi)
1015 {
1016 	struct sk_buff_head frames;
1017 	struct sk_buff *skb;
1018 
1019 	__skb_queue_head_init(&frames);
1020 
1021 	while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) {
1022 		mt76_check_sta(dev, skb);
1023 		mt76_rx_aggr_reorder(skb, &frames);
1024 	}
1025 
1026 	mt76_rx_complete(dev, &frames, napi);
1027 }
1028 EXPORT_SYMBOL_GPL(mt76_rx_poll_complete);
1029 
1030 static int
1031 mt76_sta_add(struct mt76_dev *dev, struct ieee80211_vif *vif,
1032 	     struct ieee80211_sta *sta, bool ext_phy)
1033 {
1034 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1035 	int ret;
1036 	int i;
1037 
1038 	mutex_lock(&dev->mutex);
1039 
1040 	ret = dev->drv->sta_add(dev, vif, sta);
1041 	if (ret)
1042 		goto out;
1043 
1044 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1045 		struct mt76_txq *mtxq;
1046 
1047 		if (!sta->txq[i])
1048 			continue;
1049 
1050 		mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv;
1051 		mtxq->wcid = wcid;
1052 	}
1053 
1054 	ewma_signal_init(&wcid->rssi);
1055 	if (ext_phy)
1056 		mt76_wcid_mask_set(dev->wcid_phy_mask, wcid->idx);
1057 	wcid->ext_phy = ext_phy;
1058 	rcu_assign_pointer(dev->wcid[wcid->idx], wcid);
1059 
1060 out:
1061 	mutex_unlock(&dev->mutex);
1062 
1063 	return ret;
1064 }
1065 
1066 void __mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
1067 		       struct ieee80211_sta *sta)
1068 {
1069 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1070 	int i, idx = wcid->idx;
1071 
1072 	for (i = 0; i < ARRAY_SIZE(wcid->aggr); i++)
1073 		mt76_rx_aggr_stop(dev, wcid, i);
1074 
1075 	if (dev->drv->sta_remove)
1076 		dev->drv->sta_remove(dev, vif, sta);
1077 
1078 	mt76_tx_status_check(dev, wcid, true);
1079 	mt76_wcid_mask_clear(dev->wcid_mask, idx);
1080 	mt76_wcid_mask_clear(dev->wcid_phy_mask, idx);
1081 }
1082 EXPORT_SYMBOL_GPL(__mt76_sta_remove);
1083 
1084 static void
1085 mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
1086 		struct ieee80211_sta *sta)
1087 {
1088 	mutex_lock(&dev->mutex);
1089 	__mt76_sta_remove(dev, vif, sta);
1090 	mutex_unlock(&dev->mutex);
1091 }
1092 
1093 int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1094 		   struct ieee80211_sta *sta,
1095 		   enum ieee80211_sta_state old_state,
1096 		   enum ieee80211_sta_state new_state)
1097 {
1098 	struct mt76_phy *phy = hw->priv;
1099 	struct mt76_dev *dev = phy->dev;
1100 	bool ext_phy = phy != &dev->phy;
1101 
1102 	if (old_state == IEEE80211_STA_NOTEXIST &&
1103 	    new_state == IEEE80211_STA_NONE)
1104 		return mt76_sta_add(dev, vif, sta, ext_phy);
1105 
1106 	if (old_state == IEEE80211_STA_AUTH &&
1107 	    new_state == IEEE80211_STA_ASSOC &&
1108 	    dev->drv->sta_assoc)
1109 		dev->drv->sta_assoc(dev, vif, sta);
1110 
1111 	if (old_state == IEEE80211_STA_NONE &&
1112 	    new_state == IEEE80211_STA_NOTEXIST)
1113 		mt76_sta_remove(dev, vif, sta);
1114 
1115 	return 0;
1116 }
1117 EXPORT_SYMBOL_GPL(mt76_sta_state);
1118 
1119 void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1120 			     struct ieee80211_sta *sta)
1121 {
1122 	struct mt76_phy *phy = hw->priv;
1123 	struct mt76_dev *dev = phy->dev;
1124 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1125 
1126 	mutex_lock(&dev->mutex);
1127 	rcu_assign_pointer(dev->wcid[wcid->idx], NULL);
1128 	mutex_unlock(&dev->mutex);
1129 }
1130 EXPORT_SYMBOL_GPL(mt76_sta_pre_rcu_remove);
1131 
1132 int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1133 		     int *dbm)
1134 {
1135 	struct mt76_phy *phy = hw->priv;
1136 	int n_chains = hweight8(phy->antenna_mask);
1137 	int delta = mt76_tx_power_nss_delta(n_chains);
1138 
1139 	*dbm = DIV_ROUND_UP(phy->txpower_cur + delta, 2);
1140 
1141 	return 0;
1142 }
1143 EXPORT_SYMBOL_GPL(mt76_get_txpower);
1144 
1145 static void
1146 __mt76_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif)
1147 {
1148 	if (vif->csa_active && ieee80211_beacon_cntdwn_is_complete(vif))
1149 		ieee80211_csa_finish(vif);
1150 }
1151 
1152 void mt76_csa_finish(struct mt76_dev *dev)
1153 {
1154 	if (!dev->csa_complete)
1155 		return;
1156 
1157 	ieee80211_iterate_active_interfaces_atomic(dev->hw,
1158 		IEEE80211_IFACE_ITER_RESUME_ALL,
1159 		__mt76_csa_finish, dev);
1160 
1161 	dev->csa_complete = 0;
1162 }
1163 EXPORT_SYMBOL_GPL(mt76_csa_finish);
1164 
1165 static void
1166 __mt76_csa_check(void *priv, u8 *mac, struct ieee80211_vif *vif)
1167 {
1168 	struct mt76_dev *dev = priv;
1169 
1170 	if (!vif->csa_active)
1171 		return;
1172 
1173 	dev->csa_complete |= ieee80211_beacon_cntdwn_is_complete(vif);
1174 }
1175 
1176 void mt76_csa_check(struct mt76_dev *dev)
1177 {
1178 	ieee80211_iterate_active_interfaces_atomic(dev->hw,
1179 		IEEE80211_IFACE_ITER_RESUME_ALL,
1180 		__mt76_csa_check, dev);
1181 }
1182 EXPORT_SYMBOL_GPL(mt76_csa_check);
1183 
1184 int
1185 mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
1186 {
1187 	return 0;
1188 }
1189 EXPORT_SYMBOL_GPL(mt76_set_tim);
1190 
1191 void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id)
1192 {
1193 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1194 	int hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1195 	u8 *hdr, *pn = status->iv;
1196 
1197 	__skb_push(skb, 8);
1198 	memmove(skb->data, skb->data + 8, hdr_len);
1199 	hdr = skb->data + hdr_len;
1200 
1201 	hdr[0] = pn[5];
1202 	hdr[1] = pn[4];
1203 	hdr[2] = 0;
1204 	hdr[3] = 0x20 | (key_id << 6);
1205 	hdr[4] = pn[3];
1206 	hdr[5] = pn[2];
1207 	hdr[6] = pn[1];
1208 	hdr[7] = pn[0];
1209 
1210 	status->flag &= ~RX_FLAG_IV_STRIPPED;
1211 }
1212 EXPORT_SYMBOL_GPL(mt76_insert_ccmp_hdr);
1213 
1214 int mt76_get_rate(struct mt76_dev *dev,
1215 		  struct ieee80211_supported_band *sband,
1216 		  int idx, bool cck)
1217 {
1218 	int i, offset = 0, len = sband->n_bitrates;
1219 
1220 	if (cck) {
1221 		if (sband == &dev->phy.sband_5g.sband)
1222 			return 0;
1223 
1224 		idx &= ~BIT(2); /* short preamble */
1225 	} else if (sband == &dev->phy.sband_2g.sband) {
1226 		offset = 4;
1227 	}
1228 
1229 	for (i = offset; i < len; i++) {
1230 		if ((sband->bitrates[i].hw_value & GENMASK(7, 0)) == idx)
1231 			return i;
1232 	}
1233 
1234 	return 0;
1235 }
1236 EXPORT_SYMBOL_GPL(mt76_get_rate);
1237 
1238 void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1239 		  const u8 *mac)
1240 {
1241 	struct mt76_phy *phy = hw->priv;
1242 
1243 	set_bit(MT76_SCANNING, &phy->state);
1244 }
1245 EXPORT_SYMBOL_GPL(mt76_sw_scan);
1246 
1247 void mt76_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1248 {
1249 	struct mt76_phy *phy = hw->priv;
1250 
1251 	clear_bit(MT76_SCANNING, &phy->state);
1252 }
1253 EXPORT_SYMBOL_GPL(mt76_sw_scan_complete);
1254 
1255 int mt76_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
1256 {
1257 	struct mt76_phy *phy = hw->priv;
1258 	struct mt76_dev *dev = phy->dev;
1259 
1260 	mutex_lock(&dev->mutex);
1261 	*tx_ant = phy->antenna_mask;
1262 	*rx_ant = phy->antenna_mask;
1263 	mutex_unlock(&dev->mutex);
1264 
1265 	return 0;
1266 }
1267 EXPORT_SYMBOL_GPL(mt76_get_antenna);
1268 
1269 struct mt76_queue *
1270 mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc,
1271 		int ring_base)
1272 {
1273 	struct mt76_queue *hwq;
1274 	int err;
1275 
1276 	hwq = devm_kzalloc(dev->dev, sizeof(*hwq), GFP_KERNEL);
1277 	if (!hwq)
1278 		return ERR_PTR(-ENOMEM);
1279 
1280 	err = dev->queue_ops->alloc(dev, hwq, idx, n_desc, 0, ring_base);
1281 	if (err < 0)
1282 		return ERR_PTR(err);
1283 
1284 	return hwq;
1285 }
1286 EXPORT_SYMBOL_GPL(mt76_init_queue);
1287