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