]>
Commit | Line | Data |
---|---|---|
17f1de56 FF |
1 | /* |
2 | * Copyright (C) 2016 Felix Fietkau <[email protected]> | |
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 | ||
551e1ef4 LB |
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 = __sw_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 | ||
5ebdc3e0 LB |
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 | ||
17f1de56 FF |
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; | |
17f1de56 FF |
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 | ||
a86854d0 | 184 | msband->chan = devm_kcalloc(dev->dev, n_chan, sizeof(*msband->chan), |
17f1de56 FF |
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 | | |
17f1de56 FF |
201 | (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT); |
202 | ||
17f1de56 FF |
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 | ||
551e1ef4 LB |
207 | mt76_init_stream_cap(dev, sband, vht); |
208 | ||
17f1de56 FF |
209 | if (!vht) |
210 | return 0; | |
211 | ||
212 | vht_cap = &sband->vht_cap; | |
213 | vht_cap->vht_supported = true; | |
17f1de56 | 214 | vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC | |
17f1de56 | 215 | IEEE80211_VHT_CAP_RXSTBC_1 | |
49149d3f FF |
216 | IEEE80211_VHT_CAP_SHORT_GI_80 | |
217 | (3 << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT); | |
17f1de56 FF |
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 | ||
a85b590c FF |
271 | struct mt76_dev * |
272 | mt76_alloc_device(unsigned int size, const struct ieee80211_ops *ops) | |
273 | { | |
274 | struct ieee80211_hw *hw; | |
275 | struct mt76_dev *dev; | |
276 | ||
277 | hw = ieee80211_alloc_hw(size, ops); | |
278 | if (!hw) | |
279 | return NULL; | |
280 | ||
281 | dev = hw->priv; | |
282 | dev->hw = hw; | |
283 | spin_lock_init(&dev->rx_lock); | |
284 | spin_lock_init(&dev->lock); | |
285 | spin_lock_init(&dev->cc_lock); | |
108a4861 | 286 | mutex_init(&dev->mutex); |
26e40d4c | 287 | init_waitqueue_head(&dev->tx_wait); |
a85b590c FF |
288 | |
289 | return dev; | |
290 | } | |
291 | EXPORT_SYMBOL_GPL(mt76_alloc_device); | |
292 | ||
17f1de56 FF |
293 | int mt76_register_device(struct mt76_dev *dev, bool vht, |
294 | struct ieee80211_rate *rates, int n_rates) | |
295 | { | |
296 | struct ieee80211_hw *hw = dev->hw; | |
297 | struct wiphy *wiphy = hw->wiphy; | |
298 | int ret; | |
299 | ||
300 | dev_set_drvdata(dev->dev, dev); | |
301 | ||
17f1de56 FF |
302 | INIT_LIST_HEAD(&dev->txwi_cache); |
303 | ||
304 | SET_IEEE80211_DEV(hw, dev->dev); | |
305 | SET_IEEE80211_PERM_ADDR(hw, dev->macaddr); | |
306 | ||
17f1de56 FF |
307 | wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR; |
308 | ||
b37b30af KE |
309 | wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST); |
310 | ||
24114a5f LB |
311 | wiphy->available_antennas_tx = dev->antenna_mask; |
312 | wiphy->available_antennas_rx = dev->antenna_mask; | |
313 | ||
17f1de56 FF |
314 | hw->txq_data_size = sizeof(struct mt76_txq); |
315 | hw->max_tx_fragments = 16; | |
316 | ||
317 | ieee80211_hw_set(hw, SIGNAL_DBM); | |
318 | ieee80211_hw_set(hw, PS_NULLFUNC_STACK); | |
319 | ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING); | |
320 | ieee80211_hw_set(hw, AMPDU_AGGREGATION); | |
321 | ieee80211_hw_set(hw, SUPPORTS_RC_TABLE); | |
322 | ieee80211_hw_set(hw, SUPPORT_FAST_XMIT); | |
323 | ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS); | |
324 | ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU); | |
325 | ieee80211_hw_set(hw, TX_AMSDU); | |
326 | ieee80211_hw_set(hw, TX_FRAG_LIST); | |
327 | ieee80211_hw_set(hw, MFP_CAPABLE); | |
d71ef286 | 328 | ieee80211_hw_set(hw, AP_LINK_PS); |
17f1de56 FF |
329 | |
330 | wiphy->flags |= WIPHY_FLAG_IBSS_RSN; | |
331 | ||
332 | if (dev->cap.has_2ghz) { | |
333 | ret = mt76_init_sband_2g(dev, rates, n_rates); | |
334 | if (ret) | |
335 | return ret; | |
336 | } | |
337 | ||
338 | if (dev->cap.has_5ghz) { | |
339 | ret = mt76_init_sband_5g(dev, rates + 4, n_rates - 4, vht); | |
340 | if (ret) | |
341 | return ret; | |
342 | } | |
343 | ||
344 | wiphy_read_of_freq_limits(dev->hw->wiphy); | |
345 | mt76_check_sband(dev, NL80211_BAND_2GHZ); | |
346 | mt76_check_sband(dev, NL80211_BAND_5GHZ); | |
347 | ||
b374e868 AB |
348 | if (IS_ENABLED(CONFIG_MT76_LEDS)) { |
349 | ret = mt76_led_init(dev); | |
350 | if (ret) | |
351 | return ret; | |
352 | } | |
17f1de56 FF |
353 | |
354 | return ieee80211_register_hw(hw); | |
355 | } | |
356 | EXPORT_SYMBOL_GPL(mt76_register_device); | |
357 | ||
358 | void mt76_unregister_device(struct mt76_dev *dev) | |
359 | { | |
360 | struct ieee80211_hw *hw = dev->hw; | |
361 | ||
362 | ieee80211_unregister_hw(hw); | |
363 | mt76_tx_free(dev); | |
364 | } | |
365 | EXPORT_SYMBOL_GPL(mt76_unregister_device); | |
366 | ||
367 | void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb) | |
368 | { | |
369 | if (!test_bit(MT76_STATE_RUNNING, &dev->state)) { | |
370 | dev_kfree_skb(skb); | |
371 | return; | |
372 | } | |
373 | ||
374 | __skb_queue_tail(&dev->rx_skb[q], skb); | |
375 | } | |
376 | EXPORT_SYMBOL_GPL(mt76_rx); | |
377 | ||
26e40d4c FF |
378 | static bool mt76_has_tx_pending(struct mt76_dev *dev) |
379 | { | |
380 | int i; | |
381 | ||
382 | for (i = 0; i < ARRAY_SIZE(dev->q_tx); i++) { | |
383 | if (dev->q_tx[i].queued) | |
384 | return true; | |
385 | } | |
386 | ||
387 | return false; | |
388 | } | |
389 | ||
17f1de56 FF |
390 | void mt76_set_channel(struct mt76_dev *dev) |
391 | { | |
392 | struct ieee80211_hw *hw = dev->hw; | |
393 | struct cfg80211_chan_def *chandef = &hw->conf.chandef; | |
394 | struct mt76_channel_state *state; | |
395 | bool offchannel = hw->conf.flags & IEEE80211_CONF_OFFCHANNEL; | |
26e40d4c | 396 | int timeout = HZ / 5; |
17f1de56 | 397 | |
89bc67e3 FF |
398 | if (offchannel) |
399 | set_bit(MT76_OFFCHANNEL, &dev->state); | |
400 | else | |
401 | clear_bit(MT76_OFFCHANNEL, &dev->state); | |
402 | ||
26e40d4c FF |
403 | wait_event_timeout(dev->tx_wait, !mt76_has_tx_pending(dev), timeout); |
404 | ||
17f1de56 FF |
405 | if (dev->drv->update_survey) |
406 | dev->drv->update_survey(dev); | |
407 | ||
408 | dev->chandef = *chandef; | |
409 | ||
410 | if (!offchannel) | |
411 | dev->main_chan = chandef->chan; | |
412 | ||
413 | if (chandef->chan != dev->main_chan) { | |
414 | state = mt76_channel_state(dev, chandef->chan); | |
415 | memset(state, 0, sizeof(*state)); | |
416 | } | |
417 | } | |
418 | EXPORT_SYMBOL_GPL(mt76_set_channel); | |
419 | ||
420 | int mt76_get_survey(struct ieee80211_hw *hw, int idx, | |
421 | struct survey_info *survey) | |
422 | { | |
423 | struct mt76_dev *dev = hw->priv; | |
424 | struct mt76_sband *sband; | |
425 | struct ieee80211_channel *chan; | |
426 | struct mt76_channel_state *state; | |
427 | int ret = 0; | |
428 | ||
429 | if (idx == 0 && dev->drv->update_survey) | |
430 | dev->drv->update_survey(dev); | |
431 | ||
432 | sband = &dev->sband_2g; | |
433 | if (idx >= sband->sband.n_channels) { | |
434 | idx -= sband->sband.n_channels; | |
435 | sband = &dev->sband_5g; | |
436 | } | |
437 | ||
438 | if (idx >= sband->sband.n_channels) | |
439 | return -ENOENT; | |
440 | ||
441 | chan = &sband->sband.channels[idx]; | |
442 | state = mt76_channel_state(dev, chan); | |
443 | ||
444 | memset(survey, 0, sizeof(*survey)); | |
445 | survey->channel = chan; | |
446 | survey->filled = SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY; | |
447 | if (chan == dev->main_chan) | |
448 | survey->filled |= SURVEY_INFO_IN_USE; | |
449 | ||
450 | spin_lock_bh(&dev->cc_lock); | |
451 | survey->time = div_u64(state->cc_active, 1000); | |
452 | survey->time_busy = div_u64(state->cc_busy, 1000); | |
453 | spin_unlock_bh(&dev->cc_lock); | |
454 | ||
455 | return ret; | |
456 | } | |
457 | EXPORT_SYMBOL_GPL(mt76_get_survey); | |
458 | ||
30ce7f44 FF |
459 | void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid, |
460 | struct ieee80211_key_conf *key) | |
461 | { | |
462 | struct ieee80211_key_seq seq; | |
463 | int i; | |
464 | ||
465 | wcid->rx_check_pn = false; | |
466 | ||
467 | if (!key) | |
468 | return; | |
469 | ||
470 | if (key->cipher == WLAN_CIPHER_SUITE_CCMP) | |
471 | wcid->rx_check_pn = true; | |
472 | ||
473 | for (i = 0; i < IEEE80211_NUM_TIDS; i++) { | |
474 | ieee80211_get_key_rx_seq(key, i, &seq); | |
475 | memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn)); | |
476 | } | |
477 | } | |
478 | EXPORT_SYMBOL(mt76_wcid_key_setup); | |
479 | ||
82e1dd0f | 480 | struct ieee80211_sta *mt76_rx_convert(struct sk_buff *skb) |
4e34249e FF |
481 | { |
482 | struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); | |
483 | struct mt76_rx_status mstat; | |
484 | ||
485 | mstat = *((struct mt76_rx_status *) skb->cb); | |
486 | memset(status, 0, sizeof(*status)); | |
487 | ||
488 | status->flag = mstat.flag; | |
489 | status->freq = mstat.freq; | |
490 | status->enc_flags = mstat.enc_flags; | |
491 | status->encoding = mstat.encoding; | |
492 | status->bw = mstat.bw; | |
493 | status->rate_idx = mstat.rate_idx; | |
494 | status->nss = mstat.nss; | |
495 | status->band = mstat.band; | |
496 | status->signal = mstat.signal; | |
497 | status->chains = mstat.chains; | |
498 | ||
499 | BUILD_BUG_ON(sizeof(mstat) > sizeof(skb->cb)); | |
500 | BUILD_BUG_ON(sizeof(status->chain_signal) != sizeof(mstat.chain_signal)); | |
501 | memcpy(status->chain_signal, mstat.chain_signal, sizeof(mstat.chain_signal)); | |
9c68a57b FF |
502 | |
503 | return wcid_to_sta(mstat.wcid); | |
4e34249e | 504 | } |
82e1dd0f | 505 | EXPORT_SYMBOL(mt76_rx_convert); |
4e34249e | 506 | |
30ce7f44 FF |
507 | static int |
508 | mt76_check_ccmp_pn(struct sk_buff *skb) | |
509 | { | |
510 | struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb; | |
511 | struct mt76_wcid *wcid = status->wcid; | |
512 | struct ieee80211_hdr *hdr; | |
513 | int ret; | |
514 | ||
515 | if (!(status->flag & RX_FLAG_DECRYPTED)) | |
516 | return 0; | |
517 | ||
518 | if (!wcid || !wcid->rx_check_pn) | |
519 | return 0; | |
520 | ||
521 | if (!(status->flag & RX_FLAG_IV_STRIPPED)) { | |
522 | /* | |
523 | * Validate the first fragment both here and in mac80211 | |
524 | * All further fragments will be validated by mac80211 only. | |
525 | */ | |
526 | hdr = (struct ieee80211_hdr *) skb->data; | |
527 | if (ieee80211_is_frag(hdr) && | |
528 | !ieee80211_is_first_frag(hdr->frame_control)) | |
529 | return 0; | |
530 | } | |
531 | ||
532 | BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0])); | |
533 | ret = memcmp(status->iv, wcid->rx_key_pn[status->tid], | |
534 | sizeof(status->iv)); | |
535 | if (ret <= 0) | |
536 | return -EINVAL; /* replay */ | |
537 | ||
538 | memcpy(wcid->rx_key_pn[status->tid], status->iv, sizeof(status->iv)); | |
539 | ||
540 | if (status->flag & RX_FLAG_IV_STRIPPED) | |
541 | status->flag |= RX_FLAG_PN_VALIDATED; | |
542 | ||
543 | return 0; | |
544 | } | |
545 | ||
d71ef286 FF |
546 | static void |
547 | mt76_check_ps(struct mt76_dev *dev, struct sk_buff *skb) | |
548 | { | |
549 | struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb; | |
550 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; | |
551 | struct ieee80211_sta *sta; | |
552 | struct mt76_wcid *wcid = status->wcid; | |
553 | bool ps; | |
554 | ||
36d91096 FF |
555 | if (ieee80211_is_pspoll(hdr->frame_control) && !wcid) { |
556 | sta = ieee80211_find_sta_by_ifaddr(dev->hw, hdr->addr2, NULL); | |
557 | if (sta) | |
558 | wcid = status->wcid = (struct mt76_wcid *) sta->drv_priv; | |
559 | } | |
560 | ||
d71ef286 FF |
561 | if (!wcid || !wcid->sta) |
562 | return; | |
563 | ||
564 | sta = container_of((void *) wcid, struct ieee80211_sta, drv_priv); | |
565 | ||
566 | if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags)) | |
567 | return; | |
568 | ||
569 | if (ieee80211_is_pspoll(hdr->frame_control)) { | |
570 | ieee80211_sta_pspoll(sta); | |
571 | return; | |
572 | } | |
573 | ||
574 | if (ieee80211_has_morefrags(hdr->frame_control) || | |
575 | !(ieee80211_is_mgmt(hdr->frame_control) || | |
576 | ieee80211_is_data(hdr->frame_control))) | |
577 | return; | |
578 | ||
579 | ps = ieee80211_has_pm(hdr->frame_control); | |
580 | ||
581 | if (ps && (ieee80211_is_data_qos(hdr->frame_control) || | |
582 | ieee80211_is_qos_nullfunc(hdr->frame_control))) | |
583 | ieee80211_sta_uapsd_trigger(sta, status->tid); | |
584 | ||
585 | if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps) | |
586 | return; | |
587 | ||
11b2a25f | 588 | if (ps) |
d71ef286 | 589 | set_bit(MT_WCID_FLAG_PS, &wcid->flags); |
11b2a25f | 590 | else |
d71ef286 | 591 | clear_bit(MT_WCID_FLAG_PS, &wcid->flags); |
d71ef286 | 592 | |
d71ef286 | 593 | dev->drv->sta_ps(dev, sta, ps); |
9f67c277 | 594 | ieee80211_sta_ps_transition(sta, ps); |
d71ef286 FF |
595 | } |
596 | ||
9d9d738b | 597 | void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames, |
81e850ef | 598 | struct napi_struct *napi) |
17f1de56 | 599 | { |
9c68a57b | 600 | struct ieee80211_sta *sta; |
9d9d738b FF |
601 | struct sk_buff *skb; |
602 | ||
c3d7c82a | 603 | spin_lock(&dev->rx_lock); |
9d9d738b | 604 | while ((skb = __skb_dequeue(frames)) != NULL) { |
30ce7f44 FF |
605 | if (mt76_check_ccmp_pn(skb)) { |
606 | dev_kfree_skb(skb); | |
607 | continue; | |
608 | } | |
609 | ||
9d9d738b FF |
610 | sta = mt76_rx_convert(skb); |
611 | ieee80211_rx_napi(dev->hw, sta, skb, napi); | |
612 | } | |
c3d7c82a | 613 | spin_unlock(&dev->rx_lock); |
9d9d738b FF |
614 | } |
615 | ||
81e850ef LB |
616 | void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q, |
617 | struct napi_struct *napi) | |
9d9d738b | 618 | { |
aee5b8cf | 619 | struct sk_buff_head frames; |
17f1de56 FF |
620 | struct sk_buff *skb; |
621 | ||
aee5b8cf FF |
622 | __skb_queue_head_init(&frames); |
623 | ||
d71ef286 FF |
624 | while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) { |
625 | mt76_check_ps(dev, skb); | |
aee5b8cf | 626 | mt76_rx_aggr_reorder(skb, &frames); |
d71ef286 | 627 | } |
aee5b8cf | 628 | |
81e850ef | 629 | mt76_rx_complete(dev, &frames, napi); |
17f1de56 | 630 | } |
81e850ef | 631 | EXPORT_SYMBOL_GPL(mt76_rx_poll_complete); |