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 		int qid = skb_get_queue_mapping(skb);
385 
386 		mt76_tx_queue_skb_raw(dev, dev->mphy.q_tx[qid], skb, 0);
387 	}
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->mphy, 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->mphy.q_tx[queue]->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 	mutex_lock(&dev->mt76.mutex);
646 	dev->coverage_class = max_t(s16, coverage_class, 0);
647 	mt7603_mac_set_timing(dev);
648 	mutex_unlock(&dev->mt76.mutex);
649 }
650 
651 static void mt7603_tx(struct ieee80211_hw *hw,
652 		      struct ieee80211_tx_control *control,
653 		      struct sk_buff *skb)
654 {
655 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
656 	struct ieee80211_vif *vif = info->control.vif;
657 	struct mt7603_dev *dev = hw->priv;
658 	struct mt76_wcid *wcid = &dev->global_sta.wcid;
659 
660 	if (control->sta) {
661 		struct mt7603_sta *msta;
662 
663 		msta = (struct mt7603_sta *)control->sta->drv_priv;
664 		wcid = &msta->wcid;
665 	} else if (vif) {
666 		struct mt7603_vif *mvif;
667 
668 		mvif = (struct mt7603_vif *)vif->drv_priv;
669 		wcid = &mvif->sta.wcid;
670 	}
671 
672 	mt76_tx(&dev->mphy, control->sta, wcid, skb);
673 }
674 
675 const struct ieee80211_ops mt7603_ops = {
676 	.tx = mt7603_tx,
677 	.start = mt7603_start,
678 	.stop = mt7603_stop,
679 	.add_interface = mt7603_add_interface,
680 	.remove_interface = mt7603_remove_interface,
681 	.config = mt7603_config,
682 	.configure_filter = mt7603_configure_filter,
683 	.bss_info_changed = mt7603_bss_info_changed,
684 	.sta_state = mt76_sta_state,
685 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
686 	.set_key = mt7603_set_key,
687 	.conf_tx = mt7603_conf_tx,
688 	.sw_scan_start = mt76_sw_scan,
689 	.sw_scan_complete = mt76_sw_scan_complete,
690 	.flush = mt7603_flush,
691 	.ampdu_action = mt7603_ampdu_action,
692 	.get_txpower = mt76_get_txpower,
693 	.wake_tx_queue = mt76_wake_tx_queue,
694 	.sta_rate_tbl_update = mt7603_sta_rate_tbl_update,
695 	.release_buffered_frames = mt7603_release_buffered_frames,
696 	.set_coverage_class = mt7603_set_coverage_class,
697 	.set_tim = mt76_set_tim,
698 	.get_survey = mt76_get_survey,
699 	.get_antenna = mt76_get_antenna,
700 };
701 
702 MODULE_LICENSE("Dual BSD/GPL");
703 
704 static int __init mt7603_init(void)
705 {
706 	int ret;
707 
708 	ret = platform_driver_register(&mt76_wmac_driver);
709 	if (ret)
710 		return ret;
711 
712 #ifdef CONFIG_PCI
713 	ret = pci_register_driver(&mt7603_pci_driver);
714 	if (ret)
715 		platform_driver_unregister(&mt76_wmac_driver);
716 #endif
717 	return ret;
718 }
719 
720 static void __exit mt7603_exit(void)
721 {
722 #ifdef CONFIG_PCI
723 	pci_unregister_driver(&mt7603_pci_driver);
724 #endif
725 	platform_driver_unregister(&mt76_wmac_driver);
726 }
727 
728 module_init(mt7603_init);
729 module_exit(mt7603_exit);
730