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