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