1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2019 MediaTek Inc.
3  *
4  * Author: Roy Luo <royluo@google.com>
5  *         Ryder Lee <ryder.lee@mediatek.com>
6  *         Felix Fietkau <nbd@nbd.name>
7  *         Lorenzo Bianconi <lorenzo@kernel.org>
8  */
9 
10 #include <linux/etherdevice.h>
11 #include <linux/module.h>
12 #include "mt7615.h"
13 #include "mcu.h"
14 
15 static bool mt7615_dev_running(struct mt7615_dev *dev)
16 {
17 	struct mt7615_phy *phy;
18 
19 	if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
20 		return true;
21 
22 	phy = mt7615_ext_phy(dev);
23 
24 	return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
25 }
26 
27 static int mt7615_start(struct ieee80211_hw *hw)
28 {
29 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
30 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
31 	unsigned long timeout;
32 	bool running;
33 	int ret;
34 
35 	if (!mt7615_wait_for_mcu_init(dev))
36 		return -EIO;
37 
38 	mt7615_mutex_acquire(dev);
39 
40 	running = mt7615_dev_running(dev);
41 
42 	if (!running) {
43 		ret = mt7615_mcu_set_pm(dev, 0, 0);
44 		if (ret)
45 			goto out;
46 
47 		ret = mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, true, false);
48 		if (ret)
49 			goto out;
50 
51 		mt7615_mac_enable_nf(dev, 0);
52 	}
53 
54 	if (phy != &dev->phy) {
55 		ret = mt7615_mcu_set_pm(dev, 1, 0);
56 		if (ret)
57 			goto out;
58 
59 		ret = mt76_connac_mcu_set_mac_enable(&dev->mt76, 1, true, false);
60 		if (ret)
61 			goto out;
62 
63 		mt7615_mac_enable_nf(dev, 1);
64 	}
65 
66 	if (mt7615_firmware_offload(dev)) {
67 		ret = mt76_connac_mcu_set_channel_domain(phy->mt76);
68 		if (ret)
69 			goto out;
70 
71 		ret = mt76_connac_mcu_set_rate_txpower(phy->mt76);
72 		if (ret)
73 			goto out;
74 	}
75 
76 	ret = mt7615_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 	timeout = mt7615_get_macwork_timeout(dev);
83 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, timeout);
84 
85 	if (!running)
86 		mt7615_mac_reset_counters(dev);
87 
88 out:
89 	mt7615_mutex_release(dev);
90 
91 	return ret;
92 }
93 
94 static void mt7615_stop(struct ieee80211_hw *hw)
95 {
96 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
97 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
98 
99 	cancel_delayed_work_sync(&phy->mt76->mac_work);
100 	del_timer_sync(&phy->roc_timer);
101 	cancel_work_sync(&phy->roc_work);
102 
103 	cancel_delayed_work_sync(&dev->pm.ps_work);
104 	cancel_work_sync(&dev->pm.wake_work);
105 
106 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
107 
108 	mt7615_mutex_acquire(dev);
109 
110 	mt76_testmode_reset(phy->mt76, true);
111 
112 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
113 	cancel_delayed_work_sync(&phy->scan_work);
114 
115 	if (phy != &dev->phy) {
116 		mt7615_mcu_set_pm(dev, 1, 1);
117 		mt76_connac_mcu_set_mac_enable(&dev->mt76, 1, false, false);
118 	}
119 
120 	if (!mt7615_dev_running(dev)) {
121 		mt7615_mcu_set_pm(dev, 0, 1);
122 		mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
123 	}
124 
125 	mt7615_mutex_release(dev);
126 }
127 
128 static inline int get_free_idx(u32 mask, u8 start, u8 end)
129 {
130 	return ffs(~mask & GENMASK(end, start));
131 }
132 
133 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
134 {
135 	int i;
136 
137 	switch (type) {
138 	case NL80211_IFTYPE_MESH_POINT:
139 	case NL80211_IFTYPE_ADHOC:
140 	case NL80211_IFTYPE_STATION:
141 		/* prefer hw bssid slot 1-3 */
142 		i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
143 		if (i)
144 			return i - 1;
145 
146 		if (type != NL80211_IFTYPE_STATION)
147 			break;
148 
149 		/* next, try to find a free repeater entry for the sta */
150 		i = get_free_idx(mask >> REPEATER_BSSID_START, 0,
151 				 REPEATER_BSSID_MAX - REPEATER_BSSID_START);
152 		if (i)
153 			return i + 32 - 1;
154 
155 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
156 		if (i)
157 			return i - 1;
158 
159 		if (~mask & BIT(HW_BSSID_0))
160 			return HW_BSSID_0;
161 
162 		break;
163 	case NL80211_IFTYPE_MONITOR:
164 	case NL80211_IFTYPE_AP:
165 		/* ap uses hw bssid 0 and ext bssid */
166 		if (~mask & BIT(HW_BSSID_0))
167 			return HW_BSSID_0;
168 
169 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
170 		if (i)
171 			return i - 1;
172 
173 		break;
174 	default:
175 		WARN_ON(1);
176 		break;
177 	}
178 
179 	return -1;
180 }
181 
182 static int mt7615_add_interface(struct ieee80211_hw *hw,
183 				struct ieee80211_vif *vif)
184 {
185 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
186 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
187 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
188 	struct mt76_txq *mtxq;
189 	bool ext_phy = phy != &dev->phy;
190 	int idx, ret = 0;
191 
192 	mt7615_mutex_acquire(dev);
193 
194 	mt76_testmode_reset(phy->mt76, true);
195 
196 	if (vif->type == NL80211_IFTYPE_MONITOR &&
197 	    is_zero_ether_addr(vif->addr))
198 		phy->monitor_vif = vif;
199 
200 	mvif->mt76.idx = ffs(~dev->mt76.vif_mask) - 1;
201 	if (mvif->mt76.idx >= MT7615_MAX_INTERFACES) {
202 		ret = -ENOSPC;
203 		goto out;
204 	}
205 
206 	idx = get_omac_idx(vif->type, dev->omac_mask);
207 	if (idx < 0) {
208 		ret = -ENOSPC;
209 		goto out;
210 	}
211 	mvif->mt76.omac_idx = idx;
212 
213 	mvif->mt76.band_idx = ext_phy;
214 	if (mt7615_ext_phy(dev))
215 		mvif->mt76.wmm_idx = ext_phy * (MT7615_MAX_WMM_SETS / 2) +
216 				mvif->mt76.idx % (MT7615_MAX_WMM_SETS / 2);
217 	else
218 		mvif->mt76.wmm_idx = mvif->mt76.idx % MT7615_MAX_WMM_SETS;
219 
220 	dev->mt76.vif_mask |= BIT(mvif->mt76.idx);
221 	dev->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
222 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
223 
224 	ret = mt7615_mcu_set_dbdc(dev);
225 	if (ret)
226 		goto out;
227 
228 	idx = MT7615_WTBL_RESERVED - mvif->mt76.idx;
229 
230 	INIT_LIST_HEAD(&mvif->sta.poll_list);
231 	mvif->sta.wcid.idx = idx;
232 	mvif->sta.wcid.ext_phy = mvif->mt76.band_idx;
233 	mvif->sta.wcid.hw_key_idx = -1;
234 	mt7615_mac_wtbl_update(dev, idx,
235 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
236 
237 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
238 	if (vif->txq) {
239 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
240 		mtxq->wcid = &mvif->sta.wcid;
241 	}
242 
243 	ret = mt7615_mcu_add_dev_info(phy, vif, true);
244 out:
245 	mt7615_mutex_release(dev);
246 
247 	return ret;
248 }
249 
250 static void mt7615_remove_interface(struct ieee80211_hw *hw,
251 				    struct ieee80211_vif *vif)
252 {
253 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
254 	struct mt7615_sta *msta = &mvif->sta;
255 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
256 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
257 	int idx = msta->wcid.idx;
258 
259 	mt7615_mutex_acquire(dev);
260 
261 	mt7615_mcu_add_bss_info(phy, vif, NULL, false);
262 	mt7615_mcu_sta_add(phy, vif, NULL, false);
263 
264 	mt76_testmode_reset(phy->mt76, true);
265 	if (vif == phy->monitor_vif)
266 	    phy->monitor_vif = NULL;
267 
268 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
269 
270 	mt7615_mcu_add_dev_info(phy, vif, false);
271 
272 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
273 
274 	dev->mt76.vif_mask &= ~BIT(mvif->mt76.idx);
275 	dev->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
276 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
277 
278 	mt7615_mutex_release(dev);
279 
280 	spin_lock_bh(&dev->sta_poll_lock);
281 	if (!list_empty(&msta->poll_list))
282 		list_del_init(&msta->poll_list);
283 	spin_unlock_bh(&dev->sta_poll_lock);
284 }
285 
286 static void mt7615_init_dfs_state(struct mt7615_phy *phy)
287 {
288 	struct mt76_phy *mphy = phy->mt76;
289 	struct ieee80211_hw *hw = mphy->hw;
290 	struct cfg80211_chan_def *chandef = &hw->conf.chandef;
291 
292 	if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
293 		return;
294 
295 	if (!(chandef->chan->flags & IEEE80211_CHAN_RADAR))
296 		return;
297 
298 	if (mphy->chandef.chan->center_freq == chandef->chan->center_freq &&
299 	    mphy->chandef.width == chandef->width)
300 		return;
301 
302 	phy->dfs_state = -1;
303 }
304 
305 int mt7615_set_channel(struct mt7615_phy *phy)
306 {
307 	struct mt7615_dev *dev = phy->dev;
308 	bool ext_phy = phy != &dev->phy;
309 	int ret;
310 
311 	cancel_delayed_work_sync(&phy->mt76->mac_work);
312 
313 	mt7615_mutex_acquire(dev);
314 
315 	set_bit(MT76_RESET, &phy->mt76->state);
316 
317 	mt7615_init_dfs_state(phy);
318 	mt76_set_channel(phy->mt76);
319 
320 	if (is_mt7615(&dev->mt76) && dev->flash_eeprom) {
321 		ret = mt7615_mcu_apply_rx_dcoc(phy);
322 		if (ret)
323 			goto out;
324 
325 		ret = mt7615_mcu_apply_tx_dpd(phy);
326 		if (ret)
327 			goto out;
328 	}
329 
330 	ret = mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD_CHANNEL_SWITCH);
331 	if (ret)
332 		goto out;
333 
334 	mt7615_mac_set_timing(phy);
335 	ret = mt7615_dfs_init_radar_detector(phy);
336 	if (ret)
337 		goto out;
338 
339 	mt7615_mac_cca_stats_reset(phy);
340 	ret = mt7615_mcu_set_sku_en(phy, true);
341 	if (ret)
342 		goto out;
343 
344 	mt7615_mac_reset_counters(dev);
345 	phy->noise = 0;
346 	phy->chfreq = mt76_rr(dev, MT_CHFREQ(ext_phy));
347 
348 out:
349 	clear_bit(MT76_RESET, &phy->mt76->state);
350 
351 	mt7615_mutex_release(dev);
352 
353 	mt76_worker_schedule(&dev->mt76.tx_worker);
354 	if (!mt76_testmode_enabled(phy->mt76)) {
355 		unsigned long timeout = mt7615_get_macwork_timeout(dev);
356 
357 		ieee80211_queue_delayed_work(phy->mt76->hw,
358 					     &phy->mt76->mac_work, timeout);
359 	}
360 
361 	return ret;
362 }
363 
364 static int mt7615_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
365 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
366 			  struct ieee80211_key_conf *key)
367 {
368 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
369 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
370 	struct mt7615_sta *msta = sta ? (struct mt7615_sta *)sta->drv_priv :
371 				  &mvif->sta;
372 	struct mt76_wcid *wcid = &msta->wcid;
373 	int idx = key->keyidx, err = 0;
374 	u8 *wcid_keyidx = &wcid->hw_key_idx;
375 
376 	/* The hardware does not support per-STA RX GTK, fallback
377 	 * to software mode for these.
378 	 */
379 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
380 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
381 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
382 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
383 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
384 		return -EOPNOTSUPP;
385 
386 	/* fall back to sw encryption for unsupported ciphers */
387 	switch (key->cipher) {
388 	case WLAN_CIPHER_SUITE_AES_CMAC:
389 		wcid_keyidx = &wcid->hw_key_idx2;
390 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
391 		break;
392 	case WLAN_CIPHER_SUITE_TKIP:
393 	case WLAN_CIPHER_SUITE_CCMP:
394 	case WLAN_CIPHER_SUITE_CCMP_256:
395 	case WLAN_CIPHER_SUITE_GCMP:
396 	case WLAN_CIPHER_SUITE_GCMP_256:
397 	case WLAN_CIPHER_SUITE_SMS4:
398 		break;
399 	case WLAN_CIPHER_SUITE_WEP40:
400 	case WLAN_CIPHER_SUITE_WEP104:
401 	default:
402 		return -EOPNOTSUPP;
403 	}
404 
405 	mt7615_mutex_acquire(dev);
406 
407 	if (cmd == SET_KEY)
408 		*wcid_keyidx = idx;
409 	else if (idx == *wcid_keyidx)
410 		*wcid_keyidx = -1;
411 	else
412 		goto out;
413 
414 	mt76_wcid_key_setup(&dev->mt76, wcid,
415 			    cmd == SET_KEY ? key : NULL);
416 
417 	if (mt76_is_mmio(&dev->mt76))
418 		err = mt7615_mac_wtbl_set_key(dev, wcid, key, cmd);
419 	else
420 		err = __mt7615_mac_wtbl_set_key(dev, wcid, key, cmd);
421 
422 out:
423 	mt7615_mutex_release(dev);
424 
425 	return err;
426 }
427 
428 static int mt7615_config(struct ieee80211_hw *hw, u32 changed)
429 {
430 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
431 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
432 	bool band = phy != &dev->phy;
433 	int ret = 0;
434 
435 	if (changed & (IEEE80211_CONF_CHANGE_CHANNEL |
436 		       IEEE80211_CONF_CHANGE_POWER)) {
437 #ifdef CONFIG_NL80211_TESTMODE
438 		if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
439 			mt7615_mutex_acquire(dev);
440 			mt76_testmode_reset(phy->mt76, false);
441 			mt7615_mutex_release(dev);
442 		}
443 #endif
444 		ieee80211_stop_queues(hw);
445 		ret = mt7615_set_channel(phy);
446 		ieee80211_wake_queues(hw);
447 	}
448 
449 	mt7615_mutex_acquire(dev);
450 
451 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
452 		mt76_testmode_reset(phy->mt76, true);
453 
454 		if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
455 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
456 		else
457 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
458 
459 		mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
460 	}
461 
462 	mt7615_mutex_release(dev);
463 
464 	return ret;
465 }
466 
467 static int
468 mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
469 	       const struct ieee80211_tx_queue_params *params)
470 {
471 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
472 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
473 	int err;
474 
475 	mt7615_mutex_acquire(dev);
476 
477 	queue = mt7615_lmac_mapping(dev, queue);
478 	queue += mvif->wmm_idx * MT7615_MAX_WMM_SETS;
479 	err = mt7615_mcu_set_wmm(dev, queue, params);
480 
481 	mt7615_mutex_release(dev);
482 
483 	return err;
484 }
485 
486 static void mt7615_configure_filter(struct ieee80211_hw *hw,
487 				    unsigned int changed_flags,
488 				    unsigned int *total_flags,
489 				    u64 multicast)
490 {
491 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
492 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
493 	bool band = phy != &dev->phy;
494 
495 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
496 			MT_WF_RFCR1_DROP_BF_POLL |
497 			MT_WF_RFCR1_DROP_BA |
498 			MT_WF_RFCR1_DROP_CFEND |
499 			MT_WF_RFCR1_DROP_CFACK;
500 	u32 flags = 0;
501 
502 	mt7615_mutex_acquire(dev);
503 
504 #define MT76_FILTER(_flag, _hw) do { \
505 		flags |= *total_flags & FIF_##_flag;			\
506 		phy->rxfilter &= ~(_hw);				\
507 		if (!mt76_testmode_enabled(phy->mt76))			\
508 			phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);\
509 	} while (0)
510 
511 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
512 			   MT_WF_RFCR_DROP_FRAME_REPORT |
513 			   MT_WF_RFCR_DROP_PROBEREQ |
514 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
515 			   MT_WF_RFCR_DROP_MCAST |
516 			   MT_WF_RFCR_DROP_BCAST |
517 			   MT_WF_RFCR_DROP_DUPLICATE |
518 			   MT_WF_RFCR_DROP_A2_BSSID |
519 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
520 			   MT_WF_RFCR_DROP_STBC_MULTI);
521 
522 	if (phy->n_beacon_vif || !mt7615_firmware_offload(dev))
523 		phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_BEACON;
524 
525 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
526 			       MT_WF_RFCR_DROP_A3_MAC |
527 			       MT_WF_RFCR_DROP_A3_BSSID);
528 
529 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
530 
531 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
532 			     MT_WF_RFCR_DROP_RTS |
533 			     MT_WF_RFCR_DROP_CTL_RSV |
534 			     MT_WF_RFCR_DROP_NDPA);
535 
536 	*total_flags = flags;
537 	mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
538 
539 	if (*total_flags & FIF_CONTROL)
540 		mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
541 	else
542 		mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
543 
544 	mt7615_mutex_release(dev);
545 }
546 
547 static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
548 				    struct ieee80211_vif *vif,
549 				    struct ieee80211_bss_conf *info,
550 				    u32 changed)
551 {
552 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
553 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
554 
555 	mt7615_mutex_acquire(dev);
556 
557 	if (changed & BSS_CHANGED_ERP_SLOT) {
558 		int slottime = info->use_short_slot ? 9 : 20;
559 
560 		if (slottime != phy->slottime) {
561 			phy->slottime = slottime;
562 			mt7615_mac_set_timing(phy);
563 		}
564 	}
565 
566 	if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
567 		mt7615_mcu_add_bss_info(phy, vif, NULL, true);
568 		mt7615_mcu_sta_add(phy, vif, NULL, true);
569 
570 		if (vif->p2p)
571 			mt7615_mcu_set_p2p_oppps(hw, vif);
572 	}
573 
574 	if (changed & (BSS_CHANGED_BEACON |
575 		       BSS_CHANGED_BEACON_ENABLED))
576 		mt7615_mcu_add_beacon(dev, hw, vif, info->enable_beacon);
577 
578 	if (changed & BSS_CHANGED_PS)
579 		mt76_connac_mcu_set_vif_ps(&dev->mt76, vif);
580 
581 	if ((changed & BSS_CHANGED_ARP_FILTER) &&
582 	    mt7615_firmware_offload(dev)) {
583 		struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
584 
585 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
586 						  info);
587 	}
588 
589 	if (changed & BSS_CHANGED_ASSOC)
590 		mt7615_mac_set_beacon_filter(phy, vif, info->assoc);
591 
592 	mt7615_mutex_release(dev);
593 }
594 
595 static void
596 mt7615_channel_switch_beacon(struct ieee80211_hw *hw,
597 			     struct ieee80211_vif *vif,
598 			     struct cfg80211_chan_def *chandef)
599 {
600 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
601 
602 	mt7615_mutex_acquire(dev);
603 	mt7615_mcu_add_beacon(dev, hw, vif, true);
604 	mt7615_mutex_release(dev);
605 }
606 
607 int mt7615_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
608 		       struct ieee80211_sta *sta)
609 {
610 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
611 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
612 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
613 	struct mt7615_phy *phy;
614 	int idx, err;
615 
616 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
617 	if (idx < 0)
618 		return -ENOSPC;
619 
620 	INIT_LIST_HEAD(&msta->poll_list);
621 	msta->vif = mvif;
622 	msta->wcid.sta = 1;
623 	msta->wcid.idx = idx;
624 	msta->wcid.ext_phy = mvif->mt76.band_idx;
625 
626 	phy = mvif->mt76.band_idx ? mt7615_ext_phy(dev) : &dev->phy;
627 	err = mt76_connac_pm_wake(phy->mt76, &dev->pm);
628 	if (err)
629 		return err;
630 
631 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
632 		err = mt7615_mcu_add_bss_info(phy, vif, sta, true);
633 		if (err)
634 			return err;
635 	}
636 
637 	mt7615_mac_wtbl_update(dev, idx,
638 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
639 	err = mt7615_mcu_sta_add(&dev->phy, vif, sta, true);
640 	if (err)
641 		return err;
642 
643 	mt76_connac_power_save_sched(phy->mt76, &dev->pm);
644 
645 	return err;
646 }
647 EXPORT_SYMBOL_GPL(mt7615_mac_sta_add);
648 
649 void mt7615_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
650 			   struct ieee80211_sta *sta)
651 {
652 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
653 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
654 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
655 	struct mt7615_phy *phy;
656 
657 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
658 
659 	phy = mvif->mt76.band_idx ? mt7615_ext_phy(dev) : &dev->phy;
660 	mt76_connac_pm_wake(phy->mt76, &dev->pm);
661 
662 	mt7615_mcu_sta_add(&dev->phy, vif, sta, false);
663 	mt7615_mac_wtbl_update(dev, msta->wcid.idx,
664 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
665 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
666 		mt7615_mcu_add_bss_info(phy, vif, sta, false);
667 
668 	spin_lock_bh(&dev->sta_poll_lock);
669 	if (!list_empty(&msta->poll_list))
670 		list_del_init(&msta->poll_list);
671 	spin_unlock_bh(&dev->sta_poll_lock);
672 
673 	mt76_connac_power_save_sched(phy->mt76, &dev->pm);
674 }
675 EXPORT_SYMBOL_GPL(mt7615_mac_sta_remove);
676 
677 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
678 				       struct ieee80211_vif *vif,
679 				       struct ieee80211_sta *sta)
680 {
681 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
682 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
683 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
684 	struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
685 	int i;
686 
687 	spin_lock_bh(&dev->mt76.lock);
688 	for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
689 		msta->rates[i].idx = sta_rates->rate[i].idx;
690 		msta->rates[i].count = sta_rates->rate[i].count;
691 		msta->rates[i].flags = sta_rates->rate[i].flags;
692 
693 		if (msta->rates[i].idx < 0 || !msta->rates[i].count)
694 			break;
695 	}
696 	msta->n_rates = i;
697 	if (mt76_connac_pm_ref(phy->mt76, &dev->pm)) {
698 		mt7615_mac_set_rates(phy, msta, NULL, msta->rates);
699 		mt76_connac_pm_unref(phy->mt76, &dev->pm);
700 	}
701 	spin_unlock_bh(&dev->mt76.lock);
702 }
703 
704 void mt7615_tx_worker(struct mt76_worker *w)
705 {
706 	struct mt7615_dev *dev = container_of(w, struct mt7615_dev,
707 					      mt76.tx_worker);
708 
709 	if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
710 		queue_work(dev->mt76.wq, &dev->pm.wake_work);
711 		return;
712 	}
713 
714 	mt76_tx_worker_run(&dev->mt76);
715 	mt76_connac_pm_unref(&dev->mphy, &dev->pm);
716 }
717 
718 static void mt7615_tx(struct ieee80211_hw *hw,
719 		      struct ieee80211_tx_control *control,
720 		      struct sk_buff *skb)
721 {
722 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
723 	struct mt76_phy *mphy = hw->priv;
724 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
725 	struct ieee80211_vif *vif = info->control.vif;
726 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
727 	struct mt7615_sta *msta = NULL;
728 	int qid;
729 
730 	if (control->sta) {
731 		msta = (struct mt7615_sta *)control->sta->drv_priv;
732 		wcid = &msta->wcid;
733 	}
734 
735 	if (vif && !control->sta) {
736 		struct mt7615_vif *mvif;
737 
738 		mvif = (struct mt7615_vif *)vif->drv_priv;
739 		msta = &mvif->sta;
740 		wcid = &msta->wcid;
741 	}
742 
743 	if (mt76_connac_pm_ref(mphy, &dev->pm)) {
744 		mt76_tx(mphy, control->sta, wcid, skb);
745 		mt76_connac_pm_unref(mphy, &dev->pm);
746 		return;
747 	}
748 
749 	qid = skb_get_queue_mapping(skb);
750 	if (qid >= MT_TXQ_PSD) {
751 		qid = IEEE80211_AC_BE;
752 		skb_set_queue_mapping(skb, qid);
753 	}
754 
755 	mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
756 }
757 
758 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
759 {
760 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
761 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
762 	int err, band = phy != &dev->phy;
763 
764 	mt7615_mutex_acquire(dev);
765 	err = mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, band);
766 	mt7615_mutex_release(dev);
767 
768 	return err;
769 }
770 
771 static int
772 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
773 		    struct ieee80211_ampdu_params *params)
774 {
775 	enum ieee80211_ampdu_mlme_action action = params->action;
776 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
777 	struct ieee80211_sta *sta = params->sta;
778 	struct ieee80211_txq *txq = sta->txq[params->tid];
779 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
780 	u16 tid = params->tid;
781 	u16 ssn = params->ssn;
782 	struct mt76_txq *mtxq;
783 	int ret = 0;
784 
785 	if (!txq)
786 		return -EINVAL;
787 
788 	mtxq = (struct mt76_txq *)txq->drv_priv;
789 
790 	mt7615_mutex_acquire(dev);
791 
792 	switch (action) {
793 	case IEEE80211_AMPDU_RX_START:
794 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
795 				   params->buf_size);
796 		ret = mt7615_mcu_add_rx_ba(dev, params, true);
797 		break;
798 	case IEEE80211_AMPDU_RX_STOP:
799 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
800 		ret = mt7615_mcu_add_rx_ba(dev, params, false);
801 		break;
802 	case IEEE80211_AMPDU_TX_OPERATIONAL:
803 		mtxq->aggr = true;
804 		mtxq->send_bar = false;
805 		ret = mt7615_mcu_add_tx_ba(dev, params, true);
806 		ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid);
807 		ieee80211_send_bar(vif, sta->addr, tid,
808 				   IEEE80211_SN_TO_SEQ(ssn));
809 		break;
810 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
811 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
812 		mtxq->aggr = false;
813 		ret = mt7615_mcu_add_tx_ba(dev, params, false);
814 		break;
815 	case IEEE80211_AMPDU_TX_START:
816 		ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid);
817 		params->ssn = ssn;
818 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
819 		break;
820 	case IEEE80211_AMPDU_TX_STOP_CONT:
821 		mtxq->aggr = false;
822 		ret = mt7615_mcu_add_tx_ba(dev, params, false);
823 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
824 		break;
825 	}
826 	mt7615_mutex_release(dev);
827 
828 	return ret;
829 }
830 
831 static int
832 mt7615_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
833 	       struct ieee80211_sta *sta)
834 {
835     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
836 			  IEEE80211_STA_NONE);
837 }
838 
839 static int
840 mt7615_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
841 		  struct ieee80211_sta *sta)
842 {
843     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
844 			  IEEE80211_STA_NOTEXIST);
845 }
846 
847 static int
848 mt7615_get_stats(struct ieee80211_hw *hw,
849 		 struct ieee80211_low_level_stats *stats)
850 {
851 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
852 	struct mib_stats *mib = &phy->mib;
853 
854 	mt7615_mutex_acquire(phy->dev);
855 
856 	stats->dot11RTSSuccessCount = mib->rts_cnt;
857 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
858 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
859 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
860 
861 	memset(mib, 0, sizeof(*mib));
862 
863 	mt7615_mutex_release(phy->dev);
864 
865 	return 0;
866 }
867 
868 static u64
869 mt7615_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
870 {
871 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
872 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
873 	union {
874 		u64 t64;
875 		u32 t32[2];
876 	} tsf;
877 	u16 idx = mvif->mt76.omac_idx;
878 	u32 reg;
879 
880 	idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
881 	reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
882 
883 	mt7615_mutex_acquire(dev);
884 
885 	/* TSF read */
886 	mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_READ);
887 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0);
888 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1);
889 
890 	mt7615_mutex_release(dev);
891 
892 	return tsf.t64;
893 }
894 
895 static void
896 mt7615_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
897 	       u64 timestamp)
898 {
899 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
900 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
901 	union {
902 		u64 t64;
903 		u32 t32[2];
904 	} tsf = { .t64 = timestamp, };
905 	u16 idx = mvif->mt76.omac_idx;
906 	u32 reg;
907 
908 	idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
909 	reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
910 
911 	mt7615_mutex_acquire(dev);
912 
913 	mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
914 	mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
915 	/* TSF software overwrite */
916 	mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_WRITE);
917 
918 	mt7615_mutex_release(dev);
919 }
920 
921 static void
922 mt7615_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
923 		  s64 timestamp)
924 {
925 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
926 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
927 	union {
928 		u64 t64;
929 		u32 t32[2];
930 	} tsf = { .t64 = timestamp, };
931 	u16 idx = mvif->mt76.omac_idx;
932 	u32 reg;
933 
934 	idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
935 	reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
936 
937 	mt7615_mutex_acquire(dev);
938 
939 	mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
940 	mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
941 	/* TSF software adjust*/
942 	mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_ADJUST);
943 
944 	mt7615_mutex_release(dev);
945 }
946 
947 static void
948 mt7615_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
949 {
950 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
951 	struct mt7615_dev *dev = phy->dev;
952 
953 	mt7615_mutex_acquire(dev);
954 	phy->coverage_class = max_t(s16, coverage_class, 0);
955 	mt7615_mac_set_timing(phy);
956 	mt7615_mutex_release(dev);
957 }
958 
959 static int
960 mt7615_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
961 {
962 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
963 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
964 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
965 	bool ext_phy = phy != &dev->phy;
966 
967 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
968 		return -EINVAL;
969 
970 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
971 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
972 
973 	mt7615_mutex_acquire(dev);
974 
975 	phy->mt76->antenna_mask = tx_ant;
976 	if (ext_phy) {
977 		if (dev->chainmask == 0xf)
978 			tx_ant <<= 2;
979 		else
980 			tx_ant <<= 1;
981 	}
982 	phy->mt76->chainmask = tx_ant;
983 
984 	mt76_set_stream_caps(phy->mt76, true);
985 
986 	mt7615_mutex_release(dev);
987 
988 	return 0;
989 }
990 
991 static void mt7615_roc_iter(void *priv, u8 *mac,
992 			    struct ieee80211_vif *vif)
993 {
994 	struct mt7615_phy *phy = priv;
995 
996 	mt7615_mcu_set_roc(phy, vif, NULL, 0);
997 }
998 
999 void mt7615_roc_work(struct work_struct *work)
1000 {
1001 	struct mt7615_phy *phy;
1002 
1003 	phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1004 						roc_work);
1005 
1006 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1007 		return;
1008 
1009 	mt7615_mutex_acquire(phy->dev);
1010 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
1011 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1012 					    mt7615_roc_iter, phy);
1013 	mt7615_mutex_release(phy->dev);
1014 	ieee80211_remain_on_channel_expired(phy->mt76->hw);
1015 }
1016 
1017 void mt7615_roc_timer(struct timer_list *timer)
1018 {
1019 	struct mt7615_phy *phy = from_timer(phy, timer, roc_timer);
1020 
1021 	ieee80211_queue_work(phy->mt76->hw, &phy->roc_work);
1022 }
1023 
1024 void mt7615_scan_work(struct work_struct *work)
1025 {
1026 	struct mt7615_phy *phy;
1027 
1028 	phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1029 						scan_work.work);
1030 
1031 	while (true) {
1032 		struct mt7615_mcu_rxd *rxd;
1033 		struct sk_buff *skb;
1034 
1035 		spin_lock_bh(&phy->dev->mt76.lock);
1036 		skb = __skb_dequeue(&phy->scan_event_list);
1037 		spin_unlock_bh(&phy->dev->mt76.lock);
1038 
1039 		if (!skb)
1040 			break;
1041 
1042 		rxd = (struct mt7615_mcu_rxd *)skb->data;
1043 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1044 			ieee80211_sched_scan_results(phy->mt76->hw);
1045 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
1046 					      &phy->mt76->state)) {
1047 			struct cfg80211_scan_info info = {
1048 				.aborted = false,
1049 			};
1050 
1051 			ieee80211_scan_completed(phy->mt76->hw, &info);
1052 		}
1053 		dev_kfree_skb(skb);
1054 	}
1055 }
1056 
1057 static int
1058 mt7615_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1059 	       struct ieee80211_scan_request *req)
1060 {
1061 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1062 	struct mt76_phy *mphy = hw->priv;
1063 	int err;
1064 
1065 	/* fall-back to sw-scan */
1066 	if (!mt7615_firmware_offload(dev))
1067 		return 1;
1068 
1069 	mt7615_mutex_acquire(dev);
1070 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1071 	mt7615_mutex_release(dev);
1072 
1073 	return err;
1074 }
1075 
1076 static void
1077 mt7615_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1078 {
1079 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1080 	struct mt76_phy *mphy = hw->priv;
1081 
1082 	mt7615_mutex_acquire(dev);
1083 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1084 	mt7615_mutex_release(dev);
1085 }
1086 
1087 static int
1088 mt7615_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1089 			struct cfg80211_sched_scan_request *req,
1090 			struct ieee80211_scan_ies *ies)
1091 {
1092 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1093 	struct mt76_phy *mphy = hw->priv;
1094 	int err;
1095 
1096 	if (!mt7615_firmware_offload(dev))
1097 		return -EOPNOTSUPP;
1098 
1099 	mt7615_mutex_acquire(dev);
1100 
1101 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1102 	if (err < 0)
1103 		goto out;
1104 
1105 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1106 out:
1107 	mt7615_mutex_release(dev);
1108 
1109 	return err;
1110 }
1111 
1112 static int
1113 mt7615_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1114 {
1115 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1116 	struct mt76_phy *mphy = hw->priv;
1117 	int err;
1118 
1119 	if (!mt7615_firmware_offload(dev))
1120 		return -EOPNOTSUPP;
1121 
1122 	mt7615_mutex_acquire(dev);
1123 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1124 	mt7615_mutex_release(dev);
1125 
1126 	return err;
1127 }
1128 
1129 static int mt7615_remain_on_channel(struct ieee80211_hw *hw,
1130 				    struct ieee80211_vif *vif,
1131 				    struct ieee80211_channel *chan,
1132 				    int duration,
1133 				    enum ieee80211_roc_type type)
1134 {
1135 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1136 	int err;
1137 
1138 	if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
1139 		return 0;
1140 
1141 	mt7615_mutex_acquire(phy->dev);
1142 
1143 	err = mt7615_mcu_set_roc(phy, vif, chan, duration);
1144 	if (err < 0) {
1145 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1146 		goto out;
1147 	}
1148 
1149 	if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) {
1150 		mt7615_mcu_set_roc(phy, vif, NULL, 0);
1151 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1152 		err = -ETIMEDOUT;
1153 	}
1154 
1155 out:
1156 	mt7615_mutex_release(phy->dev);
1157 
1158 	return err;
1159 }
1160 
1161 static int mt7615_cancel_remain_on_channel(struct ieee80211_hw *hw,
1162 					   struct ieee80211_vif *vif)
1163 {
1164 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1165 	int err;
1166 
1167 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1168 		return 0;
1169 
1170 	del_timer_sync(&phy->roc_timer);
1171 	cancel_work_sync(&phy->roc_work);
1172 
1173 	mt7615_mutex_acquire(phy->dev);
1174 	err = mt7615_mcu_set_roc(phy, vif, NULL, 0);
1175 	mt7615_mutex_release(phy->dev);
1176 
1177 	return err;
1178 }
1179 
1180 static void mt7615_sta_set_decap_offload(struct ieee80211_hw *hw,
1181 				 struct ieee80211_vif *vif,
1182 				 struct ieee80211_sta *sta,
1183 				 bool enabled)
1184 {
1185 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1186 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
1187 
1188 	if (enabled)
1189 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1190 	else
1191 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1192 
1193 	mt7615_mcu_set_sta_decap_offload(dev, vif, sta);
1194 }
1195 
1196 #ifdef CONFIG_PM
1197 static int mt7615_suspend(struct ieee80211_hw *hw,
1198 			  struct cfg80211_wowlan *wowlan)
1199 {
1200 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1201 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1202 	int err = 0;
1203 
1204 	cancel_delayed_work_sync(&dev->pm.ps_work);
1205 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1206 
1207 	mt7615_mutex_acquire(dev);
1208 
1209 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1210 	cancel_delayed_work_sync(&phy->scan_work);
1211 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1212 
1213 	set_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1214 	ieee80211_iterate_active_interfaces(hw,
1215 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1216 					    mt76_connac_mcu_set_suspend_iter,
1217 					    phy->mt76);
1218 
1219 	if (!mt7615_dev_running(dev))
1220 		err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
1221 
1222 	mt7615_mutex_release(dev);
1223 
1224 	return err;
1225 }
1226 
1227 static int mt7615_resume(struct ieee80211_hw *hw)
1228 {
1229 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1230 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1231 	unsigned long timeout;
1232 	bool running;
1233 
1234 	mt7615_mutex_acquire(dev);
1235 
1236 	running = mt7615_dev_running(dev);
1237 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1238 
1239 	if (!running) {
1240 		int err;
1241 
1242 		err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
1243 		if (err < 0) {
1244 			mt7615_mutex_release(dev);
1245 			return err;
1246 		}
1247 	}
1248 
1249 	clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1250 	ieee80211_iterate_active_interfaces(hw,
1251 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1252 					    mt76_connac_mcu_set_suspend_iter,
1253 					    phy->mt76);
1254 
1255 	timeout = mt7615_get_macwork_timeout(dev);
1256 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, timeout);
1257 
1258 	mt7615_mutex_release(dev);
1259 
1260 	return 0;
1261 }
1262 
1263 static void mt7615_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1264 {
1265 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1266 	struct mt76_dev *mdev = &dev->mt76;
1267 
1268 	device_set_wakeup_enable(mdev->dev, enabled);
1269 }
1270 
1271 static void mt7615_set_rekey_data(struct ieee80211_hw *hw,
1272 				  struct ieee80211_vif *vif,
1273 				  struct cfg80211_gtk_rekey_data *data)
1274 {
1275 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1276 
1277 	mt7615_mutex_acquire(dev);
1278 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1279 	mt7615_mutex_release(dev);
1280 }
1281 #endif /* CONFIG_PM */
1282 
1283 const struct ieee80211_ops mt7615_ops = {
1284 	.tx = mt7615_tx,
1285 	.start = mt7615_start,
1286 	.stop = mt7615_stop,
1287 	.add_interface = mt7615_add_interface,
1288 	.remove_interface = mt7615_remove_interface,
1289 	.config = mt7615_config,
1290 	.conf_tx = mt7615_conf_tx,
1291 	.configure_filter = mt7615_configure_filter,
1292 	.bss_info_changed = mt7615_bss_info_changed,
1293 	.sta_add = mt7615_sta_add,
1294 	.sta_remove = mt7615_sta_remove,
1295 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1296 	.set_key = mt7615_set_key,
1297 	.sta_set_decap_offload = mt7615_sta_set_decap_offload,
1298 	.ampdu_action = mt7615_ampdu_action,
1299 	.set_rts_threshold = mt7615_set_rts_threshold,
1300 	.wake_tx_queue = mt76_wake_tx_queue,
1301 	.sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
1302 	.sw_scan_start = mt76_sw_scan,
1303 	.sw_scan_complete = mt76_sw_scan_complete,
1304 	.release_buffered_frames = mt76_release_buffered_frames,
1305 	.get_txpower = mt76_get_txpower,
1306 	.channel_switch_beacon = mt7615_channel_switch_beacon,
1307 	.get_stats = mt7615_get_stats,
1308 	.get_tsf = mt7615_get_tsf,
1309 	.set_tsf = mt7615_set_tsf,
1310 	.offset_tsf = mt7615_offset_tsf,
1311 	.get_survey = mt76_get_survey,
1312 	.get_antenna = mt76_get_antenna,
1313 	.set_antenna = mt7615_set_antenna,
1314 	.set_coverage_class = mt7615_set_coverage_class,
1315 	.hw_scan = mt7615_hw_scan,
1316 	.cancel_hw_scan = mt7615_cancel_hw_scan,
1317 	.sched_scan_start = mt7615_start_sched_scan,
1318 	.sched_scan_stop = mt7615_stop_sched_scan,
1319 	.remain_on_channel = mt7615_remain_on_channel,
1320 	.cancel_remain_on_channel = mt7615_cancel_remain_on_channel,
1321 	CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1322 	CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1323 #ifdef CONFIG_PM
1324 	.suspend = mt7615_suspend,
1325 	.resume = mt7615_resume,
1326 	.set_wakeup = mt7615_set_wakeup,
1327 	.set_rekey_data = mt7615_set_rekey_data,
1328 #endif /* CONFIG_PM */
1329 };
1330 EXPORT_SYMBOL_GPL(mt7615_ops);
1331 
1332 MODULE_LICENSE("Dual BSD/GPL");
1333