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