blob: 6ca5dd068275369b5a2a06ae88ec2f1fa6a6b662 [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
586 ar->rx_software_decryption = ar->disable_offload;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200587
588 err = carl9170_set_operating_mode(ar);
589 return err;
590}
591
592static int carl9170_op_add_interface(struct ieee80211_hw *hw,
593 struct ieee80211_vif *vif)
594{
595 struct carl9170_vif_info *vif_priv = (void *) vif->drv_priv;
Christian Lamparter7f878b02012-12-16 01:41:37 +0100596 struct ieee80211_vif *main_vif, *old_main = NULL;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200597 struct ar9170 *ar = hw->priv;
598 int vif_id = -1, err = 0;
599
600 mutex_lock(&ar->mutex);
601 rcu_read_lock();
602 if (vif_priv->active) {
603 /*
604 * Skip the interface structure initialization,
605 * if the vif survived the _restart call.
606 */
607 vif_id = vif_priv->id;
608 vif_priv->enable_beacon = false;
609
610 spin_lock_bh(&ar->beacon_lock);
611 dev_kfree_skb_any(vif_priv->beacon);
612 vif_priv->beacon = NULL;
613 spin_unlock_bh(&ar->beacon_lock);
614
615 goto init;
616 }
617
Christian Lamparter7f878b02012-12-16 01:41:37 +0100618 /* Because the AR9170 HW's MAC doesn't provide full support for
619 * multiple, independent interfaces [of different operation modes].
620 * We have to select ONE main interface [main mode of HW], but we
621 * can have multiple slaves [AKA: entry in the ACK-table].
622 *
623 * The first (from HEAD/TOP) interface in the ar->vif_list is
624 * always the main intf. All following intfs in this list
625 * are considered to be slave intfs.
626 */
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200627 main_vif = carl9170_get_main_vif(ar);
628
629 if (main_vif) {
630 switch (main_vif->type) {
631 case NL80211_IFTYPE_STATION:
632 if (vif->type == NL80211_IFTYPE_STATION)
633 break;
634
Christian Lamparter7f878b02012-12-16 01:41:37 +0100635 /* P2P GO [master] use-case
636 * Because the P2P GO station is selected dynamically
637 * by all participating peers of a WIFI Direct network,
638 * the driver has be able to change the main interface
639 * operating mode on the fly.
640 */
641 if (main_vif->p2p && vif->p2p &&
642 vif->type == NL80211_IFTYPE_AP) {
643 old_main = main_vif;
644 break;
645 }
646
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200647 err = -EBUSY;
648 rcu_read_unlock();
649
650 goto unlock;
651
Javier Lopezda93c262012-07-27 11:27:25 -0700652 case NL80211_IFTYPE_MESH_POINT:
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200653 case NL80211_IFTYPE_AP:
654 if ((vif->type == NL80211_IFTYPE_STATION) ||
655 (vif->type == NL80211_IFTYPE_WDS) ||
Javier Lopezda93c262012-07-27 11:27:25 -0700656 (vif->type == NL80211_IFTYPE_AP) ||
657 (vif->type == NL80211_IFTYPE_MESH_POINT))
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200658 break;
659
660 err = -EBUSY;
661 rcu_read_unlock();
662 goto unlock;
663
664 default:
665 rcu_read_unlock();
666 goto unlock;
667 }
668 }
669
670 vif_id = bitmap_find_free_region(&ar->vif_bitmap, ar->fw.vif_num, 0);
671
672 if (vif_id < 0) {
673 rcu_read_unlock();
674
675 err = -ENOSPC;
676 goto unlock;
677 }
678
679 BUG_ON(ar->vif_priv[vif_id].id != vif_id);
680
681 vif_priv->active = true;
682 vif_priv->id = vif_id;
683 vif_priv->enable_beacon = false;
684 ar->vifs++;
Christian Lamparter7f878b02012-12-16 01:41:37 +0100685 if (old_main) {
686 /* We end up in here, if the main interface is being replaced.
687 * Put the new main interface at the HEAD of the list and the
688 * previous inteface will automatically become second in line.
689 */
690 list_add_rcu(&vif_priv->list, &ar->vif_list);
691 } else {
692 /* Add new inteface. If the list is empty, it will become the
693 * main inteface, otherwise it will be slave.
694 */
695 list_add_tail_rcu(&vif_priv->list, &ar->vif_list);
696 }
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200697 rcu_assign_pointer(ar->vif_priv[vif_id].vif, vif);
698
699init:
Christian Lamparter7f878b02012-12-16 01:41:37 +0100700 main_vif = carl9170_get_main_vif(ar);
701
702 if (main_vif == vif) {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200703 rcu_assign_pointer(ar->beacon_iter, vif_priv);
704 rcu_read_unlock();
705
Christian Lamparter7f878b02012-12-16 01:41:37 +0100706 if (old_main) {
707 struct carl9170_vif_info *old_main_priv =
708 (void *) old_main->drv_priv;
709 /* downgrade old main intf to slave intf.
710 * NOTE: We are no longer under rcu_read_lock.
711 * But we are still holding ar->mutex, so the
712 * vif data [id, addr] is safe.
713 */
714 err = carl9170_mod_virtual_mac(ar, old_main_priv->id,
715 old_main->addr);
716 if (err)
717 goto unlock;
718 }
719
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200720 err = carl9170_init_interface(ar, vif);
721 if (err)
722 goto unlock;
723 } else {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200724 rcu_read_unlock();
Christian Lamparterdafeac32010-10-23 15:02:02 +0200725 err = carl9170_mod_virtual_mac(ar, vif_id, vif->addr);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200726
727 if (err)
728 goto unlock;
729 }
730
Christian Lamparteraa324522011-01-23 00:18:28 +0100731 if (ar->fw.tx_seq_table) {
732 err = carl9170_write_reg(ar, ar->fw.tx_seq_table + vif_id * 4,
733 0);
734 if (err)
735 goto unlock;
736 }
737
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200738unlock:
Christian Lamparterb3974922010-11-20 13:15:27 +0100739 if (err && (vif_id >= 0)) {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200740 vif_priv->active = false;
741 bitmap_release_region(&ar->vif_bitmap, vif_id, 0);
742 ar->vifs--;
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000743 RCU_INIT_POINTER(ar->vif_priv[vif_id].vif, NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200744 list_del_rcu(&vif_priv->list);
745 mutex_unlock(&ar->mutex);
746 synchronize_rcu();
747 } else {
748 if (ar->vifs > 1)
749 ar->ps.off_override |= PS_OFF_VIF;
750
751 mutex_unlock(&ar->mutex);
752 }
753
754 return err;
755}
756
757static void carl9170_op_remove_interface(struct ieee80211_hw *hw,
758 struct ieee80211_vif *vif)
759{
760 struct carl9170_vif_info *vif_priv = (void *) vif->drv_priv;
761 struct ieee80211_vif *main_vif;
762 struct ar9170 *ar = hw->priv;
763 unsigned int id;
764
765 mutex_lock(&ar->mutex);
766
767 if (WARN_ON_ONCE(!vif_priv->active))
768 goto unlock;
769
770 ar->vifs--;
771
772 rcu_read_lock();
773 main_vif = carl9170_get_main_vif(ar);
774
775 id = vif_priv->id;
776
777 vif_priv->active = false;
778 WARN_ON(vif_priv->enable_beacon);
779 vif_priv->enable_beacon = false;
780 list_del_rcu(&vif_priv->list);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000781 RCU_INIT_POINTER(ar->vif_priv[id].vif, NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200782
783 if (vif == main_vif) {
784 rcu_read_unlock();
785
786 if (ar->vifs) {
787 WARN_ON(carl9170_init_interface(ar,
788 carl9170_get_main_vif(ar)));
789 } else {
790 carl9170_set_operating_mode(ar);
791 }
792 } else {
793 rcu_read_unlock();
794
795 WARN_ON(carl9170_mod_virtual_mac(ar, id, NULL));
796 }
797
798 carl9170_update_beacon(ar, false);
799 carl9170_flush_cab(ar, id);
800
801 spin_lock_bh(&ar->beacon_lock);
802 dev_kfree_skb_any(vif_priv->beacon);
803 vif_priv->beacon = NULL;
804 spin_unlock_bh(&ar->beacon_lock);
805
806 bitmap_release_region(&ar->vif_bitmap, id, 0);
807
808 carl9170_set_beacon_timers(ar);
809
810 if (ar->vifs == 1)
811 ar->ps.off_override &= ~PS_OFF_VIF;
812
813unlock:
814 mutex_unlock(&ar->mutex);
815
816 synchronize_rcu();
817}
818
819void carl9170_ps_check(struct ar9170 *ar)
820{
821 ieee80211_queue_work(ar->hw, &ar->ps_work);
822}
823
824/* caller must hold ar->mutex */
825static int carl9170_ps_update(struct ar9170 *ar)
826{
827 bool ps = false;
828 int err = 0;
829
830 if (!ar->ps.off_override)
831 ps = (ar->hw->conf.flags & IEEE80211_CONF_PS);
832
833 if (ps != ar->ps.state) {
834 err = carl9170_powersave(ar, ps);
835 if (err)
836 return err;
837
838 if (ar->ps.state && !ps) {
839 ar->ps.sleep_ms = jiffies_to_msecs(jiffies -
840 ar->ps.last_action);
841 }
842
843 if (ps)
844 ar->ps.last_slept = jiffies;
845
846 ar->ps.last_action = jiffies;
847 ar->ps.state = ps;
848 }
849
850 return 0;
851}
852
853static void carl9170_ps_work(struct work_struct *work)
854{
855 struct ar9170 *ar = container_of(work, struct ar9170,
856 ps_work);
857 mutex_lock(&ar->mutex);
858 if (IS_STARTED(ar))
859 WARN_ON_ONCE(carl9170_ps_update(ar) != 0);
860 mutex_unlock(&ar->mutex);
861}
862
Christian Lamparteracf17712011-08-15 19:50:48 +0200863static int carl9170_update_survey(struct ar9170 *ar, bool flush, bool noise)
864{
865 int err;
866
867 if (noise) {
868 err = carl9170_get_noisefloor(ar);
869 if (err)
870 return err;
871 }
872
873 if (ar->fw.hw_counters) {
874 err = carl9170_collect_tally(ar);
875 if (err)
876 return err;
877 }
878
879 if (flush)
880 memset(&ar->tally, 0, sizeof(ar->tally));
881
882 return 0;
883}
884
885static void carl9170_stat_work(struct work_struct *work)
886{
887 struct ar9170 *ar = container_of(work, struct ar9170, stat_work.work);
888 int err;
889
890 mutex_lock(&ar->mutex);
891 err = carl9170_update_survey(ar, false, true);
892 mutex_unlock(&ar->mutex);
893
894 if (err)
895 return;
896
897 ieee80211_queue_delayed_work(ar->hw, &ar->stat_work,
898 round_jiffies(msecs_to_jiffies(CARL9170_STAT_WORK)));
899}
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200900
901static int carl9170_op_config(struct ieee80211_hw *hw, u32 changed)
902{
903 struct ar9170 *ar = hw->priv;
904 int err = 0;
905
906 mutex_lock(&ar->mutex);
907 if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
908 /* TODO */
909 err = 0;
910 }
911
912 if (changed & IEEE80211_CONF_CHANGE_PS) {
913 err = carl9170_ps_update(ar);
914 if (err)
915 goto out;
916 }
917
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200918 if (changed & IEEE80211_CONF_CHANGE_SMPS) {
919 /* TODO */
920 err = 0;
921 }
922
923 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
924 /* adjust slot time for 5 GHz */
925 err = carl9170_set_slot_time(ar);
926 if (err)
927 goto out;
928
Christian Lamparteracf17712011-08-15 19:50:48 +0200929 err = carl9170_update_survey(ar, true, false);
930 if (err)
931 goto out;
932
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200933 err = carl9170_set_channel(ar, hw->conf.channel,
934 hw->conf.channel_type, CARL9170_RFI_NONE);
935 if (err)
936 goto out;
937
Christian Lamparteracf17712011-08-15 19:50:48 +0200938 err = carl9170_update_survey(ar, false, true);
939 if (err)
940 goto out;
941
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200942 err = carl9170_set_dyn_sifs_ack(ar);
943 if (err)
944 goto out;
945
946 err = carl9170_set_rts_cts_rate(ar);
947 if (err)
948 goto out;
949 }
950
Christian Lamparter67e43de2012-01-21 16:59:10 +0100951 if (changed & IEEE80211_CONF_CHANGE_POWER) {
952 err = carl9170_set_mac_tpc(ar, ar->hw->conf.channel);
953 if (err)
954 goto out;
955 }
956
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200957out:
958 mutex_unlock(&ar->mutex);
959 return err;
960}
961
962static u64 carl9170_op_prepare_multicast(struct ieee80211_hw *hw,
963 struct netdev_hw_addr_list *mc_list)
964{
965 struct netdev_hw_addr *ha;
966 u64 mchash;
967
968 /* always get broadcast frames */
969 mchash = 1ULL << (0xff >> 2);
970
971 netdev_hw_addr_list_for_each(ha, mc_list)
972 mchash |= 1ULL << (ha->addr[5] >> 2);
973
974 return mchash;
975}
976
977static void carl9170_op_configure_filter(struct ieee80211_hw *hw,
978 unsigned int changed_flags,
979 unsigned int *new_flags,
980 u64 multicast)
981{
982 struct ar9170 *ar = hw->priv;
983
984 /* mask supported flags */
Christian Lamparter5c895692010-09-28 23:00:59 +0200985 *new_flags &= FIF_ALLMULTI | ar->rx_filter_caps;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200986
987 if (!IS_ACCEPTING_CMD(ar))
988 return;
989
990 mutex_lock(&ar->mutex);
991
992 ar->filter_state = *new_flags;
993 /*
994 * We can support more by setting the sniffer bit and
995 * then checking the error flags, later.
996 */
997
Nicolas Cavallari8f7f3b22011-05-04 15:26:52 +0200998 if (*new_flags & FIF_ALLMULTI)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200999 multicast = ~0ULL;
1000
1001 if (multicast != ar->cur_mc_hash)
1002 WARN_ON(carl9170_update_multicast(ar, multicast));
1003
1004 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
1005 ar->sniffer_enabled = !!(*new_flags &
1006 (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS));
1007
1008 WARN_ON(carl9170_set_operating_mode(ar));
1009 }
1010
Christian Lamparter5c895692010-09-28 23:00:59 +02001011 if (ar->fw.rx_filter && changed_flags & ar->rx_filter_caps) {
1012 u32 rx_filter = 0;
1013
Christian Lamparterc9122c02012-07-07 21:13:59 +02001014 if (!ar->fw.ba_filter)
1015 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
1016
Christian Lamparter5c895692010-09-28 23:00:59 +02001017 if (!(*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL)))
1018 rx_filter |= CARL9170_RX_FILTER_BAD;
1019
1020 if (!(*new_flags & FIF_CONTROL))
1021 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
1022
1023 if (!(*new_flags & FIF_PSPOLL))
1024 rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
1025
1026 if (!(*new_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))) {
1027 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
1028 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
1029 }
1030
1031 WARN_ON(carl9170_rx_filter(ar, rx_filter));
1032 }
1033
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001034 mutex_unlock(&ar->mutex);
1035}
1036
1037
1038static void carl9170_op_bss_info_changed(struct ieee80211_hw *hw,
1039 struct ieee80211_vif *vif,
1040 struct ieee80211_bss_conf *bss_conf,
1041 u32 changed)
1042{
1043 struct ar9170 *ar = hw->priv;
1044 struct ath_common *common = &ar->common;
1045 int err = 0;
1046 struct carl9170_vif_info *vif_priv;
1047 struct ieee80211_vif *main_vif;
1048
1049 mutex_lock(&ar->mutex);
1050 vif_priv = (void *) vif->drv_priv;
1051 main_vif = carl9170_get_main_vif(ar);
1052 if (WARN_ON(!main_vif))
1053 goto out;
1054
1055 if (changed & BSS_CHANGED_BEACON_ENABLED) {
1056 struct carl9170_vif_info *iter;
1057 int i = 0;
1058
1059 vif_priv->enable_beacon = bss_conf->enable_beacon;
1060 rcu_read_lock();
1061 list_for_each_entry_rcu(iter, &ar->vif_list, list) {
1062 if (iter->active && iter->enable_beacon)
1063 i++;
1064
1065 }
1066 rcu_read_unlock();
1067
1068 ar->beacon_enabled = i;
1069 }
1070
1071 if (changed & BSS_CHANGED_BEACON) {
1072 err = carl9170_update_beacon(ar, false);
1073 if (err)
1074 goto out;
1075 }
1076
1077 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON |
1078 BSS_CHANGED_BEACON_INT)) {
1079
1080 if (main_vif != vif) {
1081 bss_conf->beacon_int = main_vif->bss_conf.beacon_int;
1082 bss_conf->dtim_period = main_vif->bss_conf.dtim_period;
1083 }
1084
1085 /*
1086 * Therefore a hard limit for the broadcast traffic should
1087 * prevent false alarms.
1088 */
1089 if (vif->type != NL80211_IFTYPE_STATION &&
1090 (bss_conf->beacon_int * bss_conf->dtim_period >=
1091 (CARL9170_QUEUE_STUCK_TIMEOUT / 2))) {
1092 err = -EINVAL;
1093 goto out;
1094 }
1095
1096 err = carl9170_set_beacon_timers(ar);
1097 if (err)
1098 goto out;
1099 }
1100
1101 if (changed & BSS_CHANGED_HT) {
1102 /* TODO */
1103 err = 0;
1104 if (err)
1105 goto out;
1106 }
1107
1108 if (main_vif != vif)
1109 goto out;
1110
1111 /*
1112 * The following settings can only be changed by the
1113 * master interface.
1114 */
1115
1116 if (changed & BSS_CHANGED_BSSID) {
1117 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1118 err = carl9170_set_operating_mode(ar);
1119 if (err)
1120 goto out;
1121 }
1122
1123 if (changed & BSS_CHANGED_ASSOC) {
1124 ar->common.curaid = bss_conf->aid;
1125 err = carl9170_set_beacon_timers(ar);
1126 if (err)
1127 goto out;
1128 }
1129
1130 if (changed & BSS_CHANGED_ERP_SLOT) {
1131 err = carl9170_set_slot_time(ar);
1132 if (err)
1133 goto out;
1134 }
1135
1136 if (changed & BSS_CHANGED_BASIC_RATES) {
1137 err = carl9170_set_mac_rates(ar);
1138 if (err)
1139 goto out;
1140 }
1141
1142out:
1143 WARN_ON_ONCE(err && IS_STARTED(ar));
1144 mutex_unlock(&ar->mutex);
1145}
1146
Eliad Peller37a41b42011-09-21 14:06:11 +03001147static u64 carl9170_op_get_tsf(struct ieee80211_hw *hw,
1148 struct ieee80211_vif *vif)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001149{
1150 struct ar9170 *ar = hw->priv;
1151 struct carl9170_tsf_rsp tsf;
1152 int err;
1153
1154 mutex_lock(&ar->mutex);
1155 err = carl9170_exec_cmd(ar, CARL9170_CMD_READ_TSF,
1156 0, NULL, sizeof(tsf), &tsf);
1157 mutex_unlock(&ar->mutex);
1158 if (WARN_ON(err))
1159 return 0;
1160
1161 return le64_to_cpu(tsf.tsf_64);
1162}
1163
1164static int carl9170_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1165 struct ieee80211_vif *vif,
1166 struct ieee80211_sta *sta,
1167 struct ieee80211_key_conf *key)
1168{
1169 struct ar9170 *ar = hw->priv;
1170 int err = 0, i;
1171 u8 ktype;
1172
1173 if (ar->disable_offload || !vif)
1174 return -EOPNOTSUPP;
1175
Christian Lamparter7a5c7302012-12-17 16:30:05 +01001176 /* Fall back to software encryption whenever the driver is connected
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001177 * to more than one network.
1178 *
1179 * This is very unfortunate, because some machines cannot handle
1180 * the high througput speed in 802.11n networks.
1181 */
1182
Alexey Khoroshilov66cb54b2011-08-24 00:44:32 +04001183 if (!is_main_vif(ar, vif)) {
1184 mutex_lock(&ar->mutex);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001185 goto err_softw;
Alexey Khoroshilov66cb54b2011-08-24 00:44:32 +04001186 }
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001187
1188 /*
1189 * While the hardware supports *catch-all* key, for offloading
1190 * group-key en-/de-cryption. The way of how the hardware
1191 * decides which keyId maps to which key, remains a mystery...
1192 */
1193 if ((vif->type != NL80211_IFTYPE_STATION &&
1194 vif->type != NL80211_IFTYPE_ADHOC) &&
1195 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1196 return -EOPNOTSUPP;
1197
1198 switch (key->cipher) {
1199 case WLAN_CIPHER_SUITE_WEP40:
1200 ktype = AR9170_ENC_ALG_WEP64;
1201 break;
1202 case WLAN_CIPHER_SUITE_WEP104:
1203 ktype = AR9170_ENC_ALG_WEP128;
1204 break;
1205 case WLAN_CIPHER_SUITE_TKIP:
1206 ktype = AR9170_ENC_ALG_TKIP;
1207 break;
1208 case WLAN_CIPHER_SUITE_CCMP:
1209 ktype = AR9170_ENC_ALG_AESCCMP;
Christian Lampartere37b6742012-09-02 14:25:50 +02001210 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001211 break;
1212 default:
1213 return -EOPNOTSUPP;
1214 }
1215
1216 mutex_lock(&ar->mutex);
1217 if (cmd == SET_KEY) {
1218 if (!IS_STARTED(ar)) {
1219 err = -EOPNOTSUPP;
1220 goto out;
1221 }
1222
1223 if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1224 sta = NULL;
1225
1226 i = 64 + key->keyidx;
1227 } else {
1228 for (i = 0; i < 64; i++)
1229 if (!(ar->usedkeys & BIT(i)))
1230 break;
1231 if (i == 64)
1232 goto err_softw;
1233 }
1234
1235 key->hw_key_idx = i;
1236
1237 err = carl9170_upload_key(ar, i, sta ? sta->addr : NULL,
1238 ktype, 0, key->key,
1239 min_t(u8, 16, key->keylen));
1240 if (err)
1241 goto out;
1242
1243 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1244 err = carl9170_upload_key(ar, i, sta ? sta->addr :
1245 NULL, ktype, 1,
1246 key->key + 16, 16);
1247 if (err)
1248 goto out;
1249
1250 /*
1251 * hardware is not capable generating MMIC
1252 * of fragmented frames!
1253 */
1254 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1255 }
1256
1257 if (i < 64)
1258 ar->usedkeys |= BIT(i);
1259
1260 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1261 } else {
1262 if (!IS_STARTED(ar)) {
1263 /* The device is gone... together with the key ;-) */
1264 err = 0;
1265 goto out;
1266 }
1267
1268 if (key->hw_key_idx < 64) {
1269 ar->usedkeys &= ~BIT(key->hw_key_idx);
1270 } else {
1271 err = carl9170_upload_key(ar, key->hw_key_idx, NULL,
1272 AR9170_ENC_ALG_NONE, 0,
1273 NULL, 0);
1274 if (err)
1275 goto out;
1276
1277 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1278 err = carl9170_upload_key(ar, key->hw_key_idx,
1279 NULL,
1280 AR9170_ENC_ALG_NONE,
1281 1, NULL, 0);
1282 if (err)
1283 goto out;
1284 }
1285
1286 }
1287
1288 err = carl9170_disable_key(ar, key->hw_key_idx);
1289 if (err)
1290 goto out;
1291 }
1292
1293out:
1294 mutex_unlock(&ar->mutex);
1295 return err;
1296
1297err_softw:
1298 if (!ar->rx_software_decryption) {
1299 ar->rx_software_decryption = true;
1300 carl9170_set_operating_mode(ar);
1301 }
1302 mutex_unlock(&ar->mutex);
1303 return -ENOSPC;
1304}
1305
1306static int carl9170_op_sta_add(struct ieee80211_hw *hw,
1307 struct ieee80211_vif *vif,
1308 struct ieee80211_sta *sta)
1309{
1310 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1311 unsigned int i;
1312
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001313 atomic_set(&sta_info->pending_frames, 0);
1314
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001315 if (sta->ht_cap.ht_supported) {
1316 if (sta->ht_cap.ampdu_density > 6) {
1317 /*
1318 * HW does support 16us AMPDU density.
1319 * No HT-Xmit for station.
1320 */
1321
1322 return 0;
1323 }
1324
1325 for (i = 0; i < CARL9170_NUM_TID; i++)
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00001326 RCU_INIT_POINTER(sta_info->agg[i], NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001327
1328 sta_info->ampdu_max_len = 1 << (3 + sta->ht_cap.ampdu_factor);
1329 sta_info->ht_sta = true;
1330 }
1331
1332 return 0;
1333}
1334
1335static int carl9170_op_sta_remove(struct ieee80211_hw *hw,
1336 struct ieee80211_vif *vif,
1337 struct ieee80211_sta *sta)
1338{
1339 struct ar9170 *ar = hw->priv;
1340 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1341 unsigned int i;
1342 bool cleanup = false;
1343
1344 if (sta->ht_cap.ht_supported) {
1345
1346 sta_info->ht_sta = false;
1347
1348 rcu_read_lock();
1349 for (i = 0; i < CARL9170_NUM_TID; i++) {
1350 struct carl9170_sta_tid *tid_info;
1351
1352 tid_info = rcu_dereference(sta_info->agg[i]);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00001353 RCU_INIT_POINTER(sta_info->agg[i], NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001354
1355 if (!tid_info)
1356 continue;
1357
1358 spin_lock_bh(&ar->tx_ampdu_list_lock);
1359 if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
1360 tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
1361 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1362 cleanup = true;
1363 }
1364 rcu_read_unlock();
1365
1366 if (cleanup)
1367 carl9170_ampdu_gc(ar);
1368 }
1369
1370 return 0;
1371}
1372
Eliad Peller8a3a3c82011-10-02 10:15:52 +02001373static int carl9170_op_conf_tx(struct ieee80211_hw *hw,
1374 struct ieee80211_vif *vif, u16 queue,
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001375 const struct ieee80211_tx_queue_params *param)
1376{
1377 struct ar9170 *ar = hw->priv;
1378 int ret;
1379
1380 mutex_lock(&ar->mutex);
1381 if (queue < ar->hw->queues) {
1382 memcpy(&ar->edcf[ar9170_qmap[queue]], param, sizeof(*param));
1383 ret = carl9170_set_qos(ar);
1384 } else {
1385 ret = -EINVAL;
1386 }
1387
1388 mutex_unlock(&ar->mutex);
1389 return ret;
1390}
1391
1392static void carl9170_ampdu_work(struct work_struct *work)
1393{
1394 struct ar9170 *ar = container_of(work, struct ar9170,
1395 ampdu_work);
1396
1397 if (!IS_STARTED(ar))
1398 return;
1399
1400 mutex_lock(&ar->mutex);
1401 carl9170_ampdu_gc(ar);
1402 mutex_unlock(&ar->mutex);
1403}
1404
1405static int carl9170_op_ampdu_action(struct ieee80211_hw *hw,
1406 struct ieee80211_vif *vif,
1407 enum ieee80211_ampdu_mlme_action action,
1408 struct ieee80211_sta *sta,
Johannes Berg0b01f032011-01-18 13:51:05 +01001409 u16 tid, u16 *ssn, u8 buf_size)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001410{
1411 struct ar9170 *ar = hw->priv;
1412 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1413 struct carl9170_sta_tid *tid_info;
1414
1415 if (modparam_noht)
1416 return -EOPNOTSUPP;
1417
1418 switch (action) {
1419 case IEEE80211_AMPDU_TX_START:
Christian Lamparter9c655c82010-09-26 23:06:56 +02001420 if (!sta_info->ht_sta)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001421 return -EOPNOTSUPP;
1422
1423 rcu_read_lock();
1424 if (rcu_dereference(sta_info->agg[tid])) {
1425 rcu_read_unlock();
1426 return -EBUSY;
1427 }
1428
1429 tid_info = kzalloc(sizeof(struct carl9170_sta_tid),
1430 GFP_ATOMIC);
1431 if (!tid_info) {
1432 rcu_read_unlock();
1433 return -ENOMEM;
1434 }
1435
1436 tid_info->hsn = tid_info->bsn = tid_info->snx = (*ssn);
1437 tid_info->state = CARL9170_TID_STATE_PROGRESS;
1438 tid_info->tid = tid;
1439 tid_info->max = sta_info->ampdu_max_len;
1440
1441 INIT_LIST_HEAD(&tid_info->list);
1442 INIT_LIST_HEAD(&tid_info->tmp_list);
1443 skb_queue_head_init(&tid_info->queue);
1444 spin_lock_init(&tid_info->lock);
1445
1446 spin_lock_bh(&ar->tx_ampdu_list_lock);
1447 ar->tx_ampdu_list_len++;
1448 list_add_tail_rcu(&tid_info->list, &ar->tx_ampdu_list);
1449 rcu_assign_pointer(sta_info->agg[tid], tid_info);
1450 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1451 rcu_read_unlock();
1452
1453 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1454 break;
1455
Johannes Berg18b559d2012-07-18 13:51:25 +02001456 case IEEE80211_AMPDU_TX_STOP_CONT:
1457 case IEEE80211_AMPDU_TX_STOP_FLUSH:
1458 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001459 rcu_read_lock();
1460 tid_info = rcu_dereference(sta_info->agg[tid]);
1461 if (tid_info) {
1462 spin_lock_bh(&ar->tx_ampdu_list_lock);
1463 if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
1464 tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
1465 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1466 }
1467
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00001468 RCU_INIT_POINTER(sta_info->agg[tid], NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001469 rcu_read_unlock();
1470
1471 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1472 ieee80211_queue_work(ar->hw, &ar->ampdu_work);
1473 break;
1474
1475 case IEEE80211_AMPDU_TX_OPERATIONAL:
1476 rcu_read_lock();
1477 tid_info = rcu_dereference(sta_info->agg[tid]);
1478
1479 sta_info->stats[tid].clear = true;
Christian Lamparter24047e22011-03-29 13:43:14 +02001480 sta_info->stats[tid].req = false;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001481
1482 if (tid_info) {
1483 bitmap_zero(tid_info->bitmap, CARL9170_BAW_SIZE);
1484 tid_info->state = CARL9170_TID_STATE_IDLE;
1485 }
1486 rcu_read_unlock();
1487
1488 if (WARN_ON_ONCE(!tid_info))
1489 return -EFAULT;
1490
1491 break;
1492
1493 case IEEE80211_AMPDU_RX_START:
1494 case IEEE80211_AMPDU_RX_STOP:
1495 /* Handled by hardware */
1496 break;
1497
1498 default:
1499 return -EOPNOTSUPP;
1500 }
1501
1502 return 0;
1503}
1504
1505#ifdef CONFIG_CARL9170_WPC
1506static int carl9170_register_wps_button(struct ar9170 *ar)
1507{
1508 struct input_dev *input;
1509 int err;
1510
1511 if (!(ar->features & CARL9170_WPS_BUTTON))
1512 return 0;
1513
1514 input = input_allocate_device();
1515 if (!input)
1516 return -ENOMEM;
1517
1518 snprintf(ar->wps.name, sizeof(ar->wps.name), "%s WPS Button",
1519 wiphy_name(ar->hw->wiphy));
1520
1521 snprintf(ar->wps.phys, sizeof(ar->wps.phys),
1522 "ieee80211/%s/input0", wiphy_name(ar->hw->wiphy));
1523
1524 input->name = ar->wps.name;
1525 input->phys = ar->wps.phys;
1526 input->id.bustype = BUS_USB;
1527 input->dev.parent = &ar->hw->wiphy->dev;
1528
1529 input_set_capability(input, EV_KEY, KEY_WPS_BUTTON);
1530
1531 err = input_register_device(input);
1532 if (err) {
1533 input_free_device(input);
1534 return err;
1535 }
1536
1537 ar->wps.pbc = input;
1538 return 0;
1539}
1540#endif /* CONFIG_CARL9170_WPC */
1541
Christian Lamparter00044f12011-08-15 20:09:54 +02001542#ifdef CONFIG_CARL9170_HWRNG
1543static int carl9170_rng_get(struct ar9170 *ar)
1544{
1545
1546#define RW (CARL9170_MAX_CMD_PAYLOAD_LEN / sizeof(u32))
1547#define RB (CARL9170_MAX_CMD_PAYLOAD_LEN)
1548
1549 static const __le32 rng_load[RW] = {
1550 [0 ... (RW - 1)] = cpu_to_le32(AR9170_RAND_REG_NUM)};
1551
1552 u32 buf[RW];
1553
1554 unsigned int i, off = 0, transfer, count;
1555 int err;
1556
1557 BUILD_BUG_ON(RB > CARL9170_MAX_CMD_PAYLOAD_LEN);
1558
1559 if (!IS_ACCEPTING_CMD(ar) || !ar->rng.initialized)
1560 return -EAGAIN;
1561
1562 count = ARRAY_SIZE(ar->rng.cache);
1563 while (count) {
1564 err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
1565 RB, (u8 *) rng_load,
1566 RB, (u8 *) buf);
1567 if (err)
1568 return err;
1569
1570 transfer = min_t(unsigned int, count, RW);
1571 for (i = 0; i < transfer; i++)
1572 ar->rng.cache[off + i] = buf[i];
1573
1574 off += transfer;
1575 count -= transfer;
1576 }
1577
1578 ar->rng.cache_idx = 0;
1579
1580#undef RW
1581#undef RB
1582 return 0;
1583}
1584
1585static int carl9170_rng_read(struct hwrng *rng, u32 *data)
1586{
1587 struct ar9170 *ar = (struct ar9170 *)rng->priv;
1588 int ret = -EIO;
1589
1590 mutex_lock(&ar->mutex);
1591 if (ar->rng.cache_idx >= ARRAY_SIZE(ar->rng.cache)) {
1592 ret = carl9170_rng_get(ar);
1593 if (ret) {
1594 mutex_unlock(&ar->mutex);
1595 return ret;
1596 }
1597 }
1598
1599 *data = ar->rng.cache[ar->rng.cache_idx++];
1600 mutex_unlock(&ar->mutex);
1601
1602 return sizeof(u16);
1603}
1604
1605static void carl9170_unregister_hwrng(struct ar9170 *ar)
1606{
1607 if (ar->rng.initialized) {
1608 hwrng_unregister(&ar->rng.rng);
1609 ar->rng.initialized = false;
1610 }
1611}
1612
1613static int carl9170_register_hwrng(struct ar9170 *ar)
1614{
1615 int err;
1616
1617 snprintf(ar->rng.name, ARRAY_SIZE(ar->rng.name),
1618 "%s_%s", KBUILD_MODNAME, wiphy_name(ar->hw->wiphy));
1619 ar->rng.rng.name = ar->rng.name;
1620 ar->rng.rng.data_read = carl9170_rng_read;
1621 ar->rng.rng.priv = (unsigned long)ar;
1622
1623 if (WARN_ON(ar->rng.initialized))
1624 return -EALREADY;
1625
1626 err = hwrng_register(&ar->rng.rng);
1627 if (err) {
1628 dev_err(&ar->udev->dev, "Failed to register the random "
1629 "number generator (%d)\n", err);
1630 return err;
1631 }
1632
1633 ar->rng.initialized = true;
1634
1635 err = carl9170_rng_get(ar);
1636 if (err) {
1637 carl9170_unregister_hwrng(ar);
1638 return err;
1639 }
1640
1641 return 0;
1642}
1643#endif /* CONFIG_CARL9170_HWRNG */
1644
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001645static int carl9170_op_get_survey(struct ieee80211_hw *hw, int idx,
1646 struct survey_info *survey)
1647{
1648 struct ar9170 *ar = hw->priv;
Christian Lamparteracf17712011-08-15 19:50:48 +02001649 struct ieee80211_channel *chan;
1650 struct ieee80211_supported_band *band;
1651 int err, b, i;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001652
Christian Lamparteracf17712011-08-15 19:50:48 +02001653 chan = ar->channel;
1654 if (!chan)
1655 return -ENODEV;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001656
Christian Lamparteracf17712011-08-15 19:50:48 +02001657 if (idx == chan->hw_value) {
1658 mutex_lock(&ar->mutex);
1659 err = carl9170_update_survey(ar, false, true);
1660 mutex_unlock(&ar->mutex);
1661 if (err)
1662 return err;
1663 }
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001664
Christian Lamparteracf17712011-08-15 19:50:48 +02001665 for (b = 0; b < IEEE80211_NUM_BANDS; b++) {
1666 band = ar->hw->wiphy->bands[b];
1667
1668 if (!band)
1669 continue;
1670
1671 for (i = 0; i < band->n_channels; i++) {
1672 if (band->channels[i].hw_value == idx) {
1673 chan = &band->channels[i];
1674 goto found;
1675 }
1676 }
1677 }
1678 return -ENOENT;
1679
1680found:
1681 memcpy(survey, &ar->survey[idx], sizeof(*survey));
1682
1683 survey->channel = chan;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001684 survey->filled = SURVEY_INFO_NOISE_DBM;
Christian Lamparteracf17712011-08-15 19:50:48 +02001685
1686 if (ar->channel == chan)
1687 survey->filled |= SURVEY_INFO_IN_USE;
1688
1689 if (ar->fw.hw_counters) {
1690 survey->filled |= SURVEY_INFO_CHANNEL_TIME |
1691 SURVEY_INFO_CHANNEL_TIME_BUSY |
1692 SURVEY_INFO_CHANNEL_TIME_TX;
1693 }
1694
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001695 return 0;
1696}
1697
1698static void carl9170_op_flush(struct ieee80211_hw *hw, bool drop)
1699{
1700 struct ar9170 *ar = hw->priv;
1701 unsigned int vid;
1702
1703 mutex_lock(&ar->mutex);
1704 for_each_set_bit(vid, &ar->vif_bitmap, ar->fw.vif_num)
1705 carl9170_flush_cab(ar, vid);
1706
1707 carl9170_flush(ar, drop);
1708 mutex_unlock(&ar->mutex);
1709}
1710
1711static int carl9170_op_get_stats(struct ieee80211_hw *hw,
1712 struct ieee80211_low_level_stats *stats)
1713{
1714 struct ar9170 *ar = hw->priv;
1715
1716 memset(stats, 0, sizeof(*stats));
1717 stats->dot11ACKFailureCount = ar->tx_ack_failures;
1718 stats->dot11FCSErrorCount = ar->tx_fcs_errors;
1719 return 0;
1720}
1721
1722static void carl9170_op_sta_notify(struct ieee80211_hw *hw,
1723 struct ieee80211_vif *vif,
1724 enum sta_notify_cmd cmd,
1725 struct ieee80211_sta *sta)
1726{
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001727 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001728
1729 switch (cmd) {
1730 case STA_NOTIFY_SLEEP:
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001731 sta_info->sleeping = true;
1732 if (atomic_read(&sta_info->pending_frames))
1733 ieee80211_sta_block_awake(hw, sta, true);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001734 break;
1735
1736 case STA_NOTIFY_AWAKE:
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001737 sta_info->sleeping = false;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001738 break;
1739 }
1740}
1741
Christian Lamparter69f72352011-07-07 23:01:25 +02001742static bool carl9170_tx_frames_pending(struct ieee80211_hw *hw)
1743{
1744 struct ar9170 *ar = hw->priv;
1745
1746 return !!atomic_read(&ar->tx_total_queued);
1747}
1748
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001749static const struct ieee80211_ops carl9170_ops = {
1750 .start = carl9170_op_start,
1751 .stop = carl9170_op_stop,
1752 .tx = carl9170_op_tx,
1753 .flush = carl9170_op_flush,
1754 .add_interface = carl9170_op_add_interface,
1755 .remove_interface = carl9170_op_remove_interface,
1756 .config = carl9170_op_config,
1757 .prepare_multicast = carl9170_op_prepare_multicast,
1758 .configure_filter = carl9170_op_configure_filter,
1759 .conf_tx = carl9170_op_conf_tx,
1760 .bss_info_changed = carl9170_op_bss_info_changed,
1761 .get_tsf = carl9170_op_get_tsf,
1762 .set_key = carl9170_op_set_key,
1763 .sta_add = carl9170_op_sta_add,
1764 .sta_remove = carl9170_op_sta_remove,
1765 .sta_notify = carl9170_op_sta_notify,
1766 .get_survey = carl9170_op_get_survey,
1767 .get_stats = carl9170_op_get_stats,
1768 .ampdu_action = carl9170_op_ampdu_action,
Christian Lamparter69f72352011-07-07 23:01:25 +02001769 .tx_frames_pending = carl9170_tx_frames_pending,
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001770};
1771
1772void *carl9170_alloc(size_t priv_size)
1773{
1774 struct ieee80211_hw *hw;
1775 struct ar9170 *ar;
1776 struct sk_buff *skb;
1777 int i;
1778
1779 /*
1780 * this buffer is used for rx stream reconstruction.
1781 * Under heavy load this device (or the transport layer?)
1782 * tends to split the streams into separate rx descriptors.
1783 */
1784
1785 skb = __dev_alloc_skb(AR9170_RX_STREAM_MAX_SIZE, GFP_KERNEL);
1786 if (!skb)
1787 goto err_nomem;
1788
1789 hw = ieee80211_alloc_hw(priv_size, &carl9170_ops);
1790 if (!hw)
1791 goto err_nomem;
1792
1793 ar = hw->priv;
1794 ar->hw = hw;
1795 ar->rx_failover = skb;
1796
1797 memset(&ar->rx_plcp, 0, sizeof(struct ar9170_rx_head));
1798 ar->rx_has_plcp = false;
1799
1800 /*
1801 * Here's a hidden pitfall!
1802 *
1803 * All 4 AC queues work perfectly well under _legacy_ operation.
1804 * However as soon as aggregation is enabled, the traffic flow
1805 * gets very bumpy. Therefore we have to _switch_ to a
1806 * software AC with a single HW queue.
1807 */
1808 hw->queues = __AR9170_NUM_TXQ;
1809
1810 mutex_init(&ar->mutex);
1811 spin_lock_init(&ar->beacon_lock);
1812 spin_lock_init(&ar->cmd_lock);
1813 spin_lock_init(&ar->tx_stats_lock);
1814 spin_lock_init(&ar->tx_ampdu_list_lock);
1815 spin_lock_init(&ar->mem_lock);
1816 spin_lock_init(&ar->state_lock);
1817 atomic_set(&ar->pending_restarts, 0);
1818 ar->vifs = 0;
1819 for (i = 0; i < ar->hw->queues; i++) {
1820 skb_queue_head_init(&ar->tx_status[i]);
1821 skb_queue_head_init(&ar->tx_pending[i]);
Christian Lamparterc9122c02012-07-07 21:13:59 +02001822
1823 INIT_LIST_HEAD(&ar->bar_list[i]);
1824 spin_lock_init(&ar->bar_list_lock[i]);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001825 }
1826 INIT_WORK(&ar->ps_work, carl9170_ps_work);
Christian Lampartere4a668c2010-10-29 23:26:13 +02001827 INIT_WORK(&ar->ping_work, carl9170_ping_work);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001828 INIT_WORK(&ar->restart_work, carl9170_restart_work);
1829 INIT_WORK(&ar->ampdu_work, carl9170_ampdu_work);
Christian Lamparteracf17712011-08-15 19:50:48 +02001830 INIT_DELAYED_WORK(&ar->stat_work, carl9170_stat_work);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001831 INIT_DELAYED_WORK(&ar->tx_janitor, carl9170_tx_janitor);
1832 INIT_LIST_HEAD(&ar->tx_ampdu_list);
1833 rcu_assign_pointer(ar->tx_ampdu_iter,
1834 (struct carl9170_sta_tid *) &ar->tx_ampdu_list);
1835
1836 bitmap_zero(&ar->vif_bitmap, ar->fw.vif_num);
1837 INIT_LIST_HEAD(&ar->vif_list);
1838 init_completion(&ar->tx_flush);
1839
Christian Lamparterdf649622011-05-14 02:42:38 +02001840 /* firmware decides which modes we support */
1841 hw->wiphy->interface_modes = 0;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001842
1843 hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS |
Christian Lampartere37b6742012-09-02 14:25:50 +02001844 IEEE80211_HW_MFP_CAPABLE |
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001845 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
1846 IEEE80211_HW_SUPPORTS_PS |
1847 IEEE80211_HW_PS_NULLFUNC_STACK |
Christian Lamparterf3716fd2011-06-30 20:31:34 +02001848 IEEE80211_HW_NEED_DTIM_PERIOD |
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001849 IEEE80211_HW_SIGNAL_DBM;
1850
1851 if (!modparam_noht) {
1852 /*
1853 * see the comment above, why we allow the user
1854 * to disable HT by a module parameter.
1855 */
1856 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
1857 }
1858
1859 hw->extra_tx_headroom = sizeof(struct _carl9170_tx_superframe);
1860 hw->sta_data_size = sizeof(struct carl9170_sta_info);
1861 hw->vif_data_size = sizeof(struct carl9170_vif_info);
1862
1863 hw->max_rates = CARL9170_TX_MAX_RATES;
1864 hw->max_rate_tries = CARL9170_TX_USER_RATE_TRIES;
1865
1866 for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
1867 ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
1868
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001869 return ar;
1870
1871err_nomem:
1872 kfree_skb(skb);
1873 return ERR_PTR(-ENOMEM);
1874}
1875
1876static int carl9170_read_eeprom(struct ar9170 *ar)
1877{
1878#define RW 8 /* number of words to read at once */
1879#define RB (sizeof(u32) * RW)
1880 u8 *eeprom = (void *)&ar->eeprom;
1881 __le32 offsets[RW];
1882 int i, j, err;
1883
1884 BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
1885
1886 BUILD_BUG_ON(RB > CARL9170_MAX_CMD_LEN - 4);
1887#ifndef __CHECKER__
1888 /* don't want to handle trailing remains */
1889 BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
1890#endif
1891
Pavel Roskin3f29c522011-07-13 21:38:18 -04001892 for (i = 0; i < sizeof(ar->eeprom) / RB; i++) {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001893 for (j = 0; j < RW; j++)
1894 offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
1895 RB * i + 4 * j);
1896
1897 err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
1898 RB, (u8 *) &offsets,
1899 RB, eeprom + RB * i);
1900 if (err)
1901 return err;
1902 }
1903
1904#undef RW
1905#undef RB
1906 return 0;
1907}
1908
1909static int carl9170_parse_eeprom(struct ar9170 *ar)
1910{
1911 struct ath_regulatory *regulatory = &ar->common.regulatory;
1912 unsigned int rx_streams, tx_streams, tx_params = 0;
1913 int bands = 0;
Christian Lamparteracf17712011-08-15 19:50:48 +02001914 int chans = 0;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001915
1916 if (ar->eeprom.length == cpu_to_le16(0xffff))
1917 return -ENODATA;
1918
1919 rx_streams = hweight8(ar->eeprom.rx_mask);
1920 tx_streams = hweight8(ar->eeprom.tx_mask);
1921
1922 if (rx_streams != tx_streams) {
1923 tx_params = IEEE80211_HT_MCS_TX_RX_DIFF;
1924
1925 WARN_ON(!(tx_streams >= 1 && tx_streams <=
1926 IEEE80211_HT_MCS_TX_MAX_STREAMS));
1927
1928 tx_params = (tx_streams - 1) <<
1929 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1930
1931 carl9170_band_2GHz.ht_cap.mcs.tx_params |= tx_params;
1932 carl9170_band_5GHz.ht_cap.mcs.tx_params |= tx_params;
1933 }
1934
1935 if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
1936 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
1937 &carl9170_band_2GHz;
Christian Lamparteracf17712011-08-15 19:50:48 +02001938 chans += carl9170_band_2GHz.n_channels;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001939 bands++;
1940 }
1941 if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
1942 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
1943 &carl9170_band_5GHz;
Christian Lamparteracf17712011-08-15 19:50:48 +02001944 chans += carl9170_band_5GHz.n_channels;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001945 bands++;
1946 }
1947
Christian Lamparteracf17712011-08-15 19:50:48 +02001948 if (!bands)
1949 return -EINVAL;
1950
1951 ar->survey = kzalloc(sizeof(struct survey_info) * chans, GFP_KERNEL);
1952 if (!ar->survey)
1953 return -ENOMEM;
1954 ar->num_channels = chans;
1955
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001956 /*
1957 * I measured this, a bandswitch takes roughly
1958 * 135 ms and a frequency switch about 80.
1959 *
1960 * FIXME: measure these values again once EEPROM settings
1961 * are used, that will influence them!
1962 */
1963 if (bands == 2)
1964 ar->hw->channel_change_time = 135 * 1000;
1965 else
1966 ar->hw->channel_change_time = 80 * 1000;
1967
1968 regulatory->current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001969
1970 /* second part of wiphy init */
1971 SET_IEEE80211_PERM_ADDR(ar->hw, ar->eeprom.mac_address);
1972
Christian Lamparteracf17712011-08-15 19:50:48 +02001973 return 0;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001974}
1975
1976static int carl9170_reg_notifier(struct wiphy *wiphy,
1977 struct regulatory_request *request)
1978{
1979 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1980 struct ar9170 *ar = hw->priv;
1981
1982 return ath_reg_notifier_apply(wiphy, request, &ar->common.regulatory);
1983}
1984
1985int carl9170_register(struct ar9170 *ar)
1986{
1987 struct ath_regulatory *regulatory = &ar->common.regulatory;
1988 int err = 0, i;
1989
1990 if (WARN_ON(ar->mem_bitmap))
1991 return -EINVAL;
1992
1993 ar->mem_bitmap = kzalloc(roundup(ar->fw.mem_blocks, BITS_PER_LONG) *
1994 sizeof(unsigned long), GFP_KERNEL);
1995
1996 if (!ar->mem_bitmap)
1997 return -ENOMEM;
1998
1999 /* try to read EEPROM, init MAC addr */
2000 err = carl9170_read_eeprom(ar);
2001 if (err)
2002 return err;
2003
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002004 err = carl9170_parse_eeprom(ar);
2005 if (err)
2006 return err;
2007
2008 err = ath_regd_init(regulatory, ar->hw->wiphy,
2009 carl9170_reg_notifier);
2010 if (err)
2011 return err;
2012
2013 if (modparam_noht) {
2014 carl9170_band_2GHz.ht_cap.ht_supported = false;
2015 carl9170_band_5GHz.ht_cap.ht_supported = false;
2016 }
2017
2018 for (i = 0; i < ar->fw.vif_num; i++) {
2019 ar->vif_priv[i].id = i;
2020 ar->vif_priv[i].vif = NULL;
2021 }
2022
2023 err = ieee80211_register_hw(ar->hw);
2024 if (err)
2025 return err;
2026
2027 /* mac80211 interface is now registered */
2028 ar->registered = true;
2029
2030 if (!ath_is_world_regd(regulatory))
2031 regulatory_hint(ar->hw->wiphy, regulatory->alpha2);
2032
2033#ifdef CONFIG_CARL9170_DEBUGFS
2034 carl9170_debugfs_register(ar);
2035#endif /* CONFIG_CARL9170_DEBUGFS */
2036
2037 err = carl9170_led_init(ar);
2038 if (err)
2039 goto err_unreg;
2040
2041#ifdef CONFIG_CARL9170_LEDS
2042 err = carl9170_led_register(ar);
2043 if (err)
2044 goto err_unreg;
Hauke Mehrtens8e7ce892010-10-23 19:51:32 +02002045#endif /* CONFIG_CARL9170_LEDS */
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002046
2047#ifdef CONFIG_CARL9170_WPC
2048 err = carl9170_register_wps_button(ar);
2049 if (err)
2050 goto err_unreg;
2051#endif /* CONFIG_CARL9170_WPC */
2052
Christian Lamparter00044f12011-08-15 20:09:54 +02002053#ifdef CONFIG_CARL9170_HWRNG
2054 err = carl9170_register_hwrng(ar);
2055 if (err)
2056 goto err_unreg;
2057#endif /* CONFIG_CARL9170_HWRNG */
2058
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002059 dev_info(&ar->udev->dev, "Atheros AR9170 is registered as '%s'\n",
2060 wiphy_name(ar->hw->wiphy));
2061
2062 return 0;
2063
2064err_unreg:
2065 carl9170_unregister(ar);
2066 return err;
2067}
2068
2069void carl9170_unregister(struct ar9170 *ar)
2070{
2071 if (!ar->registered)
2072 return;
2073
2074 ar->registered = false;
2075
2076#ifdef CONFIG_CARL9170_LEDS
2077 carl9170_led_unregister(ar);
2078#endif /* CONFIG_CARL9170_LEDS */
2079
2080#ifdef CONFIG_CARL9170_DEBUGFS
2081 carl9170_debugfs_unregister(ar);
2082#endif /* CONFIG_CARL9170_DEBUGFS */
2083
2084#ifdef CONFIG_CARL9170_WPC
2085 if (ar->wps.pbc) {
2086 input_unregister_device(ar->wps.pbc);
2087 ar->wps.pbc = NULL;
2088 }
2089#endif /* CONFIG_CARL9170_WPC */
2090
Christian Lamparter00044f12011-08-15 20:09:54 +02002091#ifdef CONFIG_CARL9170_HWRNG
2092 carl9170_unregister_hwrng(ar);
2093#endif /* CONFIG_CARL9170_HWRNG */
2094
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002095 carl9170_cancel_worker(ar);
2096 cancel_work_sync(&ar->restart_work);
2097
2098 ieee80211_unregister_hw(ar->hw);
2099}
2100
2101void carl9170_free(struct ar9170 *ar)
2102{
2103 WARN_ON(ar->registered);
2104 WARN_ON(IS_INITIALIZED(ar));
2105
2106 kfree_skb(ar->rx_failover);
2107 ar->rx_failover = NULL;
2108
2109 kfree(ar->mem_bitmap);
2110 ar->mem_bitmap = NULL;
2111
Christian Lamparteracf17712011-08-15 19:50:48 +02002112 kfree(ar->survey);
2113 ar->survey = NULL;
2114
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002115 mutex_destroy(&ar->mutex);
2116
2117 ieee80211_free_hw(ar->hw);
2118}