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