Merge upstream 2.6.13-rc1-git1 into 'ieee80211' branch of netdev-2.6.
[linux-2.6] / drivers / net / wireless / ipw2100.c
1 /******************************************************************************
2
3   Copyright(c) 2003 - 2005 Intel Corporation. All rights reserved.
4
5   This program is free software; you can redistribute it and/or modify it
6   under the terms of version 2 of the GNU General Public License as
7   published by the Free Software Foundation.
8
9   This program is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12   more details.
13
14   You should have received a copy of the GNU General Public License along with
15   this program; if not, write to the Free Software Foundation, Inc., 59
16   Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17
18   The full GNU General Public License is included in this distribution in the
19   file called LICENSE.
20
21   Contact Information:
22   James P. Ketrenos <ipw2100-admin@linux.intel.com>
23   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24
25   Portions of this file are based on the sample_* files provided by Wireless
26   Extensions 0.26 package and copyright (c) 1997-2003 Jean Tourrilhes
27   <jt@hpl.hp.com>
28
29   Portions of this file are based on the Host AP project,
30   Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
31     <jkmaline@cc.hut.fi>
32   Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
33
34   Portions of ipw2100_mod_firmware_load, ipw2100_do_mod_firmware_load, and
35   ipw2100_fw_load are loosely based on drivers/sound/sound_firmware.c
36   available in the 2.4.25 kernel sources, and are copyright (c) Alan Cox
37
38 ******************************************************************************/
39 /*
40
41  Initial driver on which this is based was developed by Janusz Gorycki,
42  Maciej Urbaniak, and Maciej Sosnowski.
43
44  Promiscuous mode support added by Jacek Wysoczynski and Maciej Urbaniak.
45
46 Theory of Operation
47
48 Tx - Commands and Data
49
50 Firmware and host share a circular queue of Transmit Buffer Descriptors (TBDs)
51 Each TBD contains a pointer to the physical (dma_addr_t) address of data being
52 sent to the firmware as well as the length of the data.
53
54 The host writes to the TBD queue at the WRITE index.  The WRITE index points
55 to the _next_ packet to be written and is advanced when after the TBD has been
56 filled.
57
58 The firmware pulls from the TBD queue at the READ index.  The READ index points
59 to the currently being read entry, and is advanced once the firmware is
60 done with a packet.
61
62 When data is sent to the firmware, the first TBD is used to indicate to the
63 firmware if a Command or Data is being sent.  If it is Command, all of the
64 command information is contained within the physical address referred to by the
65 TBD.  If it is Data, the first TBD indicates the type of data packet, number
66 of fragments, etc.  The next TBD then referrs to the actual packet location.
67
68 The Tx flow cycle is as follows:
69
70 1) ipw2100_tx() is called by kernel with SKB to transmit
71 2) Packet is move from the tx_free_list and appended to the transmit pending
72    list (tx_pend_list)
73 3) work is scheduled to move pending packets into the shared circular queue.
74 4) when placing packet in the circular queue, the incoming SKB is DMA mapped
75    to a physical address.  That address is entered into a TBD.  Two TBDs are
76    filled out.  The first indicating a data packet, the second referring to the
77    actual payload data.
78 5) the packet is removed from tx_pend_list and placed on the end of the
79    firmware pending list (fw_pend_list)
80 6) firmware is notified that the WRITE index has
81 7) Once the firmware has processed the TBD, INTA is triggered.
82 8) For each Tx interrupt received from the firmware, the READ index is checked
83    to see which TBDs are done being processed.
84 9) For each TBD that has been processed, the ISR pulls the oldest packet
85    from the fw_pend_list.
86 10)The packet structure contained in the fw_pend_list is then used
87    to unmap the DMA address and to free the SKB originally passed to the driver
88    from the kernel.
89 11)The packet structure is placed onto the tx_free_list
90
91 The above steps are the same for commands, only the msg_free_list/msg_pend_list
92 are used instead of tx_free_list/tx_pend_list
93
94 ...
95
96 Critical Sections / Locking :
97
98 There are two locks utilized.  The first is the low level lock (priv->low_lock)
99 that protects the following:
100
101 - Access to the Tx/Rx queue lists via priv->low_lock. The lists are as follows:
102
103   tx_free_list : Holds pre-allocated Tx buffers.
104     TAIL modified in __ipw2100_tx_process()
105     HEAD modified in ipw2100_tx()
106
107   tx_pend_list : Holds used Tx buffers waiting to go into the TBD ring
108     TAIL modified ipw2100_tx()
109     HEAD modified by X__ipw2100_tx_send_data()
110
111   msg_free_list : Holds pre-allocated Msg (Command) buffers
112     TAIL modified in __ipw2100_tx_process()
113     HEAD modified in ipw2100_hw_send_command()
114
115   msg_pend_list : Holds used Msg buffers waiting to go into the TBD ring
116     TAIL modified in ipw2100_hw_send_command()
117     HEAD modified in X__ipw2100_tx_send_commands()
118
119   The flow of data on the TX side is as follows:
120
121   MSG_FREE_LIST + COMMAND => MSG_PEND_LIST => TBD => MSG_FREE_LIST
122   TX_FREE_LIST + DATA => TX_PEND_LIST => TBD => TX_FREE_LIST
123
124   The methods that work on the TBD ring are protected via priv->low_lock.
125
126 - The internal data state of the device itself
127 - Access to the firmware read/write indexes for the BD queues
128   and associated logic
129
130 All external entry functions are locked with the priv->action_lock to ensure
131 that only one external action is invoked at a time.
132
133
134 */
135
136 #include <linux/compiler.h>
137 #include <linux/config.h>
138 #include <linux/errno.h>
139 #include <linux/if_arp.h>
140 #include <linux/in6.h>
141 #include <linux/in.h>
142 #include <linux/ip.h>
143 #include <linux/kernel.h>
144 #include <linux/kmod.h>
145 #include <linux/module.h>
146 #include <linux/netdevice.h>
147 #include <linux/ethtool.h>
148 #include <linux/pci.h>
149 #include <linux/dma-mapping.h>
150 #include <linux/proc_fs.h>
151 #include <linux/skbuff.h>
152 #include <asm/uaccess.h>
153 #include <asm/io.h>
154 #define __KERNEL_SYSCALLS__
155 #include <linux/fs.h>
156 #include <linux/mm.h>
157 #include <linux/slab.h>
158 #include <linux/unistd.h>
159 #include <linux/stringify.h>
160 #include <linux/tcp.h>
161 #include <linux/types.h>
162 #include <linux/version.h>
163 #include <linux/time.h>
164 #include <linux/firmware.h>
165 #include <linux/acpi.h>
166 #include <linux/ctype.h>
167
168 #include "ipw2100.h"
169
170 #define IPW2100_VERSION "1.1.0"
171
172 #define DRV_NAME        "ipw2100"
173 #define DRV_VERSION     IPW2100_VERSION
174 #define DRV_DESCRIPTION "Intel(R) PRO/Wireless 2100 Network Driver"
175 #define DRV_COPYRIGHT   "Copyright(c) 2003-2004 Intel Corporation"
176
177
178 /* Debugging stuff */
179 #ifdef CONFIG_IPW_DEBUG
180 #define CONFIG_IPW2100_RX_DEBUG   /* Reception debugging */
181 #endif
182
183 MODULE_DESCRIPTION(DRV_DESCRIPTION);
184 MODULE_VERSION(DRV_VERSION);
185 MODULE_AUTHOR(DRV_COPYRIGHT);
186 MODULE_LICENSE("GPL");
187
188 static int debug = 0;
189 static int mode = 0;
190 static int channel = 0;
191 static int associate = 1;
192 static int disable = 0;
193 #ifdef CONFIG_PM
194 static struct ipw2100_fw ipw2100_firmware;
195 #endif
196
197 #include <linux/moduleparam.h>
198 module_param(debug, int, 0444);
199 module_param(mode, int, 0444);
200 module_param(channel, int, 0444);
201 module_param(associate, int, 0444);
202 module_param(disable, int, 0444);
203
204 MODULE_PARM_DESC(debug, "debug level");
205 MODULE_PARM_DESC(mode, "network mode (0=BSS,1=IBSS,2=Monitor)");
206 MODULE_PARM_DESC(channel, "channel");
207 MODULE_PARM_DESC(associate, "auto associate when scanning (default on)");
208 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
209
210 u32 ipw2100_debug_level = IPW_DL_NONE;
211
212 #ifdef CONFIG_IPW_DEBUG
213 static const char *command_types[] = {
214         "undefined",
215         "unused", /* HOST_ATTENTION */
216         "HOST_COMPLETE",
217         "unused", /* SLEEP */
218         "unused", /* HOST_POWER_DOWN */
219         "unused",
220         "SYSTEM_CONFIG",
221         "unused", /* SET_IMR */
222         "SSID",
223         "MANDATORY_BSSID",
224         "AUTHENTICATION_TYPE",
225         "ADAPTER_ADDRESS",
226         "PORT_TYPE",
227         "INTERNATIONAL_MODE",
228         "CHANNEL",
229         "RTS_THRESHOLD",
230         "FRAG_THRESHOLD",
231         "POWER_MODE",
232         "TX_RATES",
233         "BASIC_TX_RATES",
234         "WEP_KEY_INFO",
235         "unused",
236         "unused",
237         "unused",
238         "unused",
239         "WEP_KEY_INDEX",
240         "WEP_FLAGS",
241         "ADD_MULTICAST",
242         "CLEAR_ALL_MULTICAST",
243         "BEACON_INTERVAL",
244         "ATIM_WINDOW",
245         "CLEAR_STATISTICS",
246         "undefined",
247         "undefined",
248         "undefined",
249         "undefined",
250         "TX_POWER_INDEX",
251         "undefined",
252         "undefined",
253         "undefined",
254         "undefined",
255         "undefined",
256         "undefined",
257         "BROADCAST_SCAN",
258         "CARD_DISABLE",
259         "PREFERRED_BSSID",
260         "SET_SCAN_OPTIONS",
261         "SCAN_DWELL_TIME",
262         "SWEEP_TABLE",
263         "AP_OR_STATION_TABLE",
264         "GROUP_ORDINALS",
265         "SHORT_RETRY_LIMIT",
266         "LONG_RETRY_LIMIT",
267         "unused", /* SAVE_CALIBRATION */
268         "unused", /* RESTORE_CALIBRATION */
269         "undefined",
270         "undefined",
271         "undefined",
272         "HOST_PRE_POWER_DOWN",
273         "unused", /* HOST_INTERRUPT_COALESCING */
274         "undefined",
275         "CARD_DISABLE_PHY_OFF",
276         "MSDU_TX_RATES"
277         "undefined",
278         "undefined",
279         "SET_STATION_STAT_BITS",
280         "CLEAR_STATIONS_STAT_BITS",
281         "LEAP_ROGUE_MODE",
282         "SET_SECURITY_INFORMATION",
283         "DISASSOCIATION_BSSID",
284         "SET_WPA_ASS_IE"
285 };
286 #endif
287
288
289 /* Pre-decl until we get the code solid and then we can clean it up */
290 static void X__ipw2100_tx_send_commands(struct ipw2100_priv *priv);
291 static void X__ipw2100_tx_send_data(struct ipw2100_priv *priv);
292 static int ipw2100_adapter_setup(struct ipw2100_priv *priv);
293
294 static void ipw2100_queues_initialize(struct ipw2100_priv *priv);
295 static void ipw2100_queues_free(struct ipw2100_priv *priv);
296 static int ipw2100_queues_allocate(struct ipw2100_priv *priv);
297
298
299 static inline void read_register(struct net_device *dev, u32 reg, u32 *val)
300 {
301         *val = readl((void *)(dev->base_addr + reg));
302         IPW_DEBUG_IO("r: 0x%08X => 0x%08X\n", reg, *val);
303 }
304
305 static inline void write_register(struct net_device *dev, u32 reg, u32 val)
306 {
307         writel(val, (void *)(dev->base_addr + reg));
308         IPW_DEBUG_IO("w: 0x%08X <= 0x%08X\n", reg, val);
309 }
310
311 static inline void read_register_word(struct net_device *dev, u32 reg, u16 *val)
312 {
313         *val = readw((void *)(dev->base_addr + reg));
314         IPW_DEBUG_IO("r: 0x%08X => %04X\n", reg, *val);
315 }
316
317 static inline void read_register_byte(struct net_device *dev, u32 reg, u8 *val)
318 {
319         *val = readb((void *)(dev->base_addr + reg));
320         IPW_DEBUG_IO("r: 0x%08X => %02X\n", reg, *val);
321 }
322
323 static inline void write_register_word(struct net_device *dev, u32 reg, u16 val)
324 {
325         writew(val, (void *)(dev->base_addr + reg));
326         IPW_DEBUG_IO("w: 0x%08X <= %04X\n", reg, val);
327 }
328
329
330 static inline void write_register_byte(struct net_device *dev, u32 reg, u8 val)
331 {
332         writeb(val, (void *)(dev->base_addr + reg));
333         IPW_DEBUG_IO("w: 0x%08X =< %02X\n", reg, val);
334 }
335
336 static inline void read_nic_dword(struct net_device *dev, u32 addr, u32 *val)
337 {
338         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
339                        addr & IPW_REG_INDIRECT_ADDR_MASK);
340         read_register(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
341 }
342
343 static inline void write_nic_dword(struct net_device *dev, u32 addr, u32 val)
344 {
345         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
346                        addr & IPW_REG_INDIRECT_ADDR_MASK);
347         write_register(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
348 }
349
350 static inline void read_nic_word(struct net_device *dev, u32 addr, u16 *val)
351 {
352         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
353                        addr & IPW_REG_INDIRECT_ADDR_MASK);
354         read_register_word(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
355 }
356
357 static inline void write_nic_word(struct net_device *dev, u32 addr, u16 val)
358 {
359         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
360                        addr & IPW_REG_INDIRECT_ADDR_MASK);
361         write_register_word(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
362 }
363
364 static inline void read_nic_byte(struct net_device *dev, u32 addr, u8 *val)
365 {
366         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
367                        addr & IPW_REG_INDIRECT_ADDR_MASK);
368         read_register_byte(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
369 }
370
371 static inline void write_nic_byte(struct net_device *dev, u32 addr, u8 val)
372 {
373         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
374                        addr & IPW_REG_INDIRECT_ADDR_MASK);
375         write_register_byte(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
376 }
377
378 static inline void write_nic_auto_inc_address(struct net_device *dev, u32 addr)
379 {
380         write_register(dev, IPW_REG_AUTOINCREMENT_ADDRESS,
381                        addr & IPW_REG_INDIRECT_ADDR_MASK);
382 }
383
384 static inline void write_nic_dword_auto_inc(struct net_device *dev, u32 val)
385 {
386         write_register(dev, IPW_REG_AUTOINCREMENT_DATA, val);
387 }
388
389 static inline void write_nic_memory(struct net_device *dev, u32 addr, u32 len,
390                                     const u8 *buf)
391 {
392         u32 aligned_addr;
393         u32 aligned_len;
394         u32 dif_len;
395         u32 i;
396
397         /* read first nibble byte by byte */
398         aligned_addr = addr & (~0x3);
399         dif_len = addr - aligned_addr;
400         if (dif_len) {
401                 /* Start reading at aligned_addr + dif_len */
402                 write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
403                                aligned_addr);
404                 for (i = dif_len; i < 4; i++, buf++)
405                         write_register_byte(
406                                 dev, IPW_REG_INDIRECT_ACCESS_DATA + i,
407                                 *buf);
408
409                 len -= dif_len;
410                 aligned_addr += 4;
411         }
412
413         /* read DWs through autoincrement registers */
414         write_register(dev, IPW_REG_AUTOINCREMENT_ADDRESS,
415                        aligned_addr);
416         aligned_len = len & (~0x3);
417         for (i = 0; i < aligned_len; i += 4, buf += 4, aligned_addr += 4)
418                 write_register(
419                         dev, IPW_REG_AUTOINCREMENT_DATA, *(u32 *)buf);
420
421         /* copy the last nibble */
422         dif_len = len - aligned_len;
423         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS, aligned_addr);
424         for (i = 0; i < dif_len; i++, buf++)
425                 write_register_byte(
426                         dev, IPW_REG_INDIRECT_ACCESS_DATA + i, *buf);
427 }
428
429 static inline void read_nic_memory(struct net_device *dev, u32 addr, u32 len,
430                                    u8 *buf)
431 {
432         u32 aligned_addr;
433         u32 aligned_len;
434         u32 dif_len;
435         u32 i;
436
437         /* read first nibble byte by byte */
438         aligned_addr = addr & (~0x3);
439         dif_len = addr - aligned_addr;
440         if (dif_len) {
441                 /* Start reading at aligned_addr + dif_len */
442                 write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
443                                aligned_addr);
444                 for (i = dif_len; i < 4; i++, buf++)
445                         read_register_byte(
446                                 dev, IPW_REG_INDIRECT_ACCESS_DATA + i, buf);
447
448                 len -= dif_len;
449                 aligned_addr += 4;
450         }
451
452         /* read DWs through autoincrement registers */
453         write_register(dev, IPW_REG_AUTOINCREMENT_ADDRESS,
454                        aligned_addr);
455         aligned_len = len & (~0x3);
456         for (i = 0; i < aligned_len; i += 4, buf += 4, aligned_addr += 4)
457                 read_register(dev, IPW_REG_AUTOINCREMENT_DATA,
458                               (u32 *)buf);
459
460         /* copy the last nibble */
461         dif_len = len - aligned_len;
462         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
463                        aligned_addr);
464         for (i = 0; i < dif_len; i++, buf++)
465                 read_register_byte(dev, IPW_REG_INDIRECT_ACCESS_DATA +
466                                    i, buf);
467 }
468
469 static inline int ipw2100_hw_is_adapter_in_system(struct net_device *dev)
470 {
471         return (dev->base_addr &&
472                 (readl((void *)(dev->base_addr + IPW_REG_DOA_DEBUG_AREA_START))
473                  == IPW_DATA_DOA_DEBUG_VALUE));
474 }
475
476 int ipw2100_get_ordinal(struct ipw2100_priv *priv, u32 ord,
477                         void *val, u32 *len)
478 {
479         struct ipw2100_ordinals *ordinals = &priv->ordinals;
480         u32 addr;
481         u32 field_info;
482         u16 field_len;
483         u16 field_count;
484         u32 total_length;
485
486         if (ordinals->table1_addr == 0) {
487                 IPW_DEBUG_WARNING(DRV_NAME ": attempt to use fw ordinals "
488                        "before they have been loaded.\n");
489                 return -EINVAL;
490         }
491
492         if (IS_ORDINAL_TABLE_ONE(ordinals, ord)) {
493                 if (*len < IPW_ORD_TAB_1_ENTRY_SIZE) {
494                         *len = IPW_ORD_TAB_1_ENTRY_SIZE;
495
496                         IPW_DEBUG_WARNING(DRV_NAME
497                                ": ordinal buffer length too small, need %zd\n",
498                                IPW_ORD_TAB_1_ENTRY_SIZE);
499
500                         return -EINVAL;
501                 }
502
503                 read_nic_dword(priv->net_dev, ordinals->table1_addr + (ord << 2),
504                                &addr);
505                 read_nic_dword(priv->net_dev, addr, val);
506
507                 *len = IPW_ORD_TAB_1_ENTRY_SIZE;
508
509                 return 0;
510         }
511
512         if (IS_ORDINAL_TABLE_TWO(ordinals, ord)) {
513
514                 ord -= IPW_START_ORD_TAB_2;
515
516                 /* get the address of statistic */
517                 read_nic_dword(priv->net_dev, ordinals->table2_addr + (ord << 3),
518                                &addr);
519
520                 /* get the second DW of statistics ;
521                  * two 16-bit words - first is length, second is count */
522                 read_nic_dword(priv->net_dev,
523                                ordinals->table2_addr + (ord << 3) + sizeof(u32),
524                                &field_info);
525
526                 /* get each entry length */
527                 field_len = *((u16 *)&field_info);
528
529                 /* get number of entries */
530                 field_count = *(((u16 *)&field_info) + 1);
531
532                 /* abort if no enought memory */
533                 total_length = field_len * field_count;
534                 if (total_length > *len) {
535                         *len = total_length;
536                         return -EINVAL;
537                 }
538
539                 *len = total_length;
540                 if (!total_length)
541                         return 0;
542
543                 /* read the ordinal data from the SRAM */
544                 read_nic_memory(priv->net_dev, addr, total_length, val);
545
546                 return 0;
547         }
548
549         IPW_DEBUG_WARNING(DRV_NAME ": ordinal %d neither in table 1 nor "
550                "in table 2\n", ord);
551
552         return -EINVAL;
553 }
554
555 static int ipw2100_set_ordinal(struct ipw2100_priv *priv, u32 ord, u32 *val,
556                                u32 *len)
557 {
558         struct ipw2100_ordinals *ordinals = &priv->ordinals;
559         u32 addr;
560
561         if (IS_ORDINAL_TABLE_ONE(ordinals, ord)) {
562                 if (*len != IPW_ORD_TAB_1_ENTRY_SIZE) {
563                         *len = IPW_ORD_TAB_1_ENTRY_SIZE;
564                         IPW_DEBUG_INFO("wrong size\n");
565                         return -EINVAL;
566                 }
567
568                 read_nic_dword(priv->net_dev, ordinals->table1_addr + (ord << 2),
569                                &addr);
570
571                 write_nic_dword(priv->net_dev, addr, *val);
572
573                 *len = IPW_ORD_TAB_1_ENTRY_SIZE;
574
575                 return 0;
576         }
577
578         IPW_DEBUG_INFO("wrong table\n");
579         if (IS_ORDINAL_TABLE_TWO(ordinals, ord))
580                 return -EINVAL;
581
582         return -EINVAL;
583 }
584
585 static char *snprint_line(char *buf, size_t count,
586                           const u8 *data, u32 len, u32 ofs)
587 {
588         int out, i, j, l;
589         char c;
590
591         out = snprintf(buf, count, "%08X", ofs);
592
593         for (l = 0, i = 0; i < 2; i++) {
594                 out += snprintf(buf + out, count - out, " ");
595                 for (j = 0; j < 8 && l < len; j++, l++)
596                         out += snprintf(buf + out, count - out, "%02X ",
597                                         data[(i * 8 + j)]);
598                 for (; j < 8; j++)
599                         out += snprintf(buf + out, count - out, "   ");
600         }
601
602         out += snprintf(buf + out, count - out, " ");
603         for (l = 0, i = 0; i < 2; i++) {
604                 out += snprintf(buf + out, count - out, " ");
605                 for (j = 0; j < 8 && l < len; j++, l++) {
606                         c = data[(i * 8 + j)];
607                         if (!isascii(c) || !isprint(c))
608                                 c = '.';
609
610                         out += snprintf(buf + out, count - out, "%c", c);
611                 }
612
613                 for (; j < 8; j++)
614                         out += snprintf(buf + out, count - out, " ");
615         }
616
617         return buf;
618 }
619
620 static void printk_buf(int level, const u8 *data, u32 len)
621 {
622         char line[81];
623         u32 ofs = 0;
624         if (!(ipw2100_debug_level & level))
625                 return;
626
627         while (len) {
628                 printk(KERN_DEBUG "%s\n",
629                        snprint_line(line, sizeof(line), &data[ofs],
630                                     min(len, 16U), ofs));
631                 ofs += 16;
632                 len -= min(len, 16U);
633         }
634 }
635
636
637
638 #define MAX_RESET_BACKOFF 10
639
640 static inline void schedule_reset(struct ipw2100_priv *priv)
641 {
642         unsigned long now = get_seconds();
643
644         /* If we haven't received a reset request within the backoff period,
645          * then we can reset the backoff interval so this reset occurs
646          * immediately */
647         if (priv->reset_backoff &&
648             (now - priv->last_reset > priv->reset_backoff))
649                 priv->reset_backoff = 0;
650
651         priv->last_reset = get_seconds();
652
653         if (!(priv->status & STATUS_RESET_PENDING)) {
654                 IPW_DEBUG_INFO("%s: Scheduling firmware restart (%ds).\n",
655                                priv->net_dev->name, priv->reset_backoff);
656                 netif_carrier_off(priv->net_dev);
657                 netif_stop_queue(priv->net_dev);
658                 priv->status |= STATUS_RESET_PENDING;
659                 if (priv->reset_backoff)
660                         queue_delayed_work(priv->workqueue, &priv->reset_work,
661                                            priv->reset_backoff * HZ);
662                 else
663                         queue_work(priv->workqueue, &priv->reset_work);
664
665                 if (priv->reset_backoff < MAX_RESET_BACKOFF)
666                         priv->reset_backoff++;
667
668                 wake_up_interruptible(&priv->wait_command_queue);
669         } else
670                 IPW_DEBUG_INFO("%s: Firmware restart already in progress.\n",
671                                priv->net_dev->name);
672
673 }
674
675 #define HOST_COMPLETE_TIMEOUT (2 * HZ)
676 static int ipw2100_hw_send_command(struct ipw2100_priv *priv,
677                                    struct host_command * cmd)
678 {
679         struct list_head *element;
680         struct ipw2100_tx_packet *packet;
681         unsigned long flags;
682         int err = 0;
683
684         IPW_DEBUG_HC("Sending %s command (#%d), %d bytes\n",
685                      command_types[cmd->host_command], cmd->host_command,
686                      cmd->host_command_length);
687         printk_buf(IPW_DL_HC, (u8*)cmd->host_command_parameters,
688                    cmd->host_command_length);
689
690         spin_lock_irqsave(&priv->low_lock, flags);
691
692         if (priv->fatal_error) {
693                 IPW_DEBUG_INFO("Attempt to send command while hardware in fatal error condition.\n");
694                 err = -EIO;
695                 goto fail_unlock;
696         }
697
698         if (!(priv->status & STATUS_RUNNING)) {
699                 IPW_DEBUG_INFO("Attempt to send command while hardware is not running.\n");
700                 err = -EIO;
701                 goto fail_unlock;
702         }
703
704         if (priv->status & STATUS_CMD_ACTIVE) {
705                 IPW_DEBUG_INFO("Attempt to send command while another command is pending.\n");
706                 err = -EBUSY;
707                 goto fail_unlock;
708         }
709
710         if (list_empty(&priv->msg_free_list)) {
711                 IPW_DEBUG_INFO("no available msg buffers\n");
712                 goto fail_unlock;
713         }
714
715         priv->status |= STATUS_CMD_ACTIVE;
716         priv->messages_sent++;
717
718         element = priv->msg_free_list.next;
719
720         packet = list_entry(element, struct ipw2100_tx_packet, list);
721         packet->jiffy_start = jiffies;
722
723         /* initialize the firmware command packet */
724         packet->info.c_struct.cmd->host_command_reg = cmd->host_command;
725         packet->info.c_struct.cmd->host_command_reg1 = cmd->host_command1;
726         packet->info.c_struct.cmd->host_command_len_reg = cmd->host_command_length;
727         packet->info.c_struct.cmd->sequence = cmd->host_command_sequence;
728
729         memcpy(packet->info.c_struct.cmd->host_command_params_reg,
730                cmd->host_command_parameters,
731                sizeof(packet->info.c_struct.cmd->host_command_params_reg));
732
733         list_del(element);
734         DEC_STAT(&priv->msg_free_stat);
735
736         list_add_tail(element, &priv->msg_pend_list);
737         INC_STAT(&priv->msg_pend_stat);
738
739         X__ipw2100_tx_send_commands(priv);
740         X__ipw2100_tx_send_data(priv);
741
742         spin_unlock_irqrestore(&priv->low_lock, flags);
743
744         /*
745          * We must wait for this command to complete before another
746          * command can be sent...  but if we wait more than 3 seconds
747          * then there is a problem.
748          */
749
750         err = wait_event_interruptible_timeout(
751                 priv->wait_command_queue, !(priv->status & STATUS_CMD_ACTIVE),
752                 HOST_COMPLETE_TIMEOUT);
753
754         if (err == 0) {
755                 IPW_DEBUG_INFO("Command completion failed out after %dms.\n",
756                                HOST_COMPLETE_TIMEOUT / (HZ / 100));
757                 priv->fatal_error = IPW2100_ERR_MSG_TIMEOUT;
758                 priv->status &= ~STATUS_CMD_ACTIVE;
759                 schedule_reset(priv);
760                 return -EIO;
761         }
762
763         if (priv->fatal_error) {
764                 IPW_DEBUG_WARNING("%s: firmware fatal error\n",
765                        priv->net_dev->name);
766                 return -EIO;
767         }
768
769         /* !!!!! HACK TEST !!!!!
770          * When lots of debug trace statements are enabled, the driver
771          * doesn't seem to have as many firmware restart cycles...
772          *
773          * As a test, we're sticking in a 1/100s delay here */
774         set_current_state(TASK_UNINTERRUPTIBLE);
775         schedule_timeout(HZ / 100);
776
777         return 0;
778
779  fail_unlock:
780         spin_unlock_irqrestore(&priv->low_lock, flags);
781
782         return err;
783 }
784
785
786 /*
787  * Verify the values and data access of the hardware
788  * No locks needed or used.  No functions called.
789  */
790 static int ipw2100_verify(struct ipw2100_priv *priv)
791 {
792         u32 data1, data2;
793         u32 address;
794
795         u32 val1 = 0x76543210;
796         u32 val2 = 0xFEDCBA98;
797
798         /* Domain 0 check - all values should be DOA_DEBUG */
799         for (address = IPW_REG_DOA_DEBUG_AREA_START;
800              address < IPW_REG_DOA_DEBUG_AREA_END;
801              address += sizeof(u32)) {
802                 read_register(priv->net_dev, address, &data1);
803                 if (data1 != IPW_DATA_DOA_DEBUG_VALUE)
804                         return -EIO;
805         }
806
807         /* Domain 1 check - use arbitrary read/write compare  */
808         for (address = 0; address < 5; address++) {
809                 /* The memory area is not used now */
810                 write_register(priv->net_dev, IPW_REG_DOMAIN_1_OFFSET + 0x32,
811                                val1);
812                 write_register(priv->net_dev, IPW_REG_DOMAIN_1_OFFSET + 0x36,
813                                val2);
814                 read_register(priv->net_dev, IPW_REG_DOMAIN_1_OFFSET + 0x32,
815                               &data1);
816                 read_register(priv->net_dev, IPW_REG_DOMAIN_1_OFFSET + 0x36,
817                               &data2);
818                 if (val1 == data1 && val2 == data2)
819                         return 0;
820         }
821
822         return -EIO;
823 }
824
825 /*
826  *
827  * Loop until the CARD_DISABLED bit is the same value as the
828  * supplied parameter
829  *
830  * TODO: See if it would be more efficient to do a wait/wake
831  *       cycle and have the completion event trigger the wakeup
832  *
833  */
834 #define IPW_CARD_DISABLE_COMPLETE_WAIT              100 // 100 milli
835 static int ipw2100_wait_for_card_state(struct ipw2100_priv *priv, int state)
836 {
837         int i;
838         u32 card_state;
839         u32 len = sizeof(card_state);
840         int err;
841
842         for (i = 0; i <= IPW_CARD_DISABLE_COMPLETE_WAIT * 1000; i += 50) {
843                 err = ipw2100_get_ordinal(priv, IPW_ORD_CARD_DISABLED,
844                                           &card_state, &len);
845                 if (err) {
846                         IPW_DEBUG_INFO("Query of CARD_DISABLED ordinal "
847                                        "failed.\n");
848                         return 0;
849                 }
850
851                 /* We'll break out if either the HW state says it is
852                  * in the state we want, or if HOST_COMPLETE command
853                  * finishes */
854                 if ((card_state == state) ||
855                     ((priv->status & STATUS_ENABLED) ?
856                      IPW_HW_STATE_ENABLED : IPW_HW_STATE_DISABLED) == state) {
857                         if (state == IPW_HW_STATE_ENABLED)
858                                 priv->status |= STATUS_ENABLED;
859                         else
860                                 priv->status &= ~STATUS_ENABLED;
861
862                         return 0;
863                 }
864
865                 udelay(50);
866         }
867
868         IPW_DEBUG_INFO("ipw2100_wait_for_card_state to %s state timed out\n",
869                        state ? "DISABLED" : "ENABLED");
870         return -EIO;
871 }
872
873
874 /*********************************************************************
875     Procedure   :   sw_reset_and_clock
876     Purpose     :   Asserts s/w reset, asserts clock initialization
877                     and waits for clock stabilization
878  ********************************************************************/
879 static int sw_reset_and_clock(struct ipw2100_priv *priv)
880 {
881         int i;
882         u32 r;
883
884         // assert s/w reset
885         write_register(priv->net_dev, IPW_REG_RESET_REG,
886                        IPW_AUX_HOST_RESET_REG_SW_RESET);
887
888         // wait for clock stabilization
889         for (i = 0; i < 1000; i++) {
890                 udelay(IPW_WAIT_RESET_ARC_COMPLETE_DELAY);
891
892                 // check clock ready bit
893                 read_register(priv->net_dev, IPW_REG_RESET_REG, &r);
894                 if (r & IPW_AUX_HOST_RESET_REG_PRINCETON_RESET)
895                         break;
896         }
897
898         if (i == 1000)
899                 return -EIO;    // TODO: better error value
900
901         /* set "initialization complete" bit to move adapter to
902          * D0 state */
903         write_register(priv->net_dev, IPW_REG_GP_CNTRL,
904                        IPW_AUX_HOST_GP_CNTRL_BIT_INIT_DONE);
905
906         /* wait for clock stabilization */
907         for (i = 0; i < 10000; i++) {
908                 udelay(IPW_WAIT_CLOCK_STABILIZATION_DELAY * 4);
909
910                 /* check clock ready bit */
911                 read_register(priv->net_dev, IPW_REG_GP_CNTRL, &r);
912                 if (r & IPW_AUX_HOST_GP_CNTRL_BIT_CLOCK_READY)
913                         break;
914         }
915
916         if (i == 10000)
917                 return -EIO;    /* TODO: better error value */
918
919         /* set D0 standby bit */
920         read_register(priv->net_dev, IPW_REG_GP_CNTRL, &r);
921         write_register(priv->net_dev, IPW_REG_GP_CNTRL,
922                        r | IPW_AUX_HOST_GP_CNTRL_BIT_HOST_ALLOWS_STANDBY);
923
924         return 0;
925 }
926
927 /*********************************************************************
928     Procedure   :   ipw2100_download_firmware
929     Purpose     :   Initiaze adapter after power on.
930                     The sequence is:
931                     1. assert s/w reset first!
932                     2. awake clocks & wait for clock stabilization
933                     3. hold ARC (don't ask me why...)
934                     4. load Dino ucode and reset/clock init again
935                     5. zero-out shared mem
936                     6. download f/w
937  *******************************************************************/
938 static int ipw2100_download_firmware(struct ipw2100_priv *priv)
939 {
940         u32 address;
941         int err;
942
943 #ifndef CONFIG_PM
944         /* Fetch the firmware and microcode */
945         struct ipw2100_fw ipw2100_firmware;
946 #endif
947
948         if (priv->fatal_error) {
949                 IPW_DEBUG_ERROR("%s: ipw2100_download_firmware called after "
950                        "fatal error %d.  Interface must be brought down.\n",
951                        priv->net_dev->name, priv->fatal_error);
952                 return -EINVAL;
953         }
954
955 #ifdef CONFIG_PM
956         if (!ipw2100_firmware.version) {
957                 err = ipw2100_get_firmware(priv, &ipw2100_firmware);
958                 if (err) {
959                         IPW_DEBUG_ERROR("%s: ipw2100_get_firmware failed: %d\n",
960                                priv->net_dev->name, err);
961                         priv->fatal_error = IPW2100_ERR_FW_LOAD;
962                         goto fail;
963                 }
964         }
965 #else
966         err = ipw2100_get_firmware(priv, &ipw2100_firmware);
967         if (err) {
968                 IPW_DEBUG_ERROR("%s: ipw2100_get_firmware failed: %d\n",
969                        priv->net_dev->name, err);
970                 priv->fatal_error = IPW2100_ERR_FW_LOAD;
971                 goto fail;
972         }
973 #endif
974         priv->firmware_version = ipw2100_firmware.version;
975
976         /* s/w reset and clock stabilization */
977         err = sw_reset_and_clock(priv);
978         if (err) {
979                 IPW_DEBUG_ERROR("%s: sw_reset_and_clock failed: %d\n",
980                        priv->net_dev->name, err);
981                 goto fail;
982         }
983
984         err = ipw2100_verify(priv);
985         if (err) {
986                 IPW_DEBUG_ERROR("%s: ipw2100_verify failed: %d\n",
987                        priv->net_dev->name, err);
988                 goto fail;
989         }
990
991         /* Hold ARC */
992         write_nic_dword(priv->net_dev,
993                         IPW_INTERNAL_REGISTER_HALT_AND_RESET,
994                         0x80000000);
995
996         /* allow ARC to run */
997         write_register(priv->net_dev, IPW_REG_RESET_REG, 0);
998
999         /* load microcode */
1000         err = ipw2100_ucode_download(priv, &ipw2100_firmware);
1001         if (err) {
1002                 IPW_DEBUG_ERROR("%s: Error loading microcode: %d\n",
1003                        priv->net_dev->name, err);
1004                 goto fail;
1005         }
1006
1007         /* release ARC */
1008         write_nic_dword(priv->net_dev,
1009                         IPW_INTERNAL_REGISTER_HALT_AND_RESET,
1010                         0x00000000);
1011
1012         /* s/w reset and clock stabilization (again!!!) */
1013         err = sw_reset_and_clock(priv);
1014         if (err) {
1015                 IPW_DEBUG_ERROR("%s: sw_reset_and_clock failed: %d\n",
1016                        priv->net_dev->name, err);
1017                 goto fail;
1018         }
1019
1020         /* load f/w */
1021         err = ipw2100_fw_download(priv, &ipw2100_firmware);
1022         if (err) {
1023                 IPW_DEBUG_ERROR("%s: Error loading firmware: %d\n",
1024                        priv->net_dev->name, err);
1025                 goto fail;
1026         }
1027
1028 #ifndef CONFIG_PM
1029         /*
1030          * When the .resume method of the driver is called, the other
1031          * part of the system, i.e. the ide driver could still stay in
1032          * the suspend stage. This prevents us from loading the firmware
1033          * from the disk.  --YZ
1034          */
1035
1036         /* free any storage allocated for firmware image */
1037         ipw2100_release_firmware(priv, &ipw2100_firmware);
1038 #endif
1039
1040         /* zero out Domain 1 area indirectly (Si requirement) */
1041         for (address = IPW_HOST_FW_SHARED_AREA0;
1042              address < IPW_HOST_FW_SHARED_AREA0_END; address += 4)
1043                 write_nic_dword(priv->net_dev, address, 0);
1044         for (address = IPW_HOST_FW_SHARED_AREA1;
1045              address < IPW_HOST_FW_SHARED_AREA1_END; address += 4)
1046                 write_nic_dword(priv->net_dev, address, 0);
1047         for (address = IPW_HOST_FW_SHARED_AREA2;
1048              address < IPW_HOST_FW_SHARED_AREA2_END; address += 4)
1049                 write_nic_dword(priv->net_dev, address, 0);
1050         for (address = IPW_HOST_FW_SHARED_AREA3;
1051              address < IPW_HOST_FW_SHARED_AREA3_END; address += 4)
1052                 write_nic_dword(priv->net_dev, address, 0);
1053         for (address = IPW_HOST_FW_INTERRUPT_AREA;
1054              address < IPW_HOST_FW_INTERRUPT_AREA_END; address += 4)
1055                 write_nic_dword(priv->net_dev, address, 0);
1056
1057         return 0;
1058
1059  fail:
1060         ipw2100_release_firmware(priv, &ipw2100_firmware);
1061         return err;
1062 }
1063
1064 static inline void ipw2100_enable_interrupts(struct ipw2100_priv *priv)
1065 {
1066         if (priv->status & STATUS_INT_ENABLED)
1067                 return;
1068         priv->status |= STATUS_INT_ENABLED;
1069         write_register(priv->net_dev, IPW_REG_INTA_MASK, IPW_INTERRUPT_MASK);
1070 }
1071
1072 static inline void ipw2100_disable_interrupts(struct ipw2100_priv *priv)
1073 {
1074         if (!(priv->status & STATUS_INT_ENABLED))
1075                 return;
1076         priv->status &= ~STATUS_INT_ENABLED;
1077         write_register(priv->net_dev, IPW_REG_INTA_MASK, 0x0);
1078 }
1079
1080
1081 static void ipw2100_initialize_ordinals(struct ipw2100_priv *priv)
1082 {
1083         struct ipw2100_ordinals *ord = &priv->ordinals;
1084
1085         IPW_DEBUG_INFO("enter\n");
1086
1087         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_ORDINALS_TABLE_1,
1088                       &ord->table1_addr);
1089
1090         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_ORDINALS_TABLE_2,
1091                       &ord->table2_addr);
1092
1093         read_nic_dword(priv->net_dev, ord->table1_addr, &ord->table1_size);
1094         read_nic_dword(priv->net_dev, ord->table2_addr, &ord->table2_size);
1095
1096         ord->table2_size &= 0x0000FFFF;
1097
1098         IPW_DEBUG_INFO("table 1 size: %d\n", ord->table1_size);
1099         IPW_DEBUG_INFO("table 2 size: %d\n", ord->table2_size);
1100         IPW_DEBUG_INFO("exit\n");
1101 }
1102
1103 static inline void ipw2100_hw_set_gpio(struct ipw2100_priv *priv)
1104 {
1105         u32 reg = 0;
1106         /*
1107          * Set GPIO 3 writable by FW; GPIO 1 writable
1108          * by driver and enable clock
1109          */
1110         reg = (IPW_BIT_GPIO_GPIO3_MASK | IPW_BIT_GPIO_GPIO1_ENABLE |
1111                IPW_BIT_GPIO_LED_OFF);
1112         write_register(priv->net_dev, IPW_REG_GPIO, reg);
1113 }
1114
1115 static inline int rf_kill_active(struct ipw2100_priv *priv)
1116 {
1117 #define MAX_RF_KILL_CHECKS 5
1118 #define RF_KILL_CHECK_DELAY 40
1119
1120         unsigned short value = 0;
1121         u32 reg = 0;
1122         int i;
1123
1124         if (!(priv->hw_features & HW_FEATURE_RFKILL)) {
1125                 priv->status &= ~STATUS_RF_KILL_HW;
1126                 return 0;
1127         }
1128
1129         for (i = 0; i < MAX_RF_KILL_CHECKS; i++) {
1130                 udelay(RF_KILL_CHECK_DELAY);
1131                 read_register(priv->net_dev, IPW_REG_GPIO, &reg);
1132                 value = (value << 1) | ((reg & IPW_BIT_GPIO_RF_KILL) ? 0 : 1);
1133         }
1134
1135         if (value == 0)
1136                 priv->status |= STATUS_RF_KILL_HW;
1137         else
1138                 priv->status &= ~STATUS_RF_KILL_HW;
1139
1140         return (value == 0);
1141 }
1142
1143 static int ipw2100_get_hw_features(struct ipw2100_priv *priv)
1144 {
1145         u32 addr, len;
1146         u32 val;
1147
1148         /*
1149          * EEPROM_SRAM_DB_START_ADDRESS using ordinal in ordinal table 1
1150          */
1151         len = sizeof(addr);
1152         if (ipw2100_get_ordinal(
1153                     priv, IPW_ORD_EEPROM_SRAM_DB_BLOCK_START_ADDRESS,
1154                     &addr, &len)) {
1155                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1156                        __LINE__);
1157                 return -EIO;
1158         }
1159
1160         IPW_DEBUG_INFO("EEPROM address: %08X\n", addr);
1161
1162         /*
1163          * EEPROM version is the byte at offset 0xfd in firmware
1164          * We read 4 bytes, then shift out the byte we actually want */
1165         read_nic_dword(priv->net_dev, addr + 0xFC, &val);
1166         priv->eeprom_version = (val >> 24) & 0xFF;
1167         IPW_DEBUG_INFO("EEPROM version: %d\n", priv->eeprom_version);
1168
1169         /*
1170          *  HW RF Kill enable is bit 0 in byte at offset 0x21 in firmware
1171          *
1172          *  notice that the EEPROM bit is reverse polarity, i.e.
1173          *     bit = 0  signifies HW RF kill switch is supported
1174          *     bit = 1  signifies HW RF kill switch is NOT supported
1175          */
1176         read_nic_dword(priv->net_dev, addr + 0x20, &val);
1177         if (!((val >> 24) & 0x01))
1178                 priv->hw_features |= HW_FEATURE_RFKILL;
1179
1180         IPW_DEBUG_INFO("HW RF Kill: %ssupported.\n",
1181                            (priv->hw_features & HW_FEATURE_RFKILL) ?
1182                            "" : "not ");
1183
1184         return 0;
1185 }
1186
1187 /*
1188  * Start firmware execution after power on and intialization
1189  * The sequence is:
1190  *  1. Release ARC
1191  *  2. Wait for f/w initialization completes;
1192  */
1193 static int ipw2100_start_adapter(struct ipw2100_priv *priv)
1194 {
1195         int i;
1196         u32 inta, inta_mask, gpio;
1197
1198         IPW_DEBUG_INFO("enter\n");
1199
1200         if (priv->status & STATUS_RUNNING)
1201                 return 0;
1202
1203         /*
1204          * Initialize the hw - drive adapter to DO state by setting
1205          * init_done bit. Wait for clk_ready bit and Download
1206          * fw & dino ucode
1207          */
1208         if (ipw2100_download_firmware(priv)) {
1209                 IPW_DEBUG_ERROR("%s: Failed to power on the adapter.\n",
1210                        priv->net_dev->name);
1211                 return -EIO;
1212         }
1213
1214         /* Clear the Tx, Rx and Msg queues and the r/w indexes
1215          * in the firmware RBD and TBD ring queue */
1216         ipw2100_queues_initialize(priv);
1217
1218         ipw2100_hw_set_gpio(priv);
1219
1220         /* TODO -- Look at disabling interrupts here to make sure none
1221          * get fired during FW initialization */
1222
1223         /* Release ARC - clear reset bit */
1224         write_register(priv->net_dev, IPW_REG_RESET_REG, 0);
1225
1226         /* wait for f/w intialization complete */
1227         IPW_DEBUG_FW("Waiting for f/w initialization to complete...\n");
1228         i = 5000;
1229         do {
1230                 set_current_state(TASK_UNINTERRUPTIBLE);
1231                 schedule_timeout(40 * HZ / 1000);
1232                 /* Todo... wait for sync command ... */
1233
1234                 read_register(priv->net_dev, IPW_REG_INTA, &inta);
1235
1236                 /* check "init done" bit */
1237                 if (inta & IPW2100_INTA_FW_INIT_DONE) {
1238                         /* reset "init done" bit */
1239                         write_register(priv->net_dev, IPW_REG_INTA,
1240                                        IPW2100_INTA_FW_INIT_DONE);
1241                         break;
1242                 }
1243
1244                 /* check error conditions : we check these after the firmware
1245                  * check so that if there is an error, the interrupt handler
1246                  * will see it and the adapter will be reset */
1247                 if (inta &
1248                     (IPW2100_INTA_FATAL_ERROR | IPW2100_INTA_PARITY_ERROR)) {
1249                         /* clear error conditions */
1250                         write_register(priv->net_dev, IPW_REG_INTA,
1251                                        IPW2100_INTA_FATAL_ERROR |
1252                                        IPW2100_INTA_PARITY_ERROR);
1253                 }
1254         } while (i--);
1255
1256         /* Clear out any pending INTAs since we aren't supposed to have
1257          * interrupts enabled at this point... */
1258         read_register(priv->net_dev, IPW_REG_INTA, &inta);
1259         read_register(priv->net_dev, IPW_REG_INTA_MASK, &inta_mask);
1260         inta &= IPW_INTERRUPT_MASK;
1261         /* Clear out any pending interrupts */
1262         if (inta & inta_mask)
1263                 write_register(priv->net_dev, IPW_REG_INTA, inta);
1264
1265         IPW_DEBUG_FW("f/w initialization complete: %s\n",
1266                      i ? "SUCCESS" : "FAILED");
1267
1268         if (!i) {
1269                 IPW_DEBUG_WARNING("%s: Firmware did not initialize.\n",
1270                        priv->net_dev->name);
1271                 return -EIO;
1272         }
1273
1274         /* allow firmware to write to GPIO1 & GPIO3 */
1275         read_register(priv->net_dev, IPW_REG_GPIO, &gpio);
1276
1277         gpio |= (IPW_BIT_GPIO_GPIO1_MASK | IPW_BIT_GPIO_GPIO3_MASK);
1278
1279         write_register(priv->net_dev, IPW_REG_GPIO, gpio);
1280
1281         /* Ready to receive commands */
1282         priv->status |= STATUS_RUNNING;
1283
1284         /* The adapter has been reset; we are not associated */
1285         priv->status &= ~(STATUS_ASSOCIATING | STATUS_ASSOCIATED);
1286
1287         IPW_DEBUG_INFO("exit\n");
1288
1289         return 0;
1290 }
1291
1292 static inline void ipw2100_reset_fatalerror(struct ipw2100_priv *priv)
1293 {
1294         if (!priv->fatal_error)
1295                 return;
1296
1297         priv->fatal_errors[priv->fatal_index++] = priv->fatal_error;
1298         priv->fatal_index %= IPW2100_ERROR_QUEUE;
1299         priv->fatal_error = 0;
1300 }
1301
1302
1303 /* NOTE: Our interrupt is disabled when this method is called */
1304 static int ipw2100_power_cycle_adapter(struct ipw2100_priv *priv)
1305 {
1306         u32 reg;
1307         int i;
1308
1309         IPW_DEBUG_INFO("Power cycling the hardware.\n");
1310
1311         ipw2100_hw_set_gpio(priv);
1312
1313         /* Step 1. Stop Master Assert */
1314         write_register(priv->net_dev, IPW_REG_RESET_REG,
1315                        IPW_AUX_HOST_RESET_REG_STOP_MASTER);
1316
1317         /* Step 2. Wait for stop Master Assert
1318          *         (not more then 50us, otherwise ret error */
1319         i = 5;
1320         do {
1321                 udelay(IPW_WAIT_RESET_MASTER_ASSERT_COMPLETE_DELAY);
1322                 read_register(priv->net_dev, IPW_REG_RESET_REG, &reg);
1323
1324                 if (reg & IPW_AUX_HOST_RESET_REG_MASTER_DISABLED)
1325                         break;
1326         }  while(i--);
1327
1328         priv->status &= ~STATUS_RESET_PENDING;
1329
1330         if (!i) {
1331                 IPW_DEBUG_INFO("exit - waited too long for master assert stop\n");
1332                 return -EIO;
1333         }
1334
1335         write_register(priv->net_dev, IPW_REG_RESET_REG,
1336                        IPW_AUX_HOST_RESET_REG_SW_RESET);
1337
1338
1339         /* Reset any fatal_error conditions */
1340         ipw2100_reset_fatalerror(priv);
1341
1342         /* At this point, the adapter is now stopped and disabled */
1343         priv->status &= ~(STATUS_RUNNING | STATUS_ASSOCIATING |
1344                           STATUS_ASSOCIATED | STATUS_ENABLED);
1345
1346         return 0;
1347 }
1348
1349 /*
1350  * Send the CARD_DISABLE_PHY_OFF comamnd to the card to disable it
1351  *
1352  * After disabling, if the card was associated, a STATUS_ASSN_LOST will be sent.
1353  *
1354  * STATUS_CARD_DISABLE_NOTIFICATION will be sent regardless of
1355  * if STATUS_ASSN_LOST is sent.
1356  */
1357 static int ipw2100_hw_phy_off(struct ipw2100_priv *priv)
1358 {
1359
1360 #define HW_PHY_OFF_LOOP_DELAY (HZ / 5000)
1361
1362         struct host_command cmd = {
1363                 .host_command = CARD_DISABLE_PHY_OFF,
1364                 .host_command_sequence = 0,
1365                 .host_command_length = 0,
1366         };
1367         int err, i;
1368         u32 val1, val2;
1369
1370         IPW_DEBUG_HC("CARD_DISABLE_PHY_OFF\n");
1371
1372         /* Turn off the radio */
1373         err = ipw2100_hw_send_command(priv, &cmd);
1374         if (err)
1375                 return err;
1376
1377         for (i = 0; i < 2500; i++) {
1378                 read_nic_dword(priv->net_dev, IPW2100_CONTROL_REG, &val1);
1379                 read_nic_dword(priv->net_dev, IPW2100_COMMAND, &val2);
1380
1381                 if ((val1 & IPW2100_CONTROL_PHY_OFF) &&
1382                     (val2 & IPW2100_COMMAND_PHY_OFF))
1383                         return 0;
1384
1385                 set_current_state(TASK_UNINTERRUPTIBLE);
1386                 schedule_timeout(HW_PHY_OFF_LOOP_DELAY);
1387         }
1388
1389         return -EIO;
1390 }
1391
1392
1393 static int ipw2100_enable_adapter(struct ipw2100_priv *priv)
1394 {
1395         struct host_command cmd = {
1396                 .host_command = HOST_COMPLETE,
1397                 .host_command_sequence = 0,
1398                 .host_command_length = 0
1399         };
1400         int err = 0;
1401
1402         IPW_DEBUG_HC("HOST_COMPLETE\n");
1403
1404         if (priv->status & STATUS_ENABLED)
1405                 return 0;
1406
1407         down(&priv->adapter_sem);
1408
1409         if (rf_kill_active(priv)) {
1410                 IPW_DEBUG_HC("Command aborted due to RF kill active.\n");
1411                 goto fail_up;
1412         }
1413
1414         err = ipw2100_hw_send_command(priv, &cmd);
1415         if (err) {
1416                 IPW_DEBUG_INFO("Failed to send HOST_COMPLETE command\n");
1417                 goto fail_up;
1418         }
1419
1420         err = ipw2100_wait_for_card_state(priv, IPW_HW_STATE_ENABLED);
1421         if (err) {
1422                 IPW_DEBUG_INFO(
1423                        "%s: card not responding to init command.\n",
1424                        priv->net_dev->name);
1425                 goto fail_up;
1426         }
1427
1428         if (priv->stop_hang_check) {
1429                 priv->stop_hang_check = 0;
1430                 queue_delayed_work(priv->workqueue, &priv->hang_check, HZ / 2);
1431         }
1432
1433 fail_up:
1434         up(&priv->adapter_sem);
1435         return err;
1436 }
1437
1438 static int ipw2100_hw_stop_adapter(struct ipw2100_priv *priv)
1439 {
1440 #define HW_POWER_DOWN_DELAY (HZ / 10)
1441
1442         struct host_command cmd = {
1443                 .host_command = HOST_PRE_POWER_DOWN,
1444                 .host_command_sequence = 0,
1445                 .host_command_length = 0,
1446         };
1447         int err, i;
1448         u32 reg;
1449
1450         if (!(priv->status & STATUS_RUNNING))
1451                 return 0;
1452
1453         priv->status |= STATUS_STOPPING;
1454
1455         /* We can only shut down the card if the firmware is operational.  So,
1456          * if we haven't reset since a fatal_error, then we can not send the
1457          * shutdown commands. */
1458         if (!priv->fatal_error) {
1459                 /* First, make sure the adapter is enabled so that the PHY_OFF
1460                  * command can shut it down */
1461                 ipw2100_enable_adapter(priv);
1462
1463                 err = ipw2100_hw_phy_off(priv);
1464                 if (err)
1465                         IPW_DEBUG_WARNING("Error disabling radio %d\n", err);
1466
1467                 /*
1468                  * If in D0-standby mode going directly to D3 may cause a
1469                  * PCI bus violation.  Therefore we must change out of the D0
1470                  * state.
1471                  *
1472                  * Sending the PREPARE_FOR_POWER_DOWN will restrict the
1473                  * hardware from going into standby mode and will transition
1474                  * out of D0-standy if it is already in that state.
1475                  *
1476                  * STATUS_PREPARE_POWER_DOWN_COMPLETE will be sent by the
1477                  * driver upon completion.  Once received, the driver can
1478                  * proceed to the D3 state.
1479                  *
1480                  * Prepare for power down command to fw.  This command would
1481                  * take HW out of D0-standby and prepare it for D3 state.
1482                  *
1483                  * Currently FW does not support event notification for this
1484                  * event. Therefore, skip waiting for it.  Just wait a fixed
1485                  * 100ms
1486                  */
1487                 IPW_DEBUG_HC("HOST_PRE_POWER_DOWN\n");
1488
1489                 err = ipw2100_hw_send_command(priv, &cmd);
1490                 if (err)
1491                         IPW_DEBUG_WARNING(
1492                                "%s: Power down command failed: Error %d\n",
1493                                priv->net_dev->name, err);
1494                 else {
1495                         set_current_state(TASK_UNINTERRUPTIBLE);
1496                         schedule_timeout(HW_POWER_DOWN_DELAY);
1497                 }
1498         }
1499
1500         priv->status &= ~STATUS_ENABLED;
1501
1502         /*
1503          * Set GPIO 3 writable by FW; GPIO 1 writable
1504          * by driver and enable clock
1505          */
1506         ipw2100_hw_set_gpio(priv);
1507
1508         /*
1509          * Power down adapter.  Sequence:
1510          * 1. Stop master assert (RESET_REG[9]=1)
1511          * 2. Wait for stop master (RESET_REG[8]==1)
1512          * 3. S/w reset assert (RESET_REG[7] = 1)
1513          */
1514
1515         /* Stop master assert */
1516         write_register(priv->net_dev, IPW_REG_RESET_REG,
1517                        IPW_AUX_HOST_RESET_REG_STOP_MASTER);
1518
1519         /* wait stop master not more than 50 usec.
1520          * Otherwise return error. */
1521         for (i = 5; i > 0; i--) {
1522                 udelay(10);
1523
1524                 /* Check master stop bit */
1525                 read_register(priv->net_dev, IPW_REG_RESET_REG, &reg);
1526
1527                 if (reg & IPW_AUX_HOST_RESET_REG_MASTER_DISABLED)
1528                         break;
1529         }
1530
1531         if (i == 0)
1532                 IPW_DEBUG_WARNING(DRV_NAME
1533                        ": %s: Could now power down adapter.\n",
1534                        priv->net_dev->name);
1535
1536         /* assert s/w reset */
1537         write_register(priv->net_dev, IPW_REG_RESET_REG,
1538                        IPW_AUX_HOST_RESET_REG_SW_RESET);
1539
1540         priv->status &= ~(STATUS_RUNNING | STATUS_STOPPING);
1541
1542         return 0;
1543 }
1544
1545
1546 static int ipw2100_disable_adapter(struct ipw2100_priv *priv)
1547 {
1548         struct host_command cmd = {
1549                 .host_command = CARD_DISABLE,
1550                 .host_command_sequence = 0,
1551                 .host_command_length = 0
1552         };
1553         int err = 0;
1554
1555         IPW_DEBUG_HC("CARD_DISABLE\n");
1556
1557         if (!(priv->status & STATUS_ENABLED))
1558                 return 0;
1559
1560         /* Make sure we clear the associated state */
1561         priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
1562
1563         if (!priv->stop_hang_check) {
1564                 priv->stop_hang_check = 1;
1565                 cancel_delayed_work(&priv->hang_check);
1566         }
1567
1568         down(&priv->adapter_sem);
1569
1570         err = ipw2100_hw_send_command(priv, &cmd);
1571         if (err) {
1572                 IPW_DEBUG_WARNING("exit - failed to send CARD_DISABLE command\n");
1573                 goto fail_up;
1574         }
1575
1576         err = ipw2100_wait_for_card_state(priv, IPW_HW_STATE_DISABLED);
1577         if (err) {
1578                 IPW_DEBUG_WARNING("exit - card failed to change to DISABLED\n");
1579                 goto fail_up;
1580         }
1581
1582         IPW_DEBUG_INFO("TODO: implement scan state machine\n");
1583
1584 fail_up:
1585         up(&priv->adapter_sem);
1586         return err;
1587 }
1588
1589 int ipw2100_set_scan_options(struct ipw2100_priv *priv)
1590 {
1591         struct host_command cmd = {
1592                 .host_command = SET_SCAN_OPTIONS,
1593                 .host_command_sequence = 0,
1594                 .host_command_length = 8
1595         };
1596         int err;
1597
1598         IPW_DEBUG_INFO("enter\n");
1599
1600         IPW_DEBUG_SCAN("setting scan options\n");
1601
1602         cmd.host_command_parameters[0] = 0;
1603
1604         if (!(priv->config & CFG_ASSOCIATE))
1605                 cmd.host_command_parameters[0] |= IPW_SCAN_NOASSOCIATE;
1606         if ((priv->sec.flags & SEC_ENABLED) && priv->sec.enabled)
1607                 cmd.host_command_parameters[0] |= IPW_SCAN_MIXED_CELL;
1608         if (priv->config & CFG_PASSIVE_SCAN)
1609                 cmd.host_command_parameters[0] |= IPW_SCAN_PASSIVE;
1610
1611         cmd.host_command_parameters[1] = priv->channel_mask;
1612
1613         err = ipw2100_hw_send_command(priv, &cmd);
1614
1615         IPW_DEBUG_HC("SET_SCAN_OPTIONS 0x%04X\n",
1616                      cmd.host_command_parameters[0]);
1617
1618         return err;
1619 }
1620
1621 int ipw2100_start_scan(struct ipw2100_priv *priv)
1622 {
1623         struct host_command cmd = {
1624                 .host_command = BROADCAST_SCAN,
1625                 .host_command_sequence = 0,
1626                 .host_command_length = 4
1627         };
1628         int err;
1629
1630         IPW_DEBUG_HC("START_SCAN\n");
1631
1632         cmd.host_command_parameters[0] = 0;
1633
1634         /* No scanning if in monitor mode */
1635         if (priv->ieee->iw_mode == IW_MODE_MONITOR)
1636                 return 1;
1637
1638         if (priv->status & STATUS_SCANNING) {
1639                 IPW_DEBUG_SCAN("Scan requested while already in scan...\n");
1640                 return 0;
1641         }
1642
1643         IPW_DEBUG_INFO("enter\n");
1644
1645         /* Not clearing here; doing so makes iwlist always return nothing...
1646          *
1647          * We should modify the table logic to use aging tables vs. clearing
1648          * the table on each scan start.
1649          */
1650         IPW_DEBUG_SCAN("starting scan\n");
1651
1652         priv->status |= STATUS_SCANNING;
1653         err = ipw2100_hw_send_command(priv, &cmd);
1654         if (err)
1655                 priv->status &= ~STATUS_SCANNING;
1656
1657         IPW_DEBUG_INFO("exit\n");
1658
1659         return err;
1660 }
1661
1662 static int ipw2100_up(struct ipw2100_priv *priv, int deferred)
1663 {
1664         unsigned long flags;
1665         int rc = 0;
1666         u32 lock;
1667         u32 ord_len = sizeof(lock);
1668
1669         /* Quite if manually disabled. */
1670         if (priv->status & STATUS_RF_KILL_SW) {
1671                 IPW_DEBUG_INFO("%s: Radio is disabled by Manual Disable "
1672                                "switch\n", priv->net_dev->name);
1673                 return 0;
1674         }
1675
1676         /* If the interrupt is enabled, turn it off... */
1677         spin_lock_irqsave(&priv->low_lock, flags);
1678         ipw2100_disable_interrupts(priv);
1679
1680         /* Reset any fatal_error conditions */
1681         ipw2100_reset_fatalerror(priv);
1682         spin_unlock_irqrestore(&priv->low_lock, flags);
1683
1684         if (priv->status & STATUS_POWERED ||
1685             (priv->status & STATUS_RESET_PENDING)) {
1686                 /* Power cycle the card ... */
1687                 if (ipw2100_power_cycle_adapter(priv)) {
1688                         IPW_DEBUG_WARNING("%s: Could not cycle adapter.\n",
1689                                           priv->net_dev->name);
1690                         rc = 1;
1691                         goto exit;
1692                 }
1693         } else
1694                 priv->status |= STATUS_POWERED;
1695
1696         /* Load the firmware, start the clocks, etc. */
1697         if (ipw2100_start_adapter(priv)) {
1698                 IPW_DEBUG_ERROR("%s: Failed to start the firmware.\n",
1699                                 priv->net_dev->name);
1700                 rc = 1;
1701                 goto exit;
1702         }
1703
1704         ipw2100_initialize_ordinals(priv);
1705
1706         /* Determine capabilities of this particular HW configuration */
1707         if (ipw2100_get_hw_features(priv)) {
1708                 IPW_DEBUG_ERROR("%s: Failed to determine HW features.\n",
1709                                 priv->net_dev->name);
1710                 rc = 1;
1711                 goto exit;
1712         }
1713
1714         lock = LOCK_NONE;
1715         if (ipw2100_set_ordinal(priv, IPW_ORD_PERS_DB_LOCK, &lock, &ord_len)) {
1716                 IPW_DEBUG_ERROR("%s: Failed to clear ordinal lock.\n",
1717                                 priv->net_dev->name);
1718                 rc = 1;
1719                 goto exit;
1720         }
1721
1722         priv->status &= ~STATUS_SCANNING;
1723
1724         if (rf_kill_active(priv)) {
1725                 printk(KERN_INFO "%s: Radio is disabled by RF switch.\n",
1726                        priv->net_dev->name);
1727
1728                 if (priv->stop_rf_kill) {
1729                         priv->stop_rf_kill = 0;
1730                         queue_delayed_work(priv->workqueue, &priv->rf_kill, HZ);
1731                 }
1732
1733                 deferred = 1;
1734         }
1735
1736         /* Turn on the interrupt so that commands can be processed */
1737         ipw2100_enable_interrupts(priv);
1738
1739         /* Send all of the commands that must be sent prior to
1740          * HOST_COMPLETE */
1741         if (ipw2100_adapter_setup(priv)) {
1742                 IPW_DEBUG_ERROR("%s: Failed to start the card.\n",
1743                                 priv->net_dev->name);
1744                 rc = 1;
1745                 goto exit;
1746         }
1747
1748         if (!deferred) {
1749                 /* Enable the adapter - sends HOST_COMPLETE */
1750                 if (ipw2100_enable_adapter(priv)) {
1751                         IPW_DEBUG_ERROR(
1752                                 "%s: failed in call to enable adapter.\n",
1753                                 priv->net_dev->name);
1754                         ipw2100_hw_stop_adapter(priv);
1755                         rc = 1;
1756                         goto exit;
1757                 }
1758
1759
1760                 /* Start a scan . . . */
1761                 ipw2100_set_scan_options(priv);
1762                 ipw2100_start_scan(priv);
1763         }
1764
1765  exit:
1766         return rc;
1767 }
1768
1769 /* Called by register_netdev() */
1770 static int ipw2100_net_init(struct net_device *dev)
1771 {
1772         struct ipw2100_priv *priv = ieee80211_priv(dev);
1773         return ipw2100_up(priv, 1);
1774 }
1775
1776 static void ipw2100_down(struct ipw2100_priv *priv)
1777 {
1778         unsigned long flags;
1779         union iwreq_data wrqu = {
1780                 .ap_addr = {
1781                         .sa_family = ARPHRD_ETHER
1782                 }
1783         };
1784         int associated = priv->status & STATUS_ASSOCIATED;
1785
1786         /* Kill the RF switch timer */
1787         if (!priv->stop_rf_kill) {
1788                 priv->stop_rf_kill = 1;
1789                 cancel_delayed_work(&priv->rf_kill);
1790         }
1791
1792         /* Kill the firmare hang check timer */
1793         if (!priv->stop_hang_check) {
1794                 priv->stop_hang_check = 1;
1795                 cancel_delayed_work(&priv->hang_check);
1796         }
1797
1798         /* Kill any pending resets */
1799         if (priv->status & STATUS_RESET_PENDING)
1800                 cancel_delayed_work(&priv->reset_work);
1801
1802         /* Make sure the interrupt is on so that FW commands will be
1803          * processed correctly */
1804         spin_lock_irqsave(&priv->low_lock, flags);
1805         ipw2100_enable_interrupts(priv);
1806         spin_unlock_irqrestore(&priv->low_lock, flags);
1807
1808         if (ipw2100_hw_stop_adapter(priv))
1809                 IPW_DEBUG_ERROR("%s: Error stopping adapter.\n",
1810                        priv->net_dev->name);
1811
1812         /* Do not disable the interrupt until _after_ we disable
1813          * the adaptor.  Otherwise the CARD_DISABLE command will never
1814          * be ack'd by the firmware */
1815         spin_lock_irqsave(&priv->low_lock, flags);
1816         ipw2100_disable_interrupts(priv);
1817         spin_unlock_irqrestore(&priv->low_lock, flags);
1818
1819 #ifdef ACPI_CSTATE_LIMIT_DEFINED
1820         if (priv->config & CFG_C3_DISABLED) {
1821                 IPW_DEBUG_INFO(DRV_NAME ": Resetting C3 transitions.\n");
1822                 acpi_set_cstate_limit(priv->cstate_limit);
1823                 priv->config &= ~CFG_C3_DISABLED;
1824         }
1825 #endif
1826
1827         /* We have to signal any supplicant if we are disassociating */
1828         if (associated)
1829                 wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
1830
1831         priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
1832         netif_carrier_off(priv->net_dev);
1833         netif_stop_queue(priv->net_dev);
1834 }
1835
1836 void ipw2100_reset_adapter(struct ipw2100_priv *priv)
1837 {
1838         unsigned long flags;
1839         union iwreq_data wrqu = {
1840                 .ap_addr = {
1841                         .sa_family = ARPHRD_ETHER
1842                 }
1843         };
1844         int associated = priv->status & STATUS_ASSOCIATED;
1845
1846         spin_lock_irqsave(&priv->low_lock, flags);
1847         IPW_DEBUG_INFO(DRV_NAME ": %s: Restarting adapter.\n",
1848                        priv->net_dev->name);
1849         priv->resets++;
1850         priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
1851         priv->status |= STATUS_SECURITY_UPDATED;
1852
1853         /* Force a power cycle even if interface hasn't been opened
1854          * yet */
1855         cancel_delayed_work(&priv->reset_work);
1856         priv->status |= STATUS_RESET_PENDING;
1857         spin_unlock_irqrestore(&priv->low_lock, flags);
1858
1859         down(&priv->action_sem);
1860         /* stop timed checks so that they don't interfere with reset */
1861         priv->stop_hang_check = 1;
1862         cancel_delayed_work(&priv->hang_check);
1863
1864         /* We have to signal any supplicant if we are disassociating */
1865         if (associated)
1866                 wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
1867
1868         ipw2100_up(priv, 0);
1869         up(&priv->action_sem);
1870
1871 }
1872
1873
1874 static void isr_indicate_associated(struct ipw2100_priv *priv, u32 status)
1875 {
1876
1877 #define MAC_ASSOCIATION_READ_DELAY (HZ)
1878         int ret, len, essid_len;
1879         char essid[IW_ESSID_MAX_SIZE];
1880         u32 txrate;
1881         u32 chan;
1882         char *txratename;
1883         u8 bssid[ETH_ALEN];
1884
1885         /*
1886          * TBD: BSSID is usually 00:00:00:00:00:00 here and not
1887          *      an actual MAC of the AP. Seems like FW sets this
1888          *      address too late. Read it later and expose through
1889          *      /proc or schedule a later task to query and update
1890          */
1891
1892         essid_len = IW_ESSID_MAX_SIZE;
1893         ret = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_SSID,
1894                                   essid, &essid_len);
1895         if (ret) {
1896                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1897                                    __LINE__);
1898                 return;
1899         }
1900
1901         len = sizeof(u32);
1902         ret = ipw2100_get_ordinal(priv, IPW_ORD_CURRENT_TX_RATE,
1903                                   &txrate, &len);
1904         if (ret) {
1905                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1906                                    __LINE__);
1907                 return;
1908         }
1909
1910         len = sizeof(u32);
1911         ret = ipw2100_get_ordinal(priv, IPW_ORD_OUR_FREQ, &chan, &len);
1912         if (ret) {
1913                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1914                                    __LINE__);
1915                 return;
1916         }
1917         len = ETH_ALEN;
1918         ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_AP_BSSID, &bssid,  &len);
1919         if (ret) {
1920                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1921                                    __LINE__);
1922                 return;
1923         }
1924         memcpy(priv->ieee->bssid, bssid, ETH_ALEN);
1925
1926
1927         switch (txrate) {
1928         case TX_RATE_1_MBIT:
1929                 txratename = "1Mbps";
1930                 break;
1931         case TX_RATE_2_MBIT:
1932                 txratename = "2Mbsp";
1933                 break;
1934         case TX_RATE_5_5_MBIT:
1935                 txratename = "5.5Mbps";
1936                 break;
1937         case TX_RATE_11_MBIT:
1938                 txratename = "11Mbps";
1939                 break;
1940         default:
1941                 IPW_DEBUG_INFO("Unknown rate: %d\n", txrate);
1942                 txratename = "unknown rate";
1943                 break;
1944         }
1945
1946         IPW_DEBUG_INFO("%s: Associated with '%s' at %s, channel %d (BSSID="
1947                        MAC_FMT ")\n",
1948                        priv->net_dev->name, escape_essid(essid, essid_len),
1949                        txratename, chan, MAC_ARG(bssid));
1950
1951         /* now we copy read ssid into dev */
1952         if (!(priv->config & CFG_STATIC_ESSID)) {
1953                 priv->essid_len = min((u8)essid_len, (u8)IW_ESSID_MAX_SIZE);
1954                 memcpy(priv->essid, essid, priv->essid_len);
1955         }
1956         priv->channel = chan;
1957         memcpy(priv->bssid, bssid, ETH_ALEN);
1958
1959         priv->status |= STATUS_ASSOCIATING;
1960         priv->connect_start = get_seconds();
1961
1962         queue_delayed_work(priv->workqueue, &priv->wx_event_work, HZ / 10);
1963 }
1964
1965
1966 int ipw2100_set_essid(struct ipw2100_priv *priv, char *essid,
1967                       int length, int batch_mode)
1968 {
1969         int ssid_len = min(length, IW_ESSID_MAX_SIZE);
1970         struct host_command cmd = {
1971                 .host_command = SSID,
1972                 .host_command_sequence = 0,
1973                 .host_command_length = ssid_len
1974         };
1975         int err;
1976
1977         IPW_DEBUG_HC("SSID: '%s'\n", escape_essid(essid, ssid_len));
1978
1979         if (ssid_len)
1980                 memcpy((char*)cmd.host_command_parameters,
1981                        essid, ssid_len);
1982
1983         if (!batch_mode) {
1984                 err = ipw2100_disable_adapter(priv);
1985                 if (err)
1986                         return err;
1987         }
1988
1989         /* Bug in FW currently doesn't honor bit 0 in SET_SCAN_OPTIONS to
1990          * disable auto association -- so we cheat by setting a bogus SSID */
1991         if (!ssid_len && !(priv->config & CFG_ASSOCIATE)) {
1992                 int i;
1993                 u8 *bogus = (u8*)cmd.host_command_parameters;
1994                 for (i = 0; i < IW_ESSID_MAX_SIZE; i++)
1995                         bogus[i] = 0x18 + i;
1996                 cmd.host_command_length = IW_ESSID_MAX_SIZE;
1997         }
1998
1999         /* NOTE:  We always send the SSID command even if the provided ESSID is
2000          * the same as what we currently think is set. */
2001
2002         err = ipw2100_hw_send_command(priv, &cmd);
2003         if (!err) {
2004                 memset(priv->essid + ssid_len, 0,
2005                        IW_ESSID_MAX_SIZE - ssid_len);
2006                 memcpy(priv->essid, essid, ssid_len);
2007                 priv->essid_len = ssid_len;
2008         }
2009
2010         if (!batch_mode) {
2011                 if (ipw2100_enable_adapter(priv))
2012                         err = -EIO;
2013         }
2014
2015         return err;
2016 }
2017
2018 static void isr_indicate_association_lost(struct ipw2100_priv *priv, u32 status)
2019 {
2020         IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE | IPW_DL_ASSOC,
2021                   "disassociated: '%s' " MAC_FMT " \n",
2022                   escape_essid(priv->essid, priv->essid_len),
2023                   MAC_ARG(priv->bssid));
2024
2025         priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
2026
2027         if (priv->status & STATUS_STOPPING) {
2028                 IPW_DEBUG_INFO("Card is stopping itself, discard ASSN_LOST.\n");
2029                 return;
2030         }
2031
2032         memset(priv->bssid, 0, ETH_ALEN);
2033         memset(priv->ieee->bssid, 0, ETH_ALEN);
2034
2035         netif_carrier_off(priv->net_dev);
2036         netif_stop_queue(priv->net_dev);
2037
2038         if (!(priv->status & STATUS_RUNNING))
2039                 return;
2040
2041         if (priv->status & STATUS_SECURITY_UPDATED)
2042                 queue_work(priv->workqueue, &priv->security_work);
2043
2044         queue_work(priv->workqueue, &priv->wx_event_work);
2045 }
2046
2047 static void isr_indicate_rf_kill(struct ipw2100_priv *priv, u32 status)
2048 {
2049         IPW_DEBUG_INFO("%s: RF Kill state changed to radio OFF.\n",
2050                priv->net_dev->name);
2051
2052         /* RF_KILL is now enabled (else we wouldn't be here) */
2053         priv->status |= STATUS_RF_KILL_HW;
2054
2055 #ifdef ACPI_CSTATE_LIMIT_DEFINED
2056         if (priv->config & CFG_C3_DISABLED) {
2057                 IPW_DEBUG_INFO(DRV_NAME ": Resetting C3 transitions.\n");
2058                 acpi_set_cstate_limit(priv->cstate_limit);
2059                 priv->config &= ~CFG_C3_DISABLED;
2060         }
2061 #endif
2062
2063         /* Make sure the RF Kill check timer is running */
2064         priv->stop_rf_kill = 0;
2065         cancel_delayed_work(&priv->rf_kill);
2066         queue_delayed_work(priv->workqueue, &priv->rf_kill, HZ);
2067 }
2068
2069 static void isr_scan_complete(struct ipw2100_priv *priv, u32 status)
2070 {
2071         IPW_DEBUG_SCAN("scan complete\n");
2072         /* Age the scan results... */
2073         priv->ieee->scans++;
2074         priv->status &= ~STATUS_SCANNING;
2075 }
2076
2077 #ifdef CONFIG_IPW_DEBUG
2078 #define IPW2100_HANDLER(v, f) { v, f, # v }
2079 struct ipw2100_status_indicator {
2080         int status;
2081         void (*cb)(struct ipw2100_priv *priv, u32 status);
2082         char *name;
2083 };
2084 #else
2085 #define IPW2100_HANDLER(v, f) { v, f }
2086 struct ipw2100_status_indicator {
2087         int status;
2088         void (*cb)(struct ipw2100_priv *priv, u32 status);
2089 };
2090 #endif /* CONFIG_IPW_DEBUG */
2091
2092 static void isr_indicate_scanning(struct ipw2100_priv *priv, u32 status)
2093 {
2094         IPW_DEBUG_SCAN("Scanning...\n");
2095         priv->status |= STATUS_SCANNING;
2096 }
2097
2098 const struct ipw2100_status_indicator status_handlers[] = {
2099         IPW2100_HANDLER(IPW_STATE_INITIALIZED, 0),
2100         IPW2100_HANDLER(IPW_STATE_COUNTRY_FOUND, 0),
2101         IPW2100_HANDLER(IPW_STATE_ASSOCIATED, isr_indicate_associated),
2102         IPW2100_HANDLER(IPW_STATE_ASSN_LOST, isr_indicate_association_lost),
2103         IPW2100_HANDLER(IPW_STATE_ASSN_CHANGED, 0),
2104         IPW2100_HANDLER(IPW_STATE_SCAN_COMPLETE, isr_scan_complete),
2105         IPW2100_HANDLER(IPW_STATE_ENTERED_PSP, 0),
2106         IPW2100_HANDLER(IPW_STATE_LEFT_PSP, 0),
2107         IPW2100_HANDLER(IPW_STATE_RF_KILL, isr_indicate_rf_kill),
2108         IPW2100_HANDLER(IPW_STATE_DISABLED, 0),
2109         IPW2100_HANDLER(IPW_STATE_POWER_DOWN, 0),
2110         IPW2100_HANDLER(IPW_STATE_SCANNING, isr_indicate_scanning),
2111         IPW2100_HANDLER(-1, 0)
2112 };
2113
2114
2115 static void isr_status_change(struct ipw2100_priv *priv, int status)
2116 {
2117         int i;
2118
2119         if (status == IPW_STATE_SCANNING &&
2120             priv->status & STATUS_ASSOCIATED &&
2121             !(priv->status & STATUS_SCANNING)) {
2122                 IPW_DEBUG_INFO("Scan detected while associated, with "
2123                                "no scan request.  Restarting firmware.\n");
2124
2125                 /* Wake up any sleeping jobs */
2126                 schedule_reset(priv);
2127         }
2128
2129         for (i = 0; status_handlers[i].status != -1; i++) {
2130                 if (status == status_handlers[i].status) {
2131                         IPW_DEBUG_NOTIF("Status change: %s\n",
2132                                          status_handlers[i].name);
2133                         if (status_handlers[i].cb)
2134                                 status_handlers[i].cb(priv, status);
2135                         priv->wstats.status = status;
2136                         return;
2137                 }
2138         }
2139
2140         IPW_DEBUG_NOTIF("unknown status received: %04x\n", status);
2141 }
2142
2143 static void isr_rx_complete_command(
2144         struct ipw2100_priv *priv,
2145         struct ipw2100_cmd_header *cmd)
2146 {
2147 #ifdef CONFIG_IPW_DEBUG
2148         if (cmd->host_command_reg < ARRAY_SIZE(command_types)) {
2149                 IPW_DEBUG_HC("Command completed '%s (%d)'\n",
2150                              command_types[cmd->host_command_reg],
2151                              cmd->host_command_reg);
2152         }
2153 #endif
2154         if (cmd->host_command_reg == HOST_COMPLETE)
2155                 priv->status |= STATUS_ENABLED;
2156
2157         if (cmd->host_command_reg == CARD_DISABLE)
2158                 priv->status &= ~STATUS_ENABLED;
2159
2160         priv->status &= ~STATUS_CMD_ACTIVE;
2161
2162         wake_up_interruptible(&priv->wait_command_queue);
2163 }
2164
2165 #ifdef CONFIG_IPW_DEBUG
2166 const char *frame_types[] = {
2167         "COMMAND_STATUS_VAL",
2168         "STATUS_CHANGE_VAL",
2169         "P80211_DATA_VAL",
2170         "P8023_DATA_VAL",
2171         "HOST_NOTIFICATION_VAL"
2172 };
2173 #endif
2174
2175
2176 static inline int ipw2100_alloc_skb(
2177         struct ipw2100_priv *priv,
2178         struct ipw2100_rx_packet *packet)
2179 {
2180         packet->skb = dev_alloc_skb(sizeof(struct ipw2100_rx));
2181         if (!packet->skb)
2182                 return -ENOMEM;
2183
2184         packet->rxp = (struct ipw2100_rx *)packet->skb->data;
2185         packet->dma_addr = pci_map_single(priv->pci_dev, packet->skb->data,
2186                                           sizeof(struct ipw2100_rx),
2187                                           PCI_DMA_FROMDEVICE);
2188         /* NOTE: pci_map_single does not return an error code, and 0 is a valid
2189          *       dma_addr */
2190
2191         return 0;
2192 }
2193
2194
2195 #define SEARCH_ERROR   0xffffffff
2196 #define SEARCH_FAIL    0xfffffffe
2197 #define SEARCH_SUCCESS 0xfffffff0
2198 #define SEARCH_DISCARD 0
2199 #define SEARCH_SNAPSHOT 1
2200
2201 #define SNAPSHOT_ADDR(ofs) (priv->snapshot[((ofs) >> 12) & 0xff] + ((ofs) & 0xfff))
2202 static inline int ipw2100_snapshot_alloc(struct ipw2100_priv *priv)
2203 {
2204         int i;
2205         if (priv->snapshot[0])
2206                 return 1;
2207         for (i = 0; i < 0x30; i++) {
2208                 priv->snapshot[i] = (u8*)kmalloc(0x1000, GFP_ATOMIC);
2209                 if (!priv->snapshot[i]) {
2210                         IPW_DEBUG_INFO("%s: Error allocating snapshot "
2211                                "buffer %d\n", priv->net_dev->name, i);
2212                         while (i > 0)
2213                                 kfree(priv->snapshot[--i]);
2214                         priv->snapshot[0] = NULL;
2215                         return 0;
2216                 }
2217         }
2218
2219         return 1;
2220 }
2221
2222 static inline void ipw2100_snapshot_free(struct ipw2100_priv *priv)
2223 {
2224         int i;
2225         if (!priv->snapshot[0])
2226                 return;
2227         for (i = 0; i < 0x30; i++)
2228                 kfree(priv->snapshot[i]);
2229         priv->snapshot[0] = NULL;
2230 }
2231
2232 static inline u32 ipw2100_match_buf(struct ipw2100_priv *priv, u8 *in_buf,
2233                                     size_t len, int mode)
2234 {
2235         u32 i, j;
2236         u32 tmp;
2237         u8 *s, *d;
2238         u32 ret;
2239
2240         s = in_buf;
2241         if (mode == SEARCH_SNAPSHOT) {
2242                 if (!ipw2100_snapshot_alloc(priv))
2243                         mode = SEARCH_DISCARD;
2244         }
2245
2246         for (ret = SEARCH_FAIL, i = 0; i < 0x30000; i += 4) {
2247                 read_nic_dword(priv->net_dev, i, &tmp);
2248                 if (mode == SEARCH_SNAPSHOT)
2249                         *(u32 *)SNAPSHOT_ADDR(i) = tmp;
2250                 if (ret == SEARCH_FAIL) {
2251                         d = (u8*)&tmp;
2252                         for (j = 0; j < 4; j++) {
2253                                 if (*s != *d) {
2254                                         s = in_buf;
2255                                         continue;
2256                                 }
2257
2258                                 s++;
2259                                 d++;
2260
2261                                 if ((s - in_buf) == len)
2262                                         ret = (i + j) - len + 1;
2263                         }
2264                 } else if (mode == SEARCH_DISCARD)
2265                         return ret;
2266         }
2267
2268         return ret;
2269 }
2270
2271 /*
2272  *
2273  * 0) Disconnect the SKB from the firmware (just unmap)
2274  * 1) Pack the ETH header into the SKB
2275  * 2) Pass the SKB to the network stack
2276  *
2277  * When packet is provided by the firmware, it contains the following:
2278  *
2279  * .  ieee80211_hdr
2280  * .  ieee80211_snap_hdr
2281  *
2282  * The size of the constructed ethernet
2283  *
2284  */
2285 #ifdef CONFIG_IPW2100_RX_DEBUG
2286 u8 packet_data[IPW_RX_NIC_BUFFER_LENGTH];
2287 #endif
2288
2289 static inline void ipw2100_corruption_detected(struct ipw2100_priv *priv,
2290                                                int i)
2291 {
2292 #ifdef CONFIG_IPW_DEBUG_C3
2293         struct ipw2100_status *status = &priv->status_queue.drv[i];
2294         u32 match, reg;
2295         int j;
2296 #endif
2297 #ifdef ACPI_CSTATE_LIMIT_DEFINED
2298         int limit;
2299 #endif
2300
2301         IPW_DEBUG_INFO(DRV_NAME ": PCI latency error detected at "
2302                        "0x%04zX.\n", i * sizeof(struct ipw2100_status));
2303
2304 #ifdef ACPI_CSTATE_LIMIT_DEFINED
2305         IPW_DEBUG_INFO(DRV_NAME ": Disabling C3 transitions.\n");
2306         limit = acpi_get_cstate_limit();
2307         if (limit > 2) {
2308                 priv->cstate_limit = limit;
2309                 acpi_set_cstate_limit(2);
2310                 priv->config |= CFG_C3_DISABLED;
2311         }
2312 #endif
2313
2314 #ifdef CONFIG_IPW_DEBUG_C3
2315         /* Halt the fimrware so we can get a good image */
2316         write_register(priv->net_dev, IPW_REG_RESET_REG,
2317                        IPW_AUX_HOST_RESET_REG_STOP_MASTER);
2318         j = 5;
2319         do {
2320                 udelay(IPW_WAIT_RESET_MASTER_ASSERT_COMPLETE_DELAY);
2321                 read_register(priv->net_dev, IPW_REG_RESET_REG, &reg);
2322
2323                 if (reg & IPW_AUX_HOST_RESET_REG_MASTER_DISABLED)
2324                         break;
2325         }  while (j--);
2326
2327         match = ipw2100_match_buf(priv, (u8*)status,
2328                                   sizeof(struct ipw2100_status),
2329                                   SEARCH_SNAPSHOT);
2330         if (match < SEARCH_SUCCESS)
2331                 IPW_DEBUG_INFO("%s: DMA status match in Firmware at "
2332                                "offset 0x%06X, length %d:\n",
2333                                priv->net_dev->name, match,
2334                                sizeof(struct ipw2100_status));
2335         else
2336                 IPW_DEBUG_INFO("%s: No DMA status match in "
2337                                "Firmware.\n", priv->net_dev->name);
2338
2339         printk_buf((u8*)priv->status_queue.drv,
2340                    sizeof(struct ipw2100_status) * RX_QUEUE_LENGTH);
2341 #endif
2342
2343         priv->fatal_error = IPW2100_ERR_C3_CORRUPTION;
2344         priv->ieee->stats.rx_errors++;
2345         schedule_reset(priv);
2346 }
2347
2348 static inline void isr_rx(struct ipw2100_priv *priv, int i,
2349                           struct ieee80211_rx_stats *stats)
2350 {
2351         struct ipw2100_status *status = &priv->status_queue.drv[i];
2352         struct ipw2100_rx_packet *packet = &priv->rx_buffers[i];
2353
2354         IPW_DEBUG_RX("Handler...\n");
2355
2356         if (unlikely(status->frame_size > skb_tailroom(packet->skb))) {
2357                 IPW_DEBUG_INFO("%s: frame_size (%u) > skb_tailroom (%u)!"
2358                                "  Dropping.\n",
2359                                priv->net_dev->name,
2360                                status->frame_size, skb_tailroom(packet->skb));
2361                 priv->ieee->stats.rx_errors++;
2362                 return;
2363         }
2364
2365         if (unlikely(!netif_running(priv->net_dev))) {
2366                 priv->ieee->stats.rx_errors++;
2367                 priv->wstats.discard.misc++;
2368                 IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
2369                 return;
2370         }
2371
2372         if (unlikely(priv->ieee->iw_mode == IW_MODE_MONITOR &&
2373                      status->flags & IPW_STATUS_FLAG_CRC_ERROR)) {
2374                 IPW_DEBUG_RX("CRC error in packet.  Dropping.\n");
2375                 priv->ieee->stats.rx_errors++;
2376                 return;
2377         }
2378
2379         if (unlikely(priv->ieee->iw_mode != IW_MODE_MONITOR &&
2380                 !(priv->status & STATUS_ASSOCIATED))) {
2381                 IPW_DEBUG_DROP("Dropping packet while not associated.\n");
2382                 priv->wstats.discard.misc++;
2383                 return;
2384         }
2385
2386
2387         pci_unmap_single(priv->pci_dev,
2388                          packet->dma_addr,
2389                          sizeof(struct ipw2100_rx),
2390                          PCI_DMA_FROMDEVICE);
2391
2392         skb_put(packet->skb, status->frame_size);
2393
2394 #ifdef CONFIG_IPW2100_RX_DEBUG
2395         /* Make a copy of the frame so we can dump it to the logs if
2396          * ieee80211_rx fails */
2397         memcpy(packet_data, packet->skb->data,
2398                min_t(u32, status->frame_size, IPW_RX_NIC_BUFFER_LENGTH));
2399 #endif
2400
2401         if (!ieee80211_rx(priv->ieee, packet->skb, stats)) {
2402 #ifdef CONFIG_IPW2100_RX_DEBUG
2403                 IPW_DEBUG_DROP("%s: Non consumed packet:\n",
2404                                priv->net_dev->name);
2405                 printk_buf(IPW_DL_DROP, packet_data, status->frame_size);
2406 #endif
2407                 priv->ieee->stats.rx_errors++;
2408
2409                 /* ieee80211_rx failed, so it didn't free the SKB */
2410                 dev_kfree_skb_any(packet->skb);
2411                 packet->skb = NULL;
2412         }
2413
2414         /* We need to allocate a new SKB and attach it to the RDB. */
2415         if (unlikely(ipw2100_alloc_skb(priv, packet))) {
2416                 IPW_DEBUG_WARNING(
2417                         "%s: Unable to allocate SKB onto RBD ring - disabling "
2418                         "adapter.\n", priv->net_dev->name);
2419                 /* TODO: schedule adapter shutdown */
2420                 IPW_DEBUG_INFO("TODO: Shutdown adapter...\n");
2421         }
2422
2423         /* Update the RDB entry */
2424         priv->rx_queue.drv[i].host_addr = packet->dma_addr;
2425 }
2426
2427 static inline int ipw2100_corruption_check(struct ipw2100_priv *priv, int i)
2428 {
2429         struct ipw2100_status *status = &priv->status_queue.drv[i];
2430         struct ipw2100_rx *u = priv->rx_buffers[i].rxp;
2431         u16 frame_type = status->status_fields & STATUS_TYPE_MASK;
2432
2433         switch (frame_type) {
2434         case COMMAND_STATUS_VAL:
2435                 return (status->frame_size != sizeof(u->rx_data.command));
2436         case STATUS_CHANGE_VAL:
2437                 return (status->frame_size != sizeof(u->rx_data.status));
2438         case HOST_NOTIFICATION_VAL:
2439                 return (status->frame_size < sizeof(u->rx_data.notification));
2440         case P80211_DATA_VAL:
2441         case P8023_DATA_VAL:
2442 #ifdef CONFIG_IPW2100_MONITOR
2443                 return 0;
2444 #else
2445                 switch (WLAN_FC_GET_TYPE(u->rx_data.header.frame_ctl)) {
2446                 case IEEE80211_FTYPE_MGMT:
2447                 case IEEE80211_FTYPE_CTL:
2448                         return 0;
2449                 case IEEE80211_FTYPE_DATA:
2450                         return (status->frame_size >
2451                                 IPW_MAX_802_11_PAYLOAD_LENGTH);
2452                 }
2453 #endif
2454         }
2455
2456         return 1;
2457 }
2458
2459 /*
2460  * ipw2100 interrupts are disabled at this point, and the ISR
2461  * is the only code that calls this method.  So, we do not need
2462  * to play with any locks.
2463  *
2464  * RX Queue works as follows:
2465  *
2466  * Read index - firmware places packet in entry identified by the
2467  *              Read index and advances Read index.  In this manner,
2468  *              Read index will always point to the next packet to
2469  *              be filled--but not yet valid.
2470  *
2471  * Write index - driver fills this entry with an unused RBD entry.
2472  *               This entry has not filled by the firmware yet.
2473  *
2474  * In between the W and R indexes are the RBDs that have been received
2475  * but not yet processed.
2476  *
2477  * The process of handling packets will start at WRITE + 1 and advance
2478  * until it reaches the READ index.
2479  *
2480  * The WRITE index is cached in the variable 'priv->rx_queue.next'.
2481  *
2482  */
2483 static inline void __ipw2100_rx_process(struct ipw2100_priv *priv)
2484 {
2485         struct ipw2100_bd_queue *rxq = &priv->rx_queue;
2486         struct ipw2100_status_queue *sq = &priv->status_queue;
2487         struct ipw2100_rx_packet *packet;
2488         u16 frame_type;
2489         u32 r, w, i, s;
2490         struct ipw2100_rx *u;
2491         struct ieee80211_rx_stats stats = {
2492                 .mac_time = jiffies,
2493         };
2494
2495         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_RX_READ_INDEX, &r);
2496         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_RX_WRITE_INDEX, &w);
2497
2498         if (r >= rxq->entries) {
2499                 IPW_DEBUG_RX("exit - bad read index\n");
2500                 return;
2501         }
2502
2503         i = (rxq->next + 1) % rxq->entries;
2504         s = i;
2505         while (i != r) {
2506                 /* IPW_DEBUG_RX("r = %d : w = %d : processing = %d\n",
2507                    r, rxq->next, i); */
2508
2509                 packet = &priv->rx_buffers[i];
2510
2511                 /* Sync the DMA for the STATUS buffer so CPU is sure to get
2512                  * the correct values */
2513                 pci_dma_sync_single_for_cpu(
2514                         priv->pci_dev,
2515                         sq->nic + sizeof(struct ipw2100_status) * i,
2516                         sizeof(struct ipw2100_status),
2517                         PCI_DMA_FROMDEVICE);
2518
2519                 /* Sync the DMA for the RX buffer so CPU is sure to get
2520                  * the correct values */
2521                 pci_dma_sync_single_for_cpu(priv->pci_dev, packet->dma_addr,
2522                                             sizeof(struct ipw2100_rx),
2523                                             PCI_DMA_FROMDEVICE);
2524
2525                 if (unlikely(ipw2100_corruption_check(priv, i))) {
2526                         ipw2100_corruption_detected(priv, i);
2527                         goto increment;
2528                 }
2529
2530                 u = packet->rxp;
2531                 frame_type = sq->drv[i].status_fields &
2532                         STATUS_TYPE_MASK;
2533                 stats.rssi = sq->drv[i].rssi + IPW2100_RSSI_TO_DBM;
2534                 stats.len = sq->drv[i].frame_size;
2535
2536                 stats.mask = 0;
2537                 if (stats.rssi != 0)
2538                         stats.mask |= IEEE80211_STATMASK_RSSI;
2539                 stats.freq = IEEE80211_24GHZ_BAND;
2540
2541                 IPW_DEBUG_RX(
2542                         "%s: '%s' frame type received (%d).\n",
2543                         priv->net_dev->name, frame_types[frame_type],
2544                         stats.len);
2545
2546                 switch (frame_type) {
2547                 case COMMAND_STATUS_VAL:
2548                         /* Reset Rx watchdog */
2549                         isr_rx_complete_command(
2550                                 priv, &u->rx_data.command);
2551                         break;
2552
2553                 case STATUS_CHANGE_VAL:
2554                         isr_status_change(priv, u->rx_data.status);
2555                         break;
2556
2557                 case P80211_DATA_VAL:
2558                 case P8023_DATA_VAL:
2559 #ifdef CONFIG_IPW2100_MONITOR
2560                         if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
2561                                 isr_rx(priv, i, &stats);
2562                                 break;
2563                         }
2564 #endif
2565                         if (stats.len < sizeof(u->rx_data.header))
2566                                 break;
2567                         switch (WLAN_FC_GET_TYPE(u->rx_data.header.
2568                                                  frame_ctl)) {
2569                         case IEEE80211_FTYPE_MGMT:
2570                                 ieee80211_rx_mgt(priv->ieee,
2571                                                  &u->rx_data.header,
2572                                                  &stats);
2573                                 break;
2574
2575                         case IEEE80211_FTYPE_CTL:
2576                                 break;
2577
2578                         case IEEE80211_FTYPE_DATA:
2579                                 isr_rx(priv, i, &stats);
2580                                 break;
2581
2582                         }
2583                         break;
2584                 }
2585
2586         increment:
2587                 /* clear status field associated with this RBD */
2588                 rxq->drv[i].status.info.field = 0;
2589
2590                 i = (i + 1) % rxq->entries;
2591         }
2592
2593         if (i != s) {
2594                 /* backtrack one entry, wrapping to end if at 0 */
2595                 rxq->next = (i ? i : rxq->entries) - 1;
2596
2597                 write_register(priv->net_dev,
2598                                IPW_MEM_HOST_SHARED_RX_WRITE_INDEX,
2599                                rxq->next);
2600         }
2601 }
2602
2603
2604 /*
2605  * __ipw2100_tx_process
2606  *
2607  * This routine will determine whether the next packet on
2608  * the fw_pend_list has been processed by the firmware yet.
2609  *
2610  * If not, then it does nothing and returns.
2611  *
2612  * If so, then it removes the item from the fw_pend_list, frees
2613  * any associated storage, and places the item back on the
2614  * free list of its source (either msg_free_list or tx_free_list)
2615  *
2616  * TX Queue works as follows:
2617  *
2618  * Read index - points to the next TBD that the firmware will
2619  *              process.  The firmware will read the data, and once
2620  *              done processing, it will advance the Read index.
2621  *
2622  * Write index - driver fills this entry with an constructed TBD
2623  *               entry.  The Write index is not advanced until the
2624  *               packet has been configured.
2625  *
2626  * In between the W and R indexes are the TBDs that have NOT been
2627  * processed.  Lagging behind the R index are packets that have
2628  * been processed but have not been freed by the driver.
2629  *
2630  * In order to free old storage, an internal index will be maintained
2631  * that points to the next packet to be freed.  When all used
2632  * packets have been freed, the oldest index will be the same as the
2633  * firmware's read index.
2634  *
2635  * The OLDEST index is cached in the variable 'priv->tx_queue.oldest'
2636  *
2637  * Because the TBD structure can not contain arbitrary data, the
2638  * driver must keep an internal queue of cached allocations such that
2639  * it can put that data back into the tx_free_list and msg_free_list
2640  * for use by future command and data packets.
2641  *
2642  */
2643 static inline int __ipw2100_tx_process(struct ipw2100_priv *priv)
2644 {
2645         struct ipw2100_bd_queue *txq = &priv->tx_queue;
2646         struct ipw2100_bd *tbd;
2647         struct list_head *element;
2648         struct ipw2100_tx_packet *packet;
2649         int descriptors_used;
2650         int e, i;
2651         u32 r, w, frag_num = 0;
2652
2653         if (list_empty(&priv->fw_pend_list))
2654                 return 0;
2655
2656         element = priv->fw_pend_list.next;
2657
2658         packet = list_entry(element, struct ipw2100_tx_packet, list);
2659         tbd = &txq->drv[packet->index];
2660
2661         /* Determine how many TBD entries must be finished... */
2662         switch (packet->type) {
2663         case COMMAND:
2664                 /* COMMAND uses only one slot; don't advance */
2665                 descriptors_used = 1;
2666                 e = txq->oldest;
2667                 break;
2668
2669         case DATA:
2670                 /* DATA uses two slots; advance and loop position. */
2671                 descriptors_used = tbd->num_fragments;
2672                 frag_num = tbd->num_fragments - 1;
2673                 e = txq->oldest + frag_num;
2674                 e %= txq->entries;
2675                 break;
2676
2677         default:
2678                 IPW_DEBUG_WARNING("%s: Bad fw_pend_list entry!\n",
2679                                    priv->net_dev->name);
2680                 return 0;
2681         }
2682
2683         /* if the last TBD is not done by NIC yet, then packet is
2684          * not ready to be released.
2685          *
2686          */
2687         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_TX_QUEUE_READ_INDEX,
2688                       &r);
2689         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX,
2690                       &w);
2691         if (w != txq->next)
2692                 IPW_DEBUG_WARNING("%s: write index mismatch\n",
2693                        priv->net_dev->name);
2694
2695         /*
2696          * txq->next is the index of the last packet written txq->oldest is
2697          * the index of the r is the index of the next packet to be read by
2698          * firmware
2699          */
2700
2701
2702         /*
2703          * Quick graphic to help you visualize the following
2704          * if / else statement
2705          *
2706          * ===>|                     s---->|===============
2707          *                               e>|
2708          * | a | b | c | d | e | f | g | h | i | j | k | l
2709          *       r---->|
2710          *               w
2711          *
2712          * w - updated by driver
2713          * r - updated by firmware
2714          * s - start of oldest BD entry (txq->oldest)
2715          * e - end of oldest BD entry
2716          *
2717          */
2718         if (!((r <= w && (e < r || e >= w)) || (e < r && e >= w))) {
2719                 IPW_DEBUG_TX("exit - no processed packets ready to release.\n");
2720                 return 0;
2721         }
2722
2723         list_del(element);
2724         DEC_STAT(&priv->fw_pend_stat);
2725
2726 #ifdef CONFIG_IPW_DEBUG
2727         {
2728                 int i = txq->oldest;
2729                 IPW_DEBUG_TX(
2730                         "TX%d V=%p P=%04X T=%04X L=%d\n", i,
2731                         &txq->drv[i],
2732                         (u32)(txq->nic + i * sizeof(struct ipw2100_bd)),
2733                         txq->drv[i].host_addr,
2734                         txq->drv[i].buf_length);
2735
2736                 if (packet->type == DATA) {
2737                         i = (i + 1) % txq->entries;
2738
2739                         IPW_DEBUG_TX(
2740                                 "TX%d V=%p P=%04X T=%04X L=%d\n", i,
2741                                 &txq->drv[i],
2742                                 (u32)(txq->nic + i *
2743                                 sizeof(struct ipw2100_bd)),
2744                                 (u32)txq->drv[i].host_addr,
2745                                 txq->drv[i].buf_length);
2746                 }
2747         }
2748 #endif
2749
2750         switch (packet->type) {
2751         case DATA:
2752                 if (txq->drv[txq->oldest].status.info.fields.txType != 0)
2753                         IPW_DEBUG_WARNING("%s: Queue mismatch.  "
2754                                "Expecting DATA TBD but pulled "
2755                                "something else: ids %d=%d.\n",
2756                                priv->net_dev->name, txq->oldest, packet->index);
2757
2758                 /* DATA packet; we have to unmap and free the SKB */
2759                 priv->ieee->stats.tx_packets++;
2760                 for (i = 0; i < frag_num; i++) {
2761                         tbd = &txq->drv[(packet->index + 1 + i) %
2762                                         txq->entries];
2763
2764                         IPW_DEBUG_TX(
2765                                 "TX%d P=%08x L=%d\n",
2766                                 (packet->index + 1 + i) % txq->entries,
2767                                 tbd->host_addr, tbd->buf_length);
2768
2769                         pci_unmap_single(priv->pci_dev,
2770                                          tbd->host_addr,
2771                                          tbd->buf_length,
2772                                          PCI_DMA_TODEVICE);
2773                 }
2774
2775                 priv->ieee->stats.tx_bytes += packet->info.d_struct.txb->payload_size;
2776                 ieee80211_txb_free(packet->info.d_struct.txb);
2777                 packet->info.d_struct.txb = NULL;
2778
2779                 list_add_tail(element, &priv->tx_free_list);
2780                 INC_STAT(&priv->tx_free_stat);
2781
2782                 /* We have a free slot in the Tx queue, so wake up the
2783                  * transmit layer if it is stopped. */
2784                 if (priv->status & STATUS_ASSOCIATED &&
2785                     netif_queue_stopped(priv->net_dev)) {
2786                         IPW_DEBUG_INFO(KERN_INFO
2787                                            "%s: Waking net queue.\n",
2788                                            priv->net_dev->name);
2789                         netif_wake_queue(priv->net_dev);
2790                 }
2791
2792                 /* A packet was processed by the hardware, so update the
2793                  * watchdog */
2794                 priv->net_dev->trans_start = jiffies;
2795
2796                 break;
2797
2798         case COMMAND:
2799                 if (txq->drv[txq->oldest].status.info.fields.txType != 1)
2800                         IPW_DEBUG_WARNING("%s: Queue mismatch.  "
2801                                "Expecting COMMAND TBD but pulled "
2802                                "something else: ids %d=%d.\n",
2803                                priv->net_dev->name, txq->oldest, packet->index);
2804
2805 #ifdef CONFIG_IPW_DEBUG
2806                 if (packet->info.c_struct.cmd->host_command_reg <
2807                     sizeof(command_types) / sizeof(*command_types))
2808                         IPW_DEBUG_TX(
2809                                 "Command '%s (%d)' processed: %d.\n",
2810                                 command_types[packet->info.c_struct.cmd->host_command_reg],
2811                                 packet->info.c_struct.cmd->host_command_reg,
2812                                 packet->info.c_struct.cmd->cmd_status_reg);
2813 #endif
2814
2815                 list_add_tail(element, &priv->msg_free_list);
2816                 INC_STAT(&priv->msg_free_stat);
2817                 break;
2818         }
2819
2820         /* advance oldest used TBD pointer to start of next entry */
2821         txq->oldest = (e + 1) % txq->entries;
2822         /* increase available TBDs number */
2823         txq->available += descriptors_used;
2824         SET_STAT(&priv->txq_stat, txq->available);
2825
2826         IPW_DEBUG_TX("packet latency (send to process)  %ld jiffies\n",
2827                          jiffies - packet->jiffy_start);
2828
2829         return (!list_empty(&priv->fw_pend_list));
2830 }
2831
2832
2833 static inline void __ipw2100_tx_complete(struct ipw2100_priv *priv)
2834 {
2835         int i = 0;
2836
2837         while (__ipw2100_tx_process(priv) && i < 200) i++;
2838
2839         if (i == 200) {
2840                 IPW_DEBUG_WARNING(
2841                        "%s: Driver is running slow (%d iters).\n",
2842                        priv->net_dev->name, i);
2843         }
2844 }
2845
2846
2847 static void X__ipw2100_tx_send_commands(struct ipw2100_priv *priv)
2848 {
2849         struct list_head *element;
2850         struct ipw2100_tx_packet *packet;
2851         struct ipw2100_bd_queue *txq = &priv->tx_queue;
2852         struct ipw2100_bd *tbd;
2853         int next = txq->next;
2854
2855         while (!list_empty(&priv->msg_pend_list)) {
2856                 /* if there isn't enough space in TBD queue, then
2857                  * don't stuff a new one in.
2858                  * NOTE: 3 are needed as a command will take one,
2859                  *       and there is a minimum of 2 that must be
2860                  *       maintained between the r and w indexes
2861                  */
2862                 if (txq->available <= 3) {
2863                         IPW_DEBUG_TX("no room in tx_queue\n");
2864                         break;
2865                 }
2866
2867                 element = priv->msg_pend_list.next;
2868                 list_del(element);
2869                 DEC_STAT(&priv->msg_pend_stat);
2870
2871                 packet = list_entry(element,
2872                                     struct ipw2100_tx_packet, list);
2873
2874                 IPW_DEBUG_TX("using TBD at virt=%p, phys=%p\n",
2875                                  &txq->drv[txq->next],
2876                                  (void*)(txq->nic + txq->next *
2877                                          sizeof(struct ipw2100_bd)));
2878
2879                 packet->index = txq->next;
2880
2881                 tbd = &txq->drv[txq->next];
2882
2883                 /* initialize TBD */
2884                 tbd->host_addr = packet->info.c_struct.cmd_phys;
2885                 tbd->buf_length = sizeof(struct ipw2100_cmd_header);
2886                 /* not marking number of fragments causes problems
2887                  * with f/w debug version */
2888                 tbd->num_fragments = 1;
2889                 tbd->status.info.field =
2890                         IPW_BD_STATUS_TX_FRAME_COMMAND |
2891                         IPW_BD_STATUS_TX_INTERRUPT_ENABLE;
2892
2893                 /* update TBD queue counters */
2894                 txq->next++;
2895                 txq->next %= txq->entries;
2896                 txq->available--;
2897                 DEC_STAT(&priv->txq_stat);
2898
2899                 list_add_tail(element, &priv->fw_pend_list);
2900                 INC_STAT(&priv->fw_pend_stat);
2901         }
2902
2903         if (txq->next != next) {
2904                 /* kick off the DMA by notifying firmware the
2905                  * write index has moved; make sure TBD stores are sync'd */
2906                 wmb();
2907                 write_register(priv->net_dev,
2908                                IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX,
2909                                txq->next);
2910         }
2911 }
2912
2913
2914 /*
2915  * X__ipw2100_tx_send_data
2916  *
2917  */
2918 static void X__ipw2100_tx_send_data(struct ipw2100_priv *priv)
2919 {
2920         struct list_head *element;
2921         struct ipw2100_tx_packet *packet;
2922         struct ipw2100_bd_queue *txq = &priv->tx_queue;
2923         struct ipw2100_bd *tbd;
2924         int next = txq->next;
2925         int i = 0;
2926         struct ipw2100_data_header *ipw_hdr;
2927         struct ieee80211_hdr *hdr;
2928
2929         while (!list_empty(&priv->tx_pend_list)) {
2930                 /* if there isn't enough space in TBD queue, then
2931                  * don't stuff a new one in.
2932                  * NOTE: 4 are needed as a data will take two,
2933                  *       and there is a minimum of 2 that must be
2934                  *       maintained between the r and w indexes
2935                  */
2936                 element = priv->tx_pend_list.next;
2937                 packet = list_entry(element, struct ipw2100_tx_packet, list);
2938
2939                 if (unlikely(1 + packet->info.d_struct.txb->nr_frags >
2940                              IPW_MAX_BDS)) {
2941                         /* TODO: Support merging buffers if more than
2942                          * IPW_MAX_BDS are used */
2943                         IPW_DEBUG_INFO(
2944                                "%s: Maximum BD theshold exceeded.  "
2945                                "Increase fragmentation level.\n",
2946                                priv->net_dev->name);
2947                 }
2948
2949                 if (txq->available <= 3 +
2950                     packet->info.d_struct.txb->nr_frags) {
2951                         IPW_DEBUG_TX("no room in tx_queue\n");
2952                         break;
2953                 }
2954
2955                 list_del(element);
2956                 DEC_STAT(&priv->tx_pend_stat);
2957
2958                 tbd = &txq->drv[txq->next];
2959
2960                 packet->index = txq->next;
2961
2962                 ipw_hdr = packet->info.d_struct.data;
2963                 hdr = (struct ieee80211_hdr *)packet->info.d_struct.txb->
2964                         fragments[0]->data;
2965
2966                 if (priv->ieee->iw_mode == IW_MODE_INFRA) {
2967                         /* To DS: Addr1 = BSSID, Addr2 = SA,
2968                            Addr3 = DA */
2969                         memcpy(ipw_hdr->src_addr, hdr->addr2, ETH_ALEN);
2970                         memcpy(ipw_hdr->dst_addr, hdr->addr3, ETH_ALEN);
2971                 } else if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
2972                         /* not From/To DS: Addr1 = DA, Addr2 = SA,
2973                            Addr3 = BSSID */
2974                         memcpy(ipw_hdr->src_addr, hdr->addr2, ETH_ALEN);
2975                         memcpy(ipw_hdr->dst_addr, hdr->addr1, ETH_ALEN);
2976                 }
2977
2978                 ipw_hdr->host_command_reg = SEND;
2979                 ipw_hdr->host_command_reg1 = 0;
2980
2981                 /* For now we only support host based encryption */
2982                 ipw_hdr->needs_encryption = 0;
2983                 ipw_hdr->encrypted = packet->info.d_struct.txb->encrypted;
2984                 if (packet->info.d_struct.txb->nr_frags > 1)
2985                         ipw_hdr->fragment_size =
2986                                 packet->info.d_struct.txb->frag_size - IEEE80211_3ADDR_LEN;
2987                 else
2988                         ipw_hdr->fragment_size = 0;
2989
2990                 tbd->host_addr = packet->info.d_struct.data_phys;
2991                 tbd->buf_length = sizeof(struct ipw2100_data_header);
2992                 tbd->num_fragments = 1 + packet->info.d_struct.txb->nr_frags;
2993                 tbd->status.info.field =
2994                         IPW_BD_STATUS_TX_FRAME_802_3 |
2995                         IPW_BD_STATUS_TX_FRAME_NOT_LAST_FRAGMENT;
2996                 txq->next++;
2997                 txq->next %= txq->entries;
2998
2999                 IPW_DEBUG_TX(
3000                         "data header tbd TX%d P=%08x L=%d\n",
3001                         packet->index, tbd->host_addr,
3002                         tbd->buf_length);
3003 #ifdef CONFIG_IPW_DEBUG
3004                 if (packet->info.d_struct.txb->nr_frags > 1)
3005                         IPW_DEBUG_FRAG("fragment Tx: %d frames\n",
3006                                        packet->info.d_struct.txb->nr_frags);
3007 #endif
3008
3009                 for (i = 0; i < packet->info.d_struct.txb->nr_frags; i++) {
3010                         tbd = &txq->drv[txq->next];
3011                         if (i == packet->info.d_struct.txb->nr_frags - 1)
3012                                 tbd->status.info.field =
3013                                         IPW_BD_STATUS_TX_FRAME_802_3 |
3014                                         IPW_BD_STATUS_TX_INTERRUPT_ENABLE;
3015                         else
3016                                 tbd->status.info.field =
3017                                         IPW_BD_STATUS_TX_FRAME_802_3 |
3018                                         IPW_BD_STATUS_TX_FRAME_NOT_LAST_FRAGMENT;
3019
3020                         tbd->buf_length = packet->info.d_struct.txb->
3021                                 fragments[i]->len - IEEE80211_3ADDR_LEN;
3022
3023                         tbd->host_addr = pci_map_single(
3024                                 priv->pci_dev,
3025                                 packet->info.d_struct.txb->fragments[i]->data +
3026                                 IEEE80211_3ADDR_LEN,
3027                                 tbd->buf_length,
3028                                 PCI_DMA_TODEVICE);
3029
3030                         IPW_DEBUG_TX(
3031                                 "data frag tbd TX%d P=%08x L=%d\n",
3032                                 txq->next, tbd->host_addr, tbd->buf_length);
3033
3034                         pci_dma_sync_single_for_device(
3035                                 priv->pci_dev, tbd->host_addr,
3036                                 tbd->buf_length,
3037                                 PCI_DMA_TODEVICE);
3038
3039                         txq->next++;
3040                         txq->next %= txq->entries;
3041                 }
3042
3043                 txq->available -= 1 + packet->info.d_struct.txb->nr_frags;
3044                 SET_STAT(&priv->txq_stat, txq->available);
3045
3046                 list_add_tail(element, &priv->fw_pend_list);
3047                 INC_STAT(&priv->fw_pend_stat);
3048         }
3049
3050         if (txq->next != next) {
3051                 /* kick off the DMA by notifying firmware the
3052                  * write index has moved; make sure TBD stores are sync'd */
3053                 write_register(priv->net_dev,
3054                                IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX,
3055                                txq->next);
3056         }
3057         return;
3058 }
3059
3060 static void ipw2100_irq_tasklet(struct ipw2100_priv *priv)
3061 {
3062         struct net_device *dev = priv->net_dev;
3063         unsigned long flags;
3064         u32 inta, tmp;
3065
3066         spin_lock_irqsave(&priv->low_lock, flags);
3067         ipw2100_disable_interrupts(priv);
3068
3069         read_register(dev, IPW_REG_INTA, &inta);
3070
3071         IPW_DEBUG_ISR("enter - INTA: 0x%08lX\n",
3072                       (unsigned long)inta & IPW_INTERRUPT_MASK);
3073
3074         priv->in_isr++;
3075         priv->interrupts++;
3076
3077         /* We do not loop and keep polling for more interrupts as this
3078          * is frowned upon and doesn't play nicely with other potentially
3079          * chained IRQs */
3080         IPW_DEBUG_ISR("INTA: 0x%08lX\n",
3081                       (unsigned long)inta & IPW_INTERRUPT_MASK);
3082
3083         if (inta & IPW2100_INTA_FATAL_ERROR) {
3084                 IPW_DEBUG_WARNING(DRV_NAME
3085                                   ": Fatal interrupt. Scheduling firmware restart.\n");
3086                 priv->inta_other++;
3087                 write_register(
3088                         dev, IPW_REG_INTA,
3089                         IPW2100_INTA_FATAL_ERROR);
3090
3091                 read_nic_dword(dev, IPW_NIC_FATAL_ERROR, &priv->fatal_error);
3092                 IPW_DEBUG_INFO("%s: Fatal error value: 0x%08X\n",
3093                                priv->net_dev->name, priv->fatal_error);
3094
3095                 read_nic_dword(dev, IPW_ERROR_ADDR(priv->fatal_error), &tmp);
3096                 IPW_DEBUG_INFO("%s: Fatal error address value: 0x%08X\n",
3097                                priv->net_dev->name, tmp);
3098
3099                 /* Wake up any sleeping jobs */
3100                 schedule_reset(priv);
3101         }
3102
3103         if (inta & IPW2100_INTA_PARITY_ERROR) {
3104                 IPW_DEBUG_ERROR("***** PARITY ERROR INTERRUPT !!!! \n");
3105                 priv->inta_other++;
3106                 write_register(
3107                         dev, IPW_REG_INTA,
3108                         IPW2100_INTA_PARITY_ERROR);
3109         }
3110
3111         if (inta & IPW2100_INTA_RX_TRANSFER) {
3112                 IPW_DEBUG_ISR("RX interrupt\n");
3113
3114                 priv->rx_interrupts++;
3115
3116                 write_register(
3117                         dev, IPW_REG_INTA,
3118                         IPW2100_INTA_RX_TRANSFER);
3119
3120                 __ipw2100_rx_process(priv);
3121                 __ipw2100_tx_complete(priv);
3122         }
3123
3124         if (inta & IPW2100_INTA_TX_TRANSFER) {
3125                 IPW_DEBUG_ISR("TX interrupt\n");
3126
3127                 priv->tx_interrupts++;
3128
3129                 write_register(dev, IPW_REG_INTA,
3130                                IPW2100_INTA_TX_TRANSFER);
3131
3132                 __ipw2100_tx_complete(priv);
3133                 X__ipw2100_tx_send_commands(priv);
3134                 X__ipw2100_tx_send_data(priv);
3135         }
3136
3137         if (inta & IPW2100_INTA_TX_COMPLETE) {
3138                 IPW_DEBUG_ISR("TX complete\n");
3139                 priv->inta_other++;
3140                 write_register(
3141                         dev, IPW_REG_INTA,
3142                         IPW2100_INTA_TX_COMPLETE);
3143
3144                 __ipw2100_tx_complete(priv);
3145         }
3146
3147         if (inta & IPW2100_INTA_EVENT_INTERRUPT) {
3148                 /* ipw2100_handle_event(dev); */
3149                 priv->inta_other++;
3150                 write_register(
3151                         dev, IPW_REG_INTA,
3152                         IPW2100_INTA_EVENT_INTERRUPT);
3153         }
3154
3155         if (inta & IPW2100_INTA_FW_INIT_DONE) {
3156                 IPW_DEBUG_ISR("FW init done interrupt\n");
3157                 priv->inta_other++;
3158
3159                 read_register(dev, IPW_REG_INTA, &tmp);
3160                 if (tmp & (IPW2100_INTA_FATAL_ERROR |
3161                            IPW2100_INTA_PARITY_ERROR)) {
3162                         write_register(
3163                                 dev, IPW_REG_INTA,
3164                                 IPW2100_INTA_FATAL_ERROR |
3165                                 IPW2100_INTA_PARITY_ERROR);
3166                 }
3167
3168                 write_register(dev, IPW_REG_INTA,
3169                                IPW2100_INTA_FW_INIT_DONE);
3170         }
3171
3172         if (inta & IPW2100_INTA_STATUS_CHANGE) {
3173                 IPW_DEBUG_ISR("Status change interrupt\n");
3174                 priv->inta_other++;
3175                 write_register(
3176                         dev, IPW_REG_INTA,
3177                         IPW2100_INTA_STATUS_CHANGE);
3178         }
3179
3180         if (inta & IPW2100_INTA_SLAVE_MODE_HOST_COMMAND_DONE) {
3181                 IPW_DEBUG_ISR("slave host mode interrupt\n");
3182                 priv->inta_other++;
3183                 write_register(
3184                         dev, IPW_REG_INTA,
3185                         IPW2100_INTA_SLAVE_MODE_HOST_COMMAND_DONE);
3186         }
3187
3188         priv->in_isr--;
3189         ipw2100_enable_interrupts(priv);
3190
3191         spin_unlock_irqrestore(&priv->low_lock, flags);
3192
3193         IPW_DEBUG_ISR("exit\n");
3194 }
3195
3196
3197 static irqreturn_t ipw2100_interrupt(int irq, void *data,
3198                                      struct pt_regs *regs)
3199 {
3200         struct ipw2100_priv *priv = data;
3201         u32 inta, inta_mask;
3202
3203         if (!data)
3204                 return IRQ_NONE;
3205
3206         spin_lock(&priv->low_lock);
3207
3208         /* We check to see if we should be ignoring interrupts before
3209          * we touch the hardware.  During ucode load if we try and handle
3210          * an interrupt we can cause keyboard problems as well as cause
3211          * the ucode to fail to initialize */
3212         if (!(priv->status & STATUS_INT_ENABLED)) {
3213                 /* Shared IRQ */
3214                 goto none;
3215         }
3216
3217         read_register(priv->net_dev, IPW_REG_INTA_MASK, &inta_mask);
3218         read_register(priv->net_dev, IPW_REG_INTA, &inta);
3219
3220         if (inta == 0xFFFFFFFF) {
3221                 /* Hardware disappeared */
3222                 IPW_DEBUG_WARNING("IRQ INTA == 0xFFFFFFFF\n");
3223                 goto none;
3224         }
3225
3226         inta &= IPW_INTERRUPT_MASK;
3227
3228         if (!(inta & inta_mask)) {
3229                 /* Shared interrupt */
3230                 goto none;
3231         }
3232
3233         /* We disable the hardware interrupt here just to prevent unneeded
3234          * calls to be made.  We disable this again within the actual
3235          * work tasklet, so if another part of the code re-enables the
3236          * interrupt, that is fine */
3237         ipw2100_disable_interrupts(priv);
3238
3239         tasklet_schedule(&priv->irq_tasklet);
3240         spin_unlock(&priv->low_lock);
3241
3242         return IRQ_HANDLED;
3243  none:
3244         spin_unlock(&priv->low_lock);
3245         return IRQ_NONE;
3246 }
3247
3248 static int ipw2100_tx(struct ieee80211_txb *txb, struct net_device *dev)
3249 {
3250         struct ipw2100_priv *priv = ieee80211_priv(dev);
3251         struct list_head *element;
3252         struct ipw2100_tx_packet *packet;
3253         unsigned long flags;
3254
3255         spin_lock_irqsave(&priv->low_lock, flags);
3256
3257         if (!(priv->status & STATUS_ASSOCIATED)) {
3258                 IPW_DEBUG_INFO("Can not transmit when not connected.\n");
3259                 priv->ieee->stats.tx_carrier_errors++;
3260                 netif_stop_queue(dev);
3261                 goto fail_unlock;
3262         }
3263
3264         if (list_empty(&priv->tx_free_list))
3265                 goto fail_unlock;
3266
3267         element = priv->tx_free_list.next;
3268         packet = list_entry(element, struct ipw2100_tx_packet, list);
3269
3270         packet->info.d_struct.txb = txb;
3271
3272         IPW_DEBUG_TX("Sending fragment (%d bytes):\n",
3273                          txb->fragments[0]->len);
3274         printk_buf(IPW_DL_TX, txb->fragments[0]->data,
3275                    txb->fragments[0]->len);
3276
3277         packet->jiffy_start = jiffies;
3278
3279         list_del(element);
3280         DEC_STAT(&priv->tx_free_stat);
3281
3282         list_add_tail(element, &priv->tx_pend_list);
3283         INC_STAT(&priv->tx_pend_stat);
3284
3285         X__ipw2100_tx_send_data(priv);
3286
3287         spin_unlock_irqrestore(&priv->low_lock, flags);
3288         return 0;
3289
3290  fail_unlock:
3291         netif_stop_queue(dev);
3292         spin_unlock_irqrestore(&priv->low_lock, flags);
3293         return 1;
3294 }
3295
3296
3297 static int ipw2100_msg_allocate(struct ipw2100_priv *priv)
3298 {
3299         int i, j, err = -EINVAL;
3300         void *v;
3301         dma_addr_t p;
3302
3303         priv->msg_buffers = (struct ipw2100_tx_packet *)kmalloc(
3304                 IPW_COMMAND_POOL_SIZE * sizeof(struct ipw2100_tx_packet),
3305                 GFP_KERNEL);
3306         if (!priv->msg_buffers) {
3307                 IPW_DEBUG_ERROR("%s: PCI alloc failed for msg "
3308                        "buffers.\n", priv->net_dev->name);
3309                 return -ENOMEM;
3310         }
3311
3312         for (i = 0; i < IPW_COMMAND_POOL_SIZE; i++) {
3313                 v = pci_alloc_consistent(
3314                         priv->pci_dev,
3315                         sizeof(struct ipw2100_cmd_header),
3316                         &p);
3317                 if (!v) {
3318                         IPW_DEBUG_ERROR(
3319                                "%s: PCI alloc failed for msg "
3320                                "buffers.\n",
3321                                priv->net_dev->name);
3322                         err = -ENOMEM;
3323                         break;
3324                 }
3325
3326                 memset(v, 0, sizeof(struct ipw2100_cmd_header));
3327
3328                 priv->msg_buffers[i].type = COMMAND;
3329                 priv->msg_buffers[i].info.c_struct.cmd =
3330                         (struct ipw2100_cmd_header*)v;
3331                 priv->msg_buffers[i].info.c_struct.cmd_phys = p;
3332         }
3333
3334         if (i == IPW_COMMAND_POOL_SIZE)
3335                 return 0;
3336
3337         for (j = 0; j < i; j++) {
3338                 pci_free_consistent(
3339                         priv->pci_dev,
3340                         sizeof(struct ipw2100_cmd_header),
3341                         priv->msg_buffers[j].info.c_struct.cmd,
3342                         priv->msg_buffers[j].info.c_struct.cmd_phys);
3343         }
3344
3345         kfree(priv->msg_buffers);
3346         priv->msg_buffers = NULL;
3347
3348         return err;
3349 }
3350
3351 static int ipw2100_msg_initialize(struct ipw2100_priv *priv)
3352 {
3353         int i;
3354
3355         INIT_LIST_HEAD(&priv->msg_free_list);
3356         INIT_LIST_HEAD(&priv->msg_pend_list);
3357
3358         for (i = 0; i < IPW_COMMAND_POOL_SIZE; i++)
3359                 list_add_tail(&priv->msg_buffers[i].list, &priv->msg_free_list);
3360         SET_STAT(&priv->msg_free_stat, i);
3361
3362         return 0;
3363 }
3364
3365 static void ipw2100_msg_free(struct ipw2100_priv *priv)
3366 {
3367         int i;
3368
3369         if (!priv->msg_buffers)
3370                 return;
3371
3372         for (i = 0; i < IPW_COMMAND_POOL_SIZE; i++) {
3373                 pci_free_consistent(priv->pci_dev,
3374                                     sizeof(struct ipw2100_cmd_header),
3375                                     priv->msg_buffers[i].info.c_struct.cmd,
3376                                     priv->msg_buffers[i].info.c_struct.cmd_phys);
3377         }
3378
3379         kfree(priv->msg_buffers);
3380         priv->msg_buffers = NULL;
3381 }
3382
3383 static ssize_t show_pci(struct device *d, struct device_attribute *attr,
3384                         char *buf)
3385 {
3386         struct pci_dev *pci_dev = container_of(d, struct pci_dev, dev);
3387         char *out = buf;
3388         int i, j;
3389         u32 val;
3390
3391         for (i = 0; i < 16; i++) {
3392                 out += sprintf(out, "[%08X] ", i * 16);
3393                 for (j = 0; j < 16; j += 4) {
3394                         pci_read_config_dword(pci_dev, i * 16 + j, &val);
3395                         out += sprintf(out, "%08X ", val);
3396                 }
3397                 out += sprintf(out, "\n");
3398         }
3399
3400         return out - buf;
3401 }
3402 static DEVICE_ATTR(pci, S_IRUGO, show_pci, NULL);
3403
3404 static ssize_t show_cfg(struct device *d, struct device_attribute *attr,
3405                         char *buf)
3406 {
3407         struct ipw2100_priv *p = d->driver_data;
3408         return sprintf(buf, "0x%08x\n", (int)p->config);
3409 }
3410 static DEVICE_ATTR(cfg, S_IRUGO, show_cfg, NULL);
3411
3412 static ssize_t show_status(struct device *d, struct device_attribute *attr,
3413                         char *buf)
3414 {
3415         struct ipw2100_priv *p = d->driver_data;
3416         return sprintf(buf, "0x%08x\n", (int)p->status);
3417 }
3418 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3419
3420 static ssize_t show_capability(struct device *d, struct device_attribute *attr,
3421                                 char *buf)
3422 {
3423         struct ipw2100_priv *p = d->driver_data;
3424         return sprintf(buf, "0x%08x\n", (int)p->capability);
3425 }
3426 static DEVICE_ATTR(capability, S_IRUGO, show_capability, NULL);
3427
3428
3429 #define IPW2100_REG(x) { IPW_ ##x, #x }
3430 const struct {
3431         u32 addr;
3432         const char *name;
3433 } hw_data[] = {
3434         IPW2100_REG(REG_GP_CNTRL),
3435         IPW2100_REG(REG_GPIO),
3436         IPW2100_REG(REG_INTA),
3437         IPW2100_REG(REG_INTA_MASK),
3438         IPW2100_REG(REG_RESET_REG),
3439 };
3440 #define IPW2100_NIC(x, s) { x, #x, s }
3441 const struct {
3442         u32 addr;
3443         const char *name;
3444         size_t size;
3445 } nic_data[] = {
3446         IPW2100_NIC(IPW2100_CONTROL_REG, 2),
3447         IPW2100_NIC(0x210014, 1),
3448         IPW2100_NIC(0x210000, 1),
3449 };
3450 #define IPW2100_ORD(x, d) { IPW_ORD_ ##x, #x, d }
3451 const struct {
3452         u8 index;
3453         const char *name;
3454         const char *desc;
3455 } ord_data[] = {
3456         IPW2100_ORD(STAT_TX_HOST_REQUESTS, "requested Host Tx's (MSDU)"),
3457         IPW2100_ORD(STAT_TX_HOST_COMPLETE, "successful Host Tx's (MSDU)"),
3458         IPW2100_ORD(STAT_TX_DIR_DATA,      "successful Directed Tx's (MSDU)"),
3459         IPW2100_ORD(STAT_TX_DIR_DATA1,     "successful Directed Tx's (MSDU) @ 1MB"),
3460         IPW2100_ORD(STAT_TX_DIR_DATA2,     "successful Directed Tx's (MSDU) @ 2MB"),
3461         IPW2100_ORD(STAT_TX_DIR_DATA5_5,   "successful Directed Tx's (MSDU) @ 5_5MB"),
3462         IPW2100_ORD(STAT_TX_DIR_DATA11,    "successful Directed Tx's (MSDU) @ 11MB"),
3463         IPW2100_ORD(STAT_TX_NODIR_DATA1,   "successful Non_Directed Tx's (MSDU) @ 1MB"),
3464         IPW2100_ORD(STAT_TX_NODIR_DATA2,   "successful Non_Directed Tx's (MSDU) @ 2MB"),
3465         IPW2100_ORD(STAT_TX_NODIR_DATA5_5, "successful Non_Directed Tx's (MSDU) @ 5.5MB"),
3466         IPW2100_ORD(STAT_TX_NODIR_DATA11,  "successful Non_Directed Tx's (MSDU) @ 11MB"),
3467         IPW2100_ORD(STAT_NULL_DATA,        "successful NULL data Tx's"),
3468         IPW2100_ORD(STAT_TX_RTS,           "successful Tx RTS"),
3469         IPW2100_ORD(STAT_TX_CTS,           "successful Tx CTS"),
3470         IPW2100_ORD(STAT_TX_ACK,           "successful Tx ACK"),
3471         IPW2100_ORD(STAT_TX_ASSN,          "successful Association Tx's"),
3472         IPW2100_ORD(STAT_TX_ASSN_RESP,     "successful Association response Tx's"),
3473         IPW2100_ORD(STAT_TX_REASSN,        "successful Reassociation Tx's"),
3474         IPW2100_ORD(STAT_TX_REASSN_RESP,   "successful Reassociation response Tx's"),
3475         IPW2100_ORD(STAT_TX_PROBE,         "probes successfully transmitted"),
3476         IPW2100_ORD(STAT_TX_PROBE_RESP,    "probe responses successfully transmitted"),
3477         IPW2100_ORD(STAT_TX_BEACON,        "tx beacon"),
3478         IPW2100_ORD(STAT_TX_ATIM,          "Tx ATIM"),
3479         IPW2100_ORD(STAT_TX_DISASSN,       "successful Disassociation TX"),
3480         IPW2100_ORD(STAT_TX_AUTH,          "successful Authentication Tx"),
3481         IPW2100_ORD(STAT_TX_DEAUTH,        "successful Deauthentication TX"),
3482         IPW2100_ORD(STAT_TX_TOTAL_BYTES,   "Total successful Tx data bytes"),
3483         IPW2100_ORD(STAT_TX_RETRIES,       "Tx retries"),
3484         IPW2100_ORD(STAT_TX_RETRY1,        "Tx retries at 1MBPS"),
3485         IPW2100_ORD(STAT_TX_RETRY2,        "Tx retries at 2MBPS"),
3486         IPW2100_ORD(STAT_TX_RETRY5_5,      "Tx retries at 5.5MBPS"),
3487         IPW2100_ORD(STAT_TX_RETRY11,       "Tx retries at 11MBPS"),
3488         IPW2100_ORD(STAT_TX_FAILURES,      "Tx Failures"),
3489         IPW2100_ORD(STAT_TX_MAX_TRIES_IN_HOP,"times max tries in a hop failed"),
3490         IPW2100_ORD(STAT_TX_DISASSN_FAIL,       "times disassociation failed"),
3491         IPW2100_ORD(STAT_TX_ERR_CTS,         "missed/bad CTS frames"),
3492         IPW2100_ORD(STAT_TX_ERR_ACK,    "tx err due to acks"),
3493         IPW2100_ORD(STAT_RX_HOST,       "packets passed to host"),
3494         IPW2100_ORD(STAT_RX_DIR_DATA,   "directed packets"),
3495         IPW2100_ORD(STAT_RX_DIR_DATA1,  "directed packets at 1MB"),
3496         IPW2100_ORD(STAT_RX_DIR_DATA2,  "directed packets at 2MB"),
3497         IPW2100_ORD(STAT_RX_DIR_DATA5_5,        "directed packets at 5.5MB"),
3498         IPW2100_ORD(STAT_RX_DIR_DATA11, "directed packets at 11MB"),
3499         IPW2100_ORD(STAT_RX_NODIR_DATA,"nondirected packets"),
3500         IPW2100_ORD(STAT_RX_NODIR_DATA1,        "nondirected packets at 1MB"),
3501         IPW2100_ORD(STAT_RX_NODIR_DATA2,        "nondirected packets at 2MB"),
3502         IPW2100_ORD(STAT_RX_NODIR_DATA5_5,      "nondirected packets at 5.5MB"),
3503         IPW2100_ORD(STAT_RX_NODIR_DATA11,       "nondirected packets at 11MB"),
3504         IPW2100_ORD(STAT_RX_NULL_DATA,  "null data rx's"),
3505         IPW2100_ORD(STAT_RX_RTS,        "Rx RTS"),
3506         IPW2100_ORD(STAT_RX_CTS,        "Rx CTS"),
3507         IPW2100_ORD(STAT_RX_ACK,        "Rx ACK"),
3508         IPW2100_ORD(STAT_RX_CFEND,      "Rx CF End"),
3509         IPW2100_ORD(STAT_RX_CFEND_ACK,  "Rx CF End + CF Ack"),
3510         IPW2100_ORD(STAT_RX_ASSN,       "Association Rx's"),
3511         IPW2100_ORD(STAT_RX_ASSN_RESP,  "Association response Rx's"),
3512         IPW2100_ORD(STAT_RX_REASSN,     "Reassociation Rx's"),
3513         IPW2100_ORD(STAT_RX_REASSN_RESP,        "Reassociation response Rx's"),
3514         IPW2100_ORD(STAT_RX_PROBE,      "probe Rx's"),
3515         IPW2100_ORD(STAT_RX_PROBE_RESP, "probe response Rx's"),
3516         IPW2100_ORD(STAT_RX_BEACON,     "Rx beacon"),
3517         IPW2100_ORD(STAT_RX_ATIM,       "Rx ATIM"),
3518         IPW2100_ORD(STAT_RX_DISASSN,    "disassociation Rx"),
3519         IPW2100_ORD(STAT_RX_AUTH,       "authentication Rx"),
3520         IPW2100_ORD(STAT_RX_DEAUTH,     "deauthentication Rx"),
3521         IPW2100_ORD(STAT_RX_TOTAL_BYTES,"Total rx data bytes received"),
3522         IPW2100_ORD(STAT_RX_ERR_CRC,     "packets with Rx CRC error"),
3523         IPW2100_ORD(STAT_RX_ERR_CRC1,    "Rx CRC errors at 1MB"),
3524         IPW2100_ORD(STAT_RX_ERR_CRC2,    "Rx CRC errors at 2MB"),
3525         IPW2100_ORD(STAT_RX_ERR_CRC5_5,  "Rx CRC errors at 5.5MB"),
3526         IPW2100_ORD(STAT_RX_ERR_CRC11,   "Rx CRC errors at 11MB"),
3527         IPW2100_ORD(STAT_RX_DUPLICATE1, "duplicate rx packets at 1MB"),
3528         IPW2100_ORD(STAT_RX_DUPLICATE2,  "duplicate rx packets at 2MB"),
3529         IPW2100_ORD(STAT_RX_DUPLICATE5_5,        "duplicate rx packets at 5.5MB"),
3530         IPW2100_ORD(STAT_RX_DUPLICATE11,         "duplicate rx packets at 11MB"),
3531         IPW2100_ORD(STAT_RX_DUPLICATE, "duplicate rx packets"),
3532         IPW2100_ORD(PERS_DB_LOCK,       "locking fw permanent  db"),
3533         IPW2100_ORD(PERS_DB_SIZE,       "size of fw permanent  db"),
3534         IPW2100_ORD(PERS_DB_ADDR,       "address of fw permanent  db"),
3535         IPW2100_ORD(STAT_RX_INVALID_PROTOCOL,   "rx frames with invalid protocol"),
3536         IPW2100_ORD(SYS_BOOT_TIME,      "Boot time"),
3537         IPW2100_ORD(STAT_RX_NO_BUFFER,  "rx frames rejected due to no buffer"),
3538         IPW2100_ORD(STAT_RX_MISSING_FRAG,       "rx frames dropped due to missing fragment"),
3539         IPW2100_ORD(STAT_RX_ORPHAN_FRAG,        "rx frames dropped due to non-sequential fragment"),
3540         IPW2100_ORD(STAT_RX_ORPHAN_FRAME,       "rx frames dropped due to unmatched 1st frame"),
3541         IPW2100_ORD(STAT_RX_FRAG_AGEOUT,        "rx frames dropped due to uncompleted frame"),
3542         IPW2100_ORD(STAT_RX_ICV_ERRORS, "ICV errors during decryption"),
3543         IPW2100_ORD(STAT_PSP_SUSPENSION,"times adapter suspended"),
3544         IPW2100_ORD(STAT_PSP_BCN_TIMEOUT,       "beacon timeout"),
3545         IPW2100_ORD(STAT_PSP_POLL_TIMEOUT,      "poll response timeouts"),
3546         IPW2100_ORD(STAT_PSP_NONDIR_TIMEOUT, "timeouts waiting for last {broad,multi}cast pkt"),
3547         IPW2100_ORD(STAT_PSP_RX_DTIMS,  "PSP DTIMs received"),
3548         IPW2100_ORD(STAT_PSP_RX_TIMS,   "PSP TIMs received"),
3549         IPW2100_ORD(STAT_PSP_STATION_ID,"PSP Station ID"),
3550         IPW2100_ORD(LAST_ASSN_TIME,     "RTC time of last association"),
3551         IPW2100_ORD(STAT_PERCENT_MISSED_BCNS,"current calculation of % missed beacons"),
3552         IPW2100_ORD(STAT_PERCENT_RETRIES,"current calculation of % missed tx retries"),
3553         IPW2100_ORD(ASSOCIATED_AP_PTR,  "0 if not associated, else pointer to AP table entry"),
3554         IPW2100_ORD(AVAILABLE_AP_CNT,   "AP's decsribed in the AP table"),
3555         IPW2100_ORD(AP_LIST_PTR,        "Ptr to list of available APs"),
3556         IPW2100_ORD(STAT_AP_ASSNS,      "associations"),
3557         IPW2100_ORD(STAT_ASSN_FAIL,     "association failures"),
3558         IPW2100_ORD(STAT_ASSN_RESP_FAIL,"failures due to response fail"),
3559         IPW2100_ORD(STAT_FULL_SCANS,    "full scans"),
3560         IPW2100_ORD(CARD_DISABLED,      "Card Disabled"),
3561         IPW2100_ORD(STAT_ROAM_INHIBIT,  "times roaming was inhibited due to activity"),
3562         IPW2100_ORD(RSSI_AT_ASSN,       "RSSI of associated AP at time of association"),
3563         IPW2100_ORD(STAT_ASSN_CAUSE1,   "reassociation: no probe response or TX on hop"),
3564         IPW2100_ORD(STAT_ASSN_CAUSE2,   "reassociation: poor tx/rx quality"),
3565         IPW2100_ORD(STAT_ASSN_CAUSE3,   "reassociation: tx/rx quality (excessive AP load"),
3566         IPW2100_ORD(STAT_ASSN_CAUSE4,   "reassociation: AP RSSI level"),
3567         IPW2100_ORD(STAT_ASSN_CAUSE5,   "reassociations due to load leveling"),
3568         IPW2100_ORD(STAT_AUTH_FAIL,     "times authentication failed"),
3569         IPW2100_ORD(STAT_AUTH_RESP_FAIL,"times authentication response failed"),
3570         IPW2100_ORD(STATION_TABLE_CNT,  "entries in association table"),
3571         IPW2100_ORD(RSSI_AVG_CURR,      "Current avg RSSI"),
3572         IPW2100_ORD(POWER_MGMT_MODE,    "Power mode - 0=CAM, 1=PSP"),
3573         IPW2100_ORD(COUNTRY_CODE,       "IEEE country code as recv'd from beacon"),
3574         IPW2100_ORD(COUNTRY_CHANNELS,   "channels suported by country"),
3575         IPW2100_ORD(RESET_CNT,  "adapter resets (warm)"),
3576         IPW2100_ORD(BEACON_INTERVAL,    "Beacon interval"),
3577         IPW2100_ORD(ANTENNA_DIVERSITY,  "TRUE if antenna diversity is disabled"),
3578         IPW2100_ORD(DTIM_PERIOD,        "beacon intervals between DTIMs"),
3579         IPW2100_ORD(OUR_FREQ,   "current radio freq lower digits - channel ID"),
3580         IPW2100_ORD(RTC_TIME,   "current RTC time"),
3581         IPW2100_ORD(PORT_TYPE,  "operating mode"),
3582         IPW2100_ORD(CURRENT_TX_RATE,    "current tx rate"),
3583         IPW2100_ORD(SUPPORTED_RATES,    "supported tx rates"),
3584         IPW2100_ORD(ATIM_WINDOW,        "current ATIM Window"),
3585         IPW2100_ORD(BASIC_RATES,        "basic tx rates"),
3586         IPW2100_ORD(NIC_HIGHEST_RATE,   "NIC highest tx rate"),
3587         IPW2100_ORD(AP_HIGHEST_RATE,    "AP highest tx rate"),
3588         IPW2100_ORD(CAPABILITIES,       "Management frame capability field"),
3589         IPW2100_ORD(AUTH_TYPE,  "Type of authentication"),
3590         IPW2100_ORD(RADIO_TYPE, "Adapter card platform type"),
3591         IPW2100_ORD(RTS_THRESHOLD,      "Min packet length for RTS handshaking"),
3592         IPW2100_ORD(INT_MODE,   "International mode"),
3593         IPW2100_ORD(FRAGMENTATION_THRESHOLD,    "protocol frag threshold"),
3594         IPW2100_ORD(EEPROM_SRAM_DB_BLOCK_START_ADDRESS, "EEPROM offset in SRAM"),
3595         IPW2100_ORD(EEPROM_SRAM_DB_BLOCK_SIZE,  "EEPROM size in SRAM"),
3596         IPW2100_ORD(EEPROM_SKU_CAPABILITY,      "EEPROM SKU Capability"),
3597         IPW2100_ORD(EEPROM_IBSS_11B_CHANNELS,   "EEPROM IBSS 11b channel set"),
3598         IPW2100_ORD(MAC_VERSION,        "MAC Version"),
3599         IPW2100_ORD(MAC_REVISION,       "MAC Revision"),
3600         IPW2100_ORD(RADIO_VERSION,      "Radio Version"),
3601         IPW2100_ORD(NIC_MANF_DATE_TIME, "MANF Date/Time STAMP"),
3602         IPW2100_ORD(UCODE_VERSION,      "Ucode Version"),
3603 };
3604
3605
3606 static ssize_t show_registers(struct device *d, struct device_attribute *attr,
3607                                 char *buf)
3608 {
3609         int i;
3610         struct ipw2100_priv *priv = dev_get_drvdata(d);
3611         struct net_device *dev = priv->net_dev;
3612         char * out = buf;
3613         u32 val = 0;
3614
3615         out += sprintf(out, "%30s [Address ] : Hex\n", "Register");
3616
3617         for (i = 0; i < (sizeof(hw_data) / sizeof(*hw_data)); i++) {
3618                 read_register(dev, hw_data[i].addr, &val);
3619                 out += sprintf(out, "%30s [%08X] : %08X\n",
3620                                hw_data[i].name, hw_data[i].addr, val);
3621         }
3622
3623         return out - buf;
3624 }
3625 static DEVICE_ATTR(registers, S_IRUGO, show_registers, NULL);
3626
3627
3628 static ssize_t show_hardware(struct device *d, struct device_attribute *attr,
3629                                 char *buf)
3630 {
3631         struct ipw2100_priv *priv = dev_get_drvdata(d);
3632         struct net_device *dev = priv->net_dev;
3633         char * out = buf;
3634         int i;
3635
3636         out += sprintf(out, "%30s [Address ] : Hex\n", "NIC entry");
3637
3638         for (i = 0; i < (sizeof(nic_data) / sizeof(*nic_data)); i++) {
3639                 u8 tmp8;
3640                 u16 tmp16;
3641                 u32 tmp32;
3642
3643                 switch (nic_data[i].size) {
3644                 case 1:
3645                         read_nic_byte(dev, nic_data[i].addr, &tmp8);
3646                         out += sprintf(out, "%30s [%08X] : %02X\n",
3647                                        nic_data[i].name, nic_data[i].addr,
3648                                        tmp8);
3649                         break;
3650                 case 2:
3651                         read_nic_word(dev, nic_data[i].addr, &tmp16);
3652                         out += sprintf(out, "%30s [%08X] : %04X\n",
3653                                        nic_data[i].name, nic_data[i].addr,
3654                                        tmp16);
3655                         break;
3656                 case 4:
3657                         read_nic_dword(dev, nic_data[i].addr, &tmp32);
3658                         out += sprintf(out, "%30s [%08X] : %08X\n",
3659                                        nic_data[i].name, nic_data[i].addr,
3660                                        tmp32);
3661                         break;
3662                 }
3663         }
3664         return out - buf;
3665 }
3666 static DEVICE_ATTR(hardware, S_IRUGO, show_hardware, NULL);
3667
3668
3669 static ssize_t show_memory(struct device *d, struct device_attribute *attr,
3670                                 char *buf)
3671 {
3672         struct ipw2100_priv *priv = dev_get_drvdata(d);
3673         struct net_device *dev = priv->net_dev;
3674         static unsigned long loop = 0;
3675         int len = 0;
3676         u32 buffer[4];
3677         int i;
3678         char line[81];
3679
3680         if (loop >= 0x30000)
3681                 loop = 0;
3682
3683         /* sysfs provides us PAGE_SIZE buffer */
3684         while (len < PAGE_SIZE - 128 && loop < 0x30000) {
3685
3686                 if (priv->snapshot[0]) for (i = 0; i < 4; i++)
3687                         buffer[i] = *(u32 *)SNAPSHOT_ADDR(loop + i * 4);
3688                 else for (i = 0; i < 4; i++)
3689                         read_nic_dword(dev, loop + i * 4, &buffer[i]);
3690
3691                 if (priv->dump_raw)
3692                         len += sprintf(buf + len,
3693                                        "%c%c%c%c"
3694                                        "%c%c%c%c"
3695                                        "%c%c%c%c"
3696                                        "%c%c%c%c",
3697                                        ((u8*)buffer)[0x0],
3698                                        ((u8*)buffer)[0x1],
3699                                        ((u8*)buffer)[0x2],
3700                                        ((u8*)buffer)[0x3],
3701                                        ((u8*)buffer)[0x4],
3702                                        ((u8*)buffer)[0x5],
3703                                        ((u8*)buffer)[0x6],
3704                                        ((u8*)buffer)[0x7],
3705                                        ((u8*)buffer)[0x8],
3706                                        ((u8*)buffer)[0x9],
3707                                        ((u8*)buffer)[0xa],
3708                                        ((u8*)buffer)[0xb],
3709                                        ((u8*)buffer)[0xc],
3710                                        ((u8*)buffer)[0xd],
3711                                        ((u8*)buffer)[0xe],
3712                                        ((u8*)buffer)[0xf]);
3713                 else
3714                         len += sprintf(buf + len, "%s\n",
3715                                        snprint_line(line, sizeof(line),
3716                                                     (u8*)buffer, 16, loop));
3717                 loop += 16;
3718         }
3719
3720         return len;
3721 }
3722
3723 static ssize_t store_memory(struct device *d, struct device_attribute *attr,
3724                                 const char *buf, size_t count)
3725 {
3726         struct ipw2100_priv *priv = dev_get_drvdata(d);
3727         struct net_device *dev = priv->net_dev;
3728         const char *p = buf;
3729
3730         if (count < 1)
3731                 return count;
3732
3733         if (p[0] == '1' ||
3734             (count >= 2 && tolower(p[0]) == 'o' && tolower(p[1]) == 'n')) {
3735                 IPW_DEBUG_INFO("%s: Setting memory dump to RAW mode.\n",
3736                        dev->name);
3737                 priv->dump_raw = 1;
3738
3739         } else if (p[0] == '0' || (count >= 2 && tolower(p[0]) == 'o' &&
3740                                   tolower(p[1]) == 'f')) {
3741                 IPW_DEBUG_INFO("%s: Setting memory dump to HEX mode.\n",
3742                        dev->name);
3743                 priv->dump_raw = 0;
3744
3745         } else if (tolower(p[0]) == 'r') {
3746                 IPW_DEBUG_INFO("%s: Resetting firmware snapshot.\n",
3747                        dev->name);
3748                 ipw2100_snapshot_free(priv);
3749
3750         } else
3751                 IPW_DEBUG_INFO("%s: Usage: 0|on = HEX, 1|off = RAW, "
3752                        "reset = clear memory snapshot\n",
3753                        dev->name);
3754
3755         return count;
3756 }
3757 static DEVICE_ATTR(memory, S_IWUSR|S_IRUGO, show_memory, store_memory);
3758
3759
3760 static ssize_t show_ordinals(struct device *d, struct device_attribute *attr,
3761                                 char *buf)
3762 {
3763         struct ipw2100_priv *priv = dev_get_drvdata(d);
3764         u32 val = 0;
3765         int len = 0;
3766         u32 val_len;
3767         static int loop = 0;
3768
3769         if (loop >= sizeof(ord_data) / sizeof(*ord_data))
3770                 loop = 0;
3771
3772         /* sysfs provides us PAGE_SIZE buffer */
3773         while (len < PAGE_SIZE - 128 &&
3774                loop < (sizeof(ord_data) / sizeof(*ord_data))) {
3775
3776                 val_len = sizeof(u32);
3777
3778                 if (ipw2100_get_ordinal(priv, ord_data[loop].index, &val,
3779                                         &val_len))
3780                         len += sprintf(buf + len, "[0x%02X] = ERROR    %s\n",
3781                                        ord_data[loop].index,
3782                                        ord_data[loop].desc);
3783                 else
3784                         len += sprintf(buf + len, "[0x%02X] = 0x%08X %s\n",
3785                                        ord_data[loop].index, val,
3786                                        ord_data[loop].desc);
3787                 loop++;
3788         }
3789
3790         return len;
3791 }
3792 static DEVICE_ATTR(ordinals, S_IRUGO, show_ordinals, NULL);
3793
3794
3795 static ssize_t show_stats(struct device *d, struct device_attribute *attr,
3796                                 char *buf)
3797 {
3798         struct ipw2100_priv *priv = dev_get_drvdata(d);
3799         char * out = buf;
3800
3801         out += sprintf(out, "interrupts: %d {tx: %d, rx: %d, other: %d}\n",
3802                        priv->interrupts, priv->tx_interrupts,
3803                        priv->rx_interrupts, priv->inta_other);
3804         out += sprintf(out, "firmware resets: %d\n", priv->resets);
3805         out += sprintf(out, "firmware hangs: %d\n", priv->hangs);
3806 #ifdef CONFIG_IPW_DEBUG
3807         out += sprintf(out, "packet mismatch image: %s\n",
3808                        priv->snapshot[0] ? "YES" : "NO");
3809 #endif
3810
3811         return out - buf;
3812 }
3813 static DEVICE_ATTR(stats, S_IRUGO, show_stats, NULL);
3814
3815
3816 int ipw2100_switch_mode(struct ipw2100_priv *priv, u32 mode)
3817 {
3818         int err;
3819
3820         if (mode == priv->ieee->iw_mode)
3821                 return 0;
3822
3823         err = ipw2100_disable_adapter(priv);
3824         if (err) {
3825                 IPW_DEBUG_ERROR("%s: Could not disable adapter %d\n",
3826                        priv->net_dev->name, err);
3827                 return err;
3828         }
3829
3830         switch (mode) {
3831         case IW_MODE_INFRA:
3832                 priv->net_dev->type = ARPHRD_ETHER;
3833                 break;
3834         case IW_MODE_ADHOC:
3835                 priv->net_dev->type = ARPHRD_ETHER;
3836                 break;
3837 #ifdef CONFIG_IPW2100_MONITOR
3838         case IW_MODE_MONITOR:
3839                 priv->last_mode = priv->ieee->iw_mode;
3840                 priv->net_dev->type = ARPHRD_IEEE80211;
3841                 break;
3842 #endif /* CONFIG_IPW2100_MONITOR */
3843         }
3844
3845         priv->ieee->iw_mode = mode;
3846
3847 #ifdef CONFIG_PM
3848         /* Indicate ipw2100_download_firmware download firmware
3849          * from disk instead of memory. */
3850         ipw2100_firmware.version = 0;
3851 #endif
3852
3853         printk(KERN_INFO "%s: Reseting on mode change.\n",
3854                 priv->net_dev->name);
3855         priv->reset_backoff = 0;
3856         schedule_reset(priv);
3857
3858         return 0;
3859 }
3860
3861 static ssize_t show_internals(struct device *d, struct device_attribute *attr,
3862                                 char *buf)
3863 {
3864         struct ipw2100_priv *priv = dev_get_drvdata(d);
3865         int len = 0;
3866
3867 #define DUMP_VAR(x,y) len += sprintf(buf + len, # x ": %" # y "\n", priv-> x)
3868
3869         if (priv->status & STATUS_ASSOCIATED)
3870                 len += sprintf(buf + len, "connected: %lu\n",
3871                                get_seconds() - priv->connect_start);
3872         else
3873                 len += sprintf(buf + len, "not connected\n");
3874
3875         DUMP_VAR(ieee->crypt[priv->ieee->tx_keyidx], p);
3876         DUMP_VAR(status, 08lx);
3877         DUMP_VAR(config, 08lx);
3878         DUMP_VAR(capability, 08lx);
3879
3880         len += sprintf(buf + len, "last_rtc: %lu\n", (unsigned long)priv->last_rtc);
3881
3882         DUMP_VAR(fatal_error, d);
3883         DUMP_VAR(stop_hang_check, d);
3884         DUMP_VAR(stop_rf_kill, d);
3885         DUMP_VAR(messages_sent, d);
3886
3887         DUMP_VAR(tx_pend_stat.value, d);
3888         DUMP_VAR(tx_pend_stat.hi, d);
3889
3890         DUMP_VAR(tx_free_stat.value, d);
3891         DUMP_VAR(tx_free_stat.lo, d);
3892
3893         DUMP_VAR(msg_free_stat.value, d);
3894         DUMP_VAR(msg_free_stat.lo, d);
3895
3896         DUMP_VAR(msg_pend_stat.value, d);
3897         DUMP_VAR(msg_pend_stat.hi, d);
3898
3899         DUMP_VAR(fw_pend_stat.value, d);
3900         DUMP_VAR(fw_pend_stat.hi, d);
3901
3902         DUMP_VAR(txq_stat.value, d);
3903         DUMP_VAR(txq_stat.lo, d);
3904
3905         DUMP_VAR(ieee->scans, d);
3906         DUMP_VAR(reset_backoff, d);
3907
3908         return len;
3909 }
3910 static DEVICE_ATTR(internals, S_IRUGO, show_internals, NULL);
3911
3912
3913 static ssize_t show_bssinfo(struct device *d, struct device_attribute *attr,
3914                                 char *buf)
3915 {
3916         struct ipw2100_priv *priv = dev_get_drvdata(d);
3917         char essid[IW_ESSID_MAX_SIZE + 1];
3918         u8 bssid[ETH_ALEN];
3919         u32 chan = 0;
3920         char * out = buf;
3921         int length;
3922         int ret;
3923
3924         memset(essid, 0, sizeof(essid));
3925         memset(bssid, 0, sizeof(bssid));
3926
3927         length = IW_ESSID_MAX_SIZE;
3928         ret = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_SSID, essid, &length);
3929         if (ret)
3930                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
3931                                __LINE__);
3932
3933         length = sizeof(bssid);
3934         ret = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_AP_BSSID,
3935                                   bssid, &length);
3936         if (ret)
3937                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
3938                                __LINE__);
3939
3940         length = sizeof(u32);
3941         ret = ipw2100_get_ordinal(priv, IPW_ORD_OUR_FREQ, &chan, &length);
3942         if (ret)
3943                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
3944                                __LINE__);
3945
3946         out += sprintf(out, "ESSID: %s\n", essid);
3947         out += sprintf(out, "BSSID:   %02x:%02x:%02x:%02x:%02x:%02x\n",
3948                        bssid[0], bssid[1], bssid[2],
3949                        bssid[3], bssid[4], bssid[5]);
3950         out += sprintf(out, "Channel: %d\n", chan);
3951
3952         return out - buf;
3953 }
3954 static DEVICE_ATTR(bssinfo, S_IRUGO, show_bssinfo, NULL);
3955
3956
3957 #ifdef CONFIG_IPW_DEBUG
3958 static ssize_t show_debug_level(struct device_driver *d, char *buf)
3959 {
3960         return sprintf(buf, "0x%08X\n", ipw2100_debug_level);
3961 }
3962
3963 static ssize_t store_debug_level(struct device_driver *d, const char *buf,
3964                                  size_t count)
3965 {
3966         char *p = (char *)buf;
3967         u32 val;
3968
3969         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
3970                 p++;
3971                 if (p[0] == 'x' || p[0] == 'X')
3972                         p++;
3973                 val = simple_strtoul(p, &p, 16);
3974         } else
3975                 val = simple_strtoul(p, &p, 10);
3976         if (p == buf)
3977                 IPW_DEBUG_INFO(DRV_NAME
3978                        ": %s is not in hex or decimal form.\n", buf);
3979         else
3980                 ipw2100_debug_level = val;
3981
3982         return strnlen(buf, count);
3983 }
3984 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO, show_debug_level,
3985                    store_debug_level);
3986 #endif /* CONFIG_IPW_DEBUG */
3987
3988
3989 static ssize_t show_fatal_error(struct device *d,
3990                         struct device_attribute *attr, char *buf)
3991 {
3992         struct ipw2100_priv *priv = dev_get_drvdata(d);
3993         char *out = buf;
3994         int i;
3995
3996         if (priv->fatal_error)
3997                 out += sprintf(out, "0x%08X\n",
3998                                priv->fatal_error);
3999         else
4000                 out += sprintf(out, "0\n");
4001
4002         for (i = 1; i <= IPW2100_ERROR_QUEUE; i++) {
4003                 if (!priv->fatal_errors[(priv->fatal_index - i) %
4004                                         IPW2100_ERROR_QUEUE])
4005                         continue;
4006
4007                 out += sprintf(out, "%d. 0x%08X\n", i,
4008                                priv->fatal_errors[(priv->fatal_index - i) %
4009                                                   IPW2100_ERROR_QUEUE]);
4010         }
4011
4012         return out - buf;
4013 }
4014
4015 static ssize_t store_fatal_error(struct device *d,
4016                 struct device_attribute *attr, const char *buf, size_t count)
4017 {
4018         struct ipw2100_priv *priv = dev_get_drvdata(d);
4019         schedule_reset(priv);
4020         return count;
4021 }
4022 static DEVICE_ATTR(fatal_error, S_IWUSR|S_IRUGO, show_fatal_error, store_fatal_error);
4023
4024
4025 static ssize_t show_scan_age(struct device *d, struct device_attribute *attr,
4026                                 char *buf)
4027 {
4028         struct ipw2100_priv *priv = dev_get_drvdata(d);
4029         return sprintf(buf, "%d\n", priv->ieee->scan_age);
4030 }
4031
4032 static ssize_t store_scan_age(struct device *d, struct device_attribute *attr,
4033                                 const char *buf, size_t count)
4034 {
4035         struct ipw2100_priv *priv = dev_get_drvdata(d);
4036         struct net_device *dev = priv->net_dev;
4037         char buffer[] = "00000000";
4038         unsigned long len =
4039             (sizeof(buffer) - 1) > count ? count : sizeof(buffer) - 1;
4040         unsigned long val;
4041         char *p = buffer;
4042
4043         IPW_DEBUG_INFO("enter\n");
4044
4045         strncpy(buffer, buf, len);
4046         buffer[len] = 0;
4047
4048         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
4049                 p++;
4050                 if (p[0] == 'x' || p[0] == 'X')
4051                         p++;
4052                 val = simple_strtoul(p, &p, 16);
4053         } else
4054                 val = simple_strtoul(p, &p, 10);
4055         if (p == buffer) {
4056                 IPW_DEBUG_INFO("%s: user supplied invalid value.\n",
4057                        dev->name);
4058         } else {
4059                 priv->ieee->scan_age = val;
4060                 IPW_DEBUG_INFO("set scan_age = %u\n", priv->ieee->scan_age);
4061         }
4062
4063         IPW_DEBUG_INFO("exit\n");
4064         return len;
4065 }
4066 static DEVICE_ATTR(scan_age, S_IWUSR | S_IRUGO, show_scan_age, store_scan_age);
4067
4068
4069 static ssize_t show_rf_kill(struct device *d, struct device_attribute *attr,
4070                                 char *buf)
4071 {
4072         /* 0 - RF kill not enabled
4073            1 - SW based RF kill active (sysfs)
4074            2 - HW based RF kill active
4075            3 - Both HW and SW baed RF kill active */
4076         struct ipw2100_priv *priv = (struct ipw2100_priv *)d->driver_data;
4077         int val = ((priv->status & STATUS_RF_KILL_SW) ? 0x1 : 0x0) |
4078                 (rf_kill_active(priv) ? 0x2 : 0x0);
4079         return sprintf(buf, "%i\n", val);
4080 }
4081
4082 static int ipw_radio_kill_sw(struct ipw2100_priv *priv, int disable_radio)
4083 {
4084         if ((disable_radio ? 1 : 0) ==
4085             (priv->status & STATUS_RF_KILL_SW ? 1 : 0))
4086                 return 0 ;
4087
4088         IPW_DEBUG_RF_KILL("Manual SW RF Kill set to: RADIO  %s\n",
4089                           disable_radio ? "OFF" : "ON");
4090
4091         down(&priv->action_sem);
4092
4093         if (disable_radio) {
4094                 priv->status |= STATUS_RF_KILL_SW;
4095                 ipw2100_down(priv);
4096         } else {
4097                 priv->status &= ~STATUS_RF_KILL_SW;
4098                 if (rf_kill_active(priv)) {
4099                         IPW_DEBUG_RF_KILL("Can not turn radio back on - "
4100                                           "disabled by HW switch\n");
4101                         /* Make sure the RF_KILL check timer is running */
4102                         priv->stop_rf_kill = 0;
4103                         cancel_delayed_work(&priv->rf_kill);
4104                         queue_delayed_work(priv->workqueue, &priv->rf_kill,
4105                                            HZ);
4106                 } else
4107                         schedule_reset(priv);
4108         }
4109
4110         up(&priv->action_sem);
4111         return 1;
4112 }
4113
4114 static ssize_t store_rf_kill(struct device *d, struct device_attribute *attr,
4115                                 const char *buf, size_t count)
4116 {
4117         struct ipw2100_priv *priv = dev_get_drvdata(d);
4118         ipw_radio_kill_sw(priv, buf[0] == '1');
4119         return count;
4120 }
4121 static DEVICE_ATTR(rf_kill, S_IWUSR|S_IRUGO, show_rf_kill, store_rf_kill);
4122
4123
4124 static struct attribute *ipw2100_sysfs_entries[] = {
4125         &dev_attr_hardware.attr,
4126         &dev_attr_registers.attr,
4127         &dev_attr_ordinals.attr,
4128         &dev_attr_pci.attr,
4129         &dev_attr_stats.attr,
4130         &dev_attr_internals.attr,
4131         &dev_attr_bssinfo.attr,
4132         &dev_attr_memory.attr,
4133         &dev_attr_scan_age.attr,
4134         &dev_attr_fatal_error.attr,
4135         &dev_attr_rf_kill.attr,
4136         &dev_attr_cfg.attr,
4137         &dev_attr_status.attr,
4138         &dev_attr_capability.attr,
4139         NULL,
4140 };
4141
4142 static struct attribute_group ipw2100_attribute_group = {
4143         .attrs = ipw2100_sysfs_entries,
4144 };
4145
4146
4147 static int status_queue_allocate(struct ipw2100_priv *priv, int entries)
4148 {
4149         struct ipw2100_status_queue *q = &priv->status_queue;
4150
4151         IPW_DEBUG_INFO("enter\n");
4152
4153         q->size = entries * sizeof(struct ipw2100_status);
4154         q->drv = (struct ipw2100_status *)pci_alloc_consistent(
4155                 priv->pci_dev, q->size, &q->nic);
4156         if (!q->drv) {
4157                 IPW_DEBUG_WARNING(
4158                        "Can not allocate status queue.\n");
4159                 return -ENOMEM;
4160         }
4161
4162         memset(q->drv, 0, q->size);
4163
4164         IPW_DEBUG_INFO("exit\n");
4165
4166         return 0;
4167 }
4168
4169 static void status_queue_free(struct ipw2100_priv *priv)
4170 {
4171         IPW_DEBUG_INFO("enter\n");
4172
4173         if (priv->status_queue.drv) {
4174                 pci_free_consistent(
4175                         priv->pci_dev, priv->status_queue.size,
4176                         priv->status_queue.drv, priv->status_queue.nic);
4177                 priv->status_queue.drv = NULL;
4178         }
4179
4180         IPW_DEBUG_INFO("exit\n");
4181 }
4182
4183 static int bd_queue_allocate(struct ipw2100_priv *priv,
4184                              struct ipw2100_bd_queue *q, int entries)
4185 {
4186         IPW_DEBUG_INFO("enter\n");
4187
4188         memset(q, 0, sizeof(struct ipw2100_bd_queue));
4189
4190         q->entries = entries;
4191         q->size = entries * sizeof(struct ipw2100_bd);
4192         q->drv = pci_alloc_consistent(priv->pci_dev, q->size, &q->nic);
4193         if (!q->drv) {
4194                 IPW_DEBUG_INFO("can't allocate shared memory for buffer descriptors\n");
4195                 return -ENOMEM;
4196         }
4197         memset(q->drv, 0, q->size);
4198
4199         IPW_DEBUG_INFO("exit\n");
4200
4201         return 0;
4202 }
4203
4204 static void bd_queue_free(struct ipw2100_priv *priv,
4205                           struct ipw2100_bd_queue *q)
4206 {
4207         IPW_DEBUG_INFO("enter\n");
4208
4209         if (!q)
4210                 return;
4211
4212         if (q->drv) {
4213                 pci_free_consistent(priv->pci_dev,
4214                                     q->size, q->drv, q->nic);
4215                 q->drv = NULL;
4216         }
4217
4218         IPW_DEBUG_INFO("exit\n");
4219 }
4220
4221 static void bd_queue_initialize(
4222         struct ipw2100_priv *priv, struct ipw2100_bd_queue * q,
4223         u32 base, u32 size, u32 r, u32 w)
4224 {
4225         IPW_DEBUG_INFO("enter\n");
4226
4227         IPW_DEBUG_INFO("initializing bd queue at virt=%p, phys=%08x\n", q->drv, (u32)q->nic);
4228
4229         write_register(priv->net_dev, base, q->nic);
4230         write_register(priv->net_dev, size, q->entries);
4231         write_register(priv->net_dev, r, q->oldest);
4232         write_register(priv->net_dev, w, q->next);
4233
4234         IPW_DEBUG_INFO("exit\n");
4235 }
4236
4237 static void ipw2100_kill_workqueue(struct ipw2100_priv *priv)
4238 {
4239         if (priv->workqueue) {
4240                 priv->stop_rf_kill = 1;
4241                 priv->stop_hang_check = 1;
4242                 cancel_delayed_work(&priv->reset_work);
4243                 cancel_delayed_work(&priv->security_work);
4244                 cancel_delayed_work(&priv->wx_event_work);
4245                 cancel_delayed_work(&priv->hang_check);
4246                 cancel_delayed_work(&priv->rf_kill);
4247                 destroy_workqueue(priv->workqueue);
4248                 priv->workqueue = NULL;
4249         }
4250 }
4251
4252 static int ipw2100_tx_allocate(struct ipw2100_priv *priv)
4253 {
4254         int i, j, err = -EINVAL;
4255         void *v;
4256         dma_addr_t p;
4257
4258         IPW_DEBUG_INFO("enter\n");
4259
4260         err = bd_queue_allocate(priv, &priv->tx_queue, TX_QUEUE_LENGTH);
4261         if (err) {
4262                 IPW_DEBUG_ERROR("%s: failed bd_queue_allocate\n",
4263                        priv->net_dev->name);
4264                 return err;
4265         }
4266
4267         priv->tx_buffers = (struct ipw2100_tx_packet *)kmalloc(
4268                 TX_PENDED_QUEUE_LENGTH * sizeof(struct ipw2100_tx_packet),
4269                 GFP_ATOMIC);
4270         if (!priv->tx_buffers) {
4271                 IPW_DEBUG_ERROR("%s: alloc failed form tx buffers.\n",
4272                        priv->net_dev->name);
4273                 bd_queue_free(priv, &priv->tx_queue);
4274                 return -ENOMEM;
4275         }
4276
4277         for (i = 0; i < TX_PENDED_QUEUE_LENGTH; i++) {
4278                 v = pci_alloc_consistent(
4279                         priv->pci_dev, sizeof(struct ipw2100_data_header), &p);
4280                 if (!v) {
4281                         IPW_DEBUG_ERROR("%s: PCI alloc failed for tx "
4282                                "buffers.\n", priv->net_dev->name);
4283                         err = -ENOMEM;
4284                         break;
4285                 }
4286
4287                 priv->tx_buffers[i].type = DATA;
4288                 priv->tx_buffers[i].info.d_struct.data = (struct ipw2100_data_header*)v;
4289                 priv->tx_buffers[i].info.d_struct.data_phys = p;
4290                 priv->tx_buffers[i].info.d_struct.txb = NULL;
4291         }
4292
4293         if (i == TX_PENDED_QUEUE_LENGTH)
4294                 return 0;
4295
4296         for (j = 0; j < i; j++) {
4297                 pci_free_consistent(
4298                         priv->pci_dev,
4299                         sizeof(struct ipw2100_data_header),
4300                         priv->tx_buffers[j].info.d_struct.data,
4301                         priv->tx_buffers[j].info.d_struct.data_phys);
4302         }
4303
4304         kfree(priv->tx_buffers);
4305         priv->tx_buffers = NULL;
4306
4307         return err;
4308 }
4309
4310 static void ipw2100_tx_initialize(struct ipw2100_priv *priv)
4311 {
4312         int i;
4313
4314         IPW_DEBUG_INFO("enter\n");
4315
4316         /*
4317          * reinitialize packet info lists
4318          */
4319         INIT_LIST_HEAD(&priv->fw_pend_list);
4320         INIT_STAT(&priv->fw_pend_stat);
4321
4322         /*
4323          * reinitialize lists
4324          */
4325         INIT_LIST_HEAD(&priv->tx_pend_list);
4326         INIT_LIST_HEAD(&priv->tx_free_list);
4327         INIT_STAT(&priv->tx_pend_stat);
4328         INIT_STAT(&priv->tx_free_stat);
4329
4330         for (i = 0; i < TX_PENDED_QUEUE_LENGTH; i++) {
4331                 /* We simply drop any SKBs that have been queued for
4332                  * transmit */
4333                 if (priv->tx_buffers[i].info.d_struct.txb) {
4334                         ieee80211_txb_free(priv->tx_buffers[i].info.d_struct.txb);
4335                         priv->tx_buffers[i].info.d_struct.txb = NULL;
4336                 }
4337
4338                 list_add_tail(&priv->tx_buffers[i].list, &priv->tx_free_list);
4339         }
4340
4341         SET_STAT(&priv->tx_free_stat, i);
4342
4343         priv->tx_queue.oldest = 0;
4344         priv->tx_queue.available = priv->tx_queue.entries;
4345         priv->tx_queue.next = 0;
4346         INIT_STAT(&priv->txq_stat);
4347         SET_STAT(&priv->txq_stat, priv->tx_queue.available);
4348
4349         bd_queue_initialize(priv, &priv->tx_queue,
4350                             IPW_MEM_HOST_SHARED_TX_QUEUE_BD_BASE,
4351                             IPW_MEM_HOST_SHARED_TX_QUEUE_BD_SIZE,
4352                             IPW_MEM_HOST_SHARED_TX_QUEUE_READ_INDEX,
4353                             IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX);
4354
4355         IPW_DEBUG_INFO("exit\n");
4356
4357 }
4358
4359 static void ipw2100_tx_free(struct ipw2100_priv *priv)
4360 {
4361         int i;
4362
4363         IPW_DEBUG_INFO("enter\n");
4364
4365         bd_queue_free(priv, &priv->tx_queue);
4366
4367         if (!priv->tx_buffers)
4368                 return;
4369
4370         for (i = 0; i < TX_PENDED_QUEUE_LENGTH; i++) {
4371                 if (priv->tx_buffers[i].info.d_struct.txb) {
4372                         ieee80211_txb_free(priv->tx_buffers[i].info.d_struct.txb);
4373                         priv->tx_buffers[i].info.d_struct.txb = NULL;
4374                 }
4375                 if (priv->tx_buffers[i].info.d_struct.data)
4376                         pci_free_consistent(
4377                                 priv->pci_dev,
4378                                 sizeof(struct ipw2100_data_header),
4379                                 priv->tx_buffers[i].info.d_struct.data,
4380                                 priv->tx_buffers[i].info.d_struct.data_phys);
4381         }
4382
4383         kfree(priv->tx_buffers);
4384         priv->tx_buffers = NULL;
4385
4386         IPW_DEBUG_INFO("exit\n");
4387 }
4388
4389
4390
4391 static int ipw2100_rx_allocate(struct ipw2100_priv *priv)
4392 {
4393         int i, j, err = -EINVAL;
4394
4395         IPW_DEBUG_INFO("enter\n");
4396
4397         err = bd_queue_allocate(priv, &priv->rx_queue, RX_QUEUE_LENGTH);
4398         if (err) {
4399                 IPW_DEBUG_INFO("failed bd_queue_allocate\n");
4400                 return err;
4401         }
4402
4403         err = status_queue_allocate(priv, RX_QUEUE_LENGTH);
4404         if (err) {
4405                 IPW_DEBUG_INFO("failed status_queue_allocate\n");
4406                 bd_queue_free(priv, &priv->rx_queue);
4407                 return err;
4408         }
4409
4410         /*
4411          * allocate packets
4412          */
4413         priv->rx_buffers = (struct ipw2100_rx_packet *)
4414             kmalloc(RX_QUEUE_LENGTH * sizeof(struct ipw2100_rx_packet),
4415                     GFP_KERNEL);
4416         if (!priv->rx_buffers) {
4417                 IPW_DEBUG_INFO("can't allocate rx packet buffer table\n");
4418
4419                 bd_queue_free(priv, &priv->rx_queue);
4420
4421                 status_queue_free(priv);
4422
4423                 return -ENOMEM;
4424         }
4425
4426         for (i = 0; i < RX_QUEUE_LENGTH; i++) {
4427                 struct ipw2100_rx_packet *packet = &priv->rx_buffers[i];
4428
4429                 err = ipw2100_alloc_skb(priv, packet);
4430                 if (unlikely(err)) {
4431                         err = -ENOMEM;
4432                         break;
4433                 }
4434
4435                 /* The BD holds the cache aligned address */
4436                 priv->rx_queue.drv[i].host_addr = packet->dma_addr;
4437                 priv->rx_queue.drv[i].buf_length = IPW_RX_NIC_BUFFER_LENGTH;
4438                 priv->status_queue.drv[i].status_fields = 0;
4439         }
4440
4441         if (i == RX_QUEUE_LENGTH)
4442                 return 0;
4443
4444         for (j = 0; j < i; j++) {
4445                 pci_unmap_single(priv->pci_dev, priv->rx_buffers[j].dma_addr,
4446                                  sizeof(struct ipw2100_rx_packet),
4447                                  PCI_DMA_FROMDEVICE);
4448                 dev_kfree_skb(priv->rx_buffers[j].skb);
4449         }
4450
4451         kfree(priv->rx_buffers);
4452         priv->rx_buffers = NULL;
4453
4454         bd_queue_free(priv, &priv->rx_queue);
4455
4456         status_queue_free(priv);
4457
4458         return err;
4459 }
4460
4461 static void ipw2100_rx_initialize(struct ipw2100_priv *priv)
4462 {
4463         IPW_DEBUG_INFO("enter\n");
4464
4465         priv->rx_queue.oldest = 0;
4466         priv->rx_queue.available = priv->rx_queue.entries - 1;
4467         priv->rx_queue.next = priv->rx_queue.entries - 1;
4468
4469         INIT_STAT(&priv->rxq_stat);
4470         SET_STAT(&priv->rxq_stat, priv->rx_queue.available);
4471
4472         bd_queue_initialize(priv, &priv->rx_queue,
4473                             IPW_MEM_HOST_SHARED_RX_BD_BASE,
4474                             IPW_MEM_HOST_SHARED_RX_BD_SIZE,
4475                             IPW_MEM_HOST_SHARED_RX_READ_INDEX,
4476                             IPW_MEM_HOST_SHARED_RX_WRITE_INDEX);
4477
4478         /* set up the status queue */
4479         write_register(priv->net_dev, IPW_MEM_HOST_SHARED_RX_STATUS_BASE,
4480                        priv->status_queue.nic);
4481
4482         IPW_DEBUG_INFO("exit\n");
4483 }
4484
4485 static void ipw2100_rx_free(struct ipw2100_priv *priv)
4486 {
4487         int i;
4488
4489         IPW_DEBUG_INFO("enter\n");
4490
4491         bd_queue_free(priv, &priv->rx_queue);
4492         status_queue_free(priv);
4493
4494         if (!priv->rx_buffers)
4495                 return;
4496
4497         for (i = 0; i < RX_QUEUE_LENGTH; i++) {
4498                 if (priv->rx_buffers[i].rxp) {
4499                         pci_unmap_single(priv->pci_dev,
4500                                          priv->rx_buffers[i].dma_addr,
4501                                          sizeof(struct ipw2100_rx),
4502                                          PCI_DMA_FROMDEVICE);
4503                         dev_kfree_skb(priv->rx_buffers[i].skb);
4504                 }
4505         }
4506
4507         kfree(priv->rx_buffers);
4508         priv->rx_buffers = NULL;
4509
4510         IPW_DEBUG_INFO("exit\n");
4511 }
4512
4513 static int ipw2100_read_mac_address(struct ipw2100_priv *priv)
4514 {
4515         u32 length = ETH_ALEN;
4516         u8 mac[ETH_ALEN];
4517
4518         int err;
4519
4520         err = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ADAPTER_MAC,
4521                                   mac, &length);
4522         if (err) {
4523                 IPW_DEBUG_INFO("MAC address read failed\n");
4524                 return -EIO;
4525         }
4526         IPW_DEBUG_INFO("card MAC is %02X:%02X:%02X:%02X:%02X:%02X\n",
4527                mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
4528
4529         memcpy(priv->net_dev->dev_addr, mac, ETH_ALEN);
4530
4531         return 0;
4532 }
4533
4534 /********************************************************************
4535  *
4536  * Firmware Commands
4537  *
4538  ********************************************************************/
4539
4540 int ipw2100_set_mac_address(struct ipw2100_priv *priv, int batch_mode)
4541 {
4542         struct host_command cmd = {
4543                 .host_command = ADAPTER_ADDRESS,
4544                 .host_command_sequence = 0,
4545                 .host_command_length = ETH_ALEN
4546         };
4547         int err;
4548
4549         IPW_DEBUG_HC("SET_MAC_ADDRESS\n");
4550
4551         IPW_DEBUG_INFO("enter\n");
4552
4553         if (priv->config & CFG_CUSTOM_MAC) {
4554                 memcpy(cmd.host_command_parameters, priv->mac_addr,
4555                        ETH_ALEN);
4556                 memcpy(priv->net_dev->dev_addr, priv->mac_addr, ETH_ALEN);
4557         } else
4558                 memcpy(cmd.host_command_parameters, priv->net_dev->dev_addr,
4559                        ETH_ALEN);
4560
4561         err = ipw2100_hw_send_command(priv, &cmd);
4562
4563         IPW_DEBUG_INFO("exit\n");
4564         return err;
4565 }
4566
4567 int ipw2100_set_port_type(struct ipw2100_priv *priv, u32 port_type,
4568                                  int batch_mode)
4569 {
4570         struct host_command cmd = {
4571                 .host_command = PORT_TYPE,
4572                 .host_command_sequence = 0,
4573                 .host_command_length = sizeof(u32)
4574         };
4575         int err;
4576
4577         switch (port_type) {
4578         case IW_MODE_INFRA:
4579                 cmd.host_command_parameters[0] = IPW_BSS;
4580                 break;
4581         case IW_MODE_ADHOC:
4582                 cmd.host_command_parameters[0] = IPW_IBSS;
4583                 break;
4584         }
4585
4586         IPW_DEBUG_HC("PORT_TYPE: %s\n",
4587                      port_type == IPW_IBSS ? "Ad-Hoc" : "Managed");
4588
4589         if (!batch_mode) {
4590                 err = ipw2100_disable_adapter(priv);
4591                 if (err) {
4592                         IPW_DEBUG_ERROR("%s: Could not disable adapter %d\n",
4593                                priv->net_dev->name, err);
4594                         return err;
4595                 }
4596         }
4597
4598         /* send cmd to firmware */
4599         err = ipw2100_hw_send_command(priv, &cmd);
4600
4601         if (!batch_mode)
4602                 ipw2100_enable_adapter(priv);
4603
4604         return err;
4605 }
4606
4607
4608 int ipw2100_set_channel(struct ipw2100_priv *priv, u32 channel, int batch_mode)
4609 {
4610         struct host_command cmd = {
4611                 .host_command = CHANNEL,
4612                 .host_command_sequence = 0,
4613                 .host_command_length = sizeof(u32)
4614         };
4615         int err;
4616
4617         cmd.host_command_parameters[0] = channel;
4618
4619         IPW_DEBUG_HC("CHANNEL: %d\n", channel);
4620
4621         /* If BSS then we don't support channel selection */
4622         if (priv->ieee->iw_mode == IW_MODE_INFRA)
4623                 return 0;
4624
4625         if ((channel != 0) &&
4626             ((channel < REG_MIN_CHANNEL) || (channel > REG_MAX_CHANNEL)))
4627                 return -EINVAL;
4628
4629         if (!batch_mode) {
4630                 err = ipw2100_disable_adapter(priv);
4631                 if (err)
4632                         return err;
4633         }
4634
4635         err = ipw2100_hw_send_command(priv, &cmd);
4636         if (err) {
4637                 IPW_DEBUG_INFO("Failed to set channel to %d",
4638                                channel);
4639                 return err;
4640         }
4641
4642         if (channel)
4643                 priv->config |= CFG_STATIC_CHANNEL;
4644         else
4645                 priv->config &= ~CFG_STATIC_CHANNEL;
4646
4647         priv->channel = channel;
4648
4649         if (!batch_mode) {
4650                 err = ipw2100_enable_adapter(priv);
4651                 if (err)
4652                         return err;
4653         }
4654
4655         return 0;
4656 }
4657
4658 int ipw2100_system_config(struct ipw2100_priv *priv, int batch_mode)
4659 {
4660         struct host_command cmd = {
4661                 .host_command = SYSTEM_CONFIG,
4662                 .host_command_sequence = 0,
4663                 .host_command_length = 12,
4664         };
4665         u32 ibss_mask, len = sizeof(u32);
4666         int err;
4667
4668         /* Set system configuration */
4669
4670         if (!batch_mode) {
4671                 err = ipw2100_disable_adapter(priv);
4672                 if (err)
4673                         return err;
4674         }
4675
4676         if (priv->ieee->iw_mode == IW_MODE_ADHOC)
4677                 cmd.host_command_parameters[0] |= IPW_CFG_IBSS_AUTO_START;
4678
4679         cmd.host_command_parameters[0] |= IPW_CFG_IBSS_MASK |
4680                 IPW_CFG_BSS_MASK |
4681                 IPW_CFG_802_1x_ENABLE;
4682
4683         if (!(priv->config & CFG_LONG_PREAMBLE))
4684                 cmd.host_command_parameters[0] |= IPW_CFG_PREAMBLE_AUTO;
4685
4686         err = ipw2100_get_ordinal(priv,
4687                                   IPW_ORD_EEPROM_IBSS_11B_CHANNELS,
4688                                   &ibss_mask,  &len);
4689         if (err)
4690                 ibss_mask = IPW_IBSS_11B_DEFAULT_MASK;
4691
4692         cmd.host_command_parameters[1] = REG_CHANNEL_MASK;
4693         cmd.host_command_parameters[2] = REG_CHANNEL_MASK & ibss_mask;
4694
4695         /* 11b only */
4696         /*cmd.host_command_parameters[0] |= DIVERSITY_ANTENNA_A;*/
4697
4698         err = ipw2100_hw_send_command(priv, &cmd);
4699         if (err)
4700                 return err;
4701
4702 /* If IPv6 is configured in the kernel then we don't want to filter out all
4703  * of the multicast packets as IPv6 needs some. */
4704 #if !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE)
4705         cmd.host_command = ADD_MULTICAST;
4706         cmd.host_command_sequence = 0;
4707         cmd.host_command_length = 0;
4708
4709         ipw2100_hw_send_command(priv, &cmd);
4710 #endif
4711         if (!batch_mode) {
4712                 err = ipw2100_enable_adapter(priv);
4713                 if (err)
4714                         return err;
4715         }
4716
4717         return 0;
4718 }
4719
4720 int ipw2100_set_tx_rates(struct ipw2100_priv *priv, u32 rate, int batch_mode)
4721 {
4722         struct host_command cmd = {
4723                 .host_command = BASIC_TX_RATES,
4724                 .host_command_sequence = 0,
4725                 .host_command_length = 4
4726         };
4727         int err;
4728
4729         cmd.host_command_parameters[0] = rate & TX_RATE_MASK;
4730
4731         if (!batch_mode) {
4732                 err = ipw2100_disable_adapter(priv);
4733                 if (err)
4734                         return err;
4735         }
4736
4737         /* Set BASIC TX Rate first */
4738         ipw2100_hw_send_command(priv, &cmd);
4739
4740         /* Set TX Rate */
4741         cmd.host_command = TX_RATES;
4742         ipw2100_hw_send_command(priv, &cmd);
4743
4744         /* Set MSDU TX Rate */
4745         cmd.host_command = MSDU_TX_RATES;
4746         ipw2100_hw_send_command(priv, &cmd);
4747
4748         if (!batch_mode) {
4749                 err = ipw2100_enable_adapter(priv);
4750                 if (err)
4751                         return err;
4752         }
4753
4754         priv->tx_rates = rate;
4755
4756         return 0;
4757 }
4758
4759 int ipw2100_set_power_mode(struct ipw2100_priv *priv,
4760                            int power_level)
4761 {
4762         struct host_command cmd = {
4763                 .host_command = POWER_MODE,
4764                 .host_command_sequence = 0,
4765                 .host_command_length = 4
4766         };
4767         int err;
4768
4769         cmd.host_command_parameters[0] = power_level;
4770
4771         err = ipw2100_hw_send_command(priv, &cmd);
4772         if (err)
4773                 return err;
4774
4775         if (power_level == IPW_POWER_MODE_CAM)
4776                 priv->power_mode = IPW_POWER_LEVEL(priv->power_mode);
4777         else
4778                 priv->power_mode = IPW_POWER_ENABLED | power_level;
4779
4780 #ifdef CONFIG_IPW2100_TX_POWER
4781         if (priv->port_type == IBSS &&
4782             priv->adhoc_power != DFTL_IBSS_TX_POWER) {
4783                 /* Set beacon interval */
4784                 cmd.host_command = TX_POWER_INDEX;
4785                 cmd.host_command_parameters[0] = (u32)priv->adhoc_power;
4786
4787                 err = ipw2100_hw_send_command(priv, &cmd);
4788                 if (err)
4789                         return err;
4790         }
4791 #endif
4792
4793         return 0;
4794 }
4795
4796
4797 int ipw2100_set_rts_threshold(struct ipw2100_priv *priv, u32 threshold)
4798 {
4799         struct host_command cmd = {
4800                 .host_command = RTS_THRESHOLD,
4801                 .host_command_sequence = 0,
4802                 .host_command_length = 4
4803         };
4804         int err;
4805
4806         if (threshold & RTS_DISABLED)
4807                 cmd.host_command_parameters[0] = MAX_RTS_THRESHOLD;
4808         else
4809                 cmd.host_command_parameters[0] = threshold & ~RTS_DISABLED;
4810
4811         err = ipw2100_hw_send_command(priv, &cmd);
4812         if (err)
4813                 return err;
4814
4815         priv->rts_threshold = threshold;
4816
4817         return 0;
4818 }
4819
4820 #if 0
4821 int ipw2100_set_fragmentation_threshold(struct ipw2100_priv *priv,
4822                                         u32 threshold, int batch_mode)
4823 {
4824         struct host_command cmd = {
4825                 .host_command = FRAG_THRESHOLD,
4826                 .host_command_sequence = 0,
4827                 .host_command_length = 4,
4828                 .host_command_parameters[0] = 0,
4829         };
4830         int err;
4831
4832         if (!batch_mode) {
4833                 err = ipw2100_disable_adapter(priv);
4834                 if (err)
4835                         return err;
4836         }
4837
4838         if (threshold == 0)
4839                 threshold = DEFAULT_FRAG_THRESHOLD;
4840         else {
4841                 threshold = max(threshold, MIN_FRAG_THRESHOLD);
4842                 threshold = min(threshold, MAX_FRAG_THRESHOLD);
4843         }
4844
4845         cmd.host_command_parameters[0] = threshold;
4846
4847         IPW_DEBUG_HC("FRAG_THRESHOLD: %u\n", threshold);
4848
4849         err = ipw2100_hw_send_command(priv, &cmd);
4850
4851         if (!batch_mode)
4852                 ipw2100_enable_adapter(priv);
4853
4854         if (!err)
4855                 priv->frag_threshold = threshold;
4856
4857         return err;
4858 }
4859 #endif
4860
4861 int ipw2100_set_short_retry(struct ipw2100_priv *priv, u32 retry)
4862 {
4863         struct host_command cmd = {
4864                 .host_command = SHORT_RETRY_LIMIT,
4865                 .host_command_sequence = 0,
4866                 .host_command_length = 4
4867         };
4868         int err;
4869
4870         cmd.host_command_parameters[0] = retry;
4871
4872         err = ipw2100_hw_send_command(priv, &cmd);
4873         if (err)
4874                 return err;
4875
4876         priv->short_retry_limit = retry;
4877
4878         return 0;
4879 }
4880
4881 int ipw2100_set_long_retry(struct ipw2100_priv *priv, u32 retry)
4882 {
4883         struct host_command cmd = {
4884                 .host_command = LONG_RETRY_LIMIT,
4885                 .host_command_sequence = 0,
4886                 .host_command_length = 4
4887         };
4888         int err;
4889
4890         cmd.host_command_parameters[0] = retry;
4891
4892         err = ipw2100_hw_send_command(priv, &cmd);
4893         if (err)
4894                 return err;
4895
4896         priv->long_retry_limit = retry;
4897
4898         return 0;
4899 }
4900
4901
4902 int ipw2100_set_mandatory_bssid(struct ipw2100_priv *priv, u8 *bssid,
4903                                 int batch_mode)
4904 {
4905         struct host_command cmd = {
4906                 .host_command = MANDATORY_BSSID,
4907                 .host_command_sequence = 0,
4908                 .host_command_length = (bssid == NULL) ? 0 : ETH_ALEN
4909         };
4910         int err;
4911
4912 #ifdef CONFIG_IPW_DEBUG
4913         if (bssid != NULL)
4914                 IPW_DEBUG_HC(
4915                         "MANDATORY_BSSID: %02X:%02X:%02X:%02X:%02X:%02X\n",
4916                         bssid[0], bssid[1], bssid[2], bssid[3], bssid[4],
4917                         bssid[5]);
4918         else
4919                 IPW_DEBUG_HC("MANDATORY_BSSID: <clear>\n");
4920 #endif
4921         /* if BSSID is empty then we disable mandatory bssid mode */
4922         if (bssid != NULL)
4923                 memcpy((u8 *)cmd.host_command_parameters, bssid, ETH_ALEN);
4924
4925         if (!batch_mode) {
4926                 err = ipw2100_disable_adapter(priv);
4927                 if (err)
4928                         return err;
4929         }
4930
4931         err = ipw2100_hw_send_command(priv, &cmd);
4932
4933         if (!batch_mode)
4934                 ipw2100_enable_adapter(priv);
4935
4936         return err;
4937 }
4938
4939 #ifdef CONFIG_IEEE80211_WPA
4940 static int ipw2100_disassociate_bssid(struct ipw2100_priv *priv)
4941 {
4942         struct host_command cmd = {
4943                 .host_command = DISASSOCIATION_BSSID,
4944                 .host_command_sequence = 0,
4945                 .host_command_length = ETH_ALEN
4946         };
4947         int err;
4948         int len;
4949
4950         IPW_DEBUG_HC("DISASSOCIATION_BSSID\n");
4951
4952         len = ETH_ALEN;
4953         /* The Firmware currently ignores the BSSID and just disassociates from
4954          * the currently associated AP -- but in the off chance that a future
4955          * firmware does use the BSSID provided here, we go ahead and try and
4956          * set it to the currently associated AP's BSSID */
4957         memcpy(cmd.host_command_parameters, priv->bssid, ETH_ALEN);
4958
4959         err = ipw2100_hw_send_command(priv, &cmd);
4960
4961         return err;
4962 }
4963 #endif
4964
4965 /*
4966  * Pseudo code for setting up wpa_frame:
4967  */
4968 #if 0
4969 void x(struct ieee80211_assoc_frame *wpa_assoc)
4970 {
4971         struct ipw2100_wpa_assoc_frame frame;
4972         frame->fixed_ie_mask = IPW_WPA_CAPABILTIES |
4973                 IPW_WPA_LISTENINTERVAL |
4974                 IPW_WPA_AP_ADDRESS;
4975         frame->capab_info = wpa_assoc->capab_info;
4976         frame->lisen_interval = wpa_assoc->listent_interval;
4977         memcpy(frame->current_ap, wpa_assoc->current_ap, ETH_ALEN);
4978
4979         /* UNKNOWN -- I'm not postivive about this part; don't have any WPA
4980          * setup here to test it with.
4981          *
4982          * Walk the IEs in the wpa_assoc and figure out the total size of all
4983          * that data.  Stick that into frame->var_ie_len.  Then memcpy() all of
4984          * the IEs from wpa_frame into frame.
4985          */
4986         frame->var_ie_len = calculate_ie_len(wpa_assoc);
4987         memcpy(frame->var_ie,  wpa_assoc->variable, frame->var_ie_len);
4988
4989         ipw2100_set_wpa_ie(priv, &frame, 0);
4990 }
4991 #endif
4992
4993
4994
4995
4996 static int ipw2100_set_wpa_ie(struct ipw2100_priv *,
4997                               struct ipw2100_wpa_assoc_frame *, int)
4998 __attribute__ ((unused));
4999
5000 static int ipw2100_set_wpa_ie(struct ipw2100_priv *priv,
5001                               struct ipw2100_wpa_assoc_frame *wpa_frame,
5002                               int batch_mode)
5003 {
5004         struct host_command cmd = {
5005                 .host_command = SET_WPA_IE,
5006                 .host_command_sequence = 0,
5007                 .host_command_length = sizeof(struct ipw2100_wpa_assoc_frame),
5008         };
5009         int err;
5010
5011         IPW_DEBUG_HC("SET_WPA_IE\n");
5012
5013         if (!batch_mode) {
5014                 err = ipw2100_disable_adapter(priv);
5015                 if (err)
5016                         return err;
5017         }
5018
5019         memcpy(cmd.host_command_parameters, wpa_frame,
5020                sizeof(struct ipw2100_wpa_assoc_frame));
5021
5022         err = ipw2100_hw_send_command(priv, &cmd);
5023
5024         if (!batch_mode) {
5025                 if (ipw2100_enable_adapter(priv))
5026                         err = -EIO;
5027         }
5028
5029         return err;
5030 }
5031
5032 struct security_info_params {
5033         u32 allowed_ciphers;
5034         u16 version;
5035         u8 auth_mode;
5036         u8 replay_counters_number;
5037         u8 unicast_using_group;
5038 } __attribute__ ((packed));
5039
5040 int ipw2100_set_security_information(struct ipw2100_priv *priv,
5041                                      int auth_mode,
5042                                      int security_level,
5043                                      int unicast_using_group,
5044                                      int batch_mode)
5045 {
5046         struct host_command cmd = {
5047                 .host_command = SET_SECURITY_INFORMATION,
5048                 .host_command_sequence = 0,
5049                 .host_command_length = sizeof(struct security_info_params)
5050         };
5051         struct security_info_params *security =
5052                 (struct security_info_params *)&cmd.host_command_parameters;
5053         int err;
5054         memset(security, 0, sizeof(*security));
5055
5056         /* If shared key AP authentication is turned on, then we need to
5057          * configure the firmware to try and use it.
5058          *
5059          * Actual data encryption/decryption is handled by the host. */
5060         security->auth_mode = auth_mode;
5061         security->unicast_using_group = unicast_using_group;
5062
5063         switch (security_level) {
5064         default:
5065         case SEC_LEVEL_0:
5066                 security->allowed_ciphers = IPW_NONE_CIPHER;
5067                 break;
5068         case SEC_LEVEL_1:
5069                 security->allowed_ciphers = IPW_WEP40_CIPHER |
5070                         IPW_WEP104_CIPHER;
5071                 break;
5072         case SEC_LEVEL_2:
5073                 security->allowed_ciphers = IPW_WEP40_CIPHER |
5074                         IPW_WEP104_CIPHER | IPW_TKIP_CIPHER;
5075                 break;
5076         case SEC_LEVEL_2_CKIP:
5077                 security->allowed_ciphers = IPW_WEP40_CIPHER |
5078                         IPW_WEP104_CIPHER | IPW_CKIP_CIPHER;
5079                 break;
5080         case SEC_LEVEL_3:
5081                 security->allowed_ciphers = IPW_WEP40_CIPHER |
5082                         IPW_WEP104_CIPHER | IPW_TKIP_CIPHER | IPW_CCMP_CIPHER;
5083                 break;
5084         }
5085
5086         IPW_DEBUG_HC(
5087                 "SET_SECURITY_INFORMATION: auth:%d cipher:0x%02X (level %d)\n",
5088                 security->auth_mode, security->allowed_ciphers, security_level);
5089
5090         security->replay_counters_number = 0;
5091
5092         if (!batch_mode) {
5093                 err = ipw2100_disable_adapter(priv);
5094                 if (err)
5095                         return err;
5096         }
5097
5098         err = ipw2100_hw_send_command(priv, &cmd);
5099
5100         if (!batch_mode)
5101                 ipw2100_enable_adapter(priv);
5102
5103         return err;
5104 }
5105
5106 int ipw2100_set_tx_power(struct ipw2100_priv *priv,
5107                          u32 tx_power)
5108 {
5109         struct host_command cmd = {
5110                 .host_command = TX_POWER_INDEX,
5111                 .host_command_sequence = 0,
5112                 .host_command_length = 4
5113         };
5114         int err = 0;
5115
5116         cmd.host_command_parameters[0] = tx_power;
5117
5118         if (priv->ieee->iw_mode == IW_MODE_ADHOC)
5119                 err = ipw2100_hw_send_command(priv, &cmd);
5120         if (!err)
5121                 priv->tx_power = tx_power;
5122
5123         return 0;
5124 }
5125
5126 int ipw2100_set_ibss_beacon_interval(struct ipw2100_priv *priv,
5127                                      u32 interval, int batch_mode)
5128 {
5129         struct host_command cmd = {
5130                 .host_command = BEACON_INTERVAL,
5131                 .host_command_sequence = 0,
5132                 .host_command_length = 4
5133         };
5134         int err;
5135
5136         cmd.host_command_parameters[0] = interval;
5137
5138         IPW_DEBUG_INFO("enter\n");
5139
5140         if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
5141                 if (!batch_mode) {
5142                         err = ipw2100_disable_adapter(priv);
5143                         if (err)
5144                                 return err;
5145                 }
5146
5147                 ipw2100_hw_send_command(priv, &cmd);
5148
5149                 if (!batch_mode) {
5150                         err = ipw2100_enable_adapter(priv);
5151                         if (err)
5152                                 return err;
5153                 }
5154         }
5155
5156         IPW_DEBUG_INFO("exit\n");
5157
5158         return 0;
5159 }
5160
5161
5162 void ipw2100_queues_initialize(struct ipw2100_priv *priv)
5163 {
5164         ipw2100_tx_initialize(priv);
5165         ipw2100_rx_initialize(priv);
5166         ipw2100_msg_initialize(priv);
5167 }
5168
5169 void ipw2100_queues_free(struct ipw2100_priv *priv)
5170 {
5171         ipw2100_tx_free(priv);
5172         ipw2100_rx_free(priv);
5173         ipw2100_msg_free(priv);
5174 }
5175
5176 int ipw2100_queues_allocate(struct ipw2100_priv *priv)
5177 {
5178         if (ipw2100_tx_allocate(priv) ||
5179             ipw2100_rx_allocate(priv) ||
5180             ipw2100_msg_allocate(priv))
5181                 goto fail;
5182
5183         return 0;
5184
5185  fail:
5186         ipw2100_tx_free(priv);
5187         ipw2100_rx_free(priv);
5188         ipw2100_msg_free(priv);
5189         return -ENOMEM;
5190 }
5191
5192 #define IPW_PRIVACY_CAPABLE 0x0008
5193
5194 static int ipw2100_set_wep_flags(struct ipw2100_priv *priv, u32 flags,
5195                                  int batch_mode)
5196 {
5197         struct host_command cmd = {
5198                 .host_command = WEP_FLAGS,
5199                 .host_command_sequence = 0,
5200                 .host_command_length = 4
5201         };
5202         int err;
5203
5204         cmd.host_command_parameters[0] = flags;
5205
5206         IPW_DEBUG_HC("WEP_FLAGS: flags = 0x%08X\n", flags);
5207
5208         if (!batch_mode) {
5209                 err = ipw2100_disable_adapter(priv);
5210                 if (err) {
5211                         IPW_DEBUG_ERROR("%s: Could not disable adapter %d\n",
5212                                priv->net_dev->name, err);
5213                         return err;
5214                 }
5215         }
5216
5217         /* send cmd to firmware */
5218         err = ipw2100_hw_send_command(priv, &cmd);
5219
5220         if (!batch_mode)
5221                 ipw2100_enable_adapter(priv);
5222
5223         return err;
5224 }
5225
5226 struct ipw2100_wep_key {
5227         u8 idx;
5228         u8 len;
5229         u8 key[13];
5230 };
5231
5232 /* Macros to ease up priting WEP keys */
5233 #define WEP_FMT_64  "%02X%02X%02X%02X-%02X"
5234 #define WEP_FMT_128 "%02X%02X%02X%02X-%02X%02X%02X%02X-%02X%02X%02X"
5235 #define WEP_STR_64(x) x[0],x[1],x[2],x[3],x[4]
5236 #define WEP_STR_128(x) x[0],x[1],x[2],x[3],x[4],x[5],x[6],x[7],x[8],x[9],x[10]
5237
5238
5239 /**
5240  * Set a the wep key
5241  *
5242  * @priv: struct to work on
5243  * @idx: index of the key we want to set
5244  * @key: ptr to the key data to set
5245  * @len: length of the buffer at @key
5246  * @batch_mode: FIXME perform the operation in batch mode, not
5247  *              disabling the device.
5248  *
5249  * @returns 0 if OK, < 0 errno code on error.
5250  *
5251  * Fill out a command structure with the new wep key, length an
5252  * index and send it down the wire.
5253  */
5254 static int ipw2100_set_key(struct ipw2100_priv *priv,
5255                            int idx, char *key, int len, int batch_mode)
5256 {
5257         int keylen = len ? (len <= 5 ? 5 : 13) : 0;
5258         struct host_command cmd = {
5259                 .host_command = WEP_KEY_INFO,
5260                 .host_command_sequence = 0,
5261                 .host_command_length = sizeof(struct ipw2100_wep_key),
5262         };
5263         struct ipw2100_wep_key *wep_key = (void*)cmd.host_command_parameters;
5264         int err;
5265
5266         IPW_DEBUG_HC("WEP_KEY_INFO: index = %d, len = %d/%d\n",
5267                                  idx, keylen, len);
5268
5269         /* NOTE: We don't check cached values in case the firmware was reset
5270          * or some other problem is occuring.  If the user is setting the key,
5271          * then we push the change */
5272
5273         wep_key->idx = idx;
5274         wep_key->len = keylen;
5275
5276         if (keylen) {
5277                 memcpy(wep_key->key, key, len);
5278                 memset(wep_key->key + len, 0, keylen - len);
5279         }
5280
5281         /* Will be optimized out on debug not being configured in */
5282         if (keylen == 0)
5283                 IPW_DEBUG_WEP("%s: Clearing key %d\n",
5284                                   priv->net_dev->name, wep_key->idx);
5285         else if (keylen == 5)
5286                 IPW_DEBUG_WEP("%s: idx: %d, len: %d key: " WEP_FMT_64 "\n",
5287                                   priv->net_dev->name, wep_key->idx, wep_key->len,
5288                                   WEP_STR_64(wep_key->key));
5289         else
5290                 IPW_DEBUG_WEP("%s: idx: %d, len: %d key: " WEP_FMT_128
5291                                   "\n",
5292                                   priv->net_dev->name, wep_key->idx, wep_key->len,
5293                                   WEP_STR_128(wep_key->key));
5294
5295         if (!batch_mode) {
5296                 err = ipw2100_disable_adapter(priv);
5297                 /* FIXME: IPG: shouldn't this prink be in _disable_adapter()? */
5298                 if (err) {
5299                         IPW_DEBUG_ERROR("%s: Could not disable adapter %d\n",
5300                                priv->net_dev->name, err);
5301                         return err;
5302                 }
5303         }
5304
5305         /* send cmd to firmware */
5306         err = ipw2100_hw_send_command(priv, &cmd);
5307
5308         if (!batch_mode) {
5309                 int err2 = ipw2100_enable_adapter(priv);
5310                 if (err == 0)
5311                         err = err2;
5312         }
5313         return err;
5314 }
5315
5316 static int ipw2100_set_key_index(struct ipw2100_priv *priv,
5317                                  int idx, int batch_mode)
5318 {
5319         struct host_command cmd = {
5320                 .host_command = WEP_KEY_INDEX,
5321                 .host_command_sequence = 0,
5322                 .host_command_length = 4,
5323                 .host_command_parameters = { idx },
5324         };
5325         int err;
5326
5327         IPW_DEBUG_HC("WEP_KEY_INDEX: index = %d\n", idx);
5328
5329         if (idx < 0 || idx > 3)
5330                 return -EINVAL;
5331
5332         if (!batch_mode) {
5333                 err = ipw2100_disable_adapter(priv);
5334                 if (err) {
5335                         IPW_DEBUG_ERROR("%s: Could not disable adapter %d\n",
5336                                priv->net_dev->name, err);
5337                         return err;
5338                 }
5339         }
5340
5341         /* send cmd to firmware */
5342         err = ipw2100_hw_send_command(priv, &cmd);
5343
5344         if (!batch_mode)
5345                 ipw2100_enable_adapter(priv);
5346
5347         return err;
5348 }
5349
5350
5351 static int ipw2100_configure_security(struct ipw2100_priv *priv,
5352                                       int batch_mode)
5353 {
5354         int i, err, auth_mode, sec_level, use_group;
5355
5356         if (!(priv->status & STATUS_RUNNING))
5357                 return 0;
5358
5359         if (!batch_mode) {
5360                 err = ipw2100_disable_adapter(priv);
5361                 if (err)
5362                         return err;
5363         }
5364
5365         if (!priv->sec.enabled) {
5366                 err = ipw2100_set_security_information(
5367                         priv, IPW_AUTH_OPEN, SEC_LEVEL_0, 0, 1);
5368         } else {
5369                 auth_mode = IPW_AUTH_OPEN;
5370                 if ((priv->sec.flags & SEC_AUTH_MODE) &&
5371                     (priv->sec.auth_mode == WLAN_AUTH_SHARED_KEY))
5372                         auth_mode = IPW_AUTH_SHARED;
5373
5374                 sec_level = SEC_LEVEL_0;
5375                 if (priv->sec.flags & SEC_LEVEL)
5376                         sec_level = priv->sec.level;
5377
5378                 use_group = 0;
5379                 if (priv->sec.flags & SEC_UNICAST_GROUP)
5380                         use_group = priv->sec.unicast_uses_group;
5381
5382                 err = ipw2100_set_security_information(
5383                             priv, auth_mode, sec_level, use_group, 1);
5384         }
5385
5386         if (err)
5387                 goto exit;
5388
5389         if (priv->sec.enabled) {
5390                 for (i = 0; i < 4; i++) {
5391                         if (!(priv->sec.flags & (1 << i))) {
5392                                 memset(priv->sec.keys[i], 0, WEP_KEY_LEN);
5393                                 priv->sec.key_sizes[i] = 0;
5394                         } else {
5395                                 err = ipw2100_set_key(priv, i,
5396                                                       priv->sec.keys[i],
5397                                                       priv->sec.key_sizes[i],
5398                                                       1);
5399                                 if (err)
5400                                         goto exit;
5401                         }
5402                 }
5403
5404                 ipw2100_set_key_index(priv, priv->ieee->tx_keyidx, 1);
5405         }
5406
5407         /* Always enable privacy so the Host can filter WEP packets if
5408          * encrypted data is sent up */
5409         err = ipw2100_set_wep_flags(
5410                 priv, priv->sec.enabled ? IPW_PRIVACY_CAPABLE : 0, 1);
5411         if (err)
5412                 goto exit;
5413
5414         priv->status &= ~STATUS_SECURITY_UPDATED;
5415
5416  exit:
5417         if (!batch_mode)
5418                 ipw2100_enable_adapter(priv);
5419
5420         return err;
5421 }
5422
5423 static void ipw2100_security_work(struct ipw2100_priv *priv)
5424 {
5425         /* If we happen to have reconnected before we get a chance to
5426          * process this, then update the security settings--which causes
5427          * a disassociation to occur */
5428         if (!(priv->status & STATUS_ASSOCIATED) &&
5429             priv->status & STATUS_SECURITY_UPDATED)
5430                 ipw2100_configure_security(priv, 0);
5431 }
5432
5433 static void shim__set_security(struct net_device *dev,
5434                                struct ieee80211_security *sec)
5435 {
5436         struct ipw2100_priv *priv = ieee80211_priv(dev);
5437         int i, force_update = 0;
5438
5439         down(&priv->action_sem);
5440         if (!(priv->status & STATUS_INITIALIZED))
5441                 goto done;
5442
5443         for (i = 0; i < 4; i++) {
5444                 if (sec->flags & (1 << i)) {
5445                         priv->sec.key_sizes[i] = sec->key_sizes[i];
5446                         if (sec->key_sizes[i] == 0)
5447                                 priv->sec.flags &= ~(1 << i);
5448                         else
5449                                 memcpy(priv->sec.keys[i], sec->keys[i],
5450                                        sec->key_sizes[i]);
5451                         priv->sec.flags |= (1 << i);
5452                         priv->status |= STATUS_SECURITY_UPDATED;
5453                 }
5454         }
5455
5456         if ((sec->flags & SEC_ACTIVE_KEY) &&
5457             priv->sec.active_key != sec->active_key) {
5458                 if (sec->active_key <= 3) {
5459                         priv->sec.active_key = sec->active_key;
5460                         priv->sec.flags |= SEC_ACTIVE_KEY;
5461                 } else
5462                         priv->sec.flags &= ~SEC_ACTIVE_KEY;
5463
5464                 priv->status |= STATUS_SECURITY_UPDATED;
5465         }
5466
5467         if ((sec->flags & SEC_AUTH_MODE) &&
5468             (priv->sec.auth_mode != sec->auth_mode)) {
5469                 priv->sec.auth_mode = sec->auth_mode;
5470                 priv->sec.flags |= SEC_AUTH_MODE;
5471                 priv->status |= STATUS_SECURITY_UPDATED;
5472         }
5473
5474         if (sec->flags & SEC_ENABLED &&
5475             priv->sec.enabled != sec->enabled) {
5476                 priv->sec.flags |= SEC_ENABLED;
5477                 priv->sec.enabled = sec->enabled;
5478                 priv->status |= STATUS_SECURITY_UPDATED;
5479                 force_update = 1;
5480         }
5481
5482         if (sec->flags & SEC_LEVEL &&
5483             priv->sec.level != sec->level) {
5484                 priv->sec.level = sec->level;
5485                 priv->sec.flags |= SEC_LEVEL;
5486                 priv->status |= STATUS_SECURITY_UPDATED;
5487         }
5488
5489         IPW_DEBUG_WEP("Security flags: %c %c%c%c%c %c%c%c%c\n",
5490                           priv->sec.flags & (1<<8) ? '1' : '0',
5491                           priv->sec.flags & (1<<7) ? '1' : '0',
5492                           priv->sec.flags & (1<<6) ? '1' : '0',
5493                           priv->sec.flags & (1<<5) ? '1' : '0',
5494                           priv->sec.flags & (1<<4) ? '1' : '0',
5495                           priv->sec.flags & (1<<3) ? '1' : '0',
5496                           priv->sec.flags & (1<<2) ? '1' : '0',
5497                           priv->sec.flags & (1<<1) ? '1' : '0',
5498                           priv->sec.flags & (1<<0) ? '1' : '0');
5499
5500 /* As a temporary work around to enable WPA until we figure out why
5501  * wpa_supplicant toggles the security capability of the driver, which
5502  * forces a disassocation with force_update...
5503  *
5504  *      if (force_update || !(priv->status & STATUS_ASSOCIATED))*/
5505         if (!(priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)))
5506                 ipw2100_configure_security(priv, 0);
5507 done:
5508         up(&priv->action_sem);
5509 }
5510
5511 static int ipw2100_adapter_setup(struct ipw2100_priv *priv)
5512 {
5513         int err;
5514         int batch_mode = 1;
5515         u8 *bssid;
5516
5517         IPW_DEBUG_INFO("enter\n");
5518
5519         err = ipw2100_disable_adapter(priv);
5520         if (err)
5521                 return err;
5522 #ifdef CONFIG_IPW2100_MONITOR
5523         if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
5524                 err = ipw2100_set_channel(priv, priv->channel, batch_mode);
5525                 if (err)
5526                         return err;
5527
5528                 IPW_DEBUG_INFO("exit\n");
5529
5530                 return 0;
5531         }
5532 #endif /* CONFIG_IPW2100_MONITOR */
5533
5534         err = ipw2100_read_mac_address(priv);
5535         if (err)
5536                 return -EIO;
5537
5538         err = ipw2100_set_mac_address(priv, batch_mode);
5539         if (err)
5540                 return err;
5541
5542         err = ipw2100_set_port_type(priv, priv->ieee->iw_mode, batch_mode);
5543         if (err)
5544                 return err;
5545
5546         if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
5547                 err = ipw2100_set_channel(priv, priv->channel, batch_mode);
5548                 if (err)
5549                         return err;
5550         }
5551
5552         err  = ipw2100_system_config(priv, batch_mode);
5553         if (err)
5554                 return err;
5555
5556         err = ipw2100_set_tx_rates(priv, priv->tx_rates, batch_mode);
5557         if (err)
5558                 return err;
5559
5560         /* Default to power mode OFF */
5561         err = ipw2100_set_power_mode(priv, IPW_POWER_MODE_CAM);
5562         if (err)
5563                 return err;
5564
5565         err = ipw2100_set_rts_threshold(priv, priv->rts_threshold);
5566         if (err)
5567                 return err;
5568
5569         if (priv->config & CFG_STATIC_BSSID)
5570                 bssid = priv->bssid;
5571         else
5572                 bssid = NULL;
5573         err = ipw2100_set_mandatory_bssid(priv, bssid, batch_mode);
5574         if (err)
5575                 return err;
5576
5577         if (priv->config & CFG_STATIC_ESSID)
5578                 err = ipw2100_set_essid(priv, priv->essid, priv->essid_len,
5579                                         batch_mode);
5580         else
5581                 err = ipw2100_set_essid(priv, NULL, 0, batch_mode);
5582         if (err)
5583                 return err;
5584
5585         err = ipw2100_configure_security(priv, batch_mode);
5586         if (err)
5587                 return err;
5588
5589         if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
5590                 err = ipw2100_set_ibss_beacon_interval(
5591                         priv, priv->beacon_interval, batch_mode);
5592                 if (err)
5593                         return err;
5594
5595                 err = ipw2100_set_tx_power(priv, priv->tx_power);
5596                 if (err)
5597                         return err;
5598         }
5599
5600         /*
5601           err = ipw2100_set_fragmentation_threshold(
5602           priv, priv->frag_threshold, batch_mode);
5603           if (err)
5604           return err;
5605         */
5606
5607         IPW_DEBUG_INFO("exit\n");
5608
5609         return 0;
5610 }
5611
5612
5613 /*************************************************************************
5614  *
5615  * EXTERNALLY CALLED METHODS
5616  *
5617  *************************************************************************/
5618
5619 /* This method is called by the network layer -- not to be confused with
5620  * ipw2100_set_mac_address() declared above called by this driver (and this
5621  * method as well) to talk to the firmware */
5622 static int ipw2100_set_address(struct net_device *dev, void *p)
5623 {
5624         struct ipw2100_priv *priv = ieee80211_priv(dev);
5625         struct sockaddr *addr = p;
5626         int err = 0;
5627
5628         if (!is_valid_ether_addr(addr->sa_data))
5629                 return -EADDRNOTAVAIL;
5630
5631         down(&priv->action_sem);
5632
5633         priv->config |= CFG_CUSTOM_MAC;
5634         memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
5635
5636         err = ipw2100_set_mac_address(priv, 0);
5637         if (err)
5638                 goto done;
5639
5640         priv->reset_backoff = 0;
5641         up(&priv->action_sem);
5642         ipw2100_reset_adapter(priv);
5643         return 0;
5644
5645  done:
5646         up(&priv->action_sem);
5647         return err;
5648 }
5649
5650 static int ipw2100_open(struct net_device *dev)
5651 {
5652         struct ipw2100_priv *priv = ieee80211_priv(dev);
5653         unsigned long flags;
5654         IPW_DEBUG_INFO("dev->open\n");
5655
5656         spin_lock_irqsave(&priv->low_lock, flags);
5657         if (priv->status & STATUS_ASSOCIATED)
5658                 netif_start_queue(dev);
5659         spin_unlock_irqrestore(&priv->low_lock, flags);
5660
5661         return 0;
5662 }
5663
5664 static int ipw2100_close(struct net_device *dev)
5665 {
5666         struct ipw2100_priv *priv = ieee80211_priv(dev);
5667         unsigned long flags;
5668         struct list_head *element;
5669         struct ipw2100_tx_packet *packet;
5670
5671         IPW_DEBUG_INFO("enter\n");
5672
5673         spin_lock_irqsave(&priv->low_lock, flags);
5674
5675         if (priv->status & STATUS_ASSOCIATED)
5676                 netif_carrier_off(dev);
5677         netif_stop_queue(dev);
5678
5679         /* Flush the TX queue ... */
5680         while (!list_empty(&priv->tx_pend_list)) {
5681                 element = priv->tx_pend_list.next;
5682                 packet = list_entry(element, struct ipw2100_tx_packet, list);
5683
5684                 list_del(element);
5685                 DEC_STAT(&priv->tx_pend_stat);
5686
5687                 ieee80211_txb_free(packet->info.d_struct.txb);
5688                 packet->info.d_struct.txb = NULL;
5689
5690                 list_add_tail(element, &priv->tx_free_list);
5691                 INC_STAT(&priv->tx_free_stat);
5692         }
5693         spin_unlock_irqrestore(&priv->low_lock, flags);
5694
5695         IPW_DEBUG_INFO("exit\n");
5696
5697         return 0;
5698 }
5699
5700
5701
5702 /*
5703  * TODO:  Fix this function... its just wrong
5704  */
5705 static void ipw2100_tx_timeout(struct net_device *dev)
5706 {
5707         struct ipw2100_priv *priv = ieee80211_priv(dev);
5708
5709         priv->ieee->stats.tx_errors++;
5710
5711 #ifdef CONFIG_IPW2100_MONITOR
5712         if (priv->ieee->iw_mode == IW_MODE_MONITOR)
5713                 return;
5714 #endif
5715
5716         IPW_DEBUG_INFO("%s: TX timed out.  Scheduling firmware restart.\n",
5717                        dev->name);
5718         schedule_reset(priv);
5719 }
5720
5721
5722 /*
5723  * TODO: reimplement it so that it reads statistics
5724  *       from the adapter using ordinal tables
5725  *       instead of/in addition to collecting them
5726  *       in the driver
5727  */
5728 static struct net_device_stats *ipw2100_stats(struct net_device *dev)
5729 {
5730         struct ipw2100_priv *priv = ieee80211_priv(dev);
5731
5732         return &priv->ieee->stats;
5733 }
5734
5735 /* Support for wpa_supplicant. Will be replaced with WEXT once
5736  * they get WPA support. */
5737 #ifdef CONFIG_IEEE80211_WPA
5738
5739 /* following definitions must match definitions in driver_ipw2100.c */
5740
5741 #define IPW2100_IOCTL_WPA_SUPPLICANT            SIOCIWFIRSTPRIV+30
5742
5743 #define IPW2100_CMD_SET_WPA_PARAM               1
5744 #define IPW2100_CMD_SET_WPA_IE                  2
5745 #define IPW2100_CMD_SET_ENCRYPTION              3
5746 #define IPW2100_CMD_MLME                        4
5747
5748 #define IPW2100_PARAM_WPA_ENABLED               1
5749 #define IPW2100_PARAM_TKIP_COUNTERMEASURES      2
5750 #define IPW2100_PARAM_DROP_UNENCRYPTED          3
5751 #define IPW2100_PARAM_PRIVACY_INVOKED           4
5752 #define IPW2100_PARAM_AUTH_ALGS                 5
5753 #define IPW2100_PARAM_IEEE_802_1X               6
5754
5755 #define IPW2100_MLME_STA_DEAUTH                 1
5756 #define IPW2100_MLME_STA_DISASSOC               2
5757
5758 #define IPW2100_CRYPT_ERR_UNKNOWN_ALG           2
5759 #define IPW2100_CRYPT_ERR_UNKNOWN_ADDR          3
5760 #define IPW2100_CRYPT_ERR_CRYPT_INIT_FAILED     4
5761 #define IPW2100_CRYPT_ERR_KEY_SET_FAILED        5
5762 #define IPW2100_CRYPT_ERR_TX_KEY_SET_FAILED     6
5763 #define IPW2100_CRYPT_ERR_CARD_CONF_FAILED      7
5764
5765 #define IPW2100_CRYPT_ALG_NAME_LEN              16
5766
5767 struct ipw2100_param {
5768         u32 cmd;
5769         u8 sta_addr[ETH_ALEN];
5770         union {
5771                 struct {
5772                         u8 name;
5773                         u32 value;
5774                 } wpa_param;
5775                 struct {
5776                         u32 len;
5777                         u8 *data;
5778                 } wpa_ie;
5779                 struct{
5780                         int command;
5781                         int reason_code;
5782                 } mlme;
5783                 struct {
5784                         u8 alg[IPW2100_CRYPT_ALG_NAME_LEN];
5785                         u8 set_tx;
5786                         u32 err;
5787                         u8 idx;
5788                         u8 seq[8]; /* sequence counter (set: RX, get: TX) */
5789                         u16 key_len;
5790                         u8 key[0];
5791                 } crypt;
5792
5793         } u;
5794 };
5795
5796 /* end of driver_ipw2100.c code */
5797
5798 static int ipw2100_wpa_enable(struct ipw2100_priv *priv, int value){
5799
5800         struct ieee80211_device *ieee = priv->ieee;
5801         struct ieee80211_security sec = {
5802                 .flags = SEC_LEVEL | SEC_ENABLED,
5803         };
5804         int ret = 0;
5805
5806         ieee->wpa_enabled = value;
5807
5808         if (value){
5809                 sec.level = SEC_LEVEL_3;
5810                 sec.enabled = 1;
5811         } else {
5812                 sec.level = SEC_LEVEL_0;
5813                 sec.enabled = 0;
5814         }
5815
5816         if (ieee->set_security)
5817                 ieee->set_security(ieee->dev, &sec);
5818         else
5819                 ret = -EOPNOTSUPP;
5820
5821         return ret;
5822 }
5823
5824 #define AUTH_ALG_OPEN_SYSTEM                    0x1
5825 #define AUTH_ALG_SHARED_KEY                     0x2
5826
5827 static int ipw2100_wpa_set_auth_algs(struct ipw2100_priv *priv, int value){
5828
5829         struct ieee80211_device *ieee = priv->ieee;
5830         struct ieee80211_security sec = {
5831                 .flags = SEC_AUTH_MODE,
5832         };
5833         int ret = 0;
5834
5835         if (value & AUTH_ALG_SHARED_KEY){
5836                 sec.auth_mode = WLAN_AUTH_SHARED_KEY;
5837                 ieee->open_wep = 0;
5838         } else {
5839                 sec.auth_mode = WLAN_AUTH_OPEN;
5840                 ieee->open_wep = 1;
5841         }
5842
5843         if (ieee->set_security)
5844                 ieee->set_security(ieee->dev, &sec);
5845         else
5846                 ret = -EOPNOTSUPP;
5847
5848         return ret;
5849 }
5850
5851
5852 static int ipw2100_wpa_set_param(struct net_device *dev, u8 name, u32 value){
5853
5854         struct ipw2100_priv *priv = ieee80211_priv(dev);
5855         int ret=0;
5856
5857         switch(name){
5858                 case IPW2100_PARAM_WPA_ENABLED:
5859                         ret = ipw2100_wpa_enable(priv, value);
5860                         break;
5861
5862                 case IPW2100_PARAM_TKIP_COUNTERMEASURES:
5863                         priv->ieee->tkip_countermeasures=value;
5864                         break;
5865
5866                 case IPW2100_PARAM_DROP_UNENCRYPTED:
5867                         priv->ieee->drop_unencrypted=value;
5868                         break;
5869
5870                 case IPW2100_PARAM_PRIVACY_INVOKED:
5871                         priv->ieee->privacy_invoked=value;
5872                         break;
5873
5874                 case IPW2100_PARAM_AUTH_ALGS:
5875                         ret = ipw2100_wpa_set_auth_algs(priv, value);
5876                         break;
5877
5878                 case IPW2100_PARAM_IEEE_802_1X:
5879                         priv->ieee->ieee802_1x=value;
5880                         break;
5881
5882                 default:
5883                         IPW_DEBUG_ERROR("%s: Unknown WPA param: %d\n",
5884                                             dev->name, name);
5885                         ret = -EOPNOTSUPP;
5886         }
5887
5888         return ret;
5889 }
5890
5891 static int ipw2100_wpa_mlme(struct net_device *dev, int command, int reason){
5892
5893         struct ipw2100_priv *priv = ieee80211_priv(dev);
5894         int ret=0;
5895
5896         switch(command){
5897                 case IPW2100_MLME_STA_DEAUTH:
5898                         // silently ignore
5899                         break;
5900
5901                 case IPW2100_MLME_STA_DISASSOC:
5902                         ipw2100_disassociate_bssid(priv);
5903                         break;
5904
5905                 default:
5906                         IPW_DEBUG_ERROR("%s: Unknown MLME request: %d\n",
5907                                             dev->name, command);
5908                         ret = -EOPNOTSUPP;
5909         }
5910
5911         return ret;
5912 }
5913
5914
5915 void ipw2100_wpa_assoc_frame(struct ipw2100_priv *priv,
5916                              char *wpa_ie, int wpa_ie_len){
5917
5918         struct ipw2100_wpa_assoc_frame frame;
5919
5920         frame.fixed_ie_mask = 0;
5921
5922         /* copy WPA IE */
5923         memcpy(frame.var_ie, wpa_ie, wpa_ie_len);
5924         frame.var_ie_len = wpa_ie_len;
5925
5926         /* make sure WPA is enabled */
5927         ipw2100_wpa_enable(priv, 1);
5928         ipw2100_set_wpa_ie(priv, &frame, 0);
5929 }
5930
5931
5932 static int ipw2100_wpa_set_wpa_ie(struct net_device *dev,
5933                                 struct ipw2100_param *param, int plen){
5934
5935         struct ipw2100_priv *priv = ieee80211_priv(dev);
5936         struct ieee80211_device *ieee = priv->ieee;
5937         u8 *buf;
5938
5939         if (! ieee->wpa_enabled)
5940             return -EOPNOTSUPP;
5941
5942         if (param->u.wpa_ie.len > MAX_WPA_IE_LEN ||
5943            (param->u.wpa_ie.len &&
5944                 param->u.wpa_ie.data==NULL))
5945                 return -EINVAL;
5946
5947         if (param->u.wpa_ie.len){
5948                 buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL);
5949                 if (buf == NULL)
5950                         return -ENOMEM;
5951
5952                 memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len);
5953
5954                 kfree(ieee->wpa_ie);
5955                 ieee->wpa_ie = buf;
5956                 ieee->wpa_ie_len = param->u.wpa_ie.len;
5957
5958         } else {
5959                 kfree(ieee->wpa_ie);
5960                 ieee->wpa_ie = NULL;
5961                 ieee->wpa_ie_len = 0;
5962         }
5963
5964         ipw2100_wpa_assoc_frame(priv, ieee->wpa_ie, ieee->wpa_ie_len);
5965
5966         return 0;
5967 }
5968
5969 /* implementation borrowed from hostap driver */
5970
5971 static int ipw2100_wpa_set_encryption(struct net_device *dev,
5972                                 struct ipw2100_param *param, int param_len){
5973
5974         int ret = 0;
5975         struct ipw2100_priv *priv = ieee80211_priv(dev);
5976         struct ieee80211_device *ieee = priv->ieee;
5977         struct ieee80211_crypto_ops *ops;
5978         struct ieee80211_crypt_data **crypt;
5979
5980         struct ieee80211_security sec = {
5981                 .flags = 0,
5982         };
5983
5984         param->u.crypt.err = 0;
5985         param->u.crypt.alg[IPW2100_CRYPT_ALG_NAME_LEN - 1] = '\0';
5986
5987         if (param_len !=
5988             (int) ((char *) param->u.crypt.key - (char *) param) +
5989             param->u.crypt.key_len){
5990                 IPW_DEBUG_INFO("Len mismatch %d, %d\n", param_len, param->u.crypt.key_len);
5991                 return -EINVAL;
5992         }
5993         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
5994             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
5995             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
5996                 if (param->u.crypt.idx >= WEP_KEYS)
5997                         return -EINVAL;
5998                 crypt = &ieee->crypt[param->u.crypt.idx];
5999         } else {
6000                 return -EINVAL;
6001         }
6002
6003         if (strcmp(param->u.crypt.alg, "none") == 0) {
6004                 if (crypt){
6005                         sec.enabled = 0;
6006                         sec.level = SEC_LEVEL_0;
6007                         sec.flags |= SEC_ENABLED | SEC_LEVEL;
6008                         ieee80211_crypt_delayed_deinit(ieee, crypt);
6009                 }
6010                 goto done;
6011         }
6012         sec.enabled = 1;
6013         sec.flags |= SEC_ENABLED;
6014
6015         ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
6016         if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0) {
6017                 request_module("ieee80211_crypt_wep");
6018                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
6019         } else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0) {
6020                 request_module("ieee80211_crypt_tkip");
6021                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
6022         } else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0) {
6023                 request_module("ieee80211_crypt_ccmp");
6024                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
6025         }
6026         if (ops == NULL) {
6027                 IPW_DEBUG_INFO("%s: unknown crypto alg '%s'\n",
6028                        dev->name, param->u.crypt.alg);
6029                 param->u.crypt.err = IPW2100_CRYPT_ERR_UNKNOWN_ALG;
6030                 ret = -EINVAL;
6031                 goto done;
6032         }
6033
6034         if (*crypt == NULL || (*crypt)->ops != ops) {
6035                 struct ieee80211_crypt_data *new_crypt;
6036
6037                 ieee80211_crypt_delayed_deinit(ieee, crypt);
6038
6039                 new_crypt = (struct ieee80211_crypt_data *)
6040                         kmalloc(sizeof(struct ieee80211_crypt_data), GFP_KERNEL);
6041                 if (new_crypt == NULL) {
6042                         ret = -ENOMEM;
6043                         goto done;
6044                 }
6045                 memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
6046                 new_crypt->ops = ops;
6047                 if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
6048                         new_crypt->priv = new_crypt->ops->init(param->u.crypt.idx);
6049
6050                 if (new_crypt->priv == NULL) {
6051                         kfree(new_crypt);
6052                         param->u.crypt.err =
6053                                 IPW2100_CRYPT_ERR_CRYPT_INIT_FAILED;
6054                         ret = -EINVAL;
6055                         goto done;
6056                 }
6057
6058                 *crypt = new_crypt;
6059         }
6060
6061         if (param->u.crypt.key_len > 0 && (*crypt)->ops->set_key &&
6062             (*crypt)->ops->set_key(param->u.crypt.key,
6063                                    param->u.crypt.key_len, param->u.crypt.seq,
6064                                    (*crypt)->priv) < 0) {
6065                 IPW_DEBUG_INFO("%s: key setting failed\n",
6066                        dev->name);
6067                 param->u.crypt.err = IPW2100_CRYPT_ERR_KEY_SET_FAILED;
6068                 ret = -EINVAL;
6069                 goto done;
6070         }
6071
6072         if (param->u.crypt.set_tx){
6073                 ieee->tx_keyidx = param->u.crypt.idx;
6074                 sec.active_key = param->u.crypt.idx;
6075                 sec.flags |= SEC_ACTIVE_KEY;
6076         }
6077
6078         if (ops->name != NULL){
6079
6080                 if (strcmp(ops->name, "WEP") == 0) {
6081                         memcpy(sec.keys[param->u.crypt.idx], param->u.crypt.key, param->u.crypt.key_len);
6082                         sec.key_sizes[param->u.crypt.idx] = param->u.crypt.key_len;
6083                         sec.flags |= (1 << param->u.crypt.idx);
6084                         sec.flags |= SEC_LEVEL;
6085                         sec.level = SEC_LEVEL_1;
6086                 } else if (strcmp(ops->name, "TKIP") == 0) {
6087                         sec.flags |= SEC_LEVEL;
6088                         sec.level = SEC_LEVEL_2;
6089                 } else if (strcmp(ops->name, "CCMP") == 0) {
6090                         sec.flags |= SEC_LEVEL;
6091                         sec.level = SEC_LEVEL_3;
6092                 }
6093         }
6094  done:
6095         if (ieee->set_security)
6096                 ieee->set_security(ieee->dev, &sec);
6097
6098         /* Do not reset port if card is in Managed mode since resetting will
6099          * generate new IEEE 802.11 authentication which may end up in looping
6100          * with IEEE 802.1X.  If your hardware requires a reset after WEP
6101          * configuration (for example... Prism2), implement the reset_port in
6102          * the callbacks structures used to initialize the 802.11 stack. */
6103         if (ieee->reset_on_keychange &&
6104             ieee->iw_mode != IW_MODE_INFRA &&
6105             ieee->reset_port &&
6106             ieee->reset_port(dev)) {
6107                 IPW_DEBUG_INFO("%s: reset_port failed\n", dev->name);
6108                 param->u.crypt.err = IPW2100_CRYPT_ERR_CARD_CONF_FAILED;
6109                 return -EINVAL;
6110         }
6111
6112         return ret;
6113 }
6114
6115
6116 static int ipw2100_wpa_supplicant(struct net_device *dev, struct iw_point *p){
6117
6118         struct ipw2100_param *param;
6119         int ret=0;
6120
6121         IPW_DEBUG_IOCTL("wpa_supplicant: len=%d\n", p->length);
6122
6123         if (p->length < sizeof(struct ipw2100_param) || !p->pointer)
6124                 return -EINVAL;
6125
6126         param = (struct ipw2100_param *)kmalloc(p->length, GFP_KERNEL);
6127         if (param == NULL)
6128                 return -ENOMEM;
6129
6130         if (copy_from_user(param, p->pointer, p->length)){
6131                 kfree(param);
6132                 return -EFAULT;
6133         }
6134
6135         switch (param->cmd){
6136
6137         case IPW2100_CMD_SET_WPA_PARAM:
6138                 ret = ipw2100_wpa_set_param(dev, param->u.wpa_param.name,
6139                                             param->u.wpa_param.value);
6140                 break;
6141
6142         case IPW2100_CMD_SET_WPA_IE:
6143                 ret = ipw2100_wpa_set_wpa_ie(dev, param, p->length);
6144                 break;
6145
6146         case IPW2100_CMD_SET_ENCRYPTION:
6147                 ret = ipw2100_wpa_set_encryption(dev, param, p->length);
6148                 break;
6149
6150         case IPW2100_CMD_MLME:
6151                 ret = ipw2100_wpa_mlme(dev, param->u.mlme.command,
6152                                        param->u.mlme.reason_code);
6153                 break;
6154
6155         default:
6156                 IPW_DEBUG_ERROR("%s: Unknown WPA supplicant request: %d\n",
6157                                 dev->name, param->cmd);
6158                 ret = -EOPNOTSUPP;
6159
6160         }
6161
6162         if (ret == 0 && copy_to_user(p->pointer, param, p->length))
6163                 ret = -EFAULT;
6164
6165         kfree(param);
6166         return ret;
6167 }
6168 #endif /* CONFIG_IEEE80211_WPA */
6169
6170 static int ipw2100_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
6171 {
6172 #ifdef CONFIG_IEEE80211_WPA
6173         struct iwreq *wrq = (struct iwreq *) rq;
6174         int ret=-1;
6175         switch (cmd){
6176             case IPW2100_IOCTL_WPA_SUPPLICANT:
6177                 ret = ipw2100_wpa_supplicant(dev, &wrq->u.data);
6178                 return ret;
6179
6180             default:
6181                 return -EOPNOTSUPP;
6182         }
6183
6184 #endif /* CONFIG_IEEE80211_WPA */
6185
6186         return -EOPNOTSUPP;
6187 }
6188
6189
6190 static void ipw_ethtool_get_drvinfo(struct net_device *dev,
6191                                     struct ethtool_drvinfo *info)
6192 {
6193         struct ipw2100_priv *priv = ieee80211_priv(dev);
6194         char fw_ver[64], ucode_ver[64];
6195
6196         strcpy(info->driver, DRV_NAME);
6197         strcpy(info->version, DRV_VERSION);
6198
6199         ipw2100_get_fwversion(priv, fw_ver, sizeof(fw_ver));
6200         ipw2100_get_ucodeversion(priv, ucode_ver, sizeof(ucode_ver));
6201
6202         snprintf(info->fw_version, sizeof(info->fw_version), "%s:%d:%s",
6203                  fw_ver, priv->eeprom_version, ucode_ver);
6204
6205         strcpy(info->bus_info, pci_name(priv->pci_dev));
6206 }
6207
6208 static u32 ipw2100_ethtool_get_link(struct net_device *dev)
6209 {
6210     struct ipw2100_priv *priv = ieee80211_priv(dev);
6211     return (priv->status & STATUS_ASSOCIATED) ? 1 : 0;
6212 }
6213
6214
6215 static struct ethtool_ops ipw2100_ethtool_ops = {
6216     .get_link        = ipw2100_ethtool_get_link,
6217     .get_drvinfo     = ipw_ethtool_get_drvinfo,
6218 };
6219
6220 static void ipw2100_hang_check(void *adapter)
6221 {
6222         struct ipw2100_priv *priv = adapter;
6223         unsigned long flags;
6224         u32 rtc = 0xa5a5a5a5;
6225         u32 len = sizeof(rtc);
6226         int restart = 0;
6227
6228         spin_lock_irqsave(&priv->low_lock, flags);
6229
6230         if (priv->fatal_error != 0) {
6231                 /* If fatal_error is set then we need to restart */
6232                 IPW_DEBUG_INFO("%s: Hardware fatal error detected.\n",
6233                                priv->net_dev->name);
6234
6235                 restart = 1;
6236         } else if (ipw2100_get_ordinal(priv, IPW_ORD_RTC_TIME, &rtc, &len) ||
6237                    (rtc == priv->last_rtc)) {
6238                 /* Check if firmware is hung */
6239                 IPW_DEBUG_INFO("%s: Firmware RTC stalled.\n",
6240                                priv->net_dev->name);
6241
6242                 restart = 1;
6243         }
6244
6245         if (restart) {
6246                 /* Kill timer */
6247                 priv->stop_hang_check = 1;
6248                 priv->hangs++;
6249
6250                 /* Restart the NIC */
6251                 schedule_reset(priv);
6252         }
6253
6254         priv->last_rtc = rtc;
6255
6256         if (!priv->stop_hang_check)
6257                 queue_delayed_work(priv->workqueue, &priv->hang_check, HZ / 2);
6258
6259         spin_unlock_irqrestore(&priv->low_lock, flags);
6260 }
6261
6262
6263 static void ipw2100_rf_kill(void *adapter)
6264 {
6265         struct ipw2100_priv *priv = adapter;
6266         unsigned long flags;
6267
6268         spin_lock_irqsave(&priv->low_lock, flags);
6269
6270         if (rf_kill_active(priv)) {
6271                 IPW_DEBUG_RF_KILL("RF Kill active, rescheduling GPIO check\n");
6272                 if (!priv->stop_rf_kill)
6273                         queue_delayed_work(priv->workqueue, &priv->rf_kill, HZ);
6274                 goto exit_unlock;
6275         }
6276
6277         /* RF Kill is now disabled, so bring the device back up */
6278
6279         if (!(priv->status & STATUS_RF_KILL_MASK)) {
6280                 IPW_DEBUG_RF_KILL("HW RF Kill no longer active, restarting "
6281                                   "device\n");
6282                 schedule_reset(priv);
6283         } else
6284                 IPW_DEBUG_RF_KILL("HW RF Kill deactivated.  SW RF Kill still "
6285                                   "enabled\n");
6286
6287  exit_unlock:
6288         spin_unlock_irqrestore(&priv->low_lock, flags);
6289 }
6290
6291 static void ipw2100_irq_tasklet(struct ipw2100_priv *priv);
6292
6293 /* Look into using netdev destructor to shutdown ieee80211? */
6294
6295 static struct net_device *ipw2100_alloc_device(
6296         struct pci_dev *pci_dev,
6297         char *base_addr,
6298         unsigned long mem_start,
6299         unsigned long mem_len)
6300 {
6301         struct ipw2100_priv *priv;
6302         struct net_device *dev;
6303
6304         dev = alloc_ieee80211(sizeof(struct ipw2100_priv));
6305         if (!dev)
6306                 return NULL;
6307         priv = ieee80211_priv(dev);
6308         priv->ieee = netdev_priv(dev);
6309         priv->pci_dev = pci_dev;
6310         priv->net_dev = dev;
6311
6312         priv->ieee->hard_start_xmit = ipw2100_tx;
6313         priv->ieee->set_security = shim__set_security;
6314
6315         dev->open = ipw2100_open;
6316         dev->stop = ipw2100_close;
6317         dev->init = ipw2100_net_init;
6318         dev->do_ioctl = ipw2100_ioctl;
6319         dev->get_stats = ipw2100_stats;
6320         dev->ethtool_ops = &ipw2100_ethtool_ops;
6321         dev->tx_timeout = ipw2100_tx_timeout;
6322         dev->wireless_handlers = &ipw2100_wx_handler_def;
6323         dev->get_wireless_stats = ipw2100_wx_wireless_stats;
6324         dev->set_mac_address = ipw2100_set_address;
6325         dev->watchdog_timeo = 3*HZ;
6326         dev->irq = 0;
6327
6328         dev->base_addr = (unsigned long)base_addr;
6329         dev->mem_start = mem_start;
6330         dev->mem_end = dev->mem_start + mem_len - 1;
6331
6332         /* NOTE: We don't use the wireless_handlers hook
6333          * in dev as the system will start throwing WX requests
6334          * to us before we're actually initialized and it just
6335          * ends up causing problems.  So, we just handle
6336          * the WX extensions through the ipw2100_ioctl interface */
6337
6338
6339         /* memset() puts everything to 0, so we only have explicitely set
6340          * those values that need to be something else */
6341
6342         /* If power management is turned on, default to AUTO mode */
6343         priv->power_mode = IPW_POWER_AUTO;
6344
6345
6346
6347 #ifdef CONFIG_IEEE80211_WPA
6348         priv->ieee->wpa_enabled = 0;
6349         priv->ieee->tkip_countermeasures = 0;
6350         priv->ieee->drop_unencrypted = 0;
6351         priv->ieee->privacy_invoked = 0;
6352         priv->ieee->ieee802_1x = 1;
6353 #endif /* CONFIG_IEEE80211_WPA */
6354
6355         /* Set module parameters */
6356         switch (mode) {
6357         case 1:
6358                 priv->ieee->iw_mode = IW_MODE_ADHOC;
6359                 break;
6360 #ifdef CONFIG_IPW2100_MONITOR
6361         case 2:
6362                 priv->ieee->iw_mode = IW_MODE_MONITOR;
6363                 break;
6364 #endif
6365         default:
6366         case 0:
6367                 priv->ieee->iw_mode = IW_MODE_INFRA;
6368                 break;
6369         }
6370
6371         if (disable == 1)
6372                 priv->status |= STATUS_RF_KILL_SW;
6373
6374         if (channel != 0 &&
6375             ((channel >= REG_MIN_CHANNEL) &&
6376              (channel <= REG_MAX_CHANNEL))) {
6377                 priv->config |= CFG_STATIC_CHANNEL;
6378                 priv->channel = channel;
6379         }
6380
6381         if (associate)
6382                 priv->config |= CFG_ASSOCIATE;
6383
6384         priv->beacon_interval = DEFAULT_BEACON_INTERVAL;
6385         priv->short_retry_limit = DEFAULT_SHORT_RETRY_LIMIT;
6386         priv->long_retry_limit = DEFAULT_LONG_RETRY_LIMIT;
6387         priv->rts_threshold = DEFAULT_RTS_THRESHOLD | RTS_DISABLED;
6388         priv->frag_threshold = DEFAULT_FTS | FRAG_DISABLED;
6389         priv->tx_power = IPW_TX_POWER_DEFAULT;
6390         priv->tx_rates = DEFAULT_TX_RATES;
6391
6392         strcpy(priv->nick, "ipw2100");
6393
6394         spin_lock_init(&priv->low_lock);
6395         sema_init(&priv->action_sem, 1);
6396         sema_init(&priv->adapter_sem, 1);
6397
6398         init_waitqueue_head(&priv->wait_command_queue);
6399
6400         netif_carrier_off(dev);
6401
6402         INIT_LIST_HEAD(&priv->msg_free_list);
6403         INIT_LIST_HEAD(&priv->msg_pend_list);
6404         INIT_STAT(&priv->msg_free_stat);
6405         INIT_STAT(&priv->msg_pend_stat);
6406
6407         INIT_LIST_HEAD(&priv->tx_free_list);
6408         INIT_LIST_HEAD(&priv->tx_pend_list);
6409         INIT_STAT(&priv->tx_free_stat);
6410         INIT_STAT(&priv->tx_pend_stat);
6411
6412         INIT_LIST_HEAD(&priv->fw_pend_list);
6413         INIT_STAT(&priv->fw_pend_stat);
6414
6415
6416 #ifdef CONFIG_SOFTWARE_SUSPEND2
6417         priv->workqueue = create_workqueue(DRV_NAME, 0);
6418 #else
6419         priv->workqueue = create_workqueue(DRV_NAME);
6420 #endif
6421         INIT_WORK(&priv->reset_work,
6422                   (void (*)(void *))ipw2100_reset_adapter, priv);
6423         INIT_WORK(&priv->security_work,
6424                   (void (*)(void *))ipw2100_security_work, priv);
6425         INIT_WORK(&priv->wx_event_work,
6426                   (void (*)(void *))ipw2100_wx_event_work, priv);
6427         INIT_WORK(&priv->hang_check, ipw2100_hang_check, priv);
6428         INIT_WORK(&priv->rf_kill, ipw2100_rf_kill, priv);
6429
6430         tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
6431                      ipw2100_irq_tasklet, (unsigned long)priv);
6432
6433         /* NOTE:  We do not start the deferred work for status checks yet */
6434         priv->stop_rf_kill = 1;
6435         priv->stop_hang_check = 1;
6436
6437         return dev;
6438 }
6439
6440 static int ipw2100_pci_init_one(struct pci_dev *pci_dev,
6441                                 const struct pci_device_id *ent)
6442 {
6443         unsigned long mem_start, mem_len, mem_flags;
6444         char *base_addr = NULL;
6445         struct net_device *dev = NULL;
6446         struct ipw2100_priv *priv = NULL;
6447         int err = 0;
6448         int registered = 0;
6449         u32 val;
6450
6451         IPW_DEBUG_INFO("enter\n");
6452
6453         mem_start = pci_resource_start(pci_dev, 0);
6454         mem_len = pci_resource_len(pci_dev, 0);
6455         mem_flags = pci_resource_flags(pci_dev, 0);
6456
6457         if ((mem_flags & IORESOURCE_MEM) != IORESOURCE_MEM) {
6458                 IPW_DEBUG_INFO("weird - resource type is not memory\n");
6459                 err = -ENODEV;
6460                 goto fail;
6461         }
6462
6463         base_addr = ioremap_nocache(mem_start, mem_len);
6464         if (!base_addr) {
6465                 printk(KERN_WARNING DRV_NAME
6466                        "Error calling ioremap_nocache.\n");
6467                 err = -EIO;
6468                 goto fail;
6469         }
6470
6471         /* allocate and initialize our net_device */
6472         dev = ipw2100_alloc_device(pci_dev, base_addr, mem_start, mem_len);
6473         if (!dev) {
6474                 printk(KERN_WARNING DRV_NAME
6475                        "Error calling ipw2100_alloc_device.\n");
6476                 err = -ENOMEM;
6477                 goto fail;
6478         }
6479
6480         /* set up PCI mappings for device */
6481         err = pci_enable_device(pci_dev);
6482         if (err) {
6483                 printk(KERN_WARNING DRV_NAME
6484                        "Error calling pci_enable_device.\n");
6485                 return err;
6486         }
6487
6488         priv = ieee80211_priv(dev);
6489
6490         pci_set_master(pci_dev);
6491         pci_set_drvdata(pci_dev, priv);
6492
6493         err = pci_set_dma_mask(pci_dev, DMA_32BIT_MASK);
6494         if (err) {
6495                 printk(KERN_WARNING DRV_NAME
6496                        "Error calling pci_set_dma_mask.\n");
6497                 pci_disable_device(pci_dev);
6498                 return err;
6499         }
6500
6501         err = pci_request_regions(pci_dev, DRV_NAME);
6502         if (err) {
6503                 printk(KERN_WARNING DRV_NAME
6504                        "Error calling pci_request_regions.\n");
6505                 pci_disable_device(pci_dev);
6506                 return err;
6507         }
6508
6509         /* We disable the RETRY_TIMEOUT register (0x41) to keep
6510          * PCI Tx retries from interfering with C3 CPU state */
6511         pci_read_config_dword(pci_dev, 0x40, &val);
6512         if ((val & 0x0000ff00) != 0)
6513                 pci_write_config_dword(pci_dev, 0x40, val & 0xffff00ff);
6514
6515         pci_set_power_state(pci_dev, PCI_D0);
6516
6517         if (!ipw2100_hw_is_adapter_in_system(dev)) {
6518                 printk(KERN_WARNING DRV_NAME
6519                        "Device not found via register read.\n");
6520                 err = -ENODEV;
6521                 goto fail;
6522         }
6523
6524         SET_NETDEV_DEV(dev, &pci_dev->dev);
6525
6526         /* Force interrupts to be shut off on the device */
6527         priv->status |= STATUS_INT_ENABLED;
6528         ipw2100_disable_interrupts(priv);
6529
6530         /* Allocate and initialize the Tx/Rx queues and lists */
6531         if (ipw2100_queues_allocate(priv)) {
6532                 printk(KERN_WARNING DRV_NAME
6533                        "Error calilng ipw2100_queues_allocate.\n");
6534                 err = -ENOMEM;
6535                 goto fail;
6536         }
6537         ipw2100_queues_initialize(priv);
6538
6539         err = request_irq(pci_dev->irq,
6540                           ipw2100_interrupt, SA_SHIRQ,
6541                           dev->name, priv);
6542         if (err) {
6543                 printk(KERN_WARNING DRV_NAME
6544                        "Error calling request_irq: %d.\n",
6545                        pci_dev->irq);
6546                 goto fail;
6547         }
6548         dev->irq = pci_dev->irq;
6549
6550         IPW_DEBUG_INFO("Attempting to register device...\n");
6551
6552         SET_MODULE_OWNER(dev);
6553
6554         printk(KERN_INFO DRV_NAME
6555                ": Detected Intel PRO/Wireless 2100 Network Connection\n");
6556
6557         /* Bring up the interface.  Pre 0.46, after we registered the
6558          * network device we would call ipw2100_up.  This introduced a race
6559          * condition with newer hotplug configurations (network was coming
6560          * up and making calls before the device was initialized).
6561          *
6562          * If we called ipw2100_up before we registered the device, then the
6563          * device name wasn't registered.  So, we instead use the net_dev->init
6564          * member to call a function that then just turns and calls ipw2100_up.
6565          * net_dev->init is called after name allocation but before the
6566          * notifier chain is called */
6567         down(&priv->action_sem);
6568         err = register_netdev(dev);
6569         if (err) {
6570                 printk(KERN_WARNING DRV_NAME
6571                        "Error calling register_netdev.\n");
6572                 goto fail_unlock;
6573         }
6574         registered = 1;
6575
6576         IPW_DEBUG_INFO("%s: Bound to %s\n", dev->name, pci_name(pci_dev));
6577
6578         /* perform this after register_netdev so that dev->name is set */
6579         sysfs_create_group(&pci_dev->dev.kobj, &ipw2100_attribute_group);
6580         netif_carrier_off(dev);
6581
6582         /* If the RF Kill switch is disabled, go ahead and complete the
6583          * startup sequence */
6584         if (!(priv->status & STATUS_RF_KILL_MASK)) {
6585                 /* Enable the adapter - sends HOST_COMPLETE */
6586                 if (ipw2100_enable_adapter(priv)) {
6587                         printk(KERN_WARNING DRV_NAME
6588                                ": %s: failed in call to enable adapter.\n",
6589                                priv->net_dev->name);
6590                         ipw2100_hw_stop_adapter(priv);
6591                         err = -EIO;
6592                         goto fail_unlock;
6593                 }
6594
6595                 /* Start a scan . . . */
6596                 ipw2100_set_scan_options(priv);
6597                 ipw2100_start_scan(priv);
6598         }
6599
6600         IPW_DEBUG_INFO("exit\n");
6601
6602         priv->status |= STATUS_INITIALIZED;
6603
6604         up(&priv->action_sem);
6605
6606         return 0;
6607
6608  fail_unlock:
6609         up(&priv->action_sem);
6610
6611  fail:
6612         if (dev) {
6613                 if (registered)
6614                         unregister_netdev(dev);
6615
6616                 ipw2100_hw_stop_adapter(priv);
6617
6618                 ipw2100_disable_interrupts(priv);
6619
6620                 if (dev->irq)
6621                         free_irq(dev->irq, priv);
6622
6623                 ipw2100_kill_workqueue(priv);
6624
6625                 /* These are safe to call even if they weren't allocated */
6626                 ipw2100_queues_free(priv);
6627                 sysfs_remove_group(&pci_dev->dev.kobj, &ipw2100_attribute_group);
6628
6629                 free_ieee80211(dev);
6630                 pci_set_drvdata(pci_dev, NULL);
6631         }
6632
6633         if (base_addr)
6634                 iounmap((char*)base_addr);
6635
6636         pci_release_regions(pci_dev);
6637         pci_disable_device(pci_dev);
6638
6639         return err;
6640 }
6641
6642 static void __devexit ipw2100_pci_remove_one(struct pci_dev *pci_dev)
6643 {
6644         struct ipw2100_priv *priv = pci_get_drvdata(pci_dev);
6645         struct net_device *dev;
6646
6647         if (priv) {
6648                 down(&priv->action_sem);
6649
6650                 priv->status &= ~STATUS_INITIALIZED;
6651
6652                 dev = priv->net_dev;
6653                 sysfs_remove_group(&pci_dev->dev.kobj, &ipw2100_attribute_group);
6654
6655 #ifdef CONFIG_PM
6656                 if (ipw2100_firmware.version)
6657                         ipw2100_release_firmware(priv, &ipw2100_firmware);
6658 #endif
6659                 /* Take down the hardware */
6660                 ipw2100_down(priv);
6661
6662                 /* Release the semaphore so that the network subsystem can
6663                  * complete any needed calls into the driver... */
6664                 up(&priv->action_sem);
6665
6666                 /* Unregister the device first - this results in close()
6667                  * being called if the device is open.  If we free storage
6668                  * first, then close() will crash. */
6669                 unregister_netdev(dev);
6670
6671                 /* ipw2100_down will ensure that there is no more pending work
6672                  * in the workqueue's, so we can safely remove them now. */
6673                 ipw2100_kill_workqueue(priv);
6674
6675                 ipw2100_queues_free(priv);
6676
6677                 /* Free potential debugging firmware snapshot */
6678                 ipw2100_snapshot_free(priv);
6679
6680                 if (dev->irq)
6681                         free_irq(dev->irq, priv);
6682
6683                 if (dev->base_addr)
6684                         iounmap((unsigned char *)dev->base_addr);
6685
6686                 free_ieee80211(dev);
6687         }
6688
6689         pci_release_regions(pci_dev);
6690         pci_disable_device(pci_dev);
6691
6692         IPW_DEBUG_INFO("exit\n");
6693 }
6694
6695
6696 #ifdef CONFIG_PM
6697 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
6698 static int ipw2100_suspend(struct pci_dev *pci_dev, u32 state)
6699 #else
6700 static int ipw2100_suspend(struct pci_dev *pci_dev, pm_message_t state)
6701 #endif
6702 {
6703         struct ipw2100_priv *priv = pci_get_drvdata(pci_dev);
6704         struct net_device *dev = priv->net_dev;
6705
6706         IPW_DEBUG_INFO("%s: Going into suspend...\n",
6707                dev->name);
6708
6709         down(&priv->action_sem);
6710         if (priv->status & STATUS_INITIALIZED) {
6711                 /* Take down the device; powers it off, etc. */
6712                 ipw2100_down(priv);
6713         }
6714
6715         /* Remove the PRESENT state of the device */
6716         netif_device_detach(dev);
6717
6718         pci_save_state(pci_dev);
6719         pci_disable_device (pci_dev);
6720         pci_set_power_state(pci_dev, PCI_D3hot);
6721
6722         up(&priv->action_sem);
6723
6724         return 0;
6725 }
6726
6727 static int ipw2100_resume(struct pci_dev *pci_dev)
6728 {
6729         struct ipw2100_priv *priv = pci_get_drvdata(pci_dev);
6730         struct net_device *dev = priv->net_dev;
6731         u32 val;
6732
6733         if (IPW2100_PM_DISABLED)
6734                 return 0;
6735
6736         down(&priv->action_sem);
6737
6738         IPW_DEBUG_INFO("%s: Coming out of suspend...\n",
6739                dev->name);
6740
6741         pci_set_power_state(pci_dev, PCI_D0);
6742         pci_enable_device(pci_dev);
6743         pci_restore_state(pci_dev);
6744
6745         /*
6746          * Suspend/Resume resets the PCI configuration space, so we have to
6747          * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
6748          * from interfering with C3 CPU state. pci_restore_state won't help
6749          * here since it only restores the first 64 bytes pci config header.
6750          */
6751         pci_read_config_dword(pci_dev, 0x40, &val);
6752         if ((val & 0x0000ff00) != 0)
6753                 pci_write_config_dword(pci_dev, 0x40, val & 0xffff00ff);
6754
6755         /* Set the device back into the PRESENT state; this will also wake
6756          * the queue of needed */
6757         netif_device_attach(dev);
6758
6759         /* Bring the device back up */
6760         if (!(priv->status & STATUS_RF_KILL_SW))
6761                 ipw2100_up(priv, 0);
6762
6763         up(&priv->action_sem);
6764
6765         return 0;
6766 }
6767 #endif
6768
6769
6770 #define IPW2100_DEV_ID(x) { PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, x }
6771
6772 static struct pci_device_id ipw2100_pci_id_table[] __devinitdata = {
6773         IPW2100_DEV_ID(0x2520), /* IN 2100A mPCI 3A */
6774         IPW2100_DEV_ID(0x2521), /* IN 2100A mPCI 3B */
6775         IPW2100_DEV_ID(0x2524), /* IN 2100A mPCI 3B */
6776         IPW2100_DEV_ID(0x2525), /* IN 2100A mPCI 3B */
6777         IPW2100_DEV_ID(0x2526), /* IN 2100A mPCI Gen A3 */
6778         IPW2100_DEV_ID(0x2522), /* IN 2100 mPCI 3B */
6779         IPW2100_DEV_ID(0x2523), /* IN 2100 mPCI 3A */
6780         IPW2100_DEV_ID(0x2527), /* IN 2100 mPCI 3B */
6781         IPW2100_DEV_ID(0x2528), /* IN 2100 mPCI 3B */
6782         IPW2100_DEV_ID(0x2529), /* IN 2100 mPCI 3B */
6783         IPW2100_DEV_ID(0x252B), /* IN 2100 mPCI 3A */
6784         IPW2100_DEV_ID(0x252C), /* IN 2100 mPCI 3A */
6785         IPW2100_DEV_ID(0x252D), /* IN 2100 mPCI 3A */
6786
6787         IPW2100_DEV_ID(0x2550), /* IB 2100A mPCI 3B */
6788         IPW2100_DEV_ID(0x2551), /* IB 2100 mPCI 3B */
6789         IPW2100_DEV_ID(0x2553), /* IB 2100 mPCI 3B */
6790         IPW2100_DEV_ID(0x2554), /* IB 2100 mPCI 3B */
6791         IPW2100_DEV_ID(0x2555), /* IB 2100 mPCI 3B */
6792
6793         IPW2100_DEV_ID(0x2560), /* DE 2100A mPCI 3A */
6794         IPW2100_DEV_ID(0x2562), /* DE 2100A mPCI 3A */
6795         IPW2100_DEV_ID(0x2563), /* DE 2100A mPCI 3A */
6796         IPW2100_DEV_ID(0x2561), /* DE 2100 mPCI 3A */
6797         IPW2100_DEV_ID(0x2565), /* DE 2100 mPCI 3A */
6798         IPW2100_DEV_ID(0x2566), /* DE 2100 mPCI 3A */
6799         IPW2100_DEV_ID(0x2567), /* DE 2100 mPCI 3A */
6800
6801         IPW2100_DEV_ID(0x2570), /* GA 2100 mPCI 3B */
6802
6803         IPW2100_DEV_ID(0x2580), /* TO 2100A mPCI 3B */
6804         IPW2100_DEV_ID(0x2582), /* TO 2100A mPCI 3B */
6805         IPW2100_DEV_ID(0x2583), /* TO 2100A mPCI 3B */
6806         IPW2100_DEV_ID(0x2581), /* TO 2100 mPCI 3B */
6807         IPW2100_DEV_ID(0x2585), /* TO 2100 mPCI 3B */
6808         IPW2100_DEV_ID(0x2586), /* TO 2100 mPCI 3B */
6809         IPW2100_DEV_ID(0x2587), /* TO 2100 mPCI 3B */
6810
6811         IPW2100_DEV_ID(0x2590), /* SO 2100A mPCI 3B */
6812         IPW2100_DEV_ID(0x2592), /* SO 2100A mPCI 3B */
6813         IPW2100_DEV_ID(0x2591), /* SO 2100 mPCI 3B */
6814         IPW2100_DEV_ID(0x2593), /* SO 2100 mPCI 3B */
6815         IPW2100_DEV_ID(0x2596), /* SO 2100 mPCI 3B */
6816         IPW2100_DEV_ID(0x2598), /* SO 2100 mPCI 3B */
6817
6818         IPW2100_DEV_ID(0x25A0), /* HP 2100 mPCI 3B */
6819         {0,},
6820 };
6821
6822 MODULE_DEVICE_TABLE(pci, ipw2100_pci_id_table);
6823
6824 static struct pci_driver ipw2100_pci_driver = {
6825         .name = DRV_NAME,
6826         .id_table = ipw2100_pci_id_table,
6827         .probe = ipw2100_pci_init_one,
6828         .remove = __devexit_p(ipw2100_pci_remove_one),
6829 #ifdef CONFIG_PM
6830         .suspend = ipw2100_suspend,
6831         .resume = ipw2100_resume,
6832 #endif
6833 };
6834
6835
6836 /**
6837  * Initialize the ipw2100 driver/module
6838  *
6839  * @returns 0 if ok, < 0 errno node con error.
6840  *
6841  * Note: we cannot init the /proc stuff until the PCI driver is there,
6842  * or we risk an unlikely race condition on someone accessing
6843  * uninitialized data in the PCI dev struct through /proc.
6844  */
6845 static int __init ipw2100_init(void)
6846 {
6847         int ret;
6848
6849         printk(KERN_INFO DRV_NAME ": %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
6850         printk(KERN_INFO DRV_NAME ": %s\n", DRV_COPYRIGHT);
6851
6852 #ifdef CONFIG_IEEE80211_NOWEP
6853         IPW_DEBUG_INFO(DRV_NAME ": Compiled with WEP disabled.\n");
6854 #endif
6855
6856         ret = pci_module_init(&ipw2100_pci_driver);
6857
6858 #ifdef CONFIG_IPW_DEBUG
6859         ipw2100_debug_level = debug;
6860         driver_create_file(&ipw2100_pci_driver.driver,
6861                            &driver_attr_debug_level);
6862 #endif
6863
6864         return ret;
6865 }
6866
6867
6868 /**
6869  * Cleanup ipw2100 driver registration
6870  */
6871 static void __exit ipw2100_exit(void)
6872 {
6873         /* FIXME: IPG: check that we have no instances of the devices open */
6874 #ifdef CONFIG_IPW_DEBUG
6875         driver_remove_file(&ipw2100_pci_driver.driver,
6876                            &driver_attr_debug_level);
6877 #endif
6878         pci_unregister_driver(&ipw2100_pci_driver);
6879 }
6880
6881 module_init(ipw2100_init);
6882 module_exit(ipw2100_exit);
6883
6884 #define WEXT_USECHANNELS 1
6885
6886 const long ipw2100_frequencies[] = {
6887         2412, 2417, 2422, 2427,
6888         2432, 2437, 2442, 2447,
6889         2452, 2457, 2462, 2467,
6890         2472, 2484
6891 };
6892
6893 #define FREQ_COUNT (sizeof(ipw2100_frequencies) / \
6894                     sizeof(ipw2100_frequencies[0]))
6895
6896 const long ipw2100_rates_11b[] = {
6897         1000000,
6898         2000000,
6899         5500000,
6900         11000000
6901 };
6902
6903 #define RATE_COUNT (sizeof(ipw2100_rates_11b) / sizeof(ipw2100_rates_11b[0]))
6904
6905 static int ipw2100_wx_get_name(struct net_device *dev,
6906                                struct iw_request_info *info,
6907                                union iwreq_data *wrqu, char *extra)
6908 {
6909         /*
6910          * This can be called at any time.  No action lock required
6911          */
6912
6913         struct ipw2100_priv *priv = ieee80211_priv(dev);
6914         if (!(priv->status & STATUS_ASSOCIATED))
6915                 strcpy(wrqu->name, "unassociated");
6916         else
6917                 snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11b");
6918
6919         IPW_DEBUG_WX("Name: %s\n", wrqu->name);
6920         return 0;
6921 }
6922
6923
6924 static int ipw2100_wx_set_freq(struct net_device *dev,
6925                                struct iw_request_info *info,
6926                                union iwreq_data *wrqu, char *extra)
6927 {
6928         struct ipw2100_priv *priv = ieee80211_priv(dev);
6929         struct iw_freq *fwrq = &wrqu->freq;
6930         int err = 0;
6931
6932         if (priv->ieee->iw_mode == IW_MODE_INFRA)
6933                 return -EOPNOTSUPP;
6934
6935         down(&priv->action_sem);
6936         if (!(priv->status & STATUS_INITIALIZED)) {
6937                 err = -EIO;
6938                 goto done;
6939         }
6940
6941         /* if setting by freq convert to channel */
6942         if (fwrq->e == 1) {
6943                 if ((fwrq->m >= (int) 2.412e8 &&
6944                      fwrq->m <= (int) 2.487e8)) {
6945                         int f = fwrq->m / 100000;
6946                         int c = 0;
6947
6948                         while ((c < REG_MAX_CHANNEL) &&
6949                                (f != ipw2100_frequencies[c]))
6950                                 c++;
6951
6952                         /* hack to fall through */
6953                         fwrq->e = 0;
6954                         fwrq->m = c + 1;
6955                 }
6956         }
6957
6958         if (fwrq->e > 0 || fwrq->m > 1000)
6959                 return -EOPNOTSUPP;
6960         else { /* Set the channel */
6961                 IPW_DEBUG_WX("SET Freq/Channel -> %d \n", fwrq->m);
6962                 err = ipw2100_set_channel(priv, fwrq->m, 0);
6963         }
6964
6965  done:
6966         up(&priv->action_sem);
6967         return err;
6968 }
6969
6970
6971 static int ipw2100_wx_get_freq(struct net_device *dev,
6972                                struct iw_request_info *info,
6973                                union iwreq_data *wrqu, char *extra)
6974 {
6975         /*
6976          * This can be called at any time.  No action lock required
6977          */
6978
6979         struct ipw2100_priv *priv = ieee80211_priv(dev);
6980
6981         wrqu->freq.e = 0;
6982
6983         /* If we are associated, trying to associate, or have a statically
6984          * configured CHANNEL then return that; otherwise return ANY */
6985         if (priv->config & CFG_STATIC_CHANNEL ||
6986             priv->status & STATUS_ASSOCIATED)
6987                 wrqu->freq.m = priv->channel;
6988         else
6989                 wrqu->freq.m = 0;
6990
6991         IPW_DEBUG_WX("GET Freq/Channel -> %d \n", priv->channel);
6992         return 0;
6993
6994 }
6995
6996 static int ipw2100_wx_set_mode(struct net_device *dev,
6997                                struct iw_request_info *info,
6998                                union iwreq_data *wrqu, char *extra)
6999 {
7000         struct ipw2100_priv *priv = ieee80211_priv(dev);
7001         int err = 0;
7002
7003         IPW_DEBUG_WX("SET Mode -> %d \n", wrqu->mode);
7004
7005         if (wrqu->mode == priv->ieee->iw_mode)
7006                 return 0;
7007
7008         down(&priv->action_sem);
7009         if (!(priv->status & STATUS_INITIALIZED)) {
7010                 err = -EIO;
7011                 goto done;
7012         }
7013
7014         switch (wrqu->mode) {
7015 #ifdef CONFIG_IPW2100_MONITOR
7016         case IW_MODE_MONITOR:
7017                 err = ipw2100_switch_mode(priv, IW_MODE_MONITOR);
7018                 break;
7019 #endif /* CONFIG_IPW2100_MONITOR */
7020         case IW_MODE_ADHOC:
7021                 err = ipw2100_switch_mode(priv, IW_MODE_ADHOC);
7022                 break;
7023         case IW_MODE_INFRA:
7024         case IW_MODE_AUTO:
7025         default:
7026                 err = ipw2100_switch_mode(priv, IW_MODE_INFRA);
7027                 break;
7028         }
7029
7030 done:
7031         up(&priv->action_sem);
7032         return err;
7033 }
7034
7035 static int ipw2100_wx_get_mode(struct net_device *dev,
7036                                struct iw_request_info *info,
7037                                union iwreq_data *wrqu, char *extra)
7038 {
7039         /*
7040          * This can be called at any time.  No action lock required
7041          */
7042
7043         struct ipw2100_priv *priv = ieee80211_priv(dev);
7044
7045         wrqu->mode = priv->ieee->iw_mode;
7046         IPW_DEBUG_WX("GET Mode -> %d\n", wrqu->mode);
7047
7048         return 0;
7049 }
7050
7051
7052 #define POWER_MODES 5
7053
7054 /* Values are in microsecond */
7055 const s32 timeout_duration[POWER_MODES] = {
7056         350000,
7057         250000,
7058         75000,
7059         37000,
7060         25000,
7061 };
7062
7063 const s32 period_duration[POWER_MODES] = {
7064         400000,
7065         700000,
7066         1000000,
7067         1000000,
7068         1000000
7069 };
7070
7071 static int ipw2100_wx_get_range(struct net_device *dev,
7072                                 struct iw_request_info *info,
7073                                 union iwreq_data *wrqu, char *extra)
7074 {
7075         /*
7076          * This can be called at any time.  No action lock required
7077          */
7078
7079         struct ipw2100_priv *priv = ieee80211_priv(dev);
7080         struct iw_range *range = (struct iw_range *)extra;
7081         u16 val;
7082         int i, level;
7083
7084         wrqu->data.length = sizeof(*range);
7085         memset(range, 0, sizeof(*range));
7086
7087         /* Let's try to keep this struct in the same order as in
7088          * linux/include/wireless.h
7089          */
7090
7091         /* TODO: See what values we can set, and remove the ones we can't
7092          * set, or fill them with some default data.
7093          */
7094
7095         /* ~5 Mb/s real (802.11b) */
7096         range->throughput = 5 * 1000 * 1000;
7097
7098 //      range->sensitivity;     /* signal level threshold range */
7099
7100         range->max_qual.qual = 100;
7101         /* TODO: Find real max RSSI and stick here */
7102         range->max_qual.level = 0;
7103         range->max_qual.noise = 0;
7104         range->max_qual.updated = 7; /* Updated all three */
7105
7106         range->avg_qual.qual = 70; /* > 8% missed beacons is 'bad' */
7107         /* TODO: Find real 'good' to 'bad' threshol value for RSSI */
7108         range->avg_qual.level = 20 + IPW2100_RSSI_TO_DBM;
7109         range->avg_qual.noise = 0;
7110         range->avg_qual.updated = 7; /* Updated all three */
7111
7112         range->num_bitrates = RATE_COUNT;
7113
7114         for (i = 0; i < RATE_COUNT && i < IW_MAX_BITRATES; i++) {
7115                 range->bitrate[i] = ipw2100_rates_11b[i];
7116         }
7117
7118         range->min_rts = MIN_RTS_THRESHOLD;
7119         range->max_rts = MAX_RTS_THRESHOLD;
7120         range->min_frag = MIN_FRAG_THRESHOLD;
7121         range->max_frag = MAX_FRAG_THRESHOLD;
7122
7123         range->min_pmp = period_duration[0];    /* Minimal PM period */
7124         range->max_pmp = period_duration[POWER_MODES-1];/* Maximal PM period */
7125         range->min_pmt = timeout_duration[POWER_MODES-1];       /* Minimal PM timeout */
7126         range->max_pmt = timeout_duration[0];/* Maximal PM timeout */
7127
7128         /* How to decode max/min PM period */
7129         range->pmp_flags = IW_POWER_PERIOD;
7130         /* How to decode max/min PM period */
7131         range->pmt_flags = IW_POWER_TIMEOUT;
7132         /* What PM options are supported */
7133         range->pm_capa = IW_POWER_TIMEOUT | IW_POWER_PERIOD;
7134
7135         range->encoding_size[0] = 5;
7136         range->encoding_size[1] = 13;           /* Different token sizes */
7137         range->num_encoding_sizes = 2;          /* Number of entry in the list */
7138         range->max_encoding_tokens = WEP_KEYS;  /* Max number of tokens */
7139 //      range->encoding_login_index;            /* token index for login token */
7140
7141         if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
7142                 range->txpower_capa = IW_TXPOW_DBM;
7143                 range->num_txpower = IW_MAX_TXPOWER;
7144                 for (i = 0, level = (IPW_TX_POWER_MAX_DBM * 16); i < IW_MAX_TXPOWER;
7145                      i++, level -= ((IPW_TX_POWER_MAX_DBM - IPW_TX_POWER_MIN_DBM) * 16) /
7146                              (IW_MAX_TXPOWER - 1))
7147                         range->txpower[i] = level / 16;
7148         } else {
7149                 range->txpower_capa = 0;
7150                 range->num_txpower = 0;
7151         }
7152
7153
7154         /* Set the Wireless Extension versions */
7155         range->we_version_compiled = WIRELESS_EXT;
7156         range->we_version_source = 16;
7157
7158 //      range->retry_capa;      /* What retry options are supported */
7159 //      range->retry_flags;     /* How to decode max/min retry limit */
7160 //      range->r_time_flags;    /* How to decode max/min retry life */
7161 //      range->min_retry;       /* Minimal number of retries */
7162 //      range->max_retry;       /* Maximal number of retries */
7163 //      range->min_r_time;      /* Minimal retry lifetime */
7164 //      range->max_r_time;      /* Maximal retry lifetime */
7165
7166         range->num_channels = FREQ_COUNT;
7167
7168         val = 0;
7169         for (i = 0; i < FREQ_COUNT; i++) {
7170                 // TODO: Include only legal frequencies for some countries
7171 //              if (local->channel_mask & (1 << i)) {
7172                         range->freq[val].i = i + 1;
7173                         range->freq[val].m = ipw2100_frequencies[i] * 100000;
7174                         range->freq[val].e = 1;
7175                         val++;
7176 //              }
7177                 if (val == IW_MAX_FREQUENCIES)
7178                 break;
7179         }
7180         range->num_frequency = val;
7181
7182         IPW_DEBUG_WX("GET Range\n");
7183
7184         return 0;
7185 }
7186
7187 static int ipw2100_wx_set_wap(struct net_device *dev,
7188                               struct iw_request_info *info,
7189                               union iwreq_data *wrqu, char *extra)
7190 {
7191         struct ipw2100_priv *priv = ieee80211_priv(dev);
7192         int err = 0;
7193
7194         static const unsigned char any[] = {
7195                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
7196         };
7197         static const unsigned char off[] = {
7198                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
7199         };
7200
7201         // sanity checks
7202         if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
7203                 return -EINVAL;
7204
7205         down(&priv->action_sem);
7206         if (!(priv->status & STATUS_INITIALIZED)) {
7207                 err = -EIO;
7208                 goto done;
7209         }
7210
7211         if (!memcmp(any, wrqu->ap_addr.sa_data, ETH_ALEN) ||
7212             !memcmp(off, wrqu->ap_addr.sa_data, ETH_ALEN)) {
7213                 /* we disable mandatory BSSID association */
7214                 IPW_DEBUG_WX("exit - disable mandatory BSSID\n");
7215                 priv->config &= ~CFG_STATIC_BSSID;
7216                 err = ipw2100_set_mandatory_bssid(priv, NULL, 0);
7217                 goto done;
7218         }
7219
7220         priv->config |= CFG_STATIC_BSSID;
7221         memcpy(priv->mandatory_bssid_mac, wrqu->ap_addr.sa_data, ETH_ALEN);
7222
7223         err = ipw2100_set_mandatory_bssid(priv, wrqu->ap_addr.sa_data, 0);
7224
7225         IPW_DEBUG_WX("SET BSSID -> %02X:%02X:%02X:%02X:%02X:%02X\n",
7226                      wrqu->ap_addr.sa_data[0] & 0xff,
7227                      wrqu->ap_addr.sa_data[1] & 0xff,
7228                      wrqu->ap_addr.sa_data[2] & 0xff,
7229                      wrqu->ap_addr.sa_data[3] & 0xff,
7230                      wrqu->ap_addr.sa_data[4] & 0xff,
7231                      wrqu->ap_addr.sa_data[5] & 0xff);
7232
7233  done:
7234         up(&priv->action_sem);
7235         return err;
7236 }
7237
7238 static int ipw2100_wx_get_wap(struct net_device *dev,
7239                               struct iw_request_info *info,
7240                               union iwreq_data *wrqu, char *extra)
7241 {
7242         /*
7243          * This can be called at any time.  No action lock required
7244          */
7245
7246         struct ipw2100_priv *priv = ieee80211_priv(dev);
7247
7248         /* If we are associated, trying to associate, or have a statically
7249          * configured BSSID then return that; otherwise return ANY */
7250         if (priv->config & CFG_STATIC_BSSID ||
7251             priv->status & STATUS_ASSOCIATED) {
7252                 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
7253                 memcpy(wrqu->ap_addr.sa_data, &priv->bssid, ETH_ALEN);
7254         } else
7255                 memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);
7256
7257         IPW_DEBUG_WX("Getting WAP BSSID: " MAC_FMT "\n",
7258                      MAC_ARG(wrqu->ap_addr.sa_data));
7259         return 0;
7260 }
7261
7262 static int ipw2100_wx_set_essid(struct net_device *dev,
7263                                 struct iw_request_info *info,
7264                                 union iwreq_data *wrqu, char *extra)
7265 {
7266         struct ipw2100_priv *priv = ieee80211_priv(dev);
7267         char *essid = ""; /* ANY */
7268         int length = 0;
7269         int err = 0;
7270
7271         down(&priv->action_sem);
7272         if (!(priv->status & STATUS_INITIALIZED)) {
7273                 err = -EIO;
7274                 goto done;
7275         }
7276
7277         if (wrqu->essid.flags && wrqu->essid.length) {
7278                 length = wrqu->essid.length - 1;
7279                 essid = extra;
7280         }
7281
7282         if (length == 0) {
7283                 IPW_DEBUG_WX("Setting ESSID to ANY\n");
7284                 priv->config &= ~CFG_STATIC_ESSID;
7285                 err = ipw2100_set_essid(priv, NULL, 0, 0);
7286                 goto done;
7287         }
7288
7289         length = min(length, IW_ESSID_MAX_SIZE);
7290
7291         priv->config |= CFG_STATIC_ESSID;
7292
7293         if (priv->essid_len == length && !memcmp(priv->essid, extra, length)) {
7294                 IPW_DEBUG_WX("ESSID set to current ESSID.\n");
7295                 err = 0;
7296                 goto done;
7297         }
7298
7299         IPW_DEBUG_WX("Setting ESSID: '%s' (%d)\n", escape_essid(essid, length),
7300                      length);
7301
7302         priv->essid_len = length;
7303         memcpy(priv->essid, essid, priv->essid_len);
7304
7305         err = ipw2100_set_essid(priv, essid, length, 0);
7306
7307  done:
7308         up(&priv->action_sem);
7309         return err;
7310 }
7311
7312 static int ipw2100_wx_get_essid(struct net_device *dev,
7313                                 struct iw_request_info *info,
7314                                 union iwreq_data *wrqu, char *extra)
7315 {
7316         /*
7317          * This can be called at any time.  No action lock required
7318          */
7319
7320         struct ipw2100_priv *priv = ieee80211_priv(dev);
7321
7322         /* If we are associated, trying to associate, or have a statically
7323          * configured ESSID then return that; otherwise return ANY */
7324         if (priv->config & CFG_STATIC_ESSID ||
7325             priv->status & STATUS_ASSOCIATED) {
7326                 IPW_DEBUG_WX("Getting essid: '%s'\n",
7327                              escape_essid(priv->essid, priv->essid_len));
7328                 memcpy(extra, priv->essid, priv->essid_len);
7329                 wrqu->essid.length = priv->essid_len;
7330                 wrqu->essid.flags = 1; /* active */
7331         } else {
7332                 IPW_DEBUG_WX("Getting essid: ANY\n");
7333                 wrqu->essid.length = 0;
7334                 wrqu->essid.flags = 0; /* active */
7335         }
7336
7337         return 0;
7338 }
7339
7340 static int ipw2100_wx_set_nick(struct net_device *dev,
7341                                struct iw_request_info *info,
7342                                union iwreq_data *wrqu, char *extra)
7343 {
7344         /*
7345          * This can be called at any time.  No action lock required
7346          */
7347
7348         struct ipw2100_priv *priv = ieee80211_priv(dev);
7349
7350         if (wrqu->data.length > IW_ESSID_MAX_SIZE)
7351                 return -E2BIG;
7352
7353         wrqu->data.length = min((size_t)wrqu->data.length, sizeof(priv->nick));
7354         memset(priv->nick, 0, sizeof(priv->nick));
7355         memcpy(priv->nick, extra,  wrqu->data.length);
7356
7357         IPW_DEBUG_WX("SET Nickname -> %s \n", priv->nick);
7358
7359         return 0;
7360 }
7361
7362 static int ipw2100_wx_get_nick(struct net_device *dev,
7363                                struct iw_request_info *info,
7364                                union iwreq_data *wrqu, char *extra)
7365 {
7366         /*
7367          * This can be called at any time.  No action lock required
7368          */
7369
7370         struct ipw2100_priv *priv = ieee80211_priv(dev);
7371
7372         wrqu->data.length = strlen(priv->nick) + 1;
7373         memcpy(extra, priv->nick, wrqu->data.length);
7374         wrqu->data.flags = 1; /* active */
7375
7376         IPW_DEBUG_WX("GET Nickname -> %s \n", extra);
7377
7378         return 0;
7379 }
7380
7381 static int ipw2100_wx_set_rate(struct net_device *dev,
7382                                struct iw_request_info *info,
7383                                union iwreq_data *wrqu, char *extra)
7384 {
7385         struct ipw2100_priv *priv = ieee80211_priv(dev);
7386         u32 target_rate = wrqu->bitrate.value;
7387         u32 rate;
7388         int err = 0;
7389
7390         down(&priv->action_sem);
7391         if (!(priv->status & STATUS_INITIALIZED)) {
7392                 err = -EIO;
7393                 goto done;
7394         }
7395
7396         rate = 0;
7397
7398         if (target_rate == 1000000 ||
7399             (!wrqu->bitrate.fixed && target_rate > 1000000))
7400                 rate |= TX_RATE_1_MBIT;
7401         if (target_rate == 2000000 ||
7402             (!wrqu->bitrate.fixed && target_rate > 2000000))
7403                 rate |= TX_RATE_2_MBIT;
7404         if (target_rate == 5500000 ||
7405             (!wrqu->bitrate.fixed && target_rate > 5500000))
7406                 rate |= TX_RATE_5_5_MBIT;
7407         if (target_rate == 11000000 ||
7408             (!wrqu->bitrate.fixed && target_rate > 11000000))
7409                 rate |= TX_RATE_11_MBIT;
7410         if (rate == 0)
7411                 rate = DEFAULT_TX_RATES;
7412
7413         err = ipw2100_set_tx_rates(priv, rate, 0);
7414
7415         IPW_DEBUG_WX("SET Rate -> %04X \n", rate);
7416  done:
7417         up(&priv->action_sem);
7418         return err;
7419 }
7420
7421
7422 static int ipw2100_wx_get_rate(struct net_device *dev,
7423                                struct iw_request_info *info,
7424                                union iwreq_data *wrqu, char *extra)
7425 {
7426         struct ipw2100_priv *priv = ieee80211_priv(dev);
7427         int val;
7428         int len = sizeof(val);
7429         int err = 0;
7430
7431         if (!(priv->status & STATUS_ENABLED) ||
7432             priv->status & STATUS_RF_KILL_MASK ||
7433             !(priv->status & STATUS_ASSOCIATED)) {
7434                 wrqu->bitrate.value = 0;
7435                 return 0;
7436         }
7437
7438         down(&priv->action_sem);
7439         if (!(priv->status & STATUS_INITIALIZED)) {
7440                 err = -EIO;
7441                 goto done;
7442         }
7443
7444         err = ipw2100_get_ordinal(priv, IPW_ORD_CURRENT_TX_RATE, &val, &len);
7445         if (err) {
7446                 IPW_DEBUG_WX("failed querying ordinals.\n");
7447                 return err;
7448         }
7449
7450         switch (val & TX_RATE_MASK) {
7451         case TX_RATE_1_MBIT:
7452                 wrqu->bitrate.value = 1000000;
7453                 break;
7454         case TX_RATE_2_MBIT:
7455                 wrqu->bitrate.value = 2000000;
7456                 break;
7457         case TX_RATE_5_5_MBIT:
7458                 wrqu->bitrate.value = 5500000;
7459                 break;
7460         case TX_RATE_11_MBIT:
7461                 wrqu->bitrate.value = 11000000;
7462                 break;
7463         default:
7464                 wrqu->bitrate.value = 0;
7465         }
7466
7467         IPW_DEBUG_WX("GET Rate -> %d \n", wrqu->bitrate.value);
7468
7469  done:
7470         up(&priv->action_sem);
7471         return err;
7472 }
7473
7474 static int ipw2100_wx_set_rts(struct net_device *dev,
7475                               struct iw_request_info *info,
7476                               union iwreq_data *wrqu, char *extra)
7477 {
7478         struct ipw2100_priv *priv = ieee80211_priv(dev);
7479         int value, err;
7480
7481         /* Auto RTS not yet supported */
7482         if (wrqu->rts.fixed == 0)
7483                 return -EINVAL;
7484
7485         down(&priv->action_sem);
7486         if (!(priv->status & STATUS_INITIALIZED)) {
7487                 err = -EIO;
7488                 goto done;
7489         }
7490
7491         if (wrqu->rts.disabled)
7492                 value = priv->rts_threshold | RTS_DISABLED;
7493         else {
7494                 if (wrqu->rts.value < 1 ||
7495                     wrqu->rts.value > 2304) {
7496                         err = -EINVAL;
7497                         goto done;
7498                 }
7499                 value = wrqu->rts.value;
7500         }
7501
7502         err = ipw2100_set_rts_threshold(priv, value);
7503
7504         IPW_DEBUG_WX("SET RTS Threshold -> 0x%08X \n", value);
7505  done:
7506         up(&priv->action_sem);
7507         return err;
7508 }
7509
7510 static int ipw2100_wx_get_rts(struct net_device *dev,
7511                               struct iw_request_info *info,
7512                               union iwreq_data *wrqu, char *extra)
7513 {
7514         /*
7515          * This can be called at any time.  No action lock required
7516          */
7517
7518         struct ipw2100_priv *priv = ieee80211_priv(dev);
7519
7520         wrqu->rts.value = priv->rts_threshold & ~RTS_DISABLED;
7521         wrqu->rts.fixed = 1; /* no auto select */
7522
7523         /* If RTS is set to the default value, then it is disabled */
7524         wrqu->rts.disabled = (priv->rts_threshold & RTS_DISABLED) ? 1 : 0;
7525
7526         IPW_DEBUG_WX("GET RTS Threshold -> 0x%08X \n", wrqu->rts.value);
7527
7528         return 0;
7529 }
7530
7531 static int ipw2100_wx_set_txpow(struct net_device *dev,
7532                                 struct iw_request_info *info,
7533                                 union iwreq_data *wrqu, char *extra)
7534 {
7535         struct ipw2100_priv *priv = ieee80211_priv(dev);
7536         int err = 0, value;
7537
7538         if (priv->ieee->iw_mode != IW_MODE_ADHOC)
7539                 return -EINVAL;
7540
7541         if (wrqu->txpower.disabled == 1 || wrqu->txpower.fixed == 0)
7542                 value = IPW_TX_POWER_DEFAULT;
7543         else {
7544                 if (wrqu->txpower.value < IPW_TX_POWER_MIN_DBM ||
7545                     wrqu->txpower.value > IPW_TX_POWER_MAX_DBM)
7546                         return -EINVAL;
7547
7548                 value = (wrqu->txpower.value - IPW_TX_POWER_MIN_DBM) * 16 /
7549                         (IPW_TX_POWER_MAX_DBM - IPW_TX_POWER_MIN_DBM);
7550         }
7551
7552         down(&priv->action_sem);
7553         if (!(priv->status & STATUS_INITIALIZED)) {
7554                 err = -EIO;
7555                 goto done;
7556         }
7557
7558         err = ipw2100_set_tx_power(priv, value);
7559
7560         IPW_DEBUG_WX("SET TX Power -> %d \n", value);
7561
7562  done:
7563         up(&priv->action_sem);
7564         return err;
7565 }
7566
7567 static int ipw2100_wx_get_txpow(struct net_device *dev,
7568                                 struct iw_request_info *info,
7569                                 union iwreq_data *wrqu, char *extra)
7570 {
7571         /*
7572          * This can be called at any time.  No action lock required
7573          */
7574
7575         struct ipw2100_priv *priv = ieee80211_priv(dev);
7576
7577         if (priv->ieee->iw_mode != IW_MODE_ADHOC) {
7578                 wrqu->power.disabled = 1;
7579                 return 0;
7580         }
7581
7582         if (priv->tx_power == IPW_TX_POWER_DEFAULT) {
7583                 wrqu->power.fixed = 0;
7584                 wrqu->power.value = IPW_TX_POWER_MAX_DBM;
7585                 wrqu->power.disabled = 1;
7586         } else {
7587                 wrqu->power.disabled = 0;
7588                 wrqu->power.fixed = 1;
7589                 wrqu->power.value =
7590                         (priv->tx_power *
7591                          (IPW_TX_POWER_MAX_DBM - IPW_TX_POWER_MIN_DBM)) /
7592                         (IPW_TX_POWER_MAX - IPW_TX_POWER_MIN) +
7593                         IPW_TX_POWER_MIN_DBM;
7594         }
7595
7596         wrqu->power.flags = IW_TXPOW_DBM;
7597
7598         IPW_DEBUG_WX("GET TX Power -> %d \n", wrqu->power.value);
7599
7600         return 0;
7601 }
7602
7603 static int ipw2100_wx_set_frag(struct net_device *dev,
7604                                struct iw_request_info *info,
7605                                union iwreq_data *wrqu, char *extra)
7606 {
7607         /*
7608          * This can be called at any time.  No action lock required
7609          */
7610
7611         struct ipw2100_priv *priv = ieee80211_priv(dev);
7612
7613         if (!wrqu->frag.fixed)
7614                 return -EINVAL;
7615
7616         if (wrqu->frag.disabled) {
7617                 priv->frag_threshold |= FRAG_DISABLED;
7618                 priv->ieee->fts = DEFAULT_FTS;
7619         } else {
7620                 if (wrqu->frag.value < MIN_FRAG_THRESHOLD ||
7621                     wrqu->frag.value > MAX_FRAG_THRESHOLD)
7622                         return -EINVAL;
7623
7624                 priv->ieee->fts = wrqu->frag.value & ~0x1;
7625                 priv->frag_threshold = priv->ieee->fts;
7626         }
7627
7628         IPW_DEBUG_WX("SET Frag Threshold -> %d \n", priv->ieee->fts);
7629
7630         return 0;
7631 }
7632
7633 static int ipw2100_wx_get_frag(struct net_device *dev,
7634                                struct iw_request_info *info,
7635                                union iwreq_data *wrqu, char *extra)
7636 {
7637         /*
7638          * This can be called at any time.  No action lock required
7639          */
7640
7641         struct ipw2100_priv *priv = ieee80211_priv(dev);
7642         wrqu->frag.value = priv->frag_threshold & ~FRAG_DISABLED;
7643         wrqu->frag.fixed = 0;   /* no auto select */
7644         wrqu->frag.disabled = (priv->frag_threshold & FRAG_DISABLED) ? 1 : 0;
7645
7646         IPW_DEBUG_WX("GET Frag Threshold -> %d \n", wrqu->frag.value);
7647
7648         return 0;
7649 }
7650
7651 static int ipw2100_wx_set_retry(struct net_device *dev,
7652                                 struct iw_request_info *info,
7653                                 union iwreq_data *wrqu, char *extra)
7654 {
7655         struct ipw2100_priv *priv = ieee80211_priv(dev);
7656         int err = 0;
7657
7658         if (wrqu->retry.flags & IW_RETRY_LIFETIME ||
7659             wrqu->retry.disabled)
7660                 return -EINVAL;
7661
7662         if (!(wrqu->retry.flags & IW_RETRY_LIMIT))
7663                 return 0;
7664
7665         down(&priv->action_sem);
7666         if (!(priv->status & STATUS_INITIALIZED)) {
7667                 err = -EIO;
7668                 goto done;
7669         }
7670
7671         if (wrqu->retry.flags & IW_RETRY_MIN) {
7672                 err = ipw2100_set_short_retry(priv, wrqu->retry.value);
7673                 IPW_DEBUG_WX("SET Short Retry Limit -> %d \n",
7674                        wrqu->retry.value);
7675                 goto done;
7676         }
7677
7678         if (wrqu->retry.flags & IW_RETRY_MAX) {
7679                 err = ipw2100_set_long_retry(priv, wrqu->retry.value);
7680                 IPW_DEBUG_WX("SET Long Retry Limit -> %d \n",
7681                        wrqu->retry.value);
7682                 goto done;
7683         }
7684
7685         err = ipw2100_set_short_retry(priv, wrqu->retry.value);
7686         if (!err)
7687                 err = ipw2100_set_long_retry(priv, wrqu->retry.value);
7688
7689         IPW_DEBUG_WX("SET Both Retry Limits -> %d \n", wrqu->retry.value);
7690
7691  done:
7692         up(&priv->action_sem);
7693         return err;
7694 }
7695
7696 static int ipw2100_wx_get_retry(struct net_device *dev,
7697                                 struct iw_request_info *info,
7698                                 union iwreq_data *wrqu, char *extra)
7699 {
7700         /*
7701          * This can be called at any time.  No action lock required
7702          */
7703
7704         struct ipw2100_priv *priv = ieee80211_priv(dev);
7705
7706         wrqu->retry.disabled = 0; /* can't be disabled */
7707
7708         if ((wrqu->retry.flags & IW_RETRY_TYPE) ==
7709             IW_RETRY_LIFETIME)
7710                 return -EINVAL;
7711
7712         if (wrqu->retry.flags & IW_RETRY_MAX) {
7713                 wrqu->retry.flags = IW_RETRY_LIMIT & IW_RETRY_MAX;
7714                 wrqu->retry.value = priv->long_retry_limit;
7715         } else {
7716                 wrqu->retry.flags =
7717                     (priv->short_retry_limit !=
7718                      priv->long_retry_limit) ?
7719                     IW_RETRY_LIMIT & IW_RETRY_MIN : IW_RETRY_LIMIT;
7720
7721                 wrqu->retry.value = priv->short_retry_limit;
7722         }
7723
7724         IPW_DEBUG_WX("GET Retry -> %d \n", wrqu->retry.value);
7725
7726         return 0;
7727 }
7728
7729 static int ipw2100_wx_set_scan(struct net_device *dev,
7730                                struct iw_request_info *info,
7731                                union iwreq_data *wrqu, char *extra)
7732 {
7733         struct ipw2100_priv *priv = ieee80211_priv(dev);
7734         int err = 0;
7735
7736         down(&priv->action_sem);
7737         if (!(priv->status & STATUS_INITIALIZED)) {
7738                 err = -EIO;
7739                 goto done;
7740         }
7741
7742         IPW_DEBUG_WX("Initiating scan...\n");
7743         if (ipw2100_set_scan_options(priv) ||
7744             ipw2100_start_scan(priv)) {
7745                 IPW_DEBUG_WX("Start scan failed.\n");
7746
7747                 /* TODO: Mark a scan as pending so when hardware initialized
7748                  *       a scan starts */
7749         }
7750
7751  done:
7752         up(&priv->action_sem);
7753         return err;
7754 }
7755
7756 static int ipw2100_wx_get_scan(struct net_device *dev,
7757                                struct iw_request_info *info,
7758                                union iwreq_data *wrqu, char *extra)
7759 {
7760         /*
7761          * This can be called at any time.  No action lock required
7762          */
7763
7764         struct ipw2100_priv *priv = ieee80211_priv(dev);
7765         return ieee80211_wx_get_scan(priv->ieee, info, wrqu, extra);
7766 }
7767
7768
7769 /*
7770  * Implementation based on code in hostap-driver v0.1.3 hostap_ioctl.c
7771  */
7772 static int ipw2100_wx_set_encode(struct net_device *dev,
7773                                  struct iw_request_info *info,
7774                                  union iwreq_data *wrqu, char *key)
7775 {
7776         /*
7777          * No check of STATUS_INITIALIZED required
7778          */
7779
7780         struct ipw2100_priv *priv = ieee80211_priv(dev);
7781         return ieee80211_wx_set_encode(priv->ieee, info, wrqu, key);
7782 }
7783
7784 static int ipw2100_wx_get_encode(struct net_device *dev,
7785                                  struct iw_request_info *info,
7786                                  union iwreq_data *wrqu, char *key)
7787 {
7788         /*
7789          * This can be called at any time.  No action lock required
7790          */
7791
7792         struct ipw2100_priv *priv = ieee80211_priv(dev);
7793         return ieee80211_wx_get_encode(priv->ieee, info, wrqu, key);
7794 }
7795
7796 static int ipw2100_wx_set_power(struct net_device *dev,
7797                                 struct iw_request_info *info,
7798                                 union iwreq_data *wrqu, char *extra)
7799 {
7800         struct ipw2100_priv *priv = ieee80211_priv(dev);
7801         int err = 0;
7802
7803         down(&priv->action_sem);
7804         if (!(priv->status & STATUS_INITIALIZED)) {
7805                 err = -EIO;
7806                 goto done;
7807         }
7808
7809         if (wrqu->power.disabled) {
7810                 priv->power_mode = IPW_POWER_LEVEL(priv->power_mode);
7811                 err = ipw2100_set_power_mode(priv, IPW_POWER_MODE_CAM);
7812                 IPW_DEBUG_WX("SET Power Management Mode -> off\n");
7813                 goto done;
7814         }
7815
7816         switch (wrqu->power.flags & IW_POWER_MODE) {
7817         case IW_POWER_ON:    /* If not specified */
7818         case IW_POWER_MODE:  /* If set all mask */
7819         case IW_POWER_ALL_R: /* If explicitely state all */
7820                 break;
7821         default: /* Otherwise we don't support it */
7822                 IPW_DEBUG_WX("SET PM Mode: %X not supported.\n",
7823                              wrqu->power.flags);
7824                 err = -EOPNOTSUPP;
7825                 goto done;
7826         }
7827
7828         /* If the user hasn't specified a power management mode yet, default
7829          * to BATTERY */
7830         priv->power_mode = IPW_POWER_ENABLED | priv->power_mode;
7831         err = ipw2100_set_power_mode(priv, IPW_POWER_LEVEL(priv->power_mode));
7832
7833         IPW_DEBUG_WX("SET Power Management Mode -> 0x%02X\n",
7834                      priv->power_mode);
7835
7836  done:
7837         up(&priv->action_sem);
7838         return err;
7839
7840 }
7841
7842 static int ipw2100_wx_get_power(struct net_device *dev,
7843                                 struct iw_request_info *info,
7844                                 union iwreq_data *wrqu, char *extra)
7845 {
7846         /*
7847          * This can be called at any time.  No action lock required
7848          */
7849
7850         struct ipw2100_priv *priv = ieee80211_priv(dev);
7851
7852         if (!(priv->power_mode & IPW_POWER_ENABLED)) {
7853                 wrqu->power.disabled = 1;
7854         } else {
7855                 wrqu->power.disabled = 0;
7856                 wrqu->power.flags = 0;
7857         }
7858
7859         IPW_DEBUG_WX("GET Power Management Mode -> %02X\n", priv->power_mode);
7860
7861         return 0;
7862 }
7863
7864
7865 /*
7866  *
7867  * IWPRIV handlers
7868  *
7869  */
7870 #ifdef CONFIG_IPW2100_MONITOR
7871 static int ipw2100_wx_set_promisc(struct net_device *dev,
7872                                   struct iw_request_info *info,
7873                                   union iwreq_data *wrqu, char *extra)
7874 {
7875         struct ipw2100_priv *priv = ieee80211_priv(dev);
7876         int *parms = (int *)extra;
7877         int enable = (parms[0] > 0);
7878         int err = 0;
7879
7880         down(&priv->action_sem);
7881         if (!(priv->status & STATUS_INITIALIZED)) {
7882                 err = -EIO;
7883                 goto done;
7884         }
7885
7886         if (enable) {
7887                 if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
7888                         err = ipw2100_set_channel(priv, parms[1], 0);
7889                         goto done;
7890                 }
7891                 priv->channel = parms[1];
7892                 err = ipw2100_switch_mode(priv, IW_MODE_MONITOR);
7893         } else {
7894                 if (priv->ieee->iw_mode == IW_MODE_MONITOR)
7895                         err = ipw2100_switch_mode(priv, priv->last_mode);
7896         }
7897  done:
7898         up(&priv->action_sem);
7899         return err;
7900 }
7901
7902 static int ipw2100_wx_reset(struct net_device *dev,
7903                             struct iw_request_info *info,
7904                             union iwreq_data *wrqu, char *extra)
7905 {
7906         struct ipw2100_priv *priv = ieee80211_priv(dev);
7907         if (priv->status & STATUS_INITIALIZED)
7908                 schedule_reset(priv);
7909         return 0;
7910 }
7911
7912 #endif
7913
7914 static int ipw2100_wx_set_powermode(struct net_device *dev,
7915                                     struct iw_request_info *info,
7916                                     union iwreq_data *wrqu, char *extra)
7917 {
7918         struct ipw2100_priv *priv = ieee80211_priv(dev);
7919         int err = 0, mode = *(int *)extra;
7920
7921         down(&priv->action_sem);
7922         if (!(priv->status & STATUS_INITIALIZED)) {
7923                 err = -EIO;
7924                 goto done;
7925         }
7926
7927         if ((mode < 1) || (mode > POWER_MODES))
7928                 mode = IPW_POWER_AUTO;
7929
7930         if (priv->power_mode != mode)
7931                 err = ipw2100_set_power_mode(priv, mode);
7932  done:
7933         up(&priv->action_sem);
7934         return err;
7935 }
7936
7937 #define MAX_POWER_STRING 80
7938 static int ipw2100_wx_get_powermode(struct net_device *dev,
7939                                     struct iw_request_info *info,
7940                                     union iwreq_data *wrqu, char *extra)
7941 {
7942         /*
7943          * This can be called at any time.  No action lock required
7944          */
7945
7946         struct ipw2100_priv *priv = ieee80211_priv(dev);
7947         int level = IPW_POWER_LEVEL(priv->power_mode);
7948         s32 timeout, period;
7949
7950         if (!(priv->power_mode & IPW_POWER_ENABLED)) {
7951                 snprintf(extra, MAX_POWER_STRING,
7952                          "Power save level: %d (Off)", level);
7953         } else {
7954                 switch (level) {
7955                 case IPW_POWER_MODE_CAM:
7956                         snprintf(extra, MAX_POWER_STRING,
7957                                  "Power save level: %d (None)", level);
7958                         break;
7959                 case IPW_POWER_AUTO:
7960                 snprintf(extra, MAX_POWER_STRING,
7961                          "Power save level: %d (Auto)", 0);
7962                         break;
7963                 default:
7964                         timeout = timeout_duration[level - 1] / 1000;
7965                         period = period_duration[level - 1] / 1000;
7966                         snprintf(extra, MAX_POWER_STRING,
7967                                  "Power save level: %d "
7968                                  "(Timeout %dms, Period %dms)",
7969                                  level, timeout, period);
7970                 }
7971         }
7972
7973         wrqu->data.length = strlen(extra) + 1;
7974
7975         return 0;
7976 }
7977
7978
7979 static int ipw2100_wx_set_preamble(struct net_device *dev,
7980                                    struct iw_request_info *info,
7981                                    union iwreq_data *wrqu, char *extra)
7982 {
7983         struct ipw2100_priv *priv = ieee80211_priv(dev);
7984         int err, mode = *(int *)extra;
7985
7986         down(&priv->action_sem);
7987         if (!(priv->status & STATUS_INITIALIZED)) {
7988                 err = -EIO;
7989                 goto done;
7990         }
7991
7992         if (mode == 1)
7993                 priv->config |= CFG_LONG_PREAMBLE;
7994         else if (mode == 0)
7995                 priv->config &= ~CFG_LONG_PREAMBLE;
7996         else {
7997                 err = -EINVAL;
7998                 goto done;
7999         }
8000
8001         err = ipw2100_system_config(priv, 0);
8002
8003 done:
8004         up(&priv->action_sem);
8005         return err;
8006 }
8007
8008 static int ipw2100_wx_get_preamble(struct net_device *dev,
8009                                     struct iw_request_info *info,
8010                                     union iwreq_data *wrqu, char *extra)
8011 {
8012         /*
8013          * This can be called at any time.  No action lock required
8014          */
8015
8016         struct ipw2100_priv *priv = ieee80211_priv(dev);
8017
8018         if (priv->config & CFG_LONG_PREAMBLE)
8019                 snprintf(wrqu->name, IFNAMSIZ, "long (1)");
8020         else
8021                 snprintf(wrqu->name, IFNAMSIZ, "auto (0)");
8022
8023         return 0;
8024 }
8025
8026 static iw_handler ipw2100_wx_handlers[] =
8027 {
8028         NULL,                     /* SIOCSIWCOMMIT */
8029         ipw2100_wx_get_name,      /* SIOCGIWNAME */
8030         NULL,                     /* SIOCSIWNWID */
8031         NULL,                     /* SIOCGIWNWID */
8032         ipw2100_wx_set_freq,      /* SIOCSIWFREQ */
8033         ipw2100_wx_get_freq,      /* SIOCGIWFREQ */
8034         ipw2100_wx_set_mode,      /* SIOCSIWMODE */
8035         ipw2100_wx_get_mode,      /* SIOCGIWMODE */
8036         NULL,                     /* SIOCSIWSENS */
8037         NULL,                     /* SIOCGIWSENS */
8038         NULL,                     /* SIOCSIWRANGE */
8039         ipw2100_wx_get_range,     /* SIOCGIWRANGE */
8040         NULL,                     /* SIOCSIWPRIV */
8041         NULL,                     /* SIOCGIWPRIV */
8042         NULL,                     /* SIOCSIWSTATS */
8043         NULL,                     /* SIOCGIWSTATS */
8044         NULL,                     /* SIOCSIWSPY */
8045         NULL,                     /* SIOCGIWSPY */
8046         NULL,                     /* SIOCGIWTHRSPY */
8047         NULL,                     /* SIOCWIWTHRSPY */
8048         ipw2100_wx_set_wap,       /* SIOCSIWAP */
8049         ipw2100_wx_get_wap,       /* SIOCGIWAP */
8050         NULL,                     /* -- hole -- */
8051         NULL,                     /* SIOCGIWAPLIST -- deprecated */
8052         ipw2100_wx_set_scan,      /* SIOCSIWSCAN */
8053         ipw2100_wx_get_scan,      /* SIOCGIWSCAN */
8054         ipw2100_wx_set_essid,     /* SIOCSIWESSID */
8055         ipw2100_wx_get_essid,     /* SIOCGIWESSID */
8056         ipw2100_wx_set_nick,      /* SIOCSIWNICKN */
8057         ipw2100_wx_get_nick,      /* SIOCGIWNICKN */
8058         NULL,                     /* -- hole -- */
8059         NULL,                     /* -- hole -- */
8060         ipw2100_wx_set_rate,      /* SIOCSIWRATE */
8061         ipw2100_wx_get_rate,      /* SIOCGIWRATE */
8062         ipw2100_wx_set_rts,       /* SIOCSIWRTS */
8063         ipw2100_wx_get_rts,       /* SIOCGIWRTS */
8064         ipw2100_wx_set_frag,      /* SIOCSIWFRAG */
8065         ipw2100_wx_get_frag,      /* SIOCGIWFRAG */
8066         ipw2100_wx_set_txpow,     /* SIOCSIWTXPOW */
8067         ipw2100_wx_get_txpow,     /* SIOCGIWTXPOW */
8068         ipw2100_wx_set_retry,     /* SIOCSIWRETRY */
8069         ipw2100_wx_get_retry,     /* SIOCGIWRETRY */
8070         ipw2100_wx_set_encode,    /* SIOCSIWENCODE */
8071         ipw2100_wx_get_encode,    /* SIOCGIWENCODE */
8072         ipw2100_wx_set_power,     /* SIOCSIWPOWER */
8073         ipw2100_wx_get_power,     /* SIOCGIWPOWER */
8074 };
8075
8076 #define IPW2100_PRIV_SET_MONITOR        SIOCIWFIRSTPRIV
8077 #define IPW2100_PRIV_RESET              SIOCIWFIRSTPRIV+1
8078 #define IPW2100_PRIV_SET_POWER          SIOCIWFIRSTPRIV+2
8079 #define IPW2100_PRIV_GET_POWER          SIOCIWFIRSTPRIV+3
8080 #define IPW2100_PRIV_SET_LONGPREAMBLE   SIOCIWFIRSTPRIV+4
8081 #define IPW2100_PRIV_GET_LONGPREAMBLE   SIOCIWFIRSTPRIV+5
8082
8083 static const struct iw_priv_args ipw2100_private_args[] = {
8084
8085 #ifdef CONFIG_IPW2100_MONITOR
8086         {
8087                 IPW2100_PRIV_SET_MONITOR,
8088                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "monitor"
8089         },
8090         {
8091                 IPW2100_PRIV_RESET,
8092                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 0, 0, "reset"
8093         },
8094 #endif /* CONFIG_IPW2100_MONITOR */
8095
8096         {
8097                 IPW2100_PRIV_SET_POWER,
8098                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "set_power"
8099         },
8100         {
8101                 IPW2100_PRIV_GET_POWER,
8102                 0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_POWER_STRING, "get_power"
8103         },
8104         {
8105                 IPW2100_PRIV_SET_LONGPREAMBLE,
8106                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "set_preamble"
8107         },
8108         {
8109                 IPW2100_PRIV_GET_LONGPREAMBLE,
8110                 0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | IFNAMSIZ, "get_preamble"
8111         },
8112 };
8113
8114 static iw_handler ipw2100_private_handler[] = {
8115 #ifdef CONFIG_IPW2100_MONITOR
8116         ipw2100_wx_set_promisc,
8117         ipw2100_wx_reset,
8118 #else /* CONFIG_IPW2100_MONITOR */
8119         NULL,
8120         NULL,
8121 #endif /* CONFIG_IPW2100_MONITOR */
8122         ipw2100_wx_set_powermode,
8123         ipw2100_wx_get_powermode,
8124         ipw2100_wx_set_preamble,
8125         ipw2100_wx_get_preamble,
8126 };
8127
8128 struct iw_handler_def ipw2100_wx_handler_def =
8129 {
8130         .standard = ipw2100_wx_handlers,
8131         .num_standard = sizeof(ipw2100_wx_handlers) / sizeof(iw_handler),
8132         .num_private = sizeof(ipw2100_private_handler) / sizeof(iw_handler),
8133         .num_private_args = sizeof(ipw2100_private_args) /
8134         sizeof(struct iw_priv_args),
8135         .private = (iw_handler *)ipw2100_private_handler,
8136         .private_args = (struct iw_priv_args *)ipw2100_private_args,
8137 };
8138
8139 /*
8140  * Get wireless statistics.
8141  * Called by /proc/net/wireless
8142  * Also called by SIOCGIWSTATS
8143  */
8144 struct iw_statistics *ipw2100_wx_wireless_stats(struct net_device * dev)
8145 {
8146         enum {
8147                 POOR = 30,
8148                 FAIR = 60,
8149                 GOOD = 80,
8150                 VERY_GOOD = 90,
8151                 EXCELLENT = 95,
8152                 PERFECT = 100
8153         };
8154         int rssi_qual;
8155         int tx_qual;
8156         int beacon_qual;
8157
8158         struct ipw2100_priv *priv = ieee80211_priv(dev);
8159         struct iw_statistics *wstats;
8160         u32 rssi, quality, tx_retries, missed_beacons, tx_failures;
8161         u32 ord_len = sizeof(u32);
8162
8163         if (!priv)
8164                 return (struct iw_statistics *) NULL;
8165
8166         wstats = &priv->wstats;
8167
8168         /* if hw is disabled, then ipw2100_get_ordinal() can't be called.
8169          * ipw2100_wx_wireless_stats seems to be called before fw is
8170          * initialized.  STATUS_ASSOCIATED will only be set if the hw is up
8171          * and associated; if not associcated, the values are all meaningless
8172          * anyway, so set them all to NULL and INVALID */
8173         if (!(priv->status & STATUS_ASSOCIATED)) {
8174                 wstats->miss.beacon = 0;
8175                 wstats->discard.retries = 0;
8176                 wstats->qual.qual = 0;
8177                 wstats->qual.level = 0;
8178                 wstats->qual.noise = 0;
8179                 wstats->qual.updated = 7;
8180                 wstats->qual.updated |= IW_QUAL_NOISE_INVALID |
8181                         IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID;
8182                 return wstats;
8183         }
8184
8185         if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_PERCENT_MISSED_BCNS,
8186                                 &missed_beacons, &ord_len))
8187                 goto fail_get_ordinal;
8188
8189         /* If we don't have a connection the quality and level is 0*/
8190         if (!(priv->status & STATUS_ASSOCIATED)) {
8191                 wstats->qual.qual = 0;
8192                 wstats->qual.level = 0;
8193         } else {
8194                 if (ipw2100_get_ordinal(priv, IPW_ORD_RSSI_AVG_CURR,
8195                                         &rssi, &ord_len))
8196                         goto fail_get_ordinal;
8197                 wstats->qual.level = rssi + IPW2100_RSSI_TO_DBM;
8198                 if (rssi < 10)
8199                         rssi_qual = rssi * POOR / 10;
8200                 else if (rssi < 15)
8201                         rssi_qual = (rssi - 10) * (FAIR - POOR) / 5 + POOR;
8202                 else if (rssi < 20)
8203                         rssi_qual = (rssi - 15) * (GOOD - FAIR) / 5 + FAIR;
8204                 else if (rssi < 30)
8205                         rssi_qual = (rssi - 20) * (VERY_GOOD - GOOD) /
8206                                 10 + GOOD;
8207                 else
8208                         rssi_qual = (rssi - 30) * (PERFECT - VERY_GOOD) /
8209                                 10 + VERY_GOOD;
8210
8211                 if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_PERCENT_RETRIES,
8212                                         &tx_retries, &ord_len))
8213                         goto fail_get_ordinal;
8214
8215                 if (tx_retries > 75)
8216                         tx_qual = (90 - tx_retries) * POOR / 15;
8217                 else if (tx_retries > 70)
8218                         tx_qual = (75 - tx_retries) * (FAIR - POOR) / 5 + POOR;
8219                 else if (tx_retries > 65)
8220                         tx_qual = (70 - tx_retries) * (GOOD - FAIR) / 5 + FAIR;
8221                 else if (tx_retries > 50)
8222                         tx_qual = (65 - tx_retries) * (VERY_GOOD - GOOD) /
8223                                 15 + GOOD;
8224                 else
8225                         tx_qual = (50 - tx_retries) *
8226                                 (PERFECT - VERY_GOOD) / 50 + VERY_GOOD;
8227
8228                 if (missed_beacons > 50)
8229                         beacon_qual = (60 - missed_beacons) * POOR / 10;
8230                 else if (missed_beacons > 40)
8231                         beacon_qual = (50 - missed_beacons) * (FAIR - POOR) /
8232                                 10 + POOR;
8233                 else if (missed_beacons > 32)
8234                         beacon_qual = (40 - missed_beacons) * (GOOD - FAIR) /
8235                                 18 + FAIR;
8236                 else if (missed_beacons > 20)
8237                         beacon_qual = (32 - missed_beacons) *
8238                                 (VERY_GOOD - GOOD) / 20 + GOOD;
8239                 else
8240                         beacon_qual = (20 - missed_beacons) *
8241                                 (PERFECT - VERY_GOOD) / 20 + VERY_GOOD;
8242
8243                 quality = min(beacon_qual, min(tx_qual, rssi_qual));
8244
8245 #ifdef CONFIG_IPW_DEBUG
8246                 if (beacon_qual == quality)
8247                         IPW_DEBUG_WX("Quality clamped by Missed Beacons\n");
8248                 else if (tx_qual == quality)
8249                         IPW_DEBUG_WX("Quality clamped by Tx Retries\n");
8250                 else if (quality != 100)
8251                         IPW_DEBUG_WX("Quality clamped by Signal Strength\n");
8252                 else
8253                         IPW_DEBUG_WX("Quality not clamped.\n");
8254 #endif
8255
8256                 wstats->qual.qual = quality;
8257                 wstats->qual.level = rssi + IPW2100_RSSI_TO_DBM;
8258         }
8259
8260         wstats->qual.noise = 0;
8261         wstats->qual.updated = 7;
8262         wstats->qual.updated |= IW_QUAL_NOISE_INVALID;
8263
8264         /* FIXME: this is percent and not a # */
8265         wstats->miss.beacon = missed_beacons;
8266
8267         if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_TX_FAILURES,
8268                                 &tx_failures, &ord_len))
8269                 goto fail_get_ordinal;
8270         wstats->discard.retries = tx_failures;
8271
8272         return wstats;
8273
8274  fail_get_ordinal:
8275         IPW_DEBUG_WX("failed querying ordinals.\n");
8276
8277         return (struct iw_statistics *) NULL;
8278 }
8279
8280 void ipw2100_wx_event_work(struct ipw2100_priv *priv)
8281 {
8282         union iwreq_data wrqu;
8283         int len = ETH_ALEN;
8284
8285         if (priv->status & STATUS_STOPPING)
8286                 return;
8287
8288         down(&priv->action_sem);
8289
8290         IPW_DEBUG_WX("enter\n");
8291
8292         up(&priv->action_sem);
8293
8294         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
8295
8296         /* Fetch BSSID from the hardware */
8297         if (!(priv->status & (STATUS_ASSOCIATING | STATUS_ASSOCIATED)) ||
8298             priv->status & STATUS_RF_KILL_MASK ||
8299             ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_AP_BSSID,
8300                                 &priv->bssid,  &len)) {
8301                 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
8302         } else {
8303                 /* We now have the BSSID, so can finish setting to the full
8304                  * associated state */
8305                 memcpy(wrqu.ap_addr.sa_data, priv->bssid, ETH_ALEN);
8306                 memcpy(&priv->ieee->bssid, priv->bssid, ETH_ALEN);
8307                 priv->status &= ~STATUS_ASSOCIATING;
8308                 priv->status |= STATUS_ASSOCIATED;
8309                 netif_carrier_on(priv->net_dev);
8310                 if (netif_queue_stopped(priv->net_dev)) {
8311                         IPW_DEBUG_INFO("Waking net queue.\n");
8312                         netif_wake_queue(priv->net_dev);
8313                 } else {
8314                         IPW_DEBUG_INFO("Starting net queue.\n");
8315                         netif_start_queue(priv->net_dev);
8316                 }
8317         }
8318
8319         if (!(priv->status & STATUS_ASSOCIATED)) {
8320                 IPW_DEBUG_WX("Configuring ESSID\n");
8321                 down(&priv->action_sem);
8322                 /* This is a disassociation event, so kick the firmware to
8323                  * look for another AP */
8324                 if (priv->config & CFG_STATIC_ESSID)
8325                         ipw2100_set_essid(priv, priv->essid, priv->essid_len, 0);
8326                 else
8327                         ipw2100_set_essid(priv, NULL, 0, 0);
8328                 up(&priv->action_sem);
8329         }
8330
8331         wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
8332 }
8333
8334 #define IPW2100_FW_MAJOR_VERSION 1
8335 #define IPW2100_FW_MINOR_VERSION 3
8336
8337 #define IPW2100_FW_MINOR(x) ((x & 0xff) >> 8)
8338 #define IPW2100_FW_MAJOR(x) (x & 0xff)
8339
8340 #define IPW2100_FW_VERSION ((IPW2100_FW_MINOR_VERSION << 8) | \
8341                              IPW2100_FW_MAJOR_VERSION)
8342
8343 #define IPW2100_FW_PREFIX "ipw2100-" __stringify(IPW2100_FW_MAJOR_VERSION) \
8344 "." __stringify(IPW2100_FW_MINOR_VERSION)
8345
8346 #define IPW2100_FW_NAME(x) IPW2100_FW_PREFIX "" x ".fw"
8347
8348
8349 /*
8350
8351 BINARY FIRMWARE HEADER FORMAT
8352
8353 offset      length   desc
8354 0           2        version
8355 2           2        mode == 0:BSS,1:IBSS,2:MONITOR
8356 4           4        fw_len
8357 8           4        uc_len
8358 C           fw_len   firmware data
8359 12 + fw_len uc_len   microcode data
8360
8361 */
8362
8363 struct ipw2100_fw_header {
8364         short version;
8365         short mode;
8366         unsigned int fw_size;
8367         unsigned int uc_size;
8368 } __attribute__ ((packed));
8369
8370
8371
8372 static int ipw2100_mod_firmware_load(struct ipw2100_fw *fw)
8373 {
8374         struct ipw2100_fw_header *h =
8375                 (struct ipw2100_fw_header *)fw->fw_entry->data;
8376
8377         if (IPW2100_FW_MAJOR(h->version) != IPW2100_FW_MAJOR_VERSION) {
8378                 IPW_DEBUG_WARNING("Firmware image not compatible "
8379                        "(detected version id of %u). "
8380                        "See Documentation/networking/README.ipw2100\n",
8381                        h->version);
8382                 return 1;
8383         }
8384
8385         fw->version = h->version;
8386         fw->fw.data = fw->fw_entry->data + sizeof(struct ipw2100_fw_header);
8387         fw->fw.size = h->fw_size;
8388         fw->uc.data = fw->fw.data + h->fw_size;
8389         fw->uc.size = h->uc_size;
8390
8391         return 0;
8392 }
8393
8394
8395 int ipw2100_get_firmware(struct ipw2100_priv *priv, struct ipw2100_fw *fw)
8396 {
8397         char *fw_name;
8398         int rc;
8399
8400         IPW_DEBUG_INFO("%s: Using hotplug firmware load.\n",
8401                priv->net_dev->name);
8402
8403         switch (priv->ieee->iw_mode) {
8404         case IW_MODE_ADHOC:
8405                 fw_name = IPW2100_FW_NAME("-i");
8406                 break;
8407 #ifdef CONFIG_IPW2100_MONITOR
8408         case IW_MODE_MONITOR:
8409                 fw_name = IPW2100_FW_NAME("-p");
8410                 break;
8411 #endif
8412         case IW_MODE_INFRA:
8413         default:
8414                 fw_name = IPW2100_FW_NAME("");
8415                 break;
8416         }
8417
8418         rc = request_firmware(&fw->fw_entry, fw_name, &priv->pci_dev->dev);
8419
8420         if (rc < 0) {
8421                 IPW_DEBUG_ERROR(
8422                        "%s: Firmware '%s' not available or load failed.\n",
8423                        priv->net_dev->name, fw_name);
8424                 return rc;
8425         }
8426         IPW_DEBUG_INFO("firmware data %p size %zd\n", fw->fw_entry->data,
8427                            fw->fw_entry->size);
8428
8429         ipw2100_mod_firmware_load(fw);
8430
8431         return 0;
8432 }
8433
8434 void ipw2100_release_firmware(struct ipw2100_priv *priv,
8435                               struct ipw2100_fw *fw)
8436 {
8437         fw->version = 0;
8438         if (fw->fw_entry)
8439                 release_firmware(fw->fw_entry);
8440         fw->fw_entry = NULL;
8441 }
8442
8443
8444 int ipw2100_get_fwversion(struct ipw2100_priv *priv, char *buf, size_t max)
8445 {
8446         char ver[MAX_FW_VERSION_LEN];
8447         u32 len = MAX_FW_VERSION_LEN;
8448         u32 tmp;
8449         int i;
8450         /* firmware version is an ascii string (max len of 14) */
8451         if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_FW_VER_NUM,
8452                                 ver, &len))
8453                 return -EIO;
8454         tmp = max;
8455         if (len >= max)
8456                 len = max - 1;
8457         for (i = 0; i < len; i++)
8458                 buf[i] = ver[i];
8459         buf[i] = '\0';
8460         return tmp;
8461 }
8462
8463 int ipw2100_get_ucodeversion(struct ipw2100_priv *priv, char *buf, size_t max)
8464 {
8465         u32 ver;
8466         u32 len = sizeof(ver);
8467         /* microcode version is a 32 bit integer */
8468         if (ipw2100_get_ordinal(priv, IPW_ORD_UCODE_VERSION,
8469                                 &ver, &len))
8470                 return -EIO;
8471         return snprintf(buf, max, "%08X", ver);
8472 }
8473
8474 /*
8475  * On exit, the firmware will have been freed from the fw list
8476  */
8477 int ipw2100_fw_download(struct ipw2100_priv *priv, struct ipw2100_fw *fw)
8478 {
8479         /* firmware is constructed of N contiguous entries, each entry is
8480          * structured as:
8481          *
8482          * offset    sie         desc
8483          * 0         4           address to write to
8484          * 4         2           length of data run
8485          * 6         length      data
8486          */
8487         unsigned int addr;
8488         unsigned short len;
8489
8490         const unsigned char *firmware_data = fw->fw.data;
8491         unsigned int firmware_data_left = fw->fw.size;
8492
8493         while (firmware_data_left > 0) {
8494                 addr = *(u32 *)(firmware_data);
8495                 firmware_data      += 4;
8496                 firmware_data_left -= 4;
8497
8498                 len = *(u16 *)(firmware_data);
8499                 firmware_data      += 2;
8500                 firmware_data_left -= 2;
8501
8502                 if (len > 32) {
8503                         IPW_DEBUG_ERROR(
8504                                "Invalid firmware run-length of %d bytes\n",
8505                                len);
8506                         return -EINVAL;
8507                 }
8508
8509                 write_nic_memory(priv->net_dev, addr, len, firmware_data);
8510                 firmware_data      += len;
8511                 firmware_data_left -= len;
8512         }
8513
8514         return 0;
8515 }
8516
8517 struct symbol_alive_response {
8518         u8 cmd_id;
8519         u8 seq_num;
8520         u8 ucode_rev;
8521         u8 eeprom_valid;
8522         u16 valid_flags;
8523         u8 IEEE_addr[6];
8524         u16 flags;
8525         u16 pcb_rev;
8526         u16 clock_settle_time;  // 1us LSB
8527         u16 powerup_settle_time;        // 1us LSB
8528         u16 hop_settle_time;    // 1us LSB
8529         u8 date[3];             // month, day, year
8530         u8 time[2];             // hours, minutes
8531         u8 ucode_valid;
8532 };
8533
8534 int ipw2100_ucode_download(struct ipw2100_priv *priv, struct ipw2100_fw *fw)
8535 {
8536         struct net_device *dev = priv->net_dev;
8537         const unsigned char *microcode_data = fw->uc.data;
8538         unsigned int microcode_data_left = fw->uc.size;
8539
8540         struct symbol_alive_response response;
8541         int i, j;
8542         u8 data;
8543
8544         /* Symbol control */
8545         write_nic_word(dev, IPW2100_CONTROL_REG, 0x703);
8546         readl((void *)(dev->base_addr));
8547         write_nic_word(dev, IPW2100_CONTROL_REG, 0x707);
8548         readl((void *)(dev->base_addr));
8549
8550         /* HW config */
8551         write_nic_byte(dev, 0x210014, 0x72);    /* fifo width =16 */
8552         readl((void *)(dev->base_addr));
8553         write_nic_byte(dev, 0x210014, 0x72);    /* fifo width =16 */
8554         readl((void *)(dev->base_addr));
8555
8556         /* EN_CS_ACCESS bit to reset control store pointer */
8557         write_nic_byte(dev, 0x210000, 0x40);
8558         readl((void *)(dev->base_addr));
8559         write_nic_byte(dev, 0x210000, 0x0);
8560         readl((void *)(dev->base_addr));
8561         write_nic_byte(dev, 0x210000, 0x40);
8562         readl((void *)(dev->base_addr));
8563
8564         /* copy microcode from buffer into Symbol */
8565
8566         while (microcode_data_left > 0) {
8567                 write_nic_byte(dev, 0x210010, *microcode_data++);
8568                 write_nic_byte(dev, 0x210010, *microcode_data++);
8569                 microcode_data_left -= 2;
8570         }
8571
8572         /* EN_CS_ACCESS bit to reset the control store pointer */
8573         write_nic_byte(dev, 0x210000, 0x0);
8574         readl((void *)(dev->base_addr));
8575
8576         /* Enable System (Reg 0)
8577          * first enable causes garbage in RX FIFO */
8578         write_nic_byte(dev, 0x210000, 0x0);
8579         readl((void *)(dev->base_addr));
8580         write_nic_byte(dev, 0x210000, 0x80);
8581         readl((void *)(dev->base_addr));
8582
8583         /* Reset External Baseband Reg */
8584         write_nic_word(dev, IPW2100_CONTROL_REG, 0x703);
8585         readl((void *)(dev->base_addr));
8586         write_nic_word(dev, IPW2100_CONTROL_REG, 0x707);
8587         readl((void *)(dev->base_addr));
8588
8589         /* HW Config (Reg 5) */
8590         write_nic_byte(dev, 0x210014, 0x72);    // fifo width =16
8591         readl((void *)(dev->base_addr));
8592         write_nic_byte(dev, 0x210014, 0x72);    // fifo width =16
8593         readl((void *)(dev->base_addr));
8594
8595         /* Enable System (Reg 0)
8596          * second enable should be OK */
8597         write_nic_byte(dev, 0x210000, 0x00);    // clear enable system
8598         readl((void *)(dev->base_addr));
8599         write_nic_byte(dev, 0x210000, 0x80);    // set enable system
8600
8601         /* check Symbol is enabled - upped this from 5 as it wasn't always
8602          * catching the update */
8603         for (i = 0; i < 10; i++) {
8604                 udelay(10);
8605
8606                 /* check Dino is enabled bit */
8607                 read_nic_byte(dev, 0x210000, &data);
8608                 if (data & 0x1)
8609                         break;
8610         }
8611
8612         if (i == 10) {
8613                 IPW_DEBUG_ERROR("%s: Error initializing Symbol\n",
8614                        dev->name);
8615                 return -EIO;
8616         }
8617
8618         /* Get Symbol alive response */
8619         for (i = 0; i < 30; i++) {
8620                 /* Read alive response structure */
8621                 for (j = 0;
8622                      j < (sizeof(struct symbol_alive_response) >> 1);
8623                      j++)
8624                         read_nic_word(dev, 0x210004,
8625                                       ((u16 *)&response) + j);
8626
8627                 if ((response.cmd_id == 1) &&
8628                     (response.ucode_valid == 0x1))
8629                         break;
8630                 udelay(10);
8631         }
8632
8633         if (i == 30) {
8634                 IPW_DEBUG_ERROR("%s: No response from Symbol - hw not alive\n",
8635                        dev->name);
8636                 printk_buf(IPW_DL_ERROR, (u8*)&response, sizeof(response));
8637                 return -EIO;
8638         }
8639
8640         return 0;
8641 }