• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/platform_device.h>
6 #include <linux/pci.h>
7 #include <linux/module.h>
8 #include "mt7915.h"
9 #include "mcu.h"
10 
mt7915_dev_running(struct mt7915_dev * dev)11 static bool mt7915_dev_running(struct mt7915_dev *dev)
12 {
13 	struct mt7915_phy *phy;
14 
15 	if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
16 		return true;
17 
18 	phy = mt7915_ext_phy(dev);
19 
20 	return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
21 }
22 
mt7915_start(struct ieee80211_hw * hw)23 static int mt7915_start(struct ieee80211_hw *hw)
24 {
25 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
26 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
27 	bool running;
28 	int ret;
29 
30 	flush_work(&dev->init_work);
31 
32 	mutex_lock(&dev->mt76.mutex);
33 
34 	running = mt7915_dev_running(dev);
35 
36 	if (!running) {
37 		ret = mt76_connac_mcu_set_pm(&dev->mt76, 0, 0);
38 		if (ret)
39 			goto out;
40 
41 		ret = mt7915_mcu_set_mac(dev, 0, true, true);
42 		if (ret)
43 			goto out;
44 
45 		mt7915_mac_enable_nf(dev, 0);
46 	}
47 
48 	if (phy != &dev->phy || phy->band_idx) {
49 		ret = mt76_connac_mcu_set_pm(&dev->mt76, 1, 0);
50 		if (ret)
51 			goto out;
52 
53 		ret = mt7915_mcu_set_mac(dev, 1, true, true);
54 		if (ret)
55 			goto out;
56 
57 		mt7915_mac_enable_nf(dev, 1);
58 	}
59 
60 	ret = mt7915_mcu_set_thermal_throttling(phy,
61 						MT7915_THERMAL_THROTTLE_MAX);
62 
63 	if (ret)
64 		goto out;
65 
66 	ret = mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b,
67 					     phy != &dev->phy);
68 	if (ret)
69 		goto out;
70 
71 	ret = mt7915_mcu_set_sku_en(phy, true);
72 	if (ret)
73 		goto out;
74 
75 	ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
76 	if (ret)
77 		goto out;
78 
79 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
80 
81 	if (!mt76_testmode_enabled(phy->mt76))
82 		ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
83 					     MT7915_WATCHDOG_TIME);
84 
85 	if (!running)
86 		mt7915_mac_reset_counters(phy);
87 
88 out:
89 	mutex_unlock(&dev->mt76.mutex);
90 
91 	return ret;
92 }
93 
mt7915_stop(struct ieee80211_hw * hw)94 static void mt7915_stop(struct ieee80211_hw *hw)
95 {
96 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
97 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
98 
99 	cancel_delayed_work_sync(&phy->mt76->mac_work);
100 
101 	mutex_lock(&dev->mt76.mutex);
102 
103 	mt76_testmode_reset(phy->mt76, true);
104 
105 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
106 
107 	if (phy != &dev->phy) {
108 		mt76_connac_mcu_set_pm(&dev->mt76, 1, 1);
109 		mt7915_mcu_set_mac(dev, 1, false, false);
110 	}
111 
112 	if (!mt7915_dev_running(dev)) {
113 		mt76_connac_mcu_set_pm(&dev->mt76, 0, 1);
114 		mt7915_mcu_set_mac(dev, 0, false, false);
115 	}
116 
117 	mutex_unlock(&dev->mt76.mutex);
118 }
119 
get_free_idx(u32 mask,u8 start,u8 end)120 static inline int get_free_idx(u32 mask, u8 start, u8 end)
121 {
122 	return ffs(~mask & GENMASK(end, start));
123 }
124 
get_omac_idx(enum nl80211_iftype type,u64 mask)125 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
126 {
127 	int i;
128 
129 	switch (type) {
130 	case NL80211_IFTYPE_MESH_POINT:
131 	case NL80211_IFTYPE_ADHOC:
132 	case NL80211_IFTYPE_STATION:
133 		/* prefer hw bssid slot 1-3 */
134 		i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
135 		if (i)
136 			return i - 1;
137 
138 		if (type != NL80211_IFTYPE_STATION)
139 			break;
140 
141 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
142 		if (i)
143 			return i - 1;
144 
145 		if (~mask & BIT(HW_BSSID_0))
146 			return HW_BSSID_0;
147 
148 		break;
149 	case NL80211_IFTYPE_MONITOR:
150 	case NL80211_IFTYPE_AP:
151 		/* ap uses hw bssid 0 and ext bssid */
152 		if (~mask & BIT(HW_BSSID_0))
153 			return HW_BSSID_0;
154 
155 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
156 		if (i)
157 			return i - 1;
158 
159 		break;
160 	default:
161 		WARN_ON(1);
162 		break;
163 	}
164 
165 	return -1;
166 }
167 
mt7915_init_bitrate_mask(struct ieee80211_vif * vif)168 static void mt7915_init_bitrate_mask(struct ieee80211_vif *vif)
169 {
170 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
171 	int i;
172 
173 	for (i = 0; i < ARRAY_SIZE(mvif->bitrate_mask.control); i++) {
174 		mvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
175 		mvif->bitrate_mask.control[i].he_gi = 0xff;
176 		mvif->bitrate_mask.control[i].he_ltf = 0xff;
177 		mvif->bitrate_mask.control[i].legacy = GENMASK(31, 0);
178 		memset(mvif->bitrate_mask.control[i].ht_mcs, 0xff,
179 		       sizeof(mvif->bitrate_mask.control[i].ht_mcs));
180 		memset(mvif->bitrate_mask.control[i].vht_mcs, 0xff,
181 		       sizeof(mvif->bitrate_mask.control[i].vht_mcs));
182 		memset(mvif->bitrate_mask.control[i].he_mcs, 0xff,
183 		       sizeof(mvif->bitrate_mask.control[i].he_mcs));
184 	}
185 }
186 
mt7915_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)187 static int mt7915_add_interface(struct ieee80211_hw *hw,
188 				struct ieee80211_vif *vif)
189 {
190 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
191 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
192 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
193 	struct mt76_txq *mtxq;
194 	bool ext_phy = phy != &dev->phy;
195 	int idx, ret = 0;
196 
197 	mutex_lock(&dev->mt76.mutex);
198 
199 	mt76_testmode_reset(phy->mt76, true);
200 
201 	if (vif->type == NL80211_IFTYPE_MONITOR &&
202 	    is_zero_ether_addr(vif->addr))
203 		phy->monitor_vif = vif;
204 
205 	mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
206 	if (mvif->mt76.idx >= (MT7915_MAX_INTERFACES << dev->dbdc_support)) {
207 		ret = -ENOSPC;
208 		goto out;
209 	}
210 
211 	idx = get_omac_idx(vif->type, phy->omac_mask);
212 	if (idx < 0) {
213 		ret = -ENOSPC;
214 		goto out;
215 	}
216 	mvif->mt76.omac_idx = idx;
217 	mvif->phy = phy;
218 	mvif->mt76.band_idx = phy->band_idx;
219 
220 	mvif->mt76.wmm_idx = vif->type != NL80211_IFTYPE_AP;
221 	if (ext_phy)
222 		mvif->mt76.wmm_idx += 2;
223 
224 	ret = mt7915_mcu_add_dev_info(phy, vif, true);
225 	if (ret)
226 		goto out;
227 
228 	dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
229 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
230 
231 	idx = MT7915_WTBL_RESERVED - mvif->mt76.idx;
232 
233 	INIT_LIST_HEAD(&mvif->sta.rc_list);
234 	INIT_LIST_HEAD(&mvif->sta.poll_list);
235 	mvif->sta.wcid.idx = idx;
236 	mvif->sta.wcid.phy_idx = ext_phy;
237 	mvif->sta.wcid.hw_key_idx = -1;
238 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
239 	mt76_packet_id_init(&mvif->sta.wcid);
240 
241 	mt7915_mac_wtbl_update(dev, idx,
242 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
243 
244 	if (vif->txq) {
245 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
246 		mtxq->wcid = idx;
247 	}
248 
249 	if (vif->type != NL80211_IFTYPE_AP &&
250 	    (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3))
251 		vif->offload_flags = 0;
252 	vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
253 
254 	mt7915_init_bitrate_mask(vif);
255 	memset(&mvif->cap, -1, sizeof(mvif->cap));
256 
257 	mt7915_mcu_add_bss_info(phy, vif, true);
258 	mt7915_mcu_add_sta(dev, vif, NULL, true);
259 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
260 
261 out:
262 	mutex_unlock(&dev->mt76.mutex);
263 
264 	return ret;
265 }
266 
mt7915_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)267 static void mt7915_remove_interface(struct ieee80211_hw *hw,
268 				    struct ieee80211_vif *vif)
269 {
270 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
271 	struct mt7915_sta *msta = &mvif->sta;
272 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
273 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
274 	int idx = msta->wcid.idx;
275 
276 	mt7915_mcu_add_bss_info(phy, vif, false);
277 	mt7915_mcu_add_sta(dev, vif, NULL, false);
278 
279 	mutex_lock(&dev->mt76.mutex);
280 	mt76_testmode_reset(phy->mt76, true);
281 	mutex_unlock(&dev->mt76.mutex);
282 
283 	if (vif == phy->monitor_vif)
284 		phy->monitor_vif = NULL;
285 
286 	mt7915_mcu_add_dev_info(phy, vif, false);
287 
288 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
289 
290 	mutex_lock(&dev->mt76.mutex);
291 	dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx);
292 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
293 	mutex_unlock(&dev->mt76.mutex);
294 
295 	spin_lock_bh(&dev->sta_poll_lock);
296 	if (!list_empty(&msta->poll_list))
297 		list_del_init(&msta->poll_list);
298 	spin_unlock_bh(&dev->sta_poll_lock);
299 
300 	mt76_packet_id_flush(&dev->mt76, &msta->wcid);
301 }
302 
mt7915_set_channel(struct mt7915_phy * phy)303 int mt7915_set_channel(struct mt7915_phy *phy)
304 {
305 	struct mt7915_dev *dev = phy->dev;
306 	int ret;
307 
308 	cancel_delayed_work_sync(&phy->mt76->mac_work);
309 
310 	mutex_lock(&dev->mt76.mutex);
311 	set_bit(MT76_RESET, &phy->mt76->state);
312 
313 	mt76_set_channel(phy->mt76);
314 
315 	if (dev->flash_mode) {
316 		ret = mt7915_mcu_apply_tx_dpd(phy);
317 		if (ret)
318 			goto out;
319 	}
320 
321 	ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
322 	if (ret)
323 		goto out;
324 
325 	mt7915_mac_set_timing(phy);
326 	ret = mt7915_dfs_init_radar_detector(phy);
327 	mt7915_mac_cca_stats_reset(phy);
328 
329 	mt7915_mac_reset_counters(phy);
330 	phy->noise = 0;
331 
332 out:
333 	clear_bit(MT76_RESET, &phy->mt76->state);
334 	mutex_unlock(&dev->mt76.mutex);
335 
336 	mt76_txq_schedule_all(phy->mt76);
337 
338 	if (!mt76_testmode_enabled(phy->mt76))
339 		ieee80211_queue_delayed_work(phy->mt76->hw,
340 					     &phy->mt76->mac_work,
341 					     MT7915_WATCHDOG_TIME);
342 
343 	return ret;
344 }
345 
mt7915_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)346 static int mt7915_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
347 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
348 			  struct ieee80211_key_conf *key)
349 {
350 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
351 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
352 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
353 	struct mt7915_sta *msta = sta ? (struct mt7915_sta *)sta->drv_priv :
354 				  &mvif->sta;
355 	struct mt76_wcid *wcid = &msta->wcid;
356 	u8 *wcid_keyidx = &wcid->hw_key_idx;
357 	int idx = key->keyidx;
358 	int err = 0;
359 
360 	/* The hardware does not support per-STA RX GTK, fallback
361 	 * to software mode for these.
362 	 */
363 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
364 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
365 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
366 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
367 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
368 		return -EOPNOTSUPP;
369 
370 	/* fall back to sw encryption for unsupported ciphers */
371 	switch (key->cipher) {
372 	case WLAN_CIPHER_SUITE_AES_CMAC:
373 		wcid_keyidx = &wcid->hw_key_idx2;
374 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
375 		break;
376 	case WLAN_CIPHER_SUITE_TKIP:
377 	case WLAN_CIPHER_SUITE_CCMP:
378 	case WLAN_CIPHER_SUITE_CCMP_256:
379 	case WLAN_CIPHER_SUITE_GCMP:
380 	case WLAN_CIPHER_SUITE_GCMP_256:
381 	case WLAN_CIPHER_SUITE_SMS4:
382 		break;
383 	case WLAN_CIPHER_SUITE_WEP40:
384 	case WLAN_CIPHER_SUITE_WEP104:
385 	default:
386 		return -EOPNOTSUPP;
387 	}
388 
389 	mutex_lock(&dev->mt76.mutex);
390 
391 	if (cmd == SET_KEY && !sta && !mvif->mt76.cipher) {
392 		mvif->mt76.cipher = mt76_connac_mcu_get_cipher(key->cipher);
393 		mt7915_mcu_add_bss_info(phy, vif, true);
394 	}
395 
396 	if (cmd == SET_KEY) {
397 		*wcid_keyidx = idx;
398 	} else {
399 		if (idx == *wcid_keyidx)
400 			*wcid_keyidx = -1;
401 		goto out;
402 	}
403 
404 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
405 	err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->bip,
406 				      key, MCU_EXT_CMD(STA_REC_UPDATE),
407 				      &msta->wcid, cmd);
408 out:
409 	mutex_unlock(&dev->mt76.mutex);
410 
411 	return err;
412 }
413 
mt7915_set_sar_specs(struct ieee80211_hw * hw,const struct cfg80211_sar_specs * sar)414 static int mt7915_set_sar_specs(struct ieee80211_hw *hw,
415 				const struct cfg80211_sar_specs *sar)
416 {
417 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
418 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
419 	int err = -EINVAL;
420 
421 	mutex_lock(&dev->mt76.mutex);
422 	if (!cfg80211_chandef_valid(&phy->mt76->chandef))
423 		goto out;
424 
425 	err = mt76_init_sar_power(hw, sar);
426 	if (err)
427 		goto out;
428 
429 	err = mt7915_mcu_set_txpower_sku(phy);
430 out:
431 	mutex_unlock(&dev->mt76.mutex);
432 
433 	return err;
434 }
435 
mt7915_config(struct ieee80211_hw * hw,u32 changed)436 static int mt7915_config(struct ieee80211_hw *hw, u32 changed)
437 {
438 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
439 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
440 	bool band = phy != &dev->phy;
441 	int ret;
442 
443 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
444 #ifdef CONFIG_NL80211_TESTMODE
445 		if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
446 			mutex_lock(&dev->mt76.mutex);
447 			mt76_testmode_reset(phy->mt76, false);
448 			mutex_unlock(&dev->mt76.mutex);
449 		}
450 #endif
451 		ieee80211_stop_queues(hw);
452 		ret = mt7915_set_channel(phy);
453 		if (ret)
454 			return ret;
455 		ieee80211_wake_queues(hw);
456 	}
457 
458 	if (changed & (IEEE80211_CONF_CHANGE_POWER |
459 		       IEEE80211_CONF_CHANGE_CHANNEL)) {
460 		ret = mt7915_mcu_set_txpower_sku(phy);
461 		if (ret)
462 			return ret;
463 	}
464 
465 	mutex_lock(&dev->mt76.mutex);
466 
467 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
468 		bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
469 
470 		if (!enabled)
471 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
472 		else
473 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
474 
475 		mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN,
476 			       enabled);
477 		mt76_testmode_reset(phy->mt76, true);
478 		mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
479 	}
480 
481 	mutex_unlock(&dev->mt76.mutex);
482 
483 	return 0;
484 }
485 
486 static int
mt7915_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,u16 queue,const struct ieee80211_tx_queue_params * params)487 mt7915_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
488 	       unsigned int link_id, u16 queue,
489 	       const struct ieee80211_tx_queue_params *params)
490 {
491 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
492 
493 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
494 	queue = mt76_connac_lmac_mapping(queue);
495 	mvif->queue_params[queue] = *params;
496 
497 	return 0;
498 }
499 
mt7915_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)500 static void mt7915_configure_filter(struct ieee80211_hw *hw,
501 				    unsigned int changed_flags,
502 				    unsigned int *total_flags,
503 				    u64 multicast)
504 {
505 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
506 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
507 	bool band = phy != &dev->phy;
508 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
509 			MT_WF_RFCR1_DROP_BF_POLL |
510 			MT_WF_RFCR1_DROP_BA |
511 			MT_WF_RFCR1_DROP_CFEND |
512 			MT_WF_RFCR1_DROP_CFACK;
513 	u32 flags = 0;
514 
515 #define MT76_FILTER(_flag, _hw) do {					\
516 		flags |= *total_flags & FIF_##_flag;			\
517 		phy->rxfilter &= ~(_hw);				\
518 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
519 	} while (0)
520 
521 	mutex_lock(&dev->mt76.mutex);
522 
523 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
524 			   MT_WF_RFCR_DROP_OTHER_BEACON |
525 			   MT_WF_RFCR_DROP_FRAME_REPORT |
526 			   MT_WF_RFCR_DROP_PROBEREQ |
527 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
528 			   MT_WF_RFCR_DROP_MCAST |
529 			   MT_WF_RFCR_DROP_BCAST |
530 			   MT_WF_RFCR_DROP_DUPLICATE |
531 			   MT_WF_RFCR_DROP_A2_BSSID |
532 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
533 			   MT_WF_RFCR_DROP_STBC_MULTI);
534 
535 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
536 			       MT_WF_RFCR_DROP_A3_MAC |
537 			       MT_WF_RFCR_DROP_A3_BSSID);
538 
539 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
540 
541 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
542 			     MT_WF_RFCR_DROP_RTS |
543 			     MT_WF_RFCR_DROP_CTL_RSV |
544 			     MT_WF_RFCR_DROP_NDPA);
545 
546 	*total_flags = flags;
547 	mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
548 
549 	if (*total_flags & FIF_CONTROL)
550 		mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
551 	else
552 		mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
553 
554 	mutex_unlock(&dev->mt76.mutex);
555 }
556 
557 static void
mt7915_update_bss_color(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_he_bss_color * bss_color)558 mt7915_update_bss_color(struct ieee80211_hw *hw,
559 			struct ieee80211_vif *vif,
560 			struct cfg80211_he_bss_color *bss_color)
561 {
562 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
563 
564 	switch (vif->type) {
565 	case NL80211_IFTYPE_AP: {
566 		struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
567 
568 		if (mvif->mt76.omac_idx > HW_BSSID_MAX)
569 			return;
570 		fallthrough;
571 	}
572 	case NL80211_IFTYPE_STATION:
573 		mt7915_mcu_update_bss_color(dev, vif, bss_color);
574 		break;
575 	default:
576 		break;
577 	}
578 }
579 
mt7915_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)580 static void mt7915_bss_info_changed(struct ieee80211_hw *hw,
581 				    struct ieee80211_vif *vif,
582 				    struct ieee80211_bss_conf *info,
583 				    u64 changed)
584 {
585 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
586 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
587 
588 	mutex_lock(&dev->mt76.mutex);
589 
590 	/*
591 	 * station mode uses BSSID to map the wlan entry to a peer,
592 	 * and then peer references bss_info_rfch to set bandwidth cap.
593 	 */
594 	if (changed & BSS_CHANGED_BSSID &&
595 	    vif->type == NL80211_IFTYPE_STATION) {
596 		bool join = !is_zero_ether_addr(info->bssid);
597 
598 		mt7915_mcu_add_bss_info(phy, vif, join);
599 		mt7915_mcu_add_sta(dev, vif, NULL, join);
600 	}
601 
602 	if (changed & BSS_CHANGED_ASSOC) {
603 		mt7915_mcu_add_bss_info(phy, vif, vif->cfg.assoc);
604 		mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
605 	}
606 
607 	if (changed & BSS_CHANGED_ERP_SLOT) {
608 		int slottime = info->use_short_slot ? 9 : 20;
609 
610 		if (slottime != phy->slottime) {
611 			phy->slottime = slottime;
612 			mt7915_mac_set_timing(phy);
613 		}
614 	}
615 
616 	if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
617 		mt7915_mcu_add_bss_info(phy, vif, true);
618 		mt7915_mcu_add_sta(dev, vif, NULL, true);
619 	}
620 
621 	/* ensure that enable txcmd_mode after bss_info */
622 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
623 		mt7915_mcu_set_tx(dev, vif);
624 
625 	if (changed & BSS_CHANGED_HE_OBSS_PD)
626 		mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
627 
628 	if (changed & BSS_CHANGED_HE_BSS_COLOR)
629 		mt7915_update_bss_color(hw, vif, &info->he_bss_color);
630 
631 	if (changed & (BSS_CHANGED_BEACON |
632 		       BSS_CHANGED_BEACON_ENABLED |
633 		       BSS_CHANGED_UNSOL_BCAST_PROBE_RESP |
634 		       BSS_CHANGED_FILS_DISCOVERY))
635 		mt7915_mcu_add_beacon(hw, vif, info->enable_beacon, changed);
636 
637 	mutex_unlock(&dev->mt76.mutex);
638 }
639 
640 static void
mt7915_channel_switch_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_chan_def * chandef)641 mt7915_channel_switch_beacon(struct ieee80211_hw *hw,
642 			     struct ieee80211_vif *vif,
643 			     struct cfg80211_chan_def *chandef)
644 {
645 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
646 
647 	mutex_lock(&dev->mt76.mutex);
648 	mt7915_mcu_add_beacon(hw, vif, true, BSS_CHANGED_BEACON);
649 	mutex_unlock(&dev->mt76.mutex);
650 }
651 
mt7915_mac_sta_add(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)652 int mt7915_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
653 		       struct ieee80211_sta *sta)
654 {
655 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
656 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
657 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
658 	bool ext_phy = mvif->phy != &dev->phy;
659 	int ret, idx;
660 
661 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA);
662 	if (idx < 0)
663 		return -ENOSPC;
664 
665 	INIT_LIST_HEAD(&msta->rc_list);
666 	INIT_LIST_HEAD(&msta->poll_list);
667 	msta->vif = mvif;
668 	msta->wcid.sta = 1;
669 	msta->wcid.idx = idx;
670 	msta->wcid.phy_idx = ext_phy;
671 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
672 	msta->jiffies = jiffies;
673 
674 	mt7915_mac_wtbl_update(dev, idx,
675 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
676 
677 	ret = mt7915_mcu_add_sta(dev, vif, sta, true);
678 	if (ret)
679 		return ret;
680 
681 	return mt7915_mcu_add_rate_ctrl(dev, vif, sta, false);
682 }
683 
mt7915_mac_sta_remove(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)684 void mt7915_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
685 			   struct ieee80211_sta *sta)
686 {
687 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
688 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
689 	int i;
690 
691 	mt7915_mcu_add_sta(dev, vif, sta, false);
692 
693 	mt7915_mac_wtbl_update(dev, msta->wcid.idx,
694 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
695 
696 	for (i = 0; i < ARRAY_SIZE(msta->twt.flow); i++)
697 		mt7915_mac_twt_teardown_flow(dev, msta, i);
698 
699 	spin_lock_bh(&dev->sta_poll_lock);
700 	if (!list_empty(&msta->poll_list))
701 		list_del_init(&msta->poll_list);
702 	if (!list_empty(&msta->rc_list))
703 		list_del_init(&msta->rc_list);
704 	spin_unlock_bh(&dev->sta_poll_lock);
705 }
706 
mt7915_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)707 static void mt7915_tx(struct ieee80211_hw *hw,
708 		      struct ieee80211_tx_control *control,
709 		      struct sk_buff *skb)
710 {
711 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
712 	struct mt76_phy *mphy = hw->priv;
713 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
714 	struct ieee80211_vif *vif = info->control.vif;
715 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
716 
717 	if (control->sta) {
718 		struct mt7915_sta *sta;
719 
720 		sta = (struct mt7915_sta *)control->sta->drv_priv;
721 		wcid = &sta->wcid;
722 	}
723 
724 	if (vif && !control->sta) {
725 		struct mt7915_vif *mvif;
726 
727 		mvif = (struct mt7915_vif *)vif->drv_priv;
728 		wcid = &mvif->sta.wcid;
729 	}
730 
731 	mt76_tx(mphy, control->sta, wcid, skb);
732 }
733 
mt7915_set_rts_threshold(struct ieee80211_hw * hw,u32 val)734 static int mt7915_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
735 {
736 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
737 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
738 	int ret;
739 
740 	mutex_lock(&dev->mt76.mutex);
741 	ret = mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, phy != &dev->phy);
742 	mutex_unlock(&dev->mt76.mutex);
743 
744 	return ret;
745 }
746 
747 static int
mt7915_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)748 mt7915_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
749 		    struct ieee80211_ampdu_params *params)
750 {
751 	enum ieee80211_ampdu_mlme_action action = params->action;
752 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
753 	struct ieee80211_sta *sta = params->sta;
754 	struct ieee80211_txq *txq = sta->txq[params->tid];
755 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
756 	u16 tid = params->tid;
757 	u16 ssn = params->ssn;
758 	struct mt76_txq *mtxq;
759 	int ret = 0;
760 
761 	if (!txq)
762 		return -EINVAL;
763 
764 	mtxq = (struct mt76_txq *)txq->drv_priv;
765 
766 	mutex_lock(&dev->mt76.mutex);
767 	switch (action) {
768 	case IEEE80211_AMPDU_RX_START:
769 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
770 				   params->buf_size);
771 		ret = mt7915_mcu_add_rx_ba(dev, params, true);
772 		break;
773 	case IEEE80211_AMPDU_RX_STOP:
774 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
775 		ret = mt7915_mcu_add_rx_ba(dev, params, false);
776 		break;
777 	case IEEE80211_AMPDU_TX_OPERATIONAL:
778 		mtxq->aggr = true;
779 		mtxq->send_bar = false;
780 		ret = mt7915_mcu_add_tx_ba(dev, params, true);
781 		break;
782 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
783 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
784 		mtxq->aggr = false;
785 		clear_bit(tid, &msta->ampdu_state);
786 		ret = mt7915_mcu_add_tx_ba(dev, params, false);
787 		break;
788 	case IEEE80211_AMPDU_TX_START:
789 		set_bit(tid, &msta->ampdu_state);
790 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
791 		break;
792 	case IEEE80211_AMPDU_TX_STOP_CONT:
793 		mtxq->aggr = false;
794 		clear_bit(tid, &msta->ampdu_state);
795 		ret = mt7915_mcu_add_tx_ba(dev, params, false);
796 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
797 		break;
798 	}
799 	mutex_unlock(&dev->mt76.mutex);
800 
801 	return ret;
802 }
803 
804 static int
mt7915_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)805 mt7915_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
806 	       struct ieee80211_sta *sta)
807 {
808 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
809 			      IEEE80211_STA_NONE);
810 }
811 
812 static int
mt7915_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)813 mt7915_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
814 		  struct ieee80211_sta *sta)
815 {
816 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
817 			      IEEE80211_STA_NOTEXIST);
818 }
819 
820 static int
mt7915_get_stats(struct ieee80211_hw * hw,struct ieee80211_low_level_stats * stats)821 mt7915_get_stats(struct ieee80211_hw *hw,
822 		 struct ieee80211_low_level_stats *stats)
823 {
824 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
825 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
826 	struct mib_stats *mib = &phy->mib;
827 
828 	mutex_lock(&dev->mt76.mutex);
829 
830 	stats->dot11RTSSuccessCount = mib->rts_cnt;
831 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
832 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
833 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
834 
835 	mutex_unlock(&dev->mt76.mutex);
836 
837 	return 0;
838 }
839 
__mt7915_get_tsf(struct ieee80211_hw * hw,struct mt7915_vif * mvif)840 u64 __mt7915_get_tsf(struct ieee80211_hw *hw, struct mt7915_vif *mvif)
841 {
842 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
843 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
844 	bool band = phy != &dev->phy;
845 	union {
846 		u64 t64;
847 		u32 t32[2];
848 	} tsf;
849 	u16 n;
850 
851 	lockdep_assert_held(&dev->mt76.mutex);
852 
853 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
854 					       : mvif->mt76.omac_idx;
855 	/* TSF software read */
856 	if (is_mt7915(&dev->mt76))
857 		mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
858 			 MT_LPON_TCR_SW_READ);
859 	else
860 		mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
861 			 MT_LPON_TCR_SW_READ);
862 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band));
863 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band));
864 
865 	return tsf.t64;
866 }
867 
868 static u64
mt7915_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)869 mt7915_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
870 {
871 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
872 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
873 	u64 ret;
874 
875 	mutex_lock(&dev->mt76.mutex);
876 	ret = __mt7915_get_tsf(hw, mvif);
877 	mutex_unlock(&dev->mt76.mutex);
878 
879 	return ret;
880 }
881 
882 static void
mt7915_set_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 timestamp)883 mt7915_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
884 	       u64 timestamp)
885 {
886 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
887 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
888 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
889 	bool band = phy != &dev->phy;
890 	union {
891 		u64 t64;
892 		u32 t32[2];
893 	} tsf = { .t64 = timestamp, };
894 	u16 n;
895 
896 	mutex_lock(&dev->mt76.mutex);
897 
898 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
899 					       : mvif->mt76.omac_idx;
900 	mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
901 	mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
902 	/* TSF software overwrite */
903 	if (is_mt7915(&dev->mt76))
904 		mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
905 			 MT_LPON_TCR_SW_WRITE);
906 	else
907 		mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
908 			 MT_LPON_TCR_SW_WRITE);
909 
910 	mutex_unlock(&dev->mt76.mutex);
911 }
912 
913 static void
mt7915_offset_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,s64 timestamp)914 mt7915_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
915 		  s64 timestamp)
916 {
917 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
918 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
919 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
920 	bool band = phy != &dev->phy;
921 	union {
922 		u64 t64;
923 		u32 t32[2];
924 	} tsf = { .t64 = timestamp, };
925 	u16 n;
926 
927 	mutex_lock(&dev->mt76.mutex);
928 
929 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
930 					       : mvif->mt76.omac_idx;
931 	mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
932 	mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
933 	/* TSF software adjust*/
934 	if (is_mt7915(&dev->mt76))
935 		mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
936 			 MT_LPON_TCR_SW_ADJUST);
937 	else
938 		mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
939 			 MT_LPON_TCR_SW_ADJUST);
940 
941 	mutex_unlock(&dev->mt76.mutex);
942 }
943 
944 static void
mt7915_set_coverage_class(struct ieee80211_hw * hw,s16 coverage_class)945 mt7915_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
946 {
947 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
948 	struct mt7915_dev *dev = phy->dev;
949 
950 	mutex_lock(&dev->mt76.mutex);
951 	phy->coverage_class = max_t(s16, coverage_class, 0);
952 	mt7915_mac_set_timing(phy);
953 	mutex_unlock(&dev->mt76.mutex);
954 }
955 
956 static int
mt7915_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)957 mt7915_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
958 {
959 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
960 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
961 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
962 	bool ext_phy = phy != &dev->phy;
963 
964 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
965 		return -EINVAL;
966 
967 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
968 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
969 
970 	mutex_lock(&dev->mt76.mutex);
971 
972 	phy->mt76->antenna_mask = tx_ant;
973 
974 	if (ext_phy)
975 		tx_ant <<= dev->chainshift;
976 
977 	phy->mt76->chainmask = tx_ant;
978 
979 	mt76_set_stream_caps(phy->mt76, true);
980 	mt7915_set_stream_vht_txbf_caps(phy);
981 	mt7915_set_stream_he_caps(phy);
982 
983 	mutex_unlock(&dev->mt76.mutex);
984 
985 	return 0;
986 }
987 
mt7915_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)988 static void mt7915_sta_statistics(struct ieee80211_hw *hw,
989 				  struct ieee80211_vif *vif,
990 				  struct ieee80211_sta *sta,
991 				  struct station_info *sinfo)
992 {
993 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
994 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
995 	struct rate_info *txrate = &msta->wcid.rate;
996 	struct rate_info rxrate = {};
997 
998 	if (is_mt7915(&phy->dev->mt76) &&
999 	    !mt7915_mcu_get_rx_rate(phy, vif, sta, &rxrate)) {
1000 		sinfo->rxrate = rxrate;
1001 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
1002 	}
1003 
1004 	if (!txrate->legacy && !txrate->flags)
1005 		return;
1006 
1007 	if (txrate->legacy) {
1008 		sinfo->txrate.legacy = txrate->legacy;
1009 	} else {
1010 		sinfo->txrate.mcs = txrate->mcs;
1011 		sinfo->txrate.nss = txrate->nss;
1012 		sinfo->txrate.bw = txrate->bw;
1013 		sinfo->txrate.he_gi = txrate->he_gi;
1014 		sinfo->txrate.he_dcm = txrate->he_dcm;
1015 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1016 	}
1017 	sinfo->txrate.flags = txrate->flags;
1018 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1019 
1020 	/* offloading flows bypass networking stack, so driver counts and
1021 	 * reports sta statistics via NL80211_STA_INFO when WED is active.
1022 	 */
1023 	if (mtk_wed_device_active(&phy->dev->mt76.mmio.wed)) {
1024 		sinfo->tx_bytes = msta->wcid.stats.tx_bytes;
1025 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
1026 
1027 		sinfo->tx_packets = msta->wcid.stats.tx_packets;
1028 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
1029 
1030 		sinfo->tx_failed = msta->wcid.stats.tx_failed;
1031 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
1032 
1033 		sinfo->tx_retries = msta->wcid.stats.tx_retries;
1034 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
1035 	}
1036 }
1037 
mt7915_sta_rc_work(void * data,struct ieee80211_sta * sta)1038 static void mt7915_sta_rc_work(void *data, struct ieee80211_sta *sta)
1039 {
1040 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1041 	struct mt7915_dev *dev = msta->vif->phy->dev;
1042 	u32 *changed = data;
1043 
1044 	spin_lock_bh(&dev->sta_poll_lock);
1045 	msta->changed |= *changed;
1046 	if (list_empty(&msta->rc_list))
1047 		list_add_tail(&msta->rc_list, &dev->sta_rc_list);
1048 	spin_unlock_bh(&dev->sta_poll_lock);
1049 }
1050 
mt7915_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u32 changed)1051 static void mt7915_sta_rc_update(struct ieee80211_hw *hw,
1052 				 struct ieee80211_vif *vif,
1053 				 struct ieee80211_sta *sta,
1054 				 u32 changed)
1055 {
1056 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1057 	struct mt7915_dev *dev = phy->dev;
1058 
1059 	mt7915_sta_rc_work(&changed, sta);
1060 	ieee80211_queue_work(hw, &dev->rc_work);
1061 }
1062 
1063 static int
mt7915_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)1064 mt7915_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1065 			const struct cfg80211_bitrate_mask *mask)
1066 {
1067 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1068 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1069 	struct mt7915_dev *dev = phy->dev;
1070 	u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1071 
1072 	mvif->bitrate_mask = *mask;
1073 
1074 	/* if multiple rates across different preambles are given we can
1075 	 * reconfigure this info with all peers using sta_rec command with
1076 	 * the below exception cases.
1077 	 * - single rate : if a rate is passed along with different preambles,
1078 	 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers.
1079 	 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT
1080 	 * then multiple MCS setting (MCS 4,5,6) is not supported.
1081 	 */
1082 	ieee80211_iterate_stations_atomic(hw, mt7915_sta_rc_work, &changed);
1083 	ieee80211_queue_work(hw, &dev->rc_work);
1084 
1085 	return 0;
1086 }
1087 
mt7915_sta_set_4addr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1088 static void mt7915_sta_set_4addr(struct ieee80211_hw *hw,
1089 				 struct ieee80211_vif *vif,
1090 				 struct ieee80211_sta *sta,
1091 				 bool enabled)
1092 {
1093 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1094 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1095 
1096 	if (enabled)
1097 		set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1098 	else
1099 		clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1100 
1101 	mt76_connac_mcu_wtbl_update_hdr_trans(&dev->mt76, vif, sta);
1102 }
1103 
mt7915_sta_set_decap_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1104 static void mt7915_sta_set_decap_offload(struct ieee80211_hw *hw,
1105 				 struct ieee80211_vif *vif,
1106 				 struct ieee80211_sta *sta,
1107 				 bool enabled)
1108 {
1109 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1110 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1111 
1112 	if (enabled)
1113 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1114 	else
1115 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1116 
1117 	mt76_connac_mcu_wtbl_update_hdr_trans(&dev->mt76, vif, sta);
1118 }
1119 
1120 static const char mt7915_gstrings_stats[][ETH_GSTRING_LEN] = {
1121 	"tx_ampdu_cnt",
1122 	"tx_stop_q_empty_cnt",
1123 	"tx_mpdu_attempts",
1124 	"tx_mpdu_success",
1125 	"tx_rwp_fail_cnt",
1126 	"tx_rwp_need_cnt",
1127 	"tx_pkt_ebf_cnt",
1128 	"tx_pkt_ibf_cnt",
1129 	"tx_ampdu_len:0-1",
1130 	"tx_ampdu_len:2-10",
1131 	"tx_ampdu_len:11-19",
1132 	"tx_ampdu_len:20-28",
1133 	"tx_ampdu_len:29-37",
1134 	"tx_ampdu_len:38-46",
1135 	"tx_ampdu_len:47-55",
1136 	"tx_ampdu_len:56-79",
1137 	"tx_ampdu_len:80-103",
1138 	"tx_ampdu_len:104-127",
1139 	"tx_ampdu_len:128-151",
1140 	"tx_ampdu_len:152-175",
1141 	"tx_ampdu_len:176-199",
1142 	"tx_ampdu_len:200-223",
1143 	"tx_ampdu_len:224-247",
1144 	"ba_miss_count",
1145 	"tx_beamformer_ppdu_iBF",
1146 	"tx_beamformer_ppdu_eBF",
1147 	"tx_beamformer_rx_feedback_all",
1148 	"tx_beamformer_rx_feedback_he",
1149 	"tx_beamformer_rx_feedback_vht",
1150 	"tx_beamformer_rx_feedback_ht",
1151 	"tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */
1152 	"tx_beamformer_rx_feedback_nc",
1153 	"tx_beamformer_rx_feedback_nr",
1154 	"tx_beamformee_ok_feedback_pkts",
1155 	"tx_beamformee_feedback_trig",
1156 	"tx_mu_beamforming",
1157 	"tx_mu_mpdu",
1158 	"tx_mu_successful_mpdu",
1159 	"tx_su_successful_mpdu",
1160 	"tx_msdu_pack_1",
1161 	"tx_msdu_pack_2",
1162 	"tx_msdu_pack_3",
1163 	"tx_msdu_pack_4",
1164 	"tx_msdu_pack_5",
1165 	"tx_msdu_pack_6",
1166 	"tx_msdu_pack_7",
1167 	"tx_msdu_pack_8",
1168 
1169 	/* rx counters */
1170 	"rx_fifo_full_cnt",
1171 	"rx_mpdu_cnt",
1172 	"channel_idle_cnt",
1173 	"primary_cca_busy_time",
1174 	"secondary_cca_busy_time",
1175 	"primary_energy_detect_time",
1176 	"cck_mdrdy_time",
1177 	"ofdm_mdrdy_time",
1178 	"green_mdrdy_time",
1179 	"rx_vector_mismatch_cnt",
1180 	"rx_delimiter_fail_cnt",
1181 	"rx_mrdy_cnt",
1182 	"rx_len_mismatch_cnt",
1183 	"rx_ampdu_cnt",
1184 	"rx_ampdu_bytes_cnt",
1185 	"rx_ampdu_valid_subframe_cnt",
1186 	"rx_ampdu_valid_subframe_b_cnt",
1187 	"rx_pfdrop_cnt",
1188 	"rx_vec_queue_overflow_drop_cnt",
1189 	"rx_ba_cnt",
1190 
1191 	/* per vif counters */
1192 	"v_tx_mode_cck",
1193 	"v_tx_mode_ofdm",
1194 	"v_tx_mode_ht",
1195 	"v_tx_mode_ht_gf",
1196 	"v_tx_mode_vht",
1197 	"v_tx_mode_he_su",
1198 	"v_tx_mode_he_ext_su",
1199 	"v_tx_mode_he_tb",
1200 	"v_tx_mode_he_mu",
1201 	"v_tx_bw_20",
1202 	"v_tx_bw_40",
1203 	"v_tx_bw_80",
1204 	"v_tx_bw_160",
1205 	"v_tx_mcs_0",
1206 	"v_tx_mcs_1",
1207 	"v_tx_mcs_2",
1208 	"v_tx_mcs_3",
1209 	"v_tx_mcs_4",
1210 	"v_tx_mcs_5",
1211 	"v_tx_mcs_6",
1212 	"v_tx_mcs_7",
1213 	"v_tx_mcs_8",
1214 	"v_tx_mcs_9",
1215 	"v_tx_mcs_10",
1216 	"v_tx_mcs_11",
1217 };
1218 
1219 #define MT7915_SSTATS_LEN ARRAY_SIZE(mt7915_gstrings_stats)
1220 
1221 /* Ethtool related API */
1222 static
mt7915_get_et_strings(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 sset,u8 * data)1223 void mt7915_get_et_strings(struct ieee80211_hw *hw,
1224 			   struct ieee80211_vif *vif,
1225 			   u32 sset, u8 *data)
1226 {
1227 	if (sset == ETH_SS_STATS)
1228 		memcpy(data, *mt7915_gstrings_stats,
1229 		       sizeof(mt7915_gstrings_stats));
1230 }
1231 
1232 static
mt7915_get_et_sset_count(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int sset)1233 int mt7915_get_et_sset_count(struct ieee80211_hw *hw,
1234 			     struct ieee80211_vif *vif, int sset)
1235 {
1236 	if (sset == ETH_SS_STATS)
1237 		return MT7915_SSTATS_LEN;
1238 
1239 	return 0;
1240 }
1241 
mt7915_ethtool_worker(void * wi_data,struct ieee80211_sta * sta)1242 static void mt7915_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1243 {
1244 	struct mt76_ethtool_worker_info *wi = wi_data;
1245 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1246 
1247 	if (msta->vif->mt76.idx != wi->idx)
1248 		return;
1249 
1250 	mt76_ethtool_worker(wi, &msta->wcid.stats);
1251 }
1252 
1253 static
mt7915_get_et_stats(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ethtool_stats * stats,u64 * data)1254 void mt7915_get_et_stats(struct ieee80211_hw *hw,
1255 			 struct ieee80211_vif *vif,
1256 			 struct ethtool_stats *stats, u64 *data)
1257 {
1258 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1259 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1260 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1261 	struct mt76_ethtool_worker_info wi = {
1262 		.data = data,
1263 		.idx = mvif->mt76.idx,
1264 	};
1265 	struct mib_stats *mib = &phy->mib;
1266 	/* See mt7915_ampdu_stat_read_phy, etc */
1267 	int i, n, ei = 0;
1268 
1269 	mutex_lock(&dev->mt76.mutex);
1270 
1271 	mt7915_mac_update_stats(phy);
1272 
1273 	data[ei++] = mib->tx_ampdu_cnt;
1274 	data[ei++] = mib->tx_stop_q_empty_cnt;
1275 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1276 	data[ei++] = mib->tx_mpdu_success_cnt;
1277 	data[ei++] = mib->tx_rwp_fail_cnt;
1278 	data[ei++] = mib->tx_rwp_need_cnt;
1279 	data[ei++] = mib->tx_pkt_ebf_cnt;
1280 	data[ei++] = mib->tx_pkt_ibf_cnt;
1281 
1282 	/* Tx ampdu stat */
1283 	n = phy->band_idx ? ARRAY_SIZE(dev->mt76.aggr_stats) / 2 : 0;
1284 	for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++)
1285 		data[ei++] = dev->mt76.aggr_stats[i + n];
1286 
1287 	data[ei++] = phy->mib.ba_miss_cnt;
1288 
1289 	/* Tx Beamformer monitor */
1290 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1291 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1292 
1293 	/* Tx Beamformer Rx feedback monitor */
1294 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1295 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1296 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1297 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1298 
1299 	data[ei++] = mib->tx_bf_rx_fb_bw;
1300 	data[ei++] = mib->tx_bf_rx_fb_nc_cnt;
1301 	data[ei++] = mib->tx_bf_rx_fb_nr_cnt;
1302 
1303 	/* Tx Beamformee Rx NDPA & Tx feedback report */
1304 	data[ei++] = mib->tx_bf_fb_cpl_cnt;
1305 	data[ei++] = mib->tx_bf_fb_trig_cnt;
1306 
1307 	/* Tx SU & MU counters */
1308 	data[ei++] = mib->tx_bf_cnt;
1309 	data[ei++] = mib->tx_mu_mpdu_cnt;
1310 	data[ei++] = mib->tx_mu_acked_mpdu_cnt;
1311 	data[ei++] = mib->tx_su_acked_mpdu_cnt;
1312 
1313 	/* Tx amsdu info (pack-count histogram) */
1314 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1315 		data[ei++] = mib->tx_amsdu[i];
1316 
1317 	/* rx counters */
1318 	data[ei++] = mib->rx_fifo_full_cnt;
1319 	data[ei++] = mib->rx_mpdu_cnt;
1320 	data[ei++] = mib->channel_idle_cnt;
1321 	data[ei++] = mib->primary_cca_busy_time;
1322 	data[ei++] = mib->secondary_cca_busy_time;
1323 	data[ei++] = mib->primary_energy_detect_time;
1324 	data[ei++] = mib->cck_mdrdy_time;
1325 	data[ei++] = mib->ofdm_mdrdy_time;
1326 	data[ei++] = mib->green_mdrdy_time;
1327 	data[ei++] = mib->rx_vector_mismatch_cnt;
1328 	data[ei++] = mib->rx_delimiter_fail_cnt;
1329 	data[ei++] = mib->rx_mrdy_cnt;
1330 	data[ei++] = mib->rx_len_mismatch_cnt;
1331 	data[ei++] = mib->rx_ampdu_cnt;
1332 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1333 	data[ei++] = mib->rx_ampdu_valid_subframe_cnt;
1334 	data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt;
1335 	data[ei++] = mib->rx_pfdrop_cnt;
1336 	data[ei++] = mib->rx_vec_queue_overflow_drop_cnt;
1337 	data[ei++] = mib->rx_ba_cnt;
1338 
1339 	/* Add values for all stations owned by this vif */
1340 	wi.initial_stat_idx = ei;
1341 	ieee80211_iterate_stations_atomic(hw, mt7915_ethtool_worker, &wi);
1342 
1343 	mutex_unlock(&dev->mt76.mutex);
1344 
1345 	if (wi.sta_count == 0)
1346 		return;
1347 
1348 	ei += wi.worker_stat_count;
1349 	if (ei != MT7915_SSTATS_LEN)
1350 		dev_err(dev->mt76.dev, "ei: %d  MT7915_SSTATS_LEN: %d",
1351 			ei, (int)MT7915_SSTATS_LEN);
1352 }
1353 
1354 static void
mt7915_twt_teardown_request(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u8 flowid)1355 mt7915_twt_teardown_request(struct ieee80211_hw *hw,
1356 			    struct ieee80211_sta *sta,
1357 			    u8 flowid)
1358 {
1359 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1360 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1361 
1362 	mutex_lock(&dev->mt76.mutex);
1363 	mt7915_mac_twt_teardown_flow(dev, msta, flowid);
1364 	mutex_unlock(&dev->mt76.mutex);
1365 }
1366 
1367 static int
mt7915_set_radar_background(struct ieee80211_hw * hw,struct cfg80211_chan_def * chandef)1368 mt7915_set_radar_background(struct ieee80211_hw *hw,
1369 			    struct cfg80211_chan_def *chandef)
1370 {
1371 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1372 	struct mt7915_dev *dev = phy->dev;
1373 	int ret = -EINVAL;
1374 	bool running;
1375 
1376 	mutex_lock(&dev->mt76.mutex);
1377 
1378 	if (dev->mt76.region == NL80211_DFS_UNSET)
1379 		goto out;
1380 
1381 	if (dev->rdd2_phy && dev->rdd2_phy != phy) {
1382 		/* rdd2 is already locked */
1383 		ret = -EBUSY;
1384 		goto out;
1385 	}
1386 
1387 	/* rdd2 already configured on a radar channel */
1388 	running = dev->rdd2_phy &&
1389 		  cfg80211_chandef_valid(&dev->rdd2_chandef) &&
1390 		  !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR);
1391 
1392 	if (!chandef || running ||
1393 	    !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) {
1394 		ret = mt7915_mcu_rdd_background_enable(phy, NULL);
1395 		if (ret)
1396 			goto out;
1397 
1398 		if (!running)
1399 			goto update_phy;
1400 	}
1401 
1402 	ret = mt7915_mcu_rdd_background_enable(phy, chandef);
1403 	if (ret)
1404 		goto out;
1405 
1406 update_phy:
1407 	dev->rdd2_phy = chandef ? phy : NULL;
1408 	if (chandef)
1409 		dev->rdd2_chandef = *chandef;
1410 out:
1411 	mutex_unlock(&dev->mt76.mutex);
1412 
1413 	return ret;
1414 }
1415 
1416 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
1417 static int
mt7915_net_fill_forward_path(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct net_device_path_ctx * ctx,struct net_device_path * path)1418 mt7915_net_fill_forward_path(struct ieee80211_hw *hw,
1419 			     struct ieee80211_vif *vif,
1420 			     struct ieee80211_sta *sta,
1421 			     struct net_device_path_ctx *ctx,
1422 			     struct net_device_path *path)
1423 {
1424 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1425 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1426 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1427 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1428 	struct mtk_wed_device *wed = &dev->mt76.mmio.wed;
1429 
1430 	if (!mtk_wed_device_active(wed))
1431 		return -ENODEV;
1432 
1433 	if (msta->wcid.idx > 0xff)
1434 		return -EIO;
1435 
1436 	path->type = DEV_PATH_MTK_WDMA;
1437 	path->dev = ctx->dev;
1438 	path->mtk_wdma.wdma_idx = wed->wdma_idx;
1439 	path->mtk_wdma.bss = mvif->mt76.idx;
1440 	path->mtk_wdma.wcid = msta->wcid.idx;
1441 	path->mtk_wdma.queue = phy != &dev->phy;
1442 
1443 	ctx->dev = NULL;
1444 
1445 	return 0;
1446 }
1447 #endif
1448 
1449 const struct ieee80211_ops mt7915_ops = {
1450 	.tx = mt7915_tx,
1451 	.start = mt7915_start,
1452 	.stop = mt7915_stop,
1453 	.add_interface = mt7915_add_interface,
1454 	.remove_interface = mt7915_remove_interface,
1455 	.config = mt7915_config,
1456 	.conf_tx = mt7915_conf_tx,
1457 	.configure_filter = mt7915_configure_filter,
1458 	.bss_info_changed = mt7915_bss_info_changed,
1459 	.sta_add = mt7915_sta_add,
1460 	.sta_remove = mt7915_sta_remove,
1461 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1462 	.sta_rc_update = mt7915_sta_rc_update,
1463 	.set_key = mt7915_set_key,
1464 	.ampdu_action = mt7915_ampdu_action,
1465 	.set_rts_threshold = mt7915_set_rts_threshold,
1466 	.wake_tx_queue = mt76_wake_tx_queue,
1467 	.sw_scan_start = mt76_sw_scan,
1468 	.sw_scan_complete = mt76_sw_scan_complete,
1469 	.release_buffered_frames = mt76_release_buffered_frames,
1470 	.get_txpower = mt76_get_txpower,
1471 	.set_sar_specs = mt7915_set_sar_specs,
1472 	.channel_switch_beacon = mt7915_channel_switch_beacon,
1473 	.get_stats = mt7915_get_stats,
1474 	.get_et_sset_count = mt7915_get_et_sset_count,
1475 	.get_et_stats = mt7915_get_et_stats,
1476 	.get_et_strings = mt7915_get_et_strings,
1477 	.get_tsf = mt7915_get_tsf,
1478 	.set_tsf = mt7915_set_tsf,
1479 	.offset_tsf = mt7915_offset_tsf,
1480 	.get_survey = mt76_get_survey,
1481 	.get_antenna = mt76_get_antenna,
1482 	.set_antenna = mt7915_set_antenna,
1483 	.set_bitrate_mask = mt7915_set_bitrate_mask,
1484 	.set_coverage_class = mt7915_set_coverage_class,
1485 	.sta_statistics = mt7915_sta_statistics,
1486 	.sta_set_4addr = mt7915_sta_set_4addr,
1487 	.sta_set_decap_offload = mt7915_sta_set_decap_offload,
1488 	.add_twt_setup = mt7915_mac_add_twt_setup,
1489 	.twt_teardown_request = mt7915_twt_teardown_request,
1490 	CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1491 	CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1492 #ifdef CONFIG_MAC80211_DEBUGFS
1493 	.sta_add_debugfs = mt7915_sta_add_debugfs,
1494 #endif
1495 	.set_radar_background = mt7915_set_radar_background,
1496 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
1497 	.net_fill_forward_path = mt7915_net_fill_forward_path,
1498 #endif
1499 };
1500