ipw2x00: fix sparse warnings
[pandora-kernel.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   James P. Ketrenos <ipw2100-admin@linux.intel.com>
23   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24
25   Portions of this file are based on the sample_* files provided by Wireless
26   Extensions 0.26 package and copyright (c) 1997-2003 Jean Tourrilhes
27   <jt@hpl.hp.com>
28
29   Portions of this file are based on the Host AP project,
30   Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
31     <j@w1.fi>
32   Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
33
34   Portions of ipw2100_mod_firmware_load, ipw2100_do_mod_firmware_load, and
35   ipw2100_fw_load are loosely based on drivers/sound/sound_firmware.c
36   available in the 2.4.25 kernel sources, and are copyright (c) Alan Cox
37
38 ******************************************************************************/
39 /*
40
41  Initial driver on which this is based was developed by Janusz Gorycki,
42  Maciej Urbaniak, and Maciej Sosnowski.
43
44  Promiscuous mode support added by Jacek Wysoczynski and Maciej Urbaniak.
45
46 Theory of Operation
47
48 Tx - Commands and Data
49
50 Firmware and host share a circular queue of Transmit Buffer Descriptors (TBDs)
51 Each TBD contains a pointer to the physical (dma_addr_t) address of data being
52 sent to the firmware as well as the length of the data.
53
54 The host writes to the TBD queue at the WRITE index.  The WRITE index points
55 to the _next_ packet to be written and is advanced when after the TBD has been
56 filled.
57
58 The firmware pulls from the TBD queue at the READ index.  The READ index points
59 to the currently being read entry, and is advanced once the firmware is
60 done with a packet.
61
62 When data is sent to the firmware, the first TBD is used to indicate to the
63 firmware if a Command or Data is being sent.  If it is Command, all of the
64 command information is contained within the physical address referred to by the
65 TBD.  If it is Data, the first TBD indicates the type of data packet, number
66 of fragments, etc.  The next TBD then referrs to the actual packet location.
67
68 The Tx flow cycle is as follows:
69
70 1) ipw2100_tx() is called by kernel with SKB to transmit
71 2) Packet is move from the tx_free_list and appended to the transmit pending
72    list (tx_pend_list)
73 3) work is scheduled to move pending packets into the shared circular queue.
74 4) when placing packet in the circular queue, the incoming SKB is DMA mapped
75    to a physical address.  That address is entered into a TBD.  Two TBDs are
76    filled out.  The first indicating a data packet, the second referring to the
77    actual payload data.
78 5) the packet is removed from tx_pend_list and placed on the end of the
79    firmware pending list (fw_pend_list)
80 6) firmware is notified that the WRITE index has
81 7) Once the firmware has processed the TBD, INTA is triggered.
82 8) For each Tx interrupt received from the firmware, the READ index is checked
83    to see which TBDs are done being processed.
84 9) For each TBD that has been processed, the ISR pulls the oldest packet
85    from the fw_pend_list.
86 10)The packet structure contained in the fw_pend_list is then used
87    to unmap the DMA address and to free the SKB originally passed to the driver
88    from the kernel.
89 11)The packet structure is placed onto the tx_free_list
90
91 The above steps are the same for commands, only the msg_free_list/msg_pend_list
92 are used instead of tx_free_list/tx_pend_list
93
94 ...
95
96 Critical Sections / Locking :
97
98 There are two locks utilized.  The first is the low level lock (priv->low_lock)
99 that protects the following:
100
101 - Access to the Tx/Rx queue lists via priv->low_lock. The lists are as follows:
102
103   tx_free_list : Holds pre-allocated Tx buffers.
104     TAIL modified in __ipw2100_tx_process()
105     HEAD modified in ipw2100_tx()
106
107   tx_pend_list : Holds used Tx buffers waiting to go into the TBD ring
108     TAIL modified ipw2100_tx()
109     HEAD modified by ipw2100_tx_send_data()
110
111   msg_free_list : Holds pre-allocated Msg (Command) buffers
112     TAIL modified in __ipw2100_tx_process()
113     HEAD modified in ipw2100_hw_send_command()
114
115   msg_pend_list : Holds used Msg buffers waiting to go into the TBD ring
116     TAIL modified in ipw2100_hw_send_command()
117     HEAD modified in ipw2100_tx_send_commands()
118
119   The flow of data on the TX side is as follows:
120
121   MSG_FREE_LIST + COMMAND => MSG_PEND_LIST => TBD => MSG_FREE_LIST
122   TX_FREE_LIST + DATA => TX_PEND_LIST => TBD => TX_FREE_LIST
123
124   The methods that work on the TBD ring are protected via priv->low_lock.
125
126 - The internal data state of the device itself
127 - Access to the firmware read/write indexes for the BD queues
128   and associated logic
129
130 All external entry functions are locked with the priv->action_lock to ensure
131 that only one external action is invoked at a time.
132
133
134 */
135
136 #include <linux/compiler.h>
137 #include <linux/errno.h>
138 #include <linux/if_arp.h>
139 #include <linux/in6.h>
140 #include <linux/in.h>
141 #include <linux/ip.h>
142 #include <linux/kernel.h>
143 #include <linux/kmod.h>
144 #include <linux/module.h>
145 #include <linux/netdevice.h>
146 #include <linux/ethtool.h>
147 #include <linux/pci.h>
148 #include <linux/dma-mapping.h>
149 #include <linux/proc_fs.h>
150 #include <linux/skbuff.h>
151 #include <asm/uaccess.h>
152 #include <asm/io.h>
153 #include <linux/fs.h>
154 #include <linux/mm.h>
155 #include <linux/slab.h>
156 #include <linux/unistd.h>
157 #include <linux/stringify.h>
158 #include <linux/tcp.h>
159 #include <linux/types.h>
160 #include <linux/time.h>
161 #include <linux/firmware.h>
162 #include <linux/acpi.h>
163 #include <linux/ctype.h>
164 #include <linux/pm_qos_params.h>
165
166 #include <net/lib80211.h>
167
168 #include "ipw2100.h"
169
170 #define IPW2100_VERSION "git-1.2.2"
171
172 #define DRV_NAME        "ipw2100"
173 #define DRV_VERSION     IPW2100_VERSION
174 #define DRV_DESCRIPTION "Intel(R) PRO/Wireless 2100 Network Driver"
175 #define DRV_COPYRIGHT   "Copyright(c) 2003-2006 Intel Corporation"
176
177 /* Debugging stuff */
178 #ifdef CONFIG_IPW2100_DEBUG
179 #define IPW2100_RX_DEBUG        /* Reception debugging */
180 #endif
181
182 MODULE_DESCRIPTION(DRV_DESCRIPTION);
183 MODULE_VERSION(DRV_VERSION);
184 MODULE_AUTHOR(DRV_COPYRIGHT);
185 MODULE_LICENSE("GPL");
186
187 static int debug = 0;
188 static int network_mode = 0;
189 static int channel = 0;
190 static int associate = 0;
191 static int disable = 0;
192 #ifdef CONFIG_PM
193 static struct ipw2100_fw ipw2100_firmware;
194 #endif
195
196 #include <linux/moduleparam.h>
197 module_param(debug, int, 0444);
198 module_param_named(mode, network_mode, int, 0444);
199 module_param(channel, int, 0444);
200 module_param(associate, int, 0444);
201 module_param(disable, int, 0444);
202
203 MODULE_PARM_DESC(debug, "debug level");
204 MODULE_PARM_DESC(mode, "network mode (0=BSS,1=IBSS,2=Monitor)");
205 MODULE_PARM_DESC(channel, "channel");
206 MODULE_PARM_DESC(associate, "auto associate when scanning (default off)");
207 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
208
209 static u32 ipw2100_debug_level = IPW_DL_NONE;
210
211 #ifdef CONFIG_IPW2100_DEBUG
212 #define IPW_DEBUG(level, message...) \
213 do { \
214         if (ipw2100_debug_level & (level)) { \
215                 printk(KERN_DEBUG "ipw2100: %c %s ", \
216                        in_interrupt() ? 'I' : 'U',  __func__); \
217                 printk(message); \
218         } \
219 } while (0)
220 #else
221 #define IPW_DEBUG(level, message...) do {} while (0)
222 #endif                          /* CONFIG_IPW2100_DEBUG */
223
224 #ifdef CONFIG_IPW2100_DEBUG
225 static const char *command_types[] = {
226         "undefined",
227         "unused",               /* HOST_ATTENTION */
228         "HOST_COMPLETE",
229         "unused",               /* SLEEP */
230         "unused",               /* HOST_POWER_DOWN */
231         "unused",
232         "SYSTEM_CONFIG",
233         "unused",               /* SET_IMR */
234         "SSID",
235         "MANDATORY_BSSID",
236         "AUTHENTICATION_TYPE",
237         "ADAPTER_ADDRESS",
238         "PORT_TYPE",
239         "INTERNATIONAL_MODE",
240         "CHANNEL",
241         "RTS_THRESHOLD",
242         "FRAG_THRESHOLD",
243         "POWER_MODE",
244         "TX_RATES",
245         "BASIC_TX_RATES",
246         "WEP_KEY_INFO",
247         "unused",
248         "unused",
249         "unused",
250         "unused",
251         "WEP_KEY_INDEX",
252         "WEP_FLAGS",
253         "ADD_MULTICAST",
254         "CLEAR_ALL_MULTICAST",
255         "BEACON_INTERVAL",
256         "ATIM_WINDOW",
257         "CLEAR_STATISTICS",
258         "undefined",
259         "undefined",
260         "undefined",
261         "undefined",
262         "TX_POWER_INDEX",
263         "undefined",
264         "undefined",
265         "undefined",
266         "undefined",
267         "undefined",
268         "undefined",
269         "BROADCAST_SCAN",
270         "CARD_DISABLE",
271         "PREFERRED_BSSID",
272         "SET_SCAN_OPTIONS",
273         "SCAN_DWELL_TIME",
274         "SWEEP_TABLE",
275         "AP_OR_STATION_TABLE",
276         "GROUP_ORDINALS",
277         "SHORT_RETRY_LIMIT",
278         "LONG_RETRY_LIMIT",
279         "unused",               /* SAVE_CALIBRATION */
280         "unused",               /* RESTORE_CALIBRATION */
281         "undefined",
282         "undefined",
283         "undefined",
284         "HOST_PRE_POWER_DOWN",
285         "unused",               /* HOST_INTERRUPT_COALESCING */
286         "undefined",
287         "CARD_DISABLE_PHY_OFF",
288         "MSDU_TX_RATES" "undefined",
289         "undefined",
290         "SET_STATION_STAT_BITS",
291         "CLEAR_STATIONS_STAT_BITS",
292         "LEAP_ROGUE_MODE",
293         "SET_SECURITY_INFORMATION",
294         "DISASSOCIATION_BSSID",
295         "SET_WPA_ASS_IE"
296 };
297 #endif
298
299 /* Pre-decl until we get the code solid and then we can clean it up */
300 static void ipw2100_tx_send_commands(struct ipw2100_priv *priv);
301 static void ipw2100_tx_send_data(struct ipw2100_priv *priv);
302 static int ipw2100_adapter_setup(struct ipw2100_priv *priv);
303
304 static void ipw2100_queues_initialize(struct ipw2100_priv *priv);
305 static void ipw2100_queues_free(struct ipw2100_priv *priv);
306 static int ipw2100_queues_allocate(struct ipw2100_priv *priv);
307
308 static int ipw2100_fw_download(struct ipw2100_priv *priv,
309                                struct ipw2100_fw *fw);
310 static int ipw2100_get_firmware(struct ipw2100_priv *priv,
311                                 struct ipw2100_fw *fw);
312 static int ipw2100_get_fwversion(struct ipw2100_priv *priv, char *buf,
313                                  size_t max);
314 static int ipw2100_get_ucodeversion(struct ipw2100_priv *priv, char *buf,
315                                     size_t max);
316 static void ipw2100_release_firmware(struct ipw2100_priv *priv,
317                                      struct ipw2100_fw *fw);
318 static int ipw2100_ucode_download(struct ipw2100_priv *priv,
319                                   struct ipw2100_fw *fw);
320 static void ipw2100_wx_event_work(struct work_struct *work);
321 static struct iw_statistics *ipw2100_wx_wireless_stats(struct net_device *dev);
322 static struct iw_handler_def ipw2100_wx_handler_def;
323
324 static inline void read_register(struct net_device *dev, u32 reg, u32 * val)
325 {
326         *val = readl((void __iomem *)(dev->base_addr + reg));
327         IPW_DEBUG_IO("r: 0x%08X => 0x%08X\n", reg, *val);
328 }
329
330 static inline void write_register(struct net_device *dev, u32 reg, u32 val)
331 {
332         writel(val, (void __iomem *)(dev->base_addr + reg));
333         IPW_DEBUG_IO("w: 0x%08X <= 0x%08X\n", reg, val);
334 }
335
336 static inline void read_register_word(struct net_device *dev, u32 reg,
337                                       u16 * val)
338 {
339         *val = readw((void __iomem *)(dev->base_addr + reg));
340         IPW_DEBUG_IO("r: 0x%08X => %04X\n", reg, *val);
341 }
342
343 static inline void read_register_byte(struct net_device *dev, u32 reg, u8 * val)
344 {
345         *val = readb((void __iomem *)(dev->base_addr + reg));
346         IPW_DEBUG_IO("r: 0x%08X => %02X\n", reg, *val);
347 }
348
349 static inline void write_register_word(struct net_device *dev, u32 reg, u16 val)
350 {
351         writew(val, (void __iomem *)(dev->base_addr + reg));
352         IPW_DEBUG_IO("w: 0x%08X <= %04X\n", reg, val);
353 }
354
355 static inline void write_register_byte(struct net_device *dev, u32 reg, u8 val)
356 {
357         writeb(val, (void __iomem *)(dev->base_addr + reg));
358         IPW_DEBUG_IO("w: 0x%08X =< %02X\n", reg, val);
359 }
360
361 static inline void read_nic_dword(struct net_device *dev, u32 addr, u32 * val)
362 {
363         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
364                        addr & IPW_REG_INDIRECT_ADDR_MASK);
365         read_register(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
366 }
367
368 static inline void write_nic_dword(struct net_device *dev, u32 addr, u32 val)
369 {
370         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
371                        addr & IPW_REG_INDIRECT_ADDR_MASK);
372         write_register(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
373 }
374
375 static inline void read_nic_word(struct net_device *dev, u32 addr, u16 * val)
376 {
377         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
378                        addr & IPW_REG_INDIRECT_ADDR_MASK);
379         read_register_word(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
380 }
381
382 static inline void write_nic_word(struct net_device *dev, u32 addr, u16 val)
383 {
384         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
385                        addr & IPW_REG_INDIRECT_ADDR_MASK);
386         write_register_word(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
387 }
388
389 static inline void read_nic_byte(struct net_device *dev, u32 addr, u8 * val)
390 {
391         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
392                        addr & IPW_REG_INDIRECT_ADDR_MASK);
393         read_register_byte(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
394 }
395
396 static inline void write_nic_byte(struct net_device *dev, u32 addr, u8 val)
397 {
398         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
399                        addr & IPW_REG_INDIRECT_ADDR_MASK);
400         write_register_byte(dev, IPW_REG_INDIRECT_ACCESS_DATA, val);
401 }
402
403 static inline void write_nic_auto_inc_address(struct net_device *dev, u32 addr)
404 {
405         write_register(dev, IPW_REG_AUTOINCREMENT_ADDRESS,
406                        addr & IPW_REG_INDIRECT_ADDR_MASK);
407 }
408
409 static inline void write_nic_dword_auto_inc(struct net_device *dev, u32 val)
410 {
411         write_register(dev, IPW_REG_AUTOINCREMENT_DATA, val);
412 }
413
414 static void write_nic_memory(struct net_device *dev, u32 addr, u32 len,
415                                     const u8 * buf)
416 {
417         u32 aligned_addr;
418         u32 aligned_len;
419         u32 dif_len;
420         u32 i;
421
422         /* read first nibble byte by byte */
423         aligned_addr = addr & (~0x3);
424         dif_len = addr - aligned_addr;
425         if (dif_len) {
426                 /* Start reading at aligned_addr + dif_len */
427                 write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
428                                aligned_addr);
429                 for (i = dif_len; i < 4; i++, buf++)
430                         write_register_byte(dev,
431                                             IPW_REG_INDIRECT_ACCESS_DATA + i,
432                                             *buf);
433
434                 len -= dif_len;
435                 aligned_addr += 4;
436         }
437
438         /* read DWs through autoincrement registers */
439         write_register(dev, IPW_REG_AUTOINCREMENT_ADDRESS, aligned_addr);
440         aligned_len = len & (~0x3);
441         for (i = 0; i < aligned_len; i += 4, buf += 4, aligned_addr += 4)
442                 write_register(dev, IPW_REG_AUTOINCREMENT_DATA, *(u32 *) buf);
443
444         /* copy the last nibble */
445         dif_len = len - aligned_len;
446         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS, aligned_addr);
447         for (i = 0; i < dif_len; i++, buf++)
448                 write_register_byte(dev, IPW_REG_INDIRECT_ACCESS_DATA + i,
449                                     *buf);
450 }
451
452 static void read_nic_memory(struct net_device *dev, u32 addr, u32 len,
453                                    u8 * buf)
454 {
455         u32 aligned_addr;
456         u32 aligned_len;
457         u32 dif_len;
458         u32 i;
459
460         /* read first nibble byte by byte */
461         aligned_addr = addr & (~0x3);
462         dif_len = addr - aligned_addr;
463         if (dif_len) {
464                 /* Start reading at aligned_addr + dif_len */
465                 write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS,
466                                aligned_addr);
467                 for (i = dif_len; i < 4; i++, buf++)
468                         read_register_byte(dev,
469                                            IPW_REG_INDIRECT_ACCESS_DATA + i,
470                                            buf);
471
472                 len -= dif_len;
473                 aligned_addr += 4;
474         }
475
476         /* read DWs through autoincrement registers */
477         write_register(dev, IPW_REG_AUTOINCREMENT_ADDRESS, aligned_addr);
478         aligned_len = len & (~0x3);
479         for (i = 0; i < aligned_len; i += 4, buf += 4, aligned_addr += 4)
480                 read_register(dev, IPW_REG_AUTOINCREMENT_DATA, (u32 *) buf);
481
482         /* copy the last nibble */
483         dif_len = len - aligned_len;
484         write_register(dev, IPW_REG_INDIRECT_ACCESS_ADDRESS, aligned_addr);
485         for (i = 0; i < dif_len; i++, buf++)
486                 read_register_byte(dev, IPW_REG_INDIRECT_ACCESS_DATA + i, buf);
487 }
488
489 static inline int ipw2100_hw_is_adapter_in_system(struct net_device *dev)
490 {
491         return (dev->base_addr &&
492                 (readl
493                  ((void __iomem *)(dev->base_addr +
494                                    IPW_REG_DOA_DEBUG_AREA_START))
495                  == IPW_DATA_DOA_DEBUG_VALUE));
496 }
497
498 static int ipw2100_get_ordinal(struct ipw2100_priv *priv, u32 ord,
499                                void *val, u32 * len)
500 {
501         struct ipw2100_ordinals *ordinals = &priv->ordinals;
502         u32 addr;
503         u32 field_info;
504         u16 field_len;
505         u16 field_count;
506         u32 total_length;
507
508         if (ordinals->table1_addr == 0) {
509                 printk(KERN_WARNING DRV_NAME ": attempt to use fw ordinals "
510                        "before they have been loaded.\n");
511                 return -EINVAL;
512         }
513
514         if (IS_ORDINAL_TABLE_ONE(ordinals, ord)) {
515                 if (*len < IPW_ORD_TAB_1_ENTRY_SIZE) {
516                         *len = IPW_ORD_TAB_1_ENTRY_SIZE;
517
518                         printk(KERN_WARNING DRV_NAME
519                                ": ordinal buffer length too small, need %zd\n",
520                                IPW_ORD_TAB_1_ENTRY_SIZE);
521
522                         return -EINVAL;
523                 }
524
525                 read_nic_dword(priv->net_dev,
526                                ordinals->table1_addr + (ord << 2), &addr);
527                 read_nic_dword(priv->net_dev, addr, val);
528
529                 *len = IPW_ORD_TAB_1_ENTRY_SIZE;
530
531                 return 0;
532         }
533
534         if (IS_ORDINAL_TABLE_TWO(ordinals, ord)) {
535
536                 ord -= IPW_START_ORD_TAB_2;
537
538                 /* get the address of statistic */
539                 read_nic_dword(priv->net_dev,
540                                ordinals->table2_addr + (ord << 3), &addr);
541
542                 /* get the second DW of statistics ;
543                  * two 16-bit words - first is length, second is count */
544                 read_nic_dword(priv->net_dev,
545                                ordinals->table2_addr + (ord << 3) + sizeof(u32),
546                                &field_info);
547
548                 /* get each entry length */
549                 field_len = *((u16 *) & field_info);
550
551                 /* get number of entries */
552                 field_count = *(((u16 *) & field_info) + 1);
553
554                 /* abort if no enought memory */
555                 total_length = field_len * field_count;
556                 if (total_length > *len) {
557                         *len = total_length;
558                         return -EINVAL;
559                 }
560
561                 *len = total_length;
562                 if (!total_length)
563                         return 0;
564
565                 /* read the ordinal data from the SRAM */
566                 read_nic_memory(priv->net_dev, addr, total_length, val);
567
568                 return 0;
569         }
570
571         printk(KERN_WARNING DRV_NAME ": ordinal %d neither in table 1 nor "
572                "in table 2\n", ord);
573
574         return -EINVAL;
575 }
576
577 static int ipw2100_set_ordinal(struct ipw2100_priv *priv, u32 ord, u32 * val,
578                                u32 * len)
579 {
580         struct ipw2100_ordinals *ordinals = &priv->ordinals;
581         u32 addr;
582
583         if (IS_ORDINAL_TABLE_ONE(ordinals, ord)) {
584                 if (*len != IPW_ORD_TAB_1_ENTRY_SIZE) {
585                         *len = IPW_ORD_TAB_1_ENTRY_SIZE;
586                         IPW_DEBUG_INFO("wrong size\n");
587                         return -EINVAL;
588                 }
589
590                 read_nic_dword(priv->net_dev,
591                                ordinals->table1_addr + (ord << 2), &addr);
592
593                 write_nic_dword(priv->net_dev, addr, *val);
594
595                 *len = IPW_ORD_TAB_1_ENTRY_SIZE;
596
597                 return 0;
598         }
599
600         IPW_DEBUG_INFO("wrong table\n");
601         if (IS_ORDINAL_TABLE_TWO(ordinals, ord))
602                 return -EINVAL;
603
604         return -EINVAL;
605 }
606
607 static char *snprint_line(char *buf, size_t count,
608                           const u8 * data, u32 len, u32 ofs)
609 {
610         int out, i, j, l;
611         char c;
612
613         out = snprintf(buf, count, "%08X", ofs);
614
615         for (l = 0, i = 0; i < 2; i++) {
616                 out += snprintf(buf + out, count - out, " ");
617                 for (j = 0; j < 8 && l < len; j++, l++)
618                         out += snprintf(buf + out, count - out, "%02X ",
619                                         data[(i * 8 + j)]);
620                 for (; j < 8; j++)
621                         out += snprintf(buf + out, count - out, "   ");
622         }
623
624         out += snprintf(buf + out, count - out, " ");
625         for (l = 0, i = 0; i < 2; i++) {
626                 out += snprintf(buf + out, count - out, " ");
627                 for (j = 0; j < 8 && l < len; j++, l++) {
628                         c = data[(i * 8 + j)];
629                         if (!isascii(c) || !isprint(c))
630                                 c = '.';
631
632                         out += snprintf(buf + out, count - out, "%c", c);
633                 }
634
635                 for (; j < 8; j++)
636                         out += snprintf(buf + out, count - out, " ");
637         }
638
639         return buf;
640 }
641
642 static void printk_buf(int level, const u8 * data, u32 len)
643 {
644         char line[81];
645         u32 ofs = 0;
646         if (!(ipw2100_debug_level & level))
647                 return;
648
649         while (len) {
650                 printk(KERN_DEBUG "%s\n",
651                        snprint_line(line, sizeof(line), &data[ofs],
652                                     min(len, 16U), ofs));
653                 ofs += 16;
654                 len -= min(len, 16U);
655         }
656 }
657
658 #define MAX_RESET_BACKOFF 10
659
660 static void schedule_reset(struct ipw2100_priv *priv)
661 {
662         unsigned long now = get_seconds();
663
664         /* If we haven't received a reset request within the backoff period,
665          * then we can reset the backoff interval so this reset occurs
666          * immediately */
667         if (priv->reset_backoff &&
668             (now - priv->last_reset > priv->reset_backoff))
669                 priv->reset_backoff = 0;
670
671         priv->last_reset = get_seconds();
672
673         if (!(priv->status & STATUS_RESET_PENDING)) {
674                 IPW_DEBUG_INFO("%s: Scheduling firmware restart (%ds).\n",
675                                priv->net_dev->name, priv->reset_backoff);
676                 netif_carrier_off(priv->net_dev);
677                 netif_stop_queue(priv->net_dev);
678                 priv->status |= STATUS_RESET_PENDING;
679                 if (priv->reset_backoff)
680                         queue_delayed_work(priv->workqueue, &priv->reset_work,
681                                            priv->reset_backoff * HZ);
682                 else
683                         queue_delayed_work(priv->workqueue, &priv->reset_work,
684                                            0);
685
686                 if (priv->reset_backoff < MAX_RESET_BACKOFF)
687                         priv->reset_backoff++;
688
689                 wake_up_interruptible(&priv->wait_command_queue);
690         } else
691                 IPW_DEBUG_INFO("%s: Firmware restart already in progress.\n",
692                                priv->net_dev->name);
693
694 }
695
696 #define HOST_COMPLETE_TIMEOUT (2 * HZ)
697 static int ipw2100_hw_send_command(struct ipw2100_priv *priv,
698                                    struct host_command *cmd)
699 {
700         struct list_head *element;
701         struct ipw2100_tx_packet *packet;
702         unsigned long flags;
703         int err = 0;
704
705         IPW_DEBUG_HC("Sending %s command (#%d), %d bytes\n",
706                      command_types[cmd->host_command], cmd->host_command,
707                      cmd->host_command_length);
708         printk_buf(IPW_DL_HC, (u8 *) cmd->host_command_parameters,
709                    cmd->host_command_length);
710
711         spin_lock_irqsave(&priv->low_lock, flags);
712
713         if (priv->fatal_error) {
714                 IPW_DEBUG_INFO
715                     ("Attempt to send command while hardware in fatal error condition.\n");
716                 err = -EIO;
717                 goto fail_unlock;
718         }
719
720         if (!(priv->status & STATUS_RUNNING)) {
721                 IPW_DEBUG_INFO
722                     ("Attempt to send command while hardware is not running.\n");
723                 err = -EIO;
724                 goto fail_unlock;
725         }
726
727         if (priv->status & STATUS_CMD_ACTIVE) {
728                 IPW_DEBUG_INFO
729                     ("Attempt to send command while another command is pending.\n");
730                 err = -EBUSY;
731                 goto fail_unlock;
732         }
733
734         if (list_empty(&priv->msg_free_list)) {
735                 IPW_DEBUG_INFO("no available msg buffers\n");
736                 goto fail_unlock;
737         }
738
739         priv->status |= STATUS_CMD_ACTIVE;
740         priv->messages_sent++;
741
742         element = priv->msg_free_list.next;
743
744         packet = list_entry(element, struct ipw2100_tx_packet, list);
745         packet->jiffy_start = jiffies;
746
747         /* initialize the firmware command packet */
748         packet->info.c_struct.cmd->host_command_reg = cmd->host_command;
749         packet->info.c_struct.cmd->host_command_reg1 = cmd->host_command1;
750         packet->info.c_struct.cmd->host_command_len_reg =
751             cmd->host_command_length;
752         packet->info.c_struct.cmd->sequence = cmd->host_command_sequence;
753
754         memcpy(packet->info.c_struct.cmd->host_command_params_reg,
755                cmd->host_command_parameters,
756                sizeof(packet->info.c_struct.cmd->host_command_params_reg));
757
758         list_del(element);
759         DEC_STAT(&priv->msg_free_stat);
760
761         list_add_tail(element, &priv->msg_pend_list);
762         INC_STAT(&priv->msg_pend_stat);
763
764         ipw2100_tx_send_commands(priv);
765         ipw2100_tx_send_data(priv);
766
767         spin_unlock_irqrestore(&priv->low_lock, flags);
768
769         /*
770          * We must wait for this command to complete before another
771          * command can be sent...  but if we wait more than 3 seconds
772          * then there is a problem.
773          */
774
775         err =
776             wait_event_interruptible_timeout(priv->wait_command_queue,
777                                              !(priv->
778                                                status & STATUS_CMD_ACTIVE),
779                                              HOST_COMPLETE_TIMEOUT);
780
781         if (err == 0) {
782                 IPW_DEBUG_INFO("Command completion failed out after %dms.\n",
783                                1000 * (HOST_COMPLETE_TIMEOUT / HZ));
784                 priv->fatal_error = IPW2100_ERR_MSG_TIMEOUT;
785                 priv->status &= ~STATUS_CMD_ACTIVE;
786                 schedule_reset(priv);
787                 return -EIO;
788         }
789
790         if (priv->fatal_error) {
791                 printk(KERN_WARNING DRV_NAME ": %s: firmware fatal error\n",
792                        priv->net_dev->name);
793                 return -EIO;
794         }
795
796         /* !!!!! HACK TEST !!!!!
797          * When lots of debug trace statements are enabled, the driver
798          * doesn't seem to have as many firmware restart cycles...
799          *
800          * As a test, we're sticking in a 1/100s delay here */
801         schedule_timeout_uninterruptible(msecs_to_jiffies(10));
802
803         return 0;
804
805       fail_unlock:
806         spin_unlock_irqrestore(&priv->low_lock, flags);
807
808         return err;
809 }
810
811 /*
812  * Verify the values and data access of the hardware
813  * No locks needed or used.  No functions called.
814  */
815 static int ipw2100_verify(struct ipw2100_priv *priv)
816 {
817         u32 data1, data2;
818         u32 address;
819
820         u32 val1 = 0x76543210;
821         u32 val2 = 0xFEDCBA98;
822
823         /* Domain 0 check - all values should be DOA_DEBUG */
824         for (address = IPW_REG_DOA_DEBUG_AREA_START;
825              address < IPW_REG_DOA_DEBUG_AREA_END; address += sizeof(u32)) {
826                 read_register(priv->net_dev, address, &data1);
827                 if (data1 != IPW_DATA_DOA_DEBUG_VALUE)
828                         return -EIO;
829         }
830
831         /* Domain 1 check - use arbitrary read/write compare  */
832         for (address = 0; address < 5; address++) {
833                 /* The memory area is not used now */
834                 write_register(priv->net_dev, IPW_REG_DOMAIN_1_OFFSET + 0x32,
835                                val1);
836                 write_register(priv->net_dev, IPW_REG_DOMAIN_1_OFFSET + 0x36,
837                                val2);
838                 read_register(priv->net_dev, IPW_REG_DOMAIN_1_OFFSET + 0x32,
839                               &data1);
840                 read_register(priv->net_dev, IPW_REG_DOMAIN_1_OFFSET + 0x36,
841                               &data2);
842                 if (val1 == data1 && val2 == data2)
843                         return 0;
844         }
845
846         return -EIO;
847 }
848
849 /*
850  *
851  * Loop until the CARD_DISABLED bit is the same value as the
852  * supplied parameter
853  *
854  * TODO: See if it would be more efficient to do a wait/wake
855  *       cycle and have the completion event trigger the wakeup
856  *
857  */
858 #define IPW_CARD_DISABLE_COMPLETE_WAIT              100 // 100 milli
859 static int ipw2100_wait_for_card_state(struct ipw2100_priv *priv, int state)
860 {
861         int i;
862         u32 card_state;
863         u32 len = sizeof(card_state);
864         int err;
865
866         for (i = 0; i <= IPW_CARD_DISABLE_COMPLETE_WAIT * 1000; i += 50) {
867                 err = ipw2100_get_ordinal(priv, IPW_ORD_CARD_DISABLED,
868                                           &card_state, &len);
869                 if (err) {
870                         IPW_DEBUG_INFO("Query of CARD_DISABLED ordinal "
871                                        "failed.\n");
872                         return 0;
873                 }
874
875                 /* We'll break out if either the HW state says it is
876                  * in the state we want, or if HOST_COMPLETE command
877                  * finishes */
878                 if ((card_state == state) ||
879                     ((priv->status & STATUS_ENABLED) ?
880                      IPW_HW_STATE_ENABLED : IPW_HW_STATE_DISABLED) == state) {
881                         if (state == IPW_HW_STATE_ENABLED)
882                                 priv->status |= STATUS_ENABLED;
883                         else
884                                 priv->status &= ~STATUS_ENABLED;
885
886                         return 0;
887                 }
888
889                 udelay(50);
890         }
891
892         IPW_DEBUG_INFO("ipw2100_wait_for_card_state to %s state timed out\n",
893                        state ? "DISABLED" : "ENABLED");
894         return -EIO;
895 }
896
897 /*********************************************************************
898     Procedure   :   sw_reset_and_clock
899     Purpose     :   Asserts s/w reset, asserts clock initialization
900                     and waits for clock stabilization
901  ********************************************************************/
902 static int sw_reset_and_clock(struct ipw2100_priv *priv)
903 {
904         int i;
905         u32 r;
906
907         // assert s/w reset
908         write_register(priv->net_dev, IPW_REG_RESET_REG,
909                        IPW_AUX_HOST_RESET_REG_SW_RESET);
910
911         // wait for clock stabilization
912         for (i = 0; i < 1000; i++) {
913                 udelay(IPW_WAIT_RESET_ARC_COMPLETE_DELAY);
914
915                 // check clock ready bit
916                 read_register(priv->net_dev, IPW_REG_RESET_REG, &r);
917                 if (r & IPW_AUX_HOST_RESET_REG_PRINCETON_RESET)
918                         break;
919         }
920
921         if (i == 1000)
922                 return -EIO;    // TODO: better error value
923
924         /* set "initialization complete" bit to move adapter to
925          * D0 state */
926         write_register(priv->net_dev, IPW_REG_GP_CNTRL,
927                        IPW_AUX_HOST_GP_CNTRL_BIT_INIT_DONE);
928
929         /* wait for clock stabilization */
930         for (i = 0; i < 10000; i++) {
931                 udelay(IPW_WAIT_CLOCK_STABILIZATION_DELAY * 4);
932
933                 /* check clock ready bit */
934                 read_register(priv->net_dev, IPW_REG_GP_CNTRL, &r);
935                 if (r & IPW_AUX_HOST_GP_CNTRL_BIT_CLOCK_READY)
936                         break;
937         }
938
939         if (i == 10000)
940                 return -EIO;    /* TODO: better error value */
941
942         /* set D0 standby bit */
943         read_register(priv->net_dev, IPW_REG_GP_CNTRL, &r);
944         write_register(priv->net_dev, IPW_REG_GP_CNTRL,
945                        r | IPW_AUX_HOST_GP_CNTRL_BIT_HOST_ALLOWS_STANDBY);
946
947         return 0;
948 }
949
950 /*********************************************************************
951     Procedure   :   ipw2100_download_firmware
952     Purpose     :   Initiaze adapter after power on.
953                     The sequence is:
954                     1. assert s/w reset first!
955                     2. awake clocks & wait for clock stabilization
956                     3. hold ARC (don't ask me why...)
957                     4. load Dino ucode and reset/clock init again
958                     5. zero-out shared mem
959                     6. download f/w
960  *******************************************************************/
961 static int ipw2100_download_firmware(struct ipw2100_priv *priv)
962 {
963         u32 address;
964         int err;
965
966 #ifndef CONFIG_PM
967         /* Fetch the firmware and microcode */
968         struct ipw2100_fw ipw2100_firmware;
969 #endif
970
971         if (priv->fatal_error) {
972                 IPW_DEBUG_ERROR("%s: ipw2100_download_firmware called after "
973                                 "fatal error %d.  Interface must be brought down.\n",
974                                 priv->net_dev->name, priv->fatal_error);
975                 return -EINVAL;
976         }
977 #ifdef CONFIG_PM
978         if (!ipw2100_firmware.version) {
979                 err = ipw2100_get_firmware(priv, &ipw2100_firmware);
980                 if (err) {
981                         IPW_DEBUG_ERROR("%s: ipw2100_get_firmware failed: %d\n",
982                                         priv->net_dev->name, err);
983                         priv->fatal_error = IPW2100_ERR_FW_LOAD;
984                         goto fail;
985                 }
986         }
987 #else
988         err = ipw2100_get_firmware(priv, &ipw2100_firmware);
989         if (err) {
990                 IPW_DEBUG_ERROR("%s: ipw2100_get_firmware failed: %d\n",
991                                 priv->net_dev->name, err);
992                 priv->fatal_error = IPW2100_ERR_FW_LOAD;
993                 goto fail;
994         }
995 #endif
996         priv->firmware_version = ipw2100_firmware.version;
997
998         /* s/w reset and clock stabilization */
999         err = sw_reset_and_clock(priv);
1000         if (err) {
1001                 IPW_DEBUG_ERROR("%s: sw_reset_and_clock failed: %d\n",
1002                                 priv->net_dev->name, err);
1003                 goto fail;
1004         }
1005
1006         err = ipw2100_verify(priv);
1007         if (err) {
1008                 IPW_DEBUG_ERROR("%s: ipw2100_verify failed: %d\n",
1009                                 priv->net_dev->name, err);
1010                 goto fail;
1011         }
1012
1013         /* Hold ARC */
1014         write_nic_dword(priv->net_dev,
1015                         IPW_INTERNAL_REGISTER_HALT_AND_RESET, 0x80000000);
1016
1017         /* allow ARC to run */
1018         write_register(priv->net_dev, IPW_REG_RESET_REG, 0);
1019
1020         /* load microcode */
1021         err = ipw2100_ucode_download(priv, &ipw2100_firmware);
1022         if (err) {
1023                 printk(KERN_ERR DRV_NAME ": %s: Error loading microcode: %d\n",
1024                        priv->net_dev->name, err);
1025                 goto fail;
1026         }
1027
1028         /* release ARC */
1029         write_nic_dword(priv->net_dev,
1030                         IPW_INTERNAL_REGISTER_HALT_AND_RESET, 0x00000000);
1031
1032         /* s/w reset and clock stabilization (again!!!) */
1033         err = sw_reset_and_clock(priv);
1034         if (err) {
1035                 printk(KERN_ERR DRV_NAME
1036                        ": %s: sw_reset_and_clock failed: %d\n",
1037                        priv->net_dev->name, err);
1038                 goto fail;
1039         }
1040
1041         /* load f/w */
1042         err = ipw2100_fw_download(priv, &ipw2100_firmware);
1043         if (err) {
1044                 IPW_DEBUG_ERROR("%s: Error loading firmware: %d\n",
1045                                 priv->net_dev->name, err);
1046                 goto fail;
1047         }
1048 #ifndef CONFIG_PM
1049         /*
1050          * When the .resume method of the driver is called, the other
1051          * part of the system, i.e. the ide driver could still stay in
1052          * the suspend stage. This prevents us from loading the firmware
1053          * from the disk.  --YZ
1054          */
1055
1056         /* free any storage allocated for firmware image */
1057         ipw2100_release_firmware(priv, &ipw2100_firmware);
1058 #endif
1059
1060         /* zero out Domain 1 area indirectly (Si requirement) */
1061         for (address = IPW_HOST_FW_SHARED_AREA0;
1062              address < IPW_HOST_FW_SHARED_AREA0_END; address += 4)
1063                 write_nic_dword(priv->net_dev, address, 0);
1064         for (address = IPW_HOST_FW_SHARED_AREA1;
1065              address < IPW_HOST_FW_SHARED_AREA1_END; address += 4)
1066                 write_nic_dword(priv->net_dev, address, 0);
1067         for (address = IPW_HOST_FW_SHARED_AREA2;
1068              address < IPW_HOST_FW_SHARED_AREA2_END; address += 4)
1069                 write_nic_dword(priv->net_dev, address, 0);
1070         for (address = IPW_HOST_FW_SHARED_AREA3;
1071              address < IPW_HOST_FW_SHARED_AREA3_END; address += 4)
1072                 write_nic_dword(priv->net_dev, address, 0);
1073         for (address = IPW_HOST_FW_INTERRUPT_AREA;
1074              address < IPW_HOST_FW_INTERRUPT_AREA_END; address += 4)
1075                 write_nic_dword(priv->net_dev, address, 0);
1076
1077         return 0;
1078
1079       fail:
1080         ipw2100_release_firmware(priv, &ipw2100_firmware);
1081         return err;
1082 }
1083
1084 static inline void ipw2100_enable_interrupts(struct ipw2100_priv *priv)
1085 {
1086         if (priv->status & STATUS_INT_ENABLED)
1087                 return;
1088         priv->status |= STATUS_INT_ENABLED;
1089         write_register(priv->net_dev, IPW_REG_INTA_MASK, IPW_INTERRUPT_MASK);
1090 }
1091
1092 static inline void ipw2100_disable_interrupts(struct ipw2100_priv *priv)
1093 {
1094         if (!(priv->status & STATUS_INT_ENABLED))
1095                 return;
1096         priv->status &= ~STATUS_INT_ENABLED;
1097         write_register(priv->net_dev, IPW_REG_INTA_MASK, 0x0);
1098 }
1099
1100 static void ipw2100_initialize_ordinals(struct ipw2100_priv *priv)
1101 {
1102         struct ipw2100_ordinals *ord = &priv->ordinals;
1103
1104         IPW_DEBUG_INFO("enter\n");
1105
1106         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_ORDINALS_TABLE_1,
1107                       &ord->table1_addr);
1108
1109         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_ORDINALS_TABLE_2,
1110                       &ord->table2_addr);
1111
1112         read_nic_dword(priv->net_dev, ord->table1_addr, &ord->table1_size);
1113         read_nic_dword(priv->net_dev, ord->table2_addr, &ord->table2_size);
1114
1115         ord->table2_size &= 0x0000FFFF;
1116
1117         IPW_DEBUG_INFO("table 1 size: %d\n", ord->table1_size);
1118         IPW_DEBUG_INFO("table 2 size: %d\n", ord->table2_size);
1119         IPW_DEBUG_INFO("exit\n");
1120 }
1121
1122 static inline void ipw2100_hw_set_gpio(struct ipw2100_priv *priv)
1123 {
1124         u32 reg = 0;
1125         /*
1126          * Set GPIO 3 writable by FW; GPIO 1 writable
1127          * by driver and enable clock
1128          */
1129         reg = (IPW_BIT_GPIO_GPIO3_MASK | IPW_BIT_GPIO_GPIO1_ENABLE |
1130                IPW_BIT_GPIO_LED_OFF);
1131         write_register(priv->net_dev, IPW_REG_GPIO, reg);
1132 }
1133
1134 static int rf_kill_active(struct ipw2100_priv *priv)
1135 {
1136 #define MAX_RF_KILL_CHECKS 5
1137 #define RF_KILL_CHECK_DELAY 40
1138
1139         unsigned short value = 0;
1140         u32 reg = 0;
1141         int i;
1142
1143         if (!(priv->hw_features & HW_FEATURE_RFKILL)) {
1144                 priv->status &= ~STATUS_RF_KILL_HW;
1145                 return 0;
1146         }
1147
1148         for (i = 0; i < MAX_RF_KILL_CHECKS; i++) {
1149                 udelay(RF_KILL_CHECK_DELAY);
1150                 read_register(priv->net_dev, IPW_REG_GPIO, &reg);
1151                 value = (value << 1) | ((reg & IPW_BIT_GPIO_RF_KILL) ? 0 : 1);
1152         }
1153
1154         if (value == 0)
1155                 priv->status |= STATUS_RF_KILL_HW;
1156         else
1157                 priv->status &= ~STATUS_RF_KILL_HW;
1158
1159         return (value == 0);
1160 }
1161
1162 static int ipw2100_get_hw_features(struct ipw2100_priv *priv)
1163 {
1164         u32 addr, len;
1165         u32 val;
1166
1167         /*
1168          * EEPROM_SRAM_DB_START_ADDRESS using ordinal in ordinal table 1
1169          */
1170         len = sizeof(addr);
1171         if (ipw2100_get_ordinal
1172             (priv, IPW_ORD_EEPROM_SRAM_DB_BLOCK_START_ADDRESS, &addr, &len)) {
1173                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1174                                __LINE__);
1175                 return -EIO;
1176         }
1177
1178         IPW_DEBUG_INFO("EEPROM address: %08X\n", addr);
1179
1180         /*
1181          * EEPROM version is the byte at offset 0xfd in firmware
1182          * We read 4 bytes, then shift out the byte we actually want */
1183         read_nic_dword(priv->net_dev, addr + 0xFC, &val);
1184         priv->eeprom_version = (val >> 24) & 0xFF;
1185         IPW_DEBUG_INFO("EEPROM version: %d\n", priv->eeprom_version);
1186
1187         /*
1188          *  HW RF Kill enable is bit 0 in byte at offset 0x21 in firmware
1189          *
1190          *  notice that the EEPROM bit is reverse polarity, i.e.
1191          *     bit = 0  signifies HW RF kill switch is supported
1192          *     bit = 1  signifies HW RF kill switch is NOT supported
1193          */
1194         read_nic_dword(priv->net_dev, addr + 0x20, &val);
1195         if (!((val >> 24) & 0x01))
1196                 priv->hw_features |= HW_FEATURE_RFKILL;
1197
1198         IPW_DEBUG_INFO("HW RF Kill: %ssupported.\n",
1199                        (priv->hw_features & HW_FEATURE_RFKILL) ? "" : "not ");
1200
1201         return 0;
1202 }
1203
1204 /*
1205  * Start firmware execution after power on and intialization
1206  * The sequence is:
1207  *  1. Release ARC
1208  *  2. Wait for f/w initialization completes;
1209  */
1210 static int ipw2100_start_adapter(struct ipw2100_priv *priv)
1211 {
1212         int i;
1213         u32 inta, inta_mask, gpio;
1214
1215         IPW_DEBUG_INFO("enter\n");
1216
1217         if (priv->status & STATUS_RUNNING)
1218                 return 0;
1219
1220         /*
1221          * Initialize the hw - drive adapter to DO state by setting
1222          * init_done bit. Wait for clk_ready bit and Download
1223          * fw & dino ucode
1224          */
1225         if (ipw2100_download_firmware(priv)) {
1226                 printk(KERN_ERR DRV_NAME
1227                        ": %s: Failed to power on the adapter.\n",
1228                        priv->net_dev->name);
1229                 return -EIO;
1230         }
1231
1232         /* Clear the Tx, Rx and Msg queues and the r/w indexes
1233          * in the firmware RBD and TBD ring queue */
1234         ipw2100_queues_initialize(priv);
1235
1236         ipw2100_hw_set_gpio(priv);
1237
1238         /* TODO -- Look at disabling interrupts here to make sure none
1239          * get fired during FW initialization */
1240
1241         /* Release ARC - clear reset bit */
1242         write_register(priv->net_dev, IPW_REG_RESET_REG, 0);
1243
1244         /* wait for f/w intialization complete */
1245         IPW_DEBUG_FW("Waiting for f/w initialization to complete...\n");
1246         i = 5000;
1247         do {
1248                 schedule_timeout_uninterruptible(msecs_to_jiffies(40));
1249                 /* Todo... wait for sync command ... */
1250
1251                 read_register(priv->net_dev, IPW_REG_INTA, &inta);
1252
1253                 /* check "init done" bit */
1254                 if (inta & IPW2100_INTA_FW_INIT_DONE) {
1255                         /* reset "init done" bit */
1256                         write_register(priv->net_dev, IPW_REG_INTA,
1257                                        IPW2100_INTA_FW_INIT_DONE);
1258                         break;
1259                 }
1260
1261                 /* check error conditions : we check these after the firmware
1262                  * check so that if there is an error, the interrupt handler
1263                  * will see it and the adapter will be reset */
1264                 if (inta &
1265                     (IPW2100_INTA_FATAL_ERROR | IPW2100_INTA_PARITY_ERROR)) {
1266                         /* clear error conditions */
1267                         write_register(priv->net_dev, IPW_REG_INTA,
1268                                        IPW2100_INTA_FATAL_ERROR |
1269                                        IPW2100_INTA_PARITY_ERROR);
1270                 }
1271         } while (--i);
1272
1273         /* Clear out any pending INTAs since we aren't supposed to have
1274          * interrupts enabled at this point... */
1275         read_register(priv->net_dev, IPW_REG_INTA, &inta);
1276         read_register(priv->net_dev, IPW_REG_INTA_MASK, &inta_mask);
1277         inta &= IPW_INTERRUPT_MASK;
1278         /* Clear out any pending interrupts */
1279         if (inta & inta_mask)
1280                 write_register(priv->net_dev, IPW_REG_INTA, inta);
1281
1282         IPW_DEBUG_FW("f/w initialization complete: %s\n",
1283                      i ? "SUCCESS" : "FAILED");
1284
1285         if (!i) {
1286                 printk(KERN_WARNING DRV_NAME
1287                        ": %s: Firmware did not initialize.\n",
1288                        priv->net_dev->name);
1289                 return -EIO;
1290         }
1291
1292         /* allow firmware to write to GPIO1 & GPIO3 */
1293         read_register(priv->net_dev, IPW_REG_GPIO, &gpio);
1294
1295         gpio |= (IPW_BIT_GPIO_GPIO1_MASK | IPW_BIT_GPIO_GPIO3_MASK);
1296
1297         write_register(priv->net_dev, IPW_REG_GPIO, gpio);
1298
1299         /* Ready to receive commands */
1300         priv->status |= STATUS_RUNNING;
1301
1302         /* The adapter has been reset; we are not associated */
1303         priv->status &= ~(STATUS_ASSOCIATING | STATUS_ASSOCIATED);
1304
1305         IPW_DEBUG_INFO("exit\n");
1306
1307         return 0;
1308 }
1309
1310 static inline void ipw2100_reset_fatalerror(struct ipw2100_priv *priv)
1311 {
1312         if (!priv->fatal_error)
1313                 return;
1314
1315         priv->fatal_errors[priv->fatal_index++] = priv->fatal_error;
1316         priv->fatal_index %= IPW2100_ERROR_QUEUE;
1317         priv->fatal_error = 0;
1318 }
1319
1320 /* NOTE: Our interrupt is disabled when this method is called */
1321 static int ipw2100_power_cycle_adapter(struct ipw2100_priv *priv)
1322 {
1323         u32 reg;
1324         int i;
1325
1326         IPW_DEBUG_INFO("Power cycling the hardware.\n");
1327
1328         ipw2100_hw_set_gpio(priv);
1329
1330         /* Step 1. Stop Master Assert */
1331         write_register(priv->net_dev, IPW_REG_RESET_REG,
1332                        IPW_AUX_HOST_RESET_REG_STOP_MASTER);
1333
1334         /* Step 2. Wait for stop Master Assert
1335          *         (not more than 50us, otherwise ret error */
1336         i = 5;
1337         do {
1338                 udelay(IPW_WAIT_RESET_MASTER_ASSERT_COMPLETE_DELAY);
1339                 read_register(priv->net_dev, IPW_REG_RESET_REG, &reg);
1340
1341                 if (reg & IPW_AUX_HOST_RESET_REG_MASTER_DISABLED)
1342                         break;
1343         } while (--i);
1344
1345         priv->status &= ~STATUS_RESET_PENDING;
1346
1347         if (!i) {
1348                 IPW_DEBUG_INFO
1349                     ("exit - waited too long for master assert stop\n");
1350                 return -EIO;
1351         }
1352
1353         write_register(priv->net_dev, IPW_REG_RESET_REG,
1354                        IPW_AUX_HOST_RESET_REG_SW_RESET);
1355
1356         /* Reset any fatal_error conditions */
1357         ipw2100_reset_fatalerror(priv);
1358
1359         /* At this point, the adapter is now stopped and disabled */
1360         priv->status &= ~(STATUS_RUNNING | STATUS_ASSOCIATING |
1361                           STATUS_ASSOCIATED | STATUS_ENABLED);
1362
1363         return 0;
1364 }
1365
1366 /*
1367  * Send the CARD_DISABLE_PHY_OFF comamnd to the card to disable it
1368  *
1369  * After disabling, if the card was associated, a STATUS_ASSN_LOST will be sent.
1370  *
1371  * STATUS_CARD_DISABLE_NOTIFICATION will be sent regardless of
1372  * if STATUS_ASSN_LOST is sent.
1373  */
1374 static int ipw2100_hw_phy_off(struct ipw2100_priv *priv)
1375 {
1376
1377 #define HW_PHY_OFF_LOOP_DELAY (HZ / 5000)
1378
1379         struct host_command cmd = {
1380                 .host_command = CARD_DISABLE_PHY_OFF,
1381                 .host_command_sequence = 0,
1382                 .host_command_length = 0,
1383         };
1384         int err, i;
1385         u32 val1, val2;
1386
1387         IPW_DEBUG_HC("CARD_DISABLE_PHY_OFF\n");
1388
1389         /* Turn off the radio */
1390         err = ipw2100_hw_send_command(priv, &cmd);
1391         if (err)
1392                 return err;
1393
1394         for (i = 0; i < 2500; i++) {
1395                 read_nic_dword(priv->net_dev, IPW2100_CONTROL_REG, &val1);
1396                 read_nic_dword(priv->net_dev, IPW2100_COMMAND, &val2);
1397
1398                 if ((val1 & IPW2100_CONTROL_PHY_OFF) &&
1399                     (val2 & IPW2100_COMMAND_PHY_OFF))
1400                         return 0;
1401
1402                 schedule_timeout_uninterruptible(HW_PHY_OFF_LOOP_DELAY);
1403         }
1404
1405         return -EIO;
1406 }
1407
1408 static int ipw2100_enable_adapter(struct ipw2100_priv *priv)
1409 {
1410         struct host_command cmd = {
1411                 .host_command = HOST_COMPLETE,
1412                 .host_command_sequence = 0,
1413                 .host_command_length = 0
1414         };
1415         int err = 0;
1416
1417         IPW_DEBUG_HC("HOST_COMPLETE\n");
1418
1419         if (priv->status & STATUS_ENABLED)
1420                 return 0;
1421
1422         mutex_lock(&priv->adapter_mutex);
1423
1424         if (rf_kill_active(priv)) {
1425                 IPW_DEBUG_HC("Command aborted due to RF kill active.\n");
1426                 goto fail_up;
1427         }
1428
1429         err = ipw2100_hw_send_command(priv, &cmd);
1430         if (err) {
1431                 IPW_DEBUG_INFO("Failed to send HOST_COMPLETE command\n");
1432                 goto fail_up;
1433         }
1434
1435         err = ipw2100_wait_for_card_state(priv, IPW_HW_STATE_ENABLED);
1436         if (err) {
1437                 IPW_DEBUG_INFO("%s: card not responding to init command.\n",
1438                                priv->net_dev->name);
1439                 goto fail_up;
1440         }
1441
1442         if (priv->stop_hang_check) {
1443                 priv->stop_hang_check = 0;
1444                 queue_delayed_work(priv->workqueue, &priv->hang_check, HZ / 2);
1445         }
1446
1447       fail_up:
1448         mutex_unlock(&priv->adapter_mutex);
1449         return err;
1450 }
1451
1452 static int ipw2100_hw_stop_adapter(struct ipw2100_priv *priv)
1453 {
1454 #define HW_POWER_DOWN_DELAY (msecs_to_jiffies(100))
1455
1456         struct host_command cmd = {
1457                 .host_command = HOST_PRE_POWER_DOWN,
1458                 .host_command_sequence = 0,
1459                 .host_command_length = 0,
1460         };
1461         int err, i;
1462         u32 reg;
1463
1464         if (!(priv->status & STATUS_RUNNING))
1465                 return 0;
1466
1467         priv->status |= STATUS_STOPPING;
1468
1469         /* We can only shut down the card if the firmware is operational.  So,
1470          * if we haven't reset since a fatal_error, then we can not send the
1471          * shutdown commands. */
1472         if (!priv->fatal_error) {
1473                 /* First, make sure the adapter is enabled so that the PHY_OFF
1474                  * command can shut it down */
1475                 ipw2100_enable_adapter(priv);
1476
1477                 err = ipw2100_hw_phy_off(priv);
1478                 if (err)
1479                         printk(KERN_WARNING DRV_NAME
1480                                ": Error disabling radio %d\n", err);
1481
1482                 /*
1483                  * If in D0-standby mode going directly to D3 may cause a
1484                  * PCI bus violation.  Therefore we must change out of the D0
1485                  * state.
1486                  *
1487                  * Sending the PREPARE_FOR_POWER_DOWN will restrict the
1488                  * hardware from going into standby mode and will transition
1489                  * out of D0-standby if it is already in that state.
1490                  *
1491                  * STATUS_PREPARE_POWER_DOWN_COMPLETE will be sent by the
1492                  * driver upon completion.  Once received, the driver can
1493                  * proceed to the D3 state.
1494                  *
1495                  * Prepare for power down command to fw.  This command would
1496                  * take HW out of D0-standby and prepare it for D3 state.
1497                  *
1498                  * Currently FW does not support event notification for this
1499                  * event. Therefore, skip waiting for it.  Just wait a fixed
1500                  * 100ms
1501                  */
1502                 IPW_DEBUG_HC("HOST_PRE_POWER_DOWN\n");
1503
1504                 err = ipw2100_hw_send_command(priv, &cmd);
1505                 if (err)
1506                         printk(KERN_WARNING DRV_NAME ": "
1507                                "%s: Power down command failed: Error %d\n",
1508                                priv->net_dev->name, err);
1509                 else
1510                         schedule_timeout_uninterruptible(HW_POWER_DOWN_DELAY);
1511         }
1512
1513         priv->status &= ~STATUS_ENABLED;
1514
1515         /*
1516          * Set GPIO 3 writable by FW; GPIO 1 writable
1517          * by driver and enable clock
1518          */
1519         ipw2100_hw_set_gpio(priv);
1520
1521         /*
1522          * Power down adapter.  Sequence:
1523          * 1. Stop master assert (RESET_REG[9]=1)
1524          * 2. Wait for stop master (RESET_REG[8]==1)
1525          * 3. S/w reset assert (RESET_REG[7] = 1)
1526          */
1527
1528         /* Stop master assert */
1529         write_register(priv->net_dev, IPW_REG_RESET_REG,
1530                        IPW_AUX_HOST_RESET_REG_STOP_MASTER);
1531
1532         /* wait stop master not more than 50 usec.
1533          * Otherwise return error. */
1534         for (i = 5; i > 0; i--) {
1535                 udelay(10);
1536
1537                 /* Check master stop bit */
1538                 read_register(priv->net_dev, IPW_REG_RESET_REG, &reg);
1539
1540                 if (reg & IPW_AUX_HOST_RESET_REG_MASTER_DISABLED)
1541                         break;
1542         }
1543
1544         if (i == 0)
1545                 printk(KERN_WARNING DRV_NAME
1546                        ": %s: Could now power down adapter.\n",
1547                        priv->net_dev->name);
1548
1549         /* assert s/w reset */
1550         write_register(priv->net_dev, IPW_REG_RESET_REG,
1551                        IPW_AUX_HOST_RESET_REG_SW_RESET);
1552
1553         priv->status &= ~(STATUS_RUNNING | STATUS_STOPPING);
1554
1555         return 0;
1556 }
1557
1558 static int ipw2100_disable_adapter(struct ipw2100_priv *priv)
1559 {
1560         struct host_command cmd = {
1561                 .host_command = CARD_DISABLE,
1562                 .host_command_sequence = 0,
1563                 .host_command_length = 0
1564         };
1565         int err = 0;
1566
1567         IPW_DEBUG_HC("CARD_DISABLE\n");
1568
1569         if (!(priv->status & STATUS_ENABLED))
1570                 return 0;
1571
1572         /* Make sure we clear the associated state */
1573         priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
1574
1575         if (!priv->stop_hang_check) {
1576                 priv->stop_hang_check = 1;
1577                 cancel_delayed_work(&priv->hang_check);
1578         }
1579
1580         mutex_lock(&priv->adapter_mutex);
1581
1582         err = ipw2100_hw_send_command(priv, &cmd);
1583         if (err) {
1584                 printk(KERN_WARNING DRV_NAME
1585                        ": exit - failed to send CARD_DISABLE command\n");
1586                 goto fail_up;
1587         }
1588
1589         err = ipw2100_wait_for_card_state(priv, IPW_HW_STATE_DISABLED);
1590         if (err) {
1591                 printk(KERN_WARNING DRV_NAME
1592                        ": exit - card failed to change to DISABLED\n");
1593                 goto fail_up;
1594         }
1595
1596         IPW_DEBUG_INFO("TODO: implement scan state machine\n");
1597
1598       fail_up:
1599         mutex_unlock(&priv->adapter_mutex);
1600         return err;
1601 }
1602
1603 static int ipw2100_set_scan_options(struct ipw2100_priv *priv)
1604 {
1605         struct host_command cmd = {
1606                 .host_command = SET_SCAN_OPTIONS,
1607                 .host_command_sequence = 0,
1608                 .host_command_length = 8
1609         };
1610         int err;
1611
1612         IPW_DEBUG_INFO("enter\n");
1613
1614         IPW_DEBUG_SCAN("setting scan options\n");
1615
1616         cmd.host_command_parameters[0] = 0;
1617
1618         if (!(priv->config & CFG_ASSOCIATE))
1619                 cmd.host_command_parameters[0] |= IPW_SCAN_NOASSOCIATE;
1620         if ((priv->ieee->sec.flags & SEC_ENABLED) && priv->ieee->sec.enabled)
1621                 cmd.host_command_parameters[0] |= IPW_SCAN_MIXED_CELL;
1622         if (priv->config & CFG_PASSIVE_SCAN)
1623                 cmd.host_command_parameters[0] |= IPW_SCAN_PASSIVE;
1624
1625         cmd.host_command_parameters[1] = priv->channel_mask;
1626
1627         err = ipw2100_hw_send_command(priv, &cmd);
1628
1629         IPW_DEBUG_HC("SET_SCAN_OPTIONS 0x%04X\n",
1630                      cmd.host_command_parameters[0]);
1631
1632         return err;
1633 }
1634
1635 static int ipw2100_start_scan(struct ipw2100_priv *priv)
1636 {
1637         struct host_command cmd = {
1638                 .host_command = BROADCAST_SCAN,
1639                 .host_command_sequence = 0,
1640                 .host_command_length = 4
1641         };
1642         int err;
1643
1644         IPW_DEBUG_HC("START_SCAN\n");
1645
1646         cmd.host_command_parameters[0] = 0;
1647
1648         /* No scanning if in monitor mode */
1649         if (priv->ieee->iw_mode == IW_MODE_MONITOR)
1650                 return 1;
1651
1652         if (priv->status & STATUS_SCANNING) {
1653                 IPW_DEBUG_SCAN("Scan requested while already in scan...\n");
1654                 return 0;
1655         }
1656
1657         IPW_DEBUG_INFO("enter\n");
1658
1659         /* Not clearing here; doing so makes iwlist always return nothing...
1660          *
1661          * We should modify the table logic to use aging tables vs. clearing
1662          * the table on each scan start.
1663          */
1664         IPW_DEBUG_SCAN("starting scan\n");
1665
1666         priv->status |= STATUS_SCANNING;
1667         err = ipw2100_hw_send_command(priv, &cmd);
1668         if (err)
1669                 priv->status &= ~STATUS_SCANNING;
1670
1671         IPW_DEBUG_INFO("exit\n");
1672
1673         return err;
1674 }
1675
1676 static const struct ieee80211_geo ipw_geos[] = {
1677         {                       /* Restricted */
1678          "---",
1679          .bg_channels = 14,
1680          .bg = {{2412, 1}, {2417, 2}, {2422, 3},
1681                 {2427, 4}, {2432, 5}, {2437, 6},
1682                 {2442, 7}, {2447, 8}, {2452, 9},
1683                 {2457, 10}, {2462, 11}, {2467, 12},
1684                 {2472, 13}, {2484, 14}},
1685          },
1686 };
1687
1688 static int ipw2100_up(struct ipw2100_priv *priv, int deferred)
1689 {
1690         unsigned long flags;
1691         int rc = 0;
1692         u32 lock;
1693         u32 ord_len = sizeof(lock);
1694
1695         /* Age scan list entries found before suspend */
1696         if (priv->suspend_time) {
1697                 ieee80211_networks_age(priv->ieee, priv->suspend_time);
1698                 priv->suspend_time = 0;
1699         }
1700
1701         /* Quiet if manually disabled. */
1702         if (priv->status & STATUS_RF_KILL_SW) {
1703                 IPW_DEBUG_INFO("%s: Radio is disabled by Manual Disable "
1704                                "switch\n", priv->net_dev->name);
1705                 return 0;
1706         }
1707
1708         /* the ipw2100 hardware really doesn't want power management delays
1709          * longer than 175usec
1710          */
1711         pm_qos_update_requirement(PM_QOS_CPU_DMA_LATENCY, "ipw2100", 175);
1712
1713         /* If the interrupt is enabled, turn it off... */
1714         spin_lock_irqsave(&priv->low_lock, flags);
1715         ipw2100_disable_interrupts(priv);
1716
1717         /* Reset any fatal_error conditions */
1718         ipw2100_reset_fatalerror(priv);
1719         spin_unlock_irqrestore(&priv->low_lock, flags);
1720
1721         if (priv->status & STATUS_POWERED ||
1722             (priv->status & STATUS_RESET_PENDING)) {
1723                 /* Power cycle the card ... */
1724                 if (ipw2100_power_cycle_adapter(priv)) {
1725                         printk(KERN_WARNING DRV_NAME
1726                                ": %s: Could not cycle adapter.\n",
1727                                priv->net_dev->name);
1728                         rc = 1;
1729                         goto exit;
1730                 }
1731         } else
1732                 priv->status |= STATUS_POWERED;
1733
1734         /* Load the firmware, start the clocks, etc. */
1735         if (ipw2100_start_adapter(priv)) {
1736                 printk(KERN_ERR DRV_NAME
1737                        ": %s: Failed to start the firmware.\n",
1738                        priv->net_dev->name);
1739                 rc = 1;
1740                 goto exit;
1741         }
1742
1743         ipw2100_initialize_ordinals(priv);
1744
1745         /* Determine capabilities of this particular HW configuration */
1746         if (ipw2100_get_hw_features(priv)) {
1747                 printk(KERN_ERR DRV_NAME
1748                        ": %s: Failed to determine HW features.\n",
1749                        priv->net_dev->name);
1750                 rc = 1;
1751                 goto exit;
1752         }
1753
1754         /* Initialize the geo */
1755         if (ieee80211_set_geo(priv->ieee, &ipw_geos[0])) {
1756                 printk(KERN_WARNING DRV_NAME "Could not set geo\n");
1757                 return 0;
1758         }
1759         priv->ieee->freq_band = IEEE80211_24GHZ_BAND;
1760
1761         lock = LOCK_NONE;
1762         if (ipw2100_set_ordinal(priv, IPW_ORD_PERS_DB_LOCK, &lock, &ord_len)) {
1763                 printk(KERN_ERR DRV_NAME
1764                        ": %s: Failed to clear ordinal lock.\n",
1765                        priv->net_dev->name);
1766                 rc = 1;
1767                 goto exit;
1768         }
1769
1770         priv->status &= ~STATUS_SCANNING;
1771
1772         if (rf_kill_active(priv)) {
1773                 printk(KERN_INFO "%s: Radio is disabled by RF switch.\n",
1774                        priv->net_dev->name);
1775
1776                 if (priv->stop_rf_kill) {
1777                         priv->stop_rf_kill = 0;
1778                         queue_delayed_work(priv->workqueue, &priv->rf_kill,
1779                                            round_jiffies_relative(HZ));
1780                 }
1781
1782                 deferred = 1;
1783         }
1784
1785         /* Turn on the interrupt so that commands can be processed */
1786         ipw2100_enable_interrupts(priv);
1787
1788         /* Send all of the commands that must be sent prior to
1789          * HOST_COMPLETE */
1790         if (ipw2100_adapter_setup(priv)) {
1791                 printk(KERN_ERR DRV_NAME ": %s: Failed to start the card.\n",
1792                        priv->net_dev->name);
1793                 rc = 1;
1794                 goto exit;
1795         }
1796
1797         if (!deferred) {
1798                 /* Enable the adapter - sends HOST_COMPLETE */
1799                 if (ipw2100_enable_adapter(priv)) {
1800                         printk(KERN_ERR DRV_NAME ": "
1801                                "%s: failed in call to enable adapter.\n",
1802                                priv->net_dev->name);
1803                         ipw2100_hw_stop_adapter(priv);
1804                         rc = 1;
1805                         goto exit;
1806                 }
1807
1808                 /* Start a scan . . . */
1809                 ipw2100_set_scan_options(priv);
1810                 ipw2100_start_scan(priv);
1811         }
1812
1813       exit:
1814         return rc;
1815 }
1816
1817 /* Called by register_netdev() */
1818 static int ipw2100_net_init(struct net_device *dev)
1819 {
1820         struct ipw2100_priv *priv = ieee80211_priv(dev);
1821         return ipw2100_up(priv, 1);
1822 }
1823
1824 static void ipw2100_down(struct ipw2100_priv *priv)
1825 {
1826         unsigned long flags;
1827         union iwreq_data wrqu = {
1828                 .ap_addr = {
1829                             .sa_family = ARPHRD_ETHER}
1830         };
1831         int associated = priv->status & STATUS_ASSOCIATED;
1832
1833         /* Kill the RF switch timer */
1834         if (!priv->stop_rf_kill) {
1835                 priv->stop_rf_kill = 1;
1836                 cancel_delayed_work(&priv->rf_kill);
1837         }
1838
1839         /* Kill the firmware hang check timer */
1840         if (!priv->stop_hang_check) {
1841                 priv->stop_hang_check = 1;
1842                 cancel_delayed_work(&priv->hang_check);
1843         }
1844
1845         /* Kill any pending resets */
1846         if (priv->status & STATUS_RESET_PENDING)
1847                 cancel_delayed_work(&priv->reset_work);
1848
1849         /* Make sure the interrupt is on so that FW commands will be
1850          * processed correctly */
1851         spin_lock_irqsave(&priv->low_lock, flags);
1852         ipw2100_enable_interrupts(priv);
1853         spin_unlock_irqrestore(&priv->low_lock, flags);
1854
1855         if (ipw2100_hw_stop_adapter(priv))
1856                 printk(KERN_ERR DRV_NAME ": %s: Error stopping adapter.\n",
1857                        priv->net_dev->name);
1858
1859         /* Do not disable the interrupt until _after_ we disable
1860          * the adaptor.  Otherwise the CARD_DISABLE command will never
1861          * be ack'd by the firmware */
1862         spin_lock_irqsave(&priv->low_lock, flags);
1863         ipw2100_disable_interrupts(priv);
1864         spin_unlock_irqrestore(&priv->low_lock, flags);
1865
1866         pm_qos_update_requirement(PM_QOS_CPU_DMA_LATENCY, "ipw2100",
1867                         PM_QOS_DEFAULT_VALUE);
1868
1869         /* We have to signal any supplicant if we are disassociating */
1870         if (associated)
1871                 wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
1872
1873         priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
1874         netif_carrier_off(priv->net_dev);
1875         netif_stop_queue(priv->net_dev);
1876 }
1877
1878 static void ipw2100_reset_adapter(struct work_struct *work)
1879 {
1880         struct ipw2100_priv *priv =
1881                 container_of(work, struct ipw2100_priv, reset_work.work);
1882         unsigned long flags;
1883         union iwreq_data wrqu = {
1884                 .ap_addr = {
1885                             .sa_family = ARPHRD_ETHER}
1886         };
1887         int associated = priv->status & STATUS_ASSOCIATED;
1888
1889         spin_lock_irqsave(&priv->low_lock, flags);
1890         IPW_DEBUG_INFO(": %s: Restarting adapter.\n", priv->net_dev->name);
1891         priv->resets++;
1892         priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
1893         priv->status |= STATUS_SECURITY_UPDATED;
1894
1895         /* Force a power cycle even if interface hasn't been opened
1896          * yet */
1897         cancel_delayed_work(&priv->reset_work);
1898         priv->status |= STATUS_RESET_PENDING;
1899         spin_unlock_irqrestore(&priv->low_lock, flags);
1900
1901         mutex_lock(&priv->action_mutex);
1902         /* stop timed checks so that they don't interfere with reset */
1903         priv->stop_hang_check = 1;
1904         cancel_delayed_work(&priv->hang_check);
1905
1906         /* We have to signal any supplicant if we are disassociating */
1907         if (associated)
1908                 wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
1909
1910         ipw2100_up(priv, 0);
1911         mutex_unlock(&priv->action_mutex);
1912
1913 }
1914
1915 static void isr_indicate_associated(struct ipw2100_priv *priv, u32 status)
1916 {
1917
1918 #define MAC_ASSOCIATION_READ_DELAY (HZ)
1919         int ret;
1920         unsigned int len, essid_len;
1921         char essid[IW_ESSID_MAX_SIZE];
1922         u32 txrate;
1923         u32 chan;
1924         char *txratename;
1925         u8 bssid[ETH_ALEN];
1926         DECLARE_SSID_BUF(ssid);
1927
1928         /*
1929          * TBD: BSSID is usually 00:00:00:00:00:00 here and not
1930          *      an actual MAC of the AP. Seems like FW sets this
1931          *      address too late. Read it later and expose through
1932          *      /proc or schedule a later task to query and update
1933          */
1934
1935         essid_len = IW_ESSID_MAX_SIZE;
1936         ret = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_SSID,
1937                                   essid, &essid_len);
1938         if (ret) {
1939                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1940                                __LINE__);
1941                 return;
1942         }
1943
1944         len = sizeof(u32);
1945         ret = ipw2100_get_ordinal(priv, IPW_ORD_CURRENT_TX_RATE, &txrate, &len);
1946         if (ret) {
1947                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1948                                __LINE__);
1949                 return;
1950         }
1951
1952         len = sizeof(u32);
1953         ret = ipw2100_get_ordinal(priv, IPW_ORD_OUR_FREQ, &chan, &len);
1954         if (ret) {
1955                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1956                                __LINE__);
1957                 return;
1958         }
1959         len = ETH_ALEN;
1960         ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_AP_BSSID, &bssid, &len);
1961         if (ret) {
1962                 IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1963                                __LINE__);
1964                 return;
1965         }
1966         memcpy(priv->ieee->bssid, bssid, ETH_ALEN);
1967
1968         switch (txrate) {
1969         case TX_RATE_1_MBIT:
1970                 txratename = "1Mbps";
1971                 break;
1972         case TX_RATE_2_MBIT:
1973                 txratename = "2Mbsp";
1974                 break;
1975         case TX_RATE_5_5_MBIT:
1976                 txratename = "5.5Mbps";
1977                 break;
1978         case TX_RATE_11_MBIT:
1979                 txratename = "11Mbps";
1980                 break;
1981         default:
1982                 IPW_DEBUG_INFO("Unknown rate: %d\n", txrate);
1983                 txratename = "unknown rate";
1984                 break;
1985         }
1986
1987         IPW_DEBUG_INFO("%s: Associated with '%s' at %s, channel %d (BSSID=%pM)\n",
1988                        priv->net_dev->name, print_ssid(ssid, essid, essid_len),
1989                        txratename, chan, bssid);
1990
1991         /* now we copy read ssid into dev */
1992         if (!(priv->config & CFG_STATIC_ESSID)) {
1993                 priv->essid_len = min((u8) essid_len, (u8) IW_ESSID_MAX_SIZE);
1994                 memcpy(priv->essid, essid, priv->essid_len);
1995         }
1996         priv->channel = chan;
1997         memcpy(priv->bssid, bssid, ETH_ALEN);
1998
1999         priv->status |= STATUS_ASSOCIATING;
2000         priv->connect_start = get_seconds();
2001
2002         queue_delayed_work(priv->workqueue, &priv->wx_event_work, HZ / 10);
2003 }
2004
2005 static int ipw2100_set_essid(struct ipw2100_priv *priv, char *essid,
2006                              int length, int batch_mode)
2007 {
2008         int ssid_len = min(length, IW_ESSID_MAX_SIZE);
2009         struct host_command cmd = {
2010                 .host_command = SSID,
2011                 .host_command_sequence = 0,
2012                 .host_command_length = ssid_len
2013         };
2014         int err;
2015         DECLARE_SSID_BUF(ssid);
2016
2017         IPW_DEBUG_HC("SSID: '%s'\n", print_ssid(ssid, essid, ssid_len));
2018
2019         if (ssid_len)
2020                 memcpy(cmd.host_command_parameters, essid, ssid_len);
2021
2022         if (!batch_mode) {
2023                 err = ipw2100_disable_adapter(priv);
2024                 if (err)
2025                         return err;
2026         }
2027
2028         /* Bug in FW currently doesn't honor bit 0 in SET_SCAN_OPTIONS to
2029          * disable auto association -- so we cheat by setting a bogus SSID */
2030         if (!ssid_len && !(priv->config & CFG_ASSOCIATE)) {
2031                 int i;
2032                 u8 *bogus = (u8 *) cmd.host_command_parameters;
2033                 for (i = 0; i < IW_ESSID_MAX_SIZE; i++)
2034                         bogus[i] = 0x18 + i;
2035                 cmd.host_command_length = IW_ESSID_MAX_SIZE;
2036         }
2037
2038         /* NOTE:  We always send the SSID command even if the provided ESSID is
2039          * the same as what we currently think is set. */
2040
2041         err = ipw2100_hw_send_command(priv, &cmd);
2042         if (!err) {
2043                 memset(priv->essid + ssid_len, 0, IW_ESSID_MAX_SIZE - ssid_len);
2044                 memcpy(priv->essid, essid, ssid_len);
2045                 priv->essid_len = ssid_len;
2046         }
2047
2048         if (!batch_mode) {
2049                 if (ipw2100_enable_adapter(priv))
2050                         err = -EIO;
2051         }
2052
2053         return err;
2054 }
2055
2056 static void isr_indicate_association_lost(struct ipw2100_priv *priv, u32 status)
2057 {
2058         DECLARE_SSID_BUF(ssid);
2059
2060         IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE | IPW_DL_ASSOC,
2061                   "disassociated: '%s' %pM \n",
2062                   print_ssid(ssid, priv->essid, priv->essid_len),
2063                   priv->bssid);
2064
2065         priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
2066
2067         if (priv->status & STATUS_STOPPING) {
2068                 IPW_DEBUG_INFO("Card is stopping itself, discard ASSN_LOST.\n");
2069                 return;
2070         }
2071
2072         memset(priv->bssid, 0, ETH_ALEN);
2073         memset(priv->ieee->bssid, 0, ETH_ALEN);
2074
2075         netif_carrier_off(priv->net_dev);
2076         netif_stop_queue(priv->net_dev);
2077
2078         if (!(priv->status & STATUS_RUNNING))
2079                 return;
2080
2081         if (priv->status & STATUS_SECURITY_UPDATED)
2082                 queue_delayed_work(priv->workqueue, &priv->security_work, 0);
2083
2084         queue_delayed_work(priv->workqueue, &priv->wx_event_work, 0);
2085 }
2086
2087 static void isr_indicate_rf_kill(struct ipw2100_priv *priv, u32 status)
2088 {
2089         IPW_DEBUG_INFO("%s: RF Kill state changed to radio OFF.\n",
2090                        priv->net_dev->name);
2091
2092         /* RF_KILL is now enabled (else we wouldn't be here) */
2093         priv->status |= STATUS_RF_KILL_HW;
2094
2095         /* Make sure the RF Kill check timer is running */
2096         priv->stop_rf_kill = 0;
2097         cancel_delayed_work(&priv->rf_kill);
2098         queue_delayed_work(priv->workqueue, &priv->rf_kill,
2099                            round_jiffies_relative(HZ));
2100 }
2101
2102 static void send_scan_event(void *data)
2103 {
2104         struct ipw2100_priv *priv = data;
2105         union iwreq_data wrqu;
2106
2107         wrqu.data.length = 0;
2108         wrqu.data.flags = 0;
2109         wireless_send_event(priv->net_dev, SIOCGIWSCAN, &wrqu, NULL);
2110 }
2111
2112 static void ipw2100_scan_event_later(struct work_struct *work)
2113 {
2114         send_scan_event(container_of(work, struct ipw2100_priv,
2115                                         scan_event_later.work));
2116 }
2117
2118 static void ipw2100_scan_event_now(struct work_struct *work)
2119 {
2120         send_scan_event(container_of(work, struct ipw2100_priv,
2121                                         scan_event_now));
2122 }
2123
2124 static void isr_scan_complete(struct ipw2100_priv *priv, u32 status)
2125 {
2126         IPW_DEBUG_SCAN("scan complete\n");
2127         /* Age the scan results... */
2128         priv->ieee->scans++;
2129         priv->status &= ~STATUS_SCANNING;
2130
2131         /* Only userspace-requested scan completion events go out immediately */
2132         if (!priv->user_requested_scan) {
2133                 if (!delayed_work_pending(&priv->scan_event_later))
2134                         queue_delayed_work(priv->workqueue,
2135                                         &priv->scan_event_later,
2136                                         round_jiffies_relative(msecs_to_jiffies(4000)));
2137         } else {
2138                 priv->user_requested_scan = 0;
2139                 cancel_delayed_work(&priv->scan_event_later);
2140                 queue_work(priv->workqueue, &priv->scan_event_now);
2141         }
2142 }
2143
2144 #ifdef CONFIG_IPW2100_DEBUG
2145 #define IPW2100_HANDLER(v, f) { v, f, # v }
2146 struct ipw2100_status_indicator {
2147         int status;
2148         void (*cb) (struct ipw2100_priv * priv, u32 status);
2149         char *name;
2150 };
2151 #else
2152 #define IPW2100_HANDLER(v, f) { v, f }
2153 struct ipw2100_status_indicator {
2154         int status;
2155         void (*cb) (struct ipw2100_priv * priv, u32 status);
2156 };
2157 #endif                          /* CONFIG_IPW2100_DEBUG */
2158
2159 static void isr_indicate_scanning(struct ipw2100_priv *priv, u32 status)
2160 {
2161         IPW_DEBUG_SCAN("Scanning...\n");
2162         priv->status |= STATUS_SCANNING;
2163 }
2164
2165 static const struct ipw2100_status_indicator status_handlers[] = {
2166         IPW2100_HANDLER(IPW_STATE_INITIALIZED, NULL),
2167         IPW2100_HANDLER(IPW_STATE_COUNTRY_FOUND, NULL),
2168         IPW2100_HANDLER(IPW_STATE_ASSOCIATED, isr_indicate_associated),
2169         IPW2100_HANDLER(IPW_STATE_ASSN_LOST, isr_indicate_association_lost),
2170         IPW2100_HANDLER(IPW_STATE_ASSN_CHANGED, NULL),
2171         IPW2100_HANDLER(IPW_STATE_SCAN_COMPLETE, isr_scan_complete),
2172         IPW2100_HANDLER(IPW_STATE_ENTERED_PSP, NULL),
2173         IPW2100_HANDLER(IPW_STATE_LEFT_PSP, NULL),
2174         IPW2100_HANDLER(IPW_STATE_RF_KILL, isr_indicate_rf_kill),
2175         IPW2100_HANDLER(IPW_STATE_DISABLED, NULL),
2176         IPW2100_HANDLER(IPW_STATE_POWER_DOWN, NULL),
2177         IPW2100_HANDLER(IPW_STATE_SCANNING, isr_indicate_scanning),
2178         IPW2100_HANDLER(-1, NULL)
2179 };
2180
2181 static void isr_status_change(struct ipw2100_priv *priv, int status)
2182 {
2183         int i;
2184
2185         if (status == IPW_STATE_SCANNING &&
2186             priv->status & STATUS_ASSOCIATED &&
2187             !(priv->status & STATUS_SCANNING)) {
2188                 IPW_DEBUG_INFO("Scan detected while associated, with "
2189                                "no scan request.  Restarting firmware.\n");
2190
2191                 /* Wake up any sleeping jobs */
2192                 schedule_reset(priv);
2193         }
2194
2195         for (i = 0; status_handlers[i].status != -1; i++) {
2196                 if (status == status_handlers[i].status) {
2197                         IPW_DEBUG_NOTIF("Status change: %s\n",
2198                                         status_handlers[i].name);
2199                         if (status_handlers[i].cb)
2200                                 status_handlers[i].cb(priv, status);
2201                         priv->wstats.status = status;
2202                         return;
2203                 }
2204         }
2205
2206         IPW_DEBUG_NOTIF("unknown status received: %04x\n", status);
2207 }
2208
2209 static void isr_rx_complete_command(struct ipw2100_priv *priv,
2210                                     struct ipw2100_cmd_header *cmd)
2211 {
2212 #ifdef CONFIG_IPW2100_DEBUG
2213         if (cmd->host_command_reg < ARRAY_SIZE(command_types)) {
2214                 IPW_DEBUG_HC("Command completed '%s (%d)'\n",
2215                              command_types[cmd->host_command_reg],
2216                              cmd->host_command_reg);
2217         }
2218 #endif
2219         if (cmd->host_command_reg == HOST_COMPLETE)
2220                 priv->status |= STATUS_ENABLED;
2221
2222         if (cmd->host_command_reg == CARD_DISABLE)
2223                 priv->status &= ~STATUS_ENABLED;
2224
2225         priv->status &= ~STATUS_CMD_ACTIVE;
2226
2227         wake_up_interruptible(&priv->wait_command_queue);
2228 }
2229
2230 #ifdef CONFIG_IPW2100_DEBUG
2231 static const char *frame_types[] = {
2232         "COMMAND_STATUS_VAL",
2233         "STATUS_CHANGE_VAL",
2234         "P80211_DATA_VAL",
2235         "P8023_DATA_VAL",
2236         "HOST_NOTIFICATION_VAL"
2237 };
2238 #endif
2239
2240 static int ipw2100_alloc_skb(struct ipw2100_priv *priv,
2241                                     struct ipw2100_rx_packet *packet)
2242 {
2243         packet->skb = dev_alloc_skb(sizeof(struct ipw2100_rx));
2244         if (!packet->skb)
2245                 return -ENOMEM;
2246
2247         packet->rxp = (struct ipw2100_rx *)packet->skb->data;
2248         packet->dma_addr = pci_map_single(priv->pci_dev, packet->skb->data,
2249                                           sizeof(struct ipw2100_rx),
2250                                           PCI_DMA_FROMDEVICE);
2251         /* NOTE: pci_map_single does not return an error code, and 0 is a valid
2252          *       dma_addr */
2253
2254         return 0;
2255 }
2256
2257 #define SEARCH_ERROR   0xffffffff
2258 #define SEARCH_FAIL    0xfffffffe
2259 #define SEARCH_SUCCESS 0xfffffff0
2260 #define SEARCH_DISCARD 0
2261 #define SEARCH_SNAPSHOT 1
2262
2263 #define SNAPSHOT_ADDR(ofs) (priv->snapshot[((ofs) >> 12) & 0xff] + ((ofs) & 0xfff))
2264 static void ipw2100_snapshot_free(struct ipw2100_priv *priv)
2265 {
2266         int i;
2267         if (!priv->snapshot[0])
2268                 return;
2269         for (i = 0; i < 0x30; i++)
2270                 kfree(priv->snapshot[i]);
2271         priv->snapshot[0] = NULL;
2272 }
2273
2274 #ifdef IPW2100_DEBUG_C3
2275 static int ipw2100_snapshot_alloc(struct ipw2100_priv *priv)
2276 {
2277         int i;
2278         if (priv->snapshot[0])
2279                 return 1;
2280         for (i = 0; i < 0x30; i++) {
2281                 priv->snapshot[i] = kmalloc(0x1000, GFP_ATOMIC);
2282                 if (!priv->snapshot[i]) {
2283                         IPW_DEBUG_INFO("%s: Error allocating snapshot "
2284                                        "buffer %d\n", priv->net_dev->name, i);
2285                         while (i > 0)
2286                                 kfree(priv->snapshot[--i]);
2287                         priv->snapshot[0] = NULL;
2288                         return 0;
2289                 }
2290         }
2291
2292         return 1;
2293 }
2294
2295 static u32 ipw2100_match_buf(struct ipw2100_priv *priv, u8 * in_buf,
2296                                     size_t len, int mode)
2297 {
2298         u32 i, j;
2299         u32 tmp;
2300         u8 *s, *d;
2301         u32 ret;
2302
2303         s = in_buf;
2304         if (mode == SEARCH_SNAPSHOT) {
2305                 if (!ipw2100_snapshot_alloc(priv))
2306                         mode = SEARCH_DISCARD;
2307         }
2308
2309         for (ret = SEARCH_FAIL, i = 0; i < 0x30000; i += 4) {
2310                 read_nic_dword(priv->net_dev, i, &tmp);
2311                 if (mode == SEARCH_SNAPSHOT)
2312                         *(u32 *) SNAPSHOT_ADDR(i) = tmp;
2313                 if (ret == SEARCH_FAIL) {
2314                         d = (u8 *) & tmp;
2315                         for (j = 0; j < 4; j++) {
2316                                 if (*s != *d) {
2317                                         s = in_buf;
2318                                         continue;
2319                                 }
2320
2321                                 s++;
2322                                 d++;
2323
2324                                 if ((s - in_buf) == len)
2325                                         ret = (i + j) - len + 1;
2326                         }
2327                 } else if (mode == SEARCH_DISCARD)
2328                         return ret;
2329         }
2330
2331         return ret;
2332 }
2333 #endif
2334
2335 /*
2336  *
2337  * 0) Disconnect the SKB from the firmware (just unmap)
2338  * 1) Pack the ETH header into the SKB
2339  * 2) Pass the SKB to the network stack
2340  *
2341  * When packet is provided by the firmware, it contains the following:
2342  *
2343  * .  ieee80211_hdr
2344  * .  ieee80211_snap_hdr
2345  *
2346  * The size of the constructed ethernet
2347  *
2348  */
2349 #ifdef IPW2100_RX_DEBUG
2350 static u8 packet_data[IPW_RX_NIC_BUFFER_LENGTH];
2351 #endif
2352
2353 static void ipw2100_corruption_detected(struct ipw2100_priv *priv, int i)
2354 {
2355 #ifdef IPW2100_DEBUG_C3
2356         struct ipw2100_status *status = &priv->status_queue.drv[i];
2357         u32 match, reg;
2358         int j;
2359 #endif
2360
2361         IPW_DEBUG_INFO(": PCI latency error detected at 0x%04zX.\n",
2362                        i * sizeof(struct ipw2100_status));
2363
2364 #ifdef IPW2100_DEBUG_C3
2365         /* Halt the firmware so we can get a good image */
2366         write_register(priv->net_dev, IPW_REG_RESET_REG,
2367                        IPW_AUX_HOST_RESET_REG_STOP_MASTER);
2368         j = 5;
2369         do {
2370                 udelay(IPW_WAIT_RESET_MASTER_ASSERT_COMPLETE_DELAY);
2371                 read_register(priv->net_dev, IPW_REG_RESET_REG, &reg);
2372
2373                 if (reg & IPW_AUX_HOST_RESET_REG_MASTER_DISABLED)
2374                         break;
2375         } while (j--);
2376
2377         match = ipw2100_match_buf(priv, (u8 *) status,
2378                                   sizeof(struct ipw2100_status),
2379                                   SEARCH_SNAPSHOT);
2380         if (match < SEARCH_SUCCESS)
2381                 IPW_DEBUG_INFO("%s: DMA status match in Firmware at "
2382                                "offset 0x%06X, length %d:\n",
2383                                priv->net_dev->name, match,
2384                                sizeof(struct ipw2100_status));
2385         else
2386                 IPW_DEBUG_INFO("%s: No DMA status match in "
2387                                "Firmware.\n", priv->net_dev->name);
2388
2389         printk_buf((u8 *) priv->status_queue.drv,
2390                    sizeof(struct ipw2100_status) * RX_QUEUE_LENGTH);
2391 #endif
2392
2393         priv->fatal_error = IPW2100_ERR_C3_CORRUPTION;
2394         priv->net_dev->stats.rx_errors++;
2395         schedule_reset(priv);
2396 }
2397
2398 static void isr_rx(struct ipw2100_priv *priv, int i,
2399                           struct ieee80211_rx_stats *stats)
2400 {
2401         struct net_device *dev = priv->net_dev;
2402         struct ipw2100_status *status = &priv->status_queue.drv[i];
2403         struct ipw2100_rx_packet *packet = &priv->rx_buffers[i];
2404
2405         IPW_DEBUG_RX("Handler...\n");
2406
2407         if (unlikely(status->frame_size > skb_tailroom(packet->skb))) {
2408                 IPW_DEBUG_INFO("%s: frame_size (%u) > skb_tailroom (%u)!"
2409                                "  Dropping.\n",
2410                                dev->name,
2411                                status->frame_size, skb_tailroom(packet->skb));
2412                 dev->stats.rx_errors++;
2413                 return;
2414         }
2415
2416         if (unlikely(!netif_running(dev))) {
2417                 dev->stats.rx_errors++;
2418                 priv->wstats.discard.misc++;
2419                 IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
2420                 return;
2421         }
2422
2423         if (unlikely(priv->ieee->iw_mode != IW_MODE_MONITOR &&
2424                      !(priv->status & STATUS_ASSOCIATED))) {
2425                 IPW_DEBUG_DROP("Dropping packet while not associated.\n");
2426                 priv->wstats.discard.misc++;
2427                 return;
2428         }
2429
2430         pci_unmap_single(priv->pci_dev,
2431                          packet->dma_addr,
2432                          sizeof(struct ipw2100_rx), PCI_DMA_FROMDEVICE);
2433
2434         skb_put(packet->skb, status->frame_size);
2435
2436 #ifdef IPW2100_RX_DEBUG
2437         /* Make a copy of the frame so we can dump it to the logs if
2438          * ieee80211_rx fails */
2439         skb_copy_from_linear_data(packet->skb, packet_data,
2440                                   min_t(u32, status->frame_size,
2441                                              IPW_RX_NIC_BUFFER_LENGTH));
2442 #endif
2443
2444         if (!ieee80211_rx(priv->ieee, packet->skb, stats)) {
2445 #ifdef IPW2100_RX_DEBUG
2446                 IPW_DEBUG_DROP("%s: Non consumed packet:\n",
2447                                dev->name);
2448                 printk_buf(IPW_DL_DROP, packet_data, status->frame_size);
2449 #endif
2450                 dev->stats.rx_errors++;
2451
2452                 /* ieee80211_rx failed, so it didn't free the SKB */
2453                 dev_kfree_skb_any(packet->skb);
2454                 packet->skb = NULL;
2455         }
2456
2457         /* We need to allocate a new SKB and attach it to the RDB. */
2458         if (unlikely(ipw2100_alloc_skb(priv, packet))) {
2459                 printk(KERN_WARNING DRV_NAME ": "
2460                        "%s: Unable to allocate SKB onto RBD ring - disabling "
2461                        "adapter.\n", dev->name);
2462                 /* TODO: schedule adapter shutdown */
2463                 IPW_DEBUG_INFO("TODO: Shutdown adapter...\n");
2464         }
2465
2466         /* Update the RDB entry */
2467         priv->rx_queue.drv[i].host_addr = packet->dma_addr;
2468 }
2469
2470 #ifdef CONFIG_IPW2100_MONITOR
2471
2472 static void isr_rx_monitor(struct ipw2100_priv *priv, int i,
2473                    struct ieee80211_rx_stats *stats)
2474 {
2475         struct net_device *dev = priv->net_dev;
2476         struct ipw2100_status *status = &priv->status_queue.drv[i];
2477         struct ipw2100_rx_packet *packet = &priv->rx_buffers[i];
2478
2479         /* Magic struct that slots into the radiotap header -- no reason
2480          * to build this manually element by element, we can write it much
2481          * more efficiently than we can parse it. ORDER MATTERS HERE */
2482         struct ipw_rt_hdr {
2483                 struct ieee80211_radiotap_header rt_hdr;
2484                 s8 rt_dbmsignal; /* signal in dbM, kluged to signed */
2485         } *ipw_rt;
2486
2487         IPW_DEBUG_RX("Handler...\n");
2488
2489         if (unlikely(status->frame_size > skb_tailroom(packet->skb) -
2490                                 sizeof(struct ipw_rt_hdr))) {
2491                 IPW_DEBUG_INFO("%s: frame_size (%u) > skb_tailroom (%u)!"
2492                                "  Dropping.\n",
2493                                dev->name,
2494                                status->frame_size,
2495                                skb_tailroom(packet->skb));
2496                 dev->stats.rx_errors++;
2497                 return;
2498         }
2499
2500         if (unlikely(!netif_running(dev))) {
2501                 dev->stats.rx_errors++;
2502                 priv->wstats.discard.misc++;
2503                 IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
2504                 return;
2505         }
2506
2507         if (unlikely(priv->config & CFG_CRC_CHECK &&
2508                      status->flags & IPW_STATUS_FLAG_CRC_ERROR)) {
2509                 IPW_DEBUG_RX("CRC error in packet.  Dropping.\n");
2510                 dev->stats.rx_errors++;
2511                 return;
2512         }
2513
2514         pci_unmap_single(priv->pci_dev, packet->dma_addr,
2515                          sizeof(struct ipw2100_rx), PCI_DMA_FROMDEVICE);
2516         memmove(packet->skb->data + sizeof(struct ipw_rt_hdr),
2517                 packet->skb->data, status->frame_size);
2518
2519         ipw_rt = (struct ipw_rt_hdr *) packet->skb->data;
2520
2521         ipw_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
2522         ipw_rt->rt_hdr.it_pad = 0; /* always good to zero */
2523         ipw_rt->rt_hdr.it_len = cpu_to_le16(sizeof(struct ipw_rt_hdr)); /* total hdr+data */
2524
2525         ipw_rt->rt_hdr.it_present = cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
2526
2527         ipw_rt->rt_dbmsignal = status->rssi + IPW2100_RSSI_TO_DBM;
2528
2529         skb_put(packet->skb, status->frame_size + sizeof(struct ipw_rt_hdr));
2530
2531         if (!ieee80211_rx(priv->ieee, packet->skb, stats)) {
2532                 dev->stats.rx_errors++;
2533
2534                 /* ieee80211_rx failed, so it didn't free the SKB */
2535                 dev_kfree_skb_any(packet->skb);
2536                 packet->skb = NULL;
2537         }
2538
2539         /* We need to allocate a new SKB and attach it to the RDB. */
2540         if (unlikely(ipw2100_alloc_skb(priv, packet))) {
2541                 IPW_DEBUG_WARNING(
2542                         "%s: Unable to allocate SKB onto RBD ring - disabling "
2543                         "adapter.\n", dev->name);
2544                 /* TODO: schedule adapter shutdown */
2545                 IPW_DEBUG_INFO("TODO: Shutdown adapter...\n");
2546         }
2547
2548         /* Update the RDB entry */
2549         priv->rx_queue.drv[i].host_addr = packet->dma_addr;
2550 }
2551
2552 #endif
2553
2554 static int ipw2100_corruption_check(struct ipw2100_priv *priv, int i)
2555 {
2556         struct ipw2100_status *status = &priv->status_queue.drv[i];
2557         struct ipw2100_rx *u = priv->rx_buffers[i].rxp;
2558         u16 frame_type = status->status_fields & STATUS_TYPE_MASK;
2559
2560         switch (frame_type) {
2561         case COMMAND_STATUS_VAL:
2562                 return (status->frame_size != sizeof(u->rx_data.command));
2563         case STATUS_CHANGE_VAL:
2564                 return (status->frame_size != sizeof(u->rx_data.status));
2565         case HOST_NOTIFICATION_VAL:
2566                 return (status->frame_size < sizeof(u->rx_data.notification));
2567         case P80211_DATA_VAL:
2568         case P8023_DATA_VAL:
2569 #ifdef CONFIG_IPW2100_MONITOR
2570                 return 0;
2571 #else
2572                 switch (WLAN_FC_GET_TYPE(le16_to_cpu(u->rx_data.header.frame_ctl))) {
2573                 case IEEE80211_FTYPE_MGMT:
2574                 case IEEE80211_FTYPE_CTL:
2575                         return 0;
2576                 case IEEE80211_FTYPE_DATA:
2577                         return (status->frame_size >
2578                                 IPW_MAX_802_11_PAYLOAD_LENGTH);
2579                 }
2580 #endif
2581         }
2582
2583         return 1;
2584 }
2585
2586 /*
2587  * ipw2100 interrupts are disabled at this point, and the ISR
2588  * is the only code that calls this method.  So, we do not need
2589  * to play with any locks.
2590  *
2591  * RX Queue works as follows:
2592  *
2593  * Read index - firmware places packet in entry identified by the
2594  *              Read index and advances Read index.  In this manner,
2595  *              Read index will always point to the next packet to
2596  *              be filled--but not yet valid.
2597  *
2598  * Write index - driver fills this entry with an unused RBD entry.
2599  *               This entry has not filled by the firmware yet.
2600  *
2601  * In between the W and R indexes are the RBDs that have been received
2602  * but not yet processed.
2603  *
2604  * The process of handling packets will start at WRITE + 1 and advance
2605  * until it reaches the READ index.
2606  *
2607  * The WRITE index is cached in the variable 'priv->rx_queue.next'.
2608  *
2609  */
2610 static void __ipw2100_rx_process(struct ipw2100_priv *priv)
2611 {
2612         struct ipw2100_bd_queue *rxq = &priv->rx_queue;
2613         struct ipw2100_status_queue *sq = &priv->status_queue;
2614         struct ipw2100_rx_packet *packet;
2615         u16 frame_type;
2616         u32 r, w, i, s;
2617         struct ipw2100_rx *u;
2618         struct ieee80211_rx_stats stats = {
2619                 .mac_time = jiffies,
2620         };
2621
2622         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_RX_READ_INDEX, &r);
2623         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_RX_WRITE_INDEX, &w);
2624
2625         if (r >= rxq->entries) {
2626                 IPW_DEBUG_RX("exit - bad read index\n");
2627                 return;
2628         }
2629
2630         i = (rxq->next + 1) % rxq->entries;
2631         s = i;
2632         while (i != r) {
2633                 /* IPW_DEBUG_RX("r = %d : w = %d : processing = %d\n",
2634                    r, rxq->next, i); */
2635
2636                 packet = &priv->rx_buffers[i];
2637
2638                 /* Sync the DMA for the STATUS buffer so CPU is sure to get
2639                  * the correct values */
2640                 pci_dma_sync_single_for_cpu(priv->pci_dev,
2641                                             sq->nic +
2642                                             sizeof(struct ipw2100_status) * i,
2643                                             sizeof(struct ipw2100_status),
2644                                             PCI_DMA_FROMDEVICE);
2645
2646                 /* Sync the DMA for the RX buffer so CPU is sure to get
2647                  * the correct values */
2648                 pci_dma_sync_single_for_cpu(priv->pci_dev, packet->dma_addr,
2649                                             sizeof(struct ipw2100_rx),
2650                                             PCI_DMA_FROMDEVICE);
2651
2652                 if (unlikely(ipw2100_corruption_check(priv, i))) {
2653                         ipw2100_corruption_detected(priv, i);
2654                         goto increment;
2655                 }
2656
2657                 u = packet->rxp;
2658                 frame_type = sq->drv[i].status_fields & STATUS_TYPE_MASK;
2659                 stats.rssi = sq->drv[i].rssi + IPW2100_RSSI_TO_DBM;
2660                 stats.len = sq->drv[i].frame_size;
2661
2662                 stats.mask = 0;
2663                 if (stats.rssi != 0)
2664                         stats.mask |= IEEE80211_STATMASK_RSSI;
2665                 stats.freq = IEEE80211_24GHZ_BAND;
2666
2667                 IPW_DEBUG_RX("%s: '%s' frame type received (%d).\n",
2668                              priv->net_dev->name, frame_types[frame_type],
2669                              stats.len);
2670
2671                 switch (frame_type) {
2672                 case COMMAND_STATUS_VAL:
2673                         /* Reset Rx watchdog */
2674                         isr_rx_complete_command(priv, &u->rx_data.command);
2675                         break;
2676
2677                 case STATUS_CHANGE_VAL:
2678                         isr_status_change(priv, u->rx_data.status);
2679                         break;
2680
2681                 case P80211_DATA_VAL:
2682                 case P8023_DATA_VAL:
2683 #ifdef CONFIG_IPW2100_MONITOR
2684                         if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
2685                                 isr_rx_monitor(priv, i, &stats);
2686                                 break;
2687                         }
2688 #endif
2689                         if (stats.len < sizeof(struct ieee80211_hdr_3addr))
2690                                 break;
2691                         switch (WLAN_FC_GET_TYPE(le16_to_cpu(u->rx_data.header.frame_ctl))) {
2692                         case IEEE80211_FTYPE_MGMT:
2693                                 ieee80211_rx_mgt(priv->ieee,
2694                                                  &u->rx_data.header, &stats);
2695                                 break;
2696
2697                         case IEEE80211_FTYPE_CTL:
2698                                 break;
2699
2700                         case IEEE80211_FTYPE_DATA:
2701                                 isr_rx(priv, i, &stats);
2702                                 break;
2703
2704                         }
2705                         break;
2706                 }
2707
2708               increment:
2709                 /* clear status field associated with this RBD */
2710                 rxq->drv[i].status.info.field = 0;
2711
2712                 i = (i + 1) % rxq->entries;
2713         }
2714
2715         if (i != s) {
2716                 /* backtrack one entry, wrapping to end if at 0 */
2717                 rxq->next = (i ? i : rxq->entries) - 1;
2718
2719                 write_register(priv->net_dev,
2720                                IPW_MEM_HOST_SHARED_RX_WRITE_INDEX, rxq->next);
2721         }
2722 }
2723
2724 /*
2725  * __ipw2100_tx_process
2726  *
2727  * This routine will determine whether the next packet on
2728  * the fw_pend_list has been processed by the firmware yet.
2729  *
2730  * If not, then it does nothing and returns.
2731  *
2732  * If so, then it removes the item from the fw_pend_list, frees
2733  * any associated storage, and places the item back on the
2734  * free list of its source (either msg_free_list or tx_free_list)
2735  *
2736  * TX Queue works as follows:
2737  *
2738  * Read index - points to the next TBD that the firmware will
2739  *              process.  The firmware will read the data, and once
2740  *              done processing, it will advance the Read index.
2741  *
2742  * Write index - driver fills this entry with an constructed TBD
2743  *               entry.  The Write index is not advanced until the
2744  *               packet has been configured.
2745  *
2746  * In between the W and R indexes are the TBDs that have NOT been
2747  * processed.  Lagging behind the R index are packets that have
2748  * been processed but have not been freed by the driver.
2749  *
2750  * In order to free old storage, an internal index will be maintained
2751  * that points to the next packet to be freed.  When all used
2752  * packets have been freed, the oldest index will be the same as the
2753  * firmware's read index.
2754  *
2755  * The OLDEST index is cached in the variable 'priv->tx_queue.oldest'
2756  *
2757  * Because the TBD structure can not contain arbitrary data, the
2758  * driver must keep an internal queue of cached allocations such that
2759  * it can put that data back into the tx_free_list and msg_free_list
2760  * for use by future command and data packets.
2761  *
2762  */
2763 static int __ipw2100_tx_process(struct ipw2100_priv *priv)
2764 {
2765         struct ipw2100_bd_queue *txq = &priv->tx_queue;
2766         struct ipw2100_bd *tbd;
2767         struct list_head *element;
2768         struct ipw2100_tx_packet *packet;
2769         int descriptors_used;
2770         int e, i;
2771         u32 r, w, frag_num = 0;
2772
2773         if (list_empty(&priv->fw_pend_list))
2774                 return 0;
2775
2776         element = priv->fw_pend_list.next;
2777
2778         packet = list_entry(element, struct ipw2100_tx_packet, list);
2779         tbd = &txq->drv[packet->index];
2780
2781         /* Determine how many TBD entries must be finished... */
2782         switch (packet->type) {
2783         case COMMAND:
2784                 /* COMMAND uses only one slot; don't advance */
2785                 descriptors_used = 1;
2786                 e = txq->oldest;
2787                 break;
2788
2789         case DATA:
2790                 /* DATA uses two slots; advance and loop position. */
2791                 descriptors_used = tbd->num_fragments;
2792                 frag_num = tbd->num_fragments - 1;
2793                 e = txq->oldest + frag_num;
2794                 e %= txq->entries;
2795                 break;
2796
2797         default:
2798                 printk(KERN_WARNING DRV_NAME ": %s: Bad fw_pend_list entry!\n",
2799                        priv->net_dev->name);
2800                 return 0;
2801         }
2802
2803         /* if the last TBD is not done by NIC yet, then packet is
2804          * not ready to be released.
2805          *
2806          */
2807         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_TX_QUEUE_READ_INDEX,
2808                       &r);
2809         read_register(priv->net_dev, IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX,
2810                       &w);
2811         if (w != txq->next)
2812                 printk(KERN_WARNING DRV_NAME ": %s: write index mismatch\n",
2813                        priv->net_dev->name);
2814
2815         /*
2816          * txq->next is the index of the last packet written txq->oldest is
2817          * the index of the r is the index of the next packet to be read by
2818          * firmware
2819          */
2820
2821         /*
2822          * Quick graphic to help you visualize the following
2823          * if / else statement
2824          *
2825          * ===>|                     s---->|===============
2826          *                               e>|
2827          * | a | b | c | d | e | f | g | h | i | j | k | l
2828          *       r---->|
2829          *               w
2830          *
2831          * w - updated by driver
2832          * r - updated by firmware
2833          * s - start of oldest BD entry (txq->oldest)
2834          * e - end of oldest BD entry
2835          *
2836          */
2837         if (!((r <= w && (e < r || e >= w)) || (e < r && e >= w))) {
2838                 IPW_DEBUG_TX("exit - no processed packets ready to release.\n");
2839                 return 0;
2840         }
2841
2842         list_del(element);
2843         DEC_STAT(&priv->fw_pend_stat);
2844
2845 #ifdef CONFIG_IPW2100_DEBUG
2846         {
2847                 i = txq->oldest;
2848                 IPW_DEBUG_TX("TX%d V=%p P=%04X T=%04X L=%d\n", i,
2849                              &txq->drv[i],
2850                              (u32) (txq->nic + i * sizeof(struct ipw2100_bd)),
2851                              txq->drv[i].host_addr, txq->drv[i].buf_length);
2852
2853                 if (packet->type == DATA) {
2854                         i = (i + 1) % txq->entries;
2855
2856                         IPW_DEBUG_TX("TX%d V=%p P=%04X T=%04X L=%d\n", i,
2857                                      &txq->drv[i],
2858                                      (u32) (txq->nic + i *
2859                                             sizeof(struct ipw2100_bd)),
2860                                      (u32) txq->drv[i].host_addr,
2861                                      txq->drv[i].buf_length);
2862                 }
2863         }
2864 #endif
2865
2866         switch (packet->type) {
2867         case DATA:
2868                 if (txq->drv[txq->oldest].status.info.fields.txType != 0)
2869                         printk(KERN_WARNING DRV_NAME ": %s: Queue mismatch.  "
2870                                "Expecting DATA TBD but pulled "
2871                                "something else: ids %d=%d.\n",
2872                                priv->net_dev->name, txq->oldest, packet->index);
2873
2874                 /* DATA packet; we have to unmap and free the SKB */
2875                 for (i = 0; i < frag_num; i++) {
2876                         tbd = &txq->drv[(packet->index + 1 + i) % txq->entries];
2877
2878                         IPW_DEBUG_TX("TX%d P=%08x L=%d\n",
2879                                      (packet->index + 1 + i) % txq->entries,
2880                                      tbd->host_addr, tbd->buf_length);
2881
2882                         pci_unmap_single(priv->pci_dev,
2883                                          tbd->host_addr,
2884                                          tbd->buf_length, PCI_DMA_TODEVICE);
2885                 }
2886
2887                 ieee80211_txb_free(packet->info.d_struct.txb);
2888                 packet->info.d_struct.txb = NULL;
2889
2890                 list_add_tail(element, &priv->tx_free_list);
2891                 INC_STAT(&priv->tx_free_stat);
2892
2893                 /* We have a free slot in the Tx queue, so wake up the
2894                  * transmit layer if it is stopped. */
2895                 if (priv->status & STATUS_ASSOCIATED)
2896                         netif_wake_queue(priv->net_dev);
2897
2898                 /* A packet was processed by the hardware, so update the
2899                  * watchdog */
2900                 priv->net_dev->trans_start = jiffies;
2901
2902                 break;
2903
2904         case COMMAND:
2905                 if (txq->drv[txq->oldest].status.info.fields.txType != 1)
2906                         printk(KERN_WARNING DRV_NAME ": %s: Queue mismatch.  "
2907                                "Expecting COMMAND TBD but pulled "
2908                                "something else: ids %d=%d.\n",
2909                                priv->net_dev->name, txq->oldest, packet->index);
2910
2911 #ifdef CONFIG_IPW2100_DEBUG
2912                 if (packet->info.c_struct.cmd->host_command_reg <
2913                     ARRAY_SIZE(command_types))
2914                         IPW_DEBUG_TX("Command '%s (%d)' processed: %d.\n",
2915                                      command_types[packet->info.c_struct.cmd->
2916                                                    host_command_reg],
2917                                      packet->info.c_struct.cmd->
2918                                      host_command_reg,
2919                                      packet->info.c_struct.cmd->cmd_status_reg);
2920 #endif
2921
2922                 list_add_tail(element, &priv->msg_free_list);
2923                 INC_STAT(&priv->msg_free_stat);
2924                 break;
2925         }
2926
2927         /* advance oldest used TBD pointer to start of next entry */
2928         txq->oldest = (e + 1) % txq->entries;
2929         /* increase available TBDs number */
2930         txq->available += descriptors_used;
2931         SET_STAT(&priv->txq_stat, txq->available);
2932
2933         IPW_DEBUG_TX("packet latency (send to process)  %ld jiffies\n",
2934                      jiffies - packet->jiffy_start);
2935
2936         return (!list_empty(&priv->fw_pend_list));
2937 }
2938
2939 static inline void __ipw2100_tx_complete(struct ipw2100_priv *priv)
2940 {
2941         int i = 0;
2942
2943         while (__ipw2100_tx_process(priv) && i < 200)
2944                 i++;
2945
2946         if (i == 200) {
2947                 printk(KERN_WARNING DRV_NAME ": "
2948                        "%s: Driver is running slow (%d iters).\n",
2949                        priv->net_dev->name, i);
2950         }
2951 }
2952
2953 static void ipw2100_tx_send_commands(struct ipw2100_priv *priv)
2954 {
2955         struct list_head *element;
2956         struct ipw2100_tx_packet *packet;
2957         struct ipw2100_bd_queue *txq = &priv->tx_queue;
2958         struct ipw2100_bd *tbd;
2959         int next = txq->next;
2960
2961         while (!list_empty(&priv->msg_pend_list)) {
2962                 /* if there isn't enough space in TBD queue, then
2963                  * don't stuff a new one in.
2964                  * NOTE: 3 are needed as a command will take one,
2965                  *       and there is a minimum of 2 that must be
2966                  *       maintained between the r and w indexes
2967                  */
2968                 if (txq->available <= 3) {
2969                         IPW_DEBUG_TX("no room in tx_queue\n");
2970                         break;
2971                 }
2972
2973                 element = priv->msg_pend_list.next;
2974                 list_del(element);
2975                 DEC_STAT(&priv->msg_pend_stat);
2976
2977                 packet = list_entry(element, struct ipw2100_tx_packet, list);
2978
2979                 IPW_DEBUG_TX("using TBD at virt=%p, phys=%p\n",
2980                              &txq->drv[txq->next],
2981                              (void *)(txq->nic + txq->next *
2982                                       sizeof(struct ipw2100_bd)));
2983
2984                 packet->index = txq->next;
2985
2986                 tbd = &txq->drv[txq->next];
2987
2988                 /* initialize TBD */
2989                 tbd->host_addr = packet->info.c_struct.cmd_phys;
2990                 tbd->buf_length = sizeof(struct ipw2100_cmd_header);
2991                 /* not marking number of fragments causes problems
2992                  * with f/w debug version */
2993                 tbd->num_fragments = 1;
2994                 tbd->status.info.field =
2995                     IPW_BD_STATUS_TX_FRAME_COMMAND |
2996                     IPW_BD_STATUS_TX_INTERRUPT_ENABLE;
2997
2998                 /* update TBD queue counters */
2999                 txq->next++;
3000                 txq->next %= txq->entries;
3001                 txq->available--;
3002                 DEC_STAT(&priv->txq_stat);
3003
3004                 list_add_tail(element, &priv->fw_pend_list);
3005                 INC_STAT(&priv->fw_pend_stat);
3006         }
3007
3008         if (txq->next != next) {
3009                 /* kick off the DMA by notifying firmware the
3010                  * write index has moved; make sure TBD stores are sync'd */
3011                 wmb();
3012                 write_register(priv->net_dev,
3013                                IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX,
3014                                txq->next);
3015         }
3016 }
3017
3018 /*
3019  * ipw2100_tx_send_data
3020  *
3021  */
3022 static void ipw2100_tx_send_data(struct ipw2100_priv *priv)
3023 {
3024         struct list_head *element;
3025         struct ipw2100_tx_packet *packet;
3026         struct ipw2100_bd_queue *txq = &priv->tx_queue;
3027         struct ipw2100_bd *tbd;
3028         int next =