]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/wireless/ath/ar9170/main.c
ar9170: interpret firmware debug commands
[mirror_ubuntu-jammy-kernel.git] / drivers / net / wireless / ath / ar9170 / main.c
CommitLineData
e9348cdd
CL
1/*
2 * Atheros AR9170 driver
3 *
4 * mac80211 interaction code
5 *
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, Christian Lamparter <chunkeey@web.de>
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
e9348cdd
CL
40#include <linux/init.h>
41#include <linux/module.h>
42#include <linux/etherdevice.h>
43#include <net/mac80211.h>
44#include "ar9170.h"
45#include "hw.h"
46#include "cmd.h"
47
48static int modparam_nohwcrypt;
49module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
50MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
e9348cdd
CL
51
52#define RATE(_bitrate, _hw_rate, _txpidx, _flags) { \
53 .bitrate = (_bitrate), \
54 .flags = (_flags), \
55 .hw_value = (_hw_rate) | (_txpidx) << 4, \
56}
57
58static struct ieee80211_rate __ar9170_ratetable[] = {
59 RATE(10, 0, 0, 0),
60 RATE(20, 1, 1, IEEE80211_RATE_SHORT_PREAMBLE),
61 RATE(55, 2, 2, IEEE80211_RATE_SHORT_PREAMBLE),
62 RATE(110, 3, 3, IEEE80211_RATE_SHORT_PREAMBLE),
63 RATE(60, 0xb, 0, 0),
64 RATE(90, 0xf, 0, 0),
65 RATE(120, 0xa, 0, 0),
66 RATE(180, 0xe, 0, 0),
67 RATE(240, 0x9, 0, 0),
68 RATE(360, 0xd, 1, 0),
69 RATE(480, 0x8, 2, 0),
70 RATE(540, 0xc, 3, 0),
71};
72#undef RATE
73
74#define ar9170_g_ratetable (__ar9170_ratetable + 0)
75#define ar9170_g_ratetable_size 12
76#define ar9170_a_ratetable (__ar9170_ratetable + 4)
77#define ar9170_a_ratetable_size 8
78
79/*
80 * NB: The hw_value is used as an index into the ar9170_phy_freq_params
81 * array in phy.c so that we don't have to do frequency lookups!
82 */
83#define CHAN(_freq, _idx) { \
84 .center_freq = (_freq), \
85 .hw_value = (_idx), \
86 .max_power = 18, /* XXX */ \
87}
88
89static struct ieee80211_channel ar9170_2ghz_chantable[] = {
90 CHAN(2412, 0),
91 CHAN(2417, 1),
92 CHAN(2422, 2),
93 CHAN(2427, 3),
94 CHAN(2432, 4),
95 CHAN(2437, 5),
96 CHAN(2442, 6),
97 CHAN(2447, 7),
98 CHAN(2452, 8),
99 CHAN(2457, 9),
100 CHAN(2462, 10),
101 CHAN(2467, 11),
102 CHAN(2472, 12),
103 CHAN(2484, 13),
104};
105
106static struct ieee80211_channel ar9170_5ghz_chantable[] = {
107 CHAN(4920, 14),
108 CHAN(4940, 15),
109 CHAN(4960, 16),
110 CHAN(4980, 17),
111 CHAN(5040, 18),
112 CHAN(5060, 19),
113 CHAN(5080, 20),
114 CHAN(5180, 21),
115 CHAN(5200, 22),
116 CHAN(5220, 23),
117 CHAN(5240, 24),
118 CHAN(5260, 25),
119 CHAN(5280, 26),
120 CHAN(5300, 27),
121 CHAN(5320, 28),
122 CHAN(5500, 29),
123 CHAN(5520, 30),
124 CHAN(5540, 31),
125 CHAN(5560, 32),
126 CHAN(5580, 33),
127 CHAN(5600, 34),
128 CHAN(5620, 35),
129 CHAN(5640, 36),
130 CHAN(5660, 37),
131 CHAN(5680, 38),
132 CHAN(5700, 39),
133 CHAN(5745, 40),
134 CHAN(5765, 41),
135 CHAN(5785, 42),
136 CHAN(5805, 43),
137 CHAN(5825, 44),
138 CHAN(5170, 45),
139 CHAN(5190, 46),
140 CHAN(5210, 47),
141 CHAN(5230, 48),
142};
143#undef CHAN
144
9e52b062
JB
145#define AR9170_HT_CAP \
146{ \
147 .ht_supported = true, \
148 .cap = IEEE80211_HT_CAP_MAX_AMSDU | \
9e52b062
JB
149 IEEE80211_HT_CAP_SUP_WIDTH_20_40 | \
150 IEEE80211_HT_CAP_SGI_40 | \
151 IEEE80211_HT_CAP_DSSSCCK40 | \
152 IEEE80211_HT_CAP_SM_PS, \
083c4687
CL
153 .ampdu_factor = 3, \
154 .ampdu_density = 6, \
9e52b062
JB
155 .mcs = { \
156 .rx_mask = { 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, }, \
157 }, \
158}
159
e9348cdd
CL
160static struct ieee80211_supported_band ar9170_band_2GHz = {
161 .channels = ar9170_2ghz_chantable,
162 .n_channels = ARRAY_SIZE(ar9170_2ghz_chantable),
163 .bitrates = ar9170_g_ratetable,
164 .n_bitrates = ar9170_g_ratetable_size,
9e52b062
JB
165 .ht_cap = AR9170_HT_CAP,
166};
167
168static struct ieee80211_supported_band ar9170_band_5GHz = {
169 .channels = ar9170_5ghz_chantable,
170 .n_channels = ARRAY_SIZE(ar9170_5ghz_chantable),
171 .bitrates = ar9170_a_ratetable,
172 .n_bitrates = ar9170_a_ratetable_size,
173 .ht_cap = AR9170_HT_CAP,
e9348cdd
CL
174};
175
176#ifdef AR9170_QUEUE_DEBUG
177/*
178 * In case some wants works with AR9170's crazy tx_status queueing techniques.
179 * He might need this rather useful probing function.
180 *
181 * NOTE: caller must hold the queue's spinlock!
182 */
183
184static void ar9170_print_txheader(struct ar9170 *ar, struct sk_buff *skb)
185{
186 struct ar9170_tx_control *txc = (void *) skb->data;
187 struct ieee80211_hdr *hdr = (void *)txc->frame_data;
188
189 printk(KERN_DEBUG "%s: => FRAME [skb:%p, queue:%d, DA:[%pM] "
190 "mac_control:%04x, phy_control:%08x]\n",
191 wiphy_name(ar->hw->wiphy), skb, skb_get_queue_mapping(skb),
192 ieee80211_get_DA(hdr), le16_to_cpu(txc->mac_control),
193 le32_to_cpu(txc->phy_control));
194}
195
196static void ar9170_dump_station_tx_status_queue(struct ar9170 *ar,
197 struct sk_buff_head *queue)
198{
199 struct sk_buff *skb;
200 int i = 0;
201
202 printk(KERN_DEBUG "---[ cut here ]---\n");
203 printk(KERN_DEBUG "%s: %d entries in tx_status queue.\n",
204 wiphy_name(ar->hw->wiphy), skb_queue_len(queue));
205
206 skb_queue_walk(queue, skb) {
207 struct ar9170_tx_control *txc = (void *) skb->data;
208 struct ieee80211_hdr *hdr = (void *)txc->frame_data;
209
210 printk(KERN_DEBUG "index:%d => \n", i);
211 ar9170_print_txheader(ar, skb);
212 }
213 printk(KERN_DEBUG "---[ end ]---\n");
214}
215#endif /* AR9170_QUEUE_DEBUG */
216
e9348cdd
CL
217void ar9170_handle_tx_status(struct ar9170 *ar, struct sk_buff *skb,
218 bool valid_status, u16 tx_status)
219{
220 struct ieee80211_tx_info *txinfo;
221 unsigned int retries = 0, queue = skb_get_queue_mapping(skb);
222 unsigned long flags;
223
224 spin_lock_irqsave(&ar->tx_stats_lock, flags);
225 ar->tx_stats[queue].len--;
226 if (ieee80211_queue_stopped(ar->hw, queue))
227 ieee80211_wake_queue(ar->hw, queue);
228 spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
229
230 txinfo = IEEE80211_SKB_CB(skb);
231 ieee80211_tx_info_clear_status(txinfo);
232
233 switch (tx_status) {
234 case AR9170_TX_STATUS_RETRY:
235 retries = 2;
236 case AR9170_TX_STATUS_COMPLETE:
237 txinfo->flags |= IEEE80211_TX_STAT_ACK;
238 break;
239
240 case AR9170_TX_STATUS_FAILED:
241 retries = ar->hw->conf.long_frame_max_tx_count;
242 break;
243
244 default:
245 printk(KERN_ERR "%s: invalid tx_status response (%x).\n",
246 wiphy_name(ar->hw->wiphy), tx_status);
247 break;
248 }
249
250 if (valid_status)
251 txinfo->status.rates[0].count = retries + 1;
252
253 skb_pull(skb, sizeof(struct ar9170_tx_control));
254 ieee80211_tx_status_irqsafe(ar->hw, skb);
255}
e9348cdd
CL
256
257static struct sk_buff *ar9170_find_skb_in_queue(struct ar9170 *ar,
258 const u8 *mac,
259 const u32 queue,
260 struct sk_buff_head *q)
261{
262 unsigned long flags;
263 struct sk_buff *skb;
264
265 spin_lock_irqsave(&q->lock, flags);
266 skb_queue_walk(q, skb) {
267 struct ar9170_tx_control *txc = (void *) skb->data;
268 struct ieee80211_hdr *hdr = (void *) txc->frame_data;
269 u32 txc_queue = (le32_to_cpu(txc->phy_control) &
270 AR9170_TX_PHY_QOS_MASK) >>
271 AR9170_TX_PHY_QOS_SHIFT;
272
273 if ((queue != txc_queue) ||
274 (compare_ether_addr(ieee80211_get_DA(hdr), mac)))
275 continue;
276
277 __skb_unlink(skb, q);
278 spin_unlock_irqrestore(&q->lock, flags);
279 return skb;
280 }
281 spin_unlock_irqrestore(&q->lock, flags);
282 return NULL;
283}
284
e9348cdd
CL
285static struct sk_buff *ar9170_find_queued_skb(struct ar9170 *ar, const u8 *mac,
286 const u32 queue)
287{
288 struct ieee80211_sta *sta;
289 struct sk_buff *skb;
290
291 /*
292 * Unfortunately, the firmware does not tell to which (queued) frame
293 * this transmission status report belongs to.
294 *
295 * So we have to make risky guesses - with the scarce information
296 * the firmware provided (-> destination MAC, and phy_control) -
297 * and hope that we picked the right one...
298 */
299 rcu_read_lock();
300 sta = ieee80211_find_sta(ar->hw, mac);
301
302 if (likely(sta)) {
303 struct ar9170_sta_info *sta_priv = (void *) sta->drv_priv;
4a48e2a4
CL
304 skb = skb_dequeue(&sta_priv->tx_status[queue]);
305 rcu_read_unlock();
306 if (likely(skb))
307 return skb;
308 } else
309 rcu_read_unlock();
e9348cdd 310
4a48e2a4
CL
311 /* scan the waste queue for candidates */
312 skb = ar9170_find_skb_in_queue(ar, mac, queue,
313 &ar->global_tx_status_waste);
314 if (!skb) {
315 /* so it still _must_ be in the global list. */
e9348cdd 316 skb = ar9170_find_skb_in_queue(ar, mac, queue,
4a48e2a4 317 &ar->global_tx_status);
e9348cdd 318 }
e9348cdd
CL
319
320#ifdef AR9170_QUEUE_DEBUG
321 if (unlikely((!skb) && net_ratelimit())) {
322 printk(KERN_ERR "%s: ESS:[%pM] does not have any "
323 "outstanding frames in this queue (%d).\n",
324 wiphy_name(ar->hw->wiphy), mac, queue);
325 }
326#endif /* AR9170_QUEUE_DEBUG */
e9348cdd
CL
327 return skb;
328}
329
330/*
331 * This worker tries to keep the global tx_status queue empty.
332 * So we can guarantee that incoming tx_status reports for
333 * unregistered stations are always synced with the actual
334 * frame - which we think - belongs to.
335 */
336
337static void ar9170_tx_status_janitor(struct work_struct *work)
338{
339 struct ar9170 *ar = container_of(work, struct ar9170,
4a48e2a4 340 tx_status_janitor.work);
e9348cdd
CL
341 struct sk_buff *skb;
342
4a48e2a4
CL
343 if (unlikely(!IS_STARTED(ar)))
344 return ;
345
e9348cdd
CL
346 /* recycle the garbage back to mac80211... one by one. */
347 while ((skb = skb_dequeue(&ar->global_tx_status_waste))) {
348#ifdef AR9170_QUEUE_DEBUG
349 printk(KERN_DEBUG "%s: dispose queued frame =>\n",
350 wiphy_name(ar->hw->wiphy));
351 ar9170_print_txheader(ar, skb);
352#endif /* AR9170_QUEUE_DEBUG */
353 ar9170_handle_tx_status(ar, skb, false,
354 AR9170_TX_STATUS_FAILED);
355 }
356
e9348cdd
CL
357 while ((skb = skb_dequeue(&ar->global_tx_status))) {
358#ifdef AR9170_QUEUE_DEBUG
359 printk(KERN_DEBUG "%s: moving frame into waste queue =>\n",
360 wiphy_name(ar->hw->wiphy));
361
362 ar9170_print_txheader(ar, skb);
363#endif /* AR9170_QUEUE_DEBUG */
364 skb_queue_tail(&ar->global_tx_status_waste, skb);
365 }
366
367 /* recall the janitor in 100ms - if there's garbage in the can. */
368 if (skb_queue_len(&ar->global_tx_status_waste) > 0)
369 queue_delayed_work(ar->hw->workqueue, &ar->tx_status_janitor,
370 msecs_to_jiffies(100));
371}
372
66d00813 373void ar9170_handle_command_response(struct ar9170 *ar, void *buf, u32 len)
e9348cdd
CL
374{
375 struct ar9170_cmd_response *cmd = (void *) buf;
376
377 if ((cmd->type & 0xc0) != 0xc0) {
378 ar->callback_cmd(ar, len, buf);
379 return;
380 }
381
382 /* hardware event handlers */
383 switch (cmd->type) {
384 case 0xc1: {
385 /*
386 * TX status notification:
387 * bytes: 0c c1 XX YY M1 M2 M3 M4 M5 M6 R4 R3 R2 R1 S2 S1
388 *
389 * XX always 81
390 * YY always 00
391 * M1-M6 is the MAC address
392 * R1-R4 is the transmit rate
393 * S1-S2 is the transmit status
394 */
395
396 struct sk_buff *skb;
397 u32 queue = (le32_to_cpu(cmd->tx_status.rate) &
398 AR9170_TX_PHY_QOS_MASK) >> AR9170_TX_PHY_QOS_SHIFT;
399
400 skb = ar9170_find_queued_skb(ar, cmd->tx_status.dst, queue);
401 if (unlikely(!skb))
402 return ;
403
404 ar9170_handle_tx_status(ar, skb, true,
405 le16_to_cpu(cmd->tx_status.status));
406 break;
407 }
408
409 case 0xc0:
410 /*
411 * pre-TBTT event
412 */
413 if (ar->vif && ar->vif->type == NL80211_IFTYPE_AP)
414 queue_work(ar->hw->workqueue, &ar->beacon_work);
415 break;
416
417 case 0xc2:
418 /*
419 * (IBSS) beacon send notification
420 * bytes: 04 c2 XX YY B4 B3 B2 B1
421 *
422 * XX always 80
423 * YY always 00
424 * B1-B4 "should" be the number of send out beacons.
425 */
426 break;
427
428 case 0xc3:
429 /* End of Atim Window */
430 break;
431
432 case 0xc4:
433 case 0xc5:
434 /* BlockACK events */
435 break;
436
437 case 0xc6:
438 /* Watchdog Interrupt */
439 break;
440
441 case 0xc9:
442 /* retransmission issue / SIFS/EIFS collision ?! */
443 break;
444
2543a0c4
JB
445 /* firmware debug */
446 case 0xca:
447 printk(KERN_DEBUG "ar9170 FW: %.*s\n", len - 4, (char *)buf + 4);
448 break;
449 case 0xcb:
450 len -= 4;
451
452 switch (len) {
453 case 1:
454 printk(KERN_DEBUG "ar9170 FW: u8: %#.2x\n",
455 *((char *)buf + 4));
456 break;
457 case 2:
458 printk(KERN_DEBUG "ar9170 FW: u8: %#.4x\n",
459 le16_to_cpup((__le16 *)((char *)buf + 4)));
460 break;
461 case 4:
462 printk(KERN_DEBUG "ar9170 FW: u8: %#.8x\n",
463 le32_to_cpup((__le32 *)((char *)buf + 4)));
464 break;
465 case 8:
466 printk(KERN_DEBUG "ar9170 FW: u8: %#.16lx\n",
467 (unsigned long)le64_to_cpup(
468 (__le64 *)((char *)buf + 4)));
469 break;
470 }
471 break;
472 case 0xcc:
473 print_hex_dump_bytes("ar9170 FW:", DUMP_PREFIX_NONE,
474 (char *)buf + 4, len - 4);
475 break;
476
e9348cdd
CL
477 default:
478 printk(KERN_INFO "received unhandled event %x\n", cmd->type);
479 print_hex_dump_bytes("dump:", DUMP_PREFIX_NONE, buf, len);
480 break;
481 }
482}
483
cca84799 484static void ar9170_rx_reset_rx_mpdu(struct ar9170 *ar)
e9348cdd 485{
cca84799
CL
486 memset(&ar->rx_mpdu.plcp, 0, sizeof(struct ar9170_rx_head));
487 ar->rx_mpdu.has_plcp = false;
488}
e9348cdd 489
cca84799
CL
490static int ar9170_nag_limiter(struct ar9170 *ar)
491{
492 bool print_message;
493
494 /*
495 * we expect all sorts of errors in promiscuous mode.
496 * don't bother with it, it's OK!
497 */
498 if (ar->sniffer_enabled)
499 return false;
500
501 /*
502 * only go for frequent errors! The hardware tends to
503 * do some stupid thing once in a while under load, in
504 * noisy environments or just for fun!
505 */
506 if (time_before(jiffies, ar->bad_hw_nagger) && net_ratelimit())
507 print_message = true;
508 else
509 print_message = false;
510
511 /* reset threshold for "once in a while" */
512 ar->bad_hw_nagger = jiffies + HZ / 4;
513 return print_message;
514}
515
516static int ar9170_rx_mac_status(struct ar9170 *ar,
517 struct ar9170_rx_head *head,
518 struct ar9170_rx_macstatus *mac,
519 struct ieee80211_rx_status *status)
520{
521 u8 error, decrypt;
e9348cdd 522
e9348cdd 523 BUILD_BUG_ON(sizeof(struct ar9170_rx_head) != 12);
cca84799 524 BUILD_BUG_ON(sizeof(struct ar9170_rx_macstatus) != 4);
e9348cdd 525
cca84799
CL
526 error = mac->error;
527 if (error & AR9170_RX_ERROR_MMIC) {
528 status->flag |= RX_FLAG_MMIC_ERROR;
529 error &= ~AR9170_RX_ERROR_MMIC;
530 }
e9348cdd 531
cca84799
CL
532 if (error & AR9170_RX_ERROR_PLCP) {
533 status->flag |= RX_FLAG_FAILED_PLCP_CRC;
534 error &= ~AR9170_RX_ERROR_PLCP;
e9348cdd 535
cca84799
CL
536 if (!(ar->filter_state & FIF_PLCPFAIL))
537 return -EINVAL;
538 }
e9348cdd 539
cca84799
CL
540 if (error & AR9170_RX_ERROR_FCS) {
541 status->flag |= RX_FLAG_FAILED_FCS_CRC;
542 error &= ~AR9170_RX_ERROR_FCS;
e9348cdd 543
cca84799
CL
544 if (!(ar->filter_state & FIF_FCSFAIL))
545 return -EINVAL;
546 }
547
548 decrypt = ar9170_get_decrypt_type(mac);
549 if (!(decrypt & AR9170_RX_ENC_SOFTWARE) &&
550 decrypt != AR9170_ENC_ALG_NONE)
551 status->flag |= RX_FLAG_DECRYPTED;
e9348cdd 552
cca84799
CL
553 /* ignore wrong RA errors */
554 error &= ~AR9170_RX_ERROR_WRONG_RA;
e9348cdd 555
cca84799
CL
556 if (error & AR9170_RX_ERROR_DECRYPT) {
557 error &= ~AR9170_RX_ERROR_DECRYPT;
558 /*
559 * Rx decryption is done in place,
560 * the original data is lost anyway.
561 */
562
563 return -EINVAL;
564 }
565
566 /* drop any other error frames */
567 if (unlikely(error)) {
568 /* TODO: update netdevice's RX dropped/errors statistics */
569
570 if (ar9170_nag_limiter(ar))
571 printk(KERN_DEBUG "%s: received frame with "
572 "suspicious error code (%#x).\n",
573 wiphy_name(ar->hw->wiphy), error);
574
575 return -EINVAL;
576 }
577
578 status->band = ar->channel->band;
579 status->freq = ar->channel->center_freq;
580
581 switch (mac->status & AR9170_RX_STATUS_MODULATION_MASK) {
e9348cdd 582 case AR9170_RX_STATUS_MODULATION_CCK:
cca84799
CL
583 if (mac->status & AR9170_RX_STATUS_SHORT_PREAMBLE)
584 status->flag |= RX_FLAG_SHORTPRE;
e9348cdd
CL
585 switch (head->plcp[0]) {
586 case 0x0a:
cca84799 587 status->rate_idx = 0;
e9348cdd
CL
588 break;
589 case 0x14:
cca84799 590 status->rate_idx = 1;
e9348cdd
CL
591 break;
592 case 0x37:
cca84799 593 status->rate_idx = 2;
e9348cdd
CL
594 break;
595 case 0x6e:
cca84799 596 status->rate_idx = 3;
e9348cdd
CL
597 break;
598 default:
cca84799 599 if (ar9170_nag_limiter(ar))
e9348cdd
CL
600 printk(KERN_ERR "%s: invalid plcp cck rate "
601 "(%x).\n", wiphy_name(ar->hw->wiphy),
602 head->plcp[0]);
cca84799 603 return -EINVAL;
e9348cdd
CL
604 }
605 break;
cca84799 606
e9348cdd 607 case AR9170_RX_STATUS_MODULATION_OFDM:
cca84799
CL
608 switch (head->plcp[0] & 0xf) {
609 case 0xb:
610 status->rate_idx = 0;
e9348cdd 611 break;
cca84799
CL
612 case 0xf:
613 status->rate_idx = 1;
e9348cdd 614 break;
cca84799
CL
615 case 0xa:
616 status->rate_idx = 2;
e9348cdd 617 break;
cca84799
CL
618 case 0xe:
619 status->rate_idx = 3;
e9348cdd
CL
620 break;
621 case 0x9:
cca84799 622 status->rate_idx = 4;
e9348cdd 623 break;
cca84799
CL
624 case 0xd:
625 status->rate_idx = 5;
e9348cdd
CL
626 break;
627 case 0x8:
cca84799 628 status->rate_idx = 6;
e9348cdd 629 break;
cca84799
CL
630 case 0xc:
631 status->rate_idx = 7;
e9348cdd
CL
632 break;
633 default:
cca84799 634 if (ar9170_nag_limiter(ar))
e9348cdd
CL
635 printk(KERN_ERR "%s: invalid plcp ofdm rate "
636 "(%x).\n", wiphy_name(ar->hw->wiphy),
637 head->plcp[0]);
cca84799 638 return -EINVAL;
e9348cdd 639 }
cca84799
CL
640 if (status->band == IEEE80211_BAND_2GHZ)
641 status->rate_idx += 4;
e9348cdd 642 break;
cca84799 643
e9348cdd 644 case AR9170_RX_STATUS_MODULATION_HT:
cca84799
CL
645 if (head->plcp[3] & 0x80)
646 status->flag |= RX_FLAG_40MHZ;
647 if (head->plcp[6] & 0x80)
648 status->flag |= RX_FLAG_SHORT_GI;
649
650 status->rate_idx = clamp(0, 75, head->plcp[6] & 0x7f);
651 status->flag |= RX_FLAG_HT;
652 break;
653
e9348cdd
CL
654 case AR9170_RX_STATUS_MODULATION_DUPOFDM:
655 /* XXX */
cca84799 656 if (ar9170_nag_limiter(ar))
e9348cdd
CL
657 printk(KERN_ERR "%s: invalid modulation\n",
658 wiphy_name(ar->hw->wiphy));
cca84799 659 return -EINVAL;
e9348cdd
CL
660 }
661
cca84799
CL
662 return 0;
663}
e9348cdd 664
cca84799
CL
665static void ar9170_rx_phy_status(struct ar9170 *ar,
666 struct ar9170_rx_phystatus *phy,
667 struct ieee80211_rx_status *status)
668{
669 int i;
e9348cdd 670
cca84799
CL
671 BUILD_BUG_ON(sizeof(struct ar9170_rx_phystatus) != 20);
672
673 for (i = 0; i < 3; i++)
674 if (phy->rssi[i] != 0x80)
675 status->antenna |= BIT(i);
676
677 /* post-process RSSI */
678 for (i = 0; i < 7; i++)
679 if (phy->rssi[i] & 0x80)
680 phy->rssi[i] = ((phy->rssi[i] & 0x7f) + 1) & 0x7f;
681
682 /* TODO: we could do something with phy_errors */
683 status->signal = ar->noise[0] + phy->rssi_combined;
684 status->noise = ar->noise[0];
685}
686
687static struct sk_buff *ar9170_rx_copy_data(u8 *buf, int len)
688{
689 struct sk_buff *skb;
690 int reserved = 0;
691 struct ieee80211_hdr *hdr = (void *) buf;
692
693 if (ieee80211_is_data_qos(hdr->frame_control)) {
694 u8 *qc = ieee80211_get_qos_ctl(hdr);
695 reserved += NET_IP_ALIGN;
696
697 if (*qc & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
698 reserved += NET_IP_ALIGN;
e9348cdd
CL
699 }
700
cca84799
CL
701 if (ieee80211_has_a4(hdr->frame_control))
702 reserved += NET_IP_ALIGN;
703
704 reserved = 32 + (reserved & NET_IP_ALIGN);
705
706 skb = dev_alloc_skb(len + reserved);
707 if (likely(skb)) {
708 skb_reserve(skb, reserved);
709 memcpy(skb_put(skb, len), buf, len);
e9348cdd
CL
710 }
711
cca84799
CL
712 return skb;
713}
e9348cdd 714
cca84799
CL
715/*
716 * If the frame alignment is right (or the kernel has
717 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS), and there
718 * is only a single MPDU in the USB frame, then we could
719 * submit to mac80211 the SKB directly. However, since
720 * there may be multiple packets in one SKB in stream
721 * mode, and we need to observe the proper ordering,
722 * this is non-trivial.
723 */
e9348cdd 724
cca84799
CL
725static void ar9170_handle_mpdu(struct ar9170 *ar, u8 *buf, int len)
726{
727 struct ar9170_rx_head *head;
728 struct ar9170_rx_macstatus *mac;
729 struct ar9170_rx_phystatus *phy = NULL;
730 struct ieee80211_rx_status status;
731 struct sk_buff *skb;
732 int mpdu_len;
733
734 if (unlikely(!IS_STARTED(ar) || len < (sizeof(*mac))))
735 return ;
736
737 /* Received MPDU */
738 mpdu_len = len - sizeof(*mac);
739
740 mac = (void *)(buf + mpdu_len);
741 if (unlikely(mac->error & AR9170_RX_ERROR_FATAL)) {
742 /* this frame is too damaged and can't be used - drop it */
e9348cdd 743
e9348cdd
CL
744 return ;
745 }
746
cca84799
CL
747 switch (mac->status & AR9170_RX_STATUS_MPDU_MASK) {
748 case AR9170_RX_STATUS_MPDU_FIRST:
749 /* first mpdu packet has the plcp header */
750 if (likely(mpdu_len >= sizeof(struct ar9170_rx_head))) {
751 head = (void *) buf;
752 memcpy(&ar->rx_mpdu.plcp, (void *) buf,
753 sizeof(struct ar9170_rx_head));
754
755 mpdu_len -= sizeof(struct ar9170_rx_head);
756 buf += sizeof(struct ar9170_rx_head);
757 ar->rx_mpdu.has_plcp = true;
758 } else {
759 if (ar9170_nag_limiter(ar))
760 printk(KERN_ERR "%s: plcp info is clipped.\n",
761 wiphy_name(ar->hw->wiphy));
762 return ;
763 }
764 break;
765
766 case AR9170_RX_STATUS_MPDU_LAST:
767 /* last mpdu has a extra tail with phy status information */
768
769 if (likely(mpdu_len >= sizeof(struct ar9170_rx_phystatus))) {
770 mpdu_len -= sizeof(struct ar9170_rx_phystatus);
771 phy = (void *)(buf + mpdu_len);
772 } else {
773 if (ar9170_nag_limiter(ar))
774 printk(KERN_ERR "%s: frame tail is clipped.\n",
775 wiphy_name(ar->hw->wiphy));
776 return ;
777 }
778
779 case AR9170_RX_STATUS_MPDU_MIDDLE:
780 /* middle mpdus are just data */
781 if (unlikely(!ar->rx_mpdu.has_plcp)) {
782 if (!ar9170_nag_limiter(ar))
783 return ;
784
785 printk(KERN_ERR "%s: rx stream did not start "
786 "with a first_mpdu frame tag.\n",
787 wiphy_name(ar->hw->wiphy));
788
789 return ;
790 }
791
792 head = &ar->rx_mpdu.plcp;
793 break;
794
795 case AR9170_RX_STATUS_MPDU_SINGLE:
796 /* single mpdu - has plcp (head) and phy status (tail) */
797 head = (void *) buf;
798
799 mpdu_len -= sizeof(struct ar9170_rx_head);
800 mpdu_len -= sizeof(struct ar9170_rx_phystatus);
801
802 buf += sizeof(struct ar9170_rx_head);
803 phy = (void *)(buf + mpdu_len);
804 break;
805
806 default:
807 BUG_ON(1);
808 break;
e9348cdd
CL
809 }
810
cca84799
CL
811 if (unlikely(mpdu_len < FCS_LEN))
812 return ;
e9348cdd 813
cca84799
CL
814 memset(&status, 0, sizeof(status));
815 if (unlikely(ar9170_rx_mac_status(ar, head, mac, &status)))
816 return ;
e9348cdd 817
cca84799
CL
818 if (phy)
819 ar9170_rx_phy_status(ar, phy, &status);
e9348cdd 820
cca84799
CL
821 skb = ar9170_rx_copy_data(buf, mpdu_len);
822 if (likely(skb))
823 ieee80211_rx_irqsafe(ar->hw, skb, &status);
e9348cdd
CL
824}
825
e9348cdd
CL
826void ar9170_rx(struct ar9170 *ar, struct sk_buff *skb)
827{
cca84799 828 unsigned int i, tlen, resplen, wlen = 0, clen = 0;
e9348cdd
CL
829 u8 *tbuf, *respbuf;
830
831 tbuf = skb->data;
832 tlen = skb->len;
833
834 while (tlen >= 4) {
cca84799
CL
835 clen = tbuf[1] << 8 | tbuf[0];
836 wlen = ALIGN(clen, 4);
e9348cdd 837
cca84799 838 /* check if this is stream has a valid tag.*/
e9348cdd 839 if (tbuf[2] != 0 || tbuf[3] != 0x4e) {
cca84799
CL
840 /*
841 * TODO: handle the highly unlikely event that the
842 * corrupted stream has the TAG at the right position.
843 */
844
845 /* check if the frame can be repaired. */
846 if (!ar->rx_failover_missing) {
847 /* this is no "short read". */
848 if (ar9170_nag_limiter(ar)) {
849 printk(KERN_ERR "%s: missing tag!\n",
850 wiphy_name(ar->hw->wiphy));
851 goto err_telluser;
852 } else
853 goto err_silent;
854 }
855
856 if (ar->rx_failover_missing > tlen) {
857 if (ar9170_nag_limiter(ar)) {
858 printk(KERN_ERR "%s: possible multi "
859 "stream corruption!\n",
860 wiphy_name(ar->hw->wiphy));
861 goto err_telluser;
862 } else
863 goto err_silent;
864 }
865
866 memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
867 ar->rx_failover_missing -= tlen;
868
869 if (ar->rx_failover_missing <= 0) {
870 /*
871 * nested ar9170_rx call!
872 * termination is guranteed, even when the
873 * combined frame also have a element with
874 * a bad tag.
875 */
876
877 ar->rx_failover_missing = 0;
878 ar9170_rx(ar, ar->rx_failover);
879
880 skb_reset_tail_pointer(ar->rx_failover);
881 skb_trim(ar->rx_failover, 0);
882 }
883
e9348cdd
CL
884 return ;
885 }
cca84799
CL
886
887 /* check if stream is clipped */
e9348cdd 888 if (wlen > tlen - 4) {
cca84799
CL
889 if (ar->rx_failover_missing) {
890 /* TODO: handle double stream corruption. */
891 if (ar9170_nag_limiter(ar)) {
892 printk(KERN_ERR "%s: double rx stream "
893 "corruption!\n",
894 wiphy_name(ar->hw->wiphy));
895 goto err_telluser;
896 } else
897 goto err_silent;
898 }
899
900 /*
901 * save incomplete data set.
902 * the firmware will resend the missing bits when
903 * the rx - descriptor comes round again.
904 */
905
906 memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
907 ar->rx_failover_missing = clen - tlen;
e9348cdd
CL
908 return ;
909 }
910 resplen = clen;
911 respbuf = tbuf + 4;
912 tbuf += wlen + 4;
913 tlen -= wlen + 4;
914
915 i = 0;
916
917 /* weird thing, but this is the same in the original driver */
918 while (resplen > 2 && i < 12 &&
919 respbuf[0] == 0xff && respbuf[1] == 0xff) {
920 i += 2;
921 resplen -= 2;
922 respbuf += 2;
923 }
924
925 if (resplen < 4)
926 continue;
927
928 /* found the 6 * 0xffff marker? */
929 if (i == 12)
930 ar9170_handle_command_response(ar, respbuf, resplen);
931 else
cca84799 932 ar9170_handle_mpdu(ar, respbuf, clen);
e9348cdd
CL
933 }
934
cca84799
CL
935 if (tlen) {
936 if (net_ratelimit())
937 printk(KERN_ERR "%s: %d bytes of unprocessed "
938 "data left in rx stream!\n",
939 wiphy_name(ar->hw->wiphy), tlen);
940
941 goto err_telluser;
942 }
943
944 return ;
945
946err_telluser:
947 printk(KERN_ERR "%s: damaged RX stream data [want:%d, "
948 "data:%d, rx:%d, pending:%d ]\n",
949 wiphy_name(ar->hw->wiphy), clen, wlen, tlen,
950 ar->rx_failover_missing);
951
952 if (ar->rx_failover_missing)
953 print_hex_dump_bytes("rxbuf:", DUMP_PREFIX_OFFSET,
954 ar->rx_failover->data,
955 ar->rx_failover->len);
956
957 print_hex_dump_bytes("stream:", DUMP_PREFIX_OFFSET,
958 skb->data, skb->len);
959
960 printk(KERN_ERR "%s: please check your hardware and cables, if "
961 "you see this message frequently.\n",
962 wiphy_name(ar->hw->wiphy));
963
964err_silent:
965 if (ar->rx_failover_missing) {
966 skb_reset_tail_pointer(ar->rx_failover);
967 skb_trim(ar->rx_failover, 0);
968 ar->rx_failover_missing = 0;
969 }
e9348cdd 970}
e9348cdd
CL
971
972#define AR9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop) \
973do { \
974 queue.aifs = ai_fs; \
975 queue.cw_min = cwmin; \
976 queue.cw_max = cwmax; \
977 queue.txop = _txop; \
978} while (0)
979
980static int ar9170_op_start(struct ieee80211_hw *hw)
981{
982 struct ar9170 *ar = hw->priv;
983 int err, i;
984
985 mutex_lock(&ar->mutex);
986
864cc02e
CL
987 ar->filter_changed = 0;
988
e9348cdd
CL
989 /* reinitialize queues statistics */
990 memset(&ar->tx_stats, 0, sizeof(ar->tx_stats));
991 for (i = 0; i < ARRAY_SIZE(ar->tx_stats); i++)
992 ar->tx_stats[i].limit = 8;
993
994 /* reset QoS defaults */
995 AR9170_FILL_QUEUE(ar->edcf[0], 3, 15, 1023, 0); /* BEST EFFORT*/
996 AR9170_FILL_QUEUE(ar->edcf[1], 7, 15, 1023, 0); /* BACKGROUND */
997 AR9170_FILL_QUEUE(ar->edcf[2], 2, 7, 15, 94); /* VIDEO */
998 AR9170_FILL_QUEUE(ar->edcf[3], 2, 3, 7, 47); /* VOICE */
999 AR9170_FILL_QUEUE(ar->edcf[4], 2, 3, 7, 0); /* SPECIAL */
1000
cca84799
CL
1001 ar->bad_hw_nagger = jiffies;
1002
e9348cdd
CL
1003 err = ar->open(ar);
1004 if (err)
1005 goto out;
1006
1007 err = ar9170_init_mac(ar);
1008 if (err)
1009 goto out;
1010
1011 err = ar9170_set_qos(ar);
1012 if (err)
1013 goto out;
1014
1015 err = ar9170_init_phy(ar, IEEE80211_BAND_2GHZ);
1016 if (err)
1017 goto out;
1018
1019 err = ar9170_init_rf(ar);
1020 if (err)
1021 goto out;
1022
1023 /* start DMA */
1024 err = ar9170_write_reg(ar, 0x1c3d30, 0x100);
1025 if (err)
1026 goto out;
1027
1028 ar->state = AR9170_STARTED;
1029
1030out:
1031 mutex_unlock(&ar->mutex);
1032 return err;
1033}
1034
1035static void ar9170_op_stop(struct ieee80211_hw *hw)
1036{
1037 struct ar9170 *ar = hw->priv;
1038
1039 if (IS_STARTED(ar))
1040 ar->state = AR9170_IDLE;
1041
32c1628f 1042 flush_workqueue(ar->hw->workqueue);
e9348cdd
CL
1043
1044 cancel_delayed_work_sync(&ar->tx_status_janitor);
1045 cancel_work_sync(&ar->filter_config_work);
1046 cancel_work_sync(&ar->beacon_work);
b55d6bcf 1047 mutex_lock(&ar->mutex);
e9348cdd
CL
1048 skb_queue_purge(&ar->global_tx_status_waste);
1049 skb_queue_purge(&ar->global_tx_status);
1050
1051 if (IS_ACCEPTING_CMD(ar)) {
1052 ar9170_set_leds_state(ar, 0);
1053
1054 /* stop DMA */
1055 ar9170_write_reg(ar, 0x1c3d30, 0);
1056 ar->stop(ar);
1057 }
1058
1059 mutex_unlock(&ar->mutex);
1060}
1061
1062int ar9170_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
1063{
1064 struct ar9170 *ar = hw->priv;
1065 struct ieee80211_hdr *hdr;
1066 struct ar9170_tx_control *txc;
1067 struct ieee80211_tx_info *info;
1068 struct ieee80211_rate *rate = NULL;
1069 struct ieee80211_tx_rate *txrate;
1070 unsigned int queue = skb_get_queue_mapping(skb);
1071 unsigned long flags = 0;
1072 struct ar9170_sta_info *sta_info = NULL;
1073 u32 power, chains;
1074 u16 keytype = 0;
1075 u16 len, icv = 0;
1076 int err;
1077 bool tx_status;
1078
1079 if (unlikely(!IS_STARTED(ar)))
1080 goto err_free;
1081
1082 hdr = (void *)skb->data;
1083 info = IEEE80211_SKB_CB(skb);
1084 len = skb->len;
1085
1086 spin_lock_irqsave(&ar->tx_stats_lock, flags);
1087 if (ar->tx_stats[queue].limit < ar->tx_stats[queue].len) {
1088 spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
1089 return NETDEV_TX_OK;
1090 }
1091
1092 ar->tx_stats[queue].len++;
1093 ar->tx_stats[queue].count++;
1094 if (ar->tx_stats[queue].limit == ar->tx_stats[queue].len)
1095 ieee80211_stop_queue(hw, queue);
1096
1097 spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
1098
1099 txc = (void *)skb_push(skb, sizeof(*txc));
1100
1101 tx_status = (((info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) != 0) ||
1102 ((info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS) != 0));
1103
1104 if (info->control.hw_key) {
1105 icv = info->control.hw_key->icv_len;
1106
1107 switch (info->control.hw_key->alg) {
1108 case ALG_WEP:
1109 keytype = AR9170_TX_MAC_ENCR_RC4;
1110 break;
1111 case ALG_TKIP:
1112 keytype = AR9170_TX_MAC_ENCR_RC4;
1113 break;
1114 case ALG_CCMP:
1115 keytype = AR9170_TX_MAC_ENCR_AES;
1116 break;
1117 default:
1118 WARN_ON(1);
1119 goto err_dequeue;
1120 }
1121 }
1122
1123 /* Length */
1124 txc->length = cpu_to_le16(len + icv + 4);
1125
1126 txc->mac_control = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
1127 AR9170_TX_MAC_BACKOFF);
1128 txc->mac_control |= cpu_to_le16(ar9170_qos_hwmap[queue] <<
1129 AR9170_TX_MAC_QOS_SHIFT);
1130 txc->mac_control |= cpu_to_le16(keytype);
1131 txc->phy_control = cpu_to_le32(0);
1132
1133 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1134 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
1135
1136 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1137 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_AGGR);
1138
1139 txrate = &info->control.rates[0];
1140
1141 if (txrate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1142 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
1143 else if (txrate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1144 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
1145
1146 if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1147 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
1148
1149 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1150 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
1151
1152 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1153 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ);
1154 /* this works because 40 MHz is 2 and dup is 3 */
1155 if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
1156 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP);
1157
1158 if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
1159 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
1160
1161 if (txrate->flags & IEEE80211_TX_RC_MCS) {
1162 u32 r = txrate->idx;
1163 u8 *txpower;
1164
1165 r <<= AR9170_TX_PHY_MCS_SHIFT;
1166 if (WARN_ON(r & ~AR9170_TX_PHY_MCS_MASK))
1167 goto err_dequeue;
1168 txc->phy_control |= cpu_to_le32(r & AR9170_TX_PHY_MCS_MASK);
1169 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
1170
1171 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
1172 if (info->band == IEEE80211_BAND_5GHZ)
1173 txpower = ar->power_5G_ht40;
1174 else
1175 txpower = ar->power_2G_ht40;
1176 } else {
1177 if (info->band == IEEE80211_BAND_5GHZ)
1178 txpower = ar->power_5G_ht20;
1179 else
1180 txpower = ar->power_2G_ht20;
1181 }
1182
1183 power = txpower[(txrate->idx) & 7];
1184 } else {
1185 u8 *txpower;
1186 u32 mod;
1187 u32 phyrate;
1188 u8 idx = txrate->idx;
1189
1190 if (info->band != IEEE80211_BAND_2GHZ) {
1191 idx += 4;
1192 txpower = ar->power_5G_leg;
1193 mod = AR9170_TX_PHY_MOD_OFDM;
1194 } else {
1195 if (idx < 4) {
1196 txpower = ar->power_2G_cck;
1197 mod = AR9170_TX_PHY_MOD_CCK;
1198 } else {
1199 mod = AR9170_TX_PHY_MOD_OFDM;
1200 txpower = ar->power_2G_ofdm;
1201 }
1202 }
1203
1204 rate = &__ar9170_ratetable[idx];
1205
1206 phyrate = rate->hw_value & 0xF;
1207 power = txpower[(rate->hw_value & 0x30) >> 4];
1208 phyrate <<= AR9170_TX_PHY_MCS_SHIFT;
1209
1210 txc->phy_control |= cpu_to_le32(mod);
1211 txc->phy_control |= cpu_to_le32(phyrate);
1212 }
1213
1214 power <<= AR9170_TX_PHY_TX_PWR_SHIFT;
1215 power &= AR9170_TX_PHY_TX_PWR_MASK;
1216 txc->phy_control |= cpu_to_le32(power);
1217
1218 /* set TX chains */
1219 if (ar->eeprom.tx_mask == 1) {
1220 chains = AR9170_TX_PHY_TXCHAIN_1;
1221 } else {
1222 chains = AR9170_TX_PHY_TXCHAIN_2;
1223
1224 /* >= 36M legacy OFDM - use only one chain */
1225 if (rate && rate->bitrate >= 360)
1226 chains = AR9170_TX_PHY_TXCHAIN_1;
1227 }
1228 txc->phy_control |= cpu_to_le32(chains << AR9170_TX_PHY_TXCHAIN_SHIFT);
1229
1230 if (tx_status) {
1231 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_RATE_PROBE);
1232 /*
1233 * WARNING:
1234 * Putting the QoS queue bits into an unexplored territory is
1235 * certainly not elegant.
1236 *
1237 * In my defense: This idea provides a reasonable way to
1238 * smuggle valuable information to the tx_status callback.
1239 * Also, the idea behind this bit-abuse came straight from
1240 * the original driver code.
1241 */
1242
1243 txc->phy_control |=
1244 cpu_to_le32(queue << AR9170_TX_PHY_QOS_SHIFT);
1245
1246 if (info->control.sta) {
1247 sta_info = (void *) info->control.sta->drv_priv;
4a48e2a4 1248 skb_queue_tail(&sta_info->tx_status[queue], skb);
e9348cdd
CL
1249 } else {
1250 skb_queue_tail(&ar->global_tx_status, skb);
1251
1252 queue_delayed_work(ar->hw->workqueue,
1253 &ar->tx_status_janitor,
1254 msecs_to_jiffies(100));
1255 }
1256 }
1257
1258 err = ar->tx(ar, skb, tx_status, 0);
1259 if (unlikely(tx_status && err)) {
1260 if (info->control.sta)
4a48e2a4 1261 skb_unlink(skb, &sta_info->tx_status[queue]);
e9348cdd
CL
1262 else
1263 skb_unlink(skb, &ar->global_tx_status);
1264 }
1265
1266 return NETDEV_TX_OK;
1267
1268err_dequeue:
1269 spin_lock_irqsave(&ar->tx_stats_lock, flags);
1270 ar->tx_stats[queue].len--;
1271 ar->tx_stats[queue].count--;
1272 spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
1273
1274err_free:
1275 dev_kfree_skb(skb);
1276 return NETDEV_TX_OK;
1277}
1278
1279static int ar9170_op_add_interface(struct ieee80211_hw *hw,
1280 struct ieee80211_if_init_conf *conf)
1281{
1282 struct ar9170 *ar = hw->priv;
1283 int err = 0;
1284
1285 mutex_lock(&ar->mutex);
1286
1287 if (ar->vif) {
1288 err = -EBUSY;
1289 goto unlock;
1290 }
1291
1292 ar->vif = conf->vif;
1293 memcpy(ar->mac_addr, conf->mac_addr, ETH_ALEN);
1294
1295 if (modparam_nohwcrypt || (ar->vif->type != NL80211_IFTYPE_STATION)) {
1296 ar->rx_software_decryption = true;
1297 ar->disable_offload = true;
1298 }
1299
1300 ar->cur_filter = 0;
1301 ar->want_filter = AR9170_MAC_REG_FTF_DEFAULTS;
1302 err = ar9170_update_frame_filter(ar);
1303 if (err)
1304 goto unlock;
1305
1306 err = ar9170_set_operating_mode(ar);
1307
1308unlock:
1309 mutex_unlock(&ar->mutex);
1310 return err;
1311}
1312
1313static void ar9170_op_remove_interface(struct ieee80211_hw *hw,
1314 struct ieee80211_if_init_conf *conf)
1315{
1316 struct ar9170 *ar = hw->priv;
1317
1318 mutex_lock(&ar->mutex);
1319 ar->vif = NULL;
1320 ar->want_filter = 0;
1321 ar9170_update_frame_filter(ar);
1322 ar9170_set_beacon_timers(ar);
1323 dev_kfree_skb(ar->beacon);
1324 ar->beacon = NULL;
1325 ar->sniffer_enabled = false;
1326 ar->rx_software_decryption = false;
1327 ar9170_set_operating_mode(ar);
1328 mutex_unlock(&ar->mutex);
1329}
1330
1331static int ar9170_op_config(struct ieee80211_hw *hw, u32 changed)
1332{
1333 struct ar9170 *ar = hw->priv;
1334 int err = 0;
1335
1336 mutex_lock(&ar->mutex);
1337
e9348cdd
CL
1338 if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
1339 /* TODO */
1340 err = 0;
1341 }
1342
1343 if (changed & IEEE80211_CONF_CHANGE_PS) {
1344 /* TODO */
1345 err = 0;
1346 }
1347
1348 if (changed & IEEE80211_CONF_CHANGE_POWER) {
1349 /* TODO */
1350 err = 0;
1351 }
1352
1353 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
1354 /*
1355 * is it long_frame_max_tx_count or short_frame_max_tx_count?
1356 */
1357
1358 err = ar9170_set_hwretry_limit(ar,
1359 ar->hw->conf.long_frame_max_tx_count);
1360 if (err)
1361 goto out;
1362 }
1363
57c4d7b4 1364 if (changed & BSS_CHANGED_BEACON_INT) {
e9348cdd
CL
1365 err = ar9170_set_beacon_timers(ar);
1366 if (err)
1367 goto out;
1368 }
1369
1370 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
29ceff5d
CL
1371
1372 /* adjust slot time for 5 GHz */
1373 err = ar9170_set_slot_time(ar);
1374 if (err)
1375 goto out;
1376
1377 err = ar9170_set_dyn_sifs_ack(ar);
1378 if (err)
1379 goto out;
1380
e9348cdd 1381 err = ar9170_set_channel(ar, hw->conf.channel,
9e52b062
JB
1382 AR9170_RFI_NONE,
1383 nl80211_to_ar9170(hw->conf.channel_type));
e9348cdd
CL
1384 if (err)
1385 goto out;
e9348cdd
CL
1386 }
1387
1388out:
1389 mutex_unlock(&ar->mutex);
1390 return err;
1391}
1392
e9348cdd
CL
1393static void ar9170_set_filters(struct work_struct *work)
1394{
1395 struct ar9170 *ar = container_of(work, struct ar9170,
1396 filter_config_work);
1397 int err;
1398
e9348cdd 1399 if (unlikely(!IS_STARTED(ar)))
32c1628f 1400 return ;
e9348cdd 1401
32c1628f 1402 mutex_lock(&ar->mutex);
864cc02e
CL
1403 if (test_and_clear_bit(AR9170_FILTER_CHANGED_MODE,
1404 &ar->filter_changed)) {
e9348cdd
CL
1405 err = ar9170_set_operating_mode(ar);
1406 if (err)
1407 goto unlock;
1408 }
1409
864cc02e
CL
1410 if (test_and_clear_bit(AR9170_FILTER_CHANGED_MULTICAST,
1411 &ar->filter_changed)) {
e9348cdd
CL
1412 err = ar9170_update_multicast(ar);
1413 if (err)
1414 goto unlock;
1415 }
1416
864cc02e
CL
1417 if (test_and_clear_bit(AR9170_FILTER_CHANGED_FRAMEFILTER,
1418 &ar->filter_changed)) {
e9348cdd 1419 err = ar9170_update_frame_filter(ar);
864cc02e
CL
1420 if (err)
1421 goto unlock;
1422 }
e9348cdd
CL
1423
1424unlock:
1425 mutex_unlock(&ar->mutex);
1426}
1427
1428static void ar9170_op_configure_filter(struct ieee80211_hw *hw,
1429 unsigned int changed_flags,
1430 unsigned int *new_flags,
1431 int mc_count, struct dev_mc_list *mclist)
1432{
1433 struct ar9170 *ar = hw->priv;
1434
1435 /* mask supported flags */
1436 *new_flags &= FIF_ALLMULTI | FIF_CONTROL | FIF_BCN_PRBRESP_PROMISC |
cca84799
CL
1437 FIF_PROMISC_IN_BSS | FIF_FCSFAIL | FIF_PLCPFAIL;
1438 ar->filter_state = *new_flags;
e9348cdd
CL
1439 /*
1440 * We can support more by setting the sniffer bit and
1441 * then checking the error flags, later.
1442 */
1443
1444 if (changed_flags & FIF_ALLMULTI) {
1445 if (*new_flags & FIF_ALLMULTI) {
1446 ar->want_mc_hash = ~0ULL;
1447 } else {
1448 u64 mchash;
1449 int i;
1450
1451 /* always get broadcast frames */
864cc02e 1452 mchash = 1ULL << (0xff >> 2);
e9348cdd
CL
1453
1454 for (i = 0; i < mc_count; i++) {
1455 if (WARN_ON(!mclist))
1456 break;
1457 mchash |= 1ULL << (mclist->dmi_addr[5] >> 2);
1458 mclist = mclist->next;
1459 }
1460 ar->want_mc_hash = mchash;
1461 }
864cc02e 1462 set_bit(AR9170_FILTER_CHANGED_MULTICAST, &ar->filter_changed);
e9348cdd
CL
1463 }
1464
1465 if (changed_flags & FIF_CONTROL) {
1466 u32 filter = AR9170_MAC_REG_FTF_PSPOLL |
1467 AR9170_MAC_REG_FTF_RTS |
1468 AR9170_MAC_REG_FTF_CTS |
1469 AR9170_MAC_REG_FTF_ACK |
1470 AR9170_MAC_REG_FTF_CFE |
1471 AR9170_MAC_REG_FTF_CFE_ACK;
1472
1473 if (*new_flags & FIF_CONTROL)
1474 ar->want_filter = ar->cur_filter | filter;
1475 else
1476 ar->want_filter = ar->cur_filter & ~filter;
1477
864cc02e
CL
1478 set_bit(AR9170_FILTER_CHANGED_FRAMEFILTER,
1479 &ar->filter_changed);
e9348cdd
CL
1480 }
1481
1482 if (changed_flags & FIF_PROMISC_IN_BSS) {
1483 ar->sniffer_enabled = ((*new_flags) & FIF_PROMISC_IN_BSS) != 0;
864cc02e
CL
1484 set_bit(AR9170_FILTER_CHANGED_MODE,
1485 &ar->filter_changed);
e9348cdd
CL
1486 }
1487
1488 if (likely(IS_STARTED(ar)))
1489 queue_work(ar->hw->workqueue, &ar->filter_config_work);
1490}
1491
1492static void ar9170_op_bss_info_changed(struct ieee80211_hw *hw,
1493 struct ieee80211_vif *vif,
1494 struct ieee80211_bss_conf *bss_conf,
1495 u32 changed)
1496{
1497 struct ar9170 *ar = hw->priv;
1498 int err = 0;
1499
1500 mutex_lock(&ar->mutex);
1501
2d0ddec5
JB
1502 if (changed & BSS_CHANGED_BSSID) {
1503 memcpy(ar->bssid, bss_conf->bssid, ETH_ALEN);
1504 err = ar9170_set_operating_mode(ar);
29ceff5d
CL
1505 if (err)
1506 goto out;
2d0ddec5
JB
1507 }
1508
1509 if (changed & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED)) {
1510 err = ar9170_update_beacon(ar);
29ceff5d
CL
1511 if (err)
1512 goto out;
2d0ddec5 1513
29ceff5d
CL
1514 err = ar9170_set_beacon_timers(ar);
1515 if (err)
1516 goto out;
1517 }
e9348cdd
CL
1518
1519 if (changed & BSS_CHANGED_ASSOC) {
e9348cdd
CL
1520#ifndef CONFIG_AR9170_LEDS
1521 /* enable assoc LED. */
1522 err = ar9170_set_leds_state(ar, bss_conf->assoc ? 2 : 0);
1523#endif /* CONFIG_AR9170_LEDS */
1524 }
1525
29ceff5d 1526 if (changed & BSS_CHANGED_BEACON_INT) {
57c4d7b4 1527 err = ar9170_set_beacon_timers(ar);
29ceff5d
CL
1528 if (err)
1529 goto out;
1530 }
57c4d7b4 1531
e9348cdd
CL
1532 if (changed & BSS_CHANGED_HT) {
1533 /* TODO */
1534 err = 0;
1535 }
1536
1537 if (changed & BSS_CHANGED_ERP_SLOT) {
29ceff5d
CL
1538 err = ar9170_set_slot_time(ar);
1539 if (err)
1540 goto out;
e9348cdd
CL
1541 }
1542
1543 if (changed & BSS_CHANGED_BASIC_RATES) {
29ceff5d
CL
1544 err = ar9170_set_basic_rates(ar);
1545 if (err)
1546 goto out;
e9348cdd
CL
1547 }
1548
29ceff5d 1549out:
e9348cdd
CL
1550 mutex_unlock(&ar->mutex);
1551}
1552
1553static u64 ar9170_op_get_tsf(struct ieee80211_hw *hw)
1554{
1555 struct ar9170 *ar = hw->priv;
1556 int err;
1557 u32 tsf_low;
1558 u32 tsf_high;
1559 u64 tsf;
1560
1561 mutex_lock(&ar->mutex);
1562 err = ar9170_read_reg(ar, AR9170_MAC_REG_TSF_L, &tsf_low);
1563 if (!err)
1564 err = ar9170_read_reg(ar, AR9170_MAC_REG_TSF_H, &tsf_high);
1565 mutex_unlock(&ar->mutex);
1566
1567 if (WARN_ON(err))
1568 return 0;
1569
1570 tsf = tsf_high;
1571 tsf = (tsf << 32) | tsf_low;
1572 return tsf;
1573}
1574
1575static int ar9170_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1576 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
1577 struct ieee80211_key_conf *key)
1578{
1579 struct ar9170 *ar = hw->priv;
1580 int err = 0, i;
1581 u8 ktype;
1582
1583 if ((!ar->vif) || (ar->disable_offload))
1584 return -EOPNOTSUPP;
1585
1586 switch (key->alg) {
1587 case ALG_WEP:
e31a16d6 1588 if (key->keylen == WLAN_KEY_LEN_WEP40)
e9348cdd
CL
1589 ktype = AR9170_ENC_ALG_WEP64;
1590 else
1591 ktype = AR9170_ENC_ALG_WEP128;
1592 break;
1593 case ALG_TKIP:
1594 ktype = AR9170_ENC_ALG_TKIP;
1595 break;
1596 case ALG_CCMP:
1597 ktype = AR9170_ENC_ALG_AESCCMP;
1598 break;
1599 default:
1600 return -EOPNOTSUPP;
1601 }
1602
1603 mutex_lock(&ar->mutex);
1604 if (cmd == SET_KEY) {
1605 if (unlikely(!IS_STARTED(ar))) {
1606 err = -EOPNOTSUPP;
1607 goto out;
1608 }
1609
1610 /* group keys need all-zeroes address */
1611 if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1612 sta = NULL;
1613
1614 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
1615 for (i = 0; i < 64; i++)
1616 if (!(ar->usedkeys & BIT(i)))
1617 break;
1618 if (i == 64) {
1619 ar->rx_software_decryption = true;
1620 ar9170_set_operating_mode(ar);
1621 err = -ENOSPC;
1622 goto out;
1623 }
1624 } else {
1625 i = 64 + key->keyidx;
1626 }
1627
1628 key->hw_key_idx = i;
1629
1630 err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL, ktype, 0,
1631 key->key, min_t(u8, 16, key->keylen));
1632 if (err)
1633 goto out;
1634
1635 if (key->alg == ALG_TKIP) {
1636 err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL,
1637 ktype, 1, key->key + 16, 16);
1638 if (err)
1639 goto out;
1640
1641 /*
1642 * hardware is not capable generating the MMIC
1643 * for fragmented frames!
1644 */
1645 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1646 }
1647
1648 if (i < 64)
1649 ar->usedkeys |= BIT(i);
1650
1651 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1652 } else {
1653 if (unlikely(!IS_STARTED(ar))) {
1654 /* The device is gone... together with the key ;-) */
1655 err = 0;
1656 goto out;
1657 }
1658
1659 err = ar9170_disable_key(ar, key->hw_key_idx);
1660 if (err)
1661 goto out;
1662
1663 if (key->hw_key_idx < 64) {
1664 ar->usedkeys &= ~BIT(key->hw_key_idx);
1665 } else {
1666 err = ar9170_upload_key(ar, key->hw_key_idx, NULL,
1667 AR9170_ENC_ALG_NONE, 0,
1668 NULL, 0);
1669 if (err)
1670 goto out;
1671
1672 if (key->alg == ALG_TKIP) {
1673 err = ar9170_upload_key(ar, key->hw_key_idx,
1674 NULL,
1675 AR9170_ENC_ALG_NONE, 1,
1676 NULL, 0);
1677 if (err)
1678 goto out;
1679 }
1680
1681 }
1682 }
1683
1684 ar9170_regwrite_begin(ar);
1685 ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_L, ar->usedkeys);
1686 ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_H, ar->usedkeys >> 32);
1687 ar9170_regwrite_finish();
1688 err = ar9170_regwrite_result();
1689
1690out:
1691 mutex_unlock(&ar->mutex);
1692
1693 return err;
1694}
1695
1696static void ar9170_sta_notify(struct ieee80211_hw *hw,
1697 struct ieee80211_vif *vif,
1698 enum sta_notify_cmd cmd,
1699 struct ieee80211_sta *sta)
1700{
1701 struct ar9170 *ar = hw->priv;
1702 struct ar9170_sta_info *info = (void *) sta->drv_priv;
1703 struct sk_buff *skb;
4a48e2a4 1704 unsigned int i;
e9348cdd
CL
1705
1706 switch (cmd) {
1707 case STA_NOTIFY_ADD:
4a48e2a4
CL
1708 for (i = 0; i < ar->hw->queues; i++)
1709 skb_queue_head_init(&info->tx_status[i]);
e9348cdd
CL
1710 break;
1711
1712 case STA_NOTIFY_REMOVE:
1713
1714 /*
1715 * transfer all outstanding frames that need a tx_status
4a48e2a4 1716 * reports to the global tx_status queue
e9348cdd
CL
1717 */
1718
4a48e2a4
CL
1719 for (i = 0; i < ar->hw->queues; i++) {
1720 while ((skb = skb_dequeue(&info->tx_status[i]))) {
e9348cdd 1721#ifdef AR9170_QUEUE_DEBUG
4a48e2a4
CL
1722 printk(KERN_DEBUG "%s: queueing frame in "
1723 "global tx_status queue =>\n",
1724 wiphy_name(ar->hw->wiphy));
e9348cdd 1725
4a48e2a4 1726 ar9170_print_txheader(ar, skb);
e9348cdd 1727#endif /* AR9170_QUEUE_DEBUG */
4a48e2a4
CL
1728 skb_queue_tail(&ar->global_tx_status, skb);
1729 }
e9348cdd 1730 }
4a48e2a4
CL
1731 queue_delayed_work(ar->hw->workqueue, &ar->tx_status_janitor,
1732 msecs_to_jiffies(100));
e9348cdd
CL
1733 break;
1734
1735 default:
1736 break;
1737 }
1738}
1739
1740static int ar9170_get_stats(struct ieee80211_hw *hw,
1741 struct ieee80211_low_level_stats *stats)
1742{
1743 struct ar9170 *ar = hw->priv;
1744 u32 val;
1745 int err;
1746
1747 mutex_lock(&ar->mutex);
1748 err = ar9170_read_reg(ar, AR9170_MAC_REG_TX_RETRY, &val);
1749 ar->stats.dot11ACKFailureCount += val;
1750
1751 memcpy(stats, &ar->stats, sizeof(*stats));
1752 mutex_unlock(&ar->mutex);
1753
1754 return 0;
1755}
1756
1757static int ar9170_get_tx_stats(struct ieee80211_hw *hw,
1758 struct ieee80211_tx_queue_stats *tx_stats)
1759{
1760 struct ar9170 *ar = hw->priv;
1761
1762 spin_lock_bh(&ar->tx_stats_lock);
1763 memcpy(tx_stats, ar->tx_stats, sizeof(tx_stats[0]) * hw->queues);
1764 spin_unlock_bh(&ar->tx_stats_lock);
1765
1766 return 0;
1767}
1768
1769static int ar9170_conf_tx(struct ieee80211_hw *hw, u16 queue,
1770 const struct ieee80211_tx_queue_params *param)
1771{
1772 struct ar9170 *ar = hw->priv;
1773 int ret;
1774
1775 mutex_lock(&ar->mutex);
4a48e2a4 1776 if ((param) && !(queue > ar->hw->queues)) {
e9348cdd
CL
1777 memcpy(&ar->edcf[ar9170_qos_hwmap[queue]],
1778 param, sizeof(*param));
1779
1780 ret = ar9170_set_qos(ar);
1781 } else
1782 ret = -EINVAL;
1783
1784 mutex_unlock(&ar->mutex);
1785 return ret;
1786}
1787
9e52b062
JB
1788static int ar9170_ampdu_action(struct ieee80211_hw *hw,
1789 enum ieee80211_ampdu_mlme_action action,
1790 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1791{
1792 switch (action) {
1793 case IEEE80211_AMPDU_RX_START:
1794 case IEEE80211_AMPDU_RX_STOP:
1795 /*
1796 * Something goes wrong -- RX locks up
1797 * after a while of receiving aggregated
1798 * frames -- not enabling for now.
1799 */
1800 return -EOPNOTSUPP;
1801 default:
1802 return -EOPNOTSUPP;
1803 }
1804}
1805
e9348cdd
CL
1806static const struct ieee80211_ops ar9170_ops = {
1807 .start = ar9170_op_start,
1808 .stop = ar9170_op_stop,
1809 .tx = ar9170_op_tx,
1810 .add_interface = ar9170_op_add_interface,
1811 .remove_interface = ar9170_op_remove_interface,
1812 .config = ar9170_op_config,
e9348cdd
CL
1813 .configure_filter = ar9170_op_configure_filter,
1814 .conf_tx = ar9170_conf_tx,
1815 .bss_info_changed = ar9170_op_bss_info_changed,
1816 .get_tsf = ar9170_op_get_tsf,
1817 .set_key = ar9170_set_key,
1818 .sta_notify = ar9170_sta_notify,
1819 .get_stats = ar9170_get_stats,
1820 .get_tx_stats = ar9170_get_tx_stats,
9e52b062 1821 .ampdu_action = ar9170_ampdu_action,
e9348cdd
CL
1822};
1823
1824void *ar9170_alloc(size_t priv_size)
1825{
1826 struct ieee80211_hw *hw;
1827 struct ar9170 *ar;
cca84799 1828 struct sk_buff *skb;
e9348cdd
CL
1829 int i;
1830
cca84799
CL
1831 /*
1832 * this buffer is used for rx stream reconstruction.
1833 * Under heavy load this device (or the transport layer?)
1834 * tends to split the streams into seperate rx descriptors.
1835 */
1836
1837 skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE, GFP_KERNEL);
1838 if (!skb)
1839 goto err_nomem;
1840
e9348cdd
CL
1841 hw = ieee80211_alloc_hw(priv_size, &ar9170_ops);
1842 if (!hw)
cca84799 1843 goto err_nomem;
e9348cdd
CL
1844
1845 ar = hw->priv;
1846 ar->hw = hw;
cca84799 1847 ar->rx_failover = skb;
e9348cdd
CL
1848
1849 mutex_init(&ar->mutex);
1850 spin_lock_init(&ar->cmdlock);
1851 spin_lock_init(&ar->tx_stats_lock);
1852 skb_queue_head_init(&ar->global_tx_status);
1853 skb_queue_head_init(&ar->global_tx_status_waste);
cca84799 1854 ar9170_rx_reset_rx_mpdu(ar);
e9348cdd
CL
1855 INIT_WORK(&ar->filter_config_work, ar9170_set_filters);
1856 INIT_WORK(&ar->beacon_work, ar9170_new_beacon);
1857 INIT_DELAYED_WORK(&ar->tx_status_janitor, ar9170_tx_status_janitor);
1858
1859 /* all hw supports 2.4 GHz, so set channel to 1 by default */
1860 ar->channel = &ar9170_2ghz_chantable[0];
1861
1862 /* first part of wiphy init */
1863 ar->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1864 BIT(NL80211_IFTYPE_WDS) |
1865 BIT(NL80211_IFTYPE_ADHOC);
1866 ar->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS |
1867 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
1868 IEEE80211_HW_SIGNAL_DBM |
1869 IEEE80211_HW_NOISE_DBM;
1870
4a48e2a4 1871 ar->hw->queues = __AR9170_NUM_TXQ;
e9348cdd
CL
1872 ar->hw->extra_tx_headroom = 8;
1873 ar->hw->sta_data_size = sizeof(struct ar9170_sta_info);
1874
1875 ar->hw->max_rates = 1;
1876 ar->hw->max_rate_tries = 3;
1877
1878 for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
1879 ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
1880
1881 return ar;
cca84799
CL
1882
1883err_nomem:
1884 kfree_skb(skb);
1885 return ERR_PTR(-ENOMEM);
e9348cdd 1886}
e9348cdd
CL
1887
1888static int ar9170_read_eeprom(struct ar9170 *ar)
1889{
1890#define RW 8 /* number of words to read at once */
1891#define RB (sizeof(u32) * RW)
1892 DECLARE_MAC_BUF(mbuf);
1893 u8 *eeprom = (void *)&ar->eeprom;
1894 u8 *addr = ar->eeprom.mac_address;
1895 __le32 offsets[RW];
1896 int i, j, err, bands = 0;
1897
1898 BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
1899
1900 BUILD_BUG_ON(RB > AR9170_MAX_CMD_LEN - 4);
1901#ifndef __CHECKER__
1902 /* don't want to handle trailing remains */
1903 BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
1904#endif
1905
1906 for (i = 0; i < sizeof(ar->eeprom)/RB; i++) {
1907 for (j = 0; j < RW; j++)
1908 offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
1909 RB * i + 4 * j);
1910
1911 err = ar->exec_cmd(ar, AR9170_CMD_RREG,
1912 RB, (u8 *) &offsets,
1913 RB, eeprom + RB * i);
1914 if (err)
1915 return err;
1916 }
1917
1918#undef RW
1919#undef RB
1920
1921 if (ar->eeprom.length == cpu_to_le16(0xFFFF))
1922 return -ENODATA;
1923
1924 if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
1925 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &ar9170_band_2GHz;
1926 bands++;
1927 }
1928 if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
1929 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &ar9170_band_5GHz;
1930 bands++;
1931 }
1932 /*
1933 * I measured this, a bandswitch takes roughly
1934 * 135 ms and a frequency switch about 80.
1935 *
1936 * FIXME: measure these values again once EEPROM settings
1937 * are used, that will influence them!
1938 */
1939 if (bands == 2)
1940 ar->hw->channel_change_time = 135 * 1000;
1941 else
1942 ar->hw->channel_change_time = 80 * 1000;
1943
1878f77e
CL
1944 ar->regulatory.current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
1945 ar->regulatory.current_rd_ext = le16_to_cpu(ar->eeprom.reg_domain[1]);
1946
e9348cdd
CL
1947 /* second part of wiphy init */
1948 SET_IEEE80211_PERM_ADDR(ar->hw, addr);
1949
1950 return bands ? 0 : -EINVAL;
1951}
1952
1878f77e
CL
1953static int ar9170_reg_notifier(struct wiphy *wiphy,
1954 struct regulatory_request *request)
1955{
1956 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1957 struct ar9170 *ar = hw->priv;
1958
1959 return ath_reg_notifier_apply(wiphy, request, &ar->regulatory);
1960}
1961
e9348cdd
CL
1962int ar9170_register(struct ar9170 *ar, struct device *pdev)
1963{
1964 int err;
1965
1966 /* try to read EEPROM, init MAC addr */
1967 err = ar9170_read_eeprom(ar);
1968 if (err)
1969 goto err_out;
1970
1878f77e
CL
1971 err = ath_regd_init(&ar->regulatory, ar->hw->wiphy,
1972 ar9170_reg_notifier);
85efc86e
LR
1973 if (err)
1974 goto err_out;
1878f77e 1975
e9348cdd
CL
1976 err = ieee80211_register_hw(ar->hw);
1977 if (err)
1978 goto err_out;
1979
1878f77e
CL
1980 if (!ath_is_world_regd(&ar->regulatory))
1981 regulatory_hint(ar->hw->wiphy, ar->regulatory.alpha2);
1982
e9348cdd
CL
1983 err = ar9170_init_leds(ar);
1984 if (err)
1985 goto err_unreg;
1986
1987#ifdef CONFIG_AR9170_LEDS
1988 err = ar9170_register_leds(ar);
1989 if (err)
1990 goto err_unreg;
1991#endif /* CONFIG_AR9170_LEDS */
1992
1993 dev_info(pdev, "Atheros AR9170 is registered as '%s'\n",
1994 wiphy_name(ar->hw->wiphy));
1995
1996 return err;
1997
1998err_unreg:
1999 ieee80211_unregister_hw(ar->hw);
2000
2001err_out:
2002 return err;
2003}
e9348cdd
CL
2004
2005void ar9170_unregister(struct ar9170 *ar)
2006{
2007#ifdef CONFIG_AR9170_LEDS
2008 ar9170_unregister_leds(ar);
2009#endif /* CONFIG_AR9170_LEDS */
2010
cca84799 2011 kfree_skb(ar->rx_failover);
e9348cdd
CL
2012 ieee80211_unregister_hw(ar->hw);
2013 mutex_destroy(&ar->mutex);
2014}