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