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