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 	bool running;
32 	int ret;
33 
34 	if (!mt7615_wait_for_mcu_init(dev))
35 		return -EIO;
36 
37 	mt7615_mutex_acquire(dev);
38 
39 	running = mt7615_dev_running(dev);
40 
41 	if (!running) {
42 		ret = mt7615_mcu_set_pm(dev, 0, 0);
43 		if (ret)
44 			goto out;
45 
46 		ret = mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, true, false);
47 		if (ret)
48 			goto out;
49 
50 		mt7615_mac_enable_nf(dev, 0);
51 	}
52 
53 	if (phy != &dev->phy) {
54 		ret = mt7615_mcu_set_pm(dev, 1, 0);
55 		if (ret)
56 			goto out;
57 
58 		ret = mt76_connac_mcu_set_mac_enable(&dev->mt76, 1, true, false);
59 		if (ret)
60 			goto out;
61 
62 		mt7615_mac_enable_nf(dev, 1);
63 	}
64 
65 	if (mt7615_firmware_offload(dev)) {
66 		ret = mt76_connac_mcu_set_channel_domain(phy->mt76);
67 		if (ret)
68 			goto out;
69 
70 		ret = mt76_connac_mcu_set_rate_txpower(phy->mt76);
71 		if (ret)
72 			goto out;
73 	}
74 
75 	ret = mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD_SET_RX_PATH);
76 	if (ret)
77 		goto out;
78 
79 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
80 
81 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
82 				     MT7615_WATCHDOG_TIME);
83 
84 	if (!running)
85 		mt7615_mac_reset_counters(dev);
86 
87 out:
88 	mt7615_mutex_release(dev);
89 
90 	return ret;
91 }
92 
93 static void mt7615_stop(struct ieee80211_hw *hw)
94 {
95 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
96 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
97 
98 	cancel_delayed_work_sync(&phy->mt76->mac_work);
99 	del_timer_sync(&phy->roc_timer);
100 	cancel_work_sync(&phy->roc_work);
101 
102 	cancel_delayed_work_sync(&dev->pm.ps_work);
103 	cancel_work_sync(&dev->pm.wake_work);
104 
105 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
106 
107 	mt7615_mutex_acquire(dev);
108 
109 	mt76_testmode_reset(phy->mt76, true);
110 
111 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
112 	cancel_delayed_work_sync(&phy->scan_work);
113 
114 	if (phy != &dev->phy) {
115 		mt7615_mcu_set_pm(dev, 1, 1);
116 		mt76_connac_mcu_set_mac_enable(&dev->mt76, 1, false, false);
117 	}
118 
119 	if (!mt7615_dev_running(dev)) {
120 		mt7615_mcu_set_pm(dev, 0, 1);
121 		mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
122 	}
123 
124 	mt7615_mutex_release(dev);
125 }
126 
127 static inline int get_free_idx(u32 mask, u8 start, u8 end)
128 {
129 	return ffs(~mask & GENMASK(end, start));
130 }
131 
132 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
133 {
134 	int i;
135 
136 	switch (type) {
137 	case NL80211_IFTYPE_MESH_POINT:
138 	case NL80211_IFTYPE_ADHOC:
139 	case NL80211_IFTYPE_STATION:
140 		/* prefer hw bssid slot 1-3 */
141 		i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
142 		if (i)
143 			return i - 1;
144 
145 		if (type != NL80211_IFTYPE_STATION)
146 			break;
147 
148 		/* next, try to find a free repeater entry for the sta */
149 		i = get_free_idx(mask >> REPEATER_BSSID_START, 0,
150 				 REPEATER_BSSID_MAX - REPEATER_BSSID_START);
151 		if (i)
152 			return i + 32 - 1;
153 
154 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
155 		if (i)
156 			return i - 1;
157 
158 		if (~mask & BIT(HW_BSSID_0))
159 			return HW_BSSID_0;
160 
161 		break;
162 	case NL80211_IFTYPE_MONITOR:
163 	case NL80211_IFTYPE_AP:
164 		/* ap uses hw bssid 0 and ext bssid */
165 		if (~mask & BIT(HW_BSSID_0))
166 			return HW_BSSID_0;
167 
168 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
169 		if (i)
170 			return i - 1;
171 
172 		break;
173 	default:
174 		WARN_ON(1);
175 		break;
176 	}
177 
178 	return -1;
179 }
180 
181 static int mt7615_add_interface(struct ieee80211_hw *hw,
182 				struct ieee80211_vif *vif)
183 {
184 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
185 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
186 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
187 	struct mt76_txq *mtxq;
188 	bool ext_phy = phy != &dev->phy;
189 	int idx, ret = 0;
190 
191 	mt7615_mutex_acquire(dev);
192 
193 	mt76_testmode_reset(phy->mt76, true);
194 
195 	if (vif->type == NL80211_IFTYPE_MONITOR &&
196 	    is_zero_ether_addr(vif->addr))
197 		phy->monitor_vif = vif;
198 
199 	mvif->mt76.idx = ffs(~dev->mt76.vif_mask) - 1;
200 	if (mvif->mt76.idx >= MT7615_MAX_INTERFACES) {
201 		ret = -ENOSPC;
202 		goto out;
203 	}
204 
205 	idx = get_omac_idx(vif->type, dev->omac_mask);
206 	if (idx < 0) {
207 		ret = -ENOSPC;
208 		goto out;
209 	}
210 	mvif->mt76.omac_idx = idx;
211 
212 	mvif->mt76.band_idx = ext_phy;
213 	if (mt7615_ext_phy(dev))
214 		mvif->mt76.wmm_idx = ext_phy * (MT7615_MAX_WMM_SETS / 2) +
215 				mvif->mt76.idx % (MT7615_MAX_WMM_SETS / 2);
216 	else
217 		mvif->mt76.wmm_idx = mvif->mt76.idx % MT7615_MAX_WMM_SETS;
218 
219 	dev->mt76.vif_mask |= BIT(mvif->mt76.idx);
220 	dev->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
221 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
222 
223 	ret = mt7615_mcu_set_dbdc(dev);
224 	if (ret)
225 		goto out;
226 
227 	idx = MT7615_WTBL_RESERVED - mvif->mt76.idx;
228 
229 	INIT_LIST_HEAD(&mvif->sta.poll_list);
230 	mvif->sta.wcid.idx = idx;
231 	mvif->sta.wcid.ext_phy = mvif->mt76.band_idx;
232 	mvif->sta.wcid.hw_key_idx = -1;
233 	mt7615_mac_wtbl_update(dev, idx,
234 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
235 
236 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
237 	if (vif->txq) {
238 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
239 		mtxq->wcid = &mvif->sta.wcid;
240 	}
241 
242 	ret = mt7615_mcu_add_dev_info(phy, vif, true);
243 	if (ret)
244 		goto out;
245 out:
246 	mt7615_mutex_release(dev);
247 
248 	return ret;
249 }
250 
251 static void mt7615_remove_interface(struct ieee80211_hw *hw,
252 				    struct ieee80211_vif *vif)
253 {
254 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
255 	struct mt7615_sta *msta = &mvif->sta;
256 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
257 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
258 	int idx = msta->wcid.idx;
259 
260 	mt7615_mutex_acquire(dev);
261 
262 	mt7615_mcu_add_bss_info(phy, vif, NULL, false);
263 	mt7615_mcu_sta_add(phy, vif, NULL, false);
264 
265 	mt76_testmode_reset(phy->mt76, true);
266 	if (vif == phy->monitor_vif)
267 	    phy->monitor_vif = NULL;
268 
269 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
270 
271 	mt7615_mcu_add_dev_info(phy, vif, false);
272 
273 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
274 
275 	dev->mt76.vif_mask &= ~BIT(mvif->mt76.idx);
276 	dev->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
277 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
278 
279 	mt7615_mutex_release(dev);
280 
281 	spin_lock_bh(&dev->sta_poll_lock);
282 	if (!list_empty(&msta->poll_list))
283 		list_del_init(&msta->poll_list);
284 	spin_unlock_bh(&dev->sta_poll_lock);
285 }
286 
287 static void mt7615_init_dfs_state(struct mt7615_phy *phy)
288 {
289 	struct mt76_phy *mphy = phy->mt76;
290 	struct ieee80211_hw *hw = mphy->hw;
291 	struct cfg80211_chan_def *chandef = &hw->conf.chandef;
292 
293 	if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
294 		return;
295 
296 	if (!(chandef->chan->flags & IEEE80211_CHAN_RADAR))
297 		return;
298 
299 	if (mphy->chandef.chan->center_freq == chandef->chan->center_freq &&
300 	    mphy->chandef.width == chandef->width)
301 		return;
302 
303 	phy->dfs_state = -1;
304 }
305 
306 int mt7615_set_channel(struct mt7615_phy *phy)
307 {
308 	struct mt7615_dev *dev = phy->dev;
309 	bool ext_phy = phy != &dev->phy;
310 	int ret;
311 
312 	cancel_delayed_work_sync(&phy->mt76->mac_work);
313 
314 	mt7615_mutex_acquire(dev);
315 
316 	set_bit(MT76_RESET, &phy->mt76->state);
317 
318 	mt7615_init_dfs_state(phy);
319 	mt76_set_channel(phy->mt76);
320 
321 	if (is_mt7615(&dev->mt76) && dev->flash_eeprom) {
322 		ret = mt7615_mcu_apply_rx_dcoc(phy);
323 		if (ret)
324 			goto out;
325 
326 		ret = mt7615_mcu_apply_tx_dpd(phy);
327 		if (ret)
328 			goto out;
329 	}
330 
331 	ret = mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD_CHANNEL_SWITCH);
332 	if (ret)
333 		goto out;
334 
335 	mt7615_mac_set_timing(phy);
336 	ret = mt7615_dfs_init_radar_detector(phy);
337 	if (ret)
338 		goto out;
339 
340 	mt7615_mac_cca_stats_reset(phy);
341 	ret = mt7615_mcu_set_sku_en(phy, true);
342 	if (ret)
343 		goto out;
344 
345 	mt7615_mac_reset_counters(dev);
346 	phy->noise = 0;
347 	phy->chfreq = mt76_rr(dev, MT_CHFREQ(ext_phy));
348 
349 out:
350 	clear_bit(MT76_RESET, &phy->mt76->state);
351 
352 	mt7615_mutex_release(dev);
353 
354 	mt76_worker_schedule(&dev->mt76.tx_worker);
355 	if (!mt76_testmode_enabled(phy->mt76))
356 		ieee80211_queue_delayed_work(phy->mt76->hw,
357 					     &phy->mt76->mac_work,
358 					     MT7615_WATCHDOG_TIME);
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 (vif->p2p)
570 			mt7615_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(&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->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(&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 	memset(mib, 0, sizeof(*mib));
861 
862 	mt7615_mutex_release(phy->dev);
863 
864 	return 0;
865 }
866 
867 static u64
868 mt7615_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
869 {
870 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
871 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
872 	union {
873 		u64 t64;
874 		u32 t32[2];
875 	} tsf;
876 	u16 idx = mvif->mt76.omac_idx;
877 	u32 reg;
878 
879 	idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
880 	reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
881 
882 	mt7615_mutex_acquire(dev);
883 
884 	mt76_set(dev, reg, MT_LPON_TCR_MODE); /* TSF read */
885 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0);
886 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1);
887 
888 	mt7615_mutex_release(dev);
889 
890 	return tsf.t64;
891 }
892 
893 static void
894 mt7615_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
895 	       u64 timestamp)
896 {
897 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
898 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
899 	union {
900 		u64 t64;
901 		u32 t32[2];
902 	} tsf = { .t64 = timestamp, };
903 	u16 idx = mvif->mt76.omac_idx;
904 	u32 reg;
905 
906 	idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
907 	reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
908 
909 	mt7615_mutex_acquire(dev);
910 
911 	mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
912 	mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
913 	/* TSF software overwrite */
914 	mt76_set(dev, reg, MT_LPON_TCR_WRITE);
915 
916 	mt7615_mutex_release(dev);
917 }
918 
919 static void
920 mt7615_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
921 {
922 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
923 	struct mt7615_dev *dev = phy->dev;
924 
925 	mt7615_mutex_acquire(dev);
926 	phy->coverage_class = max_t(s16, coverage_class, 0);
927 	mt7615_mac_set_timing(phy);
928 	mt7615_mutex_release(dev);
929 }
930 
931 static int
932 mt7615_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
933 {
934 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
935 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
936 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
937 	bool ext_phy = phy != &dev->phy;
938 
939 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
940 		return -EINVAL;
941 
942 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
943 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
944 
945 	mt7615_mutex_acquire(dev);
946 
947 	phy->mt76->antenna_mask = tx_ant;
948 	if (ext_phy) {
949 		if (dev->chainmask == 0xf)
950 			tx_ant <<= 2;
951 		else
952 			tx_ant <<= 1;
953 	}
954 	phy->mt76->chainmask = tx_ant;
955 
956 	mt76_set_stream_caps(phy->mt76, true);
957 
958 	mt7615_mutex_release(dev);
959 
960 	return 0;
961 }
962 
963 static void mt7615_roc_iter(void *priv, u8 *mac,
964 			    struct ieee80211_vif *vif)
965 {
966 	struct mt7615_phy *phy = priv;
967 
968 	mt7615_mcu_set_roc(phy, vif, NULL, 0);
969 }
970 
971 void mt7615_roc_work(struct work_struct *work)
972 {
973 	struct mt7615_phy *phy;
974 
975 	phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
976 						roc_work);
977 
978 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
979 		return;
980 
981 	mt7615_mutex_acquire(phy->dev);
982 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
983 					    IEEE80211_IFACE_ITER_RESUME_ALL,
984 					    mt7615_roc_iter, phy);
985 	mt7615_mutex_release(phy->dev);
986 	ieee80211_remain_on_channel_expired(phy->mt76->hw);
987 }
988 
989 void mt7615_roc_timer(struct timer_list *timer)
990 {
991 	struct mt7615_phy *phy = from_timer(phy, timer, roc_timer);
992 
993 	ieee80211_queue_work(phy->mt76->hw, &phy->roc_work);
994 }
995 
996 void mt7615_scan_work(struct work_struct *work)
997 {
998 	struct mt7615_phy *phy;
999 
1000 	phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1001 						scan_work.work);
1002 
1003 	while (true) {
1004 		struct mt7615_mcu_rxd *rxd;
1005 		struct sk_buff *skb;
1006 
1007 		spin_lock_bh(&phy->dev->mt76.lock);
1008 		skb = __skb_dequeue(&phy->scan_event_list);
1009 		spin_unlock_bh(&phy->dev->mt76.lock);
1010 
1011 		if (!skb)
1012 			break;
1013 
1014 		rxd = (struct mt7615_mcu_rxd *)skb->data;
1015 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1016 			ieee80211_sched_scan_results(phy->mt76->hw);
1017 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
1018 					      &phy->mt76->state)) {
1019 			struct cfg80211_scan_info info = {
1020 				.aborted = false,
1021 			};
1022 
1023 			ieee80211_scan_completed(phy->mt76->hw, &info);
1024 		}
1025 		dev_kfree_skb(skb);
1026 	}
1027 }
1028 
1029 static int
1030 mt7615_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1031 	       struct ieee80211_scan_request *req)
1032 {
1033 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1034 	struct mt76_phy *mphy = hw->priv;
1035 	int err;
1036 
1037 	/* fall-back to sw-scan */
1038 	if (!mt7615_firmware_offload(dev))
1039 		return 1;
1040 
1041 	mt7615_mutex_acquire(dev);
1042 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1043 	mt7615_mutex_release(dev);
1044 
1045 	return err;
1046 }
1047 
1048 static void
1049 mt7615_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1050 {
1051 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1052 	struct mt76_phy *mphy = hw->priv;
1053 
1054 	mt7615_mutex_acquire(dev);
1055 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1056 	mt7615_mutex_release(dev);
1057 }
1058 
1059 static int
1060 mt7615_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1061 			struct cfg80211_sched_scan_request *req,
1062 			struct ieee80211_scan_ies *ies)
1063 {
1064 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1065 	struct mt76_phy *mphy = hw->priv;
1066 	int err;
1067 
1068 	if (!mt7615_firmware_offload(dev))
1069 		return -EOPNOTSUPP;
1070 
1071 	mt7615_mutex_acquire(dev);
1072 
1073 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1074 	if (err < 0)
1075 		goto out;
1076 
1077 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1078 out:
1079 	mt7615_mutex_release(dev);
1080 
1081 	return err;
1082 }
1083 
1084 static int
1085 mt7615_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1086 {
1087 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1088 	struct mt76_phy *mphy = hw->priv;
1089 	int err;
1090 
1091 	if (!mt7615_firmware_offload(dev))
1092 		return -EOPNOTSUPP;
1093 
1094 	mt7615_mutex_acquire(dev);
1095 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1096 	mt7615_mutex_release(dev);
1097 
1098 	return err;
1099 }
1100 
1101 static int mt7615_remain_on_channel(struct ieee80211_hw *hw,
1102 				    struct ieee80211_vif *vif,
1103 				    struct ieee80211_channel *chan,
1104 				    int duration,
1105 				    enum ieee80211_roc_type type)
1106 {
1107 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1108 	int err;
1109 
1110 	if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
1111 		return 0;
1112 
1113 	mt7615_mutex_acquire(phy->dev);
1114 
1115 	err = mt7615_mcu_set_roc(phy, vif, chan, duration);
1116 	if (err < 0) {
1117 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1118 		goto out;
1119 	}
1120 
1121 	if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) {
1122 		mt7615_mcu_set_roc(phy, vif, NULL, 0);
1123 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1124 		err = -ETIMEDOUT;
1125 	}
1126 
1127 out:
1128 	mt7615_mutex_release(phy->dev);
1129 
1130 	return err;
1131 }
1132 
1133 static int mt7615_cancel_remain_on_channel(struct ieee80211_hw *hw,
1134 					   struct ieee80211_vif *vif)
1135 {
1136 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1137 	int err;
1138 
1139 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1140 		return 0;
1141 
1142 	del_timer_sync(&phy->roc_timer);
1143 	cancel_work_sync(&phy->roc_work);
1144 
1145 	mt7615_mutex_acquire(phy->dev);
1146 	err = mt7615_mcu_set_roc(phy, vif, NULL, 0);
1147 	mt7615_mutex_release(phy->dev);
1148 
1149 	return err;
1150 }
1151 
1152 static void mt7615_sta_set_decap_offload(struct ieee80211_hw *hw,
1153 				 struct ieee80211_vif *vif,
1154 				 struct ieee80211_sta *sta,
1155 				 bool enabled)
1156 {
1157 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1158 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
1159 
1160 	if (enabled)
1161 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1162 	else
1163 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1164 
1165 	mt7615_mcu_sta_update_hdr_trans(dev, vif, sta);
1166 }
1167 
1168 #ifdef CONFIG_PM
1169 static int mt7615_suspend(struct ieee80211_hw *hw,
1170 			  struct cfg80211_wowlan *wowlan)
1171 {
1172 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1173 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1174 	int err = 0;
1175 
1176 	cancel_delayed_work_sync(&dev->pm.ps_work);
1177 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1178 
1179 	mt7615_mutex_acquire(dev);
1180 
1181 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1182 	cancel_delayed_work_sync(&phy->scan_work);
1183 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1184 
1185 	set_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1186 	ieee80211_iterate_active_interfaces(hw,
1187 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1188 					    mt76_connac_mcu_set_suspend_iter,
1189 					    phy->mt76);
1190 
1191 	if (!mt7615_dev_running(dev))
1192 		err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
1193 
1194 	mt7615_mutex_release(dev);
1195 
1196 	return err;
1197 }
1198 
1199 static int mt7615_resume(struct ieee80211_hw *hw)
1200 {
1201 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1202 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1203 	bool running;
1204 
1205 	mt7615_mutex_acquire(dev);
1206 
1207 	running = mt7615_dev_running(dev);
1208 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1209 
1210 	if (!running) {
1211 		int err;
1212 
1213 		err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
1214 		if (err < 0) {
1215 			mt7615_mutex_release(dev);
1216 			return err;
1217 		}
1218 	}
1219 
1220 	clear_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 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1227 				     MT7615_WATCHDOG_TIME);
1228 
1229 	mt7615_mutex_release(dev);
1230 
1231 	return 0;
1232 }
1233 
1234 static void mt7615_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1235 {
1236 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1237 	struct mt76_dev *mdev = &dev->mt76;
1238 
1239 	device_set_wakeup_enable(mdev->dev, enabled);
1240 }
1241 
1242 static void mt7615_set_rekey_data(struct ieee80211_hw *hw,
1243 				  struct ieee80211_vif *vif,
1244 				  struct cfg80211_gtk_rekey_data *data)
1245 {
1246 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1247 
1248 	mt7615_mutex_acquire(dev);
1249 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1250 	mt7615_mutex_release(dev);
1251 }
1252 #endif /* CONFIG_PM */
1253 
1254 const struct ieee80211_ops mt7615_ops = {
1255 	.tx = mt7615_tx,
1256 	.start = mt7615_start,
1257 	.stop = mt7615_stop,
1258 	.add_interface = mt7615_add_interface,
1259 	.remove_interface = mt7615_remove_interface,
1260 	.config = mt7615_config,
1261 	.conf_tx = mt7615_conf_tx,
1262 	.configure_filter = mt7615_configure_filter,
1263 	.bss_info_changed = mt7615_bss_info_changed,
1264 	.sta_add = mt7615_sta_add,
1265 	.sta_remove = mt7615_sta_remove,
1266 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1267 	.set_key = mt7615_set_key,
1268 	.sta_set_decap_offload = mt7615_sta_set_decap_offload,
1269 	.ampdu_action = mt7615_ampdu_action,
1270 	.set_rts_threshold = mt7615_set_rts_threshold,
1271 	.wake_tx_queue = mt76_wake_tx_queue,
1272 	.sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
1273 	.sw_scan_start = mt76_sw_scan,
1274 	.sw_scan_complete = mt76_sw_scan_complete,
1275 	.release_buffered_frames = mt76_release_buffered_frames,
1276 	.get_txpower = mt76_get_txpower,
1277 	.channel_switch_beacon = mt7615_channel_switch_beacon,
1278 	.get_stats = mt7615_get_stats,
1279 	.get_tsf = mt7615_get_tsf,
1280 	.set_tsf = mt7615_set_tsf,
1281 	.get_survey = mt76_get_survey,
1282 	.get_antenna = mt76_get_antenna,
1283 	.set_antenna = mt7615_set_antenna,
1284 	.set_coverage_class = mt7615_set_coverage_class,
1285 	.hw_scan = mt7615_hw_scan,
1286 	.cancel_hw_scan = mt7615_cancel_hw_scan,
1287 	.sched_scan_start = mt7615_start_sched_scan,
1288 	.sched_scan_stop = mt7615_stop_sched_scan,
1289 	.remain_on_channel = mt7615_remain_on_channel,
1290 	.cancel_remain_on_channel = mt7615_cancel_remain_on_channel,
1291 	CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1292 	CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1293 #ifdef CONFIG_PM
1294 	.suspend = mt7615_suspend,
1295 	.resume = mt7615_resume,
1296 	.set_wakeup = mt7615_set_wakeup,
1297 	.set_rekey_data = mt7615_set_rekey_data,
1298 #endif /* CONFIG_PM */
1299 };
1300 EXPORT_SYMBOL_GPL(mt7615_ops);
1301 
1302 MODULE_LICENSE("Dual BSD/GPL");
1303