blob: 4a33c6e39ca28be1089aca1275c6e64edda6666d [file] [log] [blame]
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001/*
2 * Atheros CARL9170 driver
3 *
4 * mac80211 interaction code
5 *
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, see
21 * http://www.gnu.org/licenses/.
22 *
23 * This file incorporates work covered by the following copyright and
24 * permission notice:
25 * Copyright (c) 2007-2008 Atheros Communications, Inc.
26 *
27 * Permission to use, copy, modify, and/or distribute this software for any
28 * purpose with or without fee is hereby granted, provided that the above
29 * copyright notice and this permission notice appear in all copies.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
37 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 */
39
40#include <linux/init.h>
41#include <linux/slab.h>
42#include <linux/module.h>
43#include <linux/etherdevice.h>
44#include <linux/random.h>
45#include <net/mac80211.h>
46#include <net/cfg80211.h>
47#include "hw.h"
48#include "carl9170.h"
49#include "cmd.h"
50
Rusty Russelleb939922011-12-19 14:08:01 +000051static bool modparam_nohwcrypt;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +020052module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
53MODULE_PARM_DESC(nohwcrypt, "Disable hardware crypto offload.");
54
55int modparam_noht;
56module_param_named(noht, modparam_noht, int, S_IRUGO);
57MODULE_PARM_DESC(noht, "Disable MPDU aggregation.");
58
59#define RATE(_bitrate, _hw_rate, _txpidx, _flags) { \
60 .bitrate = (_bitrate), \
61 .flags = (_flags), \
62 .hw_value = (_hw_rate) | (_txpidx) << 4, \
63}
64
65struct ieee80211_rate __carl9170_ratetable[] = {
66 RATE(10, 0, 0, 0),
67 RATE(20, 1, 1, IEEE80211_RATE_SHORT_PREAMBLE),
68 RATE(55, 2, 2, IEEE80211_RATE_SHORT_PREAMBLE),
69 RATE(110, 3, 3, IEEE80211_RATE_SHORT_PREAMBLE),
70 RATE(60, 0xb, 0, 0),
71 RATE(90, 0xf, 0, 0),
72 RATE(120, 0xa, 0, 0),
73 RATE(180, 0xe, 0, 0),
74 RATE(240, 0x9, 0, 0),
75 RATE(360, 0xd, 1, 0),
76 RATE(480, 0x8, 2, 0),
77 RATE(540, 0xc, 3, 0),
78};
79#undef RATE
80
81#define carl9170_g_ratetable (__carl9170_ratetable + 0)
82#define carl9170_g_ratetable_size 12
83#define carl9170_a_ratetable (__carl9170_ratetable + 4)
84#define carl9170_a_ratetable_size 8
85
86/*
87 * NB: The hw_value is used as an index into the carl9170_phy_freq_params
88 * array in phy.c so that we don't have to do frequency lookups!
89 */
90#define CHAN(_freq, _idx) { \
91 .center_freq = (_freq), \
92 .hw_value = (_idx), \
93 .max_power = 18, /* XXX */ \
94}
95
96static struct ieee80211_channel carl9170_2ghz_chantable[] = {
97 CHAN(2412, 0),
98 CHAN(2417, 1),
99 CHAN(2422, 2),
100 CHAN(2427, 3),
101 CHAN(2432, 4),
102 CHAN(2437, 5),
103 CHAN(2442, 6),
104 CHAN(2447, 7),
105 CHAN(2452, 8),
106 CHAN(2457, 9),
107 CHAN(2462, 10),
108 CHAN(2467, 11),
109 CHAN(2472, 12),
110 CHAN(2484, 13),
111};
112
113static struct ieee80211_channel carl9170_5ghz_chantable[] = {
114 CHAN(4920, 14),
115 CHAN(4940, 15),
116 CHAN(4960, 16),
117 CHAN(4980, 17),
118 CHAN(5040, 18),
119 CHAN(5060, 19),
120 CHAN(5080, 20),
121 CHAN(5180, 21),
122 CHAN(5200, 22),
123 CHAN(5220, 23),
124 CHAN(5240, 24),
125 CHAN(5260, 25),
126 CHAN(5280, 26),
127 CHAN(5300, 27),
128 CHAN(5320, 28),
129 CHAN(5500, 29),
130 CHAN(5520, 30),
131 CHAN(5540, 31),
132 CHAN(5560, 32),
133 CHAN(5580, 33),
134 CHAN(5600, 34),
135 CHAN(5620, 35),
136 CHAN(5640, 36),
137 CHAN(5660, 37),
138 CHAN(5680, 38),
139 CHAN(5700, 39),
140 CHAN(5745, 40),
141 CHAN(5765, 41),
142 CHAN(5785, 42),
143 CHAN(5805, 43),
144 CHAN(5825, 44),
145 CHAN(5170, 45),
146 CHAN(5190, 46),
147 CHAN(5210, 47),
148 CHAN(5230, 48),
149};
150#undef CHAN
151
152#define CARL9170_HT_CAP \
153{ \
154 .ht_supported = true, \
155 .cap = IEEE80211_HT_CAP_MAX_AMSDU | \
156 IEEE80211_HT_CAP_SUP_WIDTH_20_40 | \
157 IEEE80211_HT_CAP_SGI_40 | \
158 IEEE80211_HT_CAP_DSSSCCK40 | \
159 IEEE80211_HT_CAP_SM_PS, \
160 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, \
161 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, \
162 .mcs = { \
163 .rx_mask = { 0xff, 0xff, 0, 0, 0x1, 0, 0, 0, 0, 0, }, \
164 .rx_highest = cpu_to_le16(300), \
165 .tx_params = IEEE80211_HT_MCS_TX_DEFINED, \
166 }, \
167}
168
169static struct ieee80211_supported_band carl9170_band_2GHz = {
170 .channels = carl9170_2ghz_chantable,
171 .n_channels = ARRAY_SIZE(carl9170_2ghz_chantable),
172 .bitrates = carl9170_g_ratetable,
173 .n_bitrates = carl9170_g_ratetable_size,
174 .ht_cap = CARL9170_HT_CAP,
175};
176
177static struct ieee80211_supported_band carl9170_band_5GHz = {
178 .channels = carl9170_5ghz_chantable,
179 .n_channels = ARRAY_SIZE(carl9170_5ghz_chantable),
180 .bitrates = carl9170_a_ratetable,
181 .n_bitrates = carl9170_a_ratetable_size,
182 .ht_cap = CARL9170_HT_CAP,
183};
184
185static void carl9170_ampdu_gc(struct ar9170 *ar)
186{
187 struct carl9170_sta_tid *tid_info;
188 LIST_HEAD(tid_gc);
189
190 rcu_read_lock();
191 list_for_each_entry_rcu(tid_info, &ar->tx_ampdu_list, list) {
192 spin_lock_bh(&ar->tx_ampdu_list_lock);
193 if (tid_info->state == CARL9170_TID_STATE_SHUTDOWN) {
194 tid_info->state = CARL9170_TID_STATE_KILLED;
195 list_del_rcu(&tid_info->list);
196 ar->tx_ampdu_list_len--;
197 list_add_tail(&tid_info->tmp_list, &tid_gc);
198 }
199 spin_unlock_bh(&ar->tx_ampdu_list_lock);
200
201 }
202 rcu_assign_pointer(ar->tx_ampdu_iter, tid_info);
203 rcu_read_unlock();
204
205 synchronize_rcu();
206
207 while (!list_empty(&tid_gc)) {
208 struct sk_buff *skb;
209 tid_info = list_first_entry(&tid_gc, struct carl9170_sta_tid,
210 tmp_list);
211
212 while ((skb = __skb_dequeue(&tid_info->queue)))
213 carl9170_tx_status(ar, skb, false);
214
215 list_del_init(&tid_info->tmp_list);
216 kfree(tid_info);
217 }
218}
219
220static void carl9170_flush(struct ar9170 *ar, bool drop_queued)
221{
222 if (drop_queued) {
223 int i;
224
225 /*
226 * We can only drop frames which have not been uploaded
227 * to the device yet.
228 */
229
230 for (i = 0; i < ar->hw->queues; i++) {
231 struct sk_buff *skb;
232
Christian Lampartercb139ec2010-09-26 22:49:34 +0200233 while ((skb = skb_dequeue(&ar->tx_pending[i]))) {
234 struct ieee80211_tx_info *info;
235
236 info = IEEE80211_SKB_CB(skb);
237 if (info->flags & IEEE80211_TX_CTL_AMPDU)
238 atomic_dec(&ar->tx_ampdu_upload);
239
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200240 carl9170_tx_status(ar, skb, false);
Christian Lampartercb139ec2010-09-26 22:49:34 +0200241 }
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200242 }
243 }
244
245 /* Wait for all other outstanding frames to timeout. */
246 if (atomic_read(&ar->tx_total_queued))
247 WARN_ON(wait_for_completion_timeout(&ar->tx_flush, HZ) == 0);
248}
249
250static void carl9170_flush_ba(struct ar9170 *ar)
251{
252 struct sk_buff_head free;
253 struct carl9170_sta_tid *tid_info;
254 struct sk_buff *skb;
255
256 __skb_queue_head_init(&free);
257
258 rcu_read_lock();
259 spin_lock_bh(&ar->tx_ampdu_list_lock);
260 list_for_each_entry_rcu(tid_info, &ar->tx_ampdu_list, list) {
261 if (tid_info->state > CARL9170_TID_STATE_SUSPEND) {
262 tid_info->state = CARL9170_TID_STATE_SUSPEND;
263
264 spin_lock(&tid_info->lock);
265 while ((skb = __skb_dequeue(&tid_info->queue)))
266 __skb_queue_tail(&free, skb);
267 spin_unlock(&tid_info->lock);
268 }
269 }
270 spin_unlock_bh(&ar->tx_ampdu_list_lock);
271 rcu_read_unlock();
272
273 while ((skb = __skb_dequeue(&free)))
274 carl9170_tx_status(ar, skb, false);
275}
276
277static void carl9170_zap_queues(struct ar9170 *ar)
278{
279 struct carl9170_vif_info *cvif;
280 unsigned int i;
281
282 carl9170_ampdu_gc(ar);
283
284 carl9170_flush_ba(ar);
285 carl9170_flush(ar, true);
286
287 for (i = 0; i < ar->hw->queues; i++) {
288 spin_lock_bh(&ar->tx_status[i].lock);
289 while (!skb_queue_empty(&ar->tx_status[i])) {
290 struct sk_buff *skb;
291
292 skb = skb_peek(&ar->tx_status[i]);
293 carl9170_tx_get_skb(skb);
294 spin_unlock_bh(&ar->tx_status[i].lock);
295 carl9170_tx_drop(ar, skb);
296 spin_lock_bh(&ar->tx_status[i].lock);
297 carl9170_tx_put_skb(skb);
298 }
299 spin_unlock_bh(&ar->tx_status[i].lock);
300 }
301
302 BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_SOFT < 1);
303 BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_HARD < CARL9170_NUM_TX_LIMIT_SOFT);
304 BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_HARD >= CARL9170_BAW_BITS);
305
306 /* reinitialize queues statistics */
307 memset(&ar->tx_stats, 0, sizeof(ar->tx_stats));
308 for (i = 0; i < ar->hw->queues; i++)
309 ar->tx_stats[i].limit = CARL9170_NUM_TX_LIMIT_HARD;
310
311 for (i = 0; i < DIV_ROUND_UP(ar->fw.mem_blocks, BITS_PER_LONG); i++)
312 ar->mem_bitmap[i] = 0;
313
314 rcu_read_lock();
315 list_for_each_entry_rcu(cvif, &ar->vif_list, list) {
316 spin_lock_bh(&ar->beacon_lock);
317 dev_kfree_skb_any(cvif->beacon);
318 cvif->beacon = NULL;
319 spin_unlock_bh(&ar->beacon_lock);
320 }
321 rcu_read_unlock();
322
323 atomic_set(&ar->tx_ampdu_upload, 0);
324 atomic_set(&ar->tx_ampdu_scheduler, 0);
325 atomic_set(&ar->tx_total_pending, 0);
326 atomic_set(&ar->tx_total_queued, 0);
327 atomic_set(&ar->mem_free_blocks, ar->fw.mem_blocks);
328}
329
330#define CARL9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop) \
331do { \
332 queue.aifs = ai_fs; \
333 queue.cw_min = cwmin; \
334 queue.cw_max = cwmax; \
335 queue.txop = _txop; \
336} while (0)
337
338static int carl9170_op_start(struct ieee80211_hw *hw)
339{
340 struct ar9170 *ar = hw->priv;
341 int err, i;
342
343 mutex_lock(&ar->mutex);
344
345 carl9170_zap_queues(ar);
346
347 /* reset QoS defaults */
Christian Lamparterc2a79652011-07-01 15:28:22 +0200348 CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_VO], 2, 3, 7, 47);
349 CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_VI], 2, 7, 15, 94);
350 CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_BE], 3, 15, 1023, 0);
351 CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_BK], 7, 15, 1023, 0);
352 CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_SPECIAL], 2, 3, 7, 0);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200353
354 ar->current_factor = ar->current_density = -1;
355 /* "The first key is unique." */
356 ar->usedkeys = 1;
357 ar->filter_state = 0;
358 ar->ps.last_action = jiffies;
359 ar->ps.last_slept = jiffies;
360 ar->erp_mode = CARL9170_ERP_AUTO;
Christian Lamparter7a5c7302012-12-17 16:30:05 +0100361
362 /* Set "disable hw crypto offload" whenever the module parameter
363 * nohwcrypt is true or if the firmware does not support it.
364 */
365 ar->disable_offload = modparam_nohwcrypt |
366 ar->fw.disable_offload_fw;
367 ar->rx_software_decryption = ar->disable_offload;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200368
369 for (i = 0; i < ar->hw->queues; i++) {
370 ar->queue_stop_timeout[i] = jiffies;
371 ar->max_queue_stop_timeout[i] = 0;
372 }
373
374 atomic_set(&ar->mem_allocs, 0);
375
376 err = carl9170_usb_open(ar);
377 if (err)
378 goto out;
379
380 err = carl9170_init_mac(ar);
381 if (err)
382 goto out;
383
384 err = carl9170_set_qos(ar);
385 if (err)
386 goto out;
387
Christian Lamparter5c895692010-09-28 23:00:59 +0200388 if (ar->fw.rx_filter) {
389 err = carl9170_rx_filter(ar, CARL9170_RX_FILTER_OTHER_RA |
390 CARL9170_RX_FILTER_CTL_OTHER | CARL9170_RX_FILTER_BAD);
391 if (err)
392 goto out;
393 }
394
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200395 err = carl9170_write_reg(ar, AR9170_MAC_REG_DMA_TRIGGER,
396 AR9170_DMA_TRIGGER_RXQ);
397 if (err)
398 goto out;
399
400 /* Clear key-cache */
401 for (i = 0; i < AR9170_CAM_MAX_USER + 4; i++) {
402 err = carl9170_upload_key(ar, i, NULL, AR9170_ENC_ALG_NONE,
403 0, NULL, 0);
404 if (err)
405 goto out;
406
407 err = carl9170_upload_key(ar, i, NULL, AR9170_ENC_ALG_NONE,
408 1, NULL, 0);
409 if (err)
410 goto out;
411
412 if (i < AR9170_CAM_MAX_USER) {
413 err = carl9170_disable_key(ar, i);
414 if (err)
415 goto out;
416 }
417 }
418
419 carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STARTED);
420
Christian Lamparteracf17712011-08-15 19:50:48 +0200421 ieee80211_queue_delayed_work(ar->hw, &ar->stat_work,
422 round_jiffies(msecs_to_jiffies(CARL9170_STAT_WORK)));
423
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200424 ieee80211_wake_queues(ar->hw);
425 err = 0;
426
427out:
428 mutex_unlock(&ar->mutex);
429 return err;
430}
431
432static void carl9170_cancel_worker(struct ar9170 *ar)
433{
Christian Lamparteracf17712011-08-15 19:50:48 +0200434 cancel_delayed_work_sync(&ar->stat_work);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200435 cancel_delayed_work_sync(&ar->tx_janitor);
436#ifdef CONFIG_CARL9170_LEDS
437 cancel_delayed_work_sync(&ar->led_work);
438#endif /* CONFIG_CARL9170_LEDS */
439 cancel_work_sync(&ar->ps_work);
Christian Lampartere4a668c2010-10-29 23:26:13 +0200440 cancel_work_sync(&ar->ping_work);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200441 cancel_work_sync(&ar->ampdu_work);
442}
443
444static void carl9170_op_stop(struct ieee80211_hw *hw)
445{
446 struct ar9170 *ar = hw->priv;
447
448 carl9170_set_state_when(ar, CARL9170_STARTED, CARL9170_IDLE);
449
450 ieee80211_stop_queues(ar->hw);
451
452 mutex_lock(&ar->mutex);
453 if (IS_ACCEPTING_CMD(ar)) {
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000454 RCU_INIT_POINTER(ar->beacon_iter, NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200455
456 carl9170_led_set_state(ar, 0);
457
458 /* stop DMA */
459 carl9170_write_reg(ar, AR9170_MAC_REG_DMA_TRIGGER, 0);
460 carl9170_usb_stop(ar);
461 }
462
463 carl9170_zap_queues(ar);
464 mutex_unlock(&ar->mutex);
465
466 carl9170_cancel_worker(ar);
467}
468
469static void carl9170_restart_work(struct work_struct *work)
470{
471 struct ar9170 *ar = container_of(work, struct ar9170,
472 restart_work);
Ronald Wahl5bcbc3f2012-10-08 14:17:07 +0200473 int err = -EIO;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200474
475 ar->usedkeys = 0;
476 ar->filter_state = 0;
477 carl9170_cancel_worker(ar);
478
479 mutex_lock(&ar->mutex);
Ronald Wahl5bcbc3f2012-10-08 14:17:07 +0200480 if (!ar->force_usb_reset) {
481 err = carl9170_usb_restart(ar);
482 if (net_ratelimit()) {
483 if (err)
484 dev_err(&ar->udev->dev, "Failed to restart device (%d).\n", err);
485 else
486 dev_info(&ar->udev->dev, "device restarted successfully.\n");
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200487 }
488 }
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200489 carl9170_zap_queues(ar);
490 mutex_unlock(&ar->mutex);
Ronald Wahl5bcbc3f2012-10-08 14:17:07 +0200491
492 if (!err && !ar->force_usb_reset) {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200493 ar->restart_counter++;
494 atomic_set(&ar->pending_restarts, 0);
495
496 ieee80211_restart_hw(ar->hw);
497 } else {
498 /*
499 * The reset was unsuccessful and the device seems to
500 * be dead. But there's still one option: a low-level
501 * usb subsystem reset...
502 */
503
504 carl9170_usb_reset(ar);
505 }
506}
507
508void carl9170_restart(struct ar9170 *ar, const enum carl9170_restart_reasons r)
509{
510 carl9170_set_state_when(ar, CARL9170_STARTED, CARL9170_IDLE);
511
512 /*
513 * Sometimes, an error can trigger several different reset events.
514 * By ignoring these *surplus* reset events, the device won't be
515 * killed again, right after it has recovered.
516 */
517 if (atomic_inc_return(&ar->pending_restarts) > 1) {
518 dev_dbg(&ar->udev->dev, "ignoring restart (%d)\n", r);
519 return;
520 }
521
522 ieee80211_stop_queues(ar->hw);
523
524 dev_err(&ar->udev->dev, "restart device (%d)\n", r);
525
526 if (!WARN_ON(r == CARL9170_RR_NO_REASON) ||
527 !WARN_ON(r >= __CARL9170_RR_LAST))
528 ar->last_reason = r;
529
530 if (!ar->registered)
531 return;
532
Ronald Wahl5bcbc3f2012-10-08 14:17:07 +0200533 if (!IS_ACCEPTING_CMD(ar) || ar->needs_full_reset)
534 ar->force_usb_reset = true;
535
536 ieee80211_queue_work(ar->hw, &ar->restart_work);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200537
538 /*
539 * At this point, the device instance might have vanished/disabled.
540 * So, don't put any code which access the ar9170 struct
541 * without proper protection.
542 */
543}
544
Christian Lampartere4a668c2010-10-29 23:26:13 +0200545static void carl9170_ping_work(struct work_struct *work)
546{
547 struct ar9170 *ar = container_of(work, struct ar9170, ping_work);
548 int err;
549
550 if (!IS_STARTED(ar))
551 return;
552
553 mutex_lock(&ar->mutex);
554 err = carl9170_echo_test(ar, 0xdeadbeef);
555 if (err)
556 carl9170_restart(ar, CARL9170_RR_UNRESPONSIVE_DEVICE);
557 mutex_unlock(&ar->mutex);
558}
559
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200560static int carl9170_init_interface(struct ar9170 *ar,
561 struct ieee80211_vif *vif)
562{
563 struct ath_common *common = &ar->common;
564 int err;
565
566 if (!vif) {
567 WARN_ON_ONCE(IS_STARTED(ar));
568 return 0;
569 }
570
571 memcpy(common->macaddr, vif->addr, ETH_ALEN);
572
Christian Lamparter7a5c7302012-12-17 16:30:05 +0100573 /* We have to fall back to software crypto, whenever
574 * the user choose to participates in an IBSS. HW
575 * offload for IBSS RSN is not supported by this driver.
576 *
577 * NOTE: If the previous main interface has already
578 * disabled hw crypto offload, we have to keep this
579 * previous disable_offload setting as it was.
580 * Altough ideally, we should notify mac80211 and tell
581 * it to forget about any HW crypto offload for now.
582 */
583 ar->disable_offload |= ((vif->type != NL80211_IFTYPE_STATION) &&
584 (vif->type != NL80211_IFTYPE_AP));
585
Christian Lamparter55fa6452012-12-17 17:05:55 +0100586 /* While the driver supports HW offload in a single
587 * P2P client configuration, it doesn't support HW
588 * offload in the favourit, concurrent P2P GO+CLIENT
589 * configuration. Hence, HW offload will always be
590 * disabled for P2P.
591 */
592 ar->disable_offload |= vif->p2p;
593
Christian Lamparter7a5c7302012-12-17 16:30:05 +0100594 ar->rx_software_decryption = ar->disable_offload;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200595
596 err = carl9170_set_operating_mode(ar);
597 return err;
598}
599
600static int carl9170_op_add_interface(struct ieee80211_hw *hw,
601 struct ieee80211_vif *vif)
602{
603 struct carl9170_vif_info *vif_priv = (void *) vif->drv_priv;
Christian Lamparter7f878b02012-12-16 01:41:37 +0100604 struct ieee80211_vif *main_vif, *old_main = NULL;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200605 struct ar9170 *ar = hw->priv;
606 int vif_id = -1, err = 0;
607
608 mutex_lock(&ar->mutex);
609 rcu_read_lock();
610 if (vif_priv->active) {
611 /*
612 * Skip the interface structure initialization,
613 * if the vif survived the _restart call.
614 */
615 vif_id = vif_priv->id;
616 vif_priv->enable_beacon = false;
617
618 spin_lock_bh(&ar->beacon_lock);
619 dev_kfree_skb_any(vif_priv->beacon);
620 vif_priv->beacon = NULL;
621 spin_unlock_bh(&ar->beacon_lock);
622
623 goto init;
624 }
625
Christian Lamparter7f878b02012-12-16 01:41:37 +0100626 /* Because the AR9170 HW's MAC doesn't provide full support for
627 * multiple, independent interfaces [of different operation modes].
628 * We have to select ONE main interface [main mode of HW], but we
629 * can have multiple slaves [AKA: entry in the ACK-table].
630 *
631 * The first (from HEAD/TOP) interface in the ar->vif_list is
632 * always the main intf. All following intfs in this list
633 * are considered to be slave intfs.
634 */
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200635 main_vif = carl9170_get_main_vif(ar);
636
637 if (main_vif) {
638 switch (main_vif->type) {
639 case NL80211_IFTYPE_STATION:
640 if (vif->type == NL80211_IFTYPE_STATION)
641 break;
642
Christian Lamparter7f878b02012-12-16 01:41:37 +0100643 /* P2P GO [master] use-case
644 * Because the P2P GO station is selected dynamically
645 * by all participating peers of a WIFI Direct network,
646 * the driver has be able to change the main interface
647 * operating mode on the fly.
648 */
649 if (main_vif->p2p && vif->p2p &&
650 vif->type == NL80211_IFTYPE_AP) {
651 old_main = main_vif;
652 break;
653 }
654
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200655 err = -EBUSY;
656 rcu_read_unlock();
657
658 goto unlock;
659
Javier Lopezda93c262012-07-27 11:27:25 -0700660 case NL80211_IFTYPE_MESH_POINT:
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200661 case NL80211_IFTYPE_AP:
662 if ((vif->type == NL80211_IFTYPE_STATION) ||
663 (vif->type == NL80211_IFTYPE_WDS) ||
Javier Lopezda93c262012-07-27 11:27:25 -0700664 (vif->type == NL80211_IFTYPE_AP) ||
665 (vif->type == NL80211_IFTYPE_MESH_POINT))
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200666 break;
667
668 err = -EBUSY;
669 rcu_read_unlock();
670 goto unlock;
671
672 default:
673 rcu_read_unlock();
674 goto unlock;
675 }
676 }
677
678 vif_id = bitmap_find_free_region(&ar->vif_bitmap, ar->fw.vif_num, 0);
679
680 if (vif_id < 0) {
681 rcu_read_unlock();
682
683 err = -ENOSPC;
684 goto unlock;
685 }
686
687 BUG_ON(ar->vif_priv[vif_id].id != vif_id);
688
689 vif_priv->active = true;
690 vif_priv->id = vif_id;
691 vif_priv->enable_beacon = false;
692 ar->vifs++;
Christian Lamparter7f878b02012-12-16 01:41:37 +0100693 if (old_main) {
694 /* We end up in here, if the main interface is being replaced.
695 * Put the new main interface at the HEAD of the list and the
696 * previous inteface will automatically become second in line.
697 */
698 list_add_rcu(&vif_priv->list, &ar->vif_list);
699 } else {
700 /* Add new inteface. If the list is empty, it will become the
701 * main inteface, otherwise it will be slave.
702 */
703 list_add_tail_rcu(&vif_priv->list, &ar->vif_list);
704 }
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200705 rcu_assign_pointer(ar->vif_priv[vif_id].vif, vif);
706
707init:
Christian Lamparter7f878b02012-12-16 01:41:37 +0100708 main_vif = carl9170_get_main_vif(ar);
709
710 if (main_vif == vif) {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200711 rcu_assign_pointer(ar->beacon_iter, vif_priv);
712 rcu_read_unlock();
713
Christian Lamparter7f878b02012-12-16 01:41:37 +0100714 if (old_main) {
715 struct carl9170_vif_info *old_main_priv =
716 (void *) old_main->drv_priv;
717 /* downgrade old main intf to slave intf.
718 * NOTE: We are no longer under rcu_read_lock.
719 * But we are still holding ar->mutex, so the
720 * vif data [id, addr] is safe.
721 */
722 err = carl9170_mod_virtual_mac(ar, old_main_priv->id,
723 old_main->addr);
724 if (err)
725 goto unlock;
726 }
727
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200728 err = carl9170_init_interface(ar, vif);
729 if (err)
730 goto unlock;
731 } else {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200732 rcu_read_unlock();
Christian Lamparterdafeac32010-10-23 15:02:02 +0200733 err = carl9170_mod_virtual_mac(ar, vif_id, vif->addr);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200734
735 if (err)
736 goto unlock;
737 }
738
Christian Lamparteraa324522011-01-23 00:18:28 +0100739 if (ar->fw.tx_seq_table) {
740 err = carl9170_write_reg(ar, ar->fw.tx_seq_table + vif_id * 4,
741 0);
742 if (err)
743 goto unlock;
744 }
745
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200746unlock:
Christian Lamparterb3974922010-11-20 13:15:27 +0100747 if (err && (vif_id >= 0)) {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200748 vif_priv->active = false;
749 bitmap_release_region(&ar->vif_bitmap, vif_id, 0);
750 ar->vifs--;
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000751 RCU_INIT_POINTER(ar->vif_priv[vif_id].vif, NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200752 list_del_rcu(&vif_priv->list);
753 mutex_unlock(&ar->mutex);
754 synchronize_rcu();
755 } else {
756 if (ar->vifs > 1)
757 ar->ps.off_override |= PS_OFF_VIF;
758
759 mutex_unlock(&ar->mutex);
760 }
761
762 return err;
763}
764
765static void carl9170_op_remove_interface(struct ieee80211_hw *hw,
766 struct ieee80211_vif *vif)
767{
768 struct carl9170_vif_info *vif_priv = (void *) vif->drv_priv;
769 struct ieee80211_vif *main_vif;
770 struct ar9170 *ar = hw->priv;
771 unsigned int id;
772
773 mutex_lock(&ar->mutex);
774
775 if (WARN_ON_ONCE(!vif_priv->active))
776 goto unlock;
777
778 ar->vifs--;
779
780 rcu_read_lock();
781 main_vif = carl9170_get_main_vif(ar);
782
783 id = vif_priv->id;
784
785 vif_priv->active = false;
786 WARN_ON(vif_priv->enable_beacon);
787 vif_priv->enable_beacon = false;
788 list_del_rcu(&vif_priv->list);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000789 RCU_INIT_POINTER(ar->vif_priv[id].vif, NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200790
791 if (vif == main_vif) {
792 rcu_read_unlock();
793
794 if (ar->vifs) {
795 WARN_ON(carl9170_init_interface(ar,
796 carl9170_get_main_vif(ar)));
797 } else {
798 carl9170_set_operating_mode(ar);
799 }
800 } else {
801 rcu_read_unlock();
802
803 WARN_ON(carl9170_mod_virtual_mac(ar, id, NULL));
804 }
805
806 carl9170_update_beacon(ar, false);
807 carl9170_flush_cab(ar, id);
808
809 spin_lock_bh(&ar->beacon_lock);
810 dev_kfree_skb_any(vif_priv->beacon);
811 vif_priv->beacon = NULL;
812 spin_unlock_bh(&ar->beacon_lock);
813
814 bitmap_release_region(&ar->vif_bitmap, id, 0);
815
816 carl9170_set_beacon_timers(ar);
817
818 if (ar->vifs == 1)
819 ar->ps.off_override &= ~PS_OFF_VIF;
820
821unlock:
822 mutex_unlock(&ar->mutex);
823
824 synchronize_rcu();
825}
826
827void carl9170_ps_check(struct ar9170 *ar)
828{
829 ieee80211_queue_work(ar->hw, &ar->ps_work);
830}
831
832/* caller must hold ar->mutex */
833static int carl9170_ps_update(struct ar9170 *ar)
834{
835 bool ps = false;
836 int err = 0;
837
838 if (!ar->ps.off_override)
839 ps = (ar->hw->conf.flags & IEEE80211_CONF_PS);
840
841 if (ps != ar->ps.state) {
842 err = carl9170_powersave(ar, ps);
843 if (err)
844 return err;
845
846 if (ar->ps.state && !ps) {
847 ar->ps.sleep_ms = jiffies_to_msecs(jiffies -
848 ar->ps.last_action);
849 }
850
851 if (ps)
852 ar->ps.last_slept = jiffies;
853
854 ar->ps.last_action = jiffies;
855 ar->ps.state = ps;
856 }
857
858 return 0;
859}
860
861static void carl9170_ps_work(struct work_struct *work)
862{
863 struct ar9170 *ar = container_of(work, struct ar9170,
864 ps_work);
865 mutex_lock(&ar->mutex);
866 if (IS_STARTED(ar))
867 WARN_ON_ONCE(carl9170_ps_update(ar) != 0);
868 mutex_unlock(&ar->mutex);
869}
870
Christian Lamparteracf17712011-08-15 19:50:48 +0200871static int carl9170_update_survey(struct ar9170 *ar, bool flush, bool noise)
872{
873 int err;
874
875 if (noise) {
876 err = carl9170_get_noisefloor(ar);
877 if (err)
878 return err;
879 }
880
881 if (ar->fw.hw_counters) {
882 err = carl9170_collect_tally(ar);
883 if (err)
884 return err;
885 }
886
887 if (flush)
888 memset(&ar->tally, 0, sizeof(ar->tally));
889
890 return 0;
891}
892
893static void carl9170_stat_work(struct work_struct *work)
894{
895 struct ar9170 *ar = container_of(work, struct ar9170, stat_work.work);
896 int err;
897
898 mutex_lock(&ar->mutex);
899 err = carl9170_update_survey(ar, false, true);
900 mutex_unlock(&ar->mutex);
901
902 if (err)
903 return;
904
905 ieee80211_queue_delayed_work(ar->hw, &ar->stat_work,
906 round_jiffies(msecs_to_jiffies(CARL9170_STAT_WORK)));
907}
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200908
909static int carl9170_op_config(struct ieee80211_hw *hw, u32 changed)
910{
911 struct ar9170 *ar = hw->priv;
912 int err = 0;
913
914 mutex_lock(&ar->mutex);
915 if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
916 /* TODO */
917 err = 0;
918 }
919
920 if (changed & IEEE80211_CONF_CHANGE_PS) {
921 err = carl9170_ps_update(ar);
922 if (err)
923 goto out;
924 }
925
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200926 if (changed & IEEE80211_CONF_CHANGE_SMPS) {
927 /* TODO */
928 err = 0;
929 }
930
931 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100932 enum nl80211_channel_type channel_type =
933 cfg80211_get_chandef_type(&hw->conf.chandef);
934
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200935 /* adjust slot time for 5 GHz */
936 err = carl9170_set_slot_time(ar);
937 if (err)
938 goto out;
939
Christian Lamparteracf17712011-08-15 19:50:48 +0200940 err = carl9170_update_survey(ar, true, false);
941 if (err)
942 goto out;
943
Karl Beldan675a0b02013-03-25 16:26:57 +0100944 err = carl9170_set_channel(ar, hw->conf.chandef.chan,
John W. Linville655d8e22013-04-10 14:09:54 -0400945 channel_type);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200946 if (err)
947 goto out;
948
Christian Lamparteracf17712011-08-15 19:50:48 +0200949 err = carl9170_update_survey(ar, false, true);
950 if (err)
951 goto out;
952
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200953 err = carl9170_set_dyn_sifs_ack(ar);
954 if (err)
955 goto out;
956
957 err = carl9170_set_rts_cts_rate(ar);
958 if (err)
959 goto out;
960 }
961
Christian Lamparter67e43de2012-01-21 16:59:10 +0100962 if (changed & IEEE80211_CONF_CHANGE_POWER) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100963 err = carl9170_set_mac_tpc(ar, ar->hw->conf.chandef.chan);
Christian Lamparter67e43de2012-01-21 16:59:10 +0100964 if (err)
965 goto out;
966 }
967
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200968out:
969 mutex_unlock(&ar->mutex);
970 return err;
971}
972
973static u64 carl9170_op_prepare_multicast(struct ieee80211_hw *hw,
974 struct netdev_hw_addr_list *mc_list)
975{
976 struct netdev_hw_addr *ha;
977 u64 mchash;
978
979 /* always get broadcast frames */
980 mchash = 1ULL << (0xff >> 2);
981
982 netdev_hw_addr_list_for_each(ha, mc_list)
983 mchash |= 1ULL << (ha->addr[5] >> 2);
984
985 return mchash;
986}
987
988static void carl9170_op_configure_filter(struct ieee80211_hw *hw,
989 unsigned int changed_flags,
990 unsigned int *new_flags,
991 u64 multicast)
992{
993 struct ar9170 *ar = hw->priv;
994
995 /* mask supported flags */
Christian Lamparter5c895692010-09-28 23:00:59 +0200996 *new_flags &= FIF_ALLMULTI | ar->rx_filter_caps;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200997
998 if (!IS_ACCEPTING_CMD(ar))
999 return;
1000
1001 mutex_lock(&ar->mutex);
1002
1003 ar->filter_state = *new_flags;
1004 /*
1005 * We can support more by setting the sniffer bit and
1006 * then checking the error flags, later.
1007 */
1008
Nicolas Cavallari8f7f3b22011-05-04 15:26:52 +02001009 if (*new_flags & FIF_ALLMULTI)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001010 multicast = ~0ULL;
1011
1012 if (multicast != ar->cur_mc_hash)
1013 WARN_ON(carl9170_update_multicast(ar, multicast));
1014
1015 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
1016 ar->sniffer_enabled = !!(*new_flags &
1017 (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS));
1018
1019 WARN_ON(carl9170_set_operating_mode(ar));
1020 }
1021
Christian Lamparter5c895692010-09-28 23:00:59 +02001022 if (ar->fw.rx_filter && changed_flags & ar->rx_filter_caps) {
1023 u32 rx_filter = 0;
1024
Christian Lamparterc9122c02012-07-07 21:13:59 +02001025 if (!ar->fw.ba_filter)
1026 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
1027
Christian Lamparter5c895692010-09-28 23:00:59 +02001028 if (!(*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL)))
1029 rx_filter |= CARL9170_RX_FILTER_BAD;
1030
1031 if (!(*new_flags & FIF_CONTROL))
1032 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
1033
1034 if (!(*new_flags & FIF_PSPOLL))
1035 rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
1036
1037 if (!(*new_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))) {
1038 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
1039 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
1040 }
1041
1042 WARN_ON(carl9170_rx_filter(ar, rx_filter));
1043 }
1044
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001045 mutex_unlock(&ar->mutex);
1046}
1047
1048
1049static void carl9170_op_bss_info_changed(struct ieee80211_hw *hw,
1050 struct ieee80211_vif *vif,
1051 struct ieee80211_bss_conf *bss_conf,
1052 u32 changed)
1053{
1054 struct ar9170 *ar = hw->priv;
1055 struct ath_common *common = &ar->common;
1056 int err = 0;
1057 struct carl9170_vif_info *vif_priv;
1058 struct ieee80211_vif *main_vif;
1059
1060 mutex_lock(&ar->mutex);
1061 vif_priv = (void *) vif->drv_priv;
1062 main_vif = carl9170_get_main_vif(ar);
1063 if (WARN_ON(!main_vif))
1064 goto out;
1065
1066 if (changed & BSS_CHANGED_BEACON_ENABLED) {
1067 struct carl9170_vif_info *iter;
1068 int i = 0;
1069
1070 vif_priv->enable_beacon = bss_conf->enable_beacon;
1071 rcu_read_lock();
1072 list_for_each_entry_rcu(iter, &ar->vif_list, list) {
1073 if (iter->active && iter->enable_beacon)
1074 i++;
1075
1076 }
1077 rcu_read_unlock();
1078
1079 ar->beacon_enabled = i;
1080 }
1081
1082 if (changed & BSS_CHANGED_BEACON) {
1083 err = carl9170_update_beacon(ar, false);
1084 if (err)
1085 goto out;
1086 }
1087
1088 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON |
1089 BSS_CHANGED_BEACON_INT)) {
1090
1091 if (main_vif != vif) {
1092 bss_conf->beacon_int = main_vif->bss_conf.beacon_int;
1093 bss_conf->dtim_period = main_vif->bss_conf.dtim_period;
1094 }
1095
1096 /*
1097 * Therefore a hard limit for the broadcast traffic should
1098 * prevent false alarms.
1099 */
1100 if (vif->type != NL80211_IFTYPE_STATION &&
1101 (bss_conf->beacon_int * bss_conf->dtim_period >=
1102 (CARL9170_QUEUE_STUCK_TIMEOUT / 2))) {
1103 err = -EINVAL;
1104 goto out;
1105 }
1106
1107 err = carl9170_set_beacon_timers(ar);
1108 if (err)
1109 goto out;
1110 }
1111
1112 if (changed & BSS_CHANGED_HT) {
1113 /* TODO */
1114 err = 0;
1115 if (err)
1116 goto out;
1117 }
1118
1119 if (main_vif != vif)
1120 goto out;
1121
1122 /*
1123 * The following settings can only be changed by the
1124 * master interface.
1125 */
1126
1127 if (changed & BSS_CHANGED_BSSID) {
1128 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1129 err = carl9170_set_operating_mode(ar);
1130 if (err)
1131 goto out;
1132 }
1133
1134 if (changed & BSS_CHANGED_ASSOC) {
1135 ar->common.curaid = bss_conf->aid;
1136 err = carl9170_set_beacon_timers(ar);
1137 if (err)
1138 goto out;
1139 }
1140
1141 if (changed & BSS_CHANGED_ERP_SLOT) {
1142 err = carl9170_set_slot_time(ar);
1143 if (err)
1144 goto out;
1145 }
1146
1147 if (changed & BSS_CHANGED_BASIC_RATES) {
1148 err = carl9170_set_mac_rates(ar);
1149 if (err)
1150 goto out;
1151 }
1152
1153out:
1154 WARN_ON_ONCE(err && IS_STARTED(ar));
1155 mutex_unlock(&ar->mutex);
1156}
1157
Eliad Peller37a41b42011-09-21 14:06:11 +03001158static u64 carl9170_op_get_tsf(struct ieee80211_hw *hw,
1159 struct ieee80211_vif *vif)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001160{
1161 struct ar9170 *ar = hw->priv;
1162 struct carl9170_tsf_rsp tsf;
1163 int err;
1164
1165 mutex_lock(&ar->mutex);
1166 err = carl9170_exec_cmd(ar, CARL9170_CMD_READ_TSF,
1167 0, NULL, sizeof(tsf), &tsf);
1168 mutex_unlock(&ar->mutex);
1169 if (WARN_ON(err))
1170 return 0;
1171
1172 return le64_to_cpu(tsf.tsf_64);
1173}
1174
1175static int carl9170_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1176 struct ieee80211_vif *vif,
1177 struct ieee80211_sta *sta,
1178 struct ieee80211_key_conf *key)
1179{
1180 struct ar9170 *ar = hw->priv;
1181 int err = 0, i;
1182 u8 ktype;
1183
1184 if (ar->disable_offload || !vif)
1185 return -EOPNOTSUPP;
1186
Christian Lamparter7a5c7302012-12-17 16:30:05 +01001187 /* Fall back to software encryption whenever the driver is connected
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001188 * to more than one network.
1189 *
1190 * This is very unfortunate, because some machines cannot handle
1191 * the high througput speed in 802.11n networks.
1192 */
1193
Alexey Khoroshilov66cb54b2011-08-24 00:44:32 +04001194 if (!is_main_vif(ar, vif)) {
1195 mutex_lock(&ar->mutex);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001196 goto err_softw;
Alexey Khoroshilov66cb54b2011-08-24 00:44:32 +04001197 }
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001198
1199 /*
1200 * While the hardware supports *catch-all* key, for offloading
1201 * group-key en-/de-cryption. The way of how the hardware
1202 * decides which keyId maps to which key, remains a mystery...
1203 */
1204 if ((vif->type != NL80211_IFTYPE_STATION &&
1205 vif->type != NL80211_IFTYPE_ADHOC) &&
1206 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1207 return -EOPNOTSUPP;
1208
1209 switch (key->cipher) {
1210 case WLAN_CIPHER_SUITE_WEP40:
1211 ktype = AR9170_ENC_ALG_WEP64;
1212 break;
1213 case WLAN_CIPHER_SUITE_WEP104:
1214 ktype = AR9170_ENC_ALG_WEP128;
1215 break;
1216 case WLAN_CIPHER_SUITE_TKIP:
1217 ktype = AR9170_ENC_ALG_TKIP;
1218 break;
1219 case WLAN_CIPHER_SUITE_CCMP:
1220 ktype = AR9170_ENC_ALG_AESCCMP;
Christian Lampartere37b6742012-09-02 14:25:50 +02001221 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001222 break;
1223 default:
1224 return -EOPNOTSUPP;
1225 }
1226
1227 mutex_lock(&ar->mutex);
1228 if (cmd == SET_KEY) {
1229 if (!IS_STARTED(ar)) {
1230 err = -EOPNOTSUPP;
1231 goto out;
1232 }
1233
1234 if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1235 sta = NULL;
1236
1237 i = 64 + key->keyidx;
1238 } else {
1239 for (i = 0; i < 64; i++)
1240 if (!(ar->usedkeys & BIT(i)))
1241 break;
1242 if (i == 64)
1243 goto err_softw;
1244 }
1245
1246 key->hw_key_idx = i;
1247
1248 err = carl9170_upload_key(ar, i, sta ? sta->addr : NULL,
1249 ktype, 0, key->key,
1250 min_t(u8, 16, key->keylen));
1251 if (err)
1252 goto out;
1253
1254 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1255 err = carl9170_upload_key(ar, i, sta ? sta->addr :
1256 NULL, ktype, 1,
1257 key->key + 16, 16);
1258 if (err)
1259 goto out;
1260
1261 /*
1262 * hardware is not capable generating MMIC
1263 * of fragmented frames!
1264 */
1265 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1266 }
1267
1268 if (i < 64)
1269 ar->usedkeys |= BIT(i);
1270
1271 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1272 } else {
1273 if (!IS_STARTED(ar)) {
1274 /* The device is gone... together with the key ;-) */
1275 err = 0;
1276 goto out;
1277 }
1278
1279 if (key->hw_key_idx < 64) {
1280 ar->usedkeys &= ~BIT(key->hw_key_idx);
1281 } else {
1282 err = carl9170_upload_key(ar, key->hw_key_idx, NULL,
1283 AR9170_ENC_ALG_NONE, 0,
1284 NULL, 0);
1285 if (err)
1286 goto out;
1287
1288 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1289 err = carl9170_upload_key(ar, key->hw_key_idx,
1290 NULL,
1291 AR9170_ENC_ALG_NONE,
1292 1, NULL, 0);
1293 if (err)
1294 goto out;
1295 }
1296
1297 }
1298
1299 err = carl9170_disable_key(ar, key->hw_key_idx);
1300 if (err)
1301 goto out;
1302 }
1303
1304out:
1305 mutex_unlock(&ar->mutex);
1306 return err;
1307
1308err_softw:
1309 if (!ar->rx_software_decryption) {
1310 ar->rx_software_decryption = true;
1311 carl9170_set_operating_mode(ar);
1312 }
1313 mutex_unlock(&ar->mutex);
1314 return -ENOSPC;
1315}
1316
1317static int carl9170_op_sta_add(struct ieee80211_hw *hw,
1318 struct ieee80211_vif *vif,
1319 struct ieee80211_sta *sta)
1320{
1321 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1322 unsigned int i;
1323
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001324 atomic_set(&sta_info->pending_frames, 0);
1325
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001326 if (sta->ht_cap.ht_supported) {
1327 if (sta->ht_cap.ampdu_density > 6) {
1328 /*
1329 * HW does support 16us AMPDU density.
1330 * No HT-Xmit for station.
1331 */
1332
1333 return 0;
1334 }
1335
Christian Lamparter558925f2012-12-20 02:44:29 +01001336 for (i = 0; i < ARRAY_SIZE(sta_info->agg); i++)
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00001337 RCU_INIT_POINTER(sta_info->agg[i], NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001338
1339 sta_info->ampdu_max_len = 1 << (3 + sta->ht_cap.ampdu_factor);
1340 sta_info->ht_sta = true;
1341 }
1342
1343 return 0;
1344}
1345
1346static int carl9170_op_sta_remove(struct ieee80211_hw *hw,
1347 struct ieee80211_vif *vif,
1348 struct ieee80211_sta *sta)
1349{
1350 struct ar9170 *ar = hw->priv;
1351 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1352 unsigned int i;
1353 bool cleanup = false;
1354
1355 if (sta->ht_cap.ht_supported) {
1356
1357 sta_info->ht_sta = false;
1358
1359 rcu_read_lock();
Christian Lamparter558925f2012-12-20 02:44:29 +01001360 for (i = 0; i < ARRAY_SIZE(sta_info->agg); i++) {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001361 struct carl9170_sta_tid *tid_info;
1362
1363 tid_info = rcu_dereference(sta_info->agg[i]);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00001364 RCU_INIT_POINTER(sta_info->agg[i], NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001365
1366 if (!tid_info)
1367 continue;
1368
1369 spin_lock_bh(&ar->tx_ampdu_list_lock);
1370 if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
1371 tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
1372 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1373 cleanup = true;
1374 }
1375 rcu_read_unlock();
1376
1377 if (cleanup)
1378 carl9170_ampdu_gc(ar);
1379 }
1380
1381 return 0;
1382}
1383
Eliad Peller8a3a3c82011-10-02 10:15:52 +02001384static int carl9170_op_conf_tx(struct ieee80211_hw *hw,
1385 struct ieee80211_vif *vif, u16 queue,
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001386 const struct ieee80211_tx_queue_params *param)
1387{
1388 struct ar9170 *ar = hw->priv;
1389 int ret;
1390
1391 mutex_lock(&ar->mutex);
1392 if (queue < ar->hw->queues) {
1393 memcpy(&ar->edcf[ar9170_qmap[queue]], param, sizeof(*param));
1394 ret = carl9170_set_qos(ar);
1395 } else {
1396 ret = -EINVAL;
1397 }
1398
1399 mutex_unlock(&ar->mutex);
1400 return ret;
1401}
1402
1403static void carl9170_ampdu_work(struct work_struct *work)
1404{
1405 struct ar9170 *ar = container_of(work, struct ar9170,
1406 ampdu_work);
1407
1408 if (!IS_STARTED(ar))
1409 return;
1410
1411 mutex_lock(&ar->mutex);
1412 carl9170_ampdu_gc(ar);
1413 mutex_unlock(&ar->mutex);
1414}
1415
1416static int carl9170_op_ampdu_action(struct ieee80211_hw *hw,
1417 struct ieee80211_vif *vif,
1418 enum ieee80211_ampdu_mlme_action action,
1419 struct ieee80211_sta *sta,
Johannes Berg0b01f032011-01-18 13:51:05 +01001420 u16 tid, u16 *ssn, u8 buf_size)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001421{
1422 struct ar9170 *ar = hw->priv;
1423 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1424 struct carl9170_sta_tid *tid_info;
1425
1426 if (modparam_noht)
1427 return -EOPNOTSUPP;
1428
1429 switch (action) {
1430 case IEEE80211_AMPDU_TX_START:
Christian Lamparter9c655c82010-09-26 23:06:56 +02001431 if (!sta_info->ht_sta)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001432 return -EOPNOTSUPP;
1433
1434 rcu_read_lock();
1435 if (rcu_dereference(sta_info->agg[tid])) {
1436 rcu_read_unlock();
1437 return -EBUSY;
1438 }
1439
1440 tid_info = kzalloc(sizeof(struct carl9170_sta_tid),
1441 GFP_ATOMIC);
1442 if (!tid_info) {
1443 rcu_read_unlock();
1444 return -ENOMEM;
1445 }
1446
1447 tid_info->hsn = tid_info->bsn = tid_info->snx = (*ssn);
1448 tid_info->state = CARL9170_TID_STATE_PROGRESS;
1449 tid_info->tid = tid;
1450 tid_info->max = sta_info->ampdu_max_len;
Christian Lamparter9ad86ed2013-06-12 21:35:39 +02001451 tid_info->sta = sta;
1452 tid_info->vif = vif;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001453
1454 INIT_LIST_HEAD(&tid_info->list);
1455 INIT_LIST_HEAD(&tid_info->tmp_list);
1456 skb_queue_head_init(&tid_info->queue);
1457 spin_lock_init(&tid_info->lock);
1458
1459 spin_lock_bh(&ar->tx_ampdu_list_lock);
1460 ar->tx_ampdu_list_len++;
1461 list_add_tail_rcu(&tid_info->list, &ar->tx_ampdu_list);
1462 rcu_assign_pointer(sta_info->agg[tid], tid_info);
1463 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1464 rcu_read_unlock();
1465
1466 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1467 break;
1468
Johannes Berg18b559d2012-07-18 13:51:25 +02001469 case IEEE80211_AMPDU_TX_STOP_CONT:
1470 case IEEE80211_AMPDU_TX_STOP_FLUSH:
1471 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001472 rcu_read_lock();
1473 tid_info = rcu_dereference(sta_info->agg[tid]);
1474 if (tid_info) {
1475 spin_lock_bh(&ar->tx_ampdu_list_lock);
1476 if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
1477 tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
1478 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1479 }
1480
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00001481 RCU_INIT_POINTER(sta_info->agg[tid], NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001482 rcu_read_unlock();
1483
1484 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1485 ieee80211_queue_work(ar->hw, &ar->ampdu_work);
1486 break;
1487
1488 case IEEE80211_AMPDU_TX_OPERATIONAL:
1489 rcu_read_lock();
1490 tid_info = rcu_dereference(sta_info->agg[tid]);
1491
1492 sta_info->stats[tid].clear = true;
Christian Lamparter24047e22011-03-29 13:43:14 +02001493 sta_info->stats[tid].req = false;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001494
1495 if (tid_info) {
1496 bitmap_zero(tid_info->bitmap, CARL9170_BAW_SIZE);
1497 tid_info->state = CARL9170_TID_STATE_IDLE;
1498 }
1499 rcu_read_unlock();
1500
1501 if (WARN_ON_ONCE(!tid_info))
1502 return -EFAULT;
1503
1504 break;
1505
1506 case IEEE80211_AMPDU_RX_START:
1507 case IEEE80211_AMPDU_RX_STOP:
1508 /* Handled by hardware */
1509 break;
1510
1511 default:
1512 return -EOPNOTSUPP;
1513 }
1514
1515 return 0;
1516}
1517
1518#ifdef CONFIG_CARL9170_WPC
1519static int carl9170_register_wps_button(struct ar9170 *ar)
1520{
1521 struct input_dev *input;
1522 int err;
1523
1524 if (!(ar->features & CARL9170_WPS_BUTTON))
1525 return 0;
1526
1527 input = input_allocate_device();
1528 if (!input)
1529 return -ENOMEM;
1530
1531 snprintf(ar->wps.name, sizeof(ar->wps.name), "%s WPS Button",
1532 wiphy_name(ar->hw->wiphy));
1533
1534 snprintf(ar->wps.phys, sizeof(ar->wps.phys),
1535 "ieee80211/%s/input0", wiphy_name(ar->hw->wiphy));
1536
1537 input->name = ar->wps.name;
1538 input->phys = ar->wps.phys;
1539 input->id.bustype = BUS_USB;
1540 input->dev.parent = &ar->hw->wiphy->dev;
1541
1542 input_set_capability(input, EV_KEY, KEY_WPS_BUTTON);
1543
1544 err = input_register_device(input);
1545 if (err) {
1546 input_free_device(input);
1547 return err;
1548 }
1549
1550 ar->wps.pbc = input;
1551 return 0;
1552}
1553#endif /* CONFIG_CARL9170_WPC */
1554
Christian Lamparter00044f12011-08-15 20:09:54 +02001555#ifdef CONFIG_CARL9170_HWRNG
1556static int carl9170_rng_get(struct ar9170 *ar)
1557{
1558
1559#define RW (CARL9170_MAX_CMD_PAYLOAD_LEN / sizeof(u32))
1560#define RB (CARL9170_MAX_CMD_PAYLOAD_LEN)
1561
1562 static const __le32 rng_load[RW] = {
1563 [0 ... (RW - 1)] = cpu_to_le32(AR9170_RAND_REG_NUM)};
1564
1565 u32 buf[RW];
1566
1567 unsigned int i, off = 0, transfer, count;
1568 int err;
1569
1570 BUILD_BUG_ON(RB > CARL9170_MAX_CMD_PAYLOAD_LEN);
1571
1572 if (!IS_ACCEPTING_CMD(ar) || !ar->rng.initialized)
1573 return -EAGAIN;
1574
1575 count = ARRAY_SIZE(ar->rng.cache);
1576 while (count) {
1577 err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
1578 RB, (u8 *) rng_load,
1579 RB, (u8 *) buf);
1580 if (err)
1581 return err;
1582
1583 transfer = min_t(unsigned int, count, RW);
1584 for (i = 0; i < transfer; i++)
1585 ar->rng.cache[off + i] = buf[i];
1586
1587 off += transfer;
1588 count -= transfer;
1589 }
1590
1591 ar->rng.cache_idx = 0;
1592
1593#undef RW
1594#undef RB
1595 return 0;
1596}
1597
1598static int carl9170_rng_read(struct hwrng *rng, u32 *data)
1599{
1600 struct ar9170 *ar = (struct ar9170 *)rng->priv;
1601 int ret = -EIO;
1602
1603 mutex_lock(&ar->mutex);
1604 if (ar->rng.cache_idx >= ARRAY_SIZE(ar->rng.cache)) {
1605 ret = carl9170_rng_get(ar);
1606 if (ret) {
1607 mutex_unlock(&ar->mutex);
1608 return ret;
1609 }
1610 }
1611
1612 *data = ar->rng.cache[ar->rng.cache_idx++];
1613 mutex_unlock(&ar->mutex);
1614
1615 return sizeof(u16);
1616}
1617
1618static void carl9170_unregister_hwrng(struct ar9170 *ar)
1619{
1620 if (ar->rng.initialized) {
1621 hwrng_unregister(&ar->rng.rng);
1622 ar->rng.initialized = false;
1623 }
1624}
1625
1626static int carl9170_register_hwrng(struct ar9170 *ar)
1627{
1628 int err;
1629
1630 snprintf(ar->rng.name, ARRAY_SIZE(ar->rng.name),
1631 "%s_%s", KBUILD_MODNAME, wiphy_name(ar->hw->wiphy));
1632 ar->rng.rng.name = ar->rng.name;
1633 ar->rng.rng.data_read = carl9170_rng_read;
1634 ar->rng.rng.priv = (unsigned long)ar;
1635
1636 if (WARN_ON(ar->rng.initialized))
1637 return -EALREADY;
1638
1639 err = hwrng_register(&ar->rng.rng);
1640 if (err) {
1641 dev_err(&ar->udev->dev, "Failed to register the random "
1642 "number generator (%d)\n", err);
1643 return err;
1644 }
1645
1646 ar->rng.initialized = true;
1647
1648 err = carl9170_rng_get(ar);
1649 if (err) {
1650 carl9170_unregister_hwrng(ar);
1651 return err;
1652 }
1653
1654 return 0;
1655}
1656#endif /* CONFIG_CARL9170_HWRNG */
1657
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001658static int carl9170_op_get_survey(struct ieee80211_hw *hw, int idx,
1659 struct survey_info *survey)
1660{
1661 struct ar9170 *ar = hw->priv;
Christian Lamparteracf17712011-08-15 19:50:48 +02001662 struct ieee80211_channel *chan;
1663 struct ieee80211_supported_band *band;
1664 int err, b, i;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001665
Christian Lamparteracf17712011-08-15 19:50:48 +02001666 chan = ar->channel;
1667 if (!chan)
1668 return -ENODEV;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001669
Christian Lamparteracf17712011-08-15 19:50:48 +02001670 if (idx == chan->hw_value) {
1671 mutex_lock(&ar->mutex);
1672 err = carl9170_update_survey(ar, false, true);
1673 mutex_unlock(&ar->mutex);
1674 if (err)
1675 return err;
1676 }
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001677
Christian Lamparteracf17712011-08-15 19:50:48 +02001678 for (b = 0; b < IEEE80211_NUM_BANDS; b++) {
1679 band = ar->hw->wiphy->bands[b];
1680
1681 if (!band)
1682 continue;
1683
1684 for (i = 0; i < band->n_channels; i++) {
1685 if (band->channels[i].hw_value == idx) {
1686 chan = &band->channels[i];
1687 goto found;
1688 }
1689 }
1690 }
1691 return -ENOENT;
1692
1693found:
1694 memcpy(survey, &ar->survey[idx], sizeof(*survey));
1695
1696 survey->channel = chan;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001697 survey->filled = SURVEY_INFO_NOISE_DBM;
Christian Lamparteracf17712011-08-15 19:50:48 +02001698
1699 if (ar->channel == chan)
1700 survey->filled |= SURVEY_INFO_IN_USE;
1701
1702 if (ar->fw.hw_counters) {
1703 survey->filled |= SURVEY_INFO_CHANNEL_TIME |
1704 SURVEY_INFO_CHANNEL_TIME_BUSY |
1705 SURVEY_INFO_CHANNEL_TIME_TX;
1706 }
1707
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001708 return 0;
1709}
1710
Johannes Berg39ecc012013-02-13 12:11:00 +01001711static void carl9170_op_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001712{
1713 struct ar9170 *ar = hw->priv;
1714 unsigned int vid;
1715
1716 mutex_lock(&ar->mutex);
1717 for_each_set_bit(vid, &ar->vif_bitmap, ar->fw.vif_num)
1718 carl9170_flush_cab(ar, vid);
1719
1720 carl9170_flush(ar, drop);
1721 mutex_unlock(&ar->mutex);
1722}
1723
1724static int carl9170_op_get_stats(struct ieee80211_hw *hw,
1725 struct ieee80211_low_level_stats *stats)
1726{
1727 struct ar9170 *ar = hw->priv;
1728
1729 memset(stats, 0, sizeof(*stats));
1730 stats->dot11ACKFailureCount = ar->tx_ack_failures;
1731 stats->dot11FCSErrorCount = ar->tx_fcs_errors;
1732 return 0;
1733}
1734
1735static void carl9170_op_sta_notify(struct ieee80211_hw *hw,
1736 struct ieee80211_vif *vif,
1737 enum sta_notify_cmd cmd,
1738 struct ieee80211_sta *sta)
1739{
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001740 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001741
1742 switch (cmd) {
1743 case STA_NOTIFY_SLEEP:
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001744 sta_info->sleeping = true;
1745 if (atomic_read(&sta_info->pending_frames))
1746 ieee80211_sta_block_awake(hw, sta, true);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001747 break;
1748
1749 case STA_NOTIFY_AWAKE:
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001750 sta_info->sleeping = false;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001751 break;
1752 }
1753}
1754
Christian Lamparter69f72352011-07-07 23:01:25 +02001755static bool carl9170_tx_frames_pending(struct ieee80211_hw *hw)
1756{
1757 struct ar9170 *ar = hw->priv;
1758
1759 return !!atomic_read(&ar->tx_total_queued);
1760}
1761
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001762static const struct ieee80211_ops carl9170_ops = {
1763 .start = carl9170_op_start,
1764 .stop = carl9170_op_stop,
1765 .tx = carl9170_op_tx,
1766 .flush = carl9170_op_flush,
1767 .add_interface = carl9170_op_add_interface,
1768 .remove_interface = carl9170_op_remove_interface,
1769 .config = carl9170_op_config,
1770 .prepare_multicast = carl9170_op_prepare_multicast,
1771 .configure_filter = carl9170_op_configure_filter,
1772 .conf_tx = carl9170_op_conf_tx,
1773 .bss_info_changed = carl9170_op_bss_info_changed,
1774 .get_tsf = carl9170_op_get_tsf,
1775 .set_key = carl9170_op_set_key,
1776 .sta_add = carl9170_op_sta_add,
1777 .sta_remove = carl9170_op_sta_remove,
1778 .sta_notify = carl9170_op_sta_notify,
1779 .get_survey = carl9170_op_get_survey,
1780 .get_stats = carl9170_op_get_stats,
1781 .ampdu_action = carl9170_op_ampdu_action,
Christian Lamparter69f72352011-07-07 23:01:25 +02001782 .tx_frames_pending = carl9170_tx_frames_pending,
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001783};
1784
1785void *carl9170_alloc(size_t priv_size)
1786{
1787 struct ieee80211_hw *hw;
1788 struct ar9170 *ar;
1789 struct sk_buff *skb;
1790 int i;
1791
1792 /*
1793 * this buffer is used for rx stream reconstruction.
1794 * Under heavy load this device (or the transport layer?)
1795 * tends to split the streams into separate rx descriptors.
1796 */
1797
1798 skb = __dev_alloc_skb(AR9170_RX_STREAM_MAX_SIZE, GFP_KERNEL);
1799 if (!skb)
1800 goto err_nomem;
1801
1802 hw = ieee80211_alloc_hw(priv_size, &carl9170_ops);
1803 if (!hw)
1804 goto err_nomem;
1805
1806 ar = hw->priv;
1807 ar->hw = hw;
1808 ar->rx_failover = skb;
1809
1810 memset(&ar->rx_plcp, 0, sizeof(struct ar9170_rx_head));
1811 ar->rx_has_plcp = false;
1812
1813 /*
1814 * Here's a hidden pitfall!
1815 *
1816 * All 4 AC queues work perfectly well under _legacy_ operation.
1817 * However as soon as aggregation is enabled, the traffic flow
1818 * gets very bumpy. Therefore we have to _switch_ to a
1819 * software AC with a single HW queue.
1820 */
1821 hw->queues = __AR9170_NUM_TXQ;
1822
1823 mutex_init(&ar->mutex);
1824 spin_lock_init(&ar->beacon_lock);
1825 spin_lock_init(&ar->cmd_lock);
1826 spin_lock_init(&ar->tx_stats_lock);
1827 spin_lock_init(&ar->tx_ampdu_list_lock);
1828 spin_lock_init(&ar->mem_lock);
1829 spin_lock_init(&ar->state_lock);
1830 atomic_set(&ar->pending_restarts, 0);
1831 ar->vifs = 0;
1832 for (i = 0; i < ar->hw->queues; i++) {
1833 skb_queue_head_init(&ar->tx_status[i]);
1834 skb_queue_head_init(&ar->tx_pending[i]);
Christian Lamparterc9122c02012-07-07 21:13:59 +02001835
1836 INIT_LIST_HEAD(&ar->bar_list[i]);
1837 spin_lock_init(&ar->bar_list_lock[i]);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001838 }
1839 INIT_WORK(&ar->ps_work, carl9170_ps_work);
Christian Lampartere4a668c2010-10-29 23:26:13 +02001840 INIT_WORK(&ar->ping_work, carl9170_ping_work);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001841 INIT_WORK(&ar->restart_work, carl9170_restart_work);
1842 INIT_WORK(&ar->ampdu_work, carl9170_ampdu_work);
Christian Lamparteracf17712011-08-15 19:50:48 +02001843 INIT_DELAYED_WORK(&ar->stat_work, carl9170_stat_work);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001844 INIT_DELAYED_WORK(&ar->tx_janitor, carl9170_tx_janitor);
1845 INIT_LIST_HEAD(&ar->tx_ampdu_list);
1846 rcu_assign_pointer(ar->tx_ampdu_iter,
1847 (struct carl9170_sta_tid *) &ar->tx_ampdu_list);
1848
1849 bitmap_zero(&ar->vif_bitmap, ar->fw.vif_num);
1850 INIT_LIST_HEAD(&ar->vif_list);
1851 init_completion(&ar->tx_flush);
1852
Christian Lamparterdf649622011-05-14 02:42:38 +02001853 /* firmware decides which modes we support */
1854 hw->wiphy->interface_modes = 0;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001855
1856 hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS |
Christian Lampartere37b6742012-09-02 14:25:50 +02001857 IEEE80211_HW_MFP_CAPABLE |
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001858 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
1859 IEEE80211_HW_SUPPORTS_PS |
1860 IEEE80211_HW_PS_NULLFUNC_STACK |
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001861 IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC |
Christian Lamparter9ad86ed2013-06-12 21:35:39 +02001862 IEEE80211_HW_SUPPORTS_RC_TABLE |
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001863 IEEE80211_HW_SIGNAL_DBM;
1864
1865 if (!modparam_noht) {
1866 /*
1867 * see the comment above, why we allow the user
1868 * to disable HT by a module parameter.
1869 */
1870 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
1871 }
1872
1873 hw->extra_tx_headroom = sizeof(struct _carl9170_tx_superframe);
1874 hw->sta_data_size = sizeof(struct carl9170_sta_info);
1875 hw->vif_data_size = sizeof(struct carl9170_vif_info);
1876
1877 hw->max_rates = CARL9170_TX_MAX_RATES;
1878 hw->max_rate_tries = CARL9170_TX_USER_RATE_TRIES;
1879
1880 for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
1881 ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
1882
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001883 return ar;
1884
1885err_nomem:
1886 kfree_skb(skb);
1887 return ERR_PTR(-ENOMEM);
1888}
1889
1890static int carl9170_read_eeprom(struct ar9170 *ar)
1891{
1892#define RW 8 /* number of words to read at once */
1893#define RB (sizeof(u32) * RW)
1894 u8 *eeprom = (void *)&ar->eeprom;
1895 __le32 offsets[RW];
1896 int i, j, err;
1897
1898 BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
1899
1900 BUILD_BUG_ON(RB > CARL9170_MAX_CMD_LEN - 4);
1901#ifndef __CHECKER__
1902 /* don't want to handle trailing remains */
1903 BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
1904#endif
1905
Pavel Roskin3f29c522011-07-13 21:38:18 -04001906 for (i = 0; i < sizeof(ar->eeprom) / RB; i++) {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001907 for (j = 0; j < RW; j++)
1908 offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
1909 RB * i + 4 * j);
1910
1911 err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
1912 RB, (u8 *) &offsets,
1913 RB, eeprom + RB * i);
1914 if (err)
1915 return err;
1916 }
1917
1918#undef RW
1919#undef RB
1920 return 0;
1921}
1922
1923static int carl9170_parse_eeprom(struct ar9170 *ar)
1924{
1925 struct ath_regulatory *regulatory = &ar->common.regulatory;
1926 unsigned int rx_streams, tx_streams, tx_params = 0;
1927 int bands = 0;
Christian Lamparteracf17712011-08-15 19:50:48 +02001928 int chans = 0;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001929
1930 if (ar->eeprom.length == cpu_to_le16(0xffff))
1931 return -ENODATA;
1932
1933 rx_streams = hweight8(ar->eeprom.rx_mask);
1934 tx_streams = hweight8(ar->eeprom.tx_mask);
1935
1936 if (rx_streams != tx_streams) {
1937 tx_params = IEEE80211_HT_MCS_TX_RX_DIFF;
1938
1939 WARN_ON(!(tx_streams >= 1 && tx_streams <=
1940 IEEE80211_HT_MCS_TX_MAX_STREAMS));
1941
1942 tx_params = (tx_streams - 1) <<
1943 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1944
1945 carl9170_band_2GHz.ht_cap.mcs.tx_params |= tx_params;
1946 carl9170_band_5GHz.ht_cap.mcs.tx_params |= tx_params;
1947 }
1948
1949 if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
1950 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
1951 &carl9170_band_2GHz;
Christian Lamparteracf17712011-08-15 19:50:48 +02001952 chans += carl9170_band_2GHz.n_channels;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001953 bands++;
1954 }
1955 if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
1956 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
1957 &carl9170_band_5GHz;
Christian Lamparteracf17712011-08-15 19:50:48 +02001958 chans += carl9170_band_5GHz.n_channels;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001959 bands++;
1960 }
1961
Christian Lamparteracf17712011-08-15 19:50:48 +02001962 if (!bands)
1963 return -EINVAL;
1964
1965 ar->survey = kzalloc(sizeof(struct survey_info) * chans, GFP_KERNEL);
1966 if (!ar->survey)
1967 return -ENOMEM;
1968 ar->num_channels = chans;
1969
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001970 /*
1971 * I measured this, a bandswitch takes roughly
1972 * 135 ms and a frequency switch about 80.
1973 *
1974 * FIXME: measure these values again once EEPROM settings
1975 * are used, that will influence them!
1976 */
1977 if (bands == 2)
1978 ar->hw->channel_change_time = 135 * 1000;
1979 else
1980 ar->hw->channel_change_time = 80 * 1000;
1981
1982 regulatory->current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001983
1984 /* second part of wiphy init */
1985 SET_IEEE80211_PERM_ADDR(ar->hw, ar->eeprom.mac_address);
1986
Christian Lamparteracf17712011-08-15 19:50:48 +02001987 return 0;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001988}
1989
Luis R. Rodriguez0c0280b2013-01-11 18:39:36 +00001990static void carl9170_reg_notifier(struct wiphy *wiphy,
1991 struct regulatory_request *request)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001992{
1993 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1994 struct ar9170 *ar = hw->priv;
1995
Luis R. Rodriguez0c0280b2013-01-11 18:39:36 +00001996 ath_reg_notifier_apply(wiphy, request, &ar->common.regulatory);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001997}
1998
1999int carl9170_register(struct ar9170 *ar)
2000{
2001 struct ath_regulatory *regulatory = &ar->common.regulatory;
2002 int err = 0, i;
2003
2004 if (WARN_ON(ar->mem_bitmap))
2005 return -EINVAL;
2006
2007 ar->mem_bitmap = kzalloc(roundup(ar->fw.mem_blocks, BITS_PER_LONG) *
2008 sizeof(unsigned long), GFP_KERNEL);
2009
2010 if (!ar->mem_bitmap)
2011 return -ENOMEM;
2012
2013 /* try to read EEPROM, init MAC addr */
2014 err = carl9170_read_eeprom(ar);
2015 if (err)
2016 return err;
2017
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002018 err = carl9170_parse_eeprom(ar);
2019 if (err)
2020 return err;
2021
2022 err = ath_regd_init(regulatory, ar->hw->wiphy,
2023 carl9170_reg_notifier);
2024 if (err)
2025 return err;
2026
2027 if (modparam_noht) {
2028 carl9170_band_2GHz.ht_cap.ht_supported = false;
2029 carl9170_band_5GHz.ht_cap.ht_supported = false;
2030 }
2031
2032 for (i = 0; i < ar->fw.vif_num; i++) {
2033 ar->vif_priv[i].id = i;
2034 ar->vif_priv[i].vif = NULL;
2035 }
2036
2037 err = ieee80211_register_hw(ar->hw);
2038 if (err)
2039 return err;
2040
2041 /* mac80211 interface is now registered */
2042 ar->registered = true;
2043
2044 if (!ath_is_world_regd(regulatory))
2045 regulatory_hint(ar->hw->wiphy, regulatory->alpha2);
2046
2047#ifdef CONFIG_CARL9170_DEBUGFS
2048 carl9170_debugfs_register(ar);
2049#endif /* CONFIG_CARL9170_DEBUGFS */
2050
2051 err = carl9170_led_init(ar);
2052 if (err)
2053 goto err_unreg;
2054
2055#ifdef CONFIG_CARL9170_LEDS
2056 err = carl9170_led_register(ar);
2057 if (err)
2058 goto err_unreg;
Hauke Mehrtens8e7ce892010-10-23 19:51:32 +02002059#endif /* CONFIG_CARL9170_LEDS */
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002060
2061#ifdef CONFIG_CARL9170_WPC
2062 err = carl9170_register_wps_button(ar);
2063 if (err)
2064 goto err_unreg;
2065#endif /* CONFIG_CARL9170_WPC */
2066
Christian Lamparter00044f12011-08-15 20:09:54 +02002067#ifdef CONFIG_CARL9170_HWRNG
2068 err = carl9170_register_hwrng(ar);
2069 if (err)
2070 goto err_unreg;
2071#endif /* CONFIG_CARL9170_HWRNG */
2072
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002073 dev_info(&ar->udev->dev, "Atheros AR9170 is registered as '%s'\n",
2074 wiphy_name(ar->hw->wiphy));
2075
2076 return 0;
2077
2078err_unreg:
2079 carl9170_unregister(ar);
2080 return err;
2081}
2082
2083void carl9170_unregister(struct ar9170 *ar)
2084{
2085 if (!ar->registered)
2086 return;
2087
2088 ar->registered = false;
2089
2090#ifdef CONFIG_CARL9170_LEDS
2091 carl9170_led_unregister(ar);
2092#endif /* CONFIG_CARL9170_LEDS */
2093
2094#ifdef CONFIG_CARL9170_DEBUGFS
2095 carl9170_debugfs_unregister(ar);
2096#endif /* CONFIG_CARL9170_DEBUGFS */
2097
2098#ifdef CONFIG_CARL9170_WPC
2099 if (ar->wps.pbc) {
2100 input_unregister_device(ar->wps.pbc);
2101 ar->wps.pbc = NULL;
2102 }
2103#endif /* CONFIG_CARL9170_WPC */
2104
Christian Lamparter00044f12011-08-15 20:09:54 +02002105#ifdef CONFIG_CARL9170_HWRNG
2106 carl9170_unregister_hwrng(ar);
2107#endif /* CONFIG_CARL9170_HWRNG */
2108
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002109 carl9170_cancel_worker(ar);
2110 cancel_work_sync(&ar->restart_work);
2111
2112 ieee80211_unregister_hw(ar->hw);
2113}
2114
2115void carl9170_free(struct ar9170 *ar)
2116{
2117 WARN_ON(ar->registered);
2118 WARN_ON(IS_INITIALIZED(ar));
2119
2120 kfree_skb(ar->rx_failover);
2121 ar->rx_failover = NULL;
2122
2123 kfree(ar->mem_bitmap);
2124 ar->mem_bitmap = NULL;
2125
Christian Lamparteracf17712011-08-15 19:50:48 +02002126 kfree(ar->survey);
2127 ar->survey = NULL;
2128
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002129 mutex_destroy(&ar->mutex);
2130
2131 ieee80211_free_hw(ar->hw);
2132}