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
mt7921_gen_ppe_thresh(u8 * he_ppet,int nss)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
mt7921_init_he_caps(struct mt7921_phy * phy,enum nl80211_band band,struct ieee80211_sband_iftype_data * data)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 
mt7921_set_stream_he_caps(struct mt7921_phy * phy)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 
__mt7921_start(struct mt7921_phy * phy)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 
mt7921_start(struct ieee80211_hw * hw)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 
mt7921_stop(struct ieee80211_hw * hw)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 
mt7921_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)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.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 
mt7921_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)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 
mt7921_set_channel(struct mt7921_phy * phy)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 
mt7921_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)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
mt7921_pm_interface_iter(void * priv,u8 * mac,struct ieee80211_vif * vif)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
mt7921_sniffer_interface_iter(void * priv,u8 * mac,struct ieee80211_vif * vif)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 
mt7921_set_runtime_pm(struct mt7921_dev * dev)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 
mt7921_config(struct ieee80211_hw * hw,u32 changed)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
mt7921_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u16 queue,const struct ieee80211_tx_queue_params * params)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 
mt7921_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)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 
mt7921_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u32 changed)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 |
657 		       BSS_CHANGED_BEACON_ENABLED))
658 		mt7921_mcu_uni_add_beacon_offload(dev, hw, vif,
659 						  info->enable_beacon);
660 
661 	/* ensure that enable txcmd_mode after bss_info */
662 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
663 		mt7921_mcu_set_tx(dev, vif);
664 
665 	if (changed & BSS_CHANGED_PS)
666 		mt7921_mcu_uni_bss_ps(dev, vif);
667 
668 	if (changed & BSS_CHANGED_ASSOC) {
669 		mt7921_mcu_sta_update(dev, NULL, vif, true,
670 				      MT76_STA_INFO_STATE_ASSOC);
671 		if (dev->pm.enable)
672 			mt7921_mcu_set_beacon_filter(dev, vif, info->assoc);
673 	}
674 
675 	if (changed & BSS_CHANGED_ARP_FILTER) {
676 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
677 
678 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
679 						  info);
680 	}
681 
682 	mt7921_mutex_release(dev);
683 }
684 
mt7921_mac_sta_add(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)685 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
686 		       struct ieee80211_sta *sta)
687 {
688 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
689 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
690 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
691 	int ret, idx;
692 
693 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
694 	if (idx < 0)
695 		return -ENOSPC;
696 
697 	INIT_LIST_HEAD(&msta->poll_list);
698 	msta->vif = mvif;
699 	msta->wcid.sta = 1;
700 	msta->wcid.idx = idx;
701 	msta->wcid.ext_phy = mvif->mt76.band_idx;
702 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
703 	msta->last_txs = jiffies;
704 
705 	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
706 	if (ret)
707 		return ret;
708 
709 	if (vif->type == NL80211_IFTYPE_STATION)
710 		mvif->wep_sta = msta;
711 
712 	mt7921_mac_wtbl_update(dev, idx,
713 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
714 
715 	ret = mt7921_mcu_sta_update(dev, sta, vif, true,
716 				    MT76_STA_INFO_STATE_NONE);
717 	if (ret)
718 		return ret;
719 
720 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
721 
722 	return 0;
723 }
724 EXPORT_SYMBOL_GPL(mt7921_mac_sta_add);
725 
mt7921_mac_sta_assoc(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)726 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
727 			  struct ieee80211_sta *sta)
728 {
729 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
730 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
731 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
732 
733 	mt7921_mutex_acquire(dev);
734 
735 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
736 		mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
737 					    true);
738 
739 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
740 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
741 
742 	mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
743 
744 	mt7921_mutex_release(dev);
745 }
746 EXPORT_SYMBOL_GPL(mt7921_mac_sta_assoc);
747 
mt7921_mac_sta_remove(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)748 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
749 			   struct ieee80211_sta *sta)
750 {
751 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
752 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
753 
754 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
755 	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
756 
757 	mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
758 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
759 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
760 
761 	if (vif->type == NL80211_IFTYPE_STATION) {
762 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
763 
764 		mvif->wep_sta = NULL;
765 		ewma_rssi_init(&mvif->rssi);
766 		if (!sta->tdls)
767 			mt76_connac_mcu_uni_add_bss(&dev->mphy, vif,
768 						    &mvif->sta.wcid, false);
769 	}
770 
771 	spin_lock_bh(&dev->sta_poll_lock);
772 	if (!list_empty(&msta->poll_list))
773 		list_del_init(&msta->poll_list);
774 	spin_unlock_bh(&dev->sta_poll_lock);
775 
776 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
777 }
778 EXPORT_SYMBOL_GPL(mt7921_mac_sta_remove);
779 
mt7921_tx_worker(struct mt76_worker * w)780 void mt7921_tx_worker(struct mt76_worker *w)
781 {
782 	struct mt7921_dev *dev = container_of(w, struct mt7921_dev,
783 					      mt76.tx_worker);
784 
785 	if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
786 		queue_work(dev->mt76.wq, &dev->pm.wake_work);
787 		return;
788 	}
789 
790 	mt76_txq_schedule_all(&dev->mphy);
791 	mt76_connac_pm_unref(&dev->mphy, &dev->pm);
792 }
793 
mt7921_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)794 static void mt7921_tx(struct ieee80211_hw *hw,
795 		      struct ieee80211_tx_control *control,
796 		      struct sk_buff *skb)
797 {
798 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
799 	struct mt76_phy *mphy = hw->priv;
800 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
801 	struct ieee80211_vif *vif = info->control.vif;
802 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
803 	int qid;
804 
805 	if (control->sta) {
806 		struct mt7921_sta *sta;
807 
808 		sta = (struct mt7921_sta *)control->sta->drv_priv;
809 		wcid = &sta->wcid;
810 	}
811 
812 	if (vif && !control->sta) {
813 		struct mt7921_vif *mvif;
814 
815 		mvif = (struct mt7921_vif *)vif->drv_priv;
816 		wcid = &mvif->sta.wcid;
817 	}
818 
819 	if (mt76_connac_pm_ref(mphy, &dev->pm)) {
820 		mt76_tx(mphy, control->sta, wcid, skb);
821 		mt76_connac_pm_unref(mphy, &dev->pm);
822 		return;
823 	}
824 
825 	qid = skb_get_queue_mapping(skb);
826 	if (qid >= MT_TXQ_PSD) {
827 		qid = IEEE80211_AC_BE;
828 		skb_set_queue_mapping(skb, qid);
829 	}
830 
831 	mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
832 }
833 
mt7921_set_rts_threshold(struct ieee80211_hw * hw,u32 val)834 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
835 {
836 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
837 
838 	mt7921_mutex_acquire(dev);
839 	mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
840 	mt7921_mutex_release(dev);
841 
842 	return 0;
843 }
844 
845 static int
mt7921_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)846 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
847 		    struct ieee80211_ampdu_params *params)
848 {
849 	enum ieee80211_ampdu_mlme_action action = params->action;
850 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
851 	struct ieee80211_sta *sta = params->sta;
852 	struct ieee80211_txq *txq = sta->txq[params->tid];
853 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
854 	u16 tid = params->tid;
855 	u16 ssn = params->ssn;
856 	struct mt76_txq *mtxq;
857 	int ret = 0;
858 
859 	if (!txq)
860 		return -EINVAL;
861 
862 	mtxq = (struct mt76_txq *)txq->drv_priv;
863 
864 	mt7921_mutex_acquire(dev);
865 	switch (action) {
866 	case IEEE80211_AMPDU_RX_START:
867 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
868 				   params->buf_size);
869 		mt7921_mcu_uni_rx_ba(dev, params, true);
870 		break;
871 	case IEEE80211_AMPDU_RX_STOP:
872 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
873 		mt7921_mcu_uni_rx_ba(dev, params, false);
874 		break;
875 	case IEEE80211_AMPDU_TX_OPERATIONAL:
876 		mtxq->aggr = true;
877 		mtxq->send_bar = false;
878 		mt7921_mcu_uni_tx_ba(dev, params, true);
879 		break;
880 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
881 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
882 		mtxq->aggr = false;
883 		clear_bit(tid, &msta->ampdu_state);
884 		mt7921_mcu_uni_tx_ba(dev, params, false);
885 		break;
886 	case IEEE80211_AMPDU_TX_START:
887 		set_bit(tid, &msta->ampdu_state);
888 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
889 		break;
890 	case IEEE80211_AMPDU_TX_STOP_CONT:
891 		mtxq->aggr = false;
892 		clear_bit(tid, &msta->ampdu_state);
893 		mt7921_mcu_uni_tx_ba(dev, params, false);
894 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
895 		break;
896 	}
897 	mt7921_mutex_release(dev);
898 
899 	return ret;
900 }
901 
mt7921_sta_state(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,enum ieee80211_sta_state old_state,enum ieee80211_sta_state new_state)902 static int mt7921_sta_state(struct ieee80211_hw *hw,
903 			    struct ieee80211_vif *vif,
904 			    struct ieee80211_sta *sta,
905 			    enum ieee80211_sta_state old_state,
906 			    enum ieee80211_sta_state new_state)
907 {
908 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
909 
910 	if (dev->pm.ds_enable) {
911 		mt7921_mutex_acquire(dev);
912 		mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state);
913 		mt7921_mutex_release(dev);
914 	}
915 
916 	return mt76_sta_state(hw, vif, sta, old_state, new_state);
917 }
918 
919 static int
mt7921_get_stats(struct ieee80211_hw * hw,struct ieee80211_low_level_stats * stats)920 mt7921_get_stats(struct ieee80211_hw *hw,
921 		 struct ieee80211_low_level_stats *stats)
922 {
923 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
924 	struct mib_stats *mib = &phy->mib;
925 
926 	mt7921_mutex_acquire(phy->dev);
927 
928 	stats->dot11RTSSuccessCount = mib->rts_cnt;
929 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
930 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
931 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
932 
933 	mt7921_mutex_release(phy->dev);
934 
935 	return 0;
936 }
937 
938 static const char mt7921_gstrings_stats[][ETH_GSTRING_LEN] = {
939 	/* tx counters */
940 	"tx_ampdu_cnt",
941 	"tx_mpdu_attempts",
942 	"tx_mpdu_success",
943 	"tx_pkt_ebf_cnt",
944 	"tx_pkt_ibf_cnt",
945 	"tx_ampdu_len:0-1",
946 	"tx_ampdu_len:2-10",
947 	"tx_ampdu_len:11-19",
948 	"tx_ampdu_len:20-28",
949 	"tx_ampdu_len:29-37",
950 	"tx_ampdu_len:38-46",
951 	"tx_ampdu_len:47-55",
952 	"tx_ampdu_len:56-79",
953 	"tx_ampdu_len:80-103",
954 	"tx_ampdu_len:104-127",
955 	"tx_ampdu_len:128-151",
956 	"tx_ampdu_len:152-175",
957 	"tx_ampdu_len:176-199",
958 	"tx_ampdu_len:200-223",
959 	"tx_ampdu_len:224-247",
960 	"ba_miss_count",
961 	"tx_beamformer_ppdu_iBF",
962 	"tx_beamformer_ppdu_eBF",
963 	"tx_beamformer_rx_feedback_all",
964 	"tx_beamformer_rx_feedback_he",
965 	"tx_beamformer_rx_feedback_vht",
966 	"tx_beamformer_rx_feedback_ht",
967 	"tx_msdu_pack_1",
968 	"tx_msdu_pack_2",
969 	"tx_msdu_pack_3",
970 	"tx_msdu_pack_4",
971 	"tx_msdu_pack_5",
972 	"tx_msdu_pack_6",
973 	"tx_msdu_pack_7",
974 	"tx_msdu_pack_8",
975 	/* rx counters */
976 	"rx_mpdu_cnt",
977 	"rx_ampdu_cnt",
978 	"rx_ampdu_bytes_cnt",
979 	"rx_ba_cnt",
980 	/* per vif counters */
981 	"v_tx_mode_cck",
982 	"v_tx_mode_ofdm",
983 	"v_tx_mode_ht",
984 	"v_tx_mode_ht_gf",
985 	"v_tx_mode_vht",
986 	"v_tx_mode_he_su",
987 	"v_tx_mode_he_ext_su",
988 	"v_tx_mode_he_tb",
989 	"v_tx_mode_he_mu",
990 	"v_tx_bw_20",
991 	"v_tx_bw_40",
992 	"v_tx_bw_80",
993 	"v_tx_bw_160",
994 	"v_tx_mcs_0",
995 	"v_tx_mcs_1",
996 	"v_tx_mcs_2",
997 	"v_tx_mcs_3",
998 	"v_tx_mcs_4",
999 	"v_tx_mcs_5",
1000 	"v_tx_mcs_6",
1001 	"v_tx_mcs_7",
1002 	"v_tx_mcs_8",
1003 	"v_tx_mcs_9",
1004 	"v_tx_mcs_10",
1005 	"v_tx_mcs_11",
1006 };
1007 
1008 static void
mt7921_get_et_strings(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 sset,u8 * data)1009 mt7921_get_et_strings(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1010 		      u32 sset, u8 *data)
1011 {
1012 	if (sset != ETH_SS_STATS)
1013 		return;
1014 
1015 	memcpy(data, *mt7921_gstrings_stats, sizeof(mt7921_gstrings_stats));
1016 }
1017 
1018 static int
mt7921_get_et_sset_count(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int sset)1019 mt7921_get_et_sset_count(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1020 			 int sset)
1021 {
1022 	return sset == ETH_SS_STATS ? ARRAY_SIZE(mt7921_gstrings_stats) : 0;
1023 }
1024 
1025 static void
mt7921_ethtool_worker(void * wi_data,struct ieee80211_sta * sta)1026 mt7921_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1027 {
1028 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1029 	struct mt76_ethtool_worker_info *wi = wi_data;
1030 
1031 	if (msta->vif->mt76.idx != wi->idx)
1032 		return;
1033 
1034 	mt76_ethtool_worker(wi, &msta->stats);
1035 }
1036 
1037 static
mt7921_get_et_stats(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ethtool_stats * stats,u64 * data)1038 void mt7921_get_et_stats(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1039 			 struct ethtool_stats *stats, u64 *data)
1040 {
1041 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1042 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1043 	struct mt7921_dev *dev = phy->dev;
1044 	struct mib_stats *mib = &phy->mib;
1045 	struct mt76_ethtool_worker_info wi = {
1046 		.data = data,
1047 		.idx = mvif->mt76.idx,
1048 	};
1049 	int i, ei = 0;
1050 
1051 	mt7921_mutex_acquire(dev);
1052 
1053 	mt7921_mac_update_mib_stats(phy);
1054 
1055 	data[ei++] = mib->tx_ampdu_cnt;
1056 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1057 	data[ei++] = mib->tx_mpdu_success_cnt;
1058 	data[ei++] = mib->tx_pkt_ebf_cnt;
1059 	data[ei++] = mib->tx_pkt_ibf_cnt;
1060 
1061 	/* Tx ampdu stat */
1062 	for (i = 0; i < 15; i++)
1063 		data[ei++] = dev->mt76.aggr_stats[i];
1064 
1065 	data[ei++] = phy->mib.ba_miss_cnt;
1066 
1067 	/* Tx Beamformer monitor */
1068 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1069 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1070 
1071 	/* Tx Beamformer Rx feedback monitor */
1072 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1073 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1074 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1075 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1076 
1077 	/* Tx amsdu info (pack-count histogram) */
1078 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1079 		data[ei++] = mib->tx_amsdu[i];
1080 
1081 	/* rx counters */
1082 	data[ei++] = mib->rx_mpdu_cnt;
1083 	data[ei++] = mib->rx_ampdu_cnt;
1084 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1085 	data[ei++] = mib->rx_ba_cnt;
1086 
1087 	/* Add values for all stations owned by this vif */
1088 	wi.initial_stat_idx = ei;
1089 	ieee80211_iterate_stations_atomic(hw, mt7921_ethtool_worker, &wi);
1090 
1091 	mt7921_mutex_release(dev);
1092 
1093 	if (!wi.sta_count)
1094 		return;
1095 
1096 	ei += wi.worker_stat_count;
1097 	if (ei != ARRAY_SIZE(mt7921_gstrings_stats))
1098 		dev_err(dev->mt76.dev, "ei: %d  SSTATS_LEN: %zu",
1099 			ei, ARRAY_SIZE(mt7921_gstrings_stats));
1100 }
1101 
1102 static u64
mt7921_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1103 mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1104 {
1105 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1106 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1107 	u8 omac_idx = mvif->mt76.omac_idx;
1108 	union {
1109 		u64 t64;
1110 		u32 t32[2];
1111 	} tsf;
1112 	u16 n;
1113 
1114 	mt7921_mutex_acquire(dev);
1115 
1116 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1117 	/* TSF software read */
1118 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_MODE);
1119 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(0));
1120 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(0));
1121 
1122 	mt7921_mutex_release(dev);
1123 
1124 	return tsf.t64;
1125 }
1126 
1127 static void
mt7921_set_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 timestamp)1128 mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1129 	       u64 timestamp)
1130 {
1131 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1132 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1133 	u8 omac_idx = mvif->mt76.omac_idx;
1134 	union {
1135 		u64 t64;
1136 		u32 t32[2];
1137 	} tsf = { .t64 = timestamp, };
1138 	u16 n;
1139 
1140 	mt7921_mutex_acquire(dev);
1141 
1142 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1143 	mt76_wr(dev, MT_LPON_UTTR0(0), tsf.t32[0]);
1144 	mt76_wr(dev, MT_LPON_UTTR1(0), tsf.t32[1]);
1145 	/* TSF software overwrite */
1146 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_WRITE);
1147 
1148 	mt7921_mutex_release(dev);
1149 }
1150 
1151 static void
mt7921_set_coverage_class(struct ieee80211_hw * hw,s16 coverage_class)1152 mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
1153 {
1154 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1155 	struct mt7921_dev *dev = phy->dev;
1156 
1157 	mt7921_mutex_acquire(dev);
1158 	phy->coverage_class = max_t(s16, coverage_class, 0);
1159 	mt7921_mac_set_timing(phy);
1160 	mt7921_mutex_release(dev);
1161 }
1162 
mt7921_scan_work(struct work_struct * work)1163 void mt7921_scan_work(struct work_struct *work)
1164 {
1165 	struct mt7921_phy *phy;
1166 
1167 	phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy,
1168 						scan_work.work);
1169 
1170 	while (true) {
1171 		struct mt7921_mcu_rxd *rxd;
1172 		struct sk_buff *skb;
1173 
1174 		spin_lock_bh(&phy->dev->mt76.lock);
1175 		skb = __skb_dequeue(&phy->scan_event_list);
1176 		spin_unlock_bh(&phy->dev->mt76.lock);
1177 
1178 		if (!skb)
1179 			break;
1180 
1181 		rxd = (struct mt7921_mcu_rxd *)skb->data;
1182 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1183 			ieee80211_sched_scan_results(phy->mt76->hw);
1184 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
1185 					      &phy->mt76->state)) {
1186 			struct cfg80211_scan_info info = {
1187 				.aborted = false,
1188 			};
1189 
1190 			ieee80211_scan_completed(phy->mt76->hw, &info);
1191 		}
1192 		dev_kfree_skb(skb);
1193 	}
1194 }
1195 
1196 static int
mt7921_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * req)1197 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1198 	       struct ieee80211_scan_request *req)
1199 {
1200 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1201 	struct mt76_phy *mphy = hw->priv;
1202 	int err;
1203 
1204 	mt7921_mutex_acquire(dev);
1205 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1206 	mt7921_mutex_release(dev);
1207 
1208 	return err;
1209 }
1210 
1211 static void
mt7921_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1212 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1213 {
1214 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1215 	struct mt76_phy *mphy = hw->priv;
1216 
1217 	mt7921_mutex_acquire(dev);
1218 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1219 	mt7921_mutex_release(dev);
1220 }
1221 
1222 static int
mt7921_start_sched_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_sched_scan_request * req,struct ieee80211_scan_ies * ies)1223 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1224 			struct cfg80211_sched_scan_request *req,
1225 			struct ieee80211_scan_ies *ies)
1226 {
1227 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1228 	struct mt76_phy *mphy = hw->priv;
1229 	int err;
1230 
1231 	mt7921_mutex_acquire(dev);
1232 
1233 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1234 	if (err < 0)
1235 		goto out;
1236 
1237 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1238 out:
1239 	mt7921_mutex_release(dev);
1240 
1241 	return err;
1242 }
1243 
1244 static int
mt7921_stop_sched_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1245 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1246 {
1247 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1248 	struct mt76_phy *mphy = hw->priv;
1249 	int err;
1250 
1251 	mt7921_mutex_acquire(dev);
1252 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1253 	mt7921_mutex_release(dev);
1254 
1255 	return err;
1256 }
1257 
1258 static int
mt7921_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)1259 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1260 {
1261 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1262 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1263 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1264 
1265 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1266 		return -EINVAL;
1267 
1268 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1269 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1270 
1271 	mt7921_mutex_acquire(dev);
1272 
1273 	phy->mt76->antenna_mask = tx_ant;
1274 	phy->mt76->chainmask = tx_ant;
1275 
1276 	mt76_set_stream_caps(phy->mt76, true);
1277 	mt7921_set_stream_he_caps(phy);
1278 
1279 	mt7921_mutex_release(dev);
1280 
1281 	return 0;
1282 }
1283 
mt7921_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)1284 static void mt7921_sta_statistics(struct ieee80211_hw *hw,
1285 				  struct ieee80211_vif *vif,
1286 				  struct ieee80211_sta *sta,
1287 				  struct station_info *sinfo)
1288 {
1289 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1290 	struct rate_info *txrate = &msta->wcid.rate;
1291 
1292 	if (!txrate->legacy && !txrate->flags)
1293 		return;
1294 
1295 	if (txrate->legacy) {
1296 		sinfo->txrate.legacy = txrate->legacy;
1297 	} else {
1298 		sinfo->txrate.mcs = txrate->mcs;
1299 		sinfo->txrate.nss = txrate->nss;
1300 		sinfo->txrate.bw = txrate->bw;
1301 		sinfo->txrate.he_gi = txrate->he_gi;
1302 		sinfo->txrate.he_dcm = txrate->he_dcm;
1303 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1304 	}
1305 	sinfo->txrate.flags = txrate->flags;
1306 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1307 }
1308 
1309 #ifdef CONFIG_PM
mt7921_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)1310 static int mt7921_suspend(struct ieee80211_hw *hw,
1311 			  struct cfg80211_wowlan *wowlan)
1312 {
1313 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1314 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1315 
1316 	cancel_delayed_work_sync(&phy->scan_work);
1317 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1318 
1319 	cancel_delayed_work_sync(&dev->pm.ps_work);
1320 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1321 
1322 	mt7921_mutex_acquire(dev);
1323 
1324 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1325 	ieee80211_iterate_active_interfaces(hw,
1326 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1327 					    mt7921_mcu_set_suspend_iter,
1328 					    &dev->mphy);
1329 
1330 	mt7921_mutex_release(dev);
1331 
1332 	return 0;
1333 }
1334 
mt7921_resume(struct ieee80211_hw * hw)1335 static int mt7921_resume(struct ieee80211_hw *hw)
1336 {
1337 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1338 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1339 
1340 	mt7921_mutex_acquire(dev);
1341 
1342 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1343 	ieee80211_iterate_active_interfaces(hw,
1344 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1345 					    mt76_connac_mcu_set_suspend_iter,
1346 					    &dev->mphy);
1347 
1348 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1349 				     MT7921_WATCHDOG_TIME);
1350 
1351 	mt7921_mutex_release(dev);
1352 
1353 	return 0;
1354 }
1355 
mt7921_set_wakeup(struct ieee80211_hw * hw,bool enabled)1356 static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1357 {
1358 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1359 	struct mt76_dev *mdev = &dev->mt76;
1360 
1361 	device_set_wakeup_enable(mdev->dev, enabled);
1362 }
1363 
mt7921_set_rekey_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_gtk_rekey_data * data)1364 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1365 				  struct ieee80211_vif *vif,
1366 				  struct cfg80211_gtk_rekey_data *data)
1367 {
1368 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1369 
1370 	mt7921_mutex_acquire(dev);
1371 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1372 	mt7921_mutex_release(dev);
1373 }
1374 #endif /* CONFIG_PM */
1375 
mt7921_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)1376 static void mt7921_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1377 			 u32 queues, bool drop)
1378 {
1379 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1380 
1381 	wait_event_timeout(dev->mt76.tx_wait, !mt76_has_tx_pending(&dev->mphy),
1382 			   HZ / 2);
1383 }
1384 
mt7921_sta_set_decap_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1385 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1386 					 struct ieee80211_vif *vif,
1387 					 struct ieee80211_sta *sta,
1388 					 bool enabled)
1389 {
1390 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1391 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1392 
1393 	if (enabled)
1394 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1395 	else
1396 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1397 
1398 	mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->wcid,
1399 					     MCU_UNI_CMD(STA_REC_UPDATE));
1400 }
1401 
1402 #if IS_ENABLED(CONFIG_IPV6)
mt7921_ipv6_addr_change(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct inet6_dev * idev)1403 static void mt7921_ipv6_addr_change(struct ieee80211_hw *hw,
1404 				    struct ieee80211_vif *vif,
1405 				    struct inet6_dev *idev)
1406 {
1407 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1408 	struct mt7921_dev *dev = mvif->phy->dev;
1409 	struct inet6_ifaddr *ifa;
1410 	struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN];
1411 	struct sk_buff *skb;
1412 	u8 i, idx = 0;
1413 
1414 	struct {
1415 		struct {
1416 			u8 bss_idx;
1417 			u8 pad[3];
1418 		} __packed hdr;
1419 		struct mt76_connac_arpns_tlv arpns;
1420 	} req_hdr = {
1421 		.hdr = {
1422 			.bss_idx = mvif->mt76.idx,
1423 		},
1424 		.arpns = {
1425 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
1426 			.mode = 2,  /* update */
1427 			.option = 1, /* update only */
1428 		},
1429 	};
1430 
1431 	read_lock_bh(&idev->lock);
1432 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
1433 		if (ifa->flags & IFA_F_TENTATIVE)
1434 			continue;
1435 		ns_addrs[idx] = ifa->addr;
1436 		if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN)
1437 			break;
1438 	}
1439 	read_unlock_bh(&idev->lock);
1440 
1441 	if (!idx)
1442 		return;
1443 
1444 	skb = __mt76_mcu_msg_alloc(&dev->mt76, NULL, sizeof(req_hdr) +
1445 				   idx * sizeof(struct in6_addr), GFP_ATOMIC);
1446 	if (!skb)
1447 		return;
1448 
1449 	req_hdr.arpns.ips_num = idx;
1450 	req_hdr.arpns.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)
1451 					+ idx * sizeof(struct in6_addr));
1452 	skb_put_data(skb, &req_hdr, sizeof(req_hdr));
1453 
1454 	for (i = 0; i < idx; i++)
1455 		skb_put_data(skb, &ns_addrs[i].in6_u, sizeof(struct in6_addr));
1456 
1457 	skb_queue_tail(&dev->ipv6_ns_list, skb);
1458 
1459 	ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work);
1460 }
1461 #endif
1462 
mt7921_set_sar_specs(struct ieee80211_hw * hw,const struct cfg80211_sar_specs * sar)1463 static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
1464 				const struct cfg80211_sar_specs *sar)
1465 {
1466 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1467 	struct mt76_phy *mphy = hw->priv;
1468 	int err;
1469 
1470 	mt7921_mutex_acquire(dev);
1471 	err = mt76_init_sar_power(hw, sar);
1472 	if (err)
1473 		goto out;
1474 
1475 	err = mt76_connac_mcu_set_rate_txpower(mphy);
1476 out:
1477 	mt7921_mutex_release(dev);
1478 
1479 	return err;
1480 }
1481 
1482 static void
mt7921_channel_switch_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_chan_def * chandef)1483 mt7921_channel_switch_beacon(struct ieee80211_hw *hw,
1484 			     struct ieee80211_vif *vif,
1485 			     struct cfg80211_chan_def *chandef)
1486 {
1487 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1488 
1489 	mt7921_mutex_acquire(dev);
1490 	mt7921_mcu_uni_add_beacon_offload(dev, hw, vif, true);
1491 	mt7921_mutex_release(dev);
1492 }
1493 
1494 static int
mt7921_start_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1495 mt7921_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1496 {
1497 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1498 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1499 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1500 	int err;
1501 
1502 	err = mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid,
1503 					  true);
1504 	if (err)
1505 		return err;
1506 
1507 	err = mt7921_mcu_set_bss_pm(dev, vif, true);
1508 	if (err)
1509 		return err;
1510 
1511 	return mt7921_mcu_sta_update(dev, NULL, vif, true,
1512 				     MT76_STA_INFO_STATE_NONE);
1513 }
1514 
1515 static void
mt7921_stop_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1516 mt7921_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1517 {
1518 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1519 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1520 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1521 	int err;
1522 
1523 	err = mt7921_mcu_set_bss_pm(dev, vif, false);
1524 	if (err)
1525 		return;
1526 
1527 	mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid, false);
1528 }
1529 
1530 const struct ieee80211_ops mt7921_ops = {
1531 	.tx = mt7921_tx,
1532 	.start = mt7921_start,
1533 	.stop = mt7921_stop,
1534 	.add_interface = mt7921_add_interface,
1535 	.remove_interface = mt7921_remove_interface,
1536 	.config = mt7921_config,
1537 	.conf_tx = mt7921_conf_tx,
1538 	.configure_filter = mt7921_configure_filter,
1539 	.bss_info_changed = mt7921_bss_info_changed,
1540 	.start_ap = mt7921_start_ap,
1541 	.stop_ap = mt7921_stop_ap,
1542 	.sta_state = mt7921_sta_state,
1543 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1544 	.set_key = mt7921_set_key,
1545 	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1546 #if IS_ENABLED(CONFIG_IPV6)
1547 	.ipv6_addr_change = mt7921_ipv6_addr_change,
1548 #endif /* CONFIG_IPV6 */
1549 	.ampdu_action = mt7921_ampdu_action,
1550 	.set_rts_threshold = mt7921_set_rts_threshold,
1551 	.wake_tx_queue = mt76_wake_tx_queue,
1552 	.release_buffered_frames = mt76_release_buffered_frames,
1553 	.channel_switch_beacon = mt7921_channel_switch_beacon,
1554 	.get_txpower = mt76_get_txpower,
1555 	.get_stats = mt7921_get_stats,
1556 	.get_et_sset_count = mt7921_get_et_sset_count,
1557 	.get_et_strings = mt7921_get_et_strings,
1558 	.get_et_stats = mt7921_get_et_stats,
1559 	.get_tsf = mt7921_get_tsf,
1560 	.set_tsf = mt7921_set_tsf,
1561 	.get_survey = mt76_get_survey,
1562 	.get_antenna = mt76_get_antenna,
1563 	.set_antenna = mt7921_set_antenna,
1564 	.set_coverage_class = mt7921_set_coverage_class,
1565 	.hw_scan = mt7921_hw_scan,
1566 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1567 	.sta_statistics = mt7921_sta_statistics,
1568 	.sched_scan_start = mt7921_start_sched_scan,
1569 	.sched_scan_stop = mt7921_stop_sched_scan,
1570 	CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
1571 	CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
1572 #ifdef CONFIG_PM
1573 	.suspend = mt7921_suspend,
1574 	.resume = mt7921_resume,
1575 	.set_wakeup = mt7921_set_wakeup,
1576 	.set_rekey_data = mt7921_set_rekey_data,
1577 #endif /* CONFIG_PM */
1578 	.flush = mt7921_flush,
1579 	.set_sar_specs = mt7921_set_sar_specs,
1580 };
1581 EXPORT_SYMBOL_GPL(mt7921_ops);
1582 
1583 MODULE_LICENSE("Dual BSD/GPL");
1584 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1585