]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/wireless/ipw2100.c
[PATCH] ipw2100: small cleanups
[linux-2.6-omap-h63xx.git] / 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, char *buf)
3384 {
3385         struct pci_dev *pci_dev = container_of(d, struct pci_dev, dev);
3386         char *out = buf;
3387         int i, j;
3388         u32 val;
3389
3390         for (i = 0; i < 16; i++) {
3391                 out += sprintf(out, "[%08X] ", i * 16);
3392                 for (j = 0; j < 16; j += 4) {
3393                         pci_read_config_dword(pci_dev, i * 16 + j, &val);
3394                         out += sprintf(out, "%08X ", val);
3395                 }
3396                 out += sprintf(out, "\n");
3397         }
3398
3399         return out - buf;
3400 }
3401 static DEVICE_ATTR(pci, S_IRUGO, show_pci, NULL);
3402
3403 static ssize_t show_cfg(struct device *d, char *buf)
3404 {
3405         struct ipw2100_priv *p = (struct ipw2100_priv *)d->driver_data;
3406         return sprintf(buf, "0x%08x\n", (int)p->config);
3407 }
3408 static DEVICE_ATTR(cfg, S_IRUGO, show_cfg, NULL);
3409
3410 static ssize_t show_status(struct device *d, char *buf)
3411 {
3412         struct ipw2100_priv *p = (struct ipw2100_priv *)d->driver_data;
3413         return sprintf(buf, "0x%08x\n", (int)p->status);
3414 }
3415 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3416
3417 static ssize_t show_capability(struct device *d, char *buf)
3418 {
3419         struct ipw2100_priv *p = (struct ipw2100_priv *)d->driver_data;
3420         return sprintf(buf, "0x%08x\n", (int)p->capability);
3421 }
3422 static DEVICE_ATTR(capability, S_IRUGO, show_capability, NULL);
3423
3424
3425 #define IPW2100_REG(x) { IPW_ ##x, #x }
3426 const struct {
3427         u32 addr;
3428         const char *name;
3429 } hw_data[] = {
3430         IPW2100_REG(REG_GP_CNTRL),
3431         IPW2100_REG(REG_GPIO),
3432         IPW2100_REG(REG_INTA),
3433         IPW2100_REG(REG_INTA_MASK),
3434         IPW2100_REG(REG_RESET_REG),
3435 };
3436 #define IPW2100_NIC(x, s) { x, #x, s }
3437 const struct {
3438         u32 addr;
3439         const char *name;
3440         size_t size;
3441 } nic_data[] = {
3442         IPW2100_NIC(IPW2100_CONTROL_REG, 2),
3443         IPW2100_NIC(0x210014, 1),
3444         IPW2100_NIC(0x210000, 1),
3445 };
3446 #define IPW2100_ORD(x, d) { IPW_ORD_ ##x, #x, d }
3447 const struct {
3448         u8 index;
3449         const char *name;
3450         const char *desc;
3451 } ord_data[] = {
3452         IPW2100_ORD(STAT_TX_HOST_REQUESTS, "requested Host Tx's (MSDU)"),
3453         IPW2100_ORD(STAT_TX_HOST_COMPLETE, "successful Host Tx's (MSDU)"),
3454         IPW2100_ORD(STAT_TX_DIR_DATA,      "successful Directed Tx's (MSDU)"),
3455         IPW2100_ORD(STAT_TX_DIR_DATA1,     "successful Directed Tx's (MSDU) @ 1MB"),
3456         IPW2100_ORD(STAT_TX_DIR_DATA2,     "successful Directed Tx's (MSDU) @ 2MB"),
3457         IPW2100_ORD(STAT_TX_DIR_DATA5_5,   "successful Directed Tx's (MSDU) @ 5_5MB"),
3458         IPW2100_ORD(STAT_TX_DIR_DATA11,    "successful Directed Tx's (MSDU) @ 11MB"),
3459         IPW2100_ORD(STAT_TX_NODIR_DATA1,   "successful Non_Directed Tx's (MSDU) @ 1MB"),
3460         IPW2100_ORD(STAT_TX_NODIR_DATA2,   "successful Non_Directed Tx's (MSDU) @ 2MB"),
3461         IPW2100_ORD(STAT_TX_NODIR_DATA5_5, "successful Non_Directed Tx's (MSDU) @ 5.5MB"),
3462         IPW2100_ORD(STAT_TX_NODIR_DATA11,  "successful Non_Directed Tx's (MSDU) @ 11MB"),
3463         IPW2100_ORD(STAT_NULL_DATA,        "successful NULL data Tx's"),
3464         IPW2100_ORD(STAT_TX_RTS,           "successful Tx RTS"),
3465         IPW2100_ORD(STAT_TX_CTS,           "successful Tx CTS"),
3466         IPW2100_ORD(STAT_TX_ACK,           "successful Tx ACK"),
3467         IPW2100_ORD(STAT_TX_ASSN,          "successful Association Tx's"),
3468         IPW2100_ORD(STAT_TX_ASSN_RESP,     "successful Association response Tx's"),
3469         IPW2100_ORD(STAT_TX_REASSN,        "successful Reassociation Tx's"),
3470         IPW2100_ORD(STAT_TX_REASSN_RESP,   "successful Reassociation response Tx's"),
3471         IPW2100_ORD(STAT_TX_PROBE,         "probes successfully transmitted"),
3472         IPW2100_ORD(STAT_TX_PROBE_RESP,    "probe responses successfully transmitted"),
3473         IPW2100_ORD(STAT_TX_BEACON,        "tx beacon"),
3474         IPW2100_ORD(STAT_TX_ATIM,          "Tx ATIM"),
3475         IPW2100_ORD(STAT_TX_DISASSN,       "successful Disassociation TX"),
3476         IPW2100_ORD(STAT_TX_AUTH,          "successful Authentication Tx"),
3477         IPW2100_ORD(STAT_TX_DEAUTH,        "successful Deauthentication TX"),
3478         IPW2100_ORD(STAT_TX_TOTAL_BYTES,   "Total successful Tx data bytes"),
3479         IPW2100_ORD(STAT_TX_RETRIES,       "Tx retries"),
3480         IPW2100_ORD(STAT_TX_RETRY1,        "Tx retries at 1MBPS"),
3481         IPW2100_ORD(STAT_TX_RETRY2,        "Tx retries at 2MBPS"),
3482         IPW2100_ORD(STAT_TX_RETRY5_5,      "Tx retries at 5.5MBPS"),
3483         IPW2100_ORD(STAT_TX_RETRY11,       "Tx retries at 11MBPS"),
3484         IPW2100_ORD(STAT_TX_FAILURES,      "Tx Failures"),
3485         IPW2100_ORD(STAT_TX_MAX_TRIES_IN_HOP,"times max tries in a hop failed"),
3486         IPW2100_ORD(STAT_TX_DISASSN_FAIL,       "times disassociation failed"),
3487         IPW2100_ORD(STAT_TX_ERR_CTS,         "missed/bad CTS frames"),
3488         IPW2100_ORD(STAT_TX_ERR_ACK,    "tx err due to acks"),
3489         IPW2100_ORD(STAT_RX_HOST,       "packets passed to host"),
3490         IPW2100_ORD(STAT_RX_DIR_DATA,   "directed packets"),
3491         IPW2100_ORD(STAT_RX_DIR_DATA1,  "directed packets at 1MB"),
3492         IPW2100_ORD(STAT_RX_DIR_DATA2,  "directed packets at 2MB"),
3493         IPW2100_ORD(STAT_RX_DIR_DATA5_5,        "directed packets at 5.5MB"),
3494         IPW2100_ORD(STAT_RX_DIR_DATA11, "directed packets at 11MB"),
3495         IPW2100_ORD(STAT_RX_NODIR_DATA,"nondirected packets"),
3496         IPW2100_ORD(STAT_RX_NODIR_DATA1,        "nondirected packets at 1MB"),
3497         IPW2100_ORD(STAT_RX_NODIR_DATA2,        "nondirected packets at 2MB"),
3498         IPW2100_ORD(STAT_RX_NODIR_DATA5_5,      "nondirected packets at 5.5MB"),
3499         IPW2100_ORD(STAT_RX_NODIR_DATA11,       "nondirected packets at 11MB"),
3500         IPW2100_ORD(STAT_RX_NULL_DATA,  "null data rx's"),
3501         IPW2100_ORD(STAT_RX_RTS,        "Rx RTS"),
3502         IPW2100_ORD(STAT_RX_CTS,        "Rx CTS"),
3503         IPW2100_ORD(STAT_RX_ACK,        "Rx ACK"),
3504         IPW2100_ORD(STAT_RX_CFEND,      "Rx CF End"),
3505         IPW2100_ORD(STAT_RX_CFEND_ACK,  "Rx CF End + CF Ack"),
3506         IPW2100_ORD(STAT_RX_ASSN,       "Association Rx's"),
3507         IPW2100_ORD(STAT_RX_ASSN_RESP,  "Association response Rx's"),
3508         IPW2100_ORD(STAT_RX_REASSN,     "Reassociation Rx's"),
3509         IPW2100_ORD(STAT_RX_REASSN_RESP,        "Reassociation response Rx's"),
3510         IPW2100_ORD(STAT_RX_PROBE,      "probe Rx's"),
3511         IPW2100_ORD(STAT_RX_PROBE_RESP, "probe response Rx's"),
3512         IPW2100_ORD(STAT_RX_BEACON,     "Rx beacon"),
3513         IPW2100_ORD(STAT_RX_ATIM,       "Rx ATIM"),
3514         IPW2100_ORD(STAT_RX_DISASSN,    "disassociation Rx"),
3515         IPW2100_ORD(STAT_RX_AUTH,       "authentication Rx"),
3516         IPW2100_ORD(STAT_RX_DEAUTH,     "deauthentication Rx"),
3517         IPW2100_ORD(STAT_RX_TOTAL_BYTES,"Total rx data bytes received"),
3518         IPW2100_ORD(STAT_RX_ERR_CRC,     "packets with Rx CRC error"),
3519         IPW2100_ORD(STAT_RX_ERR_CRC1,    "Rx CRC errors at 1MB"),
3520         IPW2100_ORD(STAT_RX_ERR_CRC2,    "Rx CRC errors at 2MB"),
3521         IPW2100_ORD(STAT_RX_ERR_CRC5_5,  "Rx CRC errors at 5.5MB"),
3522         IPW2100_ORD(STAT_RX_ERR_CRC11,   "Rx CRC errors at 11MB"),
3523         IPW2100_ORD(STAT_RX_DUPLICATE1, "duplicate rx packets at 1MB"),
3524         IPW2100_ORD(STAT_RX_DUPLICATE2,  "duplicate rx packets at 2MB"),
3525         IPW2100_ORD(STAT_RX_DUPLICATE5_5,        "duplicate rx packets at 5.5MB"),
3526         IPW2100_ORD(STAT_RX_DUPLICATE11,         "duplicate rx packets at 11MB"),
3527         IPW2100_ORD(STAT_RX_DUPLICATE, "duplicate rx packets"),
3528         IPW2100_ORD(PERS_DB_LOCK,       "locking fw permanent  db"),
3529         IPW2100_ORD(PERS_DB_SIZE,       "size of fw permanent  db"),
3530         IPW2100_ORD(PERS_DB_ADDR,       "address of fw permanent  db"),
3531         IPW2100_ORD(STAT_RX_INVALID_PROTOCOL,   "rx frames with invalid protocol"),
3532         IPW2100_ORD(SYS_BOOT_TIME,      "Boot time"),
3533         IPW2100_ORD(STAT_RX_NO_BUFFER,  "rx frames rejected due to no buffer"),
3534         IPW2100_ORD(STAT_RX_MISSING_FRAG,       "rx frames dropped due to missing fragment"),
3535         IPW2100_ORD(STAT_RX_ORPHAN_FRAG,        "rx frames dropped due to non-sequential fragment"),
3536         IPW2100_ORD(STAT_RX_ORPHAN_FRAME,       "rx frames dropped due to unmatched 1st frame"),
3537         IPW2100_ORD(STAT_RX_FRAG_AGEOUT,        "rx frames dropped due to uncompleted frame"),
3538         IPW2100_ORD(STAT_RX_ICV_ERRORS, "ICV errors during decryption"),
3539         IPW2100_ORD(STAT_PSP_SUSPENSION,"times adapter suspended"),
3540         IPW2100_ORD(STAT_PSP_BCN_TIMEOUT,       "beacon timeout"),
3541         IPW2100_ORD(STAT_PSP_POLL_TIMEOUT,      "poll response timeouts"),
3542         IPW2100_ORD(STAT_PSP_NONDIR_TIMEOUT, "timeouts waiting for last {broad,multi}cast pkt"),
3543         IPW2100_ORD(STAT_PSP_RX_DTIMS,  "PSP DTIMs received"),
3544         IPW2100_ORD(STAT_PSP_RX_TIMS,   "PSP TIMs received"),
3545         IPW2100_ORD(STAT_PSP_STATION_ID,"PSP Station ID"),
3546         IPW2100_ORD(LAST_ASSN_TIME,     "RTC time of last association"),
3547         IPW2100_ORD(STAT_PERCENT_MISSED_BCNS,"current calculation of % missed beacons"),
3548         IPW2100_ORD(STAT_PERCENT_RETRIES,"current calculation of % missed tx retries"),
3549         IPW2100_ORD(ASSOCIATED_AP_PTR,  "0 if not associated, else pointer to AP table entry"),
3550         IPW2100_ORD(AVAILABLE_AP_CNT,   "AP's decsribed in the AP table"),
3551         IPW2100_ORD(AP_LIST_PTR,        "Ptr to list of available APs"),
3552         IPW2100_ORD(STAT_AP_ASSNS,      "associations"),
3553         IPW2100_ORD(STAT_ASSN_FAIL,     "association failures"),
3554         IPW2100_ORD(STAT_ASSN_RESP_FAIL,"failures due to response fail"),
3555         IPW2100_ORD(STAT_FULL_SCANS,    "full scans"),
3556         IPW2100_ORD(CARD_DISABLED,      "Card Disabled"),
3557         IPW2100_ORD(STAT_ROAM_INHIBIT,  "times roaming was inhibited due to activity"),
3558         IPW2100_ORD(RSSI_AT_ASSN,       "RSSI of associated AP at time of association"),
3559         IPW2100_ORD(STAT_ASSN_CAUSE1,   "reassociation: no probe response or TX on hop"),
3560         IPW2100_ORD(STAT_ASSN_CAUSE2,   "reassociation: poor tx/rx quality"),
3561         IPW2100_ORD(STAT_ASSN_CAUSE3,   "reassociation: tx/rx quality (excessive AP load"),
3562         IPW2100_ORD(STAT_ASSN_CAUSE4,   "reassociation: AP RSSI level"),
3563         IPW2100_ORD(STAT_ASSN_CAUSE5,   "reassociations due to load leveling"),
3564         IPW2100_ORD(STAT_AUTH_FAIL,     "times authentication failed"),
3565         IPW2100_ORD(STAT_AUTH_RESP_FAIL,"times authentication response failed"),
3566         IPW2100_ORD(STATION_TABLE_CNT,  "entries in association table"),
3567         IPW2100_ORD(RSSI_AVG_CURR,      "Current avg RSSI"),
3568         IPW2100_ORD(POWER_MGMT_MODE,    "Power mode - 0=CAM, 1=PSP"),
3569         IPW2100_ORD(COUNTRY_CODE,       "IEEE country code as recv'd from beacon"),
3570         IPW2100_ORD(COUNTRY_CHANNELS,   "channels suported by country"),
3571         IPW2100_ORD(RESET_CNT,  "adapter resets (warm)"),
3572         IPW2100_ORD(BEACON_INTERVAL,    "Beacon interval"),
3573         IPW2100_ORD(ANTENNA_DIVERSITY,  "TRUE if antenna diversity is disabled"),
3574         IPW2100_ORD(DTIM_PERIOD,        "beacon intervals between DTIMs"),
3575         IPW2100_ORD(OUR_FREQ,   "current radio freq lower digits - channel ID"),
3576         IPW2100_ORD(RTC_TIME,   "current RTC time"),
3577         IPW2100_ORD(PORT_TYPE,  "operating mode"),
3578         IPW2100_ORD(CURRENT_TX_RATE,    "current tx rate"),
3579         IPW2100_ORD(SUPPORTED_RATES,    "supported tx rates"),
3580         IPW2100_ORD(ATIM_WINDOW,        "current ATIM Window"),
3581         IPW2100_ORD(BASIC_RATES,        "basic tx rates"),
3582         IPW2100_ORD(NIC_HIGHEST_RATE,   "NIC highest tx rate"),
3583         IPW2100_ORD(AP_HIGHEST_RATE,    "AP highest tx rate"),
3584         IPW2100_ORD(CAPABILITIES,       "Management frame capability field"),
3585         IPW2100_ORD(AUTH_TYPE,  "Type of authentication"),
3586         IPW2100_ORD(RADIO_TYPE, "Adapter card platform type"),
3587         IPW2100_ORD(RTS_THRESHOLD,      "Min packet length for RTS handshaking"),
3588         IPW2100_ORD(INT_MODE,   "International mode"),
3589         IPW2100_ORD(FRAGMENTATION_THRESHOLD,    "protocol frag threshold"),
3590         IPW2100_ORD(EEPROM_SRAM_DB_BLOCK_START_ADDRESS, "EEPROM offset in SRAM"),
3591         IPW2100_ORD(EEPROM_SRAM_DB_BLOCK_SIZE,  "EEPROM size in SRAM"),
3592         IPW2100_ORD(EEPROM_SKU_CAPABILITY,      "EEPROM SKU Capability"),
3593         IPW2100_ORD(EEPROM_IBSS_11B_CHANNELS,   "EEPROM IBSS 11b channel set"),
3594         IPW2100_ORD(MAC_VERSION,        "MAC Version"),
3595         IPW2100_ORD(MAC_REVISION,       "MAC Revision"),
3596         IPW2100_ORD(RADIO_VERSION,      "Radio Version"),
3597         IPW2100_ORD(NIC_MANF_DATE_TIME, "MANF Date/Time STAMP"),
3598         IPW2100_ORD(UCODE_VERSION,      "Ucode Version"),
3599 };
3600
3601
3602 static ssize_t show_registers(struct device *d, char *buf)
3603 {
3604         int i;
3605         struct ipw2100_priv *priv = dev_get_drvdata(d);
3606         struct net_device *dev = priv->net_dev;
3607         char * out = buf;
3608         u32 val = 0;
3609
3610         out += sprintf(out, "%30s [Address ] : Hex\n", "Register");
3611
3612         for (i = 0; i < (sizeof(hw_data) / sizeof(*hw_data)); i++) {
3613                 read_register(dev, hw_data[i].addr, &val);
3614                 out += sprintf(out, "%30s [%08X] : %08X\n",
3615                                hw_data[i].name, hw_data[i].addr, val);
3616         }
3617
3618         return out - buf;
3619 }
3620 static DEVICE_ATTR(registers, S_IRUGO, show_registers, NULL);
3621
3622
3623 static ssize_t show_hardware(struct device *d, char *buf)
3624 {
3625         struct ipw2100_priv *priv = dev_get_drvdata(d);
3626         struct net_device *dev = priv->net_dev;
3627         char * out = buf;
3628         int i;
3629
3630         out += sprintf(out, "%30s [Address ] : Hex\n", "NIC entry");
3631
3632         for (i = 0; i < (sizeof(nic_data) / sizeof(*nic_data)); i++) {
3633                 u8 tmp8;
3634                 u16 tmp16;
3635                 u32 tmp32;
3636
3637                 switch (nic_data[i].size) {
3638                 case 1:
3639                         read_nic_byte(dev, nic_data[i].addr, &tmp8);
3640                         out += sprintf(out, "%30s [%08X] : %02X\n",
3641                                        nic_data[i].name, nic_data[i].addr,
3642                                        tmp8);
3643                         break;
3644                 case 2:
3645                         read_nic_word(dev, nic_data[i].addr, &tmp16);
3646                         out += sprintf(out, "%30s [%08X] : %04X\n",
3647                                        nic_data[i].name, nic_data[i].addr,
3648                                        tmp16);
3649                         break;
3650                 case 4:
3651                         read_nic_dword(dev, nic_data[i].addr, &tmp32);
3652                         out += sprintf(out, "%30s [%08X] : %08X\n",
3653                                        nic_data[i].name, nic_data[i].addr,
3654                                        tmp32);
3655                         break;
3656                 }
3657         }
3658         return out - buf;
3659 }
3660 static DEVICE_ATTR(hardware, S_IRUGO, show_hardware, NULL);
3661
3662
3663 static ssize_t show_memory(struct device *d, char *buf)
3664 {
3665         struct ipw2100_priv *priv = dev_get_drvdata(d);
3666         struct net_device *dev = priv->net_dev;
3667         static unsigned long loop = 0;
3668         int len = 0;
3669         u32 buffer[4];
3670         int i;
3671         char line[81];
3672
3673         if (loop >= 0x30000)
3674                 loop = 0;
3675
3676         /* sysfs provides us PAGE_SIZE buffer */
3677         while (len < PAGE_SIZE - 128 && loop < 0x30000) {
3678
3679                 if (priv->snapshot[0]) for (i = 0; i < 4; i++)
3680                         buffer[i] = *(u32 *)SNAPSHOT_ADDR(loop + i * 4);
3681                 else for (i = 0; i < 4; i++)
3682                         read_nic_dword(dev, loop + i * 4, &buffer[i]);
3683
3684                 if (priv->dump_raw)
3685                         len += sprintf(buf + len,
3686                                        "%c%c%c%c"
3687                                        "%c%c%c%c"
3688                                        "%c%c%c%c"
3689                                        "%c%c%c%c",
3690                                        ((u8*)buffer)[0x0],
3691                                        ((u8*)buffer)[0x1],
3692                                        ((u8*)buffer)[0x2],
3693                                        ((u8*)buffer)[0x3],
3694                                        ((u8*)buffer)[0x4],
3695                                        ((u8*)buffer)[0x5],
3696                                        ((u8*)buffer)[0x6],
3697                                        ((u8*)buffer)[0x7],
3698                                        ((u8*)buffer)[0x8],
3699                                        ((u8*)buffer)[0x9],
3700                                        ((u8*)buffer)[0xa],
3701                                        ((u8*)buffer)[0xb],
3702                                        ((u8*)buffer)[0xc],
3703                                        ((u8*)buffer)[0xd],
3704                                        ((u8*)buffer)[0xe],
3705                                        ((u8*)buffer)[0xf]);
3706                 else
3707                         len += sprintf(buf + len, "%s\n",
3708                                        snprint_line(line, sizeof(line),
3709                                                     (u8*)buffer, 16, loop));
3710                 loop += 16;
3711         }
3712
3713         return len;
3714 }
3715
3716 static ssize_t store_memory(struct device *d, const char *buf, size_t count)
3717 {
3718         struct ipw2100_priv *priv = dev_get_drvdata(d);
3719         struct net_device *dev = priv->net_dev;
3720         const char *p = buf;
3721
3722         if (count < 1)
3723                 return count;
3724
3725         if (p[0] == '1' ||
3726             (count >= 2 && tolower(p[0]) == 'o' && tolower(p[1]) == 'n')) {
3727                 IPW_DEBUG_INFO("%s: Setting memory dump to RAW mode.\n",
3728                        dev->name);
3729                 priv->dump_raw = 1;
3730
3731         } else if (p[0] == '0' || (count >= 2 && tolower(p[0]) == 'o' &&
3732                                   tolower(p[1]) == 'f')) {
3733                 IPW_DEBUG_INFO("%s: Setting memory dump to HEX mode.\n",
3734                        dev->name);
3735                 priv->dump_raw = 0;
3736
3737         } else if (tolower(p[0]) == 'r') {
3738                 IPW_DEBUG_INFO("%s: Resetting firmware snapshot.\n",
3739                        dev->name);
3740                 ipw2100_snapshot_free(priv);
3741
3742         } else
3743                 IPW_DEBUG_INFO("%s: Usage: 0|on = HEX, 1|off = RAW, "
3744                        "reset = clear memory snapshot\n",
3745                        dev->name);
3746
3747         return count;
3748 }
3749 static DEVICE_ATTR(memory, S_IWUSR|S_IRUGO, show_memory, store_memory);
3750
3751
3752 static ssize_t show_ordinals(struct device *d, char *buf)
3753 {
3754         struct ipw2100_priv *priv = dev_get_drvdata(d);
3755         u32 val = 0;
3756         int len = 0;
3757         u32 val_len;
3758         static int loop = 0;
3759
3760         if (loop >= sizeof(ord_data) / sizeof(*ord_data))
3761                 loop = 0;
3762
3763         /* sysfs provides us PAGE_SIZE buffer */
3764         while (len < PAGE_SIZE - 128 &&
3765                loop < (sizeof(ord_data) / sizeof(*ord_data))) {
3766
3767                 val_len = sizeof(u32);
3768
3769                 if (ipw2100_get_ordinal(priv, ord_data[loop].index, &val,
3770                                         &val_len))
3771                         len += sprintf(buf + len, "[0x%02X] = ERROR    %s\n",
3772                                        ord_data[loop].index,
3773                                        ord_data[loop].desc);
3774                 else
3775                         len += sprintf(buf + len, "[0x%02X] = 0x%08X %s\n",
3776                                        ord_data[loop].index, val,
3777                                        ord_data[loop].desc);
3778                 loop++;
3779         }
3780
3781         return len;
3782 }
3783 static DEVICE_ATTR(ordinals, S_IRUGO, show_ordinals, NULL);
3784
3785
3786 static ssize_t show_stats(struct device *d, char *buf)
3787 {
3788         struct ipw2100_priv *priv = dev_get_drvdata(d);
3789         char * out = buf;
3790
3791         out += sprintf(out, "interrupts: %d {tx: %d, rx: %d, other: %d}\n",
3792                        priv->interrupts, priv->tx_interrupts,
3793                        priv->rx_interrupts, priv->inta_other);
3794         out += sprintf(out, "firmware resets: %d\n", priv->resets);
3795         out += sprintf(out, "firmware hangs: %d\n", priv->hangs);
3796 #ifdef CONFIG_IPW_DEBUG
3797         out += sprintf(out, "packet mismatch image: %s\n",
3798                        priv->snapshot[0] ? "YES" : "NO");
3799 #endif
3800
3801         return out - buf;
3802 }
3803 static DEVICE_ATTR(stats, S_IRUGO, show_stats, NULL);
3804
3805
3806 int ipw2100_switch_mode(struct ipw2100_priv *priv, u32 mode)
3807 {
3808         int err;
3809
3810         if (mode == priv->ieee->iw_mode)
3811                 return 0;
3812
3813         err = ipw2100_disable_adapter(priv);
3814         if (err) {
3815                 IPW_DEBUG_ERROR("%s: Could not disable adapter %d\n",
3816                        priv->net_dev->name, err);
3817                 return err;
3818         }
3819
3820         switch (mode) {
3821         case IW_MODE_INFRA:
3822                 priv->net_dev->type = ARPHRD_ETHER;
3823                 break;
3824         case IW_MODE_ADHOC:
3825                 priv->net_dev->type = ARPHRD_ETHER;
3826                 break;
3827 #ifdef CONFIG_IPW2100_MONITOR
3828         case IW_MODE_MONITOR:
3829                 priv->last_mode = priv->ieee->iw_mode;
3830                 priv->net_dev->type = ARPHRD_IEEE80211;
3831                 break;
3832 #endif /* CONFIG_IPW2100_MONITOR */
3833         }
3834
3835         priv->ieee->iw_mode = mode;
3836
3837 #ifdef CONFIG_PM
3838         /* Indicate ipw2100_download_firmware download firmware
3839          * from disk instead of memory. */
3840         ipw2100_firmware.version = 0;
3841 #endif
3842
3843         printk(KERN_INFO "%s: Reseting on mode change.\n",
3844                 priv->net_dev->name);
3845         priv->reset_backoff = 0;
3846         schedule_reset(priv);
3847
3848         return 0;
3849 }
3850
3851 static ssize_t show_internals(struct device *d, char *buf)
3852 {
3853         struct ipw2100_priv *priv = dev_get_drvdata(d);
3854         int len = 0;
3855
3856 #define DUMP_VAR(x,y) len += sprintf(buf + len, # x ": %" # y "\n", priv-> x)
3857
3858         if (priv->status & STATUS_ASSOCIATED)
3859                 len += sprintf(buf + len, "connected: %lu\n",
3860                                get_seconds() - priv->connect_start);
3861         else
3862                 len += sprintf(buf + len, "not connected\n");
3863
3864         DUMP_VAR(ieee->crypt[priv->ieee->tx_keyidx], p);
3865         DUMP_VAR(status, 08lx);
3866         DUMP_VAR(config, 08lx);
3867         DUMP_VAR(capability, 08lx);
3868
3869         len += sprintf(buf + len, "last_rtc: %lu\n", (unsigned long)priv->last_rtc);
3870
3871         DUMP_VAR(fatal_error, d);
3872         DUMP_VAR(stop_hang_check, d);
3873         DUMP_VAR(stop_rf_kill, d);
3874         DUMP_VAR(messages_sent, d);
3875
3876         DUMP_VAR(tx_pend_stat.value, d);
3877         DUMP_VAR(tx_pend_stat.hi, d);
3878
3879         DUMP_VAR(tx_free_stat.value, d);
3880         DUMP_VAR(tx_free_stat.lo, d);
3881
3882         DUMP_VAR(msg_free_stat.value, d);
3883         DUMP_VAR(msg_free_stat.lo, d);
3884
3885         DUMP_VAR(msg_pend_stat.value, d);
3886         DUMP_VAR(msg_pend_stat.hi, d);
3887
3888         DUMP_VAR(fw_pend_stat.value, d);
3889         DUMP_VAR(fw_pend_stat.hi, d);
3890
3891         DUMP_VAR(txq_stat.value, d);
3892         DUMP_VAR(txq_stat.lo, d);
3893
3894         DUMP_VAR(ieee->scans, d);
3895         DUMP_VAR(reset_backoff, d);
3896
3897         return len;
3898 }
3899 static DEVICE_ATTR(internals, S_IRUGO, show_internals, NULL);
3900
3901
3902 static ssize_t show_bssinfo(struct device *d, char *buf)
3903 {
3904         struct ipw2100_priv *priv = dev_get_drvdata(d);
3905         char essid[IW_ESSID_MAX_SIZE + 1];
3906         u8 bssid[ETH_ALEN];
3907         u32 chan = 0;
3908         char * out = buf;
3909         int length;
3910         int ret;
3911
3912         memset(essid, 0, sizeof(essid));
3913         memset(bssid, 0, sizeof(bssid));
3914
3915         length = IW_ESSID_MAX_SIZE;
3916         ret = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_SSID, essid, &length);
3917         if (ret)
3918                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
3919                                __LINE__);
3920
3921         length = sizeof(bssid);
3922         ret = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_AP_BSSID,
3923                                   bssid, &length);
3924         if (ret)
3925                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
3926                                __LINE__);
3927
3928         length = sizeof(u32);
3929         ret = ipw2100_get_ordinal(priv, IPW_ORD_OUR_FREQ, &chan, &length);
3930         if (ret)
3931                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
3932                                __LINE__);
3933
3934         out += sprintf(out, "ESSID: %s\n", essid);
3935         out += sprintf(out, "BSSID:   %02x:%02x:%02x:%02x:%02x:%02x\n",
3936                        bssid[0], bssid[1], bssid[2],
3937                        bssid[3], bssid[4], bssid[5]);
3938         out += sprintf(out, "Channel: %d\n", chan);
3939
3940         return out - buf;
3941 }
3942 static DEVICE_ATTR(bssinfo, S_IRUGO, show_bssinfo, NULL);
3943
3944
3945
3946
3947 #ifdef CONFIG_IPW_DEBUG
3948 static ssize_t show_debug_level(struct device_driver *d, char *buf)
3949 {
3950         return sprintf(buf, "0x%08X\n", ipw2100_debug_level);
3951 }
3952
3953 static ssize_t store_debug_level(struct device_driver *d, const char *buf,
3954                                  size_t count)
3955 {
3956         char *p = (char *)buf;
3957         u32 val;
3958
3959         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
3960                 p++;
3961                 if (p[0] == 'x' || p[0] == 'X')
3962                         p++;
3963                 val = simple_strtoul(p, &p, 16);
3964         } else
3965                 val = simple_strtoul(p, &p, 10);
3966         if (p == buf)
3967                 IPW_DEBUG_INFO(DRV_NAME
3968                        ": %s is not in hex or decimal form.\n", buf);
3969         else
3970                 ipw2100_debug_level = val;
3971
3972         return strnlen(buf, count);
3973 }
3974 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO, show_debug_level,
3975                    store_debug_level);
3976 #endif /* CONFIG_IPW_DEBUG */
3977
3978
3979 static ssize_t show_fatal_error(struct device *d, char *buf)
3980 {
3981         struct ipw2100_priv *priv = dev_get_drvdata(d);
3982         char *out = buf;
3983         int i;
3984
3985         if (priv->fatal_error)
3986                 out += sprintf(out, "0x%08X\n",
3987                                priv->fatal_error);
3988         else
3989                 out += sprintf(out, "0\n");
3990
3991         for (i = 1; i <= IPW2100_ERROR_QUEUE; i++) {
3992                 if (!priv->fatal_errors[(priv->fatal_index - i) %
3993                                         IPW2100_ERROR_QUEUE])
3994                         continue;
3995
3996                 out += sprintf(out, "%d. 0x%08X\n", i,
3997                                priv->fatal_errors[(priv->fatal_index - i) %
3998                                                   IPW2100_ERROR_QUEUE]);
3999         }
4000
4001         return out - buf;
4002 }
4003
4004 static ssize_t store_fatal_error(struct device *d, const char *buf,
4005                                  size_t count)
4006 {
4007         struct ipw2100_priv *priv = dev_get_drvdata(d);
4008         schedule_reset(priv);
4009         return count;
4010 }
4011 static DEVICE_ATTR(fatal_error, S_IWUSR|S_IRUGO, show_fatal_error, store_fatal_error);
4012
4013
4014 static ssize_t show_scan_age(struct device *d, char *buf)
4015 {
4016         struct ipw2100_priv *priv = dev_get_drvdata(d);
4017         return sprintf(buf, "%d\n", priv->ieee->scan_age);
4018 }
4019
4020 static ssize_t store_scan_age(struct device *d, const char *buf, size_t count)
4021 {
4022         struct ipw2100_priv *priv = dev_get_drvdata(d);
4023         struct net_device *dev = priv->net_dev;
4024         char buffer[] = "00000000";
4025         unsigned long len =
4026             (sizeof(buffer) - 1) > count ? count : sizeof(buffer) - 1;
4027         unsigned long val;
4028         char *p = buffer;
4029
4030         IPW_DEBUG_INFO("enter\n");
4031
4032         strncpy(buffer, buf, len);
4033         buffer[len] = 0;
4034
4035         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
4036                 p++;
4037                 if (p[0] == 'x' || p[0] == 'X')
4038                         p++;
4039                 val = simple_strtoul(p, &p, 16);
4040         } else
4041                 val = simple_strtoul(p, &p, 10);
4042         if (p == buffer) {
4043                 IPW_DEBUG_INFO("%s: user supplied invalid value.\n",
4044                        dev->name);
4045         } else {
4046                 priv->ieee->scan_age = val;
4047                 IPW_DEBUG_INFO("set scan_age = %u\n", priv->ieee->scan_age);
4048         }
4049
4050         IPW_DEBUG_INFO("exit\n");
4051         return len;
4052 }
4053 static DEVICE_ATTR(scan_age, S_IWUSR | S_IRUGO, show_scan_age, store_scan_age);
4054
4055
4056 static ssize_t show_rf_kill(struct device *d, char *buf)
4057 {
4058         /* 0 - RF kill not enabled
4059            1 - SW based RF kill active (sysfs)
4060            2 - HW based RF kill active
4061            3 - Both HW and SW baed RF kill active */
4062         struct ipw2100_priv *priv = (struct ipw2100_priv *)d->driver_data;
4063         int val = ((priv->status & STATUS_RF_KILL_SW) ? 0x1 : 0x0) |
4064                 (rf_kill_active(priv) ? 0x2 : 0x0);
4065         return sprintf(buf, "%i\n", val);
4066 }
4067
4068 static int ipw_radio_kill_sw(struct ipw2100_priv *priv, int disable_radio)
4069 {
4070         if ((disable_radio ? 1 : 0) ==
4071             (priv->status & STATUS_RF_KILL_SW ? 1 : 0))
4072                 return 0 ;
4073
4074         IPW_DEBUG_RF_KILL("Manual SW RF Kill set to: RADIO  %s\n",
4075                           disable_radio ? "OFF" : "ON");
4076
4077         down(&priv->action_sem);
4078
4079         if (disable_radio) {
4080                 priv->status |= STATUS_RF_KILL_SW;
4081                 ipw2100_down(priv);
4082         } else {
4083                 priv->status &= ~STATUS_RF_KILL_SW;
4084                 if (rf_kill_active(priv)) {
4085                         IPW_DEBUG_RF_KILL("Can not turn radio back on - "
4086                                           "disabled by HW switch\n");
4087                         /* Make sure the RF_KILL check timer is running */
4088                         priv->stop_rf_kill = 0;
4089                         cancel_delayed_work(&priv->rf_kill);
4090                         queue_delayed_work(priv->workqueue, &priv->rf_kill,
4091                                            HZ);
4092                 } else
4093                         schedule_reset(priv);
4094         }
4095
4096         up(&priv->action_sem);
4097         return 1;
4098 }
4099
4100 static ssize_t store_rf_kill(struct device *d, const char *buf, size_t count)
4101 {
4102         struct ipw2100_priv *priv = dev_get_drvdata(d);
4103         ipw_radio_kill_sw(priv, buf[0] == '1');
4104         return count;
4105 }
4106 static DEVICE_ATTR(rf_kill, S_IWUSR|S_IRUGO, show_rf_kill, store_rf_kill);
4107
4108
4109 static struct attribute *ipw2100_sysfs_entries[] = {
4110         &dev_attr_hardware.attr,
4111         &dev_attr_registers.attr,
4112         &dev_attr_ordinals.attr,
4113         &dev_attr_pci.attr,
4114         &dev_attr_stats.attr,
4115         &dev_attr_internals.attr,
4116         &dev_attr_bssinfo.attr,
4117         &dev_attr_memory.attr,
4118         &dev_attr_scan_age.attr,
4119         &dev_attr_fatal_error.attr,
4120         &dev_attr_rf_kill.attr,
4121         &dev_attr_cfg.attr,
4122         &dev_attr_status.attr,
4123         &dev_attr_capability.attr,
4124         NULL,
4125 };
4126
4127 static struct attribute_group ipw2100_attribute_group = {
4128         .attrs = ipw2100_sysfs_entries,
4129 };
4130
4131
4132 static int status_queue_allocate(struct ipw2100_priv *priv, int entries)
4133 {
4134         struct ipw2100_status_queue *q = &priv->status_queue;
4135
4136         IPW_DEBUG_INFO("enter\n");
4137
4138         q->size = entries * sizeof(struct ipw2100_status);
4139         q->drv = (struct ipw2100_status *)pci_alloc_consistent(
4140                 priv->pci_dev, q->size, &q->nic);
4141         if (!q->drv) {
4142                 IPW_DEBUG_WARNING(
4143                        "Can not allocate status queue.\n");
4144                 return -ENOMEM;
4145         }
4146
4147         memset(q->drv, 0, q->size);
4148
4149         IPW_DEBUG_INFO("exit\n");
4150
4151         return 0;
4152 }
4153
4154 static void status_queue_free(struct ipw2100_priv *priv)
4155 {
4156         IPW_DEBUG_INFO("enter\n");
4157
4158         if (priv->status_queue.drv) {
4159                 pci_free_consistent(
4160                         priv->pci_dev, priv->status_queue.size,
4161                         priv->status_queue.drv, priv->status_queue.nic);
4162                 priv->status_queue.drv = NULL;
4163         }
4164
4165         IPW_DEBUG_INFO("exit\n");
4166 }
4167
4168 static int bd_queue_allocate(struct ipw2100_priv *priv,
4169                              struct ipw2100_bd_queue *q, int entries)
4170 {
4171         IPW_DEBUG_INFO("enter\n");
4172
4173         memset(q, 0, sizeof(struct ipw2100_bd_queue));
4174
4175         q->entries = entries;
4176         q->size = entries * sizeof(struct ipw2100_bd);
4177         q->drv = pci_alloc_consistent(priv->pci_dev, q->size, &q->nic);
4178         if (!q->drv) {
4179                 IPW_DEBUG_INFO("can't allocate shared memory for buffer descriptors\n");
4180                 return -ENOMEM;
4181         }
4182         memset(q->drv, 0, q->size);
4183
4184         IPW_DEBUG_INFO("exit\n");
4185
4186         return 0;
4187 }
4188
4189 static void bd_queue_free(struct ipw2100_priv *priv,
4190                           struct ipw2100_bd_queue *q)
4191 {
4192         IPW_DEBUG_INFO("enter\n");
4193
4194         if (!q)
4195                 return;
4196
4197         if (q->drv) {
4198                 pci_free_consistent(priv->pci_dev,
4199                                     q->size, q->drv, q->nic);
4200                 q->drv = NULL;
4201         }
4202
4203         IPW_DEBUG_INFO("exit\n");
4204 }
4205
4206 static void bd_queue_initialize(
4207         struct ipw2100_priv *priv, struct ipw2100_bd_queue * q,
4208         u32 base, u32 size, u32 r, u32 w)
4209 {
4210         IPW_DEBUG_INFO("enter\n");
4211
4212         IPW_DEBUG_INFO("initializing bd queue at virt=%p, phys=%08x\n", q->drv, (u32)q->nic);
4213
4214         write_register(priv->net_dev, base, q->nic);
4215         write_register(priv->net_dev, size, q->entries);
4216         write_register(priv->net_dev, r, q->oldest);
4217         write_register(priv->net_dev, w, q->next);
4218
4219         IPW_DEBUG_INFO("exit\n");
4220 }
4221
4222 static void ipw2100_kill_workqueue(struct ipw2100_priv *priv)
4223 {
4224         if (priv->workqueue) {
4225                 priv->stop_rf_kill = 1;
4226                 priv->stop_hang_check = 1;
4227                 cancel_delayed_work(&priv->reset_work);
4228                 cancel_delayed_work(&priv->security_work);
4229                 cancel_delayed_work(&priv->wx_event_work);
4230                 cancel_delayed_work(&priv->hang_check);
4231                 cancel_delayed_work(&priv->rf_kill);
4232                 destroy_workqueue(priv->workqueue);
4233                 priv->workqueue = NULL;
4234         }
4235 }
4236
4237 static int ipw2100_tx_allocate(struct ipw2100_priv *priv)
4238 {
4239         int i, j, err = -EINVAL;
4240         void *v;
4241         dma_addr_t p;
4242
4243         IPW_DEBUG_INFO("enter\n");
4244
4245         err = bd_queue_allocate(priv, &priv->tx_queue, TX_QUEUE_LENGTH);
4246         if (err) {
4247                 IPW_DEBUG_ERROR("%s: failed bd_queue_allocate\n",
4248                        priv->net_dev->name);
4249                 return err;
4250         }
4251
4252         priv->tx_buffers = (struct ipw2100_tx_packet *)kmalloc(
4253                 TX_PENDED_QUEUE_LENGTH * sizeof(struct ipw2100_tx_packet),
4254                 GFP_ATOMIC);
4255         if (!priv->tx_buffers) {
4256                 IPW_DEBUG_ERROR("%s: alloc failed form tx buffers.\n",
4257                        priv->net_dev->name);
4258                 bd_queue_free(priv, &priv->tx_queue);
4259                 return -ENOMEM;
4260         }
4261
4262         for (i = 0; i < TX_PENDED_QUEUE_LENGTH; i++) {
4263                 v = pci_alloc_consistent(
4264                         priv->pci_dev, sizeof(struct ipw2100_data_header), &p);
4265                 if (!v) {
4266                         IPW_DEBUG_ERROR("%s: PCI alloc failed for tx "
4267                                "buffers.\n", priv->net_dev->name);
4268                         err = -ENOMEM;
4269                         break;
4270                 }
4271
4272                 priv->tx_buffers[i].type = DATA;
4273                 priv->tx_buffers[i].info.d_struct.data = (struct ipw2100_data_header*)v;
4274                 priv->tx_buffers[i].info.d_struct.data_phys = p;
4275                 priv->tx_buffers[i].info.d_struct.txb = NULL;
4276         }
4277
4278         if (i == TX_PENDED_QUEUE_LENGTH)
4279                 return 0;
4280
4281         for (j = 0; j < i; j++) {
4282                 pci_free_consistent(
4283                         priv->pci_dev,
4284                         sizeof(struct ipw2100_data_header),
4285                         priv->tx_buffers[j].info.d_struct.data,
4286                         priv->tx_buffers[j].info.d_struct.data_phys);
4287         }
4288
4289         kfree(priv->tx_buffers);
4290         priv->tx_buffers = NULL;
4291
4292         return err;
4293 }
4294
4295 static void ipw2100_tx_initialize(struct ipw2100_priv *priv)
4296 {
4297         int i;
4298
4299         IPW_DEBUG_INFO("enter\n");
4300
4301         /*
4302          * reinitialize packet info lists
4303          */
4304         INIT_LIST_HEAD(&priv->fw_pend_list);
4305         INIT_STAT(&priv->fw_pend_stat);
4306
4307         /*
4308          * reinitialize lists
4309          */
4310         INIT_LIST_HEAD(&priv->tx_pend_list);
4311         INIT_LIST_HEAD(&priv->tx_free_list);
4312         INIT_STAT(&priv->tx_pend_stat);
4313         INIT_STAT(&priv->tx_free_stat);
4314
4315         for (i = 0; i < TX_PENDED_QUEUE_LENGTH; i++) {
4316                 /* We simply drop any SKBs that have been queued for
4317                  * transmit */
4318                 if (priv->tx_buffers[i].info.d_struct.txb) {
4319                         ieee80211_txb_free(priv->tx_buffers[i].info.d_struct.txb);
4320                         priv->tx_buffers[i].info.d_struct.txb = NULL;
4321                 }
4322
4323                 list_add_tail(&priv->tx_buffers[i].list, &priv->tx_free_list);
4324         }
4325
4326         SET_STAT(&priv->tx_free_stat, i);
4327
4328         priv->tx_queue.oldest = 0;
4329         priv->tx_queue.available = priv->tx_queue.entries;
4330         priv->tx_queue.next = 0;
4331         INIT_STAT(&priv->txq_stat);
4332         SET_STAT(&priv->txq_stat, priv->tx_queue.available);
4333
4334         bd_queue_initialize(priv, &priv->tx_queue,
4335                             IPW_MEM_HOST_SHARED_TX_QUEUE_BD_BASE,
4336                             IPW_MEM_HOST_SHARED_TX_QUEUE_BD_SIZE,
4337                             IPW_MEM_HOST_SHARED_TX_QUEUE_READ_INDEX,
4338                             IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX);
4339
4340         IPW_DEBUG_INFO("exit\n");
4341
4342 }
4343
4344 static void ipw2100_tx_free(struct ipw2100_priv *priv)
4345 {
4346         int i;
4347
4348         IPW_DEBUG_INFO("enter\n");
4349
4350         bd_queue_free(priv, &priv->tx_queue);
4351
4352         if (!priv->tx_buffers)
4353                 return;
4354
4355         for (i = 0; i < TX_PENDED_QUEUE_LENGTH; i++) {
4356                 if (priv->tx_buffers[i].info.d_struct.txb) {
4357                         ieee80211_txb_free(priv->tx_buffers[i].info.d_struct.txb);
4358                         priv->tx_buffers[i].info.d_struct.txb = NULL;
4359                 }
4360                 if (priv->tx_buffers[i].info.d_struct.data)
4361                         pci_free_consistent(
4362                                 priv->pci_dev,
4363                                 sizeof(struct ipw2100_data_header),
4364                                 priv->tx_buffers[i].info.d_struct.data,
4365                                 priv->tx_buffers[i].info.d_struct.data_phys);
4366         }
4367
4368         kfree(priv->tx_buffers);
4369         priv->tx_buffers = NULL;
4370
4371         IPW_DEBUG_INFO("exit\n");
4372 }
4373
4374
4375
4376 static int ipw2100_rx_allocate(struct ipw2100_priv *priv)
4377 {
4378         int i, j, err = -EINVAL;
4379
4380         IPW_DEBUG_INFO("enter\n");
4381
4382         err = bd_queue_allocate(priv, &priv->rx_queue, RX_QUEUE_LENGTH);
4383         if (err) {
4384                 IPW_DEBUG_INFO("failed bd_queue_allocate\n");
4385                 return err;
4386         }
4387
4388         err = status_queue_allocate(priv, RX_QUEUE_LENGTH);
4389         if (err) {
4390                 IPW_DEBUG_INFO("failed status_queue_allocate\n");
4391                 bd_queue_free(priv, &priv->rx_queue);
4392                 return err;
4393         }
4394
4395         /*
4396          * allocate packets
4397          */
4398         priv->rx_buffers = (struct ipw2100_rx_packet *)
4399             kmalloc(RX_QUEUE_LENGTH * sizeof(struct ipw2100_rx_packet),
4400                     GFP_KERNEL);
4401         if (!priv->rx_buffers) {
4402                 IPW_DEBUG_INFO("can't allocate rx packet buffer table\n");
4403
4404                 bd_queue_free(priv, &priv->rx_queue);
4405
4406                 status_queue_free(priv);
4407
4408                 return -ENOMEM;
4409         }
4410
4411         for (i = 0; i < RX_QUEUE_LENGTH; i++) {
4412                 struct ipw2100_rx_packet *packet = &priv->rx_buffers[i];
4413
4414                 err = ipw2100_alloc_skb(priv, packet);
4415                 if (unlikely(err)) {
4416                         err = -ENOMEM;
4417                         break;
4418                 }
4419
4420                 /* The BD holds the cache aligned address */
4421                 priv->rx_queue.drv[i].host_addr = packet->dma_addr;
4422                 priv->rx_queue.drv[i].buf_length = IPW_RX_NIC_BUFFER_LENGTH;
4423                 priv->status_queue.drv[i].status_fields = 0;
4424         }
4425
4426         if (i == RX_QUEUE_LENGTH)
4427                 return 0;
4428
4429         for (j = 0; j < i; j++) {
4430                 pci_unmap_single(priv->pci_dev, priv->rx_buffers[j].dma_addr,
4431                                  sizeof(struct ipw2100_rx_packet),
4432                                  PCI_DMA_FROMDEVICE);
4433                 dev_kfree_skb(priv->rx_buffers[j].skb);
4434         }
4435
4436         kfree(priv->rx_buffers);
4437         priv->rx_buffers = NULL;
4438
4439         bd_queue_free(priv, &priv->rx_queue);
4440
4441         status_queue_free(priv);
4442
4443         return err;
4444 }
4445
4446 static void ipw2100_rx_initialize(struct ipw2100_priv *priv)
4447 {
4448         IPW_DEBUG_INFO("enter\n");
4449
4450         priv->rx_queue.oldest = 0;
4451         priv->rx_queue.available = priv->rx_queue.entries - 1;
4452         priv->rx_queue.next = priv->rx_queue.entries - 1;
4453
4454         INIT_STAT(&priv->rxq_stat);
4455         SET_STAT(&priv->rxq_stat, priv->rx_queue.available);
4456
4457         bd_queue_initialize(priv, &priv->rx_queue,
4458                             IPW_MEM_HOST_SHARED_RX_BD_BASE,
4459                             IPW_MEM_HOST_SHARED_RX_BD_SIZE,
4460                             IPW_MEM_HOST_SHARED_RX_READ_INDEX,
4461                             IPW_MEM_HOST_SHARED_RX_WRITE_INDEX);
4462
4463         /* set up the status queue */
4464         write_register(priv->net_dev, IPW_MEM_HOST_SHARED_RX_STATUS_BASE,
4465                        priv->status_queue.nic);
4466
4467         IPW_DEBUG_INFO("exit\n");
4468 }
4469
4470 static void ipw2100_rx_free(struct ipw2100_priv *priv)
4471 {
4472         int i;
4473
4474         IPW_DEBUG_INFO("enter\n");
4475
4476         bd_queue_free(priv, &priv->rx_queue);
4477         status_queue_free(priv);
4478
4479         if (!priv->rx_buffers)
4480                 return;
4481
4482         for (i = 0; i < RX_QUEUE_LENGTH; i++) {
4483                 if (priv->rx_buffers[i].rxp) {
4484                         pci_unmap_single(priv->pci_dev,
4485                                          priv->rx_buffers[i].dma_addr,
4486                                          sizeof(struct ipw2100_rx),
4487                                          PCI_DMA_FROMDEVICE);
4488                         dev_kfree_skb(priv->rx_buffers[i].skb);
4489                 }
4490         }
4491
4492         kfree(priv->rx_buffers);
4493         priv->rx_buffers = NULL;
4494
4495         IPW_DEBUG_INFO("exit\n");
4496 }
4497
4498 static int ipw2100_read_mac_address(struct ipw2100_priv *priv)
4499 {
4500         u32 length = ETH_ALEN;
4501         u8 mac[ETH_ALEN];
4502
4503         int err;
4504
4505         err = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ADAPTER_MAC,
4506                                   mac, &length);
4507         if (err) {
4508                 IPW_DEBUG_INFO("MAC address read failed\n");
4509                 return -EIO;
4510         }
4511         IPW_DEBUG_INFO("card MAC is %02X:%02X:%02X:%02X:%02X:%02X\n",
4512                mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
4513
4514         memcpy(priv->net_dev->dev_addr, mac, ETH_ALEN);
4515
4516         return 0;
4517 }
4518
4519 /********************************************************************
4520  *
4521  * Firmware Commands
4522  *
4523  ********************************************************************/
4524
4525 int ipw2100_set_mac_address(struct ipw2100_priv *priv, int batch_mode)
4526 {
4527         struct host_command cmd = {
4528                 .host_command = ADAPTER_ADDRESS,
4529                 .host_command_sequence = 0,
4530                 .host_command_length = ETH_ALEN
4531         };
4532         int err;
4533
4534         IPW_DEBUG_HC("SET_MAC_ADDRESS\n");
4535
4536         IPW_DEBUG_INFO("enter\n");
4537
4538         if (priv->config & CFG_CUSTOM_MAC) {
4539                 memcpy(cmd.host_command_parameters, priv->mac_addr,
4540                        ETH_ALEN);
4541                 memcpy(priv->net_dev->dev_addr, priv->mac_addr, ETH_ALEN);
4542         } else
4543                 memcpy(cmd.host_command_parameters, priv->net_dev->dev_addr,
4544                        ETH_ALEN);
4545
4546         err = ipw2100_hw_send_command(priv, &cmd);
4547
4548         IPW_DEBUG_INFO("exit\n");
4549         return err;
4550 }
4551
4552 int ipw2100_set_port_type(struct ipw2100_priv *priv, u32 port_type,
4553                                  int batch_mode)
4554 {
4555         struct host_command cmd = {
4556                 .host_command = PORT_TYPE,
4557                 .host_command_sequence = 0,
4558                 .host_command_length = sizeof(u32)
4559         };
4560         int err;
4561
4562         switch (port_type) {
4563         case IW_MODE_INFRA:
4564                 cmd.host_command_parameters[0] = IPW_BSS;
4565                 break;
4566         case IW_MODE_ADHOC:
4567                 cmd.host_command_parameters[0] = IPW_IBSS;
4568                 break;
4569         }
4570
4571         IPW_DEBUG_HC("PORT_TYPE: %s\n",
4572                      port_type == IPW_IBSS ? "Ad-Hoc" : "Managed");
4573
4574         if (!batch_mode) {
4575                 err = ipw2100_disable_adapter(priv);
4576                 if (err) {
4577                         IPW_DEBUG_ERROR("%s: Could not disable adapter %d\n",
4578                                priv->net_dev->name, err);
4579                         return err;
4580                 }
4581         }
4582
4583         /* send cmd to firmware */
4584         err = ipw2100_hw_send_command(priv, &cmd);
4585
4586         if (!batch_mode)
4587                 ipw2100_enable_adapter(priv);
4588
4589         return err;
4590 }
4591
4592
4593 int ipw2100_set_channel(struct ipw2100_priv *priv, u32 channel, int batch_mode)
4594 {
4595         struct host_command cmd = {
4596                 .host_command = CHANNEL,
4597                 .host_command_sequence = 0,
4598                 .host_command_length = sizeof(u32)
4599         };
4600         int err;
4601
4602         cmd.host_command_parameters[0] = channel;
4603
4604         IPW_DEBUG_HC("CHANNEL: %d\n", channel);
4605
4606         /* If BSS then we don't support channel selection */
4607         if (priv->ieee->iw_mode == IW_MODE_INFRA)
4608                 return 0;
4609
4610         if ((channel != 0) &&
4611             ((channel < REG_MIN_CHANNEL) || (channel > REG_MAX_CHANNEL)))
4612                 return -EINVAL;
4613
4614         if (!batch_mode) {
4615                 err = ipw2100_disable_adapter(priv);
4616                 if (err)
4617                         return err;
4618         }
4619
4620         err = ipw2100_hw_send_command(priv, &cmd);
4621         if (err) {
4622                 IPW_DEBUG_INFO("Failed to set channel to %d",
4623                                channel);
4624                 return err;
4625         }
4626
4627         if (channel)
4628                 priv->config |= CFG_STATIC_CHANNEL;
4629         else
4630                 priv->config &= ~CFG_STATIC_CHANNEL;
4631
4632         priv->channel = channel;
4633
4634         if (!batch_mode) {
4635                 err = ipw2100_enable_adapter(priv);
4636                 if (err)
4637                         return err;
4638         }
4639
4640         return 0;
4641 }
4642
4643 int ipw2100_system_config(struct ipw2100_priv *priv, int batch_mode)
4644 {
4645         struct host_command cmd = {
4646                 .host_command = SYSTEM_CONFIG,
4647                 .host_command_sequence = 0,
4648                 .host_command_length = 12,
4649         };
4650         u32 ibss_mask, len = sizeof(u32);
4651         int err;
4652
4653         /* Set system configuration */
4654
4655         if (!batch_mode) {
4656                 err = ipw2100_disable_adapter(priv);
4657                 if (err)
4658                         return err;
4659         }
4660
4661         if (priv->ieee->iw_mode == IW_MODE_ADHOC)
4662                 cmd.host_command_parameters[0] |= IPW_CFG_IBSS_AUTO_START;
4663
4664         cmd.host_command_parameters[0] |= IPW_CFG_IBSS_MASK |
4665                 IPW_CFG_BSS_MASK |
4666                 IPW_CFG_802_1x_ENABLE;
4667
4668         if (!(priv->config & CFG_LONG_PREAMBLE))
4669                 cmd.host_command_parameters[0] |= IPW_CFG_PREAMBLE_AUTO;
4670
4671         err = ipw2100_get_ordinal(priv,
4672                                   IPW_ORD_EEPROM_IBSS_11B_CHANNELS,
4673                                   &ibss_mask,  &len);
4674         if (err)
4675                 ibss_mask = IPW_IBSS_11B_DEFAULT_MASK;
4676
4677         cmd.host_command_parameters[1] = REG_CHANNEL_MASK;
4678         cmd.host_command_parameters[2] = REG_CHANNEL_MASK & ibss_mask;
4679
4680         /* 11b only */
4681         /*cmd.host_command_parameters[0] |= DIVERSITY_ANTENNA_A;*/
4682
4683         err = ipw2100_hw_send_command(priv, &cmd);
4684         if (err)
4685                 return err;
4686
4687 /* If IPv6 is configured in the kernel then we don't want to filter out all
4688  * of the multicast packets as IPv6 needs some. */
4689 #if !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE)
4690         cmd.host_command = ADD_MULTICAST;
4691         cmd.host_command_sequence = 0;
4692         cmd.host_command_length = 0;
4693
4694         ipw2100_hw_send_command(priv, &cmd);
4695 #endif
4696         if (!batch_mode) {
4697                 err = ipw2100_enable_adapter(priv);
4698                 if (err)
4699                         return err;
4700         }
4701
4702         return 0;
4703 }
4704
4705 int ipw2100_set_tx_rates(struct ipw2100_priv *priv, u32 rate, int batch_mode)
4706 {
4707         struct host_command cmd = {
4708                 .host_command = BASIC_TX_RATES,
4709                 .host_command_sequence = 0,
4710                 .host_command_length = 4
4711         };
4712         int err;
4713
4714         cmd.host_command_parameters[0] = rate & TX_RATE_MASK;
4715
4716         if (!batch_mode) {
4717                 err = ipw2100_disable_adapter(priv);
4718                 if (err)
4719                         return err;
4720         }
4721
4722         /* Set BASIC TX Rate first */
4723         ipw2100_hw_send_command(priv, &cmd);
4724
4725         /* Set TX Rate */
4726         cmd.host_command = TX_RATES;
4727         ipw2100_hw_send_command(priv, &cmd);
4728
4729         /* Set MSDU TX Rate */
4730         cmd.host_command = MSDU_TX_RATES;
4731         ipw2100_hw_send_command(priv, &cmd);
4732
4733         if (!batch_mode) {
4734                 err = ipw2100_enable_adapter(priv);
4735                 if (err)
4736                         return err;
4737         }
4738
4739         priv->tx_rates = rate;
4740
4741         return 0;
4742 }
4743
4744 int ipw2100_set_power_mode(struct ipw2100_priv *priv,
4745                            int power_level)
4746 {
4747         struct host_command cmd = {
4748                 .host_command = POWER_MODE,
4749                 .host_command_sequence = 0,
4750                 .host_command_length = 4
4751         };
4752         int err;
4753
4754         cmd.host_command_parameters[0] = power_level;
4755
4756         err = ipw2100_hw_send_command(priv, &cmd);
4757         if (err)
4758                 return err;
4759
4760         if (power_level == IPW_POWER_MODE_CAM)
4761                 priv->power_mode = IPW_POWER_LEVEL(priv->power_mode);
4762         else
4763                 priv->power_mode = IPW_POWER_ENABLED | power_level;
4764
4765 #ifdef CONFIG_IPW2100_TX_POWER
4766         if (priv->port_type == IBSS &&
4767             priv->adhoc_power != DFTL_IBSS_TX_POWER) {
4768                 /* Set beacon interval */
4769                 cmd.host_command = TX_POWER_INDEX;
4770                 cmd.host_command_parameters[0] = (u32)priv->adhoc_power;
4771
4772                 err = ipw2100_hw_send_command(priv, &cmd);
4773                 if (err)
4774                         return err;
4775         }
4776 #endif
4777
4778         return 0;
4779 }
4780
4781
4782 int ipw2100_set_rts_threshold(struct ipw2100_priv *priv, u32 threshold)
4783 {
4784         struct host_command cmd = {
4785                 .host_command = RTS_THRESHOLD,
4786                 .host_command_sequence = 0,
4787                 .host_command_length = 4
4788         };
4789         int err;
4790
4791         if (threshold & RTS_DISABLED)
4792                 cmd.host_command_parameters[0] = MAX_RTS_THRESHOLD;
4793         else
4794                 cmd.host_command_parameters[0] = threshold & ~RTS_DISABLED;
4795
4796         err = ipw2100_hw_send_command(priv, &cmd);
4797         if (err)
4798                 return err;
4799
4800         priv->rts_threshold = threshold;
4801
4802         return 0;
4803 }
4804
4805 #if 0
4806 int ipw2100_set_fragmentation_threshold(struct ipw2100_priv *priv,
4807                                         u32 threshold, int batch_mode)
4808 {
4809         struct host_command cmd = {
4810                 .host_command = FRAG_THRESHOLD,
4811                 .host_command_sequence = 0,
4812                 .host_command_length = 4,
4813                 .host_command_parameters[0] = 0,
4814         };
4815         int err;
4816
4817         if (!batch_mode) {
4818                 err = ipw2100_disable_adapter(priv);
4819                 if (err)
4820                         return err;
4821         }
4822
4823         if (threshold == 0)
4824                 threshold = DEFAULT_FRAG_THRESHOLD;
4825         else {
4826                 threshold = max(threshold, MIN_FRAG_THRESHOLD);
4827                 threshold = min(threshold, MAX_FRAG_THRESHOLD);
4828         }
4829
4830         cmd.host_command_parameters[0] = threshold;
4831
4832         IPW_DEBUG_HC("FRAG_THRESHOLD: %u\n", threshold);
4833
4834         err = ipw2100_hw_send_command(priv, &cmd);
4835
4836         if (!batch_mode)
4837                 ipw2100_enable_adapter(priv);
4838
4839         if (!err)
4840                 priv->frag_threshold = threshold;
4841
4842         return err;
4843 }
4844 #endif
4845
4846 int ipw2100_set_short_retry(struct ipw2100_priv *priv, u32 retry)
4847 {
4848         struct host_command cmd = {
4849                 .host_command = SHORT_RETRY_LIMIT,
4850                 .host_command_sequence = 0,
4851                 .host_command_length = 4
4852         };
4853         int err;
4854
4855         cmd.host_command_parameters[0] = retry;
4856
4857         err = ipw2100_hw_send_command(priv, &cmd);
4858         if (err)
4859                 return err;
4860
4861         priv->short_retry_limit = retry;
4862
4863         return 0;
4864 }
4865
4866 int ipw2100_set_long_retry(struct ipw2100_priv *priv, u32 retry)
4867 {
4868         struct host_command cmd = {
4869                 .host_command = LONG_RETRY_LIMIT,
4870                 .host_command_sequence = 0,
4871                 .host_command_length = 4
4872         };
4873         int err;
4874
4875         cmd.host_command_parameters[0] = retry;
4876
4877         err = ipw2100_hw_send_command(priv, &cmd);
4878         if (err)
4879                 return err;
4880
4881         priv->long_retry_limit = retry;
4882
4883         return 0;
4884 }
4885
4886
4887 int ipw2100_set_mandatory_bssid(struct ipw2100_priv *priv, u8 *bssid,
4888                                 int batch_mode)
4889 {
4890         struct host_command cmd = {
4891                 .host_command = MANDATORY_BSSID,
4892                 .host_command_sequence = 0,
4893                 .host_command_length = (bssid == NULL) ? 0 : ETH_ALEN
4894         };
4895         int err;
4896
4897 #ifdef CONFIG_IPW_DEBUG
4898         if (bssid != NULL)
4899                 IPW_DEBUG_HC(
4900                         "MANDATORY_BSSID: %02X:%02X:%02X:%02X:%02X:%02X\n",
4901                         bssid[0], bssid[1], bssid[2], bssid[3], bssid[4],
4902                         bssid[5]);
4903         else
4904                 IPW_DEBUG_HC("MANDATORY_BSSID: <clear>\n");
4905 #endif
4906         /* if BSSID is empty then we disable mandatory bssid mode */
4907         if (bssid != NULL)
4908                 memcpy((u8 *)cmd.host_command_parameters, bssid, ETH_ALEN);
4909
4910         if (!batch_mode) {
4911                 err = ipw2100_disable_adapter(priv);
4912                 if (err)
4913                         return err;
4914         }
4915
4916         err = ipw2100_hw_send_command(priv, &cmd);
4917
4918         if (!batch_mode)
4919                 ipw2100_enable_adapter(priv);
4920
4921         return err;
4922 }
4923
4924 #ifdef CONFIG_IEEE80211_WPA
4925 static int ipw2100_disassociate_bssid(struct ipw2100_priv *priv)
4926 {
4927         struct host_command cmd = {
4928                 .host_command = DISASSOCIATION_BSSID,
4929                 .host_command_sequence = 0,
4930                 .host_command_length = ETH_ALEN
4931         };
4932         int err;
4933         int len;
4934
4935         IPW_DEBUG_HC("DISASSOCIATION_BSSID\n");
4936
4937         len = ETH_ALEN;
4938         /* The Firmware currently ignores the BSSID and just disassociates from
4939          * the currently associated AP -- but in the off chance that a future
4940          * firmware does use the BSSID provided here, we go ahead and try and
4941          * set it to the currently associated AP's BSSID */
4942         memcpy(cmd.host_command_parameters, priv->bssid, ETH_ALEN);
4943
4944         err = ipw2100_hw_send_command(priv, &cmd);
4945
4946         return err;
4947 }
4948 #endif
4949
4950 /*
4951  * Pseudo code for setting up wpa_frame:
4952  */
4953 #if 0
4954 void x(struct ieee80211_assoc_frame *wpa_assoc)
4955 {
4956         struct ipw2100_wpa_assoc_frame frame;
4957         frame->fixed_ie_mask = IPW_WPA_CAPABILTIES |
4958                 IPW_WPA_LISTENINTERVAL |
4959                 IPW_WPA_AP_ADDRESS;
4960         frame->capab_info = wpa_assoc->capab_info;
4961         frame->lisen_interval = wpa_assoc->listent_interval;
4962         memcpy(frame->current_ap, wpa_assoc->current_ap, ETH_ALEN);
4963
4964         /* UNKNOWN -- I'm not postivive about this part; don't have any WPA
4965          * setup here to test it with.
4966          *
4967          * Walk the IEs in the wpa_assoc and figure out the total size of all
4968          * that data.  Stick that into frame->var_ie_len.  Then memcpy() all of
4969          * the IEs from wpa_frame into frame.
4970          */
4971         frame->var_ie_len = calculate_ie_len(wpa_assoc);
4972         memcpy(frame->var_ie,  wpa_assoc->variable, frame->var_ie_len);
4973
4974         ipw2100_set_wpa_ie(priv, &frame, 0);
4975 }
4976 #endif
4977
4978
4979
4980
4981 static int ipw2100_set_wpa_ie(struct ipw2100_priv *,
4982                               struct ipw2100_wpa_assoc_frame *, int)
4983 __attribute__ ((unused));
4984
4985 static int ipw2100_set_wpa_ie(struct ipw2100_priv *priv,
4986                               struct ipw2100_wpa_assoc_frame *wpa_frame,
4987                               int batch_mode)
4988 {
4989         struct host_command cmd = {
4990                 .host_command = SET_WPA_IE,
4991                 .host_command_sequence = 0,
4992                 .host_command_length = sizeof(struct ipw2100_wpa_assoc_frame),
4993         };
4994         int err;
4995
4996         IPW_DEBUG_HC("SET_WPA_IE\n");
4997
4998         if (!batch_mode) {
4999                 err = ipw2100_disable_adapter(priv);
5000                 if (err)
5001                         return err;
5002         }
5003
5004         memcpy(cmd.host_command_parameters, wpa_frame,
5005                sizeof(struct ipw2100_wpa_assoc_frame));
5006
5007         err = ipw2100_hw_send_command(priv, &cmd);
5008
5009         if (!batch_mode) {
5010                 if (ipw2100_enable_adapter(priv))
5011                         err = -EIO;
5012         }
5013
5014         return err;
5015 }
5016
5017 struct security_info_params {
5018         u32 allowed_ciphers;
5019         u16 version;
5020         u8 auth_mode;
5021         u8 replay_counters_number;
5022         u8 unicast_using_group;
5023 } __attribute__ ((packed));
5024
5025 int ipw2100_set_security_information(struct ipw2100_priv *priv,
5026                                      int auth_mode,
5027                                      int security_level,
5028                                      int unicast_using_group,
5029                                      int batch_mode)
5030 {
5031         struct host_command cmd = {
5032                 .host_command = SET_SECURITY_INFORMATION,
5033                 .host_command_sequence = 0,
5034                 .host_command_length = sizeof(struct security_info_params)
5035         };
5036         struct security_info_params *security =
5037                 (struct security_info_params *)&cmd.host_command_parameters;
5038         int err;
5039         memset(security, 0, sizeof(*security));
5040
5041         /* If shared key AP authentication is turned on, then we need to
5042          * configure the firmware to try and use it.
5043          *
5044          * Actual data encryption/decryption is handled by the host. */
5045         security->auth_mode = auth_mode;
5046         security->unicast_using_group = unicast_using_group;
5047
5048         switch (security_level) {
5049         default:
5050         case SEC_LEVEL_0:
5051                 security->allowed_ciphers = IPW_NONE_CIPHER;
5052                 break;
5053         case SEC_LEVEL_1:
5054                 security->allowed_ciphers = IPW_WEP40_CIPHER |
5055                         IPW_WEP104_CIPHER;
5056                 break;
5057         case SEC_LEVEL_2:
5058                 security->allowed_ciphers = IPW_WEP40_CIPHER |
5059                         IPW_WEP104_CIPHER | IPW_TKIP_CIPHER;
5060                 break;
5061         case SEC_LEVEL_2_CKIP:
5062                 security->allowed_ciphers = IPW_WEP40_CIPHER |
5063                         IPW_WEP104_CIPHER | IPW_CKIP_CIPHER;
5064                 break;
5065         case SEC_LEVEL_3:
5066                 security->allowed_ciphers = IPW_WEP40_CIPHER |
5067                         IPW_WEP104_CIPHER | IPW_TKIP_CIPHER | IPW_CCMP_CIPHER;
5068                 break;
5069         }
5070
5071         IPW_DEBUG_HC(
5072                 "SET_SECURITY_INFORMATION: auth:%d cipher:0x%02X (level %d)\n",
5073                 security->auth_mode, security->allowed_ciphers, security_level);
5074
5075         security->replay_counters_number = 0;
5076
5077         if (!batch_mode) {
5078                 err = ipw2100_disable_adapter(priv);
5079                 if (err)
5080                         return err;
5081         }
5082
5083         err = ipw2100_hw_send_command(priv, &cmd);
5084
5085         if (!batch_mode)
5086                 ipw2100_enable_adapter(priv);
5087
5088         return err;
5089 }
5090
5091 int ipw2100_set_tx_power(struct ipw2100_priv *priv,
5092                          u32 tx_power)
5093 {
5094         struct host_command cmd = {
5095                 .host_command = TX_POWER_INDEX,
5096                 .host_command_sequence = 0,
5097                 .host_command_length = 4
5098         };
5099         int err = 0;
5100
5101         cmd.host_command_parameters[0] = tx_power;
5102
5103         if (priv->ieee->iw_mode == IW_MODE_ADHOC)
5104                 err = ipw2100_hw_send_command(priv, &cmd);
5105         if (!err)
5106                 priv->tx_power = tx_power;
5107
5108         return 0;
5109 }
5110
5111 int ipw2100_set_ibss_beacon_interval(struct ipw2100_priv *priv,
5112                                      u32 interval, int batch_mode)
5113 {
5114         struct host_command cmd = {
5115                 .host_command = BEACON_INTERVAL,
5116                 .host_command_sequence = 0,
5117                 .host_command_length = 4
5118         };
5119         int err;
5120
5121         cmd.host_command_parameters[0] = interval;
5122
5123         IPW_DEBUG_INFO("enter\n");
5124
5125         if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
5126                 if (!batch_mode) {
5127                         err = ipw2100_disable_adapter(priv);
5128                         if (err)
5129                                 return err;
5130                 }
5131
5132                 ipw2100_hw_send_command(priv, &cmd);
5133
5134                 if (!batch_mode) {
5135                         err = ipw2100_enable_adapter(priv);
5136                         if (err)
5137                                 return err;
5138                 }
5139         }
5140
5141         IPW_DEBUG_INFO("exit\n");
5142
5143         return 0;
5144 }
5145
5146
5147 void ipw2100_queues_initialize(struct ipw2100_priv *priv)
5148 {
5149         ipw2100_tx_initialize(priv);
5150         ipw2100_rx_initialize(priv);
5151         ipw2100_msg_initialize(priv);
5152 }
5153
5154 void ipw2100_queues_free(struct ipw2100_priv *priv)
5155 {
5156         ipw2100_tx_free(priv);
5157         ipw2100_rx_free(priv);
5158         ipw2100_msg_free(priv);
5159 }
5160
5161 int ipw2100_queues_allocate(struct ipw2100_priv *priv)
5162 {
5163         if (ipw2100_tx_allocate(priv) ||
5164             ipw2100_rx_allocate(priv) ||
5165             ipw2100_msg_allocate(priv))
5166                 goto fail;
5167
5168         return 0;
5169
5170  fail:
5171         ipw2100_tx_free(priv);
5172         ipw2100_rx_free(priv);
5173         ipw2100_msg_free(priv);
5174         return -ENOMEM;
5175 }
5176
5177 #define IPW_PRIVACY_CAPABLE 0x0008
5178
5179 static int ipw2100_set_wep_flags(struct ipw2100_priv *priv, u32 flags,
5180                                  int batch_mode)
5181 {
5182         struct host_command cmd = {
5183                 .host_command = WEP_FLAGS,
5184                 .host_command_sequence = 0,
5185                 .host_command_length = 4
5186         };
5187         int err;
5188
5189         cmd.host_command_parameters[0] = flags;
5190
5191         IPW_DEBUG_HC("WEP_FLAGS: flags = 0x%08X\n", flags);
5192
5193         if (!batch_mode) {
5194                 err = ipw2100_disable_adapter(priv);
5195                 if (err) {
5196                         IPW_DEBUG_ERROR("%s: Could not disable adapter %d\n",
5197                                priv->net_dev->name, err);
5198                         return err;
5199                 }
5200         }
5201
5202         /* send cmd to firmware */
5203         err = ipw2100_hw_send_command(priv, &cmd);
5204
5205         if (!batch_mode)
5206                 ipw2100_enable_adapter(priv);
5207
5208         return err;
5209 }
5210
5211 struct ipw2100_wep_key {
5212         u8 idx;
5213         u8 len;
5214         u8 key[13];
5215 };
5216
5217 /* Macros to ease up priting WEP keys */
5218 #define WEP_FMT_64  "%02X%02X%02X%02X-%02X"
5219 #define WEP_FMT_128 "%02X%02X%02X%02X-%02X%02X%02X%02X-%02X%02X%02X"
5220 #define WEP_STR_64(x) x[0],x[1],x[2],x[3],x[4]
5221 #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]
5222
5223
5224 /**
5225  * Set a the wep key
5226  *
5227  * @priv: struct to work on
5228  * @idx: index of the key we want to set
5229  * @key: ptr to the key data to set
5230  * @len: length of the buffer at @key
5231  * @batch_mode: FIXME perform the operation in batch mode, not
5232  *              disabling the device.
5233  *
5234  * @returns 0 if OK, < 0 errno code on error.
5235  *
5236  * Fill out a command structure with the new wep key, length an
5237  * index and send it down the wire.
5238  */
5239 static int ipw2100_set_key(struct ipw2100_priv *priv,
5240                            int idx, char *key, int len, int batch_mode)
5241 {
5242         int keylen = len ? (len <= 5 ? 5 : 13) : 0;
5243         struct host_command cmd = {
5244                 .host_command = WEP_KEY_INFO,
5245                 .host_command_sequence = 0,
5246                 .host_command_length = sizeof(struct ipw2100_wep_key),
5247         };
5248         struct ipw2100_wep_key *wep_key = (void*)cmd.host_command_parameters;
5249         int err;
5250
5251         IPW_DEBUG_HC("WEP_KEY_INFO: index = %d, len = %d/%d\n",
5252                                  idx, keylen, len);
5253
5254         /* NOTE: We don't check cached values in case the firmware was reset
5255          * or some other problem is occuring.  If the user is setting the key,
5256          * then we push the change */
5257
5258         wep_key->idx = idx;
5259         wep_key->len = keylen;
5260
5261         if (keylen) {
5262                 memcpy(wep_key->key, key, len);
5263                 memset(wep_key->key + len, 0, keylen - len);
5264         }
5265
5266         /* Will be optimized out on debug not being configured in */
5267         if (keylen == 0)
5268                 IPW_DEBUG_WEP("%s: Clearing key %d\n",
5269                                   priv->net_dev->name, wep_key->idx);
5270         else if (keylen == 5)
5271                 IPW_DEBUG_WEP("%s: idx: %d, len: %d key: " WEP_FMT_64 "\n",
5272                                   priv->net_dev->name, wep_key->idx, wep_key->len,
5273                                   WEP_STR_64(wep_key->key));
5274         else
5275                 IPW_DEBUG_WEP("%s: idx: %d, len: %d key: " WEP_FMT_128
5276                                   "\n",
5277                                   priv->net_dev->name, wep_key->idx, wep_key->len,
5278                                   WEP_STR_128(wep_key->key));
5279
5280         if (!batch_mode) {
5281                 err = ipw2100_disable_adapter(priv);
5282                 /* FIXME: IPG: shouldn't this prink be in _disable_adapter()? */
5283                 if (err) {
5284                         IPW_DEBUG_ERROR("%s: Could not disable adapter %d\n",
5285                                priv->net_dev->name, err);
5286                         return err;
5287                 }
5288         }
5289
5290         /* send cmd to firmware */
5291         err = ipw2100_hw_send_command(priv, &cmd);
5292
5293         if (!batch_mode) {
5294                 int err2 = ipw2100_enable_adapter(priv);
5295                 if (err == 0)
5296                         err = err2;
5297         }
5298         return err;
5299 }
5300
5301 static int ipw2100_set_key_index(struct ipw2100_priv *priv,
5302                                  int idx, int batch_mode)
5303 {
5304         struct host_command cmd = {
5305                 .host_command = WEP_KEY_INDEX,
5306                 .host_command_sequence = 0,
5307                 .host_command_length = 4,
5308                 .host_command_parameters[0] = idx,
5309         };
5310         int err;
5311
5312         IPW_DEBUG_HC("WEP_KEY_INDEX: index = %d\n", idx);
5313
5314         if (idx < 0 || idx > 3)
5315                 return -EINVAL;
5316
5317         if (!batch_mode) {
5318                 err = ipw2100_disable_adapter(priv);
5319                 if (err) {
5320                         IPW_DEBUG_ERROR("%s: Could not disable adapter %d\n",
5321                                priv->net_dev->name, err);
5322                         return err;
5323                 }
5324         }
5325
5326         /* send cmd to firmware */
5327         err = ipw2100_hw_send_command(priv, &cmd);
5328
5329         if (!batch_mode)
5330                 ipw2100_enable_adapter(priv);
5331
5332         return err;
5333 }
5334
5335
5336 static int ipw2100_configure_security(struct ipw2100_priv *priv,
5337                                       int batch_mode)
5338 {
5339         int i, err, auth_mode, sec_level, use_group;
5340
5341         if (!(priv->status & STATUS_RUNNING))
5342                 return 0;
5343
5344         if (!batch_mode) {
5345                 err = ipw2100_disable_adapter(priv);
5346                 if (err)
5347                         return err;
5348         }
5349
5350         if (!priv->sec.enabled) {
5351                 err = ipw2100_set_security_information(
5352                         priv, IPW_AUTH_OPEN, SEC_LEVEL_0, 0, 1);
5353         } else {
5354                 auth_mode = IPW_AUTH_OPEN;
5355                 if ((priv->sec.flags & SEC_AUTH_MODE) &&
5356                     (priv->sec.auth_mode == WLAN_AUTH_SHARED_KEY))
5357                         auth_mode = IPW_AUTH_SHARED;
5358
5359                 sec_level = SEC_LEVEL_0;
5360                 if (priv->sec.flags & SEC_LEVEL)
5361                         sec_level = priv->sec.level;
5362
5363                 use_group = 0;
5364                 if (priv->sec.flags & SEC_UNICAST_GROUP)
5365                         use_group = priv->sec.unicast_uses_group;
5366
5367                 err = ipw2100_set_security_information(
5368                             priv, auth_mode, sec_level, use_group, 1);
5369         }
5370
5371         if (err)
5372                 goto exit;
5373
5374         if (priv->sec.enabled) {
5375                 for (i = 0; i < 4; i++) {
5376                         if (!(priv->sec.flags & (1 << i))) {
5377                                 memset(priv->sec.keys[i], 0, WEP_KEY_LEN);
5378                                 priv->sec.key_sizes[i] = 0;
5379                         } else {
5380                                 err = ipw2100_set_key(priv, i,
5381                                                       priv->sec.keys[i],
5382                                                       priv->sec.key_sizes[i],
5383                                                       1);
5384                                 if (err)
5385                                         goto exit;
5386                         }
5387                 }
5388
5389                 ipw2100_set_key_index(priv, priv->ieee->tx_keyidx, 1);
5390         }
5391
5392         /* Always enable privacy so the Host can filter WEP packets if
5393          * encrypted data is sent up */
5394         err = ipw2100_set_wep_flags(
5395                 priv, priv->sec.enabled ? IPW_PRIVACY_CAPABLE : 0, 1);
5396         if (err)
5397                 goto exit;
5398
5399         priv->status &= ~STATUS_SECURITY_UPDATED;
5400
5401  exit:
5402         if (!batch_mode)
5403                 ipw2100_enable_adapter(priv);
5404
5405         return err;
5406 }
5407
5408 static void ipw2100_security_work(struct ipw2100_priv *priv)
5409 {
5410         /* If we happen to have reconnected before we get a chance to
5411          * process this, then update the security settings--which causes
5412          * a disassociation to occur */
5413         if (!(priv->status & STATUS_ASSOCIATED) &&
5414             priv->status & STATUS_SECURITY_UPDATED)
5415                 ipw2100_configure_security(priv, 0);
5416 }
5417
5418 static void shim__set_security(struct net_device *dev,
5419                                struct ieee80211_security *sec)
5420 {
5421         struct ipw2100_priv *priv = ieee80211_priv(dev);
5422         int i, force_update = 0;
5423
5424         down(&priv->action_sem);
5425         if (!(priv->status & STATUS_INITIALIZED))
5426                 goto done;
5427
5428         for (i = 0; i < 4; i++) {
5429                 if (sec->flags & (1 << i)) {
5430                         priv->sec.key_sizes[i] = sec->key_sizes[i];
5431                         if (sec->key_sizes[i] == 0)
5432                                 priv->sec.flags &= ~(1 << i);
5433                         else
5434                                 memcpy(priv->sec.keys[i], sec->keys[i],
5435                                        sec->key_sizes[i]);
5436                         priv->sec.flags |= (1 << i);
5437                         priv->status |= STATUS_SECURITY_UPDATED;
5438                 }
5439         }
5440
5441         if ((sec->flags & SEC_ACTIVE_KEY) &&
5442             priv->sec.active_key != sec->active_key) {
5443                 if (sec->active_key <= 3) {
5444                         priv->sec.active_key = sec->active_key;
5445                         priv->sec.flags |= SEC_ACTIVE_KEY;
5446                 } else
5447                         priv->sec.flags &= ~SEC_ACTIVE_KEY;
5448
5449                 priv->status |= STATUS_SECURITY_UPDATED;
5450         }
5451
5452         if ((sec->flags & SEC_AUTH_MODE) &&
5453             (priv->sec.auth_mode != sec->auth_mode)) {
5454                 priv->sec.auth_mode = sec->auth_mode;
5455                 priv->sec.flags |= SEC_AUTH_MODE;
5456                 priv->status |= STATUS_SECURITY_UPDATED;
5457         }
5458
5459         if (sec->flags & SEC_ENABLED &&
5460             priv->sec.enabled != sec->enabled) {
5461                 priv->sec.flags |= SEC_ENABLED;
5462                 priv->sec.enabled = sec->enabled;
5463                 priv->status |= STATUS_SECURITY_UPDATED;
5464                 force_update = 1;
5465         }
5466
5467         if (sec->flags & SEC_LEVEL &&
5468             priv->sec.level != sec->level) {
5469                 priv->sec.level = sec->level;
5470                 priv->sec.flags |= SEC_LEVEL;
5471                 priv->status |= STATUS_SECURITY_UPDATED;
5472         }
5473
5474         IPW_DEBUG_WEP("Security flags: %c %c%c%c%c %c%c%c%c\n",
5475                           priv->sec.flags & (1<<8) ? '1' : '0',
5476                           priv->sec.flags & (1<<7) ? '1' : '0',
5477                           priv->sec.flags & (1<<6) ? '1' : '0',
5478                           priv->sec.flags & (1<<5) ? '1' : '0',
5479                           priv->sec.flags & (1<<4) ? '1' : '0',
5480                           priv->sec.flags & (1<<3) ? '1' : '0',
5481                           priv->sec.flags & (1<<2) ? '1' : '0',
5482                           priv->sec.flags & (1<<1) ? '1' : '0',
5483                           priv->sec.flags & (1<<0) ? '1' : '0');
5484
5485 /* As a temporary work around to enable WPA until we figure out why
5486  * wpa_supplicant toggles the security capability of the driver, which
5487  * forces a disassocation with force_update...
5488  *
5489  *      if (force_update || !(priv->status & STATUS_ASSOCIATED))*/
5490         if (!(priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)))
5491                 ipw2100_configure_security(priv, 0);
5492 done:
5493         up(&priv->action_sem);
5494 }
5495
5496 static int ipw2100_adapter_setup(struct ipw2100_priv *priv)
5497 {
5498         int err;
5499         int batch_mode = 1;
5500         u8 *bssid;
5501
5502         IPW_DEBUG_INFO("enter\n");
5503
5504         err = ipw2100_disable_adapter(priv);
5505         if (err)
5506                 return err;
5507 #ifdef CONFIG_IPW2100_MONITOR
5508         if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
5509                 err = ipw2100_set_channel(priv, priv->channel, batch_mode);
5510                 if (err)
5511                         return err;
5512
5513                 IPW_DEBUG_INFO("exit\n");
5514
5515                 return 0;
5516         }
5517 #endif /* CONFIG_IPW2100_MONITOR */
5518
5519         err = ipw2100_read_mac_address(priv);
5520         if (err)
5521                 return -EIO;
5522
5523         err = ipw2100_set_mac_address(priv, batch_mode);
5524         if (err)
5525                 return err;
5526
5527         err = ipw2100_set_port_type(priv, priv->ieee->iw_mode, batch_mode);
5528         if (err)
5529                 return err;
5530
5531         if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
5532                 err = ipw2100_set_channel(priv, priv->channel, batch_mode);
5533                 if (err)
5534                         return err;
5535         }
5536
5537         err  = ipw2100_system_config(priv, batch_mode);
5538         if (err)
5539                 return err;
5540
5541         err = ipw2100_set_tx_rates(priv, priv->tx_rates, batch_mode);
5542         if (err)
5543                 return err;
5544
5545         /* Default to power mode OFF */
5546         err = ipw2100_set_power_mode(priv, IPW_POWER_MODE_CAM);
5547         if (err)
5548                 return err;
5549
5550         err = ipw2100_set_rts_threshold(priv, priv->rts_threshold);
5551         if (err)
5552                 return err;
5553
5554         if (priv->config & CFG_STATIC_BSSID)
5555                 bssid = priv->bssid;
5556         else
5557                 bssid = NULL;
5558         err = ipw2100_set_mandatory_bssid(priv, bssid, batch_mode);
5559         if (err)
5560                 return err;
5561
5562         if (priv->config & CFG_STATIC_ESSID)
5563                 err = ipw2100_set_essid(priv, priv->essid, priv->essid_len,
5564                                         batch_mode);
5565         else
5566                 err = ipw2100_set_essid(priv, NULL, 0, batch_mode);
5567         if (err)
5568                 return err;
5569
5570         err = ipw2100_configure_security(priv, batch_mode);
5571         if (err)
5572                 return err;
5573
5574         if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
5575                 err = ipw2100_set_ibss_beacon_interval(
5576                         priv, priv->beacon_interval, batch_mode);
5577                 if (err)
5578                         return err;
5579
5580                 err = ipw2100_set_tx_power(priv, priv->tx_power);
5581                 if (err)
5582                         return err;
5583         }
5584
5585         /*
5586           err = ipw2100_set_fragmentation_threshold(
5587           priv, priv->frag_threshold, batch_mode);
5588           if (err)
5589           return err;
5590         */
5591
5592         IPW_DEBUG_INFO("exit\n");
5593
5594         return 0;
5595 }
5596
5597
5598 /*************************************************************************
5599  *
5600  * EXTERNALLY CALLED METHODS
5601  *
5602  *************************************************************************/
5603
5604 /* This method is called by the network layer -- not to be confused with
5605  * ipw2100_set_mac_address() declared above called by this driver (and this
5606  * method as well) to talk to the firmware */
5607 static int ipw2100_set_address(struct net_device *dev, void *p)
5608 {
5609         struct ipw2100_priv *priv = ieee80211_priv(dev);
5610         struct sockaddr *addr = p;
5611         int err = 0;
5612
5613         if (!is_valid_ether_addr(addr->sa_data))
5614                 return -EADDRNOTAVAIL;
5615
5616         down(&priv->action_sem);
5617
5618         priv->config |= CFG_CUSTOM_MAC;
5619         memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
5620
5621         err = ipw2100_set_mac_address(priv, 0);
5622         if (err)
5623                 goto done;
5624
5625         priv->reset_backoff = 0;
5626         up(&priv->action_sem);
5627         ipw2100_reset_adapter(priv);
5628         return 0;
5629
5630  done:
5631         up(&priv->action_sem);
5632         return err;
5633 }
5634
5635 static int ipw2100_open(struct net_device *dev)
5636 {
5637         struct ipw2100_priv *priv = ieee80211_priv(dev);
5638         unsigned long flags;
5639         IPW_DEBUG_INFO("dev->open\n");
5640
5641         spin_lock_irqsave(&priv->low_lock, flags);
5642         if (priv->status & STATUS_ASSOCIATED)
5643                 netif_start_queue(dev);
5644         spin_unlock_irqrestore(&priv->low_lock, flags);
5645
5646         return 0;
5647 }
5648
5649 static int ipw2100_close(struct net_device *dev)
5650 {
5651         struct ipw2100_priv *priv = ieee80211_priv(dev);
5652         unsigned long flags;
5653         struct list_head *element;
5654         struct ipw2100_tx_packet *packet;
5655
5656         IPW_DEBUG_INFO("enter\n");
5657
5658         spin_lock_irqsave(&priv->low_lock, flags);
5659
5660         if (priv->status & STATUS_ASSOCIATED)
5661                 netif_carrier_off(dev);
5662         netif_stop_queue(dev);
5663
5664         /* Flush the TX queue ... */
5665         while (!list_empty(&priv->tx_pend_list)) {
5666                 element = priv->tx_pend_list.next;
5667                 packet = list_entry(element, struct ipw2100_tx_packet, list);
5668
5669                 list_del(element);
5670                 DEC_STAT(&priv->tx_pend_stat);
5671
5672                 ieee80211_txb_free(packet->info.d_struct.txb);
5673                 packet->info.d_struct.txb = NULL;
5674
5675                 list_add_tail(element, &priv->tx_free_list);
5676                 INC_STAT(&priv->tx_free_stat);
5677         }
5678         spin_unlock_irqrestore(&priv->low_lock, flags);
5679
5680         IPW_DEBUG_INFO("exit\n");
5681
5682         return 0;
5683 }
5684
5685
5686
5687 /*
5688  * TODO:  Fix this function... its just wrong
5689  */
5690 static void ipw2100_tx_timeout(struct net_device *dev)
5691 {
5692         struct ipw2100_priv *priv = ieee80211_priv(dev);
5693
5694         priv->ieee->stats.tx_errors++;
5695
5696 #ifdef CONFIG_IPW2100_MONITOR
5697         if (priv->ieee->iw_mode == IW_MODE_MONITOR)
5698                 return;
5699 #endif
5700
5701         IPW_DEBUG_INFO("%s: TX timed out.  Scheduling firmware restart.\n",
5702                        dev->name);
5703         schedule_reset(priv);
5704 }
5705
5706
5707 /*
5708  * TODO: reimplement it so that it reads statistics
5709  *       from the adapter using ordinal tables
5710  *       instead of/in addition to collecting them
5711  *       in the driver
5712  */
5713 static struct net_device_stats *ipw2100_stats(struct net_device *dev)
5714 {
5715         struct ipw2100_priv *priv = ieee80211_priv(dev);
5716
5717         return &priv->ieee->stats;
5718 }
5719
5720 /* Support for wpa_supplicant. Will be replaced with WEXT once
5721  * they get WPA support. */
5722 #ifdef CONFIG_IEEE80211_WPA
5723
5724 /* following definitions must match definitions in driver_ipw2100.c */
5725
5726 #define IPW2100_IOCTL_WPA_SUPPLICANT            SIOCIWFIRSTPRIV+30
5727
5728 #define IPW2100_CMD_SET_WPA_PARAM               1
5729 #define IPW2100_CMD_SET_WPA_IE                  2
5730 #define IPW2100_CMD_SET_ENCRYPTION              3
5731 #define IPW2100_CMD_MLME                        4
5732
5733 #define IPW2100_PARAM_WPA_ENABLED               1
5734 #define IPW2100_PARAM_TKIP_COUNTERMEASURES      2
5735 #define IPW2100_PARAM_DROP_UNENCRYPTED          3
5736 #define IPW2100_PARAM_PRIVACY_INVOKED           4
5737 #define IPW2100_PARAM_AUTH_ALGS                 5
5738 #define IPW2100_PARAM_IEEE_802_1X               6
5739
5740 #define IPW2100_MLME_STA_DEAUTH                 1
5741 #define IPW2100_MLME_STA_DISASSOC               2
5742
5743 #define IPW2100_CRYPT_ERR_UNKNOWN_ALG           2
5744 #define IPW2100_CRYPT_ERR_UNKNOWN_ADDR          3
5745 #define IPW2100_CRYPT_ERR_CRYPT_INIT_FAILED     4
5746 #define IPW2100_CRYPT_ERR_KEY_SET_FAILED        5
5747 #define IPW2100_CRYPT_ERR_TX_KEY_SET_FAILED     6
5748 #define IPW2100_CRYPT_ERR_CARD_CONF_FAILED      7
5749
5750 #define IPW2100_CRYPT_ALG_NAME_LEN              16
5751
5752 struct ipw2100_param {
5753         u32 cmd;
5754         u8 sta_addr[ETH_ALEN];
5755         union {
5756                 struct {
5757                         u8 name;
5758                         u32 value;
5759                 } wpa_param;
5760                 struct {
5761                         u32 len;
5762                         u8 *data;
5763                 } wpa_ie;
5764                 struct{
5765                         int command;
5766                         int reason_code;
5767                 } mlme;
5768                 struct {
5769                         u8 alg[IPW2100_CRYPT_ALG_NAME_LEN];
5770                         u8 set_tx;
5771                         u32 err;
5772                         u8 idx;
5773                         u8 seq[8]; /* sequence counter (set: RX, get: TX) */
5774                         u16 key_len;
5775                         u8 key[0];
5776                 } crypt;
5777
5778         } u;
5779 };
5780
5781 /* end of driver_ipw2100.c code */
5782
5783 static int ipw2100_wpa_enable(struct ipw2100_priv *priv, int value){
5784
5785         struct ieee80211_device *ieee = priv->ieee;
5786         struct ieee80211_security sec = {
5787                 .flags = SEC_LEVEL | SEC_ENABLED,
5788         };
5789         int ret = 0;
5790
5791         ieee->wpa_enabled = value;
5792
5793         if (value){
5794                 sec.level = SEC_LEVEL_3;
5795                 sec.enabled = 1;
5796         } else {
5797                 sec.level = SEC_LEVEL_0;
5798                 sec.enabled = 0;
5799         }
5800
5801         if (ieee->set_security)
5802                 ieee->set_security(ieee->dev, &sec);
5803         else
5804                 ret = -EOPNOTSUPP;
5805
5806         return ret;
5807 }
5808
5809 #define AUTH_ALG_OPEN_SYSTEM                    0x1
5810 #define AUTH_ALG_SHARED_KEY                     0x2
5811
5812 static int ipw2100_wpa_set_auth_algs(struct ipw2100_priv *priv, int value){
5813
5814         struct ieee80211_device *ieee = priv->ieee;
5815         struct ieee80211_security sec = {
5816                 .flags = SEC_AUTH_MODE,
5817         };
5818         int ret = 0;
5819
5820         if (value & AUTH_ALG_SHARED_KEY){
5821                 sec.auth_mode = WLAN_AUTH_SHARED_KEY;
5822                 ieee->open_wep = 0;
5823         } else {
5824                 sec.auth_mode = WLAN_AUTH_OPEN;
5825                 ieee->open_wep = 1;
5826         }
5827
5828         if (ieee->set_security)
5829                 ieee->set_security(ieee->dev, &sec);
5830         else
5831                 ret = -EOPNOTSUPP;
5832
5833         return ret;
5834 }
5835
5836
5837 static int ipw2100_wpa_set_param(struct net_device *dev, u8 name, u32 value){
5838
5839         struct ipw2100_priv *priv = ieee80211_priv(dev);
5840         int ret=0;
5841
5842         switch(name){
5843                 case IPW2100_PARAM_WPA_ENABLED:
5844                         ret = ipw2100_wpa_enable(priv, value);
5845                         break;
5846
5847                 case IPW2100_PARAM_TKIP_COUNTERMEASURES:
5848                         priv->ieee->tkip_countermeasures=value;
5849                         break;
5850
5851                 case IPW2100_PARAM_DROP_UNENCRYPTED:
5852                         priv->ieee->drop_unencrypted=value;
5853                         break;
5854
5855                 case IPW2100_PARAM_PRIVACY_INVOKED:
5856                         priv->ieee->privacy_invoked=value;
5857                         break;
5858
5859                 case IPW2100_PARAM_AUTH_ALGS:
5860                         ret = ipw2100_wpa_set_auth_algs(priv, value);
5861                         break;
5862
5863                 case IPW2100_PARAM_IEEE_802_1X:
5864                         priv->ieee->ieee802_1x=value;
5865                         break;
5866
5867                 default:
5868                         IPW_DEBUG_ERROR("%s: Unknown WPA param: %d\n",
5869                                             dev->name, name);
5870                         ret = -EOPNOTSUPP;
5871         }
5872
5873         return ret;
5874 }
5875
5876 static int ipw2100_wpa_mlme(struct net_device *dev, int command, int reason){
5877
5878         struct ipw2100_priv *priv = ieee80211_priv(dev);
5879         int ret=0;
5880
5881         switch(command){
5882                 case IPW2100_MLME_STA_DEAUTH:
5883                         // silently ignore
5884                         break;
5885
5886                 case IPW2100_MLME_STA_DISASSOC:
5887                         ipw2100_disassociate_bssid(priv);
5888                         break;
5889
5890                 default:
5891                         IPW_DEBUG_ERROR("%s: Unknown MLME request: %d\n",
5892                                             dev->name, command);
5893                         ret = -EOPNOTSUPP;
5894         }
5895
5896         return ret;
5897 }
5898
5899
5900 void ipw2100_wpa_assoc_frame(struct ipw2100_priv *priv,
5901                              char *wpa_ie, int wpa_ie_len){
5902
5903         struct ipw2100_wpa_assoc_frame frame;
5904
5905         frame.fixed_ie_mask = 0;
5906
5907         /* copy WPA IE */
5908         memcpy(frame.var_ie, wpa_ie, wpa_ie_len);
5909         frame.var_ie_len = wpa_ie_len;
5910
5911         /* make sure WPA is enabled */
5912         ipw2100_wpa_enable(priv, 1);
5913         ipw2100_set_wpa_ie(priv, &frame, 0);
5914 }
5915
5916
5917 static int ipw2100_wpa_set_wpa_ie(struct net_device *dev,
5918                                 struct ipw2100_param *param, int plen){
5919
5920         struct ipw2100_priv *priv = ieee80211_priv(dev);
5921         struct ieee80211_device *ieee = priv->ieee;
5922         u8 *buf;
5923
5924         if (! ieee->wpa_enabled)
5925             return -EOPNOTSUPP;
5926
5927         if (param->u.wpa_ie.len > MAX_WPA_IE_LEN ||
5928            (param->u.wpa_ie.len &&
5929                 param->u.wpa_ie.data==NULL))
5930                 return -EINVAL;
5931
5932         if (param->u.wpa_ie.len){
5933                 buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL);
5934                 if (buf == NULL)
5935                         return -ENOMEM;
5936
5937                 memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len);
5938
5939                 kfree(ieee->wpa_ie);
5940                 ieee->wpa_ie = buf;
5941                 ieee->wpa_ie_len = param->u.wpa_ie.len;
5942
5943         } else {
5944                 kfree(ieee->wpa_ie);
5945                 ieee->wpa_ie = NULL;
5946                 ieee->wpa_ie_len = 0;
5947         }
5948
5949         ipw2100_wpa_assoc_frame(priv, ieee->wpa_ie, ieee->wpa_ie_len);
5950
5951         return 0;
5952 }
5953
5954 /* implementation borrowed from hostap driver */
5955
5956 static int ipw2100_wpa_set_encryption(struct net_device *dev,
5957                                 struct ipw2100_param *param, int param_len){
5958
5959         int ret = 0;
5960         struct ipw2100_priv *priv = ieee80211_priv(dev);
5961         struct ieee80211_device *ieee = priv->ieee;
5962         struct ieee80211_crypto_ops *ops;
5963         struct ieee80211_crypt_data **crypt;
5964
5965         struct ieee80211_security sec = {
5966                 .flags = 0,
5967         };
5968
5969         param->u.crypt.err = 0;
5970         param->u.crypt.alg[IPW2100_CRYPT_ALG_NAME_LEN - 1] = '\0';
5971
5972         if (param_len !=
5973             (int) ((char *) param->u.crypt.key - (char *) param) +
5974             param->u.crypt.key_len){
5975                 IPW_DEBUG_INFO("Len mismatch %d, %d\n", param_len, param->u.crypt.key_len);
5976                 return -EINVAL;
5977         }
5978         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
5979             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
5980             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
5981                 if (param->u.crypt.idx >= WEP_KEYS)
5982                         return -EINVAL;
5983                 crypt = &ieee->crypt[param->u.crypt.idx];
5984         } else {
5985                 return -EINVAL;
5986         }
5987
5988         if (strcmp(param->u.crypt.alg, "none") == 0) {
5989                 if (crypt){
5990                         sec.enabled = 0;
5991                         sec.level = SEC_LEVEL_0;
5992                         sec.flags |= SEC_ENABLED | SEC_LEVEL;
5993                         ieee80211_crypt_delayed_deinit(ieee, crypt);
5994                 }
5995                 goto done;
5996         }
5997         sec.enabled = 1;
5998         sec.flags |= SEC_ENABLED;
5999
6000         ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
6001         if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0) {
6002                 request_module("ieee80211_crypt_wep");
6003                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
6004         } else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0) {
6005                 request_module("ieee80211_crypt_tkip");
6006                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
6007         } else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0) {
6008                 request_module("ieee80211_crypt_ccmp");
6009                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
6010         }
6011         if (ops == NULL) {
6012                 IPW_DEBUG_INFO("%s: unknown crypto alg '%s'\n",
6013                        dev->name, param->u.crypt.alg);
6014                 param->u.crypt.err = IPW2100_CRYPT_ERR_UNKNOWN_ALG;
6015                 ret = -EINVAL;
6016                 goto done;
6017         }
6018
6019         if (*crypt == NULL || (*crypt)->ops != ops) {
6020                 struct ieee80211_crypt_data *new_crypt;
6021
6022                 ieee80211_crypt_delayed_deinit(ieee, crypt);
6023
6024                 new_crypt = (struct ieee80211_crypt_data *)
6025                         kmalloc(sizeof(struct ieee80211_crypt_data), GFP_KERNEL);
6026                 if (new_crypt == NULL) {
6027                         ret = -ENOMEM;
6028                         goto done;
6029                 }
6030                 memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
6031                 new_crypt->ops = ops;
6032                 if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
6033                         new_crypt->priv = new_crypt->ops->init(param->u.crypt.idx);
6034
6035                 if (new_crypt->priv == NULL) {
6036                         kfree(new_crypt);
6037                         param->u.crypt.err =
6038                                 IPW2100_CRYPT_ERR_CRYPT_INIT_FAILED;
6039                         ret = -EINVAL;
6040                         goto done;
6041                 }
6042
6043                 *crypt = new_crypt;
6044         }
6045
6046         if (param->u.crypt.key_len > 0 && (*crypt)->ops->set_key &&
6047             (*crypt)->ops->set_key(param->u.crypt.key,
6048                                    param->u.crypt.key_len, param->u.crypt.seq,
6049                                    (*crypt)->priv) < 0) {
6050                 IPW_DEBUG_INFO("%s: key setting failed\n",
6051                        dev->name);
6052                 param->u.crypt.err = IPW2100_CRYPT_ERR_KEY_SET_FAILED;
6053                 ret = -EINVAL;
6054                 goto done;
6055         }
6056
6057         if (param->u.crypt.set_tx){
6058                 ieee->tx_keyidx = param->u.crypt.idx;
6059                 sec.active_key = param->u.crypt.idx;
6060                 sec.flags |= SEC_ACTIVE_KEY;
6061         }
6062
6063         if (ops->name != NULL){
6064
6065                 if (strcmp(ops->name, "WEP") == 0) {
6066                         memcpy(sec.keys[param->u.crypt.idx], param->u.crypt.key, param->u.crypt.key_len);
6067                         sec.key_sizes[param->u.crypt.idx] = param->u.crypt.key_len;
6068                         sec.flags |= (1 << param->u.crypt.idx);
6069                         sec.flags |= SEC_LEVEL;
6070                         sec.level = SEC_LEVEL_1;
6071                 } else if (strcmp(ops->name, "TKIP") == 0) {
6072                         sec.flags |= SEC_LEVEL;
6073                         sec.level = SEC_LEVEL_2;
6074                 } else if (strcmp(ops->name, "CCMP") == 0) {
6075                         sec.flags |= SEC_LEVEL;
6076                         sec.level = SEC_LEVEL_3;
6077                 }
6078         }
6079  done:
6080         if (ieee->set_security)
6081                 ieee->set_security(ieee->dev, &sec);
6082
6083         /* Do not reset port if card is in Managed mode since resetting will
6084          * generate new IEEE 802.11 authentication which may end up in looping
6085          * with IEEE 802.1X.  If your hardware requires a reset after WEP
6086          * configuration (for example... Prism2), implement the reset_port in
6087          * the callbacks structures used to initialize the 802.11 stack. */
6088         if (ieee->reset_on_keychange &&
6089             ieee->iw_mode != IW_MODE_INFRA &&
6090             ieee->reset_port &&
6091             ieee->reset_port(dev)) {
6092                 IPW_DEBUG_INFO("%s: reset_port failed\n", dev->name);
6093                 param->u.crypt.err = IPW2100_CRYPT_ERR_CARD_CONF_FAILED;
6094                 return -EINVAL;
6095         }
6096
6097         return ret;
6098 }
6099
6100
6101 static int ipw2100_wpa_supplicant(struct net_device *dev, struct iw_point *p){
6102
6103         struct ipw2100_param *param;
6104         int ret=0;
6105
6106         IPW_DEBUG_IOCTL("wpa_supplicant: len=%d\n", p->length);
6107
6108         if (p->length < sizeof(struct ipw2100_param) || !p->pointer)
6109                 return -EINVAL;
6110
6111         param = (struct ipw2100_param *)kmalloc(p->length, GFP_KERNEL);
6112         if (param == NULL)
6113                 return -ENOMEM;
6114
6115         if (copy_from_user(param, p->pointer, p->length)){
6116                 kfree(param);
6117                 return -EFAULT;
6118         }
6119
6120         switch (param->cmd){
6121
6122         case IPW2100_CMD_SET_WPA_PARAM:
6123                 ret = ipw2100_wpa_set_param(dev, param->u.wpa_param.name,
6124                                             param->u.wpa_param.value);
6125                 break;
6126
6127         case IPW2100_CMD_SET_WPA_IE:
6128                 ret = ipw2100_wpa_set_wpa_ie(dev, param, p->length);
6129                 break;
6130
6131         case IPW2100_CMD_SET_ENCRYPTION:
6132                 ret = ipw2100_wpa_set_encryption(dev, param, p->length);
6133                 break;
6134
6135         case IPW2100_CMD_MLME:
6136                 ret = ipw2100_wpa_mlme(dev, param->u.mlme.command,
6137                                        param->u.mlme.reason_code);
6138                 break;
6139
6140         default:
6141                 IPW_DEBUG_ERROR("%s: Unknown WPA supplicant request: %d\n",
6142                                 dev->name, param->cmd);
6143                 ret = -EOPNOTSUPP;
6144
6145         }
6146
6147         if (ret == 0 && copy_to_user(p->pointer, param, p->length))
6148                 ret = -EFAULT;
6149
6150         kfree(param);
6151         return ret;
6152 }
6153 #endif /* CONFIG_IEEE80211_WPA */
6154
6155 static int ipw2100_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
6156 {
6157 #ifdef CONFIG_IEEE80211_WPA
6158         struct iwreq *wrq = (struct iwreq *) rq;
6159         int ret=-1;
6160         switch (cmd){
6161             case IPW2100_IOCTL_WPA_SUPPLICANT:
6162                 ret = ipw2100_wpa_supplicant(dev, &wrq->u.data);
6163                 return ret;
6164
6165             default:
6166                 return -EOPNOTSUPP;
6167         }
6168
6169 #endif /* CONFIG_IEEE80211_WPA */
6170
6171         return -EOPNOTSUPP;
6172 }
6173
6174
6175 static void ipw_ethtool_get_drvinfo(struct net_device *dev,
6176                                     struct ethtool_drvinfo *info)
6177 {
6178         struct ipw2100_priv *priv = ieee80211_priv(dev);
6179         char fw_ver[64], ucode_ver[64];
6180
6181         strcpy(info->driver, DRV_NAME);
6182         strcpy(info->version, DRV_VERSION);
6183
6184         ipw2100_get_fwversion(priv, fw_ver, sizeof(fw_ver));
6185         ipw2100_get_ucodeversion(priv, ucode_ver, sizeof(ucode_ver));
6186
6187         snprintf(info->fw_version, sizeof(info->fw_version), "%s:%d:%s",
6188                  fw_ver, priv->eeprom_version, ucode_ver);
6189
6190         strcpy(info->bus_info, pci_name(priv->pci_dev));
6191 }
6192
6193 static u32 ipw2100_ethtool_get_link(struct net_device *dev)
6194 {
6195     struct ipw2100_priv *priv = ieee80211_priv(dev);
6196     return (priv->status & STATUS_ASSOCIATED) ? 1 : 0;
6197 }
6198
6199
6200 static struct ethtool_ops ipw2100_ethtool_ops = {
6201     .get_link        = ipw2100_ethtool_get_link,
6202     .get_drvinfo     = ipw_ethtool_get_drvinfo,
6203 };
6204
6205 static void ipw2100_hang_check(void *adapter)
6206 {
6207         struct ipw2100_priv *priv = adapter;
6208         unsigned long flags;
6209         u32 rtc = 0xa5a5a5a5;
6210         u32 len = sizeof(rtc);
6211         int restart = 0;
6212
6213         spin_lock_irqsave(&priv->low_lock, flags);
6214
6215         if (priv->fatal_error != 0) {
6216                 /* If fatal_error is set then we need to restart */
6217                 IPW_DEBUG_INFO("%s: Hardware fatal error detected.\n",
6218                                priv->net_dev->name);
6219
6220                 restart = 1;
6221         } else if (ipw2100_get_ordinal(priv, IPW_ORD_RTC_TIME, &rtc, &len) ||
6222                    (rtc == priv->last_rtc)) {
6223                 /* Check if firmware is hung */
6224                 IPW_DEBUG_INFO("%s: Firmware RTC stalled.\n",
6225                                priv->net_dev->name);
6226
6227                 restart = 1;
6228         }
6229
6230         if (restart) {
6231                 /* Kill timer */
6232                 priv->stop_hang_check = 1;
6233                 priv->hangs++;
6234
6235                 /* Restart the NIC */
6236                 schedule_reset(priv);
6237         }
6238
6239         priv->last_rtc = rtc;
6240
6241         if (!priv->stop_hang_check)
6242                 queue_delayed_work(priv->workqueue, &priv->hang_check, HZ / 2);
6243
6244         spin_unlock_irqrestore(&priv->low_lock, flags);
6245 }
6246
6247
6248 static void ipw2100_rf_kill(void *adapter)
6249 {
6250         struct ipw2100_priv *priv = adapter;
6251         unsigned long flags;
6252
6253         spin_lock_irqsave(&priv->low_lock, flags);
6254
6255         if (rf_kill_active(priv)) {
6256                 IPW_DEBUG_RF_KILL("RF Kill active, rescheduling GPIO check\n");
6257                 if (!priv->stop_rf_kill)
6258                         queue_delayed_work(priv->workqueue, &priv->rf_kill, HZ);
6259                 goto exit_unlock;
6260         }
6261
6262         /* RF Kill is now disabled, so bring the device back up */
6263
6264         if (!(priv->status & STATUS_RF_KILL_MASK)) {
6265                 IPW_DEBUG_RF_KILL("HW RF Kill no longer active, restarting "
6266                                   "device\n");
6267                 schedule_reset(priv);
6268         } else
6269                 IPW_DEBUG_RF_KILL("HW RF Kill deactivated.  SW RF Kill still "
6270                                   "enabled\n");
6271
6272  exit_unlock:
6273         spin_unlock_irqrestore(&priv->low_lock, flags);
6274 }
6275
6276 static void ipw2100_irq_tasklet(struct ipw2100_priv *priv);
6277
6278 /* Look into using netdev destructor to shutdown ieee80211? */
6279
6280 static struct net_device *ipw2100_alloc_device(
6281         struct pci_dev *pci_dev,
6282         char *base_addr,
6283         unsigned long mem_start,
6284         unsigned long mem_len)
6285 {
6286         struct ipw2100_priv *priv;
6287         struct net_device *dev;
6288
6289         dev = alloc_ieee80211(sizeof(struct ipw2100_priv));
6290         if (!dev)
6291                 return NULL;
6292         priv = ieee80211_priv(dev);
6293         priv->ieee = netdev_priv(dev);
6294         priv->pci_dev = pci_dev;
6295         priv->net_dev = dev;
6296
6297         priv->ieee->hard_start_xmit = ipw2100_tx;
6298         priv->ieee->set_security = shim__set_security;
6299
6300         dev->open = ipw2100_open;
6301         dev->stop = ipw2100_close;
6302         dev->init = ipw2100_net_init;
6303         dev->do_ioctl = ipw2100_ioctl;
6304         dev->get_stats = ipw2100_stats;
6305         dev->ethtool_ops = &ipw2100_ethtool_ops;
6306         dev->tx_timeout = ipw2100_tx_timeout;
6307         dev->wireless_handlers = &ipw2100_wx_handler_def;
6308         dev->get_wireless_stats = ipw2100_wx_wireless_stats;
6309         dev->set_mac_address = ipw2100_set_address;
6310         dev->watchdog_timeo = 3*HZ;
6311         dev->irq = 0;
6312
6313         dev->base_addr = (unsigned long)base_addr;
6314         dev->mem_start = mem_start;
6315         dev->mem_end = dev->mem_start + mem_len - 1;
6316
6317         /* NOTE: We don't use the wireless_handlers hook
6318          * in dev as the system will start throwing WX requests
6319          * to us before we're actually initialized and it just
6320          * ends up causing problems.  So, we just handle
6321          * the WX extensions through the ipw2100_ioctl interface */
6322
6323
6324         /* memset() puts everything to 0, so we only have explicitely set
6325          * those values that need to be something else */
6326
6327         /* If power management is turned on, default to AUTO mode */
6328         priv->power_mode = IPW_POWER_AUTO;
6329
6330
6331
6332 #ifdef CONFIG_IEEE80211_WPA
6333         priv->ieee->wpa_enabled = 0;
6334         priv->ieee->tkip_countermeasures = 0;
6335         priv->ieee->drop_unencrypted = 0;
6336         priv->ieee->privacy_invoked = 0;
6337         priv->ieee->ieee802_1x = 1;
6338 #endif /* CONFIG_IEEE80211_WPA */
6339
6340         /* Set module parameters */
6341         switch (mode) {
6342         case 1:
6343                 priv->ieee->iw_mode = IW_MODE_ADHOC;
6344                 break;
6345 #ifdef CONFIG_IPW2100_MONITOR
6346         case 2:
6347                 priv->ieee->iw_mode = IW_MODE_MONITOR;
6348                 break;
6349 #endif
6350         default:
6351         case 0:
6352                 priv->ieee->iw_mode = IW_MODE_INFRA;
6353                 break;
6354         }
6355
6356         if (disable == 1)
6357                 priv->status |= STATUS_RF_KILL_SW;
6358
6359         if (channel != 0 &&
6360             ((channel >= REG_MIN_CHANNEL) &&
6361              (channel <= REG_MAX_CHANNEL))) {
6362                 priv->config |= CFG_STATIC_CHANNEL;
6363                 priv->channel = channel;
6364         }
6365
6366         if (associate)
6367                 priv->config |= CFG_ASSOCIATE;
6368
6369         priv->beacon_interval = DEFAULT_BEACON_INTERVAL;
6370         priv->short_retry_limit = DEFAULT_SHORT_RETRY_LIMIT;
6371         priv->long_retry_limit = DEFAULT_LONG_RETRY_LIMIT;
6372         priv->rts_threshold = DEFAULT_RTS_THRESHOLD | RTS_DISABLED;
6373         priv->frag_threshold = DEFAULT_FTS | FRAG_DISABLED;
6374         priv->tx_power = IPW_TX_POWER_DEFAULT;
6375         priv->tx_rates = DEFAULT_TX_RATES;
6376
6377         strcpy(priv->nick, "ipw2100");
6378
6379         spin_lock_init(&priv->low_lock);
6380         sema_init(&priv->action_sem, 1);
6381         sema_init(&priv->adapter_sem, 1);
6382
6383         init_waitqueue_head(&priv->wait_command_queue);
6384
6385         netif_carrier_off(dev);
6386
6387         INIT_LIST_HEAD(&priv->msg_free_list);
6388         INIT_LIST_HEAD(&priv->msg_pend_list);
6389         INIT_STAT(&priv->msg_free_stat);
6390         INIT_STAT(&priv->msg_pend_stat);
6391
6392         INIT_LIST_HEAD(&priv->tx_free_list);
6393         INIT_LIST_HEAD(&priv->tx_pend_list);
6394         INIT_STAT(&priv->tx_free_stat);
6395         INIT_STAT(&priv->tx_pend_stat);
6396
6397         INIT_LIST_HEAD(&priv->fw_pend_list);
6398         INIT_STAT(&priv->fw_pend_stat);
6399
6400
6401 #ifdef CONFIG_SOFTWARE_SUSPEND2
6402         priv->workqueue = create_workqueue(DRV_NAME, 0);
6403 #else
6404         priv->workqueue = create_workqueue(DRV_NAME);
6405 #endif
6406         INIT_WORK(&priv->reset_work,
6407                   (void (*)(void *))ipw2100_reset_adapter, priv);
6408         INIT_WORK(&priv->security_work,
6409                   (void (*)(void *))ipw2100_security_work, priv);
6410         INIT_WORK(&priv->wx_event_work,
6411                   (void (*)(void *))ipw2100_wx_event_work, priv);
6412         INIT_WORK(&priv->hang_check, ipw2100_hang_check, priv);
6413         INIT_WORK(&priv->rf_kill, ipw2100_rf_kill, priv);
6414
6415         tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
6416                      ipw2100_irq_tasklet, (unsigned long)priv);
6417
6418         /* NOTE:  We do not start the deferred work for status checks yet */
6419         priv->stop_rf_kill = 1;
6420         priv->stop_hang_check = 1;
6421
6422         return dev;
6423 }
6424
6425 static int ipw2100_pci_init_one(struct pci_dev *pci_dev,
6426                                 const struct pci_device_id *ent)
6427 {
6428         unsigned long mem_start, mem_len, mem_flags;
6429         char *base_addr = NULL;
6430         struct net_device *dev = NULL;
6431         struct ipw2100_priv *priv = NULL;
6432         int err = 0;
6433         int registered = 0;
6434         u32 val;
6435
6436         IPW_DEBUG_INFO("enter\n");
6437
6438         mem_start = pci_resource_start(pci_dev, 0);
6439         mem_len = pci_resource_len(pci_dev, 0);
6440         mem_flags = pci_resource_flags(pci_dev, 0);
6441
6442         if ((mem_flags & IORESOURCE_MEM) != IORESOURCE_MEM) {
6443                 IPW_DEBUG_INFO("weird - resource type is not memory\n");
6444                 err = -ENODEV;
6445                 goto fail;
6446         }
6447
6448         base_addr = ioremap_nocache(mem_start, mem_len);
6449         if (!base_addr) {
6450                 printk(KERN_WARNING DRV_NAME
6451                        "Error calling ioremap_nocache.\n");
6452                 err = -EIO;
6453                 goto fail;
6454         }
6455
6456         /* allocate and initialize our net_device */
6457         dev = ipw2100_alloc_device(pci_dev, base_addr, mem_start, mem_len);
6458         if (!dev) {
6459                 printk(KERN_WARNING DRV_NAME
6460                        "Error calling ipw2100_alloc_device.\n");
6461                 err = -ENOMEM;
6462                 goto fail;
6463         }
6464
6465         /* set up PCI mappings for device */
6466         err = pci_enable_device(pci_dev);
6467         if (err) {
6468                 printk(KERN_WARNING DRV_NAME
6469                        "Error calling pci_enable_device.\n");
6470                 return err;
6471         }
6472
6473         priv = ieee80211_priv(dev);
6474
6475         pci_set_master(pci_dev);
6476         pci_set_drvdata(pci_dev, priv);
6477
6478         err = pci_set_dma_mask(pci_dev, DMA_32BIT_MASK);
6479         if (err) {
6480                 printk(KERN_WARNING DRV_NAME
6481                        "Error calling pci_set_dma_mask.\n");
6482                 pci_disable_device(pci_dev);
6483                 return err;
6484         }
6485
6486         err = pci_request_regions(pci_dev, DRV_NAME);
6487         if (err) {
6488                 printk(KERN_WARNING DRV_NAME
6489                        "Error calling pci_request_regions.\n");
6490                 pci_disable_device(pci_dev);
6491                 return err;
6492         }
6493
6494         /* We disable the RETRY_TIMEOUT register (0x41) to keep
6495          * PCI Tx retries from interfering with C3 CPU state */
6496         pci_read_config_dword(pci_dev, 0x40, &val);
6497         if ((val & 0x0000ff00) != 0)
6498                 pci_write_config_dword(pci_dev, 0x40, val & 0xffff00ff);
6499
6500         pci_set_power_state(pci_dev, PCI_D0);
6501
6502         if (!ipw2100_hw_is_adapter_in_system(dev)) {
6503                 printk(KERN_WARNING DRV_NAME
6504                        "Device not found via register read.\n");
6505                 err = -ENODEV;
6506                 goto fail;
6507         }
6508
6509         SET_NETDEV_DEV(dev, &pci_dev->dev);
6510
6511         /* Force interrupts to be shut off on the device */
6512         priv->status |= STATUS_INT_ENABLED;
6513         ipw2100_disable_interrupts(priv);
6514
6515         /* Allocate and initialize the Tx/Rx queues and lists */
6516         if (ipw2100_queues_allocate(priv)) {
6517                 printk(KERN_WARNING DRV_NAME
6518                        "Error calilng ipw2100_queues_allocate.\n");
6519                 err = -ENOMEM;
6520                 goto fail;
6521         }
6522         ipw2100_queues_initialize(priv);
6523
6524         err = request_irq(pci_dev->irq,
6525                           ipw2100_interrupt, SA_SHIRQ,
6526                           dev->name, priv);
6527         if (err) {
6528                 printk(KERN_WARNING DRV_NAME
6529                        "Error calling request_irq: %d.\n",
6530                        pci_dev->irq);
6531                 goto fail;
6532         }
6533         dev->irq = pci_dev->irq;
6534
6535         IPW_DEBUG_INFO("Attempting to register device...\n");
6536
6537         SET_MODULE_OWNER(dev);
6538
6539         printk(KERN_INFO DRV_NAME
6540                ": Detected Intel PRO/Wireless 2100 Network Connection\n");
6541
6542         /* Bring up the interface.  Pre 0.46, after we registered the
6543          * network device we would call ipw2100_up.  This introduced a race
6544          * condition with newer hotplug configurations (network was coming
6545          * up and making calls before the device was initialized).
6546          *
6547          * If we called ipw2100_up before we registered the device, then the
6548          * device name wasn't registered.  So, we instead use the net_dev->init
6549          * member to call a function that then just turns and calls ipw2100_up.
6550          * net_dev->init is called after name allocation but before the
6551          * notifier chain is called */
6552         down(&priv->action_sem);
6553         err = register_netdev(dev);
6554         if (err) {
6555                 printk(KERN_WARNING DRV_NAME
6556                        "Error calling register_netdev.\n");
6557                 goto fail_unlock;
6558         }
6559         registered = 1;
6560
6561         IPW_DEBUG_INFO("%s: Bound to %s\n", dev->name, pci_name(pci_dev));
6562
6563         /* perform this after register_netdev so that dev->name is set */
6564         sysfs_create_group(&pci_dev->dev.kobj, &ipw2100_attribute_group);
6565         netif_carrier_off(dev);
6566
6567         /* If the RF Kill switch is disabled, go ahead and complete the
6568          * startup sequence */
6569         if (!(priv->status & STATUS_RF_KILL_MASK)) {
6570                 /* Enable the adapter - sends HOST_COMPLETE */
6571                 if (ipw2100_enable_adapter(priv)) {
6572                         printk(KERN_WARNING DRV_NAME
6573                                ": %s: failed in call to enable adapter.\n",
6574                                priv->net_dev->name);
6575                         ipw2100_hw_stop_adapter(priv);
6576                         err = -EIO;
6577                         goto fail_unlock;
6578                 }
6579
6580                 /* Start a scan . . . */
6581                 ipw2100_set_scan_options(priv);
6582                 ipw2100_start_scan(priv);
6583         }
6584
6585         IPW_DEBUG_INFO("exit\n");
6586
6587         priv->status |= STATUS_INITIALIZED;
6588
6589         up(&priv->action_sem);
6590
6591         return 0;
6592
6593  fail_unlock:
6594         up(&priv->action_sem);
6595
6596  fail:
6597         if (dev) {
6598                 if (registered)
6599                         unregister_netdev(dev);
6600
6601                 ipw2100_hw_stop_adapter(priv);
6602
6603                 ipw2100_disable_interrupts(priv);
6604
6605                 if (dev->irq)
6606                         free_irq(dev->irq, priv);
6607
6608                 ipw2100_kill_workqueue(priv);
6609
6610                 /* These are safe to call even if they weren't allocated */
6611                 ipw2100_queues_free(priv);
6612                 sysfs_remove_group(&pci_dev->dev.kobj, &ipw2100_attribute_group);
6613
6614                 free_ieee80211(dev);
6615                 pci_set_drvdata(pci_dev, NULL);
6616         }
6617
6618         if (base_addr)
6619                 iounmap((char*)base_addr);
6620
6621         pci_release_regions(pci_dev);
6622         pci_disable_device(pci_dev);
6623
6624         return err;
6625 }
6626
6627 static void __devexit ipw2100_pci_remove_one(struct pci_dev *pci_dev)
6628 {
6629         struct ipw2100_priv *priv = pci_get_drvdata(pci_dev);
6630         struct net_device *dev;
6631
6632         if (priv) {
6633                 down(&priv->action_sem);
6634
6635                 priv->status &= ~STATUS_INITIALIZED;
6636
6637                 dev = priv->net_dev;
6638                 sysfs_remove_group(&pci_dev->dev.kobj, &ipw2100_attribute_group);
6639
6640 #ifdef CONFIG_PM
6641                 if (ipw2100_firmware.version)
6642                         ipw2100_release_firmware(priv, &ipw2100_firmware);
6643 #endif
6644                 /* Take down the hardware */
6645                 ipw2100_down(priv);
6646
6647                 /* Release the semaphore so that the network subsystem can
6648                  * complete any needed calls into the driver... */
6649                 up(&priv->action_sem);
6650
6651                 /* Unregister the device first - this results in close()
6652                  * being called if the device is open.  If we free storage
6653                  * first, then close() will crash. */
6654                 unregister_netdev(dev);
6655
6656                 /* ipw2100_down will ensure that there is no more pending work
6657                  * in the workqueue's, so we can safely remove them now. */
6658                 ipw2100_kill_workqueue(priv);
6659
6660                 ipw2100_queues_free(priv);
6661
6662                 /* Free potential debugging firmware snapshot */
6663                 ipw2100_snapshot_free(priv);
6664
6665                 if (dev->irq)
6666                         free_irq(dev->irq, priv);
6667
6668                 if (dev->base_addr)
6669                         iounmap((unsigned char *)dev->base_addr);
6670
6671                 free_ieee80211(dev);
6672         }
6673
6674         pci_release_regions(pci_dev);
6675         pci_disable_device(pci_dev);
6676
6677         IPW_DEBUG_INFO("exit\n");
6678 }
6679
6680
6681 #ifdef CONFIG_PM
6682 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
6683 static int ipw2100_suspend(struct pci_dev *pci_dev, u32 state)
6684 #else
6685 static int ipw2100_suspend(struct pci_dev *pci_dev, pm_message_t state)
6686 #endif
6687 {
6688         struct ipw2100_priv *priv = pci_get_drvdata(pci_dev);
6689         struct net_device *dev = priv->net_dev;
6690
6691         IPW_DEBUG_INFO("%s: Going into suspend...\n",
6692                dev->name);
6693
6694         down(&priv->action_sem);
6695         if (priv->status & STATUS_INITIALIZED) {
6696                 /* Take down the device; powers it off, etc. */
6697                 ipw2100_down(priv);
6698         }
6699
6700         /* Remove the PRESENT state of the device */
6701         netif_device_detach(dev);
6702
6703         pci_save_state(pci_dev);
6704         pci_disable_device (pci_dev);
6705         pci_set_power_state(pci_dev, PCI_D3hot);
6706
6707         up(&priv->action_sem);
6708
6709         return 0;
6710 }
6711
6712 static int ipw2100_resume(struct pci_dev *pci_dev)
6713 {
6714         struct ipw2100_priv *priv = pci_get_drvdata(pci_dev);
6715         struct net_device *dev = priv->net_dev;
6716         u32 val;
6717
6718         if (IPW2100_PM_DISABLED)
6719                 return 0;
6720
6721         down(&priv->action_sem);
6722
6723         IPW_DEBUG_INFO("%s: Coming out of suspend...\n",
6724                dev->name);
6725
6726         pci_set_power_state(pci_dev, PCI_D0);
6727         pci_enable_device(pci_dev);
6728         pci_restore_state(pci_dev);
6729
6730         /*
6731          * Suspend/Resume resets the PCI configuration space, so we have to
6732          * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
6733          * from interfering with C3 CPU state. pci_restore_state won't help
6734          * here since it only restores the first 64 bytes pci config header.
6735          */
6736         pci_read_config_dword(pci_dev, 0x40, &val);
6737         if ((val & 0x0000ff00) != 0)
6738                 pci_write_config_dword(pci_dev, 0x40, val & 0xffff00ff);
6739
6740         /* Set the device back into the PRESENT state; this will also wake
6741          * the queue of needed */
6742         netif_device_attach(dev);
6743
6744         /* Bring the device back up */
6745         if (!(priv->status & STATUS_RF_KILL_SW))
6746                 ipw2100_up(priv, 0);
6747
6748         up(&priv->action_sem);
6749
6750         return 0;
6751 }
6752 #endif
6753
6754
6755 #define IPW2100_DEV_ID(x) { PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, x }
6756
6757 static struct pci_device_id ipw2100_pci_id_table[] __devinitdata = {
6758         IPW2100_DEV_ID(0x2520), /* IN 2100A mPCI 3A */
6759         IPW2100_DEV_ID(0x2521), /* IN 2100A mPCI 3B */
6760         IPW2100_DEV_ID(0x2524), /* IN 2100A mPCI 3B */
6761         IPW2100_DEV_ID(0x2525), /* IN 2100A mPCI 3B */
6762         IPW2100_DEV_ID(0x2526), /* IN 2100A mPCI Gen A3 */
6763         IPW2100_DEV_ID(0x2522), /* IN 2100 mPCI 3B */
6764         IPW2100_DEV_ID(0x2523), /* IN 2100 mPCI 3A */
6765         IPW2100_DEV_ID(0x2527), /* IN 2100 mPCI 3B */
6766         IPW2100_DEV_ID(0x2528), /* IN 2100 mPCI 3B */
6767         IPW2100_DEV_ID(0x2529), /* IN 2100 mPCI 3B */
6768         IPW2100_DEV_ID(0x252B), /* IN 2100 mPCI 3A */
6769         IPW2100_DEV_ID(0x252C), /* IN 2100 mPCI 3A */
6770         IPW2100_DEV_ID(0x252D), /* IN 2100 mPCI 3A */
6771
6772         IPW2100_DEV_ID(0x2550), /* IB 2100A mPCI 3B */
6773         IPW2100_DEV_ID(0x2551), /* IB 2100 mPCI 3B */
6774         IPW2100_DEV_ID(0x2553), /* IB 2100 mPCI 3B */
6775         IPW2100_DEV_ID(0x2554), /* IB 2100 mPCI 3B */
6776         IPW2100_DEV_ID(0x2555), /* IB 2100 mPCI 3B */
6777
6778         IPW2100_DEV_ID(0x2560), /* DE 2100A mPCI 3A */
6779         IPW2100_DEV_ID(0x2562), /* DE 2100A mPCI 3A */
6780         IPW2100_DEV_ID(0x2563), /* DE 2100A mPCI 3A */
6781         IPW2100_DEV_ID(0x2561), /* DE 2100 mPCI 3A */
6782         IPW2100_DEV_ID(0x2565), /* DE 2100 mPCI 3A */
6783         IPW2100_DEV_ID(0x2566), /* DE 2100 mPCI 3A */
6784         IPW2100_DEV_ID(0x2567), /* DE 2100 mPCI 3A */
6785
6786         IPW2100_DEV_ID(0x2570), /* GA 2100 mPCI 3B */
6787
6788         IPW2100_DEV_ID(0x2580), /* TO 2100A mPCI 3B */
6789         IPW2100_DEV_ID(0x2582), /* TO 2100A mPCI 3B */
6790         IPW2100_DEV_ID(0x2583), /* TO 2100A mPCI 3B */
6791         IPW2100_DEV_ID(0x2581), /* TO 2100 mPCI 3B */
6792         IPW2100_DEV_ID(0x2585), /* TO 2100 mPCI 3B */
6793         IPW2100_DEV_ID(0x2586), /* TO 2100 mPCI 3B */
6794         IPW2100_DEV_ID(0x2587), /* TO 2100 mPCI 3B */
6795
6796         IPW2100_DEV_ID(0x2590), /* SO 2100A mPCI 3B */
6797         IPW2100_DEV_ID(0x2592), /* SO 2100A mPCI 3B */
6798         IPW2100_DEV_ID(0x2591), /* SO 2100 mPCI 3B */
6799         IPW2100_DEV_ID(0x2593), /* SO 2100 mPCI 3B */
6800         IPW2100_DEV_ID(0x2596), /* SO 2100 mPCI 3B */
6801         IPW2100_DEV_ID(0x2598), /* SO 2100 mPCI 3B */
6802
6803         IPW2100_DEV_ID(0x25A0), /* HP 2100 mPCI 3B */
6804         {0,},
6805 };
6806
6807 MODULE_DEVICE_TABLE(pci, ipw2100_pci_id_table);
6808
6809 static struct pci_driver ipw2100_pci_driver = {
6810         .name = DRV_NAME,
6811         .id_table = ipw2100_pci_id_table,
6812         .probe = ipw2100_pci_init_one,
6813         .remove = __devexit_p(ipw2100_pci_remove_one),
6814 #ifdef CONFIG_PM
6815         .suspend = ipw2100_suspend,
6816         .resume = ipw2100_resume,
6817 #endif
6818 };
6819
6820
6821 /**
6822  * Initialize the ipw2100 driver/module
6823  *
6824  * @returns 0 if ok, < 0 errno node con error.
6825  *
6826  * Note: we cannot init the /proc stuff until the PCI driver is there,
6827  * or we risk an unlikely race condition on someone accessing
6828  * uninitialized data in the PCI dev struct through /proc.
6829  */
6830 static int __init ipw2100_init(void)
6831 {
6832         int ret;
6833
6834         printk(KERN_INFO DRV_NAME ": %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
6835         printk(KERN_INFO DRV_NAME ": %s\n", DRV_COPYRIGHT);
6836
6837 #ifdef CONFIG_IEEE80211_NOWEP
6838         IPW_DEBUG_INFO(DRV_NAME ": Compiled with WEP disabled.\n");
6839 #endif
6840
6841         ret = pci_module_init(&ipw2100_pci_driver);
6842
6843 #ifdef CONFIG_IPW_DEBUG
6844         ipw2100_debug_level = debug;
6845         driver_create_file(&ipw2100_pci_driver.driver,
6846                            &driver_attr_debug_level);
6847 #endif
6848
6849         return ret;
6850 }
6851
6852
6853 /**
6854  * Cleanup ipw2100 driver registration
6855  */
6856 static void __exit ipw2100_exit(void)
6857 {
6858         /* FIXME: IPG: check that we have no instances of the devices open */
6859 #ifdef CONFIG_IPW_DEBUG
6860         driver_remove_file(&ipw2100_pci_driver.driver,
6861                            &driver_attr_debug_level);
6862 #endif
6863         pci_unregister_driver(&ipw2100_pci_driver);
6864 }
6865
6866 module_init(ipw2100_init);
6867 module_exit(ipw2100_exit);
6868
6869 #define WEXT_USECHANNELS 1
6870
6871 const long ipw2100_frequencies[] = {
6872         2412, 2417, 2422, 2427,
6873         2432, 2437, 2442, 2447,
6874         2452, 2457, 2462, 2467,
6875         2472, 2484
6876 };
6877
6878 #define FREQ_COUNT (sizeof(ipw2100_frequencies) / \
6879                     sizeof(ipw2100_frequencies[0]))
6880
6881 const long ipw2100_rates_11b[] = {
6882         1000000,
6883         2000000,
6884         5500000,
6885         11000000
6886 };
6887
6888 #define RATE_COUNT (sizeof(ipw2100_rates_11b) / sizeof(ipw2100_rates_11b[0]))
6889
6890 static int ipw2100_wx_get_name(struct net_device *dev,
6891                                struct iw_request_info *info,
6892                                union iwreq_data *wrqu, char *extra)
6893 {
6894         /*
6895          * This can be called at any time.  No action lock required
6896          */
6897
6898         struct ipw2100_priv *priv = ieee80211_priv(dev);
6899         if (!(priv->status & STATUS_ASSOCIATED))
6900                 strcpy(wrqu->name, "unassociated");
6901         else
6902                 snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11b");
6903
6904         IPW_DEBUG_WX("Name: %s\n", wrqu->name);
6905         return 0;
6906 }
6907
6908
6909 static int ipw2100_wx_set_freq(struct net_device *dev,
6910                                struct iw_request_info *info,
6911                                union iwreq_data *wrqu, char *extra)
6912 {
6913         struct ipw2100_priv *priv = ieee80211_priv(dev);
6914         struct iw_freq *fwrq = &wrqu->freq;
6915         int err = 0;
6916
6917         if (priv->ieee->iw_mode == IW_MODE_INFRA)
6918                 return -EOPNOTSUPP;
6919
6920         down(&priv->action_sem);
6921         if (!(priv->status & STATUS_INITIALIZED)) {
6922                 err = -EIO;
6923                 goto done;
6924         }
6925
6926         /* if setting by freq convert to channel */
6927         if (fwrq->e == 1) {
6928                 if ((fwrq->m >= (int) 2.412e8 &&
6929                      fwrq->m <= (int) 2.487e8)) {
6930                         int f = fwrq->m / 100000;
6931                         int c = 0;
6932
6933                         while ((c < REG_MAX_CHANNEL) &&
6934                                (f != ipw2100_frequencies[c]))
6935                                 c++;
6936
6937                         /* hack to fall through */
6938                         fwrq->e = 0;
6939                         fwrq->m = c + 1;
6940                 }
6941         }
6942
6943         if (fwrq->e > 0 || fwrq->m > 1000)
6944                 return -EOPNOTSUPP;
6945         else { /* Set the channel */
6946                 IPW_DEBUG_WX("SET Freq/Channel -> %d \n", fwrq->m);
6947                 err = ipw2100_set_channel(priv, fwrq->m, 0);
6948         }
6949
6950  done:
6951         up(&priv->action_sem);
6952         return err;
6953 }
6954
6955
6956 static int ipw2100_wx_get_freq(struct net_device *dev,
6957                                struct iw_request_info *info,
6958                                union iwreq_data *wrqu, char *extra)
6959 {
6960         /*
6961          * This can be called at any time.  No action lock required
6962          */
6963
6964         struct ipw2100_priv *priv = ieee80211_priv(dev);
6965
6966         wrqu->freq.e = 0;
6967
6968         /* If we are associated, trying to associate, or have a statically
6969          * configured CHANNEL then return that; otherwise return ANY */
6970         if (priv->config & CFG_STATIC_CHANNEL ||
6971             priv->status & STATUS_ASSOCIATED)
6972                 wrqu->freq.m = priv->channel;
6973         else
6974                 wrqu->freq.m = 0;
6975
6976         IPW_DEBUG_WX("GET Freq/Channel -> %d \n", priv->channel);
6977         return 0;
6978
6979 }
6980
6981 static int ipw2100_wx_set_mode(struct net_device *dev,
6982                                struct iw_request_info *info,
6983                                union iwreq_data *wrqu, char *extra)
6984 {
6985         struct ipw2100_priv *priv = ieee80211_priv(dev);
6986         int err = 0;
6987
6988         IPW_DEBUG_WX("SET Mode -> %d \n", wrqu->mode);
6989
6990         if (wrqu->mode == priv->ieee->iw_mode)
6991                 return 0;
6992
6993         down(&priv->action_sem);
6994         if (!(priv->status & STATUS_INITIALIZED)) {
6995                 err = -EIO;
6996                 goto done;
6997         }
6998
6999         switch (wrqu->mode) {
7000 #ifdef CONFIG_IPW2100_MONITOR
7001         case IW_MODE_MONITOR:
7002                 err = ipw2100_switch_mode(priv, IW_MODE_MONITOR);
7003                 break;
7004 #endif /* CONFIG_IPW2100_MONITOR */
7005         case IW_MODE_ADHOC:
7006                 err = ipw2100_switch_mode(priv, IW_MODE_ADHOC);
7007                 break;
7008         case IW_MODE_INFRA:
7009         case IW_MODE_AUTO:
7010         default:
7011                 err = ipw2100_switch_mode(priv, IW_MODE_INFRA);
7012                 break;
7013         }
7014
7015 done:
7016         up(&priv->action_sem);
7017         return err;
7018 }
7019
7020 static int ipw2100_wx_get_mode(struct net_device *dev,
7021                                struct iw_request_info *info,
7022                                union iwreq_data *wrqu, char *extra)
7023 {
7024         /*
7025          * This can be called at any time.  No action lock required
7026          */
7027
7028         struct ipw2100_priv *priv = ieee80211_priv(dev);
7029
7030         wrqu->mode = priv->ieee->iw_mode;
7031         IPW_DEBUG_WX("GET Mode -> %d\n", wrqu->mode);
7032
7033         return 0;
7034 }
7035
7036
7037 #define POWER_MODES 5
7038
7039 /* Values are in microsecond */
7040 const s32 timeout_duration[POWER_MODES] = {
7041         350000,
7042         250000,
7043         75000,
7044         37000,
7045         25000,
7046 };
7047
7048 const s32 period_duration[POWER_MODES] = {
7049         400000,
7050         700000,
7051         1000000,
7052         1000000,
7053         1000000
7054 };
7055
7056 static int ipw2100_wx_get_range(struct net_device *dev,
7057                                 struct iw_request_info *info,
7058                                 union iwreq_data *wrqu, char *extra)
7059 {
7060         /*
7061          * This can be called at any time.  No action lock required
7062          */
7063
7064         struct ipw2100_priv *priv = ieee80211_priv(dev);
7065         struct iw_range *range = (struct iw_range *)extra;
7066         u16 val;
7067         int i, level;
7068
7069         wrqu->data.length = sizeof(*range);
7070         memset(range, 0, sizeof(*range));
7071
7072         /* Let's try to keep this struct in the same order as in
7073          * linux/include/wireless.h
7074          */
7075
7076         /* TODO: See what values we can set, and remove the ones we can't
7077          * set, or fill them with some default data.
7078          */
7079
7080         /* ~5 Mb/s real (802.11b) */
7081         range->throughput = 5 * 1000 * 1000;
7082
7083 //      range->sensitivity;     /* signal level threshold range */
7084
7085         range->max_qual.qual = 100;
7086         /* TODO: Find real max RSSI and stick here */
7087         range->max_qual.level = 0;
7088         range->max_qual.noise = 0;
7089         range->max_qual.updated = 7; /* Updated all three */
7090
7091         range->avg_qual.qual = 70; /* > 8% missed beacons is 'bad' */
7092         /* TODO: Find real 'good' to 'bad' threshol value for RSSI */
7093         range->avg_qual.level = 20 + IPW2100_RSSI_TO_DBM;
7094         range->avg_qual.noise = 0;
7095         range->avg_qual.updated = 7; /* Updated all three */
7096
7097         range->num_bitrates = RATE_COUNT;
7098
7099         for (i = 0; i < RATE_COUNT && i < IW_MAX_BITRATES; i++) {
7100                 range->bitrate[i] = ipw2100_rates_11b[i];
7101         }
7102
7103         range->min_rts = MIN_RTS_THRESHOLD;
7104         range->max_rts = MAX_RTS_THRESHOLD;
7105         range->min_frag = MIN_FRAG_THRESHOLD;
7106         range->max_frag = MAX_FRAG_THRESHOLD;
7107
7108         range->min_pmp = period_duration[0];    /* Minimal PM period */
7109         range->max_pmp = period_duration[POWER_MODES-1];/* Maximal PM period */
7110         range->min_pmt = timeout_duration[POWER_MODES-1];       /* Minimal PM timeout */
7111         range->max_pmt = timeout_duration[0];/* Maximal PM timeout */
7112
7113         /* How to decode max/min PM period */
7114         range->pmp_flags = IW_POWER_PERIOD;
7115         /* How to decode max/min PM period */
7116         range->pmt_flags = IW_POWER_TIMEOUT;
7117         /* What PM options are supported */
7118         range->pm_capa = IW_POWER_TIMEOUT | IW_POWER_PERIOD;
7119
7120         range->encoding_size[0] = 5;
7121         range->encoding_size[1] = 13;           /* Different token sizes */
7122         range->num_encoding_sizes = 2;          /* Number of entry in the list */
7123         range->max_encoding_tokens = WEP_KEYS;  /* Max number of tokens */
7124 //      range->encoding_login_index;            /* token index for login token */
7125
7126         if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
7127                 range->txpower_capa = IW_TXPOW_DBM;
7128                 range->num_txpower = IW_MAX_TXPOWER;
7129                 for (i = 0, level = (IPW_TX_POWER_MAX_DBM * 16); i < IW_MAX_TXPOWER;
7130                      i++, level -= ((IPW_TX_POWER_MAX_DBM - IPW_TX_POWER_MIN_DBM) * 16) /
7131                              (IW_MAX_TXPOWER - 1))
7132                         range->txpower[i] = level / 16;
7133         } else {
7134                 range->txpower_capa = 0;
7135                 range->num_txpower = 0;
7136         }
7137
7138
7139         /* Set the Wireless Extension versions */
7140         range->we_version_compiled = WIRELESS_EXT;
7141         range->we_version_source = 16;
7142
7143 //      range->retry_capa;      /* What retry options are supported */
7144 //      range->retry_flags;     /* How to decode max/min retry limit */
7145 //      range->r_time_flags;    /* How to decode max/min retry life */
7146 //      range->min_retry;       /* Minimal number of retries */
7147 //      range->max_retry;       /* Maximal number of retries */
7148 //      range->min_r_time;      /* Minimal retry lifetime */
7149 //      range->max_r_time;      /* Maximal retry lifetime */
7150
7151         range->num_channels = FREQ_COUNT;
7152
7153         val = 0;
7154         for (i = 0; i < FREQ_COUNT; i++) {
7155                 // TODO: Include only legal frequencies for some countries
7156 //              if (local->channel_mask & (1 << i)) {
7157                         range->freq[val].i = i + 1;
7158                         range->freq[val].m = ipw2100_frequencies[i] * 100000;
7159                         range->freq[val].e = 1;
7160                         val++;
7161 //              }
7162                 if (val == IW_MAX_FREQUENCIES)
7163                 break;
7164         }
7165         range->num_frequency = val;
7166
7167         IPW_DEBUG_WX("GET Range\n");
7168
7169         return 0;
7170 }
7171
7172 static int ipw2100_wx_set_wap(struct net_device *dev,
7173                               struct iw_request_info *info,
7174                               union iwreq_data *wrqu, char *extra)
7175 {
7176         struct ipw2100_priv *priv = ieee80211_priv(dev);
7177         int err = 0;
7178
7179         static const unsigned char any[] = {
7180                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
7181         };
7182         static const unsigned char off[] = {
7183                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
7184         };
7185
7186         // sanity checks
7187         if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
7188                 return -EINVAL;
7189
7190         down(&priv->action_sem);
7191         if (!(priv->status & STATUS_INITIALIZED)) {
7192                 err = -EIO;
7193                 goto done;
7194         }
7195
7196         if (!memcmp(any, wrqu->ap_addr.sa_data, ETH_ALEN) ||
7197             !memcmp(off, wrqu->ap_addr.sa_data, ETH_ALEN)) {
7198                 /* we disable mandatory BSSID association */
7199                 IPW_DEBUG_WX("exit - disable mandatory BSSID\n");
7200                 priv->config &= ~CFG_STATIC_BSSID;
7201                 err = ipw2100_set_mandatory_bssid(priv, NULL, 0);
7202                 goto done;
7203         }
7204
7205         priv->config |= CFG_STATIC_BSSID;
7206         memcpy(priv->mandatory_bssid_mac, wrqu->ap_addr.sa_data, ETH_ALEN);
7207
7208         err = ipw2100_set_mandatory_bssid(priv, wrqu->ap_addr.sa_data, 0);
7209
7210         IPW_DEBUG_WX("SET BSSID -> %02X:%02X:%02X:%02X:%02X:%02X\n",
7211                      wrqu->ap_addr.sa_data[0] & 0xff,
7212                      wrqu->ap_addr.sa_data[1] & 0xff,
7213                      wrqu->ap_addr.sa_data[2] & 0xff,
7214                      wrqu->ap_addr.sa_data[3] & 0xff,
7215                      wrqu->ap_addr.sa_data[4] & 0xff,
7216                      wrqu->ap_addr.sa_data[5] & 0xff);
7217
7218  done:
7219         up(&priv->action_sem);
7220         return err;
7221 }
7222
7223 static int ipw2100_wx_get_wap(struct net_device *dev,
7224                               struct iw_request_info *info,
7225                               union iwreq_data *wrqu, char *extra)
7226 {
7227         /*
7228          * This can be called at any time.  No action lock required
7229          */
7230
7231         struct ipw2100_priv *priv = ieee80211_priv(dev);
7232
7233         /* If we are associated, trying to associate, or have a statically
7234          * configured BSSID then return that; otherwise return ANY */
7235         if (priv->config & CFG_STATIC_BSSID ||
7236             priv->status & STATUS_ASSOCIATED) {
7237                 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
7238                 memcpy(wrqu->ap_addr.sa_data, &priv->bssid, ETH_ALEN);
7239         } else
7240                 memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);
7241
7242         IPW_DEBUG_WX("Getting WAP BSSID: " MAC_FMT "\n",
7243                      MAC_ARG(wrqu->ap_addr.sa_data));
7244         return 0;
7245 }
7246
7247 static int ipw2100_wx_set_essid(struct net_device *dev,
7248                                 struct iw_request_info *info,
7249                                 union iwreq_data *wrqu, char *extra)
7250 {
7251         struct ipw2100_priv *priv = ieee80211_priv(dev);
7252         char *essid = ""; /* ANY */
7253         int length = 0;
7254         int err = 0;
7255
7256         down(&priv->action_sem);
7257         if (!(priv->status & STATUS_INITIALIZED)) {
7258                 err = -EIO;
7259                 goto done;
7260         }
7261
7262         if (wrqu->essid.flags && wrqu->essid.length) {
7263                 length = wrqu->essid.length - 1;
7264                 essid = extra;
7265         }
7266
7267         if (length == 0) {
7268                 IPW_DEBUG_WX("Setting ESSID to ANY\n");
7269                 priv->config &= ~CFG_STATIC_ESSID;
7270                 err = ipw2100_set_essid(priv, NULL, 0, 0);
7271                 goto done;
7272         }
7273
7274         length = min(length, IW_ESSID_MAX_SIZE);
7275
7276         priv->config |= CFG_STATIC_ESSID;
7277
7278         if (priv->essid_len == length && !memcmp(priv->essid, extra, length)) {
7279                 IPW_DEBUG_WX("ESSID set to current ESSID.\n");
7280                 err = 0;
7281                 goto done;
7282         }
7283
7284         IPW_DEBUG_WX("Setting ESSID: '%s' (%d)\n", escape_essid(essid, length),
7285                      length);
7286
7287         priv->essid_len = length;
7288         memcpy(priv->essid, essid, priv->essid_len);
7289
7290         err = ipw2100_set_essid(priv, essid, length, 0);
7291
7292  done:
7293         up(&priv->action_sem);
7294         return err;
7295 }
7296
7297 static int ipw2100_wx_get_essid(struct net_device *dev,
7298                                 struct iw_request_info *info,
7299                                 union iwreq_data *wrqu, char *extra)
7300 {
7301         /*
7302          * This can be called at any time.  No action lock required
7303          */
7304
7305         struct ipw2100_priv *priv = ieee80211_priv(dev);
7306
7307         /* If we are associated, trying to associate, or have a statically
7308          * configured ESSID then return that; otherwise return ANY */
7309         if (priv->config & CFG_STATIC_ESSID ||
7310             priv->status & STATUS_ASSOCIATED) {
7311                 IPW_DEBUG_WX("Getting essid: '%s'\n",
7312                              escape_essid(priv->essid, priv->essid_len));
7313                 memcpy(extra, priv->essid, priv->essid_len);
7314                 wrqu->essid.length = priv->essid_len;
7315                 wrqu->essid.flags = 1; /* active */
7316         } else {
7317                 IPW_DEBUG_WX("Getting essid: ANY\n");
7318                 wrqu->essid.length = 0;
7319                 wrqu->essid.flags = 0; /* active */
7320         }
7321
7322         return 0;
7323 }
7324
7325 static int ipw2100_wx_set_nick(struct net_device *dev,
7326                                struct iw_request_info *info,
7327                                union iwreq_data *wrqu, char *extra)
7328 {
7329         /*
7330          * This can be called at any time.  No action lock required
7331          */
7332
7333         struct ipw2100_priv *priv = ieee80211_priv(dev);
7334
7335         if (wrqu->data.length > IW_ESSID_MAX_SIZE)
7336                 return -E2BIG;
7337
7338         wrqu->data.length = min((size_t)wrqu->data.length, sizeof(priv->nick));
7339         memset(priv->nick, 0, sizeof(priv->nick));
7340         memcpy(priv->nick, extra,  wrqu->data.length);
7341
7342         IPW_DEBUG_WX("SET Nickname -> %s \n", priv->nick);
7343
7344         return 0;
7345 }
7346
7347 static int ipw2100_wx_get_nick(struct net_device *dev,
7348                                struct iw_request_info *info,
7349                                union iwreq_data *wrqu, char *extra)
7350 {
7351         /*
7352          * This can be called at any time.  No action lock required
7353          */
7354
7355         struct ipw2100_priv *priv = ieee80211_priv(dev);
7356
7357         wrqu->data.length = strlen(priv->nick) + 1;
7358         memcpy(extra, priv->nick, wrqu->data.length);
7359         wrqu->data.flags = 1; /* active */
7360
7361         IPW_DEBUG_WX("GET Nickname -> %s \n", extra);
7362
7363         return 0;
7364 }
7365
7366 static int ipw2100_wx_set_rate(struct net_device *dev,
7367                                struct iw_request_info *info,
7368                                union iwreq_data *wrqu, char *extra)
7369 {
7370         struct ipw2100_priv *priv = ieee80211_priv(dev);
7371         u32 target_rate = wrqu->bitrate.value;
7372         u32 rate;
7373         int err = 0;
7374
7375         down(&priv->action_sem);
7376         if (!(priv->status & STATUS_INITIALIZED)) {
7377                 err = -EIO;
7378                 goto done;
7379         }
7380
7381         rate = 0;
7382
7383         if (target_rate == 1000000 ||
7384             (!wrqu->bitrate.fixed && target_rate > 1000000))
7385                 rate |= TX_RATE_1_MBIT;
7386         if (target_rate == 2000000 ||
7387             (!wrqu->bitrate.fixed && target_rate > 2000000))
7388                 rate |= TX_RATE_2_MBIT;
7389         if (target_rate == 5500000 ||
7390             (!wrqu->bitrate.fixed && target_rate > 5500000))
7391                 rate |= TX_RATE_5_5_MBIT;
7392         if (target_rate == 11000000 ||
7393             (!wrqu->bitrate.fixed && target_rate > 11000000))
7394                 rate |= TX_RATE_11_MBIT;
7395         if (rate == 0)
7396                 rate = DEFAULT_TX_RATES;
7397
7398         err = ipw2100_set_tx_rates(priv, rate, 0);
7399
7400         IPW_DEBUG_WX("SET Rate -> %04X \n", rate);
7401  done:
7402         up(&priv->action_sem);
7403         return err;
7404 }
7405
7406
7407 static int ipw2100_wx_get_rate(struct net_device *dev,
7408                                struct iw_request_info *info,
7409                                union iwreq_data *wrqu, char *extra)
7410 {
7411         struct ipw2100_priv *priv = ieee80211_priv(dev);
7412         int val;
7413         int len = sizeof(val);
7414         int err = 0;
7415
7416         if (!(priv->status & STATUS_ENABLED) ||
7417             priv->status & STATUS_RF_KILL_MASK ||
7418             !(priv->status & STATUS_ASSOCIATED)) {
7419                 wrqu->bitrate.value = 0;
7420                 return 0;
7421         }
7422
7423         down(&priv->action_sem);
7424         if (!(priv->status & STATUS_INITIALIZED)) {
7425                 err = -EIO;
7426                 goto done;
7427         }
7428
7429         err = ipw2100_get_ordinal(priv, IPW_ORD_CURRENT_TX_RATE, &val, &len);
7430         if (err) {
7431                 IPW_DEBUG_WX("failed querying ordinals.\n");
7432                 return err;
7433         }
7434
7435         switch (val & TX_RATE_MASK) {
7436         case TX_RATE_1_MBIT:
7437                 wrqu->bitrate.value = 1000000;
7438                 break;
7439         case TX_RATE_2_MBIT:
7440                 wrqu->bitrate.value = 2000000;
7441                 break;
7442         case TX_RATE_5_5_MBIT:
7443                 wrqu->bitrate.value = 5500000;
7444                 break;
7445         case TX_RATE_11_MBIT:
7446                 wrqu->bitrate.value = 11000000;
7447                 break;
7448         default:
7449                 wrqu->bitrate.value = 0;
7450         }
7451
7452         IPW_DEBUG_WX("GET Rate -> %d \n", wrqu->bitrate.value);
7453
7454  done:
7455         up(&priv->action_sem);
7456         return err;
7457 }
7458
7459 static int ipw2100_wx_set_rts(struct net_device *dev,
7460                               struct iw_request_info *info,
7461                               union iwreq_data *wrqu, char *extra)
7462 {
7463         struct ipw2100_priv *priv = ieee80211_priv(dev);
7464         int value, err;
7465
7466         /* Auto RTS not yet supported */
7467         if (wrqu->rts.fixed == 0)
7468                 return -EINVAL;
7469
7470         down(&priv->action_sem);
7471         if (!(priv->status & STATUS_INITIALIZED)) {
7472                 err = -EIO;
7473                 goto done;
7474         }
7475
7476         if (wrqu->rts.disabled)
7477                 value = priv->rts_threshold | RTS_DISABLED;
7478         else {
7479                 if (wrqu->rts.value < 1 ||
7480                     wrqu->rts.value > 2304) {
7481                         err = -EINVAL;
7482                         goto done;
7483                 }
7484                 value = wrqu->rts.value;
7485         }
7486
7487         err = ipw2100_set_rts_threshold(priv, value);
7488
7489         IPW_DEBUG_WX("SET RTS Threshold -> 0x%08X \n", value);
7490  done:
7491         up(&priv->action_sem);
7492         return err;
7493 }
7494
7495 static int ipw2100_wx_get_rts(struct net_device *dev,
7496                               struct iw_request_info *info,
7497                               union iwreq_data *wrqu, char *extra)
7498 {
7499         /*
7500          * This can be called at any time.  No action lock required
7501          */
7502
7503         struct ipw2100_priv *priv = ieee80211_priv(dev);
7504
7505         wrqu->rts.value = priv->rts_threshold & ~RTS_DISABLED;
7506         wrqu->rts.fixed = 1; /* no auto select */
7507
7508         /* If RTS is set to the default value, then it is disabled */
7509         wrqu->rts.disabled = (priv->rts_threshold & RTS_DISABLED) ? 1 : 0;
7510
7511         IPW_DEBUG_WX("GET RTS Threshold -> 0x%08X \n", wrqu->rts.value);
7512
7513         return 0;
7514 }
7515
7516 static int ipw2100_wx_set_txpow(struct net_device *dev,
7517                                 struct iw_request_info *info,
7518                                 union iwreq_data *wrqu, char *extra)
7519 {
7520         struct ipw2100_priv *priv = ieee80211_priv(dev);
7521         int err = 0, value;
7522
7523         if (priv->ieee->iw_mode != IW_MODE_ADHOC)
7524                 return -EINVAL;
7525
7526         if (wrqu->txpower.disabled == 1 || wrqu->txpower.fixed == 0)
7527                 value = IPW_TX_POWER_DEFAULT;
7528         else {
7529                 if (wrqu->txpower.value < IPW_TX_POWER_MIN_DBM ||
7530                     wrqu->txpower.value > IPW_TX_POWER_MAX_DBM)
7531                         return -EINVAL;
7532
7533                 value = (wrqu->txpower.value - IPW_TX_POWER_MIN_DBM) * 16 /
7534                         (IPW_TX_POWER_MAX_DBM - IPW_TX_POWER_MIN_DBM);
7535         }
7536
7537         down(&priv->action_sem);
7538         if (!(priv->status & STATUS_INITIALIZED)) {
7539                 err = -EIO;
7540                 goto done;
7541         }
7542
7543         err = ipw2100_set_tx_power(priv, value);
7544
7545         IPW_DEBUG_WX("SET TX Power -> %d \n", value);
7546
7547  done:
7548         up(&priv->action_sem);
7549         return err;
7550 }
7551
7552 static int ipw2100_wx_get_txpow(struct net_device *dev,
7553                                 struct iw_request_info *info,
7554                                 union iwreq_data *wrqu, char *extra)
7555 {
7556         /*
7557          * This can be called at any time.  No action lock required
7558          */
7559
7560         struct ipw2100_priv *priv = ieee80211_priv(dev);
7561
7562         if (priv->ieee->iw_mode != IW_MODE_ADHOC) {
7563                 wrqu->power.disabled = 1;
7564                 return 0;
7565         }
7566
7567         if (priv->tx_power == IPW_TX_POWER_DEFAULT) {
7568                 wrqu->power.fixed = 0;
7569                 wrqu->power.value = IPW_TX_POWER_MAX_DBM;
7570                 wrqu->power.disabled = 1;
7571         } else {
7572                 wrqu->power.disabled = 0;
7573                 wrqu->power.fixed = 1;
7574                 wrqu->power.value =
7575                         (priv->tx_power *
7576                          (IPW_TX_POWER_MAX_DBM - IPW_TX_POWER_MIN_DBM)) /
7577                         (IPW_TX_POWER_MAX - IPW_TX_POWER_MIN) +
7578                         IPW_TX_POWER_MIN_DBM;
7579         }
7580
7581         wrqu->power.flags = IW_TXPOW_DBM;
7582
7583         IPW_DEBUG_WX("GET TX Power -> %d \n", wrqu->power.value);
7584
7585         return 0;
7586 }
7587
7588 static int ipw2100_wx_set_frag(struct net_device *dev,
7589                                struct iw_request_info *info,
7590                                union iwreq_data *wrqu, char *extra)
7591 {
7592         /*
7593          * This can be called at any time.  No action lock required
7594          */
7595
7596         struct ipw2100_priv *priv = ieee80211_priv(dev);
7597
7598         if (!wrqu->frag.fixed)
7599                 return -EINVAL;
7600
7601         if (wrqu->frag.disabled) {
7602                 priv->frag_threshold |= FRAG_DISABLED;
7603                 priv->ieee->fts = DEFAULT_FTS;
7604         } else {
7605                 if (wrqu->frag.value < MIN_FRAG_THRESHOLD ||
7606                     wrqu->frag.value > MAX_FRAG_THRESHOLD)
7607                         return -EINVAL;
7608
7609                 priv->ieee->fts = wrqu->frag.value & ~0x1;
7610                 priv->frag_threshold = priv->ieee->fts;
7611         }
7612
7613         IPW_DEBUG_WX("SET Frag Threshold -> %d \n", priv->ieee->fts);
7614
7615         return 0;
7616 }
7617
7618 static int ipw2100_wx_get_frag(struct net_device *dev,
7619                                struct iw_request_info *info,
7620                                union iwreq_data *wrqu, char *extra)
7621 {
7622         /*
7623          * This can be called at any time.  No action lock required
7624          */
7625
7626         struct ipw2100_priv *priv = ieee80211_priv(dev);
7627         wrqu->frag.value = priv->frag_threshold & ~FRAG_DISABLED;
7628         wrqu->frag.fixed = 0;   /* no auto select */
7629         wrqu->frag.disabled = (priv->frag_threshold & FRAG_DISABLED) ? 1 : 0;
7630
7631         IPW_DEBUG_WX("GET Frag Threshold -> %d \n", wrqu->frag.value);
7632
7633         return 0;
7634 }
7635
7636 static int ipw2100_wx_set_retry(struct net_device *dev,
7637                                 struct iw_request_info *info,
7638                                 union iwreq_data *wrqu, char *extra)
7639 {
7640         struct ipw2100_priv *priv = ieee80211_priv(dev);
7641         int err = 0;
7642
7643         if (wrqu->retry.flags & IW_RETRY_LIFETIME ||
7644             wrqu->retry.disabled)
7645                 return -EINVAL;
7646
7647         if (!(wrqu->retry.flags & IW_RETRY_LIMIT))
7648                 return 0;
7649
7650         down(&priv->action_sem);
7651         if (!(priv->status & STATUS_INITIALIZED)) {
7652                 err = -EIO;
7653                 goto done;
7654         }
7655
7656         if (wrqu->retry.flags & IW_RETRY_MIN) {
7657                 err = ipw2100_set_short_retry(priv, wrqu->retry.value);
7658                 IPW_DEBUG_WX("SET Short Retry Limit -> %d \n",
7659                        wrqu->retry.value);
7660                 goto done;
7661         }
7662
7663         if (wrqu->retry.flags & IW_RETRY_MAX) {
7664                 err = ipw2100_set_long_retry(priv, wrqu->retry.value);
7665                 IPW_DEBUG_WX("SET Long Retry Limit -> %d \n",
7666                        wrqu->retry.value);
7667                 goto done;
7668         }
7669
7670         err = ipw2100_set_short_retry(priv, wrqu->retry.value);
7671         if (!err)
7672                 err = ipw2100_set_long_retry(priv, wrqu->retry.value);
7673
7674         IPW_DEBUG_WX("SET Both Retry Limits -> %d \n", wrqu->retry.value);
7675
7676  done:
7677         up(&priv->action_sem);
7678         return err;
7679 }
7680
7681 static int ipw2100_wx_get_retry(struct net_device *dev,
7682                                 struct iw_request_info *info,
7683                                 union iwreq_data *wrqu, char *extra)
7684 {
7685         /*
7686          * This can be called at any time.  No action lock required
7687          */
7688
7689         struct ipw2100_priv *priv = ieee80211_priv(dev);
7690
7691         wrqu->retry.disabled = 0; /* can't be disabled */
7692
7693         if ((wrqu->retry.flags & IW_RETRY_TYPE) ==
7694             IW_RETRY_LIFETIME)
7695                 return -EINVAL;
7696
7697         if (wrqu->retry.flags & IW_RETRY_MAX) {
7698                 wrqu->retry.flags = IW_RETRY_LIMIT & IW_RETRY_MAX;
7699                 wrqu->retry.value = priv->long_retry_limit;
7700         } else {
7701                 wrqu->retry.flags =
7702                     (priv->short_retry_limit !=
7703                      priv->long_retry_limit) ?
7704                     IW_RETRY_LIMIT & IW_RETRY_MIN : IW_RETRY_LIMIT;
7705
7706                 wrqu->retry.value = priv->short_retry_limit;
7707         }
7708
7709         IPW_DEBUG_WX("GET Retry -> %d \n", wrqu->retry.value);
7710
7711         return 0;
7712 }
7713
7714 static int ipw2100_wx_set_scan(struct net_device *dev,
7715                                struct iw_request_info *info,
7716                                union iwreq_data *wrqu, char *extra)
7717 {
7718         struct ipw2100_priv *priv = ieee80211_priv(dev);
7719         int err = 0;
7720
7721         down(&priv->action_sem);
7722         if (!(priv->status & STATUS_INITIALIZED)) {
7723                 err = -EIO;
7724                 goto done;
7725         }
7726
7727         IPW_DEBUG_WX("Initiating scan...\n");
7728         if (ipw2100_set_scan_options(priv) ||
7729             ipw2100_start_scan(priv)) {
7730                 IPW_DEBUG_WX("Start scan failed.\n");
7731
7732                 /* TODO: Mark a scan as pending so when hardware initialized
7733                  *       a scan starts */
7734         }
7735
7736  done:
7737         up(&priv->action_sem);
7738         return err;
7739 }
7740
7741 static int ipw2100_wx_get_scan(struct net_device *dev,
7742                                struct iw_request_info *info,
7743                                union iwreq_data *wrqu, char *extra)
7744 {
7745         /*
7746          * This can be called at any time.  No action lock required
7747          */
7748
7749         struct ipw2100_priv *priv = ieee80211_priv(dev);
7750         return ieee80211_wx_get_scan(priv->ieee, info, wrqu, extra);
7751 }
7752
7753
7754 /*
7755  * Implementation based on code in hostap-driver v0.1.3 hostap_ioctl.c
7756  */
7757 static int ipw2100_wx_set_encode(struct net_device *dev,
7758                                  struct iw_request_info *info,
7759                                  union iwreq_data *wrqu, char *key)
7760 {
7761         /*
7762          * No check of STATUS_INITIALIZED required
7763          */
7764
7765         struct ipw2100_priv *priv = ieee80211_priv(dev);
7766         return ieee80211_wx_set_encode(priv->ieee, info, wrqu, key);
7767 }
7768
7769 static int ipw2100_wx_get_encode(struct net_device *dev,
7770                                  struct iw_request_info *info,
7771                                  union iwreq_data *wrqu, char *key)
7772 {
7773         /*
7774          * This can be called at any time.  No action lock required
7775          */
7776
7777         struct ipw2100_priv *priv = ieee80211_priv(dev);
7778         return ieee80211_wx_get_encode(priv->ieee, info, wrqu, key);
7779 }
7780
7781 static int ipw2100_wx_set_power(struct net_device *dev,
7782                                 struct iw_request_info *info,
7783                                 union iwreq_data *wrqu, char *extra)
7784 {
7785         struct ipw2100_priv *priv = ieee80211_priv(dev);
7786         int err = 0;
7787
7788         down(&priv->action_sem);
7789         if (!(priv->status & STATUS_INITIALIZED)) {
7790                 err = -EIO;
7791                 goto done;
7792         }
7793
7794         if (wrqu->power.disabled) {
7795                 priv->power_mode = IPW_POWER_LEVEL(priv->power_mode);
7796                 err = ipw2100_set_power_mode(priv, IPW_POWER_MODE_CAM);
7797                 IPW_DEBUG_WX("SET Power Management Mode -> off\n");
7798                 goto done;
7799         }
7800
7801         switch (wrqu->power.flags & IW_POWER_MODE) {
7802         case IW_POWER_ON:    /* If not specified */
7803         case IW_POWER_MODE:  /* If set all mask */
7804         case IW_POWER_ALL_R: /* If explicitely state all */
7805                 break;
7806         default: /* Otherwise we don't support it */
7807                 IPW_DEBUG_WX("SET PM Mode: %X not supported.\n",
7808                              wrqu->power.flags);
7809                 err = -EOPNOTSUPP;
7810                 goto done;
7811         }
7812
7813         /* If the user hasn't specified a power management mode yet, default
7814          * to BATTERY */
7815         priv->power_mode = IPW_POWER_ENABLED | priv->power_mode;
7816         err = ipw2100_set_power_mode(priv, IPW_POWER_LEVEL(priv->power_mode));
7817
7818         IPW_DEBUG_WX("SET Power Management Mode -> 0x%02X\n",
7819                      priv->power_mode);
7820
7821  done:
7822         up(&priv->action_sem);
7823         return err;
7824
7825 }
7826
7827 static int ipw2100_wx_get_power(struct net_device *dev,
7828                                 struct iw_request_info *info,
7829                                 union iwreq_data *wrqu, char *extra)
7830 {
7831         /*
7832          * This can be called at any time.  No action lock required
7833          */
7834
7835         struct ipw2100_priv *priv = ieee80211_priv(dev);
7836
7837         if (!(priv->power_mode & IPW_POWER_ENABLED)) {
7838                 wrqu->power.disabled = 1;
7839         } else {
7840                 wrqu->power.disabled = 0;
7841                 wrqu->power.flags = 0;
7842         }
7843
7844         IPW_DEBUG_WX("GET Power Management Mode -> %02X\n", priv->power_mode);
7845
7846         return 0;
7847 }
7848
7849
7850 /*
7851  *
7852  * IWPRIV handlers
7853  *
7854  */
7855 #ifdef CONFIG_IPW2100_MONITOR
7856 static int ipw2100_wx_set_promisc(struct net_device *dev,
7857                                   struct iw_request_info *info,
7858                                   union iwreq_data *wrqu, char *extra)
7859 {
7860         struct ipw2100_priv *priv = ieee80211_priv(dev);
7861         int *parms = (int *)extra;
7862         int enable = (parms[0] > 0);
7863         int err = 0;
7864
7865         down(&priv->action_sem);
7866         if (!(priv->status & STATUS_INITIALIZED)) {
7867                 err = -EIO;
7868                 goto done;
7869         }
7870
7871         if (enable) {
7872                 if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
7873                         err = ipw2100_set_channel(priv, parms[1], 0);
7874                         goto done;
7875                 }
7876                 priv->channel = parms[1];
7877                 err = ipw2100_switch_mode(priv, IW_MODE_MONITOR);
7878         } else {
7879                 if (priv->ieee->iw_mode == IW_MODE_MONITOR)
7880                         err = ipw2100_switch_mode(priv, priv->last_mode);
7881         }
7882  done:
7883         up(&priv->action_sem);
7884         return err;
7885 }
7886
7887 static int ipw2100_wx_reset(struct net_device *dev,
7888                             struct iw_request_info *info,
7889                             union iwreq_data *wrqu, char *extra)
7890 {
7891         struct ipw2100_priv *priv = ieee80211_priv(dev);
7892         if (priv->status & STATUS_INITIALIZED)
7893                 schedule_reset(priv);
7894         return 0;
7895 }
7896
7897 #endif
7898
7899 static int ipw2100_wx_set_powermode(struct net_device *dev,
7900                                     struct iw_request_info *info,
7901                                     union iwreq_data *wrqu, char *extra)
7902 {
7903         struct ipw2100_priv *priv = ieee80211_priv(dev);
7904         int err = 0, mode = *(int *)extra;
7905
7906         down(&priv->action_sem);
7907         if (!(priv->status & STATUS_INITIALIZED)) {
7908                 err = -EIO;
7909                 goto done;
7910         }
7911
7912         if ((mode < 1) || (mode > POWER_MODES))
7913                 mode = IPW_POWER_AUTO;
7914
7915         if (priv->power_mode != mode)
7916                 err = ipw2100_set_power_mode(priv, mode);
7917  done:
7918         up(&priv->action_sem);
7919         return err;
7920 }
7921
7922 #define MAX_POWER_STRING 80
7923 static int ipw2100_wx_get_powermode(struct net_device *dev,
7924                                     struct iw_request_info *info,
7925                                     union iwreq_data *wrqu, char *extra)
7926 {
7927         /*
7928          * This can be called at any time.  No action lock required
7929          */
7930
7931         struct ipw2100_priv *priv = ieee80211_priv(dev);
7932         int level = IPW_POWER_LEVEL(priv->power_mode);
7933         s32 timeout, period;
7934
7935         if (!(priv->power_mode & IPW_POWER_ENABLED)) {
7936                 snprintf(extra, MAX_POWER_STRING,
7937                          "Power save level: %d (Off)", level);
7938         } else {
7939                 switch (level) {
7940                 case IPW_POWER_MODE_CAM:
7941                         snprintf(extra, MAX_POWER_STRING,
7942                                  "Power save level: %d (None)", level);
7943                         break;
7944                 case IPW_POWER_AUTO:
7945                 snprintf(extra, MAX_POWER_STRING,
7946                          "Power save level: %d (Auto)", 0);
7947                         break;
7948                 default:
7949                         timeout = timeout_duration[level - 1] / 1000;
7950                         period = period_duration[level - 1] / 1000;
7951                         snprintf(extra, MAX_POWER_STRING,
7952                                  "Power save level: %d "
7953                                  "(Timeout %dms, Period %dms)",
7954                                  level, timeout, period);
7955                 }
7956         }
7957
7958         wrqu->data.length = strlen(extra) + 1;
7959
7960         return 0;
7961 }
7962
7963
7964 static int ipw2100_wx_set_preamble(struct net_device *dev,
7965                                    struct iw_request_info *info,
7966                                    union iwreq_data *wrqu, char *extra)
7967 {
7968         struct ipw2100_priv *priv = ieee80211_priv(dev);
7969         int err, mode = *(int *)extra;
7970
7971         down(&priv->action_sem);
7972         if (!(priv->status & STATUS_INITIALIZED)) {
7973                 err = -EIO;
7974                 goto done;
7975         }
7976
7977         if (mode == 1)
7978                 priv->config |= CFG_LONG_PREAMBLE;
7979         else if (mode == 0)
7980                 priv->config &= ~CFG_LONG_PREAMBLE;
7981         else {
7982                 err = -EINVAL;
7983                 goto done;
7984         }
7985
7986         err = ipw2100_system_config(priv, 0);
7987
7988 done:
7989         up(&priv->action_sem);
7990         return err;
7991 }
7992
7993 static int ipw2100_wx_get_preamble(struct net_device *dev,
7994                                     struct iw_request_info *info,
7995                                     union iwreq_data *wrqu, char *extra)
7996 {
7997         /*
7998          * This can be called at any time.  No action lock required
7999          */
8000
8001         struct ipw2100_priv *priv = ieee80211_priv(dev);
8002
8003         if (priv->config & CFG_LONG_PREAMBLE)
8004                 snprintf(wrqu->name, IFNAMSIZ, "long (1)");
8005         else
8006                 snprintf(wrqu->name, IFNAMSIZ, "auto (0)");
8007
8008         return 0;
8009 }
8010
8011 static iw_handler ipw2100_wx_handlers[] =
8012 {
8013         NULL,                     /* SIOCSIWCOMMIT */
8014         ipw2100_wx_get_name,      /* SIOCGIWNAME */
8015         NULL,                     /* SIOCSIWNWID */
8016         NULL,                     /* SIOCGIWNWID */
8017         ipw2100_wx_set_freq,      /* SIOCSIWFREQ */
8018         ipw2100_wx_get_freq,      /* SIOCGIWFREQ */
8019         ipw2100_wx_set_mode,      /* SIOCSIWMODE */
8020         ipw2100_wx_get_mode,      /* SIOCGIWMODE */
8021         NULL,                     /* SIOCSIWSENS */
8022         NULL,                     /* SIOCGIWSENS */
8023         NULL,                     /* SIOCSIWRANGE */
8024         ipw2100_wx_get_range,     /* SIOCGIWRANGE */
8025         NULL,                     /* SIOCSIWPRIV */
8026         NULL,                     /* SIOCGIWPRIV */
8027         NULL,                     /* SIOCSIWSTATS */
8028         NULL,                     /* SIOCGIWSTATS */
8029         NULL,                     /* SIOCSIWSPY */
8030         NULL,                     /* SIOCGIWSPY */
8031         NULL,                     /* SIOCGIWTHRSPY */
8032         NULL,                     /* SIOCWIWTHRSPY */
8033         ipw2100_wx_set_wap,       /* SIOCSIWAP */
8034         ipw2100_wx_get_wap,       /* SIOCGIWAP */
8035         NULL,                     /* -- hole -- */
8036         NULL,                     /* SIOCGIWAPLIST -- deprecated */
8037         ipw2100_wx_set_scan,      /* SIOCSIWSCAN */
8038         ipw2100_wx_get_scan,      /* SIOCGIWSCAN */
8039         ipw2100_wx_set_essid,     /* SIOCSIWESSID */
8040         ipw2100_wx_get_essid,     /* SIOCGIWESSID */
8041         ipw2100_wx_set_nick,      /* SIOCSIWNICKN */
8042         ipw2100_wx_get_nick,      /* SIOCGIWNICKN */
8043         NULL,                     /* -- hole -- */
8044         NULL,                     /* -- hole -- */
8045         ipw2100_wx_set_rate,      /* SIOCSIWRATE */
8046         ipw2100_wx_get_rate,      /* SIOCGIWRATE */
8047         ipw2100_wx_set_rts,       /* SIOCSIWRTS */
8048         ipw2100_wx_get_rts,       /* SIOCGIWRTS */
8049         ipw2100_wx_set_frag,      /* SIOCSIWFRAG */
8050         ipw2100_wx_get_frag,      /* SIOCGIWFRAG */
8051         ipw2100_wx_set_txpow,     /* SIOCSIWTXPOW */
8052         ipw2100_wx_get_txpow,     /* SIOCGIWTXPOW */
8053         ipw2100_wx_set_retry,     /* SIOCSIWRETRY */
8054         ipw2100_wx_get_retry,     /* SIOCGIWRETRY */
8055         ipw2100_wx_set_encode,    /* SIOCSIWENCODE */
8056         ipw2100_wx_get_encode,    /* SIOCGIWENCODE */
8057         ipw2100_wx_set_power,     /* SIOCSIWPOWER */
8058         ipw2100_wx_get_power,     /* SIOCGIWPOWER */
8059 };
8060
8061 #define IPW2100_PRIV_SET_MONITOR        SIOCIWFIRSTPRIV
8062 #define IPW2100_PRIV_RESET              SIOCIWFIRSTPRIV+1
8063 #define IPW2100_PRIV_SET_POWER          SIOCIWFIRSTPRIV+2
8064 #define IPW2100_PRIV_GET_POWER          SIOCIWFIRSTPRIV+3
8065 #define IPW2100_PRIV_SET_LONGPREAMBLE   SIOCIWFIRSTPRIV+4
8066 #define IPW2100_PRIV_GET_LONGPREAMBLE   SIOCIWFIRSTPRIV+5
8067
8068 static const struct iw_priv_args ipw2100_private_args[] = {
8069
8070 #ifdef CONFIG_IPW2100_MONITOR
8071         {
8072                 IPW2100_PRIV_SET_MONITOR,
8073                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "monitor"
8074         },
8075         {
8076                 IPW2100_PRIV_RESET,
8077                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 0, 0, "reset"
8078         },
8079 #endif /* CONFIG_IPW2100_MONITOR */
8080
8081         {
8082                 IPW2100_PRIV_SET_POWER,
8083                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "set_power"
8084         },
8085         {
8086                 IPW2100_PRIV_GET_POWER,
8087                 0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_POWER_STRING, "get_power"
8088         },
8089         {
8090                 IPW2100_PRIV_SET_LONGPREAMBLE,
8091                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "set_preamble"
8092         },
8093         {
8094                 IPW2100_PRIV_GET_LONGPREAMBLE,
8095                 0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | IFNAMSIZ, "get_preamble"
8096         },
8097 };
8098
8099 static iw_handler ipw2100_private_handler[] = {
8100 #ifdef CONFIG_IPW2100_MONITOR
8101         ipw2100_wx_set_promisc,
8102         ipw2100_wx_reset,
8103 #else /* CONFIG_IPW2100_MONITOR */
8104         NULL,
8105         NULL,
8106 #endif /* CONFIG_IPW2100_MONITOR */
8107         ipw2100_wx_set_powermode,
8108         ipw2100_wx_get_powermode,
8109         ipw2100_wx_set_preamble,
8110         ipw2100_wx_get_preamble,
8111 };
8112
8113 struct iw_handler_def ipw2100_wx_handler_def =
8114 {
8115         .standard = ipw2100_wx_handlers,
8116         .num_standard = sizeof(ipw2100_wx_handlers) / sizeof(iw_handler),
8117         .num_private = sizeof(ipw2100_private_handler) / sizeof(iw_handler),
8118         .num_private_args = sizeof(ipw2100_private_args) /
8119         sizeof(struct iw_priv_args),
8120         .private = (iw_handler *)ipw2100_private_handler,
8121         .private_args = (struct iw_priv_args *)ipw2100_private_args,
8122 };
8123
8124 /*
8125  * Get wireless statistics.
8126  * Called by /proc/net/wireless
8127  * Also called by SIOCGIWSTATS
8128  */
8129 struct iw_statistics *ipw2100_wx_wireless_stats(struct net_device * dev)
8130 {
8131         enum {
8132                 POOR = 30,
8133                 FAIR = 60,
8134                 GOOD = 80,
8135                 VERY_GOOD = 90,
8136                 EXCELLENT = 95,
8137                 PERFECT = 100
8138         };
8139         int rssi_qual;
8140         int tx_qual;
8141         int beacon_qual;
8142
8143         struct ipw2100_priv *priv = ieee80211_priv(dev);
8144         struct iw_statistics *wstats;
8145         u32 rssi, quality, tx_retries, missed_beacons, tx_failures;
8146         u32 ord_len = sizeof(u32);
8147
8148         if (!priv)
8149                 return (struct iw_statistics *) NULL;
8150
8151         wstats = &priv->wstats;
8152
8153         /* if hw is disabled, then ipw2100_get_ordinal() can't be called.
8154          * ipw2100_wx_wireless_stats seems to be called before fw is
8155          * initialized.  STATUS_ASSOCIATED will only be set if the hw is up
8156          * and associated; if not associcated, the values are all meaningless
8157          * anyway, so set them all to NULL and INVALID */
8158         if (!(priv->status & STATUS_ASSOCIATED)) {
8159                 wstats->miss.beacon = 0;
8160                 wstats->discard.retries = 0;
8161                 wstats->qual.qual = 0;
8162                 wstats->qual.level = 0;
8163                 wstats->qual.noise = 0;
8164                 wstats->qual.updated = 7;
8165                 wstats->qual.updated |= IW_QUAL_NOISE_INVALID |
8166                         IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID;
8167                 return wstats;
8168         }
8169
8170         if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_PERCENT_MISSED_BCNS,
8171                                 &missed_beacons, &ord_len))
8172                 goto fail_get_ordinal;
8173
8174         /* If we don't have a connection the quality and level is 0*/
8175         if (!(priv->status & STATUS_ASSOCIATED)) {
8176                 wstats->qual.qual = 0;
8177                 wstats->qual.level = 0;
8178         } else {
8179                 if (ipw2100_get_ordinal(priv, IPW_ORD_RSSI_AVG_CURR,
8180                                         &rssi, &ord_len))
8181                         goto fail_get_ordinal;
8182                 wstats->qual.level = rssi + IPW2100_RSSI_TO_DBM;
8183                 if (rssi < 10)
8184                         rssi_qual = rssi * POOR / 10;
8185                 else if (rssi < 15)
8186                         rssi_qual = (rssi - 10) * (FAIR - POOR) / 5 + POOR;
8187                 else if (rssi < 20)
8188                         rssi_qual = (rssi - 15) * (GOOD - FAIR) / 5 + FAIR;
8189                 else if (rssi < 30)
8190                         rssi_qual = (rssi - 20) * (VERY_GOOD - GOOD) /
8191                                 10 + GOOD;
8192                 else
8193                         rssi_qual = (rssi - 30) * (PERFECT - VERY_GOOD) /
8194                                 10 + VERY_GOOD;
8195
8196                 if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_PERCENT_RETRIES,
8197                                         &tx_retries, &ord_len))
8198                         goto fail_get_ordinal;
8199
8200                 if (tx_retries > 75)
8201                         tx_qual = (90 - tx_retries) * POOR / 15;
8202                 else if (tx_retries > 70)
8203                         tx_qual = (75 - tx_retries) * (FAIR - POOR) / 5 + POOR;
8204                 else if (tx_retries > 65)
8205                         tx_qual = (70 - tx_retries) * (GOOD - FAIR) / 5 + FAIR;
8206                 else if (tx_retries > 50)
8207                         tx_qual = (65 - tx_retries) * (VERY_GOOD - GOOD) /
8208                                 15 + GOOD;
8209                 else
8210                         tx_qual = (50 - tx_retries) *
8211                                 (PERFECT - VERY_GOOD) / 50 + VERY_GOOD;
8212
8213                 if (missed_beacons > 50)
8214                         beacon_qual = (60 - missed_beacons) * POOR / 10;
8215                 else if (missed_beacons > 40)
8216                         beacon_qual = (50 - missed_beacons) * (FAIR - POOR) /
8217                                 10 + POOR;
8218                 else if (missed_beacons > 32)
8219                         beacon_qual = (40 - missed_beacons) * (GOOD - FAIR) /
8220                                 18 + FAIR;
8221                 else if (missed_beacons > 20)
8222                         beacon_qual = (32 - missed_beacons) *
8223                                 (VERY_GOOD - GOOD) / 20 + GOOD;
8224                 else
8225                         beacon_qual = (20 - missed_beacons) *
8226                                 (PERFECT - VERY_GOOD) / 20 + VERY_GOOD;
8227
8228                 quality = min(beacon_qual, min(tx_qual, rssi_qual));
8229
8230 #ifdef CONFIG_IPW_DEBUG
8231                 if (beacon_qual == quality)
8232                         IPW_DEBUG_WX("Quality clamped by Missed Beacons\n");
8233                 else if (tx_qual == quality)
8234                         IPW_DEBUG_WX("Quality clamped by Tx Retries\n");
8235                 else if (quality != 100)
8236                         IPW_DEBUG_WX("Quality clamped by Signal Strength\n");
8237                 else
8238                         IPW_DEBUG_WX("Quality not clamped.\n");
8239 #endif
8240
8241                 wstats->qual.qual = quality;
8242                 wstats->qual.level = rssi + IPW2100_RSSI_TO_DBM;
8243         }
8244
8245         wstats->qual.noise = 0;
8246         wstats->qual.updated = 7;
8247         wstats->qual.updated |= IW_QUAL_NOISE_INVALID;
8248
8249         /* FIXME: this is percent and not a # */
8250         wstats->miss.beacon = missed_beacons;
8251
8252         if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_TX_FAILURES,
8253                                 &tx_failures, &ord_len))
8254                 goto fail_get_ordinal;
8255         wstats->discard.retries = tx_failures;
8256
8257         return wstats;
8258
8259  fail_get_ordinal:
8260         IPW_DEBUG_WX("failed querying ordinals.\n");
8261
8262         return (struct iw_statistics *) NULL;
8263 }
8264
8265 void ipw2100_wx_event_work(struct ipw2100_priv *priv)
8266 {
8267         union iwreq_data wrqu;
8268         int len = ETH_ALEN;
8269
8270         if (priv->status & STATUS_STOPPING)
8271                 return;
8272
8273         down(&priv->action_sem);
8274
8275         IPW_DEBUG_WX("enter\n");
8276
8277         up(&priv->action_sem);
8278
8279         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
8280
8281         /* Fetch BSSID from the hardware */
8282         if (!(priv->status & (STATUS_ASSOCIATING | STATUS_ASSOCIATED)) ||
8283             priv->status & STATUS_RF_KILL_MASK ||
8284             ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_AP_BSSID,
8285                                 &priv->bssid,  &len)) {
8286                 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
8287         } else {
8288                 /* We now have the BSSID, so can finish setting to the full
8289                  * associated state */
8290                 memcpy(wrqu.ap_addr.sa_data, priv->bssid, ETH_ALEN);
8291                 memcpy(&priv->ieee->bssid, priv->bssid, ETH_ALEN);
8292                 priv->status &= ~STATUS_ASSOCIATING;
8293                 priv->status |= STATUS_ASSOCIATED;
8294                 netif_carrier_on(priv->net_dev);
8295                 if (netif_queue_stopped(priv->net_dev)) {
8296                         IPW_DEBUG_INFO("Waking net queue.\n");
8297                         netif_wake_queue(priv->net_dev);
8298                 } else {
8299                         IPW_DEBUG_INFO("Starting net queue.\n");
8300                         netif_start_queue(priv->net_dev);
8301                 }
8302         }
8303
8304         if (!(priv->status & STATUS_ASSOCIATED)) {
8305                 IPW_DEBUG_WX("Configuring ESSID\n");
8306                 down(&priv->action_sem);
8307                 /* This is a disassociation event, so kick the firmware to
8308                  * look for another AP */
8309                 if (priv->config & CFG_STATIC_ESSID)
8310                         ipw2100_set_essid(priv, priv->essid, priv->essid_len, 0);
8311                 else
8312                         ipw2100_set_essid(priv, NULL, 0, 0);
8313                 up(&priv->action_sem);
8314         }
8315
8316         wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
8317 }
8318
8319 #define IPW2100_FW_MAJOR_VERSION 1
8320 #define IPW2100_FW_MINOR_VERSION 3
8321
8322 #define IPW2100_FW_MINOR(x) ((x & 0xff) >> 8)
8323 #define IPW2100_FW_MAJOR(x) (x & 0xff)
8324
8325 #define IPW2100_FW_VERSION ((IPW2100_FW_MINOR_VERSION << 8) | \
8326                              IPW2100_FW_MAJOR_VERSION)
8327
8328 #define IPW2100_FW_PREFIX "ipw2100-" __stringify(IPW2100_FW_MAJOR_VERSION) \
8329 "." __stringify(IPW2100_FW_MINOR_VERSION)
8330
8331 #define IPW2100_FW_NAME(x) IPW2100_FW_PREFIX "" x ".fw"
8332
8333
8334 /*
8335
8336 BINARY FIRMWARE HEADER FORMAT
8337
8338 offset      length   desc
8339 0           2        version
8340 2           2        mode == 0:BSS,1:IBSS,2:MONITOR
8341 4           4        fw_len
8342 8           4        uc_len
8343 C           fw_len   firmware data
8344 12 + fw_len uc_len   microcode data
8345
8346 */
8347
8348 struct ipw2100_fw_header {
8349         short version;
8350         short mode;
8351         unsigned int fw_size;
8352         unsigned int uc_size;
8353 } __attribute__ ((packed));
8354
8355
8356
8357 static int ipw2100_mod_firmware_load(struct ipw2100_fw *fw)
8358 {
8359         struct ipw2100_fw_header *h =
8360                 (struct ipw2100_fw_header *)fw->fw_entry->data;
8361
8362         if (IPW2100_FW_MAJOR(h->version) != IPW2100_FW_MAJOR_VERSION) {
8363                 IPW_DEBUG_WARNING("Firmware image not compatible "
8364                        "(detected version id of %u). "
8365                        "See Documentation/networking/README.ipw2100\n",
8366                        h->version);
8367                 return 1;
8368         }
8369
8370         fw->version = h->version;
8371         fw->fw.data = fw->fw_entry->data + sizeof(struct ipw2100_fw_header);
8372         fw->fw.size = h->fw_size;
8373         fw->uc.data = fw->fw.data + h->fw_size;
8374         fw->uc.size = h->uc_size;
8375
8376         return 0;
8377 }
8378
8379
8380 int ipw2100_get_firmware(struct ipw2100_priv *priv, struct ipw2100_fw *fw)
8381 {
8382         char *fw_name;
8383         int rc;
8384
8385         IPW_DEBUG_INFO("%s: Using hotplug firmware load.\n",
8386                priv->net_dev->name);
8387
8388         switch (priv->ieee->iw_mode) {
8389         case IW_MODE_ADHOC:
8390                 fw_name = IPW2100_FW_NAME("-i");
8391                 break;
8392 #ifdef CONFIG_IPW2100_MONITOR
8393         case IW_MODE_MONITOR:
8394                 fw_name = IPW2100_FW_NAME("-p");
8395                 break;
8396 #endif
8397         case IW_MODE_INFRA:
8398         default:
8399                 fw_name = IPW2100_FW_NAME("");
8400                 break;
8401         }
8402
8403         rc = request_firmware(&fw->fw_entry, fw_name, &priv->pci_dev->dev);
8404
8405         if (rc < 0) {
8406                 IPW_DEBUG_ERROR(
8407                        "%s: Firmware '%s' not available or load failed.\n",
8408                        priv->net_dev->name, fw_name);
8409                 return rc;
8410         }
8411         IPW_DEBUG_INFO("firmware data %p size %zd\n", fw->fw_entry->data,
8412                            fw->fw_entry->size);
8413
8414         ipw2100_mod_firmware_load(fw);
8415
8416         return 0;
8417 }
8418
8419 void ipw2100_release_firmware(struct ipw2100_priv *priv,
8420                               struct ipw2100_fw *fw)
8421 {
8422         fw->version = 0;
8423         if (fw->fw_entry)
8424                 release_firmware(fw->fw_entry);
8425         fw->fw_entry = NULL;
8426 }
8427
8428
8429 int ipw2100_get_fwversion(struct ipw2100_priv *priv, char *buf, size_t max)
8430 {
8431         char ver[MAX_FW_VERSION_LEN];
8432         u32 len = MAX_FW_VERSION_LEN;
8433         u32 tmp;
8434         int i;
8435         /* firmware version is an ascii string (max len of 14) */
8436         if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_FW_VER_NUM,
8437                                 ver, &len))
8438                 return -EIO;
8439         tmp = max;
8440         if (len >= max)
8441                 len = max - 1;
8442         for (i = 0; i < len; i++)
8443                 buf[i] = ver[i];
8444         buf[i] = '\0';
8445         return tmp;
8446 }
8447
8448 int ipw2100_get_ucodeversion(struct ipw2100_priv *priv, char *buf, size_t max)
8449 {
8450         u32 ver;
8451         u32 len = sizeof(ver);
8452         /* microcode version is a 32 bit integer */
8453         if (ipw2100_get_ordinal(priv, IPW_ORD_UCODE_VERSION,
8454                                 &ver, &len))
8455                 return -EIO;
8456         return snprintf(buf, max, "%08X", ver);
8457 }
8458
8459 /*
8460  * On exit, the firmware will have been freed from the fw list
8461  */
8462 int ipw2100_fw_download(struct ipw2100_priv *priv, struct ipw2100_fw *fw)
8463 {
8464         /* firmware is constructed of N contiguous entries, each entry is
8465          * structured as:
8466          *
8467          * offset    sie         desc
8468          * 0         4           address to write to
8469          * 4         2           length of data run
8470          * 6         length      data
8471          */
8472         unsigned int addr;
8473         unsigned short len;
8474
8475         const unsigned char *firmware_data = fw->fw.data;
8476         unsigned int firmware_data_left = fw->fw.size;
8477
8478         while (firmware_data_left > 0) {
8479                 addr = *(u32 *)(firmware_data);
8480                 firmware_data      += 4;
8481                 firmware_data_left -= 4;
8482
8483                 len = *(u16 *)(firmware_data);
8484                 firmware_data      += 2;
8485                 firmware_data_left -= 2;
8486
8487                 if (len > 32) {
8488                         IPW_DEBUG_ERROR(
8489                                "Invalid firmware run-length of %d bytes\n",
8490                                len);
8491                         return -EINVAL;
8492                 }
8493
8494                 write_nic_memory(priv->net_dev, addr, len, firmware_data);
8495                 firmware_data      += len;
8496                 firmware_data_left -= len;
8497         }
8498
8499         return 0;
8500 }
8501
8502 struct symbol_alive_response {
8503         u8 cmd_id;
8504         u8 seq_num;
8505         u8 ucode_rev;
8506         u8 eeprom_valid;
8507         u16 valid_flags;
8508         u8 IEEE_addr[6];
8509         u16 flags;
8510         u16 pcb_rev;
8511         u16 clock_settle_time;  // 1us LSB
8512         u16 powerup_settle_time;        // 1us LSB
8513         u16 hop_settle_time;    // 1us LSB
8514         u8 date[3];             // month, day, year
8515         u8 time[2];             // hours, minutes
8516         u8 ucode_valid;
8517 };
8518
8519 int ipw2100_ucode_download(struct ipw2100_priv *priv, struct ipw2100_fw *fw)
8520 {
8521         struct net_device *dev = priv->net_dev;
8522         const unsigned char *microcode_data = fw->uc.data;
8523         unsigned int microcode_data_left = fw->uc.size;
8524
8525         struct symbol_alive_response response;
8526         int i, j;
8527         u8 data;
8528
8529         /* Symbol control */
8530         write_nic_word(dev, IPW2100_CONTROL_REG, 0x703);
8531         readl((void *)(dev->base_addr));
8532         write_nic_word(dev, IPW2100_CONTROL_REG, 0x707);
8533         readl((void *)(dev->base_addr));
8534
8535         /* HW config */
8536         write_nic_byte(dev, 0x210014, 0x72);    /* fifo width =16 */
8537         readl((void *)(dev->base_addr));
8538         write_nic_byte(dev, 0x210014, 0x72);    /* fifo width =16 */
8539         readl((void *)(dev->base_addr));
8540
8541         /* EN_CS_ACCESS bit to reset control store pointer */
8542         write_nic_byte(dev, 0x210000, 0x40);
8543         readl((void *)(dev->base_addr));
8544         write_nic_byte(dev, 0x210000, 0x0);
8545         readl((void *)(dev->base_addr));
8546         write_nic_byte(dev, 0x210000, 0x40);
8547         readl((void *)(dev->base_addr));
8548
8549         /* copy microcode from buffer into Symbol */
8550
8551         while (microcode_data_left > 0) {
8552                 write_nic_byte(dev, 0x210010, *microcode_data++);
8553                 write_nic_byte(dev, 0x210010, *microcode_data++);
8554                 microcode_data_left -= 2;
8555         }
8556
8557         /* EN_CS_ACCESS bit to reset the control store pointer */
8558         write_nic_byte(dev, 0x210000, 0x0);
8559         readl((void *)(dev->base_addr));
8560
8561         /* Enable System (Reg 0)
8562          * first enable causes garbage in RX FIFO */
8563         write_nic_byte(dev, 0x210000, 0x0);
8564         readl((void *)(dev->base_addr));
8565         write_nic_byte(dev, 0x210000, 0x80);
8566         readl((void *)(dev->base_addr));
8567
8568         /* Reset External Baseband Reg */
8569         write_nic_word(dev, IPW2100_CONTROL_REG, 0x703);
8570         readl((void *)(dev->base_addr));
8571         write_nic_word(dev, IPW2100_CONTROL_REG, 0x707);
8572         readl((void *)(dev->base_addr));
8573
8574         /* HW Config (Reg 5) */
8575         write_nic_byte(dev, 0x210014, 0x72);    // fifo width =16
8576         readl((void *)(dev->base_addr));
8577         write_nic_byte(dev, 0x210014, 0x72);    // fifo width =16
8578         readl((void *)(dev->base_addr));
8579
8580         /* Enable System (Reg 0)
8581          * second enable should be OK */
8582         write_nic_byte(dev, 0x210000, 0x00);    // clear enable system
8583         readl((void *)(dev->base_addr));
8584         write_nic_byte(dev, 0x210000, 0x80);    // set enable system
8585
8586         /* check Symbol is enabled - upped this from 5 as it wasn't always
8587          * catching the update */
8588         for (i = 0; i < 10; i++) {
8589                 udelay(10);
8590
8591                 /* check Dino is enabled bit */
8592                 read_nic_byte(dev, 0x210000, &data);
8593                 if (data & 0x1)
8594                         break;
8595         }
8596
8597         if (i == 10) {
8598                 IPW_DEBUG_ERROR("%s: Error initializing Symbol\n",
8599                        dev->name);
8600                 return -EIO;
8601         }
8602
8603         /* Get Symbol alive response */
8604         for (i = 0; i < 30; i++) {
8605                 /* Read alive response structure */
8606                 for (j = 0;
8607                      j < (sizeof(struct symbol_alive_response) >> 1);
8608                      j++)
8609                         read_nic_word(dev, 0x210004,
8610                                       ((u16 *)&response) + j);
8611
8612                 if ((response.cmd_id == 1) &&
8613                     (response.ucode_valid == 0x1))
8614                         break;
8615                 udelay(10);
8616         }
8617
8618         if (i == 30) {
8619                 IPW_DEBUG_ERROR("%s: No response from Symbol - hw not alive\n",
8620                        dev->name);
8621                 printk_buf(IPW_DL_ERROR, (u8*)&response, sizeof(response));
8622                 return -EIO;
8623         }
8624
8625         return 0;
8626 }