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(dev);
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.ext_phy = 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(dev);
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,u16 queue,const struct ieee80211_tx_queue_params * params)476 mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
477 	       const struct ieee80211_tx_queue_params *params)
478 {
479 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
480 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
481 	int err;
482 
483 	mt7615_mutex_acquire(dev);
484 
485 	queue = mt7615_lmac_mapping(dev, queue);
486 	queue += mvif->wmm_idx * MT7615_MAX_WMM_SETS;
487 	err = mt7615_mcu_set_wmm(dev, queue, params);
488 
489 	mt7615_mutex_release(dev);
490 
491 	return err;
492 }
493 
mt7615_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)494 static void mt7615_configure_filter(struct ieee80211_hw *hw,
495 				    unsigned int changed_flags,
496 				    unsigned int *total_flags,
497 				    u64 multicast)
498 {
499 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
500 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
501 	bool band = phy != &dev->phy;
502 
503 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
504 			MT_WF_RFCR1_DROP_BF_POLL |
505 			MT_WF_RFCR1_DROP_BA |
506 			MT_WF_RFCR1_DROP_CFEND |
507 			MT_WF_RFCR1_DROP_CFACK;
508 	u32 flags = 0;
509 
510 	mt7615_mutex_acquire(dev);
511 
512 #define MT76_FILTER(_flag, _hw) do { \
513 		flags |= *total_flags & FIF_##_flag;			\
514 		phy->rxfilter &= ~(_hw);				\
515 		if (!mt76_testmode_enabled(phy->mt76))			\
516 			phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);\
517 	} while (0)
518 
519 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
520 			   MT_WF_RFCR_DROP_FRAME_REPORT |
521 			   MT_WF_RFCR_DROP_PROBEREQ |
522 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
523 			   MT_WF_RFCR_DROP_MCAST |
524 			   MT_WF_RFCR_DROP_BCAST |
525 			   MT_WF_RFCR_DROP_DUPLICATE |
526 			   MT_WF_RFCR_DROP_A2_BSSID |
527 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
528 			   MT_WF_RFCR_DROP_STBC_MULTI);
529 
530 	if (phy->n_beacon_vif || !mt7615_firmware_offload(dev))
531 		phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_BEACON;
532 
533 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
534 			       MT_WF_RFCR_DROP_A3_MAC |
535 			       MT_WF_RFCR_DROP_A3_BSSID);
536 
537 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
538 
539 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
540 			     MT_WF_RFCR_DROP_RTS |
541 			     MT_WF_RFCR_DROP_CTL_RSV |
542 			     MT_WF_RFCR_DROP_NDPA);
543 
544 	*total_flags = flags;
545 	mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
546 
547 	if (*total_flags & FIF_CONTROL)
548 		mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
549 	else
550 		mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
551 
552 	mt7615_mutex_release(dev);
553 }
554 
mt7615_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u32 changed)555 static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
556 				    struct ieee80211_vif *vif,
557 				    struct ieee80211_bss_conf *info,
558 				    u32 changed)
559 {
560 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
561 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
562 
563 	mt7615_mutex_acquire(dev);
564 
565 	if (changed & BSS_CHANGED_ERP_SLOT) {
566 		int slottime = info->use_short_slot ? 9 : 20;
567 
568 		if (slottime != phy->slottime) {
569 			phy->slottime = slottime;
570 			mt7615_mac_set_timing(phy);
571 		}
572 	}
573 
574 	if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
575 		mt7615_mcu_add_bss_info(phy, vif, NULL, true);
576 		mt7615_mcu_sta_add(phy, vif, NULL, true);
577 
578 		if (mt7615_firmware_offload(dev) && vif->p2p)
579 			mt76_connac_mcu_set_p2p_oppps(hw, vif);
580 	}
581 
582 	if (changed & (BSS_CHANGED_BEACON |
583 		       BSS_CHANGED_BEACON_ENABLED))
584 		mt7615_mcu_add_beacon(dev, hw, vif, info->enable_beacon);
585 
586 	if (changed & BSS_CHANGED_PS)
587 		mt76_connac_mcu_set_vif_ps(&dev->mt76, vif);
588 
589 	if ((changed & BSS_CHANGED_ARP_FILTER) &&
590 	    mt7615_firmware_offload(dev)) {
591 		struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
592 
593 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
594 						  info);
595 	}
596 
597 	if (changed & BSS_CHANGED_ASSOC)
598 		mt7615_mac_set_beacon_filter(phy, vif, info->assoc);
599 
600 	mt7615_mutex_release(dev);
601 }
602 
603 static void
mt7615_channel_switch_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_chan_def * chandef)604 mt7615_channel_switch_beacon(struct ieee80211_hw *hw,
605 			     struct ieee80211_vif *vif,
606 			     struct cfg80211_chan_def *chandef)
607 {
608 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
609 
610 	mt7615_mutex_acquire(dev);
611 	mt7615_mcu_add_beacon(dev, hw, vif, true);
612 	mt7615_mutex_release(dev);
613 }
614 
mt7615_mac_sta_add(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)615 int mt7615_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
616 		       struct ieee80211_sta *sta)
617 {
618 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
619 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
620 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
621 	struct mt7615_phy *phy;
622 	int idx, err;
623 
624 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
625 	if (idx < 0)
626 		return -ENOSPC;
627 
628 	INIT_LIST_HEAD(&msta->poll_list);
629 	msta->vif = mvif;
630 	msta->wcid.sta = 1;
631 	msta->wcid.idx = idx;
632 	msta->wcid.ext_phy = mvif->mt76.band_idx;
633 
634 	phy = mvif->mt76.band_idx ? mt7615_ext_phy(dev) : &dev->phy;
635 	err = mt76_connac_pm_wake(phy->mt76, &dev->pm);
636 	if (err)
637 		return err;
638 
639 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
640 		err = mt7615_mcu_add_bss_info(phy, vif, sta, true);
641 		if (err)
642 			return err;
643 	}
644 
645 	mt7615_mac_wtbl_update(dev, idx,
646 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
647 	err = mt7615_mcu_sta_add(&dev->phy, vif, sta, true);
648 	if (err)
649 		return err;
650 
651 	mt76_connac_power_save_sched(phy->mt76, &dev->pm);
652 
653 	return err;
654 }
655 EXPORT_SYMBOL_GPL(mt7615_mac_sta_add);
656 
mt7615_mac_sta_remove(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)657 void mt7615_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
658 			   struct ieee80211_sta *sta)
659 {
660 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
661 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
662 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
663 	struct mt7615_phy *phy;
664 
665 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
666 
667 	phy = mvif->mt76.band_idx ? mt7615_ext_phy(dev) : &dev->phy;
668 	mt76_connac_pm_wake(phy->mt76, &dev->pm);
669 
670 	mt7615_mcu_sta_add(&dev->phy, vif, sta, false);
671 	mt7615_mac_wtbl_update(dev, msta->wcid.idx,
672 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
673 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
674 		mt7615_mcu_add_bss_info(phy, vif, sta, false);
675 
676 	spin_lock_bh(&dev->sta_poll_lock);
677 	if (!list_empty(&msta->poll_list))
678 		list_del_init(&msta->poll_list);
679 	spin_unlock_bh(&dev->sta_poll_lock);
680 
681 	mt76_connac_power_save_sched(phy->mt76, &dev->pm);
682 }
683 EXPORT_SYMBOL_GPL(mt7615_mac_sta_remove);
684 
mt7615_sta_rate_tbl_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)685 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
686 				       struct ieee80211_vif *vif,
687 				       struct ieee80211_sta *sta)
688 {
689 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
690 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
691 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
692 	struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
693 	int i;
694 
695 	if (!sta_rates)
696 		return;
697 
698 	spin_lock_bh(&dev->mt76.lock);
699 	for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
700 		msta->rates[i].idx = sta_rates->rate[i].idx;
701 		msta->rates[i].count = sta_rates->rate[i].count;
702 		msta->rates[i].flags = sta_rates->rate[i].flags;
703 
704 		if (msta->rates[i].idx < 0 || !msta->rates[i].count)
705 			break;
706 	}
707 	msta->n_rates = i;
708 	if (mt76_connac_pm_ref(phy->mt76, &dev->pm)) {
709 		mt7615_mac_set_rates(phy, msta, NULL, msta->rates);
710 		mt76_connac_pm_unref(phy->mt76, &dev->pm);
711 	}
712 	spin_unlock_bh(&dev->mt76.lock);
713 }
714 
mt7615_tx_worker(struct mt76_worker * w)715 void mt7615_tx_worker(struct mt76_worker *w)
716 {
717 	struct mt7615_dev *dev = container_of(w, struct mt7615_dev,
718 					      mt76.tx_worker);
719 
720 	if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
721 		queue_work(dev->mt76.wq, &dev->pm.wake_work);
722 		return;
723 	}
724 
725 	mt76_tx_worker_run(&dev->mt76);
726 	mt76_connac_pm_unref(&dev->mphy, &dev->pm);
727 }
728 
mt7615_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)729 static void mt7615_tx(struct ieee80211_hw *hw,
730 		      struct ieee80211_tx_control *control,
731 		      struct sk_buff *skb)
732 {
733 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
734 	struct mt76_phy *mphy = hw->priv;
735 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
736 	struct ieee80211_vif *vif = info->control.vif;
737 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
738 	struct mt7615_sta *msta = NULL;
739 	int qid;
740 
741 	if (control->sta) {
742 		msta = (struct mt7615_sta *)control->sta->drv_priv;
743 		wcid = &msta->wcid;
744 	}
745 
746 	if (vif && !control->sta) {
747 		struct mt7615_vif *mvif;
748 
749 		mvif = (struct mt7615_vif *)vif->drv_priv;
750 		msta = &mvif->sta;
751 		wcid = &msta->wcid;
752 	}
753 
754 	if (mt76_connac_pm_ref(mphy, &dev->pm)) {
755 		mt76_tx(mphy, control->sta, wcid, skb);
756 		mt76_connac_pm_unref(mphy, &dev->pm);
757 		return;
758 	}
759 
760 	qid = skb_get_queue_mapping(skb);
761 	if (qid >= MT_TXQ_PSD) {
762 		qid = IEEE80211_AC_BE;
763 		skb_set_queue_mapping(skb, qid);
764 	}
765 
766 	mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
767 }
768 
mt7615_set_rts_threshold(struct ieee80211_hw * hw,u32 val)769 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
770 {
771 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
772 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
773 	int err, band = phy != &dev->phy;
774 
775 	mt7615_mutex_acquire(dev);
776 	err = mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, band);
777 	mt7615_mutex_release(dev);
778 
779 	return err;
780 }
781 
782 static int
mt7615_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)783 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
784 		    struct ieee80211_ampdu_params *params)
785 {
786 	enum ieee80211_ampdu_mlme_action action = params->action;
787 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
788 	struct ieee80211_sta *sta = params->sta;
789 	struct ieee80211_txq *txq = sta->txq[params->tid];
790 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
791 	u16 tid = params->tid;
792 	u16 ssn = params->ssn;
793 	struct mt76_txq *mtxq;
794 	int ret = 0;
795 
796 	if (!txq)
797 		return -EINVAL;
798 
799 	mtxq = (struct mt76_txq *)txq->drv_priv;
800 
801 	mt7615_mutex_acquire(dev);
802 
803 	switch (action) {
804 	case IEEE80211_AMPDU_RX_START:
805 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
806 				   params->buf_size);
807 		ret = mt7615_mcu_add_rx_ba(dev, params, true);
808 		break;
809 	case IEEE80211_AMPDU_RX_STOP:
810 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
811 		ret = mt7615_mcu_add_rx_ba(dev, params, false);
812 		break;
813 	case IEEE80211_AMPDU_TX_OPERATIONAL:
814 		mtxq->aggr = true;
815 		mtxq->send_bar = false;
816 		ret = mt7615_mcu_add_tx_ba(dev, params, true);
817 		ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid);
818 		ieee80211_send_bar(vif, sta->addr, tid,
819 				   IEEE80211_SN_TO_SEQ(ssn));
820 		break;
821 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
822 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
823 		mtxq->aggr = false;
824 		ret = mt7615_mcu_add_tx_ba(dev, params, false);
825 		break;
826 	case IEEE80211_AMPDU_TX_START:
827 		ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid);
828 		params->ssn = ssn;
829 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
830 		break;
831 	case IEEE80211_AMPDU_TX_STOP_CONT:
832 		mtxq->aggr = false;
833 		ret = mt7615_mcu_add_tx_ba(dev, params, false);
834 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
835 		break;
836 	}
837 	mt7615_mutex_release(dev);
838 
839 	return ret;
840 }
841 
842 static int
mt7615_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)843 mt7615_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
844 	       struct ieee80211_sta *sta)
845 {
846     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
847 			  IEEE80211_STA_NONE);
848 }
849 
850 static int
mt7615_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)851 mt7615_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
852 		  struct ieee80211_sta *sta)
853 {
854     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
855 			  IEEE80211_STA_NOTEXIST);
856 }
857 
858 static int
mt7615_get_stats(struct ieee80211_hw * hw,struct ieee80211_low_level_stats * stats)859 mt7615_get_stats(struct ieee80211_hw *hw,
860 		 struct ieee80211_low_level_stats *stats)
861 {
862 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
863 	struct mib_stats *mib = &phy->mib;
864 
865 	mt7615_mutex_acquire(phy->dev);
866 
867 	stats->dot11RTSSuccessCount = mib->rts_cnt;
868 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
869 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
870 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
871 
872 	mt7615_mutex_release(phy->dev);
873 
874 	return 0;
875 }
876 
877 static u64
mt7615_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)878 mt7615_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
879 {
880 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
881 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
882 	union {
883 		u64 t64;
884 		u32 t32[2];
885 	} tsf;
886 	u16 idx = mvif->mt76.omac_idx;
887 	u32 reg;
888 
889 	idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
890 	reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
891 
892 	mt7615_mutex_acquire(dev);
893 
894 	/* TSF read */
895 	mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_READ);
896 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0);
897 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1);
898 
899 	mt7615_mutex_release(dev);
900 
901 	return tsf.t64;
902 }
903 
904 static void
mt7615_set_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 timestamp)905 mt7615_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
906 	       u64 timestamp)
907 {
908 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
909 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
910 	union {
911 		u64 t64;
912 		u32 t32[2];
913 	} tsf = { .t64 = timestamp, };
914 	u16 idx = mvif->mt76.omac_idx;
915 	u32 reg;
916 
917 	idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
918 	reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
919 
920 	mt7615_mutex_acquire(dev);
921 
922 	mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
923 	mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
924 	/* TSF software overwrite */
925 	mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_WRITE);
926 
927 	mt7615_mutex_release(dev);
928 }
929 
930 static void
mt7615_offset_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,s64 timestamp)931 mt7615_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
932 		  s64 timestamp)
933 {
934 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
935 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
936 	union {
937 		u64 t64;
938 		u32 t32[2];
939 	} tsf = { .t64 = timestamp, };
940 	u16 idx = mvif->mt76.omac_idx;
941 	u32 reg;
942 
943 	idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
944 	reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
945 
946 	mt7615_mutex_acquire(dev);
947 
948 	mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
949 	mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
950 	/* TSF software adjust*/
951 	mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_ADJUST);
952 
953 	mt7615_mutex_release(dev);
954 }
955 
956 static void
mt7615_set_coverage_class(struct ieee80211_hw * hw,s16 coverage_class)957 mt7615_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
958 {
959 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
960 	struct mt7615_dev *dev = phy->dev;
961 
962 	mt7615_mutex_acquire(dev);
963 	phy->coverage_class = max_t(s16, coverage_class, 0);
964 	mt7615_mac_set_timing(phy);
965 	mt7615_mutex_release(dev);
966 }
967 
968 static int
mt7615_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)969 mt7615_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
970 {
971 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
972 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
973 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
974 	bool ext_phy = phy != &dev->phy;
975 
976 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
977 		return -EINVAL;
978 
979 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
980 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
981 
982 	mt7615_mutex_acquire(dev);
983 
984 	phy->mt76->antenna_mask = tx_ant;
985 	if (ext_phy) {
986 		if (dev->chainmask == 0xf)
987 			tx_ant <<= 2;
988 		else
989 			tx_ant <<= 1;
990 	}
991 	phy->mt76->chainmask = tx_ant;
992 
993 	mt76_set_stream_caps(phy->mt76, true);
994 
995 	mt7615_mutex_release(dev);
996 
997 	return 0;
998 }
999 
mt7615_roc_iter(void * priv,u8 * mac,struct ieee80211_vif * vif)1000 static void mt7615_roc_iter(void *priv, u8 *mac,
1001 			    struct ieee80211_vif *vif)
1002 {
1003 	struct mt7615_phy *phy = priv;
1004 
1005 	mt7615_mcu_set_roc(phy, vif, NULL, 0);
1006 }
1007 
mt7615_roc_work(struct work_struct * work)1008 void mt7615_roc_work(struct work_struct *work)
1009 {
1010 	struct mt7615_phy *phy;
1011 
1012 	phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1013 						roc_work);
1014 
1015 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1016 		return;
1017 
1018 	mt7615_mutex_acquire(phy->dev);
1019 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
1020 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1021 					    mt7615_roc_iter, phy);
1022 	mt7615_mutex_release(phy->dev);
1023 	ieee80211_remain_on_channel_expired(phy->mt76->hw);
1024 }
1025 
mt7615_roc_timer(struct timer_list * timer)1026 void mt7615_roc_timer(struct timer_list *timer)
1027 {
1028 	struct mt7615_phy *phy = from_timer(phy, timer, roc_timer);
1029 
1030 	ieee80211_queue_work(phy->mt76->hw, &phy->roc_work);
1031 }
1032 
mt7615_scan_work(struct work_struct * work)1033 void mt7615_scan_work(struct work_struct *work)
1034 {
1035 	struct mt7615_phy *phy;
1036 
1037 	phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1038 						scan_work.work);
1039 
1040 	while (true) {
1041 		struct mt7615_mcu_rxd *rxd;
1042 		struct sk_buff *skb;
1043 
1044 		spin_lock_bh(&phy->dev->mt76.lock);
1045 		skb = __skb_dequeue(&phy->scan_event_list);
1046 		spin_unlock_bh(&phy->dev->mt76.lock);
1047 
1048 		if (!skb)
1049 			break;
1050 
1051 		rxd = (struct mt7615_mcu_rxd *)skb->data;
1052 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1053 			ieee80211_sched_scan_results(phy->mt76->hw);
1054 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
1055 					      &phy->mt76->state)) {
1056 			struct cfg80211_scan_info info = {
1057 				.aborted = false,
1058 			};
1059 
1060 			ieee80211_scan_completed(phy->mt76->hw, &info);
1061 		}
1062 		dev_kfree_skb(skb);
1063 	}
1064 }
1065 
1066 static int
mt7615_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * req)1067 mt7615_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1068 	       struct ieee80211_scan_request *req)
1069 {
1070 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1071 	struct mt76_phy *mphy = hw->priv;
1072 	int err;
1073 
1074 	/* fall-back to sw-scan */
1075 	if (!mt7615_firmware_offload(dev))
1076 		return 1;
1077 
1078 	mt7615_mutex_acquire(dev);
1079 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1080 	mt7615_mutex_release(dev);
1081 
1082 	return err;
1083 }
1084 
1085 static void
mt7615_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1086 mt7615_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1087 {
1088 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1089 	struct mt76_phy *mphy = hw->priv;
1090 
1091 	mt7615_mutex_acquire(dev);
1092 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1093 	mt7615_mutex_release(dev);
1094 }
1095 
1096 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)1097 mt7615_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1098 			struct cfg80211_sched_scan_request *req,
1099 			struct ieee80211_scan_ies *ies)
1100 {
1101 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1102 	struct mt76_phy *mphy = hw->priv;
1103 	int err;
1104 
1105 	if (!mt7615_firmware_offload(dev))
1106 		return -EOPNOTSUPP;
1107 
1108 	mt7615_mutex_acquire(dev);
1109 
1110 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1111 	if (err < 0)
1112 		goto out;
1113 
1114 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1115 out:
1116 	mt7615_mutex_release(dev);
1117 
1118 	return err;
1119 }
1120 
1121 static int
mt7615_stop_sched_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1122 mt7615_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1123 {
1124 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1125 	struct mt76_phy *mphy = hw->priv;
1126 	int err;
1127 
1128 	if (!mt7615_firmware_offload(dev))
1129 		return -EOPNOTSUPP;
1130 
1131 	mt7615_mutex_acquire(dev);
1132 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1133 	mt7615_mutex_release(dev);
1134 
1135 	return err;
1136 }
1137 
mt7615_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel * chan,int duration,enum ieee80211_roc_type type)1138 static int mt7615_remain_on_channel(struct ieee80211_hw *hw,
1139 				    struct ieee80211_vif *vif,
1140 				    struct ieee80211_channel *chan,
1141 				    int duration,
1142 				    enum ieee80211_roc_type type)
1143 {
1144 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1145 	int err;
1146 
1147 	if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
1148 		return 0;
1149 
1150 	mt7615_mutex_acquire(phy->dev);
1151 
1152 	err = mt7615_mcu_set_roc(phy, vif, chan, duration);
1153 	if (err < 0) {
1154 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1155 		goto out;
1156 	}
1157 
1158 	if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) {
1159 		mt7615_mcu_set_roc(phy, vif, NULL, 0);
1160 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1161 		err = -ETIMEDOUT;
1162 	}
1163 
1164 out:
1165 	mt7615_mutex_release(phy->dev);
1166 
1167 	return err;
1168 }
1169 
mt7615_cancel_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1170 static int mt7615_cancel_remain_on_channel(struct ieee80211_hw *hw,
1171 					   struct ieee80211_vif *vif)
1172 {
1173 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1174 	int err;
1175 
1176 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1177 		return 0;
1178 
1179 	del_timer_sync(&phy->roc_timer);
1180 	cancel_work_sync(&phy->roc_work);
1181 
1182 	mt7615_mutex_acquire(phy->dev);
1183 	err = mt7615_mcu_set_roc(phy, vif, NULL, 0);
1184 	mt7615_mutex_release(phy->dev);
1185 
1186 	return err;
1187 }
1188 
mt7615_sta_set_decap_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1189 static void mt7615_sta_set_decap_offload(struct ieee80211_hw *hw,
1190 				 struct ieee80211_vif *vif,
1191 				 struct ieee80211_sta *sta,
1192 				 bool enabled)
1193 {
1194 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1195 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
1196 
1197 	if (enabled)
1198 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1199 	else
1200 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1201 
1202 	mt7615_mcu_set_sta_decap_offload(dev, vif, sta);
1203 }
1204 
1205 #ifdef CONFIG_PM
mt7615_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)1206 static int mt7615_suspend(struct ieee80211_hw *hw,
1207 			  struct cfg80211_wowlan *wowlan)
1208 {
1209 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1210 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1211 	int err = 0;
1212 
1213 	cancel_delayed_work_sync(&dev->pm.ps_work);
1214 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1215 
1216 	mt7615_mutex_acquire(dev);
1217 
1218 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1219 	cancel_delayed_work_sync(&phy->scan_work);
1220 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1221 
1222 	set_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1223 	ieee80211_iterate_active_interfaces(hw,
1224 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1225 					    mt76_connac_mcu_set_suspend_iter,
1226 					    phy->mt76);
1227 
1228 	if (!mt7615_dev_running(dev))
1229 		err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
1230 
1231 	mt7615_mutex_release(dev);
1232 
1233 	return err;
1234 }
1235 
mt7615_resume(struct ieee80211_hw * hw)1236 static int mt7615_resume(struct ieee80211_hw *hw)
1237 {
1238 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1239 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1240 	unsigned long timeout;
1241 	bool running;
1242 
1243 	mt7615_mutex_acquire(dev);
1244 
1245 	running = mt7615_dev_running(dev);
1246 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1247 
1248 	if (!running) {
1249 		int err;
1250 
1251 		err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
1252 		if (err < 0) {
1253 			mt7615_mutex_release(dev);
1254 			return err;
1255 		}
1256 	}
1257 
1258 	clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1259 	ieee80211_iterate_active_interfaces(hw,
1260 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1261 					    mt76_connac_mcu_set_suspend_iter,
1262 					    phy->mt76);
1263 
1264 	timeout = mt7615_get_macwork_timeout(dev);
1265 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, timeout);
1266 
1267 	mt7615_mutex_release(dev);
1268 
1269 	return 0;
1270 }
1271 
mt7615_set_wakeup(struct ieee80211_hw * hw,bool enabled)1272 static void mt7615_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1273 {
1274 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1275 	struct mt76_dev *mdev = &dev->mt76;
1276 
1277 	device_set_wakeup_enable(mdev->dev, enabled);
1278 }
1279 
mt7615_set_rekey_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_gtk_rekey_data * data)1280 static void mt7615_set_rekey_data(struct ieee80211_hw *hw,
1281 				  struct ieee80211_vif *vif,
1282 				  struct cfg80211_gtk_rekey_data *data)
1283 {
1284 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1285 
1286 	mt7615_mutex_acquire(dev);
1287 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1288 	mt7615_mutex_release(dev);
1289 }
1290 #endif /* CONFIG_PM */
1291 
1292 const struct ieee80211_ops mt7615_ops = {
1293 	.tx = mt7615_tx,
1294 	.start = mt7615_start,
1295 	.stop = mt7615_stop,
1296 	.add_interface = mt7615_add_interface,
1297 	.remove_interface = mt7615_remove_interface,
1298 	.config = mt7615_config,
1299 	.conf_tx = mt7615_conf_tx,
1300 	.configure_filter = mt7615_configure_filter,
1301 	.bss_info_changed = mt7615_bss_info_changed,
1302 	.sta_add = mt7615_sta_add,
1303 	.sta_remove = mt7615_sta_remove,
1304 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1305 	.set_key = mt7615_set_key,
1306 	.sta_set_decap_offload = mt7615_sta_set_decap_offload,
1307 	.ampdu_action = mt7615_ampdu_action,
1308 	.set_rts_threshold = mt7615_set_rts_threshold,
1309 	.wake_tx_queue = mt76_wake_tx_queue,
1310 	.sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
1311 	.sw_scan_start = mt76_sw_scan,
1312 	.sw_scan_complete = mt76_sw_scan_complete,
1313 	.release_buffered_frames = mt76_release_buffered_frames,
1314 	.get_txpower = mt76_get_txpower,
1315 	.channel_switch_beacon = mt7615_channel_switch_beacon,
1316 	.get_stats = mt7615_get_stats,
1317 	.get_tsf = mt7615_get_tsf,
1318 	.set_tsf = mt7615_set_tsf,
1319 	.offset_tsf = mt7615_offset_tsf,
1320 	.get_survey = mt76_get_survey,
1321 	.get_antenna = mt76_get_antenna,
1322 	.set_antenna = mt7615_set_antenna,
1323 	.set_coverage_class = mt7615_set_coverage_class,
1324 	.hw_scan = mt7615_hw_scan,
1325 	.cancel_hw_scan = mt7615_cancel_hw_scan,
1326 	.sched_scan_start = mt7615_start_sched_scan,
1327 	.sched_scan_stop = mt7615_stop_sched_scan,
1328 	.remain_on_channel = mt7615_remain_on_channel,
1329 	.cancel_remain_on_channel = mt7615_cancel_remain_on_channel,
1330 	CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1331 	CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1332 #ifdef CONFIG_PM
1333 	.suspend = mt7615_suspend,
1334 	.resume = mt7615_resume,
1335 	.set_wakeup = mt7615_set_wakeup,
1336 	.set_rekey_data = mt7615_set_rekey_data,
1337 #endif /* CONFIG_PM */
1338 	.set_sar_specs = mt7615_set_sar_specs,
1339 };
1340 EXPORT_SYMBOL_GPL(mt7615_ops);
1341 
1342 MODULE_LICENSE("Dual BSD/GPL");
1343