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