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 
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 
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 = mt7915_mcu_set_pm(dev, 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 		ret = mt7915_mcu_set_scs(dev, 0, true);
46 		if (ret)
47 			goto out;
48 
49 		mt7915_mac_enable_nf(dev, 0);
50 	}
51 
52 	if (phy != &dev->phy) {
53 		ret = mt7915_mcu_set_pm(dev, 1, 0);
54 		if (ret)
55 			goto out;
56 
57 		ret = mt7915_mcu_set_mac(dev, 1, true, true);
58 		if (ret)
59 			goto out;
60 
61 		ret = mt7915_mcu_set_scs(dev, 1, true);
62 		if (ret)
63 			goto out;
64 
65 		mt7915_mac_enable_nf(dev, 1);
66 	}
67 
68 	ret = mt7915_mcu_set_rts_thresh(phy, 0x92b);
69 	if (ret)
70 		goto out;
71 
72 	ret = mt7915_mcu_set_sku_en(phy, true);
73 	if (ret)
74 		goto out;
75 
76 	ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
77 	if (ret)
78 		goto out;
79 
80 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
81 
82 	if (!mt76_testmode_enabled(phy->mt76))
83 		ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
84 					     MT7915_WATCHDOG_TIME);
85 
86 	if (!running)
87 		mt7915_mac_reset_counters(phy);
88 
89 out:
90 	mutex_unlock(&dev->mt76.mutex);
91 
92 	return ret;
93 }
94 
95 static void mt7915_stop(struct ieee80211_hw *hw)
96 {
97 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
98 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
99 
100 	cancel_delayed_work_sync(&phy->mt76->mac_work);
101 
102 	mutex_lock(&dev->mt76.mutex);
103 
104 	mt76_testmode_reset(phy->mt76, true);
105 
106 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
107 
108 	if (phy != &dev->phy) {
109 		mt7915_mcu_set_pm(dev, 1, 1);
110 		mt7915_mcu_set_mac(dev, 1, false, false);
111 	}
112 
113 	if (!mt7915_dev_running(dev)) {
114 		mt7915_mcu_set_pm(dev, 0, 1);
115 		mt7915_mcu_set_mac(dev, 0, false, false);
116 	}
117 
118 	mutex_unlock(&dev->mt76.mutex);
119 }
120 
121 static inline int get_free_idx(u32 mask, u8 start, u8 end)
122 {
123 	return ffs(~mask & GENMASK(end, start));
124 }
125 
126 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
127 {
128 	int i;
129 
130 	switch (type) {
131 	case NL80211_IFTYPE_MESH_POINT:
132 	case NL80211_IFTYPE_ADHOC:
133 	case NL80211_IFTYPE_STATION:
134 		/* prefer hw bssid slot 1-3 */
135 		i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
136 		if (i)
137 			return i - 1;
138 
139 		if (type != NL80211_IFTYPE_STATION)
140 			break;
141 
142 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
143 		if (i)
144 			return i - 1;
145 
146 		if (~mask & BIT(HW_BSSID_0))
147 			return HW_BSSID_0;
148 
149 		break;
150 	case NL80211_IFTYPE_MONITOR:
151 	case NL80211_IFTYPE_AP:
152 		/* ap uses hw bssid 0 and ext bssid */
153 		if (~mask & BIT(HW_BSSID_0))
154 			return HW_BSSID_0;
155 
156 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
157 		if (i)
158 			return i - 1;
159 
160 		break;
161 	default:
162 		WARN_ON(1);
163 		break;
164 	}
165 
166 	return -1;
167 }
168 
169 static void mt7915_init_bitrate_mask(struct ieee80211_vif *vif)
170 {
171 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
172 	int i;
173 
174 	for (i = 0; i < ARRAY_SIZE(mvif->bitrate_mask.control); i++) {
175 		mvif->bitrate_mask.control[i].legacy = GENMASK(31, 0);
176 		memset(mvif->bitrate_mask.control[i].ht_mcs, GENMASK(7, 0),
177 		       sizeof(mvif->bitrate_mask.control[i].ht_mcs));
178 		memset(mvif->bitrate_mask.control[i].vht_mcs, GENMASK(15, 0),
179 		       sizeof(mvif->bitrate_mask.control[i].vht_mcs));
180 		memset(mvif->bitrate_mask.control[i].he_mcs, GENMASK(15, 0),
181 		       sizeof(mvif->bitrate_mask.control[i].he_mcs));
182 	}
183 }
184 
185 static int mt7915_add_interface(struct ieee80211_hw *hw,
186 				struct ieee80211_vif *vif)
187 {
188 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
189 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
190 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
191 	struct mt76_txq *mtxq;
192 	bool ext_phy = phy != &dev->phy;
193 	int idx, ret = 0;
194 
195 	mutex_lock(&dev->mt76.mutex);
196 
197 	mt76_testmode_reset(phy->mt76, true);
198 
199 	if (vif->type == NL80211_IFTYPE_MONITOR &&
200 	    is_zero_ether_addr(vif->addr))
201 		phy->monitor_vif = vif;
202 
203 	mvif->idx = ffs(~dev->mt76.vif_mask) - 1;
204 	if (mvif->idx >= MT7915_MAX_INTERFACES) {
205 		ret = -ENOSPC;
206 		goto out;
207 	}
208 
209 	idx = get_omac_idx(vif->type, phy->omac_mask);
210 	if (idx < 0) {
211 		ret = -ENOSPC;
212 		goto out;
213 	}
214 	mvif->omac_idx = idx;
215 	mvif->phy = phy;
216 	mvif->band_idx = ext_phy;
217 
218 	if (ext_phy)
219 		mvif->wmm_idx = ext_phy * (MT7915_MAX_WMM_SETS / 2) +
220 				mvif->idx % (MT7915_MAX_WMM_SETS / 2);
221 	else
222 		mvif->wmm_idx = mvif->idx % MT7915_MAX_WMM_SETS;
223 
224 	ret = mt7915_mcu_add_dev_info(phy, vif, true);
225 	if (ret)
226 		goto out;
227 
228 	dev->mt76.vif_mask |= BIT(mvif->idx);
229 	phy->omac_mask |= BIT_ULL(mvif->omac_idx);
230 
231 	idx = MT7915_WTBL_RESERVED - mvif->idx;
232 
233 	INIT_LIST_HEAD(&mvif->sta.rc_list);
234 	INIT_LIST_HEAD(&mvif->sta.stats_list);
235 	INIT_LIST_HEAD(&mvif->sta.poll_list);
236 	mvif->sta.wcid.idx = idx;
237 	mvif->sta.wcid.ext_phy = mvif->band_idx;
238 	mvif->sta.wcid.hw_key_idx = -1;
239 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
240 	mt7915_mac_wtbl_update(dev, idx,
241 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
242 
243 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
244 	if (vif->txq) {
245 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
246 		mtxq->wcid = &mvif->sta.wcid;
247 	}
248 
249 	if (vif->type != NL80211_IFTYPE_AP &&
250 	    (!mvif->omac_idx || mvif->omac_idx > 3))
251 		vif->offload_flags = 0;
252 	vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
253 
254 	mt7915_init_bitrate_mask(vif);
255 
256 out:
257 	mutex_unlock(&dev->mt76.mutex);
258 
259 	return ret;
260 }
261 
262 static void mt7915_remove_interface(struct ieee80211_hw *hw,
263 				    struct ieee80211_vif *vif)
264 {
265 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
266 	struct mt7915_sta *msta = &mvif->sta;
267 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
268 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
269 	int idx = msta->wcid.idx;
270 
271 	mt7915_mcu_add_bss_info(phy, vif, false);
272 	mt7915_mcu_add_sta(dev, vif, NULL, false);
273 
274 	mutex_lock(&dev->mt76.mutex);
275 	mt76_testmode_reset(phy->mt76, true);
276 	mutex_unlock(&dev->mt76.mutex);
277 
278 	if (vif == phy->monitor_vif)
279 		phy->monitor_vif = NULL;
280 
281 	mt7915_mcu_add_dev_info(phy, vif, false);
282 
283 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
284 
285 	mutex_lock(&dev->mt76.mutex);
286 	dev->mt76.vif_mask &= ~BIT(mvif->idx);
287 	phy->omac_mask &= ~BIT_ULL(mvif->omac_idx);
288 	mutex_unlock(&dev->mt76.mutex);
289 
290 	spin_lock_bh(&dev->sta_poll_lock);
291 	if (!list_empty(&msta->poll_list))
292 		list_del_init(&msta->poll_list);
293 	spin_unlock_bh(&dev->sta_poll_lock);
294 }
295 
296 static void mt7915_init_dfs_state(struct mt7915_phy *phy)
297 {
298 	struct mt76_phy *mphy = phy->mt76;
299 	struct ieee80211_hw *hw = mphy->hw;
300 	struct cfg80211_chan_def *chandef = &hw->conf.chandef;
301 
302 	if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
303 		return;
304 
305 	if (!(chandef->chan->flags & IEEE80211_CHAN_RADAR))
306 		return;
307 
308 	if (mphy->chandef.chan->center_freq == chandef->chan->center_freq &&
309 	    mphy->chandef.width == chandef->width)
310 		return;
311 
312 	phy->dfs_state = -1;
313 }
314 
315 int mt7915_set_channel(struct mt7915_phy *phy)
316 {
317 	struct mt7915_dev *dev = phy->dev;
318 	int ret;
319 
320 	cancel_delayed_work_sync(&phy->mt76->mac_work);
321 
322 	mutex_lock(&dev->mt76.mutex);
323 	set_bit(MT76_RESET, &phy->mt76->state);
324 
325 	mt7915_init_dfs_state(phy);
326 	mt76_set_channel(phy->mt76);
327 
328 	if (dev->flash_mode) {
329 		ret = mt7915_mcu_apply_tx_dpd(phy);
330 		if (ret)
331 			goto out;
332 	}
333 
334 	ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
335 	if (ret)
336 		goto out;
337 
338 	mt7915_mac_set_timing(phy);
339 	ret = mt7915_dfs_init_radar_detector(phy);
340 	mt7915_mac_cca_stats_reset(phy);
341 
342 	mt7915_mac_reset_counters(phy);
343 	phy->noise = 0;
344 
345 out:
346 	clear_bit(MT76_RESET, &phy->mt76->state);
347 	mutex_unlock(&dev->mt76.mutex);
348 
349 	mt76_txq_schedule_all(phy->mt76);
350 
351 	if (!mt76_testmode_enabled(phy->mt76))
352 		ieee80211_queue_delayed_work(phy->mt76->hw,
353 					     &phy->mt76->mac_work,
354 					     MT7915_WATCHDOG_TIME);
355 
356 	return ret;
357 }
358 
359 static int mt7915_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
360 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
361 			  struct ieee80211_key_conf *key)
362 {
363 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
364 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
365 	struct mt7915_sta *msta = sta ? (struct mt7915_sta *)sta->drv_priv :
366 				  &mvif->sta;
367 	struct mt76_wcid *wcid = &msta->wcid;
368 	u8 *wcid_keyidx = &wcid->hw_key_idx;
369 	int idx = key->keyidx;
370 	int err = 0;
371 
372 	/* The hardware does not support per-STA RX GTK, fallback
373 	 * to software mode for these.
374 	 */
375 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
376 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
377 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
378 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
379 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
380 		return -EOPNOTSUPP;
381 
382 	/* fall back to sw encryption for unsupported ciphers */
383 	switch (key->cipher) {
384 	case WLAN_CIPHER_SUITE_AES_CMAC:
385 		wcid_keyidx = &wcid->hw_key_idx2;
386 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
387 		break;
388 	case WLAN_CIPHER_SUITE_TKIP:
389 	case WLAN_CIPHER_SUITE_CCMP:
390 	case WLAN_CIPHER_SUITE_CCMP_256:
391 	case WLAN_CIPHER_SUITE_GCMP:
392 	case WLAN_CIPHER_SUITE_GCMP_256:
393 	case WLAN_CIPHER_SUITE_SMS4:
394 		break;
395 	case WLAN_CIPHER_SUITE_WEP40:
396 	case WLAN_CIPHER_SUITE_WEP104:
397 	default:
398 		return -EOPNOTSUPP;
399 	}
400 
401 	mutex_lock(&dev->mt76.mutex);
402 
403 	if (cmd == SET_KEY)
404 		*wcid_keyidx = idx;
405 	else if (idx == *wcid_keyidx)
406 		*wcid_keyidx = -1;
407 	else
408 		goto out;
409 
410 	mt76_wcid_key_setup(&dev->mt76, wcid,
411 			    cmd == SET_KEY ? key : NULL);
412 
413 	err = mt7915_mcu_add_key(dev, vif, msta, key, cmd);
414 
415 out:
416 	mutex_unlock(&dev->mt76.mutex);
417 
418 	return err;
419 }
420 
421 static int mt7915_config(struct ieee80211_hw *hw, u32 changed)
422 {
423 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
424 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
425 	bool band = phy != &dev->phy;
426 	int ret;
427 
428 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
429 #ifdef CONFIG_NL80211_TESTMODE
430 		if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
431 			mutex_lock(&dev->mt76.mutex);
432 			mt76_testmode_reset(phy->mt76, false);
433 			mutex_unlock(&dev->mt76.mutex);
434 		}
435 #endif
436 		ieee80211_stop_queues(hw);
437 		ret = mt7915_set_channel(phy);
438 		if (ret)
439 			return ret;
440 		ieee80211_wake_queues(hw);
441 	}
442 
443 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
444 		ret = mt7915_mcu_set_txpower_sku(phy);
445 		if (ret)
446 			return ret;
447 	}
448 
449 	mutex_lock(&dev->mt76.mutex);
450 
451 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
452 		bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
453 
454 		if (!enabled)
455 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
456 		else
457 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
458 
459 		mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN,
460 			       enabled);
461 		mt76_testmode_reset(phy->mt76, true);
462 		mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
463 	}
464 
465 	mutex_unlock(&dev->mt76.mutex);
466 
467 	return 0;
468 }
469 
470 static int
471 mt7915_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
472 	       const struct ieee80211_tx_queue_params *params)
473 {
474 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
475 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
476 
477 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
478 	queue = mt7915_lmac_mapping(dev, queue);
479 	mvif->queue_params[queue] = *params;
480 
481 	return 0;
482 }
483 
484 static void mt7915_configure_filter(struct ieee80211_hw *hw,
485 				    unsigned int changed_flags,
486 				    unsigned int *total_flags,
487 				    u64 multicast)
488 {
489 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
490 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
491 	bool band = phy != &dev->phy;
492 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
493 			MT_WF_RFCR1_DROP_BF_POLL |
494 			MT_WF_RFCR1_DROP_BA |
495 			MT_WF_RFCR1_DROP_CFEND |
496 			MT_WF_RFCR1_DROP_CFACK;
497 	u32 flags = 0;
498 
499 #define MT76_FILTER(_flag, _hw) do {					\
500 		flags |= *total_flags & FIF_##_flag;			\
501 		phy->rxfilter &= ~(_hw);				\
502 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
503 	} while (0)
504 
505 	mutex_lock(&dev->mt76.mutex);
506 
507 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
508 			   MT_WF_RFCR_DROP_OTHER_BEACON |
509 			   MT_WF_RFCR_DROP_FRAME_REPORT |
510 			   MT_WF_RFCR_DROP_PROBEREQ |
511 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
512 			   MT_WF_RFCR_DROP_MCAST |
513 			   MT_WF_RFCR_DROP_BCAST |
514 			   MT_WF_RFCR_DROP_DUPLICATE |
515 			   MT_WF_RFCR_DROP_A2_BSSID |
516 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
517 			   MT_WF_RFCR_DROP_STBC_MULTI);
518 
519 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
520 			       MT_WF_RFCR_DROP_A3_MAC |
521 			       MT_WF_RFCR_DROP_A3_BSSID);
522 
523 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
524 
525 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
526 			     MT_WF_RFCR_DROP_RTS |
527 			     MT_WF_RFCR_DROP_CTL_RSV |
528 			     MT_WF_RFCR_DROP_NDPA);
529 
530 	*total_flags = flags;
531 	mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
532 
533 	if (*total_flags & FIF_CONTROL)
534 		mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
535 	else
536 		mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
537 
538 	mutex_unlock(&dev->mt76.mutex);
539 }
540 
541 static void mt7915_bss_info_changed(struct ieee80211_hw *hw,
542 				    struct ieee80211_vif *vif,
543 				    struct ieee80211_bss_conf *info,
544 				    u32 changed)
545 {
546 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
547 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
548 
549 	mutex_lock(&dev->mt76.mutex);
550 
551 	/*
552 	 * station mode uses BSSID to map the wlan entry to a peer,
553 	 * and then peer references bss_info_rfch to set bandwidth cap.
554 	 */
555 	if (changed & BSS_CHANGED_BSSID &&
556 	    vif->type == NL80211_IFTYPE_STATION) {
557 		bool join = !is_zero_ether_addr(info->bssid);
558 
559 		mt7915_mcu_add_bss_info(phy, vif, join);
560 		mt7915_mcu_add_sta(dev, vif, NULL, join);
561 	}
562 
563 	if (changed & BSS_CHANGED_ASSOC) {
564 		mt7915_mcu_add_bss_info(phy, vif, info->assoc);
565 		mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
566 	}
567 
568 	if (changed & BSS_CHANGED_ERP_SLOT) {
569 		int slottime = info->use_short_slot ? 9 : 20;
570 
571 		if (slottime != phy->slottime) {
572 			phy->slottime = slottime;
573 			mt7915_mac_set_timing(phy);
574 		}
575 	}
576 
577 	if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
578 		mt7915_mcu_add_bss_info(phy, vif, true);
579 		mt7915_mcu_add_sta(dev, vif, NULL, true);
580 	}
581 
582 	/* ensure that enable txcmd_mode after bss_info */
583 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
584 		mt7915_mcu_set_tx(dev, vif);
585 
586 	if (changed & BSS_CHANGED_HE_OBSS_PD)
587 		mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
588 
589 	if (changed & (BSS_CHANGED_BEACON |
590 		       BSS_CHANGED_BEACON_ENABLED))
591 		mt7915_mcu_add_beacon(hw, vif, info->enable_beacon);
592 
593 	mutex_unlock(&dev->mt76.mutex);
594 }
595 
596 static void
597 mt7915_channel_switch_beacon(struct ieee80211_hw *hw,
598 			     struct ieee80211_vif *vif,
599 			     struct cfg80211_chan_def *chandef)
600 {
601 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
602 
603 	mutex_lock(&dev->mt76.mutex);
604 	mt7915_mcu_add_beacon(hw, vif, true);
605 	mutex_unlock(&dev->mt76.mutex);
606 }
607 
608 int mt7915_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
609 		       struct ieee80211_sta *sta)
610 {
611 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
612 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
613 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
614 	int ret, idx;
615 
616 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA - 1);
617 	if (idx < 0)
618 		return -ENOSPC;
619 
620 	INIT_LIST_HEAD(&msta->rc_list);
621 	INIT_LIST_HEAD(&msta->stats_list);
622 	INIT_LIST_HEAD(&msta->poll_list);
623 	msta->vif = mvif;
624 	msta->wcid.sta = 1;
625 	msta->wcid.idx = idx;
626 	msta->wcid.ext_phy = mvif->band_idx;
627 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
628 	msta->stats.jiffies = jiffies;
629 
630 	mt7915_mac_wtbl_update(dev, idx,
631 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
632 
633 	ret = mt7915_mcu_add_sta(dev, vif, sta, true);
634 	if (ret)
635 		return ret;
636 
637 	return mt7915_mcu_add_sta_adv(dev, vif, sta, true);
638 }
639 
640 void mt7915_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
641 			   struct ieee80211_sta *sta)
642 {
643 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
644 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
645 
646 	mt7915_mcu_add_sta_adv(dev, vif, sta, false);
647 	mt7915_mcu_add_sta(dev, vif, sta, false);
648 
649 	mt7915_mac_wtbl_update(dev, msta->wcid.idx,
650 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
651 
652 	spin_lock_bh(&dev->sta_poll_lock);
653 	if (!list_empty(&msta->poll_list))
654 		list_del_init(&msta->poll_list);
655 	if (!list_empty(&msta->stats_list))
656 		list_del_init(&msta->stats_list);
657 	if (!list_empty(&msta->rc_list))
658 		list_del_init(&msta->rc_list);
659 	spin_unlock_bh(&dev->sta_poll_lock);
660 }
661 
662 static void mt7915_tx(struct ieee80211_hw *hw,
663 		      struct ieee80211_tx_control *control,
664 		      struct sk_buff *skb)
665 {
666 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
667 	struct mt76_phy *mphy = hw->priv;
668 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
669 	struct ieee80211_vif *vif = info->control.vif;
670 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
671 
672 	if (control->sta) {
673 		struct mt7915_sta *sta;
674 
675 		sta = (struct mt7915_sta *)control->sta->drv_priv;
676 		wcid = &sta->wcid;
677 	}
678 
679 	if (vif && !control->sta) {
680 		struct mt7915_vif *mvif;
681 
682 		mvif = (struct mt7915_vif *)vif->drv_priv;
683 		wcid = &mvif->sta.wcid;
684 	}
685 
686 	mt76_tx(mphy, control->sta, wcid, skb);
687 }
688 
689 static int mt7915_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
690 {
691 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
692 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
693 	int ret;
694 
695 	mutex_lock(&dev->mt76.mutex);
696 	ret = mt7915_mcu_set_rts_thresh(phy, val);
697 	mutex_unlock(&dev->mt76.mutex);
698 
699 	return ret;
700 }
701 
702 static int
703 mt7915_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
704 		    struct ieee80211_ampdu_params *params)
705 {
706 	enum ieee80211_ampdu_mlme_action action = params->action;
707 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
708 	struct ieee80211_sta *sta = params->sta;
709 	struct ieee80211_txq *txq = sta->txq[params->tid];
710 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
711 	u16 tid = params->tid;
712 	u16 ssn = params->ssn;
713 	struct mt76_txq *mtxq;
714 	int ret = 0;
715 
716 	if (!txq)
717 		return -EINVAL;
718 
719 	mtxq = (struct mt76_txq *)txq->drv_priv;
720 
721 	mutex_lock(&dev->mt76.mutex);
722 	switch (action) {
723 	case IEEE80211_AMPDU_RX_START:
724 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
725 				   params->buf_size);
726 		ret = mt7915_mcu_add_rx_ba(dev, params, true);
727 		break;
728 	case IEEE80211_AMPDU_RX_STOP:
729 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
730 		ret = mt7915_mcu_add_rx_ba(dev, params, false);
731 		break;
732 	case IEEE80211_AMPDU_TX_OPERATIONAL:
733 		mtxq->aggr = true;
734 		mtxq->send_bar = false;
735 		ret = mt7915_mcu_add_tx_ba(dev, params, true);
736 		break;
737 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
738 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
739 		mtxq->aggr = false;
740 		clear_bit(tid, &msta->ampdu_state);
741 		ret = mt7915_mcu_add_tx_ba(dev, params, false);
742 		break;
743 	case IEEE80211_AMPDU_TX_START:
744 		set_bit(tid, &msta->ampdu_state);
745 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
746 		break;
747 	case IEEE80211_AMPDU_TX_STOP_CONT:
748 		mtxq->aggr = false;
749 		clear_bit(tid, &msta->ampdu_state);
750 		ret = mt7915_mcu_add_tx_ba(dev, params, false);
751 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
752 		break;
753 	}
754 	mutex_unlock(&dev->mt76.mutex);
755 
756 	return ret;
757 }
758 
759 static int
760 mt7915_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
761 	       struct ieee80211_sta *sta)
762 {
763 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
764 			      IEEE80211_STA_NONE);
765 }
766 
767 static int
768 mt7915_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
769 		  struct ieee80211_sta *sta)
770 {
771 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
772 			      IEEE80211_STA_NOTEXIST);
773 }
774 
775 static int
776 mt7915_get_stats(struct ieee80211_hw *hw,
777 		 struct ieee80211_low_level_stats *stats)
778 {
779 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
780 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
781 	struct mib_stats *mib = &phy->mib;
782 
783 	mutex_lock(&dev->mt76.mutex);
784 	stats->dot11RTSSuccessCount = mib->rts_cnt;
785 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
786 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
787 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
788 
789 	memset(mib, 0, sizeof(*mib));
790 
791 	mutex_unlock(&dev->mt76.mutex);
792 
793 	return 0;
794 }
795 
796 static u64
797 mt7915_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
798 {
799 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
800 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
801 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
802 	bool band = phy != &dev->phy;
803 	union {
804 		u64 t64;
805 		u32 t32[2];
806 	} tsf;
807 	u16 n;
808 
809 	mutex_lock(&dev->mt76.mutex);
810 
811 	n = mvif->omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : mvif->omac_idx;
812 	/* TSF software read */
813 	mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
814 		 MT_LPON_TCR_SW_READ);
815 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band));
816 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band));
817 
818 	mutex_unlock(&dev->mt76.mutex);
819 
820 	return tsf.t64;
821 }
822 
823 static void
824 mt7915_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
825 	       u64 timestamp)
826 {
827 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
828 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
829 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
830 	bool band = phy != &dev->phy;
831 	union {
832 		u64 t64;
833 		u32 t32[2];
834 	} tsf = { .t64 = timestamp, };
835 	u16 n;
836 
837 	mutex_lock(&dev->mt76.mutex);
838 
839 	n = mvif->omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : mvif->omac_idx;
840 	mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
841 	mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
842 	/* TSF software overwrite */
843 	mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
844 		 MT_LPON_TCR_SW_WRITE);
845 
846 	mutex_unlock(&dev->mt76.mutex);
847 }
848 
849 static void
850 mt7915_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
851 		  s64 timestamp)
852 {
853 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
854 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
855 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
856 	bool band = phy != &dev->phy;
857 	union {
858 		u64 t64;
859 		u32 t32[2];
860 	} tsf = { .t64 = timestamp, };
861 	u16 n;
862 
863 	mutex_lock(&dev->mt76.mutex);
864 
865 	n = mvif->omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : mvif->omac_idx;
866 	mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
867 	mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
868 	/* TSF software adjust*/
869 	mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
870 		 MT_LPON_TCR_SW_ADJUST);
871 
872 	mutex_unlock(&dev->mt76.mutex);
873 }
874 
875 static void
876 mt7915_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
877 {
878 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
879 	struct mt7915_dev *dev = phy->dev;
880 
881 	mutex_lock(&dev->mt76.mutex);
882 	phy->coverage_class = max_t(s16, coverage_class, 0);
883 	mt7915_mac_set_timing(phy);
884 	mutex_unlock(&dev->mt76.mutex);
885 }
886 
887 static int
888 mt7915_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
889 {
890 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
891 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
892 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
893 	bool ext_phy = phy != &dev->phy;
894 
895 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
896 		return -EINVAL;
897 
898 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
899 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
900 
901 	mutex_lock(&dev->mt76.mutex);
902 
903 	phy->mt76->antenna_mask = tx_ant;
904 
905 	if (ext_phy) {
906 		if (dev->chainmask == 0xf)
907 			tx_ant <<= 2;
908 		else
909 			tx_ant <<= 1;
910 	}
911 	phy->mt76->chainmask = tx_ant;
912 
913 	mt76_set_stream_caps(phy->mt76, true);
914 	mt7915_set_stream_vht_txbf_caps(phy);
915 	mt7915_set_stream_he_caps(phy);
916 
917 	mutex_unlock(&dev->mt76.mutex);
918 
919 	return 0;
920 }
921 
922 static void mt7915_sta_statistics(struct ieee80211_hw *hw,
923 				  struct ieee80211_vif *vif,
924 				  struct ieee80211_sta *sta,
925 				  struct station_info *sinfo)
926 {
927 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
928 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
929 	struct mt7915_sta_stats *stats = &msta->stats;
930 	struct rate_info rxrate = {};
931 
932 	if (!mt7915_mcu_get_rx_rate(phy, vif, sta, &rxrate)) {
933 		sinfo->rxrate = rxrate;
934 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
935 	}
936 
937 	if (!stats->tx_rate.legacy && !stats->tx_rate.flags)
938 		return;
939 
940 	if (stats->tx_rate.legacy) {
941 		sinfo->txrate.legacy = stats->tx_rate.legacy;
942 	} else {
943 		sinfo->txrate.mcs = stats->tx_rate.mcs;
944 		sinfo->txrate.nss = stats->tx_rate.nss;
945 		sinfo->txrate.bw = stats->tx_rate.bw;
946 		sinfo->txrate.he_gi = stats->tx_rate.he_gi;
947 		sinfo->txrate.he_dcm = stats->tx_rate.he_dcm;
948 		sinfo->txrate.he_ru_alloc = stats->tx_rate.he_ru_alloc;
949 	}
950 	sinfo->txrate.flags = stats->tx_rate.flags;
951 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
952 }
953 
954 static void mt7915_sta_rc_work(void *data, struct ieee80211_sta *sta)
955 {
956 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
957 	struct mt7915_dev *dev = msta->vif->phy->dev;
958 	struct ieee80211_hw *hw = msta->vif->phy->mt76->hw;
959 	u32 *changed = data;
960 
961 	spin_lock_bh(&dev->sta_poll_lock);
962 	msta->stats.changed |= *changed;
963 	if (list_empty(&msta->rc_list))
964 		list_add_tail(&msta->rc_list, &dev->sta_rc_list);
965 	spin_unlock_bh(&dev->sta_poll_lock);
966 
967 	ieee80211_queue_work(hw, &dev->rc_work);
968 }
969 
970 static void mt7915_sta_rc_update(struct ieee80211_hw *hw,
971 				 struct ieee80211_vif *vif,
972 				 struct ieee80211_sta *sta,
973 				 u32 changed)
974 {
975 	mt7915_sta_rc_work(&changed, sta);
976 }
977 
978 static int
979 mt7915_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
980 			const struct cfg80211_bitrate_mask *mask)
981 {
982 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
983 	enum nl80211_band band = mvif->phy->mt76->chandef.chan->band;
984 	u32 changed;
985 
986 	if (mask->control[band].gi == NL80211_TXRATE_FORCE_LGI)
987 		return -EINVAL;
988 
989 	changed = IEEE80211_RC_SUPP_RATES_CHANGED;
990 	mvif->bitrate_mask = *mask;
991 
992 	/* Update firmware rate control to add a boundary on top of table
993 	 * to limit the rate selection for each peer, so when set bitrates
994 	 * vht-mcs-5 1:9, which actually means nss = 1 mcs = 0~9. This only
995 	 * applies to data frames as for the other mgmt, mcast, bcast still
996 	 * use legacy rates as it is.
997 	 */
998 	ieee80211_iterate_stations_atomic(hw, mt7915_sta_rc_work, &changed);
999 
1000 	return 0;
1001 }
1002 
1003 static void mt7915_sta_set_4addr(struct ieee80211_hw *hw,
1004 				 struct ieee80211_vif *vif,
1005 				 struct ieee80211_sta *sta,
1006 				 bool enabled)
1007 {
1008 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1009 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1010 
1011 	if (enabled)
1012 		set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1013 	else
1014 		clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1015 
1016 	mt7915_mcu_sta_update_hdr_trans(dev, vif, sta);
1017 }
1018 
1019 static void mt7915_sta_set_decap_offload(struct ieee80211_hw *hw,
1020 				 struct ieee80211_vif *vif,
1021 				 struct ieee80211_sta *sta,
1022 				 bool enabled)
1023 {
1024 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1025 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1026 
1027 	if (enabled)
1028 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1029 	else
1030 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1031 
1032 	mt7915_mcu_sta_update_hdr_trans(dev, vif, sta);
1033 }
1034 
1035 const struct ieee80211_ops mt7915_ops = {
1036 	.tx = mt7915_tx,
1037 	.start = mt7915_start,
1038 	.stop = mt7915_stop,
1039 	.add_interface = mt7915_add_interface,
1040 	.remove_interface = mt7915_remove_interface,
1041 	.config = mt7915_config,
1042 	.conf_tx = mt7915_conf_tx,
1043 	.configure_filter = mt7915_configure_filter,
1044 	.bss_info_changed = mt7915_bss_info_changed,
1045 	.sta_add = mt7915_sta_add,
1046 	.sta_remove = mt7915_sta_remove,
1047 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1048 	.sta_rc_update = mt7915_sta_rc_update,
1049 	.set_key = mt7915_set_key,
1050 	.ampdu_action = mt7915_ampdu_action,
1051 	.set_rts_threshold = mt7915_set_rts_threshold,
1052 	.wake_tx_queue = mt76_wake_tx_queue,
1053 	.sw_scan_start = mt76_sw_scan,
1054 	.sw_scan_complete = mt76_sw_scan_complete,
1055 	.release_buffered_frames = mt76_release_buffered_frames,
1056 	.get_txpower = mt76_get_txpower,
1057 	.channel_switch_beacon = mt7915_channel_switch_beacon,
1058 	.get_stats = mt7915_get_stats,
1059 	.get_tsf = mt7915_get_tsf,
1060 	.set_tsf = mt7915_set_tsf,
1061 	.offset_tsf = mt7915_offset_tsf,
1062 	.get_survey = mt76_get_survey,
1063 	.get_antenna = mt76_get_antenna,
1064 	.set_antenna = mt7915_set_antenna,
1065 	.set_bitrate_mask = mt7915_set_bitrate_mask,
1066 	.set_coverage_class = mt7915_set_coverage_class,
1067 	.sta_statistics = mt7915_sta_statistics,
1068 	.sta_set_4addr = mt7915_sta_set_4addr,
1069 	.sta_set_decap_offload = mt7915_sta_set_decap_offload,
1070 	CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1071 	CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1072 #ifdef CONFIG_MAC80211_DEBUGFS
1073 	.sta_add_debugfs = mt7915_sta_add_debugfs,
1074 #endif
1075 };
1076