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