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