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