blob: f293b3ff475628a389c1337df81d24cb3b3b5640 [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 Lamparter7f878b0d2012-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 Lamparter7f878b0d2012-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 Lamparter7f878b0d2012-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 Lamparter7f878b0d2012-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 Lamparter7f878b0d2012-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 Lamparter7f878b0d2012-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) {
932 /* adjust slot time for 5 GHz */
933 err = carl9170_set_slot_time(ar);
934 if (err)
935 goto out;
936
Christian Lamparteracf17712011-08-15 19:50:48 +0200937 err = carl9170_update_survey(ar, true, false);
938 if (err)
939 goto out;
940
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200941 err = carl9170_set_channel(ar, hw->conf.channel,
942 hw->conf.channel_type, CARL9170_RFI_NONE);
943 if (err)
944 goto out;
945
Christian Lamparteracf17712011-08-15 19:50:48 +0200946 err = carl9170_update_survey(ar, false, true);
947 if (err)
948 goto out;
949
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200950 err = carl9170_set_dyn_sifs_ack(ar);
951 if (err)
952 goto out;
953
954 err = carl9170_set_rts_cts_rate(ar);
955 if (err)
956 goto out;
957 }
958
Christian Lamparter67e43de2012-01-21 16:59:10 +0100959 if (changed & IEEE80211_CONF_CHANGE_POWER) {
960 err = carl9170_set_mac_tpc(ar, ar->hw->conf.channel);
961 if (err)
962 goto out;
963 }
964
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200965out:
966 mutex_unlock(&ar->mutex);
967 return err;
968}
969
970static u64 carl9170_op_prepare_multicast(struct ieee80211_hw *hw,
971 struct netdev_hw_addr_list *mc_list)
972{
973 struct netdev_hw_addr *ha;
974 u64 mchash;
975
976 /* always get broadcast frames */
977 mchash = 1ULL << (0xff >> 2);
978
979 netdev_hw_addr_list_for_each(ha, mc_list)
980 mchash |= 1ULL << (ha->addr[5] >> 2);
981
982 return mchash;
983}
984
985static void carl9170_op_configure_filter(struct ieee80211_hw *hw,
986 unsigned int changed_flags,
987 unsigned int *new_flags,
988 u64 multicast)
989{
990 struct ar9170 *ar = hw->priv;
991
992 /* mask supported flags */
Christian Lamparter5c895692010-09-28 23:00:59 +0200993 *new_flags &= FIF_ALLMULTI | ar->rx_filter_caps;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200994
995 if (!IS_ACCEPTING_CMD(ar))
996 return;
997
998 mutex_lock(&ar->mutex);
999
1000 ar->filter_state = *new_flags;
1001 /*
1002 * We can support more by setting the sniffer bit and
1003 * then checking the error flags, later.
1004 */
1005
Nicolas Cavallari8f7f3b22011-05-04 15:26:52 +02001006 if (*new_flags & FIF_ALLMULTI)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001007 multicast = ~0ULL;
1008
1009 if (multicast != ar->cur_mc_hash)
1010 WARN_ON(carl9170_update_multicast(ar, multicast));
1011
1012 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
1013 ar->sniffer_enabled = !!(*new_flags &
1014 (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS));
1015
1016 WARN_ON(carl9170_set_operating_mode(ar));
1017 }
1018
Christian Lamparter5c895692010-09-28 23:00:59 +02001019 if (ar->fw.rx_filter && changed_flags & ar->rx_filter_caps) {
1020 u32 rx_filter = 0;
1021
Christian Lamparterc9122c02012-07-07 21:13:59 +02001022 if (!ar->fw.ba_filter)
1023 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
1024
Christian Lamparter5c895692010-09-28 23:00:59 +02001025 if (!(*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL)))
1026 rx_filter |= CARL9170_RX_FILTER_BAD;
1027
1028 if (!(*new_flags & FIF_CONTROL))
1029 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
1030
1031 if (!(*new_flags & FIF_PSPOLL))
1032 rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
1033
1034 if (!(*new_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))) {
1035 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
1036 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
1037 }
1038
1039 WARN_ON(carl9170_rx_filter(ar, rx_filter));
1040 }
1041
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001042 mutex_unlock(&ar->mutex);
1043}
1044
1045
1046static void carl9170_op_bss_info_changed(struct ieee80211_hw *hw,
1047 struct ieee80211_vif *vif,
1048 struct ieee80211_bss_conf *bss_conf,
1049 u32 changed)
1050{
1051 struct ar9170 *ar = hw->priv;
1052 struct ath_common *common = &ar->common;
1053 int err = 0;
1054 struct carl9170_vif_info *vif_priv;
1055 struct ieee80211_vif *main_vif;
1056
1057 mutex_lock(&ar->mutex);
1058 vif_priv = (void *) vif->drv_priv;
1059 main_vif = carl9170_get_main_vif(ar);
1060 if (WARN_ON(!main_vif))
1061 goto out;
1062
1063 if (changed & BSS_CHANGED_BEACON_ENABLED) {
1064 struct carl9170_vif_info *iter;
1065 int i = 0;
1066
1067 vif_priv->enable_beacon = bss_conf->enable_beacon;
1068 rcu_read_lock();
1069 list_for_each_entry_rcu(iter, &ar->vif_list, list) {
1070 if (iter->active && iter->enable_beacon)
1071 i++;
1072
1073 }
1074 rcu_read_unlock();
1075
1076 ar->beacon_enabled = i;
1077 }
1078
1079 if (changed & BSS_CHANGED_BEACON) {
1080 err = carl9170_update_beacon(ar, false);
1081 if (err)
1082 goto out;
1083 }
1084
1085 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON |
1086 BSS_CHANGED_BEACON_INT)) {
1087
1088 if (main_vif != vif) {
1089 bss_conf->beacon_int = main_vif->bss_conf.beacon_int;
1090 bss_conf->dtim_period = main_vif->bss_conf.dtim_period;
1091 }
1092
1093 /*
1094 * Therefore a hard limit for the broadcast traffic should
1095 * prevent false alarms.
1096 */
1097 if (vif->type != NL80211_IFTYPE_STATION &&
1098 (bss_conf->beacon_int * bss_conf->dtim_period >=
1099 (CARL9170_QUEUE_STUCK_TIMEOUT / 2))) {
1100 err = -EINVAL;
1101 goto out;
1102 }
1103
1104 err = carl9170_set_beacon_timers(ar);
1105 if (err)
1106 goto out;
1107 }
1108
1109 if (changed & BSS_CHANGED_HT) {
1110 /* TODO */
1111 err = 0;
1112 if (err)
1113 goto out;
1114 }
1115
1116 if (main_vif != vif)
1117 goto out;
1118
1119 /*
1120 * The following settings can only be changed by the
1121 * master interface.
1122 */
1123
1124 if (changed & BSS_CHANGED_BSSID) {
1125 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1126 err = carl9170_set_operating_mode(ar);
1127 if (err)
1128 goto out;
1129 }
1130
1131 if (changed & BSS_CHANGED_ASSOC) {
1132 ar->common.curaid = bss_conf->aid;
1133 err = carl9170_set_beacon_timers(ar);
1134 if (err)
1135 goto out;
1136 }
1137
1138 if (changed & BSS_CHANGED_ERP_SLOT) {
1139 err = carl9170_set_slot_time(ar);
1140 if (err)
1141 goto out;
1142 }
1143
1144 if (changed & BSS_CHANGED_BASIC_RATES) {
1145 err = carl9170_set_mac_rates(ar);
1146 if (err)
1147 goto out;
1148 }
1149
1150out:
1151 WARN_ON_ONCE(err && IS_STARTED(ar));
1152 mutex_unlock(&ar->mutex);
1153}
1154
Eliad Peller37a41b42011-09-21 14:06:11 +03001155static u64 carl9170_op_get_tsf(struct ieee80211_hw *hw,
1156 struct ieee80211_vif *vif)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001157{
1158 struct ar9170 *ar = hw->priv;
1159 struct carl9170_tsf_rsp tsf;
1160 int err;
1161
1162 mutex_lock(&ar->mutex);
1163 err = carl9170_exec_cmd(ar, CARL9170_CMD_READ_TSF,
1164 0, NULL, sizeof(tsf), &tsf);
1165 mutex_unlock(&ar->mutex);
1166 if (WARN_ON(err))
1167 return 0;
1168
1169 return le64_to_cpu(tsf.tsf_64);
1170}
1171
1172static int carl9170_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1173 struct ieee80211_vif *vif,
1174 struct ieee80211_sta *sta,
1175 struct ieee80211_key_conf *key)
1176{
1177 struct ar9170 *ar = hw->priv;
1178 int err = 0, i;
1179 u8 ktype;
1180
1181 if (ar->disable_offload || !vif)
1182 return -EOPNOTSUPP;
1183
Christian Lamparter7a5c7302012-12-17 16:30:05 +01001184 /* Fall back to software encryption whenever the driver is connected
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001185 * to more than one network.
1186 *
1187 * This is very unfortunate, because some machines cannot handle
1188 * the high througput speed in 802.11n networks.
1189 */
1190
Alexey Khoroshilov66cb54b2011-08-24 00:44:32 +04001191 if (!is_main_vif(ar, vif)) {
1192 mutex_lock(&ar->mutex);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001193 goto err_softw;
Alexey Khoroshilov66cb54b2011-08-24 00:44:32 +04001194 }
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001195
1196 /*
1197 * While the hardware supports *catch-all* key, for offloading
1198 * group-key en-/de-cryption. The way of how the hardware
1199 * decides which keyId maps to which key, remains a mystery...
1200 */
1201 if ((vif->type != NL80211_IFTYPE_STATION &&
1202 vif->type != NL80211_IFTYPE_ADHOC) &&
1203 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1204 return -EOPNOTSUPP;
1205
1206 switch (key->cipher) {
1207 case WLAN_CIPHER_SUITE_WEP40:
1208 ktype = AR9170_ENC_ALG_WEP64;
1209 break;
1210 case WLAN_CIPHER_SUITE_WEP104:
1211 ktype = AR9170_ENC_ALG_WEP128;
1212 break;
1213 case WLAN_CIPHER_SUITE_TKIP:
1214 ktype = AR9170_ENC_ALG_TKIP;
1215 break;
1216 case WLAN_CIPHER_SUITE_CCMP:
1217 ktype = AR9170_ENC_ALG_AESCCMP;
Christian Lampartere37b6742012-09-02 14:25:50 +02001218 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001219 break;
1220 default:
1221 return -EOPNOTSUPP;
1222 }
1223
1224 mutex_lock(&ar->mutex);
1225 if (cmd == SET_KEY) {
1226 if (!IS_STARTED(ar)) {
1227 err = -EOPNOTSUPP;
1228 goto out;
1229 }
1230
1231 if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1232 sta = NULL;
1233
1234 i = 64 + key->keyidx;
1235 } else {
1236 for (i = 0; i < 64; i++)
1237 if (!(ar->usedkeys & BIT(i)))
1238 break;
1239 if (i == 64)
1240 goto err_softw;
1241 }
1242
1243 key->hw_key_idx = i;
1244
1245 err = carl9170_upload_key(ar, i, sta ? sta->addr : NULL,
1246 ktype, 0, key->key,
1247 min_t(u8, 16, key->keylen));
1248 if (err)
1249 goto out;
1250
1251 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1252 err = carl9170_upload_key(ar, i, sta ? sta->addr :
1253 NULL, ktype, 1,
1254 key->key + 16, 16);
1255 if (err)
1256 goto out;
1257
1258 /*
1259 * hardware is not capable generating MMIC
1260 * of fragmented frames!
1261 */
1262 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1263 }
1264
1265 if (i < 64)
1266 ar->usedkeys |= BIT(i);
1267
1268 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1269 } else {
1270 if (!IS_STARTED(ar)) {
1271 /* The device is gone... together with the key ;-) */
1272 err = 0;
1273 goto out;
1274 }
1275
1276 if (key->hw_key_idx < 64) {
1277 ar->usedkeys &= ~BIT(key->hw_key_idx);
1278 } else {
1279 err = carl9170_upload_key(ar, key->hw_key_idx, NULL,
1280 AR9170_ENC_ALG_NONE, 0,
1281 NULL, 0);
1282 if (err)
1283 goto out;
1284
1285 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1286 err = carl9170_upload_key(ar, key->hw_key_idx,
1287 NULL,
1288 AR9170_ENC_ALG_NONE,
1289 1, NULL, 0);
1290 if (err)
1291 goto out;
1292 }
1293
1294 }
1295
1296 err = carl9170_disable_key(ar, key->hw_key_idx);
1297 if (err)
1298 goto out;
1299 }
1300
1301out:
1302 mutex_unlock(&ar->mutex);
1303 return err;
1304
1305err_softw:
1306 if (!ar->rx_software_decryption) {
1307 ar->rx_software_decryption = true;
1308 carl9170_set_operating_mode(ar);
1309 }
1310 mutex_unlock(&ar->mutex);
1311 return -ENOSPC;
1312}
1313
1314static int carl9170_op_sta_add(struct ieee80211_hw *hw,
1315 struct ieee80211_vif *vif,
1316 struct ieee80211_sta *sta)
1317{
1318 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1319 unsigned int i;
1320
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001321 atomic_set(&sta_info->pending_frames, 0);
1322
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001323 if (sta->ht_cap.ht_supported) {
1324 if (sta->ht_cap.ampdu_density > 6) {
1325 /*
1326 * HW does support 16us AMPDU density.
1327 * No HT-Xmit for station.
1328 */
1329
1330 return 0;
1331 }
1332
Christian Lamparter558925f2012-12-20 02:44:29 +01001333 for (i = 0; i < ARRAY_SIZE(sta_info->agg); i++)
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00001334 RCU_INIT_POINTER(sta_info->agg[i], NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001335
1336 sta_info->ampdu_max_len = 1 << (3 + sta->ht_cap.ampdu_factor);
1337 sta_info->ht_sta = true;
1338 }
1339
1340 return 0;
1341}
1342
1343static int carl9170_op_sta_remove(struct ieee80211_hw *hw,
1344 struct ieee80211_vif *vif,
1345 struct ieee80211_sta *sta)
1346{
1347 struct ar9170 *ar = hw->priv;
1348 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1349 unsigned int i;
1350 bool cleanup = false;
1351
1352 if (sta->ht_cap.ht_supported) {
1353
1354 sta_info->ht_sta = false;
1355
1356 rcu_read_lock();
Christian Lamparter558925f2012-12-20 02:44:29 +01001357 for (i = 0; i < ARRAY_SIZE(sta_info->agg); i++) {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001358 struct carl9170_sta_tid *tid_info;
1359
1360 tid_info = rcu_dereference(sta_info->agg[i]);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00001361 RCU_INIT_POINTER(sta_info->agg[i], NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001362
1363 if (!tid_info)
1364 continue;
1365
1366 spin_lock_bh(&ar->tx_ampdu_list_lock);
1367 if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
1368 tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
1369 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1370 cleanup = true;
1371 }
1372 rcu_read_unlock();
1373
1374 if (cleanup)
1375 carl9170_ampdu_gc(ar);
1376 }
1377
1378 return 0;
1379}
1380
Eliad Peller8a3a3c82011-10-02 10:15:52 +02001381static int carl9170_op_conf_tx(struct ieee80211_hw *hw,
1382 struct ieee80211_vif *vif, u16 queue,
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001383 const struct ieee80211_tx_queue_params *param)
1384{
1385 struct ar9170 *ar = hw->priv;
1386 int ret;
1387
1388 mutex_lock(&ar->mutex);
1389 if (queue < ar->hw->queues) {
1390 memcpy(&ar->edcf[ar9170_qmap[queue]], param, sizeof(*param));
1391 ret = carl9170_set_qos(ar);
1392 } else {
1393 ret = -EINVAL;
1394 }
1395
1396 mutex_unlock(&ar->mutex);
1397 return ret;
1398}
1399
1400static void carl9170_ampdu_work(struct work_struct *work)
1401{
1402 struct ar9170 *ar = container_of(work, struct ar9170,
1403 ampdu_work);
1404
1405 if (!IS_STARTED(ar))
1406 return;
1407
1408 mutex_lock(&ar->mutex);
1409 carl9170_ampdu_gc(ar);
1410 mutex_unlock(&ar->mutex);
1411}
1412
1413static int carl9170_op_ampdu_action(struct ieee80211_hw *hw,
1414 struct ieee80211_vif *vif,
1415 enum ieee80211_ampdu_mlme_action action,
1416 struct ieee80211_sta *sta,
Johannes Berg0b01f032011-01-18 13:51:05 +01001417 u16 tid, u16 *ssn, u8 buf_size)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001418{
1419 struct ar9170 *ar = hw->priv;
1420 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1421 struct carl9170_sta_tid *tid_info;
1422
1423 if (modparam_noht)
1424 return -EOPNOTSUPP;
1425
1426 switch (action) {
1427 case IEEE80211_AMPDU_TX_START:
Christian Lamparter9c655c82010-09-26 23:06:56 +02001428 if (!sta_info->ht_sta)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001429 return -EOPNOTSUPP;
1430
1431 rcu_read_lock();
1432 if (rcu_dereference(sta_info->agg[tid])) {
1433 rcu_read_unlock();
1434 return -EBUSY;
1435 }
1436
1437 tid_info = kzalloc(sizeof(struct carl9170_sta_tid),
1438 GFP_ATOMIC);
1439 if (!tid_info) {
1440 rcu_read_unlock();
1441 return -ENOMEM;
1442 }
1443
1444 tid_info->hsn = tid_info->bsn = tid_info->snx = (*ssn);
1445 tid_info->state = CARL9170_TID_STATE_PROGRESS;
1446 tid_info->tid = tid;
1447 tid_info->max = sta_info->ampdu_max_len;
1448
1449 INIT_LIST_HEAD(&tid_info->list);
1450 INIT_LIST_HEAD(&tid_info->tmp_list);
1451 skb_queue_head_init(&tid_info->queue);
1452 spin_lock_init(&tid_info->lock);
1453
1454 spin_lock_bh(&ar->tx_ampdu_list_lock);
1455 ar->tx_ampdu_list_len++;
1456 list_add_tail_rcu(&tid_info->list, &ar->tx_ampdu_list);
1457 rcu_assign_pointer(sta_info->agg[tid], tid_info);
1458 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1459 rcu_read_unlock();
1460
1461 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1462 break;
1463
Johannes Berg18b559d2012-07-18 13:51:25 +02001464 case IEEE80211_AMPDU_TX_STOP_CONT:
1465 case IEEE80211_AMPDU_TX_STOP_FLUSH:
1466 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001467 rcu_read_lock();
1468 tid_info = rcu_dereference(sta_info->agg[tid]);
1469 if (tid_info) {
1470 spin_lock_bh(&ar->tx_ampdu_list_lock);
1471 if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
1472 tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
1473 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1474 }
1475
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00001476 RCU_INIT_POINTER(sta_info->agg[tid], NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001477 rcu_read_unlock();
1478
1479 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1480 ieee80211_queue_work(ar->hw, &ar->ampdu_work);
1481 break;
1482
1483 case IEEE80211_AMPDU_TX_OPERATIONAL:
1484 rcu_read_lock();
1485 tid_info = rcu_dereference(sta_info->agg[tid]);
1486
1487 sta_info->stats[tid].clear = true;
Christian Lamparter24047e22011-03-29 13:43:14 +02001488 sta_info->stats[tid].req = false;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001489
1490 if (tid_info) {
1491 bitmap_zero(tid_info->bitmap, CARL9170_BAW_SIZE);
1492 tid_info->state = CARL9170_TID_STATE_IDLE;
1493 }
1494 rcu_read_unlock();
1495
1496 if (WARN_ON_ONCE(!tid_info))
1497 return -EFAULT;
1498
1499 break;
1500
1501 case IEEE80211_AMPDU_RX_START:
1502 case IEEE80211_AMPDU_RX_STOP:
1503 /* Handled by hardware */
1504 break;
1505
1506 default:
1507 return -EOPNOTSUPP;
1508 }
1509
1510 return 0;
1511}
1512
1513#ifdef CONFIG_CARL9170_WPC
1514static int carl9170_register_wps_button(struct ar9170 *ar)
1515{
1516 struct input_dev *input;
1517 int err;
1518
1519 if (!(ar->features & CARL9170_WPS_BUTTON))
1520 return 0;
1521
1522 input = input_allocate_device();
1523 if (!input)
1524 return -ENOMEM;
1525
1526 snprintf(ar->wps.name, sizeof(ar->wps.name), "%s WPS Button",
1527 wiphy_name(ar->hw->wiphy));
1528
1529 snprintf(ar->wps.phys, sizeof(ar->wps.phys),
1530 "ieee80211/%s/input0", wiphy_name(ar->hw->wiphy));
1531
1532 input->name = ar->wps.name;
1533 input->phys = ar->wps.phys;
1534 input->id.bustype = BUS_USB;
1535 input->dev.parent = &ar->hw->wiphy->dev;
1536
1537 input_set_capability(input, EV_KEY, KEY_WPS_BUTTON);
1538
1539 err = input_register_device(input);
1540 if (err) {
1541 input_free_device(input);
1542 return err;
1543 }
1544
1545 ar->wps.pbc = input;
1546 return 0;
1547}
1548#endif /* CONFIG_CARL9170_WPC */
1549
Christian Lamparter00044f12011-08-15 20:09:54 +02001550#ifdef CONFIG_CARL9170_HWRNG
1551static int carl9170_rng_get(struct ar9170 *ar)
1552{
1553
1554#define RW (CARL9170_MAX_CMD_PAYLOAD_LEN / sizeof(u32))
1555#define RB (CARL9170_MAX_CMD_PAYLOAD_LEN)
1556
1557 static const __le32 rng_load[RW] = {
1558 [0 ... (RW - 1)] = cpu_to_le32(AR9170_RAND_REG_NUM)};
1559
1560 u32 buf[RW];
1561
1562 unsigned int i, off = 0, transfer, count;
1563 int err;
1564
1565 BUILD_BUG_ON(RB > CARL9170_MAX_CMD_PAYLOAD_LEN);
1566
1567 if (!IS_ACCEPTING_CMD(ar) || !ar->rng.initialized)
1568 return -EAGAIN;
1569
1570 count = ARRAY_SIZE(ar->rng.cache);
1571 while (count) {
1572 err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
1573 RB, (u8 *) rng_load,
1574 RB, (u8 *) buf);
1575 if (err)
1576 return err;
1577
1578 transfer = min_t(unsigned int, count, RW);
1579 for (i = 0; i < transfer; i++)
1580 ar->rng.cache[off + i] = buf[i];
1581
1582 off += transfer;
1583 count -= transfer;
1584 }
1585
1586 ar->rng.cache_idx = 0;
1587
1588#undef RW
1589#undef RB
1590 return 0;
1591}
1592
1593static int carl9170_rng_read(struct hwrng *rng, u32 *data)
1594{
1595 struct ar9170 *ar = (struct ar9170 *)rng->priv;
1596 int ret = -EIO;
1597
1598 mutex_lock(&ar->mutex);
1599 if (ar->rng.cache_idx >= ARRAY_SIZE(ar->rng.cache)) {
1600 ret = carl9170_rng_get(ar);
1601 if (ret) {
1602 mutex_unlock(&ar->mutex);
1603 return ret;
1604 }
1605 }
1606
1607 *data = ar->rng.cache[ar->rng.cache_idx++];
1608 mutex_unlock(&ar->mutex);
1609
1610 return sizeof(u16);
1611}
1612
1613static void carl9170_unregister_hwrng(struct ar9170 *ar)
1614{
1615 if (ar->rng.initialized) {
1616 hwrng_unregister(&ar->rng.rng);
1617 ar->rng.initialized = false;
1618 }
1619}
1620
1621static int carl9170_register_hwrng(struct ar9170 *ar)
1622{
1623 int err;
1624
1625 snprintf(ar->rng.name, ARRAY_SIZE(ar->rng.name),
1626 "%s_%s", KBUILD_MODNAME, wiphy_name(ar->hw->wiphy));
1627 ar->rng.rng.name = ar->rng.name;
1628 ar->rng.rng.data_read = carl9170_rng_read;
1629 ar->rng.rng.priv = (unsigned long)ar;
1630
1631 if (WARN_ON(ar->rng.initialized))
1632 return -EALREADY;
1633
1634 err = hwrng_register(&ar->rng.rng);
1635 if (err) {
1636 dev_err(&ar->udev->dev, "Failed to register the random "
1637 "number generator (%d)\n", err);
1638 return err;
1639 }
1640
1641 ar->rng.initialized = true;
1642
1643 err = carl9170_rng_get(ar);
1644 if (err) {
1645 carl9170_unregister_hwrng(ar);
1646 return err;
1647 }
1648
1649 return 0;
1650}
1651#endif /* CONFIG_CARL9170_HWRNG */
1652
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001653static int carl9170_op_get_survey(struct ieee80211_hw *hw, int idx,
1654 struct survey_info *survey)
1655{
1656 struct ar9170 *ar = hw->priv;
Christian Lamparteracf17712011-08-15 19:50:48 +02001657 struct ieee80211_channel *chan;
1658 struct ieee80211_supported_band *band;
1659 int err, b, i;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001660
Christian Lamparteracf17712011-08-15 19:50:48 +02001661 chan = ar->channel;
1662 if (!chan)
1663 return -ENODEV;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001664
Christian Lamparteracf17712011-08-15 19:50:48 +02001665 if (idx == chan->hw_value) {
1666 mutex_lock(&ar->mutex);
1667 err = carl9170_update_survey(ar, false, true);
1668 mutex_unlock(&ar->mutex);
1669 if (err)
1670 return err;
1671 }
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001672
Christian Lamparteracf17712011-08-15 19:50:48 +02001673 for (b = 0; b < IEEE80211_NUM_BANDS; b++) {
1674 band = ar->hw->wiphy->bands[b];
1675
1676 if (!band)
1677 continue;
1678
1679 for (i = 0; i < band->n_channels; i++) {
1680 if (band->channels[i].hw_value == idx) {
1681 chan = &band->channels[i];
1682 goto found;
1683 }
1684 }
1685 }
1686 return -ENOENT;
1687
1688found:
1689 memcpy(survey, &ar->survey[idx], sizeof(*survey));
1690
1691 survey->channel = chan;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001692 survey->filled = SURVEY_INFO_NOISE_DBM;
Christian Lamparteracf17712011-08-15 19:50:48 +02001693
1694 if (ar->channel == chan)
1695 survey->filled |= SURVEY_INFO_IN_USE;
1696
1697 if (ar->fw.hw_counters) {
1698 survey->filled |= SURVEY_INFO_CHANNEL_TIME |
1699 SURVEY_INFO_CHANNEL_TIME_BUSY |
1700 SURVEY_INFO_CHANNEL_TIME_TX;
1701 }
1702
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001703 return 0;
1704}
1705
1706static void carl9170_op_flush(struct ieee80211_hw *hw, bool drop)
1707{
1708 struct ar9170 *ar = hw->priv;
1709 unsigned int vid;
1710
1711 mutex_lock(&ar->mutex);
1712 for_each_set_bit(vid, &ar->vif_bitmap, ar->fw.vif_num)
1713 carl9170_flush_cab(ar, vid);
1714
1715 carl9170_flush(ar, drop);
1716 mutex_unlock(&ar->mutex);
1717}
1718
1719static int carl9170_op_get_stats(struct ieee80211_hw *hw,
1720 struct ieee80211_low_level_stats *stats)
1721{
1722 struct ar9170 *ar = hw->priv;
1723
1724 memset(stats, 0, sizeof(*stats));
1725 stats->dot11ACKFailureCount = ar->tx_ack_failures;
1726 stats->dot11FCSErrorCount = ar->tx_fcs_errors;
1727 return 0;
1728}
1729
1730static void carl9170_op_sta_notify(struct ieee80211_hw *hw,
1731 struct ieee80211_vif *vif,
1732 enum sta_notify_cmd cmd,
1733 struct ieee80211_sta *sta)
1734{
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001735 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001736
1737 switch (cmd) {
1738 case STA_NOTIFY_SLEEP:
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001739 sta_info->sleeping = true;
1740 if (atomic_read(&sta_info->pending_frames))
1741 ieee80211_sta_block_awake(hw, sta, true);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001742 break;
1743
1744 case STA_NOTIFY_AWAKE:
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001745 sta_info->sleeping = false;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001746 break;
1747 }
1748}
1749
Christian Lamparter69f72352011-07-07 23:01:25 +02001750static bool carl9170_tx_frames_pending(struct ieee80211_hw *hw)
1751{
1752 struct ar9170 *ar = hw->priv;
1753
1754 return !!atomic_read(&ar->tx_total_queued);
1755}
1756
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001757static const struct ieee80211_ops carl9170_ops = {
1758 .start = carl9170_op_start,
1759 .stop = carl9170_op_stop,
1760 .tx = carl9170_op_tx,
1761 .flush = carl9170_op_flush,
1762 .add_interface = carl9170_op_add_interface,
1763 .remove_interface = carl9170_op_remove_interface,
1764 .config = carl9170_op_config,
1765 .prepare_multicast = carl9170_op_prepare_multicast,
1766 .configure_filter = carl9170_op_configure_filter,
1767 .conf_tx = carl9170_op_conf_tx,
1768 .bss_info_changed = carl9170_op_bss_info_changed,
1769 .get_tsf = carl9170_op_get_tsf,
1770 .set_key = carl9170_op_set_key,
1771 .sta_add = carl9170_op_sta_add,
1772 .sta_remove = carl9170_op_sta_remove,
1773 .sta_notify = carl9170_op_sta_notify,
1774 .get_survey = carl9170_op_get_survey,
1775 .get_stats = carl9170_op_get_stats,
1776 .ampdu_action = carl9170_op_ampdu_action,
Christian Lamparter69f72352011-07-07 23:01:25 +02001777 .tx_frames_pending = carl9170_tx_frames_pending,
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001778};
1779
1780void *carl9170_alloc(size_t priv_size)
1781{
1782 struct ieee80211_hw *hw;
1783 struct ar9170 *ar;
1784 struct sk_buff *skb;
1785 int i;
1786
1787 /*
1788 * this buffer is used for rx stream reconstruction.
1789 * Under heavy load this device (or the transport layer?)
1790 * tends to split the streams into separate rx descriptors.
1791 */
1792
1793 skb = __dev_alloc_skb(AR9170_RX_STREAM_MAX_SIZE, GFP_KERNEL);
1794 if (!skb)
1795 goto err_nomem;
1796
1797 hw = ieee80211_alloc_hw(priv_size, &carl9170_ops);
1798 if (!hw)
1799 goto err_nomem;
1800
1801 ar = hw->priv;
1802 ar->hw = hw;
1803 ar->rx_failover = skb;
1804
1805 memset(&ar->rx_plcp, 0, sizeof(struct ar9170_rx_head));
1806 ar->rx_has_plcp = false;
1807
1808 /*
1809 * Here's a hidden pitfall!
1810 *
1811 * All 4 AC queues work perfectly well under _legacy_ operation.
1812 * However as soon as aggregation is enabled, the traffic flow
1813 * gets very bumpy. Therefore we have to _switch_ to a
1814 * software AC with a single HW queue.
1815 */
1816 hw->queues = __AR9170_NUM_TXQ;
1817
1818 mutex_init(&ar->mutex);
1819 spin_lock_init(&ar->beacon_lock);
1820 spin_lock_init(&ar->cmd_lock);
1821 spin_lock_init(&ar->tx_stats_lock);
1822 spin_lock_init(&ar->tx_ampdu_list_lock);
1823 spin_lock_init(&ar->mem_lock);
1824 spin_lock_init(&ar->state_lock);
1825 atomic_set(&ar->pending_restarts, 0);
1826 ar->vifs = 0;
1827 for (i = 0; i < ar->hw->queues; i++) {
1828 skb_queue_head_init(&ar->tx_status[i]);
1829 skb_queue_head_init(&ar->tx_pending[i]);
Christian Lamparterc9122c02012-07-07 21:13:59 +02001830
1831 INIT_LIST_HEAD(&ar->bar_list[i]);
1832 spin_lock_init(&ar->bar_list_lock[i]);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001833 }
1834 INIT_WORK(&ar->ps_work, carl9170_ps_work);
Christian Lampartere4a668c2010-10-29 23:26:13 +02001835 INIT_WORK(&ar->ping_work, carl9170_ping_work);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001836 INIT_WORK(&ar->restart_work, carl9170_restart_work);
1837 INIT_WORK(&ar->ampdu_work, carl9170_ampdu_work);
Christian Lamparteracf17712011-08-15 19:50:48 +02001838 INIT_DELAYED_WORK(&ar->stat_work, carl9170_stat_work);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001839 INIT_DELAYED_WORK(&ar->tx_janitor, carl9170_tx_janitor);
1840 INIT_LIST_HEAD(&ar->tx_ampdu_list);
1841 rcu_assign_pointer(ar->tx_ampdu_iter,
1842 (struct carl9170_sta_tid *) &ar->tx_ampdu_list);
1843
1844 bitmap_zero(&ar->vif_bitmap, ar->fw.vif_num);
1845 INIT_LIST_HEAD(&ar->vif_list);
1846 init_completion(&ar->tx_flush);
1847
Christian Lamparterdf649622011-05-14 02:42:38 +02001848 /* firmware decides which modes we support */
1849 hw->wiphy->interface_modes = 0;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001850
1851 hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS |
Christian Lampartere37b6742012-09-02 14:25:50 +02001852 IEEE80211_HW_MFP_CAPABLE |
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001853 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
1854 IEEE80211_HW_SUPPORTS_PS |
1855 IEEE80211_HW_PS_NULLFUNC_STACK |
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001856 IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC |
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001857 IEEE80211_HW_SIGNAL_DBM;
1858
1859 if (!modparam_noht) {
1860 /*
1861 * see the comment above, why we allow the user
1862 * to disable HT by a module parameter.
1863 */
1864 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
1865 }
1866
1867 hw->extra_tx_headroom = sizeof(struct _carl9170_tx_superframe);
1868 hw->sta_data_size = sizeof(struct carl9170_sta_info);
1869 hw->vif_data_size = sizeof(struct carl9170_vif_info);
1870
1871 hw->max_rates = CARL9170_TX_MAX_RATES;
1872 hw->max_rate_tries = CARL9170_TX_USER_RATE_TRIES;
1873
1874 for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
1875 ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
1876
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001877 return ar;
1878
1879err_nomem:
1880 kfree_skb(skb);
1881 return ERR_PTR(-ENOMEM);
1882}
1883
1884static int carl9170_read_eeprom(struct ar9170 *ar)
1885{
1886#define RW 8 /* number of words to read at once */
1887#define RB (sizeof(u32) * RW)
1888 u8 *eeprom = (void *)&ar->eeprom;
1889 __le32 offsets[RW];
1890 int i, j, err;
1891
1892 BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
1893
1894 BUILD_BUG_ON(RB > CARL9170_MAX_CMD_LEN - 4);
1895#ifndef __CHECKER__
1896 /* don't want to handle trailing remains */
1897 BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
1898#endif
1899
Pavel Roskin3f29c522011-07-13 21:38:18 -04001900 for (i = 0; i < sizeof(ar->eeprom) / RB; i++) {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001901 for (j = 0; j < RW; j++)
1902 offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
1903 RB * i + 4 * j);
1904
1905 err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
1906 RB, (u8 *) &offsets,
1907 RB, eeprom + RB * i);
1908 if (err)
1909 return err;
1910 }
1911
1912#undef RW
1913#undef RB
1914 return 0;
1915}
1916
1917static int carl9170_parse_eeprom(struct ar9170 *ar)
1918{
1919 struct ath_regulatory *regulatory = &ar->common.regulatory;
1920 unsigned int rx_streams, tx_streams, tx_params = 0;
1921 int bands = 0;
Christian Lamparteracf17712011-08-15 19:50:48 +02001922 int chans = 0;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001923
1924 if (ar->eeprom.length == cpu_to_le16(0xffff))
1925 return -ENODATA;
1926
1927 rx_streams = hweight8(ar->eeprom.rx_mask);
1928 tx_streams = hweight8(ar->eeprom.tx_mask);
1929
1930 if (rx_streams != tx_streams) {
1931 tx_params = IEEE80211_HT_MCS_TX_RX_DIFF;
1932
1933 WARN_ON(!(tx_streams >= 1 && tx_streams <=
1934 IEEE80211_HT_MCS_TX_MAX_STREAMS));
1935
1936 tx_params = (tx_streams - 1) <<
1937 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1938
1939 carl9170_band_2GHz.ht_cap.mcs.tx_params |= tx_params;
1940 carl9170_band_5GHz.ht_cap.mcs.tx_params |= tx_params;
1941 }
1942
1943 if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
1944 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
1945 &carl9170_band_2GHz;
Christian Lamparteracf17712011-08-15 19:50:48 +02001946 chans += carl9170_band_2GHz.n_channels;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001947 bands++;
1948 }
1949 if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
1950 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
1951 &carl9170_band_5GHz;
Christian Lamparteracf17712011-08-15 19:50:48 +02001952 chans += carl9170_band_5GHz.n_channels;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001953 bands++;
1954 }
1955
Christian Lamparteracf17712011-08-15 19:50:48 +02001956 if (!bands)
1957 return -EINVAL;
1958
1959 ar->survey = kzalloc(sizeof(struct survey_info) * chans, GFP_KERNEL);
1960 if (!ar->survey)
1961 return -ENOMEM;
1962 ar->num_channels = chans;
1963
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001964 /*
1965 * I measured this, a bandswitch takes roughly
1966 * 135 ms and a frequency switch about 80.
1967 *
1968 * FIXME: measure these values again once EEPROM settings
1969 * are used, that will influence them!
1970 */
1971 if (bands == 2)
1972 ar->hw->channel_change_time = 135 * 1000;
1973 else
1974 ar->hw->channel_change_time = 80 * 1000;
1975
1976 regulatory->current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001977
1978 /* second part of wiphy init */
1979 SET_IEEE80211_PERM_ADDR(ar->hw, ar->eeprom.mac_address);
1980
Christian Lamparteracf17712011-08-15 19:50:48 +02001981 return 0;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001982}
1983
Luis R. Rodriguez0c0280b2013-01-11 18:39:36 +00001984static void carl9170_reg_notifier(struct wiphy *wiphy,
1985 struct regulatory_request *request)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001986{
1987 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1988 struct ar9170 *ar = hw->priv;
1989
Luis R. Rodriguez0c0280b2013-01-11 18:39:36 +00001990 ath_reg_notifier_apply(wiphy, request, &ar->common.regulatory);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001991}
1992
1993int carl9170_register(struct ar9170 *ar)
1994{
1995 struct ath_regulatory *regulatory = &ar->common.regulatory;
1996 int err = 0, i;
1997
1998 if (WARN_ON(ar->mem_bitmap))
1999 return -EINVAL;
2000
2001 ar->mem_bitmap = kzalloc(roundup(ar->fw.mem_blocks, BITS_PER_LONG) *
2002 sizeof(unsigned long), GFP_KERNEL);
2003
2004 if (!ar->mem_bitmap)
2005 return -ENOMEM;
2006
2007 /* try to read EEPROM, init MAC addr */
2008 err = carl9170_read_eeprom(ar);
2009 if (err)
2010 return err;
2011
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002012 err = carl9170_parse_eeprom(ar);
2013 if (err)
2014 return err;
2015
2016 err = ath_regd_init(regulatory, ar->hw->wiphy,
2017 carl9170_reg_notifier);
2018 if (err)
2019 return err;
2020
2021 if (modparam_noht) {
2022 carl9170_band_2GHz.ht_cap.ht_supported = false;
2023 carl9170_band_5GHz.ht_cap.ht_supported = false;
2024 }
2025
2026 for (i = 0; i < ar->fw.vif_num; i++) {
2027 ar->vif_priv[i].id = i;
2028 ar->vif_priv[i].vif = NULL;
2029 }
2030
2031 err = ieee80211_register_hw(ar->hw);
2032 if (err)
2033 return err;
2034
2035 /* mac80211 interface is now registered */
2036 ar->registered = true;
2037
2038 if (!ath_is_world_regd(regulatory))
2039 regulatory_hint(ar->hw->wiphy, regulatory->alpha2);
2040
2041#ifdef CONFIG_CARL9170_DEBUGFS
2042 carl9170_debugfs_register(ar);
2043#endif /* CONFIG_CARL9170_DEBUGFS */
2044
2045 err = carl9170_led_init(ar);
2046 if (err)
2047 goto err_unreg;
2048
2049#ifdef CONFIG_CARL9170_LEDS
2050 err = carl9170_led_register(ar);
2051 if (err)
2052 goto err_unreg;
Hauke Mehrtens8e7ce892010-10-23 19:51:32 +02002053#endif /* CONFIG_CARL9170_LEDS */
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002054
2055#ifdef CONFIG_CARL9170_WPC
2056 err = carl9170_register_wps_button(ar);
2057 if (err)
2058 goto err_unreg;
2059#endif /* CONFIG_CARL9170_WPC */
2060
Christian Lamparter00044f12011-08-15 20:09:54 +02002061#ifdef CONFIG_CARL9170_HWRNG
2062 err = carl9170_register_hwrng(ar);
2063 if (err)
2064 goto err_unreg;
2065#endif /* CONFIG_CARL9170_HWRNG */
2066
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002067 dev_info(&ar->udev->dev, "Atheros AR9170 is registered as '%s'\n",
2068 wiphy_name(ar->hw->wiphy));
2069
2070 return 0;
2071
2072err_unreg:
2073 carl9170_unregister(ar);
2074 return err;
2075}
2076
2077void carl9170_unregister(struct ar9170 *ar)
2078{
2079 if (!ar->registered)
2080 return;
2081
2082 ar->registered = false;
2083
2084#ifdef CONFIG_CARL9170_LEDS
2085 carl9170_led_unregister(ar);
2086#endif /* CONFIG_CARL9170_LEDS */
2087
2088#ifdef CONFIG_CARL9170_DEBUGFS
2089 carl9170_debugfs_unregister(ar);
2090#endif /* CONFIG_CARL9170_DEBUGFS */
2091
2092#ifdef CONFIG_CARL9170_WPC
2093 if (ar->wps.pbc) {
2094 input_unregister_device(ar->wps.pbc);
2095 ar->wps.pbc = NULL;
2096 }
2097#endif /* CONFIG_CARL9170_WPC */
2098
Christian Lamparter00044f12011-08-15 20:09:54 +02002099#ifdef CONFIG_CARL9170_HWRNG
2100 carl9170_unregister_hwrng(ar);
2101#endif /* CONFIG_CARL9170_HWRNG */
2102
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002103 carl9170_cancel_worker(ar);
2104 cancel_work_sync(&ar->restart_work);
2105
2106 ieee80211_unregister_hw(ar->hw);
2107}
2108
2109void carl9170_free(struct ar9170 *ar)
2110{
2111 WARN_ON(ar->registered);
2112 WARN_ON(IS_INITIALIZED(ar));
2113
2114 kfree_skb(ar->rx_failover);
2115 ar->rx_failover = NULL;
2116
2117 kfree(ar->mem_bitmap);
2118 ar->mem_bitmap = NULL;
2119
Christian Lamparteracf17712011-08-15 19:50:48 +02002120 kfree(ar->survey);
2121 ar->survey = NULL;
2122
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002123 mutex_destroy(&ar->mutex);
2124
2125 ieee80211_free_hw(ar->hw);
2126}