1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/platform_device.h>
6 #include <linux/pci.h>
7 #include <linux/module.h>
8 #include <net/ipv6.h>
9 #include "mt7921.h"
10 #include "mcu.h"
11 
12 static void
13 mt7921_gen_ppe_thresh(u8 *he_ppet, int nss)
14 {
15 	u8 i, ppet_bits, ppet_size, ru_bit_mask = 0x7; /* HE80 */
16 	static const u8 ppet16_ppet8_ru3_ru0[] = {0x1c, 0xc7, 0x71};
17 
18 	he_ppet[0] = FIELD_PREP(IEEE80211_PPE_THRES_NSS_MASK, nss - 1) |
19 		     FIELD_PREP(IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK,
20 				ru_bit_mask);
21 
22 	ppet_bits = IEEE80211_PPE_THRES_INFO_PPET_SIZE *
23 		    nss * hweight8(ru_bit_mask) * 2;
24 	ppet_size = DIV_ROUND_UP(ppet_bits, 8);
25 
26 	for (i = 0; i < ppet_size - 1; i++)
27 		he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3];
28 
29 	he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3] &
30 			 (0xff >> (8 - (ppet_bits - 1) % 8));
31 }
32 
33 static int
34 mt7921_init_he_caps(struct mt7921_phy *phy, enum nl80211_band band,
35 		    struct ieee80211_sband_iftype_data *data)
36 {
37 	int i, idx = 0;
38 	int nss = hweight8(phy->mt76->chainmask);
39 	u16 mcs_map = 0;
40 
41 	for (i = 0; i < 8; i++) {
42 		if (i < nss)
43 			mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2));
44 		else
45 			mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2));
46 	}
47 
48 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
49 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
50 		struct ieee80211_he_cap_elem *he_cap_elem =
51 				&he_cap->he_cap_elem;
52 		struct ieee80211_he_mcs_nss_supp *he_mcs =
53 				&he_cap->he_mcs_nss_supp;
54 
55 		switch (i) {
56 		case NL80211_IFTYPE_STATION:
57 		case NL80211_IFTYPE_AP:
58 			break;
59 		default:
60 			continue;
61 		}
62 
63 		data[idx].types_mask = BIT(i);
64 		he_cap->has_he = true;
65 
66 		he_cap_elem->mac_cap_info[0] =
67 			IEEE80211_HE_MAC_CAP0_HTC_HE;
68 		he_cap_elem->mac_cap_info[3] =
69 			IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
70 			IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3;
71 		he_cap_elem->mac_cap_info[4] =
72 			IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
73 
74 		if (band == NL80211_BAND_2GHZ)
75 			he_cap_elem->phy_cap_info[0] =
76 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
77 		else
78 			he_cap_elem->phy_cap_info[0] =
79 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G;
80 
81 		he_cap_elem->phy_cap_info[1] =
82 			IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD;
83 		he_cap_elem->phy_cap_info[2] =
84 			IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
85 			IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
86 			IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
87 			IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
88 			IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
89 
90 		switch (i) {
91 		case NL80211_IFTYPE_AP:
92 			he_cap_elem->mac_cap_info[2] |=
93 				IEEE80211_HE_MAC_CAP2_BSR;
94 			he_cap_elem->mac_cap_info[4] |=
95 				IEEE80211_HE_MAC_CAP4_BQR;
96 			he_cap_elem->mac_cap_info[5] |=
97 				IEEE80211_HE_MAC_CAP5_OM_CTRL_UL_MU_DATA_DIS_RX;
98 			he_cap_elem->phy_cap_info[3] |=
99 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
100 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
101 			he_cap_elem->phy_cap_info[6] |=
102 				IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
103 				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
104 			he_cap_elem->phy_cap_info[9] |=
105 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
106 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
107 			break;
108 		case NL80211_IFTYPE_STATION:
109 			he_cap_elem->mac_cap_info[1] |=
110 				IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
111 
112 			if (band == NL80211_BAND_2GHZ)
113 				he_cap_elem->phy_cap_info[0] |=
114 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G;
115 			else
116 				he_cap_elem->phy_cap_info[0] |=
117 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G;
118 
119 			he_cap_elem->phy_cap_info[1] |=
120 				IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
121 				IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
122 			he_cap_elem->phy_cap_info[3] |=
123 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
124 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
125 			he_cap_elem->phy_cap_info[4] |=
126 				IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE |
127 				IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4;
128 			he_cap_elem->phy_cap_info[5] |=
129 				IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK |
130 				IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
131 			he_cap_elem->phy_cap_info[6] |=
132 				IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU |
133 				IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
134 				IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
135 				IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
136 				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
137 			he_cap_elem->phy_cap_info[7] |=
138 				IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
139 				IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI;
140 			he_cap_elem->phy_cap_info[8] |=
141 				IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
142 				IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484;
143 			he_cap_elem->phy_cap_info[9] |=
144 				IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
145 				IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
146 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
147 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
148 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
149 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
150 
151 			if (is_mt7922(phy->mt76->dev)) {
152 				he_cap_elem->phy_cap_info[0] |=
153 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
154 				he_cap_elem->phy_cap_info[8] |=
155 					IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
156 					IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
157 			}
158 			break;
159 		}
160 
161 		he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map);
162 		he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map);
163 		if (is_mt7922(phy->mt76->dev)) {
164 			he_mcs->rx_mcs_160 = cpu_to_le16(mcs_map);
165 			he_mcs->tx_mcs_160 = cpu_to_le16(mcs_map);
166 		}
167 
168 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
169 		if (he_cap_elem->phy_cap_info[6] &
170 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
171 			mt7921_gen_ppe_thresh(he_cap->ppe_thres, nss);
172 		} else {
173 			he_cap_elem->phy_cap_info[9] |=
174 				u8_encode_bits(IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US,
175 					       IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
176 		}
177 
178 		if (band == NL80211_BAND_6GHZ) {
179 			struct ieee80211_supported_band *sband =
180 				&phy->mt76->sband_5g.sband;
181 			struct ieee80211_sta_vht_cap *vht_cap = &sband->vht_cap;
182 			struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
183 			u32 exp;
184 			u16 cap;
185 
186 			cap = u16_encode_bits(ht_cap->ampdu_density,
187 					IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
188 			exp = u32_get_bits(vht_cap->cap,
189 				IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
190 			cap |= u16_encode_bits(exp,
191 					IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
192 			exp = u32_get_bits(vht_cap->cap,
193 					   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
194 			cap |= u16_encode_bits(exp,
195 					IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
196 			if (vht_cap->cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
197 				cap |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
198 			if (vht_cap->cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
199 				cap |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
200 
201 			data[idx].he_6ghz_capa.capa = cpu_to_le16(cap);
202 		}
203 		idx++;
204 	}
205 
206 	return idx;
207 }
208 
209 void mt7921_set_stream_he_caps(struct mt7921_phy *phy)
210 {
211 	struct ieee80211_sband_iftype_data *data;
212 	struct ieee80211_supported_band *band;
213 	int n;
214 
215 	if (phy->mt76->cap.has_2ghz) {
216 		data = phy->iftype[NL80211_BAND_2GHZ];
217 		n = mt7921_init_he_caps(phy, NL80211_BAND_2GHZ, data);
218 
219 		band = &phy->mt76->sband_2g.sband;
220 		band->iftype_data = data;
221 		band->n_iftype_data = n;
222 	}
223 
224 	if (phy->mt76->cap.has_5ghz) {
225 		data = phy->iftype[NL80211_BAND_5GHZ];
226 		n = mt7921_init_he_caps(phy, NL80211_BAND_5GHZ, data);
227 
228 		band = &phy->mt76->sband_5g.sband;
229 		band->iftype_data = data;
230 		band->n_iftype_data = n;
231 
232 		if (phy->mt76->cap.has_6ghz) {
233 			data = phy->iftype[NL80211_BAND_6GHZ];
234 			n = mt7921_init_he_caps(phy, NL80211_BAND_6GHZ, data);
235 
236 			band = &phy->mt76->sband_6g.sband;
237 			band->iftype_data = data;
238 			band->n_iftype_data = n;
239 		}
240 	}
241 }
242 
243 int __mt7921_start(struct mt7921_phy *phy)
244 {
245 	struct mt76_phy *mphy = phy->mt76;
246 	int err;
247 
248 	err = mt76_connac_mcu_set_mac_enable(mphy->dev, 0, true, false);
249 	if (err)
250 		return err;
251 
252 	err = mt76_connac_mcu_set_channel_domain(mphy);
253 	if (err)
254 		return err;
255 
256 	err = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
257 	if (err)
258 		return err;
259 
260 	err = mt76_connac_mcu_set_rate_txpower(phy->mt76);
261 	if (err)
262 		return err;
263 
264 	mt7921_mac_reset_counters(phy);
265 	set_bit(MT76_STATE_RUNNING, &mphy->state);
266 
267 	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
268 				     MT7921_WATCHDOG_TIME);
269 
270 	return 0;
271 }
272 EXPORT_SYMBOL_GPL(__mt7921_start);
273 
274 static int mt7921_start(struct ieee80211_hw *hw)
275 {
276 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
277 	int err;
278 
279 	mt7921_mutex_acquire(phy->dev);
280 	err = __mt7921_start(phy);
281 	mt7921_mutex_release(phy->dev);
282 
283 	return err;
284 }
285 
286 void mt7921_stop(struct ieee80211_hw *hw)
287 {
288 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
289 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
290 
291 	cancel_delayed_work_sync(&phy->mt76->mac_work);
292 
293 	cancel_delayed_work_sync(&dev->pm.ps_work);
294 	cancel_work_sync(&dev->pm.wake_work);
295 	cancel_work_sync(&dev->reset_work);
296 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
297 
298 	mt7921_mutex_acquire(dev);
299 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
300 	mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
301 	mt7921_mutex_release(dev);
302 }
303 EXPORT_SYMBOL_GPL(mt7921_stop);
304 
305 static int mt7921_add_interface(struct ieee80211_hw *hw,
306 				struct ieee80211_vif *vif)
307 {
308 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
309 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
310 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
311 	struct mt76_txq *mtxq;
312 	int idx, ret = 0;
313 
314 	mt7921_mutex_acquire(dev);
315 
316 	mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
317 	if (mvif->mt76.idx >= MT7921_MAX_INTERFACES) {
318 		ret = -ENOSPC;
319 		goto out;
320 	}
321 
322 	mvif->mt76.omac_idx = mvif->mt76.idx;
323 	mvif->phy = phy;
324 	mvif->mt76.band_idx = 0;
325 	mvif->mt76.wmm_idx = mvif->mt76.idx % MT7921_MAX_WMM_SETS;
326 
327 	ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid,
328 					  true);
329 	if (ret)
330 		goto out;
331 
332 	dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
333 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
334 
335 	idx = MT7921_WTBL_RESERVED - mvif->mt76.idx;
336 
337 	INIT_LIST_HEAD(&mvif->sta.poll_list);
338 	mvif->sta.wcid.idx = idx;
339 	mvif->sta.wcid.ext_phy = mvif->mt76.band_idx;
340 	mvif->sta.wcid.hw_key_idx = -1;
341 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
342 	mt76_packet_id_init(&mvif->sta.wcid);
343 
344 	mt7921_mac_wtbl_update(dev, idx,
345 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
346 
347 	ewma_rssi_init(&mvif->rssi);
348 
349 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
350 	if (vif->txq) {
351 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
352 		mtxq->wcid = idx;
353 	}
354 
355 out:
356 	mt7921_mutex_release(dev);
357 
358 	return ret;
359 }
360 
361 static void mt7921_remove_interface(struct ieee80211_hw *hw,
362 				    struct ieee80211_vif *vif)
363 {
364 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
365 	struct mt7921_sta *msta = &mvif->sta;
366 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
367 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
368 	int idx = msta->wcid.idx;
369 
370 	mt7921_mutex_acquire(dev);
371 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
372 	mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid, false);
373 
374 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
375 
376 	dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx);
377 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
378 	mt7921_mutex_release(dev);
379 
380 	spin_lock_bh(&dev->sta_poll_lock);
381 	if (!list_empty(&msta->poll_list))
382 		list_del_init(&msta->poll_list);
383 	spin_unlock_bh(&dev->sta_poll_lock);
384 
385 	mt76_packet_id_flush(&dev->mt76, &msta->wcid);
386 }
387 
388 static int mt7921_set_channel(struct mt7921_phy *phy)
389 {
390 	struct mt7921_dev *dev = phy->dev;
391 	int ret;
392 
393 	cancel_delayed_work_sync(&phy->mt76->mac_work);
394 
395 	mt7921_mutex_acquire(dev);
396 	set_bit(MT76_RESET, &phy->mt76->state);
397 
398 	mt76_set_channel(phy->mt76);
399 
400 	ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
401 	if (ret)
402 		goto out;
403 
404 	mt7921_mac_set_timing(phy);
405 
406 	mt7921_mac_reset_counters(phy);
407 	phy->noise = 0;
408 
409 out:
410 	clear_bit(MT76_RESET, &phy->mt76->state);
411 	mt7921_mutex_release(dev);
412 
413 	mt76_worker_schedule(&dev->mt76.tx_worker);
414 	ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mt76->mac_work,
415 				     MT7921_WATCHDOG_TIME);
416 
417 	return ret;
418 }
419 
420 static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
421 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
422 			  struct ieee80211_key_conf *key)
423 {
424 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
425 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
426 	struct mt7921_sta *msta = sta ? (struct mt7921_sta *)sta->drv_priv :
427 				  &mvif->sta;
428 	struct mt76_wcid *wcid = &msta->wcid;
429 	u8 *wcid_keyidx = &wcid->hw_key_idx;
430 	int idx = key->keyidx, err = 0;
431 
432 	/* The hardware does not support per-STA RX GTK, fallback
433 	 * to software mode for these.
434 	 */
435 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
436 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
437 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
438 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
439 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
440 		return -EOPNOTSUPP;
441 
442 	/* fall back to sw encryption for unsupported ciphers */
443 	switch (key->cipher) {
444 	case WLAN_CIPHER_SUITE_AES_CMAC:
445 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
446 		wcid_keyidx = &wcid->hw_key_idx2;
447 		break;
448 	case WLAN_CIPHER_SUITE_WEP40:
449 	case WLAN_CIPHER_SUITE_WEP104:
450 		if (!mvif->wep_sta)
451 			return -EOPNOTSUPP;
452 		break;
453 	case WLAN_CIPHER_SUITE_TKIP:
454 	case WLAN_CIPHER_SUITE_CCMP:
455 	case WLAN_CIPHER_SUITE_CCMP_256:
456 	case WLAN_CIPHER_SUITE_GCMP:
457 	case WLAN_CIPHER_SUITE_GCMP_256:
458 	case WLAN_CIPHER_SUITE_SMS4:
459 		break;
460 	default:
461 		return -EOPNOTSUPP;
462 	}
463 
464 	mt7921_mutex_acquire(dev);
465 
466 	if (cmd == SET_KEY)
467 		*wcid_keyidx = idx;
468 	else if (idx == *wcid_keyidx)
469 		*wcid_keyidx = -1;
470 	else
471 		goto out;
472 
473 	mt76_wcid_key_setup(&dev->mt76, wcid,
474 			    cmd == SET_KEY ? key : NULL);
475 
476 	err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->bip,
477 				      key, MCU_UNI_CMD(STA_REC_UPDATE),
478 				      &msta->wcid, cmd);
479 	if (err)
480 		goto out;
481 
482 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
483 	    key->cipher == WLAN_CIPHER_SUITE_WEP40)
484 		err = mt76_connac_mcu_add_key(&dev->mt76, vif,
485 					      &mvif->wep_sta->bip,
486 					      key, MCU_UNI_CMD(STA_REC_UPDATE),
487 					      &mvif->wep_sta->wcid, cmd);
488 out:
489 	mt7921_mutex_release(dev);
490 
491 	return err;
492 }
493 
494 static void
495 mt7921_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
496 {
497 	struct mt7921_dev *dev = priv;
498 
499 	mt7921_mcu_set_beacon_filter(dev, vif, dev->pm.enable);
500 }
501 
502 static void
503 mt7921_sniffer_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
504 {
505 	struct mt7921_dev *dev = priv;
506 	struct ieee80211_hw *hw = mt76_hw(dev);
507 	struct mt76_connac_pm *pm = &dev->pm;
508 	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
509 
510 	mt7921_mcu_set_sniffer(dev, vif, monitor);
511 	pm->enable = pm->enable_user && !monitor;
512 	pm->ds_enable = pm->ds_enable_user && !monitor;
513 
514 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
515 
516 	if (monitor)
517 		mt7921_mcu_set_beacon_filter(dev, vif, false);
518 }
519 
520 void mt7921_set_runtime_pm(struct mt7921_dev *dev)
521 {
522 	struct ieee80211_hw *hw = mt76_hw(dev);
523 	struct mt76_connac_pm *pm = &dev->pm;
524 	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
525 
526 	pm->enable = pm->enable_user && !monitor;
527 	ieee80211_iterate_active_interfaces(hw,
528 					    IEEE80211_IFACE_ITER_RESUME_ALL,
529 					    mt7921_pm_interface_iter, dev);
530 	pm->ds_enable = pm->ds_enable_user && !monitor;
531 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
532 }
533 
534 static int mt7921_config(struct ieee80211_hw *hw, u32 changed)
535 {
536 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
537 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
538 	int ret = 0;
539 
540 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
541 		ieee80211_stop_queues(hw);
542 		ret = mt7921_set_channel(phy);
543 		if (ret)
544 			return ret;
545 		ieee80211_wake_queues(hw);
546 	}
547 
548 	mt7921_mutex_acquire(dev);
549 
550 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
551 		ret = mt76_connac_mcu_set_rate_txpower(phy->mt76);
552 		if (ret)
553 			goto out;
554 	}
555 
556 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
557 		ieee80211_iterate_active_interfaces(hw,
558 						    IEEE80211_IFACE_ITER_RESUME_ALL,
559 						    mt7921_sniffer_interface_iter, dev);
560 		dev->mt76.rxfilter = mt76_rr(dev, MT_WF_RFCR(0));
561 	}
562 
563 out:
564 	mt7921_mutex_release(dev);
565 
566 	return ret;
567 }
568 
569 static int
570 mt7921_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
571 	       const struct ieee80211_tx_queue_params *params)
572 {
573 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
574 
575 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
576 	queue = mt76_connac_lmac_mapping(queue);
577 	mvif->queue_params[queue] = *params;
578 
579 	return 0;
580 }
581 
582 static void mt7921_configure_filter(struct ieee80211_hw *hw,
583 				    unsigned int changed_flags,
584 				    unsigned int *total_flags,
585 				    u64 multicast)
586 {
587 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
588 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
589 			MT_WF_RFCR1_DROP_BF_POLL |
590 			MT_WF_RFCR1_DROP_BA |
591 			MT_WF_RFCR1_DROP_CFEND |
592 			MT_WF_RFCR1_DROP_CFACK;
593 	u32 flags = 0;
594 
595 #define MT76_FILTER(_flag, _hw) do {					\
596 		flags |= *total_flags & FIF_##_flag;			\
597 		dev->mt76.rxfilter &= ~(_hw);				\
598 		dev->mt76.rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
599 	} while (0)
600 
601 	mt7921_mutex_acquire(dev);
602 
603 	dev->mt76.rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
604 				MT_WF_RFCR_DROP_OTHER_BEACON |
605 				MT_WF_RFCR_DROP_FRAME_REPORT |
606 				MT_WF_RFCR_DROP_PROBEREQ |
607 				MT_WF_RFCR_DROP_MCAST_FILTERED |
608 				MT_WF_RFCR_DROP_MCAST |
609 				MT_WF_RFCR_DROP_BCAST |
610 				MT_WF_RFCR_DROP_DUPLICATE |
611 				MT_WF_RFCR_DROP_A2_BSSID |
612 				MT_WF_RFCR_DROP_UNWANTED_CTL |
613 				MT_WF_RFCR_DROP_STBC_MULTI);
614 
615 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
616 			       MT_WF_RFCR_DROP_A3_MAC |
617 			       MT_WF_RFCR_DROP_A3_BSSID);
618 
619 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
620 
621 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
622 			     MT_WF_RFCR_DROP_RTS |
623 			     MT_WF_RFCR_DROP_CTL_RSV |
624 			     MT_WF_RFCR_DROP_NDPA);
625 
626 	*total_flags = flags;
627 	mt76_wr(dev, MT_WF_RFCR(0), dev->mt76.rxfilter);
628 
629 	if (*total_flags & FIF_CONTROL)
630 		mt76_clear(dev, MT_WF_RFCR1(0), ctl_flags);
631 	else
632 		mt76_set(dev, MT_WF_RFCR1(0), ctl_flags);
633 
634 	mt7921_mutex_release(dev);
635 }
636 
637 static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
638 				    struct ieee80211_vif *vif,
639 				    struct ieee80211_bss_conf *info,
640 				    u32 changed)
641 {
642 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
643 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
644 
645 	mt7921_mutex_acquire(dev);
646 
647 	if (changed & BSS_CHANGED_ERP_SLOT) {
648 		int slottime = info->use_short_slot ? 9 : 20;
649 
650 		if (slottime != phy->slottime) {
651 			phy->slottime = slottime;
652 			mt7921_mac_set_timing(phy);
653 		}
654 	}
655 
656 	if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
657 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
658 
659 		mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid,
660 					    true);
661 		mt7921_mcu_sta_update(dev, NULL, vif, true,
662 				      MT76_STA_INFO_STATE_NONE);
663 	}
664 
665 	if (changed & (BSS_CHANGED_BEACON |
666 		       BSS_CHANGED_BEACON_ENABLED))
667 		mt7921_mcu_uni_add_beacon_offload(dev, hw, vif,
668 						  info->enable_beacon);
669 
670 	/* ensure that enable txcmd_mode after bss_info */
671 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
672 		mt7921_mcu_set_tx(dev, vif);
673 
674 	if (changed & BSS_CHANGED_PS)
675 		mt7921_mcu_uni_bss_ps(dev, vif);
676 
677 	if (changed & BSS_CHANGED_ASSOC) {
678 		mt7921_mcu_sta_update(dev, NULL, vif, true,
679 				      MT76_STA_INFO_STATE_ASSOC);
680 		if (dev->pm.enable)
681 			mt7921_mcu_set_beacon_filter(dev, vif, info->assoc);
682 	}
683 
684 	if (changed & BSS_CHANGED_ARP_FILTER) {
685 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
686 
687 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
688 						  info);
689 	}
690 
691 	mt7921_mutex_release(dev);
692 }
693 
694 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
695 		       struct ieee80211_sta *sta)
696 {
697 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
698 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
699 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
700 	int ret, idx;
701 
702 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
703 	if (idx < 0)
704 		return -ENOSPC;
705 
706 	INIT_LIST_HEAD(&msta->poll_list);
707 	msta->vif = mvif;
708 	msta->wcid.sta = 1;
709 	msta->wcid.idx = idx;
710 	msta->wcid.ext_phy = mvif->mt76.band_idx;
711 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
712 	msta->last_txs = jiffies;
713 
714 	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
715 	if (ret)
716 		return ret;
717 
718 	if (vif->type == NL80211_IFTYPE_STATION)
719 		mvif->wep_sta = msta;
720 
721 	mt7921_mac_wtbl_update(dev, idx,
722 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
723 
724 	ret = mt7921_mcu_sta_update(dev, sta, vif, true,
725 				    MT76_STA_INFO_STATE_NONE);
726 	if (ret)
727 		return ret;
728 
729 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
730 
731 	return 0;
732 }
733 EXPORT_SYMBOL_GPL(mt7921_mac_sta_add);
734 
735 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
736 			  struct ieee80211_sta *sta)
737 {
738 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
739 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
740 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
741 
742 	mt7921_mutex_acquire(dev);
743 
744 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
745 		mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
746 					    true);
747 
748 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
749 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
750 
751 	mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
752 
753 	mt7921_mutex_release(dev);
754 }
755 EXPORT_SYMBOL_GPL(mt7921_mac_sta_assoc);
756 
757 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
758 			   struct ieee80211_sta *sta)
759 {
760 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
761 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
762 
763 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
764 	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
765 
766 	mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
767 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
768 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
769 
770 	if (vif->type == NL80211_IFTYPE_STATION) {
771 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
772 
773 		mvif->wep_sta = NULL;
774 		ewma_rssi_init(&mvif->rssi);
775 		if (!sta->tdls)
776 			mt76_connac_mcu_uni_add_bss(&dev->mphy, vif,
777 						    &mvif->sta.wcid, false);
778 	}
779 
780 	spin_lock_bh(&dev->sta_poll_lock);
781 	if (!list_empty(&msta->poll_list))
782 		list_del_init(&msta->poll_list);
783 	spin_unlock_bh(&dev->sta_poll_lock);
784 
785 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
786 }
787 EXPORT_SYMBOL_GPL(mt7921_mac_sta_remove);
788 
789 void mt7921_tx_worker(struct mt76_worker *w)
790 {
791 	struct mt7921_dev *dev = container_of(w, struct mt7921_dev,
792 					      mt76.tx_worker);
793 
794 	if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
795 		queue_work(dev->mt76.wq, &dev->pm.wake_work);
796 		return;
797 	}
798 
799 	mt76_txq_schedule_all(&dev->mphy);
800 	mt76_connac_pm_unref(&dev->mphy, &dev->pm);
801 }
802 
803 static void mt7921_tx(struct ieee80211_hw *hw,
804 		      struct ieee80211_tx_control *control,
805 		      struct sk_buff *skb)
806 {
807 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
808 	struct mt76_phy *mphy = hw->priv;
809 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
810 	struct ieee80211_vif *vif = info->control.vif;
811 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
812 	int qid;
813 
814 	if (control->sta) {
815 		struct mt7921_sta *sta;
816 
817 		sta = (struct mt7921_sta *)control->sta->drv_priv;
818 		wcid = &sta->wcid;
819 	}
820 
821 	if (vif && !control->sta) {
822 		struct mt7921_vif *mvif;
823 
824 		mvif = (struct mt7921_vif *)vif->drv_priv;
825 		wcid = &mvif->sta.wcid;
826 	}
827 
828 	if (mt76_connac_pm_ref(mphy, &dev->pm)) {
829 		mt76_tx(mphy, control->sta, wcid, skb);
830 		mt76_connac_pm_unref(mphy, &dev->pm);
831 		return;
832 	}
833 
834 	qid = skb_get_queue_mapping(skb);
835 	if (qid >= MT_TXQ_PSD) {
836 		qid = IEEE80211_AC_BE;
837 		skb_set_queue_mapping(skb, qid);
838 	}
839 
840 	mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
841 }
842 
843 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
844 {
845 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
846 
847 	mt7921_mutex_acquire(dev);
848 	mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
849 	mt7921_mutex_release(dev);
850 
851 	return 0;
852 }
853 
854 static int
855 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
856 		    struct ieee80211_ampdu_params *params)
857 {
858 	enum ieee80211_ampdu_mlme_action action = params->action;
859 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
860 	struct ieee80211_sta *sta = params->sta;
861 	struct ieee80211_txq *txq = sta->txq[params->tid];
862 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
863 	u16 tid = params->tid;
864 	u16 ssn = params->ssn;
865 	struct mt76_txq *mtxq;
866 	int ret = 0;
867 
868 	if (!txq)
869 		return -EINVAL;
870 
871 	mtxq = (struct mt76_txq *)txq->drv_priv;
872 
873 	mt7921_mutex_acquire(dev);
874 	switch (action) {
875 	case IEEE80211_AMPDU_RX_START:
876 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
877 				   params->buf_size);
878 		mt7921_mcu_uni_rx_ba(dev, params, true);
879 		break;
880 	case IEEE80211_AMPDU_RX_STOP:
881 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
882 		mt7921_mcu_uni_rx_ba(dev, params, false);
883 		break;
884 	case IEEE80211_AMPDU_TX_OPERATIONAL:
885 		mtxq->aggr = true;
886 		mtxq->send_bar = false;
887 		mt7921_mcu_uni_tx_ba(dev, params, true);
888 		break;
889 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
890 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
891 		mtxq->aggr = false;
892 		clear_bit(tid, &msta->ampdu_state);
893 		mt7921_mcu_uni_tx_ba(dev, params, false);
894 		break;
895 	case IEEE80211_AMPDU_TX_START:
896 		set_bit(tid, &msta->ampdu_state);
897 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
898 		break;
899 	case IEEE80211_AMPDU_TX_STOP_CONT:
900 		mtxq->aggr = false;
901 		clear_bit(tid, &msta->ampdu_state);
902 		mt7921_mcu_uni_tx_ba(dev, params, false);
903 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
904 		break;
905 	}
906 	mt7921_mutex_release(dev);
907 
908 	return ret;
909 }
910 
911 static int mt7921_sta_state(struct ieee80211_hw *hw,
912 			    struct ieee80211_vif *vif,
913 			    struct ieee80211_sta *sta,
914 			    enum ieee80211_sta_state old_state,
915 			    enum ieee80211_sta_state new_state)
916 {
917 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
918 
919 	if (dev->pm.ds_enable) {
920 		mt7921_mutex_acquire(dev);
921 		mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state);
922 		mt7921_mutex_release(dev);
923 	}
924 
925 	return mt76_sta_state(hw, vif, sta, old_state, new_state);
926 }
927 
928 static int
929 mt7921_get_stats(struct ieee80211_hw *hw,
930 		 struct ieee80211_low_level_stats *stats)
931 {
932 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
933 	struct mib_stats *mib = &phy->mib;
934 
935 	mt7921_mutex_acquire(phy->dev);
936 
937 	stats->dot11RTSSuccessCount = mib->rts_cnt;
938 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
939 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
940 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
941 
942 	mt7921_mutex_release(phy->dev);
943 
944 	return 0;
945 }
946 
947 static const char mt7921_gstrings_stats[][ETH_GSTRING_LEN] = {
948 	/* tx counters */
949 	"tx_ampdu_cnt",
950 	"tx_mpdu_attempts",
951 	"tx_mpdu_success",
952 	"tx_pkt_ebf_cnt",
953 	"tx_pkt_ibf_cnt",
954 	"tx_ampdu_len:0-1",
955 	"tx_ampdu_len:2-10",
956 	"tx_ampdu_len:11-19",
957 	"tx_ampdu_len:20-28",
958 	"tx_ampdu_len:29-37",
959 	"tx_ampdu_len:38-46",
960 	"tx_ampdu_len:47-55",
961 	"tx_ampdu_len:56-79",
962 	"tx_ampdu_len:80-103",
963 	"tx_ampdu_len:104-127",
964 	"tx_ampdu_len:128-151",
965 	"tx_ampdu_len:152-175",
966 	"tx_ampdu_len:176-199",
967 	"tx_ampdu_len:200-223",
968 	"tx_ampdu_len:224-247",
969 	"ba_miss_count",
970 	"tx_beamformer_ppdu_iBF",
971 	"tx_beamformer_ppdu_eBF",
972 	"tx_beamformer_rx_feedback_all",
973 	"tx_beamformer_rx_feedback_he",
974 	"tx_beamformer_rx_feedback_vht",
975 	"tx_beamformer_rx_feedback_ht",
976 	"tx_msdu_pack_1",
977 	"tx_msdu_pack_2",
978 	"tx_msdu_pack_3",
979 	"tx_msdu_pack_4",
980 	"tx_msdu_pack_5",
981 	"tx_msdu_pack_6",
982 	"tx_msdu_pack_7",
983 	"tx_msdu_pack_8",
984 	/* rx counters */
985 	"rx_mpdu_cnt",
986 	"rx_ampdu_cnt",
987 	"rx_ampdu_bytes_cnt",
988 	"rx_ba_cnt",
989 	/* per vif counters */
990 	"v_tx_mode_cck",
991 	"v_tx_mode_ofdm",
992 	"v_tx_mode_ht",
993 	"v_tx_mode_ht_gf",
994 	"v_tx_mode_vht",
995 	"v_tx_mode_he_su",
996 	"v_tx_mode_he_ext_su",
997 	"v_tx_mode_he_tb",
998 	"v_tx_mode_he_mu",
999 	"v_tx_bw_20",
1000 	"v_tx_bw_40",
1001 	"v_tx_bw_80",
1002 	"v_tx_bw_160",
1003 	"v_tx_mcs_0",
1004 	"v_tx_mcs_1",
1005 	"v_tx_mcs_2",
1006 	"v_tx_mcs_3",
1007 	"v_tx_mcs_4",
1008 	"v_tx_mcs_5",
1009 	"v_tx_mcs_6",
1010 	"v_tx_mcs_7",
1011 	"v_tx_mcs_8",
1012 	"v_tx_mcs_9",
1013 	"v_tx_mcs_10",
1014 	"v_tx_mcs_11",
1015 };
1016 
1017 static void
1018 mt7921_get_et_strings(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1019 		      u32 sset, u8 *data)
1020 {
1021 	if (sset != ETH_SS_STATS)
1022 		return;
1023 
1024 	memcpy(data, *mt7921_gstrings_stats, sizeof(mt7921_gstrings_stats));
1025 }
1026 
1027 static int
1028 mt7921_get_et_sset_count(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1029 			 int sset)
1030 {
1031 	return sset == ETH_SS_STATS ? ARRAY_SIZE(mt7921_gstrings_stats) : 0;
1032 }
1033 
1034 static void
1035 mt7921_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1036 {
1037 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1038 	struct mt76_ethtool_worker_info *wi = wi_data;
1039 
1040 	if (msta->vif->mt76.idx != wi->idx)
1041 		return;
1042 
1043 	mt76_ethtool_worker(wi, &msta->stats);
1044 }
1045 
1046 static
1047 void mt7921_get_et_stats(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1048 			 struct ethtool_stats *stats, u64 *data)
1049 {
1050 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1051 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1052 	struct mt7921_dev *dev = phy->dev;
1053 	struct mib_stats *mib = &phy->mib;
1054 	struct mt76_ethtool_worker_info wi = {
1055 		.data = data,
1056 		.idx = mvif->mt76.idx,
1057 	};
1058 	int i, ei = 0;
1059 
1060 	mt7921_mutex_acquire(dev);
1061 
1062 	mt7921_mac_update_mib_stats(phy);
1063 
1064 	data[ei++] = mib->tx_ampdu_cnt;
1065 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1066 	data[ei++] = mib->tx_mpdu_success_cnt;
1067 	data[ei++] = mib->tx_pkt_ebf_cnt;
1068 	data[ei++] = mib->tx_pkt_ibf_cnt;
1069 
1070 	/* Tx ampdu stat */
1071 	for (i = 0; i < 15; i++)
1072 		data[ei++] = dev->mt76.aggr_stats[i];
1073 
1074 	data[ei++] = phy->mib.ba_miss_cnt;
1075 
1076 	/* Tx Beamformer monitor */
1077 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1078 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1079 
1080 	/* Tx Beamformer Rx feedback monitor */
1081 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1082 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1083 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1084 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1085 
1086 	/* Tx amsdu info (pack-count histogram) */
1087 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1088 		data[ei++] = mib->tx_amsdu[i];
1089 
1090 	/* rx counters */
1091 	data[ei++] = mib->rx_mpdu_cnt;
1092 	data[ei++] = mib->rx_ampdu_cnt;
1093 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1094 	data[ei++] = mib->rx_ba_cnt;
1095 
1096 	/* Add values for all stations owned by this vif */
1097 	wi.initial_stat_idx = ei;
1098 	ieee80211_iterate_stations_atomic(hw, mt7921_ethtool_worker, &wi);
1099 
1100 	mt7921_mutex_release(dev);
1101 
1102 	if (!wi.sta_count)
1103 		return;
1104 
1105 	ei += wi.worker_stat_count;
1106 	if (ei != ARRAY_SIZE(mt7921_gstrings_stats))
1107 		dev_err(dev->mt76.dev, "ei: %d  SSTATS_LEN: %zu",
1108 			ei, ARRAY_SIZE(mt7921_gstrings_stats));
1109 }
1110 
1111 static u64
1112 mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1113 {
1114 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1115 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1116 	u8 omac_idx = mvif->mt76.omac_idx;
1117 	union {
1118 		u64 t64;
1119 		u32 t32[2];
1120 	} tsf;
1121 	u16 n;
1122 
1123 	mt7921_mutex_acquire(dev);
1124 
1125 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1126 	/* TSF software read */
1127 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_MODE);
1128 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(0));
1129 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(0));
1130 
1131 	mt7921_mutex_release(dev);
1132 
1133 	return tsf.t64;
1134 }
1135 
1136 static void
1137 mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1138 	       u64 timestamp)
1139 {
1140 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1141 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1142 	u8 omac_idx = mvif->mt76.omac_idx;
1143 	union {
1144 		u64 t64;
1145 		u32 t32[2];
1146 	} tsf = { .t64 = timestamp, };
1147 	u16 n;
1148 
1149 	mt7921_mutex_acquire(dev);
1150 
1151 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1152 	mt76_wr(dev, MT_LPON_UTTR0(0), tsf.t32[0]);
1153 	mt76_wr(dev, MT_LPON_UTTR1(0), tsf.t32[1]);
1154 	/* TSF software overwrite */
1155 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_WRITE);
1156 
1157 	mt7921_mutex_release(dev);
1158 }
1159 
1160 static void
1161 mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
1162 {
1163 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1164 	struct mt7921_dev *dev = phy->dev;
1165 
1166 	mt7921_mutex_acquire(dev);
1167 	phy->coverage_class = max_t(s16, coverage_class, 0);
1168 	mt7921_mac_set_timing(phy);
1169 	mt7921_mutex_release(dev);
1170 }
1171 
1172 void mt7921_scan_work(struct work_struct *work)
1173 {
1174 	struct mt7921_phy *phy;
1175 
1176 	phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy,
1177 						scan_work.work);
1178 
1179 	while (true) {
1180 		struct mt7921_mcu_rxd *rxd;
1181 		struct sk_buff *skb;
1182 
1183 		spin_lock_bh(&phy->dev->mt76.lock);
1184 		skb = __skb_dequeue(&phy->scan_event_list);
1185 		spin_unlock_bh(&phy->dev->mt76.lock);
1186 
1187 		if (!skb)
1188 			break;
1189 
1190 		rxd = (struct mt7921_mcu_rxd *)skb->data;
1191 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1192 			ieee80211_sched_scan_results(phy->mt76->hw);
1193 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
1194 					      &phy->mt76->state)) {
1195 			struct cfg80211_scan_info info = {
1196 				.aborted = false,
1197 			};
1198 
1199 			ieee80211_scan_completed(phy->mt76->hw, &info);
1200 		}
1201 		dev_kfree_skb(skb);
1202 	}
1203 }
1204 
1205 static int
1206 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1207 	       struct ieee80211_scan_request *req)
1208 {
1209 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1210 	struct mt76_phy *mphy = hw->priv;
1211 	int err;
1212 
1213 	mt7921_mutex_acquire(dev);
1214 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1215 	mt7921_mutex_release(dev);
1216 
1217 	return err;
1218 }
1219 
1220 static void
1221 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1222 {
1223 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1224 	struct mt76_phy *mphy = hw->priv;
1225 
1226 	mt7921_mutex_acquire(dev);
1227 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1228 	mt7921_mutex_release(dev);
1229 }
1230 
1231 static int
1232 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1233 			struct cfg80211_sched_scan_request *req,
1234 			struct ieee80211_scan_ies *ies)
1235 {
1236 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1237 	struct mt76_phy *mphy = hw->priv;
1238 	int err;
1239 
1240 	mt7921_mutex_acquire(dev);
1241 
1242 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1243 	if (err < 0)
1244 		goto out;
1245 
1246 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1247 out:
1248 	mt7921_mutex_release(dev);
1249 
1250 	return err;
1251 }
1252 
1253 static int
1254 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1255 {
1256 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1257 	struct mt76_phy *mphy = hw->priv;
1258 	int err;
1259 
1260 	mt7921_mutex_acquire(dev);
1261 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1262 	mt7921_mutex_release(dev);
1263 
1264 	return err;
1265 }
1266 
1267 static int
1268 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1269 {
1270 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1271 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1272 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1273 
1274 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1275 		return -EINVAL;
1276 
1277 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1278 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1279 
1280 	mt7921_mutex_acquire(dev);
1281 
1282 	phy->mt76->antenna_mask = tx_ant;
1283 	phy->mt76->chainmask = tx_ant;
1284 
1285 	mt76_set_stream_caps(phy->mt76, true);
1286 	mt7921_set_stream_he_caps(phy);
1287 
1288 	mt7921_mutex_release(dev);
1289 
1290 	return 0;
1291 }
1292 
1293 static void mt7921_sta_statistics(struct ieee80211_hw *hw,
1294 				  struct ieee80211_vif *vif,
1295 				  struct ieee80211_sta *sta,
1296 				  struct station_info *sinfo)
1297 {
1298 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1299 	struct rate_info *txrate = &msta->wcid.rate;
1300 
1301 	if (!txrate->legacy && !txrate->flags)
1302 		return;
1303 
1304 	if (txrate->legacy) {
1305 		sinfo->txrate.legacy = txrate->legacy;
1306 	} else {
1307 		sinfo->txrate.mcs = txrate->mcs;
1308 		sinfo->txrate.nss = txrate->nss;
1309 		sinfo->txrate.bw = txrate->bw;
1310 		sinfo->txrate.he_gi = txrate->he_gi;
1311 		sinfo->txrate.he_dcm = txrate->he_dcm;
1312 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1313 	}
1314 	sinfo->txrate.flags = txrate->flags;
1315 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1316 }
1317 
1318 #ifdef CONFIG_PM
1319 static int mt7921_suspend(struct ieee80211_hw *hw,
1320 			  struct cfg80211_wowlan *wowlan)
1321 {
1322 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1323 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1324 
1325 	cancel_delayed_work_sync(&phy->scan_work);
1326 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1327 
1328 	cancel_delayed_work_sync(&dev->pm.ps_work);
1329 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1330 
1331 	mt7921_mutex_acquire(dev);
1332 
1333 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1334 	ieee80211_iterate_active_interfaces(hw,
1335 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1336 					    mt7921_mcu_set_suspend_iter,
1337 					    &dev->mphy);
1338 
1339 	mt7921_mutex_release(dev);
1340 
1341 	return 0;
1342 }
1343 
1344 static int mt7921_resume(struct ieee80211_hw *hw)
1345 {
1346 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1347 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1348 
1349 	mt7921_mutex_acquire(dev);
1350 
1351 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1352 	ieee80211_iterate_active_interfaces(hw,
1353 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1354 					    mt76_connac_mcu_set_suspend_iter,
1355 					    &dev->mphy);
1356 
1357 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1358 				     MT7921_WATCHDOG_TIME);
1359 
1360 	mt7921_mutex_release(dev);
1361 
1362 	return 0;
1363 }
1364 
1365 static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1366 {
1367 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1368 	struct mt76_dev *mdev = &dev->mt76;
1369 
1370 	device_set_wakeup_enable(mdev->dev, enabled);
1371 }
1372 
1373 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1374 				  struct ieee80211_vif *vif,
1375 				  struct cfg80211_gtk_rekey_data *data)
1376 {
1377 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1378 
1379 	mt7921_mutex_acquire(dev);
1380 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1381 	mt7921_mutex_release(dev);
1382 }
1383 #endif /* CONFIG_PM */
1384 
1385 static void mt7921_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1386 			 u32 queues, bool drop)
1387 {
1388 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1389 
1390 	wait_event_timeout(dev->mt76.tx_wait, !mt76_has_tx_pending(&dev->mphy),
1391 			   HZ / 2);
1392 }
1393 
1394 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1395 					 struct ieee80211_vif *vif,
1396 					 struct ieee80211_sta *sta,
1397 					 bool enabled)
1398 {
1399 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1400 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1401 
1402 	if (enabled)
1403 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1404 	else
1405 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1406 
1407 	mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->wcid,
1408 					     MCU_UNI_CMD(STA_REC_UPDATE));
1409 }
1410 
1411 #if IS_ENABLED(CONFIG_IPV6)
1412 static void mt7921_ipv6_addr_change(struct ieee80211_hw *hw,
1413 				    struct ieee80211_vif *vif,
1414 				    struct inet6_dev *idev)
1415 {
1416 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1417 	struct mt7921_dev *dev = mvif->phy->dev;
1418 	struct inet6_ifaddr *ifa;
1419 	struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN];
1420 	struct sk_buff *skb;
1421 	u8 i, idx = 0;
1422 
1423 	struct {
1424 		struct {
1425 			u8 bss_idx;
1426 			u8 pad[3];
1427 		} __packed hdr;
1428 		struct mt76_connac_arpns_tlv arpns;
1429 	} req_hdr = {
1430 		.hdr = {
1431 			.bss_idx = mvif->mt76.idx,
1432 		},
1433 		.arpns = {
1434 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
1435 			.mode = 2,  /* update */
1436 			.option = 1, /* update only */
1437 		},
1438 	};
1439 
1440 	read_lock_bh(&idev->lock);
1441 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
1442 		if (ifa->flags & IFA_F_TENTATIVE)
1443 			continue;
1444 		ns_addrs[idx] = ifa->addr;
1445 		if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN)
1446 			break;
1447 	}
1448 	read_unlock_bh(&idev->lock);
1449 
1450 	if (!idx)
1451 		return;
1452 
1453 	skb = __mt76_mcu_msg_alloc(&dev->mt76, NULL, sizeof(req_hdr) +
1454 				   idx * sizeof(struct in6_addr), GFP_ATOMIC);
1455 	if (!skb)
1456 		return;
1457 
1458 	req_hdr.arpns.ips_num = idx;
1459 	req_hdr.arpns.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)
1460 					+ idx * sizeof(struct in6_addr));
1461 	skb_put_data(skb, &req_hdr, sizeof(req_hdr));
1462 
1463 	for (i = 0; i < idx; i++)
1464 		skb_put_data(skb, &ns_addrs[i].in6_u, sizeof(struct in6_addr));
1465 
1466 	skb_queue_tail(&dev->ipv6_ns_list, skb);
1467 
1468 	ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work);
1469 }
1470 #endif
1471 
1472 static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
1473 				const struct cfg80211_sar_specs *sar)
1474 {
1475 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1476 	struct mt76_phy *mphy = hw->priv;
1477 	int err;
1478 
1479 	mt7921_mutex_acquire(dev);
1480 	err = mt76_init_sar_power(hw, sar);
1481 	if (err)
1482 		goto out;
1483 
1484 	err = mt76_connac_mcu_set_rate_txpower(mphy);
1485 out:
1486 	mt7921_mutex_release(dev);
1487 
1488 	return err;
1489 }
1490 
1491 static void
1492 mt7921_channel_switch_beacon(struct ieee80211_hw *hw,
1493 			     struct ieee80211_vif *vif,
1494 			     struct cfg80211_chan_def *chandef)
1495 {
1496 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1497 
1498 	mt7921_mutex_acquire(dev);
1499 	mt7921_mcu_uni_add_beacon_offload(dev, hw, vif, true);
1500 	mt7921_mutex_release(dev);
1501 }
1502 
1503 const struct ieee80211_ops mt7921_ops = {
1504 	.tx = mt7921_tx,
1505 	.start = mt7921_start,
1506 	.stop = mt7921_stop,
1507 	.add_interface = mt7921_add_interface,
1508 	.remove_interface = mt7921_remove_interface,
1509 	.config = mt7921_config,
1510 	.conf_tx = mt7921_conf_tx,
1511 	.configure_filter = mt7921_configure_filter,
1512 	.bss_info_changed = mt7921_bss_info_changed,
1513 	.sta_state = mt7921_sta_state,
1514 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1515 	.set_key = mt7921_set_key,
1516 	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1517 #if IS_ENABLED(CONFIG_IPV6)
1518 	.ipv6_addr_change = mt7921_ipv6_addr_change,
1519 #endif /* CONFIG_IPV6 */
1520 	.ampdu_action = mt7921_ampdu_action,
1521 	.set_rts_threshold = mt7921_set_rts_threshold,
1522 	.wake_tx_queue = mt76_wake_tx_queue,
1523 	.release_buffered_frames = mt76_release_buffered_frames,
1524 	.channel_switch_beacon = mt7921_channel_switch_beacon,
1525 	.get_txpower = mt76_get_txpower,
1526 	.get_stats = mt7921_get_stats,
1527 	.get_et_sset_count = mt7921_get_et_sset_count,
1528 	.get_et_strings = mt7921_get_et_strings,
1529 	.get_et_stats = mt7921_get_et_stats,
1530 	.get_tsf = mt7921_get_tsf,
1531 	.set_tsf = mt7921_set_tsf,
1532 	.get_survey = mt76_get_survey,
1533 	.get_antenna = mt76_get_antenna,
1534 	.set_antenna = mt7921_set_antenna,
1535 	.set_coverage_class = mt7921_set_coverage_class,
1536 	.hw_scan = mt7921_hw_scan,
1537 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1538 	.sta_statistics = mt7921_sta_statistics,
1539 	.sched_scan_start = mt7921_start_sched_scan,
1540 	.sched_scan_stop = mt7921_stop_sched_scan,
1541 	CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
1542 	CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
1543 #ifdef CONFIG_PM
1544 	.suspend = mt7921_suspend,
1545 	.resume = mt7921_resume,
1546 	.set_wakeup = mt7921_set_wakeup,
1547 	.set_rekey_data = mt7921_set_rekey_data,
1548 #endif /* CONFIG_PM */
1549 	.flush = mt7921_flush,
1550 	.set_sar_specs = mt7921_set_sar_specs,
1551 };
1552 EXPORT_SYMBOL_GPL(mt7921_ops);
1553 
1554 MODULE_LICENSE("Dual BSD/GPL");
1555 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1556