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