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 #define CHAN6G(_idx, _freq) {			\
24 	.band = NL80211_BAND_6GHZ,		\
25 	.center_freq = (_freq),			\
26 	.hw_value = (_idx),			\
27 	.max_power = 30,			\
28 }
29 
30 static const struct ieee80211_channel mt76_channels_2ghz[] = {
31 	CHAN2G(1, 2412),
32 	CHAN2G(2, 2417),
33 	CHAN2G(3, 2422),
34 	CHAN2G(4, 2427),
35 	CHAN2G(5, 2432),
36 	CHAN2G(6, 2437),
37 	CHAN2G(7, 2442),
38 	CHAN2G(8, 2447),
39 	CHAN2G(9, 2452),
40 	CHAN2G(10, 2457),
41 	CHAN2G(11, 2462),
42 	CHAN2G(12, 2467),
43 	CHAN2G(13, 2472),
44 	CHAN2G(14, 2484),
45 };
46 
47 static const struct ieee80211_channel mt76_channels_5ghz[] = {
48 	CHAN5G(36, 5180),
49 	CHAN5G(40, 5200),
50 	CHAN5G(44, 5220),
51 	CHAN5G(48, 5240),
52 
53 	CHAN5G(52, 5260),
54 	CHAN5G(56, 5280),
55 	CHAN5G(60, 5300),
56 	CHAN5G(64, 5320),
57 
58 	CHAN5G(100, 5500),
59 	CHAN5G(104, 5520),
60 	CHAN5G(108, 5540),
61 	CHAN5G(112, 5560),
62 	CHAN5G(116, 5580),
63 	CHAN5G(120, 5600),
64 	CHAN5G(124, 5620),
65 	CHAN5G(128, 5640),
66 	CHAN5G(132, 5660),
67 	CHAN5G(136, 5680),
68 	CHAN5G(140, 5700),
69 	CHAN5G(144, 5720),
70 
71 	CHAN5G(149, 5745),
72 	CHAN5G(153, 5765),
73 	CHAN5G(157, 5785),
74 	CHAN5G(161, 5805),
75 	CHAN5G(165, 5825),
76 	CHAN5G(169, 5845),
77 	CHAN5G(173, 5865),
78 };
79 
80 static const struct ieee80211_channel mt76_channels_6ghz[] = {
81 	/* UNII-5 */
82 	CHAN6G(1, 5955),
83 	CHAN6G(5, 5975),
84 	CHAN6G(9, 5995),
85 	CHAN6G(13, 6015),
86 	CHAN6G(17, 6035),
87 	CHAN6G(21, 6055),
88 	CHAN6G(25, 6075),
89 	CHAN6G(29, 6095),
90 	CHAN6G(33, 6115),
91 	CHAN6G(37, 6135),
92 	CHAN6G(41, 6155),
93 	CHAN6G(45, 6175),
94 	CHAN6G(49, 6195),
95 	CHAN6G(53, 6215),
96 	CHAN6G(57, 6235),
97 	CHAN6G(61, 6255),
98 	CHAN6G(65, 6275),
99 	CHAN6G(69, 6295),
100 	CHAN6G(73, 6315),
101 	CHAN6G(77, 6335),
102 	CHAN6G(81, 6355),
103 	CHAN6G(85, 6375),
104 	CHAN6G(89, 6395),
105 	CHAN6G(93, 6415),
106 	/* UNII-6 */
107 	CHAN6G(97, 6435),
108 	CHAN6G(101, 6455),
109 	CHAN6G(105, 6475),
110 	CHAN6G(109, 6495),
111 	CHAN6G(113, 6515),
112 	CHAN6G(117, 6535),
113 	/* UNII-7 */
114 	CHAN6G(121, 6555),
115 	CHAN6G(125, 6575),
116 	CHAN6G(129, 6595),
117 	CHAN6G(133, 6615),
118 	CHAN6G(137, 6635),
119 	CHAN6G(141, 6655),
120 	CHAN6G(145, 6675),
121 	CHAN6G(149, 6695),
122 	CHAN6G(153, 6715),
123 	CHAN6G(157, 6735),
124 	CHAN6G(161, 6755),
125 	CHAN6G(165, 6775),
126 	CHAN6G(169, 6795),
127 	CHAN6G(173, 6815),
128 	CHAN6G(177, 6835),
129 	CHAN6G(181, 6855),
130 	CHAN6G(185, 6875),
131 	/* UNII-8 */
132 	CHAN6G(189, 6895),
133 	CHAN6G(193, 6915),
134 	CHAN6G(197, 6935),
135 	CHAN6G(201, 6955),
136 	CHAN6G(205, 6975),
137 	CHAN6G(209, 6995),
138 	CHAN6G(213, 7015),
139 	CHAN6G(217, 7035),
140 	CHAN6G(221, 7055),
141 	CHAN6G(225, 7075),
142 	CHAN6G(229, 7095),
143 	CHAN6G(233, 7115),
144 };
145 
146 static const struct ieee80211_tpt_blink mt76_tpt_blink[] = {
147 	{ .throughput =   0 * 1024, .blink_time = 334 },
148 	{ .throughput =   1 * 1024, .blink_time = 260 },
149 	{ .throughput =   5 * 1024, .blink_time = 220 },
150 	{ .throughput =  10 * 1024, .blink_time = 190 },
151 	{ .throughput =  20 * 1024, .blink_time = 170 },
152 	{ .throughput =  50 * 1024, .blink_time = 150 },
153 	{ .throughput =  70 * 1024, .blink_time = 130 },
154 	{ .throughput = 100 * 1024, .blink_time = 110 },
155 	{ .throughput = 200 * 1024, .blink_time =  80 },
156 	{ .throughput = 300 * 1024, .blink_time =  50 },
157 };
158 
159 struct ieee80211_rate mt76_rates[] = {
160 	CCK_RATE(0, 10),
161 	CCK_RATE(1, 20),
162 	CCK_RATE(2, 55),
163 	CCK_RATE(3, 110),
164 	OFDM_RATE(11, 60),
165 	OFDM_RATE(15, 90),
166 	OFDM_RATE(10, 120),
167 	OFDM_RATE(14, 180),
168 	OFDM_RATE(9,  240),
169 	OFDM_RATE(13, 360),
170 	OFDM_RATE(8,  480),
171 	OFDM_RATE(12, 540),
172 };
173 EXPORT_SYMBOL_GPL(mt76_rates);
174 
175 static const struct cfg80211_sar_freq_ranges mt76_sar_freq_ranges[] = {
176 	{ .start_freq = 2402, .end_freq = 2494, },
177 	{ .start_freq = 5150, .end_freq = 5350, },
178 	{ .start_freq = 5350, .end_freq = 5470, },
179 	{ .start_freq = 5470, .end_freq = 5725, },
180 	{ .start_freq = 5725, .end_freq = 5950, },
181 	{ .start_freq = 5945, .end_freq = 6165, },
182 	{ .start_freq = 6165, .end_freq = 6405, },
183 	{ .start_freq = 6405, .end_freq = 6525, },
184 	{ .start_freq = 6525, .end_freq = 6705, },
185 	{ .start_freq = 6705, .end_freq = 6865, },
186 	{ .start_freq = 6865, .end_freq = 7125, },
187 };
188 
189 static const struct cfg80211_sar_capa mt76_sar_capa = {
190 	.type = NL80211_SAR_TYPE_POWER,
191 	.num_freq_ranges = ARRAY_SIZE(mt76_sar_freq_ranges),
192 	.freq_ranges = &mt76_sar_freq_ranges[0],
193 };
194 
195 static int mt76_led_init(struct mt76_dev *dev)
196 {
197 	struct device_node *np = dev->dev->of_node;
198 	struct ieee80211_hw *hw = dev->hw;
199 	int led_pin;
200 
201 	if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
202 		return 0;
203 
204 	snprintf(dev->led_name, sizeof(dev->led_name),
205 		 "mt76-%s", wiphy_name(hw->wiphy));
206 
207 	dev->led_cdev.name = dev->led_name;
208 	dev->led_cdev.default_trigger =
209 		ieee80211_create_tpt_led_trigger(hw,
210 					IEEE80211_TPT_LEDTRIG_FL_RADIO,
211 					mt76_tpt_blink,
212 					ARRAY_SIZE(mt76_tpt_blink));
213 
214 	np = of_get_child_by_name(np, "led");
215 	if (np) {
216 		if (!of_property_read_u32(np, "led-sources", &led_pin))
217 			dev->led_pin = led_pin;
218 		dev->led_al = of_property_read_bool(np, "led-active-low");
219 		of_node_put(np);
220 	}
221 
222 	return led_classdev_register(dev->dev, &dev->led_cdev);
223 }
224 
225 static void mt76_led_cleanup(struct mt76_dev *dev)
226 {
227 	if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
228 		return;
229 
230 	led_classdev_unregister(&dev->led_cdev);
231 }
232 
233 static void mt76_init_stream_cap(struct mt76_phy *phy,
234 				 struct ieee80211_supported_band *sband,
235 				 bool vht)
236 {
237 	struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
238 	int i, nstream = hweight8(phy->antenna_mask);
239 	struct ieee80211_sta_vht_cap *vht_cap;
240 	u16 mcs_map = 0;
241 
242 	if (nstream > 1)
243 		ht_cap->cap |= IEEE80211_HT_CAP_TX_STBC;
244 	else
245 		ht_cap->cap &= ~IEEE80211_HT_CAP_TX_STBC;
246 
247 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
248 		ht_cap->mcs.rx_mask[i] = i < nstream ? 0xff : 0;
249 
250 	if (!vht)
251 		return;
252 
253 	vht_cap = &sband->vht_cap;
254 	if (nstream > 1)
255 		vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
256 	else
257 		vht_cap->cap &= ~IEEE80211_VHT_CAP_TXSTBC;
258 	vht_cap->cap |= IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
259 			IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN;
260 
261 	for (i = 0; i < 8; i++) {
262 		if (i < nstream)
263 			mcs_map |= (IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2));
264 		else
265 			mcs_map |=
266 				(IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2));
267 	}
268 	vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
269 	vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
270 	vht_cap->vht_mcs.tx_highest |=
271 				cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
272 }
273 
274 void mt76_set_stream_caps(struct mt76_phy *phy, bool vht)
275 {
276 	if (phy->cap.has_2ghz)
277 		mt76_init_stream_cap(phy, &phy->sband_2g.sband, false);
278 	if (phy->cap.has_5ghz)
279 		mt76_init_stream_cap(phy, &phy->sband_5g.sband, vht);
280 	if (phy->cap.has_6ghz)
281 		mt76_init_stream_cap(phy, &phy->sband_6g.sband, vht);
282 }
283 EXPORT_SYMBOL_GPL(mt76_set_stream_caps);
284 
285 static int
286 mt76_init_sband(struct mt76_phy *phy, struct mt76_sband *msband,
287 		const struct ieee80211_channel *chan, int n_chan,
288 		struct ieee80211_rate *rates, int n_rates,
289 		bool ht, bool vht)
290 {
291 	struct ieee80211_supported_band *sband = &msband->sband;
292 	struct ieee80211_sta_vht_cap *vht_cap;
293 	struct ieee80211_sta_ht_cap *ht_cap;
294 	struct mt76_dev *dev = phy->dev;
295 	void *chanlist;
296 	int size;
297 
298 	size = n_chan * sizeof(*chan);
299 	chanlist = devm_kmemdup(dev->dev, chan, size, GFP_KERNEL);
300 	if (!chanlist)
301 		return -ENOMEM;
302 
303 	msband->chan = devm_kcalloc(dev->dev, n_chan, sizeof(*msband->chan),
304 				    GFP_KERNEL);
305 	if (!msband->chan)
306 		return -ENOMEM;
307 
308 	sband->channels = chanlist;
309 	sband->n_channels = n_chan;
310 	sband->bitrates = rates;
311 	sband->n_bitrates = n_rates;
312 
313 	if (!ht)
314 		return 0;
315 
316 	ht_cap = &sband->ht_cap;
317 	ht_cap->ht_supported = true;
318 	ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
319 		       IEEE80211_HT_CAP_GRN_FLD |
320 		       IEEE80211_HT_CAP_SGI_20 |
321 		       IEEE80211_HT_CAP_SGI_40 |
322 		       (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
323 
324 	ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
325 	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
326 
327 	mt76_init_stream_cap(phy, sband, vht);
328 
329 	if (!vht)
330 		return 0;
331 
332 	vht_cap = &sband->vht_cap;
333 	vht_cap->vht_supported = true;
334 	vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC |
335 			IEEE80211_VHT_CAP_RXSTBC_1 |
336 			IEEE80211_VHT_CAP_SHORT_GI_80 |
337 			(3 << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT);
338 
339 	return 0;
340 }
341 
342 static int
343 mt76_init_sband_2g(struct mt76_phy *phy, struct ieee80211_rate *rates,
344 		   int n_rates)
345 {
346 	phy->hw->wiphy->bands[NL80211_BAND_2GHZ] = &phy->sband_2g.sband;
347 
348 	return mt76_init_sband(phy, &phy->sband_2g, mt76_channels_2ghz,
349 			       ARRAY_SIZE(mt76_channels_2ghz), rates,
350 			       n_rates, true, false);
351 }
352 
353 static int
354 mt76_init_sband_5g(struct mt76_phy *phy, struct ieee80211_rate *rates,
355 		   int n_rates, bool vht)
356 {
357 	phy->hw->wiphy->bands[NL80211_BAND_5GHZ] = &phy->sband_5g.sband;
358 
359 	return mt76_init_sband(phy, &phy->sband_5g, mt76_channels_5ghz,
360 			       ARRAY_SIZE(mt76_channels_5ghz), rates,
361 			       n_rates, true, vht);
362 }
363 
364 static int
365 mt76_init_sband_6g(struct mt76_phy *phy, struct ieee80211_rate *rates,
366 		   int n_rates)
367 {
368 	phy->hw->wiphy->bands[NL80211_BAND_6GHZ] = &phy->sband_6g.sband;
369 
370 	return mt76_init_sband(phy, &phy->sband_6g, mt76_channels_6ghz,
371 			       ARRAY_SIZE(mt76_channels_6ghz), rates,
372 			       n_rates, false, false);
373 }
374 
375 static void
376 mt76_check_sband(struct mt76_phy *phy, struct mt76_sband *msband,
377 		 enum nl80211_band band)
378 {
379 	struct ieee80211_supported_band *sband = &msband->sband;
380 	bool found = false;
381 	int i;
382 
383 	if (!sband)
384 		return;
385 
386 	for (i = 0; i < sband->n_channels; i++) {
387 		if (sband->channels[i].flags & IEEE80211_CHAN_DISABLED)
388 			continue;
389 
390 		found = true;
391 		break;
392 	}
393 
394 	if (found) {
395 		phy->chandef.chan = &sband->channels[0];
396 		phy->chan_state = &msband->chan[0];
397 		return;
398 	}
399 
400 	sband->n_channels = 0;
401 	phy->hw->wiphy->bands[band] = NULL;
402 }
403 
404 static int
405 mt76_phy_init(struct mt76_phy *phy, struct ieee80211_hw *hw)
406 {
407 	struct mt76_dev *dev = phy->dev;
408 	struct wiphy *wiphy = hw->wiphy;
409 
410 	SET_IEEE80211_DEV(hw, dev->dev);
411 	SET_IEEE80211_PERM_ADDR(hw, phy->macaddr);
412 
413 	wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
414 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH |
415 			WIPHY_FLAG_SUPPORTS_TDLS |
416 			WIPHY_FLAG_AP_UAPSD;
417 
418 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
419 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
420 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AQL);
421 
422 	wiphy->available_antennas_tx = phy->antenna_mask;
423 	wiphy->available_antennas_rx = phy->antenna_mask;
424 
425 	wiphy->sar_capa = &mt76_sar_capa;
426 	phy->frp = devm_kcalloc(dev->dev, wiphy->sar_capa->num_freq_ranges,
427 				sizeof(struct mt76_freq_range_power),
428 				GFP_KERNEL);
429 	if (!phy->frp)
430 		return -ENOMEM;
431 
432 	hw->txq_data_size = sizeof(struct mt76_txq);
433 	hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
434 
435 	if (!hw->max_tx_fragments)
436 		hw->max_tx_fragments = 16;
437 
438 	ieee80211_hw_set(hw, SIGNAL_DBM);
439 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
440 	ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
441 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
442 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
443 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
444 	ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
445 	ieee80211_hw_set(hw, TX_AMSDU);
446 	ieee80211_hw_set(hw, TX_FRAG_LIST);
447 	ieee80211_hw_set(hw, MFP_CAPABLE);
448 	ieee80211_hw_set(hw, AP_LINK_PS);
449 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
450 
451 	return 0;
452 }
453 
454 struct mt76_phy *
455 mt76_alloc_phy(struct mt76_dev *dev, unsigned int size,
456 	       const struct ieee80211_ops *ops, u8 band_idx)
457 {
458 	struct ieee80211_hw *hw;
459 	unsigned int phy_size;
460 	struct mt76_phy *phy;
461 
462 	phy_size = ALIGN(sizeof(*phy), 8);
463 	hw = ieee80211_alloc_hw(size + phy_size, ops);
464 	if (!hw)
465 		return NULL;
466 
467 	phy = hw->priv;
468 	phy->dev = dev;
469 	phy->hw = hw;
470 	phy->priv = hw->priv + phy_size;
471 	phy->band_idx = band_idx;
472 
473 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
474 	hw->wiphy->interface_modes =
475 		BIT(NL80211_IFTYPE_STATION) |
476 		BIT(NL80211_IFTYPE_AP) |
477 #ifdef CONFIG_MAC80211_MESH
478 		BIT(NL80211_IFTYPE_MESH_POINT) |
479 #endif
480 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
481 		BIT(NL80211_IFTYPE_P2P_GO) |
482 		BIT(NL80211_IFTYPE_ADHOC);
483 
484 	return phy;
485 }
486 EXPORT_SYMBOL_GPL(mt76_alloc_phy);
487 
488 int mt76_register_phy(struct mt76_phy *phy, bool vht,
489 		      struct ieee80211_rate *rates, int n_rates)
490 {
491 	int ret;
492 
493 	ret = mt76_phy_init(phy, phy->hw);
494 	if (ret)
495 		return ret;
496 
497 	if (phy->cap.has_2ghz) {
498 		ret = mt76_init_sband_2g(phy, rates, n_rates);
499 		if (ret)
500 			return ret;
501 	}
502 
503 	if (phy->cap.has_5ghz) {
504 		ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht);
505 		if (ret)
506 			return ret;
507 	}
508 
509 	if (phy->cap.has_6ghz) {
510 		ret = mt76_init_sband_6g(phy, rates + 4, n_rates - 4);
511 		if (ret)
512 			return ret;
513 	}
514 
515 	wiphy_read_of_freq_limits(phy->hw->wiphy);
516 	mt76_check_sband(phy, &phy->sband_2g, NL80211_BAND_2GHZ);
517 	mt76_check_sband(phy, &phy->sband_5g, NL80211_BAND_5GHZ);
518 	mt76_check_sband(phy, &phy->sband_6g, NL80211_BAND_6GHZ);
519 
520 	ret = ieee80211_register_hw(phy->hw);
521 	if (ret)
522 		return ret;
523 
524 	phy->dev->phys[phy->band_idx] = phy;
525 
526 	return 0;
527 }
528 EXPORT_SYMBOL_GPL(mt76_register_phy);
529 
530 void mt76_unregister_phy(struct mt76_phy *phy)
531 {
532 	struct mt76_dev *dev = phy->dev;
533 
534 	mt76_tx_status_check(dev, true);
535 	ieee80211_unregister_hw(phy->hw);
536 	dev->phys[phy->band_idx] = NULL;
537 }
538 EXPORT_SYMBOL_GPL(mt76_unregister_phy);
539 
540 struct mt76_dev *
541 mt76_alloc_device(struct device *pdev, unsigned int size,
542 		  const struct ieee80211_ops *ops,
543 		  const struct mt76_driver_ops *drv_ops)
544 {
545 	struct ieee80211_hw *hw;
546 	struct mt76_phy *phy;
547 	struct mt76_dev *dev;
548 	int i;
549 
550 	hw = ieee80211_alloc_hw(size, ops);
551 	if (!hw)
552 		return NULL;
553 
554 	dev = hw->priv;
555 	dev->hw = hw;
556 	dev->dev = pdev;
557 	dev->drv = drv_ops;
558 	dev->dma_dev = pdev;
559 
560 	phy = &dev->phy;
561 	phy->dev = dev;
562 	phy->hw = hw;
563 	phy->band_idx = MT_BAND0;
564 	dev->phys[phy->band_idx] = phy;
565 
566 	spin_lock_init(&dev->rx_lock);
567 	spin_lock_init(&dev->lock);
568 	spin_lock_init(&dev->cc_lock);
569 	spin_lock_init(&dev->status_lock);
570 	mutex_init(&dev->mutex);
571 	init_waitqueue_head(&dev->tx_wait);
572 
573 	skb_queue_head_init(&dev->mcu.res_q);
574 	init_waitqueue_head(&dev->mcu.wait);
575 	mutex_init(&dev->mcu.mutex);
576 	dev->tx_worker.fn = mt76_tx_worker;
577 
578 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
579 	hw->wiphy->interface_modes =
580 		BIT(NL80211_IFTYPE_STATION) |
581 		BIT(NL80211_IFTYPE_AP) |
582 #ifdef CONFIG_MAC80211_MESH
583 		BIT(NL80211_IFTYPE_MESH_POINT) |
584 #endif
585 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
586 		BIT(NL80211_IFTYPE_P2P_GO) |
587 		BIT(NL80211_IFTYPE_ADHOC);
588 
589 	spin_lock_init(&dev->token_lock);
590 	idr_init(&dev->token);
591 
592 	INIT_LIST_HEAD(&dev->wcid_list);
593 
594 	INIT_LIST_HEAD(&dev->txwi_cache);
595 	dev->token_size = dev->drv->token_size;
596 
597 	for (i = 0; i < ARRAY_SIZE(dev->q_rx); i++)
598 		skb_queue_head_init(&dev->rx_skb[i]);
599 
600 	dev->wq = alloc_ordered_workqueue("mt76", 0);
601 	if (!dev->wq) {
602 		ieee80211_free_hw(hw);
603 		return NULL;
604 	}
605 
606 	return dev;
607 }
608 EXPORT_SYMBOL_GPL(mt76_alloc_device);
609 
610 int mt76_register_device(struct mt76_dev *dev, bool vht,
611 			 struct ieee80211_rate *rates, int n_rates)
612 {
613 	struct ieee80211_hw *hw = dev->hw;
614 	struct mt76_phy *phy = &dev->phy;
615 	int ret;
616 
617 	dev_set_drvdata(dev->dev, dev);
618 	ret = mt76_phy_init(phy, hw);
619 	if (ret)
620 		return ret;
621 
622 	if (phy->cap.has_2ghz) {
623 		ret = mt76_init_sband_2g(phy, rates, n_rates);
624 		if (ret)
625 			return ret;
626 	}
627 
628 	if (phy->cap.has_5ghz) {
629 		ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht);
630 		if (ret)
631 			return ret;
632 	}
633 
634 	if (phy->cap.has_6ghz) {
635 		ret = mt76_init_sband_6g(phy, rates + 4, n_rates - 4);
636 		if (ret)
637 			return ret;
638 	}
639 
640 	wiphy_read_of_freq_limits(hw->wiphy);
641 	mt76_check_sband(&dev->phy, &phy->sband_2g, NL80211_BAND_2GHZ);
642 	mt76_check_sband(&dev->phy, &phy->sband_5g, NL80211_BAND_5GHZ);
643 	mt76_check_sband(&dev->phy, &phy->sband_6g, NL80211_BAND_6GHZ);
644 
645 	if (IS_ENABLED(CONFIG_MT76_LEDS)) {
646 		ret = mt76_led_init(dev);
647 		if (ret)
648 			return ret;
649 	}
650 
651 	ret = ieee80211_register_hw(hw);
652 	if (ret)
653 		return ret;
654 
655 	WARN_ON(mt76_worker_setup(hw, &dev->tx_worker, NULL, "tx"));
656 	sched_set_fifo_low(dev->tx_worker.task);
657 
658 	return 0;
659 }
660 EXPORT_SYMBOL_GPL(mt76_register_device);
661 
662 void mt76_unregister_device(struct mt76_dev *dev)
663 {
664 	struct ieee80211_hw *hw = dev->hw;
665 
666 	if (IS_ENABLED(CONFIG_MT76_LEDS))
667 		mt76_led_cleanup(dev);
668 	mt76_tx_status_check(dev, true);
669 	ieee80211_unregister_hw(hw);
670 }
671 EXPORT_SYMBOL_GPL(mt76_unregister_device);
672 
673 void mt76_free_device(struct mt76_dev *dev)
674 {
675 	mt76_worker_teardown(&dev->tx_worker);
676 	if (dev->wq) {
677 		destroy_workqueue(dev->wq);
678 		dev->wq = NULL;
679 	}
680 	ieee80211_free_hw(dev->hw);
681 }
682 EXPORT_SYMBOL_GPL(mt76_free_device);
683 
684 static void mt76_rx_release_amsdu(struct mt76_phy *phy, enum mt76_rxq_id q)
685 {
686 	struct sk_buff *skb = phy->rx_amsdu[q].head;
687 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
688 	struct mt76_dev *dev = phy->dev;
689 
690 	phy->rx_amsdu[q].head = NULL;
691 	phy->rx_amsdu[q].tail = NULL;
692 
693 	/*
694 	 * Validate if the amsdu has a proper first subframe.
695 	 * A single MSDU can be parsed as A-MSDU when the unauthenticated A-MSDU
696 	 * flag of the QoS header gets flipped. In such cases, the first
697 	 * subframe has a LLC/SNAP header in the location of the destination
698 	 * address.
699 	 */
700 	if (skb_shinfo(skb)->frag_list) {
701 		int offset = 0;
702 
703 		if (!(status->flag & RX_FLAG_8023)) {
704 			offset = ieee80211_get_hdrlen_from_skb(skb);
705 
706 			if ((status->flag &
707 			     (RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED)) ==
708 			    RX_FLAG_DECRYPTED)
709 				offset += 8;
710 		}
711 
712 		if (ether_addr_equal(skb->data + offset, rfc1042_header)) {
713 			dev_kfree_skb(skb);
714 			return;
715 		}
716 	}
717 	__skb_queue_tail(&dev->rx_skb[q], skb);
718 }
719 
720 static void mt76_rx_release_burst(struct mt76_phy *phy, enum mt76_rxq_id q,
721 				  struct sk_buff *skb)
722 {
723 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
724 
725 	if (phy->rx_amsdu[q].head &&
726 	    (!status->amsdu || status->first_amsdu ||
727 	     status->seqno != phy->rx_amsdu[q].seqno))
728 		mt76_rx_release_amsdu(phy, q);
729 
730 	if (!phy->rx_amsdu[q].head) {
731 		phy->rx_amsdu[q].tail = &skb_shinfo(skb)->frag_list;
732 		phy->rx_amsdu[q].seqno = status->seqno;
733 		phy->rx_amsdu[q].head = skb;
734 	} else {
735 		*phy->rx_amsdu[q].tail = skb;
736 		phy->rx_amsdu[q].tail = &skb->next;
737 	}
738 
739 	if (!status->amsdu || status->last_amsdu)
740 		mt76_rx_release_amsdu(phy, q);
741 }
742 
743 void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb)
744 {
745 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
746 	struct mt76_phy *phy = mt76_dev_phy(dev, status->phy_idx);
747 
748 	if (!test_bit(MT76_STATE_RUNNING, &phy->state)) {
749 		dev_kfree_skb(skb);
750 		return;
751 	}
752 
753 #ifdef CONFIG_NL80211_TESTMODE
754 	if (phy->test.state == MT76_TM_STATE_RX_FRAMES) {
755 		phy->test.rx_stats.packets[q]++;
756 		if (status->flag & RX_FLAG_FAILED_FCS_CRC)
757 			phy->test.rx_stats.fcs_error[q]++;
758 	}
759 #endif
760 
761 	mt76_rx_release_burst(phy, q, skb);
762 }
763 EXPORT_SYMBOL_GPL(mt76_rx);
764 
765 bool mt76_has_tx_pending(struct mt76_phy *phy)
766 {
767 	struct mt76_queue *q;
768 	int i;
769 
770 	for (i = 0; i < __MT_TXQ_MAX; i++) {
771 		q = phy->q_tx[i];
772 		if (q && q->queued)
773 			return true;
774 	}
775 
776 	return false;
777 }
778 EXPORT_SYMBOL_GPL(mt76_has_tx_pending);
779 
780 static struct mt76_channel_state *
781 mt76_channel_state(struct mt76_phy *phy, struct ieee80211_channel *c)
782 {
783 	struct mt76_sband *msband;
784 	int idx;
785 
786 	if (c->band == NL80211_BAND_2GHZ)
787 		msband = &phy->sband_2g;
788 	else if (c->band == NL80211_BAND_6GHZ)
789 		msband = &phy->sband_6g;
790 	else
791 		msband = &phy->sband_5g;
792 
793 	idx = c - &msband->sband.channels[0];
794 	return &msband->chan[idx];
795 }
796 
797 void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time)
798 {
799 	struct mt76_channel_state *state = phy->chan_state;
800 
801 	state->cc_active += ktime_to_us(ktime_sub(time,
802 						  phy->survey_time));
803 	phy->survey_time = time;
804 }
805 EXPORT_SYMBOL_GPL(mt76_update_survey_active_time);
806 
807 void mt76_update_survey(struct mt76_phy *phy)
808 {
809 	struct mt76_dev *dev = phy->dev;
810 	ktime_t cur_time;
811 
812 	if (dev->drv->update_survey)
813 		dev->drv->update_survey(phy);
814 
815 	cur_time = ktime_get_boottime();
816 	mt76_update_survey_active_time(phy, cur_time);
817 
818 	if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME) {
819 		struct mt76_channel_state *state = phy->chan_state;
820 
821 		spin_lock_bh(&dev->cc_lock);
822 		state->cc_bss_rx += dev->cur_cc_bss_rx;
823 		dev->cur_cc_bss_rx = 0;
824 		spin_unlock_bh(&dev->cc_lock);
825 	}
826 }
827 EXPORT_SYMBOL_GPL(mt76_update_survey);
828 
829 void mt76_set_channel(struct mt76_phy *phy)
830 {
831 	struct mt76_dev *dev = phy->dev;
832 	struct ieee80211_hw *hw = phy->hw;
833 	struct cfg80211_chan_def *chandef = &hw->conf.chandef;
834 	bool offchannel = hw->conf.flags & IEEE80211_CONF_OFFCHANNEL;
835 	int timeout = HZ / 5;
836 
837 	wait_event_timeout(dev->tx_wait, !mt76_has_tx_pending(phy), timeout);
838 	mt76_update_survey(phy);
839 
840 	if (phy->chandef.chan->center_freq != chandef->chan->center_freq ||
841 	    phy->chandef.width != chandef->width)
842 		phy->dfs_state = MT_DFS_STATE_UNKNOWN;
843 
844 	phy->chandef = *chandef;
845 	phy->chan_state = mt76_channel_state(phy, chandef->chan);
846 
847 	if (!offchannel)
848 		phy->main_chan = chandef->chan;
849 
850 	if (chandef->chan != phy->main_chan)
851 		memset(phy->chan_state, 0, sizeof(*phy->chan_state));
852 }
853 EXPORT_SYMBOL_GPL(mt76_set_channel);
854 
855 int mt76_get_survey(struct ieee80211_hw *hw, int idx,
856 		    struct survey_info *survey)
857 {
858 	struct mt76_phy *phy = hw->priv;
859 	struct mt76_dev *dev = phy->dev;
860 	struct mt76_sband *sband;
861 	struct ieee80211_channel *chan;
862 	struct mt76_channel_state *state;
863 	int ret = 0;
864 
865 	mutex_lock(&dev->mutex);
866 	if (idx == 0 && dev->drv->update_survey)
867 		mt76_update_survey(phy);
868 
869 	if (idx >= phy->sband_2g.sband.n_channels +
870 		   phy->sband_5g.sband.n_channels) {
871 		idx -= (phy->sband_2g.sband.n_channels +
872 			phy->sband_5g.sband.n_channels);
873 		sband = &phy->sband_6g;
874 	} else if (idx >= phy->sband_2g.sband.n_channels) {
875 		idx -= phy->sband_2g.sband.n_channels;
876 		sband = &phy->sband_5g;
877 	} else {
878 		sband = &phy->sband_2g;
879 	}
880 
881 	if (idx >= sband->sband.n_channels) {
882 		ret = -ENOENT;
883 		goto out;
884 	}
885 
886 	chan = &sband->sband.channels[idx];
887 	state = mt76_channel_state(phy, chan);
888 
889 	memset(survey, 0, sizeof(*survey));
890 	survey->channel = chan;
891 	survey->filled = SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY;
892 	survey->filled |= dev->drv->survey_flags;
893 	if (state->noise)
894 		survey->filled |= SURVEY_INFO_NOISE_DBM;
895 
896 	if (chan == phy->main_chan) {
897 		survey->filled |= SURVEY_INFO_IN_USE;
898 
899 		if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME)
900 			survey->filled |= SURVEY_INFO_TIME_BSS_RX;
901 	}
902 
903 	survey->time_busy = div_u64(state->cc_busy, 1000);
904 	survey->time_rx = div_u64(state->cc_rx, 1000);
905 	survey->time = div_u64(state->cc_active, 1000);
906 	survey->noise = state->noise;
907 
908 	spin_lock_bh(&dev->cc_lock);
909 	survey->time_bss_rx = div_u64(state->cc_bss_rx, 1000);
910 	survey->time_tx = div_u64(state->cc_tx, 1000);
911 	spin_unlock_bh(&dev->cc_lock);
912 
913 out:
914 	mutex_unlock(&dev->mutex);
915 
916 	return ret;
917 }
918 EXPORT_SYMBOL_GPL(mt76_get_survey);
919 
920 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
921 			 struct ieee80211_key_conf *key)
922 {
923 	struct ieee80211_key_seq seq;
924 	int i;
925 
926 	wcid->rx_check_pn = false;
927 
928 	if (!key)
929 		return;
930 
931 	if (key->cipher != WLAN_CIPHER_SUITE_CCMP)
932 		return;
933 
934 	wcid->rx_check_pn = true;
935 
936 	/* data frame */
937 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
938 		ieee80211_get_key_rx_seq(key, i, &seq);
939 		memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
940 	}
941 
942 	/* robust management frame */
943 	ieee80211_get_key_rx_seq(key, -1, &seq);
944 	memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
945 
946 }
947 EXPORT_SYMBOL(mt76_wcid_key_setup);
948 
949 static int
950 mt76_rx_signal(struct mt76_rx_status *status)
951 {
952 	s8 *chain_signal = status->chain_signal;
953 	int signal = -128;
954 	u8 chains;
955 
956 	for (chains = status->chains; chains; chains >>= 1, chain_signal++) {
957 		int cur, diff;
958 
959 		cur = *chain_signal;
960 		if (!(chains & BIT(0)) ||
961 		    cur > 0)
962 			continue;
963 
964 		if (cur > signal)
965 			swap(cur, signal);
966 
967 		diff = signal - cur;
968 		if (diff == 0)
969 			signal += 3;
970 		else if (diff <= 2)
971 			signal += 2;
972 		else if (diff <= 6)
973 			signal += 1;
974 	}
975 
976 	return signal;
977 }
978 
979 static void
980 mt76_rx_convert(struct mt76_dev *dev, struct sk_buff *skb,
981 		struct ieee80211_hw **hw,
982 		struct ieee80211_sta **sta)
983 {
984 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
985 	struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
986 	struct mt76_rx_status mstat;
987 
988 	mstat = *((struct mt76_rx_status *)skb->cb);
989 	memset(status, 0, sizeof(*status));
990 
991 	status->flag = mstat.flag;
992 	status->freq = mstat.freq;
993 	status->enc_flags = mstat.enc_flags;
994 	status->encoding = mstat.encoding;
995 	status->bw = mstat.bw;
996 	status->he_ru = mstat.he_ru;
997 	status->he_gi = mstat.he_gi;
998 	status->he_dcm = mstat.he_dcm;
999 	status->rate_idx = mstat.rate_idx;
1000 	status->nss = mstat.nss;
1001 	status->band = mstat.band;
1002 	status->signal = mstat.signal;
1003 	status->chains = mstat.chains;
1004 	status->ampdu_reference = mstat.ampdu_ref;
1005 	status->device_timestamp = mstat.timestamp;
1006 	status->mactime = mstat.timestamp;
1007 	status->signal = mt76_rx_signal(&mstat);
1008 	if (status->signal <= -128)
1009 		status->flag |= RX_FLAG_NO_SIGNAL_VAL;
1010 
1011 	if (ieee80211_is_beacon(hdr->frame_control) ||
1012 	    ieee80211_is_probe_resp(hdr->frame_control))
1013 		status->boottime_ns = ktime_get_boottime_ns();
1014 
1015 	BUILD_BUG_ON(sizeof(mstat) > sizeof(skb->cb));
1016 	BUILD_BUG_ON(sizeof(status->chain_signal) !=
1017 		     sizeof(mstat.chain_signal));
1018 	memcpy(status->chain_signal, mstat.chain_signal,
1019 	       sizeof(mstat.chain_signal));
1020 
1021 	*sta = wcid_to_sta(mstat.wcid);
1022 	*hw = mt76_phy_hw(dev, mstat.phy_idx);
1023 }
1024 
1025 static void
1026 mt76_check_ccmp_pn(struct sk_buff *skb)
1027 {
1028 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1029 	struct mt76_wcid *wcid = status->wcid;
1030 	struct ieee80211_hdr *hdr;
1031 	int security_idx;
1032 	int ret;
1033 
1034 	if (!(status->flag & RX_FLAG_DECRYPTED))
1035 		return;
1036 
1037 	if (status->flag & RX_FLAG_ONLY_MONITOR)
1038 		return;
1039 
1040 	if (!wcid || !wcid->rx_check_pn)
1041 		return;
1042 
1043 	security_idx = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
1044 	if (status->flag & RX_FLAG_8023)
1045 		goto skip_hdr_check;
1046 
1047 	hdr = mt76_skb_get_hdr(skb);
1048 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1049 		/*
1050 		 * Validate the first fragment both here and in mac80211
1051 		 * All further fragments will be validated by mac80211 only.
1052 		 */
1053 		if (ieee80211_is_frag(hdr) &&
1054 		    !ieee80211_is_first_frag(hdr->frame_control))
1055 			return;
1056 	}
1057 
1058 	/* IEEE 802.11-2020, 12.5.3.4.4 "PN and replay detection" c):
1059 	 *
1060 	 * the recipient shall maintain a single replay counter for received
1061 	 * individually addressed robust Management frames that are received
1062 	 * with the To DS subfield equal to 0, [...]
1063 	 */
1064 	if (ieee80211_is_mgmt(hdr->frame_control) &&
1065 	    !ieee80211_has_tods(hdr->frame_control))
1066 		security_idx = IEEE80211_NUM_TIDS;
1067 
1068 skip_hdr_check:
1069 	BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0]));
1070 	ret = memcmp(status->iv, wcid->rx_key_pn[security_idx],
1071 		     sizeof(status->iv));
1072 	if (ret <= 0) {
1073 		status->flag |= RX_FLAG_ONLY_MONITOR;
1074 		return;
1075 	}
1076 
1077 	memcpy(wcid->rx_key_pn[security_idx], status->iv, sizeof(status->iv));
1078 
1079 	if (status->flag & RX_FLAG_IV_STRIPPED)
1080 		status->flag |= RX_FLAG_PN_VALIDATED;
1081 }
1082 
1083 static void
1084 mt76_airtime_report(struct mt76_dev *dev, struct mt76_rx_status *status,
1085 		    int len)
1086 {
1087 	struct mt76_wcid *wcid = status->wcid;
1088 	struct ieee80211_rx_status info = {
1089 		.enc_flags = status->enc_flags,
1090 		.rate_idx = status->rate_idx,
1091 		.encoding = status->encoding,
1092 		.band = status->band,
1093 		.nss = status->nss,
1094 		.bw = status->bw,
1095 	};
1096 	struct ieee80211_sta *sta;
1097 	u32 airtime;
1098 	u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
1099 
1100 	airtime = ieee80211_calc_rx_airtime(dev->hw, &info, len);
1101 	spin_lock(&dev->cc_lock);
1102 	dev->cur_cc_bss_rx += airtime;
1103 	spin_unlock(&dev->cc_lock);
1104 
1105 	if (!wcid || !wcid->sta)
1106 		return;
1107 
1108 	sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
1109 	ieee80211_sta_register_airtime(sta, tidno, 0, airtime);
1110 }
1111 
1112 static void
1113 mt76_airtime_flush_ampdu(struct mt76_dev *dev)
1114 {
1115 	struct mt76_wcid *wcid;
1116 	int wcid_idx;
1117 
1118 	if (!dev->rx_ampdu_len)
1119 		return;
1120 
1121 	wcid_idx = dev->rx_ampdu_status.wcid_idx;
1122 	if (wcid_idx < ARRAY_SIZE(dev->wcid))
1123 		wcid = rcu_dereference(dev->wcid[wcid_idx]);
1124 	else
1125 		wcid = NULL;
1126 	dev->rx_ampdu_status.wcid = wcid;
1127 
1128 	mt76_airtime_report(dev, &dev->rx_ampdu_status, dev->rx_ampdu_len);
1129 
1130 	dev->rx_ampdu_len = 0;
1131 	dev->rx_ampdu_ref = 0;
1132 }
1133 
1134 static void
1135 mt76_airtime_check(struct mt76_dev *dev, struct sk_buff *skb)
1136 {
1137 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1138 	struct mt76_wcid *wcid = status->wcid;
1139 
1140 	if (!(dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME))
1141 		return;
1142 
1143 	if (!wcid || !wcid->sta) {
1144 		struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
1145 
1146 		if (status->flag & RX_FLAG_8023)
1147 			return;
1148 
1149 		if (!ether_addr_equal(hdr->addr1, dev->phy.macaddr))
1150 			return;
1151 
1152 		wcid = NULL;
1153 	}
1154 
1155 	if (!(status->flag & RX_FLAG_AMPDU_DETAILS) ||
1156 	    status->ampdu_ref != dev->rx_ampdu_ref)
1157 		mt76_airtime_flush_ampdu(dev);
1158 
1159 	if (status->flag & RX_FLAG_AMPDU_DETAILS) {
1160 		if (!dev->rx_ampdu_len ||
1161 		    status->ampdu_ref != dev->rx_ampdu_ref) {
1162 			dev->rx_ampdu_status = *status;
1163 			dev->rx_ampdu_status.wcid_idx = wcid ? wcid->idx : 0xff;
1164 			dev->rx_ampdu_ref = status->ampdu_ref;
1165 		}
1166 
1167 		dev->rx_ampdu_len += skb->len;
1168 		return;
1169 	}
1170 
1171 	mt76_airtime_report(dev, status, skb->len);
1172 }
1173 
1174 static void
1175 mt76_check_sta(struct mt76_dev *dev, struct sk_buff *skb)
1176 {
1177 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1178 	struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
1179 	struct ieee80211_sta *sta;
1180 	struct ieee80211_hw *hw;
1181 	struct mt76_wcid *wcid = status->wcid;
1182 	u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
1183 	bool ps;
1184 
1185 	hw = mt76_phy_hw(dev, status->phy_idx);
1186 	if (ieee80211_is_pspoll(hdr->frame_control) && !wcid &&
1187 	    !(status->flag & RX_FLAG_8023)) {
1188 		sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr2, NULL);
1189 		if (sta)
1190 			wcid = status->wcid = (struct mt76_wcid *)sta->drv_priv;
1191 	}
1192 
1193 	mt76_airtime_check(dev, skb);
1194 
1195 	if (!wcid || !wcid->sta)
1196 		return;
1197 
1198 	sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
1199 
1200 	if (status->signal <= 0)
1201 		ewma_signal_add(&wcid->rssi, -status->signal);
1202 
1203 	wcid->inactive_count = 0;
1204 
1205 	if (status->flag & RX_FLAG_8023)
1206 		return;
1207 
1208 	if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags))
1209 		return;
1210 
1211 	if (ieee80211_is_pspoll(hdr->frame_control)) {
1212 		ieee80211_sta_pspoll(sta);
1213 		return;
1214 	}
1215 
1216 	if (ieee80211_has_morefrags(hdr->frame_control) ||
1217 	    !(ieee80211_is_mgmt(hdr->frame_control) ||
1218 	      ieee80211_is_data(hdr->frame_control)))
1219 		return;
1220 
1221 	ps = ieee80211_has_pm(hdr->frame_control);
1222 
1223 	if (ps && (ieee80211_is_data_qos(hdr->frame_control) ||
1224 		   ieee80211_is_qos_nullfunc(hdr->frame_control)))
1225 		ieee80211_sta_uapsd_trigger(sta, tidno);
1226 
1227 	if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps)
1228 		return;
1229 
1230 	if (ps)
1231 		set_bit(MT_WCID_FLAG_PS, &wcid->flags);
1232 
1233 	dev->drv->sta_ps(dev, sta, ps);
1234 
1235 	if (!ps)
1236 		clear_bit(MT_WCID_FLAG_PS, &wcid->flags);
1237 
1238 	ieee80211_sta_ps_transition(sta, ps);
1239 }
1240 
1241 void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
1242 		      struct napi_struct *napi)
1243 {
1244 	struct ieee80211_sta *sta;
1245 	struct ieee80211_hw *hw;
1246 	struct sk_buff *skb, *tmp;
1247 	LIST_HEAD(list);
1248 
1249 	spin_lock(&dev->rx_lock);
1250 	while ((skb = __skb_dequeue(frames)) != NULL) {
1251 		struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1252 
1253 		mt76_check_ccmp_pn(skb);
1254 		skb_shinfo(skb)->frag_list = NULL;
1255 		mt76_rx_convert(dev, skb, &hw, &sta);
1256 		ieee80211_rx_list(hw, sta, skb, &list);
1257 
1258 		/* subsequent amsdu frames */
1259 		while (nskb) {
1260 			skb = nskb;
1261 			nskb = nskb->next;
1262 			skb->next = NULL;
1263 
1264 			mt76_rx_convert(dev, skb, &hw, &sta);
1265 			ieee80211_rx_list(hw, sta, skb, &list);
1266 		}
1267 	}
1268 	spin_unlock(&dev->rx_lock);
1269 
1270 	if (!napi) {
1271 		netif_receive_skb_list(&list);
1272 		return;
1273 	}
1274 
1275 	list_for_each_entry_safe(skb, tmp, &list, list) {
1276 		skb_list_del_init(skb);
1277 		napi_gro_receive(napi, skb);
1278 	}
1279 }
1280 
1281 void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
1282 			   struct napi_struct *napi)
1283 {
1284 	struct sk_buff_head frames;
1285 	struct sk_buff *skb;
1286 
1287 	__skb_queue_head_init(&frames);
1288 
1289 	while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) {
1290 		mt76_check_sta(dev, skb);
1291 		mt76_rx_aggr_reorder(skb, &frames);
1292 	}
1293 
1294 	mt76_rx_complete(dev, &frames, napi);
1295 }
1296 EXPORT_SYMBOL_GPL(mt76_rx_poll_complete);
1297 
1298 static int
1299 mt76_sta_add(struct mt76_phy *phy, struct ieee80211_vif *vif,
1300 	     struct ieee80211_sta *sta)
1301 {
1302 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1303 	struct mt76_dev *dev = phy->dev;
1304 	int ret;
1305 	int i;
1306 
1307 	mutex_lock(&dev->mutex);
1308 
1309 	ret = dev->drv->sta_add(dev, vif, sta);
1310 	if (ret)
1311 		goto out;
1312 
1313 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1314 		struct mt76_txq *mtxq;
1315 
1316 		if (!sta->txq[i])
1317 			continue;
1318 
1319 		mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv;
1320 		mtxq->wcid = wcid->idx;
1321 	}
1322 
1323 	ewma_signal_init(&wcid->rssi);
1324 	if (phy->band_idx == MT_BAND1)
1325 		mt76_wcid_mask_set(dev->wcid_phy_mask, wcid->idx);
1326 	wcid->phy_idx = phy->band_idx;
1327 	rcu_assign_pointer(dev->wcid[wcid->idx], wcid);
1328 
1329 	mt76_packet_id_init(wcid);
1330 out:
1331 	mutex_unlock(&dev->mutex);
1332 
1333 	return ret;
1334 }
1335 
1336 void __mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
1337 		       struct ieee80211_sta *sta)
1338 {
1339 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1340 	int i, idx = wcid->idx;
1341 
1342 	for (i = 0; i < ARRAY_SIZE(wcid->aggr); i++)
1343 		mt76_rx_aggr_stop(dev, wcid, i);
1344 
1345 	if (dev->drv->sta_remove)
1346 		dev->drv->sta_remove(dev, vif, sta);
1347 
1348 	mt76_packet_id_flush(dev, wcid);
1349 
1350 	mt76_wcid_mask_clear(dev->wcid_mask, idx);
1351 	mt76_wcid_mask_clear(dev->wcid_phy_mask, idx);
1352 }
1353 EXPORT_SYMBOL_GPL(__mt76_sta_remove);
1354 
1355 static void
1356 mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
1357 		struct ieee80211_sta *sta)
1358 {
1359 	mutex_lock(&dev->mutex);
1360 	__mt76_sta_remove(dev, vif, sta);
1361 	mutex_unlock(&dev->mutex);
1362 }
1363 
1364 int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1365 		   struct ieee80211_sta *sta,
1366 		   enum ieee80211_sta_state old_state,
1367 		   enum ieee80211_sta_state new_state)
1368 {
1369 	struct mt76_phy *phy = hw->priv;
1370 	struct mt76_dev *dev = phy->dev;
1371 
1372 	if (old_state == IEEE80211_STA_NOTEXIST &&
1373 	    new_state == IEEE80211_STA_NONE)
1374 		return mt76_sta_add(phy, vif, sta);
1375 
1376 	if (old_state == IEEE80211_STA_AUTH &&
1377 	    new_state == IEEE80211_STA_ASSOC &&
1378 	    dev->drv->sta_assoc)
1379 		dev->drv->sta_assoc(dev, vif, sta);
1380 
1381 	if (old_state == IEEE80211_STA_NONE &&
1382 	    new_state == IEEE80211_STA_NOTEXIST)
1383 		mt76_sta_remove(dev, vif, sta);
1384 
1385 	return 0;
1386 }
1387 EXPORT_SYMBOL_GPL(mt76_sta_state);
1388 
1389 void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1390 			     struct ieee80211_sta *sta)
1391 {
1392 	struct mt76_phy *phy = hw->priv;
1393 	struct mt76_dev *dev = phy->dev;
1394 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1395 
1396 	mutex_lock(&dev->mutex);
1397 	spin_lock_bh(&dev->status_lock);
1398 	rcu_assign_pointer(dev->wcid[wcid->idx], NULL);
1399 	spin_unlock_bh(&dev->status_lock);
1400 	mutex_unlock(&dev->mutex);
1401 }
1402 EXPORT_SYMBOL_GPL(mt76_sta_pre_rcu_remove);
1403 
1404 int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1405 		     int *dbm)
1406 {
1407 	struct mt76_phy *phy = hw->priv;
1408 	int n_chains = hweight8(phy->antenna_mask);
1409 	int delta = mt76_tx_power_nss_delta(n_chains);
1410 
1411 	*dbm = DIV_ROUND_UP(phy->txpower_cur + delta, 2);
1412 
1413 	return 0;
1414 }
1415 EXPORT_SYMBOL_GPL(mt76_get_txpower);
1416 
1417 int mt76_init_sar_power(struct ieee80211_hw *hw,
1418 			const struct cfg80211_sar_specs *sar)
1419 {
1420 	struct mt76_phy *phy = hw->priv;
1421 	const struct cfg80211_sar_capa *capa = hw->wiphy->sar_capa;
1422 	int i;
1423 
1424 	if (sar->type != NL80211_SAR_TYPE_POWER || !sar->num_sub_specs)
1425 		return -EINVAL;
1426 
1427 	for (i = 0; i < sar->num_sub_specs; i++) {
1428 		u32 index = sar->sub_specs[i].freq_range_index;
1429 		/* SAR specifies power limitaton in 0.25dbm */
1430 		s32 power = sar->sub_specs[i].power >> 1;
1431 
1432 		if (power > 127 || power < -127)
1433 			power = 127;
1434 
1435 		phy->frp[index].range = &capa->freq_ranges[index];
1436 		phy->frp[index].power = power;
1437 	}
1438 
1439 	return 0;
1440 }
1441 EXPORT_SYMBOL_GPL(mt76_init_sar_power);
1442 
1443 int mt76_get_sar_power(struct mt76_phy *phy,
1444 		       struct ieee80211_channel *chan,
1445 		       int power)
1446 {
1447 	const struct cfg80211_sar_capa *capa = phy->hw->wiphy->sar_capa;
1448 	int freq, i;
1449 
1450 	if (!capa || !phy->frp)
1451 		return power;
1452 
1453 	if (power > 127 || power < -127)
1454 		power = 127;
1455 
1456 	freq = ieee80211_channel_to_frequency(chan->hw_value, chan->band);
1457 	for (i = 0 ; i < capa->num_freq_ranges; i++) {
1458 		if (phy->frp[i].range &&
1459 		    freq >= phy->frp[i].range->start_freq &&
1460 		    freq < phy->frp[i].range->end_freq) {
1461 			power = min_t(int, phy->frp[i].power, power);
1462 			break;
1463 		}
1464 	}
1465 
1466 	return power;
1467 }
1468 EXPORT_SYMBOL_GPL(mt76_get_sar_power);
1469 
1470 static void
1471 __mt76_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif)
1472 {
1473 	if (vif->bss_conf.csa_active && ieee80211_beacon_cntdwn_is_complete(vif))
1474 		ieee80211_csa_finish(vif);
1475 }
1476 
1477 void mt76_csa_finish(struct mt76_dev *dev)
1478 {
1479 	if (!dev->csa_complete)
1480 		return;
1481 
1482 	ieee80211_iterate_active_interfaces_atomic(dev->hw,
1483 		IEEE80211_IFACE_ITER_RESUME_ALL,
1484 		__mt76_csa_finish, dev);
1485 
1486 	dev->csa_complete = 0;
1487 }
1488 EXPORT_SYMBOL_GPL(mt76_csa_finish);
1489 
1490 static void
1491 __mt76_csa_check(void *priv, u8 *mac, struct ieee80211_vif *vif)
1492 {
1493 	struct mt76_dev *dev = priv;
1494 
1495 	if (!vif->bss_conf.csa_active)
1496 		return;
1497 
1498 	dev->csa_complete |= ieee80211_beacon_cntdwn_is_complete(vif);
1499 }
1500 
1501 void mt76_csa_check(struct mt76_dev *dev)
1502 {
1503 	ieee80211_iterate_active_interfaces_atomic(dev->hw,
1504 		IEEE80211_IFACE_ITER_RESUME_ALL,
1505 		__mt76_csa_check, dev);
1506 }
1507 EXPORT_SYMBOL_GPL(mt76_csa_check);
1508 
1509 int
1510 mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
1511 {
1512 	return 0;
1513 }
1514 EXPORT_SYMBOL_GPL(mt76_set_tim);
1515 
1516 void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id)
1517 {
1518 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1519 	int hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1520 	u8 *hdr, *pn = status->iv;
1521 
1522 	__skb_push(skb, 8);
1523 	memmove(skb->data, skb->data + 8, hdr_len);
1524 	hdr = skb->data + hdr_len;
1525 
1526 	hdr[0] = pn[5];
1527 	hdr[1] = pn[4];
1528 	hdr[2] = 0;
1529 	hdr[3] = 0x20 | (key_id << 6);
1530 	hdr[4] = pn[3];
1531 	hdr[5] = pn[2];
1532 	hdr[6] = pn[1];
1533 	hdr[7] = pn[0];
1534 
1535 	status->flag &= ~RX_FLAG_IV_STRIPPED;
1536 }
1537 EXPORT_SYMBOL_GPL(mt76_insert_ccmp_hdr);
1538 
1539 int mt76_get_rate(struct mt76_dev *dev,
1540 		  struct ieee80211_supported_band *sband,
1541 		  int idx, bool cck)
1542 {
1543 	int i, offset = 0, len = sband->n_bitrates;
1544 
1545 	if (cck) {
1546 		if (sband != &dev->phy.sband_2g.sband)
1547 			return 0;
1548 
1549 		idx &= ~BIT(2); /* short preamble */
1550 	} else if (sband == &dev->phy.sband_2g.sband) {
1551 		offset = 4;
1552 	}
1553 
1554 	for (i = offset; i < len; i++) {
1555 		if ((sband->bitrates[i].hw_value & GENMASK(7, 0)) == idx)
1556 			return i;
1557 	}
1558 
1559 	return 0;
1560 }
1561 EXPORT_SYMBOL_GPL(mt76_get_rate);
1562 
1563 void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1564 		  const u8 *mac)
1565 {
1566 	struct mt76_phy *phy = hw->priv;
1567 
1568 	set_bit(MT76_SCANNING, &phy->state);
1569 }
1570 EXPORT_SYMBOL_GPL(mt76_sw_scan);
1571 
1572 void mt76_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1573 {
1574 	struct mt76_phy *phy = hw->priv;
1575 
1576 	clear_bit(MT76_SCANNING, &phy->state);
1577 }
1578 EXPORT_SYMBOL_GPL(mt76_sw_scan_complete);
1579 
1580 int mt76_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
1581 {
1582 	struct mt76_phy *phy = hw->priv;
1583 	struct mt76_dev *dev = phy->dev;
1584 
1585 	mutex_lock(&dev->mutex);
1586 	*tx_ant = phy->antenna_mask;
1587 	*rx_ant = phy->antenna_mask;
1588 	mutex_unlock(&dev->mutex);
1589 
1590 	return 0;
1591 }
1592 EXPORT_SYMBOL_GPL(mt76_get_antenna);
1593 
1594 struct mt76_queue *
1595 mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc,
1596 		int ring_base, u32 flags)
1597 {
1598 	struct mt76_queue *hwq;
1599 	int err;
1600 
1601 	hwq = devm_kzalloc(dev->dev, sizeof(*hwq), GFP_KERNEL);
1602 	if (!hwq)
1603 		return ERR_PTR(-ENOMEM);
1604 
1605 	hwq->flags = flags;
1606 
1607 	err = dev->queue_ops->alloc(dev, hwq, idx, n_desc, 0, ring_base);
1608 	if (err < 0)
1609 		return ERR_PTR(err);
1610 
1611 	return hwq;
1612 }
1613 EXPORT_SYMBOL_GPL(mt76_init_queue);
1614 
1615 u16 mt76_calculate_default_rate(struct mt76_phy *phy, int rateidx)
1616 {
1617 	int offset = 0;
1618 
1619 	if (phy->chandef.chan->band != NL80211_BAND_2GHZ)
1620 		offset = 4;
1621 
1622 	/* pick the lowest rate for hidden nodes */
1623 	if (rateidx < 0)
1624 		rateidx = 0;
1625 
1626 	rateidx += offset;
1627 	if (rateidx >= ARRAY_SIZE(mt76_rates))
1628 		rateidx = offset;
1629 
1630 	return mt76_rates[rateidx].hw_value;
1631 }
1632 EXPORT_SYMBOL_GPL(mt76_calculate_default_rate);
1633 
1634 void mt76_ethtool_worker(struct mt76_ethtool_worker_info *wi,
1635 			 struct mt76_sta_stats *stats)
1636 {
1637 	int i, ei = wi->initial_stat_idx;
1638 	u64 *data = wi->data;
1639 
1640 	wi->sta_count++;
1641 
1642 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_CCK];
1643 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_OFDM];
1644 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_HT];
1645 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_HT_GF];
1646 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_VHT];
1647 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_SU];
1648 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_EXT_SU];
1649 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_TB];
1650 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_MU];
1651 
1652 	for (i = 0; i < ARRAY_SIZE(stats->tx_bw); i++)
1653 		data[ei++] += stats->tx_bw[i];
1654 
1655 	for (i = 0; i < 12; i++)
1656 		data[ei++] += stats->tx_mcs[i];
1657 
1658 	wi->worker_stat_count = ei - wi->initial_stat_idx;
1659 }
1660 EXPORT_SYMBOL_GPL(mt76_ethtool_worker);
1661 
1662 enum mt76_dfs_state mt76_phy_dfs_state(struct mt76_phy *phy)
1663 {
1664 	struct ieee80211_hw *hw = phy->hw;
1665 	struct mt76_dev *dev = phy->dev;
1666 
1667 	if (dev->region == NL80211_DFS_UNSET ||
1668 	    test_bit(MT76_SCANNING, &phy->state))
1669 		return MT_DFS_STATE_DISABLED;
1670 
1671 	if (!hw->conf.radar_enabled) {
1672 		if ((hw->conf.flags & IEEE80211_CONF_MONITOR) &&
1673 		    (phy->chandef.chan->flags & IEEE80211_CHAN_RADAR))
1674 			return MT_DFS_STATE_ACTIVE;
1675 
1676 		return MT_DFS_STATE_DISABLED;
1677 	}
1678 
1679 	if (!cfg80211_reg_can_beacon(hw->wiphy, &phy->chandef, NL80211_IFTYPE_AP))
1680 		return MT_DFS_STATE_CAC;
1681 
1682 	return MT_DFS_STATE_ACTIVE;
1683 }
1684 EXPORT_SYMBOL_GPL(mt76_phy_dfs_state);
1685