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