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