xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/main.c (revision ea518afc992032f7570c0a89ac9240b387dc0faf)
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 int
13 mt7921_init_he_caps(struct mt792x_phy *phy, enum nl80211_band band,
14 		    struct ieee80211_sband_iftype_data *data)
15 {
16 	int i, idx = 0;
17 	int nss = hweight8(phy->mt76->chainmask);
18 	u16 mcs_map = 0;
19 
20 	for (i = 0; i < 8; i++) {
21 		if (i < nss)
22 			mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2));
23 		else
24 			mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2));
25 	}
26 
27 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
28 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
29 		struct ieee80211_he_cap_elem *he_cap_elem =
30 				&he_cap->he_cap_elem;
31 		struct ieee80211_he_mcs_nss_supp *he_mcs =
32 				&he_cap->he_mcs_nss_supp;
33 
34 		switch (i) {
35 		case NL80211_IFTYPE_STATION:
36 		case NL80211_IFTYPE_AP:
37 			break;
38 		default:
39 			continue;
40 		}
41 
42 		data[idx].types_mask = BIT(i);
43 		he_cap->has_he = true;
44 
45 		he_cap_elem->mac_cap_info[0] =
46 			IEEE80211_HE_MAC_CAP0_HTC_HE;
47 		he_cap_elem->mac_cap_info[3] =
48 			IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
49 			IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3;
50 		he_cap_elem->mac_cap_info[4] =
51 			IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
52 
53 		if (band == NL80211_BAND_2GHZ)
54 			he_cap_elem->phy_cap_info[0] =
55 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
56 		else
57 			he_cap_elem->phy_cap_info[0] =
58 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G;
59 
60 		he_cap_elem->phy_cap_info[1] =
61 			IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD;
62 		he_cap_elem->phy_cap_info[2] =
63 			IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
64 			IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
65 			IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
66 			IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
67 			IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
68 
69 		switch (i) {
70 		case NL80211_IFTYPE_AP:
71 			he_cap_elem->mac_cap_info[2] |=
72 				IEEE80211_HE_MAC_CAP2_BSR;
73 			he_cap_elem->mac_cap_info[4] |=
74 				IEEE80211_HE_MAC_CAP4_BQR;
75 			he_cap_elem->mac_cap_info[5] |=
76 				IEEE80211_HE_MAC_CAP5_OM_CTRL_UL_MU_DATA_DIS_RX;
77 			he_cap_elem->phy_cap_info[3] |=
78 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
79 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
80 			he_cap_elem->phy_cap_info[6] |=
81 				IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
82 				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
83 			he_cap_elem->phy_cap_info[9] |=
84 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
85 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
86 			break;
87 		case NL80211_IFTYPE_STATION:
88 			he_cap_elem->mac_cap_info[1] |=
89 				IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
90 
91 			if (band == NL80211_BAND_2GHZ)
92 				he_cap_elem->phy_cap_info[0] |=
93 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G;
94 			else
95 				he_cap_elem->phy_cap_info[0] |=
96 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G;
97 
98 			he_cap_elem->phy_cap_info[1] |=
99 				IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
100 				IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
101 			he_cap_elem->phy_cap_info[3] |=
102 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
103 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
104 			he_cap_elem->phy_cap_info[4] |=
105 				IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE |
106 				IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4;
107 			he_cap_elem->phy_cap_info[5] |=
108 				IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK |
109 				IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
110 			he_cap_elem->phy_cap_info[6] |=
111 				IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU |
112 				IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
113 				IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
114 				IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
115 				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
116 			he_cap_elem->phy_cap_info[7] |=
117 				IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
118 				IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI;
119 			he_cap_elem->phy_cap_info[8] |=
120 				IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
121 				IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484;
122 			he_cap_elem->phy_cap_info[9] |=
123 				IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
124 				IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
125 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
126 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
127 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
128 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
129 
130 			if (is_mt7922(phy->mt76->dev)) {
131 				he_cap_elem->phy_cap_info[0] |=
132 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
133 				he_cap_elem->phy_cap_info[8] |=
134 					IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
135 					IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
136 			}
137 			break;
138 		}
139 
140 		he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map);
141 		he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map);
142 		if (is_mt7922(phy->mt76->dev)) {
143 			he_mcs->rx_mcs_160 = cpu_to_le16(mcs_map);
144 			he_mcs->tx_mcs_160 = cpu_to_le16(mcs_map);
145 		}
146 
147 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
148 		if (he_cap_elem->phy_cap_info[6] &
149 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
150 			mt76_connac_gen_ppe_thresh(he_cap->ppe_thres, nss);
151 		} else {
152 			he_cap_elem->phy_cap_info[9] |=
153 				u8_encode_bits(IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US,
154 					       IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
155 		}
156 
157 		if (band == NL80211_BAND_6GHZ) {
158 			struct ieee80211_supported_band *sband =
159 				&phy->mt76->sband_5g.sband;
160 			struct ieee80211_sta_vht_cap *vht_cap = &sband->vht_cap;
161 			struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
162 			u32 exp;
163 			u16 cap;
164 
165 			cap = u16_encode_bits(ht_cap->ampdu_density,
166 					IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
167 			exp = u32_get_bits(vht_cap->cap,
168 				IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
169 			cap |= u16_encode_bits(exp,
170 					IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
171 			exp = u32_get_bits(vht_cap->cap,
172 					   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
173 			cap |= u16_encode_bits(exp,
174 					IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
175 			if (vht_cap->cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
176 				cap |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
177 			if (vht_cap->cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
178 				cap |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
179 
180 			data[idx].he_6ghz_capa.capa = cpu_to_le16(cap);
181 		}
182 		idx++;
183 	}
184 
185 	return idx;
186 }
187 
188 void mt7921_set_stream_he_caps(struct mt792x_phy *phy)
189 {
190 	struct ieee80211_sband_iftype_data *data;
191 	struct ieee80211_supported_band *band;
192 	int n;
193 
194 	if (phy->mt76->cap.has_2ghz) {
195 		data = phy->iftype[NL80211_BAND_2GHZ];
196 		n = mt7921_init_he_caps(phy, NL80211_BAND_2GHZ, data);
197 
198 		band = &phy->mt76->sband_2g.sband;
199 		_ieee80211_set_sband_iftype_data(band, data, n);
200 	}
201 
202 	if (phy->mt76->cap.has_5ghz) {
203 		data = phy->iftype[NL80211_BAND_5GHZ];
204 		n = mt7921_init_he_caps(phy, NL80211_BAND_5GHZ, data);
205 
206 		band = &phy->mt76->sband_5g.sband;
207 		_ieee80211_set_sband_iftype_data(band, data, n);
208 
209 		if (phy->mt76->cap.has_6ghz) {
210 			data = phy->iftype[NL80211_BAND_6GHZ];
211 			n = mt7921_init_he_caps(phy, NL80211_BAND_6GHZ, data);
212 
213 			band = &phy->mt76->sband_6g.sband;
214 			_ieee80211_set_sband_iftype_data(band, data, n);
215 		}
216 	}
217 }
218 
219 int __mt7921_start(struct mt792x_phy *phy)
220 {
221 	struct mt76_phy *mphy = phy->mt76;
222 	int err;
223 
224 	err = mt76_connac_mcu_set_mac_enable(mphy->dev, 0, true, false);
225 	if (err)
226 		return err;
227 
228 	err = mt76_connac_mcu_set_channel_domain(mphy);
229 	if (err)
230 		return err;
231 
232 	err = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
233 	if (err)
234 		return err;
235 
236 	err = mt7921_set_tx_sar_pwr(mphy->hw, NULL);
237 	if (err)
238 		return err;
239 
240 	mt792x_mac_reset_counters(phy);
241 	set_bit(MT76_STATE_RUNNING, &mphy->state);
242 
243 	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
244 				     MT792x_WATCHDOG_TIME);
245 
246 	return 0;
247 }
248 EXPORT_SYMBOL_GPL(__mt7921_start);
249 
250 static int mt7921_start(struct ieee80211_hw *hw)
251 {
252 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
253 	int err;
254 
255 	mt792x_mutex_acquire(phy->dev);
256 	err = __mt7921_start(phy);
257 	mt792x_mutex_release(phy->dev);
258 
259 	return err;
260 }
261 
262 static int
263 mt7921_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
264 {
265 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
266 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
267 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
268 	struct mt76_txq *mtxq;
269 	int idx, ret = 0;
270 
271 	mt792x_mutex_acquire(dev);
272 
273 	mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
274 	if (mvif->mt76.idx >= MT792x_MAX_INTERFACES) {
275 		ret = -ENOSPC;
276 		goto out;
277 	}
278 
279 	mvif->mt76.omac_idx = mvif->mt76.idx;
280 	mvif->phy = phy;
281 	mvif->mt76.band_idx = 0;
282 	mvif->mt76.wmm_idx = mvif->mt76.idx % MT76_CONNAC_MAX_WMM_SETS;
283 
284 	ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid,
285 					  true);
286 	if (ret)
287 		goto out;
288 
289 	dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
290 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
291 
292 	idx = MT792x_WTBL_RESERVED - mvif->mt76.idx;
293 
294 	INIT_LIST_HEAD(&mvif->sta.wcid.poll_list);
295 	mvif->sta.wcid.idx = idx;
296 	mvif->sta.wcid.phy_idx = mvif->mt76.band_idx;
297 	mvif->sta.wcid.hw_key_idx = -1;
298 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
299 	mt76_wcid_init(&mvif->sta.wcid);
300 
301 	mt7921_mac_wtbl_update(dev, idx,
302 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
303 
304 	ewma_rssi_init(&mvif->rssi);
305 
306 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
307 	if (vif->txq) {
308 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
309 		mtxq->wcid = idx;
310 	}
311 
312 	vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
313 out:
314 	mt792x_mutex_release(dev);
315 
316 	return ret;
317 }
318 
319 static void mt7921_roc_iter(void *priv, u8 *mac,
320 			    struct ieee80211_vif *vif)
321 {
322 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
323 	struct mt792x_phy *phy = priv;
324 
325 	mt7921_mcu_abort_roc(phy, mvif, phy->roc_token_id);
326 }
327 
328 void mt7921_roc_work(struct work_struct *work)
329 {
330 	struct mt792x_phy *phy;
331 
332 	phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy,
333 						roc_work);
334 
335 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
336 		return;
337 
338 	mt792x_mutex_acquire(phy->dev);
339 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
340 					    IEEE80211_IFACE_ITER_RESUME_ALL,
341 					    mt7921_roc_iter, phy);
342 	mt792x_mutex_release(phy->dev);
343 	ieee80211_remain_on_channel_expired(phy->mt76->hw);
344 }
345 
346 static int mt7921_abort_roc(struct mt792x_phy *phy, struct mt792x_vif *vif)
347 {
348 	int err = 0;
349 
350 	del_timer_sync(&phy->roc_timer);
351 	cancel_work_sync(&phy->roc_work);
352 
353 	mt792x_mutex_acquire(phy->dev);
354 	if (test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
355 		err = mt7921_mcu_abort_roc(phy, vif, phy->roc_token_id);
356 	mt792x_mutex_release(phy->dev);
357 
358 	return err;
359 }
360 
361 static int mt7921_set_roc(struct mt792x_phy *phy,
362 			  struct mt792x_vif *vif,
363 			  struct ieee80211_channel *chan,
364 			  int duration,
365 			  enum mt7921_roc_req type)
366 {
367 	int err;
368 
369 	if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
370 		return -EBUSY;
371 
372 	phy->roc_grant = false;
373 
374 	err = mt7921_mcu_set_roc(phy, vif, chan, duration, type,
375 				 ++phy->roc_token_id);
376 	if (err < 0) {
377 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
378 		goto out;
379 	}
380 
381 	if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) {
382 		mt7921_mcu_abort_roc(phy, vif, phy->roc_token_id);
383 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
384 		err = -ETIMEDOUT;
385 	}
386 
387 out:
388 	return err;
389 }
390 
391 static int mt7921_remain_on_channel(struct ieee80211_hw *hw,
392 				    struct ieee80211_vif *vif,
393 				    struct ieee80211_channel *chan,
394 				    int duration,
395 				    enum ieee80211_roc_type type)
396 {
397 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
398 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
399 	int err;
400 
401 	mt792x_mutex_acquire(phy->dev);
402 	err = mt7921_set_roc(phy, mvif, chan, duration, MT7921_ROC_REQ_ROC);
403 	mt792x_mutex_release(phy->dev);
404 
405 	return err;
406 }
407 
408 static int mt7921_cancel_remain_on_channel(struct ieee80211_hw *hw,
409 					   struct ieee80211_vif *vif)
410 {
411 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
412 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
413 
414 	return mt7921_abort_roc(phy, mvif);
415 }
416 
417 static int mt7921_set_channel(struct mt792x_phy *phy)
418 {
419 	struct mt792x_dev *dev = phy->dev;
420 	int ret;
421 
422 	cancel_delayed_work_sync(&phy->mt76->mac_work);
423 
424 	mt792x_mutex_acquire(dev);
425 	set_bit(MT76_RESET, &phy->mt76->state);
426 
427 	mt76_set_channel(phy->mt76);
428 
429 	ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
430 	if (ret)
431 		goto out;
432 
433 	mt792x_mac_set_timeing(phy);
434 
435 	mt792x_mac_reset_counters(phy);
436 	phy->noise = 0;
437 
438 out:
439 	clear_bit(MT76_RESET, &phy->mt76->state);
440 	mt792x_mutex_release(dev);
441 
442 	mt76_worker_schedule(&dev->mt76.tx_worker);
443 	ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mt76->mac_work,
444 				     MT792x_WATCHDOG_TIME);
445 
446 	return ret;
447 }
448 
449 static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
450 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
451 			  struct ieee80211_key_conf *key)
452 {
453 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
454 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
455 	struct mt792x_sta *msta = sta ? (struct mt792x_sta *)sta->drv_priv :
456 				  &mvif->sta;
457 	struct mt76_wcid *wcid = &msta->wcid;
458 	u8 *wcid_keyidx = &wcid->hw_key_idx;
459 	int idx = key->keyidx, err = 0;
460 
461 	/* The hardware does not support per-STA RX GTK, fallback
462 	 * to software mode for these.
463 	 */
464 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
465 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
466 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
467 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
468 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
469 		return -EOPNOTSUPP;
470 
471 	/* fall back to sw encryption for unsupported ciphers */
472 	switch (key->cipher) {
473 	case WLAN_CIPHER_SUITE_AES_CMAC:
474 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
475 		wcid_keyidx = &wcid->hw_key_idx2;
476 		break;
477 	case WLAN_CIPHER_SUITE_WEP40:
478 	case WLAN_CIPHER_SUITE_WEP104:
479 		if (!mvif->wep_sta)
480 			return -EOPNOTSUPP;
481 		break;
482 	case WLAN_CIPHER_SUITE_TKIP:
483 	case WLAN_CIPHER_SUITE_CCMP:
484 	case WLAN_CIPHER_SUITE_CCMP_256:
485 	case WLAN_CIPHER_SUITE_GCMP:
486 	case WLAN_CIPHER_SUITE_GCMP_256:
487 	case WLAN_CIPHER_SUITE_SMS4:
488 		break;
489 	default:
490 		return -EOPNOTSUPP;
491 	}
492 
493 	mt792x_mutex_acquire(dev);
494 
495 	if (cmd == SET_KEY) {
496 		*wcid_keyidx = idx;
497 	} else {
498 		if (idx == *wcid_keyidx)
499 			*wcid_keyidx = -1;
500 		goto out;
501 	}
502 
503 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
504 	err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->bip,
505 				      key, MCU_UNI_CMD(STA_REC_UPDATE),
506 				      &msta->wcid, cmd);
507 	if (err)
508 		goto out;
509 
510 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
511 	    key->cipher == WLAN_CIPHER_SUITE_WEP40)
512 		err = mt76_connac_mcu_add_key(&dev->mt76, vif,
513 					      &mvif->wep_sta->bip,
514 					      key, MCU_UNI_CMD(STA_REC_UPDATE),
515 					      &mvif->wep_sta->wcid, cmd);
516 out:
517 	mt792x_mutex_release(dev);
518 
519 	return err;
520 }
521 
522 static void
523 mt7921_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
524 {
525 	struct mt792x_dev *dev = priv;
526 	struct ieee80211_hw *hw = mt76_hw(dev);
527 	bool pm_enable = dev->pm.enable;
528 	int err;
529 
530 	err = mt7921_mcu_set_beacon_filter(dev, vif, pm_enable);
531 	if (err < 0)
532 		return;
533 
534 	if (pm_enable) {
535 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
536 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
537 	} else {
538 		vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
539 		__clear_bit(IEEE80211_HW_CONNECTION_MONITOR, hw->flags);
540 	}
541 }
542 
543 static void
544 mt7921_sniffer_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
545 {
546 	struct mt792x_dev *dev = priv;
547 	struct ieee80211_hw *hw = mt76_hw(dev);
548 	struct mt76_connac_pm *pm = &dev->pm;
549 	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
550 
551 	mt7921_mcu_set_sniffer(dev, vif, monitor);
552 	pm->enable = pm->enable_user && !monitor;
553 	pm->ds_enable = pm->ds_enable_user && !monitor;
554 
555 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
556 
557 	if (monitor)
558 		mt7921_mcu_set_beacon_filter(dev, vif, false);
559 }
560 
561 void mt7921_set_runtime_pm(struct mt792x_dev *dev)
562 {
563 	struct ieee80211_hw *hw = mt76_hw(dev);
564 	struct mt76_connac_pm *pm = &dev->pm;
565 	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
566 
567 	pm->enable = pm->enable_user && !monitor;
568 	ieee80211_iterate_active_interfaces(hw,
569 					    IEEE80211_IFACE_ITER_RESUME_ALL,
570 					    mt7921_pm_interface_iter, dev);
571 	pm->ds_enable = pm->ds_enable_user && !monitor;
572 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
573 }
574 
575 static int mt7921_config(struct ieee80211_hw *hw, u32 changed)
576 {
577 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
578 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
579 	int ret = 0;
580 
581 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
582 		ieee80211_stop_queues(hw);
583 		ret = mt7921_set_channel(phy);
584 		if (ret)
585 			return ret;
586 		ieee80211_wake_queues(hw);
587 	}
588 
589 	mt792x_mutex_acquire(dev);
590 
591 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
592 		ret = mt7921_set_tx_sar_pwr(hw, NULL);
593 		if (ret)
594 			goto out;
595 	}
596 
597 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
598 		ieee80211_iterate_active_interfaces(hw,
599 						    IEEE80211_IFACE_ITER_RESUME_ALL,
600 						    mt7921_sniffer_interface_iter, dev);
601 	}
602 
603 out:
604 	mt792x_mutex_release(dev);
605 
606 	return ret;
607 }
608 
609 static void mt7921_configure_filter(struct ieee80211_hw *hw,
610 				    unsigned int changed_flags,
611 				    unsigned int *total_flags,
612 				    u64 multicast)
613 {
614 #define MT7921_FILTER_FCSFAIL    BIT(2)
615 #define MT7921_FILTER_CONTROL    BIT(5)
616 #define MT7921_FILTER_OTHER_BSS  BIT(6)
617 #define MT7921_FILTER_ENABLE     BIT(31)
618 
619 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
620 	u32 flags = MT7921_FILTER_ENABLE;
621 
622 #define MT7921_FILTER(_fif, _type) do {			\
623 		if (*total_flags & (_fif))		\
624 			flags |= MT7921_FILTER_##_type;	\
625 	} while (0)
626 
627 	MT7921_FILTER(FIF_FCSFAIL, FCSFAIL);
628 	MT7921_FILTER(FIF_CONTROL, CONTROL);
629 	MT7921_FILTER(FIF_OTHER_BSS, OTHER_BSS);
630 
631 	mt792x_mutex_acquire(dev);
632 	mt7921_mcu_set_rxfilter(dev, flags, 0, 0);
633 	mt792x_mutex_release(dev);
634 
635 	*total_flags &= (FIF_OTHER_BSS | FIF_FCSFAIL | FIF_CONTROL);
636 }
637 
638 static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
639 				    struct ieee80211_vif *vif,
640 				    struct ieee80211_bss_conf *info,
641 				    u64 changed)
642 {
643 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
644 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
645 
646 	mt792x_mutex_acquire(dev);
647 
648 	if (changed & BSS_CHANGED_ERP_SLOT) {
649 		int slottime = info->use_short_slot ? 9 : 20;
650 
651 		if (slottime != phy->slottime) {
652 			phy->slottime = slottime;
653 			mt792x_mac_set_timeing(phy);
654 		}
655 	}
656 
657 	if (changed & (BSS_CHANGED_BEACON |
658 		       BSS_CHANGED_BEACON_ENABLED))
659 		mt7921_mcu_uni_add_beacon_offload(dev, hw, vif,
660 						  info->enable_beacon);
661 
662 	/* ensure that enable txcmd_mode after bss_info */
663 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
664 		mt7921_mcu_set_tx(dev, vif);
665 
666 	if (changed & BSS_CHANGED_PS)
667 		mt7921_mcu_uni_bss_ps(dev, vif);
668 
669 	if (changed & BSS_CHANGED_ASSOC) {
670 		mt7921_mcu_sta_update(dev, NULL, vif, true,
671 				      MT76_STA_INFO_STATE_ASSOC);
672 		mt7921_mcu_set_beacon_filter(dev, vif, vif->cfg.assoc);
673 	}
674 
675 	if (changed & BSS_CHANGED_ARP_FILTER) {
676 		struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
677 
678 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
679 						  info);
680 	}
681 
682 	mt792x_mutex_release(dev);
683 }
684 
685 static void
686 mt7921_calc_vif_num(void *priv, u8 *mac, struct ieee80211_vif *vif)
687 {
688 	u32 *num = priv;
689 
690 	if (!priv)
691 		return;
692 
693 	switch (vif->type) {
694 	case NL80211_IFTYPE_STATION:
695 	case NL80211_IFTYPE_P2P_CLIENT:
696 	case NL80211_IFTYPE_AP:
697 	case NL80211_IFTYPE_P2P_GO:
698 		*num += 1;
699 		break;
700 	default:
701 		break;
702 	}
703 }
704 
705 static void
706 mt7921_regd_set_6ghz_power_type(struct ieee80211_vif *vif, bool is_add)
707 {
708 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
709 	struct mt792x_phy *phy = mvif->phy;
710 	struct mt792x_dev *dev = phy->dev;
711 	u32 valid_vif_num = 0;
712 
713 	ieee80211_iterate_active_interfaces(mt76_hw(dev),
714 					    IEEE80211_IFACE_ITER_RESUME_ALL,
715 					    mt7921_calc_vif_num, &valid_vif_num);
716 
717 	if (valid_vif_num > 1) {
718 		phy->power_type = MT_AP_DEFAULT;
719 		goto out;
720 	}
721 
722 	if (!is_add)
723 		vif->bss_conf.power_type = IEEE80211_REG_UNSET_AP;
724 
725 	switch (vif->bss_conf.power_type) {
726 	case IEEE80211_REG_SP_AP:
727 		phy->power_type = MT_AP_SP;
728 		break;
729 	case IEEE80211_REG_VLP_AP:
730 		phy->power_type = MT_AP_VLP;
731 		break;
732 	case IEEE80211_REG_LPI_AP:
733 		phy->power_type = MT_AP_LPI;
734 		break;
735 	case IEEE80211_REG_UNSET_AP:
736 		phy->power_type = MT_AP_UNSET;
737 		break;
738 	default:
739 		phy->power_type = MT_AP_DEFAULT;
740 		break;
741 	}
742 
743 out:
744 	mt7921_mcu_set_clc(dev, dev->mt76.alpha2, dev->country_ie_env);
745 }
746 
747 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
748 		       struct ieee80211_sta *sta)
749 {
750 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
751 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
752 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
753 	int ret, idx;
754 
755 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT792x_WTBL_STA - 1);
756 	if (idx < 0)
757 		return -ENOSPC;
758 
759 	INIT_LIST_HEAD(&msta->wcid.poll_list);
760 	msta->vif = mvif;
761 	msta->wcid.sta = 1;
762 	msta->wcid.idx = idx;
763 	msta->wcid.phy_idx = mvif->mt76.band_idx;
764 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
765 	msta->last_txs = jiffies;
766 
767 	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
768 	if (ret)
769 		return ret;
770 
771 	if (vif->type == NL80211_IFTYPE_STATION)
772 		mvif->wep_sta = msta;
773 
774 	mt7921_mac_wtbl_update(dev, idx,
775 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
776 
777 	ret = mt7921_mcu_sta_update(dev, sta, vif, true,
778 				    MT76_STA_INFO_STATE_NONE);
779 	if (ret)
780 		return ret;
781 
782 	mt7921_regd_set_6ghz_power_type(vif, true);
783 
784 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
785 
786 	return 0;
787 }
788 EXPORT_SYMBOL_GPL(mt7921_mac_sta_add);
789 
790 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
791 			  struct ieee80211_sta *sta)
792 {
793 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
794 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
795 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
796 
797 	mt792x_mutex_acquire(dev);
798 
799 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
800 		mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
801 					    true, mvif->mt76.ctx);
802 
803 	ewma_avg_signal_init(&msta->avg_ack_signal);
804 
805 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
806 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
807 	memset(msta->airtime_ac, 0, sizeof(msta->airtime_ac));
808 
809 	mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
810 
811 	mt792x_mutex_release(dev);
812 }
813 EXPORT_SYMBOL_GPL(mt7921_mac_sta_assoc);
814 
815 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
816 			   struct ieee80211_sta *sta)
817 {
818 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
819 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
820 
821 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
822 	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
823 
824 	mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
825 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
826 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
827 
828 	if (vif->type == NL80211_IFTYPE_STATION) {
829 		struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
830 
831 		mvif->wep_sta = NULL;
832 		ewma_rssi_init(&mvif->rssi);
833 		if (!sta->tdls)
834 			mt76_connac_mcu_uni_add_bss(&dev->mphy, vif,
835 						    &mvif->sta.wcid, false,
836 						    mvif->mt76.ctx);
837 	}
838 
839 	spin_lock_bh(&dev->mt76.sta_poll_lock);
840 	if (!list_empty(&msta->wcid.poll_list))
841 		list_del_init(&msta->wcid.poll_list);
842 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
843 
844 	mt7921_regd_set_6ghz_power_type(vif, false);
845 
846 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
847 }
848 EXPORT_SYMBOL_GPL(mt7921_mac_sta_remove);
849 
850 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
851 {
852 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
853 
854 	mt792x_mutex_acquire(dev);
855 	mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
856 	mt792x_mutex_release(dev);
857 
858 	return 0;
859 }
860 
861 static int
862 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
863 		    struct ieee80211_ampdu_params *params)
864 {
865 	enum ieee80211_ampdu_mlme_action action = params->action;
866 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
867 	struct ieee80211_sta *sta = params->sta;
868 	struct ieee80211_txq *txq = sta->txq[params->tid];
869 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
870 	u16 tid = params->tid;
871 	u16 ssn = params->ssn;
872 	struct mt76_txq *mtxq;
873 	int ret = 0;
874 
875 	if (!txq)
876 		return -EINVAL;
877 
878 	mtxq = (struct mt76_txq *)txq->drv_priv;
879 
880 	mt792x_mutex_acquire(dev);
881 	switch (action) {
882 	case IEEE80211_AMPDU_RX_START:
883 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
884 				   params->buf_size);
885 		mt7921_mcu_uni_rx_ba(dev, params, true);
886 		break;
887 	case IEEE80211_AMPDU_RX_STOP:
888 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
889 		mt7921_mcu_uni_rx_ba(dev, params, false);
890 		break;
891 	case IEEE80211_AMPDU_TX_OPERATIONAL:
892 		mtxq->aggr = true;
893 		mtxq->send_bar = false;
894 		mt7921_mcu_uni_tx_ba(dev, params, true);
895 		break;
896 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
897 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
898 		mtxq->aggr = false;
899 		clear_bit(tid, &msta->wcid.ampdu_state);
900 		mt7921_mcu_uni_tx_ba(dev, params, false);
901 		break;
902 	case IEEE80211_AMPDU_TX_START:
903 		set_bit(tid, &msta->wcid.ampdu_state);
904 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
905 		break;
906 	case IEEE80211_AMPDU_TX_STOP_CONT:
907 		mtxq->aggr = false;
908 		clear_bit(tid, &msta->wcid.ampdu_state);
909 		mt7921_mcu_uni_tx_ba(dev, params, false);
910 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
911 		break;
912 	}
913 	mt792x_mutex_release(dev);
914 
915 	return ret;
916 }
917 
918 static int mt7921_sta_state(struct ieee80211_hw *hw,
919 			    struct ieee80211_vif *vif,
920 			    struct ieee80211_sta *sta,
921 			    enum ieee80211_sta_state old_state,
922 			    enum ieee80211_sta_state new_state)
923 {
924 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
925 
926 	if (dev->pm.ds_enable) {
927 		mt792x_mutex_acquire(dev);
928 		mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state);
929 		mt792x_mutex_release(dev);
930 	}
931 
932 	return mt76_sta_state(hw, vif, sta, old_state, new_state);
933 }
934 
935 void mt7921_scan_work(struct work_struct *work)
936 {
937 	struct mt792x_phy *phy;
938 
939 	phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy,
940 						scan_work.work);
941 
942 	while (true) {
943 		struct mt76_connac2_mcu_rxd *rxd;
944 		struct sk_buff *skb;
945 
946 		spin_lock_bh(&phy->dev->mt76.lock);
947 		skb = __skb_dequeue(&phy->scan_event_list);
948 		spin_unlock_bh(&phy->dev->mt76.lock);
949 
950 		if (!skb)
951 			break;
952 
953 		rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
954 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
955 			ieee80211_sched_scan_results(phy->mt76->hw);
956 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
957 					      &phy->mt76->state)) {
958 			struct cfg80211_scan_info info = {
959 				.aborted = false,
960 			};
961 
962 			ieee80211_scan_completed(phy->mt76->hw, &info);
963 		}
964 		dev_kfree_skb(skb);
965 	}
966 }
967 
968 static int
969 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
970 	       struct ieee80211_scan_request *req)
971 {
972 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
973 	struct mt76_phy *mphy = hw->priv;
974 	int err;
975 
976 	mt792x_mutex_acquire(dev);
977 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
978 	mt792x_mutex_release(dev);
979 
980 	return err;
981 }
982 
983 static void
984 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
985 {
986 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
987 	struct mt76_phy *mphy = hw->priv;
988 
989 	mt792x_mutex_acquire(dev);
990 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
991 	mt792x_mutex_release(dev);
992 }
993 
994 static int
995 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
996 			struct cfg80211_sched_scan_request *req,
997 			struct ieee80211_scan_ies *ies)
998 {
999 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1000 	struct mt76_phy *mphy = hw->priv;
1001 	int err;
1002 
1003 	mt792x_mutex_acquire(dev);
1004 
1005 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1006 	if (err < 0)
1007 		goto out;
1008 
1009 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1010 out:
1011 	mt792x_mutex_release(dev);
1012 
1013 	return err;
1014 }
1015 
1016 static int
1017 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1018 {
1019 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1020 	struct mt76_phy *mphy = hw->priv;
1021 	int err;
1022 
1023 	mt792x_mutex_acquire(dev);
1024 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1025 	mt792x_mutex_release(dev);
1026 
1027 	return err;
1028 }
1029 
1030 static int
1031 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1032 {
1033 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1034 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1035 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1036 
1037 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1038 		return -EINVAL;
1039 
1040 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1041 		return -EINVAL;
1042 
1043 	mt792x_mutex_acquire(dev);
1044 
1045 	phy->mt76->antenna_mask = tx_ant;
1046 	phy->mt76->chainmask = tx_ant;
1047 
1048 	mt76_set_stream_caps(phy->mt76, true);
1049 	mt7921_set_stream_he_caps(phy);
1050 
1051 	mt792x_mutex_release(dev);
1052 
1053 	return 0;
1054 }
1055 
1056 #ifdef CONFIG_PM
1057 static int mt7921_suspend(struct ieee80211_hw *hw,
1058 			  struct cfg80211_wowlan *wowlan)
1059 {
1060 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1061 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1062 
1063 	cancel_delayed_work_sync(&phy->scan_work);
1064 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1065 
1066 	cancel_delayed_work_sync(&dev->pm.ps_work);
1067 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1068 
1069 	mt792x_mutex_acquire(dev);
1070 
1071 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1072 	ieee80211_iterate_active_interfaces(hw,
1073 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1074 					    mt7921_mcu_set_suspend_iter,
1075 					    &dev->mphy);
1076 
1077 	mt792x_mutex_release(dev);
1078 
1079 	return 0;
1080 }
1081 
1082 static int mt7921_resume(struct ieee80211_hw *hw)
1083 {
1084 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1085 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1086 
1087 	mt792x_mutex_acquire(dev);
1088 
1089 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1090 	ieee80211_iterate_active_interfaces(hw,
1091 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1092 					    mt76_connac_mcu_set_suspend_iter,
1093 					    &dev->mphy);
1094 
1095 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1096 				     MT792x_WATCHDOG_TIME);
1097 
1098 	mt792x_mutex_release(dev);
1099 
1100 	return 0;
1101 }
1102 
1103 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1104 				  struct ieee80211_vif *vif,
1105 				  struct cfg80211_gtk_rekey_data *data)
1106 {
1107 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1108 
1109 	mt792x_mutex_acquire(dev);
1110 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1111 	mt792x_mutex_release(dev);
1112 }
1113 #endif /* CONFIG_PM */
1114 
1115 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1116 					 struct ieee80211_vif *vif,
1117 					 struct ieee80211_sta *sta,
1118 					 bool enabled)
1119 {
1120 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1121 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1122 
1123 	mt792x_mutex_acquire(dev);
1124 
1125 	if (enabled)
1126 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1127 	else
1128 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1129 
1130 	mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->wcid,
1131 					     MCU_UNI_CMD(STA_REC_UPDATE));
1132 
1133 	mt792x_mutex_release(dev);
1134 }
1135 
1136 #if IS_ENABLED(CONFIG_IPV6)
1137 static void mt7921_ipv6_addr_change(struct ieee80211_hw *hw,
1138 				    struct ieee80211_vif *vif,
1139 				    struct inet6_dev *idev)
1140 {
1141 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1142 	struct mt792x_dev *dev = mvif->phy->dev;
1143 	struct inet6_ifaddr *ifa;
1144 	struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN];
1145 	struct sk_buff *skb;
1146 	u8 i, idx = 0;
1147 
1148 	struct {
1149 		struct {
1150 			u8 bss_idx;
1151 			u8 pad[3];
1152 		} __packed hdr;
1153 		struct mt76_connac_arpns_tlv arpns;
1154 	} req_hdr = {
1155 		.hdr = {
1156 			.bss_idx = mvif->mt76.idx,
1157 		},
1158 		.arpns = {
1159 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
1160 			.mode = 2,  /* update */
1161 			.option = 1, /* update only */
1162 		},
1163 	};
1164 
1165 	read_lock_bh(&idev->lock);
1166 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
1167 		if (ifa->flags & IFA_F_TENTATIVE)
1168 			continue;
1169 		ns_addrs[idx] = ifa->addr;
1170 		if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN)
1171 			break;
1172 	}
1173 	read_unlock_bh(&idev->lock);
1174 
1175 	if (!idx)
1176 		return;
1177 
1178 	req_hdr.arpns.ips_num = idx;
1179 	req_hdr.arpns.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)
1180 					+ idx * sizeof(struct in6_addr));
1181 	skb = __mt76_mcu_msg_alloc(&dev->mt76, &req_hdr,
1182 			sizeof(req_hdr) + idx * sizeof(struct in6_addr),
1183 			sizeof(req_hdr), GFP_ATOMIC);
1184 	if (!skb)
1185 		return;
1186 
1187 	for (i = 0; i < idx; i++)
1188 		skb_put_data(skb, &ns_addrs[i].in6_u, sizeof(struct in6_addr));
1189 
1190 	skb_queue_tail(&dev->ipv6_ns_list, skb);
1191 
1192 	ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work);
1193 }
1194 #endif
1195 
1196 int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw,
1197 			  const struct cfg80211_sar_specs *sar)
1198 {
1199 	struct mt76_phy *mphy = hw->priv;
1200 
1201 	if (sar) {
1202 		int err = mt76_init_sar_power(hw, sar);
1203 
1204 		if (err)
1205 			return err;
1206 	}
1207 	mt792x_init_acpi_sar_power(mt792x_hw_phy(hw), !sar);
1208 
1209 	return mt76_connac_mcu_set_rate_txpower(mphy);
1210 }
1211 
1212 static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
1213 				const struct cfg80211_sar_specs *sar)
1214 {
1215 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1216 	int err;
1217 
1218 	mt792x_mutex_acquire(dev);
1219 	err = mt7921_mcu_set_clc(dev, dev->mt76.alpha2,
1220 				 dev->country_ie_env);
1221 	if (err < 0)
1222 		goto out;
1223 
1224 	err = mt7921_set_tx_sar_pwr(hw, sar);
1225 out:
1226 	mt792x_mutex_release(dev);
1227 
1228 	return err;
1229 }
1230 
1231 static void
1232 mt7921_channel_switch_beacon(struct ieee80211_hw *hw,
1233 			     struct ieee80211_vif *vif,
1234 			     struct cfg80211_chan_def *chandef)
1235 {
1236 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1237 
1238 	mt792x_mutex_acquire(dev);
1239 	mt7921_mcu_uni_add_beacon_offload(dev, hw, vif, true);
1240 	mt792x_mutex_release(dev);
1241 }
1242 
1243 static int
1244 mt7921_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1245 		struct ieee80211_bss_conf *link_conf)
1246 {
1247 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1248 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1249 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1250 	int err;
1251 
1252 	mt792x_mutex_acquire(dev);
1253 
1254 	err = mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid,
1255 					  true, mvif->mt76.ctx);
1256 	if (err)
1257 		goto out;
1258 
1259 	err = mt7921_mcu_set_bss_pm(dev, vif, true);
1260 	if (err)
1261 		goto out;
1262 
1263 	err = mt7921_mcu_sta_update(dev, NULL, vif, true,
1264 				    MT76_STA_INFO_STATE_NONE);
1265 out:
1266 	mt792x_mutex_release(dev);
1267 
1268 	return err;
1269 }
1270 
1271 static void
1272 mt7921_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1273 	       struct ieee80211_bss_conf *link_conf)
1274 {
1275 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1276 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1277 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1278 	int err;
1279 
1280 	mt792x_mutex_acquire(dev);
1281 
1282 	err = mt7921_mcu_set_bss_pm(dev, vif, false);
1283 	if (err)
1284 		goto out;
1285 
1286 	mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid, false,
1287 				    mvif->mt76.ctx);
1288 
1289 out:
1290 	mt792x_mutex_release(dev);
1291 }
1292 
1293 static int
1294 mt7921_add_chanctx(struct ieee80211_hw *hw,
1295 		   struct ieee80211_chanctx_conf *ctx)
1296 {
1297 	return 0;
1298 }
1299 
1300 static void
1301 mt7921_remove_chanctx(struct ieee80211_hw *hw,
1302 		      struct ieee80211_chanctx_conf *ctx)
1303 {
1304 }
1305 
1306 static void mt7921_ctx_iter(void *priv, u8 *mac,
1307 			    struct ieee80211_vif *vif)
1308 {
1309 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1310 	struct ieee80211_chanctx_conf *ctx = priv;
1311 
1312 	if (ctx != mvif->mt76.ctx)
1313 		return;
1314 
1315 	if (vif->type == NL80211_IFTYPE_MONITOR)
1316 		mt7921_mcu_config_sniffer(mvif, ctx);
1317 	else
1318 		mt76_connac_mcu_uni_set_chctx(mvif->phy->mt76, &mvif->mt76, ctx);
1319 }
1320 
1321 static void
1322 mt7921_change_chanctx(struct ieee80211_hw *hw,
1323 		      struct ieee80211_chanctx_conf *ctx,
1324 		      u32 changed)
1325 {
1326 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1327 
1328 	mt792x_mutex_acquire(phy->dev);
1329 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
1330 					    IEEE80211_IFACE_ITER_ACTIVE,
1331 					    mt7921_ctx_iter, ctx);
1332 	mt792x_mutex_release(phy->dev);
1333 }
1334 
1335 static void mt7921_mgd_prepare_tx(struct ieee80211_hw *hw,
1336 				  struct ieee80211_vif *vif,
1337 				  struct ieee80211_prep_tx_info *info)
1338 {
1339 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1340 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1341 	u16 duration = info->duration ? info->duration :
1342 		       jiffies_to_msecs(HZ);
1343 
1344 	mt792x_mutex_acquire(dev);
1345 	mt7921_set_roc(mvif->phy, mvif, mvif->mt76.ctx->def.chan, duration,
1346 		       MT7921_ROC_REQ_JOIN);
1347 	mt792x_mutex_release(dev);
1348 }
1349 
1350 static void mt7921_mgd_complete_tx(struct ieee80211_hw *hw,
1351 				   struct ieee80211_vif *vif,
1352 				   struct ieee80211_prep_tx_info *info)
1353 {
1354 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1355 
1356 	mt7921_abort_roc(mvif->phy, mvif);
1357 }
1358 
1359 const struct ieee80211_ops mt7921_ops = {
1360 	.tx = mt792x_tx,
1361 	.start = mt7921_start,
1362 	.stop = mt792x_stop,
1363 	.add_interface = mt7921_add_interface,
1364 	.remove_interface = mt792x_remove_interface,
1365 	.config = mt7921_config,
1366 	.conf_tx = mt792x_conf_tx,
1367 	.configure_filter = mt7921_configure_filter,
1368 	.bss_info_changed = mt7921_bss_info_changed,
1369 	.start_ap = mt7921_start_ap,
1370 	.stop_ap = mt7921_stop_ap,
1371 	.sta_state = mt7921_sta_state,
1372 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1373 	.set_key = mt7921_set_key,
1374 	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1375 #if IS_ENABLED(CONFIG_IPV6)
1376 	.ipv6_addr_change = mt7921_ipv6_addr_change,
1377 #endif /* CONFIG_IPV6 */
1378 	.ampdu_action = mt7921_ampdu_action,
1379 	.set_rts_threshold = mt7921_set_rts_threshold,
1380 	.wake_tx_queue = mt76_wake_tx_queue,
1381 	.release_buffered_frames = mt76_release_buffered_frames,
1382 	.channel_switch_beacon = mt7921_channel_switch_beacon,
1383 	.get_txpower = mt76_get_txpower,
1384 	.get_stats = mt792x_get_stats,
1385 	.get_et_sset_count = mt792x_get_et_sset_count,
1386 	.get_et_strings = mt792x_get_et_strings,
1387 	.get_et_stats = mt792x_get_et_stats,
1388 	.get_tsf = mt792x_get_tsf,
1389 	.set_tsf = mt792x_set_tsf,
1390 	.get_survey = mt76_get_survey,
1391 	.get_antenna = mt76_get_antenna,
1392 	.set_antenna = mt7921_set_antenna,
1393 	.set_coverage_class = mt792x_set_coverage_class,
1394 	.hw_scan = mt7921_hw_scan,
1395 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1396 	.sta_statistics = mt792x_sta_statistics,
1397 	.sched_scan_start = mt7921_start_sched_scan,
1398 	.sched_scan_stop = mt7921_stop_sched_scan,
1399 	CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
1400 	CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
1401 #ifdef CONFIG_PM
1402 	.suspend = mt7921_suspend,
1403 	.resume = mt7921_resume,
1404 	.set_wakeup = mt792x_set_wakeup,
1405 	.set_rekey_data = mt7921_set_rekey_data,
1406 #endif /* CONFIG_PM */
1407 	.flush = mt792x_flush,
1408 	.set_sar_specs = mt7921_set_sar_specs,
1409 	.remain_on_channel = mt7921_remain_on_channel,
1410 	.cancel_remain_on_channel = mt7921_cancel_remain_on_channel,
1411 	.add_chanctx = mt7921_add_chanctx,
1412 	.remove_chanctx = mt7921_remove_chanctx,
1413 	.change_chanctx = mt7921_change_chanctx,
1414 	.assign_vif_chanctx = mt792x_assign_vif_chanctx,
1415 	.unassign_vif_chanctx = mt792x_unassign_vif_chanctx,
1416 	.mgd_prepare_tx = mt7921_mgd_prepare_tx,
1417 	.mgd_complete_tx = mt7921_mgd_complete_tx,
1418 };
1419 EXPORT_SYMBOL_GPL(mt7921_ops);
1420 
1421 MODULE_LICENSE("Dual BSD/GPL");
1422 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1423