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