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