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 "mt7921.h"
9 #include "mcu.h"
10 
11 static void
12 mt7921_gen_ppe_thresh(u8 *he_ppet, int nss)
13 {
14 	u8 i, ppet_bits, ppet_size, ru_bit_mask = 0x7; /* HE80 */
15 	u8 ppet16_ppet8_ru3_ru0[] = {0x1c, 0xc7, 0x71};
16 
17 	he_ppet[0] = FIELD_PREP(IEEE80211_PPE_THRES_NSS_MASK, nss - 1) |
18 		     FIELD_PREP(IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK,
19 				ru_bit_mask);
20 
21 	ppet_bits = IEEE80211_PPE_THRES_INFO_PPET_SIZE *
22 		    nss * hweight8(ru_bit_mask) * 2;
23 	ppet_size = DIV_ROUND_UP(ppet_bits, 8);
24 
25 	for (i = 0; i < ppet_size - 1; i++)
26 		he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3];
27 
28 	he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3] &
29 			 (0xff >> (8 - (ppet_bits - 1) % 8));
30 }
31 
32 static int
33 mt7921_init_he_caps(struct mt7921_phy *phy, enum nl80211_band band,
34 		    struct ieee80211_sband_iftype_data *data)
35 {
36 	int i, idx = 0;
37 	int nss = hweight8(phy->mt76->chainmask);
38 	u16 mcs_map = 0;
39 
40 	for (i = 0; i < 8; i++) {
41 		if (i < nss)
42 			mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2));
43 		else
44 			mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2));
45 	}
46 
47 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
48 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
49 		struct ieee80211_he_cap_elem *he_cap_elem =
50 				&he_cap->he_cap_elem;
51 		struct ieee80211_he_mcs_nss_supp *he_mcs =
52 				&he_cap->he_mcs_nss_supp;
53 
54 		switch (i) {
55 		case NL80211_IFTYPE_STATION:
56 			break;
57 		default:
58 			continue;
59 		}
60 
61 		data[idx].types_mask = BIT(i);
62 		he_cap->has_he = true;
63 
64 		he_cap_elem->mac_cap_info[0] =
65 			IEEE80211_HE_MAC_CAP0_HTC_HE;
66 		he_cap_elem->mac_cap_info[3] =
67 			IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
68 			IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3;
69 		he_cap_elem->mac_cap_info[4] =
70 			IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
71 
72 		if (band == NL80211_BAND_2GHZ)
73 			he_cap_elem->phy_cap_info[0] =
74 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
75 		else
76 			he_cap_elem->phy_cap_info[0] =
77 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G;
78 
79 		he_cap_elem->phy_cap_info[1] =
80 			IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD;
81 		he_cap_elem->phy_cap_info[2] =
82 			IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
83 			IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
84 			IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
85 			IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
86 			IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
87 
88 		switch (i) {
89 		case NL80211_IFTYPE_STATION:
90 			he_cap_elem->mac_cap_info[1] |=
91 				IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
92 
93 			if (band == NL80211_BAND_2GHZ)
94 				he_cap_elem->phy_cap_info[0] |=
95 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G;
96 			else
97 				he_cap_elem->phy_cap_info[0] |=
98 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G;
99 
100 			he_cap_elem->phy_cap_info[1] |=
101 				IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
102 				IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
103 			he_cap_elem->phy_cap_info[3] |=
104 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
105 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
106 			he_cap_elem->phy_cap_info[4] |=
107 				IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE |
108 				IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4;
109 			he_cap_elem->phy_cap_info[5] |=
110 				IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK |
111 				IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
112 			he_cap_elem->phy_cap_info[6] |=
113 				IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU |
114 				IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
115 				IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
116 				IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
117 				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
118 			he_cap_elem->phy_cap_info[7] |=
119 				IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
120 				IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI;
121 			he_cap_elem->phy_cap_info[8] |=
122 				IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
123 				IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484;
124 			he_cap_elem->phy_cap_info[9] |=
125 				IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
126 				IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
127 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
128 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
129 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
130 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
131 			break;
132 		}
133 
134 		he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map);
135 		he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map);
136 
137 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
138 		if (he_cap_elem->phy_cap_info[6] &
139 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
140 			mt7921_gen_ppe_thresh(he_cap->ppe_thres, nss);
141 		} else {
142 			he_cap_elem->phy_cap_info[9] |=
143 				IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US;
144 		}
145 
146 		if (band == NL80211_BAND_6GHZ) {
147 			struct ieee80211_supported_band *sband =
148 				&phy->mt76->sband_5g.sband;
149 			struct ieee80211_sta_vht_cap *vht_cap = &sband->vht_cap;
150 			struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
151 			u32 exp;
152 			u16 cap;
153 
154 			cap = u16_encode_bits(ht_cap->ampdu_density,
155 					IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
156 			exp = u32_get_bits(vht_cap->cap,
157 				IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
158 			cap |= u16_encode_bits(exp,
159 					IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
160 			exp = u32_get_bits(vht_cap->cap,
161 					   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
162 			cap |= u16_encode_bits(exp,
163 					IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
164 			if (vht_cap->cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
165 				cap |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
166 			if (vht_cap->cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
167 				cap |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
168 
169 			data->he_6ghz_capa.capa = cpu_to_le16(cap);
170 		}
171 		idx++;
172 	}
173 
174 	return idx;
175 }
176 
177 void mt7921_set_stream_he_caps(struct mt7921_phy *phy)
178 {
179 	struct ieee80211_sband_iftype_data *data;
180 	struct ieee80211_supported_band *band;
181 	int n;
182 
183 	if (phy->mt76->cap.has_2ghz) {
184 		data = phy->iftype[NL80211_BAND_2GHZ];
185 		n = mt7921_init_he_caps(phy, NL80211_BAND_2GHZ, data);
186 
187 		band = &phy->mt76->sband_2g.sband;
188 		band->iftype_data = data;
189 		band->n_iftype_data = n;
190 	}
191 
192 	if (phy->mt76->cap.has_5ghz) {
193 		data = phy->iftype[NL80211_BAND_5GHZ];
194 		n = mt7921_init_he_caps(phy, NL80211_BAND_5GHZ, data);
195 
196 		band = &phy->mt76->sband_5g.sband;
197 		band->iftype_data = data;
198 		band->n_iftype_data = n;
199 
200 		if (phy->mt76->cap.has_6ghz) {
201 			data = phy->iftype[NL80211_BAND_6GHZ];
202 			n = mt7921_init_he_caps(phy, NL80211_BAND_6GHZ, data);
203 
204 			band = &phy->mt76->sband_6g.sband;
205 			band->iftype_data = data;
206 			band->n_iftype_data = n;
207 		}
208 	}
209 }
210 
211 int __mt7921_start(struct mt7921_phy *phy)
212 {
213 	struct mt76_phy *mphy = phy->mt76;
214 	int err;
215 
216 	err = mt76_connac_mcu_set_mac_enable(mphy->dev, 0, true, false);
217 	if (err)
218 		return err;
219 
220 	err = mt76_connac_mcu_set_channel_domain(mphy);
221 	if (err)
222 		return err;
223 
224 	err = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD_SET_RX_PATH);
225 	if (err)
226 		return err;
227 
228 	err = mt76_connac_mcu_set_rate_txpower(phy->mt76);
229 	if (err)
230 		return err;
231 
232 	mt7921_mac_reset_counters(phy);
233 	set_bit(MT76_STATE_RUNNING, &mphy->state);
234 
235 	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
236 				     MT7921_WATCHDOG_TIME);
237 
238 	return 0;
239 }
240 EXPORT_SYMBOL_GPL(__mt7921_start);
241 
242 static int mt7921_start(struct ieee80211_hw *hw)
243 {
244 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
245 	int err;
246 
247 	mt7921_mutex_acquire(phy->dev);
248 	err = __mt7921_start(phy);
249 	mt7921_mutex_release(phy->dev);
250 
251 	return err;
252 }
253 
254 static void mt7921_stop(struct ieee80211_hw *hw)
255 {
256 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
257 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
258 
259 	cancel_delayed_work_sync(&phy->mt76->mac_work);
260 
261 	cancel_delayed_work_sync(&dev->pm.ps_work);
262 	cancel_work_sync(&dev->pm.wake_work);
263 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
264 
265 	mt7921_mutex_acquire(dev);
266 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
267 	mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
268 	mt7921_mutex_release(dev);
269 }
270 
271 static int mt7921_add_interface(struct ieee80211_hw *hw,
272 				struct ieee80211_vif *vif)
273 {
274 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
275 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
276 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
277 	struct mt76_txq *mtxq;
278 	int idx, ret = 0;
279 
280 	mt7921_mutex_acquire(dev);
281 
282 	mvif->mt76.idx = ffs(~dev->mt76.vif_mask) - 1;
283 	if (mvif->mt76.idx >= MT7921_MAX_INTERFACES) {
284 		ret = -ENOSPC;
285 		goto out;
286 	}
287 
288 	mvif->mt76.omac_idx = mvif->mt76.idx;
289 	mvif->phy = phy;
290 	mvif->mt76.band_idx = 0;
291 	mvif->mt76.wmm_idx = mvif->mt76.idx % MT7921_MAX_WMM_SETS;
292 
293 	ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid,
294 					  true);
295 	if (ret)
296 		goto out;
297 
298 	dev->mt76.vif_mask |= BIT(mvif->mt76.idx);
299 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
300 
301 	idx = MT7921_WTBL_RESERVED - mvif->mt76.idx;
302 
303 	INIT_LIST_HEAD(&mvif->sta.poll_list);
304 	mvif->sta.wcid.idx = idx;
305 	mvif->sta.wcid.ext_phy = mvif->mt76.band_idx;
306 	mvif->sta.wcid.hw_key_idx = -1;
307 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
308 	mt76_packet_id_init(&mvif->sta.wcid);
309 
310 	mt7921_mac_wtbl_update(dev, idx,
311 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
312 
313 	ewma_rssi_init(&mvif->rssi);
314 
315 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
316 	if (vif->txq) {
317 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
318 		mtxq->wcid = &mvif->sta.wcid;
319 	}
320 
321 	if (vif->type != NL80211_IFTYPE_AP &&
322 	    (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3))
323 		vif->offload_flags = 0;
324 
325 	vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
326 
327 out:
328 	mt7921_mutex_release(dev);
329 
330 	return ret;
331 }
332 
333 static void mt7921_remove_interface(struct ieee80211_hw *hw,
334 				    struct ieee80211_vif *vif)
335 {
336 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
337 	struct mt7921_sta *msta = &mvif->sta;
338 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
339 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
340 	int idx = msta->wcid.idx;
341 
342 	mt7921_mutex_acquire(dev);
343 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
344 	mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid, false);
345 
346 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
347 
348 	dev->mt76.vif_mask &= ~BIT(mvif->mt76.idx);
349 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
350 	mt7921_mutex_release(dev);
351 
352 	spin_lock_bh(&dev->sta_poll_lock);
353 	if (!list_empty(&msta->poll_list))
354 		list_del_init(&msta->poll_list);
355 	spin_unlock_bh(&dev->sta_poll_lock);
356 
357 	mt76_packet_id_flush(&dev->mt76, &msta->wcid);
358 }
359 
360 static int mt7921_set_channel(struct mt7921_phy *phy)
361 {
362 	struct mt7921_dev *dev = phy->dev;
363 	int ret;
364 
365 	cancel_delayed_work_sync(&phy->mt76->mac_work);
366 
367 	mt7921_mutex_acquire(dev);
368 	set_bit(MT76_RESET, &phy->mt76->state);
369 
370 	mt76_set_channel(phy->mt76);
371 
372 	ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD_CHANNEL_SWITCH);
373 	if (ret)
374 		goto out;
375 
376 	mt7921_mac_set_timing(phy);
377 
378 	mt7921_mac_reset_counters(phy);
379 	phy->noise = 0;
380 
381 out:
382 	clear_bit(MT76_RESET, &phy->mt76->state);
383 	mt7921_mutex_release(dev);
384 
385 	mt76_worker_schedule(&dev->mt76.tx_worker);
386 	ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mt76->mac_work,
387 				     MT7921_WATCHDOG_TIME);
388 
389 	return ret;
390 }
391 
392 static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
393 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
394 			  struct ieee80211_key_conf *key)
395 {
396 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
397 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
398 	struct mt7921_sta *msta = sta ? (struct mt7921_sta *)sta->drv_priv :
399 				  &mvif->sta;
400 	struct mt76_wcid *wcid = &msta->wcid;
401 	u8 *wcid_keyidx = &wcid->hw_key_idx;
402 	int idx = key->keyidx, err = 0;
403 
404 	/* The hardware does not support per-STA RX GTK, fallback
405 	 * to software mode for these.
406 	 */
407 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
408 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
409 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
410 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
411 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
412 		return -EOPNOTSUPP;
413 
414 	/* fall back to sw encryption for unsupported ciphers */
415 	switch (key->cipher) {
416 	case WLAN_CIPHER_SUITE_AES_CMAC:
417 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
418 		wcid_keyidx = &wcid->hw_key_idx2;
419 		break;
420 	case WLAN_CIPHER_SUITE_WEP40:
421 	case WLAN_CIPHER_SUITE_WEP104:
422 		if (!mvif->wep_sta)
423 			return -EOPNOTSUPP;
424 		break;
425 	case WLAN_CIPHER_SUITE_TKIP:
426 	case WLAN_CIPHER_SUITE_CCMP:
427 	case WLAN_CIPHER_SUITE_CCMP_256:
428 	case WLAN_CIPHER_SUITE_GCMP:
429 	case WLAN_CIPHER_SUITE_GCMP_256:
430 	case WLAN_CIPHER_SUITE_SMS4:
431 		break;
432 	default:
433 		return -EOPNOTSUPP;
434 	}
435 
436 	mt7921_mutex_acquire(dev);
437 
438 	if (cmd == SET_KEY)
439 		*wcid_keyidx = idx;
440 	else if (idx == *wcid_keyidx)
441 		*wcid_keyidx = -1;
442 	else
443 		goto out;
444 
445 	mt76_wcid_key_setup(&dev->mt76, wcid,
446 			    cmd == SET_KEY ? key : NULL);
447 
448 	err = mt7921_mcu_add_key(dev, vif, msta, key, cmd);
449 	if (err)
450 		goto out;
451 
452 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
453 	    key->cipher == WLAN_CIPHER_SUITE_WEP40)
454 		err = mt7921_mcu_add_key(dev, vif, mvif->wep_sta, key, cmd);
455 out:
456 	mt7921_mutex_release(dev);
457 
458 	return err;
459 }
460 
461 static int mt7921_config(struct ieee80211_hw *hw, u32 changed)
462 {
463 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
464 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
465 	int ret;
466 
467 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
468 		ieee80211_stop_queues(hw);
469 		ret = mt7921_set_channel(phy);
470 		if (ret)
471 			return ret;
472 		ieee80211_wake_queues(hw);
473 	}
474 
475 	mt7921_mutex_acquire(dev);
476 
477 	if (changed & IEEE80211_CONF_CHANGE_POWER)
478 		mt76_connac_mcu_set_rate_txpower(phy->mt76);
479 
480 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
481 		bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
482 
483 		if (!enabled)
484 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
485 		else
486 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
487 
488 		mt76_rmw_field(dev, MT_DMA_DCR0(0), MT_DMA_DCR0_RXD_G5_EN,
489 			       enabled);
490 		mt76_wr(dev, MT_WF_RFCR(0), phy->rxfilter);
491 	}
492 
493 	mt7921_mutex_release(dev);
494 
495 	return 0;
496 }
497 
498 static int
499 mt7921_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
500 	       const struct ieee80211_tx_queue_params *params)
501 {
502 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
503 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
504 
505 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
506 	queue = mt7921_lmac_mapping(dev, queue);
507 	mvif->queue_params[queue] = *params;
508 
509 	return 0;
510 }
511 
512 static void mt7921_configure_filter(struct ieee80211_hw *hw,
513 				    unsigned int changed_flags,
514 				    unsigned int *total_flags,
515 				    u64 multicast)
516 {
517 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
518 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
519 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
520 			MT_WF_RFCR1_DROP_BF_POLL |
521 			MT_WF_RFCR1_DROP_BA |
522 			MT_WF_RFCR1_DROP_CFEND |
523 			MT_WF_RFCR1_DROP_CFACK;
524 	u32 flags = 0;
525 
526 #define MT76_FILTER(_flag, _hw) do {					\
527 		flags |= *total_flags & FIF_##_flag;			\
528 		phy->rxfilter &= ~(_hw);				\
529 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
530 	} while (0)
531 
532 	mt7921_mutex_acquire(dev);
533 
534 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
535 			   MT_WF_RFCR_DROP_OTHER_BEACON |
536 			   MT_WF_RFCR_DROP_FRAME_REPORT |
537 			   MT_WF_RFCR_DROP_PROBEREQ |
538 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
539 			   MT_WF_RFCR_DROP_MCAST |
540 			   MT_WF_RFCR_DROP_BCAST |
541 			   MT_WF_RFCR_DROP_DUPLICATE |
542 			   MT_WF_RFCR_DROP_A2_BSSID |
543 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
544 			   MT_WF_RFCR_DROP_STBC_MULTI);
545 
546 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
547 			       MT_WF_RFCR_DROP_A3_MAC |
548 			       MT_WF_RFCR_DROP_A3_BSSID);
549 
550 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
551 
552 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
553 			     MT_WF_RFCR_DROP_RTS |
554 			     MT_WF_RFCR_DROP_CTL_RSV |
555 			     MT_WF_RFCR_DROP_NDPA);
556 
557 	*total_flags = flags;
558 	mt76_wr(dev, MT_WF_RFCR(0), phy->rxfilter);
559 
560 	if (*total_flags & FIF_CONTROL)
561 		mt76_clear(dev, MT_WF_RFCR1(0), ctl_flags);
562 	else
563 		mt76_set(dev, MT_WF_RFCR1(0), ctl_flags);
564 
565 	mt7921_mutex_release(dev);
566 }
567 
568 static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
569 				    struct ieee80211_vif *vif,
570 				    struct ieee80211_bss_conf *info,
571 				    u32 changed)
572 {
573 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
574 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
575 
576 	mt7921_mutex_acquire(dev);
577 
578 	if (changed & BSS_CHANGED_ERP_SLOT) {
579 		int slottime = info->use_short_slot ? 9 : 20;
580 
581 		if (slottime != phy->slottime) {
582 			phy->slottime = slottime;
583 			mt7921_mac_set_timing(phy);
584 		}
585 	}
586 
587 	/* ensure that enable txcmd_mode after bss_info */
588 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
589 		mt7921_mcu_set_tx(dev, vif);
590 
591 	if (changed & BSS_CHANGED_PS)
592 		mt7921_mcu_uni_bss_ps(dev, vif);
593 
594 	if (changed & BSS_CHANGED_ASSOC) {
595 		mt7921_mcu_sta_update(dev, NULL, vif, true,
596 				      MT76_STA_INFO_STATE_ASSOC);
597 		if (dev->pm.enable)
598 			mt7921_mcu_set_beacon_filter(dev, vif, info->assoc);
599 	}
600 
601 	if (changed & BSS_CHANGED_ARP_FILTER) {
602 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
603 
604 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
605 						  info);
606 	}
607 
608 	mt7921_mutex_release(dev);
609 }
610 
611 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
612 		       struct ieee80211_sta *sta)
613 {
614 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
615 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
616 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
617 	int ret, idx;
618 
619 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
620 	if (idx < 0)
621 		return -ENOSPC;
622 
623 	INIT_LIST_HEAD(&msta->poll_list);
624 	msta->vif = mvif;
625 	msta->wcid.sta = 1;
626 	msta->wcid.idx = idx;
627 	msta->wcid.ext_phy = mvif->mt76.band_idx;
628 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
629 	msta->last_txs = jiffies;
630 
631 	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
632 	if (ret)
633 		return ret;
634 
635 	if (vif->type == NL80211_IFTYPE_STATION)
636 		mvif->wep_sta = msta;
637 
638 	mt7921_mac_wtbl_update(dev, idx,
639 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
640 
641 	ret = mt7921_mcu_sta_update(dev, sta, vif, true,
642 				    MT76_STA_INFO_STATE_NONE);
643 	if (ret)
644 		return ret;
645 
646 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
647 
648 	return 0;
649 }
650 EXPORT_SYMBOL_GPL(mt7921_mac_sta_add);
651 
652 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
653 			  struct ieee80211_sta *sta)
654 {
655 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
656 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
657 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
658 
659 	mt7921_mutex_acquire(dev);
660 
661 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
662 		mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
663 					    true);
664 
665 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
666 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
667 
668 	mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
669 
670 	mt7921_mutex_release(dev);
671 }
672 EXPORT_SYMBOL_GPL(mt7921_mac_sta_assoc);
673 
674 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
675 			   struct ieee80211_sta *sta)
676 {
677 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
678 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
679 
680 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
681 	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
682 
683 	mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
684 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
685 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
686 
687 	if (vif->type == NL80211_IFTYPE_STATION) {
688 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
689 
690 		mvif->wep_sta = NULL;
691 		ewma_rssi_init(&mvif->rssi);
692 		if (!sta->tdls)
693 			mt76_connac_mcu_uni_add_bss(&dev->mphy, vif,
694 						    &mvif->sta.wcid, false);
695 	}
696 
697 	spin_lock_bh(&dev->sta_poll_lock);
698 	if (!list_empty(&msta->poll_list))
699 		list_del_init(&msta->poll_list);
700 	spin_unlock_bh(&dev->sta_poll_lock);
701 
702 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
703 }
704 EXPORT_SYMBOL_GPL(mt7921_mac_sta_remove);
705 
706 void mt7921_tx_worker(struct mt76_worker *w)
707 {
708 	struct mt7921_dev *dev = container_of(w, struct mt7921_dev,
709 					      mt76.tx_worker);
710 
711 	if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
712 		queue_work(dev->mt76.wq, &dev->pm.wake_work);
713 		return;
714 	}
715 
716 	mt76_txq_schedule_all(&dev->mphy);
717 	mt76_connac_pm_unref(&dev->mphy, &dev->pm);
718 }
719 
720 static void mt7921_tx(struct ieee80211_hw *hw,
721 		      struct ieee80211_tx_control *control,
722 		      struct sk_buff *skb)
723 {
724 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
725 	struct mt76_phy *mphy = hw->priv;
726 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
727 	struct ieee80211_vif *vif = info->control.vif;
728 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
729 	int qid;
730 
731 	if (control->sta) {
732 		struct mt7921_sta *sta;
733 
734 		sta = (struct mt7921_sta *)control->sta->drv_priv;
735 		wcid = &sta->wcid;
736 	}
737 
738 	if (vif && !control->sta) {
739 		struct mt7921_vif *mvif;
740 
741 		mvif = (struct mt7921_vif *)vif->drv_priv;
742 		wcid = &mvif->sta.wcid;
743 	}
744 
745 	if (mt76_connac_pm_ref(mphy, &dev->pm)) {
746 		mt76_tx(mphy, control->sta, wcid, skb);
747 		mt76_connac_pm_unref(mphy, &dev->pm);
748 		return;
749 	}
750 
751 	qid = skb_get_queue_mapping(skb);
752 	if (qid >= MT_TXQ_PSD) {
753 		qid = IEEE80211_AC_BE;
754 		skb_set_queue_mapping(skb, qid);
755 	}
756 
757 	mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
758 }
759 
760 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
761 {
762 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
763 
764 	mt7921_mutex_acquire(dev);
765 	mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
766 	mt7921_mutex_release(dev);
767 
768 	return 0;
769 }
770 
771 static int
772 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
773 		    struct ieee80211_ampdu_params *params)
774 {
775 	enum ieee80211_ampdu_mlme_action action = params->action;
776 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
777 	struct ieee80211_sta *sta = params->sta;
778 	struct ieee80211_txq *txq = sta->txq[params->tid];
779 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
780 	u16 tid = params->tid;
781 	u16 ssn = params->ssn;
782 	struct mt76_txq *mtxq;
783 	int ret = 0;
784 
785 	if (!txq)
786 		return -EINVAL;
787 
788 	mtxq = (struct mt76_txq *)txq->drv_priv;
789 
790 	mt7921_mutex_acquire(dev);
791 	switch (action) {
792 	case IEEE80211_AMPDU_RX_START:
793 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
794 				   params->buf_size);
795 		mt7921_mcu_uni_rx_ba(dev, params, true);
796 		break;
797 	case IEEE80211_AMPDU_RX_STOP:
798 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
799 		mt7921_mcu_uni_rx_ba(dev, params, false);
800 		break;
801 	case IEEE80211_AMPDU_TX_OPERATIONAL:
802 		mtxq->aggr = true;
803 		mtxq->send_bar = false;
804 		mt7921_mcu_uni_tx_ba(dev, params, true);
805 		break;
806 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
807 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
808 		mtxq->aggr = false;
809 		clear_bit(tid, &msta->ampdu_state);
810 		mt7921_mcu_uni_tx_ba(dev, params, false);
811 		break;
812 	case IEEE80211_AMPDU_TX_START:
813 		set_bit(tid, &msta->ampdu_state);
814 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
815 		break;
816 	case IEEE80211_AMPDU_TX_STOP_CONT:
817 		mtxq->aggr = false;
818 		clear_bit(tid, &msta->ampdu_state);
819 		mt7921_mcu_uni_tx_ba(dev, params, false);
820 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
821 		break;
822 	}
823 	mt7921_mutex_release(dev);
824 
825 	return ret;
826 }
827 
828 static int mt7921_sta_state(struct ieee80211_hw *hw,
829 			    struct ieee80211_vif *vif,
830 			    struct ieee80211_sta *sta,
831 			    enum ieee80211_sta_state old_state,
832 			    enum ieee80211_sta_state new_state)
833 {
834 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
835 
836 	if (dev->pm.ds_enable) {
837 		mt7921_mutex_acquire(dev);
838 		mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state);
839 		mt7921_mutex_release(dev);
840 	}
841 
842 	return mt76_sta_state(hw, vif, sta, old_state, new_state);
843 }
844 
845 static int
846 mt7921_get_stats(struct ieee80211_hw *hw,
847 		 struct ieee80211_low_level_stats *stats)
848 {
849 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
850 	struct mib_stats *mib = &phy->mib;
851 
852 	mt7921_mutex_acquire(phy->dev);
853 
854 	stats->dot11RTSSuccessCount = mib->rts_cnt;
855 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
856 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
857 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
858 
859 	mt7921_mutex_release(phy->dev);
860 
861 	return 0;
862 }
863 
864 static const char mt7921_gstrings_stats[][ETH_GSTRING_LEN] = {
865 	/* tx counters */
866 	"tx_ampdu_cnt",
867 	"tx_mpdu_attempts",
868 	"tx_mpdu_success",
869 	"tx_pkt_ebf_cnt",
870 	"tx_pkt_ibf_cnt",
871 	"tx_ampdu_len:0-1",
872 	"tx_ampdu_len:2-10",
873 	"tx_ampdu_len:11-19",
874 	"tx_ampdu_len:20-28",
875 	"tx_ampdu_len:29-37",
876 	"tx_ampdu_len:38-46",
877 	"tx_ampdu_len:47-55",
878 	"tx_ampdu_len:56-79",
879 	"tx_ampdu_len:80-103",
880 	"tx_ampdu_len:104-127",
881 	"tx_ampdu_len:128-151",
882 	"tx_ampdu_len:152-175",
883 	"tx_ampdu_len:176-199",
884 	"tx_ampdu_len:200-223",
885 	"tx_ampdu_len:224-247",
886 	"ba_miss_count",
887 	"tx_beamformer_ppdu_iBF",
888 	"tx_beamformer_ppdu_eBF",
889 	"tx_beamformer_rx_feedback_all",
890 	"tx_beamformer_rx_feedback_he",
891 	"tx_beamformer_rx_feedback_vht",
892 	"tx_beamformer_rx_feedback_ht",
893 	"tx_msdu_pack_1",
894 	"tx_msdu_pack_2",
895 	"tx_msdu_pack_3",
896 	"tx_msdu_pack_4",
897 	"tx_msdu_pack_5",
898 	"tx_msdu_pack_6",
899 	"tx_msdu_pack_7",
900 	"tx_msdu_pack_8",
901 	/* rx counters */
902 	"rx_mpdu_cnt",
903 	"rx_ampdu_cnt",
904 	"rx_ampdu_bytes_cnt",
905 	"rx_ba_cnt",
906 	/* per vif counters */
907 	"v_tx_mode_cck",
908 	"v_tx_mode_ofdm",
909 	"v_tx_mode_ht",
910 	"v_tx_mode_ht_gf",
911 	"v_tx_mode_vht",
912 	"v_tx_mode_he_su",
913 	"v_tx_mode_he_ext_su",
914 	"v_tx_mode_he_tb",
915 	"v_tx_mode_he_mu",
916 	"v_tx_bw_20",
917 	"v_tx_bw_40",
918 	"v_tx_bw_80",
919 	"v_tx_bw_160",
920 	"v_tx_mcs_0",
921 	"v_tx_mcs_1",
922 	"v_tx_mcs_2",
923 	"v_tx_mcs_3",
924 	"v_tx_mcs_4",
925 	"v_tx_mcs_5",
926 	"v_tx_mcs_6",
927 	"v_tx_mcs_7",
928 	"v_tx_mcs_8",
929 	"v_tx_mcs_9",
930 	"v_tx_mcs_10",
931 	"v_tx_mcs_11",
932 };
933 
934 static void
935 mt7921_get_et_strings(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
936 		      u32 sset, u8 *data)
937 {
938 	if (sset != ETH_SS_STATS)
939 		return;
940 
941 	memcpy(data, *mt7921_gstrings_stats, sizeof(mt7921_gstrings_stats));
942 }
943 
944 static int
945 mt7921_get_et_sset_count(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
946 			 int sset)
947 {
948 	return sset == ETH_SS_STATS ? ARRAY_SIZE(mt7921_gstrings_stats) : 0;
949 }
950 
951 static void
952 mt7921_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
953 {
954 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
955 	struct mt76_ethtool_worker_info *wi = wi_data;
956 
957 	if (msta->vif->mt76.idx != wi->idx)
958 		return;
959 
960 	mt76_ethtool_worker(wi, &msta->stats);
961 }
962 
963 static
964 void mt7921_get_et_stats(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
965 			 struct ethtool_stats *stats, u64 *data)
966 {
967 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
968 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
969 	struct mt7921_dev *dev = phy->dev;
970 	struct mib_stats *mib = &phy->mib;
971 	struct mt76_ethtool_worker_info wi = {
972 		.data = data,
973 		.idx = mvif->mt76.idx,
974 	};
975 	int i, ei = 0;
976 
977 	mt7921_mutex_acquire(dev);
978 
979 	mt7921_mac_update_mib_stats(phy);
980 
981 	data[ei++] = mib->tx_ampdu_cnt;
982 	data[ei++] = mib->tx_mpdu_attempts_cnt;
983 	data[ei++] = mib->tx_mpdu_success_cnt;
984 	data[ei++] = mib->tx_pkt_ebf_cnt;
985 	data[ei++] = mib->tx_pkt_ibf_cnt;
986 
987 	/* Tx ampdu stat */
988 	for (i = 0; i < 15; i++)
989 		data[ei++] = dev->mt76.aggr_stats[i];
990 
991 	data[ei++] = phy->mib.ba_miss_cnt;
992 
993 	/* Tx Beamformer monitor */
994 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
995 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
996 
997 	/* Tx Beamformer Rx feedback monitor */
998 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
999 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1000 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1001 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1002 
1003 	/* Tx amsdu info (pack-count histogram) */
1004 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1005 		data[ei++] = mib->tx_amsdu[i];
1006 
1007 	/* rx counters */
1008 	data[ei++] = mib->rx_mpdu_cnt;
1009 	data[ei++] = mib->rx_ampdu_cnt;
1010 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1011 	data[ei++] = mib->rx_ba_cnt;
1012 
1013 	/* Add values for all stations owned by this vif */
1014 	wi.initial_stat_idx = ei;
1015 	ieee80211_iterate_stations_atomic(hw, mt7921_ethtool_worker, &wi);
1016 
1017 	mt7921_mutex_release(dev);
1018 
1019 	if (!wi.sta_count)
1020 		return;
1021 
1022 	ei += wi.worker_stat_count;
1023 	if (ei != ARRAY_SIZE(mt7921_gstrings_stats))
1024 		dev_err(dev->mt76.dev, "ei: %d  SSTATS_LEN: %zu",
1025 			ei, ARRAY_SIZE(mt7921_gstrings_stats));
1026 }
1027 
1028 static u64
1029 mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1030 {
1031 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1032 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1033 	u8 omac_idx = mvif->mt76.omac_idx;
1034 	union {
1035 		u64 t64;
1036 		u32 t32[2];
1037 	} tsf;
1038 	u16 n;
1039 
1040 	mt7921_mutex_acquire(dev);
1041 
1042 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1043 	/* TSF software read */
1044 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_MODE);
1045 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(0));
1046 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(0));
1047 
1048 	mt7921_mutex_release(dev);
1049 
1050 	return tsf.t64;
1051 }
1052 
1053 static void
1054 mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1055 	       u64 timestamp)
1056 {
1057 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1058 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1059 	u8 omac_idx = mvif->mt76.omac_idx;
1060 	union {
1061 		u64 t64;
1062 		u32 t32[2];
1063 	} tsf = { .t64 = timestamp, };
1064 	u16 n;
1065 
1066 	mt7921_mutex_acquire(dev);
1067 
1068 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1069 	mt76_wr(dev, MT_LPON_UTTR0(0), tsf.t32[0]);
1070 	mt76_wr(dev, MT_LPON_UTTR1(0), tsf.t32[1]);
1071 	/* TSF software overwrite */
1072 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_WRITE);
1073 
1074 	mt7921_mutex_release(dev);
1075 }
1076 
1077 static void
1078 mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
1079 {
1080 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1081 	struct mt7921_dev *dev = phy->dev;
1082 
1083 	mt7921_mutex_acquire(dev);
1084 	phy->coverage_class = max_t(s16, coverage_class, 0);
1085 	mt7921_mac_set_timing(phy);
1086 	mt7921_mutex_release(dev);
1087 }
1088 
1089 void mt7921_scan_work(struct work_struct *work)
1090 {
1091 	struct mt7921_phy *phy;
1092 
1093 	phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy,
1094 						scan_work.work);
1095 
1096 	while (true) {
1097 		struct mt7921_mcu_rxd *rxd;
1098 		struct sk_buff *skb;
1099 
1100 		spin_lock_bh(&phy->dev->mt76.lock);
1101 		skb = __skb_dequeue(&phy->scan_event_list);
1102 		spin_unlock_bh(&phy->dev->mt76.lock);
1103 
1104 		if (!skb)
1105 			break;
1106 
1107 		rxd = (struct mt7921_mcu_rxd *)skb->data;
1108 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1109 			ieee80211_sched_scan_results(phy->mt76->hw);
1110 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
1111 					      &phy->mt76->state)) {
1112 			struct cfg80211_scan_info info = {
1113 				.aborted = false,
1114 			};
1115 
1116 			ieee80211_scan_completed(phy->mt76->hw, &info);
1117 		}
1118 		dev_kfree_skb(skb);
1119 	}
1120 }
1121 
1122 static int
1123 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1124 	       struct ieee80211_scan_request *req)
1125 {
1126 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1127 	struct mt76_phy *mphy = hw->priv;
1128 	int err;
1129 
1130 	mt7921_mutex_acquire(dev);
1131 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1132 	mt7921_mutex_release(dev);
1133 
1134 	return err;
1135 }
1136 
1137 static void
1138 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1139 {
1140 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1141 	struct mt76_phy *mphy = hw->priv;
1142 
1143 	mt7921_mutex_acquire(dev);
1144 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1145 	mt7921_mutex_release(dev);
1146 }
1147 
1148 static int
1149 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1150 			struct cfg80211_sched_scan_request *req,
1151 			struct ieee80211_scan_ies *ies)
1152 {
1153 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1154 	struct mt76_phy *mphy = hw->priv;
1155 	int err;
1156 
1157 	mt7921_mutex_acquire(dev);
1158 
1159 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1160 	if (err < 0)
1161 		goto out;
1162 
1163 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1164 out:
1165 	mt7921_mutex_release(dev);
1166 
1167 	return err;
1168 }
1169 
1170 static int
1171 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1172 {
1173 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1174 	struct mt76_phy *mphy = hw->priv;
1175 	int err;
1176 
1177 	mt7921_mutex_acquire(dev);
1178 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1179 	mt7921_mutex_release(dev);
1180 
1181 	return err;
1182 }
1183 
1184 static int
1185 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1186 {
1187 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1188 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1189 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1190 
1191 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1192 		return -EINVAL;
1193 
1194 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1195 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1196 
1197 	mt7921_mutex_acquire(dev);
1198 
1199 	phy->mt76->antenna_mask = tx_ant;
1200 	phy->mt76->chainmask = tx_ant;
1201 
1202 	mt76_set_stream_caps(phy->mt76, true);
1203 	mt7921_set_stream_he_caps(phy);
1204 
1205 	mt7921_mutex_release(dev);
1206 
1207 	return 0;
1208 }
1209 
1210 static void mt7921_sta_statistics(struct ieee80211_hw *hw,
1211 				  struct ieee80211_vif *vif,
1212 				  struct ieee80211_sta *sta,
1213 				  struct station_info *sinfo)
1214 {
1215 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1216 	struct rate_info *txrate = &msta->wcid.rate;
1217 
1218 	if (!txrate->legacy && !txrate->flags)
1219 		return;
1220 
1221 	if (txrate->legacy) {
1222 		sinfo->txrate.legacy = txrate->legacy;
1223 	} else {
1224 		sinfo->txrate.mcs = txrate->mcs;
1225 		sinfo->txrate.nss = txrate->nss;
1226 		sinfo->txrate.bw = txrate->bw;
1227 		sinfo->txrate.he_gi = txrate->he_gi;
1228 		sinfo->txrate.he_dcm = txrate->he_dcm;
1229 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1230 	}
1231 	sinfo->txrate.flags = txrate->flags;
1232 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1233 }
1234 
1235 #ifdef CONFIG_PM
1236 static int mt7921_suspend(struct ieee80211_hw *hw,
1237 			  struct cfg80211_wowlan *wowlan)
1238 {
1239 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1240 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1241 	int err;
1242 
1243 	cancel_delayed_work_sync(&phy->scan_work);
1244 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1245 
1246 	cancel_delayed_work_sync(&dev->pm.ps_work);
1247 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1248 
1249 	mt7921_mutex_acquire(dev);
1250 
1251 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1252 
1253 	set_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1254 	ieee80211_iterate_active_interfaces(hw,
1255 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1256 					    mt76_connac_mcu_set_suspend_iter,
1257 					    &dev->mphy);
1258 
1259 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
1260 
1261 	mt7921_mutex_release(dev);
1262 
1263 	return err;
1264 }
1265 
1266 static int mt7921_resume(struct ieee80211_hw *hw)
1267 {
1268 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1269 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1270 	int err;
1271 
1272 	mt7921_mutex_acquire(dev);
1273 
1274 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
1275 	if (err < 0)
1276 		goto out;
1277 
1278 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1279 	clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1280 	ieee80211_iterate_active_interfaces(hw,
1281 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1282 					    mt76_connac_mcu_set_suspend_iter,
1283 					    &dev->mphy);
1284 
1285 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1286 				     MT7921_WATCHDOG_TIME);
1287 out:
1288 
1289 	mt7921_mutex_release(dev);
1290 
1291 	return err;
1292 }
1293 
1294 static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1295 {
1296 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1297 	struct mt76_dev *mdev = &dev->mt76;
1298 
1299 	device_set_wakeup_enable(mdev->dev, enabled);
1300 }
1301 
1302 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1303 				  struct ieee80211_vif *vif,
1304 				  struct cfg80211_gtk_rekey_data *data)
1305 {
1306 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1307 
1308 	mt7921_mutex_acquire(dev);
1309 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1310 	mt7921_mutex_release(dev);
1311 }
1312 #endif /* CONFIG_PM */
1313 
1314 static void mt7921_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1315 			 u32 queues, bool drop)
1316 {
1317 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1318 
1319 	wait_event_timeout(dev->mt76.tx_wait, !mt76_has_tx_pending(&dev->mphy),
1320 			   HZ / 2);
1321 }
1322 
1323 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1324 					 struct ieee80211_vif *vif,
1325 					 struct ieee80211_sta *sta,
1326 					 bool enabled)
1327 {
1328 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1329 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1330 
1331 	if (enabled)
1332 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1333 	else
1334 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1335 
1336 	mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->wcid,
1337 					     MCU_UNI_CMD_STA_REC_UPDATE);
1338 }
1339 
1340 static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
1341 				const struct cfg80211_sar_specs *sar)
1342 {
1343 	const struct cfg80211_sar_capa *capa = hw->wiphy->sar_capa;
1344 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1345 	struct mt76_freq_range_power *data, *frp;
1346 	struct mt76_phy *mphy = hw->priv;
1347 	int err;
1348 	u32 i;
1349 
1350 	if (sar->type != NL80211_SAR_TYPE_POWER || !sar->num_sub_specs)
1351 		return -EINVAL;
1352 
1353 	mt7921_mutex_acquire(dev);
1354 
1355 	data = mphy->frp;
1356 
1357 	for (i = 0; i < sar->num_sub_specs; i++) {
1358 		u32 index = sar->sub_specs[i].freq_range_index;
1359 		/* SAR specifies power limitaton in 0.25dbm */
1360 		s32 power = sar->sub_specs[i].power >> 1;
1361 
1362 		if (power > 127 || power < -127)
1363 			power = 127;
1364 
1365 		frp = &data[index];
1366 		frp->range = &capa->freq_ranges[index];
1367 		frp->power = power;
1368 	}
1369 
1370 	err = mt76_connac_mcu_set_rate_txpower(mphy);
1371 
1372 	mt7921_mutex_release(dev);
1373 
1374 	return err;
1375 }
1376 
1377 const struct ieee80211_ops mt7921_ops = {
1378 	.tx = mt7921_tx,
1379 	.start = mt7921_start,
1380 	.stop = mt7921_stop,
1381 	.add_interface = mt7921_add_interface,
1382 	.remove_interface = mt7921_remove_interface,
1383 	.config = mt7921_config,
1384 	.conf_tx = mt7921_conf_tx,
1385 	.configure_filter = mt7921_configure_filter,
1386 	.bss_info_changed = mt7921_bss_info_changed,
1387 	.sta_state = mt7921_sta_state,
1388 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1389 	.set_key = mt7921_set_key,
1390 	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1391 	.ampdu_action = mt7921_ampdu_action,
1392 	.set_rts_threshold = mt7921_set_rts_threshold,
1393 	.wake_tx_queue = mt76_wake_tx_queue,
1394 	.release_buffered_frames = mt76_release_buffered_frames,
1395 	.get_txpower = mt76_get_txpower,
1396 	.get_stats = mt7921_get_stats,
1397 	.get_et_sset_count = mt7921_get_et_sset_count,
1398 	.get_et_strings = mt7921_get_et_strings,
1399 	.get_et_stats = mt7921_get_et_stats,
1400 	.get_tsf = mt7921_get_tsf,
1401 	.set_tsf = mt7921_set_tsf,
1402 	.get_survey = mt76_get_survey,
1403 	.get_antenna = mt76_get_antenna,
1404 	.set_antenna = mt7921_set_antenna,
1405 	.set_coverage_class = mt7921_set_coverage_class,
1406 	.hw_scan = mt7921_hw_scan,
1407 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1408 	.sta_statistics = mt7921_sta_statistics,
1409 	.sched_scan_start = mt7921_start_sched_scan,
1410 	.sched_scan_stop = mt7921_stop_sched_scan,
1411 	CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
1412 	CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
1413 #ifdef CONFIG_PM
1414 	.suspend = mt7921_suspend,
1415 	.resume = mt7921_resume,
1416 	.set_wakeup = mt7921_set_wakeup,
1417 	.set_rekey_data = mt7921_set_rekey_data,
1418 #endif /* CONFIG_PM */
1419 	.flush = mt7921_flush,
1420 	.set_sar_specs = mt7921_set_sar_specs,
1421 };
1422 EXPORT_SYMBOL_GPL(mt7921_ops);
1423 
1424 MODULE_LICENSE("Dual BSD/GPL");
1425 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1426