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 = mt7921_set_tx_sar_pwr(mphy->hw, NULL);
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 % MT76_CONNAC_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.phy_idx = 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 	vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
356 out:
357 	mt7921_mutex_release(dev);
358 
359 	return ret;
360 }
361 
362 static void mt7921_remove_interface(struct ieee80211_hw *hw,
363 				    struct ieee80211_vif *vif)
364 {
365 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
366 	struct mt7921_sta *msta = &mvif->sta;
367 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
368 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
369 	int idx = msta->wcid.idx;
370 
371 	mt7921_mutex_acquire(dev);
372 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
373 	mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid, false);
374 
375 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
376 
377 	dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx);
378 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
379 	mt7921_mutex_release(dev);
380 
381 	spin_lock_bh(&dev->sta_poll_lock);
382 	if (!list_empty(&msta->poll_list))
383 		list_del_init(&msta->poll_list);
384 	spin_unlock_bh(&dev->sta_poll_lock);
385 
386 	mt76_packet_id_flush(&dev->mt76, &msta->wcid);
387 }
388 
389 static void mt7921_roc_iter(void *priv, u8 *mac,
390 			    struct ieee80211_vif *vif)
391 {
392 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
393 	struct mt7921_phy *phy = priv;
394 
395 	mt7921_mcu_abort_roc(phy, mvif, phy->roc_token_id);
396 }
397 
398 void mt7921_roc_work(struct work_struct *work)
399 {
400 	struct mt7921_phy *phy;
401 
402 	phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy,
403 						roc_work);
404 
405 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
406 		return;
407 
408 	mt7921_mutex_acquire(phy->dev);
409 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
410 					    IEEE80211_IFACE_ITER_RESUME_ALL,
411 					    mt7921_roc_iter, phy);
412 	mt7921_mutex_release(phy->dev);
413 	ieee80211_remain_on_channel_expired(phy->mt76->hw);
414 }
415 
416 void mt7921_roc_timer(struct timer_list *timer)
417 {
418 	struct mt7921_phy *phy = from_timer(phy, timer, roc_timer);
419 
420 	ieee80211_queue_work(phy->mt76->hw, &phy->roc_work);
421 }
422 
423 static int mt7921_abort_roc(struct mt7921_phy *phy, struct mt7921_vif *vif)
424 {
425 	int err = 0;
426 
427 	del_timer_sync(&phy->roc_timer);
428 	cancel_work_sync(&phy->roc_work);
429 
430 	mt7921_mutex_acquire(phy->dev);
431 	if (test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
432 		err = mt7921_mcu_abort_roc(phy, vif, phy->roc_token_id);
433 	mt7921_mutex_release(phy->dev);
434 
435 	return err;
436 }
437 
438 static int mt7921_set_roc(struct mt7921_phy *phy,
439 			  struct mt7921_vif *vif,
440 			  struct ieee80211_channel *chan,
441 			  int duration,
442 			  enum mt7921_roc_req type)
443 {
444 	int err;
445 
446 	if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
447 		return -EBUSY;
448 
449 	phy->roc_grant = false;
450 
451 	err = mt7921_mcu_set_roc(phy, vif, chan, duration, type,
452 				 ++phy->roc_token_id);
453 	if (err < 0) {
454 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
455 		goto out;
456 	}
457 
458 	if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) {
459 		mt7921_mcu_abort_roc(phy, vif, phy->roc_token_id);
460 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
461 		err = -ETIMEDOUT;
462 	}
463 
464 out:
465 	return err;
466 }
467 
468 static int mt7921_remain_on_channel(struct ieee80211_hw *hw,
469 				    struct ieee80211_vif *vif,
470 				    struct ieee80211_channel *chan,
471 				    int duration,
472 				    enum ieee80211_roc_type type)
473 {
474 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
475 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
476 	int err;
477 
478 	mt7921_mutex_acquire(phy->dev);
479 	err = mt7921_set_roc(phy, mvif, chan, duration, MT7921_ROC_REQ_ROC);
480 	mt7921_mutex_release(phy->dev);
481 
482 	return err;
483 }
484 
485 static int mt7921_cancel_remain_on_channel(struct ieee80211_hw *hw,
486 					   struct ieee80211_vif *vif)
487 {
488 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
489 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
490 
491 	return mt7921_abort_roc(phy, mvif);
492 }
493 
494 static int mt7921_set_channel(struct mt7921_phy *phy)
495 {
496 	struct mt7921_dev *dev = phy->dev;
497 	int ret;
498 
499 	cancel_delayed_work_sync(&phy->mt76->mac_work);
500 
501 	mt7921_mutex_acquire(dev);
502 	set_bit(MT76_RESET, &phy->mt76->state);
503 
504 	mt76_set_channel(phy->mt76);
505 
506 	ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
507 	if (ret)
508 		goto out;
509 
510 	mt7921_mac_set_timing(phy);
511 
512 	mt7921_mac_reset_counters(phy);
513 	phy->noise = 0;
514 
515 out:
516 	clear_bit(MT76_RESET, &phy->mt76->state);
517 	mt7921_mutex_release(dev);
518 
519 	mt76_worker_schedule(&dev->mt76.tx_worker);
520 	ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mt76->mac_work,
521 				     MT7921_WATCHDOG_TIME);
522 
523 	return ret;
524 }
525 
526 static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
527 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
528 			  struct ieee80211_key_conf *key)
529 {
530 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
531 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
532 	struct mt7921_sta *msta = sta ? (struct mt7921_sta *)sta->drv_priv :
533 				  &mvif->sta;
534 	struct mt76_wcid *wcid = &msta->wcid;
535 	u8 *wcid_keyidx = &wcid->hw_key_idx;
536 	int idx = key->keyidx, err = 0;
537 
538 	/* The hardware does not support per-STA RX GTK, fallback
539 	 * to software mode for these.
540 	 */
541 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
542 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
543 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
544 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
545 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
546 		return -EOPNOTSUPP;
547 
548 	/* fall back to sw encryption for unsupported ciphers */
549 	switch (key->cipher) {
550 	case WLAN_CIPHER_SUITE_AES_CMAC:
551 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
552 		wcid_keyidx = &wcid->hw_key_idx2;
553 		break;
554 	case WLAN_CIPHER_SUITE_WEP40:
555 	case WLAN_CIPHER_SUITE_WEP104:
556 		if (!mvif->wep_sta)
557 			return -EOPNOTSUPP;
558 		break;
559 	case WLAN_CIPHER_SUITE_TKIP:
560 	case WLAN_CIPHER_SUITE_CCMP:
561 	case WLAN_CIPHER_SUITE_CCMP_256:
562 	case WLAN_CIPHER_SUITE_GCMP:
563 	case WLAN_CIPHER_SUITE_GCMP_256:
564 	case WLAN_CIPHER_SUITE_SMS4:
565 		break;
566 	default:
567 		return -EOPNOTSUPP;
568 	}
569 
570 	mt7921_mutex_acquire(dev);
571 
572 	if (cmd == SET_KEY) {
573 		*wcid_keyidx = idx;
574 	} else {
575 		if (idx == *wcid_keyidx)
576 			*wcid_keyidx = -1;
577 		goto out;
578 	}
579 
580 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
581 	err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->bip,
582 				      key, MCU_UNI_CMD(STA_REC_UPDATE),
583 				      &msta->wcid, cmd);
584 	if (err)
585 		goto out;
586 
587 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
588 	    key->cipher == WLAN_CIPHER_SUITE_WEP40)
589 		err = mt76_connac_mcu_add_key(&dev->mt76, vif,
590 					      &mvif->wep_sta->bip,
591 					      key, MCU_UNI_CMD(STA_REC_UPDATE),
592 					      &mvif->wep_sta->wcid, cmd);
593 out:
594 	mt7921_mutex_release(dev);
595 
596 	return err;
597 }
598 
599 static void
600 mt7921_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
601 {
602 	struct mt7921_dev *dev = priv;
603 	struct ieee80211_hw *hw = mt76_hw(dev);
604 	bool pm_enable = dev->pm.enable;
605 	int err;
606 
607 	err = mt7921_mcu_set_beacon_filter(dev, vif, pm_enable);
608 	if (err < 0)
609 		return;
610 
611 	if (pm_enable) {
612 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
613 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
614 	} else {
615 		vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
616 		__clear_bit(IEEE80211_HW_CONNECTION_MONITOR, hw->flags);
617 	}
618 }
619 
620 static void
621 mt7921_sniffer_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
622 {
623 	struct mt7921_dev *dev = priv;
624 	struct ieee80211_hw *hw = mt76_hw(dev);
625 	struct mt76_connac_pm *pm = &dev->pm;
626 	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
627 
628 	mt7921_mcu_set_sniffer(dev, vif, monitor);
629 	pm->enable = pm->enable_user && !monitor;
630 	pm->ds_enable = pm->ds_enable_user && !monitor;
631 
632 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
633 
634 	if (monitor)
635 		mt7921_mcu_set_beacon_filter(dev, vif, false);
636 }
637 
638 void mt7921_set_runtime_pm(struct mt7921_dev *dev)
639 {
640 	struct ieee80211_hw *hw = mt76_hw(dev);
641 	struct mt76_connac_pm *pm = &dev->pm;
642 	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
643 
644 	pm->enable = pm->enable_user && !monitor;
645 	ieee80211_iterate_active_interfaces(hw,
646 					    IEEE80211_IFACE_ITER_RESUME_ALL,
647 					    mt7921_pm_interface_iter, dev);
648 	pm->ds_enable = pm->ds_enable_user && !monitor;
649 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
650 }
651 
652 static int mt7921_config(struct ieee80211_hw *hw, u32 changed)
653 {
654 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
655 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
656 	int ret = 0;
657 
658 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
659 		ieee80211_stop_queues(hw);
660 		ret = mt7921_set_channel(phy);
661 		if (ret)
662 			return ret;
663 		ieee80211_wake_queues(hw);
664 	}
665 
666 	mt7921_mutex_acquire(dev);
667 
668 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
669 		ret = mt7921_set_tx_sar_pwr(hw, NULL);
670 		if (ret)
671 			goto out;
672 	}
673 
674 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
675 		ieee80211_iterate_active_interfaces(hw,
676 						    IEEE80211_IFACE_ITER_RESUME_ALL,
677 						    mt7921_sniffer_interface_iter, dev);
678 	}
679 
680 out:
681 	mt7921_mutex_release(dev);
682 
683 	return ret;
684 }
685 
686 static int
687 mt7921_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
688 	       unsigned int link_id, u16 queue,
689 	       const struct ieee80211_tx_queue_params *params)
690 {
691 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
692 
693 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
694 	queue = mt76_connac_lmac_mapping(queue);
695 	mvif->queue_params[queue] = *params;
696 
697 	return 0;
698 }
699 
700 static void mt7921_configure_filter(struct ieee80211_hw *hw,
701 				    unsigned int changed_flags,
702 				    unsigned int *total_flags,
703 				    u64 multicast)
704 {
705 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
706 
707 	mt7921_mutex_acquire(dev);
708 	mt7921_mcu_set_rxfilter(dev, *total_flags, 0, 0);
709 	mt7921_mutex_release(dev);
710 
711 	*total_flags &= (FIF_OTHER_BSS | FIF_FCSFAIL | FIF_CONTROL);
712 }
713 
714 static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
715 				    struct ieee80211_vif *vif,
716 				    struct ieee80211_bss_conf *info,
717 				    u64 changed)
718 {
719 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
720 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
721 
722 	mt7921_mutex_acquire(dev);
723 
724 	if (changed & BSS_CHANGED_ERP_SLOT) {
725 		int slottime = info->use_short_slot ? 9 : 20;
726 
727 		if (slottime != phy->slottime) {
728 			phy->slottime = slottime;
729 			mt7921_mac_set_timing(phy);
730 		}
731 	}
732 
733 	if (changed & (BSS_CHANGED_BEACON |
734 		       BSS_CHANGED_BEACON_ENABLED))
735 		mt7921_mcu_uni_add_beacon_offload(dev, hw, vif,
736 						  info->enable_beacon);
737 
738 	/* ensure that enable txcmd_mode after bss_info */
739 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
740 		mt7921_mcu_set_tx(dev, vif);
741 
742 	if (changed & BSS_CHANGED_PS)
743 		mt7921_mcu_uni_bss_ps(dev, vif);
744 
745 	if (changed & BSS_CHANGED_ASSOC) {
746 		mt7921_mcu_sta_update(dev, NULL, vif, true,
747 				      MT76_STA_INFO_STATE_ASSOC);
748 		mt7921_mcu_set_beacon_filter(dev, vif, vif->cfg.assoc);
749 	}
750 
751 	if (changed & BSS_CHANGED_ARP_FILTER) {
752 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
753 
754 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
755 						  info);
756 	}
757 
758 	mt7921_mutex_release(dev);
759 }
760 
761 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
762 		       struct ieee80211_sta *sta)
763 {
764 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
765 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
766 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
767 	int ret, idx;
768 
769 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
770 	if (idx < 0)
771 		return -ENOSPC;
772 
773 	INIT_LIST_HEAD(&msta->poll_list);
774 	msta->vif = mvif;
775 	msta->wcid.sta = 1;
776 	msta->wcid.idx = idx;
777 	msta->wcid.phy_idx = mvif->mt76.band_idx;
778 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
779 	msta->last_txs = jiffies;
780 
781 	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
782 	if (ret)
783 		return ret;
784 
785 	if (vif->type == NL80211_IFTYPE_STATION)
786 		mvif->wep_sta = msta;
787 
788 	mt7921_mac_wtbl_update(dev, idx,
789 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
790 
791 	ret = mt7921_mcu_sta_update(dev, sta, vif, true,
792 				    MT76_STA_INFO_STATE_NONE);
793 	if (ret)
794 		return ret;
795 
796 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
797 
798 	return 0;
799 }
800 EXPORT_SYMBOL_GPL(mt7921_mac_sta_add);
801 
802 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
803 			  struct ieee80211_sta *sta)
804 {
805 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
806 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
807 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
808 
809 	mt7921_mutex_acquire(dev);
810 
811 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
812 		mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
813 					    true, mvif->ctx);
814 
815 	ewma_avg_signal_init(&msta->avg_ack_signal);
816 
817 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
818 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
819 	memset(msta->airtime_ac, 0, sizeof(msta->airtime_ac));
820 
821 	mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
822 
823 	mt7921_mutex_release(dev);
824 }
825 EXPORT_SYMBOL_GPL(mt7921_mac_sta_assoc);
826 
827 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
828 			   struct ieee80211_sta *sta)
829 {
830 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
831 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
832 
833 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
834 	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
835 
836 	mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
837 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
838 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
839 
840 	if (vif->type == NL80211_IFTYPE_STATION) {
841 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
842 
843 		mvif->wep_sta = NULL;
844 		ewma_rssi_init(&mvif->rssi);
845 		if (!sta->tdls)
846 			mt76_connac_mcu_uni_add_bss(&dev->mphy, vif,
847 						    &mvif->sta.wcid, false,
848 						    mvif->ctx);
849 	}
850 
851 	spin_lock_bh(&dev->sta_poll_lock);
852 	if (!list_empty(&msta->poll_list))
853 		list_del_init(&msta->poll_list);
854 	spin_unlock_bh(&dev->sta_poll_lock);
855 
856 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
857 }
858 EXPORT_SYMBOL_GPL(mt7921_mac_sta_remove);
859 
860 void mt7921_tx_worker(struct mt76_worker *w)
861 {
862 	struct mt7921_dev *dev = container_of(w, struct mt7921_dev,
863 					      mt76.tx_worker);
864 
865 	if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
866 		queue_work(dev->mt76.wq, &dev->pm.wake_work);
867 		return;
868 	}
869 
870 	mt76_txq_schedule_all(&dev->mphy);
871 	mt76_connac_pm_unref(&dev->mphy, &dev->pm);
872 }
873 
874 static void mt7921_tx(struct ieee80211_hw *hw,
875 		      struct ieee80211_tx_control *control,
876 		      struct sk_buff *skb)
877 {
878 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
879 	struct mt76_phy *mphy = hw->priv;
880 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
881 	struct ieee80211_vif *vif = info->control.vif;
882 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
883 	int qid;
884 
885 	if (control->sta) {
886 		struct mt7921_sta *sta;
887 
888 		sta = (struct mt7921_sta *)control->sta->drv_priv;
889 		wcid = &sta->wcid;
890 	}
891 
892 	if (vif && !control->sta) {
893 		struct mt7921_vif *mvif;
894 
895 		mvif = (struct mt7921_vif *)vif->drv_priv;
896 		wcid = &mvif->sta.wcid;
897 	}
898 
899 	if (mt76_connac_pm_ref(mphy, &dev->pm)) {
900 		mt76_tx(mphy, control->sta, wcid, skb);
901 		mt76_connac_pm_unref(mphy, &dev->pm);
902 		return;
903 	}
904 
905 	qid = skb_get_queue_mapping(skb);
906 	if (qid >= MT_TXQ_PSD) {
907 		qid = IEEE80211_AC_BE;
908 		skb_set_queue_mapping(skb, qid);
909 	}
910 
911 	mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
912 }
913 
914 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
915 {
916 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
917 
918 	mt7921_mutex_acquire(dev);
919 	mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
920 	mt7921_mutex_release(dev);
921 
922 	return 0;
923 }
924 
925 static int
926 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
927 		    struct ieee80211_ampdu_params *params)
928 {
929 	enum ieee80211_ampdu_mlme_action action = params->action;
930 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
931 	struct ieee80211_sta *sta = params->sta;
932 	struct ieee80211_txq *txq = sta->txq[params->tid];
933 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
934 	u16 tid = params->tid;
935 	u16 ssn = params->ssn;
936 	struct mt76_txq *mtxq;
937 	int ret = 0;
938 
939 	if (!txq)
940 		return -EINVAL;
941 
942 	mtxq = (struct mt76_txq *)txq->drv_priv;
943 
944 	mt7921_mutex_acquire(dev);
945 	switch (action) {
946 	case IEEE80211_AMPDU_RX_START:
947 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
948 				   params->buf_size);
949 		mt7921_mcu_uni_rx_ba(dev, params, true);
950 		break;
951 	case IEEE80211_AMPDU_RX_STOP:
952 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
953 		mt7921_mcu_uni_rx_ba(dev, params, false);
954 		break;
955 	case IEEE80211_AMPDU_TX_OPERATIONAL:
956 		mtxq->aggr = true;
957 		mtxq->send_bar = false;
958 		mt7921_mcu_uni_tx_ba(dev, params, true);
959 		break;
960 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
961 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
962 		mtxq->aggr = false;
963 		clear_bit(tid, &msta->ampdu_state);
964 		mt7921_mcu_uni_tx_ba(dev, params, false);
965 		break;
966 	case IEEE80211_AMPDU_TX_START:
967 		set_bit(tid, &msta->ampdu_state);
968 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
969 		break;
970 	case IEEE80211_AMPDU_TX_STOP_CONT:
971 		mtxq->aggr = false;
972 		clear_bit(tid, &msta->ampdu_state);
973 		mt7921_mcu_uni_tx_ba(dev, params, false);
974 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
975 		break;
976 	}
977 	mt7921_mutex_release(dev);
978 
979 	return ret;
980 }
981 
982 static int mt7921_sta_state(struct ieee80211_hw *hw,
983 			    struct ieee80211_vif *vif,
984 			    struct ieee80211_sta *sta,
985 			    enum ieee80211_sta_state old_state,
986 			    enum ieee80211_sta_state new_state)
987 {
988 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
989 
990 	if (dev->pm.ds_enable) {
991 		mt7921_mutex_acquire(dev);
992 		mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state);
993 		mt7921_mutex_release(dev);
994 	}
995 
996 	return mt76_sta_state(hw, vif, sta, old_state, new_state);
997 }
998 
999 static int
1000 mt7921_get_stats(struct ieee80211_hw *hw,
1001 		 struct ieee80211_low_level_stats *stats)
1002 {
1003 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1004 	struct mib_stats *mib = &phy->mib;
1005 
1006 	mt7921_mutex_acquire(phy->dev);
1007 
1008 	stats->dot11RTSSuccessCount = mib->rts_cnt;
1009 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
1010 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
1011 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
1012 
1013 	mt7921_mutex_release(phy->dev);
1014 
1015 	return 0;
1016 }
1017 
1018 static const char mt7921_gstrings_stats[][ETH_GSTRING_LEN] = {
1019 	/* tx counters */
1020 	"tx_ampdu_cnt",
1021 	"tx_mpdu_attempts",
1022 	"tx_mpdu_success",
1023 	"tx_pkt_ebf_cnt",
1024 	"tx_pkt_ibf_cnt",
1025 	"tx_ampdu_len:0-1",
1026 	"tx_ampdu_len:2-10",
1027 	"tx_ampdu_len:11-19",
1028 	"tx_ampdu_len:20-28",
1029 	"tx_ampdu_len:29-37",
1030 	"tx_ampdu_len:38-46",
1031 	"tx_ampdu_len:47-55",
1032 	"tx_ampdu_len:56-79",
1033 	"tx_ampdu_len:80-103",
1034 	"tx_ampdu_len:104-127",
1035 	"tx_ampdu_len:128-151",
1036 	"tx_ampdu_len:152-175",
1037 	"tx_ampdu_len:176-199",
1038 	"tx_ampdu_len:200-223",
1039 	"tx_ampdu_len:224-247",
1040 	"ba_miss_count",
1041 	"tx_beamformer_ppdu_iBF",
1042 	"tx_beamformer_ppdu_eBF",
1043 	"tx_beamformer_rx_feedback_all",
1044 	"tx_beamformer_rx_feedback_he",
1045 	"tx_beamformer_rx_feedback_vht",
1046 	"tx_beamformer_rx_feedback_ht",
1047 	"tx_msdu_pack_1",
1048 	"tx_msdu_pack_2",
1049 	"tx_msdu_pack_3",
1050 	"tx_msdu_pack_4",
1051 	"tx_msdu_pack_5",
1052 	"tx_msdu_pack_6",
1053 	"tx_msdu_pack_7",
1054 	"tx_msdu_pack_8",
1055 	/* rx counters */
1056 	"rx_mpdu_cnt",
1057 	"rx_ampdu_cnt",
1058 	"rx_ampdu_bytes_cnt",
1059 	"rx_ba_cnt",
1060 	/* per vif counters */
1061 	"v_tx_mode_cck",
1062 	"v_tx_mode_ofdm",
1063 	"v_tx_mode_ht",
1064 	"v_tx_mode_ht_gf",
1065 	"v_tx_mode_vht",
1066 	"v_tx_mode_he_su",
1067 	"v_tx_mode_he_ext_su",
1068 	"v_tx_mode_he_tb",
1069 	"v_tx_mode_he_mu",
1070 	"v_tx_bw_20",
1071 	"v_tx_bw_40",
1072 	"v_tx_bw_80",
1073 	"v_tx_bw_160",
1074 	"v_tx_mcs_0",
1075 	"v_tx_mcs_1",
1076 	"v_tx_mcs_2",
1077 	"v_tx_mcs_3",
1078 	"v_tx_mcs_4",
1079 	"v_tx_mcs_5",
1080 	"v_tx_mcs_6",
1081 	"v_tx_mcs_7",
1082 	"v_tx_mcs_8",
1083 	"v_tx_mcs_9",
1084 	"v_tx_mcs_10",
1085 	"v_tx_mcs_11",
1086 };
1087 
1088 static void
1089 mt7921_get_et_strings(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1090 		      u32 sset, u8 *data)
1091 {
1092 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1093 
1094 	if (sset != ETH_SS_STATS)
1095 		return;
1096 
1097 	memcpy(data, *mt7921_gstrings_stats, sizeof(mt7921_gstrings_stats));
1098 
1099 	if (mt76_is_sdio(&dev->mt76))
1100 		return;
1101 
1102 	data += sizeof(mt7921_gstrings_stats);
1103 	page_pool_ethtool_stats_get_strings(data);
1104 }
1105 
1106 static int
1107 mt7921_get_et_sset_count(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1108 			 int sset)
1109 {
1110 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1111 
1112 	if (sset != ETH_SS_STATS)
1113 		return 0;
1114 
1115 	if (mt76_is_sdio(&dev->mt76))
1116 		return ARRAY_SIZE(mt7921_gstrings_stats);
1117 
1118 	return ARRAY_SIZE(mt7921_gstrings_stats) +
1119 	       page_pool_ethtool_stats_get_count();
1120 }
1121 
1122 static void
1123 mt7921_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1124 {
1125 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1126 	struct mt76_ethtool_worker_info *wi = wi_data;
1127 
1128 	if (msta->vif->mt76.idx != wi->idx)
1129 		return;
1130 
1131 	mt76_ethtool_worker(wi, &msta->wcid.stats, false);
1132 }
1133 
1134 static
1135 void mt7921_get_et_stats(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1136 			 struct ethtool_stats *stats, u64 *data)
1137 {
1138 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1139 	int stats_size = ARRAY_SIZE(mt7921_gstrings_stats);
1140 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1141 	struct mt7921_dev *dev = phy->dev;
1142 	struct mib_stats *mib = &phy->mib;
1143 	struct mt76_ethtool_worker_info wi = {
1144 		.data = data,
1145 		.idx = mvif->mt76.idx,
1146 	};
1147 	int i, ei = 0;
1148 
1149 	mt7921_mutex_acquire(dev);
1150 
1151 	mt7921_mac_update_mib_stats(phy);
1152 
1153 	data[ei++] = mib->tx_ampdu_cnt;
1154 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1155 	data[ei++] = mib->tx_mpdu_success_cnt;
1156 	data[ei++] = mib->tx_pkt_ebf_cnt;
1157 	data[ei++] = mib->tx_pkt_ibf_cnt;
1158 
1159 	/* Tx ampdu stat */
1160 	for (i = 0; i < 15; i++)
1161 		data[ei++] = phy->mt76->aggr_stats[i];
1162 
1163 	data[ei++] = phy->mib.ba_miss_cnt;
1164 
1165 	/* Tx Beamformer monitor */
1166 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1167 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1168 
1169 	/* Tx Beamformer Rx feedback monitor */
1170 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1171 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1172 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1173 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1174 
1175 	/* Tx amsdu info (pack-count histogram) */
1176 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1177 		data[ei++] = mib->tx_amsdu[i];
1178 
1179 	/* rx counters */
1180 	data[ei++] = mib->rx_mpdu_cnt;
1181 	data[ei++] = mib->rx_ampdu_cnt;
1182 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1183 	data[ei++] = mib->rx_ba_cnt;
1184 
1185 	/* Add values for all stations owned by this vif */
1186 	wi.initial_stat_idx = ei;
1187 	ieee80211_iterate_stations_atomic(hw, mt7921_ethtool_worker, &wi);
1188 
1189 	mt7921_mutex_release(dev);
1190 
1191 	if (!wi.sta_count)
1192 		return;
1193 
1194 	ei += wi.worker_stat_count;
1195 
1196 	if (!mt76_is_sdio(&dev->mt76)) {
1197 		mt76_ethtool_page_pool_stats(&dev->mt76, &data[ei], &ei);
1198 		stats_size += page_pool_ethtool_stats_get_count();
1199 	}
1200 
1201 	if (ei != stats_size)
1202 		dev_err(dev->mt76.dev, "ei: %d  SSTATS_LEN: %d", ei, stats_size);
1203 }
1204 
1205 static u64
1206 mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1207 {
1208 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1209 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1210 	u8 omac_idx = mvif->mt76.omac_idx;
1211 	union {
1212 		u64 t64;
1213 		u32 t32[2];
1214 	} tsf;
1215 	u16 n;
1216 
1217 	mt7921_mutex_acquire(dev);
1218 
1219 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1220 	/* TSF software read */
1221 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_MODE);
1222 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(0));
1223 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(0));
1224 
1225 	mt7921_mutex_release(dev);
1226 
1227 	return tsf.t64;
1228 }
1229 
1230 static void
1231 mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1232 	       u64 timestamp)
1233 {
1234 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1235 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1236 	u8 omac_idx = mvif->mt76.omac_idx;
1237 	union {
1238 		u64 t64;
1239 		u32 t32[2];
1240 	} tsf = { .t64 = timestamp, };
1241 	u16 n;
1242 
1243 	mt7921_mutex_acquire(dev);
1244 
1245 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1246 	mt76_wr(dev, MT_LPON_UTTR0(0), tsf.t32[0]);
1247 	mt76_wr(dev, MT_LPON_UTTR1(0), tsf.t32[1]);
1248 	/* TSF software overwrite */
1249 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_WRITE);
1250 
1251 	mt7921_mutex_release(dev);
1252 }
1253 
1254 static void
1255 mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
1256 {
1257 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1258 	struct mt7921_dev *dev = phy->dev;
1259 
1260 	mt7921_mutex_acquire(dev);
1261 	phy->coverage_class = max_t(s16, coverage_class, 0);
1262 	mt7921_mac_set_timing(phy);
1263 	mt7921_mutex_release(dev);
1264 }
1265 
1266 void mt7921_scan_work(struct work_struct *work)
1267 {
1268 	struct mt7921_phy *phy;
1269 
1270 	phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy,
1271 						scan_work.work);
1272 
1273 	while (true) {
1274 		struct mt76_connac2_mcu_rxd *rxd;
1275 		struct sk_buff *skb;
1276 
1277 		spin_lock_bh(&phy->dev->mt76.lock);
1278 		skb = __skb_dequeue(&phy->scan_event_list);
1279 		spin_unlock_bh(&phy->dev->mt76.lock);
1280 
1281 		if (!skb)
1282 			break;
1283 
1284 		rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
1285 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1286 			ieee80211_sched_scan_results(phy->mt76->hw);
1287 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
1288 					      &phy->mt76->state)) {
1289 			struct cfg80211_scan_info info = {
1290 				.aborted = false,
1291 			};
1292 
1293 			ieee80211_scan_completed(phy->mt76->hw, &info);
1294 		}
1295 		dev_kfree_skb(skb);
1296 	}
1297 }
1298 
1299 static int
1300 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1301 	       struct ieee80211_scan_request *req)
1302 {
1303 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1304 	struct mt76_phy *mphy = hw->priv;
1305 	int err;
1306 
1307 	mt7921_mutex_acquire(dev);
1308 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1309 	mt7921_mutex_release(dev);
1310 
1311 	return err;
1312 }
1313 
1314 static void
1315 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1316 {
1317 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1318 	struct mt76_phy *mphy = hw->priv;
1319 
1320 	mt7921_mutex_acquire(dev);
1321 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1322 	mt7921_mutex_release(dev);
1323 }
1324 
1325 static int
1326 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1327 			struct cfg80211_sched_scan_request *req,
1328 			struct ieee80211_scan_ies *ies)
1329 {
1330 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1331 	struct mt76_phy *mphy = hw->priv;
1332 	int err;
1333 
1334 	mt7921_mutex_acquire(dev);
1335 
1336 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1337 	if (err < 0)
1338 		goto out;
1339 
1340 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1341 out:
1342 	mt7921_mutex_release(dev);
1343 
1344 	return err;
1345 }
1346 
1347 static int
1348 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1349 {
1350 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1351 	struct mt76_phy *mphy = hw->priv;
1352 	int err;
1353 
1354 	mt7921_mutex_acquire(dev);
1355 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1356 	mt7921_mutex_release(dev);
1357 
1358 	return err;
1359 }
1360 
1361 static int
1362 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1363 {
1364 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1365 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1366 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1367 
1368 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1369 		return -EINVAL;
1370 
1371 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1372 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1373 
1374 	mt7921_mutex_acquire(dev);
1375 
1376 	phy->mt76->antenna_mask = tx_ant;
1377 	phy->mt76->chainmask = tx_ant;
1378 
1379 	mt76_set_stream_caps(phy->mt76, true);
1380 	mt7921_set_stream_he_caps(phy);
1381 
1382 	mt7921_mutex_release(dev);
1383 
1384 	return 0;
1385 }
1386 
1387 static void mt7921_sta_statistics(struct ieee80211_hw *hw,
1388 				  struct ieee80211_vif *vif,
1389 				  struct ieee80211_sta *sta,
1390 				  struct station_info *sinfo)
1391 {
1392 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1393 	struct rate_info *txrate = &msta->wcid.rate;
1394 
1395 	if (!txrate->legacy && !txrate->flags)
1396 		return;
1397 
1398 	if (txrate->legacy) {
1399 		sinfo->txrate.legacy = txrate->legacy;
1400 	} else {
1401 		sinfo->txrate.mcs = txrate->mcs;
1402 		sinfo->txrate.nss = txrate->nss;
1403 		sinfo->txrate.bw = txrate->bw;
1404 		sinfo->txrate.he_gi = txrate->he_gi;
1405 		sinfo->txrate.he_dcm = txrate->he_dcm;
1406 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1407 	}
1408 	sinfo->txrate.flags = txrate->flags;
1409 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1410 
1411 	sinfo->ack_signal = (s8)msta->ack_signal;
1412 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
1413 
1414 	sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->avg_ack_signal);
1415 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
1416 }
1417 
1418 #ifdef CONFIG_PM
1419 static int mt7921_suspend(struct ieee80211_hw *hw,
1420 			  struct cfg80211_wowlan *wowlan)
1421 {
1422 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1423 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1424 
1425 	cancel_delayed_work_sync(&phy->scan_work);
1426 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1427 
1428 	cancel_delayed_work_sync(&dev->pm.ps_work);
1429 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1430 
1431 	mt7921_mutex_acquire(dev);
1432 
1433 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1434 	ieee80211_iterate_active_interfaces(hw,
1435 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1436 					    mt7921_mcu_set_suspend_iter,
1437 					    &dev->mphy);
1438 
1439 	mt7921_mutex_release(dev);
1440 
1441 	return 0;
1442 }
1443 
1444 static int mt7921_resume(struct ieee80211_hw *hw)
1445 {
1446 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1447 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1448 
1449 	mt7921_mutex_acquire(dev);
1450 
1451 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1452 	ieee80211_iterate_active_interfaces(hw,
1453 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1454 					    mt76_connac_mcu_set_suspend_iter,
1455 					    &dev->mphy);
1456 
1457 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1458 				     MT7921_WATCHDOG_TIME);
1459 
1460 	mt7921_mutex_release(dev);
1461 
1462 	return 0;
1463 }
1464 
1465 static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1466 {
1467 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1468 	struct mt76_dev *mdev = &dev->mt76;
1469 
1470 	device_set_wakeup_enable(mdev->dev, enabled);
1471 }
1472 
1473 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1474 				  struct ieee80211_vif *vif,
1475 				  struct cfg80211_gtk_rekey_data *data)
1476 {
1477 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1478 
1479 	mt7921_mutex_acquire(dev);
1480 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1481 	mt7921_mutex_release(dev);
1482 }
1483 #endif /* CONFIG_PM */
1484 
1485 static void mt7921_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1486 			 u32 queues, bool drop)
1487 {
1488 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1489 
1490 	wait_event_timeout(dev->mt76.tx_wait, !mt76_has_tx_pending(&dev->mphy),
1491 			   HZ / 2);
1492 }
1493 
1494 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1495 					 struct ieee80211_vif *vif,
1496 					 struct ieee80211_sta *sta,
1497 					 bool enabled)
1498 {
1499 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1500 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1501 
1502 	mt7921_mutex_acquire(dev);
1503 
1504 	if (enabled)
1505 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1506 	else
1507 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1508 
1509 	mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->wcid,
1510 					     MCU_UNI_CMD(STA_REC_UPDATE));
1511 
1512 	mt7921_mutex_release(dev);
1513 }
1514 
1515 #if IS_ENABLED(CONFIG_IPV6)
1516 static void mt7921_ipv6_addr_change(struct ieee80211_hw *hw,
1517 				    struct ieee80211_vif *vif,
1518 				    struct inet6_dev *idev)
1519 {
1520 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1521 	struct mt7921_dev *dev = mvif->phy->dev;
1522 	struct inet6_ifaddr *ifa;
1523 	struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN];
1524 	struct sk_buff *skb;
1525 	u8 i, idx = 0;
1526 
1527 	struct {
1528 		struct {
1529 			u8 bss_idx;
1530 			u8 pad[3];
1531 		} __packed hdr;
1532 		struct mt76_connac_arpns_tlv arpns;
1533 	} req_hdr = {
1534 		.hdr = {
1535 			.bss_idx = mvif->mt76.idx,
1536 		},
1537 		.arpns = {
1538 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
1539 			.mode = 2,  /* update */
1540 			.option = 1, /* update only */
1541 		},
1542 	};
1543 
1544 	read_lock_bh(&idev->lock);
1545 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
1546 		if (ifa->flags & IFA_F_TENTATIVE)
1547 			continue;
1548 		ns_addrs[idx] = ifa->addr;
1549 		if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN)
1550 			break;
1551 	}
1552 	read_unlock_bh(&idev->lock);
1553 
1554 	if (!idx)
1555 		return;
1556 
1557 	req_hdr.arpns.ips_num = idx;
1558 	req_hdr.arpns.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)
1559 					+ idx * sizeof(struct in6_addr));
1560 	skb = __mt76_mcu_msg_alloc(&dev->mt76, &req_hdr,
1561 			sizeof(req_hdr) + idx * sizeof(struct in6_addr),
1562 			sizeof(req_hdr), GFP_ATOMIC);
1563 	if (!skb)
1564 		return;
1565 
1566 	for (i = 0; i < idx; i++)
1567 		skb_put_data(skb, &ns_addrs[i].in6_u, sizeof(struct in6_addr));
1568 
1569 	skb_queue_tail(&dev->ipv6_ns_list, skb);
1570 
1571 	ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work);
1572 }
1573 #endif
1574 
1575 int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw,
1576 			  const struct cfg80211_sar_specs *sar)
1577 {
1578 	struct mt76_phy *mphy = hw->priv;
1579 	int err;
1580 
1581 	if (sar) {
1582 		err = mt76_init_sar_power(hw, sar);
1583 		if (err)
1584 			return err;
1585 	}
1586 
1587 	mt7921_init_acpi_sar_power(mt7921_hw_phy(hw), !sar);
1588 
1589 	err = mt76_connac_mcu_set_rate_txpower(mphy);
1590 
1591 	return err;
1592 }
1593 
1594 static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
1595 				const struct cfg80211_sar_specs *sar)
1596 {
1597 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1598 	int err;
1599 
1600 	mt7921_mutex_acquire(dev);
1601 	err = mt7921_mcu_set_clc(dev, dev->mt76.alpha2,
1602 				 dev->country_ie_env);
1603 	if (err < 0)
1604 		goto out;
1605 
1606 	err = mt7921_set_tx_sar_pwr(hw, sar);
1607 out:
1608 	mt7921_mutex_release(dev);
1609 
1610 	return err;
1611 }
1612 
1613 static void
1614 mt7921_channel_switch_beacon(struct ieee80211_hw *hw,
1615 			     struct ieee80211_vif *vif,
1616 			     struct cfg80211_chan_def *chandef)
1617 {
1618 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1619 
1620 	mt7921_mutex_acquire(dev);
1621 	mt7921_mcu_uni_add_beacon_offload(dev, hw, vif, true);
1622 	mt7921_mutex_release(dev);
1623 }
1624 
1625 static int
1626 mt7921_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1627 		struct ieee80211_bss_conf *link_conf)
1628 {
1629 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1630 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1631 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1632 	int err;
1633 
1634 	mt7921_mutex_acquire(dev);
1635 
1636 	err = mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid,
1637 					  true, mvif->ctx);
1638 	if (err)
1639 		goto out;
1640 
1641 	err = mt7921_mcu_set_bss_pm(dev, vif, true);
1642 	if (err)
1643 		goto out;
1644 
1645 	err = mt7921_mcu_sta_update(dev, NULL, vif, true,
1646 				    MT76_STA_INFO_STATE_NONE);
1647 out:
1648 	mt7921_mutex_release(dev);
1649 
1650 	return err;
1651 }
1652 
1653 static void
1654 mt7921_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1655 	       struct ieee80211_bss_conf *link_conf)
1656 {
1657 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1658 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1659 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1660 	int err;
1661 
1662 	mt7921_mutex_acquire(dev);
1663 
1664 	err = mt7921_mcu_set_bss_pm(dev, vif, false);
1665 	if (err)
1666 		goto out;
1667 
1668 	mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid, false,
1669 				    mvif->ctx);
1670 
1671 out:
1672 	mt7921_mutex_release(dev);
1673 }
1674 
1675 static int
1676 mt7921_add_chanctx(struct ieee80211_hw *hw,
1677 		   struct ieee80211_chanctx_conf *ctx)
1678 {
1679 	return 0;
1680 }
1681 
1682 static void
1683 mt7921_remove_chanctx(struct ieee80211_hw *hw,
1684 		      struct ieee80211_chanctx_conf *ctx)
1685 {
1686 }
1687 
1688 static void mt7921_ctx_iter(void *priv, u8 *mac,
1689 			    struct ieee80211_vif *vif)
1690 {
1691 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1692 	struct ieee80211_chanctx_conf *ctx = priv;
1693 
1694 	if (ctx != mvif->ctx)
1695 		return;
1696 
1697 	if (vif->type & NL80211_IFTYPE_MONITOR)
1698 		mt7921_mcu_config_sniffer(mvif, ctx);
1699 	else
1700 		mt76_connac_mcu_uni_set_chctx(mvif->phy->mt76, &mvif->mt76, ctx);
1701 }
1702 
1703 static void
1704 mt7921_change_chanctx(struct ieee80211_hw *hw,
1705 		      struct ieee80211_chanctx_conf *ctx,
1706 		      u32 changed)
1707 {
1708 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1709 
1710 	mt7921_mutex_acquire(phy->dev);
1711 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
1712 					    IEEE80211_IFACE_ITER_ACTIVE,
1713 					    mt7921_ctx_iter, ctx);
1714 	mt7921_mutex_release(phy->dev);
1715 }
1716 
1717 static int
1718 mt7921_assign_vif_chanctx(struct ieee80211_hw *hw,
1719 			  struct ieee80211_vif *vif,
1720 			  struct ieee80211_bss_conf *link_conf,
1721 			  struct ieee80211_chanctx_conf *ctx)
1722 {
1723 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1724 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1725 
1726 	mutex_lock(&dev->mt76.mutex);
1727 	mvif->ctx = ctx;
1728 	mutex_unlock(&dev->mt76.mutex);
1729 
1730 	return 0;
1731 }
1732 
1733 static void
1734 mt7921_unassign_vif_chanctx(struct ieee80211_hw *hw,
1735 			    struct ieee80211_vif *vif,
1736 			    struct ieee80211_bss_conf *link_conf,
1737 			    struct ieee80211_chanctx_conf *ctx)
1738 {
1739 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1740 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1741 
1742 	mutex_lock(&dev->mt76.mutex);
1743 	mvif->ctx = NULL;
1744 	mutex_unlock(&dev->mt76.mutex);
1745 }
1746 
1747 static void mt7921_mgd_prepare_tx(struct ieee80211_hw *hw,
1748 				  struct ieee80211_vif *vif,
1749 				  struct ieee80211_prep_tx_info *info)
1750 {
1751 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1752 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1753 	u16 duration = info->duration ? info->duration :
1754 		       jiffies_to_msecs(HZ);
1755 
1756 	mt7921_mutex_acquire(dev);
1757 	mt7921_set_roc(mvif->phy, mvif, mvif->ctx->def.chan, duration,
1758 		       MT7921_ROC_REQ_JOIN);
1759 	mt7921_mutex_release(dev);
1760 }
1761 
1762 static void mt7921_mgd_complete_tx(struct ieee80211_hw *hw,
1763 				   struct ieee80211_vif *vif,
1764 				   struct ieee80211_prep_tx_info *info)
1765 {
1766 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1767 
1768 	mt7921_abort_roc(mvif->phy, mvif);
1769 }
1770 
1771 const struct ieee80211_ops mt7921_ops = {
1772 	.tx = mt7921_tx,
1773 	.start = mt7921_start,
1774 	.stop = mt7921_stop,
1775 	.add_interface = mt7921_add_interface,
1776 	.remove_interface = mt7921_remove_interface,
1777 	.config = mt7921_config,
1778 	.conf_tx = mt7921_conf_tx,
1779 	.configure_filter = mt7921_configure_filter,
1780 	.bss_info_changed = mt7921_bss_info_changed,
1781 	.start_ap = mt7921_start_ap,
1782 	.stop_ap = mt7921_stop_ap,
1783 	.sta_state = mt7921_sta_state,
1784 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1785 	.set_key = mt7921_set_key,
1786 	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1787 #if IS_ENABLED(CONFIG_IPV6)
1788 	.ipv6_addr_change = mt7921_ipv6_addr_change,
1789 #endif /* CONFIG_IPV6 */
1790 	.ampdu_action = mt7921_ampdu_action,
1791 	.set_rts_threshold = mt7921_set_rts_threshold,
1792 	.wake_tx_queue = mt76_wake_tx_queue,
1793 	.release_buffered_frames = mt76_release_buffered_frames,
1794 	.channel_switch_beacon = mt7921_channel_switch_beacon,
1795 	.get_txpower = mt76_get_txpower,
1796 	.get_stats = mt7921_get_stats,
1797 	.get_et_sset_count = mt7921_get_et_sset_count,
1798 	.get_et_strings = mt7921_get_et_strings,
1799 	.get_et_stats = mt7921_get_et_stats,
1800 	.get_tsf = mt7921_get_tsf,
1801 	.set_tsf = mt7921_set_tsf,
1802 	.get_survey = mt76_get_survey,
1803 	.get_antenna = mt76_get_antenna,
1804 	.set_antenna = mt7921_set_antenna,
1805 	.set_coverage_class = mt7921_set_coverage_class,
1806 	.hw_scan = mt7921_hw_scan,
1807 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1808 	.sta_statistics = mt7921_sta_statistics,
1809 	.sched_scan_start = mt7921_start_sched_scan,
1810 	.sched_scan_stop = mt7921_stop_sched_scan,
1811 	CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
1812 	CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
1813 #ifdef CONFIG_PM
1814 	.suspend = mt7921_suspend,
1815 	.resume = mt7921_resume,
1816 	.set_wakeup = mt7921_set_wakeup,
1817 	.set_rekey_data = mt7921_set_rekey_data,
1818 #endif /* CONFIG_PM */
1819 	.flush = mt7921_flush,
1820 	.set_sar_specs = mt7921_set_sar_specs,
1821 	.remain_on_channel = mt7921_remain_on_channel,
1822 	.cancel_remain_on_channel = mt7921_cancel_remain_on_channel,
1823 	.add_chanctx = mt7921_add_chanctx,
1824 	.remove_chanctx = mt7921_remove_chanctx,
1825 	.change_chanctx = mt7921_change_chanctx,
1826 	.assign_vif_chanctx = mt7921_assign_vif_chanctx,
1827 	.unassign_vif_chanctx = mt7921_unassign_vif_chanctx,
1828 	.mgd_prepare_tx = mt7921_mgd_prepare_tx,
1829 	.mgd_complete_tx = mt7921_mgd_complete_tx,
1830 };
1831 EXPORT_SYMBOL_GPL(mt7921_ops);
1832 
1833 MODULE_LICENSE("Dual BSD/GPL");
1834 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1835