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