xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/main.c (revision ef9226cd56b718c79184a3466d32984a51cb449c)
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_abort_sync(struct mt792x_dev *dev)
329 {
330 	struct mt792x_phy *phy = &dev->phy;
331 
332 	del_timer_sync(&phy->roc_timer);
333 	cancel_work_sync(&phy->roc_work);
334 	if (test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
335 		ieee80211_iterate_active_interfaces(mt76_hw(dev),
336 						    IEEE80211_IFACE_ITER_RESUME_ALL,
337 						    mt7921_roc_iter, (void *)phy);
338 }
339 EXPORT_SYMBOL_GPL(mt7921_roc_abort_sync);
340 
341 void mt7921_roc_work(struct work_struct *work)
342 {
343 	struct mt792x_phy *phy;
344 
345 	phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy,
346 						roc_work);
347 
348 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
349 		return;
350 
351 	mt792x_mutex_acquire(phy->dev);
352 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
353 					    IEEE80211_IFACE_ITER_RESUME_ALL,
354 					    mt7921_roc_iter, phy);
355 	mt792x_mutex_release(phy->dev);
356 	ieee80211_remain_on_channel_expired(phy->mt76->hw);
357 }
358 
359 static int mt7921_abort_roc(struct mt792x_phy *phy, struct mt792x_vif *vif)
360 {
361 	int err = 0;
362 
363 	del_timer_sync(&phy->roc_timer);
364 	cancel_work_sync(&phy->roc_work);
365 
366 	mt792x_mutex_acquire(phy->dev);
367 	if (test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
368 		err = mt7921_mcu_abort_roc(phy, vif, phy->roc_token_id);
369 	mt792x_mutex_release(phy->dev);
370 
371 	return err;
372 }
373 
374 static int mt7921_set_roc(struct mt792x_phy *phy,
375 			  struct mt792x_vif *vif,
376 			  struct ieee80211_channel *chan,
377 			  int duration,
378 			  enum mt7921_roc_req type)
379 {
380 	int err;
381 
382 	if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
383 		return -EBUSY;
384 
385 	phy->roc_grant = false;
386 
387 	err = mt7921_mcu_set_roc(phy, vif, chan, duration, type,
388 				 ++phy->roc_token_id);
389 	if (err < 0) {
390 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
391 		goto out;
392 	}
393 
394 	if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) {
395 		mt7921_mcu_abort_roc(phy, vif, phy->roc_token_id);
396 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
397 		err = -ETIMEDOUT;
398 	}
399 
400 out:
401 	return err;
402 }
403 
404 static int mt7921_remain_on_channel(struct ieee80211_hw *hw,
405 				    struct ieee80211_vif *vif,
406 				    struct ieee80211_channel *chan,
407 				    int duration,
408 				    enum ieee80211_roc_type type)
409 {
410 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
411 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
412 	int err;
413 
414 	mt792x_mutex_acquire(phy->dev);
415 	err = mt7921_set_roc(phy, mvif, chan, duration, MT7921_ROC_REQ_ROC);
416 	mt792x_mutex_release(phy->dev);
417 
418 	return err;
419 }
420 
421 static int mt7921_cancel_remain_on_channel(struct ieee80211_hw *hw,
422 					   struct ieee80211_vif *vif)
423 {
424 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
425 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
426 
427 	return mt7921_abort_roc(phy, mvif);
428 }
429 
430 static int mt7921_set_channel(struct mt792x_phy *phy)
431 {
432 	struct mt792x_dev *dev = phy->dev;
433 	int ret;
434 
435 	cancel_delayed_work_sync(&phy->mt76->mac_work);
436 
437 	mt792x_mutex_acquire(dev);
438 	set_bit(MT76_RESET, &phy->mt76->state);
439 
440 	mt76_set_channel(phy->mt76);
441 
442 	ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
443 	if (ret)
444 		goto out;
445 
446 	mt792x_mac_set_timeing(phy);
447 
448 	mt792x_mac_reset_counters(phy);
449 	phy->noise = 0;
450 
451 out:
452 	clear_bit(MT76_RESET, &phy->mt76->state);
453 	mt792x_mutex_release(dev);
454 
455 	mt76_worker_schedule(&dev->mt76.tx_worker);
456 	ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mt76->mac_work,
457 				     MT792x_WATCHDOG_TIME);
458 
459 	return ret;
460 }
461 
462 static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
463 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
464 			  struct ieee80211_key_conf *key)
465 {
466 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
467 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
468 	struct mt792x_sta *msta = sta ? (struct mt792x_sta *)sta->drv_priv :
469 				  &mvif->sta;
470 	struct mt76_wcid *wcid = &msta->wcid;
471 	u8 *wcid_keyidx = &wcid->hw_key_idx;
472 	int idx = key->keyidx, err = 0;
473 
474 	/* The hardware does not support per-STA RX GTK, fallback
475 	 * to software mode for these.
476 	 */
477 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
478 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
479 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
480 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
481 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
482 		return -EOPNOTSUPP;
483 
484 	/* fall back to sw encryption for unsupported ciphers */
485 	switch (key->cipher) {
486 	case WLAN_CIPHER_SUITE_AES_CMAC:
487 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
488 		wcid_keyidx = &wcid->hw_key_idx2;
489 		break;
490 	case WLAN_CIPHER_SUITE_WEP40:
491 	case WLAN_CIPHER_SUITE_WEP104:
492 		if (!mvif->wep_sta)
493 			return -EOPNOTSUPP;
494 		break;
495 	case WLAN_CIPHER_SUITE_TKIP:
496 	case WLAN_CIPHER_SUITE_CCMP:
497 	case WLAN_CIPHER_SUITE_CCMP_256:
498 	case WLAN_CIPHER_SUITE_GCMP:
499 	case WLAN_CIPHER_SUITE_GCMP_256:
500 	case WLAN_CIPHER_SUITE_SMS4:
501 		break;
502 	default:
503 		return -EOPNOTSUPP;
504 	}
505 
506 	mt792x_mutex_acquire(dev);
507 
508 	if (cmd == SET_KEY) {
509 		*wcid_keyidx = idx;
510 	} else {
511 		if (idx == *wcid_keyidx)
512 			*wcid_keyidx = -1;
513 		goto out;
514 	}
515 
516 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
517 	err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->bip,
518 				      key, MCU_UNI_CMD(STA_REC_UPDATE),
519 				      &msta->wcid, cmd);
520 	if (err)
521 		goto out;
522 
523 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
524 	    key->cipher == WLAN_CIPHER_SUITE_WEP40)
525 		err = mt76_connac_mcu_add_key(&dev->mt76, vif,
526 					      &mvif->wep_sta->bip,
527 					      key, MCU_UNI_CMD(STA_REC_UPDATE),
528 					      &mvif->wep_sta->wcid, cmd);
529 out:
530 	mt792x_mutex_release(dev);
531 
532 	return err;
533 }
534 
535 static void
536 mt7921_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
537 {
538 	struct mt792x_dev *dev = priv;
539 	struct ieee80211_hw *hw = mt76_hw(dev);
540 	bool pm_enable = dev->pm.enable;
541 	int err;
542 
543 	err = mt7921_mcu_set_beacon_filter(dev, vif, pm_enable);
544 	if (err < 0)
545 		return;
546 
547 	if (pm_enable) {
548 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
549 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
550 	} else {
551 		vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
552 		__clear_bit(IEEE80211_HW_CONNECTION_MONITOR, hw->flags);
553 	}
554 }
555 
556 static void
557 mt7921_sniffer_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
558 {
559 	struct mt792x_dev *dev = priv;
560 	struct ieee80211_hw *hw = mt76_hw(dev);
561 	struct mt76_connac_pm *pm = &dev->pm;
562 	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
563 
564 	mt7921_mcu_set_sniffer(dev, vif, monitor);
565 	pm->enable = pm->enable_user && !monitor;
566 	pm->ds_enable = pm->ds_enable_user && !monitor;
567 
568 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
569 
570 	if (monitor)
571 		mt7921_mcu_set_beacon_filter(dev, vif, false);
572 }
573 
574 void mt7921_set_runtime_pm(struct mt792x_dev *dev)
575 {
576 	struct ieee80211_hw *hw = mt76_hw(dev);
577 	struct mt76_connac_pm *pm = &dev->pm;
578 	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
579 
580 	pm->enable = pm->enable_user && !monitor;
581 	ieee80211_iterate_active_interfaces(hw,
582 					    IEEE80211_IFACE_ITER_RESUME_ALL,
583 					    mt7921_pm_interface_iter, dev);
584 	pm->ds_enable = pm->ds_enable_user && !monitor;
585 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
586 }
587 
588 static int mt7921_config(struct ieee80211_hw *hw, u32 changed)
589 {
590 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
591 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
592 	int ret = 0;
593 
594 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
595 		ieee80211_stop_queues(hw);
596 		ret = mt7921_set_channel(phy);
597 		if (ret)
598 			return ret;
599 		ieee80211_wake_queues(hw);
600 	}
601 
602 	mt792x_mutex_acquire(dev);
603 
604 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
605 		ret = mt7921_set_tx_sar_pwr(hw, NULL);
606 		if (ret)
607 			goto out;
608 	}
609 
610 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
611 		ieee80211_iterate_active_interfaces(hw,
612 						    IEEE80211_IFACE_ITER_RESUME_ALL,
613 						    mt7921_sniffer_interface_iter, dev);
614 	}
615 
616 out:
617 	mt792x_mutex_release(dev);
618 
619 	return ret;
620 }
621 
622 static void mt7921_configure_filter(struct ieee80211_hw *hw,
623 				    unsigned int changed_flags,
624 				    unsigned int *total_flags,
625 				    u64 multicast)
626 {
627 #define MT7921_FILTER_FCSFAIL    BIT(2)
628 #define MT7921_FILTER_CONTROL    BIT(5)
629 #define MT7921_FILTER_OTHER_BSS  BIT(6)
630 #define MT7921_FILTER_ENABLE     BIT(31)
631 
632 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
633 	u32 flags = MT7921_FILTER_ENABLE;
634 
635 #define MT7921_FILTER(_fif, _type) do {			\
636 		if (*total_flags & (_fif))		\
637 			flags |= MT7921_FILTER_##_type;	\
638 	} while (0)
639 
640 	MT7921_FILTER(FIF_FCSFAIL, FCSFAIL);
641 	MT7921_FILTER(FIF_CONTROL, CONTROL);
642 	MT7921_FILTER(FIF_OTHER_BSS, OTHER_BSS);
643 
644 	mt792x_mutex_acquire(dev);
645 	mt7921_mcu_set_rxfilter(dev, flags, 0, 0);
646 	mt792x_mutex_release(dev);
647 
648 	*total_flags &= (FIF_OTHER_BSS | FIF_FCSFAIL | FIF_CONTROL);
649 }
650 
651 static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
652 				    struct ieee80211_vif *vif,
653 				    struct ieee80211_bss_conf *info,
654 				    u64 changed)
655 {
656 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
657 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
658 
659 	mt792x_mutex_acquire(dev);
660 
661 	if (changed & BSS_CHANGED_ERP_SLOT) {
662 		int slottime = info->use_short_slot ? 9 : 20;
663 
664 		if (slottime != phy->slottime) {
665 			phy->slottime = slottime;
666 			mt792x_mac_set_timeing(phy);
667 		}
668 	}
669 
670 	if (changed & (BSS_CHANGED_BEACON |
671 		       BSS_CHANGED_BEACON_ENABLED))
672 		mt7921_mcu_uni_add_beacon_offload(dev, hw, vif,
673 						  info->enable_beacon);
674 
675 	/* ensure that enable txcmd_mode after bss_info */
676 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
677 		mt7921_mcu_set_tx(dev, vif);
678 
679 	if (changed & BSS_CHANGED_PS)
680 		mt7921_mcu_uni_bss_ps(dev, vif);
681 
682 	if (changed & BSS_CHANGED_ASSOC) {
683 		mt7921_mcu_sta_update(dev, NULL, vif, true,
684 				      MT76_STA_INFO_STATE_ASSOC);
685 		mt7921_mcu_set_beacon_filter(dev, vif, vif->cfg.assoc);
686 	}
687 
688 	if (changed & BSS_CHANGED_ARP_FILTER) {
689 		struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
690 
691 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
692 						  info);
693 	}
694 
695 	mt792x_mutex_release(dev);
696 }
697 
698 static void
699 mt7921_calc_vif_num(void *priv, u8 *mac, struct ieee80211_vif *vif)
700 {
701 	u32 *num = priv;
702 
703 	if (!priv)
704 		return;
705 
706 	switch (vif->type) {
707 	case NL80211_IFTYPE_STATION:
708 	case NL80211_IFTYPE_P2P_CLIENT:
709 	case NL80211_IFTYPE_AP:
710 	case NL80211_IFTYPE_P2P_GO:
711 		*num += 1;
712 		break;
713 	default:
714 		break;
715 	}
716 }
717 
718 static void
719 mt7921_regd_set_6ghz_power_type(struct ieee80211_vif *vif, bool is_add)
720 {
721 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
722 	struct mt792x_phy *phy = mvif->phy;
723 	struct mt792x_dev *dev = phy->dev;
724 	u32 valid_vif_num = 0;
725 
726 	ieee80211_iterate_active_interfaces(mt76_hw(dev),
727 					    IEEE80211_IFACE_ITER_RESUME_ALL,
728 					    mt7921_calc_vif_num, &valid_vif_num);
729 
730 	if (valid_vif_num > 1) {
731 		phy->power_type = MT_AP_DEFAULT;
732 		goto out;
733 	}
734 
735 	if (!is_add)
736 		vif->bss_conf.power_type = IEEE80211_REG_UNSET_AP;
737 
738 	switch (vif->bss_conf.power_type) {
739 	case IEEE80211_REG_SP_AP:
740 		phy->power_type = MT_AP_SP;
741 		break;
742 	case IEEE80211_REG_VLP_AP:
743 		phy->power_type = MT_AP_VLP;
744 		break;
745 	case IEEE80211_REG_LPI_AP:
746 		phy->power_type = MT_AP_LPI;
747 		break;
748 	case IEEE80211_REG_UNSET_AP:
749 		phy->power_type = MT_AP_UNSET;
750 		break;
751 	default:
752 		phy->power_type = MT_AP_DEFAULT;
753 		break;
754 	}
755 
756 out:
757 	mt7921_mcu_set_clc(dev, dev->mt76.alpha2, dev->country_ie_env);
758 }
759 
760 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
761 		       struct ieee80211_sta *sta)
762 {
763 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
764 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
765 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
766 	int ret, idx;
767 
768 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT792x_WTBL_STA - 1);
769 	if (idx < 0)
770 		return -ENOSPC;
771 
772 	INIT_LIST_HEAD(&msta->wcid.poll_list);
773 	msta->vif = mvif;
774 	msta->wcid.sta = 1;
775 	msta->wcid.idx = idx;
776 	msta->wcid.phy_idx = mvif->mt76.band_idx;
777 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
778 	msta->last_txs = jiffies;
779 
780 	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
781 	if (ret)
782 		return ret;
783 
784 	if (vif->type == NL80211_IFTYPE_STATION)
785 		mvif->wep_sta = msta;
786 
787 	mt7921_mac_wtbl_update(dev, idx,
788 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
789 
790 	ret = mt7921_mcu_sta_update(dev, sta, vif, true,
791 				    MT76_STA_INFO_STATE_NONE);
792 	if (ret)
793 		return ret;
794 
795 	mt7921_regd_set_6ghz_power_type(vif, true);
796 
797 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
798 
799 	return 0;
800 }
801 EXPORT_SYMBOL_GPL(mt7921_mac_sta_add);
802 
803 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
804 			  struct ieee80211_sta *sta)
805 {
806 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
807 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
808 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
809 
810 	mt792x_mutex_acquire(dev);
811 
812 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
813 		mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
814 					    true, mvif->mt76.ctx);
815 
816 	ewma_avg_signal_init(&msta->avg_ack_signal);
817 
818 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
819 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
820 	memset(msta->airtime_ac, 0, sizeof(msta->airtime_ac));
821 
822 	mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
823 
824 	mt792x_mutex_release(dev);
825 }
826 EXPORT_SYMBOL_GPL(mt7921_mac_sta_assoc);
827 
828 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
829 			   struct ieee80211_sta *sta)
830 {
831 	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
832 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
833 
834 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
835 	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
836 
837 	mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
838 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
839 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
840 
841 	if (vif->type == NL80211_IFTYPE_STATION) {
842 		struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
843 
844 		mvif->wep_sta = NULL;
845 		ewma_rssi_init(&mvif->rssi);
846 		if (!sta->tdls)
847 			mt76_connac_mcu_uni_add_bss(&dev->mphy, vif,
848 						    &mvif->sta.wcid, false,
849 						    mvif->mt76.ctx);
850 	}
851 
852 	spin_lock_bh(&dev->mt76.sta_poll_lock);
853 	if (!list_empty(&msta->wcid.poll_list))
854 		list_del_init(&msta->wcid.poll_list);
855 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
856 
857 	mt7921_regd_set_6ghz_power_type(vif, false);
858 
859 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
860 }
861 EXPORT_SYMBOL_GPL(mt7921_mac_sta_remove);
862 
863 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
864 {
865 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
866 
867 	mt792x_mutex_acquire(dev);
868 	mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
869 	mt792x_mutex_release(dev);
870 
871 	return 0;
872 }
873 
874 static int
875 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
876 		    struct ieee80211_ampdu_params *params)
877 {
878 	enum ieee80211_ampdu_mlme_action action = params->action;
879 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
880 	struct ieee80211_sta *sta = params->sta;
881 	struct ieee80211_txq *txq = sta->txq[params->tid];
882 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
883 	u16 tid = params->tid;
884 	u16 ssn = params->ssn;
885 	struct mt76_txq *mtxq;
886 	int ret = 0;
887 
888 	if (!txq)
889 		return -EINVAL;
890 
891 	mtxq = (struct mt76_txq *)txq->drv_priv;
892 
893 	mt792x_mutex_acquire(dev);
894 	switch (action) {
895 	case IEEE80211_AMPDU_RX_START:
896 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
897 				   params->buf_size);
898 		mt7921_mcu_uni_rx_ba(dev, params, true);
899 		break;
900 	case IEEE80211_AMPDU_RX_STOP:
901 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
902 		mt7921_mcu_uni_rx_ba(dev, params, false);
903 		break;
904 	case IEEE80211_AMPDU_TX_OPERATIONAL:
905 		mtxq->aggr = true;
906 		mtxq->send_bar = false;
907 		mt7921_mcu_uni_tx_ba(dev, params, true);
908 		break;
909 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
910 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
911 		mtxq->aggr = false;
912 		clear_bit(tid, &msta->wcid.ampdu_state);
913 		mt7921_mcu_uni_tx_ba(dev, params, false);
914 		break;
915 	case IEEE80211_AMPDU_TX_START:
916 		set_bit(tid, &msta->wcid.ampdu_state);
917 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
918 		break;
919 	case IEEE80211_AMPDU_TX_STOP_CONT:
920 		mtxq->aggr = false;
921 		clear_bit(tid, &msta->wcid.ampdu_state);
922 		mt7921_mcu_uni_tx_ba(dev, params, false);
923 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
924 		break;
925 	}
926 	mt792x_mutex_release(dev);
927 
928 	return ret;
929 }
930 
931 static int mt7921_sta_state(struct ieee80211_hw *hw,
932 			    struct ieee80211_vif *vif,
933 			    struct ieee80211_sta *sta,
934 			    enum ieee80211_sta_state old_state,
935 			    enum ieee80211_sta_state new_state)
936 {
937 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
938 
939 	if (dev->pm.ds_enable) {
940 		mt792x_mutex_acquire(dev);
941 		mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state);
942 		mt792x_mutex_release(dev);
943 	}
944 
945 	return mt76_sta_state(hw, vif, sta, old_state, new_state);
946 }
947 
948 void mt7921_scan_work(struct work_struct *work)
949 {
950 	struct mt792x_phy *phy;
951 
952 	phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy,
953 						scan_work.work);
954 
955 	while (true) {
956 		struct mt76_connac2_mcu_rxd *rxd;
957 		struct sk_buff *skb;
958 
959 		spin_lock_bh(&phy->dev->mt76.lock);
960 		skb = __skb_dequeue(&phy->scan_event_list);
961 		spin_unlock_bh(&phy->dev->mt76.lock);
962 
963 		if (!skb)
964 			break;
965 
966 		rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
967 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
968 			ieee80211_sched_scan_results(phy->mt76->hw);
969 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
970 					      &phy->mt76->state)) {
971 			struct cfg80211_scan_info info = {
972 				.aborted = false,
973 			};
974 
975 			ieee80211_scan_completed(phy->mt76->hw, &info);
976 		}
977 		dev_kfree_skb(skb);
978 	}
979 }
980 
981 static int
982 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
983 	       struct ieee80211_scan_request *req)
984 {
985 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
986 	struct mt76_phy *mphy = hw->priv;
987 	int err;
988 
989 	mt792x_mutex_acquire(dev);
990 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
991 	mt792x_mutex_release(dev);
992 
993 	return err;
994 }
995 
996 static void
997 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
998 {
999 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1000 	struct mt76_phy *mphy = hw->priv;
1001 
1002 	mt792x_mutex_acquire(dev);
1003 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1004 	mt792x_mutex_release(dev);
1005 }
1006 
1007 static int
1008 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1009 			struct cfg80211_sched_scan_request *req,
1010 			struct ieee80211_scan_ies *ies)
1011 {
1012 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1013 	struct mt76_phy *mphy = hw->priv;
1014 	int err;
1015 
1016 	mt792x_mutex_acquire(dev);
1017 
1018 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1019 	if (err < 0)
1020 		goto out;
1021 
1022 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1023 out:
1024 	mt792x_mutex_release(dev);
1025 
1026 	return err;
1027 }
1028 
1029 static int
1030 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1031 {
1032 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1033 	struct mt76_phy *mphy = hw->priv;
1034 	int err;
1035 
1036 	mt792x_mutex_acquire(dev);
1037 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1038 	mt792x_mutex_release(dev);
1039 
1040 	return err;
1041 }
1042 
1043 static int
1044 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1045 {
1046 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1047 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1048 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1049 
1050 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1051 		return -EINVAL;
1052 
1053 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1054 		return -EINVAL;
1055 
1056 	mt792x_mutex_acquire(dev);
1057 
1058 	phy->mt76->antenna_mask = tx_ant;
1059 	phy->mt76->chainmask = tx_ant;
1060 
1061 	mt76_set_stream_caps(phy->mt76, true);
1062 	mt7921_set_stream_he_caps(phy);
1063 
1064 	mt792x_mutex_release(dev);
1065 
1066 	return 0;
1067 }
1068 
1069 #ifdef CONFIG_PM
1070 static int mt7921_suspend(struct ieee80211_hw *hw,
1071 			  struct cfg80211_wowlan *wowlan)
1072 {
1073 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1074 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1075 
1076 	cancel_delayed_work_sync(&phy->scan_work);
1077 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1078 
1079 	cancel_delayed_work_sync(&dev->pm.ps_work);
1080 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1081 
1082 	mt792x_mutex_acquire(dev);
1083 
1084 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1085 	ieee80211_iterate_active_interfaces(hw,
1086 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1087 					    mt7921_mcu_set_suspend_iter,
1088 					    &dev->mphy);
1089 
1090 	mt792x_mutex_release(dev);
1091 
1092 	return 0;
1093 }
1094 
1095 static int mt7921_resume(struct ieee80211_hw *hw)
1096 {
1097 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1098 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1099 
1100 	mt792x_mutex_acquire(dev);
1101 
1102 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1103 	ieee80211_iterate_active_interfaces(hw,
1104 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1105 					    mt76_connac_mcu_set_suspend_iter,
1106 					    &dev->mphy);
1107 
1108 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1109 				     MT792x_WATCHDOG_TIME);
1110 
1111 	mt792x_mutex_release(dev);
1112 
1113 	return 0;
1114 }
1115 
1116 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1117 				  struct ieee80211_vif *vif,
1118 				  struct cfg80211_gtk_rekey_data *data)
1119 {
1120 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1121 
1122 	mt792x_mutex_acquire(dev);
1123 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1124 	mt792x_mutex_release(dev);
1125 }
1126 #endif /* CONFIG_PM */
1127 
1128 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1129 					 struct ieee80211_vif *vif,
1130 					 struct ieee80211_sta *sta,
1131 					 bool enabled)
1132 {
1133 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1134 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1135 
1136 	mt792x_mutex_acquire(dev);
1137 
1138 	if (enabled)
1139 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1140 	else
1141 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1142 
1143 	mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->wcid,
1144 					     MCU_UNI_CMD(STA_REC_UPDATE));
1145 
1146 	mt792x_mutex_release(dev);
1147 }
1148 
1149 #if IS_ENABLED(CONFIG_IPV6)
1150 static void mt7921_ipv6_addr_change(struct ieee80211_hw *hw,
1151 				    struct ieee80211_vif *vif,
1152 				    struct inet6_dev *idev)
1153 {
1154 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1155 	struct mt792x_dev *dev = mvif->phy->dev;
1156 	struct inet6_ifaddr *ifa;
1157 	struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN];
1158 	struct sk_buff *skb;
1159 	u8 i, idx = 0;
1160 
1161 	struct {
1162 		struct {
1163 			u8 bss_idx;
1164 			u8 pad[3];
1165 		} __packed hdr;
1166 		struct mt76_connac_arpns_tlv arpns;
1167 	} req_hdr = {
1168 		.hdr = {
1169 			.bss_idx = mvif->mt76.idx,
1170 		},
1171 		.arpns = {
1172 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
1173 			.mode = 2,  /* update */
1174 			.option = 1, /* update only */
1175 		},
1176 	};
1177 
1178 	read_lock_bh(&idev->lock);
1179 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
1180 		if (ifa->flags & IFA_F_TENTATIVE)
1181 			continue;
1182 		ns_addrs[idx] = ifa->addr;
1183 		if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN)
1184 			break;
1185 	}
1186 	read_unlock_bh(&idev->lock);
1187 
1188 	if (!idx)
1189 		return;
1190 
1191 	req_hdr.arpns.ips_num = idx;
1192 	req_hdr.arpns.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)
1193 					+ idx * sizeof(struct in6_addr));
1194 	skb = __mt76_mcu_msg_alloc(&dev->mt76, &req_hdr,
1195 			sizeof(req_hdr) + idx * sizeof(struct in6_addr),
1196 			sizeof(req_hdr), GFP_ATOMIC);
1197 	if (!skb)
1198 		return;
1199 
1200 	for (i = 0; i < idx; i++)
1201 		skb_put_data(skb, &ns_addrs[i].in6_u, sizeof(struct in6_addr));
1202 
1203 	skb_queue_tail(&dev->ipv6_ns_list, skb);
1204 
1205 	ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work);
1206 }
1207 #endif
1208 
1209 int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw,
1210 			  const struct cfg80211_sar_specs *sar)
1211 {
1212 	struct mt76_phy *mphy = hw->priv;
1213 
1214 	if (sar) {
1215 		int err = mt76_init_sar_power(hw, sar);
1216 
1217 		if (err)
1218 			return err;
1219 	}
1220 	mt792x_init_acpi_sar_power(mt792x_hw_phy(hw), !sar);
1221 
1222 	return mt76_connac_mcu_set_rate_txpower(mphy);
1223 }
1224 
1225 static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
1226 				const struct cfg80211_sar_specs *sar)
1227 {
1228 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1229 	int err;
1230 
1231 	mt792x_mutex_acquire(dev);
1232 	err = mt7921_mcu_set_clc(dev, dev->mt76.alpha2,
1233 				 dev->country_ie_env);
1234 	if (err < 0)
1235 		goto out;
1236 
1237 	err = mt7921_set_tx_sar_pwr(hw, sar);
1238 out:
1239 	mt792x_mutex_release(dev);
1240 
1241 	return err;
1242 }
1243 
1244 static void
1245 mt7921_channel_switch_beacon(struct ieee80211_hw *hw,
1246 			     struct ieee80211_vif *vif,
1247 			     struct cfg80211_chan_def *chandef)
1248 {
1249 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1250 
1251 	mt792x_mutex_acquire(dev);
1252 	mt7921_mcu_uni_add_beacon_offload(dev, hw, vif, true);
1253 	mt792x_mutex_release(dev);
1254 }
1255 
1256 static int
1257 mt7921_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1258 		struct ieee80211_bss_conf *link_conf)
1259 {
1260 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1261 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1262 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1263 	int err;
1264 
1265 	mt792x_mutex_acquire(dev);
1266 
1267 	err = mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid,
1268 					  true, mvif->mt76.ctx);
1269 	if (err)
1270 		goto out;
1271 
1272 	err = mt7921_mcu_set_bss_pm(dev, vif, true);
1273 	if (err)
1274 		goto out;
1275 
1276 	err = mt7921_mcu_sta_update(dev, NULL, vif, true,
1277 				    MT76_STA_INFO_STATE_NONE);
1278 out:
1279 	mt792x_mutex_release(dev);
1280 
1281 	return err;
1282 }
1283 
1284 static void
1285 mt7921_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1286 	       struct ieee80211_bss_conf *link_conf)
1287 {
1288 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1289 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1290 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1291 	int err;
1292 
1293 	mt792x_mutex_acquire(dev);
1294 
1295 	err = mt7921_mcu_set_bss_pm(dev, vif, false);
1296 	if (err)
1297 		goto out;
1298 
1299 	mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid, false,
1300 				    mvif->mt76.ctx);
1301 
1302 out:
1303 	mt792x_mutex_release(dev);
1304 }
1305 
1306 static int
1307 mt7921_add_chanctx(struct ieee80211_hw *hw,
1308 		   struct ieee80211_chanctx_conf *ctx)
1309 {
1310 	return 0;
1311 }
1312 
1313 static void
1314 mt7921_remove_chanctx(struct ieee80211_hw *hw,
1315 		      struct ieee80211_chanctx_conf *ctx)
1316 {
1317 }
1318 
1319 static void mt7921_ctx_iter(void *priv, u8 *mac,
1320 			    struct ieee80211_vif *vif)
1321 {
1322 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1323 	struct ieee80211_chanctx_conf *ctx = priv;
1324 
1325 	if (ctx != mvif->mt76.ctx)
1326 		return;
1327 
1328 	if (vif->type == NL80211_IFTYPE_MONITOR)
1329 		mt7921_mcu_config_sniffer(mvif, ctx);
1330 	else
1331 		mt76_connac_mcu_uni_set_chctx(mvif->phy->mt76, &mvif->mt76, ctx);
1332 }
1333 
1334 static void
1335 mt7921_change_chanctx(struct ieee80211_hw *hw,
1336 		      struct ieee80211_chanctx_conf *ctx,
1337 		      u32 changed)
1338 {
1339 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1340 
1341 	mt792x_mutex_acquire(phy->dev);
1342 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
1343 					    IEEE80211_IFACE_ITER_ACTIVE,
1344 					    mt7921_ctx_iter, ctx);
1345 	mt792x_mutex_release(phy->dev);
1346 }
1347 
1348 static void mt7921_mgd_prepare_tx(struct ieee80211_hw *hw,
1349 				  struct ieee80211_vif *vif,
1350 				  struct ieee80211_prep_tx_info *info)
1351 {
1352 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1353 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1354 	u16 duration = info->duration ? info->duration :
1355 		       jiffies_to_msecs(HZ);
1356 
1357 	mt792x_mutex_acquire(dev);
1358 	mt7921_set_roc(mvif->phy, mvif, mvif->mt76.ctx->def.chan, duration,
1359 		       MT7921_ROC_REQ_JOIN);
1360 	mt792x_mutex_release(dev);
1361 }
1362 
1363 static void mt7921_mgd_complete_tx(struct ieee80211_hw *hw,
1364 				   struct ieee80211_vif *vif,
1365 				   struct ieee80211_prep_tx_info *info)
1366 {
1367 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1368 
1369 	mt7921_abort_roc(mvif->phy, mvif);
1370 }
1371 
1372 const struct ieee80211_ops mt7921_ops = {
1373 	.tx = mt792x_tx,
1374 	.start = mt7921_start,
1375 	.stop = mt792x_stop,
1376 	.add_interface = mt7921_add_interface,
1377 	.remove_interface = mt792x_remove_interface,
1378 	.config = mt7921_config,
1379 	.conf_tx = mt792x_conf_tx,
1380 	.configure_filter = mt7921_configure_filter,
1381 	.bss_info_changed = mt7921_bss_info_changed,
1382 	.start_ap = mt7921_start_ap,
1383 	.stop_ap = mt7921_stop_ap,
1384 	.sta_state = mt7921_sta_state,
1385 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1386 	.set_key = mt7921_set_key,
1387 	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1388 #if IS_ENABLED(CONFIG_IPV6)
1389 	.ipv6_addr_change = mt7921_ipv6_addr_change,
1390 #endif /* CONFIG_IPV6 */
1391 	.ampdu_action = mt7921_ampdu_action,
1392 	.set_rts_threshold = mt7921_set_rts_threshold,
1393 	.wake_tx_queue = mt76_wake_tx_queue,
1394 	.release_buffered_frames = mt76_release_buffered_frames,
1395 	.channel_switch_beacon = mt7921_channel_switch_beacon,
1396 	.get_txpower = mt76_get_txpower,
1397 	.get_stats = mt792x_get_stats,
1398 	.get_et_sset_count = mt792x_get_et_sset_count,
1399 	.get_et_strings = mt792x_get_et_strings,
1400 	.get_et_stats = mt792x_get_et_stats,
1401 	.get_tsf = mt792x_get_tsf,
1402 	.set_tsf = mt792x_set_tsf,
1403 	.get_survey = mt76_get_survey,
1404 	.get_antenna = mt76_get_antenna,
1405 	.set_antenna = mt7921_set_antenna,
1406 	.set_coverage_class = mt792x_set_coverage_class,
1407 	.hw_scan = mt7921_hw_scan,
1408 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1409 	.sta_statistics = mt792x_sta_statistics,
1410 	.sched_scan_start = mt7921_start_sched_scan,
1411 	.sched_scan_stop = mt7921_stop_sched_scan,
1412 	CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
1413 	CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
1414 #ifdef CONFIG_PM
1415 	.suspend = mt7921_suspend,
1416 	.resume = mt7921_resume,
1417 	.set_wakeup = mt792x_set_wakeup,
1418 	.set_rekey_data = mt7921_set_rekey_data,
1419 #endif /* CONFIG_PM */
1420 	.flush = mt792x_flush,
1421 	.set_sar_specs = mt7921_set_sar_specs,
1422 	.remain_on_channel = mt7921_remain_on_channel,
1423 	.cancel_remain_on_channel = mt7921_cancel_remain_on_channel,
1424 	.add_chanctx = mt7921_add_chanctx,
1425 	.remove_chanctx = mt7921_remove_chanctx,
1426 	.change_chanctx = mt7921_change_chanctx,
1427 	.assign_vif_chanctx = mt792x_assign_vif_chanctx,
1428 	.unassign_vif_chanctx = mt792x_unassign_vif_chanctx,
1429 	.mgd_prepare_tx = mt7921_mgd_prepare_tx,
1430 	.mgd_complete_tx = mt7921_mgd_complete_tx,
1431 };
1432 EXPORT_SYMBOL_GPL(mt7921_ops);
1433 
1434 MODULE_DESCRIPTION("MediaTek MT7921 core driver");
1435 MODULE_LICENSE("Dual BSD/GPL");
1436 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1437