1 // SPDX-License-Identifier: ISC
2 
3 #include <linux/etherdevice.h>
4 #include <linux/platform_device.h>
5 #include <linux/pci.h>
6 #include <linux/module.h>
7 #include "mt7603.h"
8 #include "mac.h"
9 #include "eeprom.h"
10 
11 static int
12 mt7603_start(struct ieee80211_hw *hw)
13 {
14 	struct mt7603_dev *dev = hw->priv;
15 
16 	mt7603_mac_reset_counters(dev);
17 	mt7603_mac_start(dev);
18 	dev->mphy.survey_time = ktime_get_boottime();
19 	set_bit(MT76_STATE_RUNNING, &dev->mphy.state);
20 	mt7603_mac_work(&dev->mt76.mac_work.work);
21 
22 	return 0;
23 }
24 
25 static void
26 mt7603_stop(struct ieee80211_hw *hw)
27 {
28 	struct mt7603_dev *dev = hw->priv;
29 
30 	clear_bit(MT76_STATE_RUNNING, &dev->mphy.state);
31 	cancel_delayed_work_sync(&dev->mt76.mac_work);
32 	mt7603_mac_stop(dev);
33 }
34 
35 static int
36 mt7603_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
37 {
38 	struct mt7603_vif *mvif = (struct mt7603_vif *)vif->drv_priv;
39 	struct mt7603_dev *dev = hw->priv;
40 	struct mt76_txq *mtxq;
41 	u8 bc_addr[ETH_ALEN];
42 	int idx;
43 	int ret = 0;
44 
45 	mutex_lock(&dev->mt76.mutex);
46 
47 	mvif->idx = ffs(~dev->mphy.vif_mask) - 1;
48 	if (mvif->idx >= MT7603_MAX_INTERFACES) {
49 		ret = -ENOSPC;
50 		goto out;
51 	}
52 
53 	mt76_wr(dev, MT_MAC_ADDR0(mvif->idx),
54 		get_unaligned_le32(vif->addr));
55 	mt76_wr(dev, MT_MAC_ADDR1(mvif->idx),
56 		(get_unaligned_le16(vif->addr + 4) |
57 		 MT_MAC_ADDR1_VALID));
58 
59 	if (vif->type == NL80211_IFTYPE_AP) {
60 		mt76_wr(dev, MT_BSSID0(mvif->idx),
61 			get_unaligned_le32(vif->addr));
62 		mt76_wr(dev, MT_BSSID1(mvif->idx),
63 			(get_unaligned_le16(vif->addr + 4) |
64 			 MT_BSSID1_VALID));
65 	}
66 
67 	idx = MT7603_WTBL_RESERVED - 1 - mvif->idx;
68 	dev->mphy.vif_mask |= BIT(mvif->idx);
69 	INIT_LIST_HEAD(&mvif->sta.poll_list);
70 	mvif->sta.wcid.idx = idx;
71 	mvif->sta.wcid.hw_key_idx = -1;
72 
73 	eth_broadcast_addr(bc_addr);
74 	mt7603_wtbl_init(dev, idx, mvif->idx, bc_addr);
75 
76 	mtxq = (struct mt76_txq *)vif->txq->drv_priv;
77 	mtxq->wcid = &mvif->sta.wcid;
78 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
79 
80 out:
81 	mutex_unlock(&dev->mt76.mutex);
82 
83 	return ret;
84 }
85 
86 static void
87 mt7603_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
88 {
89 	struct mt7603_vif *mvif = (struct mt7603_vif *)vif->drv_priv;
90 	struct mt7603_sta *msta = &mvif->sta;
91 	struct mt7603_dev *dev = hw->priv;
92 	int idx = msta->wcid.idx;
93 
94 	mt76_wr(dev, MT_MAC_ADDR0(mvif->idx), 0);
95 	mt76_wr(dev, MT_MAC_ADDR1(mvif->idx), 0);
96 	mt76_wr(dev, MT_BSSID0(mvif->idx), 0);
97 	mt76_wr(dev, MT_BSSID1(mvif->idx), 0);
98 	mt7603_beacon_set_timer(dev, mvif->idx, 0);
99 
100 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
101 
102 	spin_lock_bh(&dev->sta_poll_lock);
103 	if (!list_empty(&msta->poll_list))
104 		list_del_init(&msta->poll_list);
105 	spin_unlock_bh(&dev->sta_poll_lock);
106 
107 	mutex_lock(&dev->mt76.mutex);
108 	dev->mphy.vif_mask &= ~BIT(mvif->idx);
109 	mutex_unlock(&dev->mt76.mutex);
110 }
111 
112 void mt7603_init_edcca(struct mt7603_dev *dev)
113 {
114 	/* Set lower signal level to -65dBm */
115 	mt76_rmw_field(dev, MT_RXTD(8), MT_RXTD_8_LOWER_SIGNAL, 0x23);
116 
117 	/* clear previous energy detect monitor results */
118 	mt76_rr(dev, MT_MIB_STAT_ED);
119 
120 	if (dev->ed_monitor)
121 		mt76_set(dev, MT_MIB_CTL, MT_MIB_CTL_ED_TIME);
122 	else
123 		mt76_clear(dev, MT_MIB_CTL, MT_MIB_CTL_ED_TIME);
124 
125 	dev->ed_strict_mode = 0xff;
126 	dev->ed_strong_signal = 0;
127 	dev->ed_time = ktime_get_boottime();
128 
129 	mt7603_edcca_set_strict(dev, false);
130 }
131 
132 static int
133 mt7603_set_channel(struct mt7603_dev *dev, struct cfg80211_chan_def *def)
134 {
135 	u8 *rssi_data = (u8 *)dev->mt76.eeprom.data;
136 	int idx, ret;
137 	u8 bw = MT_BW_20;
138 	bool failed = false;
139 
140 	cancel_delayed_work_sync(&dev->mt76.mac_work);
141 	tasklet_disable(&dev->mt76.pre_tbtt_tasklet);
142 
143 	mutex_lock(&dev->mt76.mutex);
144 	set_bit(MT76_RESET, &dev->mphy.state);
145 
146 	mt7603_beacon_set_timer(dev, -1, 0);
147 	mt76_set_channel(&dev->mphy);
148 	mt7603_mac_stop(dev);
149 
150 	if (def->width == NL80211_CHAN_WIDTH_40)
151 		bw = MT_BW_40;
152 
153 	dev->mphy.chandef = *def;
154 	mt76_rmw_field(dev, MT_AGG_BWCR, MT_AGG_BWCR_BW, bw);
155 	ret = mt7603_mcu_set_channel(dev);
156 	if (ret) {
157 		failed = true;
158 		goto out;
159 	}
160 
161 	if (def->chan->band == NL80211_BAND_5GHZ) {
162 		idx = 1;
163 		rssi_data += MT_EE_RSSI_OFFSET_5G;
164 	} else {
165 		idx = 0;
166 		rssi_data += MT_EE_RSSI_OFFSET_2G;
167 	}
168 
169 	memcpy(dev->rssi_offset, rssi_data, sizeof(dev->rssi_offset));
170 
171 	idx |= (def->chan -
172 		mt76_hw(dev)->wiphy->bands[def->chan->band]->channels) << 1;
173 	mt76_wr(dev, MT_WF_RMAC_CH_FREQ, idx);
174 	mt7603_mac_set_timing(dev);
175 	mt7603_mac_start(dev);
176 
177 	clear_bit(MT76_RESET, &dev->mphy.state);
178 
179 	mt76_txq_schedule_all(&dev->mphy);
180 
181 	ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mt76.mac_work,
182 				     msecs_to_jiffies(MT7603_WATCHDOG_TIME));
183 
184 	/* reset channel stats */
185 	mt76_clear(dev, MT_MIB_CTL, MT_MIB_CTL_READ_CLR_DIS);
186 	mt76_set(dev, MT_MIB_CTL,
187 		 MT_MIB_CTL_CCA_NAV_TX | MT_MIB_CTL_PSCCA_TIME);
188 	mt76_rr(dev, MT_MIB_STAT_CCA);
189 	mt7603_cca_stats_reset(dev);
190 
191 	dev->mphy.survey_time = ktime_get_boottime();
192 
193 	mt7603_init_edcca(dev);
194 
195 out:
196 	if (!(mt76_hw(dev)->conf.flags & IEEE80211_CONF_OFFCHANNEL))
197 		mt7603_beacon_set_timer(dev, -1, dev->mt76.beacon_int);
198 	mutex_unlock(&dev->mt76.mutex);
199 
200 	tasklet_enable(&dev->mt76.pre_tbtt_tasklet);
201 
202 	if (failed)
203 		mt7603_mac_work(&dev->mt76.mac_work.work);
204 
205 	return ret;
206 }
207 
208 static int
209 mt7603_config(struct ieee80211_hw *hw, u32 changed)
210 {
211 	struct mt7603_dev *dev = hw->priv;
212 	int ret = 0;
213 
214 	if (changed & (IEEE80211_CONF_CHANGE_CHANNEL |
215 		       IEEE80211_CONF_CHANGE_POWER)) {
216 		ieee80211_stop_queues(hw);
217 		ret = mt7603_set_channel(dev, &hw->conf.chandef);
218 		ieee80211_wake_queues(hw);
219 	}
220 
221 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
222 		mutex_lock(&dev->mt76.mutex);
223 
224 		if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
225 			dev->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
226 		else
227 			dev->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
228 
229 		mt76_wr(dev, MT_WF_RFCR, dev->rxfilter);
230 
231 		mutex_unlock(&dev->mt76.mutex);
232 	}
233 
234 	return ret;
235 }
236 
237 static void
238 mt7603_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
239 			unsigned int *total_flags, u64 multicast)
240 {
241 	struct mt7603_dev *dev = hw->priv;
242 	u32 flags = 0;
243 
244 #define MT76_FILTER(_flag, _hw) do { \
245 		flags |= *total_flags & FIF_##_flag;			\
246 		dev->rxfilter &= ~(_hw);				\
247 		dev->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
248 	} while (0)
249 
250 	dev->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
251 			   MT_WF_RFCR_DROP_OTHER_BEACON |
252 			   MT_WF_RFCR_DROP_FRAME_REPORT |
253 			   MT_WF_RFCR_DROP_PROBEREQ |
254 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
255 			   MT_WF_RFCR_DROP_MCAST |
256 			   MT_WF_RFCR_DROP_BCAST |
257 			   MT_WF_RFCR_DROP_DUPLICATE |
258 			   MT_WF_RFCR_DROP_A2_BSSID |
259 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
260 			   MT_WF_RFCR_DROP_STBC_MULTI);
261 
262 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
263 			       MT_WF_RFCR_DROP_A3_MAC |
264 			       MT_WF_RFCR_DROP_A3_BSSID);
265 
266 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
267 
268 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
269 			     MT_WF_RFCR_DROP_RTS |
270 			     MT_WF_RFCR_DROP_CTL_RSV |
271 			     MT_WF_RFCR_DROP_NDPA);
272 
273 	*total_flags = flags;
274 	mt76_wr(dev, MT_WF_RFCR, dev->rxfilter);
275 }
276 
277 static void
278 mt7603_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
279 			struct ieee80211_bss_conf *info, u32 changed)
280 {
281 	struct mt7603_dev *dev = hw->priv;
282 	struct mt7603_vif *mvif = (struct mt7603_vif *)vif->drv_priv;
283 
284 	mutex_lock(&dev->mt76.mutex);
285 
286 	if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BSSID)) {
287 		if (info->assoc || info->ibss_joined) {
288 			mt76_wr(dev, MT_BSSID0(mvif->idx),
289 				get_unaligned_le32(info->bssid));
290 			mt76_wr(dev, MT_BSSID1(mvif->idx),
291 				(get_unaligned_le16(info->bssid + 4) |
292 				 MT_BSSID1_VALID));
293 		} else {
294 			mt76_wr(dev, MT_BSSID0(mvif->idx), 0);
295 			mt76_wr(dev, MT_BSSID1(mvif->idx), 0);
296 		}
297 	}
298 
299 	if (changed & BSS_CHANGED_ERP_SLOT) {
300 		int slottime = info->use_short_slot ? 9 : 20;
301 
302 		if (slottime != dev->slottime) {
303 			dev->slottime = slottime;
304 			mt7603_mac_set_timing(dev);
305 		}
306 	}
307 
308 	if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON_INT)) {
309 		int beacon_int = !!info->enable_beacon * info->beacon_int;
310 
311 		tasklet_disable(&dev->mt76.pre_tbtt_tasklet);
312 		mt7603_beacon_set_timer(dev, mvif->idx, beacon_int);
313 		tasklet_enable(&dev->mt76.pre_tbtt_tasklet);
314 	}
315 
316 	mutex_unlock(&dev->mt76.mutex);
317 }
318 
319 int
320 mt7603_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
321 	       struct ieee80211_sta *sta)
322 {
323 	struct mt7603_dev *dev = container_of(mdev, struct mt7603_dev, mt76);
324 	struct mt7603_sta *msta = (struct mt7603_sta *)sta->drv_priv;
325 	struct mt7603_vif *mvif = (struct mt7603_vif *)vif->drv_priv;
326 	int idx;
327 	int ret = 0;
328 
329 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7603_WTBL_STA - 1);
330 	if (idx < 0)
331 		return -ENOSPC;
332 
333 	INIT_LIST_HEAD(&msta->poll_list);
334 	__skb_queue_head_init(&msta->psq);
335 	msta->ps = ~0;
336 	msta->smps = ~0;
337 	msta->wcid.sta = 1;
338 	msta->wcid.idx = idx;
339 	mt7603_wtbl_init(dev, idx, mvif->idx, sta->addr);
340 	mt7603_wtbl_set_ps(dev, msta, false);
341 
342 	if (vif->type == NL80211_IFTYPE_AP)
343 		set_bit(MT_WCID_FLAG_CHECK_PS, &msta->wcid.flags);
344 
345 	return ret;
346 }
347 
348 void
349 mt7603_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
350 		 struct ieee80211_sta *sta)
351 {
352 	struct mt7603_dev *dev = container_of(mdev, struct mt7603_dev, mt76);
353 
354 	mt7603_wtbl_update_cap(dev, sta);
355 }
356 
357 void
358 mt7603_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
359 		  struct ieee80211_sta *sta)
360 {
361 	struct mt7603_dev *dev = container_of(mdev, struct mt7603_dev, mt76);
362 	struct mt7603_sta *msta = (struct mt7603_sta *)sta->drv_priv;
363 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
364 
365 	spin_lock_bh(&dev->ps_lock);
366 	__skb_queue_purge(&msta->psq);
367 	mt7603_filter_tx(dev, wcid->idx, true);
368 	spin_unlock_bh(&dev->ps_lock);
369 
370 	spin_lock_bh(&dev->sta_poll_lock);
371 	if (!list_empty(&msta->poll_list))
372 		list_del_init(&msta->poll_list);
373 	spin_unlock_bh(&dev->sta_poll_lock);
374 
375 	mt7603_wtbl_clear(dev, wcid->idx);
376 }
377 
378 static void
379 mt7603_ps_tx_list(struct mt7603_dev *dev, struct sk_buff_head *list)
380 {
381 	struct sk_buff *skb;
382 
383 	while ((skb = __skb_dequeue(list)) != NULL)
384 		mt76_tx_queue_skb_raw(dev, skb_get_queue_mapping(skb),
385 				      skb, 0);
386 }
387 
388 void
389 mt7603_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta, bool ps)
390 {
391 	struct mt7603_dev *dev = container_of(mdev, struct mt7603_dev, mt76);
392 	struct mt7603_sta *msta = (struct mt7603_sta *)sta->drv_priv;
393 	struct sk_buff_head list;
394 
395 	mt76_stop_tx_queues(&dev->mt76, sta, true);
396 	mt7603_wtbl_set_ps(dev, msta, ps);
397 	if (ps)
398 		return;
399 
400 	__skb_queue_head_init(&list);
401 
402 	spin_lock_bh(&dev->ps_lock);
403 	skb_queue_splice_tail_init(&msta->psq, &list);
404 	spin_unlock_bh(&dev->ps_lock);
405 
406 	mt7603_ps_tx_list(dev, &list);
407 }
408 
409 static void
410 mt7603_ps_set_more_data(struct sk_buff *skb)
411 {
412 	struct ieee80211_hdr *hdr;
413 
414 	hdr = (struct ieee80211_hdr *)&skb->data[MT_TXD_SIZE];
415 	hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
416 }
417 
418 static void
419 mt7603_release_buffered_frames(struct ieee80211_hw *hw,
420 			       struct ieee80211_sta *sta,
421 			       u16 tids, int nframes,
422 			       enum ieee80211_frame_release_type reason,
423 			       bool more_data)
424 {
425 	struct mt7603_dev *dev = hw->priv;
426 	struct mt7603_sta *msta = (struct mt7603_sta *)sta->drv_priv;
427 	struct sk_buff_head list;
428 	struct sk_buff *skb, *tmp;
429 
430 	__skb_queue_head_init(&list);
431 
432 	mt7603_wtbl_set_ps(dev, msta, false);
433 
434 	spin_lock_bh(&dev->ps_lock);
435 	skb_queue_walk_safe(&msta->psq, skb, tmp) {
436 		if (!nframes)
437 			break;
438 
439 		if (!(tids & BIT(skb->priority)))
440 			continue;
441 
442 		skb_set_queue_mapping(skb, MT_TXQ_PSD);
443 		__skb_unlink(skb, &msta->psq);
444 		mt7603_ps_set_more_data(skb);
445 		__skb_queue_tail(&list, skb);
446 		nframes--;
447 	}
448 	spin_unlock_bh(&dev->ps_lock);
449 
450 	if (!skb_queue_empty(&list))
451 		ieee80211_sta_eosp(sta);
452 
453 	mt7603_ps_tx_list(dev, &list);
454 
455 	if (nframes)
456 		mt76_release_buffered_frames(hw, sta, tids, nframes, reason,
457 					     more_data);
458 }
459 
460 static int
461 mt7603_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
462 	       struct ieee80211_vif *vif, struct ieee80211_sta *sta,
463 	       struct ieee80211_key_conf *key)
464 {
465 	struct mt7603_dev *dev = hw->priv;
466 	struct mt7603_vif *mvif = (struct mt7603_vif *)vif->drv_priv;
467 	struct mt7603_sta *msta = sta ? (struct mt7603_sta *)sta->drv_priv :
468 				  &mvif->sta;
469 	struct mt76_wcid *wcid = &msta->wcid;
470 	int idx = key->keyidx;
471 
472 	/* fall back to sw encryption for unsupported ciphers */
473 	switch (key->cipher) {
474 	case WLAN_CIPHER_SUITE_TKIP:
475 	case WLAN_CIPHER_SUITE_CCMP:
476 		break;
477 	default:
478 		return -EOPNOTSUPP;
479 	}
480 
481 	/*
482 	 * The hardware does not support per-STA RX GTK, fall back
483 	 * to software mode for these.
484 	 */
485 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
486 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
487 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
488 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
489 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
490 		return -EOPNOTSUPP;
491 
492 	if (cmd == SET_KEY) {
493 		key->hw_key_idx = wcid->idx;
494 		wcid->hw_key_idx = idx;
495 	} else {
496 		if (idx == wcid->hw_key_idx)
497 			wcid->hw_key_idx = -1;
498 
499 		key = NULL;
500 	}
501 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
502 
503 	return mt7603_wtbl_set_key(dev, wcid->idx, key);
504 }
505 
506 static int
507 mt7603_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
508 	       const struct ieee80211_tx_queue_params *params)
509 {
510 	struct mt7603_dev *dev = hw->priv;
511 	u16 cw_min = (1 << 5) - 1;
512 	u16 cw_max = (1 << 10) - 1;
513 	u32 val;
514 
515 	queue = dev->mt76.q_tx[queue]->hw_idx;
516 
517 	if (params->cw_min)
518 		cw_min = params->cw_min;
519 	if (params->cw_max)
520 		cw_max = params->cw_max;
521 
522 	mutex_lock(&dev->mt76.mutex);
523 	mt7603_mac_stop(dev);
524 
525 	val = mt76_rr(dev, MT_WMM_TXOP(queue));
526 	val &= ~(MT_WMM_TXOP_MASK << MT_WMM_TXOP_SHIFT(queue));
527 	val |= params->txop << MT_WMM_TXOP_SHIFT(queue);
528 	mt76_wr(dev, MT_WMM_TXOP(queue), val);
529 
530 	val = mt76_rr(dev, MT_WMM_AIFSN);
531 	val &= ~(MT_WMM_AIFSN_MASK << MT_WMM_AIFSN_SHIFT(queue));
532 	val |= params->aifs << MT_WMM_AIFSN_SHIFT(queue);
533 	mt76_wr(dev, MT_WMM_AIFSN, val);
534 
535 	val = mt76_rr(dev, MT_WMM_CWMIN);
536 	val &= ~(MT_WMM_CWMIN_MASK << MT_WMM_CWMIN_SHIFT(queue));
537 	val |= cw_min << MT_WMM_CWMIN_SHIFT(queue);
538 	mt76_wr(dev, MT_WMM_CWMIN, val);
539 
540 	val = mt76_rr(dev, MT_WMM_CWMAX(queue));
541 	val &= ~(MT_WMM_CWMAX_MASK << MT_WMM_CWMAX_SHIFT(queue));
542 	val |= cw_max << MT_WMM_CWMAX_SHIFT(queue);
543 	mt76_wr(dev, MT_WMM_CWMAX(queue), val);
544 
545 	mt7603_mac_start(dev);
546 	mutex_unlock(&dev->mt76.mutex);
547 
548 	return 0;
549 }
550 
551 static void
552 mt7603_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
553 	     u32 queues, bool drop)
554 {
555 }
556 
557 static int
558 mt7603_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
559 		    struct ieee80211_ampdu_params *params)
560 {
561 	enum ieee80211_ampdu_mlme_action action = params->action;
562 	struct mt7603_dev *dev = hw->priv;
563 	struct ieee80211_sta *sta = params->sta;
564 	struct ieee80211_txq *txq = sta->txq[params->tid];
565 	struct mt7603_sta *msta = (struct mt7603_sta *)sta->drv_priv;
566 	u16 tid = params->tid;
567 	u16 ssn = params->ssn;
568 	u8 ba_size = params->buf_size;
569 	struct mt76_txq *mtxq;
570 	int ret = 0;
571 
572 	if (!txq)
573 		return -EINVAL;
574 
575 	mtxq = (struct mt76_txq *)txq->drv_priv;
576 
577 	mutex_lock(&dev->mt76.mutex);
578 	switch (action) {
579 	case IEEE80211_AMPDU_RX_START:
580 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
581 				   params->buf_size);
582 		mt7603_mac_rx_ba_reset(dev, sta->addr, tid);
583 		break;
584 	case IEEE80211_AMPDU_RX_STOP:
585 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
586 		break;
587 	case IEEE80211_AMPDU_TX_OPERATIONAL:
588 		mtxq->aggr = true;
589 		mtxq->send_bar = false;
590 		mt7603_mac_tx_ba_reset(dev, msta->wcid.idx, tid, ba_size);
591 		break;
592 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
593 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
594 		mtxq->aggr = false;
595 		mt7603_mac_tx_ba_reset(dev, msta->wcid.idx, tid, -1);
596 		break;
597 	case IEEE80211_AMPDU_TX_START:
598 		mtxq->agg_ssn = IEEE80211_SN_TO_SEQ(ssn);
599 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
600 		break;
601 	case IEEE80211_AMPDU_TX_STOP_CONT:
602 		mtxq->aggr = false;
603 		mt7603_mac_tx_ba_reset(dev, msta->wcid.idx, tid, -1);
604 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
605 		break;
606 	}
607 	mutex_unlock(&dev->mt76.mutex);
608 
609 	return ret;
610 }
611 
612 static void
613 mt7603_sta_rate_tbl_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
614 			   struct ieee80211_sta *sta)
615 {
616 	struct mt7603_dev *dev = hw->priv;
617 	struct mt7603_sta *msta = (struct mt7603_sta *)sta->drv_priv;
618 	struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
619 	int i;
620 
621 	spin_lock_bh(&dev->mt76.lock);
622 	for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
623 		msta->rates[i].idx = sta_rates->rate[i].idx;
624 		msta->rates[i].count = sta_rates->rate[i].count;
625 		msta->rates[i].flags = sta_rates->rate[i].flags;
626 
627 		if (msta->rates[i].idx < 0 || !msta->rates[i].count)
628 			break;
629 	}
630 	msta->n_rates = i;
631 	mt7603_wtbl_set_rates(dev, msta, NULL, msta->rates);
632 	msta->rate_probe = false;
633 	mt7603_wtbl_set_smps(dev, msta,
634 			     sta->smps_mode == IEEE80211_SMPS_DYNAMIC);
635 	spin_unlock_bh(&dev->mt76.lock);
636 }
637 
638 static void
639 mt7603_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
640 {
641 	struct mt7603_dev *dev = hw->priv;
642 
643 	mutex_lock(&dev->mt76.mutex);
644 	dev->coverage_class = max_t(s16, coverage_class, 0);
645 	mt7603_mac_set_timing(dev);
646 	mutex_unlock(&dev->mt76.mutex);
647 }
648 
649 static void mt7603_tx(struct ieee80211_hw *hw,
650 		      struct ieee80211_tx_control *control,
651 		      struct sk_buff *skb)
652 {
653 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
654 	struct ieee80211_vif *vif = info->control.vif;
655 	struct mt7603_dev *dev = hw->priv;
656 	struct mt76_wcid *wcid = &dev->global_sta.wcid;
657 
658 	if (control->sta) {
659 		struct mt7603_sta *msta;
660 
661 		msta = (struct mt7603_sta *)control->sta->drv_priv;
662 		wcid = &msta->wcid;
663 	} else if (vif) {
664 		struct mt7603_vif *mvif;
665 
666 		mvif = (struct mt7603_vif *)vif->drv_priv;
667 		wcid = &mvif->sta.wcid;
668 	}
669 
670 	mt76_tx(&dev->mphy, control->sta, wcid, skb);
671 }
672 
673 const struct ieee80211_ops mt7603_ops = {
674 	.tx = mt7603_tx,
675 	.start = mt7603_start,
676 	.stop = mt7603_stop,
677 	.add_interface = mt7603_add_interface,
678 	.remove_interface = mt7603_remove_interface,
679 	.config = mt7603_config,
680 	.configure_filter = mt7603_configure_filter,
681 	.bss_info_changed = mt7603_bss_info_changed,
682 	.sta_state = mt76_sta_state,
683 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
684 	.set_key = mt7603_set_key,
685 	.conf_tx = mt7603_conf_tx,
686 	.sw_scan_start = mt76_sw_scan,
687 	.sw_scan_complete = mt76_sw_scan_complete,
688 	.flush = mt7603_flush,
689 	.ampdu_action = mt7603_ampdu_action,
690 	.get_txpower = mt76_get_txpower,
691 	.wake_tx_queue = mt76_wake_tx_queue,
692 	.sta_rate_tbl_update = mt7603_sta_rate_tbl_update,
693 	.release_buffered_frames = mt7603_release_buffered_frames,
694 	.set_coverage_class = mt7603_set_coverage_class,
695 	.set_tim = mt76_set_tim,
696 	.get_survey = mt76_get_survey,
697 	.get_antenna = mt76_get_antenna,
698 };
699 
700 MODULE_LICENSE("Dual BSD/GPL");
701 
702 static int __init mt7603_init(void)
703 {
704 	int ret;
705 
706 	ret = platform_driver_register(&mt76_wmac_driver);
707 	if (ret)
708 		return ret;
709 
710 #ifdef CONFIG_PCI
711 	ret = pci_register_driver(&mt7603_pci_driver);
712 	if (ret)
713 		platform_driver_unregister(&mt76_wmac_driver);
714 #endif
715 	return ret;
716 }
717 
718 static void __exit mt7603_exit(void)
719 {
720 #ifdef CONFIG_PCI
721 	pci_unregister_driver(&mt7603_pci_driver);
722 #endif
723 	platform_driver_unregister(&mt76_wmac_driver);
724 }
725 
726 module_init(mt7603_init);
727 module_exit(mt7603_exit);
728