xref: /linux/drivers/net/wireless/mediatek/mt76/mac80211.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
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 	CHAN5G(177, 5885),
79 };
80 
81 static const struct ieee80211_channel mt76_channels_6ghz[] = {
82 	/* UNII-5 */
83 	CHAN6G(1, 5955),
84 	CHAN6G(5, 5975),
85 	CHAN6G(9, 5995),
86 	CHAN6G(13, 6015),
87 	CHAN6G(17, 6035),
88 	CHAN6G(21, 6055),
89 	CHAN6G(25, 6075),
90 	CHAN6G(29, 6095),
91 	CHAN6G(33, 6115),
92 	CHAN6G(37, 6135),
93 	CHAN6G(41, 6155),
94 	CHAN6G(45, 6175),
95 	CHAN6G(49, 6195),
96 	CHAN6G(53, 6215),
97 	CHAN6G(57, 6235),
98 	CHAN6G(61, 6255),
99 	CHAN6G(65, 6275),
100 	CHAN6G(69, 6295),
101 	CHAN6G(73, 6315),
102 	CHAN6G(77, 6335),
103 	CHAN6G(81, 6355),
104 	CHAN6G(85, 6375),
105 	CHAN6G(89, 6395),
106 	CHAN6G(93, 6415),
107 	/* UNII-6 */
108 	CHAN6G(97, 6435),
109 	CHAN6G(101, 6455),
110 	CHAN6G(105, 6475),
111 	CHAN6G(109, 6495),
112 	CHAN6G(113, 6515),
113 	CHAN6G(117, 6535),
114 	/* UNII-7 */
115 	CHAN6G(121, 6555),
116 	CHAN6G(125, 6575),
117 	CHAN6G(129, 6595),
118 	CHAN6G(133, 6615),
119 	CHAN6G(137, 6635),
120 	CHAN6G(141, 6655),
121 	CHAN6G(145, 6675),
122 	CHAN6G(149, 6695),
123 	CHAN6G(153, 6715),
124 	CHAN6G(157, 6735),
125 	CHAN6G(161, 6755),
126 	CHAN6G(165, 6775),
127 	CHAN6G(169, 6795),
128 	CHAN6G(173, 6815),
129 	CHAN6G(177, 6835),
130 	CHAN6G(181, 6855),
131 	CHAN6G(185, 6875),
132 	/* UNII-8 */
133 	CHAN6G(189, 6895),
134 	CHAN6G(193, 6915),
135 	CHAN6G(197, 6935),
136 	CHAN6G(201, 6955),
137 	CHAN6G(205, 6975),
138 	CHAN6G(209, 6995),
139 	CHAN6G(213, 7015),
140 	CHAN6G(217, 7035),
141 	CHAN6G(221, 7055),
142 	CHAN6G(225, 7075),
143 	CHAN6G(229, 7095),
144 	CHAN6G(233, 7115),
145 };
146 
147 static const struct ieee80211_tpt_blink mt76_tpt_blink[] = {
148 	{ .throughput =   0 * 1024, .blink_time = 334 },
149 	{ .throughput =   1 * 1024, .blink_time = 260 },
150 	{ .throughput =   5 * 1024, .blink_time = 220 },
151 	{ .throughput =  10 * 1024, .blink_time = 190 },
152 	{ .throughput =  20 * 1024, .blink_time = 170 },
153 	{ .throughput =  50 * 1024, .blink_time = 150 },
154 	{ .throughput =  70 * 1024, .blink_time = 130 },
155 	{ .throughput = 100 * 1024, .blink_time = 110 },
156 	{ .throughput = 200 * 1024, .blink_time =  80 },
157 	{ .throughput = 300 * 1024, .blink_time =  50 },
158 };
159 
160 struct ieee80211_rate mt76_rates[] = {
161 	CCK_RATE(0, 10),
162 	CCK_RATE(1, 20),
163 	CCK_RATE(2, 55),
164 	CCK_RATE(3, 110),
165 	OFDM_RATE(11, 60),
166 	OFDM_RATE(15, 90),
167 	OFDM_RATE(10, 120),
168 	OFDM_RATE(14, 180),
169 	OFDM_RATE(9,  240),
170 	OFDM_RATE(13, 360),
171 	OFDM_RATE(8,  480),
172 	OFDM_RATE(12, 540),
173 };
174 EXPORT_SYMBOL_GPL(mt76_rates);
175 
176 static const struct cfg80211_sar_freq_ranges mt76_sar_freq_ranges[] = {
177 	{ .start_freq = 2402, .end_freq = 2494, },
178 	{ .start_freq = 5150, .end_freq = 5350, },
179 	{ .start_freq = 5350, .end_freq = 5470, },
180 	{ .start_freq = 5470, .end_freq = 5725, },
181 	{ .start_freq = 5725, .end_freq = 5950, },
182 	{ .start_freq = 5945, .end_freq = 6165, },
183 	{ .start_freq = 6165, .end_freq = 6405, },
184 	{ .start_freq = 6405, .end_freq = 6525, },
185 	{ .start_freq = 6525, .end_freq = 6705, },
186 	{ .start_freq = 6705, .end_freq = 6865, },
187 	{ .start_freq = 6865, .end_freq = 7125, },
188 };
189 
190 static const struct cfg80211_sar_capa mt76_sar_capa = {
191 	.type = NL80211_SAR_TYPE_POWER,
192 	.num_freq_ranges = ARRAY_SIZE(mt76_sar_freq_ranges),
193 	.freq_ranges = &mt76_sar_freq_ranges[0],
194 };
195 
mt76_led_init(struct mt76_phy * phy)196 static int mt76_led_init(struct mt76_phy *phy)
197 {
198 	struct mt76_dev *dev = phy->dev;
199 	struct ieee80211_hw *hw = phy->hw;
200 	struct device_node *np = dev->dev->of_node;
201 
202 	if (!phy->leds.cdev.brightness_set && !phy->leds.cdev.blink_set)
203 		return 0;
204 
205 	np = of_get_child_by_name(np, "led");
206 	if (np) {
207 		if (!of_device_is_available(np)) {
208 			of_node_put(np);
209 			dev_info(dev->dev,
210 				"led registration was explicitly disabled by dts\n");
211 			return 0;
212 		}
213 
214 		if (phy == &dev->phy) {
215 			int led_pin;
216 
217 			if (!of_property_read_u32(np, "led-sources", &led_pin))
218 				phy->leds.pin = led_pin;
219 
220 			phy->leds.al =
221 				of_property_read_bool(np, "led-active-low");
222 		}
223 
224 		of_node_put(np);
225 	}
226 
227 	snprintf(phy->leds.name, sizeof(phy->leds.name), "mt76-%s",
228 		 wiphy_name(hw->wiphy));
229 
230 	phy->leds.cdev.name = phy->leds.name;
231 	phy->leds.cdev.default_trigger =
232 		ieee80211_create_tpt_led_trigger(hw,
233 					IEEE80211_TPT_LEDTRIG_FL_RADIO,
234 					mt76_tpt_blink,
235 					ARRAY_SIZE(mt76_tpt_blink));
236 
237 	dev_info(dev->dev,
238 		"registering led '%s'\n", phy->leds.name);
239 
240 	return led_classdev_register(dev->dev, &phy->leds.cdev);
241 }
242 
mt76_led_cleanup(struct mt76_phy * phy)243 static void mt76_led_cleanup(struct mt76_phy *phy)
244 {
245 	if (!phy->leds.cdev.brightness_set && !phy->leds.cdev.blink_set)
246 		return;
247 
248 	led_classdev_unregister(&phy->leds.cdev);
249 }
250 
mt76_init_stream_cap(struct mt76_phy * phy,struct ieee80211_supported_band * sband,bool vht)251 static void mt76_init_stream_cap(struct mt76_phy *phy,
252 				 struct ieee80211_supported_band *sband,
253 				 bool vht)
254 {
255 	struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
256 	int i, nstream = hweight8(phy->antenna_mask);
257 	struct ieee80211_sta_vht_cap *vht_cap;
258 	u16 mcs_map = 0;
259 
260 	if (nstream > 1)
261 		ht_cap->cap |= IEEE80211_HT_CAP_TX_STBC;
262 	else
263 		ht_cap->cap &= ~IEEE80211_HT_CAP_TX_STBC;
264 
265 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
266 		ht_cap->mcs.rx_mask[i] = i < nstream ? 0xff : 0;
267 
268 	if (!vht)
269 		return;
270 
271 	vht_cap = &sband->vht_cap;
272 	if (nstream > 1)
273 		vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
274 	else
275 		vht_cap->cap &= ~IEEE80211_VHT_CAP_TXSTBC;
276 	vht_cap->cap |= IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
277 			IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN;
278 
279 	for (i = 0; i < 8; i++) {
280 		if (i < nstream)
281 			mcs_map |= (IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2));
282 		else
283 			mcs_map |=
284 				(IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2));
285 	}
286 	vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
287 	vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
288 	if (ieee80211_hw_check(phy->hw, SUPPORTS_VHT_EXT_NSS_BW))
289 		vht_cap->vht_mcs.tx_highest |=
290 				cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
291 }
292 
mt76_set_stream_caps(struct mt76_phy * phy,bool vht)293 void mt76_set_stream_caps(struct mt76_phy *phy, bool vht)
294 {
295 	if (phy->cap.has_2ghz)
296 		mt76_init_stream_cap(phy, &phy->sband_2g.sband, false);
297 	if (phy->cap.has_5ghz)
298 		mt76_init_stream_cap(phy, &phy->sband_5g.sband, vht);
299 	if (phy->cap.has_6ghz)
300 		mt76_init_stream_cap(phy, &phy->sband_6g.sband, vht);
301 }
302 EXPORT_SYMBOL_GPL(mt76_set_stream_caps);
303 
304 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)305 mt76_init_sband(struct mt76_phy *phy, struct mt76_sband *msband,
306 		const struct ieee80211_channel *chan, int n_chan,
307 		struct ieee80211_rate *rates, int n_rates,
308 		bool ht, bool vht)
309 {
310 	struct ieee80211_supported_band *sband = &msband->sband;
311 	struct ieee80211_sta_vht_cap *vht_cap;
312 	struct ieee80211_sta_ht_cap *ht_cap;
313 	struct mt76_dev *dev = phy->dev;
314 	void *chanlist;
315 	int size;
316 
317 	size = n_chan * sizeof(*chan);
318 	chanlist = devm_kmemdup(dev->dev, chan, size, GFP_KERNEL);
319 	if (!chanlist)
320 		return -ENOMEM;
321 
322 	msband->chan = devm_kcalloc(dev->dev, n_chan, sizeof(*msband->chan),
323 				    GFP_KERNEL);
324 	if (!msband->chan)
325 		return -ENOMEM;
326 
327 	sband->channels = chanlist;
328 	sband->n_channels = n_chan;
329 	sband->bitrates = rates;
330 	sband->n_bitrates = n_rates;
331 
332 	if (!ht)
333 		return 0;
334 
335 	ht_cap = &sband->ht_cap;
336 	ht_cap->ht_supported = true;
337 	ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
338 		       IEEE80211_HT_CAP_GRN_FLD |
339 		       IEEE80211_HT_CAP_SGI_20 |
340 		       IEEE80211_HT_CAP_SGI_40 |
341 		       (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
342 
343 	ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
344 	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
345 
346 	mt76_init_stream_cap(phy, sband, vht);
347 
348 	if (!vht)
349 		return 0;
350 
351 	vht_cap = &sband->vht_cap;
352 	vht_cap->vht_supported = true;
353 	vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC |
354 			IEEE80211_VHT_CAP_RXSTBC_1 |
355 			IEEE80211_VHT_CAP_SHORT_GI_80 |
356 			(3 << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT);
357 
358 	return 0;
359 }
360 
361 static int
mt76_init_sband_2g(struct mt76_phy * phy,struct ieee80211_rate * rates,int n_rates)362 mt76_init_sband_2g(struct mt76_phy *phy, struct ieee80211_rate *rates,
363 		   int n_rates)
364 {
365 	phy->hw->wiphy->bands[NL80211_BAND_2GHZ] = &phy->sband_2g.sband;
366 
367 	return mt76_init_sband(phy, &phy->sband_2g, mt76_channels_2ghz,
368 			       ARRAY_SIZE(mt76_channels_2ghz), rates,
369 			       n_rates, true, false);
370 }
371 
372 static int
mt76_init_sband_5g(struct mt76_phy * phy,struct ieee80211_rate * rates,int n_rates,bool vht)373 mt76_init_sband_5g(struct mt76_phy *phy, struct ieee80211_rate *rates,
374 		   int n_rates, bool vht)
375 {
376 	phy->hw->wiphy->bands[NL80211_BAND_5GHZ] = &phy->sband_5g.sband;
377 
378 	return mt76_init_sband(phy, &phy->sband_5g, mt76_channels_5ghz,
379 			       ARRAY_SIZE(mt76_channels_5ghz), rates,
380 			       n_rates, true, vht);
381 }
382 
383 static int
mt76_init_sband_6g(struct mt76_phy * phy,struct ieee80211_rate * rates,int n_rates)384 mt76_init_sband_6g(struct mt76_phy *phy, struct ieee80211_rate *rates,
385 		   int n_rates)
386 {
387 	phy->hw->wiphy->bands[NL80211_BAND_6GHZ] = &phy->sband_6g.sband;
388 
389 	return mt76_init_sband(phy, &phy->sband_6g, mt76_channels_6ghz,
390 			       ARRAY_SIZE(mt76_channels_6ghz), rates,
391 			       n_rates, false, false);
392 }
393 
394 static void
mt76_check_sband(struct mt76_phy * phy,struct mt76_sband * msband,enum nl80211_band band)395 mt76_check_sband(struct mt76_phy *phy, struct mt76_sband *msband,
396 		 enum nl80211_band band)
397 {
398 	struct ieee80211_supported_band *sband = &msband->sband;
399 	bool found = false;
400 	int i;
401 
402 	if (!sband)
403 		return;
404 
405 	for (i = 0; i < sband->n_channels; i++) {
406 		if (sband->channels[i].flags & IEEE80211_CHAN_DISABLED)
407 			continue;
408 
409 		found = true;
410 		break;
411 	}
412 
413 	if (found) {
414 		cfg80211_chandef_create(&phy->chandef, &sband->channels[0],
415 					NL80211_CHAN_HT20);
416 		phy->chan_state = &msband->chan[0];
417 		phy->dev->band_phys[band] = phy;
418 		return;
419 	}
420 
421 	sband->n_channels = 0;
422 	if (phy->hw->wiphy->bands[band] == sband)
423 		phy->hw->wiphy->bands[band] = NULL;
424 }
425 
426 static int
mt76_phy_init(struct mt76_phy * phy,struct ieee80211_hw * hw)427 mt76_phy_init(struct mt76_phy *phy, struct ieee80211_hw *hw)
428 {
429 	struct mt76_dev *dev = phy->dev;
430 	struct wiphy *wiphy = hw->wiphy;
431 
432 	INIT_LIST_HEAD(&phy->tx_list);
433 	spin_lock_init(&phy->tx_lock);
434 	INIT_DELAYED_WORK(&phy->roc_work, mt76_roc_complete_work);
435 
436 	if ((void *)phy != hw->priv)
437 		return 0;
438 
439 	SET_IEEE80211_DEV(hw, dev->dev);
440 	SET_IEEE80211_PERM_ADDR(hw, phy->macaddr);
441 
442 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
443 	if (!phy->no_active_monitor)
444 		wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
445 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH |
446 			WIPHY_FLAG_SUPPORTS_TDLS |
447 			WIPHY_FLAG_AP_UAPSD;
448 
449 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
450 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
451 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AQL);
452 
453 	if (!wiphy->max_remain_on_channel_duration)
454 		wiphy->max_remain_on_channel_duration = 5000;
455 	if (!wiphy->available_antennas_tx)
456 		wiphy->available_antennas_tx = phy->antenna_mask;
457 	if (!wiphy->available_antennas_rx)
458 		wiphy->available_antennas_rx = phy->antenna_mask;
459 
460 	wiphy->sar_capa = &mt76_sar_capa;
461 	phy->frp = devm_kcalloc(dev->dev, wiphy->sar_capa->num_freq_ranges,
462 				sizeof(struct mt76_freq_range_power),
463 				GFP_KERNEL);
464 	if (!phy->frp)
465 		return -ENOMEM;
466 
467 	hw->txq_data_size = sizeof(struct mt76_txq);
468 	hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
469 
470 	if (!hw->max_tx_fragments)
471 		hw->max_tx_fragments = 16;
472 
473 	ieee80211_hw_set(hw, SIGNAL_DBM);
474 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
475 	ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
476 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
477 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
478 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
479 	ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
480 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
481 
482 	if (!(dev->drv->drv_flags & MT_DRV_AMSDU_OFFLOAD) &&
483 	    hw->max_tx_fragments > 1) {
484 		ieee80211_hw_set(hw, TX_AMSDU);
485 		ieee80211_hw_set(hw, TX_FRAG_LIST);
486 	}
487 
488 	ieee80211_hw_set(hw, MFP_CAPABLE);
489 	ieee80211_hw_set(hw, AP_LINK_PS);
490 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
491 
492 	return 0;
493 }
494 
495 struct mt76_phy *
mt76_alloc_radio_phy(struct mt76_dev * dev,unsigned int size,u8 band_idx)496 mt76_alloc_radio_phy(struct mt76_dev *dev, unsigned int size,
497 		     u8 band_idx)
498 {
499 	struct ieee80211_hw *hw = dev->phy.hw;
500 	unsigned int phy_size;
501 	struct mt76_phy *phy;
502 
503 	phy_size = ALIGN(sizeof(*phy), 8);
504 	phy = devm_kzalloc(dev->dev, size + phy_size, GFP_KERNEL);
505 	if (!phy)
506 		return NULL;
507 
508 	phy->dev = dev;
509 	phy->hw = hw;
510 	phy->priv = (void *)phy + phy_size;
511 	phy->band_idx = band_idx;
512 
513 	return phy;
514 }
515 EXPORT_SYMBOL_GPL(mt76_alloc_radio_phy);
516 
517 struct mt76_phy *
mt76_alloc_phy(struct mt76_dev * dev,unsigned int size,const struct ieee80211_ops * ops,u8 band_idx)518 mt76_alloc_phy(struct mt76_dev *dev, unsigned int size,
519 	       const struct ieee80211_ops *ops, u8 band_idx)
520 {
521 	struct ieee80211_hw *hw;
522 	unsigned int phy_size;
523 	struct mt76_phy *phy;
524 
525 	phy_size = ALIGN(sizeof(*phy), 8);
526 	hw = ieee80211_alloc_hw(size + phy_size, ops);
527 	if (!hw)
528 		return NULL;
529 
530 	phy = hw->priv;
531 	phy->dev = dev;
532 	phy->hw = hw;
533 	phy->priv = hw->priv + phy_size;
534 	phy->band_idx = band_idx;
535 
536 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
537 	hw->wiphy->interface_modes =
538 		BIT(NL80211_IFTYPE_STATION) |
539 		BIT(NL80211_IFTYPE_AP) |
540 #ifdef CONFIG_MAC80211_MESH
541 		BIT(NL80211_IFTYPE_MESH_POINT) |
542 #endif
543 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
544 		BIT(NL80211_IFTYPE_P2P_GO) |
545 		BIT(NL80211_IFTYPE_ADHOC);
546 
547 	return phy;
548 }
549 EXPORT_SYMBOL_GPL(mt76_alloc_phy);
550 
mt76_register_phy(struct mt76_phy * phy,bool vht,struct ieee80211_rate * rates,int n_rates)551 int mt76_register_phy(struct mt76_phy *phy, bool vht,
552 		      struct ieee80211_rate *rates, int n_rates)
553 {
554 	int ret;
555 
556 	ret = mt76_phy_init(phy, phy->hw);
557 	if (ret)
558 		return ret;
559 
560 	if (phy->cap.has_2ghz) {
561 		ret = mt76_init_sband_2g(phy, rates, n_rates);
562 		if (ret)
563 			return ret;
564 	}
565 
566 	if (phy->cap.has_5ghz) {
567 		ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht);
568 		if (ret)
569 			return ret;
570 	}
571 
572 	if (phy->cap.has_6ghz) {
573 		ret = mt76_init_sband_6g(phy, rates + 4, n_rates - 4);
574 		if (ret)
575 			return ret;
576 	}
577 
578 	if (IS_ENABLED(CONFIG_MT76_LEDS)) {
579 		ret = mt76_led_init(phy);
580 		if (ret)
581 			return ret;
582 	}
583 
584 	wiphy_read_of_freq_limits(phy->hw->wiphy);
585 	mt76_check_sband(phy, &phy->sband_2g, NL80211_BAND_2GHZ);
586 	mt76_check_sband(phy, &phy->sband_5g, NL80211_BAND_5GHZ);
587 	mt76_check_sband(phy, &phy->sband_6g, NL80211_BAND_6GHZ);
588 
589 	if ((void *)phy == phy->hw->priv) {
590 		ret = ieee80211_register_hw(phy->hw);
591 		if (ret)
592 			return ret;
593 	}
594 
595 	set_bit(MT76_STATE_REGISTERED, &phy->state);
596 	phy->dev->phys[phy->band_idx] = phy;
597 
598 	return 0;
599 }
600 EXPORT_SYMBOL_GPL(mt76_register_phy);
601 
mt76_unregister_phy(struct mt76_phy * phy)602 void mt76_unregister_phy(struct mt76_phy *phy)
603 {
604 	struct mt76_dev *dev = phy->dev;
605 
606 	if (!test_bit(MT76_STATE_REGISTERED, &phy->state))
607 		return;
608 
609 	if (IS_ENABLED(CONFIG_MT76_LEDS))
610 		mt76_led_cleanup(phy);
611 	mt76_tx_status_check(dev, true);
612 	ieee80211_unregister_hw(phy->hw);
613 	dev->phys[phy->band_idx] = NULL;
614 }
615 EXPORT_SYMBOL_GPL(mt76_unregister_phy);
616 
mt76_create_page_pool(struct mt76_dev * dev,struct mt76_queue * q)617 int mt76_create_page_pool(struct mt76_dev *dev, struct mt76_queue *q)
618 {
619 	bool is_qrx = mt76_queue_is_rx(dev, q);
620 	struct page_pool_params pp_params = {
621 		.order = 0,
622 		.flags = 0,
623 		.nid = NUMA_NO_NODE,
624 		.dev = dev->dma_dev,
625 	};
626 	int idx = is_qrx ? q - dev->q_rx : -1;
627 
628 	/* Allocate page_pools just for rx/wed_tx_free queues */
629 	if (!is_qrx && !mt76_queue_is_wed_tx_free(q))
630 		return 0;
631 
632 	switch (idx) {
633 	case MT_RXQ_MAIN:
634 	case MT_RXQ_BAND1:
635 	case MT_RXQ_BAND2:
636 	case MT_RXQ_NPU0:
637 	case MT_RXQ_NPU1:
638 		pp_params.pool_size = 256;
639 		break;
640 	default:
641 		pp_params.pool_size = 16;
642 		break;
643 	}
644 
645 	if (mt76_is_mmio(dev)) {
646 		/* rely on page_pool for DMA mapping */
647 		pp_params.flags |= PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
648 		pp_params.dma_dir = DMA_FROM_DEVICE;
649 		pp_params.max_len = PAGE_SIZE;
650 		pp_params.offset = 0;
651 		/* NAPI is available just for rx queues */
652 		if (idx >= 0 && idx < ARRAY_SIZE(dev->napi))
653 			pp_params.napi = &dev->napi[idx];
654 	}
655 
656 	q->page_pool = page_pool_create(&pp_params);
657 	if (IS_ERR(q->page_pool)) {
658 		int err = PTR_ERR(q->page_pool);
659 
660 		q->page_pool = NULL;
661 		return err;
662 	}
663 
664 	return 0;
665 }
666 EXPORT_SYMBOL_GPL(mt76_create_page_pool);
667 
668 struct mt76_dev *
mt76_alloc_device(struct device * pdev,unsigned int size,const struct ieee80211_ops * ops,const struct mt76_driver_ops * drv_ops)669 mt76_alloc_device(struct device *pdev, unsigned int size,
670 		  const struct ieee80211_ops *ops,
671 		  const struct mt76_driver_ops *drv_ops)
672 {
673 	struct ieee80211_hw *hw;
674 	struct mt76_phy *phy;
675 	struct mt76_dev *dev;
676 	int i;
677 
678 	hw = ieee80211_alloc_hw(size, ops);
679 	if (!hw)
680 		return NULL;
681 
682 	dev = hw->priv;
683 	dev->hw = hw;
684 	dev->dev = pdev;
685 	dev->init_wiphy = NULL;
686 	dev->drv = drv_ops;
687 	dev->dma_dev = pdev;
688 
689 	phy = &dev->phy;
690 	phy->dev = dev;
691 	phy->hw = hw;
692 	phy->band_idx = MT_BAND0;
693 	dev->phys[phy->band_idx] = phy;
694 
695 	spin_lock_init(&dev->rx_lock);
696 	spin_lock_init(&dev->lock);
697 	spin_lock_init(&dev->cc_lock);
698 	spin_lock_init(&dev->status_lock);
699 	spin_lock_init(&dev->wed_lock);
700 	mutex_init(&dev->mutex);
701 	init_waitqueue_head(&dev->tx_wait);
702 
703 	skb_queue_head_init(&dev->mcu.res_q);
704 	init_waitqueue_head(&dev->mcu.wait);
705 	mutex_init(&dev->mcu.mutex);
706 	dev->tx_worker.fn = mt76_tx_worker;
707 
708 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
709 	hw->wiphy->interface_modes =
710 		BIT(NL80211_IFTYPE_STATION) |
711 		BIT(NL80211_IFTYPE_AP) |
712 #ifdef CONFIG_MAC80211_MESH
713 		BIT(NL80211_IFTYPE_MESH_POINT) |
714 #endif
715 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
716 		BIT(NL80211_IFTYPE_P2P_GO) |
717 		BIT(NL80211_IFTYPE_ADHOC);
718 
719 	spin_lock_init(&dev->token_lock);
720 	idr_init(&dev->token);
721 
722 	spin_lock_init(&dev->rx_token_lock);
723 	idr_init(&dev->rx_token);
724 
725 	INIT_LIST_HEAD(&dev->wcid_list);
726 	INIT_LIST_HEAD(&dev->sta_poll_list);
727 	spin_lock_init(&dev->sta_poll_lock);
728 
729 	INIT_LIST_HEAD(&dev->txwi_cache);
730 	INIT_LIST_HEAD(&dev->rxwi_cache);
731 	dev->token_size = dev->drv->token_size;
732 	INIT_DELAYED_WORK(&dev->scan_work, mt76_scan_work);
733 	spin_lock_init(&dev->scan_lock);
734 
735 	for (i = 0; i < ARRAY_SIZE(dev->q_rx); i++)
736 		skb_queue_head_init(&dev->rx_skb[i]);
737 
738 	dev->wq = alloc_ordered_workqueue("mt76", 0);
739 	if (!dev->wq) {
740 		ieee80211_free_hw(hw);
741 		return NULL;
742 	}
743 
744 	return dev;
745 }
746 EXPORT_SYMBOL_GPL(mt76_alloc_device);
747 
mt76_register_device(struct mt76_dev * dev,bool vht,struct ieee80211_rate * rates,int n_rates)748 int mt76_register_device(struct mt76_dev *dev, bool vht,
749 			 struct ieee80211_rate *rates, int n_rates)
750 {
751 	struct ieee80211_hw *hw = dev->hw;
752 	struct mt76_phy *phy = &dev->phy;
753 	int ret;
754 
755 	dev_set_drvdata(dev->dev, dev);
756 	mt76_wcid_init(&dev->global_wcid, phy->band_idx);
757 	ret = mt76_phy_init(phy, hw);
758 	if (ret)
759 		return ret;
760 
761 	if (phy->cap.has_2ghz) {
762 		ret = mt76_init_sband_2g(phy, rates, n_rates);
763 		if (ret)
764 			return ret;
765 	}
766 
767 	if (phy->cap.has_5ghz) {
768 		ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht);
769 		if (ret)
770 			return ret;
771 	}
772 
773 	if (phy->cap.has_6ghz) {
774 		ret = mt76_init_sband_6g(phy, rates + 4, n_rates - 4);
775 		if (ret)
776 			return ret;
777 	}
778 
779 	wiphy_read_of_freq_limits(hw->wiphy);
780 	mt76_check_sband(&dev->phy, &phy->sband_2g, NL80211_BAND_2GHZ);
781 	mt76_check_sband(&dev->phy, &phy->sband_5g, NL80211_BAND_5GHZ);
782 	mt76_check_sband(&dev->phy, &phy->sband_6g, NL80211_BAND_6GHZ);
783 
784 	if (dev->init_wiphy) {
785 		ret = dev->init_wiphy(dev);
786 		if (ret)
787 			return ret;
788 	}
789 
790 	if (IS_ENABLED(CONFIG_MT76_LEDS)) {
791 		ret = mt76_led_init(phy);
792 		if (ret)
793 			return ret;
794 	}
795 
796 	ret = ieee80211_register_hw(hw);
797 	if (ret)
798 		return ret;
799 
800 	WARN_ON(mt76_worker_setup(hw, &dev->tx_worker, NULL, "tx"));
801 	set_bit(MT76_STATE_REGISTERED, &phy->state);
802 	sched_set_fifo_low(dev->tx_worker.task);
803 
804 	return 0;
805 }
806 EXPORT_SYMBOL_GPL(mt76_register_device);
807 
mt76_unregister_device(struct mt76_dev * dev)808 void mt76_unregister_device(struct mt76_dev *dev)
809 {
810 	struct ieee80211_hw *hw = dev->hw;
811 
812 	if (!test_bit(MT76_STATE_REGISTERED, &dev->phy.state))
813 		return;
814 
815 	if (IS_ENABLED(CONFIG_MT76_LEDS))
816 		mt76_led_cleanup(&dev->phy);
817 	mt76_tx_status_check(dev, true);
818 	mt76_wcid_cleanup(dev, &dev->global_wcid);
819 	ieee80211_unregister_hw(hw);
820 }
821 EXPORT_SYMBOL_GPL(mt76_unregister_device);
822 
mt76_free_device(struct mt76_dev * dev)823 void mt76_free_device(struct mt76_dev *dev)
824 {
825 	mt76_worker_teardown(&dev->tx_worker);
826 	if (dev->wq) {
827 		destroy_workqueue(dev->wq);
828 		dev->wq = NULL;
829 	}
830 	mt76_npu_deinit(dev);
831 	ieee80211_free_hw(dev->hw);
832 }
833 EXPORT_SYMBOL_GPL(mt76_free_device);
834 
mt76_reset_phy(struct mt76_phy * phy)835 static void mt76_reset_phy(struct mt76_phy *phy)
836 {
837 	if (!phy)
838 		return;
839 
840 	INIT_LIST_HEAD(&phy->tx_list);
841 	phy->num_sta = 0;
842 	phy->chanctx = NULL;
843 	mt76_roc_complete(phy);
844 }
845 
mt76_reset_device(struct mt76_dev * dev)846 void mt76_reset_device(struct mt76_dev *dev)
847 {
848 	int i;
849 
850 	rcu_read_lock();
851 	for (i = 0; i < ARRAY_SIZE(dev->wcid); i++) {
852 		struct mt76_wcid *wcid;
853 
854 		wcid = rcu_dereference(dev->wcid[i]);
855 		if (!wcid)
856 			continue;
857 
858 		wcid->sta = 0;
859 		mt76_wcid_cleanup(dev, wcid);
860 		rcu_assign_pointer(dev->wcid[i], NULL);
861 	}
862 	rcu_read_unlock();
863 
864 	INIT_LIST_HEAD(&dev->wcid_list);
865 	INIT_LIST_HEAD(&dev->sta_poll_list);
866 	dev->vif_mask = 0;
867 	memset(dev->wcid_mask, 0, sizeof(dev->wcid_mask));
868 
869 	mt76_reset_phy(&dev->phy);
870 	for (i = 0; i < ARRAY_SIZE(dev->phys); i++)
871 		mt76_reset_phy(dev->phys[i]);
872 }
873 EXPORT_SYMBOL_GPL(mt76_reset_device);
874 
mt76_vif_phy(struct ieee80211_hw * hw,struct ieee80211_vif * vif)875 struct mt76_phy *mt76_vif_phy(struct ieee80211_hw *hw,
876 			      struct ieee80211_vif *vif)
877 {
878 	struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv;
879 	struct mt76_chanctx *ctx;
880 
881 	if (!hw->wiphy->n_radio)
882 		return hw->priv;
883 
884 	if (!mlink->ctx)
885 		return NULL;
886 
887 	ctx = (struct mt76_chanctx *)mlink->ctx->drv_priv;
888 	return ctx->phy;
889 }
890 EXPORT_SYMBOL_GPL(mt76_vif_phy);
891 
mt76_rx_release_amsdu(struct mt76_phy * phy,enum mt76_rxq_id q)892 static void mt76_rx_release_amsdu(struct mt76_phy *phy, enum mt76_rxq_id q)
893 {
894 	struct sk_buff *skb = phy->rx_amsdu[q].head;
895 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
896 	struct mt76_dev *dev = phy->dev;
897 	struct mt76_queue *rxq = &dev->q_rx[q];
898 
899 	phy->rx_amsdu[q].head = NULL;
900 	phy->rx_amsdu[q].tail = NULL;
901 
902 	/*
903 	 * Validate if the amsdu has a proper first subframe.
904 	 * A single MSDU can be parsed as A-MSDU when the unauthenticated A-MSDU
905 	 * flag of the QoS header gets flipped. In such cases, the first
906 	 * subframe has a LLC/SNAP header in the location of the destination
907 	 * address.
908 	 */
909 	if (skb_shinfo(skb)->frag_list) {
910 		int offset = 0;
911 
912 		if (!(status->flag & RX_FLAG_8023)) {
913 			offset = ieee80211_get_hdrlen_from_skb(skb);
914 
915 			if ((status->flag &
916 			     (RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED)) ==
917 			    RX_FLAG_DECRYPTED)
918 				offset += 8;
919 		}
920 
921 		if (ether_addr_equal(skb->data + offset, rfc1042_header)) {
922 			dev_kfree_skb(skb);
923 			return;
924 		}
925 	}
926 
927 	/* RRO 3.0 data queue skbs are processed and completed in the context
928 	 * of the indicator queue NAPI, which only polls its own skb list
929 	 */
930 	if (mt76_queue_is_wed_rro_data(rxq) && dev->hwrro_mode == MT76_HWRRO_V3)
931 		q = MT_RXQ_RRO_IND;
932 
933 	__skb_queue_tail(&dev->rx_skb[q], skb);
934 }
935 
mt76_rx_release_burst(struct mt76_phy * phy,enum mt76_rxq_id q,struct sk_buff * skb)936 static void mt76_rx_release_burst(struct mt76_phy *phy, enum mt76_rxq_id q,
937 				  struct sk_buff *skb)
938 {
939 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
940 
941 	if (phy->rx_amsdu[q].head &&
942 	    (!status->amsdu || status->first_amsdu ||
943 	     status->seqno != phy->rx_amsdu[q].seqno))
944 		mt76_rx_release_amsdu(phy, q);
945 
946 	if (!phy->rx_amsdu[q].head) {
947 		phy->rx_amsdu[q].tail = &skb_shinfo(skb)->frag_list;
948 		phy->rx_amsdu[q].seqno = status->seqno;
949 		phy->rx_amsdu[q].head = skb;
950 	} else {
951 		*phy->rx_amsdu[q].tail = skb;
952 		phy->rx_amsdu[q].tail = &skb->next;
953 	}
954 
955 	if (!status->amsdu || status->last_amsdu)
956 		mt76_rx_release_amsdu(phy, q);
957 }
958 
mt76_rx(struct mt76_dev * dev,enum mt76_rxq_id q,struct sk_buff * skb)959 void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb)
960 {
961 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
962 	struct mt76_phy *phy = mt76_dev_phy(dev, status->phy_idx);
963 
964 	if (!test_bit(MT76_STATE_RUNNING, &phy->state)) {
965 		dev_kfree_skb(skb);
966 		return;
967 	}
968 
969 #ifdef CONFIG_NL80211_TESTMODE
970 	if (phy->test.state == MT76_TM_STATE_RX_FRAMES) {
971 		phy->test.rx_stats.packets[q]++;
972 		if (status->flag & RX_FLAG_FAILED_FCS_CRC)
973 			phy->test.rx_stats.fcs_error[q]++;
974 	}
975 #endif
976 
977 	mt76_rx_release_burst(phy, q, skb);
978 }
979 EXPORT_SYMBOL_GPL(mt76_rx);
980 
mt76_has_tx_pending(struct mt76_phy * phy)981 bool mt76_has_tx_pending(struct mt76_phy *phy)
982 {
983 	struct mt76_queue *q;
984 	int i;
985 
986 	for (i = 0; i < __MT_TXQ_MAX; i++) {
987 		q = phy->q_tx[i];
988 		if (q && q->queued)
989 			return true;
990 	}
991 
992 	if (atomic_read(&phy->mgmt_tx_pending))
993 		return true;
994 
995 	return false;
996 }
997 EXPORT_SYMBOL_GPL(mt76_has_tx_pending);
998 
999 static struct mt76_channel_state *
mt76_channel_state(struct mt76_phy * phy,struct ieee80211_channel * c)1000 mt76_channel_state(struct mt76_phy *phy, struct ieee80211_channel *c)
1001 {
1002 	struct mt76_sband *msband;
1003 	int idx;
1004 
1005 	if (c->band == NL80211_BAND_2GHZ)
1006 		msband = &phy->sband_2g;
1007 	else if (c->band == NL80211_BAND_6GHZ)
1008 		msband = &phy->sband_6g;
1009 	else
1010 		msband = &phy->sband_5g;
1011 
1012 	idx = c - &msband->sband.channels[0];
1013 	return &msband->chan[idx];
1014 }
1015 
mt76_update_survey_active_time(struct mt76_phy * phy,ktime_t time)1016 void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time)
1017 {
1018 	struct mt76_channel_state *state = phy->chan_state;
1019 
1020 	state->cc_active += ktime_to_us(ktime_sub(time,
1021 						  phy->survey_time));
1022 	phy->survey_time = time;
1023 }
1024 EXPORT_SYMBOL_GPL(mt76_update_survey_active_time);
1025 
mt76_update_survey(struct mt76_phy * phy)1026 void mt76_update_survey(struct mt76_phy *phy)
1027 {
1028 	struct mt76_dev *dev = phy->dev;
1029 	ktime_t cur_time;
1030 
1031 	if (dev->drv->update_survey)
1032 		dev->drv->update_survey(phy);
1033 
1034 	cur_time = ktime_get_boottime();
1035 	mt76_update_survey_active_time(phy, cur_time);
1036 
1037 	if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME) {
1038 		struct mt76_channel_state *state = phy->chan_state;
1039 
1040 		spin_lock_bh(&dev->cc_lock);
1041 		state->cc_bss_rx += dev->cur_cc_bss_rx;
1042 		dev->cur_cc_bss_rx = 0;
1043 		spin_unlock_bh(&dev->cc_lock);
1044 	}
1045 }
1046 EXPORT_SYMBOL_GPL(mt76_update_survey);
1047 
__mt76_set_channel(struct mt76_phy * phy,struct cfg80211_chan_def * chandef,bool offchannel)1048 int __mt76_set_channel(struct mt76_phy *phy, struct cfg80211_chan_def *chandef,
1049 		       bool offchannel)
1050 {
1051 	struct mt76_dev *dev = phy->dev;
1052 	int timeout = HZ / 5;
1053 	int ret;
1054 
1055 	mt76_worker_disable(&dev->tx_worker);
1056 	mt76_txq_schedule_pending(phy);
1057 
1058 	set_bit(MT76_RESET, &phy->state);
1059 	wait_event_timeout(dev->tx_wait, !mt76_has_tx_pending(phy), timeout);
1060 	mt76_update_survey(phy);
1061 
1062 	if (phy->chandef.chan->center_freq != chandef->chan->center_freq ||
1063 	    phy->chandef.width != chandef->width)
1064 		phy->dfs_state = MT_DFS_STATE_UNKNOWN;
1065 
1066 	phy->chandef = *chandef;
1067 	phy->chan_state = mt76_channel_state(phy, chandef->chan);
1068 	phy->offchannel = offchannel;
1069 
1070 	if (!offchannel)
1071 		phy->main_chandef = *chandef;
1072 
1073 	if (chandef->chan != phy->main_chandef.chan)
1074 		memset(phy->chan_state, 0, sizeof(*phy->chan_state));
1075 
1076 	ret = dev->drv->set_channel(phy);
1077 
1078 	clear_bit(MT76_RESET, &phy->state);
1079 	mt76_worker_enable(&dev->tx_worker);
1080 	mt76_worker_schedule(&dev->tx_worker);
1081 
1082 	return ret;
1083 }
1084 
mt76_set_channel(struct mt76_phy * phy,struct cfg80211_chan_def * chandef,bool offchannel)1085 int mt76_set_channel(struct mt76_phy *phy, struct cfg80211_chan_def *chandef,
1086 		     bool offchannel)
1087 {
1088 	struct mt76_dev *dev = phy->dev;
1089 	int ret;
1090 
1091 	cancel_delayed_work_sync(&phy->mac_work);
1092 
1093 	mutex_lock(&dev->mutex);
1094 	ret = __mt76_set_channel(phy, chandef, offchannel);
1095 	mutex_unlock(&dev->mutex);
1096 
1097 	return ret;
1098 }
1099 
mt76_update_channel(struct mt76_phy * phy)1100 int mt76_update_channel(struct mt76_phy *phy)
1101 {
1102 	struct ieee80211_hw *hw = phy->hw;
1103 	struct cfg80211_chan_def *chandef = &hw->conf.chandef;
1104 	bool offchannel = hw->conf.flags & IEEE80211_CONF_OFFCHANNEL;
1105 
1106 	phy->radar_enabled = hw->conf.radar_enabled;
1107 
1108 	return mt76_set_channel(phy, chandef, offchannel);
1109 }
1110 EXPORT_SYMBOL_GPL(mt76_update_channel);
1111 
1112 static struct mt76_sband *
mt76_get_survey_sband(struct mt76_phy * phy,int * idx)1113 mt76_get_survey_sband(struct mt76_phy *phy, int *idx)
1114 {
1115 	if (*idx < phy->sband_2g.sband.n_channels)
1116 		return &phy->sband_2g;
1117 
1118 	*idx -= phy->sband_2g.sband.n_channels;
1119 	if (*idx < phy->sband_5g.sband.n_channels)
1120 		return &phy->sband_5g;
1121 
1122 	*idx -= phy->sband_5g.sband.n_channels;
1123 	if (*idx < phy->sband_6g.sband.n_channels)
1124 		return &phy->sband_6g;
1125 
1126 	*idx -= phy->sband_6g.sband.n_channels;
1127 	return NULL;
1128 }
1129 
mt76_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)1130 int mt76_get_survey(struct ieee80211_hw *hw, int idx,
1131 		    struct survey_info *survey)
1132 {
1133 	struct mt76_phy *phy = hw->priv;
1134 	struct mt76_dev *dev = phy->dev;
1135 	struct mt76_sband *sband = NULL;
1136 	struct ieee80211_channel *chan;
1137 	struct mt76_channel_state *state;
1138 	int phy_idx = 0;
1139 	int ret = 0;
1140 
1141 	mutex_lock(&dev->mutex);
1142 
1143 	for (phy_idx = 0; phy_idx < ARRAY_SIZE(dev->phys); phy_idx++) {
1144 		sband = NULL;
1145 		phy = dev->phys[phy_idx];
1146 		if (!phy || phy->hw != hw)
1147 			continue;
1148 
1149 		sband = mt76_get_survey_sband(phy, &idx);
1150 
1151 		if (idx == 0 && phy->dev->drv->update_survey)
1152 			mt76_update_survey(phy);
1153 
1154 		if (sband || !hw->wiphy->n_radio)
1155 			break;
1156 	}
1157 
1158 	if (!sband) {
1159 		ret = -ENOENT;
1160 		goto out;
1161 	}
1162 
1163 	chan = &sband->sband.channels[idx];
1164 	state = mt76_channel_state(phy, chan);
1165 
1166 	memset(survey, 0, sizeof(*survey));
1167 	survey->channel = chan;
1168 	survey->filled = SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY;
1169 	survey->filled |= dev->drv->survey_flags;
1170 	if (state->noise)
1171 		survey->filled |= SURVEY_INFO_NOISE_DBM;
1172 
1173 	if (chan == phy->main_chandef.chan) {
1174 		survey->filled |= SURVEY_INFO_IN_USE;
1175 
1176 		if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME)
1177 			survey->filled |= SURVEY_INFO_TIME_BSS_RX;
1178 	}
1179 
1180 	survey->time_busy = div_u64(state->cc_busy, 1000);
1181 	survey->time_rx = div_u64(state->cc_rx, 1000);
1182 	survey->time = div_u64(state->cc_active, 1000);
1183 	survey->noise = state->noise;
1184 
1185 	spin_lock_bh(&dev->cc_lock);
1186 	survey->time_bss_rx = div_u64(state->cc_bss_rx, 1000);
1187 	survey->time_tx = div_u64(state->cc_tx, 1000);
1188 	spin_unlock_bh(&dev->cc_lock);
1189 
1190 out:
1191 	mutex_unlock(&dev->mutex);
1192 
1193 	return ret;
1194 }
1195 EXPORT_SYMBOL_GPL(mt76_get_survey);
1196 
mt76_wcid_key_setup(struct mt76_dev * dev,struct mt76_wcid * wcid,struct ieee80211_key_conf * key)1197 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
1198 			 struct ieee80211_key_conf *key)
1199 {
1200 	struct ieee80211_key_seq seq;
1201 	int i;
1202 
1203 	wcid->rx_check_pn = false;
1204 
1205 	if (!key)
1206 		return;
1207 
1208 	if (key->cipher != WLAN_CIPHER_SUITE_CCMP)
1209 		return;
1210 
1211 	wcid->rx_check_pn = true;
1212 
1213 	/* data frame */
1214 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
1215 		ieee80211_get_key_rx_seq(key, i, &seq);
1216 		memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
1217 	}
1218 
1219 	/* robust management frame */
1220 	ieee80211_get_key_rx_seq(key, -1, &seq);
1221 	memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
1222 
1223 }
1224 EXPORT_SYMBOL(mt76_wcid_key_setup);
1225 
mt76_rx_signal(u8 chain_mask,s8 * chain_signal)1226 int mt76_rx_signal(u8 chain_mask, s8 *chain_signal)
1227 {
1228 	int signal = -128;
1229 	u8 chains;
1230 
1231 	for (chains = chain_mask; chains; chains >>= 1, chain_signal++) {
1232 		int cur, diff;
1233 
1234 		cur = *chain_signal;
1235 		if (!(chains & BIT(0)) ||
1236 		    cur > 0)
1237 			continue;
1238 
1239 		if (cur > signal)
1240 			swap(cur, signal);
1241 
1242 		diff = signal - cur;
1243 		if (diff == 0)
1244 			signal += 3;
1245 		else if (diff <= 2)
1246 			signal += 2;
1247 		else if (diff <= 6)
1248 			signal += 1;
1249 	}
1250 
1251 	return signal;
1252 }
1253 EXPORT_SYMBOL(mt76_rx_signal);
1254 
1255 static void
mt76_rx_convert(struct mt76_dev * dev,struct sk_buff * skb,struct ieee80211_hw ** hw,struct ieee80211_sta ** sta)1256 mt76_rx_convert(struct mt76_dev *dev, struct sk_buff *skb,
1257 		struct ieee80211_hw **hw,
1258 		struct ieee80211_sta **sta)
1259 {
1260 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1261 	struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
1262 	struct mt76_rx_status mstat;
1263 
1264 	mstat = *((struct mt76_rx_status *)skb->cb);
1265 	memset(status, 0, sizeof(*status));
1266 
1267 	skb->priority = mstat.qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
1268 
1269 	status->flag = mstat.flag;
1270 	status->freq = mstat.freq;
1271 	status->enc_flags = mstat.enc_flags;
1272 	status->encoding = mstat.encoding;
1273 	status->bw = mstat.bw;
1274 	if (status->encoding == RX_ENC_EHT) {
1275 		status->eht.ru = mstat.eht.ru;
1276 		status->eht.gi = mstat.eht.gi;
1277 	} else {
1278 		status->he_ru = mstat.he_ru;
1279 		status->he_gi = mstat.he_gi;
1280 		status->he_dcm = mstat.he_dcm;
1281 	}
1282 	status->rate_idx = mstat.rate_idx;
1283 	status->nss = mstat.nss;
1284 	status->band = mstat.band;
1285 	status->signal = mstat.signal;
1286 	status->chains = mstat.chains;
1287 	status->ampdu_reference = mstat.ampdu_ref;
1288 	status->device_timestamp = mstat.timestamp;
1289 	status->mactime = mstat.timestamp;
1290 	status->signal = mt76_rx_signal(mstat.chains, mstat.chain_signal);
1291 	if (status->signal <= -128)
1292 		status->flag |= RX_FLAG_NO_SIGNAL_VAL;
1293 
1294 	if (ieee80211_is_beacon(hdr->frame_control) ||
1295 	    ieee80211_is_probe_resp(hdr->frame_control))
1296 		status->boottime_ns = ktime_get_boottime_ns();
1297 
1298 	BUILD_BUG_ON(sizeof(mstat) > sizeof(skb->cb));
1299 	BUILD_BUG_ON(sizeof(status->chain_signal) !=
1300 		     sizeof(mstat.chain_signal));
1301 	memcpy(status->chain_signal, mstat.chain_signal,
1302 	       sizeof(mstat.chain_signal));
1303 
1304 	if (mstat.wcid) {
1305 		status->link_valid = mstat.wcid->link_valid;
1306 		status->link_id = mstat.wcid->link_id;
1307 	}
1308 
1309 	*sta = wcid_to_sta(mstat.wcid);
1310 	*hw = mt76_phy_hw(dev, mstat.phy_idx);
1311 }
1312 
1313 static void
mt76_check_ccmp_pn(struct sk_buff * skb)1314 mt76_check_ccmp_pn(struct sk_buff *skb)
1315 {
1316 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1317 	struct mt76_wcid *wcid = status->wcid;
1318 	struct ieee80211_hdr *hdr;
1319 	int security_idx;
1320 	int ret;
1321 
1322 	if (!(status->flag & RX_FLAG_DECRYPTED))
1323 		return;
1324 
1325 	if (status->flag & RX_FLAG_ONLY_MONITOR)
1326 		return;
1327 
1328 	if (!wcid || !wcid->rx_check_pn)
1329 		return;
1330 
1331 	security_idx = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
1332 	if (status->flag & RX_FLAG_8023)
1333 		goto skip_hdr_check;
1334 
1335 	hdr = mt76_skb_get_hdr(skb);
1336 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1337 		/*
1338 		 * Validate the first fragment both here and in mac80211
1339 		 * All further fragments will be validated by mac80211 only.
1340 		 */
1341 		if (ieee80211_is_frag(hdr) &&
1342 		    !ieee80211_is_first_frag(hdr->seq_ctrl))
1343 			return;
1344 	}
1345 
1346 	/* IEEE 802.11-2020, 12.5.3.4.4 "PN and replay detection" c):
1347 	 *
1348 	 * the recipient shall maintain a single replay counter for received
1349 	 * individually addressed robust Management frames that are received
1350 	 * with the To DS subfield equal to 0, [...]
1351 	 */
1352 	if (ieee80211_is_mgmt(hdr->frame_control) &&
1353 	    !ieee80211_has_tods(hdr->frame_control))
1354 		security_idx = IEEE80211_NUM_TIDS;
1355 
1356 skip_hdr_check:
1357 	BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0]));
1358 	ret = memcmp(status->iv, wcid->rx_key_pn[security_idx],
1359 		     sizeof(status->iv));
1360 	if (ret <= 0) {
1361 		status->flag |= RX_FLAG_ONLY_MONITOR;
1362 		return;
1363 	}
1364 
1365 	memcpy(wcid->rx_key_pn[security_idx], status->iv, sizeof(status->iv));
1366 
1367 	if (status->flag & RX_FLAG_IV_STRIPPED)
1368 		status->flag |= RX_FLAG_PN_VALIDATED;
1369 }
1370 
1371 static void
mt76_airtime_report(struct mt76_dev * dev,struct mt76_rx_status * status,int len)1372 mt76_airtime_report(struct mt76_dev *dev, struct mt76_rx_status *status,
1373 		    int len)
1374 {
1375 	struct mt76_wcid *wcid = status->wcid;
1376 	struct ieee80211_rx_status info = {
1377 		.enc_flags = status->enc_flags,
1378 		.rate_idx = status->rate_idx,
1379 		.encoding = status->encoding,
1380 		.band = status->band,
1381 		.nss = status->nss,
1382 		.bw = status->bw,
1383 	};
1384 	struct ieee80211_sta *sta;
1385 	u32 airtime;
1386 	u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
1387 
1388 	airtime = ieee80211_calc_rx_airtime(dev->hw, &info, len);
1389 	spin_lock(&dev->cc_lock);
1390 	dev->cur_cc_bss_rx += airtime;
1391 	spin_unlock(&dev->cc_lock);
1392 
1393 	if (!wcid || !wcid->sta)
1394 		return;
1395 
1396 	sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
1397 	ieee80211_sta_register_airtime(sta, tidno, 0, airtime);
1398 }
1399 
1400 static void
mt76_airtime_flush_ampdu(struct mt76_dev * dev)1401 mt76_airtime_flush_ampdu(struct mt76_dev *dev)
1402 {
1403 	struct mt76_wcid *wcid;
1404 	int wcid_idx;
1405 
1406 	if (!dev->rx_ampdu_len)
1407 		return;
1408 
1409 	wcid_idx = dev->rx_ampdu_status.wcid_idx;
1410 	if (wcid_idx < ARRAY_SIZE(dev->wcid))
1411 		wcid = rcu_dereference(dev->wcid[wcid_idx]);
1412 	else
1413 		wcid = NULL;
1414 	dev->rx_ampdu_status.wcid = wcid;
1415 
1416 	mt76_airtime_report(dev, &dev->rx_ampdu_status, dev->rx_ampdu_len);
1417 
1418 	dev->rx_ampdu_len = 0;
1419 	dev->rx_ampdu_ref = 0;
1420 }
1421 
1422 static void
mt76_airtime_check(struct mt76_dev * dev,struct sk_buff * skb)1423 mt76_airtime_check(struct mt76_dev *dev, struct sk_buff *skb)
1424 {
1425 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1426 	struct mt76_wcid *wcid = status->wcid;
1427 
1428 	if (!(dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME))
1429 		return;
1430 
1431 	if (!wcid || !wcid->sta) {
1432 		struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
1433 
1434 		if (status->flag & RX_FLAG_8023)
1435 			return;
1436 
1437 		if (!ether_addr_equal(hdr->addr1, dev->phy.macaddr))
1438 			return;
1439 
1440 		wcid = NULL;
1441 	}
1442 
1443 	if (!(status->flag & RX_FLAG_AMPDU_DETAILS) ||
1444 	    status->ampdu_ref != dev->rx_ampdu_ref)
1445 		mt76_airtime_flush_ampdu(dev);
1446 
1447 	if (status->flag & RX_FLAG_AMPDU_DETAILS) {
1448 		if (!dev->rx_ampdu_len ||
1449 		    status->ampdu_ref != dev->rx_ampdu_ref) {
1450 			dev->rx_ampdu_status = *status;
1451 			dev->rx_ampdu_status.wcid_idx = wcid ? wcid->idx : 0xff;
1452 			dev->rx_ampdu_ref = status->ampdu_ref;
1453 		}
1454 
1455 		dev->rx_ampdu_len += skb->len;
1456 		return;
1457 	}
1458 
1459 	mt76_airtime_report(dev, status, skb->len);
1460 }
1461 
1462 static void
mt76_check_sta(struct mt76_dev * dev,struct sk_buff * skb)1463 mt76_check_sta(struct mt76_dev *dev, struct sk_buff *skb)
1464 {
1465 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1466 	struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
1467 	struct ieee80211_sta *sta;
1468 	struct ieee80211_hw *hw;
1469 	struct mt76_wcid *wcid = status->wcid;
1470 	u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
1471 	bool ps;
1472 
1473 	hw = mt76_phy_hw(dev, status->phy_idx);
1474 	if (ieee80211_is_pspoll(hdr->frame_control) && !wcid &&
1475 	    !(status->flag & RX_FLAG_8023)) {
1476 		sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr2, NULL);
1477 		if (sta)
1478 			wcid = status->wcid = (struct mt76_wcid *)sta->drv_priv;
1479 	}
1480 
1481 	mt76_airtime_check(dev, skb);
1482 
1483 	if (!wcid || !wcid->sta)
1484 		return;
1485 
1486 	sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
1487 
1488 	if (status->signal <= 0)
1489 		ewma_signal_add(&wcid->rssi, -status->signal);
1490 
1491 	wcid->inactive_count = 0;
1492 
1493 	if (status->flag & RX_FLAG_8023)
1494 		return;
1495 
1496 	if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags))
1497 		return;
1498 
1499 	if (ieee80211_is_pspoll(hdr->frame_control)) {
1500 		ieee80211_sta_pspoll(sta);
1501 		return;
1502 	}
1503 
1504 	if (ieee80211_has_morefrags(hdr->frame_control) ||
1505 	    !(ieee80211_is_mgmt(hdr->frame_control) ||
1506 	      ieee80211_is_data(hdr->frame_control)))
1507 		return;
1508 
1509 	ps = ieee80211_has_pm(hdr->frame_control);
1510 
1511 	if (ps && (ieee80211_is_data_qos(hdr->frame_control) ||
1512 		   ieee80211_is_qos_nullfunc(hdr->frame_control)))
1513 		ieee80211_sta_uapsd_trigger(sta, tidno);
1514 
1515 	if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps)
1516 		return;
1517 
1518 	if (ps)
1519 		set_bit(MT_WCID_FLAG_PS, &wcid->flags);
1520 
1521 	if (dev->drv->sta_ps)
1522 		dev->drv->sta_ps(dev, sta, ps);
1523 
1524 	if (!ps)
1525 		clear_bit(MT_WCID_FLAG_PS, &wcid->flags);
1526 
1527 	ieee80211_sta_ps_transition(sta, ps);
1528 }
1529 
mt76_rx_complete(struct mt76_dev * dev,struct sk_buff_head * frames,struct napi_struct * napi)1530 void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
1531 		      struct napi_struct *napi)
1532 {
1533 	struct ieee80211_sta *sta;
1534 	struct ieee80211_hw *hw;
1535 	struct sk_buff *skb, *tmp;
1536 	LIST_HEAD(list);
1537 
1538 	spin_lock(&dev->rx_lock);
1539 	while ((skb = __skb_dequeue(frames)) != NULL) {
1540 		struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1541 
1542 		mt76_check_ccmp_pn(skb);
1543 		skb_shinfo(skb)->frag_list = NULL;
1544 		mt76_rx_convert(dev, skb, &hw, &sta);
1545 		ieee80211_rx_list(hw, sta, skb, &list);
1546 
1547 		/* subsequent amsdu frames */
1548 		while (nskb) {
1549 			skb = nskb;
1550 			nskb = nskb->next;
1551 			skb->next = NULL;
1552 
1553 			mt76_rx_convert(dev, skb, &hw, &sta);
1554 			ieee80211_rx_list(hw, sta, skb, &list);
1555 		}
1556 	}
1557 	spin_unlock(&dev->rx_lock);
1558 
1559 	if (!napi) {
1560 		netif_receive_skb_list(&list);
1561 		return;
1562 	}
1563 
1564 	list_for_each_entry_safe(skb, tmp, &list, list) {
1565 		skb_list_del_init(skb);
1566 		napi_gro_receive(napi, skb);
1567 	}
1568 }
1569 
mt76_rx_poll_complete(struct mt76_dev * dev,enum mt76_rxq_id q,struct napi_struct * napi)1570 void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
1571 			   struct napi_struct *napi)
1572 {
1573 	struct sk_buff_head frames;
1574 	struct sk_buff *skb;
1575 
1576 	__skb_queue_head_init(&frames);
1577 
1578 	while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) {
1579 		mt76_check_sta(dev, skb);
1580 		if (mtk_wed_device_active(&dev->mmio.wed) ||
1581 		    mt76_npu_device_active(dev))
1582 			__skb_queue_tail(&frames, skb);
1583 		else
1584 			mt76_rx_aggr_reorder(skb, &frames);
1585 	}
1586 
1587 	mt76_rx_complete(dev, &frames, napi);
1588 }
1589 EXPORT_SYMBOL_GPL(mt76_rx_poll_complete);
1590 
1591 static int
mt76_sta_add(struct mt76_phy * phy,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1592 mt76_sta_add(struct mt76_phy *phy, struct ieee80211_vif *vif,
1593 	     struct ieee80211_sta *sta)
1594 {
1595 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1596 	struct mt76_dev *dev = phy->dev;
1597 	struct mt76_wcid *published;
1598 	int ret;
1599 	int i;
1600 
1601 	mutex_lock(&dev->mutex);
1602 
1603 	ret = dev->drv->sta_add(dev, vif, sta);
1604 	if (ret)
1605 		goto out;
1606 
1607 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1608 		struct mt76_txq *mtxq;
1609 
1610 		if (!sta->txq[i])
1611 			continue;
1612 
1613 		mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv;
1614 		mtxq->wcid = wcid->idx;
1615 	}
1616 
1617 	published = rcu_dereference_protected(dev->wcid[wcid->idx],
1618 					      lockdep_is_held(&dev->mutex));
1619 	if (published != wcid) {
1620 		WARN_ON_ONCE(published);
1621 		ewma_signal_init(&wcid->rssi);
1622 		rcu_assign_pointer(dev->wcid[wcid->idx], wcid);
1623 		mt76_wcid_init(wcid, phy->band_idx);
1624 	} else {
1625 		wcid->phy_idx = phy->band_idx;
1626 	}
1627 
1628 	phy->num_sta++;
1629 
1630 out:
1631 	mutex_unlock(&dev->mutex);
1632 
1633 	return ret;
1634 }
1635 
__mt76_sta_remove(struct mt76_phy * phy,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1636 void __mt76_sta_remove(struct mt76_phy *phy, struct ieee80211_vif *vif,
1637 		       struct ieee80211_sta *sta)
1638 {
1639 	struct mt76_dev *dev = phy->dev;
1640 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1641 	int i, idx = wcid->idx;
1642 
1643 	for (i = 0; i < ARRAY_SIZE(wcid->aggr); i++)
1644 		mt76_rx_aggr_stop(dev, wcid, i);
1645 
1646 	if (dev->drv->sta_remove)
1647 		dev->drv->sta_remove(dev, vif, sta);
1648 
1649 	mt76_wcid_cleanup(dev, wcid);
1650 
1651 	mt76_wcid_mask_clear(dev->wcid_mask, idx);
1652 	phy->num_sta--;
1653 }
1654 EXPORT_SYMBOL_GPL(__mt76_sta_remove);
1655 
1656 static void
mt76_sta_remove(struct mt76_phy * phy,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1657 mt76_sta_remove(struct mt76_phy *phy, struct ieee80211_vif *vif,
1658 		struct ieee80211_sta *sta)
1659 {
1660 	struct mt76_dev *dev = phy->dev;
1661 
1662 	mutex_lock(&dev->mutex);
1663 	__mt76_sta_remove(phy, vif, sta);
1664 	mutex_unlock(&dev->mutex);
1665 }
1666 
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)1667 int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1668 		   struct ieee80211_sta *sta,
1669 		   enum ieee80211_sta_state old_state,
1670 		   enum ieee80211_sta_state new_state)
1671 {
1672 	struct mt76_phy *phy = hw->priv;
1673 	struct mt76_dev *dev = phy->dev;
1674 	enum mt76_sta_event ev;
1675 
1676 	phy = mt76_vif_phy(hw, vif);
1677 	if (!phy)
1678 		return -EINVAL;
1679 
1680 	if (old_state == IEEE80211_STA_NOTEXIST &&
1681 	    new_state == IEEE80211_STA_NONE)
1682 		return mt76_sta_add(phy, vif, sta);
1683 
1684 	if (old_state == IEEE80211_STA_NONE &&
1685 	    new_state == IEEE80211_STA_NOTEXIST)
1686 		mt76_sta_remove(phy, vif, sta);
1687 
1688 	if (!dev->drv->sta_event)
1689 		return 0;
1690 
1691 	if (old_state == IEEE80211_STA_AUTH &&
1692 	    new_state == IEEE80211_STA_ASSOC)
1693 		ev = MT76_STA_EVENT_ASSOC;
1694 	else if (old_state == IEEE80211_STA_ASSOC &&
1695 		 new_state == IEEE80211_STA_AUTHORIZED)
1696 		ev = MT76_STA_EVENT_AUTHORIZE;
1697 	else if (old_state == IEEE80211_STA_ASSOC &&
1698 		 new_state == IEEE80211_STA_AUTH)
1699 		ev = MT76_STA_EVENT_DISASSOC;
1700 	else
1701 		return 0;
1702 
1703 	return dev->drv->sta_event(dev, vif, sta, ev);
1704 }
1705 EXPORT_SYMBOL_GPL(mt76_sta_state);
1706 
mt76_sta_pre_rcu_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1707 void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1708 			     struct ieee80211_sta *sta)
1709 {
1710 	struct mt76_phy *phy = hw->priv;
1711 	struct mt76_dev *dev = phy->dev;
1712 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1713 
1714 	mutex_lock(&dev->mutex);
1715 	spin_lock_bh(&dev->status_lock);
1716 	rcu_assign_pointer(dev->wcid[wcid->idx], NULL);
1717 	spin_unlock_bh(&dev->status_lock);
1718 	mutex_unlock(&dev->mutex);
1719 }
1720 EXPORT_SYMBOL_GPL(mt76_sta_pre_rcu_remove);
1721 
mt76_wcid_init(struct mt76_wcid * wcid,u8 band_idx)1722 void mt76_wcid_init(struct mt76_wcid *wcid, u8 band_idx)
1723 {
1724 	wcid->hw_key_idx = -1;
1725 	wcid->phy_idx = band_idx;
1726 
1727 	INIT_LIST_HEAD(&wcid->tx_list);
1728 	skb_queue_head_init(&wcid->tx_pending);
1729 	skb_queue_head_init(&wcid->tx_offchannel);
1730 
1731 	INIT_LIST_HEAD(&wcid->list);
1732 	idr_init(&wcid->pktid);
1733 
1734 	INIT_LIST_HEAD(&wcid->poll_list);
1735 }
1736 EXPORT_SYMBOL_GPL(mt76_wcid_init);
1737 
mt76_wcid_cleanup(struct mt76_dev * dev,struct mt76_wcid * wcid)1738 void mt76_wcid_cleanup(struct mt76_dev *dev, struct mt76_wcid *wcid)
1739 {
1740 	struct mt76_phy *phy = mt76_dev_phy(dev, wcid->phy_idx);
1741 	struct ieee80211_hw *hw;
1742 	struct sk_buff_head list;
1743 	struct sk_buff *skb;
1744 
1745 	mt76_tx_status_lock(dev, &list);
1746 	mt76_tx_status_skb_get(dev, wcid, -1, &list);
1747 	mt76_tx_status_unlock(dev, &list);
1748 
1749 	idr_destroy(&wcid->pktid);
1750 
1751 	/* Remove from sta_poll_list to prevent list corruption after reset.
1752 	 * Without this, mt76_reset_device() reinitializes sta_poll_list but
1753 	 * leaves wcid->poll_list with stale pointers, causing list corruption
1754 	 * when mt76_wcid_add_poll() checks list_empty().
1755 	 */
1756 	spin_lock_bh(&dev->sta_poll_lock);
1757 	if (!list_empty(&wcid->poll_list))
1758 		list_del_init(&wcid->poll_list);
1759 	spin_unlock_bh(&dev->sta_poll_lock);
1760 
1761 	spin_lock_bh(&phy->tx_lock);
1762 
1763 	if (!list_empty(&wcid->tx_list))
1764 		list_del_init(&wcid->tx_list);
1765 
1766 	spin_lock(&wcid->tx_pending.lock);
1767 	skb_queue_splice_tail_init(&wcid->tx_pending, &list);
1768 	spin_unlock(&wcid->tx_pending.lock);
1769 
1770 	spin_lock(&wcid->tx_offchannel.lock);
1771 	skb_queue_splice_tail_init(&wcid->tx_offchannel, &list);
1772 	spin_unlock(&wcid->tx_offchannel.lock);
1773 
1774 	spin_unlock_bh(&phy->tx_lock);
1775 
1776 	while ((skb = __skb_dequeue(&list)) != NULL) {
1777 		hw = mt76_tx_status_get_hw(dev, skb);
1778 		ieee80211_free_txskb(hw, skb);
1779 	}
1780 }
1781 EXPORT_SYMBOL_GPL(mt76_wcid_cleanup);
1782 
mt76_wcid_add_poll(struct mt76_dev * dev,struct mt76_wcid * wcid)1783 void mt76_wcid_add_poll(struct mt76_dev *dev, struct mt76_wcid *wcid)
1784 {
1785 	if (test_bit(MT76_MCU_RESET, &dev->phy.state) || !wcid->sta)
1786 		return;
1787 
1788 	spin_lock_bh(&dev->sta_poll_lock);
1789 	if (list_empty(&wcid->poll_list))
1790 		list_add_tail(&wcid->poll_list, &dev->sta_poll_list);
1791 	spin_unlock_bh(&dev->sta_poll_lock);
1792 }
1793 EXPORT_SYMBOL_GPL(mt76_wcid_add_poll);
1794 
mt76_get_power_bound(struct mt76_phy * phy,s8 txpower)1795 s8 mt76_get_power_bound(struct mt76_phy *phy, s8 txpower)
1796 {
1797 	int n_chains = hweight16(phy->chainmask);
1798 
1799 	txpower = mt76_get_sar_power(phy, phy->chandef.chan, txpower * 2);
1800 	txpower -= mt76_tx_power_path_delta(n_chains);
1801 
1802 	return txpower;
1803 }
1804 EXPORT_SYMBOL_GPL(mt76_get_power_bound);
1805 
mt76_get_txpower(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,int * dbm)1806 int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1807 		     unsigned int link_id, int *dbm)
1808 {
1809 	struct mt76_phy *phy = mt76_vif_phy(hw, vif);
1810 	int n_chains, delta;
1811 
1812 	if (!phy)
1813 		return -EINVAL;
1814 
1815 	n_chains = hweight16(phy->chainmask);
1816 	delta = mt76_tx_power_path_delta(n_chains);
1817 	*dbm = DIV_ROUND_UP(phy->txpower_cur + delta, 2);
1818 
1819 	return 0;
1820 }
1821 EXPORT_SYMBOL_GPL(mt76_get_txpower);
1822 
mt76_init_sar_power(struct ieee80211_hw * hw,const struct cfg80211_sar_specs * sar)1823 int mt76_init_sar_power(struct ieee80211_hw *hw,
1824 			const struct cfg80211_sar_specs *sar)
1825 {
1826 	struct mt76_phy *phy = hw->priv;
1827 	const struct cfg80211_sar_capa *capa = hw->wiphy->sar_capa;
1828 	int i;
1829 
1830 	if (sar->type != NL80211_SAR_TYPE_POWER || !sar->num_sub_specs)
1831 		return -EINVAL;
1832 
1833 	for (i = 0; i < sar->num_sub_specs; i++) {
1834 		u32 index = sar->sub_specs[i].freq_range_index;
1835 		/* SAR specifies power limitaton in 0.25dbm */
1836 		s32 power = sar->sub_specs[i].power >> 1;
1837 
1838 		if (power > 127 || power < -127)
1839 			power = 127;
1840 
1841 		phy->frp[index].range = &capa->freq_ranges[index];
1842 		phy->frp[index].power = power;
1843 	}
1844 
1845 	return 0;
1846 }
1847 EXPORT_SYMBOL_GPL(mt76_init_sar_power);
1848 
mt76_get_sar_power(struct mt76_phy * phy,struct ieee80211_channel * chan,int power)1849 int mt76_get_sar_power(struct mt76_phy *phy,
1850 		       struct ieee80211_channel *chan,
1851 		       int power)
1852 {
1853 	const struct cfg80211_sar_capa *capa = phy->hw->wiphy->sar_capa;
1854 	int freq, i;
1855 
1856 	if (!capa || !phy->frp)
1857 		return power;
1858 
1859 	if (power > 127 || power < -127)
1860 		power = 127;
1861 
1862 	freq = ieee80211_channel_to_frequency(chan->hw_value, chan->band);
1863 	for (i = 0 ; i < capa->num_freq_ranges; i++) {
1864 		if (phy->frp[i].range &&
1865 		    freq >= phy->frp[i].range->start_freq &&
1866 		    freq < phy->frp[i].range->end_freq) {
1867 			power = min_t(int, phy->frp[i].power, power);
1868 			break;
1869 		}
1870 	}
1871 
1872 	return power;
1873 }
1874 EXPORT_SYMBOL_GPL(mt76_get_sar_power);
1875 
1876 static void
__mt76_csa_finish(void * priv,u8 * mac,struct ieee80211_vif * vif)1877 __mt76_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif)
1878 {
1879 	if (vif->bss_conf.csa_active && ieee80211_beacon_cntdwn_is_complete(vif, 0))
1880 		ieee80211_csa_finish(vif, 0);
1881 }
1882 
mt76_csa_finish(struct mt76_dev * dev)1883 void mt76_csa_finish(struct mt76_dev *dev)
1884 {
1885 	if (!dev->csa_complete)
1886 		return;
1887 
1888 	ieee80211_iterate_active_interfaces_atomic(dev->hw,
1889 		IEEE80211_IFACE_ITER_RESUME_ALL,
1890 		__mt76_csa_finish, dev);
1891 
1892 	dev->csa_complete = 0;
1893 }
1894 EXPORT_SYMBOL_GPL(mt76_csa_finish);
1895 
1896 static void
__mt76_csa_check(void * priv,u8 * mac,struct ieee80211_vif * vif)1897 __mt76_csa_check(void *priv, u8 *mac, struct ieee80211_vif *vif)
1898 {
1899 	struct mt76_dev *dev = priv;
1900 
1901 	if (!vif->bss_conf.csa_active)
1902 		return;
1903 
1904 	dev->csa_complete |= ieee80211_beacon_cntdwn_is_complete(vif, 0);
1905 }
1906 
mt76_csa_check(struct mt76_dev * dev)1907 void mt76_csa_check(struct mt76_dev *dev)
1908 {
1909 	ieee80211_iterate_active_interfaces_atomic(dev->hw,
1910 		IEEE80211_IFACE_ITER_RESUME_ALL,
1911 		__mt76_csa_check, dev);
1912 }
1913 EXPORT_SYMBOL_GPL(mt76_csa_check);
1914 
1915 int
mt76_set_tim(struct ieee80211_hw * hw,struct ieee80211_sta * sta,bool set)1916 mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
1917 {
1918 	return 0;
1919 }
1920 EXPORT_SYMBOL_GPL(mt76_set_tim);
1921 
mt76_insert_ccmp_hdr(struct sk_buff * skb,u8 key_id)1922 void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id)
1923 {
1924 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1925 	int hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1926 	u8 *hdr, *pn = status->iv;
1927 
1928 	__skb_push(skb, 8);
1929 	memmove(skb->data, skb->data + 8, hdr_len);
1930 	hdr = skb->data + hdr_len;
1931 
1932 	hdr[0] = pn[5];
1933 	hdr[1] = pn[4];
1934 	hdr[2] = 0;
1935 	hdr[3] = 0x20 | (key_id << 6);
1936 	hdr[4] = pn[3];
1937 	hdr[5] = pn[2];
1938 	hdr[6] = pn[1];
1939 	hdr[7] = pn[0];
1940 
1941 	status->flag &= ~RX_FLAG_IV_STRIPPED;
1942 }
1943 EXPORT_SYMBOL_GPL(mt76_insert_ccmp_hdr);
1944 
mt76_get_rate(struct mt76_dev * dev,struct ieee80211_supported_band * sband,int idx,bool cck)1945 int mt76_get_rate(struct mt76_dev *dev,
1946 		  struct ieee80211_supported_band *sband,
1947 		  int idx, bool cck)
1948 {
1949 	bool is_2g = sband->band == NL80211_BAND_2GHZ;
1950 	int i, offset = 0, len = sband->n_bitrates;
1951 
1952 	if (cck) {
1953 		if (!is_2g)
1954 			return 0;
1955 
1956 		idx &= ~BIT(2); /* short preamble */
1957 	} else if (is_2g) {
1958 		offset = 4;
1959 	}
1960 
1961 	for (i = offset; i < len; i++) {
1962 		if ((sband->bitrates[i].hw_value & GENMASK(7, 0)) == idx)
1963 			return i;
1964 	}
1965 
1966 	return 0;
1967 }
1968 EXPORT_SYMBOL_GPL(mt76_get_rate);
1969 
mt76_sw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const u8 * mac)1970 void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1971 		  const u8 *mac)
1972 {
1973 	struct mt76_phy *phy = hw->priv;
1974 
1975 	set_bit(MT76_SCANNING, &phy->state);
1976 }
1977 EXPORT_SYMBOL_GPL(mt76_sw_scan);
1978 
mt76_sw_scan_complete(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1979 void mt76_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1980 {
1981 	struct mt76_phy *phy = hw->priv;
1982 
1983 	clear_bit(MT76_SCANNING, &phy->state);
1984 }
1985 EXPORT_SYMBOL_GPL(mt76_sw_scan_complete);
1986 
mt76_get_antenna(struct ieee80211_hw * hw,int radio_idx,u32 * tx_ant,u32 * rx_ant)1987 int mt76_get_antenna(struct ieee80211_hw *hw, int radio_idx, u32 *tx_ant,
1988 		     u32 *rx_ant)
1989 {
1990 	struct mt76_phy *phy = hw->priv;
1991 	struct mt76_dev *dev = phy->dev;
1992 	int i;
1993 
1994 	mutex_lock(&dev->mutex);
1995 	*tx_ant = 0;
1996 	for (i = 0; i < ARRAY_SIZE(dev->phys); i++)
1997 		if (dev->phys[i] && dev->phys[i]->hw == hw)
1998 			*tx_ant |= dev->phys[i]->chainmask;
1999 	*rx_ant = *tx_ant;
2000 	mutex_unlock(&dev->mutex);
2001 
2002 	return 0;
2003 }
2004 EXPORT_SYMBOL_GPL(mt76_get_antenna);
2005 
2006 struct mt76_queue *
mt76_init_queue(struct mt76_dev * dev,int qid,int idx,int n_desc,int ring_base,void * wed,u32 flags)2007 mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc,
2008 		int ring_base, void *wed, u32 flags)
2009 {
2010 	struct mt76_queue *hwq;
2011 	int err;
2012 
2013 	hwq = devm_kzalloc(dev->dev, sizeof(*hwq), GFP_KERNEL);
2014 	if (!hwq)
2015 		return ERR_PTR(-ENOMEM);
2016 
2017 	hwq->flags = flags;
2018 	hwq->wed = wed;
2019 
2020 	err = dev->queue_ops->alloc(dev, hwq, idx, n_desc, 0, ring_base);
2021 	if (err < 0)
2022 		return ERR_PTR(err);
2023 
2024 	return hwq;
2025 }
2026 EXPORT_SYMBOL_GPL(mt76_init_queue);
2027 
mt76_ethtool_worker(struct mt76_ethtool_worker_info * wi,struct mt76_sta_stats * stats,bool eht)2028 void mt76_ethtool_worker(struct mt76_ethtool_worker_info *wi,
2029 			 struct mt76_sta_stats *stats, bool eht)
2030 {
2031 	int i, ei = wi->initial_stat_idx;
2032 	u64 *data = wi->data;
2033 
2034 	wi->sta_count++;
2035 
2036 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_CCK];
2037 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_OFDM];
2038 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_HT];
2039 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_HT_GF];
2040 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_VHT];
2041 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_SU];
2042 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_EXT_SU];
2043 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_TB];
2044 	data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_MU];
2045 	if (eht) {
2046 		data[ei++] += stats->tx_mode[MT_PHY_TYPE_EHT_SU];
2047 		data[ei++] += stats->tx_mode[MT_PHY_TYPE_EHT_TRIG];
2048 		data[ei++] += stats->tx_mode[MT_PHY_TYPE_EHT_MU];
2049 	}
2050 
2051 	for (i = 0; i < (ARRAY_SIZE(stats->tx_bw) - !eht); i++)
2052 		data[ei++] += stats->tx_bw[i];
2053 
2054 	for (i = 0; i < (eht ? 14 : 12); i++)
2055 		data[ei++] += stats->tx_mcs[i];
2056 
2057 	for (i = 0; i < 4; i++)
2058 		data[ei++] += stats->tx_nss[i];
2059 
2060 	wi->worker_stat_count = ei - wi->initial_stat_idx;
2061 }
2062 EXPORT_SYMBOL_GPL(mt76_ethtool_worker);
2063 
mt76_ethtool_page_pool_stats(struct mt76_dev * dev,u64 * data,int * index)2064 void mt76_ethtool_page_pool_stats(struct mt76_dev *dev, u64 *data, int *index)
2065 {
2066 #ifdef CONFIG_PAGE_POOL_STATS
2067 	struct page_pool_stats stats = {};
2068 	int i;
2069 
2070 	mt76_for_each_q_rx(dev, i)
2071 		page_pool_get_stats(dev->q_rx[i].page_pool, &stats);
2072 
2073 	page_pool_ethtool_stats_get(data, &stats);
2074 	*index += page_pool_ethtool_stats_get_count();
2075 #endif
2076 }
2077 EXPORT_SYMBOL_GPL(mt76_ethtool_page_pool_stats);
2078 
mt76_phy_dfs_state(struct mt76_phy * phy)2079 enum mt76_dfs_state mt76_phy_dfs_state(struct mt76_phy *phy)
2080 {
2081 	struct ieee80211_hw *hw = phy->hw;
2082 	struct mt76_dev *dev = phy->dev;
2083 
2084 	if (dev->region == NL80211_DFS_UNSET ||
2085 	    test_bit(MT76_SCANNING, &phy->state))
2086 		return MT_DFS_STATE_DISABLED;
2087 
2088 	if (!phy->radar_enabled) {
2089 		if ((hw->conf.flags & IEEE80211_CONF_MONITOR) &&
2090 		    (phy->chandef.chan->flags & IEEE80211_CHAN_RADAR))
2091 			return MT_DFS_STATE_ACTIVE;
2092 
2093 		return MT_DFS_STATE_DISABLED;
2094 	}
2095 
2096 	if (!cfg80211_reg_can_beacon(hw->wiphy, &phy->chandef, NL80211_IFTYPE_AP))
2097 		return MT_DFS_STATE_CAC;
2098 
2099 	return MT_DFS_STATE_ACTIVE;
2100 }
2101 EXPORT_SYMBOL_GPL(mt76_phy_dfs_state);
2102 
mt76_vif_cleanup(struct mt76_dev * dev,struct ieee80211_vif * vif)2103 void mt76_vif_cleanup(struct mt76_dev *dev, struct ieee80211_vif *vif)
2104 {
2105 	struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv;
2106 	struct mt76_vif_data *mvif = mlink->mvif;
2107 
2108 	rcu_assign_pointer(mvif->link[0], NULL);
2109 	mt76_abort_scan(dev);
2110 	if (mvif->roc_phy)
2111 		mt76_abort_roc(mvif->roc_phy);
2112 }
2113 EXPORT_SYMBOL_GPL(mt76_vif_cleanup);
2114 
mt76_select_links(struct ieee80211_vif * vif,int max_active_links)2115 u16 mt76_select_links(struct ieee80211_vif *vif, int max_active_links)
2116 {
2117 	unsigned long usable_links = ieee80211_vif_usable_links(vif);
2118 	struct  {
2119 		u8 link_id;
2120 		enum nl80211_band band;
2121 	} data[IEEE80211_MLD_MAX_NUM_LINKS];
2122 	unsigned int link_id;
2123 	int i, n_data = 0;
2124 	u16 sel_links = 0;
2125 
2126 	if (!ieee80211_vif_is_mld(vif))
2127 		return 0;
2128 
2129 	if (vif->active_links == usable_links)
2130 		return vif->active_links;
2131 
2132 	rcu_read_lock();
2133 	for_each_set_bit(link_id, &usable_links, IEEE80211_MLD_MAX_NUM_LINKS) {
2134 		struct ieee80211_bss_conf *link_conf;
2135 
2136 		link_conf = rcu_dereference(vif->link_conf[link_id]);
2137 		if (WARN_ON_ONCE(!link_conf))
2138 			continue;
2139 
2140 		data[n_data].link_id = link_id;
2141 		data[n_data].band = link_conf->chanreq.oper.chan->band;
2142 		n_data++;
2143 	}
2144 	rcu_read_unlock();
2145 
2146 	for (i = 0; i < n_data; i++) {
2147 		int j;
2148 
2149 		if (!(BIT(data[i].link_id) & vif->active_links))
2150 			continue;
2151 
2152 		sel_links = BIT(data[i].link_id);
2153 		for (j = 0; j < n_data; j++) {
2154 			if (data[i].band != data[j].band) {
2155 				sel_links |= BIT(data[j].link_id);
2156 				if (hweight16(sel_links) == max_active_links)
2157 					break;
2158 			}
2159 		}
2160 		break;
2161 	}
2162 
2163 	return sel_links;
2164 }
2165 EXPORT_SYMBOL_GPL(mt76_select_links);
2166 
2167 struct mt76_offchannel_cb_data {
2168 	struct mt76_phy *phy;
2169 	bool offchannel;
2170 };
2171 
2172 static void
mt76_offchannel_send_nullfunc(struct mt76_offchannel_cb_data * data,struct ieee80211_vif * vif,int link_id)2173 mt76_offchannel_send_nullfunc(struct mt76_offchannel_cb_data *data,
2174 			      struct ieee80211_vif *vif, int link_id)
2175 {
2176 	struct mt76_phy *phy = data->phy;
2177 	struct ieee80211_tx_info *info;
2178 	struct ieee80211_sta *sta = NULL;
2179 	struct ieee80211_hdr *hdr;
2180 	struct mt76_wcid *wcid;
2181 	struct sk_buff *skb;
2182 
2183 	skb = ieee80211_nullfunc_get(phy->hw, vif, link_id, true);
2184 	if (!skb)
2185 		return;
2186 
2187 	hdr = (struct ieee80211_hdr *)skb->data;
2188 	if (data->offchannel)
2189 		hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
2190 
2191 	skb->priority = 7;
2192 	skb_set_queue_mapping(skb, IEEE80211_AC_VO);
2193 
2194 	if (!ieee80211_tx_prepare_skb(phy->hw, vif, skb,
2195 				      phy->main_chandef.chan->band,
2196 				      &sta))
2197 		return;
2198 
2199 	if (sta)
2200 		wcid = (struct mt76_wcid *)sta->drv_priv;
2201 	else
2202 		wcid = ((struct mt76_vif_link *)vif->drv_priv)->wcid;
2203 
2204 	if (link_id >= 0) {
2205 		info = IEEE80211_SKB_CB(skb);
2206 		info->control.flags &= ~IEEE80211_TX_CTRL_MLO_LINK;
2207 		info->control.flags |=
2208 			u32_encode_bits(link_id, IEEE80211_TX_CTRL_MLO_LINK);
2209 	}
2210 
2211 	mt76_tx(phy, sta, wcid, skb);
2212 }
2213 
2214 static void
mt76_offchannel_notify_iter(void * _data,u8 * mac,struct ieee80211_vif * vif)2215 mt76_offchannel_notify_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
2216 {
2217 	struct mt76_offchannel_cb_data *data = _data;
2218 	struct mt76_vif_link *mlink;
2219 	struct mt76_vif_data *mvif;
2220 	int link_id;
2221 
2222 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
2223 		return;
2224 
2225 	mlink = (struct mt76_vif_link *)vif->drv_priv;
2226 	mvif = mlink->mvif;
2227 
2228 	if (!ieee80211_vif_is_mld(vif)) {
2229 		if (mt76_vif_link_phy(mlink) == data->phy) {
2230 			if (!data->offchannel && mlink->beacon_mon_interval)
2231 				WRITE_ONCE(mlink->beacon_mon_last, jiffies);
2232 			mt76_offchannel_send_nullfunc(data, vif, -1);
2233 		}
2234 		return;
2235 	}
2236 
2237 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
2238 		if (link_id == mvif->deflink_id)
2239 			mlink = (struct mt76_vif_link *)vif->drv_priv;
2240 		else
2241 			mlink = rcu_dereference(mvif->link[link_id]);
2242 		if (!mlink)
2243 			continue;
2244 		if (mt76_vif_link_phy(mlink) != data->phy)
2245 			continue;
2246 
2247 		if (!data->offchannel && mlink->beacon_mon_interval)
2248 			WRITE_ONCE(mlink->beacon_mon_last, jiffies);
2249 
2250 		mt76_offchannel_send_nullfunc(data, vif, link_id);
2251 	}
2252 }
2253 
mt76_offchannel_notify(struct mt76_phy * phy,bool offchannel)2254 void mt76_offchannel_notify(struct mt76_phy *phy, bool offchannel)
2255 {
2256 	struct mt76_offchannel_cb_data data = {
2257 		.phy = phy,
2258 		.offchannel = offchannel,
2259 	};
2260 
2261 	if (!phy->num_sta)
2262 		return;
2263 
2264 	local_bh_disable();
2265 	ieee80211_iterate_active_interfaces_atomic(phy->hw,
2266 		IEEE80211_IFACE_ITER_NORMAL,
2267 		mt76_offchannel_notify_iter, &data);
2268 	local_bh_enable();
2269 }
2270 EXPORT_SYMBOL_GPL(mt76_offchannel_notify);
2271 
2272 struct mt76_rx_beacon_data {
2273 	struct mt76_phy *phy;
2274 	const u8 *bssid;
2275 };
2276 
mt76_rx_beacon_iter(void * _data,u8 * mac,struct ieee80211_vif * vif)2277 static void mt76_rx_beacon_iter(void *_data, u8 *mac,
2278 				struct ieee80211_vif *vif)
2279 {
2280 	struct mt76_rx_beacon_data *data = _data;
2281 	struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv;
2282 	struct mt76_vif_data *mvif = mlink->mvif;
2283 	int link_id;
2284 
2285 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
2286 		return;
2287 
2288 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
2289 		struct ieee80211_bss_conf *link_conf;
2290 
2291 		if (link_id == mvif->deflink_id)
2292 			mlink = (struct mt76_vif_link *)vif->drv_priv;
2293 		else
2294 			mlink = rcu_dereference(mvif->link[link_id]);
2295 		if (!mlink || !mlink->beacon_mon_interval)
2296 			continue;
2297 
2298 		if (mt76_vif_link_phy(mlink) != data->phy)
2299 			continue;
2300 
2301 		link_conf = rcu_dereference(vif->link_conf[link_id]);
2302 		if (!link_conf)
2303 			continue;
2304 
2305 		if (!ether_addr_equal(link_conf->bssid, data->bssid) &&
2306 		    (!link_conf->nontransmitted ||
2307 		     !ether_addr_equal(link_conf->transmitter_bssid,
2308 				       data->bssid)))
2309 			continue;
2310 
2311 		WRITE_ONCE(mlink->beacon_mon_last, jiffies);
2312 	}
2313 }
2314 
mt76_rx_beacon(struct mt76_phy * phy,struct sk_buff * skb)2315 void mt76_rx_beacon(struct mt76_phy *phy, struct sk_buff *skb)
2316 {
2317 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
2318 	struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
2319 	struct mt76_rx_beacon_data data = {
2320 		.phy = phy,
2321 		.bssid = hdr->addr3,
2322 	};
2323 
2324 	mt76_scan_rx_beacon(phy->dev, phy->chandef.chan);
2325 
2326 	if (!phy->num_sta)
2327 		return;
2328 
2329 	if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_ONLY_MONITOR))
2330 		return;
2331 
2332 	ieee80211_iterate_active_interfaces_atomic(phy->hw,
2333 		IEEE80211_IFACE_ITER_RESUME_ALL,
2334 		mt76_rx_beacon_iter, &data);
2335 }
2336 EXPORT_SYMBOL_GPL(mt76_rx_beacon);
2337 
mt76_beacon_mon_iter(void * data,u8 * mac,struct ieee80211_vif * vif)2338 static void mt76_beacon_mon_iter(void *data, u8 *mac,
2339 				 struct ieee80211_vif *vif)
2340 {
2341 	struct mt76_phy *phy = data;
2342 	struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv;
2343 	struct mt76_vif_data *mvif = mlink->mvif;
2344 	int link_id;
2345 
2346 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
2347 		return;
2348 
2349 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
2350 		if (link_id == mvif->deflink_id)
2351 			mlink = (struct mt76_vif_link *)vif->drv_priv;
2352 		else
2353 			mlink = rcu_dereference(mvif->link[link_id]);
2354 		if (!mlink || !mlink->beacon_mon_interval)
2355 			continue;
2356 
2357 		if (mt76_vif_link_phy(mlink) != phy)
2358 			continue;
2359 
2360 		if (time_after(jiffies,
2361 			       READ_ONCE(mlink->beacon_mon_last) +
2362 			       MT76_BEACON_MON_MAX_MISS * mlink->beacon_mon_interval))
2363 			ieee80211_beacon_loss(vif);
2364 	}
2365 }
2366 
mt76_beacon_mon_check(struct mt76_phy * phy)2367 void mt76_beacon_mon_check(struct mt76_phy *phy)
2368 {
2369 	if (phy->offchannel)
2370 		return;
2371 
2372 	ieee80211_iterate_active_interfaces_atomic(phy->hw,
2373 		IEEE80211_IFACE_ITER_RESUME_ALL,
2374 		mt76_beacon_mon_iter, phy);
2375 }
2376 EXPORT_SYMBOL_GPL(mt76_beacon_mon_check);
2377