blob: 67997b39aba79f0d14c47ffdbe4e85248bece20b [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;
361 ar->rx_software_decryption = false;
362 ar->disable_offload = false;
363
364 for (i = 0; i < ar->hw->queues; i++) {
365 ar->queue_stop_timeout[i] = jiffies;
366 ar->max_queue_stop_timeout[i] = 0;
367 }
368
369 atomic_set(&ar->mem_allocs, 0);
370
371 err = carl9170_usb_open(ar);
372 if (err)
373 goto out;
374
375 err = carl9170_init_mac(ar);
376 if (err)
377 goto out;
378
379 err = carl9170_set_qos(ar);
380 if (err)
381 goto out;
382
Christian Lamparter5c895692010-09-28 23:00:59 +0200383 if (ar->fw.rx_filter) {
384 err = carl9170_rx_filter(ar, CARL9170_RX_FILTER_OTHER_RA |
385 CARL9170_RX_FILTER_CTL_OTHER | CARL9170_RX_FILTER_BAD);
386 if (err)
387 goto out;
388 }
389
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200390 err = carl9170_write_reg(ar, AR9170_MAC_REG_DMA_TRIGGER,
391 AR9170_DMA_TRIGGER_RXQ);
392 if (err)
393 goto out;
394
395 /* Clear key-cache */
396 for (i = 0; i < AR9170_CAM_MAX_USER + 4; i++) {
397 err = carl9170_upload_key(ar, i, NULL, AR9170_ENC_ALG_NONE,
398 0, NULL, 0);
399 if (err)
400 goto out;
401
402 err = carl9170_upload_key(ar, i, NULL, AR9170_ENC_ALG_NONE,
403 1, NULL, 0);
404 if (err)
405 goto out;
406
407 if (i < AR9170_CAM_MAX_USER) {
408 err = carl9170_disable_key(ar, i);
409 if (err)
410 goto out;
411 }
412 }
413
414 carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STARTED);
415
Christian Lamparteracf17712011-08-15 19:50:48 +0200416 ieee80211_queue_delayed_work(ar->hw, &ar->stat_work,
417 round_jiffies(msecs_to_jiffies(CARL9170_STAT_WORK)));
418
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200419 ieee80211_wake_queues(ar->hw);
420 err = 0;
421
422out:
423 mutex_unlock(&ar->mutex);
424 return err;
425}
426
427static void carl9170_cancel_worker(struct ar9170 *ar)
428{
Christian Lamparteracf17712011-08-15 19:50:48 +0200429 cancel_delayed_work_sync(&ar->stat_work);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200430 cancel_delayed_work_sync(&ar->tx_janitor);
431#ifdef CONFIG_CARL9170_LEDS
432 cancel_delayed_work_sync(&ar->led_work);
433#endif /* CONFIG_CARL9170_LEDS */
434 cancel_work_sync(&ar->ps_work);
Christian Lampartere4a668c2010-10-29 23:26:13 +0200435 cancel_work_sync(&ar->ping_work);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200436 cancel_work_sync(&ar->ampdu_work);
437}
438
439static void carl9170_op_stop(struct ieee80211_hw *hw)
440{
441 struct ar9170 *ar = hw->priv;
442
443 carl9170_set_state_when(ar, CARL9170_STARTED, CARL9170_IDLE);
444
445 ieee80211_stop_queues(ar->hw);
446
447 mutex_lock(&ar->mutex);
448 if (IS_ACCEPTING_CMD(ar)) {
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000449 RCU_INIT_POINTER(ar->beacon_iter, NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200450
451 carl9170_led_set_state(ar, 0);
452
453 /* stop DMA */
454 carl9170_write_reg(ar, AR9170_MAC_REG_DMA_TRIGGER, 0);
455 carl9170_usb_stop(ar);
456 }
457
458 carl9170_zap_queues(ar);
459 mutex_unlock(&ar->mutex);
460
461 carl9170_cancel_worker(ar);
462}
463
464static void carl9170_restart_work(struct work_struct *work)
465{
466 struct ar9170 *ar = container_of(work, struct ar9170,
467 restart_work);
468 int err;
469
470 ar->usedkeys = 0;
471 ar->filter_state = 0;
472 carl9170_cancel_worker(ar);
473
474 mutex_lock(&ar->mutex);
475 err = carl9170_usb_restart(ar);
476 if (net_ratelimit()) {
477 if (err) {
478 dev_err(&ar->udev->dev, "Failed to restart device "
479 " (%d).\n", err);
480 } else {
481 dev_info(&ar->udev->dev, "device restarted "
482 "successfully.\n");
483 }
484 }
485
486 carl9170_zap_queues(ar);
487 mutex_unlock(&ar->mutex);
488 if (!err) {
489 ar->restart_counter++;
490 atomic_set(&ar->pending_restarts, 0);
491
492 ieee80211_restart_hw(ar->hw);
493 } else {
494 /*
495 * The reset was unsuccessful and the device seems to
496 * be dead. But there's still one option: a low-level
497 * usb subsystem reset...
498 */
499
500 carl9170_usb_reset(ar);
501 }
502}
503
504void carl9170_restart(struct ar9170 *ar, const enum carl9170_restart_reasons r)
505{
506 carl9170_set_state_when(ar, CARL9170_STARTED, CARL9170_IDLE);
507
508 /*
509 * Sometimes, an error can trigger several different reset events.
510 * By ignoring these *surplus* reset events, the device won't be
511 * killed again, right after it has recovered.
512 */
513 if (atomic_inc_return(&ar->pending_restarts) > 1) {
514 dev_dbg(&ar->udev->dev, "ignoring restart (%d)\n", r);
515 return;
516 }
517
518 ieee80211_stop_queues(ar->hw);
519
520 dev_err(&ar->udev->dev, "restart device (%d)\n", r);
521
522 if (!WARN_ON(r == CARL9170_RR_NO_REASON) ||
523 !WARN_ON(r >= __CARL9170_RR_LAST))
524 ar->last_reason = r;
525
526 if (!ar->registered)
527 return;
528
529 if (IS_ACCEPTING_CMD(ar) && !ar->needs_full_reset)
530 ieee80211_queue_work(ar->hw, &ar->restart_work);
531 else
532 carl9170_usb_reset(ar);
533
534 /*
535 * At this point, the device instance might have vanished/disabled.
536 * So, don't put any code which access the ar9170 struct
537 * without proper protection.
538 */
539}
540
Christian Lampartere4a668c2010-10-29 23:26:13 +0200541static void carl9170_ping_work(struct work_struct *work)
542{
543 struct ar9170 *ar = container_of(work, struct ar9170, ping_work);
544 int err;
545
546 if (!IS_STARTED(ar))
547 return;
548
549 mutex_lock(&ar->mutex);
550 err = carl9170_echo_test(ar, 0xdeadbeef);
551 if (err)
552 carl9170_restart(ar, CARL9170_RR_UNRESPONSIVE_DEVICE);
553 mutex_unlock(&ar->mutex);
554}
555
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200556static int carl9170_init_interface(struct ar9170 *ar,
557 struct ieee80211_vif *vif)
558{
559 struct ath_common *common = &ar->common;
560 int err;
561
562 if (!vif) {
563 WARN_ON_ONCE(IS_STARTED(ar));
564 return 0;
565 }
566
567 memcpy(common->macaddr, vif->addr, ETH_ALEN);
568
569 if (modparam_nohwcrypt ||
570 ((vif->type != NL80211_IFTYPE_STATION) &&
571 (vif->type != NL80211_IFTYPE_AP))) {
572 ar->rx_software_decryption = true;
573 ar->disable_offload = true;
574 }
575
576 err = carl9170_set_operating_mode(ar);
577 return err;
578}
579
580static int carl9170_op_add_interface(struct ieee80211_hw *hw,
581 struct ieee80211_vif *vif)
582{
583 struct carl9170_vif_info *vif_priv = (void *) vif->drv_priv;
584 struct ieee80211_vif *main_vif;
585 struct ar9170 *ar = hw->priv;
586 int vif_id = -1, err = 0;
587
588 mutex_lock(&ar->mutex);
589 rcu_read_lock();
590 if (vif_priv->active) {
591 /*
592 * Skip the interface structure initialization,
593 * if the vif survived the _restart call.
594 */
595 vif_id = vif_priv->id;
596 vif_priv->enable_beacon = false;
597
598 spin_lock_bh(&ar->beacon_lock);
599 dev_kfree_skb_any(vif_priv->beacon);
600 vif_priv->beacon = NULL;
601 spin_unlock_bh(&ar->beacon_lock);
602
603 goto init;
604 }
605
606 main_vif = carl9170_get_main_vif(ar);
607
608 if (main_vif) {
609 switch (main_vif->type) {
610 case NL80211_IFTYPE_STATION:
611 if (vif->type == NL80211_IFTYPE_STATION)
612 break;
613
614 err = -EBUSY;
615 rcu_read_unlock();
616
617 goto unlock;
618
Javier Lopezda93c262012-07-27 11:27:25 -0700619 case NL80211_IFTYPE_MESH_POINT:
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200620 case NL80211_IFTYPE_AP:
621 if ((vif->type == NL80211_IFTYPE_STATION) ||
622 (vif->type == NL80211_IFTYPE_WDS) ||
Javier Lopezda93c262012-07-27 11:27:25 -0700623 (vif->type == NL80211_IFTYPE_AP) ||
624 (vif->type == NL80211_IFTYPE_MESH_POINT))
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200625 break;
626
627 err = -EBUSY;
628 rcu_read_unlock();
629 goto unlock;
630
631 default:
632 rcu_read_unlock();
633 goto unlock;
634 }
635 }
636
637 vif_id = bitmap_find_free_region(&ar->vif_bitmap, ar->fw.vif_num, 0);
638
639 if (vif_id < 0) {
640 rcu_read_unlock();
641
642 err = -ENOSPC;
643 goto unlock;
644 }
645
646 BUG_ON(ar->vif_priv[vif_id].id != vif_id);
647
648 vif_priv->active = true;
649 vif_priv->id = vif_id;
650 vif_priv->enable_beacon = false;
651 ar->vifs++;
652 list_add_tail_rcu(&vif_priv->list, &ar->vif_list);
653 rcu_assign_pointer(ar->vif_priv[vif_id].vif, vif);
654
655init:
656 if (carl9170_get_main_vif(ar) == vif) {
657 rcu_assign_pointer(ar->beacon_iter, vif_priv);
658 rcu_read_unlock();
659
660 err = carl9170_init_interface(ar, vif);
661 if (err)
662 goto unlock;
663 } else {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200664 rcu_read_unlock();
Christian Lamparterdafeac32010-10-23 15:02:02 +0200665 err = carl9170_mod_virtual_mac(ar, vif_id, vif->addr);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200666
667 if (err)
668 goto unlock;
669 }
670
Christian Lamparteraa324522011-01-23 00:18:28 +0100671 if (ar->fw.tx_seq_table) {
672 err = carl9170_write_reg(ar, ar->fw.tx_seq_table + vif_id * 4,
673 0);
674 if (err)
675 goto unlock;
676 }
677
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200678unlock:
Christian Lamparterb3974922010-11-20 13:15:27 +0100679 if (err && (vif_id >= 0)) {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200680 vif_priv->active = false;
681 bitmap_release_region(&ar->vif_bitmap, vif_id, 0);
682 ar->vifs--;
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000683 RCU_INIT_POINTER(ar->vif_priv[vif_id].vif, NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200684 list_del_rcu(&vif_priv->list);
685 mutex_unlock(&ar->mutex);
686 synchronize_rcu();
687 } else {
688 if (ar->vifs > 1)
689 ar->ps.off_override |= PS_OFF_VIF;
690
691 mutex_unlock(&ar->mutex);
692 }
693
694 return err;
695}
696
697static void carl9170_op_remove_interface(struct ieee80211_hw *hw,
698 struct ieee80211_vif *vif)
699{
700 struct carl9170_vif_info *vif_priv = (void *) vif->drv_priv;
701 struct ieee80211_vif *main_vif;
702 struct ar9170 *ar = hw->priv;
703 unsigned int id;
704
705 mutex_lock(&ar->mutex);
706
707 if (WARN_ON_ONCE(!vif_priv->active))
708 goto unlock;
709
710 ar->vifs--;
711
712 rcu_read_lock();
713 main_vif = carl9170_get_main_vif(ar);
714
715 id = vif_priv->id;
716
717 vif_priv->active = false;
718 WARN_ON(vif_priv->enable_beacon);
719 vif_priv->enable_beacon = false;
720 list_del_rcu(&vif_priv->list);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000721 RCU_INIT_POINTER(ar->vif_priv[id].vif, NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200722
723 if (vif == main_vif) {
724 rcu_read_unlock();
725
726 if (ar->vifs) {
727 WARN_ON(carl9170_init_interface(ar,
728 carl9170_get_main_vif(ar)));
729 } else {
730 carl9170_set_operating_mode(ar);
731 }
732 } else {
733 rcu_read_unlock();
734
735 WARN_ON(carl9170_mod_virtual_mac(ar, id, NULL));
736 }
737
738 carl9170_update_beacon(ar, false);
739 carl9170_flush_cab(ar, id);
740
741 spin_lock_bh(&ar->beacon_lock);
742 dev_kfree_skb_any(vif_priv->beacon);
743 vif_priv->beacon = NULL;
744 spin_unlock_bh(&ar->beacon_lock);
745
746 bitmap_release_region(&ar->vif_bitmap, id, 0);
747
748 carl9170_set_beacon_timers(ar);
749
750 if (ar->vifs == 1)
751 ar->ps.off_override &= ~PS_OFF_VIF;
752
753unlock:
754 mutex_unlock(&ar->mutex);
755
756 synchronize_rcu();
757}
758
759void carl9170_ps_check(struct ar9170 *ar)
760{
761 ieee80211_queue_work(ar->hw, &ar->ps_work);
762}
763
764/* caller must hold ar->mutex */
765static int carl9170_ps_update(struct ar9170 *ar)
766{
767 bool ps = false;
768 int err = 0;
769
770 if (!ar->ps.off_override)
771 ps = (ar->hw->conf.flags & IEEE80211_CONF_PS);
772
773 if (ps != ar->ps.state) {
774 err = carl9170_powersave(ar, ps);
775 if (err)
776 return err;
777
778 if (ar->ps.state && !ps) {
779 ar->ps.sleep_ms = jiffies_to_msecs(jiffies -
780 ar->ps.last_action);
781 }
782
783 if (ps)
784 ar->ps.last_slept = jiffies;
785
786 ar->ps.last_action = jiffies;
787 ar->ps.state = ps;
788 }
789
790 return 0;
791}
792
793static void carl9170_ps_work(struct work_struct *work)
794{
795 struct ar9170 *ar = container_of(work, struct ar9170,
796 ps_work);
797 mutex_lock(&ar->mutex);
798 if (IS_STARTED(ar))
799 WARN_ON_ONCE(carl9170_ps_update(ar) != 0);
800 mutex_unlock(&ar->mutex);
801}
802
Christian Lamparteracf17712011-08-15 19:50:48 +0200803static int carl9170_update_survey(struct ar9170 *ar, bool flush, bool noise)
804{
805 int err;
806
807 if (noise) {
808 err = carl9170_get_noisefloor(ar);
809 if (err)
810 return err;
811 }
812
813 if (ar->fw.hw_counters) {
814 err = carl9170_collect_tally(ar);
815 if (err)
816 return err;
817 }
818
819 if (flush)
820 memset(&ar->tally, 0, sizeof(ar->tally));
821
822 return 0;
823}
824
825static void carl9170_stat_work(struct work_struct *work)
826{
827 struct ar9170 *ar = container_of(work, struct ar9170, stat_work.work);
828 int err;
829
830 mutex_lock(&ar->mutex);
831 err = carl9170_update_survey(ar, false, true);
832 mutex_unlock(&ar->mutex);
833
834 if (err)
835 return;
836
837 ieee80211_queue_delayed_work(ar->hw, &ar->stat_work,
838 round_jiffies(msecs_to_jiffies(CARL9170_STAT_WORK)));
839}
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200840
841static int carl9170_op_config(struct ieee80211_hw *hw, u32 changed)
842{
843 struct ar9170 *ar = hw->priv;
844 int err = 0;
845
846 mutex_lock(&ar->mutex);
847 if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
848 /* TODO */
849 err = 0;
850 }
851
852 if (changed & IEEE80211_CONF_CHANGE_PS) {
853 err = carl9170_ps_update(ar);
854 if (err)
855 goto out;
856 }
857
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200858 if (changed & IEEE80211_CONF_CHANGE_SMPS) {
859 /* TODO */
860 err = 0;
861 }
862
863 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
864 /* adjust slot time for 5 GHz */
865 err = carl9170_set_slot_time(ar);
866 if (err)
867 goto out;
868
Christian Lamparteracf17712011-08-15 19:50:48 +0200869 err = carl9170_update_survey(ar, true, false);
870 if (err)
871 goto out;
872
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200873 err = carl9170_set_channel(ar, hw->conf.channel,
874 hw->conf.channel_type, CARL9170_RFI_NONE);
875 if (err)
876 goto out;
877
Christian Lamparteracf17712011-08-15 19:50:48 +0200878 err = carl9170_update_survey(ar, false, true);
879 if (err)
880 goto out;
881
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200882 err = carl9170_set_dyn_sifs_ack(ar);
883 if (err)
884 goto out;
885
886 err = carl9170_set_rts_cts_rate(ar);
887 if (err)
888 goto out;
889 }
890
Christian Lamparter67e43de2012-01-21 16:59:10 +0100891 if (changed & IEEE80211_CONF_CHANGE_POWER) {
892 err = carl9170_set_mac_tpc(ar, ar->hw->conf.channel);
893 if (err)
894 goto out;
895 }
896
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200897out:
898 mutex_unlock(&ar->mutex);
899 return err;
900}
901
902static u64 carl9170_op_prepare_multicast(struct ieee80211_hw *hw,
903 struct netdev_hw_addr_list *mc_list)
904{
905 struct netdev_hw_addr *ha;
906 u64 mchash;
907
908 /* always get broadcast frames */
909 mchash = 1ULL << (0xff >> 2);
910
911 netdev_hw_addr_list_for_each(ha, mc_list)
912 mchash |= 1ULL << (ha->addr[5] >> 2);
913
914 return mchash;
915}
916
917static void carl9170_op_configure_filter(struct ieee80211_hw *hw,
918 unsigned int changed_flags,
919 unsigned int *new_flags,
920 u64 multicast)
921{
922 struct ar9170 *ar = hw->priv;
923
924 /* mask supported flags */
Christian Lamparter5c895692010-09-28 23:00:59 +0200925 *new_flags &= FIF_ALLMULTI | ar->rx_filter_caps;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200926
927 if (!IS_ACCEPTING_CMD(ar))
928 return;
929
930 mutex_lock(&ar->mutex);
931
932 ar->filter_state = *new_flags;
933 /*
934 * We can support more by setting the sniffer bit and
935 * then checking the error flags, later.
936 */
937
Nicolas Cavallari8f7f3b22011-05-04 15:26:52 +0200938 if (*new_flags & FIF_ALLMULTI)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200939 multicast = ~0ULL;
940
941 if (multicast != ar->cur_mc_hash)
942 WARN_ON(carl9170_update_multicast(ar, multicast));
943
944 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
945 ar->sniffer_enabled = !!(*new_flags &
946 (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS));
947
948 WARN_ON(carl9170_set_operating_mode(ar));
949 }
950
Christian Lamparter5c895692010-09-28 23:00:59 +0200951 if (ar->fw.rx_filter && changed_flags & ar->rx_filter_caps) {
952 u32 rx_filter = 0;
953
Christian Lamparterc9122c02012-07-07 21:13:59 +0200954 if (!ar->fw.ba_filter)
955 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
956
Christian Lamparter5c895692010-09-28 23:00:59 +0200957 if (!(*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL)))
958 rx_filter |= CARL9170_RX_FILTER_BAD;
959
960 if (!(*new_flags & FIF_CONTROL))
961 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
962
963 if (!(*new_flags & FIF_PSPOLL))
964 rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
965
966 if (!(*new_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))) {
967 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
968 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
969 }
970
971 WARN_ON(carl9170_rx_filter(ar, rx_filter));
972 }
973
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +0200974 mutex_unlock(&ar->mutex);
975}
976
977
978static void carl9170_op_bss_info_changed(struct ieee80211_hw *hw,
979 struct ieee80211_vif *vif,
980 struct ieee80211_bss_conf *bss_conf,
981 u32 changed)
982{
983 struct ar9170 *ar = hw->priv;
984 struct ath_common *common = &ar->common;
985 int err = 0;
986 struct carl9170_vif_info *vif_priv;
987 struct ieee80211_vif *main_vif;
988
989 mutex_lock(&ar->mutex);
990 vif_priv = (void *) vif->drv_priv;
991 main_vif = carl9170_get_main_vif(ar);
992 if (WARN_ON(!main_vif))
993 goto out;
994
995 if (changed & BSS_CHANGED_BEACON_ENABLED) {
996 struct carl9170_vif_info *iter;
997 int i = 0;
998
999 vif_priv->enable_beacon = bss_conf->enable_beacon;
1000 rcu_read_lock();
1001 list_for_each_entry_rcu(iter, &ar->vif_list, list) {
1002 if (iter->active && iter->enable_beacon)
1003 i++;
1004
1005 }
1006 rcu_read_unlock();
1007
1008 ar->beacon_enabled = i;
1009 }
1010
1011 if (changed & BSS_CHANGED_BEACON) {
1012 err = carl9170_update_beacon(ar, false);
1013 if (err)
1014 goto out;
1015 }
1016
1017 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON |
1018 BSS_CHANGED_BEACON_INT)) {
1019
1020 if (main_vif != vif) {
1021 bss_conf->beacon_int = main_vif->bss_conf.beacon_int;
1022 bss_conf->dtim_period = main_vif->bss_conf.dtim_period;
1023 }
1024
1025 /*
1026 * Therefore a hard limit for the broadcast traffic should
1027 * prevent false alarms.
1028 */
1029 if (vif->type != NL80211_IFTYPE_STATION &&
1030 (bss_conf->beacon_int * bss_conf->dtim_period >=
1031 (CARL9170_QUEUE_STUCK_TIMEOUT / 2))) {
1032 err = -EINVAL;
1033 goto out;
1034 }
1035
1036 err = carl9170_set_beacon_timers(ar);
1037 if (err)
1038 goto out;
1039 }
1040
1041 if (changed & BSS_CHANGED_HT) {
1042 /* TODO */
1043 err = 0;
1044 if (err)
1045 goto out;
1046 }
1047
1048 if (main_vif != vif)
1049 goto out;
1050
1051 /*
1052 * The following settings can only be changed by the
1053 * master interface.
1054 */
1055
1056 if (changed & BSS_CHANGED_BSSID) {
1057 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1058 err = carl9170_set_operating_mode(ar);
1059 if (err)
1060 goto out;
1061 }
1062
1063 if (changed & BSS_CHANGED_ASSOC) {
1064 ar->common.curaid = bss_conf->aid;
1065 err = carl9170_set_beacon_timers(ar);
1066 if (err)
1067 goto out;
1068 }
1069
1070 if (changed & BSS_CHANGED_ERP_SLOT) {
1071 err = carl9170_set_slot_time(ar);
1072 if (err)
1073 goto out;
1074 }
1075
1076 if (changed & BSS_CHANGED_BASIC_RATES) {
1077 err = carl9170_set_mac_rates(ar);
1078 if (err)
1079 goto out;
1080 }
1081
1082out:
1083 WARN_ON_ONCE(err && IS_STARTED(ar));
1084 mutex_unlock(&ar->mutex);
1085}
1086
Eliad Peller37a41b42011-09-21 14:06:11 +03001087static u64 carl9170_op_get_tsf(struct ieee80211_hw *hw,
1088 struct ieee80211_vif *vif)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001089{
1090 struct ar9170 *ar = hw->priv;
1091 struct carl9170_tsf_rsp tsf;
1092 int err;
1093
1094 mutex_lock(&ar->mutex);
1095 err = carl9170_exec_cmd(ar, CARL9170_CMD_READ_TSF,
1096 0, NULL, sizeof(tsf), &tsf);
1097 mutex_unlock(&ar->mutex);
1098 if (WARN_ON(err))
1099 return 0;
1100
1101 return le64_to_cpu(tsf.tsf_64);
1102}
1103
1104static int carl9170_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1105 struct ieee80211_vif *vif,
1106 struct ieee80211_sta *sta,
1107 struct ieee80211_key_conf *key)
1108{
1109 struct ar9170 *ar = hw->priv;
1110 int err = 0, i;
1111 u8 ktype;
1112
1113 if (ar->disable_offload || !vif)
1114 return -EOPNOTSUPP;
1115
1116 /*
1117 * We have to fall back to software encryption, whenever
1118 * the user choose to participates in an IBSS or is connected
1119 * to more than one network.
1120 *
1121 * This is very unfortunate, because some machines cannot handle
1122 * the high througput speed in 802.11n networks.
1123 */
1124
Alexey Khoroshilov66cb54b2011-08-24 00:44:32 +04001125 if (!is_main_vif(ar, vif)) {
1126 mutex_lock(&ar->mutex);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001127 goto err_softw;
Alexey Khoroshilov66cb54b2011-08-24 00:44:32 +04001128 }
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001129
1130 /*
1131 * While the hardware supports *catch-all* key, for offloading
1132 * group-key en-/de-cryption. The way of how the hardware
1133 * decides which keyId maps to which key, remains a mystery...
1134 */
1135 if ((vif->type != NL80211_IFTYPE_STATION &&
1136 vif->type != NL80211_IFTYPE_ADHOC) &&
1137 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1138 return -EOPNOTSUPP;
1139
1140 switch (key->cipher) {
1141 case WLAN_CIPHER_SUITE_WEP40:
1142 ktype = AR9170_ENC_ALG_WEP64;
1143 break;
1144 case WLAN_CIPHER_SUITE_WEP104:
1145 ktype = AR9170_ENC_ALG_WEP128;
1146 break;
1147 case WLAN_CIPHER_SUITE_TKIP:
1148 ktype = AR9170_ENC_ALG_TKIP;
1149 break;
1150 case WLAN_CIPHER_SUITE_CCMP:
1151 ktype = AR9170_ENC_ALG_AESCCMP;
Christian Lampartere37b6742012-09-02 14:25:50 +02001152 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001153 break;
1154 default:
1155 return -EOPNOTSUPP;
1156 }
1157
1158 mutex_lock(&ar->mutex);
1159 if (cmd == SET_KEY) {
1160 if (!IS_STARTED(ar)) {
1161 err = -EOPNOTSUPP;
1162 goto out;
1163 }
1164
1165 if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1166 sta = NULL;
1167
1168 i = 64 + key->keyidx;
1169 } else {
1170 for (i = 0; i < 64; i++)
1171 if (!(ar->usedkeys & BIT(i)))
1172 break;
1173 if (i == 64)
1174 goto err_softw;
1175 }
1176
1177 key->hw_key_idx = i;
1178
1179 err = carl9170_upload_key(ar, i, sta ? sta->addr : NULL,
1180 ktype, 0, key->key,
1181 min_t(u8, 16, key->keylen));
1182 if (err)
1183 goto out;
1184
1185 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1186 err = carl9170_upload_key(ar, i, sta ? sta->addr :
1187 NULL, ktype, 1,
1188 key->key + 16, 16);
1189 if (err)
1190 goto out;
1191
1192 /*
1193 * hardware is not capable generating MMIC
1194 * of fragmented frames!
1195 */
1196 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1197 }
1198
1199 if (i < 64)
1200 ar->usedkeys |= BIT(i);
1201
1202 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1203 } else {
1204 if (!IS_STARTED(ar)) {
1205 /* The device is gone... together with the key ;-) */
1206 err = 0;
1207 goto out;
1208 }
1209
1210 if (key->hw_key_idx < 64) {
1211 ar->usedkeys &= ~BIT(key->hw_key_idx);
1212 } else {
1213 err = carl9170_upload_key(ar, key->hw_key_idx, NULL,
1214 AR9170_ENC_ALG_NONE, 0,
1215 NULL, 0);
1216 if (err)
1217 goto out;
1218
1219 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1220 err = carl9170_upload_key(ar, key->hw_key_idx,
1221 NULL,
1222 AR9170_ENC_ALG_NONE,
1223 1, NULL, 0);
1224 if (err)
1225 goto out;
1226 }
1227
1228 }
1229
1230 err = carl9170_disable_key(ar, key->hw_key_idx);
1231 if (err)
1232 goto out;
1233 }
1234
1235out:
1236 mutex_unlock(&ar->mutex);
1237 return err;
1238
1239err_softw:
1240 if (!ar->rx_software_decryption) {
1241 ar->rx_software_decryption = true;
1242 carl9170_set_operating_mode(ar);
1243 }
1244 mutex_unlock(&ar->mutex);
1245 return -ENOSPC;
1246}
1247
1248static int carl9170_op_sta_add(struct ieee80211_hw *hw,
1249 struct ieee80211_vif *vif,
1250 struct ieee80211_sta *sta)
1251{
1252 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1253 unsigned int i;
1254
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001255 atomic_set(&sta_info->pending_frames, 0);
1256
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001257 if (sta->ht_cap.ht_supported) {
1258 if (sta->ht_cap.ampdu_density > 6) {
1259 /*
1260 * HW does support 16us AMPDU density.
1261 * No HT-Xmit for station.
1262 */
1263
1264 return 0;
1265 }
1266
1267 for (i = 0; i < CARL9170_NUM_TID; i++)
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00001268 RCU_INIT_POINTER(sta_info->agg[i], NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001269
1270 sta_info->ampdu_max_len = 1 << (3 + sta->ht_cap.ampdu_factor);
1271 sta_info->ht_sta = true;
1272 }
1273
1274 return 0;
1275}
1276
1277static int carl9170_op_sta_remove(struct ieee80211_hw *hw,
1278 struct ieee80211_vif *vif,
1279 struct ieee80211_sta *sta)
1280{
1281 struct ar9170 *ar = hw->priv;
1282 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1283 unsigned int i;
1284 bool cleanup = false;
1285
1286 if (sta->ht_cap.ht_supported) {
1287
1288 sta_info->ht_sta = false;
1289
1290 rcu_read_lock();
1291 for (i = 0; i < CARL9170_NUM_TID; i++) {
1292 struct carl9170_sta_tid *tid_info;
1293
1294 tid_info = rcu_dereference(sta_info->agg[i]);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00001295 RCU_INIT_POINTER(sta_info->agg[i], NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001296
1297 if (!tid_info)
1298 continue;
1299
1300 spin_lock_bh(&ar->tx_ampdu_list_lock);
1301 if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
1302 tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
1303 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1304 cleanup = true;
1305 }
1306 rcu_read_unlock();
1307
1308 if (cleanup)
1309 carl9170_ampdu_gc(ar);
1310 }
1311
1312 return 0;
1313}
1314
Eliad Peller8a3a3c82011-10-02 10:15:52 +02001315static int carl9170_op_conf_tx(struct ieee80211_hw *hw,
1316 struct ieee80211_vif *vif, u16 queue,
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001317 const struct ieee80211_tx_queue_params *param)
1318{
1319 struct ar9170 *ar = hw->priv;
1320 int ret;
1321
1322 mutex_lock(&ar->mutex);
1323 if (queue < ar->hw->queues) {
1324 memcpy(&ar->edcf[ar9170_qmap[queue]], param, sizeof(*param));
1325 ret = carl9170_set_qos(ar);
1326 } else {
1327 ret = -EINVAL;
1328 }
1329
1330 mutex_unlock(&ar->mutex);
1331 return ret;
1332}
1333
1334static void carl9170_ampdu_work(struct work_struct *work)
1335{
1336 struct ar9170 *ar = container_of(work, struct ar9170,
1337 ampdu_work);
1338
1339 if (!IS_STARTED(ar))
1340 return;
1341
1342 mutex_lock(&ar->mutex);
1343 carl9170_ampdu_gc(ar);
1344 mutex_unlock(&ar->mutex);
1345}
1346
1347static int carl9170_op_ampdu_action(struct ieee80211_hw *hw,
1348 struct ieee80211_vif *vif,
1349 enum ieee80211_ampdu_mlme_action action,
1350 struct ieee80211_sta *sta,
Johannes Berg0b01f032011-01-18 13:51:05 +01001351 u16 tid, u16 *ssn, u8 buf_size)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001352{
1353 struct ar9170 *ar = hw->priv;
1354 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1355 struct carl9170_sta_tid *tid_info;
1356
1357 if (modparam_noht)
1358 return -EOPNOTSUPP;
1359
1360 switch (action) {
1361 case IEEE80211_AMPDU_TX_START:
Christian Lamparter9c655c82010-09-26 23:06:56 +02001362 if (!sta_info->ht_sta)
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001363 return -EOPNOTSUPP;
1364
1365 rcu_read_lock();
1366 if (rcu_dereference(sta_info->agg[tid])) {
1367 rcu_read_unlock();
1368 return -EBUSY;
1369 }
1370
1371 tid_info = kzalloc(sizeof(struct carl9170_sta_tid),
1372 GFP_ATOMIC);
1373 if (!tid_info) {
1374 rcu_read_unlock();
1375 return -ENOMEM;
1376 }
1377
1378 tid_info->hsn = tid_info->bsn = tid_info->snx = (*ssn);
1379 tid_info->state = CARL9170_TID_STATE_PROGRESS;
1380 tid_info->tid = tid;
1381 tid_info->max = sta_info->ampdu_max_len;
1382
1383 INIT_LIST_HEAD(&tid_info->list);
1384 INIT_LIST_HEAD(&tid_info->tmp_list);
1385 skb_queue_head_init(&tid_info->queue);
1386 spin_lock_init(&tid_info->lock);
1387
1388 spin_lock_bh(&ar->tx_ampdu_list_lock);
1389 ar->tx_ampdu_list_len++;
1390 list_add_tail_rcu(&tid_info->list, &ar->tx_ampdu_list);
1391 rcu_assign_pointer(sta_info->agg[tid], tid_info);
1392 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1393 rcu_read_unlock();
1394
1395 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1396 break;
1397
1398 case IEEE80211_AMPDU_TX_STOP:
1399 rcu_read_lock();
1400 tid_info = rcu_dereference(sta_info->agg[tid]);
1401 if (tid_info) {
1402 spin_lock_bh(&ar->tx_ampdu_list_lock);
1403 if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
1404 tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
1405 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1406 }
1407
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00001408 RCU_INIT_POINTER(sta_info->agg[tid], NULL);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001409 rcu_read_unlock();
1410
1411 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1412 ieee80211_queue_work(ar->hw, &ar->ampdu_work);
1413 break;
1414
1415 case IEEE80211_AMPDU_TX_OPERATIONAL:
1416 rcu_read_lock();
1417 tid_info = rcu_dereference(sta_info->agg[tid]);
1418
1419 sta_info->stats[tid].clear = true;
Christian Lamparter24047e22011-03-29 13:43:14 +02001420 sta_info->stats[tid].req = false;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001421
1422 if (tid_info) {
1423 bitmap_zero(tid_info->bitmap, CARL9170_BAW_SIZE);
1424 tid_info->state = CARL9170_TID_STATE_IDLE;
1425 }
1426 rcu_read_unlock();
1427
1428 if (WARN_ON_ONCE(!tid_info))
1429 return -EFAULT;
1430
1431 break;
1432
1433 case IEEE80211_AMPDU_RX_START:
1434 case IEEE80211_AMPDU_RX_STOP:
1435 /* Handled by hardware */
1436 break;
1437
1438 default:
1439 return -EOPNOTSUPP;
1440 }
1441
1442 return 0;
1443}
1444
1445#ifdef CONFIG_CARL9170_WPC
1446static int carl9170_register_wps_button(struct ar9170 *ar)
1447{
1448 struct input_dev *input;
1449 int err;
1450
1451 if (!(ar->features & CARL9170_WPS_BUTTON))
1452 return 0;
1453
1454 input = input_allocate_device();
1455 if (!input)
1456 return -ENOMEM;
1457
1458 snprintf(ar->wps.name, sizeof(ar->wps.name), "%s WPS Button",
1459 wiphy_name(ar->hw->wiphy));
1460
1461 snprintf(ar->wps.phys, sizeof(ar->wps.phys),
1462 "ieee80211/%s/input0", wiphy_name(ar->hw->wiphy));
1463
1464 input->name = ar->wps.name;
1465 input->phys = ar->wps.phys;
1466 input->id.bustype = BUS_USB;
1467 input->dev.parent = &ar->hw->wiphy->dev;
1468
1469 input_set_capability(input, EV_KEY, KEY_WPS_BUTTON);
1470
1471 err = input_register_device(input);
1472 if (err) {
1473 input_free_device(input);
1474 return err;
1475 }
1476
1477 ar->wps.pbc = input;
1478 return 0;
1479}
1480#endif /* CONFIG_CARL9170_WPC */
1481
Christian Lamparter00044f12011-08-15 20:09:54 +02001482#ifdef CONFIG_CARL9170_HWRNG
1483static int carl9170_rng_get(struct ar9170 *ar)
1484{
1485
1486#define RW (CARL9170_MAX_CMD_PAYLOAD_LEN / sizeof(u32))
1487#define RB (CARL9170_MAX_CMD_PAYLOAD_LEN)
1488
1489 static const __le32 rng_load[RW] = {
1490 [0 ... (RW - 1)] = cpu_to_le32(AR9170_RAND_REG_NUM)};
1491
1492 u32 buf[RW];
1493
1494 unsigned int i, off = 0, transfer, count;
1495 int err;
1496
1497 BUILD_BUG_ON(RB > CARL9170_MAX_CMD_PAYLOAD_LEN);
1498
1499 if (!IS_ACCEPTING_CMD(ar) || !ar->rng.initialized)
1500 return -EAGAIN;
1501
1502 count = ARRAY_SIZE(ar->rng.cache);
1503 while (count) {
1504 err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
1505 RB, (u8 *) rng_load,
1506 RB, (u8 *) buf);
1507 if (err)
1508 return err;
1509
1510 transfer = min_t(unsigned int, count, RW);
1511 for (i = 0; i < transfer; i++)
1512 ar->rng.cache[off + i] = buf[i];
1513
1514 off += transfer;
1515 count -= transfer;
1516 }
1517
1518 ar->rng.cache_idx = 0;
1519
1520#undef RW
1521#undef RB
1522 return 0;
1523}
1524
1525static int carl9170_rng_read(struct hwrng *rng, u32 *data)
1526{
1527 struct ar9170 *ar = (struct ar9170 *)rng->priv;
1528 int ret = -EIO;
1529
1530 mutex_lock(&ar->mutex);
1531 if (ar->rng.cache_idx >= ARRAY_SIZE(ar->rng.cache)) {
1532 ret = carl9170_rng_get(ar);
1533 if (ret) {
1534 mutex_unlock(&ar->mutex);
1535 return ret;
1536 }
1537 }
1538
1539 *data = ar->rng.cache[ar->rng.cache_idx++];
1540 mutex_unlock(&ar->mutex);
1541
1542 return sizeof(u16);
1543}
1544
1545static void carl9170_unregister_hwrng(struct ar9170 *ar)
1546{
1547 if (ar->rng.initialized) {
1548 hwrng_unregister(&ar->rng.rng);
1549 ar->rng.initialized = false;
1550 }
1551}
1552
1553static int carl9170_register_hwrng(struct ar9170 *ar)
1554{
1555 int err;
1556
1557 snprintf(ar->rng.name, ARRAY_SIZE(ar->rng.name),
1558 "%s_%s", KBUILD_MODNAME, wiphy_name(ar->hw->wiphy));
1559 ar->rng.rng.name = ar->rng.name;
1560 ar->rng.rng.data_read = carl9170_rng_read;
1561 ar->rng.rng.priv = (unsigned long)ar;
1562
1563 if (WARN_ON(ar->rng.initialized))
1564 return -EALREADY;
1565
1566 err = hwrng_register(&ar->rng.rng);
1567 if (err) {
1568 dev_err(&ar->udev->dev, "Failed to register the random "
1569 "number generator (%d)\n", err);
1570 return err;
1571 }
1572
1573 ar->rng.initialized = true;
1574
1575 err = carl9170_rng_get(ar);
1576 if (err) {
1577 carl9170_unregister_hwrng(ar);
1578 return err;
1579 }
1580
1581 return 0;
1582}
1583#endif /* CONFIG_CARL9170_HWRNG */
1584
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001585static int carl9170_op_get_survey(struct ieee80211_hw *hw, int idx,
1586 struct survey_info *survey)
1587{
1588 struct ar9170 *ar = hw->priv;
Christian Lamparteracf17712011-08-15 19:50:48 +02001589 struct ieee80211_channel *chan;
1590 struct ieee80211_supported_band *band;
1591 int err, b, i;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001592
Christian Lamparteracf17712011-08-15 19:50:48 +02001593 chan = ar->channel;
1594 if (!chan)
1595 return -ENODEV;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001596
Christian Lamparteracf17712011-08-15 19:50:48 +02001597 if (idx == chan->hw_value) {
1598 mutex_lock(&ar->mutex);
1599 err = carl9170_update_survey(ar, false, true);
1600 mutex_unlock(&ar->mutex);
1601 if (err)
1602 return err;
1603 }
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001604
Christian Lamparteracf17712011-08-15 19:50:48 +02001605 for (b = 0; b < IEEE80211_NUM_BANDS; b++) {
1606 band = ar->hw->wiphy->bands[b];
1607
1608 if (!band)
1609 continue;
1610
1611 for (i = 0; i < band->n_channels; i++) {
1612 if (band->channels[i].hw_value == idx) {
1613 chan = &band->channels[i];
1614 goto found;
1615 }
1616 }
1617 }
1618 return -ENOENT;
1619
1620found:
1621 memcpy(survey, &ar->survey[idx], sizeof(*survey));
1622
1623 survey->channel = chan;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001624 survey->filled = SURVEY_INFO_NOISE_DBM;
Christian Lamparteracf17712011-08-15 19:50:48 +02001625
1626 if (ar->channel == chan)
1627 survey->filled |= SURVEY_INFO_IN_USE;
1628
1629 if (ar->fw.hw_counters) {
1630 survey->filled |= SURVEY_INFO_CHANNEL_TIME |
1631 SURVEY_INFO_CHANNEL_TIME_BUSY |
1632 SURVEY_INFO_CHANNEL_TIME_TX;
1633 }
1634
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001635 return 0;
1636}
1637
1638static void carl9170_op_flush(struct ieee80211_hw *hw, bool drop)
1639{
1640 struct ar9170 *ar = hw->priv;
1641 unsigned int vid;
1642
1643 mutex_lock(&ar->mutex);
1644 for_each_set_bit(vid, &ar->vif_bitmap, ar->fw.vif_num)
1645 carl9170_flush_cab(ar, vid);
1646
1647 carl9170_flush(ar, drop);
1648 mutex_unlock(&ar->mutex);
1649}
1650
1651static int carl9170_op_get_stats(struct ieee80211_hw *hw,
1652 struct ieee80211_low_level_stats *stats)
1653{
1654 struct ar9170 *ar = hw->priv;
1655
1656 memset(stats, 0, sizeof(*stats));
1657 stats->dot11ACKFailureCount = ar->tx_ack_failures;
1658 stats->dot11FCSErrorCount = ar->tx_fcs_errors;
1659 return 0;
1660}
1661
1662static void carl9170_op_sta_notify(struct ieee80211_hw *hw,
1663 struct ieee80211_vif *vif,
1664 enum sta_notify_cmd cmd,
1665 struct ieee80211_sta *sta)
1666{
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001667 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001668
1669 switch (cmd) {
1670 case STA_NOTIFY_SLEEP:
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001671 sta_info->sleeping = true;
1672 if (atomic_read(&sta_info->pending_frames))
1673 ieee80211_sta_block_awake(hw, sta, true);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001674 break;
1675
1676 case STA_NOTIFY_AWAKE:
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001677 sta_info->sleeping = false;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001678 break;
1679 }
1680}
1681
Christian Lamparter69f72352011-07-07 23:01:25 +02001682static bool carl9170_tx_frames_pending(struct ieee80211_hw *hw)
1683{
1684 struct ar9170 *ar = hw->priv;
1685
1686 return !!atomic_read(&ar->tx_total_queued);
1687}
1688
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001689static const struct ieee80211_ops carl9170_ops = {
1690 .start = carl9170_op_start,
1691 .stop = carl9170_op_stop,
1692 .tx = carl9170_op_tx,
1693 .flush = carl9170_op_flush,
1694 .add_interface = carl9170_op_add_interface,
1695 .remove_interface = carl9170_op_remove_interface,
1696 .config = carl9170_op_config,
1697 .prepare_multicast = carl9170_op_prepare_multicast,
1698 .configure_filter = carl9170_op_configure_filter,
1699 .conf_tx = carl9170_op_conf_tx,
1700 .bss_info_changed = carl9170_op_bss_info_changed,
1701 .get_tsf = carl9170_op_get_tsf,
1702 .set_key = carl9170_op_set_key,
1703 .sta_add = carl9170_op_sta_add,
1704 .sta_remove = carl9170_op_sta_remove,
1705 .sta_notify = carl9170_op_sta_notify,
1706 .get_survey = carl9170_op_get_survey,
1707 .get_stats = carl9170_op_get_stats,
1708 .ampdu_action = carl9170_op_ampdu_action,
Christian Lamparter69f72352011-07-07 23:01:25 +02001709 .tx_frames_pending = carl9170_tx_frames_pending,
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001710};
1711
1712void *carl9170_alloc(size_t priv_size)
1713{
1714 struct ieee80211_hw *hw;
1715 struct ar9170 *ar;
1716 struct sk_buff *skb;
1717 int i;
1718
1719 /*
1720 * this buffer is used for rx stream reconstruction.
1721 * Under heavy load this device (or the transport layer?)
1722 * tends to split the streams into separate rx descriptors.
1723 */
1724
1725 skb = __dev_alloc_skb(AR9170_RX_STREAM_MAX_SIZE, GFP_KERNEL);
1726 if (!skb)
1727 goto err_nomem;
1728
1729 hw = ieee80211_alloc_hw(priv_size, &carl9170_ops);
1730 if (!hw)
1731 goto err_nomem;
1732
1733 ar = hw->priv;
1734 ar->hw = hw;
1735 ar->rx_failover = skb;
1736
1737 memset(&ar->rx_plcp, 0, sizeof(struct ar9170_rx_head));
1738 ar->rx_has_plcp = false;
1739
1740 /*
1741 * Here's a hidden pitfall!
1742 *
1743 * All 4 AC queues work perfectly well under _legacy_ operation.
1744 * However as soon as aggregation is enabled, the traffic flow
1745 * gets very bumpy. Therefore we have to _switch_ to a
1746 * software AC with a single HW queue.
1747 */
1748 hw->queues = __AR9170_NUM_TXQ;
1749
1750 mutex_init(&ar->mutex);
1751 spin_lock_init(&ar->beacon_lock);
1752 spin_lock_init(&ar->cmd_lock);
1753 spin_lock_init(&ar->tx_stats_lock);
1754 spin_lock_init(&ar->tx_ampdu_list_lock);
1755 spin_lock_init(&ar->mem_lock);
1756 spin_lock_init(&ar->state_lock);
1757 atomic_set(&ar->pending_restarts, 0);
1758 ar->vifs = 0;
1759 for (i = 0; i < ar->hw->queues; i++) {
1760 skb_queue_head_init(&ar->tx_status[i]);
1761 skb_queue_head_init(&ar->tx_pending[i]);
Christian Lamparterc9122c02012-07-07 21:13:59 +02001762
1763 INIT_LIST_HEAD(&ar->bar_list[i]);
1764 spin_lock_init(&ar->bar_list_lock[i]);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001765 }
1766 INIT_WORK(&ar->ps_work, carl9170_ps_work);
Christian Lampartere4a668c2010-10-29 23:26:13 +02001767 INIT_WORK(&ar->ping_work, carl9170_ping_work);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001768 INIT_WORK(&ar->restart_work, carl9170_restart_work);
1769 INIT_WORK(&ar->ampdu_work, carl9170_ampdu_work);
Christian Lamparteracf17712011-08-15 19:50:48 +02001770 INIT_DELAYED_WORK(&ar->stat_work, carl9170_stat_work);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001771 INIT_DELAYED_WORK(&ar->tx_janitor, carl9170_tx_janitor);
1772 INIT_LIST_HEAD(&ar->tx_ampdu_list);
1773 rcu_assign_pointer(ar->tx_ampdu_iter,
1774 (struct carl9170_sta_tid *) &ar->tx_ampdu_list);
1775
1776 bitmap_zero(&ar->vif_bitmap, ar->fw.vif_num);
1777 INIT_LIST_HEAD(&ar->vif_list);
1778 init_completion(&ar->tx_flush);
1779
Christian Lamparterdf649622011-05-14 02:42:38 +02001780 /* firmware decides which modes we support */
1781 hw->wiphy->interface_modes = 0;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001782
1783 hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS |
Christian Lampartere37b6742012-09-02 14:25:50 +02001784 IEEE80211_HW_MFP_CAPABLE |
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001785 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
1786 IEEE80211_HW_SUPPORTS_PS |
1787 IEEE80211_HW_PS_NULLFUNC_STACK |
Christian Lamparterf3716fd2011-06-30 20:31:34 +02001788 IEEE80211_HW_NEED_DTIM_PERIOD |
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001789 IEEE80211_HW_SIGNAL_DBM;
1790
1791 if (!modparam_noht) {
1792 /*
1793 * see the comment above, why we allow the user
1794 * to disable HT by a module parameter.
1795 */
1796 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
1797 }
1798
1799 hw->extra_tx_headroom = sizeof(struct _carl9170_tx_superframe);
1800 hw->sta_data_size = sizeof(struct carl9170_sta_info);
1801 hw->vif_data_size = sizeof(struct carl9170_vif_info);
1802
1803 hw->max_rates = CARL9170_TX_MAX_RATES;
1804 hw->max_rate_tries = CARL9170_TX_USER_RATE_TRIES;
1805
1806 for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
1807 ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
1808
1809 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
Nicolas Cavallarie2d75c12012-01-11 16:44:50 +01001810
1811 /* As IBSS Encryption is software-based, IBSS RSN is supported. */
1812 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001813 return ar;
1814
1815err_nomem:
1816 kfree_skb(skb);
1817 return ERR_PTR(-ENOMEM);
1818}
1819
1820static int carl9170_read_eeprom(struct ar9170 *ar)
1821{
1822#define RW 8 /* number of words to read at once */
1823#define RB (sizeof(u32) * RW)
1824 u8 *eeprom = (void *)&ar->eeprom;
1825 __le32 offsets[RW];
1826 int i, j, err;
1827
1828 BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
1829
1830 BUILD_BUG_ON(RB > CARL9170_MAX_CMD_LEN - 4);
1831#ifndef __CHECKER__
1832 /* don't want to handle trailing remains */
1833 BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
1834#endif
1835
Pavel Roskin3f29c522011-07-13 21:38:18 -04001836 for (i = 0; i < sizeof(ar->eeprom) / RB; i++) {
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001837 for (j = 0; j < RW; j++)
1838 offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
1839 RB * i + 4 * j);
1840
1841 err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
1842 RB, (u8 *) &offsets,
1843 RB, eeprom + RB * i);
1844 if (err)
1845 return err;
1846 }
1847
1848#undef RW
1849#undef RB
1850 return 0;
1851}
1852
1853static int carl9170_parse_eeprom(struct ar9170 *ar)
1854{
1855 struct ath_regulatory *regulatory = &ar->common.regulatory;
1856 unsigned int rx_streams, tx_streams, tx_params = 0;
1857 int bands = 0;
Christian Lamparteracf17712011-08-15 19:50:48 +02001858 int chans = 0;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001859
1860 if (ar->eeprom.length == cpu_to_le16(0xffff))
1861 return -ENODATA;
1862
1863 rx_streams = hweight8(ar->eeprom.rx_mask);
1864 tx_streams = hweight8(ar->eeprom.tx_mask);
1865
1866 if (rx_streams != tx_streams) {
1867 tx_params = IEEE80211_HT_MCS_TX_RX_DIFF;
1868
1869 WARN_ON(!(tx_streams >= 1 && tx_streams <=
1870 IEEE80211_HT_MCS_TX_MAX_STREAMS));
1871
1872 tx_params = (tx_streams - 1) <<
1873 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1874
1875 carl9170_band_2GHz.ht_cap.mcs.tx_params |= tx_params;
1876 carl9170_band_5GHz.ht_cap.mcs.tx_params |= tx_params;
1877 }
1878
1879 if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
1880 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
1881 &carl9170_band_2GHz;
Christian Lamparteracf17712011-08-15 19:50:48 +02001882 chans += carl9170_band_2GHz.n_channels;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001883 bands++;
1884 }
1885 if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
1886 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
1887 &carl9170_band_5GHz;
Christian Lamparteracf17712011-08-15 19:50:48 +02001888 chans += carl9170_band_5GHz.n_channels;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001889 bands++;
1890 }
1891
Christian Lamparteracf17712011-08-15 19:50:48 +02001892 if (!bands)
1893 return -EINVAL;
1894
1895 ar->survey = kzalloc(sizeof(struct survey_info) * chans, GFP_KERNEL);
1896 if (!ar->survey)
1897 return -ENOMEM;
1898 ar->num_channels = chans;
1899
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001900 /*
1901 * I measured this, a bandswitch takes roughly
1902 * 135 ms and a frequency switch about 80.
1903 *
1904 * FIXME: measure these values again once EEPROM settings
1905 * are used, that will influence them!
1906 */
1907 if (bands == 2)
1908 ar->hw->channel_change_time = 135 * 1000;
1909 else
1910 ar->hw->channel_change_time = 80 * 1000;
1911
1912 regulatory->current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001913
1914 /* second part of wiphy init */
1915 SET_IEEE80211_PERM_ADDR(ar->hw, ar->eeprom.mac_address);
1916
Christian Lamparteracf17712011-08-15 19:50:48 +02001917 return 0;
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001918}
1919
1920static int carl9170_reg_notifier(struct wiphy *wiphy,
1921 struct regulatory_request *request)
1922{
1923 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1924 struct ar9170 *ar = hw->priv;
1925
1926 return ath_reg_notifier_apply(wiphy, request, &ar->common.regulatory);
1927}
1928
1929int carl9170_register(struct ar9170 *ar)
1930{
1931 struct ath_regulatory *regulatory = &ar->common.regulatory;
1932 int err = 0, i;
1933
1934 if (WARN_ON(ar->mem_bitmap))
1935 return -EINVAL;
1936
1937 ar->mem_bitmap = kzalloc(roundup(ar->fw.mem_blocks, BITS_PER_LONG) *
1938 sizeof(unsigned long), GFP_KERNEL);
1939
1940 if (!ar->mem_bitmap)
1941 return -ENOMEM;
1942
1943 /* try to read EEPROM, init MAC addr */
1944 err = carl9170_read_eeprom(ar);
1945 if (err)
1946 return err;
1947
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001948 err = carl9170_parse_eeprom(ar);
1949 if (err)
1950 return err;
1951
1952 err = ath_regd_init(regulatory, ar->hw->wiphy,
1953 carl9170_reg_notifier);
1954 if (err)
1955 return err;
1956
1957 if (modparam_noht) {
1958 carl9170_band_2GHz.ht_cap.ht_supported = false;
1959 carl9170_band_5GHz.ht_cap.ht_supported = false;
1960 }
1961
1962 for (i = 0; i < ar->fw.vif_num; i++) {
1963 ar->vif_priv[i].id = i;
1964 ar->vif_priv[i].vif = NULL;
1965 }
1966
1967 err = ieee80211_register_hw(ar->hw);
1968 if (err)
1969 return err;
1970
1971 /* mac80211 interface is now registered */
1972 ar->registered = true;
1973
1974 if (!ath_is_world_regd(regulatory))
1975 regulatory_hint(ar->hw->wiphy, regulatory->alpha2);
1976
1977#ifdef CONFIG_CARL9170_DEBUGFS
1978 carl9170_debugfs_register(ar);
1979#endif /* CONFIG_CARL9170_DEBUGFS */
1980
1981 err = carl9170_led_init(ar);
1982 if (err)
1983 goto err_unreg;
1984
1985#ifdef CONFIG_CARL9170_LEDS
1986 err = carl9170_led_register(ar);
1987 if (err)
1988 goto err_unreg;
Hauke Mehrtens8e7ce892010-10-23 19:51:32 +02001989#endif /* CONFIG_CARL9170_LEDS */
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02001990
1991#ifdef CONFIG_CARL9170_WPC
1992 err = carl9170_register_wps_button(ar);
1993 if (err)
1994 goto err_unreg;
1995#endif /* CONFIG_CARL9170_WPC */
1996
Christian Lamparter00044f12011-08-15 20:09:54 +02001997#ifdef CONFIG_CARL9170_HWRNG
1998 err = carl9170_register_hwrng(ar);
1999 if (err)
2000 goto err_unreg;
2001#endif /* CONFIG_CARL9170_HWRNG */
2002
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002003 dev_info(&ar->udev->dev, "Atheros AR9170 is registered as '%s'\n",
2004 wiphy_name(ar->hw->wiphy));
2005
2006 return 0;
2007
2008err_unreg:
2009 carl9170_unregister(ar);
2010 return err;
2011}
2012
2013void carl9170_unregister(struct ar9170 *ar)
2014{
2015 if (!ar->registered)
2016 return;
2017
2018 ar->registered = false;
2019
2020#ifdef CONFIG_CARL9170_LEDS
2021 carl9170_led_unregister(ar);
2022#endif /* CONFIG_CARL9170_LEDS */
2023
2024#ifdef CONFIG_CARL9170_DEBUGFS
2025 carl9170_debugfs_unregister(ar);
2026#endif /* CONFIG_CARL9170_DEBUGFS */
2027
2028#ifdef CONFIG_CARL9170_WPC
2029 if (ar->wps.pbc) {
2030 input_unregister_device(ar->wps.pbc);
2031 ar->wps.pbc = NULL;
2032 }
2033#endif /* CONFIG_CARL9170_WPC */
2034
Christian Lamparter00044f12011-08-15 20:09:54 +02002035#ifdef CONFIG_CARL9170_HWRNG
2036 carl9170_unregister_hwrng(ar);
2037#endif /* CONFIG_CARL9170_HWRNG */
2038
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002039 carl9170_cancel_worker(ar);
2040 cancel_work_sync(&ar->restart_work);
2041
2042 ieee80211_unregister_hw(ar->hw);
2043}
2044
2045void carl9170_free(struct ar9170 *ar)
2046{
2047 WARN_ON(ar->registered);
2048 WARN_ON(IS_INITIALIZED(ar));
2049
2050 kfree_skb(ar->rx_failover);
2051 ar->rx_failover = NULL;
2052
2053 kfree(ar->mem_bitmap);
2054 ar->mem_bitmap = NULL;
2055
Christian Lamparteracf17712011-08-15 19:50:48 +02002056 kfree(ar->survey);
2057 ar->survey = NULL;
2058
Christian Lamparterfe8ee9a2010-09-06 00:48:55 +02002059 mutex_destroy(&ar->mutex);
2060
2061 ieee80211_free_hw(ar->hw);
2062}