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