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