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