]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/net/wireless/mediatek/mt76/mac80211.c
mt76: initialize available_antennas_{tx,rx} info
[thirdparty/kernel/stable.git] / drivers / net / wireless / mediatek / mt76 / mac80211.c
CommitLineData
17f1de56
FF
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
33static 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
50static 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
80static 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
93static 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
122static int
123mt76_init_sband(struct mt76_dev *dev, struct mt76_sband *msband,
124 const struct ieee80211_channel *chan, int n_chan,
125 struct ieee80211_rate *rates, int n_rates, bool vht)
126{
127 struct ieee80211_supported_band *sband = &msband->sband;
128 struct ieee80211_sta_ht_cap *ht_cap;
129 struct ieee80211_sta_vht_cap *vht_cap;
130 void *chanlist;
131 u16 mcs_map;
132 int size;
133
134 size = n_chan * sizeof(*chan);
135 chanlist = devm_kmemdup(dev->dev, chan, size, GFP_KERNEL);
136 if (!chanlist)
137 return -ENOMEM;
138
139 msband->chan = devm_kzalloc(dev->dev, n_chan * sizeof(*msband->chan),
140 GFP_KERNEL);
141 if (!msband->chan)
142 return -ENOMEM;
143
144 sband->channels = chanlist;
145 sband->n_channels = n_chan;
146 sband->bitrates = rates;
147 sband->n_bitrates = n_rates;
148 dev->chandef.chan = &sband->channels[0];
149
150 ht_cap = &sband->ht_cap;
151 ht_cap->ht_supported = true;
152 ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
153 IEEE80211_HT_CAP_GRN_FLD |
154 IEEE80211_HT_CAP_SGI_20 |
155 IEEE80211_HT_CAP_SGI_40 |
156 IEEE80211_HT_CAP_TX_STBC |
157 (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
158
159 ht_cap->mcs.rx_mask[0] = 0xff;
160 ht_cap->mcs.rx_mask[1] = 0xff;
161 ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
162 ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
163 ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_4;
164
165 if (!vht)
166 return 0;
167
168 vht_cap = &sband->vht_cap;
169 vht_cap->vht_supported = true;
170
171 mcs_map = (IEEE80211_VHT_MCS_SUPPORT_0_9 << (0 * 2)) |
172 (IEEE80211_VHT_MCS_SUPPORT_0_9 << (1 * 2)) |
173 (IEEE80211_VHT_MCS_NOT_SUPPORTED << (2 * 2)) |
174 (IEEE80211_VHT_MCS_NOT_SUPPORTED << (3 * 2)) |
175 (IEEE80211_VHT_MCS_NOT_SUPPORTED << (4 * 2)) |
176 (IEEE80211_VHT_MCS_NOT_SUPPORTED << (5 * 2)) |
177 (IEEE80211_VHT_MCS_NOT_SUPPORTED << (6 * 2)) |
178 (IEEE80211_VHT_MCS_NOT_SUPPORTED << (7 * 2));
179
180 vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
181 vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
182 vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC |
183 IEEE80211_VHT_CAP_TXSTBC |
184 IEEE80211_VHT_CAP_RXSTBC_1 |
185 IEEE80211_VHT_CAP_SHORT_GI_80;
186
187 return 0;
188}
189
190static int
191mt76_init_sband_2g(struct mt76_dev *dev, struct ieee80211_rate *rates,
192 int n_rates)
193{
194 dev->hw->wiphy->bands[NL80211_BAND_2GHZ] = &dev->sband_2g.sband;
195
196 return mt76_init_sband(dev, &dev->sband_2g,
197 mt76_channels_2ghz,
198 ARRAY_SIZE(mt76_channels_2ghz),
199 rates, n_rates, false);
200}
201
202static int
203mt76_init_sband_5g(struct mt76_dev *dev, struct ieee80211_rate *rates,
204 int n_rates, bool vht)
205{
206 dev->hw->wiphy->bands[NL80211_BAND_5GHZ] = &dev->sband_5g.sband;
207
208 return mt76_init_sband(dev, &dev->sband_5g,
209 mt76_channels_5ghz,
210 ARRAY_SIZE(mt76_channels_5ghz),
211 rates, n_rates, vht);
212}
213
214static void
215mt76_check_sband(struct mt76_dev *dev, int band)
216{
217 struct ieee80211_supported_band *sband = dev->hw->wiphy->bands[band];
218 bool found = false;
219 int i;
220
221 if (!sband)
222 return;
223
224 for (i = 0; i < sband->n_channels; i++) {
225 if (sband->channels[i].flags & IEEE80211_CHAN_DISABLED)
226 continue;
227
228 found = true;
229 break;
230 }
231
232 if (found)
233 return;
234
235 sband->n_channels = 0;
236 dev->hw->wiphy->bands[band] = NULL;
237}
238
239int mt76_register_device(struct mt76_dev *dev, bool vht,
240 struct ieee80211_rate *rates, int n_rates)
241{
242 struct ieee80211_hw *hw = dev->hw;
243 struct wiphy *wiphy = hw->wiphy;
244 int ret;
245
246 dev_set_drvdata(dev->dev, dev);
247
248 spin_lock_init(&dev->lock);
249 spin_lock_init(&dev->cc_lock);
250 INIT_LIST_HEAD(&dev->txwi_cache);
251
252 SET_IEEE80211_DEV(hw, dev->dev);
253 SET_IEEE80211_PERM_ADDR(hw, dev->macaddr);
254
255 wiphy->interface_modes =
256 BIT(NL80211_IFTYPE_STATION) |
257 BIT(NL80211_IFTYPE_AP) |
258#ifdef CONFIG_MAC80211_MESH
259 BIT(NL80211_IFTYPE_MESH_POINT) |
260#endif
261 BIT(NL80211_IFTYPE_ADHOC);
262
263 wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
264
24114a5f
LB
265 wiphy->available_antennas_tx = dev->antenna_mask;
266 wiphy->available_antennas_rx = dev->antenna_mask;
267
17f1de56
FF
268 hw->txq_data_size = sizeof(struct mt76_txq);
269 hw->max_tx_fragments = 16;
270
271 ieee80211_hw_set(hw, SIGNAL_DBM);
272 ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
273 ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
274 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
275 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
276 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
277 ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
278 ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
279 ieee80211_hw_set(hw, TX_AMSDU);
280 ieee80211_hw_set(hw, TX_FRAG_LIST);
281 ieee80211_hw_set(hw, MFP_CAPABLE);
d71ef286 282 ieee80211_hw_set(hw, AP_LINK_PS);
17f1de56
FF
283
284 wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
285
286 if (dev->cap.has_2ghz) {
287 ret = mt76_init_sband_2g(dev, rates, n_rates);
288 if (ret)
289 return ret;
290 }
291
292 if (dev->cap.has_5ghz) {
293 ret = mt76_init_sband_5g(dev, rates + 4, n_rates - 4, vht);
294 if (ret)
295 return ret;
296 }
297
298 wiphy_read_of_freq_limits(dev->hw->wiphy);
299 mt76_check_sband(dev, NL80211_BAND_2GHZ);
300 mt76_check_sband(dev, NL80211_BAND_5GHZ);
301
302 ret = mt76_led_init(dev);
303 if (ret)
304 return ret;
305
306 return ieee80211_register_hw(hw);
307}
308EXPORT_SYMBOL_GPL(mt76_register_device);
309
310void mt76_unregister_device(struct mt76_dev *dev)
311{
312 struct ieee80211_hw *hw = dev->hw;
313
314 ieee80211_unregister_hw(hw);
315 mt76_tx_free(dev);
316}
317EXPORT_SYMBOL_GPL(mt76_unregister_device);
318
319void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb)
320{
321 if (!test_bit(MT76_STATE_RUNNING, &dev->state)) {
322 dev_kfree_skb(skb);
323 return;
324 }
325
326 __skb_queue_tail(&dev->rx_skb[q], skb);
327}
328EXPORT_SYMBOL_GPL(mt76_rx);
329
330void mt76_set_channel(struct mt76_dev *dev)
331{
332 struct ieee80211_hw *hw = dev->hw;
333 struct cfg80211_chan_def *chandef = &hw->conf.chandef;
334 struct mt76_channel_state *state;
335 bool offchannel = hw->conf.flags & IEEE80211_CONF_OFFCHANNEL;
336
337 if (dev->drv->update_survey)
338 dev->drv->update_survey(dev);
339
340 dev->chandef = *chandef;
341
342 if (!offchannel)
343 dev->main_chan = chandef->chan;
344
345 if (chandef->chan != dev->main_chan) {
346 state = mt76_channel_state(dev, chandef->chan);
347 memset(state, 0, sizeof(*state));
348 }
349}
350EXPORT_SYMBOL_GPL(mt76_set_channel);
351
352int mt76_get_survey(struct ieee80211_hw *hw, int idx,
353 struct survey_info *survey)
354{
355 struct mt76_dev *dev = hw->priv;
356 struct mt76_sband *sband;
357 struct ieee80211_channel *chan;
358 struct mt76_channel_state *state;
359 int ret = 0;
360
361 if (idx == 0 && dev->drv->update_survey)
362 dev->drv->update_survey(dev);
363
364 sband = &dev->sband_2g;
365 if (idx >= sband->sband.n_channels) {
366 idx -= sband->sband.n_channels;
367 sband = &dev->sband_5g;
368 }
369
370 if (idx >= sband->sband.n_channels)
371 return -ENOENT;
372
373 chan = &sband->sband.channels[idx];
374 state = mt76_channel_state(dev, chan);
375
376 memset(survey, 0, sizeof(*survey));
377 survey->channel = chan;
378 survey->filled = SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY;
379 if (chan == dev->main_chan)
380 survey->filled |= SURVEY_INFO_IN_USE;
381
382 spin_lock_bh(&dev->cc_lock);
383 survey->time = div_u64(state->cc_active, 1000);
384 survey->time_busy = div_u64(state->cc_busy, 1000);
385 spin_unlock_bh(&dev->cc_lock);
386
387 return ret;
388}
389EXPORT_SYMBOL_GPL(mt76_get_survey);
390
30ce7f44
FF
391void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
392 struct ieee80211_key_conf *key)
393{
394 struct ieee80211_key_seq seq;
395 int i;
396
397 wcid->rx_check_pn = false;
398
399 if (!key)
400 return;
401
402 if (key->cipher == WLAN_CIPHER_SUITE_CCMP)
403 wcid->rx_check_pn = true;
404
405 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
406 ieee80211_get_key_rx_seq(key, i, &seq);
407 memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
408 }
409}
410EXPORT_SYMBOL(mt76_wcid_key_setup);
411
9d9d738b 412static struct ieee80211_sta *mt76_rx_convert(struct sk_buff *skb)
4e34249e
FF
413{
414 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
415 struct mt76_rx_status mstat;
416
417 mstat = *((struct mt76_rx_status *) skb->cb);
418 memset(status, 0, sizeof(*status));
419
420 status->flag = mstat.flag;
421 status->freq = mstat.freq;
422 status->enc_flags = mstat.enc_flags;
423 status->encoding = mstat.encoding;
424 status->bw = mstat.bw;
425 status->rate_idx = mstat.rate_idx;
426 status->nss = mstat.nss;
427 status->band = mstat.band;
428 status->signal = mstat.signal;
429 status->chains = mstat.chains;
430
431 BUILD_BUG_ON(sizeof(mstat) > sizeof(skb->cb));
432 BUILD_BUG_ON(sizeof(status->chain_signal) != sizeof(mstat.chain_signal));
433 memcpy(status->chain_signal, mstat.chain_signal, sizeof(mstat.chain_signal));
9c68a57b
FF
434
435 return wcid_to_sta(mstat.wcid);
4e34249e
FF
436}
437
30ce7f44
FF
438static int
439mt76_check_ccmp_pn(struct sk_buff *skb)
440{
441 struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb;
442 struct mt76_wcid *wcid = status->wcid;
443 struct ieee80211_hdr *hdr;
444 int ret;
445
446 if (!(status->flag & RX_FLAG_DECRYPTED))
447 return 0;
448
449 if (!wcid || !wcid->rx_check_pn)
450 return 0;
451
452 if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
453 /*
454 * Validate the first fragment both here and in mac80211
455 * All further fragments will be validated by mac80211 only.
456 */
457 hdr = (struct ieee80211_hdr *) skb->data;
458 if (ieee80211_is_frag(hdr) &&
459 !ieee80211_is_first_frag(hdr->frame_control))
460 return 0;
461 }
462
463 BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0]));
464 ret = memcmp(status->iv, wcid->rx_key_pn[status->tid],
465 sizeof(status->iv));
466 if (ret <= 0)
467 return -EINVAL; /* replay */
468
469 memcpy(wcid->rx_key_pn[status->tid], status->iv, sizeof(status->iv));
470
471 if (status->flag & RX_FLAG_IV_STRIPPED)
472 status->flag |= RX_FLAG_PN_VALIDATED;
473
474 return 0;
475}
476
d71ef286
FF
477static void
478mt76_check_ps(struct mt76_dev *dev, struct sk_buff *skb)
479{
480 struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb;
481 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
482 struct ieee80211_sta *sta;
483 struct mt76_wcid *wcid = status->wcid;
484 bool ps;
485
486 if (!wcid || !wcid->sta)
487 return;
488
489 sta = container_of((void *) wcid, struct ieee80211_sta, drv_priv);
490
491 if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags))
492 return;
493
494 if (ieee80211_is_pspoll(hdr->frame_control)) {
495 ieee80211_sta_pspoll(sta);
496 return;
497 }
498
499 if (ieee80211_has_morefrags(hdr->frame_control) ||
500 !(ieee80211_is_mgmt(hdr->frame_control) ||
501 ieee80211_is_data(hdr->frame_control)))
502 return;
503
504 ps = ieee80211_has_pm(hdr->frame_control);
505
506 if (ps && (ieee80211_is_data_qos(hdr->frame_control) ||
507 ieee80211_is_qos_nullfunc(hdr->frame_control)))
508 ieee80211_sta_uapsd_trigger(sta, status->tid);
509
510 if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps)
511 return;
512
513 if (ps) {
514 set_bit(MT_WCID_FLAG_PS, &wcid->flags);
515 mt76_stop_tx_queues(dev, sta, true);
516 } else {
517 clear_bit(MT_WCID_FLAG_PS, &wcid->flags);
518 }
519
520 ieee80211_sta_ps_transition(sta, ps);
521 dev->drv->sta_ps(dev, sta, ps);
522}
523
9d9d738b
FF
524void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
525 int queue)
17f1de56 526{
9d9d738b 527 struct napi_struct *napi = NULL;
9c68a57b 528 struct ieee80211_sta *sta;
9d9d738b
FF
529 struct sk_buff *skb;
530
531 if (queue >= 0)
532 napi = &dev->napi[queue];
533
534 while ((skb = __skb_dequeue(frames)) != NULL) {
30ce7f44
FF
535 if (mt76_check_ccmp_pn(skb)) {
536 dev_kfree_skb(skb);
537 continue;
538 }
539
9d9d738b
FF
540 sta = mt76_rx_convert(skb);
541 ieee80211_rx_napi(dev->hw, sta, skb, napi);
542 }
543}
544
545void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q)
546{
aee5b8cf 547 struct sk_buff_head frames;
17f1de56
FF
548 struct sk_buff *skb;
549
aee5b8cf
FF
550 __skb_queue_head_init(&frames);
551
d71ef286
FF
552 while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) {
553 mt76_check_ps(dev, skb);
aee5b8cf 554 mt76_rx_aggr_reorder(skb, &frames);
d71ef286 555 }
aee5b8cf 556
9d9d738b 557 mt76_rx_complete(dev, &frames, q);
17f1de56 558}