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