2 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
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.
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.
19 #define CHAN2G(_idx, _freq) { \
20 .band = NL80211_BAND_2GHZ, \
21 .center_freq = (_freq), \
26 #define CHAN5G(_idx, _freq) { \
27 .band = NL80211_BAND_5GHZ, \
28 .center_freq = (_freq), \
33 static const struct ieee80211_channel mt76_channels_2ghz[] = {
50 static const struct ieee80211_channel mt76_channels_5ghz[] = {
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 },
93 static int mt76_led_init(struct mt76_dev *dev)
95 struct device_node *np = dev->dev->of_node;
96 struct ieee80211_hw *hw = dev->hw;
99 if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
102 snprintf(dev->led_name, sizeof(dev->led_name),
103 "mt76-%s", wiphy_name(hw->wiphy));
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,
110 ARRAY_SIZE(mt76_tpt_blink));
112 np = of_get_child_by_name(np, "led");
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");
119 return devm_led_classdev_register(dev->dev, &dev->led_cdev);
122 static void mt76_init_stream_cap(struct mt76_dev *dev,
123 struct ieee80211_supported_band *sband,
126 struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
127 int i, nstream = __sw_hweight8(dev->antenna_mask);
128 struct ieee80211_sta_vht_cap *vht_cap;
132 ht_cap->cap |= IEEE80211_HT_CAP_TX_STBC;
134 ht_cap->cap &= ~IEEE80211_HT_CAP_TX_STBC;
136 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
137 ht_cap->mcs.rx_mask[i] = i < nstream ? 0xff : 0;
142 vht_cap = &sband->vht_cap;
144 vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
146 vht_cap->cap &= ~IEEE80211_VHT_CAP_TXSTBC;
148 for (i = 0; i < 8; i++) {
150 mcs_map |= (IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2));
153 (IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2));
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);
159 void mt76_set_stream_caps(struct mt76_dev *dev, bool vht)
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);
166 EXPORT_SYMBOL_GPL(mt76_set_stream_caps);
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)
173 struct ieee80211_supported_band *sband = &msband->sband;
174 struct ieee80211_sta_ht_cap *ht_cap;
175 struct ieee80211_sta_vht_cap *vht_cap;
179 size = n_chan * sizeof(*chan);
180 chanlist = devm_kmemdup(dev->dev, chan, size, GFP_KERNEL);
184 msband->chan = devm_kzalloc(dev->dev, n_chan * sizeof(*msband->chan),
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];
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);
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;
207 mt76_init_stream_cap(dev, sband, vht);
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;
222 mt76_init_sband_2g(struct mt76_dev *dev, struct ieee80211_rate *rates,
225 dev->hw->wiphy->bands[NL80211_BAND_2GHZ] = &dev->sband_2g.sband;
227 return mt76_init_sband(dev, &dev->sband_2g,
229 ARRAY_SIZE(mt76_channels_2ghz),
230 rates, n_rates, false);
234 mt76_init_sband_5g(struct mt76_dev *dev, struct ieee80211_rate *rates,
235 int n_rates, bool vht)
237 dev->hw->wiphy->bands[NL80211_BAND_5GHZ] = &dev->sband_5g.sband;
239 return mt76_init_sband(dev, &dev->sband_5g,
241 ARRAY_SIZE(mt76_channels_5ghz),
242 rates, n_rates, vht);
246 mt76_check_sband(struct mt76_dev *dev, int band)
248 struct ieee80211_supported_band *sband = dev->hw->wiphy->bands[band];
255 for (i = 0; i < sband->n_channels; i++) {
256 if (sband->channels[i].flags & IEEE80211_CHAN_DISABLED)
266 sband->n_channels = 0;
267 dev->hw->wiphy->bands[band] = NULL;
270 int mt76_register_device(struct mt76_dev *dev, bool vht,
271 struct ieee80211_rate *rates, int n_rates)
273 struct ieee80211_hw *hw = dev->hw;
274 struct wiphy *wiphy = hw->wiphy;
277 dev_set_drvdata(dev->dev, dev);
279 spin_lock_init(&dev->lock);
280 spin_lock_init(&dev->cc_lock);
281 INIT_LIST_HEAD(&dev->txwi_cache);
283 SET_IEEE80211_DEV(hw, dev->dev);
284 SET_IEEE80211_PERM_ADDR(hw, dev->macaddr);
286 wiphy->interface_modes =
287 BIT(NL80211_IFTYPE_STATION) |
288 BIT(NL80211_IFTYPE_AP) |
289 #ifdef CONFIG_MAC80211_MESH
290 BIT(NL80211_IFTYPE_MESH_POINT) |
292 BIT(NL80211_IFTYPE_ADHOC);
294 wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
296 wiphy->available_antennas_tx = dev->antenna_mask;
297 wiphy->available_antennas_rx = dev->antenna_mask;
299 hw->txq_data_size = sizeof(struct mt76_txq);
300 hw->max_tx_fragments = 16;
302 ieee80211_hw_set(hw, SIGNAL_DBM);
303 ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
304 ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
305 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
306 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
307 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
308 ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
309 ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
310 ieee80211_hw_set(hw, TX_AMSDU);
311 ieee80211_hw_set(hw, TX_FRAG_LIST);
312 ieee80211_hw_set(hw, MFP_CAPABLE);
313 ieee80211_hw_set(hw, AP_LINK_PS);
315 wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
317 if (dev->cap.has_2ghz) {
318 ret = mt76_init_sband_2g(dev, rates, n_rates);
323 if (dev->cap.has_5ghz) {
324 ret = mt76_init_sband_5g(dev, rates + 4, n_rates - 4, vht);
329 wiphy_read_of_freq_limits(dev->hw->wiphy);
330 mt76_check_sband(dev, NL80211_BAND_2GHZ);
331 mt76_check_sband(dev, NL80211_BAND_5GHZ);
333 ret = mt76_led_init(dev);
337 return ieee80211_register_hw(hw);
339 EXPORT_SYMBOL_GPL(mt76_register_device);
341 void mt76_unregister_device(struct mt76_dev *dev)
343 struct ieee80211_hw *hw = dev->hw;
345 ieee80211_unregister_hw(hw);
348 EXPORT_SYMBOL_GPL(mt76_unregister_device);
350 void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb)
352 if (!test_bit(MT76_STATE_RUNNING, &dev->state)) {
357 __skb_queue_tail(&dev->rx_skb[q], skb);
359 EXPORT_SYMBOL_GPL(mt76_rx);
361 void mt76_set_channel(struct mt76_dev *dev)
363 struct ieee80211_hw *hw = dev->hw;
364 struct cfg80211_chan_def *chandef = &hw->conf.chandef;
365 struct mt76_channel_state *state;
366 bool offchannel = hw->conf.flags & IEEE80211_CONF_OFFCHANNEL;
368 if (dev->drv->update_survey)
369 dev->drv->update_survey(dev);
371 dev->chandef = *chandef;
374 dev->main_chan = chandef->chan;
376 if (chandef->chan != dev->main_chan) {
377 state = mt76_channel_state(dev, chandef->chan);
378 memset(state, 0, sizeof(*state));
381 EXPORT_SYMBOL_GPL(mt76_set_channel);
383 int mt76_get_survey(struct ieee80211_hw *hw, int idx,
384 struct survey_info *survey)
386 struct mt76_dev *dev = hw->priv;
387 struct mt76_sband *sband;
388 struct ieee80211_channel *chan;
389 struct mt76_channel_state *state;
392 if (idx == 0 && dev->drv->update_survey)
393 dev->drv->update_survey(dev);
395 sband = &dev->sband_2g;
396 if (idx >= sband->sband.n_channels) {
397 idx -= sband->sband.n_channels;
398 sband = &dev->sband_5g;
401 if (idx >= sband->sband.n_channels)
404 chan = &sband->sband.channels[idx];
405 state = mt76_channel_state(dev, chan);
407 memset(survey, 0, sizeof(*survey));
408 survey->channel = chan;
409 survey->filled = SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY;
410 if (chan == dev->main_chan)
411 survey->filled |= SURVEY_INFO_IN_USE;
413 spin_lock_bh(&dev->cc_lock);
414 survey->time = div_u64(state->cc_active, 1000);
415 survey->time_busy = div_u64(state->cc_busy, 1000);
416 spin_unlock_bh(&dev->cc_lock);
420 EXPORT_SYMBOL_GPL(mt76_get_survey);
422 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
423 struct ieee80211_key_conf *key)
425 struct ieee80211_key_seq seq;
428 wcid->rx_check_pn = false;
433 if (key->cipher == WLAN_CIPHER_SUITE_CCMP)
434 wcid->rx_check_pn = true;
436 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
437 ieee80211_get_key_rx_seq(key, i, &seq);
438 memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
441 EXPORT_SYMBOL(mt76_wcid_key_setup);
443 static struct ieee80211_sta *mt76_rx_convert(struct sk_buff *skb)
445 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
446 struct mt76_rx_status mstat;
448 mstat = *((struct mt76_rx_status *) skb->cb);
449 memset(status, 0, sizeof(*status));
451 status->flag = mstat.flag;
452 status->freq = mstat.freq;
453 status->enc_flags = mstat.enc_flags;
454 status->encoding = mstat.encoding;
455 status->bw = mstat.bw;
456 status->rate_idx = mstat.rate_idx;
457 status->nss = mstat.nss;
458 status->band = mstat.band;
459 status->signal = mstat.signal;
460 status->chains = mstat.chains;
462 BUILD_BUG_ON(sizeof(mstat) > sizeof(skb->cb));
463 BUILD_BUG_ON(sizeof(status->chain_signal) != sizeof(mstat.chain_signal));
464 memcpy(status->chain_signal, mstat.chain_signal, sizeof(mstat.chain_signal));
466 return wcid_to_sta(mstat.wcid);
470 mt76_check_ccmp_pn(struct sk_buff *skb)
472 struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb;
473 struct mt76_wcid *wcid = status->wcid;
474 struct ieee80211_hdr *hdr;
477 if (!(status->flag & RX_FLAG_DECRYPTED))
480 if (!wcid || !wcid->rx_check_pn)
483 if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
485 * Validate the first fragment both here and in mac80211
486 * All further fragments will be validated by mac80211 only.
488 hdr = (struct ieee80211_hdr *) skb->data;
489 if (ieee80211_is_frag(hdr) &&
490 !ieee80211_is_first_frag(hdr->frame_control))
494 BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0]));
495 ret = memcmp(status->iv, wcid->rx_key_pn[status->tid],
498 return -EINVAL; /* replay */
500 memcpy(wcid->rx_key_pn[status->tid], status->iv, sizeof(status->iv));
502 if (status->flag & RX_FLAG_IV_STRIPPED)
503 status->flag |= RX_FLAG_PN_VALIDATED;
509 mt76_check_ps(struct mt76_dev *dev, struct sk_buff *skb)
511 struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb;
512 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
513 struct ieee80211_sta *sta;
514 struct mt76_wcid *wcid = status->wcid;
517 if (!wcid || !wcid->sta)
520 sta = container_of((void *) wcid, struct ieee80211_sta, drv_priv);
522 if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags))
525 if (ieee80211_is_pspoll(hdr->frame_control)) {
526 ieee80211_sta_pspoll(sta);
530 if (ieee80211_has_morefrags(hdr->frame_control) ||
531 !(ieee80211_is_mgmt(hdr->frame_control) ||
532 ieee80211_is_data(hdr->frame_control)))
535 ps = ieee80211_has_pm(hdr->frame_control);
537 if (ps && (ieee80211_is_data_qos(hdr->frame_control) ||
538 ieee80211_is_qos_nullfunc(hdr->frame_control)))
539 ieee80211_sta_uapsd_trigger(sta, status->tid);
541 if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps)
545 set_bit(MT_WCID_FLAG_PS, &wcid->flags);
547 clear_bit(MT_WCID_FLAG_PS, &wcid->flags);
549 ieee80211_sta_ps_transition(sta, ps);
550 dev->drv->sta_ps(dev, sta, ps);
553 void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
556 struct napi_struct *napi = NULL;
557 struct ieee80211_sta *sta;
561 napi = &dev->napi[queue];
563 while ((skb = __skb_dequeue(frames)) != NULL) {
564 if (mt76_check_ccmp_pn(skb)) {
569 sta = mt76_rx_convert(skb);
570 ieee80211_rx_napi(dev->hw, sta, skb, napi);
574 void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q)
576 struct sk_buff_head frames;
579 __skb_queue_head_init(&frames);
581 while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) {
582 mt76_check_ps(dev, skb);
583 mt76_rx_aggr_reorder(skb, &frames);
586 mt76_rx_complete(dev, &frames, q);