1 /*
2  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 #include <linux/of.h>
17 #include "mt76.h"
18 
19 #define CHAN2G(_idx, _freq) {			\
20 	.band = NL80211_BAND_2GHZ,		\
21 	.center_freq = (_freq),			\
22 	.hw_value = (_idx),			\
23 	.max_power = 30,			\
24 }
25 
26 #define CHAN5G(_idx, _freq) {			\
27 	.band = NL80211_BAND_5GHZ,		\
28 	.center_freq = (_freq),			\
29 	.hw_value = (_idx),			\
30 	.max_power = 30,			\
31 }
32 
33 static const struct ieee80211_channel mt76_channels_2ghz[] = {
34 	CHAN2G(1, 2412),
35 	CHAN2G(2, 2417),
36 	CHAN2G(3, 2422),
37 	CHAN2G(4, 2427),
38 	CHAN2G(5, 2432),
39 	CHAN2G(6, 2437),
40 	CHAN2G(7, 2442),
41 	CHAN2G(8, 2447),
42 	CHAN2G(9, 2452),
43 	CHAN2G(10, 2457),
44 	CHAN2G(11, 2462),
45 	CHAN2G(12, 2467),
46 	CHAN2G(13, 2472),
47 	CHAN2G(14, 2484),
48 };
49 
50 static const struct ieee80211_channel mt76_channels_5ghz[] = {
51 	CHAN5G(36, 5180),
52 	CHAN5G(40, 5200),
53 	CHAN5G(44, 5220),
54 	CHAN5G(48, 5240),
55 
56 	CHAN5G(52, 5260),
57 	CHAN5G(56, 5280),
58 	CHAN5G(60, 5300),
59 	CHAN5G(64, 5320),
60 
61 	CHAN5G(100, 5500),
62 	CHAN5G(104, 5520),
63 	CHAN5G(108, 5540),
64 	CHAN5G(112, 5560),
65 	CHAN5G(116, 5580),
66 	CHAN5G(120, 5600),
67 	CHAN5G(124, 5620),
68 	CHAN5G(128, 5640),
69 	CHAN5G(132, 5660),
70 	CHAN5G(136, 5680),
71 	CHAN5G(140, 5700),
72 
73 	CHAN5G(149, 5745),
74 	CHAN5G(153, 5765),
75 	CHAN5G(157, 5785),
76 	CHAN5G(161, 5805),
77 	CHAN5G(165, 5825),
78 };
79 
80 static const struct ieee80211_tpt_blink mt76_tpt_blink[] = {
81 	{ .throughput =   0 * 1024, .blink_time = 334 },
82 	{ .throughput =   1 * 1024, .blink_time = 260 },
83 	{ .throughput =   5 * 1024, .blink_time = 220 },
84 	{ .throughput =  10 * 1024, .blink_time = 190 },
85 	{ .throughput =  20 * 1024, .blink_time = 170 },
86 	{ .throughput =  50 * 1024, .blink_time = 150 },
87 	{ .throughput =  70 * 1024, .blink_time = 130 },
88 	{ .throughput = 100 * 1024, .blink_time = 110 },
89 	{ .throughput = 200 * 1024, .blink_time =  80 },
90 	{ .throughput = 300 * 1024, .blink_time =  50 },
91 };
92 
93 static int mt76_led_init(struct mt76_dev *dev)
94 {
95 	struct device_node *np = dev->dev->of_node;
96 	struct ieee80211_hw *hw = dev->hw;
97 	int led_pin;
98 
99 	if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
100 		return 0;
101 
102 	snprintf(dev->led_name, sizeof(dev->led_name),
103 		 "mt76-%s", wiphy_name(hw->wiphy));
104 
105 	dev->led_cdev.name = dev->led_name;
106 	dev->led_cdev.default_trigger =
107 		ieee80211_create_tpt_led_trigger(hw,
108 					IEEE80211_TPT_LEDTRIG_FL_RADIO,
109 					mt76_tpt_blink,
110 					ARRAY_SIZE(mt76_tpt_blink));
111 
112 	np = of_get_child_by_name(np, "led");
113 	if (np) {
114 		if (!of_property_read_u32(np, "led-sources", &led_pin))
115 			dev->led_pin = led_pin;
116 		dev->led_al = of_property_read_bool(np, "led-active-low");
117 	}
118 
119 	return devm_led_classdev_register(dev->dev, &dev->led_cdev);
120 }
121 
122 static void mt76_init_stream_cap(struct mt76_dev *dev,
123 				 struct ieee80211_supported_band *sband,
124 				 bool vht)
125 {
126 	struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
127 	int i, nstream = hweight8(dev->antenna_mask);
128 	struct ieee80211_sta_vht_cap *vht_cap;
129 	u16 mcs_map = 0;
130 
131 	if (nstream > 1)
132 		ht_cap->cap |= IEEE80211_HT_CAP_TX_STBC;
133 	else
134 		ht_cap->cap &= ~IEEE80211_HT_CAP_TX_STBC;
135 
136 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
137 		ht_cap->mcs.rx_mask[i] = i < nstream ? 0xff : 0;
138 
139 	if (!vht)
140 		return;
141 
142 	vht_cap = &sband->vht_cap;
143 	if (nstream > 1)
144 		vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
145 	else
146 		vht_cap->cap &= ~IEEE80211_VHT_CAP_TXSTBC;
147 
148 	for (i = 0; i < 8; i++) {
149 		if (i < nstream)
150 			mcs_map |= (IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2));
151 		else
152 			mcs_map |=
153 				(IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2));
154 	}
155 	vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
156 	vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
157 }
158 
159 void mt76_set_stream_caps(struct mt76_dev *dev, bool vht)
160 {
161 	if (dev->cap.has_2ghz)
162 		mt76_init_stream_cap(dev, &dev->sband_2g.sband, false);
163 	if (dev->cap.has_5ghz)
164 		mt76_init_stream_cap(dev, &dev->sband_5g.sband, vht);
165 }
166 EXPORT_SYMBOL_GPL(mt76_set_stream_caps);
167 
168 static int
169 mt76_init_sband(struct mt76_dev *dev, struct mt76_sband *msband,
170 		const struct ieee80211_channel *chan, int n_chan,
171 		struct ieee80211_rate *rates, int n_rates, bool vht)
172 {
173 	struct ieee80211_supported_band *sband = &msband->sband;
174 	struct ieee80211_sta_ht_cap *ht_cap;
175 	struct ieee80211_sta_vht_cap *vht_cap;
176 	void *chanlist;
177 	int size;
178 
179 	size = n_chan * sizeof(*chan);
180 	chanlist = devm_kmemdup(dev->dev, chan, size, GFP_KERNEL);
181 	if (!chanlist)
182 		return -ENOMEM;
183 
184 	msband->chan = devm_kcalloc(dev->dev, n_chan, sizeof(*msband->chan),
185 				    GFP_KERNEL);
186 	if (!msband->chan)
187 		return -ENOMEM;
188 
189 	sband->channels = chanlist;
190 	sband->n_channels = n_chan;
191 	sband->bitrates = rates;
192 	sband->n_bitrates = n_rates;
193 	dev->chandef.chan = &sband->channels[0];
194 
195 	ht_cap = &sband->ht_cap;
196 	ht_cap->ht_supported = true;
197 	ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
198 		       IEEE80211_HT_CAP_GRN_FLD |
199 		       IEEE80211_HT_CAP_SGI_20 |
200 		       IEEE80211_HT_CAP_SGI_40 |
201 		       (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
202 
203 	ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
204 	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
205 	ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_4;
206 
207 	mt76_init_stream_cap(dev, sband, vht);
208 
209 	if (!vht)
210 		return 0;
211 
212 	vht_cap = &sband->vht_cap;
213 	vht_cap->vht_supported = true;
214 	vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC |
215 			IEEE80211_VHT_CAP_RXSTBC_1 |
216 			IEEE80211_VHT_CAP_SHORT_GI_80 |
217 			(3 << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT);
218 
219 	return 0;
220 }
221 
222 static int
223 mt76_init_sband_2g(struct mt76_dev *dev, struct ieee80211_rate *rates,
224 		   int n_rates)
225 {
226 	dev->hw->wiphy->bands[NL80211_BAND_2GHZ] = &dev->sband_2g.sband;
227 
228 	return mt76_init_sband(dev, &dev->sband_2g,
229 			       mt76_channels_2ghz,
230 			       ARRAY_SIZE(mt76_channels_2ghz),
231 			       rates, n_rates, false);
232 }
233 
234 static int
235 mt76_init_sband_5g(struct mt76_dev *dev, struct ieee80211_rate *rates,
236 		   int n_rates, bool vht)
237 {
238 	dev->hw->wiphy->bands[NL80211_BAND_5GHZ] = &dev->sband_5g.sband;
239 
240 	return mt76_init_sband(dev, &dev->sband_5g,
241 			       mt76_channels_5ghz,
242 			       ARRAY_SIZE(mt76_channels_5ghz),
243 			       rates, n_rates, vht);
244 }
245 
246 static void
247 mt76_check_sband(struct mt76_dev *dev, int band)
248 {
249 	struct ieee80211_supported_band *sband = dev->hw->wiphy->bands[band];
250 	bool found = false;
251 	int i;
252 
253 	if (!sband)
254 		return;
255 
256 	for (i = 0; i < sband->n_channels; i++) {
257 		if (sband->channels[i].flags & IEEE80211_CHAN_DISABLED)
258 			continue;
259 
260 		found = true;
261 		break;
262 	}
263 
264 	if (found)
265 		return;
266 
267 	sband->n_channels = 0;
268 	dev->hw->wiphy->bands[band] = NULL;
269 }
270 
271 struct mt76_dev *
272 mt76_alloc_device(struct device *pdev, unsigned int size,
273 		  const struct ieee80211_ops *ops,
274 		  const struct mt76_driver_ops *drv_ops)
275 {
276 	struct ieee80211_hw *hw;
277 	struct mt76_dev *dev;
278 
279 	hw = ieee80211_alloc_hw(size, ops);
280 	if (!hw)
281 		return NULL;
282 
283 	dev = hw->priv;
284 	dev->hw = hw;
285 	dev->dev = pdev;
286 	dev->drv = drv_ops;
287 
288 	spin_lock_init(&dev->rx_lock);
289 	spin_lock_init(&dev->lock);
290 	spin_lock_init(&dev->cc_lock);
291 	mutex_init(&dev->mutex);
292 	init_waitqueue_head(&dev->tx_wait);
293 	skb_queue_head_init(&dev->status_list);
294 
295 	return dev;
296 }
297 EXPORT_SYMBOL_GPL(mt76_alloc_device);
298 
299 int mt76_register_device(struct mt76_dev *dev, bool vht,
300 			 struct ieee80211_rate *rates, int n_rates)
301 {
302 	struct ieee80211_hw *hw = dev->hw;
303 	struct wiphy *wiphy = hw->wiphy;
304 	int ret;
305 
306 	dev_set_drvdata(dev->dev, dev);
307 
308 	INIT_LIST_HEAD(&dev->txwi_cache);
309 
310 	SET_IEEE80211_DEV(hw, dev->dev);
311 	SET_IEEE80211_PERM_ADDR(hw, dev->macaddr);
312 
313 	wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
314 
315 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
316 
317 	wiphy->available_antennas_tx = dev->antenna_mask;
318 	wiphy->available_antennas_rx = dev->antenna_mask;
319 
320 	hw->txq_data_size = sizeof(struct mt76_txq);
321 	hw->max_tx_fragments = 16;
322 
323 	ieee80211_hw_set(hw, SIGNAL_DBM);
324 	ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
325 	ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
326 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
327 	ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
328 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
329 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
330 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
331 	ieee80211_hw_set(hw, TX_AMSDU);
332 	ieee80211_hw_set(hw, TX_FRAG_LIST);
333 	ieee80211_hw_set(hw, MFP_CAPABLE);
334 	ieee80211_hw_set(hw, AP_LINK_PS);
335 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
336 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
337 
338 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
339 
340 	if (dev->cap.has_2ghz) {
341 		ret = mt76_init_sband_2g(dev, rates, n_rates);
342 		if (ret)
343 			return ret;
344 	}
345 
346 	if (dev->cap.has_5ghz) {
347 		ret = mt76_init_sband_5g(dev, rates + 4, n_rates - 4, vht);
348 		if (ret)
349 			return ret;
350 	}
351 
352 	wiphy_read_of_freq_limits(dev->hw->wiphy);
353 	mt76_check_sband(dev, NL80211_BAND_2GHZ);
354 	mt76_check_sband(dev, NL80211_BAND_5GHZ);
355 
356 	if (IS_ENABLED(CONFIG_MT76_LEDS)) {
357 		ret = mt76_led_init(dev);
358 		if (ret)
359 			return ret;
360 	}
361 
362 	return ieee80211_register_hw(hw);
363 }
364 EXPORT_SYMBOL_GPL(mt76_register_device);
365 
366 void mt76_unregister_device(struct mt76_dev *dev)
367 {
368 	struct ieee80211_hw *hw = dev->hw;
369 
370 	mt76_tx_status_check(dev, NULL, true);
371 	ieee80211_unregister_hw(hw);
372 	mt76_tx_free(dev);
373 }
374 EXPORT_SYMBOL_GPL(mt76_unregister_device);
375 
376 void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb)
377 {
378 	if (!test_bit(MT76_STATE_RUNNING, &dev->state)) {
379 		dev_kfree_skb(skb);
380 		return;
381 	}
382 
383 	__skb_queue_tail(&dev->rx_skb[q], skb);
384 }
385 EXPORT_SYMBOL_GPL(mt76_rx);
386 
387 static bool mt76_has_tx_pending(struct mt76_dev *dev)
388 {
389 	int i;
390 
391 	for (i = 0; i < ARRAY_SIZE(dev->q_tx); i++) {
392 		if (dev->q_tx[i].queued)
393 			return true;
394 	}
395 
396 	return false;
397 }
398 
399 void mt76_set_channel(struct mt76_dev *dev)
400 {
401 	struct ieee80211_hw *hw = dev->hw;
402 	struct cfg80211_chan_def *chandef = &hw->conf.chandef;
403 	struct mt76_channel_state *state;
404 	bool offchannel = hw->conf.flags & IEEE80211_CONF_OFFCHANNEL;
405 	int timeout = HZ / 5;
406 
407 	if (offchannel)
408 		set_bit(MT76_OFFCHANNEL, &dev->state);
409 	else
410 		clear_bit(MT76_OFFCHANNEL, &dev->state);
411 
412 	wait_event_timeout(dev->tx_wait, !mt76_has_tx_pending(dev), timeout);
413 
414 	if (dev->drv->update_survey)
415 		dev->drv->update_survey(dev);
416 
417 	dev->chandef = *chandef;
418 
419 	if (!offchannel)
420 		dev->main_chan = chandef->chan;
421 
422 	if (chandef->chan != dev->main_chan) {
423 		state = mt76_channel_state(dev, chandef->chan);
424 		memset(state, 0, sizeof(*state));
425 	}
426 }
427 EXPORT_SYMBOL_GPL(mt76_set_channel);
428 
429 int mt76_get_survey(struct ieee80211_hw *hw, int idx,
430 		    struct survey_info *survey)
431 {
432 	struct mt76_dev *dev = hw->priv;
433 	struct mt76_sband *sband;
434 	struct ieee80211_channel *chan;
435 	struct mt76_channel_state *state;
436 	int ret = 0;
437 
438 	if (idx == 0 && dev->drv->update_survey)
439 		dev->drv->update_survey(dev);
440 
441 	sband = &dev->sband_2g;
442 	if (idx >= sband->sband.n_channels) {
443 		idx -= sband->sband.n_channels;
444 		sband = &dev->sband_5g;
445 	}
446 
447 	if (idx >= sband->sband.n_channels)
448 		return -ENOENT;
449 
450 	chan = &sband->sband.channels[idx];
451 	state = mt76_channel_state(dev, chan);
452 
453 	memset(survey, 0, sizeof(*survey));
454 	survey->channel = chan;
455 	survey->filled = SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY;
456 	if (chan == dev->main_chan)
457 		survey->filled |= SURVEY_INFO_IN_USE;
458 
459 	spin_lock_bh(&dev->cc_lock);
460 	survey->time = div_u64(state->cc_active, 1000);
461 	survey->time_busy = div_u64(state->cc_busy, 1000);
462 	spin_unlock_bh(&dev->cc_lock);
463 
464 	return ret;
465 }
466 EXPORT_SYMBOL_GPL(mt76_get_survey);
467 
468 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
469 			 struct ieee80211_key_conf *key)
470 {
471 	struct ieee80211_key_seq seq;
472 	int i;
473 
474 	wcid->rx_check_pn = false;
475 
476 	if (!key)
477 		return;
478 
479 	if (key->cipher == WLAN_CIPHER_SUITE_CCMP)
480 		wcid->rx_check_pn = true;
481 
482 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
483 		ieee80211_get_key_rx_seq(key, i, &seq);
484 		memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
485 	}
486 }
487 EXPORT_SYMBOL(mt76_wcid_key_setup);
488 
489 struct ieee80211_sta *mt76_rx_convert(struct sk_buff *skb)
490 {
491 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
492 	struct mt76_rx_status mstat;
493 
494 	mstat = *((struct mt76_rx_status *) skb->cb);
495 	memset(status, 0, sizeof(*status));
496 
497 	status->flag = mstat.flag;
498 	status->freq = mstat.freq;
499 	status->enc_flags = mstat.enc_flags;
500 	status->encoding = mstat.encoding;
501 	status->bw = mstat.bw;
502 	status->rate_idx = mstat.rate_idx;
503 	status->nss = mstat.nss;
504 	status->band = mstat.band;
505 	status->signal = mstat.signal;
506 	status->chains = mstat.chains;
507 
508 	BUILD_BUG_ON(sizeof(mstat) > sizeof(skb->cb));
509 	BUILD_BUG_ON(sizeof(status->chain_signal) != sizeof(mstat.chain_signal));
510 	memcpy(status->chain_signal, mstat.chain_signal, sizeof(mstat.chain_signal));
511 
512 	return wcid_to_sta(mstat.wcid);
513 }
514 EXPORT_SYMBOL(mt76_rx_convert);
515 
516 static int
517 mt76_check_ccmp_pn(struct sk_buff *skb)
518 {
519 	struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb;
520 	struct mt76_wcid *wcid = status->wcid;
521 	struct ieee80211_hdr *hdr;
522 	int ret;
523 
524 	if (!(status->flag & RX_FLAG_DECRYPTED))
525 		return 0;
526 
527 	if (!wcid || !wcid->rx_check_pn)
528 		return 0;
529 
530 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
531 		/*
532 		 * Validate the first fragment both here and in mac80211
533 		 * All further fragments will be validated by mac80211 only.
534 		 */
535 		hdr = (struct ieee80211_hdr *) skb->data;
536 		if (ieee80211_is_frag(hdr) &&
537 		    !ieee80211_is_first_frag(hdr->frame_control))
538 			return 0;
539 	}
540 
541 	BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0]));
542 	ret = memcmp(status->iv, wcid->rx_key_pn[status->tid],
543 		     sizeof(status->iv));
544 	if (ret <= 0)
545 		return -EINVAL; /* replay */
546 
547 	memcpy(wcid->rx_key_pn[status->tid], status->iv, sizeof(status->iv));
548 
549 	if (status->flag & RX_FLAG_IV_STRIPPED)
550 		status->flag |= RX_FLAG_PN_VALIDATED;
551 
552 	return 0;
553 }
554 
555 static void
556 mt76_check_sta(struct mt76_dev *dev, struct sk_buff *skb)
557 {
558 	struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb;
559 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
560 	struct ieee80211_sta *sta;
561 	struct mt76_wcid *wcid = status->wcid;
562 	bool ps;
563 
564 	if (ieee80211_is_pspoll(hdr->frame_control) && !wcid) {
565 		sta = ieee80211_find_sta_by_ifaddr(dev->hw, hdr->addr2, NULL);
566 		if (sta)
567 			wcid = status->wcid = (struct mt76_wcid *) sta->drv_priv;
568 	}
569 
570 	if (!wcid || !wcid->sta)
571 		return;
572 
573 	sta = container_of((void *) wcid, struct ieee80211_sta, drv_priv);
574 
575 	if (status->signal <= 0)
576 		ewma_signal_add(&wcid->rssi, -status->signal);
577 
578 	wcid->inactive_count = 0;
579 
580 	if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags))
581 		return;
582 
583 	if (ieee80211_is_pspoll(hdr->frame_control)) {
584 		ieee80211_sta_pspoll(sta);
585 		return;
586 	}
587 
588 	if (ieee80211_has_morefrags(hdr->frame_control) ||
589 		!(ieee80211_is_mgmt(hdr->frame_control) ||
590 		  ieee80211_is_data(hdr->frame_control)))
591 		return;
592 
593 	ps = ieee80211_has_pm(hdr->frame_control);
594 
595 	if (ps && (ieee80211_is_data_qos(hdr->frame_control) ||
596 		   ieee80211_is_qos_nullfunc(hdr->frame_control)))
597 		ieee80211_sta_uapsd_trigger(sta, status->tid);
598 
599 	if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps)
600 		return;
601 
602 	if (ps)
603 		set_bit(MT_WCID_FLAG_PS, &wcid->flags);
604 	else
605 		clear_bit(MT_WCID_FLAG_PS, &wcid->flags);
606 
607 	dev->drv->sta_ps(dev, sta, ps);
608 	ieee80211_sta_ps_transition(sta, ps);
609 }
610 
611 void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
612 		      struct napi_struct *napi)
613 {
614 	struct ieee80211_sta *sta;
615 	struct sk_buff *skb;
616 
617 	spin_lock(&dev->rx_lock);
618 	while ((skb = __skb_dequeue(frames)) != NULL) {
619 		if (mt76_check_ccmp_pn(skb)) {
620 			dev_kfree_skb(skb);
621 			continue;
622 		}
623 
624 		sta = mt76_rx_convert(skb);
625 		ieee80211_rx_napi(dev->hw, sta, skb, napi);
626 	}
627 	spin_unlock(&dev->rx_lock);
628 }
629 
630 void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
631 			   struct napi_struct *napi)
632 {
633 	struct sk_buff_head frames;
634 	struct sk_buff *skb;
635 
636 	__skb_queue_head_init(&frames);
637 
638 	while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) {
639 		mt76_check_sta(dev, skb);
640 		mt76_rx_aggr_reorder(skb, &frames);
641 	}
642 
643 	mt76_rx_complete(dev, &frames, napi);
644 }
645 EXPORT_SYMBOL_GPL(mt76_rx_poll_complete);
646 
647 static int
648 mt76_sta_add(struct mt76_dev *dev, struct ieee80211_vif *vif,
649 	     struct ieee80211_sta *sta)
650 {
651 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
652 	int ret;
653 	int i;
654 
655 	mutex_lock(&dev->mutex);
656 
657 	ret = dev->drv->sta_add(dev, vif, sta);
658 	if (ret)
659 		goto out;
660 
661 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
662 		struct mt76_txq *mtxq;
663 
664 		if (!sta->txq[i])
665 			continue;
666 
667 		mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv;
668 		mtxq->wcid = wcid;
669 
670 		mt76_txq_init(dev, sta->txq[i]);
671 	}
672 
673 	ewma_signal_init(&wcid->rssi);
674 	rcu_assign_pointer(dev->wcid[wcid->idx], wcid);
675 
676 out:
677 	mutex_unlock(&dev->mutex);
678 
679 	return ret;
680 }
681 
682 void __mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
683 		       struct ieee80211_sta *sta)
684 {
685 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
686 	int i, idx = wcid->idx;
687 
688 	rcu_assign_pointer(dev->wcid[idx], NULL);
689 	synchronize_rcu();
690 
691 	if (dev->drv->sta_remove)
692 		dev->drv->sta_remove(dev, vif, sta);
693 
694 	mt76_tx_status_check(dev, wcid, true);
695 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
696 		mt76_txq_remove(dev, sta->txq[i]);
697 	mt76_wcid_free(dev->wcid_mask, idx);
698 }
699 EXPORT_SYMBOL_GPL(__mt76_sta_remove);
700 
701 static void
702 mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
703 		struct ieee80211_sta *sta)
704 {
705 	mutex_lock(&dev->mutex);
706 	__mt76_sta_remove(dev, vif, sta);
707 	mutex_unlock(&dev->mutex);
708 }
709 
710 int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
711 		   struct ieee80211_sta *sta,
712 		   enum ieee80211_sta_state old_state,
713 		   enum ieee80211_sta_state new_state)
714 {
715 	struct mt76_dev *dev = hw->priv;
716 
717 	if (old_state == IEEE80211_STA_NOTEXIST &&
718 	    new_state == IEEE80211_STA_NONE)
719 		return mt76_sta_add(dev, vif, sta);
720 
721 	if (old_state == IEEE80211_STA_AUTH &&
722 	    new_state == IEEE80211_STA_ASSOC &&
723 	    dev->drv->sta_assoc)
724 		dev->drv->sta_assoc(dev, vif, sta);
725 
726 	if (old_state == IEEE80211_STA_NONE &&
727 		 new_state == IEEE80211_STA_NOTEXIST)
728 		mt76_sta_remove(dev, vif, sta);
729 
730 	return 0;
731 }
732 EXPORT_SYMBOL_GPL(mt76_sta_state);
733 
734 int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
735 		     int *dbm)
736 {
737 	struct mt76_dev *dev = hw->priv;
738 	int n_chains = hweight8(dev->antenna_mask);
739 
740 	*dbm = dev->txpower_cur / 2;
741 
742 	/* convert from per-chain power to combined
743 	 * output on 2x2 devices
744 	 */
745 	if (n_chains > 1)
746 		*dbm += 3;
747 
748 	return 0;
749 }
750 EXPORT_SYMBOL_GPL(mt76_get_txpower);
751 
752 static void
753 __mt76_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif)
754 {
755 	if (vif->csa_active && ieee80211_csa_is_complete(vif))
756 	    ieee80211_csa_finish(vif);
757 }
758 
759 void mt76_csa_finish(struct mt76_dev *dev)
760 {
761 	if (!dev->csa_complete)
762 		return;
763 
764 	ieee80211_iterate_active_interfaces_atomic(dev->hw,
765 		IEEE80211_IFACE_ITER_RESUME_ALL,
766 		__mt76_csa_finish, dev);
767 
768 	dev->csa_complete = 0;
769 }
770 EXPORT_SYMBOL_GPL(mt76_csa_finish);
771 
772 static void
773 __mt76_csa_check(void *priv, u8 *mac, struct ieee80211_vif *vif)
774 {
775 	struct mt76_dev *dev = priv;
776 
777 	if (!vif->csa_active)
778 		return;
779 
780 	dev->csa_complete |= ieee80211_csa_is_complete(vif);
781 }
782 
783 void mt76_csa_check(struct mt76_dev *dev)
784 {
785 	ieee80211_iterate_active_interfaces_atomic(dev->hw,
786 		IEEE80211_IFACE_ITER_RESUME_ALL,
787 		__mt76_csa_check, dev);
788 }
789 EXPORT_SYMBOL_GPL(mt76_csa_check);
790