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