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