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