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.wcid.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->mt76.sta_poll_lock);
278 	if (!list_empty(&msta->wcid.poll_list))
279 		list_del_init(&msta->wcid.poll_list);
280 	spin_unlock_bh(&dev->mt76.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 {
395 		if (idx == *wcid_keyidx)
396 			*wcid_keyidx = -1;
397 		goto out;
398 	}
399 
400 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
401 	if (mt76_is_mmio(&dev->mt76))
402 		err = mt7615_mac_wtbl_set_key(dev, wcid, key);
403 	else
404 		err = __mt7615_mac_wtbl_set_key(dev, wcid, key);
405 
406 out:
407 	mt7615_mutex_release(dev);
408 
409 	return err;
410 }
411 
mt7615_set_sar_specs(struct ieee80211_hw * hw,const struct cfg80211_sar_specs * sar)412 static int mt7615_set_sar_specs(struct ieee80211_hw *hw,
413 				const struct cfg80211_sar_specs *sar)
414 {
415 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
416 	int err;
417 
418 	if (!cfg80211_chandef_valid(&phy->mt76->chandef))
419 		return -EINVAL;
420 
421 	err = mt76_init_sar_power(hw, sar);
422 	if (err)
423 		return err;
424 
425 	if (mt7615_firmware_offload(phy->dev))
426 		return mt76_connac_mcu_set_rate_txpower(phy->mt76);
427 
428 	ieee80211_stop_queues(hw);
429 	err = mt7615_set_channel(phy);
430 	ieee80211_wake_queues(hw);
431 
432 	return err;
433 }
434 
mt7615_config(struct ieee80211_hw * hw,u32 changed)435 static int mt7615_config(struct ieee80211_hw *hw, u32 changed)
436 {
437 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
438 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
439 	bool band = phy != &dev->phy;
440 	int ret = 0;
441 
442 	if (changed & (IEEE80211_CONF_CHANGE_CHANNEL |
443 		       IEEE80211_CONF_CHANGE_POWER)) {
444 #ifdef CONFIG_NL80211_TESTMODE
445 		if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
446 			mt7615_mutex_acquire(dev);
447 			mt76_testmode_reset(phy->mt76, false);
448 			mt7615_mutex_release(dev);
449 		}
450 #endif
451 		ieee80211_stop_queues(hw);
452 		ret = mt7615_set_channel(phy);
453 		ieee80211_wake_queues(hw);
454 	}
455 
456 	mt7615_mutex_acquire(dev);
457 
458 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
459 		mt76_testmode_reset(phy->mt76, true);
460 
461 		if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
462 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
463 		else
464 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
465 
466 		mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
467 	}
468 
469 	mt7615_mutex_release(dev);
470 
471 	return ret;
472 }
473 
474 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)475 mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
476 	       unsigned int link_id, 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 
555 static void
mt7615_update_mu_group(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info)556 mt7615_update_mu_group(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
557 		       struct ieee80211_bss_conf *info)
558 {
559 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
560 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
561 	u8 i, band = mvif->mt76.band_idx;
562 	u32 *mu;
563 
564 	mu = (u32 *)info->mu_group.membership;
565 	for (i = 0; i < WLAN_MEMBERSHIP_LEN / sizeof(*mu); i++) {
566 		if (is_mt7663(&dev->mt76))
567 			mt76_wr(dev, MT7663_WF_PHY_GID_TAB_VLD(band, i), mu[i]);
568 		else
569 			mt76_wr(dev, MT_WF_PHY_GID_TAB_VLD(band, i), mu[i]);
570 	}
571 
572 	mu = (u32 *)info->mu_group.position;
573 	for (i = 0; i < WLAN_USER_POSITION_LEN / sizeof(*mu); i++) {
574 		if (is_mt7663(&dev->mt76))
575 			mt76_wr(dev, MT7663_WF_PHY_GID_TAB_POS(band, i), mu[i]);
576 		else
577 			mt76_wr(dev, MT_WF_PHY_GID_TAB_POS(band, i), mu[i]);
578 	}
579 }
580 
mt7615_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)581 static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
582 				    struct ieee80211_vif *vif,
583 				    struct ieee80211_bss_conf *info,
584 				    u64 changed)
585 {
586 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
587 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
588 
589 	mt7615_mutex_acquire(dev);
590 
591 	if (changed & BSS_CHANGED_ERP_SLOT) {
592 		int slottime = info->use_short_slot ? 9 : 20;
593 
594 		if (slottime != phy->slottime) {
595 			phy->slottime = slottime;
596 			mt7615_mac_set_timing(phy);
597 		}
598 	}
599 
600 	if (changed & BSS_CHANGED_ERP_CTS_PROT)
601 		mt7615_mac_enable_rtscts(dev, vif, info->use_cts_prot);
602 
603 	if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
604 		mt7615_mcu_add_bss_info(phy, vif, NULL, true);
605 		mt7615_mcu_sta_add(phy, vif, NULL, true);
606 
607 		if (mt7615_firmware_offload(dev) && vif->p2p)
608 			mt76_connac_mcu_set_p2p_oppps(hw, vif);
609 	}
610 
611 	if (changed & (BSS_CHANGED_BEACON |
612 		       BSS_CHANGED_BEACON_ENABLED))
613 		mt7615_mcu_add_beacon(dev, hw, vif, info->enable_beacon);
614 
615 	if (changed & BSS_CHANGED_PS)
616 		mt76_connac_mcu_set_vif_ps(&dev->mt76, vif);
617 
618 	if ((changed & BSS_CHANGED_ARP_FILTER) &&
619 	    mt7615_firmware_offload(dev)) {
620 		struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
621 
622 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
623 						  info);
624 	}
625 
626 	if (changed & BSS_CHANGED_ASSOC)
627 		mt7615_mac_set_beacon_filter(phy, vif, vif->cfg.assoc);
628 
629 	if (changed & BSS_CHANGED_MU_GROUPS)
630 		 mt7615_update_mu_group(hw, vif, info);
631 
632 	mt7615_mutex_release(dev);
633 }
634 
635 static void
mt7615_channel_switch_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_chan_def * chandef)636 mt7615_channel_switch_beacon(struct ieee80211_hw *hw,
637 			     struct ieee80211_vif *vif,
638 			     struct cfg80211_chan_def *chandef)
639 {
640 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
641 
642 	mt7615_mutex_acquire(dev);
643 	mt7615_mcu_add_beacon(dev, hw, vif, true);
644 	mt7615_mutex_release(dev);
645 }
646 
mt7615_mac_sta_add(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)647 int mt7615_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
648 		       struct ieee80211_sta *sta)
649 {
650 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
651 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
652 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
653 	struct mt7615_phy *phy;
654 	int idx, err;
655 
656 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
657 	if (idx < 0)
658 		return -ENOSPC;
659 
660 	INIT_LIST_HEAD(&msta->wcid.poll_list);
661 	msta->vif = mvif;
662 	msta->wcid.sta = 1;
663 	msta->wcid.idx = idx;
664 	msta->wcid.phy_idx = mvif->mt76.band_idx;
665 
666 	phy = mvif->mt76.band_idx ? mt7615_ext_phy(dev) : &dev->phy;
667 	err = mt76_connac_pm_wake(phy->mt76, &dev->pm);
668 	if (err)
669 		return err;
670 
671 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
672 		err = mt7615_mcu_add_bss_info(phy, vif, sta, true);
673 		if (err)
674 			return err;
675 	}
676 
677 	mt7615_mac_wtbl_update(dev, idx,
678 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
679 	err = mt7615_mcu_sta_add(&dev->phy, vif, sta, true);
680 	if (err)
681 		return err;
682 
683 	mt76_connac_power_save_sched(phy->mt76, &dev->pm);
684 
685 	return err;
686 }
687 EXPORT_SYMBOL_GPL(mt7615_mac_sta_add);
688 
mt7615_mac_sta_remove(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)689 void mt7615_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
690 			   struct ieee80211_sta *sta)
691 {
692 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
693 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
694 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
695 	struct mt7615_phy *phy;
696 
697 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
698 
699 	phy = mvif->mt76.band_idx ? mt7615_ext_phy(dev) : &dev->phy;
700 	mt76_connac_pm_wake(phy->mt76, &dev->pm);
701 
702 	mt7615_mcu_sta_add(&dev->phy, vif, sta, false);
703 	mt7615_mac_wtbl_update(dev, msta->wcid.idx,
704 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
705 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
706 		mt7615_mcu_add_bss_info(phy, vif, sta, false);
707 
708 	spin_lock_bh(&mdev->sta_poll_lock);
709 	if (!list_empty(&msta->wcid.poll_list))
710 		list_del_init(&msta->wcid.poll_list);
711 	spin_unlock_bh(&mdev->sta_poll_lock);
712 
713 	mt76_connac_power_save_sched(phy->mt76, &dev->pm);
714 }
715 EXPORT_SYMBOL_GPL(mt7615_mac_sta_remove);
716 
mt7615_sta_rate_tbl_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)717 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
718 				       struct ieee80211_vif *vif,
719 				       struct ieee80211_sta *sta)
720 {
721 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
722 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
723 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
724 	struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
725 	int i;
726 
727 	if (!sta_rates)
728 		return;
729 
730 	spin_lock_bh(&dev->mt76.lock);
731 	for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
732 		msta->rates[i].idx = sta_rates->rate[i].idx;
733 		msta->rates[i].count = sta_rates->rate[i].count;
734 		msta->rates[i].flags = sta_rates->rate[i].flags;
735 
736 		if (msta->rates[i].idx < 0 || !msta->rates[i].count)
737 			break;
738 	}
739 	msta->n_rates = i;
740 	if (mt76_connac_pm_ref(phy->mt76, &dev->pm)) {
741 		mt7615_mac_set_rates(phy, msta, NULL, msta->rates);
742 		mt76_connac_pm_unref(phy->mt76, &dev->pm);
743 	}
744 	spin_unlock_bh(&dev->mt76.lock);
745 }
746 
mt7615_tx_worker(struct mt76_worker * w)747 void mt7615_tx_worker(struct mt76_worker *w)
748 {
749 	struct mt7615_dev *dev = container_of(w, struct mt7615_dev,
750 					      mt76.tx_worker);
751 
752 	if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
753 		queue_work(dev->mt76.wq, &dev->pm.wake_work);
754 		return;
755 	}
756 
757 	mt76_tx_worker_run(&dev->mt76);
758 	mt76_connac_pm_unref(&dev->mphy, &dev->pm);
759 }
760 
mt7615_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)761 static void mt7615_tx(struct ieee80211_hw *hw,
762 		      struct ieee80211_tx_control *control,
763 		      struct sk_buff *skb)
764 {
765 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
766 	struct mt76_phy *mphy = hw->priv;
767 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
768 	struct ieee80211_vif *vif = info->control.vif;
769 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
770 	struct mt7615_sta *msta = NULL;
771 	int qid;
772 
773 	if (control->sta) {
774 		msta = (struct mt7615_sta *)control->sta->drv_priv;
775 		wcid = &msta->wcid;
776 	}
777 
778 	if (vif && !control->sta) {
779 		struct mt7615_vif *mvif;
780 
781 		mvif = (struct mt7615_vif *)vif->drv_priv;
782 		msta = &mvif->sta;
783 		wcid = &msta->wcid;
784 	}
785 
786 	if (mt76_connac_pm_ref(mphy, &dev->pm)) {
787 		mt76_tx(mphy, control->sta, wcid, skb);
788 		mt76_connac_pm_unref(mphy, &dev->pm);
789 		return;
790 	}
791 
792 	qid = skb_get_queue_mapping(skb);
793 	if (qid >= MT_TXQ_PSD) {
794 		qid = IEEE80211_AC_BE;
795 		skb_set_queue_mapping(skb, qid);
796 	}
797 
798 	mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
799 }
800 
mt7615_set_rts_threshold(struct ieee80211_hw * hw,u32 val)801 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
802 {
803 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
804 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
805 	int err, band = phy != &dev->phy;
806 
807 	mt7615_mutex_acquire(dev);
808 	err = mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, band);
809 	mt7615_mutex_release(dev);
810 
811 	return err;
812 }
813 
814 static int
mt7615_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)815 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
816 		    struct ieee80211_ampdu_params *params)
817 {
818 	enum ieee80211_ampdu_mlme_action action = params->action;
819 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
820 	struct ieee80211_sta *sta = params->sta;
821 	struct ieee80211_txq *txq = sta->txq[params->tid];
822 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
823 	u16 tid = params->tid;
824 	u16 ssn = params->ssn;
825 	struct mt76_txq *mtxq;
826 	int ret = 0;
827 
828 	if (!txq)
829 		return -EINVAL;
830 
831 	mtxq = (struct mt76_txq *)txq->drv_priv;
832 
833 	mt7615_mutex_acquire(dev);
834 
835 	switch (action) {
836 	case IEEE80211_AMPDU_RX_START:
837 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
838 				   params->buf_size);
839 		ret = mt7615_mcu_add_rx_ba(dev, params, true);
840 		break;
841 	case IEEE80211_AMPDU_RX_STOP:
842 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
843 		ret = mt7615_mcu_add_rx_ba(dev, params, false);
844 		break;
845 	case IEEE80211_AMPDU_TX_OPERATIONAL:
846 		mtxq->aggr = true;
847 		mtxq->send_bar = false;
848 		ret = mt7615_mcu_add_tx_ba(dev, params, true);
849 		ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid);
850 		ieee80211_send_bar(vif, sta->addr, tid,
851 				   IEEE80211_SN_TO_SEQ(ssn));
852 		break;
853 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
854 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
855 		mtxq->aggr = false;
856 		ret = mt7615_mcu_add_tx_ba(dev, params, false);
857 		break;
858 	case IEEE80211_AMPDU_TX_START:
859 		ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid);
860 		params->ssn = ssn;
861 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
862 		break;
863 	case IEEE80211_AMPDU_TX_STOP_CONT:
864 		mtxq->aggr = false;
865 		ret = mt7615_mcu_add_tx_ba(dev, params, false);
866 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
867 		break;
868 	}
869 	mt7615_mutex_release(dev);
870 
871 	return ret;
872 }
873 
874 static int
mt7615_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)875 mt7615_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
876 	       struct ieee80211_sta *sta)
877 {
878     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
879 			  IEEE80211_STA_NONE);
880 }
881 
882 static int
mt7615_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)883 mt7615_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
884 		  struct ieee80211_sta *sta)
885 {
886     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
887 			  IEEE80211_STA_NOTEXIST);
888 }
889 
890 static int
mt7615_get_stats(struct ieee80211_hw * hw,struct ieee80211_low_level_stats * stats)891 mt7615_get_stats(struct ieee80211_hw *hw,
892 		 struct ieee80211_low_level_stats *stats)
893 {
894 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
895 	struct mib_stats *mib = &phy->mib;
896 
897 	mt7615_mutex_acquire(phy->dev);
898 
899 	stats->dot11RTSSuccessCount = mib->rts_cnt;
900 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
901 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
902 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
903 
904 	mt7615_mutex_release(phy->dev);
905 
906 	return 0;
907 }
908 
909 static u64
mt7615_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)910 mt7615_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
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;
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 	/* TSF read */
927 	mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_READ);
928 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0);
929 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1);
930 
931 	mt7615_mutex_release(dev);
932 
933 	return tsf.t64;
934 }
935 
936 static void
mt7615_set_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 timestamp)937 mt7615_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
938 	       u64 timestamp)
939 {
940 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
941 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
942 	union {
943 		u64 t64;
944 		u32 t32[2];
945 	} tsf = { .t64 = timestamp, };
946 	u16 idx = mvif->mt76.omac_idx;
947 	u32 reg;
948 
949 	idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
950 	reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
951 
952 	mt7615_mutex_acquire(dev);
953 
954 	mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
955 	mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
956 	/* TSF software overwrite */
957 	mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_WRITE);
958 
959 	mt7615_mutex_release(dev);
960 }
961 
962 static void
mt7615_offset_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,s64 timestamp)963 mt7615_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
964 		  s64 timestamp)
965 {
966 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
967 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
968 	union {
969 		u64 t64;
970 		u32 t32[2];
971 	} tsf = { .t64 = timestamp, };
972 	u16 idx = mvif->mt76.omac_idx;
973 	u32 reg;
974 
975 	idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
976 	reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
977 
978 	mt7615_mutex_acquire(dev);
979 
980 	mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
981 	mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
982 	/* TSF software adjust*/
983 	mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_ADJUST);
984 
985 	mt7615_mutex_release(dev);
986 }
987 
988 static void
mt7615_set_coverage_class(struct ieee80211_hw * hw,s16 coverage_class)989 mt7615_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
990 {
991 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
992 	struct mt7615_dev *dev = phy->dev;
993 
994 	mt7615_mutex_acquire(dev);
995 	phy->coverage_class = max_t(s16, coverage_class, 0);
996 	mt7615_mac_set_timing(phy);
997 	mt7615_mutex_release(dev);
998 }
999 
1000 static int
mt7615_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)1001 mt7615_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1002 {
1003 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1004 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1005 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1006 	bool ext_phy = phy != &dev->phy;
1007 
1008 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1009 		return -EINVAL;
1010 
1011 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1012 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1013 
1014 	mt7615_mutex_acquire(dev);
1015 
1016 	phy->mt76->antenna_mask = tx_ant;
1017 	if (ext_phy) {
1018 		if (dev->chainmask == 0xf)
1019 			tx_ant <<= 2;
1020 		else
1021 			tx_ant <<= 1;
1022 	}
1023 	phy->mt76->chainmask = tx_ant;
1024 
1025 	mt76_set_stream_caps(phy->mt76, true);
1026 
1027 	mt7615_mutex_release(dev);
1028 
1029 	return 0;
1030 }
1031 
mt7615_roc_iter(void * priv,u8 * mac,struct ieee80211_vif * vif)1032 static void mt7615_roc_iter(void *priv, u8 *mac,
1033 			    struct ieee80211_vif *vif)
1034 {
1035 	struct mt7615_phy *phy = priv;
1036 
1037 	mt7615_mcu_set_roc(phy, vif, NULL, 0);
1038 }
1039 
mt7615_roc_work(struct work_struct * work)1040 void mt7615_roc_work(struct work_struct *work)
1041 {
1042 	struct mt7615_phy *phy;
1043 
1044 	phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1045 						roc_work);
1046 
1047 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1048 		return;
1049 
1050 	mt7615_mutex_acquire(phy->dev);
1051 	ieee80211_iterate_active_interfaces(phy->mt76->hw,
1052 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1053 					    mt7615_roc_iter, phy);
1054 	mt7615_mutex_release(phy->dev);
1055 	ieee80211_remain_on_channel_expired(phy->mt76->hw);
1056 }
1057 
mt7615_roc_timer(struct timer_list * timer)1058 void mt7615_roc_timer(struct timer_list *timer)
1059 {
1060 	struct mt7615_phy *phy = from_timer(phy, timer, roc_timer);
1061 
1062 	ieee80211_queue_work(phy->mt76->hw, &phy->roc_work);
1063 }
1064 
mt7615_scan_work(struct work_struct * work)1065 void mt7615_scan_work(struct work_struct *work)
1066 {
1067 	struct mt7615_phy *phy;
1068 
1069 	phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1070 						scan_work.work);
1071 
1072 	while (true) {
1073 		struct mt7615_mcu_rxd *rxd;
1074 		struct sk_buff *skb;
1075 
1076 		spin_lock_bh(&phy->dev->mt76.lock);
1077 		skb = __skb_dequeue(&phy->scan_event_list);
1078 		spin_unlock_bh(&phy->dev->mt76.lock);
1079 
1080 		if (!skb)
1081 			break;
1082 
1083 		rxd = (struct mt7615_mcu_rxd *)skb->data;
1084 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1085 			ieee80211_sched_scan_results(phy->mt76->hw);
1086 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
1087 					      &phy->mt76->state)) {
1088 			struct cfg80211_scan_info info = {
1089 				.aborted = false,
1090 			};
1091 
1092 			ieee80211_scan_completed(phy->mt76->hw, &info);
1093 		}
1094 		dev_kfree_skb(skb);
1095 	}
1096 }
1097 
1098 static int
mt7615_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * req)1099 mt7615_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1100 	       struct ieee80211_scan_request *req)
1101 {
1102 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1103 	struct mt76_phy *mphy = hw->priv;
1104 	int err;
1105 
1106 	/* fall-back to sw-scan */
1107 	if (!mt7615_firmware_offload(dev))
1108 		return 1;
1109 
1110 	mt7615_mutex_acquire(dev);
1111 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1112 	mt7615_mutex_release(dev);
1113 
1114 	return err;
1115 }
1116 
1117 static void
mt7615_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1118 mt7615_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1119 {
1120 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1121 	struct mt76_phy *mphy = hw->priv;
1122 
1123 	mt7615_mutex_acquire(dev);
1124 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1125 	mt7615_mutex_release(dev);
1126 }
1127 
1128 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)1129 mt7615_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1130 			struct cfg80211_sched_scan_request *req,
1131 			struct ieee80211_scan_ies *ies)
1132 {
1133 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1134 	struct mt76_phy *mphy = hw->priv;
1135 	int err;
1136 
1137 	if (!mt7615_firmware_offload(dev))
1138 		return -EOPNOTSUPP;
1139 
1140 	mt7615_mutex_acquire(dev);
1141 
1142 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1143 	if (err < 0)
1144 		goto out;
1145 
1146 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1147 out:
1148 	mt7615_mutex_release(dev);
1149 
1150 	return err;
1151 }
1152 
1153 static int
mt7615_stop_sched_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1154 mt7615_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1155 {
1156 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1157 	struct mt76_phy *mphy = hw->priv;
1158 	int err;
1159 
1160 	if (!mt7615_firmware_offload(dev))
1161 		return -EOPNOTSUPP;
1162 
1163 	mt7615_mutex_acquire(dev);
1164 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1165 	mt7615_mutex_release(dev);
1166 
1167 	return err;
1168 }
1169 
mt7615_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel * chan,int duration,enum ieee80211_roc_type type)1170 static int mt7615_remain_on_channel(struct ieee80211_hw *hw,
1171 				    struct ieee80211_vif *vif,
1172 				    struct ieee80211_channel *chan,
1173 				    int duration,
1174 				    enum ieee80211_roc_type type)
1175 {
1176 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1177 	int err;
1178 
1179 	if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
1180 		return 0;
1181 
1182 	mt7615_mutex_acquire(phy->dev);
1183 
1184 	err = mt7615_mcu_set_roc(phy, vif, chan, duration);
1185 	if (err < 0) {
1186 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1187 		goto out;
1188 	}
1189 
1190 	if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) {
1191 		mt7615_mcu_set_roc(phy, vif, NULL, 0);
1192 		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1193 		err = -ETIMEDOUT;
1194 	}
1195 
1196 out:
1197 	mt7615_mutex_release(phy->dev);
1198 
1199 	return err;
1200 }
1201 
mt7615_cancel_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1202 static int mt7615_cancel_remain_on_channel(struct ieee80211_hw *hw,
1203 					   struct ieee80211_vif *vif)
1204 {
1205 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1206 	int err;
1207 
1208 	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1209 		return 0;
1210 
1211 	del_timer_sync(&phy->roc_timer);
1212 	cancel_work_sync(&phy->roc_work);
1213 
1214 	mt7615_mutex_acquire(phy->dev);
1215 	err = mt7615_mcu_set_roc(phy, vif, NULL, 0);
1216 	mt7615_mutex_release(phy->dev);
1217 
1218 	return err;
1219 }
1220 
mt7615_sta_set_decap_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1221 static void mt7615_sta_set_decap_offload(struct ieee80211_hw *hw,
1222 				 struct ieee80211_vif *vif,
1223 				 struct ieee80211_sta *sta,
1224 				 bool enabled)
1225 {
1226 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1227 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
1228 
1229 	mt7615_mutex_acquire(dev);
1230 
1231 	if (enabled)
1232 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1233 	else
1234 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1235 
1236 	mt7615_mcu_set_sta_decap_offload(dev, vif, sta);
1237 
1238 	mt7615_mutex_release(dev);
1239 }
1240 
1241 #ifdef CONFIG_PM
mt7615_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)1242 static int mt7615_suspend(struct ieee80211_hw *hw,
1243 			  struct cfg80211_wowlan *wowlan)
1244 {
1245 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1246 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1247 	int err = 0;
1248 
1249 	cancel_delayed_work_sync(&dev->pm.ps_work);
1250 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1251 
1252 	mt7615_mutex_acquire(dev);
1253 
1254 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1255 	cancel_delayed_work_sync(&phy->scan_work);
1256 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1257 
1258 	set_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 	if (!mt7615_dev_running(dev))
1265 		err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
1266 
1267 	mt7615_mutex_release(dev);
1268 
1269 	return err;
1270 }
1271 
mt7615_resume(struct ieee80211_hw * hw)1272 static int mt7615_resume(struct ieee80211_hw *hw)
1273 {
1274 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
1275 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1276 	unsigned long timeout;
1277 	bool running;
1278 
1279 	mt7615_mutex_acquire(dev);
1280 
1281 	running = mt7615_dev_running(dev);
1282 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1283 
1284 	if (!running) {
1285 		int err;
1286 
1287 		err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
1288 		if (err < 0) {
1289 			mt7615_mutex_release(dev);
1290 			return err;
1291 		}
1292 	}
1293 
1294 	clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1295 	ieee80211_iterate_active_interfaces(hw,
1296 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1297 					    mt76_connac_mcu_set_suspend_iter,
1298 					    phy->mt76);
1299 
1300 	timeout = mt7615_get_macwork_timeout(dev);
1301 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, timeout);
1302 
1303 	mt7615_mutex_release(dev);
1304 
1305 	return 0;
1306 }
1307 
mt7615_set_wakeup(struct ieee80211_hw * hw,bool enabled)1308 static void mt7615_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1309 {
1310 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1311 	struct mt76_dev *mdev = &dev->mt76;
1312 
1313 	device_set_wakeup_enable(mdev->dev, enabled);
1314 }
1315 
mt7615_set_rekey_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_gtk_rekey_data * data)1316 static void mt7615_set_rekey_data(struct ieee80211_hw *hw,
1317 				  struct ieee80211_vif *vif,
1318 				  struct cfg80211_gtk_rekey_data *data)
1319 {
1320 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
1321 
1322 	mt7615_mutex_acquire(dev);
1323 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1324 	mt7615_mutex_release(dev);
1325 }
1326 #endif /* CONFIG_PM */
1327 
1328 const struct ieee80211_ops mt7615_ops = {
1329 	.tx = mt7615_tx,
1330 	.start = mt7615_start,
1331 	.stop = mt7615_stop,
1332 	.add_interface = mt7615_add_interface,
1333 	.remove_interface = mt7615_remove_interface,
1334 	.config = mt7615_config,
1335 	.conf_tx = mt7615_conf_tx,
1336 	.configure_filter = mt7615_configure_filter,
1337 	.bss_info_changed = mt7615_bss_info_changed,
1338 	.sta_add = mt7615_sta_add,
1339 	.sta_remove = mt7615_sta_remove,
1340 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1341 	.set_key = mt7615_set_key,
1342 	.sta_set_decap_offload = mt7615_sta_set_decap_offload,
1343 	.ampdu_action = mt7615_ampdu_action,
1344 	.set_rts_threshold = mt7615_set_rts_threshold,
1345 	.wake_tx_queue = mt76_wake_tx_queue,
1346 	.sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
1347 	.sw_scan_start = mt76_sw_scan,
1348 	.sw_scan_complete = mt76_sw_scan_complete,
1349 	.release_buffered_frames = mt76_release_buffered_frames,
1350 	.get_txpower = mt76_get_txpower,
1351 	.channel_switch_beacon = mt7615_channel_switch_beacon,
1352 	.get_stats = mt7615_get_stats,
1353 	.get_tsf = mt7615_get_tsf,
1354 	.set_tsf = mt7615_set_tsf,
1355 	.offset_tsf = mt7615_offset_tsf,
1356 	.get_survey = mt76_get_survey,
1357 	.get_antenna = mt76_get_antenna,
1358 	.set_antenna = mt7615_set_antenna,
1359 	.set_coverage_class = mt7615_set_coverage_class,
1360 	.hw_scan = mt7615_hw_scan,
1361 	.cancel_hw_scan = mt7615_cancel_hw_scan,
1362 	.sched_scan_start = mt7615_start_sched_scan,
1363 	.sched_scan_stop = mt7615_stop_sched_scan,
1364 	.remain_on_channel = mt7615_remain_on_channel,
1365 	.cancel_remain_on_channel = mt7615_cancel_remain_on_channel,
1366 	CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1367 	CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1368 #ifdef CONFIG_PM
1369 	.suspend = mt7615_suspend,
1370 	.resume = mt7615_resume,
1371 	.set_wakeup = mt7615_set_wakeup,
1372 	.set_rekey_data = mt7615_set_rekey_data,
1373 #endif /* CONFIG_PM */
1374 	.set_sar_specs = mt7615_set_sar_specs,
1375 };
1376 EXPORT_SYMBOL_GPL(mt7615_ops);
1377 
1378 MODULE_LICENSE("Dual BSD/GPL");
1379