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