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