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