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_RESERVED;
69 		he_cap_elem->mac_cap_info[4] =
70 			IEEE80211_HE_MAC_CAP4_AMDSU_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 if (band == NL80211_BAND_5GHZ)
76 			he_cap_elem->phy_cap_info[0] =
77 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
78 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
79 
80 		he_cap_elem->phy_cap_info[1] =
81 			IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD;
82 		he_cap_elem->phy_cap_info[2] =
83 			IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
84 			IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ;
85 
86 		switch (i) {
87 		case NL80211_IFTYPE_STATION:
88 			he_cap_elem->mac_cap_info[0] |=
89 				IEEE80211_HE_MAC_CAP0_TWT_REQ;
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 if (band == NL80211_BAND_5GHZ)
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[6] |=
107 				IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
108 				IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
109 				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
110 			he_cap_elem->phy_cap_info[7] |=
111 				IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_AR |
112 				IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI;
113 			he_cap_elem->phy_cap_info[8] |=
114 				IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
115 				IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484;
116 			he_cap_elem->phy_cap_info[9] |=
117 				IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
118 				IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
119 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
120 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
121 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
122 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
123 			break;
124 		}
125 
126 		he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map);
127 		he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map);
128 		he_mcs->rx_mcs_160 = cpu_to_le16(mcs_map);
129 		he_mcs->tx_mcs_160 = cpu_to_le16(mcs_map);
130 		he_mcs->rx_mcs_80p80 = cpu_to_le16(mcs_map);
131 		he_mcs->tx_mcs_80p80 = cpu_to_le16(mcs_map);
132 
133 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
134 		if (he_cap_elem->phy_cap_info[6] &
135 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
136 			mt7921_gen_ppe_thresh(he_cap->ppe_thres, nss);
137 		} else {
138 			he_cap_elem->phy_cap_info[9] |=
139 				IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US;
140 		}
141 		idx++;
142 	}
143 
144 	return idx;
145 }
146 
147 void mt7921_set_stream_he_caps(struct mt7921_phy *phy)
148 {
149 	struct ieee80211_sband_iftype_data *data;
150 	struct ieee80211_supported_band *band;
151 	int n;
152 
153 	if (phy->mt76->cap.has_2ghz) {
154 		data = phy->iftype[NL80211_BAND_2GHZ];
155 		n = mt7921_init_he_caps(phy, NL80211_BAND_2GHZ, data);
156 
157 		band = &phy->mt76->sband_2g.sband;
158 		band->iftype_data = data;
159 		band->n_iftype_data = n;
160 	}
161 
162 	if (phy->mt76->cap.has_5ghz) {
163 		data = phy->iftype[NL80211_BAND_5GHZ];
164 		n = mt7921_init_he_caps(phy, NL80211_BAND_5GHZ, data);
165 
166 		band = &phy->mt76->sband_5g.sband;
167 		band->iftype_data = data;
168 		band->n_iftype_data = n;
169 	}
170 }
171 
172 static int mt7921_start(struct ieee80211_hw *hw)
173 {
174 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
175 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
176 
177 	mt7921_mutex_acquire(dev);
178 
179 	mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, true, false);
180 	mt76_connac_mcu_set_channel_domain(phy->mt76);
181 
182 	mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD_SET_RX_PATH);
183 	mt7921_mac_reset_counters(phy);
184 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
185 
186 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
187 				     MT7921_WATCHDOG_TIME);
188 
189 	mt7921_mutex_release(dev);
190 
191 	return 0;
192 }
193 
194 static void mt7921_stop(struct ieee80211_hw *hw)
195 {
196 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
197 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
198 
199 	cancel_delayed_work_sync(&phy->mt76->mac_work);
200 
201 	cancel_delayed_work_sync(&dev->pm.ps_work);
202 	cancel_work_sync(&dev->pm.wake_work);
203 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
204 
205 	mt7921_mutex_acquire(dev);
206 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
207 	mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
208 	mt7921_mutex_release(dev);
209 }
210 
211 static inline int get_free_idx(u32 mask, u8 start, u8 end)
212 {
213 	return ffs(~mask & GENMASK(end, start));
214 }
215 
216 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
217 {
218 	int i;
219 
220 	switch (type) {
221 	case NL80211_IFTYPE_STATION:
222 		/* prefer hw bssid slot 1-3 */
223 		i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
224 		if (i)
225 			return i - 1;
226 
227 		if (type != NL80211_IFTYPE_STATION)
228 			break;
229 
230 		/* next, try to find a free repeater entry for the sta */
231 		i = get_free_idx(mask >> REPEATER_BSSID_START, 0,
232 				 REPEATER_BSSID_MAX - REPEATER_BSSID_START);
233 		if (i)
234 			return i + 32 - 1;
235 
236 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
237 		if (i)
238 			return i - 1;
239 
240 		if (~mask & BIT(HW_BSSID_0))
241 			return HW_BSSID_0;
242 
243 		break;
244 	case NL80211_IFTYPE_MONITOR:
245 		/* ap uses hw bssid 0 and ext bssid */
246 		if (~mask & BIT(HW_BSSID_0))
247 			return HW_BSSID_0;
248 
249 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
250 		if (i)
251 			return i - 1;
252 
253 		break;
254 	default:
255 		WARN_ON(1);
256 		break;
257 	}
258 
259 	return -1;
260 }
261 
262 static int mt7921_add_interface(struct ieee80211_hw *hw,
263 				struct ieee80211_vif *vif)
264 {
265 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
266 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
267 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
268 	struct mt76_txq *mtxq;
269 	int idx, ret = 0;
270 
271 	mt7921_mutex_acquire(dev);
272 
273 	if (vif->type == NL80211_IFTYPE_MONITOR &&
274 	    is_zero_ether_addr(vif->addr))
275 		phy->monitor_vif = vif;
276 
277 	mvif->mt76.idx = ffs(~dev->mt76.vif_mask) - 1;
278 	if (mvif->mt76.idx >= MT7921_MAX_INTERFACES) {
279 		ret = -ENOSPC;
280 		goto out;
281 	}
282 
283 	idx = get_omac_idx(vif->type, phy->omac_mask);
284 	if (idx < 0) {
285 		ret = -ENOSPC;
286 		goto out;
287 	}
288 	mvif->mt76.omac_idx = 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 	if (dev->pm.enable) {
299 		ret = mt7921_mcu_set_bss_pm(dev, vif, true);
300 		if (ret)
301 			goto out;
302 
303 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
304 		mt76_set(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
305 	}
306 
307 	dev->mt76.vif_mask |= BIT(mvif->mt76.idx);
308 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
309 
310 	idx = MT7921_WTBL_RESERVED - mvif->mt76.idx;
311 
312 	INIT_LIST_HEAD(&mvif->sta.stats_list);
313 	INIT_LIST_HEAD(&mvif->sta.poll_list);
314 	mvif->sta.wcid.idx = idx;
315 	mvif->sta.wcid.ext_phy = mvif->mt76.band_idx;
316 	mvif->sta.wcid.hw_key_idx = -1;
317 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
318 	mt7921_mac_wtbl_update(dev, idx,
319 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
320 
321 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
322 	if (vif->txq) {
323 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
324 		mtxq->wcid = &mvif->sta.wcid;
325 	}
326 
327 	if (vif->type != NL80211_IFTYPE_AP &&
328 	    (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3))
329 		vif->offload_flags = 0;
330 
331 	vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
332 
333 out:
334 	mt7921_mutex_release(dev);
335 
336 	return ret;
337 }
338 
339 static void mt7921_remove_interface(struct ieee80211_hw *hw,
340 				    struct ieee80211_vif *vif)
341 {
342 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
343 	struct mt7921_sta *msta = &mvif->sta;
344 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
345 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
346 	int idx = msta->wcid.idx;
347 
348 	if (vif == phy->monitor_vif)
349 		phy->monitor_vif = NULL;
350 
351 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
352 
353 	if (dev->pm.enable) {
354 		mt7921_mcu_set_bss_pm(dev, vif, false);
355 		mt76_clear(dev, MT_WF_RFCR(0),
356 			   MT_WF_RFCR_DROP_OTHER_BEACON);
357 	}
358 
359 	mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid, false);
360 
361 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
362 
363 	mt7921_mutex_acquire(dev);
364 	dev->mt76.vif_mask &= ~BIT(mvif->mt76.idx);
365 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
366 	mt7921_mutex_release(dev);
367 
368 	spin_lock_bh(&dev->sta_poll_lock);
369 	if (!list_empty(&msta->poll_list))
370 		list_del_init(&msta->poll_list);
371 	spin_unlock_bh(&dev->sta_poll_lock);
372 }
373 
374 int mt7921_set_channel(struct mt7921_phy *phy)
375 {
376 	struct mt7921_dev *dev = phy->dev;
377 	int ret;
378 
379 	cancel_delayed_work_sync(&phy->mt76->mac_work);
380 
381 	mt7921_mutex_acquire(dev);
382 	set_bit(MT76_RESET, &phy->mt76->state);
383 
384 	mt76_set_channel(phy->mt76);
385 
386 	ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD_CHANNEL_SWITCH);
387 	if (ret)
388 		goto out;
389 
390 	mt7921_mac_set_timing(phy);
391 
392 	mt7921_mac_reset_counters(phy);
393 	phy->noise = 0;
394 
395 out:
396 	clear_bit(MT76_RESET, &phy->mt76->state);
397 	mt7921_mutex_release(dev);
398 
399 	mt76_txq_schedule_all(phy->mt76);
400 
401 	ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mt76->mac_work,
402 				     MT7921_WATCHDOG_TIME);
403 
404 	return ret;
405 }
406 
407 static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
408 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
409 			  struct ieee80211_key_conf *key)
410 {
411 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
412 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
413 	struct mt7921_sta *msta = sta ? (struct mt7921_sta *)sta->drv_priv :
414 				  &mvif->sta;
415 	struct mt76_wcid *wcid = &msta->wcid;
416 	int idx = key->keyidx;
417 
418 	/* The hardware does not support per-STA RX GTK, fallback
419 	 * to software mode for these.
420 	 */
421 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
422 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
423 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
424 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
425 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
426 		return -EOPNOTSUPP;
427 
428 	/* fall back to sw encryption for unsupported ciphers */
429 	switch (key->cipher) {
430 	case WLAN_CIPHER_SUITE_AES_CMAC:
431 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
432 		break;
433 	case WLAN_CIPHER_SUITE_TKIP:
434 	case WLAN_CIPHER_SUITE_CCMP:
435 	case WLAN_CIPHER_SUITE_CCMP_256:
436 	case WLAN_CIPHER_SUITE_GCMP:
437 	case WLAN_CIPHER_SUITE_GCMP_256:
438 	case WLAN_CIPHER_SUITE_SMS4:
439 		break;
440 	case WLAN_CIPHER_SUITE_WEP40:
441 	case WLAN_CIPHER_SUITE_WEP104:
442 	default:
443 		return -EOPNOTSUPP;
444 	}
445 
446 	if (cmd == SET_KEY) {
447 		key->hw_key_idx = wcid->idx;
448 		wcid->hw_key_idx = idx;
449 	} else if (idx == wcid->hw_key_idx) {
450 		wcid->hw_key_idx = -1;
451 	}
452 	mt76_wcid_key_setup(&dev->mt76, wcid,
453 			    cmd == SET_KEY ? key : NULL);
454 
455 	return mt7921_mcu_add_key(dev, vif, msta, key, cmd);
456 }
457 
458 static int mt7921_config(struct ieee80211_hw *hw, u32 changed)
459 {
460 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
461 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
462 	bool band = phy != &dev->phy;
463 	int ret;
464 
465 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
466 		ieee80211_stop_queues(hw);
467 		ret = mt7921_set_channel(phy);
468 		if (ret)
469 			return ret;
470 		ieee80211_wake_queues(hw);
471 	}
472 
473 	mt7921_mutex_acquire(dev);
474 
475 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
476 		bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
477 
478 		if (!enabled)
479 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
480 		else
481 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
482 
483 		mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN,
484 			       enabled);
485 		mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
486 	}
487 
488 	mt7921_mutex_release(dev);
489 
490 	return 0;
491 }
492 
493 static int
494 mt7921_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
495 	       const struct ieee80211_tx_queue_params *params)
496 {
497 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
498 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
499 
500 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
501 	queue = mt7921_lmac_mapping(dev, queue);
502 	mvif->queue_params[queue] = *params;
503 
504 	return 0;
505 }
506 
507 static void mt7921_configure_filter(struct ieee80211_hw *hw,
508 				    unsigned int changed_flags,
509 				    unsigned int *total_flags,
510 				    u64 multicast)
511 {
512 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
513 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
514 	bool band = phy != &dev->phy;
515 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
516 			MT_WF_RFCR1_DROP_BF_POLL |
517 			MT_WF_RFCR1_DROP_BA |
518 			MT_WF_RFCR1_DROP_CFEND |
519 			MT_WF_RFCR1_DROP_CFACK;
520 	u32 flags = 0;
521 
522 #define MT76_FILTER(_flag, _hw) do {					\
523 		flags |= *total_flags & FIF_##_flag;			\
524 		phy->rxfilter &= ~(_hw);				\
525 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
526 	} while (0)
527 
528 	mt7921_mutex_acquire(dev);
529 
530 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
531 			   MT_WF_RFCR_DROP_OTHER_BEACON |
532 			   MT_WF_RFCR_DROP_FRAME_REPORT |
533 			   MT_WF_RFCR_DROP_PROBEREQ |
534 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
535 			   MT_WF_RFCR_DROP_MCAST |
536 			   MT_WF_RFCR_DROP_BCAST |
537 			   MT_WF_RFCR_DROP_DUPLICATE |
538 			   MT_WF_RFCR_DROP_A2_BSSID |
539 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
540 			   MT_WF_RFCR_DROP_STBC_MULTI);
541 
542 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
543 			       MT_WF_RFCR_DROP_A3_MAC |
544 			       MT_WF_RFCR_DROP_A3_BSSID);
545 
546 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
547 
548 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
549 			     MT_WF_RFCR_DROP_RTS |
550 			     MT_WF_RFCR_DROP_CTL_RSV |
551 			     MT_WF_RFCR_DROP_NDPA);
552 
553 	*total_flags = flags;
554 	mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
555 
556 	if (*total_flags & FIF_CONTROL)
557 		mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
558 	else
559 		mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
560 
561 	mt7921_mutex_release(dev);
562 }
563 
564 static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
565 				    struct ieee80211_vif *vif,
566 				    struct ieee80211_bss_conf *info,
567 				    u32 changed)
568 {
569 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
570 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
571 
572 	mt7921_mutex_acquire(dev);
573 
574 	if (changed & BSS_CHANGED_ERP_SLOT) {
575 		int slottime = info->use_short_slot ? 9 : 20;
576 
577 		if (slottime != phy->slottime) {
578 			phy->slottime = slottime;
579 			mt7921_mac_set_timing(phy);
580 		}
581 	}
582 
583 	/* ensure that enable txcmd_mode after bss_info */
584 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
585 		mt7921_mcu_set_tx(dev, vif);
586 
587 	if (changed & BSS_CHANGED_PS)
588 		mt7921_mcu_uni_bss_ps(dev, vif);
589 
590 	mt7921_mutex_release(dev);
591 }
592 
593 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
594 		       struct ieee80211_sta *sta)
595 {
596 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
597 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
598 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
599 	int ret, idx;
600 
601 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
602 	if (idx < 0)
603 		return -ENOSPC;
604 
605 	INIT_LIST_HEAD(&msta->stats_list);
606 	INIT_LIST_HEAD(&msta->poll_list);
607 	msta->vif = mvif;
608 	msta->wcid.sta = 1;
609 	msta->wcid.idx = idx;
610 	msta->wcid.ext_phy = mvif->mt76.band_idx;
611 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
612 	msta->stats.jiffies = jiffies;
613 
614 	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
615 	if (ret)
616 		return ret;
617 
618 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
619 		mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
620 					    true);
621 
622 	mt7921_mac_wtbl_update(dev, idx,
623 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
624 
625 	ret = mt76_connac_mcu_add_sta_cmd(&dev->mphy, vif, sta, &msta->wcid,
626 					  true, MCU_UNI_CMD_STA_REC_UPDATE);
627 	if (ret)
628 		return ret;
629 
630 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
631 
632 	return 0;
633 }
634 
635 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
636 			   struct ieee80211_sta *sta)
637 {
638 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
639 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
640 
641 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
642 	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
643 
644 	mt76_connac_mcu_add_sta_cmd(&dev->mphy, vif, sta, &msta->wcid, false,
645 				    MCU_UNI_CMD_STA_REC_UPDATE);
646 
647 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
648 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
649 
650 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
651 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
652 
653 		mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
654 					    false);
655 	}
656 
657 	spin_lock_bh(&dev->sta_poll_lock);
658 	if (!list_empty(&msta->poll_list))
659 		list_del_init(&msta->poll_list);
660 	if (!list_empty(&msta->stats_list))
661 		list_del_init(&msta->stats_list);
662 	spin_unlock_bh(&dev->sta_poll_lock);
663 
664 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
665 }
666 
667 static void
668 mt7921_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
669 {
670 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
671 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
672 	struct mt76_phy *mphy = phy->mt76;
673 
674 	if (!test_bit(MT76_STATE_RUNNING, &mphy->state))
675 		return;
676 
677 	if (test_bit(MT76_STATE_PM, &mphy->state)) {
678 		queue_work(dev->mt76.wq, &dev->pm.wake_work);
679 		return;
680 	}
681 
682 	dev->pm.last_activity = jiffies;
683 	mt76_worker_schedule(&dev->mt76.tx_worker);
684 }
685 
686 static void mt7921_tx(struct ieee80211_hw *hw,
687 		      struct ieee80211_tx_control *control,
688 		      struct sk_buff *skb)
689 {
690 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
691 	struct mt76_phy *mphy = hw->priv;
692 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
693 	struct ieee80211_vif *vif = info->control.vif;
694 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
695 	int qid;
696 
697 	if (control->sta) {
698 		struct mt7921_sta *sta;
699 
700 		sta = (struct mt7921_sta *)control->sta->drv_priv;
701 		wcid = &sta->wcid;
702 	}
703 
704 	if (vif && !control->sta) {
705 		struct mt7921_vif *mvif;
706 
707 		mvif = (struct mt7921_vif *)vif->drv_priv;
708 		wcid = &mvif->sta.wcid;
709 	}
710 
711 	if (!test_bit(MT76_STATE_PM, &mphy->state)) {
712 		dev->pm.last_activity = jiffies;
713 		mt76_tx(mphy, control->sta, wcid, skb);
714 		return;
715 	}
716 
717 	qid = skb_get_queue_mapping(skb);
718 	if (qid >= MT_TXQ_PSD) {
719 		qid = IEEE80211_AC_BE;
720 		skb_set_queue_mapping(skb, qid);
721 	}
722 
723 	mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
724 }
725 
726 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
727 {
728 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
729 
730 	mt7921_mutex_acquire(dev);
731 	mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
732 	mt7921_mutex_release(dev);
733 
734 	return 0;
735 }
736 
737 static int
738 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
739 		    struct ieee80211_ampdu_params *params)
740 {
741 	enum ieee80211_ampdu_mlme_action action = params->action;
742 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
743 	struct ieee80211_sta *sta = params->sta;
744 	struct ieee80211_txq *txq = sta->txq[params->tid];
745 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
746 	u16 tid = params->tid;
747 	u16 ssn = params->ssn;
748 	struct mt76_txq *mtxq;
749 	int ret = 0;
750 
751 	if (!txq)
752 		return -EINVAL;
753 
754 	mtxq = (struct mt76_txq *)txq->drv_priv;
755 
756 	mt7921_mutex_acquire(dev);
757 	switch (action) {
758 	case IEEE80211_AMPDU_RX_START:
759 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
760 				   params->buf_size);
761 		mt7921_mcu_uni_rx_ba(dev, params, true);
762 		break;
763 	case IEEE80211_AMPDU_RX_STOP:
764 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
765 		mt7921_mcu_uni_rx_ba(dev, params, false);
766 		break;
767 	case IEEE80211_AMPDU_TX_OPERATIONAL:
768 		mtxq->aggr = true;
769 		mtxq->send_bar = false;
770 		mt7921_mcu_uni_tx_ba(dev, params, true);
771 		break;
772 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
773 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
774 		mtxq->aggr = false;
775 		clear_bit(tid, &msta->ampdu_state);
776 		mt7921_mcu_uni_tx_ba(dev, params, false);
777 		break;
778 	case IEEE80211_AMPDU_TX_START:
779 		set_bit(tid, &msta->ampdu_state);
780 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
781 		break;
782 	case IEEE80211_AMPDU_TX_STOP_CONT:
783 		mtxq->aggr = false;
784 		clear_bit(tid, &msta->ampdu_state);
785 		mt7921_mcu_uni_tx_ba(dev, params, false);
786 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
787 		break;
788 	}
789 	mt7921_mutex_release(dev);
790 
791 	return ret;
792 }
793 
794 static int
795 mt7921_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
796 	       struct ieee80211_sta *sta)
797 {
798 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
799 			      IEEE80211_STA_NONE);
800 }
801 
802 static int
803 mt7921_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
804 		  struct ieee80211_sta *sta)
805 {
806 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
807 			      IEEE80211_STA_NOTEXIST);
808 }
809 
810 static int
811 mt7921_get_stats(struct ieee80211_hw *hw,
812 		 struct ieee80211_low_level_stats *stats)
813 {
814 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
815 	struct mib_stats *mib = &phy->mib;
816 
817 	stats->dot11RTSSuccessCount = mib->rts_cnt;
818 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
819 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
820 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
821 
822 	return 0;
823 }
824 
825 static u64
826 mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
827 {
828 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
829 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
830 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
831 	u8 omac_idx = mvif->mt76.omac_idx;
832 	bool band = phy != &dev->phy;
833 	union {
834 		u64 t64;
835 		u32 t32[2];
836 	} tsf;
837 	u16 n;
838 
839 	mt7921_mutex_acquire(dev);
840 
841 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
842 	/* TSF software read */
843 	mt76_set(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE);
844 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band));
845 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band));
846 
847 	mt7921_mutex_release(dev);
848 
849 	return tsf.t64;
850 }
851 
852 static void
853 mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
854 	       u64 timestamp)
855 {
856 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
857 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
858 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
859 	u8 omac_idx = mvif->mt76.omac_idx;
860 	bool band = phy != &dev->phy;
861 	union {
862 		u64 t64;
863 		u32 t32[2];
864 	} tsf = { .t64 = timestamp, };
865 	u16 n;
866 
867 	mt7921_mutex_acquire(dev);
868 
869 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
870 	mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
871 	mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
872 	/* TSF software overwrite */
873 	mt76_set(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_WRITE);
874 
875 	mt7921_mutex_release(dev);
876 }
877 
878 static void
879 mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
880 {
881 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
882 	struct mt7921_dev *dev = phy->dev;
883 
884 	mt7921_mutex_acquire(dev);
885 	phy->coverage_class = max_t(s16, coverage_class, 0);
886 	mt7921_mac_set_timing(phy);
887 	mt7921_mutex_release(dev);
888 }
889 
890 void mt7921_scan_work(struct work_struct *work)
891 {
892 	struct mt7921_phy *phy;
893 
894 	phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy,
895 						scan_work.work);
896 
897 	while (true) {
898 		struct mt7921_mcu_rxd *rxd;
899 		struct sk_buff *skb;
900 
901 		spin_lock_bh(&phy->dev->mt76.lock);
902 		skb = __skb_dequeue(&phy->scan_event_list);
903 		spin_unlock_bh(&phy->dev->mt76.lock);
904 
905 		if (!skb)
906 			break;
907 
908 		rxd = (struct mt7921_mcu_rxd *)skb->data;
909 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
910 			ieee80211_sched_scan_results(phy->mt76->hw);
911 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
912 					      &phy->mt76->state)) {
913 			struct cfg80211_scan_info info = {
914 				.aborted = false,
915 			};
916 
917 			ieee80211_scan_completed(phy->mt76->hw, &info);
918 		}
919 		dev_kfree_skb(skb);
920 	}
921 }
922 
923 static int
924 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
925 	       struct ieee80211_scan_request *req)
926 {
927 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
928 	struct mt76_phy *mphy = hw->priv;
929 	int err;
930 
931 	mt7921_mutex_acquire(dev);
932 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
933 	mt7921_mutex_release(dev);
934 
935 	return err;
936 }
937 
938 static void
939 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
940 {
941 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
942 	struct mt76_phy *mphy = hw->priv;
943 
944 	mt7921_mutex_acquire(dev);
945 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
946 	mt7921_mutex_release(dev);
947 }
948 
949 static int
950 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
951 			struct cfg80211_sched_scan_request *req,
952 			struct ieee80211_scan_ies *ies)
953 {
954 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
955 	struct mt76_phy *mphy = hw->priv;
956 	int err;
957 
958 	mt7921_mutex_acquire(dev);
959 
960 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
961 	if (err < 0)
962 		goto out;
963 
964 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
965 out:
966 	mt7921_mutex_release(dev);
967 
968 	return err;
969 }
970 
971 static int
972 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
973 {
974 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
975 	struct mt76_phy *mphy = hw->priv;
976 	int err;
977 
978 	mt7921_mutex_acquire(dev);
979 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
980 	mt7921_mutex_release(dev);
981 
982 	return err;
983 }
984 
985 static int
986 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
987 {
988 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
989 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
990 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
991 
992 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
993 		return -EINVAL;
994 
995 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
996 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
997 
998 	mt7921_mutex_acquire(dev);
999 
1000 	phy->mt76->antenna_mask = tx_ant;
1001 	phy->mt76->chainmask = tx_ant;
1002 
1003 	mt76_set_stream_caps(phy->mt76, true);
1004 	mt7921_set_stream_he_caps(phy);
1005 
1006 	mt7921_mutex_release(dev);
1007 
1008 	return 0;
1009 }
1010 
1011 static void
1012 mt7921_sta_rc_update(struct ieee80211_hw *hw,
1013 		     struct ieee80211_vif *vif,
1014 		     struct ieee80211_sta *sta,
1015 		     u32 changed)
1016 {
1017 }
1018 
1019 static void mt7921_sta_statistics(struct ieee80211_hw *hw,
1020 				  struct ieee80211_vif *vif,
1021 				  struct ieee80211_sta *sta,
1022 				  struct station_info *sinfo)
1023 {
1024 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1025 	struct mt7921_sta_stats *stats = &msta->stats;
1026 
1027 	if (!stats->tx_rate.legacy && !stats->tx_rate.flags)
1028 		return;
1029 
1030 	if (stats->tx_rate.legacy) {
1031 		sinfo->txrate.legacy = stats->tx_rate.legacy;
1032 	} else {
1033 		sinfo->txrate.mcs = stats->tx_rate.mcs;
1034 		sinfo->txrate.nss = stats->tx_rate.nss;
1035 		sinfo->txrate.bw = stats->tx_rate.bw;
1036 		sinfo->txrate.he_gi = stats->tx_rate.he_gi;
1037 		sinfo->txrate.he_dcm = stats->tx_rate.he_dcm;
1038 		sinfo->txrate.he_ru_alloc = stats->tx_rate.he_ru_alloc;
1039 	}
1040 	sinfo->txrate.flags = stats->tx_rate.flags;
1041 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1042 }
1043 
1044 #ifdef CONFIG_PM
1045 static int mt7921_suspend(struct ieee80211_hw *hw,
1046 			  struct cfg80211_wowlan *wowlan)
1047 {
1048 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1049 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1050 	int err;
1051 
1052 	cancel_delayed_work_sync(&phy->scan_work);
1053 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1054 
1055 	cancel_delayed_work_sync(&dev->pm.ps_work);
1056 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1057 
1058 	mt7921_mutex_acquire(dev);
1059 
1060 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1061 
1062 	set_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1063 	ieee80211_iterate_active_interfaces(hw,
1064 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1065 					    mt76_connac_mcu_set_suspend_iter,
1066 					    &dev->mphy);
1067 
1068 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
1069 
1070 	mt7921_mutex_release(dev);
1071 
1072 	return err;
1073 }
1074 
1075 static int mt7921_resume(struct ieee80211_hw *hw)
1076 {
1077 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1078 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1079 	int err;
1080 
1081 	mt7921_mutex_acquire(dev);
1082 
1083 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
1084 	if (err < 0)
1085 		goto out;
1086 
1087 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1088 	clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1089 	ieee80211_iterate_active_interfaces(hw,
1090 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1091 					    mt76_connac_mcu_set_suspend_iter,
1092 					    &dev->mphy);
1093 
1094 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1095 				     MT7921_WATCHDOG_TIME);
1096 out:
1097 
1098 	mt7921_mutex_release(dev);
1099 
1100 	return err;
1101 }
1102 
1103 static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1104 {
1105 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1106 	struct mt76_dev *mdev = &dev->mt76;
1107 
1108 	device_set_wakeup_enable(mdev->dev, enabled);
1109 }
1110 
1111 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1112 				  struct ieee80211_vif *vif,
1113 				  struct cfg80211_gtk_rekey_data *data)
1114 {
1115 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1116 
1117 	mt7921_mutex_acquire(dev);
1118 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1119 	mt7921_mutex_release(dev);
1120 }
1121 #endif /* CONFIG_PM */
1122 
1123 const struct ieee80211_ops mt7921_ops = {
1124 	.tx = mt7921_tx,
1125 	.start = mt7921_start,
1126 	.stop = mt7921_stop,
1127 	.add_interface = mt7921_add_interface,
1128 	.remove_interface = mt7921_remove_interface,
1129 	.config = mt7921_config,
1130 	.conf_tx = mt7921_conf_tx,
1131 	.configure_filter = mt7921_configure_filter,
1132 	.bss_info_changed = mt7921_bss_info_changed,
1133 	.sta_add = mt7921_sta_add,
1134 	.sta_remove = mt7921_sta_remove,
1135 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1136 	.sta_rc_update = mt7921_sta_rc_update,
1137 	.set_key = mt7921_set_key,
1138 	.ampdu_action = mt7921_ampdu_action,
1139 	.set_rts_threshold = mt7921_set_rts_threshold,
1140 	.wake_tx_queue = mt7921_wake_tx_queue,
1141 	.release_buffered_frames = mt76_release_buffered_frames,
1142 	.get_txpower = mt76_get_txpower,
1143 	.get_stats = mt7921_get_stats,
1144 	.get_tsf = mt7921_get_tsf,
1145 	.set_tsf = mt7921_set_tsf,
1146 	.get_survey = mt76_get_survey,
1147 	.get_antenna = mt76_get_antenna,
1148 	.set_antenna = mt7921_set_antenna,
1149 	.set_coverage_class = mt7921_set_coverage_class,
1150 	.hw_scan = mt7921_hw_scan,
1151 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1152 	.sta_statistics = mt7921_sta_statistics,
1153 	.sched_scan_start = mt7921_start_sched_scan,
1154 	.sched_scan_stop = mt7921_stop_sched_scan,
1155 #ifdef CONFIG_PM
1156 	.suspend = mt7921_suspend,
1157 	.resume = mt7921_resume,
1158 	.set_wakeup = mt7921_set_wakeup,
1159 	.set_rekey_data = mt7921_set_rekey_data,
1160 #endif /* CONFIG_PM */
1161 };
1162