xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/main.c (revision 8910a4e5ba342ad661acb6f99fa14e0dd809465e)
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 	memset(mib, 0, sizeof(*mib));
860 
861 	mt7921_mutex_release(phy->dev);
862 
863 	return 0;
864 }
865 
866 static u64
867 mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
868 {
869 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
870 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
871 	u8 omac_idx = mvif->mt76.omac_idx;
872 	union {
873 		u64 t64;
874 		u32 t32[2];
875 	} tsf;
876 	u16 n;
877 
878 	mt7921_mutex_acquire(dev);
879 
880 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
881 	/* TSF software read */
882 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_MODE);
883 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(0));
884 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(0));
885 
886 	mt7921_mutex_release(dev);
887 
888 	return tsf.t64;
889 }
890 
891 static void
892 mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
893 	       u64 timestamp)
894 {
895 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
896 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
897 	u8 omac_idx = mvif->mt76.omac_idx;
898 	union {
899 		u64 t64;
900 		u32 t32[2];
901 	} tsf = { .t64 = timestamp, };
902 	u16 n;
903 
904 	mt7921_mutex_acquire(dev);
905 
906 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
907 	mt76_wr(dev, MT_LPON_UTTR0(0), tsf.t32[0]);
908 	mt76_wr(dev, MT_LPON_UTTR1(0), tsf.t32[1]);
909 	/* TSF software overwrite */
910 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_WRITE);
911 
912 	mt7921_mutex_release(dev);
913 }
914 
915 static void
916 mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
917 {
918 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
919 	struct mt7921_dev *dev = phy->dev;
920 
921 	mt7921_mutex_acquire(dev);
922 	phy->coverage_class = max_t(s16, coverage_class, 0);
923 	mt7921_mac_set_timing(phy);
924 	mt7921_mutex_release(dev);
925 }
926 
927 void mt7921_scan_work(struct work_struct *work)
928 {
929 	struct mt7921_phy *phy;
930 
931 	phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy,
932 						scan_work.work);
933 
934 	while (true) {
935 		struct mt7921_mcu_rxd *rxd;
936 		struct sk_buff *skb;
937 
938 		spin_lock_bh(&phy->dev->mt76.lock);
939 		skb = __skb_dequeue(&phy->scan_event_list);
940 		spin_unlock_bh(&phy->dev->mt76.lock);
941 
942 		if (!skb)
943 			break;
944 
945 		rxd = (struct mt7921_mcu_rxd *)skb->data;
946 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
947 			ieee80211_sched_scan_results(phy->mt76->hw);
948 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
949 					      &phy->mt76->state)) {
950 			struct cfg80211_scan_info info = {
951 				.aborted = false,
952 			};
953 
954 			ieee80211_scan_completed(phy->mt76->hw, &info);
955 		}
956 		dev_kfree_skb(skb);
957 	}
958 }
959 
960 static int
961 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
962 	       struct ieee80211_scan_request *req)
963 {
964 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
965 	struct mt76_phy *mphy = hw->priv;
966 	int err;
967 
968 	mt7921_mutex_acquire(dev);
969 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
970 	mt7921_mutex_release(dev);
971 
972 	return err;
973 }
974 
975 static void
976 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
977 {
978 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
979 	struct mt76_phy *mphy = hw->priv;
980 
981 	mt7921_mutex_acquire(dev);
982 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
983 	mt7921_mutex_release(dev);
984 }
985 
986 static int
987 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
988 			struct cfg80211_sched_scan_request *req,
989 			struct ieee80211_scan_ies *ies)
990 {
991 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
992 	struct mt76_phy *mphy = hw->priv;
993 	int err;
994 
995 	mt7921_mutex_acquire(dev);
996 
997 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
998 	if (err < 0)
999 		goto out;
1000 
1001 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1002 out:
1003 	mt7921_mutex_release(dev);
1004 
1005 	return err;
1006 }
1007 
1008 static int
1009 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1010 {
1011 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1012 	struct mt76_phy *mphy = hw->priv;
1013 	int err;
1014 
1015 	mt7921_mutex_acquire(dev);
1016 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1017 	mt7921_mutex_release(dev);
1018 
1019 	return err;
1020 }
1021 
1022 static int
1023 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1024 {
1025 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1026 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1027 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1028 
1029 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1030 		return -EINVAL;
1031 
1032 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1033 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1034 
1035 	mt7921_mutex_acquire(dev);
1036 
1037 	phy->mt76->antenna_mask = tx_ant;
1038 	phy->mt76->chainmask = tx_ant;
1039 
1040 	mt76_set_stream_caps(phy->mt76, true);
1041 	mt7921_set_stream_he_caps(phy);
1042 
1043 	mt7921_mutex_release(dev);
1044 
1045 	return 0;
1046 }
1047 
1048 static void mt7921_sta_statistics(struct ieee80211_hw *hw,
1049 				  struct ieee80211_vif *vif,
1050 				  struct ieee80211_sta *sta,
1051 				  struct station_info *sinfo)
1052 {
1053 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1054 	struct rate_info *txrate = &msta->wcid.rate;
1055 
1056 	if (!txrate->legacy && !txrate->flags)
1057 		return;
1058 
1059 	if (txrate->legacy) {
1060 		sinfo->txrate.legacy = txrate->legacy;
1061 	} else {
1062 		sinfo->txrate.mcs = txrate->mcs;
1063 		sinfo->txrate.nss = txrate->nss;
1064 		sinfo->txrate.bw = txrate->bw;
1065 		sinfo->txrate.he_gi = txrate->he_gi;
1066 		sinfo->txrate.he_dcm = txrate->he_dcm;
1067 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1068 	}
1069 	sinfo->txrate.flags = txrate->flags;
1070 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1071 }
1072 
1073 #ifdef CONFIG_PM
1074 static int mt7921_suspend(struct ieee80211_hw *hw,
1075 			  struct cfg80211_wowlan *wowlan)
1076 {
1077 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1078 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1079 	int err;
1080 
1081 	cancel_delayed_work_sync(&phy->scan_work);
1082 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1083 
1084 	cancel_delayed_work_sync(&dev->pm.ps_work);
1085 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1086 
1087 	mt7921_mutex_acquire(dev);
1088 
1089 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1090 
1091 	set_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1092 	ieee80211_iterate_active_interfaces(hw,
1093 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1094 					    mt76_connac_mcu_set_suspend_iter,
1095 					    &dev->mphy);
1096 
1097 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
1098 
1099 	mt7921_mutex_release(dev);
1100 
1101 	return err;
1102 }
1103 
1104 static int mt7921_resume(struct ieee80211_hw *hw)
1105 {
1106 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1107 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1108 	int err;
1109 
1110 	mt7921_mutex_acquire(dev);
1111 
1112 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
1113 	if (err < 0)
1114 		goto out;
1115 
1116 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1117 	clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1118 	ieee80211_iterate_active_interfaces(hw,
1119 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1120 					    mt76_connac_mcu_set_suspend_iter,
1121 					    &dev->mphy);
1122 
1123 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1124 				     MT7921_WATCHDOG_TIME);
1125 out:
1126 
1127 	mt7921_mutex_release(dev);
1128 
1129 	return err;
1130 }
1131 
1132 static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1133 {
1134 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1135 	struct mt76_dev *mdev = &dev->mt76;
1136 
1137 	device_set_wakeup_enable(mdev->dev, enabled);
1138 }
1139 
1140 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1141 				  struct ieee80211_vif *vif,
1142 				  struct cfg80211_gtk_rekey_data *data)
1143 {
1144 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1145 
1146 	mt7921_mutex_acquire(dev);
1147 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1148 	mt7921_mutex_release(dev);
1149 }
1150 #endif /* CONFIG_PM */
1151 
1152 static void mt7921_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1153 			 u32 queues, bool drop)
1154 {
1155 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1156 
1157 	wait_event_timeout(dev->mt76.tx_wait, !mt76_has_tx_pending(&dev->mphy),
1158 			   HZ / 2);
1159 }
1160 
1161 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1162 					 struct ieee80211_vif *vif,
1163 					 struct ieee80211_sta *sta,
1164 					 bool enabled)
1165 {
1166 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1167 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1168 
1169 	if (enabled)
1170 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1171 	else
1172 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1173 
1174 	mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->wcid,
1175 					     MCU_UNI_CMD_STA_REC_UPDATE);
1176 }
1177 
1178 static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
1179 				const struct cfg80211_sar_specs *sar)
1180 {
1181 	const struct cfg80211_sar_capa *capa = hw->wiphy->sar_capa;
1182 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1183 	struct mt76_freq_range_power *data, *frp;
1184 	struct mt76_phy *mphy = hw->priv;
1185 	int err;
1186 	u32 i;
1187 
1188 	if (sar->type != NL80211_SAR_TYPE_POWER || !sar->num_sub_specs)
1189 		return -EINVAL;
1190 
1191 	mt7921_mutex_acquire(dev);
1192 
1193 	data = mphy->frp;
1194 
1195 	for (i = 0; i < sar->num_sub_specs; i++) {
1196 		u32 index = sar->sub_specs[i].freq_range_index;
1197 		/* SAR specifies power limitaton in 0.25dbm */
1198 		s32 power = sar->sub_specs[i].power >> 1;
1199 
1200 		if (power > 127 || power < -127)
1201 			power = 127;
1202 
1203 		frp = &data[index];
1204 		frp->range = &capa->freq_ranges[index];
1205 		frp->power = power;
1206 	}
1207 
1208 	err = mt76_connac_mcu_set_rate_txpower(mphy);
1209 
1210 	mt7921_mutex_release(dev);
1211 
1212 	return err;
1213 }
1214 
1215 const struct ieee80211_ops mt7921_ops = {
1216 	.tx = mt7921_tx,
1217 	.start = mt7921_start,
1218 	.stop = mt7921_stop,
1219 	.add_interface = mt7921_add_interface,
1220 	.remove_interface = mt7921_remove_interface,
1221 	.config = mt7921_config,
1222 	.conf_tx = mt7921_conf_tx,
1223 	.configure_filter = mt7921_configure_filter,
1224 	.bss_info_changed = mt7921_bss_info_changed,
1225 	.sta_state = mt7921_sta_state,
1226 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1227 	.set_key = mt7921_set_key,
1228 	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1229 	.ampdu_action = mt7921_ampdu_action,
1230 	.set_rts_threshold = mt7921_set_rts_threshold,
1231 	.wake_tx_queue = mt76_wake_tx_queue,
1232 	.release_buffered_frames = mt76_release_buffered_frames,
1233 	.get_txpower = mt76_get_txpower,
1234 	.get_stats = mt7921_get_stats,
1235 	.get_tsf = mt7921_get_tsf,
1236 	.set_tsf = mt7921_set_tsf,
1237 	.get_survey = mt76_get_survey,
1238 	.get_antenna = mt76_get_antenna,
1239 	.set_antenna = mt7921_set_antenna,
1240 	.set_coverage_class = mt7921_set_coverage_class,
1241 	.hw_scan = mt7921_hw_scan,
1242 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1243 	.sta_statistics = mt7921_sta_statistics,
1244 	.sched_scan_start = mt7921_start_sched_scan,
1245 	.sched_scan_stop = mt7921_stop_sched_scan,
1246 	CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
1247 	CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
1248 #ifdef CONFIG_PM
1249 	.suspend = mt7921_suspend,
1250 	.resume = mt7921_resume,
1251 	.set_wakeup = mt7921_set_wakeup,
1252 	.set_rekey_data = mt7921_set_rekey_data,
1253 #endif /* CONFIG_PM */
1254 	.flush = mt7921_flush,
1255 	.set_sar_specs = mt7921_set_sar_specs,
1256 };
1257 EXPORT_SYMBOL_GPL(mt7921_ops);
1258 
1259 MODULE_LICENSE("Dual BSD/GPL");
1260 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1261