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