Merge branch 'fixes-jgarzik' of git://git.kernel.org/pub/scm/linux/kernel/git/linvill...
[linux-2.6.git] / drivers / net / wireless / iwlwifi / iwl3945-base.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2007 Intel Corporation. All rights reserved.
4  *
5  * Portions of this file are derived from the ipw3945 project, as well
6  * as portions of the ieee80211 subsystem header files.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of version 2 of the GNU General Public License as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20  *
21  * The full GNU General Public License is included in this distribution in the
22  * file called LICENSE.
23  *
24  * Contact Information:
25  * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  *****************************************************************************/
29
30 /*
31  * NOTE:  This file (iwl-base.c) is used to build to multiple hardware targets
32  * by defining IWL to either 3945 or 4965.  The Makefile used when building
33  * the base targets will create base-3945.o and base-4965.o
34  *
35  * The eventual goal is to move as many of the #if IWL / #endif blocks out of
36  * this file and into the hardware specific implementation files (iwl-XXXX.c)
37  * and leave only the common (non #ifdef sprinkled) code in this file
38  */
39
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/version.h>
43 #include <linux/init.h>
44 #include <linux/pci.h>
45 #include <linux/dma-mapping.h>
46 #include <linux/delay.h>
47 #include <linux/skbuff.h>
48 #include <linux/netdevice.h>
49 #include <linux/wireless.h>
50 #include <linux/firmware.h>
51 #include <linux/etherdevice.h>
52 #include <linux/if_arp.h>
53
54 #include <net/ieee80211_radiotap.h>
55 #include <net/mac80211.h>
56
57 #include <asm/div64.h>
58
59 #define IWL 3945
60
61 #include "iwlwifi.h"
62 #include "iwl-3945.h"
63 #include "iwl-helpers.h"
64
65 #ifdef CONFIG_IWLWIFI_DEBUG
66 u32 iwl_debug_level;
67 #endif
68
69 /******************************************************************************
70  *
71  * module boiler plate
72  *
73  ******************************************************************************/
74
75 /* module parameters */
76 int iwl_param_disable_hw_scan;
77 int iwl_param_debug;
78 int iwl_param_disable;      /* def: enable radio */
79 int iwl_param_antenna;      /* def: 0 = both antennas (use diversity) */
80 int iwl_param_hwcrypto;     /* def: using software encryption */
81 int iwl_param_qos_enable = 1;
82 int iwl_param_queues_num = IWL_MAX_NUM_QUEUES;
83
84 /*
85  * module name, copyright, version, etc.
86  * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
87  */
88
89 #define DRV_DESCRIPTION \
90 "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
91
92 #ifdef CONFIG_IWLWIFI_DEBUG
93 #define VD "d"
94 #else
95 #define VD
96 #endif
97
98 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
99 #define VS "s"
100 #else
101 #define VS
102 #endif
103
104 #define IWLWIFI_VERSION "1.1.17k" VD VS
105 #define DRV_COPYRIGHT   "Copyright(c) 2003-2007 Intel Corporation"
106 #define DRV_VERSION     IWLWIFI_VERSION
107
108 /* Change firmware file name, using "-" and incrementing number,
109  *   *only* when uCode interface or architecture changes so that it
110  *   is not compatible with earlier drivers.
111  * This number will also appear in << 8 position of 1st dword of uCode file */
112 #define IWL3945_UCODE_API "-1"
113
114 MODULE_DESCRIPTION(DRV_DESCRIPTION);
115 MODULE_VERSION(DRV_VERSION);
116 MODULE_AUTHOR(DRV_COPYRIGHT);
117 MODULE_LICENSE("GPL");
118
119 __le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
120 {
121         u16 fc = le16_to_cpu(hdr->frame_control);
122         int hdr_len = ieee80211_get_hdrlen(fc);
123
124         if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
125                 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
126         return NULL;
127 }
128
129 static const struct ieee80211_hw_mode *iwl_get_hw_mode(
130                 struct iwl_priv *priv, int mode)
131 {
132         int i;
133
134         for (i = 0; i < 3; i++)
135                 if (priv->modes[i].mode == mode)
136                         return &priv->modes[i];
137
138         return NULL;
139 }
140
141 static int iwl_is_empty_essid(const char *essid, int essid_len)
142 {
143         /* Single white space is for Linksys APs */
144         if (essid_len == 1 && essid[0] == ' ')
145                 return 1;
146
147         /* Otherwise, if the entire essid is 0, we assume it is hidden */
148         while (essid_len) {
149                 essid_len--;
150                 if (essid[essid_len] != '\0')
151                         return 0;
152         }
153
154         return 1;
155 }
156
157 static const char *iwl_escape_essid(const char *essid, u8 essid_len)
158 {
159         static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
160         const char *s = essid;
161         char *d = escaped;
162
163         if (iwl_is_empty_essid(essid, essid_len)) {
164                 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
165                 return escaped;
166         }
167
168         essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
169         while (essid_len--) {
170                 if (*s == '\0') {
171                         *d++ = '\\';
172                         *d++ = '0';
173                         s++;
174                 } else
175                         *d++ = *s++;
176         }
177         *d = '\0';
178         return escaped;
179 }
180
181 static void iwl_print_hex_dump(int level, void *p, u32 len)
182 {
183 #ifdef CONFIG_IWLWIFI_DEBUG
184         if (!(iwl_debug_level & level))
185                 return;
186
187         print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
188                         p, len, 1);
189 #endif
190 }
191
192 /*************** DMA-QUEUE-GENERAL-FUNCTIONS  *****
193  * DMA services
194  *
195  * Theory of operation
196  *
197  * A queue is a circular buffers with 'Read' and 'Write' pointers.
198  * 2 empty entries always kept in the buffer to protect from overflow.
199  *
200  * For Tx queue, there are low mark and high mark limits. If, after queuing
201  * the packet for Tx, free space become < low mark, Tx queue stopped. When
202  * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
203  * Tx queue resumed.
204  *
205  * The IWL operates with six queues, one receive queue in the device's
206  * sram, one transmit queue for sending commands to the device firmware,
207  * and four transmit queues for data.
208  ***************************************************/
209
210 static int iwl_queue_space(const struct iwl_queue *q)
211 {
212         int s = q->last_used - q->first_empty;
213
214         if (q->last_used > q->first_empty)
215                 s -= q->n_bd;
216
217         if (s <= 0)
218                 s += q->n_window;
219         /* keep some reserve to not confuse empty and full situations */
220         s -= 2;
221         if (s < 0)
222                 s = 0;
223         return s;
224 }
225
226 /* XXX: n_bd must be power-of-two size */
227 static inline int iwl_queue_inc_wrap(int index, int n_bd)
228 {
229         return ++index & (n_bd - 1);
230 }
231
232 /* XXX: n_bd must be power-of-two size */
233 static inline int iwl_queue_dec_wrap(int index, int n_bd)
234 {
235         return --index & (n_bd - 1);
236 }
237
238 static inline int x2_queue_used(const struct iwl_queue *q, int i)
239 {
240         return q->first_empty > q->last_used ?
241                 (i >= q->last_used && i < q->first_empty) :
242                 !(i < q->last_used && i >= q->first_empty);
243 }
244
245 static inline u8 get_cmd_index(struct iwl_queue *q, u32 index, int is_huge)
246 {
247         if (is_huge)
248                 return q->n_window;
249
250         return index & (q->n_window - 1);
251 }
252
253 static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
254                           int count, int slots_num, u32 id)
255 {
256         q->n_bd = count;
257         q->n_window = slots_num;
258         q->id = id;
259
260         /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
261          * and iwl_queue_dec_wrap are broken. */
262         BUG_ON(!is_power_of_2(count));
263
264         /* slots_num must be power-of-two size, otherwise
265          * get_cmd_index is broken. */
266         BUG_ON(!is_power_of_2(slots_num));
267
268         q->low_mark = q->n_window / 4;
269         if (q->low_mark < 4)
270                 q->low_mark = 4;
271
272         q->high_mark = q->n_window / 8;
273         if (q->high_mark < 2)
274                 q->high_mark = 2;
275
276         q->first_empty = q->last_used = 0;
277
278         return 0;
279 }
280
281 static int iwl_tx_queue_alloc(struct iwl_priv *priv,
282                               struct iwl_tx_queue *txq, u32 id)
283 {
284         struct pci_dev *dev = priv->pci_dev;
285
286         if (id != IWL_CMD_QUEUE_NUM) {
287                 txq->txb = kmalloc(sizeof(txq->txb[0]) *
288                                    TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
289                 if (!txq->txb) {
290                         IWL_ERROR("kmalloc for auxilary BD "
291                                   "structures failed\n");
292                         goto error;
293                 }
294         } else
295                 txq->txb = NULL;
296
297         txq->bd = pci_alloc_consistent(dev,
298                         sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
299                         &txq->q.dma_addr);
300
301         if (!txq->bd) {
302                 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
303                           sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
304                 goto error;
305         }
306         txq->q.id = id;
307
308         return 0;
309
310  error:
311         if (txq->txb) {
312                 kfree(txq->txb);
313                 txq->txb = NULL;
314         }
315
316         return -ENOMEM;
317 }
318
319 int iwl_tx_queue_init(struct iwl_priv *priv,
320                       struct iwl_tx_queue *txq, int slots_num, u32 txq_id)
321 {
322         struct pci_dev *dev = priv->pci_dev;
323         int len;
324         int rc = 0;
325
326         /* alocate command space + one big command for scan since scan
327          * command is very huge the system will not have two scan at the
328          * same time */
329         len = sizeof(struct iwl_cmd) * slots_num;
330         if (txq_id == IWL_CMD_QUEUE_NUM)
331                 len +=  IWL_MAX_SCAN_SIZE;
332         txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
333         if (!txq->cmd)
334                 return -ENOMEM;
335
336         rc = iwl_tx_queue_alloc(priv, txq, txq_id);
337         if (rc) {
338                 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
339
340                 return -ENOMEM;
341         }
342         txq->need_update = 0;
343
344         /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
345          * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
346         BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
347         iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
348
349         iwl_hw_tx_queue_init(priv, txq);
350
351         return 0;
352 }
353
354 /**
355  * iwl_tx_queue_free - Deallocate DMA queue.
356  * @txq: Transmit queue to deallocate.
357  *
358  * Empty queue by removing and destroying all BD's.
359  * Free all buffers.  txq itself is not freed.
360  *
361  */
362 void iwl_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
363 {
364         struct iwl_queue *q = &txq->q;
365         struct pci_dev *dev = priv->pci_dev;
366         int len;
367
368         if (q->n_bd == 0)
369                 return;
370
371         /* first, empty all BD's */
372         for (; q->first_empty != q->last_used;
373              q->last_used = iwl_queue_inc_wrap(q->last_used, q->n_bd))
374                 iwl_hw_txq_free_tfd(priv, txq);
375
376         len = sizeof(struct iwl_cmd) * q->n_window;
377         if (q->id == IWL_CMD_QUEUE_NUM)
378                 len += IWL_MAX_SCAN_SIZE;
379
380         pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
381
382         /* free buffers belonging to queue itself */
383         if (txq->q.n_bd)
384                 pci_free_consistent(dev, sizeof(struct iwl_tfd_frame) *
385                                     txq->q.n_bd, txq->bd, txq->q.dma_addr);
386
387         if (txq->txb) {
388                 kfree(txq->txb);
389                 txq->txb = NULL;
390         }
391
392         /* 0 fill whole structure */
393         memset(txq, 0, sizeof(*txq));
394 }
395
396 const u8 BROADCAST_ADDR[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
397
398 /*************** STATION TABLE MANAGEMENT ****
399  *
400  * NOTE:  This needs to be overhauled to better synchronize between
401  * how the iwl-4965.c is using iwl_hw_find_station vs. iwl-3945.c
402  *
403  * mac80211 should also be examined to determine if sta_info is duplicating
404  * the functionality provided here
405  */
406
407 /**************************************************************/
408 #if 0 /* temparary disable till we add real remove station */
409 static u8 iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
410 {
411         int index = IWL_INVALID_STATION;
412         int i;
413         unsigned long flags;
414
415         spin_lock_irqsave(&priv->sta_lock, flags);
416
417         if (is_ap)
418                 index = IWL_AP_ID;
419         else if (is_broadcast_ether_addr(addr))
420                 index = priv->hw_setting.bcast_sta_id;
421         else
422                 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
423                         if (priv->stations[i].used &&
424                             !compare_ether_addr(priv->stations[i].sta.sta.addr,
425                                                 addr)) {
426                                 index = i;
427                                 break;
428                         }
429
430         if (unlikely(index == IWL_INVALID_STATION))
431                 goto out;
432
433         if (priv->stations[index].used) {
434                 priv->stations[index].used = 0;
435                 priv->num_stations--;
436         }
437
438         BUG_ON(priv->num_stations < 0);
439
440 out:
441         spin_unlock_irqrestore(&priv->sta_lock, flags);
442         return 0;
443 }
444 #endif
445 static void iwl_clear_stations_table(struct iwl_priv *priv)
446 {
447         unsigned long flags;
448
449         spin_lock_irqsave(&priv->sta_lock, flags);
450
451         priv->num_stations = 0;
452         memset(priv->stations, 0, sizeof(priv->stations));
453
454         spin_unlock_irqrestore(&priv->sta_lock, flags);
455 }
456
457
458 u8 iwl_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
459 {
460         int i;
461         int index = IWL_INVALID_STATION;
462         struct iwl_station_entry *station;
463         unsigned long flags_spin;
464         DECLARE_MAC_BUF(mac);
465         u8 rate;
466
467         spin_lock_irqsave(&priv->sta_lock, flags_spin);
468         if (is_ap)
469                 index = IWL_AP_ID;
470         else if (is_broadcast_ether_addr(addr))
471                 index = priv->hw_setting.bcast_sta_id;
472         else
473                 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
474                         if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
475                                                 addr)) {
476                                 index = i;
477                                 break;
478                         }
479
480                         if (!priv->stations[i].used &&
481                             index == IWL_INVALID_STATION)
482                                 index = i;
483                 }
484
485         /* These twh conditions has the same outcome but keep them separate
486           since they have different meaning */
487         if (unlikely(index == IWL_INVALID_STATION)) {
488                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
489                 return index;
490         }
491
492         if (priv->stations[index].used &&
493            !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
494                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
495                 return index;
496         }
497
498         IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
499         station = &priv->stations[index];
500         station->used = 1;
501         priv->num_stations++;
502
503         memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
504         memcpy(station->sta.sta.addr, addr, ETH_ALEN);
505         station->sta.mode = 0;
506         station->sta.sta.sta_id = index;
507         station->sta.station_flags = 0;
508
509         rate = (priv->phymode == MODE_IEEE80211A) ? IWL_RATE_6M_PLCP :
510                                 IWL_RATE_1M_PLCP | priv->hw_setting.cck_flag;
511
512         /* Turn on both antennas for the station... */
513         station->sta.rate_n_flags =
514                         iwl_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
515         station->current_rate.rate_n_flags =
516                         le16_to_cpu(station->sta.rate_n_flags);
517
518         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
519         iwl_send_add_station(priv, &station->sta, flags);
520         return index;
521
522 }
523
524 /*************** DRIVER STATUS FUNCTIONS   *****/
525
526 static inline int iwl_is_ready(struct iwl_priv *priv)
527 {
528         /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
529          * set but EXIT_PENDING is not */
530         return test_bit(STATUS_READY, &priv->status) &&
531                test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
532                !test_bit(STATUS_EXIT_PENDING, &priv->status);
533 }
534
535 static inline int iwl_is_alive(struct iwl_priv *priv)
536 {
537         return test_bit(STATUS_ALIVE, &priv->status);
538 }
539
540 static inline int iwl_is_init(struct iwl_priv *priv)
541 {
542         return test_bit(STATUS_INIT, &priv->status);
543 }
544
545 static inline int iwl_is_rfkill(struct iwl_priv *priv)
546 {
547         return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
548                test_bit(STATUS_RF_KILL_SW, &priv->status);
549 }
550
551 static inline int iwl_is_ready_rf(struct iwl_priv *priv)
552 {
553
554         if (iwl_is_rfkill(priv))
555                 return 0;
556
557         return iwl_is_ready(priv);
558 }
559
560 /*************** HOST COMMAND QUEUE FUNCTIONS   *****/
561
562 #define IWL_CMD(x) case x : return #x
563
564 static const char *get_cmd_string(u8 cmd)
565 {
566         switch (cmd) {
567                 IWL_CMD(REPLY_ALIVE);
568                 IWL_CMD(REPLY_ERROR);
569                 IWL_CMD(REPLY_RXON);
570                 IWL_CMD(REPLY_RXON_ASSOC);
571                 IWL_CMD(REPLY_QOS_PARAM);
572                 IWL_CMD(REPLY_RXON_TIMING);
573                 IWL_CMD(REPLY_ADD_STA);
574                 IWL_CMD(REPLY_REMOVE_STA);
575                 IWL_CMD(REPLY_REMOVE_ALL_STA);
576                 IWL_CMD(REPLY_3945_RX);
577                 IWL_CMD(REPLY_TX);
578                 IWL_CMD(REPLY_RATE_SCALE);
579                 IWL_CMD(REPLY_LEDS_CMD);
580                 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
581                 IWL_CMD(RADAR_NOTIFICATION);
582                 IWL_CMD(REPLY_QUIET_CMD);
583                 IWL_CMD(REPLY_CHANNEL_SWITCH);
584                 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
585                 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
586                 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
587                 IWL_CMD(POWER_TABLE_CMD);
588                 IWL_CMD(PM_SLEEP_NOTIFICATION);
589                 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
590                 IWL_CMD(REPLY_SCAN_CMD);
591                 IWL_CMD(REPLY_SCAN_ABORT_CMD);
592                 IWL_CMD(SCAN_START_NOTIFICATION);
593                 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
594                 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
595                 IWL_CMD(BEACON_NOTIFICATION);
596                 IWL_CMD(REPLY_TX_BEACON);
597                 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
598                 IWL_CMD(QUIET_NOTIFICATION);
599                 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
600                 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
601                 IWL_CMD(REPLY_BT_CONFIG);
602                 IWL_CMD(REPLY_STATISTICS_CMD);
603                 IWL_CMD(STATISTICS_NOTIFICATION);
604                 IWL_CMD(REPLY_CARD_STATE_CMD);
605                 IWL_CMD(CARD_STATE_NOTIFICATION);
606                 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
607         default:
608                 return "UNKNOWN";
609
610         }
611 }
612
613 #define HOST_COMPLETE_TIMEOUT (HZ / 2)
614
615 /**
616  * iwl_enqueue_hcmd - enqueue a uCode command
617  * @priv: device private data point
618  * @cmd: a point to the ucode command structure
619  *
620  * The function returns < 0 values to indicate the operation is
621  * failed. On success, it turns the index (> 0) of command in the
622  * command queue.
623  */
624 static int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
625 {
626         struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
627         struct iwl_queue *q = &txq->q;
628         struct iwl_tfd_frame *tfd;
629         u32 *control_flags;
630         struct iwl_cmd *out_cmd;
631         u32 idx;
632         u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
633         dma_addr_t phys_addr;
634         int pad;
635         u16 count;
636         int ret;
637         unsigned long flags;
638
639         /* If any of the command structures end up being larger than
640          * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
641          * we will need to increase the size of the TFD entries */
642         BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
643                !(cmd->meta.flags & CMD_SIZE_HUGE));
644
645         if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
646                 IWL_ERROR("No space for Tx\n");
647                 return -ENOSPC;
648         }
649
650         spin_lock_irqsave(&priv->hcmd_lock, flags);
651
652         tfd = &txq->bd[q->first_empty];
653         memset(tfd, 0, sizeof(*tfd));
654
655         control_flags = (u32 *) tfd;
656
657         idx = get_cmd_index(q, q->first_empty, cmd->meta.flags & CMD_SIZE_HUGE);
658         out_cmd = &txq->cmd[idx];
659
660         out_cmd->hdr.cmd = cmd->id;
661         memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
662         memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
663
664         /* At this point, the out_cmd now has all of the incoming cmd
665          * information */
666
667         out_cmd->hdr.flags = 0;
668         out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
669                         INDEX_TO_SEQ(q->first_empty));
670         if (out_cmd->meta.flags & CMD_SIZE_HUGE)
671                 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
672
673         phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
674                         offsetof(struct iwl_cmd, hdr);
675         iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
676
677         pad = U32_PAD(cmd->len);
678         count = TFD_CTL_COUNT_GET(*control_flags);
679         *control_flags = TFD_CTL_COUNT_SET(count) | TFD_CTL_PAD_SET(pad);
680
681         IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
682                      "%d bytes at %d[%d]:%d\n",
683                      get_cmd_string(out_cmd->hdr.cmd),
684                      out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
685                      fix_size, q->first_empty, idx, IWL_CMD_QUEUE_NUM);
686
687         txq->need_update = 1;
688         q->first_empty = iwl_queue_inc_wrap(q->first_empty, q->n_bd);
689         ret = iwl_tx_queue_update_write_ptr(priv, txq);
690
691         spin_unlock_irqrestore(&priv->hcmd_lock, flags);
692         return ret ? ret : idx;
693 }
694
695 int iwl_send_cmd_async(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
696 {
697         int ret;
698
699         BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
700
701         /* An asynchronous command can not expect an SKB to be set. */
702         BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
703
704         /* An asynchronous command MUST have a callback. */
705         BUG_ON(!cmd->meta.u.callback);
706
707         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
708                 return -EBUSY;
709
710         ret = iwl_enqueue_hcmd(priv, cmd);
711         if (ret < 0) {
712                 IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
713                           get_cmd_string(cmd->id), ret);
714                 return ret;
715         }
716         return 0;
717 }
718
719 int iwl_send_cmd_sync(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
720 {
721         int cmd_idx;
722         int ret;
723         static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
724
725         BUG_ON(cmd->meta.flags & CMD_ASYNC);
726
727          /* A synchronous command can not have a callback set. */
728         BUG_ON(cmd->meta.u.callback != NULL);
729
730         if (atomic_xchg(&entry, 1)) {
731                 IWL_ERROR("Error sending %s: Already sending a host command\n",
732                           get_cmd_string(cmd->id));
733                 return -EBUSY;
734         }
735
736         set_bit(STATUS_HCMD_ACTIVE, &priv->status);
737
738         if (cmd->meta.flags & CMD_WANT_SKB)
739                 cmd->meta.source = &cmd->meta;
740
741         cmd_idx = iwl_enqueue_hcmd(priv, cmd);
742         if (cmd_idx < 0) {
743                 ret = cmd_idx;
744                 IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
745                           get_cmd_string(cmd->id), ret);
746                 goto out;
747         }
748
749         ret = wait_event_interruptible_timeout(priv->wait_command_queue,
750                         !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
751                         HOST_COMPLETE_TIMEOUT);
752         if (!ret) {
753                 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
754                         IWL_ERROR("Error sending %s: time out after %dms.\n",
755                                   get_cmd_string(cmd->id),
756                                   jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
757
758                         clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
759                         ret = -ETIMEDOUT;
760                         goto cancel;
761                 }
762         }
763
764         if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
765                 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
766                                get_cmd_string(cmd->id));
767                 ret = -ECANCELED;
768                 goto fail;
769         }
770         if (test_bit(STATUS_FW_ERROR, &priv->status)) {
771                 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
772                                get_cmd_string(cmd->id));
773                 ret = -EIO;
774                 goto fail;
775         }
776         if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
777                 IWL_ERROR("Error: Response NULL in '%s'\n",
778                           get_cmd_string(cmd->id));
779                 ret = -EIO;
780                 goto out;
781         }
782
783         ret = 0;
784         goto out;
785
786 cancel:
787         if (cmd->meta.flags & CMD_WANT_SKB) {
788                 struct iwl_cmd *qcmd;
789
790                 /* Cancel the CMD_WANT_SKB flag for the cmd in the
791                  * TX cmd queue. Otherwise in case the cmd comes
792                  * in later, it will possibly set an invalid
793                  * address (cmd->meta.source). */
794                 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
795                 qcmd->meta.flags &= ~CMD_WANT_SKB;
796         }
797 fail:
798         if (cmd->meta.u.skb) {
799                 dev_kfree_skb_any(cmd->meta.u.skb);
800                 cmd->meta.u.skb = NULL;
801         }
802 out:
803         atomic_set(&entry, 0);
804         return ret;
805 }
806
807 int iwl_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
808 {
809         /* A command can not be asynchronous AND expect an SKB to be set. */
810         BUG_ON((cmd->meta.flags & CMD_ASYNC) &&
811                (cmd->meta.flags & CMD_WANT_SKB));
812
813         if (cmd->meta.flags & CMD_ASYNC)
814                 return iwl_send_cmd_async(priv, cmd);
815
816         return iwl_send_cmd_sync(priv, cmd);
817 }
818
819 int iwl_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
820 {
821         struct iwl_host_cmd cmd = {
822                 .id = id,
823                 .len = len,
824                 .data = data,
825         };
826
827         return iwl_send_cmd_sync(priv, &cmd);
828 }
829
830 static int __must_check iwl_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
831 {
832         struct iwl_host_cmd cmd = {
833                 .id = id,
834                 .len = sizeof(val),
835                 .data = &val,
836         };
837
838         return iwl_send_cmd_sync(priv, &cmd);
839 }
840
841 int iwl_send_statistics_request(struct iwl_priv *priv)
842 {
843         return iwl_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
844 }
845
846 /**
847  * iwl_set_rxon_channel - Set the phymode and channel values in staging RXON
848  * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
849  * @channel: Any channel valid for the requested phymode
850
851  * In addition to setting the staging RXON, priv->phymode is also set.
852  *
853  * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
854  * in the staging RXON flag structure based on the phymode
855  */
856 static int iwl_set_rxon_channel(struct iwl_priv *priv, u8 phymode, u16 channel)
857 {
858         if (!iwl_get_channel_info(priv, phymode, channel)) {
859                 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
860                                channel, phymode);
861                 return -EINVAL;
862         }
863
864         if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
865             (priv->phymode == phymode))
866                 return 0;
867
868         priv->staging_rxon.channel = cpu_to_le16(channel);
869         if (phymode == MODE_IEEE80211A)
870                 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
871         else
872                 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
873
874         priv->phymode = phymode;
875
876         IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
877
878         return 0;
879 }
880
881 /**
882  * iwl_check_rxon_cmd - validate RXON structure is valid
883  *
884  * NOTE:  This is really only useful during development and can eventually
885  * be #ifdef'd out once the driver is stable and folks aren't actively
886  * making changes
887  */
888 static int iwl_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
889 {
890         int error = 0;
891         int counter = 1;
892
893         if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
894                 error |= le32_to_cpu(rxon->flags &
895                                 (RXON_FLG_TGJ_NARROW_BAND_MSK |
896                                  RXON_FLG_RADAR_DETECT_MSK));
897                 if (error)
898                         IWL_WARNING("check 24G fields %d | %d\n",
899                                     counter++, error);
900         } else {
901                 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
902                                 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
903                 if (error)
904                         IWL_WARNING("check 52 fields %d | %d\n",
905                                     counter++, error);
906                 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
907                 if (error)
908                         IWL_WARNING("check 52 CCK %d | %d\n",
909                                     counter++, error);
910         }
911         error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
912         if (error)
913                 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
914
915         /* make sure basic rates 6Mbps and 1Mbps are supported */
916         error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
917                   ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
918         if (error)
919                 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
920
921         error |= (le16_to_cpu(rxon->assoc_id) > 2007);
922         if (error)
923                 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
924
925         error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
926                         == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
927         if (error)
928                 IWL_WARNING("check CCK and short slot %d | %d\n",
929                             counter++, error);
930
931         error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
932                         == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
933         if (error)
934                 IWL_WARNING("check CCK & auto detect %d | %d\n",
935                             counter++, error);
936
937         error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
938                         RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
939         if (error)
940                 IWL_WARNING("check TGG and auto detect %d | %d\n",
941                             counter++, error);
942
943         if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
944                 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
945                                 RXON_FLG_ANT_A_MSK)) == 0);
946         if (error)
947                 IWL_WARNING("check antenna %d %d\n", counter++, error);
948
949         if (error)
950                 IWL_WARNING("Tuning to channel %d\n",
951                             le16_to_cpu(rxon->channel));
952
953         if (error) {
954                 IWL_ERROR("Not a valid iwl_rxon_assoc_cmd field values\n");
955                 return -1;
956         }
957         return 0;
958 }
959
960 /**
961  * iwl_full_rxon_required - determine if RXON_ASSOC can be used in RXON commit
962  * @priv: staging_rxon is comapred to active_rxon
963  *
964  * If the RXON structure is changing sufficient to require a new
965  * tune or to clear and reset the RXON_FILTER_ASSOC_MSK then return 1
966  * to indicate a new tune is required.
967  */
968 static int iwl_full_rxon_required(struct iwl_priv *priv)
969 {
970
971         /* These items are only settable from the full RXON command */
972         if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
973             compare_ether_addr(priv->staging_rxon.bssid_addr,
974                                priv->active_rxon.bssid_addr) ||
975             compare_ether_addr(priv->staging_rxon.node_addr,
976                                priv->active_rxon.node_addr) ||
977             compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
978                                priv->active_rxon.wlap_bssid_addr) ||
979             (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
980             (priv->staging_rxon.channel != priv->active_rxon.channel) ||
981             (priv->staging_rxon.air_propagation !=
982              priv->active_rxon.air_propagation) ||
983             (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
984                 return 1;
985
986         /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
987          * be updated with the RXON_ASSOC command -- however only some
988          * flag transitions are allowed using RXON_ASSOC */
989
990         /* Check if we are not switching bands */
991         if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
992             (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
993                 return 1;
994
995         /* Check if we are switching association toggle */
996         if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
997                 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
998                 return 1;
999
1000         return 0;
1001 }
1002
1003 static int iwl_send_rxon_assoc(struct iwl_priv *priv)
1004 {
1005         int rc = 0;
1006         struct iwl_rx_packet *res = NULL;
1007         struct iwl_rxon_assoc_cmd rxon_assoc;
1008         struct iwl_host_cmd cmd = {
1009                 .id = REPLY_RXON_ASSOC,
1010                 .len = sizeof(rxon_assoc),
1011                 .meta.flags = CMD_WANT_SKB,
1012                 .data = &rxon_assoc,
1013         };
1014         const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
1015         const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
1016
1017         if ((rxon1->flags == rxon2->flags) &&
1018             (rxon1->filter_flags == rxon2->filter_flags) &&
1019             (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1020             (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1021                 IWL_DEBUG_INFO("Using current RXON_ASSOC.  Not resending.\n");
1022                 return 0;
1023         }
1024
1025         rxon_assoc.flags = priv->staging_rxon.flags;
1026         rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1027         rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1028         rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1029         rxon_assoc.reserved = 0;
1030
1031         rc = iwl_send_cmd_sync(priv, &cmd);
1032         if (rc)
1033                 return rc;
1034
1035         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1036         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1037                 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1038                 rc = -EIO;
1039         }
1040
1041         priv->alloc_rxb_skb--;
1042         dev_kfree_skb_any(cmd.meta.u.skb);
1043
1044         return rc;
1045 }
1046
1047 /**
1048  * iwl_commit_rxon - commit staging_rxon to hardware
1049  *
1050  * The RXON command in staging_rxon is commited to the hardware and
1051  * the active_rxon structure is updated with the new data.  This
1052  * function correctly transitions out of the RXON_ASSOC_MSK state if
1053  * a HW tune is required based on the RXON structure changes.
1054  */
1055 static int iwl_commit_rxon(struct iwl_priv *priv)
1056 {
1057         /* cast away the const for active_rxon in this function */
1058         struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
1059         int rc = 0;
1060         DECLARE_MAC_BUF(mac);
1061
1062         if (!iwl_is_alive(priv))
1063                 return -1;
1064
1065         /* always get timestamp with Rx frame */
1066         priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1067
1068         /* select antenna */
1069         priv->staging_rxon.flags &=
1070             ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
1071         priv->staging_rxon.flags |= iwl3945_get_antenna_flags(priv);
1072
1073         rc = iwl_check_rxon_cmd(&priv->staging_rxon);
1074         if (rc) {
1075                 IWL_ERROR("Invalid RXON configuration.  Not committing.\n");
1076                 return -EINVAL;
1077         }
1078
1079         /* If we don't need to send a full RXON, we can use
1080          * iwl_rxon_assoc_cmd which is used to reconfigure filter
1081          * and other flags for the current radio configuration. */
1082         if (!iwl_full_rxon_required(priv)) {
1083                 rc = iwl_send_rxon_assoc(priv);
1084                 if (rc) {
1085                         IWL_ERROR("Error setting RXON_ASSOC "
1086                                   "configuration (%d).\n", rc);
1087                         return rc;
1088                 }
1089
1090                 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1091
1092                 return 0;
1093         }
1094
1095         /* If we are currently associated and the new config requires
1096          * an RXON_ASSOC and the new config wants the associated mask enabled,
1097          * we must clear the associated from the active configuration
1098          * before we apply the new config */
1099         if (iwl_is_associated(priv) &&
1100             (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1101                 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1102                 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1103
1104                 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
1105                                       sizeof(struct iwl_rxon_cmd),
1106                                       &priv->active_rxon);
1107
1108                 /* If the mask clearing failed then we set
1109                  * active_rxon back to what it was previously */
1110                 if (rc) {
1111                         active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1112                         IWL_ERROR("Error clearing ASSOC_MSK on current "
1113                                   "configuration (%d).\n", rc);
1114                         return rc;
1115                 }
1116         }
1117
1118         IWL_DEBUG_INFO("Sending RXON\n"
1119                        "* with%s RXON_FILTER_ASSOC_MSK\n"
1120                        "* channel = %d\n"
1121                        "* bssid = %s\n",
1122                        ((priv->staging_rxon.filter_flags &
1123                          RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1124                        le16_to_cpu(priv->staging_rxon.channel),
1125                        print_mac(mac, priv->staging_rxon.bssid_addr));
1126
1127         /* Apply the new configuration */
1128         rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
1129                               sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
1130         if (rc) {
1131                 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1132                 return rc;
1133         }
1134
1135         memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1136
1137         iwl_clear_stations_table(priv);
1138
1139         /* If we issue a new RXON command which required a tune then we must
1140          * send a new TXPOWER command or we won't be able to Tx any frames */
1141         rc = iwl_hw_reg_send_txpower(priv);
1142         if (rc) {
1143                 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1144                 return rc;
1145         }
1146
1147         /* Add the broadcast address so we can send broadcast frames */
1148         if (iwl_add_station(priv, BROADCAST_ADDR, 0, 0) ==
1149             IWL_INVALID_STATION) {
1150                 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1151                 return -EIO;
1152         }
1153
1154         /* If we have set the ASSOC_MSK and we are in BSS mode then
1155          * add the IWL_AP_ID to the station rate table */
1156         if (iwl_is_associated(priv) &&
1157             (priv->iw_mode == IEEE80211_IF_TYPE_STA))
1158                 if (iwl_add_station(priv, priv->active_rxon.bssid_addr, 1, 0)
1159                     == IWL_INVALID_STATION) {
1160                         IWL_ERROR("Error adding AP address for transmit.\n");
1161                         return -EIO;
1162                 }
1163
1164         /* Init the hardware's rate fallback order based on the
1165          * phymode */
1166         rc = iwl3945_init_hw_rate_table(priv);
1167         if (rc) {
1168                 IWL_ERROR("Error setting HW rate table: %02X\n", rc);
1169                 return -EIO;
1170         }
1171
1172         return 0;
1173 }
1174
1175 static int iwl_send_bt_config(struct iwl_priv *priv)
1176 {
1177         struct iwl_bt_cmd bt_cmd = {
1178                 .flags = 3,
1179                 .lead_time = 0xAA,
1180                 .max_kill = 1,
1181                 .kill_ack_mask = 0,
1182                 .kill_cts_mask = 0,
1183         };
1184
1185         return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1186                                 sizeof(struct iwl_bt_cmd), &bt_cmd);
1187 }
1188
1189 static int iwl_send_scan_abort(struct iwl_priv *priv)
1190 {
1191         int rc = 0;
1192         struct iwl_rx_packet *res;
1193         struct iwl_host_cmd cmd = {
1194                 .id = REPLY_SCAN_ABORT_CMD,
1195                 .meta.flags = CMD_WANT_SKB,
1196         };
1197
1198         /* If there isn't a scan actively going on in the hardware
1199          * then we are in between scan bands and not actually
1200          * actively scanning, so don't send the abort command */
1201         if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1202                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1203                 return 0;
1204         }
1205
1206         rc = iwl_send_cmd_sync(priv, &cmd);
1207         if (rc) {
1208                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1209                 return rc;
1210         }
1211
1212         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1213         if (res->u.status != CAN_ABORT_STATUS) {
1214                 /* The scan abort will return 1 for success or
1215                  * 2 for "failure".  A failure condition can be
1216                  * due to simply not being in an active scan which
1217                  * can occur if we send the scan abort before we
1218                  * the microcode has notified us that a scan is
1219                  * completed. */
1220                 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1221                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1222                 clear_bit(STATUS_SCAN_HW, &priv->status);
1223         }
1224
1225         dev_kfree_skb_any(cmd.meta.u.skb);
1226
1227         return rc;
1228 }
1229
1230 static int iwl_card_state_sync_callback(struct iwl_priv *priv,
1231                                         struct iwl_cmd *cmd,
1232                                         struct sk_buff *skb)
1233 {
1234         return 1;
1235 }
1236
1237 /*
1238  * CARD_STATE_CMD
1239  *
1240  * Use: Sets the internal card state to enable, disable, or halt
1241  *
1242  * When in the 'enable' state the card operates as normal.
1243  * When in the 'disable' state, the card enters into a low power mode.
1244  * When in the 'halt' state, the card is shut down and must be fully
1245  * restarted to come back on.
1246  */
1247 static int iwl_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
1248 {
1249         struct iwl_host_cmd cmd = {
1250                 .id = REPLY_CARD_STATE_CMD,
1251                 .len = sizeof(u32),
1252                 .data = &flags,
1253                 .meta.flags = meta_flag,
1254         };
1255
1256         if (meta_flag & CMD_ASYNC)
1257                 cmd.meta.u.callback = iwl_card_state_sync_callback;
1258
1259         return iwl_send_cmd(priv, &cmd);
1260 }
1261
1262 static int iwl_add_sta_sync_callback(struct iwl_priv *priv,
1263                                      struct iwl_cmd *cmd, struct sk_buff *skb)
1264 {
1265         struct iwl_rx_packet *res = NULL;
1266
1267         if (!skb) {
1268                 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1269                 return 1;
1270         }
1271
1272         res = (struct iwl_rx_packet *)skb->data;
1273         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1274                 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1275                           res->hdr.flags);
1276                 return 1;
1277         }
1278
1279         switch (res->u.add_sta.status) {
1280         case ADD_STA_SUCCESS_MSK:
1281                 break;
1282         default:
1283                 break;
1284         }
1285
1286         /* We didn't cache the SKB; let the caller free it */
1287         return 1;
1288 }
1289
1290 int iwl_send_add_station(struct iwl_priv *priv,
1291                          struct iwl_addsta_cmd *sta, u8 flags)
1292 {
1293         struct iwl_rx_packet *res = NULL;
1294         int rc = 0;
1295         struct iwl_host_cmd cmd = {
1296                 .id = REPLY_ADD_STA,
1297                 .len = sizeof(struct iwl_addsta_cmd),
1298                 .meta.flags = flags,
1299                 .data = sta,
1300         };
1301
1302         if (flags & CMD_ASYNC)
1303                 cmd.meta.u.callback = iwl_add_sta_sync_callback;
1304         else
1305                 cmd.meta.flags |= CMD_WANT_SKB;
1306
1307         rc = iwl_send_cmd(priv, &cmd);
1308
1309         if (rc || (flags & CMD_ASYNC))
1310                 return rc;
1311
1312         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1313         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1314                 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1315                           res->hdr.flags);
1316                 rc = -EIO;
1317         }
1318
1319         if (rc == 0) {
1320                 switch (res->u.add_sta.status) {
1321                 case ADD_STA_SUCCESS_MSK:
1322                         IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1323                         break;
1324                 default:
1325                         rc = -EIO;
1326                         IWL_WARNING("REPLY_ADD_STA failed\n");
1327                         break;
1328                 }
1329         }
1330
1331         priv->alloc_rxb_skb--;
1332         dev_kfree_skb_any(cmd.meta.u.skb);
1333
1334         return rc;
1335 }
1336
1337 static int iwl_update_sta_key_info(struct iwl_priv *priv,
1338                                    struct ieee80211_key_conf *keyconf,
1339                                    u8 sta_id)
1340 {
1341         unsigned long flags;
1342         __le16 key_flags = 0;
1343
1344         switch (keyconf->alg) {
1345         case ALG_CCMP:
1346                 key_flags |= STA_KEY_FLG_CCMP;
1347                 key_flags |= cpu_to_le16(
1348                                 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1349                 key_flags &= ~STA_KEY_FLG_INVALID;
1350                 break;
1351         case ALG_TKIP:
1352         case ALG_WEP:
1353                 return -EINVAL;
1354         default:
1355                 return -EINVAL;
1356         }
1357         spin_lock_irqsave(&priv->sta_lock, flags);
1358         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1359         priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1360         memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1361                keyconf->keylen);
1362
1363         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1364                keyconf->keylen);
1365         priv->stations[sta_id].sta.key.key_flags = key_flags;
1366         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1367         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1368
1369         spin_unlock_irqrestore(&priv->sta_lock, flags);
1370
1371         IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
1372         iwl_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1373         return 0;
1374 }
1375
1376 static int iwl_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
1377 {
1378         unsigned long flags;
1379
1380         spin_lock_irqsave(&priv->sta_lock, flags);
1381         memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
1382         memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl_keyinfo));
1383         priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1384         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1385         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1386         spin_unlock_irqrestore(&priv->sta_lock, flags);
1387
1388         IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
1389         iwl_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1390         return 0;
1391 }
1392
1393 static void iwl_clear_free_frames(struct iwl_priv *priv)
1394 {
1395         struct list_head *element;
1396
1397         IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1398                        priv->frames_count);
1399
1400         while (!list_empty(&priv->free_frames)) {
1401                 element = priv->free_frames.next;
1402                 list_del(element);
1403                 kfree(list_entry(element, struct iwl_frame, list));
1404                 priv->frames_count--;
1405         }
1406
1407         if (priv->frames_count) {
1408                 IWL_WARNING("%d frames still in use.  Did we lose one?\n",
1409                             priv->frames_count);
1410                 priv->frames_count = 0;
1411         }
1412 }
1413
1414 static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
1415 {
1416         struct iwl_frame *frame;
1417         struct list_head *element;
1418         if (list_empty(&priv->free_frames)) {
1419                 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1420                 if (!frame) {
1421                         IWL_ERROR("Could not allocate frame!\n");
1422                         return NULL;
1423                 }
1424
1425                 priv->frames_count++;
1426                 return frame;
1427         }
1428
1429         element = priv->free_frames.next;
1430         list_del(element);
1431         return list_entry(element, struct iwl_frame, list);
1432 }
1433
1434 static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
1435 {
1436         memset(frame, 0, sizeof(*frame));
1437         list_add(&frame->list, &priv->free_frames);
1438 }
1439
1440 unsigned int iwl_fill_beacon_frame(struct iwl_priv *priv,
1441                                 struct ieee80211_hdr *hdr,
1442                                 const u8 *dest, int left)
1443 {
1444
1445         if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
1446             ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1447              (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1448                 return 0;
1449
1450         if (priv->ibss_beacon->len > left)
1451                 return 0;
1452
1453         memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1454
1455         return priv->ibss_beacon->len;
1456 }
1457
1458 static int iwl_rate_index_from_plcp(int plcp)
1459 {
1460         int i = 0;
1461
1462         for (i = 0; i < IWL_RATE_COUNT; i++)
1463                 if (iwl_rates[i].plcp == plcp)
1464                         return i;
1465         return -1;
1466 }
1467
1468 static u8 iwl_rate_get_lowest_plcp(int rate_mask)
1469 {
1470         u8 i;
1471
1472         for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
1473              i = iwl_rates[i].next_ieee) {
1474                 if (rate_mask & (1 << i))
1475                         return iwl_rates[i].plcp;
1476         }
1477
1478         return IWL_RATE_INVALID;
1479 }
1480
1481 static int iwl_send_beacon_cmd(struct iwl_priv *priv)
1482 {
1483         struct iwl_frame *frame;
1484         unsigned int frame_size;
1485         int rc;
1486         u8 rate;
1487
1488         frame = iwl_get_free_frame(priv);
1489
1490         if (!frame) {
1491                 IWL_ERROR("Could not obtain free frame buffer for beacon "
1492                           "command.\n");
1493                 return -ENOMEM;
1494         }
1495
1496         if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
1497                 rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic &
1498                                                 0xFF0);
1499                 if (rate == IWL_INVALID_RATE)
1500                         rate = IWL_RATE_6M_PLCP;
1501         } else {
1502                 rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
1503                 if (rate == IWL_INVALID_RATE)
1504                         rate = IWL_RATE_1M_PLCP;
1505         }
1506
1507         frame_size = iwl_hw_get_beacon_cmd(priv, frame, rate);
1508
1509         rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
1510                               &frame->u.cmd[0]);
1511
1512         iwl_free_frame(priv, frame);
1513
1514         return rc;
1515 }
1516
1517 /******************************************************************************
1518  *
1519  * EEPROM related functions
1520  *
1521  ******************************************************************************/
1522
1523 static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
1524 {
1525         memcpy(mac, priv->eeprom.mac_address, 6);
1526 }
1527
1528 /**
1529  * iwl_eeprom_init - read EEPROM contents
1530  *
1531  * Load the EEPROM from adapter into priv->eeprom
1532  *
1533  * NOTE:  This routine uses the non-debug IO access functions.
1534  */
1535 int iwl_eeprom_init(struct iwl_priv *priv)
1536 {
1537         u16 *e = (u16 *)&priv->eeprom;
1538         u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
1539         u32 r;
1540         int sz = sizeof(priv->eeprom);
1541         int rc;
1542         int i;
1543         u16 addr;
1544
1545         /* The EEPROM structure has several padding buffers within it
1546          * and when adding new EEPROM maps is subject to programmer errors
1547          * which may be very difficult to identify without explicitly
1548          * checking the resulting size of the eeprom map. */
1549         BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1550
1551         if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1552                 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1553                 return -ENOENT;
1554         }
1555
1556         rc = iwl_eeprom_aqcuire_semaphore(priv);
1557         if (rc < 0) {
1558                 IWL_ERROR("Failed to aqcuire EEPROM semaphore.\n");
1559                 return -ENOENT;
1560         }
1561
1562         /* eeprom is an array of 16bit values */
1563         for (addr = 0; addr < sz; addr += sizeof(u16)) {
1564                 _iwl_write32(priv, CSR_EEPROM_REG, addr << 1);
1565                 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1566
1567                 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1568                                         i += IWL_EEPROM_ACCESS_DELAY) {
1569                         r = _iwl_read_restricted(priv, CSR_EEPROM_REG);
1570                         if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1571                                 break;
1572                         udelay(IWL_EEPROM_ACCESS_DELAY);
1573                 }
1574
1575                 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1576                         IWL_ERROR("Time out reading EEPROM[%d]", addr);
1577                         return -ETIMEDOUT;
1578                 }
1579                 e[addr / 2] = le16_to_cpu(r >> 16);
1580         }
1581
1582         return 0;
1583 }
1584
1585 /******************************************************************************
1586  *
1587  * Misc. internal state and helper functions
1588  *
1589  ******************************************************************************/
1590 #ifdef CONFIG_IWLWIFI_DEBUG
1591
1592 /**
1593  * iwl_report_frame - dump frame to syslog during debug sessions
1594  *
1595  * hack this function to show different aspects of received frames,
1596  * including selective frame dumps.
1597  * group100 parameter selects whether to show 1 out of 100 good frames.
1598  *
1599  * TODO:  ieee80211_hdr stuff is common to 3945 and 4965, so frame type
1600  *        info output is okay, but some of this stuff (e.g. iwl_rx_frame_stats)
1601  *        is 3945-specific and gives bad output for 4965.  Need to split the
1602  *        functionality, keep common stuff here.
1603  */
1604 void iwl_report_frame(struct iwl_priv *priv,
1605                       struct iwl_rx_packet *pkt,
1606                       struct ieee80211_hdr *header, int group100)
1607 {
1608         u32 to_us;
1609         u32 print_summary = 0;
1610         u32 print_dump = 0;     /* set to 1 to dump all frames' contents */
1611         u32 hundred = 0;
1612         u32 dataframe = 0;
1613         u16 fc;
1614         u16 seq_ctl;
1615         u16 channel;
1616         u16 phy_flags;
1617         int rate_sym;
1618         u16 length;
1619         u16 status;
1620         u16 bcn_tmr;
1621         u32 tsf_low;
1622         u64 tsf;
1623         u8 rssi;
1624         u8 agc;
1625         u16 sig_avg;
1626         u16 noise_diff;
1627         struct iwl_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
1628         struct iwl_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
1629         struct iwl_rx_frame_end *rx_end = IWL_RX_END(pkt);
1630         u8 *data = IWL_RX_DATA(pkt);
1631
1632         /* MAC header */
1633         fc = le16_to_cpu(header->frame_control);
1634         seq_ctl = le16_to_cpu(header->seq_ctrl);
1635
1636         /* metadata */
1637         channel = le16_to_cpu(rx_hdr->channel);
1638         phy_flags = le16_to_cpu(rx_hdr->phy_flags);
1639         rate_sym = rx_hdr->rate;
1640         length = le16_to_cpu(rx_hdr->len);
1641
1642         /* end-of-frame status and timestamp */
1643         status = le32_to_cpu(rx_end->status);
1644         bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
1645         tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
1646         tsf = le64_to_cpu(rx_end->timestamp);
1647
1648         /* signal statistics */
1649         rssi = rx_stats->rssi;
1650         agc = rx_stats->agc;
1651         sig_avg = le16_to_cpu(rx_stats->sig_avg);
1652         noise_diff = le16_to_cpu(rx_stats->noise_diff);
1653
1654         to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
1655
1656         /* if data frame is to us and all is good,
1657          *   (optionally) print summary for only 1 out of every 100 */
1658         if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
1659             (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
1660                 dataframe = 1;
1661                 if (!group100)
1662                         print_summary = 1;      /* print each frame */
1663                 else if (priv->framecnt_to_us < 100) {
1664                         priv->framecnt_to_us++;
1665                         print_summary = 0;
1666                 } else {
1667                         priv->framecnt_to_us = 0;
1668                         print_summary = 1;
1669                         hundred = 1;
1670                 }
1671         } else {
1672                 /* print summary for all other frames */
1673                 print_summary = 1;
1674         }
1675
1676         if (print_summary) {
1677                 char *title;
1678                 u32 rate;
1679
1680                 if (hundred)
1681                         title = "100Frames";
1682                 else if (fc & IEEE80211_FCTL_RETRY)
1683                         title = "Retry";
1684                 else if (ieee80211_is_assoc_response(fc))
1685                         title = "AscRsp";
1686                 else if (ieee80211_is_reassoc_response(fc))
1687                         title = "RasRsp";
1688                 else if (ieee80211_is_probe_response(fc)) {
1689                         title = "PrbRsp";
1690                         print_dump = 1; /* dump frame contents */
1691                 } else if (ieee80211_is_beacon(fc)) {
1692                         title = "Beacon";
1693                         print_dump = 1; /* dump frame contents */
1694                 } else if (ieee80211_is_atim(fc))
1695                         title = "ATIM";
1696                 else if (ieee80211_is_auth(fc))
1697                         title = "Auth";
1698                 else if (ieee80211_is_deauth(fc))
1699                         title = "DeAuth";
1700                 else if (ieee80211_is_disassoc(fc))
1701                         title = "DisAssoc";
1702                 else
1703                         title = "Frame";
1704
1705                 rate = iwl_rate_index_from_plcp(rate_sym);
1706                 if (rate == -1)
1707                         rate = 0;
1708                 else
1709                         rate = iwl_rates[rate].ieee / 2;
1710
1711                 /* print frame summary.
1712                  * MAC addresses show just the last byte (for brevity),
1713                  *    but you can hack it to show more, if you'd like to. */
1714                 if (dataframe)
1715                         IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
1716                                      "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
1717                                      title, fc, header->addr1[5],
1718                                      length, rssi, channel, rate);
1719                 else {
1720                         /* src/dst addresses assume managed mode */
1721                         IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
1722                                      "src=0x%02x, rssi=%u, tim=%lu usec, "
1723                                      "phy=0x%02x, chnl=%d\n",
1724                                      title, fc, header->addr1[5],
1725                                      header->addr3[5], rssi,
1726                                      tsf_low - priv->scan_start_tsf,
1727                                      phy_flags, channel);
1728                 }
1729         }
1730         if (print_dump)
1731                 iwl_print_hex_dump(IWL_DL_RX, data, length);
1732 }
1733 #endif
1734
1735 static void iwl_unset_hw_setting(struct iwl_priv *priv)
1736 {
1737         if (priv->hw_setting.shared_virt)
1738                 pci_free_consistent(priv->pci_dev,
1739                                     sizeof(struct iwl_shared),
1740                                     priv->hw_setting.shared_virt,
1741                                     priv->hw_setting.shared_phys);
1742 }
1743
1744 /**
1745  * iwl_supported_rate_to_ie - fill in the supported rate in IE field
1746  *
1747  * return : set the bit for each supported rate insert in ie
1748  */
1749 static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate,
1750                                     u16 basic_rate, int *left)
1751 {
1752         u16 ret_rates = 0, bit;
1753         int i;
1754         u8 *cnt = ie;
1755         u8 *rates = ie + 1;
1756
1757         for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1758                 if (bit & supported_rate) {
1759                         ret_rates |= bit;
1760                         rates[*cnt] = iwl_rates[i].ieee |
1761                                 ((bit & basic_rate) ? 0x80 : 0x00);
1762                         (*cnt)++;
1763                         (*left)--;
1764                         if ((*left <= 0) ||
1765                             (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
1766                                 break;
1767                 }
1768         }
1769
1770         return ret_rates;
1771 }
1772
1773 /**
1774  * iwl_fill_probe_req - fill in all required fields and IE for probe request
1775  */
1776 static u16 iwl_fill_probe_req(struct iwl_priv *priv,
1777                               struct ieee80211_mgmt *frame,
1778                               int left, int is_direct)
1779 {
1780         int len = 0;
1781         u8 *pos = NULL;
1782         u16 active_rates, ret_rates, cck_rates;
1783
1784         /* Make sure there is enough space for the probe request,
1785          * two mandatory IEs and the data */
1786         left -= 24;
1787         if (left < 0)
1788                 return 0;
1789         len += 24;
1790
1791         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1792         memcpy(frame->da, BROADCAST_ADDR, ETH_ALEN);
1793         memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
1794         memcpy(frame->bssid, BROADCAST_ADDR, ETH_ALEN);
1795         frame->seq_ctrl = 0;
1796
1797         /* fill in our indirect SSID IE */
1798         /* ...next IE... */
1799
1800         left -= 2;
1801         if (left < 0)
1802                 return 0;
1803         len += 2;
1804         pos = &(frame->u.probe_req.variable[0]);
1805         *pos++ = WLAN_EID_SSID;
1806         *pos++ = 0;
1807
1808         /* fill in our direct SSID IE... */
1809         if (is_direct) {
1810                 /* ...next IE... */
1811                 left -= 2 + priv->essid_len;
1812                 if (left < 0)
1813                         return 0;
1814                 /* ... fill it in... */
1815                 *pos++ = WLAN_EID_SSID;
1816                 *pos++ = priv->essid_len;
1817                 memcpy(pos, priv->essid, priv->essid_len);
1818                 pos += priv->essid_len;
1819                 len += 2 + priv->essid_len;
1820         }
1821
1822         /* fill in supported rate */
1823         /* ...next IE... */
1824         left -= 2;
1825         if (left < 0)
1826                 return 0;
1827
1828         /* ... fill it in... */
1829         *pos++ = WLAN_EID_SUPP_RATES;
1830         *pos = 0;
1831
1832         priv->active_rate = priv->rates_mask;
1833         active_rates = priv->active_rate;
1834         priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1835
1836         cck_rates = IWL_CCK_RATES_MASK & active_rates;
1837         ret_rates = iwl_supported_rate_to_ie(pos, cck_rates,
1838                         priv->active_rate_basic, &left);
1839         active_rates &= ~ret_rates;
1840
1841         ret_rates = iwl_supported_rate_to_ie(pos, active_rates,
1842                                  priv->active_rate_basic, &left);
1843         active_rates &= ~ret_rates;
1844
1845         len += 2 + *pos;
1846         pos += (*pos) + 1;
1847         if (active_rates == 0)
1848                 goto fill_end;
1849
1850         /* fill in supported extended rate */
1851         /* ...next IE... */
1852         left -= 2;
1853         if (left < 0)
1854                 return 0;
1855         /* ... fill it in... */
1856         *pos++ = WLAN_EID_EXT_SUPP_RATES;
1857         *pos = 0;
1858         iwl_supported_rate_to_ie(pos, active_rates,
1859                                  priv->active_rate_basic, &left);
1860         if (*pos > 0)
1861                 len += 2 + *pos;
1862
1863  fill_end:
1864         return (u16)len;
1865 }
1866
1867 /*
1868  * QoS  support
1869 */
1870 #ifdef CONFIG_IWLWIFI_QOS
1871 static int iwl_send_qos_params_command(struct iwl_priv *priv,
1872                                        struct iwl_qosparam_cmd *qos)
1873 {
1874
1875         return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1876                                 sizeof(struct iwl_qosparam_cmd), qos);
1877 }
1878
1879 static void iwl_reset_qos(struct iwl_priv *priv)
1880 {
1881         u16 cw_min = 15;
1882         u16 cw_max = 1023;
1883         u8 aifs = 2;
1884         u8 is_legacy = 0;
1885         unsigned long flags;
1886         int i;
1887
1888         spin_lock_irqsave(&priv->lock, flags);
1889         priv->qos_data.qos_active = 0;
1890
1891         if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
1892                 if (priv->qos_data.qos_enable)
1893                         priv->qos_data.qos_active = 1;
1894                 if (!(priv->active_rate & 0xfff0)) {
1895                         cw_min = 31;
1896                         is_legacy = 1;
1897                 }
1898         } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1899                 if (priv->qos_data.qos_enable)
1900                         priv->qos_data.qos_active = 1;
1901         } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
1902                 cw_min = 31;
1903                 is_legacy = 1;
1904         }
1905
1906         if (priv->qos_data.qos_active)
1907                 aifs = 3;
1908
1909         priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1910         priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1911         priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1912         priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1913         priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1914
1915         if (priv->qos_data.qos_active) {
1916                 i = 1;
1917                 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1918                 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1919                 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1920                 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1921                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1922
1923                 i = 2;
1924                 priv->qos_data.def_qos_parm.ac[i].cw_min =
1925                         cpu_to_le16((cw_min + 1) / 2 - 1);
1926                 priv->qos_data.def_qos_parm.ac[i].cw_max =
1927                         cpu_to_le16(cw_max);
1928                 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1929                 if (is_legacy)
1930                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
1931                                 cpu_to_le16(6016);
1932                 else
1933                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
1934                                 cpu_to_le16(3008);
1935                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1936
1937                 i = 3;
1938                 priv->qos_data.def_qos_parm.ac[i].cw_min =
1939                         cpu_to_le16((cw_min + 1) / 4 - 1);
1940                 priv->qos_data.def_qos_parm.ac[i].cw_max =
1941                         cpu_to_le16((cw_max + 1) / 2 - 1);
1942                 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1943                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1944                 if (is_legacy)
1945                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
1946                                 cpu_to_le16(3264);
1947                 else
1948                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
1949                                 cpu_to_le16(1504);
1950         } else {
1951                 for (i = 1; i < 4; i++) {
1952                         priv->qos_data.def_qos_parm.ac[i].cw_min =
1953                                 cpu_to_le16(cw_min);
1954                         priv->qos_data.def_qos_parm.ac[i].cw_max =
1955                                 cpu_to_le16(cw_max);
1956                         priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
1957                         priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1958                         priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1959                 }
1960         }
1961         IWL_DEBUG_QOS("set QoS to default \n");
1962
1963         spin_unlock_irqrestore(&priv->lock, flags);
1964 }
1965
1966 static void iwl_activate_qos(struct iwl_priv *priv, u8 force)
1967 {
1968         unsigned long flags;
1969
1970         if (priv == NULL)
1971                 return;
1972
1973         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1974                 return;
1975
1976         if (!priv->qos_data.qos_enable)
1977                 return;
1978
1979         spin_lock_irqsave(&priv->lock, flags);
1980         priv->qos_data.def_qos_parm.qos_flags = 0;
1981
1982         if (priv->qos_data.qos_cap.q_AP.queue_request &&
1983             !priv->qos_data.qos_cap.q_AP.txop_request)
1984                 priv->qos_data.def_qos_parm.qos_flags |=
1985                         QOS_PARAM_FLG_TXOP_TYPE_MSK;
1986
1987         if (priv->qos_data.qos_active)
1988                 priv->qos_data.def_qos_parm.qos_flags |=
1989                         QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1990
1991         spin_unlock_irqrestore(&priv->lock, flags);
1992
1993         if (force || iwl_is_associated(priv)) {
1994                 IWL_DEBUG_QOS("send QoS cmd with Qos active %d \n",
1995                               priv->qos_data.qos_active);
1996
1997                 iwl_send_qos_params_command(priv,
1998                                 &(priv->qos_data.def_qos_parm));
1999         }
2000 }
2001
2002 #endif /* CONFIG_IWLWIFI_QOS */
2003 /*
2004  * Power management (not Tx power!) functions
2005  */
2006 #define MSEC_TO_USEC 1024
2007
2008 #define NOSLP __constant_cpu_to_le32(0)
2009 #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK
2010 #define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2011 #define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2012                                      __constant_cpu_to_le32(X1), \
2013                                      __constant_cpu_to_le32(X2), \
2014                                      __constant_cpu_to_le32(X3), \
2015                                      __constant_cpu_to_le32(X4)}
2016
2017
2018 /* default power management (not Tx power) table values */
2019 /* for tim  0-10 */
2020 static struct iwl_power_vec_entry range_0[IWL_POWER_AC] = {
2021         {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2022         {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2023         {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2024         {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2025         {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2026         {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2027 };
2028
2029 /* for tim > 10 */
2030 static struct iwl_power_vec_entry range_1[IWL_POWER_AC] = {
2031         {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2032         {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2033                  SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2034         {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2035                  SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2036         {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2037                  SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2038         {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2039         {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2040                  SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2041 };
2042
2043 int iwl_power_init_handle(struct iwl_priv *priv)
2044 {
2045         int rc = 0, i;
2046         struct iwl_power_mgr *pow_data;
2047         int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_AC;
2048         u16 pci_pm;
2049
2050         IWL_DEBUG_POWER("Initialize power \n");
2051
2052         pow_data = &(priv->power_data);
2053
2054         memset(pow_data, 0, sizeof(*pow_data));
2055
2056         pow_data->active_index = IWL_POWER_RANGE_0;
2057         pow_data->dtim_val = 0xffff;
2058
2059         memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2060         memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2061
2062         rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2063         if (rc != 0)
2064                 return 0;
2065         else {
2066                 struct iwl_powertable_cmd *cmd;
2067
2068                 IWL_DEBUG_POWER("adjust power command flags\n");
2069
2070                 for (i = 0; i < IWL_POWER_AC; i++) {
2071                         cmd = &pow_data->pwr_range_0[i].cmd;
2072
2073                         if (pci_pm & 0x1)
2074                                 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2075                         else
2076                                 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2077                 }
2078         }
2079         return rc;
2080 }
2081
2082 static int iwl_update_power_cmd(struct iwl_priv *priv,
2083                                 struct iwl_powertable_cmd *cmd, u32 mode)
2084 {
2085         int rc = 0, i;
2086         u8 skip;
2087         u32 max_sleep = 0;
2088         struct iwl_power_vec_entry *range;
2089         u8 period = 0;
2090         struct iwl_power_mgr *pow_data;
2091
2092         if (mode > IWL_POWER_INDEX_5) {
2093                 IWL_DEBUG_POWER("Error invalid power mode \n");
2094                 return -1;
2095         }
2096         pow_data = &(priv->power_data);
2097
2098         if (pow_data->active_index == IWL_POWER_RANGE_0)
2099                 range = &pow_data->pwr_range_0[0];
2100         else
2101                 range = &pow_data->pwr_range_1[1];
2102
2103         memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
2104
2105 #ifdef IWL_MAC80211_DISABLE
2106         if (priv->assoc_network != NULL) {
2107                 unsigned long flags;
2108
2109                 period = priv->assoc_network->tim.tim_period;
2110         }
2111 #endif  /*IWL_MAC80211_DISABLE */
2112         skip = range[mode].no_dtim;
2113
2114         if (period == 0) {
2115                 period = 1;
2116                 skip = 0;
2117         }
2118
2119         if (skip == 0) {
2120                 max_sleep = period;
2121                 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2122         } else {
2123                 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2124                 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2125                 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2126         }
2127
2128         for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2129                 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2130                         cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2131         }
2132
2133         IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2134         IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2135         IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2136         IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2137                         le32_to_cpu(cmd->sleep_interval[0]),
2138                         le32_to_cpu(cmd->sleep_interval[1]),
2139                         le32_to_cpu(cmd->sleep_interval[2]),
2140                         le32_to_cpu(cmd->sleep_interval[3]),
2141                         le32_to_cpu(cmd->sleep_interval[4]));
2142
2143         return rc;
2144 }
2145
2146 static int iwl_send_power_mode(struct iwl_priv *priv, u32 mode)
2147 {
2148         u32 final_mode = mode;
2149         int rc;
2150         struct iwl_powertable_cmd cmd;
2151
2152         /* If on battery, set to 3,
2153          * if plugged into AC power, set to CAM ("continuosly aware mode"),
2154          * else user level */
2155         switch (mode) {
2156         case IWL_POWER_BATTERY:
2157                 final_mode = IWL_POWER_INDEX_3;
2158                 break;
2159         case IWL_POWER_AC:
2160                 final_mode = IWL_POWER_MODE_CAM;
2161                 break;
2162         default:
2163                 final_mode = mode;
2164                 break;
2165         }
2166
2167         iwl_update_power_cmd(priv, &cmd, final_mode);
2168
2169         rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
2170
2171         if (final_mode == IWL_POWER_MODE_CAM)
2172                 clear_bit(STATUS_POWER_PMI, &priv->status);
2173         else
2174                 set_bit(STATUS_POWER_PMI, &priv->status);
2175
2176         return rc;
2177 }
2178
2179 int iwl_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
2180 {
2181         /* Filter incoming packets to determine if they are targeted toward
2182          * this network, discarding packets coming from ourselves */
2183         switch (priv->iw_mode) {
2184         case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source    | BSSID */
2185                 /* packets from our adapter are dropped (echo) */
2186                 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2187                         return 0;
2188                 /* {broad,multi}cast packets to our IBSS go through */
2189                 if (is_multicast_ether_addr(header->addr1))
2190                         return !compare_ether_addr(header->addr3, priv->bssid);
2191                 /* packets to our adapter go through */
2192                 return !compare_ether_addr(header->addr1, priv->mac_addr);
2193         case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2194                 /* packets from our adapter are dropped (echo) */
2195                 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2196                         return 0;
2197                 /* {broad,multi}cast packets to our BSS go through */
2198                 if (is_multicast_ether_addr(header->addr1))
2199                         return !compare_ether_addr(header->addr2, priv->bssid);
2200                 /* packets to our adapter go through */
2201                 return !compare_ether_addr(header->addr1, priv->mac_addr);
2202         }
2203
2204         return 1;
2205 }
2206
2207 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2208
2209 const char *iwl_get_tx_fail_reason(u32 status)
2210 {
2211         switch (status & TX_STATUS_MSK) {
2212         case TX_STATUS_SUCCESS:
2213                 return "SUCCESS";
2214                 TX_STATUS_ENTRY(SHORT_LIMIT);
2215                 TX_STATUS_ENTRY(LONG_LIMIT);
2216                 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2217                 TX_STATUS_ENTRY(MGMNT_ABORT);
2218                 TX_STATUS_ENTRY(NEXT_FRAG);
2219                 TX_STATUS_ENTRY(LIFE_EXPIRE);
2220                 TX_STATUS_ENTRY(DEST_PS);
2221                 TX_STATUS_ENTRY(ABORTED);
2222                 TX_STATUS_ENTRY(BT_RETRY);
2223                 TX_STATUS_ENTRY(STA_INVALID);
2224                 TX_STATUS_ENTRY(FRAG_DROPPED);
2225                 TX_STATUS_ENTRY(TID_DISABLE);
2226                 TX_STATUS_ENTRY(FRAME_FLUSHED);
2227                 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2228                 TX_STATUS_ENTRY(TX_LOCKED);
2229                 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2230         }
2231
2232         return "UNKNOWN";
2233 }
2234
2235 /**
2236  * iwl_scan_cancel - Cancel any currently executing HW scan
2237  *
2238  * NOTE: priv->mutex is not required before calling this function
2239  */
2240 static int iwl_scan_cancel(struct iwl_priv *priv)
2241 {
2242         if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2243                 clear_bit(STATUS_SCANNING, &priv->status);
2244                 return 0;
2245         }
2246
2247         if (test_bit(STATUS_SCANNING, &priv->status)) {
2248                 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2249                         IWL_DEBUG_SCAN("Queuing scan abort.\n");
2250                         set_bit(STATUS_SCAN_ABORTING, &priv->status);
2251                         queue_work(priv->workqueue, &priv->abort_scan);
2252
2253                 } else
2254                         IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2255
2256                 return test_bit(STATUS_SCANNING, &priv->status);
2257         }
2258
2259         return 0;
2260 }
2261
2262 /**
2263  * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
2264  * @ms: amount of time to wait (in milliseconds) for scan to abort
2265  *
2266  * NOTE: priv->mutex must be held before calling this function
2267  */
2268 static int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
2269 {
2270         unsigned long now = jiffies;
2271         int ret;
2272
2273         ret = iwl_scan_cancel(priv);
2274         if (ret && ms) {
2275                 mutex_unlock(&priv->mutex);
2276                 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2277                                 test_bit(STATUS_SCANNING, &priv->status))
2278                         msleep(1);
2279                 mutex_lock(&priv->mutex);
2280
2281                 return test_bit(STATUS_SCANNING, &priv->status);
2282         }
2283
2284         return ret;
2285 }
2286
2287 static void iwl_sequence_reset(struct iwl_priv *priv)
2288 {
2289         /* Reset ieee stats */
2290
2291         /* We don't reset the net_device_stats (ieee->stats) on
2292          * re-association */
2293
2294         priv->last_seq_num = -1;
2295         priv->last_frag_num = -1;
2296         priv->last_packet_time = 0;
2297
2298         iwl_scan_cancel(priv);
2299 }
2300
2301 #define MAX_UCODE_BEACON_INTERVAL       1024
2302 #define INTEL_CONN_LISTEN_INTERVAL      __constant_cpu_to_le16(0xA)
2303
2304 static __le16 iwl_adjust_beacon_interval(u16 beacon_val)
2305 {
2306         u16 new_val = 0;
2307         u16 beacon_factor = 0;
2308
2309         beacon_factor =
2310             (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2311                 / MAX_UCODE_BEACON_INTERVAL;
2312         new_val = beacon_val / beacon_factor;
2313
2314         return cpu_to_le16(new_val);
2315 }
2316
2317 static void iwl_setup_rxon_timing(struct iwl_priv *priv)
2318 {
2319         u64 interval_tm_unit;
2320         u64 tsf, result;
2321         unsigned long flags;
2322         struct ieee80211_conf *conf = NULL;
2323         u16 beacon_int = 0;
2324
2325         conf = ieee80211_get_hw_conf(priv->hw);
2326
2327         spin_lock_irqsave(&priv->lock, flags);
2328         priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2329         priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2330
2331         priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2332
2333         tsf = priv->timestamp1;
2334         tsf = ((tsf << 32) | priv->timestamp0);
2335
2336         beacon_int = priv->beacon_int;
2337         spin_unlock_irqrestore(&priv->lock, flags);
2338
2339         if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2340                 if (beacon_int == 0) {
2341                         priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2342                         priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2343                 } else {
2344                         priv->rxon_timing.beacon_interval =
2345                                 cpu_to_le16(beacon_int);
2346                         priv->rxon_timing.beacon_interval =
2347                             iwl_adjust_beacon_interval(
2348                                 le16_to_cpu(priv->rxon_timing.beacon_interval));
2349                 }
2350
2351                 priv->rxon_timing.atim_window = 0;
2352         } else {
2353                 priv->rxon_timing.beacon_interval =
2354                         iwl_adjust_beacon_interval(conf->beacon_int);
2355                 /* TODO: we need to get atim_window from upper stack
2356                  * for now we set to 0 */
2357                 priv->rxon_timing.atim_window = 0;
2358         }
2359
2360         interval_tm_unit =
2361                 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2362         result = do_div(tsf, interval_tm_unit);
2363         priv->rxon_timing.beacon_init_val =
2364             cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2365
2366         IWL_DEBUG_ASSOC
2367             ("beacon interval %d beacon timer %d beacon tim %d\n",
2368                 le16_to_cpu(priv->rxon_timing.beacon_interval),
2369                 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2370                 le16_to_cpu(priv->rxon_timing.atim_window));
2371 }
2372
2373 static int iwl_scan_initiate(struct iwl_priv *priv)
2374 {
2375         if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2376                 IWL_ERROR("APs don't scan.\n");
2377                 return 0;
2378         }
2379
2380         if (!iwl_is_ready_rf(priv)) {
2381                 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2382                 return -EIO;
2383         }
2384
2385         if (test_bit(STATUS_SCANNING, &priv->status)) {
2386                 IWL_DEBUG_SCAN("Scan already in progress.\n");
2387                 return -EAGAIN;
2388         }
2389
2390         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2391                 IWL_DEBUG_SCAN("Scan request while abort pending.  "
2392                                "Queuing.\n");
2393                 return -EAGAIN;
2394         }
2395
2396         IWL_DEBUG_INFO("Starting scan...\n");
2397         priv->scan_bands = 2;
2398         set_bit(STATUS_SCANNING, &priv->status);
2399         priv->scan_start = jiffies;
2400         priv->scan_pass_start = priv->scan_start;
2401
2402         queue_work(priv->workqueue, &priv->request_scan);
2403
2404         return 0;
2405 }
2406
2407 static int iwl_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
2408 {
2409         struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
2410
2411         if (hw_decrypt)
2412                 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2413         else
2414                 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2415
2416         return 0;
2417 }
2418
2419 static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode)
2420 {
2421         if (phymode == MODE_IEEE80211A) {
2422                 priv->staging_rxon.flags &=
2423                     ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2424                       | RXON_FLG_CCK_MSK);
2425                 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2426         } else {
2427                 /* Copied from iwl_bg_post_associate() */
2428                 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2429                         priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2430                 else
2431                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2432
2433                 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2434                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2435
2436                 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2437                 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2438                 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2439         }
2440 }
2441
2442 /*
2443  * initilize rxon structure with default values fromm eeprom
2444  */
2445 static void iwl_connection_init_rx_config(struct iwl_priv *priv)
2446 {
2447         const struct iwl_channel_info *ch_info;
2448
2449         memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2450
2451         switch (priv->iw_mode) {
2452         case IEEE80211_IF_TYPE_AP:
2453                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2454                 break;
2455
2456         case IEEE80211_IF_TYPE_STA:
2457                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2458                 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2459                 break;
2460
2461         case IEEE80211_IF_TYPE_IBSS:
2462                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2463                 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2464                 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2465                                                   RXON_FILTER_ACCEPT_GRP_MSK;
2466                 break;
2467
2468         case IEEE80211_IF_TYPE_MNTR:
2469                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2470                 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2471                     RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2472                 break;
2473         }
2474
2475 #if 0
2476         /* TODO:  Figure out when short_preamble would be set and cache from
2477          * that */
2478         if (!hw_to_local(priv->hw)->short_preamble)
2479                 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2480         else
2481                 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2482 #endif
2483
2484         ch_info = iwl_get_channel_info(priv, priv->phymode,
2485                                        le16_to_cpu(priv->staging_rxon.channel));
2486
2487         if (!ch_info)
2488                 ch_info = &priv->channel_info[0];
2489
2490         /*
2491          * in some case A channels are all non IBSS
2492          * in this case force B/G channel
2493          */
2494         if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2495             !(is_channel_ibss(ch_info)))
2496                 ch_info = &priv->channel_info[0];
2497
2498         priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2499         if (is_channel_a_band(ch_info))
2500                 priv->phymode = MODE_IEEE80211A;
2501         else
2502                 priv->phymode = MODE_IEEE80211G;
2503
2504         iwl_set_flags_for_phymode(priv, priv->phymode);
2505
2506         priv->staging_rxon.ofdm_basic_rates =
2507             (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2508         priv->staging_rxon.cck_basic_rates =
2509             (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2510 }
2511
2512 static int iwl_set_mode(struct iwl_priv *priv, int mode)
2513 {
2514         if (!iwl_is_ready_rf(priv))
2515                 return -EAGAIN;
2516
2517         if (mode == IEEE80211_IF_TYPE_IBSS) {
2518                 const struct iwl_channel_info *ch_info;
2519
2520                 ch_info = iwl_get_channel_info(priv,
2521                         priv->phymode,
2522                         le16_to_cpu(priv->staging_rxon.channel));
2523
2524                 if (!ch_info || !is_channel_ibss(ch_info)) {
2525                         IWL_ERROR("channel %d not IBSS channel\n",
2526                                   le16_to_cpu(priv->staging_rxon.channel));
2527                         return -EINVAL;
2528                 }
2529         }
2530
2531         cancel_delayed_work(&priv->scan_check);
2532         if (iwl_scan_cancel_timeout(priv, 100)) {
2533                 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2534                 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2535                 return -EAGAIN;
2536         }
2537
2538         priv->iw_mode = mode;
2539
2540         iwl_connection_init_rx_config(priv);
2541         memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2542
2543         iwl_clear_stations_table(priv);
2544
2545         iwl_commit_rxon(priv);
2546
2547         return 0;
2548 }
2549
2550 static void iwl_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
2551                                       struct ieee80211_tx_control *ctl,
2552                                       struct iwl_cmd *cmd,
2553                                       struct sk_buff *skb_frag,
2554                                       int last_frag)
2555 {
2556         struct iwl_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
2557
2558         switch (keyinfo->alg) {
2559         case ALG_CCMP:
2560                 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2561                 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2562                 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2563                 break;
2564
2565         case ALG_TKIP:
2566 #if 0
2567                 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2568
2569                 if (last_frag)
2570                         memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2571                                8);
2572                 else
2573                         memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2574 #endif
2575                 break;
2576
2577         case ALG_WEP:
2578                 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2579                     (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2580
2581                 if (keyinfo->keylen == 13)
2582                         cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2583
2584                 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2585
2586                 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2587                              "with key %d\n", ctl->key_idx);
2588                 break;
2589
2590         default:
2591                 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2592                 break;
2593         }
2594 }
2595
2596 /*
2597  * handle build REPLY_TX command notification.
2598  */
2599 static void iwl_build_tx_cmd_basic(struct iwl_priv *priv,
2600                                   struct iwl_cmd *cmd,
2601                                   struct ieee80211_tx_control *ctrl,
2602                                   struct ieee80211_hdr *hdr,
2603                                   int is_unicast, u8 std_id)
2604 {
2605         __le16 *qc;
2606         u16 fc = le16_to_cpu(hdr->frame_control);
2607         __le32 tx_flags = cmd->cmd.tx.tx_flags;
2608
2609         cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2610         if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2611                 tx_flags |= TX_CMD_FLG_ACK_MSK;
2612                 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2613                         tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2614                 if (ieee80211_is_probe_response(fc) &&
2615                     !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2616                         tx_flags |= TX_CMD_FLG_TSF_MSK;
2617         } else {
2618                 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2619                 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2620         }
2621
2622         cmd->cmd.tx.sta_id = std_id;
2623         if (ieee80211_get_morefrag(hdr))
2624                 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2625
2626         qc = ieee80211_get_qos_ctrl(hdr);
2627         if (qc) {
2628                 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2629                 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2630         } else
2631                 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2632
2633         if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2634                 tx_flags |= TX_CMD_FLG_RTS_MSK;
2635                 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2636         } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2637                 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2638                 tx_flags |= TX_CMD_FLG_CTS_MSK;
2639         }
2640
2641         if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2642                 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2643
2644         tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2645         if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2646                 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2647                     (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
2648                         cmd->cmd.tx.timeout.pm_frame_timeout =
2649                                 cpu_to_le16(3);
2650                 else
2651                         cmd->cmd.tx.timeout.pm_frame_timeout =
2652                                 cpu_to_le16(2);
2653         } else
2654                 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2655
2656         cmd->cmd.tx.driver_txop = 0;
2657         cmd->cmd.tx.tx_flags = tx_flags;
2658         cmd->cmd.tx.next_frame_len = 0;
2659 }
2660
2661 static int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
2662 {
2663         int sta_id;
2664         u16 fc = le16_to_cpu(hdr->frame_control);
2665
2666         /* If this frame is broadcast or not data then use the broadcast
2667          * station id */
2668         if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2669             is_multicast_ether_addr(hdr->addr1))
2670                 return priv->hw_setting.bcast_sta_id;
2671
2672         switch (priv->iw_mode) {
2673
2674         /* If this frame is part of a BSS network (we're a station), then
2675          * we use the AP's station id */
2676         case IEEE80211_IF_TYPE_STA:
2677                 return IWL_AP_ID;
2678
2679         /* If we are an AP, then find the station, or use BCAST */
2680         case IEEE80211_IF_TYPE_AP:
2681                 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2682                 if (sta_id != IWL_INVALID_STATION)
2683                         return sta_id;
2684                 return priv->hw_setting.bcast_sta_id;
2685
2686         /* If this frame is part of a IBSS network, then we use the
2687          * target specific station id */
2688         case IEEE80211_IF_TYPE_IBSS: {
2689                 DECLARE_MAC_BUF(mac);
2690
2691                 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2692                 if (sta_id != IWL_INVALID_STATION)
2693                         return sta_id;
2694
2695                 sta_id = iwl_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
2696
2697                 if (sta_id != IWL_INVALID_STATION)
2698                         return sta_id;
2699
2700                 IWL_DEBUG_DROP("Station %s not in station map. "
2701                                "Defaulting to broadcast...\n",
2702                                print_mac(mac, hdr->addr1));
2703                 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
2704                 return priv->hw_setting.bcast_sta_id;
2705         }
2706         default:
2707                 IWL_WARNING("Unkown mode of operation: %d", priv->iw_mode);
2708                 return priv->hw_setting.bcast_sta_id;
2709         }
2710 }
2711
2712 /*
2713  * start REPLY_TX command process
2714  */
2715 static int iwl_tx_skb(struct iwl_priv *priv,
2716                       struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2717 {
2718         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2719         struct iwl_tfd_frame *tfd;
2720         u32 *control_flags;
2721         int txq_id = ctl->queue;
2722         struct iwl_tx_queue *txq = NULL;
2723         struct iwl_queue *q = NULL;
2724         dma_addr_t phys_addr;
2725         dma_addr_t txcmd_phys;
2726         struct iwl_cmd *out_cmd = NULL;
2727         u16 len, idx, len_org;
2728         u8 id, hdr_len, unicast;
2729         u8 sta_id;
2730         u16 seq_number = 0;
2731         u16 fc;
2732         __le16 *qc;
2733         u8 wait_write_ptr = 0;
2734         unsigned long flags;
2735         int rc;
2736
2737         spin_lock_irqsave(&priv->lock, flags);
2738         if (iwl_is_rfkill(priv)) {
2739                 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2740                 goto drop_unlock;
2741         }
2742
2743         if (!priv->interface_id) {
2744                 IWL_DEBUG_DROP("Dropping - !priv->interface_id\n");
2745                 goto drop_unlock;
2746         }
2747
2748         if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2749                 IWL_ERROR("ERROR: No TX rate available.\n");
2750                 goto drop_unlock;
2751         }
2752
2753         unicast = !is_multicast_ether_addr(hdr->addr1);
2754         id = 0;
2755
2756         fc = le16_to_cpu(hdr->frame_control);
2757
2758 #ifdef CONFIG_IWLWIFI_DEBUG
2759         if (ieee80211_is_auth(fc))
2760                 IWL_DEBUG_TX("Sending AUTH frame\n");
2761         else if (ieee80211_is_assoc_request(fc))
2762                 IWL_DEBUG_TX("Sending ASSOC frame\n");
2763         else if (ieee80211_is_reassoc_request(fc))
2764                 IWL_DEBUG_TX("Sending REASSOC frame\n");
2765 #endif
2766
2767         if (!iwl_is_associated(priv) &&
2768             ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
2769                 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
2770                 goto drop_unlock;
2771         }
2772
2773         spin_unlock_irqrestore(&priv->lock, flags);
2774
2775         hdr_len = ieee80211_get_hdrlen(fc);
2776         sta_id = iwl_get_sta_id(priv, hdr);
2777         if (sta_id == IWL_INVALID_STATION) {
2778                 DECLARE_MAC_BUF(mac);
2779
2780                 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2781                                print_mac(mac, hdr->addr1));
2782                 goto drop;
2783         }
2784
2785         IWL_DEBUG_RATE("station Id %d\n", sta_id);
2786
2787         qc = ieee80211_get_qos_ctrl(hdr);
2788         if (qc) {
2789                 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2790                 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2791                                 IEEE80211_SCTL_SEQ;
2792                 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2793                         (hdr->seq_ctrl &
2794                                 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2795                 seq_number += 0x10;
2796         }
2797         txq = &priv->txq[txq_id];
2798         q = &txq->q;
2799
2800         spin_lock_irqsave(&priv->lock, flags);
2801
2802         tfd = &txq->bd[q->first_empty];
2803         memset(tfd, 0, sizeof(*tfd));
2804         control_flags = (u32 *) tfd;
2805         idx = get_cmd_index(q, q->first_empty, 0);
2806
2807         memset(&(txq->txb[q->first_empty]), 0, sizeof(struct iwl_tx_info));
2808         txq->txb[q->first_empty].skb[0] = skb;
2809         memcpy(&(txq->txb[q->first_empty].status.control),
2810                ctl, sizeof(struct ieee80211_tx_control));
2811         out_cmd = &txq->cmd[idx];
2812         memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2813         memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
2814         out_cmd->hdr.cmd = REPLY_TX;
2815         out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
2816                                 INDEX_TO_SEQ(q->first_empty)));
2817         /* copy frags header */
2818         memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2819
2820         /* hdr = (struct ieee80211_hdr *)out_cmd->cmd.tx.hdr; */
2821         len = priv->hw_setting.tx_cmd_len +
2822                 sizeof(struct iwl_cmd_header) + hdr_len;
2823
2824         len_org = len;
2825         len = (len + 3) & ~3;
2826
2827         if (len_org != len)
2828                 len_org = 1;
2829         else
2830                 len_org = 0;
2831
2832         txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2833                      offsetof(struct iwl_cmd, hdr);
2834
2835         iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
2836
2837         if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
2838                 iwl_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
2839
2840         /* 802.11 null functions have no payload... */
2841         len = skb->len - hdr_len;
2842         if (len) {
2843                 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2844                                            len, PCI_DMA_TODEVICE);
2845                 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
2846         }
2847
2848         /* If there is no payload, then only one TFD is used */
2849         if (!len)
2850                 *control_flags = TFD_CTL_COUNT_SET(1);
2851         else
2852                 *control_flags = TFD_CTL_COUNT_SET(2) |
2853                         TFD_CTL_PAD_SET(U32_PAD(len));
2854
2855         len = (u16)skb->len;
2856         out_cmd->cmd.tx.len = cpu_to_le16(len);
2857
2858         /* TODO need this for burst mode later on */
2859         iwl_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
2860
2861         /* set is_hcca to 0; it probably will never be implemented */
2862         iwl_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
2863
2864         out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2865         out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
2866
2867         if (!ieee80211_get_morefrag(hdr)) {
2868                 txq->need_update = 1;
2869                 if (qc) {
2870                         u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2871                         priv->stations[sta_id].tid[tid].seq_number = seq_number;
2872                 }
2873         } else {
2874                 wait_write_ptr = 1;
2875                 txq->need_update = 0;
2876         }
2877
2878         iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
2879                            sizeof(out_cmd->cmd.tx));
2880
2881         iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
2882                            ieee80211_get_hdrlen(fc));
2883
2884         q->first_empty = iwl_queue_inc_wrap(q->first_empty, q->n_bd);
2885         rc = iwl_tx_queue_update_write_ptr(priv, txq);
2886         spin_unlock_irqrestore(&priv->lock, flags);
2887
2888         if (rc)
2889                 return rc;
2890
2891         if ((iwl_queue_space(q) < q->high_mark)
2892             && priv->mac80211_registered) {
2893                 if (wait_write_ptr) {
2894                         spin_lock_irqsave(&priv->lock, flags);
2895                         txq->need_update = 1;
2896                         iwl_tx_queue_update_write_ptr(priv, txq);
2897                         spin_unlock_irqrestore(&priv->lock, flags);
2898                 }
2899
2900                 ieee80211_stop_queue(priv->hw, ctl->queue);
2901         }
2902
2903         return 0;
2904
2905 drop_unlock:
2906         spin_unlock_irqrestore(&priv->lock, flags);
2907 drop:
2908         return -1;
2909 }
2910
2911 static void iwl_set_rate(struct iwl_priv *priv)
2912 {
2913         const struct ieee80211_hw_mode *hw = NULL;
2914         struct ieee80211_rate *rate;
2915         int i;
2916
2917         hw = iwl_get_hw_mode(priv, priv->phymode);
2918
2919         priv->active_rate = 0;
2920         priv->active_rate_basic = 0;
2921
2922         IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
2923                        hw->mode == MODE_IEEE80211A ?
2924                        'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
2925
2926         for (i = 0; i < hw->num_rates; i++) {
2927                 rate = &(hw->rates[i]);
2928                 if ((rate->val < IWL_RATE_COUNT) &&
2929                     (rate->flags & IEEE80211_RATE_SUPPORTED)) {
2930                         IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
2931                                        rate->val, iwl_rates[rate->val].plcp,
2932                                        (rate->flags & IEEE80211_RATE_BASIC) ?
2933                                        "*" : "");
2934                         priv->active_rate |= (1 << rate->val);
2935                         if (rate->flags & IEEE80211_RATE_BASIC)
2936                                 priv->active_rate_basic |= (1 << rate->val);
2937                 } else
2938                         IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
2939                                        rate->val, iwl_rates[rate->val].plcp);
2940         }
2941
2942         IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2943                        priv->active_rate, priv->active_rate_basic);
2944
2945         /*
2946          * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2947          * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2948          * OFDM
2949          */
2950         if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2951                 priv->staging_rxon.cck_basic_rates =
2952                     ((priv->active_rate_basic &
2953                       IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2954         else
2955                 priv->staging_rxon.cck_basic_rates =
2956                     (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2957
2958         if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2959                 priv->staging_rxon.ofdm_basic_rates =
2960                     ((priv->active_rate_basic &
2961                       (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2962                       IWL_FIRST_OFDM_RATE) & 0xFF;
2963         else
2964                 priv->staging_rxon.ofdm_basic_rates =
2965                    (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2966 }
2967
2968 static void iwl_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
2969 {
2970         unsigned long flags;
2971
2972         if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2973                 return;
2974
2975         IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2976                           disable_radio ? "OFF" : "ON");
2977
2978         if (disable_radio) {
2979                 iwl_scan_cancel(priv);
2980                 /* FIXME: This is a workaround for AP */
2981                 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2982                         spin_lock_irqsave(&priv->lock, flags);
2983                         iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
2984                                     CSR_UCODE_SW_BIT_RFKILL);
2985                         spin_unlock_irqrestore(&priv->lock, flags);
2986                         iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
2987                         set_bit(STATUS_RF_KILL_SW, &priv->status);
2988                 }
2989                 return;
2990         }
2991
2992         spin_lock_irqsave(&priv->lock, flags);
2993         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2994
2995         clear_bit(STATUS_RF_KILL_SW, &priv->status);
2996         spin_unlock_irqrestore(&priv->lock, flags);
2997
2998         /* wake up ucode */
2999         msleep(10);
3000
3001         spin_lock_irqsave(&priv->lock, flags);
3002         iwl_read32(priv, CSR_UCODE_DRV_GP1);
3003         if (!iwl_grab_restricted_access(priv))
3004                 iwl_release_restricted_access(priv);
3005         spin_unlock_irqrestore(&priv->lock, flags);
3006
3007         if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3008                 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3009                                   "disabled by HW switch\n");
3010                 return;
3011         }
3012
3013         queue_work(priv->workqueue, &priv->restart);
3014         return;
3015 }
3016
3017 void iwl_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
3018                             u32 decrypt_res, struct ieee80211_rx_status *stats)
3019 {
3020         u16 fc =
3021             le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3022
3023         if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3024                 return;
3025
3026         if (!(fc & IEEE80211_FCTL_PROTECTED))
3027                 return;
3028
3029         IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3030         switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3031         case RX_RES_STATUS_SEC_TYPE_TKIP:
3032                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3033                     RX_RES_STATUS_BAD_ICV_MIC)
3034                         stats->flag |= RX_FLAG_MMIC_ERROR;
3035         case RX_RES_STATUS_SEC_TYPE_WEP:
3036         case RX_RES_STATUS_SEC_TYPE_CCMP:
3037                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3038                     RX_RES_STATUS_DECRYPT_OK) {
3039                         IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3040                         stats->flag |= RX_FLAG_DECRYPTED;
3041                 }
3042                 break;
3043
3044         default:
3045                 break;
3046         }
3047 }
3048
3049 void iwl_handle_data_packet_monitor(struct iwl_priv *priv,
3050                                     struct iwl_rx_mem_buffer *rxb,
3051                                     void *data, short len,
3052                                     struct ieee80211_rx_status *stats,
3053                                     u16 phy_flags)
3054 {
3055         struct iwl_rt_rx_hdr *iwl_rt;
3056
3057         /* First cache any information we need before we overwrite
3058          * the information provided in the skb from the hardware */
3059         s8 signal = stats->ssi;
3060         s8 noise = 0;
3061         int rate = stats->rate;
3062         u64 tsf = stats->mactime;
3063         __le16 phy_flags_hw = cpu_to_le16(phy_flags);
3064
3065         /* We received data from the HW, so stop the watchdog */
3066         if (len > IWL_RX_BUF_SIZE - sizeof(*iwl_rt)) {
3067                 IWL_DEBUG_DROP("Dropping too large packet in monitor\n");
3068                 return;
3069         }
3070
3071         /* copy the frame data to write after where the radiotap header goes */
3072         iwl_rt = (void *)rxb->skb->data;
3073         memmove(iwl_rt->payload, data, len);
3074
3075         iwl_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
3076         iwl_rt->rt_hdr.it_pad = 0; /* always good to zero */
3077
3078         /* total header + data */
3079         iwl_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*iwl_rt));
3080
3081         /* Set the size of the skb to the size of the frame */
3082         skb_put(rxb->skb, sizeof(*iwl_rt) + len);
3083
3084         /* Big bitfield of all the fields we provide in radiotap */
3085         iwl_rt->rt_hdr.it_present =
3086             cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
3087                         (1 << IEEE80211_RADIOTAP_FLAGS) |
3088                         (1 << IEEE80211_RADIOTAP_RATE) |
3089                         (1 << IEEE80211_RADIOTAP_CHANNEL) |
3090                         (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
3091                         (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
3092                         (1 << IEEE80211_RADIOTAP_ANTENNA));
3093
3094         /* Zero the flags, we'll add to them as we go */
3095         iwl_rt->rt_flags = 0;
3096
3097         iwl_rt->rt_tsf = cpu_to_le64(tsf);
3098
3099         /* Convert to dBm */
3100         iwl_rt->rt_dbmsignal = signal;
3101         iwl_rt->rt_dbmnoise = noise;
3102
3103         /* Convert the channel frequency and set the flags */
3104         iwl_rt->rt_channelMHz = cpu_to_le16(stats->freq);
3105         if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
3106                 iwl_rt->rt_chbitmask =
3107                     cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
3108         else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
3109                 iwl_rt->rt_chbitmask =
3110                     cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
3111         else    /* 802.11g */
3112                 iwl_rt->rt_chbitmask =
3113                     cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ));
3114
3115         rate = iwl_rate_index_from_plcp(rate);
3116         if (rate == -1)
3117                 iwl_rt->rt_rate = 0;
3118         else
3119                 iwl_rt->rt_rate = iwl_rates[rate].ieee;
3120
3121         /* antenna number */
3122         iwl_rt->rt_antenna =
3123                 le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
3124
3125         /* set the preamble flag if we have it */
3126         if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
3127                 iwl_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3128
3129         IWL_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
3130
3131         stats->flag |= RX_FLAG_RADIOTAP;
3132         ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
3133         rxb->skb = NULL;
3134 }
3135