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