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 = mt7615_lmac_mapping(dev, queue);
401 	queue += mvif->wmm_idx * MT7615_MAX_WMM_SETS;
402 
403 	return mt7615_mcu_set_wmm(dev, queue, params);
404 }
405 
406 static void mt7615_configure_filter(struct ieee80211_hw *hw,
407 				    unsigned int changed_flags,
408 				    unsigned int *total_flags,
409 				    u64 multicast)
410 {
411 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
412 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
413 	bool band = phy != &dev->phy;
414 
415 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
416 			MT_WF_RFCR1_DROP_BF_POLL |
417 			MT_WF_RFCR1_DROP_BA |
418 			MT_WF_RFCR1_DROP_CFEND |
419 			MT_WF_RFCR1_DROP_CFACK;
420 	u32 flags = 0;
421 
422 #define MT76_FILTER(_flag, _hw) do { \
423 		flags |= *total_flags & FIF_##_flag;			\
424 		phy->rxfilter &= ~(_hw);				\
425 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
426 	} while (0)
427 
428 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
429 			   MT_WF_RFCR_DROP_OTHER_BEACON |
430 			   MT_WF_RFCR_DROP_FRAME_REPORT |
431 			   MT_WF_RFCR_DROP_PROBEREQ |
432 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
433 			   MT_WF_RFCR_DROP_MCAST |
434 			   MT_WF_RFCR_DROP_BCAST |
435 			   MT_WF_RFCR_DROP_DUPLICATE |
436 			   MT_WF_RFCR_DROP_A2_BSSID |
437 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
438 			   MT_WF_RFCR_DROP_STBC_MULTI);
439 
440 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
441 			       MT_WF_RFCR_DROP_A3_MAC |
442 			       MT_WF_RFCR_DROP_A3_BSSID);
443 
444 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
445 
446 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
447 			     MT_WF_RFCR_DROP_RTS |
448 			     MT_WF_RFCR_DROP_CTL_RSV |
449 			     MT_WF_RFCR_DROP_NDPA);
450 
451 	*total_flags = flags;
452 	mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
453 
454 	if (*total_flags & FIF_CONTROL)
455 		mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
456 	else
457 		mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
458 }
459 
460 static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
461 				    struct ieee80211_vif *vif,
462 				    struct ieee80211_bss_conf *info,
463 				    u32 changed)
464 {
465 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
466 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
467 
468 	mutex_lock(&dev->mt76.mutex);
469 
470 	if (changed & BSS_CHANGED_ERP_SLOT) {
471 		int slottime = info->use_short_slot ? 9 : 20;
472 
473 		if (slottime != phy->slottime) {
474 			phy->slottime = slottime;
475 			mt7615_mac_set_timing(phy);
476 		}
477 	}
478 
479 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
480 		mt7615_mcu_add_bss_info(phy, vif, NULL, info->enable_beacon);
481 		mt7615_mcu_sta_add(dev, vif, NULL, info->enable_beacon);
482 
483 		if (vif->p2p && info->enable_beacon)
484 			mt7615_mcu_set_p2p_oppps(hw, vif);
485 	}
486 
487 	if (changed & (BSS_CHANGED_BEACON |
488 		       BSS_CHANGED_BEACON_ENABLED))
489 		mt7615_mcu_add_beacon(dev, hw, vif, info->enable_beacon);
490 
491 	if (changed & BSS_CHANGED_PS)
492 		mt7615_mcu_set_vif_ps(dev, vif);
493 
494 	mutex_unlock(&dev->mt76.mutex);
495 }
496 
497 static void
498 mt7615_channel_switch_beacon(struct ieee80211_hw *hw,
499 			     struct ieee80211_vif *vif,
500 			     struct cfg80211_chan_def *chandef)
501 {
502 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
503 
504 	mutex_lock(&dev->mt76.mutex);
505 	mt7615_mcu_add_beacon(dev, hw, vif, true);
506 	mutex_unlock(&dev->mt76.mutex);
507 }
508 
509 int mt7615_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
510 		       struct ieee80211_sta *sta)
511 {
512 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
513 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
514 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
515 	int idx;
516 
517 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
518 	if (idx < 0)
519 		return -ENOSPC;
520 
521 	INIT_LIST_HEAD(&msta->poll_list);
522 	msta->vif = mvif;
523 	msta->wcid.sta = 1;
524 	msta->wcid.idx = idx;
525 	msta->wcid.ext_phy = mvif->band_idx;
526 
527 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
528 		struct mt7615_phy *phy;
529 
530 		phy = mvif->band_idx ? mt7615_ext_phy(dev) : &dev->phy;
531 		mt7615_mcu_add_bss_info(phy, vif, sta, true);
532 	}
533 	mt7615_mac_wtbl_update(dev, idx,
534 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
535 	mt7615_mcu_sta_add(dev, vif, sta, true);
536 
537 	return 0;
538 }
539 EXPORT_SYMBOL_GPL(mt7615_mac_sta_add);
540 
541 void mt7615_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
542 			   struct ieee80211_sta *sta)
543 {
544 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
545 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
546 
547 	mt7615_mcu_sta_add(dev, vif, sta, false);
548 	mt7615_mac_wtbl_update(dev, msta->wcid.idx,
549 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
550 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
551 		struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
552 		struct mt7615_phy *phy;
553 
554 		phy = mvif->band_idx ? mt7615_ext_phy(dev) : &dev->phy;
555 		mt7615_mcu_add_bss_info(phy, vif, sta, false);
556 	}
557 
558 	spin_lock_bh(&dev->sta_poll_lock);
559 	if (!list_empty(&msta->poll_list))
560 		list_del_init(&msta->poll_list);
561 	spin_unlock_bh(&dev->sta_poll_lock);
562 }
563 EXPORT_SYMBOL_GPL(mt7615_mac_sta_remove);
564 
565 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
566 				       struct ieee80211_vif *vif,
567 				       struct ieee80211_sta *sta)
568 {
569 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
570 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
571 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
572 	struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
573 	int i;
574 
575 	spin_lock_bh(&dev->mt76.lock);
576 	for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
577 		msta->rates[i].idx = sta_rates->rate[i].idx;
578 		msta->rates[i].count = sta_rates->rate[i].count;
579 		msta->rates[i].flags = sta_rates->rate[i].flags;
580 
581 		if (msta->rates[i].idx < 0 || !msta->rates[i].count)
582 			break;
583 	}
584 	msta->n_rates = i;
585 	mt7615_mac_set_rates(phy, msta, NULL, msta->rates);
586 	msta->rate_probe = false;
587 	spin_unlock_bh(&dev->mt76.lock);
588 }
589 
590 static void mt7615_tx(struct ieee80211_hw *hw,
591 		      struct ieee80211_tx_control *control,
592 		      struct sk_buff *skb)
593 {
594 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
595 	struct mt76_phy *mphy = hw->priv;
596 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
597 	struct ieee80211_vif *vif = info->control.vif;
598 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
599 
600 	if (control->sta) {
601 		struct mt7615_sta *sta;
602 
603 		sta = (struct mt7615_sta *)control->sta->drv_priv;
604 		wcid = &sta->wcid;
605 	}
606 
607 	if (vif && !control->sta) {
608 		struct mt7615_vif *mvif;
609 
610 		mvif = (struct mt7615_vif *)vif->drv_priv;
611 		wcid = &mvif->sta.wcid;
612 	}
613 
614 	mt76_tx(mphy, control->sta, wcid, skb);
615 }
616 
617 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
618 {
619 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
620 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
621 
622 	mutex_lock(&dev->mt76.mutex);
623 	mt7615_mcu_set_rts_thresh(phy, val);
624 	mutex_unlock(&dev->mt76.mutex);
625 
626 	return 0;
627 }
628 
629 static int
630 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
631 		    struct ieee80211_ampdu_params *params)
632 {
633 	enum ieee80211_ampdu_mlme_action action = params->action;
634 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
635 	struct ieee80211_sta *sta = params->sta;
636 	struct ieee80211_txq *txq = sta->txq[params->tid];
637 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
638 	u16 tid = params->tid;
639 	u16 ssn = params->ssn;
640 	struct mt76_txq *mtxq;
641 	int ret = 0;
642 
643 	if (!txq)
644 		return -EINVAL;
645 
646 	mtxq = (struct mt76_txq *)txq->drv_priv;
647 
648 	mutex_lock(&dev->mt76.mutex);
649 	switch (action) {
650 	case IEEE80211_AMPDU_RX_START:
651 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
652 				   params->buf_size);
653 		mt7615_mcu_add_rx_ba(dev, params, true);
654 		break;
655 	case IEEE80211_AMPDU_RX_STOP:
656 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
657 		mt7615_mcu_add_rx_ba(dev, params, false);
658 		break;
659 	case IEEE80211_AMPDU_TX_OPERATIONAL:
660 		mtxq->aggr = true;
661 		mtxq->send_bar = false;
662 		mt7615_mcu_add_tx_ba(dev, params, true);
663 		break;
664 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
665 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
666 		mtxq->aggr = false;
667 		mt7615_mcu_add_tx_ba(dev, params, false);
668 		break;
669 	case IEEE80211_AMPDU_TX_START:
670 		mtxq->agg_ssn = IEEE80211_SN_TO_SEQ(ssn);
671 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
672 		break;
673 	case IEEE80211_AMPDU_TX_STOP_CONT:
674 		mtxq->aggr = false;
675 		mt7615_mcu_add_tx_ba(dev, params, false);
676 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
677 		break;
678 	}
679 	mutex_unlock(&dev->mt76.mutex);
680 
681 	return ret;
682 }
683 
684 static int
685 mt7615_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
686 	       struct ieee80211_sta *sta)
687 {
688     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
689 			  IEEE80211_STA_NONE);
690 }
691 
692 static int
693 mt7615_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
694 		  struct ieee80211_sta *sta)
695 {
696     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
697 			  IEEE80211_STA_NOTEXIST);
698 }
699 
700 static int
701 mt7615_get_stats(struct ieee80211_hw *hw,
702 		 struct ieee80211_low_level_stats *stats)
703 {
704 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
705 	struct mib_stats *mib = &phy->mib;
706 
707 	stats->dot11RTSSuccessCount = mib->rts_cnt;
708 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
709 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
710 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
711 
712 	return 0;
713 }
714 
715 static u64
716 mt7615_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
717 {
718 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
719 	union {
720 		u64 t64;
721 		u32 t32[2];
722 	} tsf;
723 
724 	mutex_lock(&dev->mt76.mutex);
725 
726 	mt76_set(dev, MT_LPON_T0CR, MT_LPON_T0CR_MODE); /* TSF read */
727 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0);
728 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1);
729 
730 	mutex_unlock(&dev->mt76.mutex);
731 
732 	return tsf.t64;
733 }
734 
735 static void
736 mt7615_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
737 {
738 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
739 	struct mt7615_dev *dev = phy->dev;
740 
741 	mutex_lock(&dev->mt76.mutex);
742 	phy->coverage_class = max_t(s16, coverage_class, 0);
743 	mt7615_mac_set_timing(phy);
744 	mutex_unlock(&dev->mt76.mutex);
745 }
746 
747 static int
748 mt7615_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
749 {
750 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
751 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
752 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
753 	bool ext_phy = phy != &dev->phy;
754 
755 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
756 		return -EINVAL;
757 
758 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
759 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
760 
761 	mutex_lock(&dev->mt76.mutex);
762 
763 	phy->mt76->antenna_mask = tx_ant;
764 	if (ext_phy) {
765 		if (dev->chainmask == 0xf)
766 			tx_ant <<= 2;
767 		else
768 			tx_ant <<= 1;
769 	}
770 	phy->chainmask = tx_ant;
771 
772 	mt76_set_stream_caps(phy->mt76, true);
773 
774 	mutex_unlock(&dev->mt76.mutex);
775 
776 	return 0;
777 }
778 
779 static void mt7615_roc_iter(void *priv, u8 *mac,
780 			    struct ieee80211_vif *vif)
781 {
782 	struct mt7615_phy *phy = priv;
783 
784 	mt7615_mcu_set_roc(phy, vif, NULL, 0);
785 }
786 
787 void mt7615_roc_work(struct work_struct *work)
788 {
789 	struct mt7615_phy *phy;
790 
791 	phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
792 						roc_work);
793 
794 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
795 		return;
796 
797 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
798 					    IEEE80211_IFACE_ITER_RESUME_ALL,
799 					    mt7615_roc_iter, phy);
800 	ieee80211_remain_on_channel_expired(phy->mt76->hw);
801 }
802 
803 void mt7615_roc_timer(struct timer_list *timer)
804 {
805 	struct mt7615_phy *phy = from_timer(phy, timer, roc_timer);
806 
807 	ieee80211_queue_work(phy->mt76->hw, &phy->roc_work);
808 }
809 
810 void mt7615_scan_work(struct work_struct *work)
811 {
812 	struct mt7615_phy *phy;
813 
814 	phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
815 						scan_work.work);
816 
817 	while (true) {
818 		struct mt7615_mcu_rxd *rxd;
819 		struct sk_buff *skb;
820 
821 		spin_lock_bh(&phy->dev->mt76.lock);
822 		skb = __skb_dequeue(&phy->scan_event_list);
823 		spin_unlock_bh(&phy->dev->mt76.lock);
824 
825 		if (!skb)
826 			break;
827 
828 		rxd = (struct mt7615_mcu_rxd *)skb->data;
829 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
830 			ieee80211_sched_scan_results(phy->mt76->hw);
831 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
832 					      &phy->mt76->state)) {
833 			struct cfg80211_scan_info info = {
834 				.aborted = false,
835 			};
836 
837 			ieee80211_scan_completed(phy->mt76->hw, &info);
838 		}
839 		dev_kfree_skb(skb);
840 	}
841 }
842 
843 static int
844 mt7615_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
845 	       struct ieee80211_scan_request *req)
846 {
847 	struct mt76_phy *mphy = hw->priv;
848 
849 	return mt7615_mcu_hw_scan(mphy->priv, vif, req);
850 }
851 
852 static void
853 mt7615_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
854 {
855 	struct mt76_phy *mphy = hw->priv;
856 
857 	mt7615_mcu_cancel_hw_scan(mphy->priv, vif);
858 }
859 
860 static int
861 mt7615_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
862 			struct cfg80211_sched_scan_request *req,
863 			struct ieee80211_scan_ies *ies)
864 {
865 	struct mt76_phy *mphy = hw->priv;
866 	int err;
867 
868 	err = mt7615_mcu_sched_scan_req(mphy->priv, vif, req);
869 	if (err < 0)
870 		return err;
871 
872 	return mt7615_mcu_sched_scan_enable(mphy->priv, vif, true);
873 }
874 
875 static int
876 mt7615_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
877 {
878 	struct mt76_phy *mphy = hw->priv;
879 
880 	return mt7615_mcu_sched_scan_enable(mphy->priv, vif, false);
881 }
882 
883 static int mt7615_remain_on_channel(struct ieee80211_hw *hw,
884 				    struct ieee80211_vif *vif,
885 				    struct ieee80211_channel *chan,
886 				    int duration,
887 				    enum ieee80211_roc_type type)
888 {
889 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
890 	int err;
891 
892 	if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
893 		return 0;
894 
895 	err = mt7615_mcu_set_roc(phy, vif, chan, duration);
896 	if (err < 0) {
897 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
898 		return err;
899 	}
900 
901 	if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) {
902 		mt7615_mcu_set_roc(phy, vif, NULL, 0);
903 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
904 
905 		return -ETIMEDOUT;
906 	}
907 
908 	return 0;
909 }
910 
911 static int mt7615_cancel_remain_on_channel(struct ieee80211_hw *hw,
912 					   struct ieee80211_vif *vif)
913 {
914 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
915 
916 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
917 		return 0;
918 
919 	del_timer_sync(&phy->roc_timer);
920 	cancel_work_sync(&phy->roc_work);
921 
922 	mt7615_mcu_set_roc(phy, vif, NULL, 0);
923 
924 	return 0;
925 }
926 
927 #ifdef CONFIG_PM
928 static int mt7615_suspend(struct ieee80211_hw *hw,
929 			  struct cfg80211_wowlan *wowlan)
930 {
931 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
932 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
933 	bool ext_phy = phy != &dev->phy;
934 	int err = 0;
935 
936 	mutex_lock(&dev->mt76.mutex);
937 
938 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
939 	cancel_delayed_work_sync(&phy->scan_work);
940 	cancel_delayed_work_sync(&phy->mac_work);
941 
942 	mt76_set(dev, MT_WF_RFCR(ext_phy), MT_WF_RFCR_DROP_OTHER_BEACON);
943 
944 	set_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
945 	ieee80211_iterate_active_interfaces(hw,
946 					    IEEE80211_IFACE_ITER_RESUME_ALL,
947 					    mt7615_mcu_set_suspend_iter, phy);
948 
949 	if (!mt7615_dev_running(dev))
950 		err = mt7615_mcu_set_hif_suspend(dev, true);
951 
952 	mutex_unlock(&dev->mt76.mutex);
953 
954 	return err;
955 }
956 
957 static int mt7615_resume(struct ieee80211_hw *hw)
958 {
959 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
960 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
961 	bool running, ext_phy = phy != &dev->phy;
962 
963 	mutex_lock(&dev->mt76.mutex);
964 
965 	running = mt7615_dev_running(dev);
966 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
967 
968 	if (!running) {
969 		int err;
970 
971 		err = mt7615_mcu_set_hif_suspend(dev, false);
972 		if (err < 0) {
973 			mutex_unlock(&dev->mt76.mutex);
974 			return err;
975 		}
976 	}
977 
978 	clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
979 	ieee80211_iterate_active_interfaces(hw,
980 					    IEEE80211_IFACE_ITER_RESUME_ALL,
981 					    mt7615_mcu_set_suspend_iter, phy);
982 
983 	ieee80211_queue_delayed_work(hw, &phy->mac_work,
984 				     MT7615_WATCHDOG_TIME);
985 	mt76_clear(dev, MT_WF_RFCR(ext_phy), MT_WF_RFCR_DROP_OTHER_BEACON);
986 
987 	mutex_unlock(&dev->mt76.mutex);
988 
989 	return 0;
990 }
991 
992 static void mt7615_set_wakeup(struct ieee80211_hw *hw, bool enabled)
993 {
994 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
995 	struct mt76_dev *mdev = &dev->mt76;
996 
997 	device_set_wakeup_enable(mdev->dev, enabled);
998 }
999 
1000 static void mt7615_set_rekey_data(struct ieee80211_hw *hw,
1001 				  struct ieee80211_vif *vif,
1002 				  struct cfg80211_gtk_rekey_data *data)
1003 {
1004 	mt7615_mcu_update_gtk_rekey(hw, vif, data);
1005 }
1006 #endif /* CONFIG_PM */
1007 
1008 const struct ieee80211_ops mt7615_ops = {
1009 	.tx = mt7615_tx,
1010 	.start = mt7615_start,
1011 	.stop = mt7615_stop,
1012 	.add_interface = mt7615_add_interface,
1013 	.remove_interface = mt7615_remove_interface,
1014 	.config = mt7615_config,
1015 	.conf_tx = mt7615_conf_tx,
1016 	.configure_filter = mt7615_configure_filter,
1017 	.bss_info_changed = mt7615_bss_info_changed,
1018 	.sta_add = mt7615_sta_add,
1019 	.sta_remove = mt7615_sta_remove,
1020 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1021 	.set_key = mt7615_set_key,
1022 	.ampdu_action = mt7615_ampdu_action,
1023 	.set_rts_threshold = mt7615_set_rts_threshold,
1024 	.wake_tx_queue = mt76_wake_tx_queue,
1025 	.sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
1026 	.sw_scan_start = mt76_sw_scan,
1027 	.sw_scan_complete = mt76_sw_scan_complete,
1028 	.release_buffered_frames = mt76_release_buffered_frames,
1029 	.get_txpower = mt76_get_txpower,
1030 	.channel_switch_beacon = mt7615_channel_switch_beacon,
1031 	.get_stats = mt7615_get_stats,
1032 	.get_tsf = mt7615_get_tsf,
1033 	.get_survey = mt76_get_survey,
1034 	.get_antenna = mt76_get_antenna,
1035 	.set_antenna = mt7615_set_antenna,
1036 	.set_coverage_class = mt7615_set_coverage_class,
1037 	.hw_scan = mt7615_hw_scan,
1038 	.cancel_hw_scan = mt7615_cancel_hw_scan,
1039 	.sched_scan_start = mt7615_start_sched_scan,
1040 	.sched_scan_stop = mt7615_stop_sched_scan,
1041 	.remain_on_channel = mt7615_remain_on_channel,
1042 	.cancel_remain_on_channel = mt7615_cancel_remain_on_channel,
1043 #ifdef CONFIG_PM
1044 	.suspend = mt7615_suspend,
1045 	.resume = mt7615_resume,
1046 	.set_wakeup = mt7615_set_wakeup,
1047 	.set_rekey_data = mt7615_set_rekey_data,
1048 #endif /* CONFIG_PM */
1049 };
1050 EXPORT_SYMBOL_GPL(mt7615_ops);
1051 
1052 MODULE_LICENSE("Dual BSD/GPL");
1053