xref: /linux/drivers/net/wireless/intel/ipw2x00/ipw2100.c (revision fcc79e1714e8c2b8e216dc3149812edd37884eef)
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 = kcalloc(geo->bg_channels,
1889 					    sizeof(struct ieee80211_channel),
1890 					    GFP_KERNEL);
1891 		if (!bg_band->channels) {
1892 			ipw2100_down(priv);
1893 			return -ENOMEM;
1894 		}
1895 		/* translate geo->bg to bg_band.channels */
1896 		for (i = 0; i < geo->bg_channels; i++) {
1897 			bg_band->channels[i].band = NL80211_BAND_2GHZ;
1898 			bg_band->channels[i].center_freq = geo->bg[i].freq;
1899 			bg_band->channels[i].hw_value = geo->bg[i].channel;
1900 			bg_band->channels[i].max_power = geo->bg[i].max_power;
1901 			if (geo->bg[i].flags & LIBIPW_CH_PASSIVE_ONLY)
1902 				bg_band->channels[i].flags |=
1903 					IEEE80211_CHAN_NO_IR;
1904 			if (geo->bg[i].flags & LIBIPW_CH_NO_IBSS)
1905 				bg_band->channels[i].flags |=
1906 					IEEE80211_CHAN_NO_IR;
1907 			if (geo->bg[i].flags & LIBIPW_CH_RADAR_DETECT)
1908 				bg_band->channels[i].flags |=
1909 					IEEE80211_CHAN_RADAR;
1910 			/* No equivalent for LIBIPW_CH_80211H_RULES,
1911 			   LIBIPW_CH_UNIFORM_SPREADING, or
1912 			   LIBIPW_CH_B_ONLY... */
1913 		}
1914 		/* point at bitrate info */
1915 		bg_band->bitrates = ipw2100_bg_rates;
1916 		bg_band->n_bitrates = RATE_COUNT;
1917 
1918 		wdev->wiphy->bands[NL80211_BAND_2GHZ] = bg_band;
1919 	}
1920 
1921 	wdev->wiphy->cipher_suites = ipw_cipher_suites;
1922 	wdev->wiphy->n_cipher_suites = ARRAY_SIZE(ipw_cipher_suites);
1923 
1924 	set_wiphy_dev(wdev->wiphy, &priv->pci_dev->dev);
1925 	if (wiphy_register(wdev->wiphy))
1926 		return -EIO;
1927 	return 0;
1928 }
1929 
1930 static void ipw2100_reset_adapter(struct work_struct *work)
1931 {
1932 	struct ipw2100_priv *priv =
1933 		container_of(work, struct ipw2100_priv, reset_work.work);
1934 	unsigned long flags;
1935 	union iwreq_data wrqu = {
1936 		.ap_addr = {
1937 			    .sa_family = ARPHRD_ETHER}
1938 	};
1939 	int associated = priv->status & STATUS_ASSOCIATED;
1940 
1941 	spin_lock_irqsave(&priv->low_lock, flags);
1942 	IPW_DEBUG_INFO(": %s: Restarting adapter.\n", priv->net_dev->name);
1943 	priv->resets++;
1944 	priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
1945 	priv->status |= STATUS_SECURITY_UPDATED;
1946 
1947 	/* Force a power cycle even if interface hasn't been opened
1948 	 * yet */
1949 	cancel_delayed_work(&priv->reset_work);
1950 	priv->status |= STATUS_RESET_PENDING;
1951 	spin_unlock_irqrestore(&priv->low_lock, flags);
1952 
1953 	mutex_lock(&priv->action_mutex);
1954 	/* stop timed checks so that they don't interfere with reset */
1955 	priv->stop_hang_check = 1;
1956 	cancel_delayed_work(&priv->hang_check);
1957 
1958 	/* We have to signal any supplicant if we are disassociating */
1959 	if (associated)
1960 		wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
1961 
1962 	ipw2100_up(priv, 0);
1963 	mutex_unlock(&priv->action_mutex);
1964 
1965 }
1966 
1967 static void isr_indicate_associated(struct ipw2100_priv *priv, u32 status)
1968 {
1969 
1970 #define MAC_ASSOCIATION_READ_DELAY (HZ)
1971 	int ret;
1972 	unsigned int len, essid_len;
1973 	char essid[IW_ESSID_MAX_SIZE];
1974 	u32 txrate;
1975 	u32 chan;
1976 	char *txratename;
1977 	u8 bssid[ETH_ALEN];
1978 
1979 	/*
1980 	 * TBD: BSSID is usually 00:00:00:00:00:00 here and not
1981 	 *      an actual MAC of the AP. Seems like FW sets this
1982 	 *      address too late. Read it later and expose through
1983 	 *      /proc or schedule a later task to query and update
1984 	 */
1985 
1986 	essid_len = IW_ESSID_MAX_SIZE;
1987 	ret = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_SSID,
1988 				  essid, &essid_len);
1989 	if (ret) {
1990 		IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1991 			       __LINE__);
1992 		return;
1993 	}
1994 
1995 	len = sizeof(u32);
1996 	ret = ipw2100_get_ordinal(priv, IPW_ORD_CURRENT_TX_RATE, &txrate, &len);
1997 	if (ret) {
1998 		IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
1999 			       __LINE__);
2000 		return;
2001 	}
2002 
2003 	len = sizeof(u32);
2004 	ret = ipw2100_get_ordinal(priv, IPW_ORD_OUR_FREQ, &chan, &len);
2005 	if (ret) {
2006 		IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
2007 			       __LINE__);
2008 		return;
2009 	}
2010 	len = ETH_ALEN;
2011 	ret = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_AP_BSSID, bssid,
2012 				  &len);
2013 	if (ret) {
2014 		IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
2015 			       __LINE__);
2016 		return;
2017 	}
2018 	memcpy(priv->ieee->bssid, bssid, ETH_ALEN);
2019 
2020 	switch (txrate) {
2021 	case TX_RATE_1_MBIT:
2022 		txratename = "1Mbps";
2023 		break;
2024 	case TX_RATE_2_MBIT:
2025 		txratename = "2Mbsp";
2026 		break;
2027 	case TX_RATE_5_5_MBIT:
2028 		txratename = "5.5Mbps";
2029 		break;
2030 	case TX_RATE_11_MBIT:
2031 		txratename = "11Mbps";
2032 		break;
2033 	default:
2034 		IPW_DEBUG_INFO("Unknown rate: %d\n", txrate);
2035 		txratename = "unknown rate";
2036 		break;
2037 	}
2038 
2039 	IPW_DEBUG_INFO("%s: Associated with '%*pE' at %s, channel %d (BSSID=%pM)\n",
2040 		       priv->net_dev->name, essid_len, essid,
2041 		       txratename, chan, bssid);
2042 
2043 	/* now we copy read ssid into dev */
2044 	if (!(priv->config & CFG_STATIC_ESSID)) {
2045 		priv->essid_len = min((u8) essid_len, (u8) IW_ESSID_MAX_SIZE);
2046 		memcpy(priv->essid, essid, priv->essid_len);
2047 	}
2048 	priv->channel = chan;
2049 	memcpy(priv->bssid, bssid, ETH_ALEN);
2050 
2051 	priv->status |= STATUS_ASSOCIATING;
2052 	priv->connect_start = ktime_get_boottime_seconds();
2053 
2054 	schedule_delayed_work(&priv->wx_event_work, HZ / 10);
2055 }
2056 
2057 static int ipw2100_set_essid(struct ipw2100_priv *priv, char *essid,
2058 			     int length, int batch_mode)
2059 {
2060 	int ssid_len = min(length, IW_ESSID_MAX_SIZE);
2061 	struct host_command cmd = {
2062 		.host_command = SSID,
2063 		.host_command_sequence = 0,
2064 		.host_command_length = ssid_len
2065 	};
2066 	int err;
2067 
2068 	IPW_DEBUG_HC("SSID: '%*pE'\n", ssid_len, essid);
2069 
2070 	if (ssid_len)
2071 		memcpy(cmd.host_command_parameters, essid, ssid_len);
2072 
2073 	if (!batch_mode) {
2074 		err = ipw2100_disable_adapter(priv);
2075 		if (err)
2076 			return err;
2077 	}
2078 
2079 	/* Bug in FW currently doesn't honor bit 0 in SET_SCAN_OPTIONS to
2080 	 * disable auto association -- so we cheat by setting a bogus SSID */
2081 	if (!ssid_len && !(priv->config & CFG_ASSOCIATE)) {
2082 		int i;
2083 		u8 *bogus = (u8 *) cmd.host_command_parameters;
2084 		for (i = 0; i < IW_ESSID_MAX_SIZE; i++)
2085 			bogus[i] = 0x18 + i;
2086 		cmd.host_command_length = IW_ESSID_MAX_SIZE;
2087 	}
2088 
2089 	/* NOTE:  We always send the SSID command even if the provided ESSID is
2090 	 * the same as what we currently think is set. */
2091 
2092 	err = ipw2100_hw_send_command(priv, &cmd);
2093 	if (!err) {
2094 		memset(priv->essid + ssid_len, 0, IW_ESSID_MAX_SIZE - ssid_len);
2095 		memcpy(priv->essid, essid, ssid_len);
2096 		priv->essid_len = ssid_len;
2097 	}
2098 
2099 	if (!batch_mode) {
2100 		if (ipw2100_enable_adapter(priv))
2101 			err = -EIO;
2102 	}
2103 
2104 	return err;
2105 }
2106 
2107 static void isr_indicate_association_lost(struct ipw2100_priv *priv, u32 status)
2108 {
2109 	IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE | IPW_DL_ASSOC,
2110 		  "disassociated: '%*pE' %pM\n", priv->essid_len, priv->essid,
2111 		  priv->bssid);
2112 
2113 	priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
2114 
2115 	if (priv->status & STATUS_STOPPING) {
2116 		IPW_DEBUG_INFO("Card is stopping itself, discard ASSN_LOST.\n");
2117 		return;
2118 	}
2119 
2120 	eth_zero_addr(priv->bssid);
2121 	eth_zero_addr(priv->ieee->bssid);
2122 
2123 	netif_carrier_off(priv->net_dev);
2124 	netif_stop_queue(priv->net_dev);
2125 
2126 	if (!(priv->status & STATUS_RUNNING))
2127 		return;
2128 
2129 	if (priv->status & STATUS_SECURITY_UPDATED)
2130 		schedule_delayed_work(&priv->security_work, 0);
2131 
2132 	schedule_delayed_work(&priv->wx_event_work, 0);
2133 }
2134 
2135 static void isr_indicate_rf_kill(struct ipw2100_priv *priv, u32 status)
2136 {
2137 	IPW_DEBUG_INFO("%s: RF Kill state changed to radio OFF.\n",
2138 		       priv->net_dev->name);
2139 
2140 	/* RF_KILL is now enabled (else we wouldn't be here) */
2141 	wiphy_rfkill_set_hw_state(priv->ieee->wdev.wiphy, true);
2142 	priv->status |= STATUS_RF_KILL_HW;
2143 
2144 	/* Make sure the RF Kill check timer is running */
2145 	priv->stop_rf_kill = 0;
2146 	mod_delayed_work(system_wq, &priv->rf_kill, round_jiffies_relative(HZ));
2147 }
2148 
2149 static void ipw2100_scan_event(struct work_struct *work)
2150 {
2151 	struct ipw2100_priv *priv = container_of(work, struct ipw2100_priv,
2152 						 scan_event.work);
2153 	union iwreq_data wrqu;
2154 
2155 	wrqu.data.length = 0;
2156 	wrqu.data.flags = 0;
2157 	wireless_send_event(priv->net_dev, SIOCGIWSCAN, &wrqu, NULL);
2158 }
2159 
2160 static void isr_scan_complete(struct ipw2100_priv *priv, u32 status)
2161 {
2162 	IPW_DEBUG_SCAN("scan complete\n");
2163 	/* Age the scan results... */
2164 	priv->ieee->scans++;
2165 	priv->status &= ~STATUS_SCANNING;
2166 
2167 	/* Only userspace-requested scan completion events go out immediately */
2168 	if (!priv->user_requested_scan) {
2169 		schedule_delayed_work(&priv->scan_event,
2170 				      round_jiffies_relative(msecs_to_jiffies(4000)));
2171 	} else {
2172 		priv->user_requested_scan = 0;
2173 		mod_delayed_work(system_wq, &priv->scan_event, 0);
2174 	}
2175 }
2176 
2177 #ifdef CONFIG_IPW2100_DEBUG
2178 #define IPW2100_HANDLER(v, f) { v, f, # v }
2179 struct ipw2100_status_indicator {
2180 	int status;
2181 	void (*cb) (struct ipw2100_priv * priv, u32 status);
2182 	char *name;
2183 };
2184 #else
2185 #define IPW2100_HANDLER(v, f) { v, f }
2186 struct ipw2100_status_indicator {
2187 	int status;
2188 	void (*cb) (struct ipw2100_priv * priv, u32 status);
2189 };
2190 #endif				/* CONFIG_IPW2100_DEBUG */
2191 
2192 static void isr_indicate_scanning(struct ipw2100_priv *priv, u32 status)
2193 {
2194 	IPW_DEBUG_SCAN("Scanning...\n");
2195 	priv->status |= STATUS_SCANNING;
2196 }
2197 
2198 static const struct ipw2100_status_indicator status_handlers[] = {
2199 	IPW2100_HANDLER(IPW_STATE_INITIALIZED, NULL),
2200 	IPW2100_HANDLER(IPW_STATE_COUNTRY_FOUND, NULL),
2201 	IPW2100_HANDLER(IPW_STATE_ASSOCIATED, isr_indicate_associated),
2202 	IPW2100_HANDLER(IPW_STATE_ASSN_LOST, isr_indicate_association_lost),
2203 	IPW2100_HANDLER(IPW_STATE_ASSN_CHANGED, NULL),
2204 	IPW2100_HANDLER(IPW_STATE_SCAN_COMPLETE, isr_scan_complete),
2205 	IPW2100_HANDLER(IPW_STATE_ENTERED_PSP, NULL),
2206 	IPW2100_HANDLER(IPW_STATE_LEFT_PSP, NULL),
2207 	IPW2100_HANDLER(IPW_STATE_RF_KILL, isr_indicate_rf_kill),
2208 	IPW2100_HANDLER(IPW_STATE_DISABLED, NULL),
2209 	IPW2100_HANDLER(IPW_STATE_POWER_DOWN, NULL),
2210 	IPW2100_HANDLER(IPW_STATE_SCANNING, isr_indicate_scanning),
2211 	IPW2100_HANDLER(-1, NULL)
2212 };
2213 
2214 static void isr_status_change(struct ipw2100_priv *priv, int status)
2215 {
2216 	int i;
2217 
2218 	if (status == IPW_STATE_SCANNING &&
2219 	    priv->status & STATUS_ASSOCIATED &&
2220 	    !(priv->status & STATUS_SCANNING)) {
2221 		IPW_DEBUG_INFO("Scan detected while associated, with "
2222 			       "no scan request.  Restarting firmware.\n");
2223 
2224 		/* Wake up any sleeping jobs */
2225 		schedule_reset(priv);
2226 	}
2227 
2228 	for (i = 0; status_handlers[i].status != -1; i++) {
2229 		if (status == status_handlers[i].status) {
2230 			IPW_DEBUG_NOTIF("Status change: %s\n",
2231 					status_handlers[i].name);
2232 			if (status_handlers[i].cb)
2233 				status_handlers[i].cb(priv, status);
2234 			priv->wstats.status = status;
2235 			return;
2236 		}
2237 	}
2238 
2239 	IPW_DEBUG_NOTIF("unknown status received: %04x\n", status);
2240 }
2241 
2242 static void isr_rx_complete_command(struct ipw2100_priv *priv,
2243 				    struct ipw2100_cmd_header *cmd)
2244 {
2245 #ifdef CONFIG_IPW2100_DEBUG
2246 	if (cmd->host_command_reg < ARRAY_SIZE(command_types)) {
2247 		IPW_DEBUG_HC("Command completed '%s (%d)'\n",
2248 			     command_types[cmd->host_command_reg],
2249 			     cmd->host_command_reg);
2250 	}
2251 #endif
2252 	if (cmd->host_command_reg == HOST_COMPLETE)
2253 		priv->status |= STATUS_ENABLED;
2254 
2255 	if (cmd->host_command_reg == CARD_DISABLE)
2256 		priv->status &= ~STATUS_ENABLED;
2257 
2258 	priv->status &= ~STATUS_CMD_ACTIVE;
2259 
2260 	wake_up_interruptible(&priv->wait_command_queue);
2261 }
2262 
2263 #ifdef CONFIG_IPW2100_DEBUG
2264 static const char *frame_types[] = {
2265 	"COMMAND_STATUS_VAL",
2266 	"STATUS_CHANGE_VAL",
2267 	"P80211_DATA_VAL",
2268 	"P8023_DATA_VAL",
2269 	"HOST_NOTIFICATION_VAL"
2270 };
2271 #endif
2272 
2273 static int ipw2100_alloc_skb(struct ipw2100_priv *priv,
2274 				    struct ipw2100_rx_packet *packet)
2275 {
2276 	packet->skb = dev_alloc_skb(sizeof(struct ipw2100_rx));
2277 	if (!packet->skb)
2278 		return -ENOMEM;
2279 
2280 	packet->rxp = (struct ipw2100_rx *)packet->skb->data;
2281 	packet->dma_addr = dma_map_single(&priv->pci_dev->dev,
2282 					  packet->skb->data,
2283 					  sizeof(struct ipw2100_rx),
2284 					  DMA_FROM_DEVICE);
2285 	if (dma_mapping_error(&priv->pci_dev->dev, packet->dma_addr)) {
2286 		dev_kfree_skb(packet->skb);
2287 		return -ENOMEM;
2288 	}
2289 
2290 	return 0;
2291 }
2292 
2293 #define SEARCH_ERROR   0xffffffff
2294 #define SEARCH_FAIL    0xfffffffe
2295 #define SEARCH_SUCCESS 0xfffffff0
2296 #define SEARCH_DISCARD 0
2297 #define SEARCH_SNAPSHOT 1
2298 
2299 #define SNAPSHOT_ADDR(ofs) (priv->snapshot[((ofs) >> 12) & 0xff] + ((ofs) & 0xfff))
2300 static void ipw2100_snapshot_free(struct ipw2100_priv *priv)
2301 {
2302 	int i;
2303 	if (!priv->snapshot[0])
2304 		return;
2305 	for (i = 0; i < 0x30; i++)
2306 		kfree(priv->snapshot[i]);
2307 	priv->snapshot[0] = NULL;
2308 }
2309 
2310 #ifdef IPW2100_DEBUG_C3
2311 static int ipw2100_snapshot_alloc(struct ipw2100_priv *priv)
2312 {
2313 	int i;
2314 	if (priv->snapshot[0])
2315 		return 1;
2316 	for (i = 0; i < 0x30; i++) {
2317 		priv->snapshot[i] = kmalloc(0x1000, GFP_ATOMIC);
2318 		if (!priv->snapshot[i]) {
2319 			IPW_DEBUG_INFO("%s: Error allocating snapshot "
2320 				       "buffer %d\n", priv->net_dev->name, i);
2321 			while (i > 0)
2322 				kfree(priv->snapshot[--i]);
2323 			priv->snapshot[0] = NULL;
2324 			return 0;
2325 		}
2326 	}
2327 
2328 	return 1;
2329 }
2330 
2331 static u32 ipw2100_match_buf(struct ipw2100_priv *priv, u8 * in_buf,
2332 				    size_t len, int mode)
2333 {
2334 	u32 i, j;
2335 	u32 tmp;
2336 	u8 *s, *d;
2337 	u32 ret;
2338 
2339 	s = in_buf;
2340 	if (mode == SEARCH_SNAPSHOT) {
2341 		if (!ipw2100_snapshot_alloc(priv))
2342 			mode = SEARCH_DISCARD;
2343 	}
2344 
2345 	for (ret = SEARCH_FAIL, i = 0; i < 0x30000; i += 4) {
2346 		read_nic_dword(priv->net_dev, i, &tmp);
2347 		if (mode == SEARCH_SNAPSHOT)
2348 			*(u32 *) SNAPSHOT_ADDR(i) = tmp;
2349 		if (ret == SEARCH_FAIL) {
2350 			d = (u8 *) & tmp;
2351 			for (j = 0; j < 4; j++) {
2352 				if (*s != *d) {
2353 					s = in_buf;
2354 					continue;
2355 				}
2356 
2357 				s++;
2358 				d++;
2359 
2360 				if ((s - in_buf) == len)
2361 					ret = (i + j) - len + 1;
2362 			}
2363 		} else if (mode == SEARCH_DISCARD)
2364 			return ret;
2365 	}
2366 
2367 	return ret;
2368 }
2369 #endif
2370 
2371 /*
2372  *
2373  * 0) Disconnect the SKB from the firmware (just unmap)
2374  * 1) Pack the ETH header into the SKB
2375  * 2) Pass the SKB to the network stack
2376  *
2377  * When packet is provided by the firmware, it contains the following:
2378  *
2379  * .  libipw_hdr
2380  * .  libipw_snap_hdr
2381  *
2382  * The size of the constructed ethernet
2383  *
2384  */
2385 #ifdef IPW2100_RX_DEBUG
2386 static u8 packet_data[IPW_RX_NIC_BUFFER_LENGTH];
2387 #endif
2388 
2389 static void ipw2100_corruption_detected(struct ipw2100_priv *priv, int i)
2390 {
2391 #ifdef IPW2100_DEBUG_C3
2392 	struct ipw2100_status *status = &priv->status_queue.drv[i];
2393 	u32 match, reg;
2394 	int j;
2395 #endif
2396 
2397 	IPW_DEBUG_INFO(": PCI latency error detected at 0x%04zX.\n",
2398 		       i * sizeof(struct ipw2100_status));
2399 
2400 #ifdef IPW2100_DEBUG_C3
2401 	/* Halt the firmware so we can get a good image */
2402 	write_register(priv->net_dev, IPW_REG_RESET_REG,
2403 		       IPW_AUX_HOST_RESET_REG_STOP_MASTER);
2404 	j = 5;
2405 	do {
2406 		udelay(IPW_WAIT_RESET_MASTER_ASSERT_COMPLETE_DELAY);
2407 		read_register(priv->net_dev, IPW_REG_RESET_REG, &reg);
2408 
2409 		if (reg & IPW_AUX_HOST_RESET_REG_MASTER_DISABLED)
2410 			break;
2411 	} while (j--);
2412 
2413 	match = ipw2100_match_buf(priv, (u8 *) status,
2414 				  sizeof(struct ipw2100_status),
2415 				  SEARCH_SNAPSHOT);
2416 	if (match < SEARCH_SUCCESS)
2417 		IPW_DEBUG_INFO("%s: DMA status match in Firmware at "
2418 			       "offset 0x%06X, length %d:\n",
2419 			       priv->net_dev->name, match,
2420 			       sizeof(struct ipw2100_status));
2421 	else
2422 		IPW_DEBUG_INFO("%s: No DMA status match in "
2423 			       "Firmware.\n", priv->net_dev->name);
2424 
2425 	printk_buf((u8 *) priv->status_queue.drv,
2426 		   sizeof(struct ipw2100_status) * RX_QUEUE_LENGTH);
2427 #endif
2428 
2429 	priv->fatal_error = IPW2100_ERR_C3_CORRUPTION;
2430 	priv->net_dev->stats.rx_errors++;
2431 	schedule_reset(priv);
2432 }
2433 
2434 static void isr_rx(struct ipw2100_priv *priv, int i,
2435 			  struct libipw_rx_stats *stats)
2436 {
2437 	struct net_device *dev = priv->net_dev;
2438 	struct ipw2100_status *status = &priv->status_queue.drv[i];
2439 	struct ipw2100_rx_packet *packet = &priv->rx_buffers[i];
2440 
2441 	IPW_DEBUG_RX("Handler...\n");
2442 
2443 	if (unlikely(status->frame_size > skb_tailroom(packet->skb))) {
2444 		IPW_DEBUG_INFO("%s: frame_size (%u) > skb_tailroom (%u)!"
2445 			       "  Dropping.\n",
2446 			       dev->name,
2447 			       status->frame_size, skb_tailroom(packet->skb));
2448 		dev->stats.rx_errors++;
2449 		return;
2450 	}
2451 
2452 	if (unlikely(!netif_running(dev))) {
2453 		dev->stats.rx_errors++;
2454 		priv->wstats.discard.misc++;
2455 		IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
2456 		return;
2457 	}
2458 
2459 	if (unlikely(priv->ieee->iw_mode != IW_MODE_MONITOR &&
2460 		     !(priv->status & STATUS_ASSOCIATED))) {
2461 		IPW_DEBUG_DROP("Dropping packet while not associated.\n");
2462 		priv->wstats.discard.misc++;
2463 		return;
2464 	}
2465 
2466 	dma_unmap_single(&priv->pci_dev->dev, packet->dma_addr,
2467 			 sizeof(struct ipw2100_rx), DMA_FROM_DEVICE);
2468 
2469 	skb_put(packet->skb, status->frame_size);
2470 
2471 #ifdef IPW2100_RX_DEBUG
2472 	/* Make a copy of the frame so we can dump it to the logs if
2473 	 * libipw_rx fails */
2474 	skb_copy_from_linear_data(packet->skb, packet_data,
2475 				  min_t(u32, status->frame_size,
2476 					     IPW_RX_NIC_BUFFER_LENGTH));
2477 #endif
2478 
2479 	if (!libipw_rx(priv->ieee, packet->skb, stats)) {
2480 #ifdef IPW2100_RX_DEBUG
2481 		IPW_DEBUG_DROP("%s: Non consumed packet:\n",
2482 			       dev->name);
2483 		printk_buf(IPW_DL_DROP, packet_data, status->frame_size);
2484 #endif
2485 		dev->stats.rx_errors++;
2486 
2487 		/* libipw_rx failed, so it didn't free the SKB */
2488 		dev_kfree_skb_any(packet->skb);
2489 		packet->skb = NULL;
2490 	}
2491 
2492 	/* We need to allocate a new SKB and attach it to the RDB. */
2493 	if (unlikely(ipw2100_alloc_skb(priv, packet))) {
2494 		printk(KERN_WARNING DRV_NAME ": "
2495 		       "%s: Unable to allocate SKB onto RBD ring - disabling "
2496 		       "adapter.\n", dev->name);
2497 		/* TODO: schedule adapter shutdown */
2498 		IPW_DEBUG_INFO("TODO: Shutdown adapter...\n");
2499 	}
2500 
2501 	/* Update the RDB entry */
2502 	priv->rx_queue.drv[i].host_addr = packet->dma_addr;
2503 }
2504 
2505 #ifdef CONFIG_IPW2100_MONITOR
2506 
2507 static void isr_rx_monitor(struct ipw2100_priv *priv, int i,
2508 		   struct libipw_rx_stats *stats)
2509 {
2510 	struct net_device *dev = priv->net_dev;
2511 	struct ipw2100_status *status = &priv->status_queue.drv[i];
2512 	struct ipw2100_rx_packet *packet = &priv->rx_buffers[i];
2513 
2514 	/* Magic struct that slots into the radiotap header -- no reason
2515 	 * to build this manually element by element, we can write it much
2516 	 * more efficiently than we can parse it. ORDER MATTERS HERE */
2517 	struct ipw_rt_hdr {
2518 		struct ieee80211_radiotap_header_fixed rt_hdr;
2519 		s8 rt_dbmsignal; /* signal in dbM, kluged to signed */
2520 	} *ipw_rt;
2521 
2522 	IPW_DEBUG_RX("Handler...\n");
2523 
2524 	if (unlikely(status->frame_size > skb_tailroom(packet->skb) -
2525 				sizeof(struct ipw_rt_hdr))) {
2526 		IPW_DEBUG_INFO("%s: frame_size (%u) > skb_tailroom (%u)!"
2527 			       "  Dropping.\n",
2528 			       dev->name,
2529 			       status->frame_size,
2530 			       skb_tailroom(packet->skb));
2531 		dev->stats.rx_errors++;
2532 		return;
2533 	}
2534 
2535 	if (unlikely(!netif_running(dev))) {
2536 		dev->stats.rx_errors++;
2537 		priv->wstats.discard.misc++;
2538 		IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
2539 		return;
2540 	}
2541 
2542 	if (unlikely(priv->config & CFG_CRC_CHECK &&
2543 		     status->flags & IPW_STATUS_FLAG_CRC_ERROR)) {
2544 		IPW_DEBUG_RX("CRC error in packet.  Dropping.\n");
2545 		dev->stats.rx_errors++;
2546 		return;
2547 	}
2548 
2549 	dma_unmap_single(&priv->pci_dev->dev, packet->dma_addr,
2550 			 sizeof(struct ipw2100_rx), DMA_FROM_DEVICE);
2551 	memmove(packet->skb->data + sizeof(struct ipw_rt_hdr),
2552 		packet->skb->data, status->frame_size);
2553 
2554 	ipw_rt = (struct ipw_rt_hdr *) packet->skb->data;
2555 
2556 	ipw_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
2557 	ipw_rt->rt_hdr.it_pad = 0; /* always good to zero */
2558 	ipw_rt->rt_hdr.it_len = cpu_to_le16(sizeof(struct ipw_rt_hdr)); /* total hdr+data */
2559 
2560 	ipw_rt->rt_hdr.it_present = cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
2561 
2562 	ipw_rt->rt_dbmsignal = status->rssi + IPW2100_RSSI_TO_DBM;
2563 
2564 	skb_put(packet->skb, status->frame_size + sizeof(struct ipw_rt_hdr));
2565 
2566 	if (!libipw_rx(priv->ieee, packet->skb, stats)) {
2567 		dev->stats.rx_errors++;
2568 
2569 		/* libipw_rx failed, so it didn't free the SKB */
2570 		dev_kfree_skb_any(packet->skb);
2571 		packet->skb = NULL;
2572 	}
2573 
2574 	/* We need to allocate a new SKB and attach it to the RDB. */
2575 	if (unlikely(ipw2100_alloc_skb(priv, packet))) {
2576 		IPW_DEBUG_WARNING(
2577 			"%s: Unable to allocate SKB onto RBD ring - disabling "
2578 			"adapter.\n", dev->name);
2579 		/* TODO: schedule adapter shutdown */
2580 		IPW_DEBUG_INFO("TODO: Shutdown adapter...\n");
2581 	}
2582 
2583 	/* Update the RDB entry */
2584 	priv->rx_queue.drv[i].host_addr = packet->dma_addr;
2585 }
2586 
2587 #endif
2588 
2589 static int ipw2100_corruption_check(struct ipw2100_priv *priv, int i)
2590 {
2591 	struct ipw2100_status *status = &priv->status_queue.drv[i];
2592 	struct ipw2100_rx *u = priv->rx_buffers[i].rxp;
2593 	u16 frame_type = status->status_fields & STATUS_TYPE_MASK;
2594 
2595 	switch (frame_type) {
2596 	case COMMAND_STATUS_VAL:
2597 		return (status->frame_size != sizeof(u->rx_data.command));
2598 	case STATUS_CHANGE_VAL:
2599 		return (status->frame_size != sizeof(u->rx_data.status));
2600 	case HOST_NOTIFICATION_VAL:
2601 		return (status->frame_size < sizeof(u->rx_data.notification));
2602 	case P80211_DATA_VAL:
2603 	case P8023_DATA_VAL:
2604 #ifdef CONFIG_IPW2100_MONITOR
2605 		return 0;
2606 #else
2607 		switch (WLAN_FC_GET_TYPE(le16_to_cpu(u->rx_data.header.frame_ctl))) {
2608 		case IEEE80211_FTYPE_MGMT:
2609 		case IEEE80211_FTYPE_CTL:
2610 			return 0;
2611 		case IEEE80211_FTYPE_DATA:
2612 			return (status->frame_size >
2613 				IPW_MAX_802_11_PAYLOAD_LENGTH);
2614 		}
2615 #endif
2616 	}
2617 
2618 	return 1;
2619 }
2620 
2621 /*
2622  * ipw2100 interrupts are disabled at this point, and the ISR
2623  * is the only code that calls this method.  So, we do not need
2624  * to play with any locks.
2625  *
2626  * RX Queue works as follows:
2627  *
2628  * Read index - firmware places packet in entry identified by the
2629  *              Read index and advances Read index.  In this manner,
2630  *              Read index will always point to the next packet to
2631  *              be filled--but not yet valid.
2632  *
2633  * Write index - driver fills this entry with an unused RBD entry.
2634  *               This entry has not filled by the firmware yet.
2635  *
2636  * In between the W and R indexes are the RBDs that have been received
2637  * but not yet processed.
2638  *
2639  * The process of handling packets will start at WRITE + 1 and advance
2640  * until it reaches the READ index.
2641  *
2642  * The WRITE index is cached in the variable 'priv->rx_queue.next'.
2643  *
2644  */
2645 static void __ipw2100_rx_process(struct ipw2100_priv *priv)
2646 {
2647 	struct ipw2100_bd_queue *rxq = &priv->rx_queue;
2648 	struct ipw2100_status_queue *sq = &priv->status_queue;
2649 	struct ipw2100_rx_packet *packet;
2650 	u16 frame_type;
2651 	u32 r, w, i, s;
2652 	struct ipw2100_rx *u;
2653 	struct libipw_rx_stats stats = {
2654 		.mac_time = jiffies,
2655 	};
2656 
2657 	read_register(priv->net_dev, IPW_MEM_HOST_SHARED_RX_READ_INDEX, &r);
2658 	read_register(priv->net_dev, IPW_MEM_HOST_SHARED_RX_WRITE_INDEX, &w);
2659 
2660 	if (r >= rxq->entries) {
2661 		IPW_DEBUG_RX("exit - bad read index\n");
2662 		return;
2663 	}
2664 
2665 	i = (rxq->next + 1) % rxq->entries;
2666 	s = i;
2667 	while (i != r) {
2668 		/* IPW_DEBUG_RX("r = %d : w = %d : processing = %d\n",
2669 		   r, rxq->next, i); */
2670 
2671 		packet = &priv->rx_buffers[i];
2672 
2673 		/* Sync the DMA for the RX buffer so CPU is sure to get
2674 		 * the correct values */
2675 		dma_sync_single_for_cpu(&priv->pci_dev->dev, packet->dma_addr,
2676 					sizeof(struct ipw2100_rx),
2677 					DMA_FROM_DEVICE);
2678 
2679 		if (unlikely(ipw2100_corruption_check(priv, i))) {
2680 			ipw2100_corruption_detected(priv, i);
2681 			goto increment;
2682 		}
2683 
2684 		u = packet->rxp;
2685 		frame_type = sq->drv[i].status_fields & STATUS_TYPE_MASK;
2686 		stats.rssi = sq->drv[i].rssi + IPW2100_RSSI_TO_DBM;
2687 		stats.len = sq->drv[i].frame_size;
2688 
2689 		stats.mask = 0;
2690 		if (stats.rssi != 0)
2691 			stats.mask |= LIBIPW_STATMASK_RSSI;
2692 		stats.freq = LIBIPW_24GHZ_BAND;
2693 
2694 		IPW_DEBUG_RX("%s: '%s' frame type received (%d).\n",
2695 			     priv->net_dev->name, frame_types[frame_type],
2696 			     stats.len);
2697 
2698 		switch (frame_type) {
2699 		case COMMAND_STATUS_VAL:
2700 			/* Reset Rx watchdog */
2701 			isr_rx_complete_command(priv, &u->rx_data.command);
2702 			break;
2703 
2704 		case STATUS_CHANGE_VAL:
2705 			isr_status_change(priv, u->rx_data.status);
2706 			break;
2707 
2708 		case P80211_DATA_VAL:
2709 		case P8023_DATA_VAL:
2710 #ifdef CONFIG_IPW2100_MONITOR
2711 			if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
2712 				isr_rx_monitor(priv, i, &stats);
2713 				break;
2714 			}
2715 #endif
2716 			if (stats.len < sizeof(struct libipw_hdr_3addr))
2717 				break;
2718 			switch (WLAN_FC_GET_TYPE(le16_to_cpu(u->rx_data.header.frame_ctl))) {
2719 			case IEEE80211_FTYPE_MGMT:
2720 				libipw_rx_mgt(priv->ieee,
2721 						 &u->rx_data.header, &stats);
2722 				break;
2723 
2724 			case IEEE80211_FTYPE_CTL:
2725 				break;
2726 
2727 			case IEEE80211_FTYPE_DATA:
2728 				isr_rx(priv, i, &stats);
2729 				break;
2730 
2731 			}
2732 			break;
2733 		}
2734 
2735 	      increment:
2736 		/* clear status field associated with this RBD */
2737 		rxq->drv[i].status.info.field = 0;
2738 
2739 		i = (i + 1) % rxq->entries;
2740 	}
2741 
2742 	if (i != s) {
2743 		/* backtrack one entry, wrapping to end if at 0 */
2744 		rxq->next = (i ? i : rxq->entries) - 1;
2745 
2746 		write_register(priv->net_dev,
2747 			       IPW_MEM_HOST_SHARED_RX_WRITE_INDEX, rxq->next);
2748 	}
2749 }
2750 
2751 /*
2752  * __ipw2100_tx_process
2753  *
2754  * This routine will determine whether the next packet on
2755  * the fw_pend_list has been processed by the firmware yet.
2756  *
2757  * If not, then it does nothing and returns.
2758  *
2759  * If so, then it removes the item from the fw_pend_list, frees
2760  * any associated storage, and places the item back on the
2761  * free list of its source (either msg_free_list or tx_free_list)
2762  *
2763  * TX Queue works as follows:
2764  *
2765  * Read index - points to the next TBD that the firmware will
2766  *              process.  The firmware will read the data, and once
2767  *              done processing, it will advance the Read index.
2768  *
2769  * Write index - driver fills this entry with an constructed TBD
2770  *               entry.  The Write index is not advanced until the
2771  *               packet has been configured.
2772  *
2773  * In between the W and R indexes are the TBDs that have NOT been
2774  * processed.  Lagging behind the R index are packets that have
2775  * been processed but have not been freed by the driver.
2776  *
2777  * In order to free old storage, an internal index will be maintained
2778  * that points to the next packet to be freed.  When all used
2779  * packets have been freed, the oldest index will be the same as the
2780  * firmware's read index.
2781  *
2782  * The OLDEST index is cached in the variable 'priv->tx_queue.oldest'
2783  *
2784  * Because the TBD structure can not contain arbitrary data, the
2785  * driver must keep an internal queue of cached allocations such that
2786  * it can put that data back into the tx_free_list and msg_free_list
2787  * for use by future command and data packets.
2788  *
2789  */
2790 static int __ipw2100_tx_process(struct ipw2100_priv *priv)
2791 {
2792 	struct ipw2100_bd_queue *txq = &priv->tx_queue;
2793 	struct ipw2100_bd *tbd;
2794 	struct list_head *element;
2795 	struct ipw2100_tx_packet *packet;
2796 	int descriptors_used;
2797 	int e, i;
2798 	u32 r, w, frag_num = 0;
2799 
2800 	if (list_empty(&priv->fw_pend_list))
2801 		return 0;
2802 
2803 	element = priv->fw_pend_list.next;
2804 
2805 	packet = list_entry(element, struct ipw2100_tx_packet, list);
2806 	tbd = &txq->drv[packet->index];
2807 
2808 	/* Determine how many TBD entries must be finished... */
2809 	switch (packet->type) {
2810 	case COMMAND:
2811 		/* COMMAND uses only one slot; don't advance */
2812 		descriptors_used = 1;
2813 		e = txq->oldest;
2814 		break;
2815 
2816 	case DATA:
2817 		/* DATA uses two slots; advance and loop position. */
2818 		descriptors_used = tbd->num_fragments;
2819 		frag_num = tbd->num_fragments - 1;
2820 		e = txq->oldest + frag_num;
2821 		e %= txq->entries;
2822 		break;
2823 
2824 	default:
2825 		printk(KERN_WARNING DRV_NAME ": %s: Bad fw_pend_list entry!\n",
2826 		       priv->net_dev->name);
2827 		return 0;
2828 	}
2829 
2830 	/* if the last TBD is not done by NIC yet, then packet is
2831 	 * not ready to be released.
2832 	 *
2833 	 */
2834 	read_register(priv->net_dev, IPW_MEM_HOST_SHARED_TX_QUEUE_READ_INDEX,
2835 		      &r);
2836 	read_register(priv->net_dev, IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX,
2837 		      &w);
2838 	if (w != txq->next)
2839 		printk(KERN_WARNING DRV_NAME ": %s: write index mismatch\n",
2840 		       priv->net_dev->name);
2841 
2842 	/*
2843 	 * txq->next is the index of the last packet written txq->oldest is
2844 	 * the index of the r is the index of the next packet to be read by
2845 	 * firmware
2846 	 */
2847 
2848 	/*
2849 	 * Quick graphic to help you visualize the following
2850 	 * if / else statement
2851 	 *
2852 	 * ===>|                     s---->|===============
2853 	 *                               e>|
2854 	 * | a | b | c | d | e | f | g | h | i | j | k | l
2855 	 *       r---->|
2856 	 *               w
2857 	 *
2858 	 * w - updated by driver
2859 	 * r - updated by firmware
2860 	 * s - start of oldest BD entry (txq->oldest)
2861 	 * e - end of oldest BD entry
2862 	 *
2863 	 */
2864 	if (!((r <= w && (e < r || e >= w)) || (e < r && e >= w))) {
2865 		IPW_DEBUG_TX("exit - no processed packets ready to release.\n");
2866 		return 0;
2867 	}
2868 
2869 	list_del(element);
2870 	DEC_STAT(&priv->fw_pend_stat);
2871 
2872 #ifdef CONFIG_IPW2100_DEBUG
2873 	{
2874 		i = txq->oldest;
2875 		IPW_DEBUG_TX("TX%d V=%p P=%04X T=%04X L=%d\n", i,
2876 			     &txq->drv[i],
2877 			     (u32) (txq->nic + i * sizeof(struct ipw2100_bd)),
2878 			     txq->drv[i].host_addr, txq->drv[i].buf_length);
2879 
2880 		if (packet->type == DATA) {
2881 			i = (i + 1) % txq->entries;
2882 
2883 			IPW_DEBUG_TX("TX%d V=%p P=%04X T=%04X L=%d\n", i,
2884 				     &txq->drv[i],
2885 				     (u32) (txq->nic + i *
2886 					    sizeof(struct ipw2100_bd)),
2887 				     (u32) txq->drv[i].host_addr,
2888 				     txq->drv[i].buf_length);
2889 		}
2890 	}
2891 #endif
2892 
2893 	switch (packet->type) {
2894 	case DATA:
2895 		if (txq->drv[txq->oldest].status.info.fields.txType != 0)
2896 			printk(KERN_WARNING DRV_NAME ": %s: Queue mismatch.  "
2897 			       "Expecting DATA TBD but pulled "
2898 			       "something else: ids %d=%d.\n",
2899 			       priv->net_dev->name, txq->oldest, packet->index);
2900 
2901 		/* DATA packet; we have to unmap and free the SKB */
2902 		for (i = 0; i < frag_num; i++) {
2903 			tbd = &txq->drv[(packet->index + 1 + i) % txq->entries];
2904 
2905 			IPW_DEBUG_TX("TX%d P=%08x L=%d\n",
2906 				     (packet->index + 1 + i) % txq->entries,
2907 				     tbd->host_addr, tbd->buf_length);
2908 
2909 			dma_unmap_single(&priv->pci_dev->dev, tbd->host_addr,
2910 					 tbd->buf_length, DMA_TO_DEVICE);
2911 		}
2912 
2913 		libipw_txb_free(packet->info.d_struct.txb);
2914 		packet->info.d_struct.txb = NULL;
2915 
2916 		list_add_tail(element, &priv->tx_free_list);
2917 		INC_STAT(&priv->tx_free_stat);
2918 
2919 		/* We have a free slot in the Tx queue, so wake up the
2920 		 * transmit layer if it is stopped. */
2921 		if (priv->status & STATUS_ASSOCIATED)
2922 			netif_wake_queue(priv->net_dev);
2923 
2924 		/* A packet was processed by the hardware, so update the
2925 		 * watchdog */
2926 		netif_trans_update(priv->net_dev);
2927 
2928 		break;
2929 
2930 	case COMMAND:
2931 		if (txq->drv[txq->oldest].status.info.fields.txType != 1)
2932 			printk(KERN_WARNING DRV_NAME ": %s: Queue mismatch.  "
2933 			       "Expecting COMMAND TBD but pulled "
2934 			       "something else: ids %d=%d.\n",
2935 			       priv->net_dev->name, txq->oldest, packet->index);
2936 
2937 #ifdef CONFIG_IPW2100_DEBUG
2938 		if (packet->info.c_struct.cmd->host_command_reg <
2939 		    ARRAY_SIZE(command_types))
2940 			IPW_DEBUG_TX("Command '%s (%d)' processed: %d.\n",
2941 				     command_types[packet->info.c_struct.cmd->
2942 						   host_command_reg],
2943 				     packet->info.c_struct.cmd->
2944 				     host_command_reg,
2945 				     packet->info.c_struct.cmd->cmd_status_reg);
2946 #endif
2947 
2948 		list_add_tail(element, &priv->msg_free_list);
2949 		INC_STAT(&priv->msg_free_stat);
2950 		break;
2951 	}
2952 
2953 	/* advance oldest used TBD pointer to start of next entry */
2954 	txq->oldest = (e + 1) % txq->entries;
2955 	/* increase available TBDs number */
2956 	txq->available += descriptors_used;
2957 	SET_STAT(&priv->txq_stat, txq->available);
2958 
2959 	IPW_DEBUG_TX("packet latency (send to process)  %ld jiffies\n",
2960 		     jiffies - packet->jiffy_start);
2961 
2962 	return (!list_empty(&priv->fw_pend_list));
2963 }
2964 
2965 static inline void __ipw2100_tx_complete(struct ipw2100_priv *priv)
2966 {
2967 	int i = 0;
2968 
2969 	while (__ipw2100_tx_process(priv) && i < 200)
2970 		i++;
2971 
2972 	if (i == 200) {
2973 		printk(KERN_WARNING DRV_NAME ": "
2974 		       "%s: Driver is running slow (%d iters).\n",
2975 		       priv->net_dev->name, i);
2976 	}
2977 }
2978 
2979 static void ipw2100_tx_send_commands(struct ipw2100_priv *priv)
2980 {
2981 	struct list_head *element;
2982 	struct ipw2100_tx_packet *packet;
2983 	struct ipw2100_bd_queue *txq = &priv->tx_queue;
2984 	struct ipw2100_bd *tbd;
2985 	int next = txq->next;
2986 
2987 	while (!list_empty(&priv->msg_pend_list)) {
2988 		/* if there isn't enough space in TBD queue, then
2989 		 * don't stuff a new one in.
2990 		 * NOTE: 3 are needed as a command will take one,
2991 		 *       and there is a minimum of 2 that must be
2992 		 *       maintained between the r and w indexes
2993 		 */
2994 		if (txq->available <= 3) {
2995 			IPW_DEBUG_TX("no room in tx_queue\n");
2996 			break;
2997 		}
2998 
2999 		element = priv->msg_pend_list.next;
3000 		list_del(element);
3001 		DEC_STAT(&priv->msg_pend_stat);
3002 
3003 		packet = list_entry(element, struct ipw2100_tx_packet, list);
3004 
3005 		IPW_DEBUG_TX("using TBD at virt=%p, phys=%04X\n",
3006 			     &txq->drv[txq->next],
3007 			     (u32) (txq->nic + txq->next *
3008 				      sizeof(struct ipw2100_bd)));
3009 
3010 		packet->index = txq->next;
3011 
3012 		tbd = &txq->drv[txq->next];
3013 
3014 		/* initialize TBD */
3015 		tbd->host_addr = packet->info.c_struct.cmd_phys;
3016 		tbd->buf_length = sizeof(struct ipw2100_cmd_header);
3017 		/* not marking number of fragments causes problems
3018 		 * with f/w debug version */
3019 		tbd->num_fragments = 1;
3020 		tbd->status.info.field =
3021 		    IPW_BD_STATUS_TX_FRAME_COMMAND |
3022 		    IPW_BD_STATUS_TX_INTERRUPT_ENABLE;
3023 
3024 		/* update TBD queue counters */
3025 		txq->next++;
3026 		txq->next %= txq->entries;
3027 		txq->available--;
3028 		DEC_STAT(&priv->txq_stat);
3029 
3030 		list_add_tail(element, &priv->fw_pend_list);
3031 		INC_STAT(&priv->fw_pend_stat);
3032 	}
3033 
3034 	if (txq->next != next) {
3035 		/* kick off the DMA by notifying firmware the
3036 		 * write index has moved; make sure TBD stores are sync'd */
3037 		wmb();
3038 		write_register(priv->net_dev,
3039 			       IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX,
3040 			       txq->next);
3041 	}
3042 }
3043 
3044 /*
3045  * ipw2100_tx_send_data
3046  *
3047  */
3048 static void ipw2100_tx_send_data(struct ipw2100_priv *priv)
3049 {
3050 	struct list_head *element;
3051 	struct ipw2100_tx_packet *packet;
3052 	struct ipw2100_bd_queue *txq = &priv->tx_queue;
3053 	struct ipw2100_bd *tbd;
3054 	int next = txq->next;
3055 	int i = 0;
3056 	struct ipw2100_data_header *ipw_hdr;
3057 	struct libipw_hdr_3addr *hdr;
3058 
3059 	while (!list_empty(&priv->tx_pend_list)) {
3060 		/* if there isn't enough space in TBD queue, then
3061 		 * don't stuff a new one in.
3062 		 * NOTE: 4 are needed as a data will take two,
3063 		 *       and there is a minimum of 2 that must be
3064 		 *       maintained between the r and w indexes
3065 		 */
3066 		element = priv->tx_pend_list.next;
3067 		packet = list_entry(element, struct ipw2100_tx_packet, list);
3068 
3069 		if (unlikely(1 + packet->info.d_struct.txb->nr_frags >
3070 			     IPW_MAX_BDS)) {
3071 			/* TODO: Support merging buffers if more than
3072 			 * IPW_MAX_BDS are used */
3073 			IPW_DEBUG_INFO("%s: Maximum BD threshold exceeded.  "
3074 				       "Increase fragmentation level.\n",
3075 				       priv->net_dev->name);
3076 		}
3077 
3078 		if (txq->available <= 3 + packet->info.d_struct.txb->nr_frags) {
3079 			IPW_DEBUG_TX("no room in tx_queue\n");
3080 			break;
3081 		}
3082 
3083 		list_del(element);
3084 		DEC_STAT(&priv->tx_pend_stat);
3085 
3086 		tbd = &txq->drv[txq->next];
3087 
3088 		packet->index = txq->next;
3089 
3090 		ipw_hdr = packet->info.d_struct.data;
3091 		hdr = (struct libipw_hdr_3addr *)packet->info.d_struct.txb->
3092 		    fragments[0]->data;
3093 
3094 		if (priv->ieee->iw_mode == IW_MODE_INFRA) {
3095 			/* To DS: Addr1 = BSSID, Addr2 = SA,
3096 			   Addr3 = DA */
3097 			memcpy(ipw_hdr->src_addr, hdr->addr2, ETH_ALEN);
3098 			memcpy(ipw_hdr->dst_addr, hdr->addr3, ETH_ALEN);
3099 		} else if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
3100 			/* not From/To DS: Addr1 = DA, Addr2 = SA,
3101 			   Addr3 = BSSID */
3102 			memcpy(ipw_hdr->src_addr, hdr->addr2, ETH_ALEN);
3103 			memcpy(ipw_hdr->dst_addr, hdr->addr1, ETH_ALEN);
3104 		}
3105 
3106 		ipw_hdr->host_command_reg = SEND;
3107 		ipw_hdr->host_command_reg1 = 0;
3108 
3109 		/* For now we only support host based encryption */
3110 		ipw_hdr->needs_encryption = 0;
3111 		ipw_hdr->encrypted = packet->info.d_struct.txb->encrypted;
3112 		if (packet->info.d_struct.txb->nr_frags > 1)
3113 			ipw_hdr->fragment_size =
3114 			    packet->info.d_struct.txb->frag_size -
3115 			    LIBIPW_3ADDR_LEN;
3116 		else
3117 			ipw_hdr->fragment_size = 0;
3118 
3119 		tbd->host_addr = packet->info.d_struct.data_phys;
3120 		tbd->buf_length = sizeof(struct ipw2100_data_header);
3121 		tbd->num_fragments = 1 + packet->info.d_struct.txb->nr_frags;
3122 		tbd->status.info.field =
3123 		    IPW_BD_STATUS_TX_FRAME_802_3 |
3124 		    IPW_BD_STATUS_TX_FRAME_NOT_LAST_FRAGMENT;
3125 		txq->next++;
3126 		txq->next %= txq->entries;
3127 
3128 		IPW_DEBUG_TX("data header tbd TX%d P=%08x L=%d\n",
3129 			     packet->index, tbd->host_addr, tbd->buf_length);
3130 #ifdef CONFIG_IPW2100_DEBUG
3131 		if (packet->info.d_struct.txb->nr_frags > 1)
3132 			IPW_DEBUG_FRAG("fragment Tx: %d frames\n",
3133 				       packet->info.d_struct.txb->nr_frags);
3134 #endif
3135 
3136 		for (i = 0; i < packet->info.d_struct.txb->nr_frags; i++) {
3137 			tbd = &txq->drv[txq->next];
3138 			if (i == packet->info.d_struct.txb->nr_frags - 1)
3139 				tbd->status.info.field =
3140 				    IPW_BD_STATUS_TX_FRAME_802_3 |
3141 				    IPW_BD_STATUS_TX_INTERRUPT_ENABLE;
3142 			else
3143 				tbd->status.info.field =
3144 				    IPW_BD_STATUS_TX_FRAME_802_3 |
3145 				    IPW_BD_STATUS_TX_FRAME_NOT_LAST_FRAGMENT;
3146 
3147 			tbd->buf_length = packet->info.d_struct.txb->
3148 			    fragments[i]->len - LIBIPW_3ADDR_LEN;
3149 
3150 			tbd->host_addr = dma_map_single(&priv->pci_dev->dev,
3151 							packet->info.d_struct.
3152 							txb->fragments[i]->data +
3153 							LIBIPW_3ADDR_LEN,
3154 							tbd->buf_length,
3155 							DMA_TO_DEVICE);
3156 			if (dma_mapping_error(&priv->pci_dev->dev, tbd->host_addr)) {
3157 				IPW_DEBUG_TX("dma mapping error\n");
3158 				break;
3159 			}
3160 
3161 			IPW_DEBUG_TX("data frag tbd TX%d P=%08x L=%d\n",
3162 				     txq->next, tbd->host_addr,
3163 				     tbd->buf_length);
3164 
3165 			dma_sync_single_for_device(&priv->pci_dev->dev,
3166 						   tbd->host_addr,
3167 						   tbd->buf_length,
3168 						   DMA_TO_DEVICE);
3169 
3170 			txq->next++;
3171 			txq->next %= txq->entries;
3172 		}
3173 
3174 		txq->available -= 1 + packet->info.d_struct.txb->nr_frags;
3175 		SET_STAT(&priv->txq_stat, txq->available);
3176 
3177 		list_add_tail(element, &priv->fw_pend_list);
3178 		INC_STAT(&priv->fw_pend_stat);
3179 	}
3180 
3181 	if (txq->next != next) {
3182 		/* kick off the DMA by notifying firmware the
3183 		 * write index has moved; make sure TBD stores are sync'd */
3184 		write_register(priv->net_dev,
3185 			       IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX,
3186 			       txq->next);
3187 	}
3188 }
3189 
3190 static void ipw2100_irq_tasklet(struct tasklet_struct *t)
3191 {
3192 	struct ipw2100_priv *priv = from_tasklet(priv, t, irq_tasklet);
3193 	struct net_device *dev = priv->net_dev;
3194 	unsigned long flags;
3195 	u32 inta, tmp;
3196 
3197 	spin_lock_irqsave(&priv->low_lock, flags);
3198 	ipw2100_disable_interrupts(priv);
3199 
3200 	read_register(dev, IPW_REG_INTA, &inta);
3201 
3202 	IPW_DEBUG_ISR("enter - INTA: 0x%08lX\n",
3203 		      (unsigned long)inta & IPW_INTERRUPT_MASK);
3204 
3205 	priv->in_isr++;
3206 	priv->interrupts++;
3207 
3208 	/* We do not loop and keep polling for more interrupts as this
3209 	 * is frowned upon and doesn't play nicely with other potentially
3210 	 * chained IRQs */
3211 	IPW_DEBUG_ISR("INTA: 0x%08lX\n",
3212 		      (unsigned long)inta & IPW_INTERRUPT_MASK);
3213 
3214 	if (inta & IPW2100_INTA_FATAL_ERROR) {
3215 		printk(KERN_WARNING DRV_NAME
3216 		       ": Fatal interrupt. Scheduling firmware restart.\n");
3217 		priv->inta_other++;
3218 		write_register(dev, IPW_REG_INTA, IPW2100_INTA_FATAL_ERROR);
3219 
3220 		read_nic_dword(dev, IPW_NIC_FATAL_ERROR, &priv->fatal_error);
3221 		IPW_DEBUG_INFO("%s: Fatal error value: 0x%08X\n",
3222 			       priv->net_dev->name, priv->fatal_error);
3223 
3224 		read_nic_dword(dev, IPW_ERROR_ADDR(priv->fatal_error), &tmp);
3225 		IPW_DEBUG_INFO("%s: Fatal error address value: 0x%08X\n",
3226 			       priv->net_dev->name, tmp);
3227 
3228 		/* Wake up any sleeping jobs */
3229 		schedule_reset(priv);
3230 	}
3231 
3232 	if (inta & IPW2100_INTA_PARITY_ERROR) {
3233 		printk(KERN_ERR DRV_NAME
3234 		       ": ***** PARITY ERROR INTERRUPT !!!!\n");
3235 		priv->inta_other++;
3236 		write_register(dev, IPW_REG_INTA, IPW2100_INTA_PARITY_ERROR);
3237 	}
3238 
3239 	if (inta & IPW2100_INTA_RX_TRANSFER) {
3240 		IPW_DEBUG_ISR("RX interrupt\n");
3241 
3242 		priv->rx_interrupts++;
3243 
3244 		write_register(dev, IPW_REG_INTA, IPW2100_INTA_RX_TRANSFER);
3245 
3246 		__ipw2100_rx_process(priv);
3247 		__ipw2100_tx_complete(priv);
3248 	}
3249 
3250 	if (inta & IPW2100_INTA_TX_TRANSFER) {
3251 		IPW_DEBUG_ISR("TX interrupt\n");
3252 
3253 		priv->tx_interrupts++;
3254 
3255 		write_register(dev, IPW_REG_INTA, IPW2100_INTA_TX_TRANSFER);
3256 
3257 		__ipw2100_tx_complete(priv);
3258 		ipw2100_tx_send_commands(priv);
3259 		ipw2100_tx_send_data(priv);
3260 	}
3261 
3262 	if (inta & IPW2100_INTA_TX_COMPLETE) {
3263 		IPW_DEBUG_ISR("TX complete\n");
3264 		priv->inta_other++;
3265 		write_register(dev, IPW_REG_INTA, IPW2100_INTA_TX_COMPLETE);
3266 
3267 		__ipw2100_tx_complete(priv);
3268 	}
3269 
3270 	if (inta & IPW2100_INTA_EVENT_INTERRUPT) {
3271 		/* ipw2100_handle_event(dev); */
3272 		priv->inta_other++;
3273 		write_register(dev, IPW_REG_INTA, IPW2100_INTA_EVENT_INTERRUPT);
3274 	}
3275 
3276 	if (inta & IPW2100_INTA_FW_INIT_DONE) {
3277 		IPW_DEBUG_ISR("FW init done interrupt\n");
3278 		priv->inta_other++;
3279 
3280 		read_register(dev, IPW_REG_INTA, &tmp);
3281 		if (tmp & (IPW2100_INTA_FATAL_ERROR |
3282 			   IPW2100_INTA_PARITY_ERROR)) {
3283 			write_register(dev, IPW_REG_INTA,
3284 				       IPW2100_INTA_FATAL_ERROR |
3285 				       IPW2100_INTA_PARITY_ERROR);
3286 		}
3287 
3288 		write_register(dev, IPW_REG_INTA, IPW2100_INTA_FW_INIT_DONE);
3289 	}
3290 
3291 	if (inta & IPW2100_INTA_STATUS_CHANGE) {
3292 		IPW_DEBUG_ISR("Status change interrupt\n");
3293 		priv->inta_other++;
3294 		write_register(dev, IPW_REG_INTA, IPW2100_INTA_STATUS_CHANGE);
3295 	}
3296 
3297 	if (inta & IPW2100_INTA_SLAVE_MODE_HOST_COMMAND_DONE) {
3298 		IPW_DEBUG_ISR("slave host mode interrupt\n");
3299 		priv->inta_other++;
3300 		write_register(dev, IPW_REG_INTA,
3301 			       IPW2100_INTA_SLAVE_MODE_HOST_COMMAND_DONE);
3302 	}
3303 
3304 	priv->in_isr--;
3305 	ipw2100_enable_interrupts(priv);
3306 
3307 	spin_unlock_irqrestore(&priv->low_lock, flags);
3308 
3309 	IPW_DEBUG_ISR("exit\n");
3310 }
3311 
3312 static irqreturn_t ipw2100_interrupt(int irq, void *data)
3313 {
3314 	struct ipw2100_priv *priv = data;
3315 	u32 inta, inta_mask;
3316 
3317 	if (!data)
3318 		return IRQ_NONE;
3319 
3320 	spin_lock(&priv->low_lock);
3321 
3322 	/* We check to see if we should be ignoring interrupts before
3323 	 * we touch the hardware.  During ucode load if we try and handle
3324 	 * an interrupt we can cause keyboard problems as well as cause
3325 	 * the ucode to fail to initialize */
3326 	if (!(priv->status & STATUS_INT_ENABLED)) {
3327 		/* Shared IRQ */
3328 		goto none;
3329 	}
3330 
3331 	read_register(priv->net_dev, IPW_REG_INTA_MASK, &inta_mask);
3332 	read_register(priv->net_dev, IPW_REG_INTA, &inta);
3333 
3334 	if (inta == 0xFFFFFFFF) {
3335 		/* Hardware disappeared */
3336 		printk(KERN_WARNING DRV_NAME ": IRQ INTA == 0xFFFFFFFF\n");
3337 		goto none;
3338 	}
3339 
3340 	inta &= IPW_INTERRUPT_MASK;
3341 
3342 	if (!(inta & inta_mask)) {
3343 		/* Shared interrupt */
3344 		goto none;
3345 	}
3346 
3347 	/* We disable the hardware interrupt here just to prevent unneeded
3348 	 * calls to be made.  We disable this again within the actual
3349 	 * work tasklet, so if another part of the code re-enables the
3350 	 * interrupt, that is fine */
3351 	ipw2100_disable_interrupts(priv);
3352 
3353 	tasklet_schedule(&priv->irq_tasklet);
3354 	spin_unlock(&priv->low_lock);
3355 
3356 	return IRQ_HANDLED;
3357       none:
3358 	spin_unlock(&priv->low_lock);
3359 	return IRQ_NONE;
3360 }
3361 
3362 static netdev_tx_t ipw2100_tx(struct libipw_txb *txb,
3363 			      struct net_device *dev, int pri)
3364 {
3365 	struct ipw2100_priv *priv = libipw_priv(dev);
3366 	struct list_head *element;
3367 	struct ipw2100_tx_packet *packet;
3368 	unsigned long flags;
3369 
3370 	spin_lock_irqsave(&priv->low_lock, flags);
3371 
3372 	if (!(priv->status & STATUS_ASSOCIATED)) {
3373 		IPW_DEBUG_INFO("Can not transmit when not connected.\n");
3374 		priv->net_dev->stats.tx_carrier_errors++;
3375 		netif_stop_queue(dev);
3376 		goto fail_unlock;
3377 	}
3378 
3379 	if (list_empty(&priv->tx_free_list))
3380 		goto fail_unlock;
3381 
3382 	element = priv->tx_free_list.next;
3383 	packet = list_entry(element, struct ipw2100_tx_packet, list);
3384 
3385 	packet->info.d_struct.txb = txb;
3386 
3387 	IPW_DEBUG_TX("Sending fragment (%d bytes):\n", txb->fragments[0]->len);
3388 	printk_buf(IPW_DL_TX, txb->fragments[0]->data, txb->fragments[0]->len);
3389 
3390 	packet->jiffy_start = jiffies;
3391 
3392 	list_del(element);
3393 	DEC_STAT(&priv->tx_free_stat);
3394 
3395 	list_add_tail(element, &priv->tx_pend_list);
3396 	INC_STAT(&priv->tx_pend_stat);
3397 
3398 	ipw2100_tx_send_data(priv);
3399 
3400 	spin_unlock_irqrestore(&priv->low_lock, flags);
3401 	return NETDEV_TX_OK;
3402 
3403 fail_unlock:
3404 	netif_stop_queue(dev);
3405 	spin_unlock_irqrestore(&priv->low_lock, flags);
3406 	return NETDEV_TX_BUSY;
3407 }
3408 
3409 static int ipw2100_msg_allocate(struct ipw2100_priv *priv)
3410 {
3411 	int i, j, err = -EINVAL;
3412 	void *v;
3413 	dma_addr_t p;
3414 
3415 	priv->msg_buffers =
3416 	    kmalloc_array(IPW_COMMAND_POOL_SIZE,
3417 			  sizeof(struct ipw2100_tx_packet),
3418 			  GFP_KERNEL);
3419 	if (!priv->msg_buffers)
3420 		return -ENOMEM;
3421 
3422 	for (i = 0; i < IPW_COMMAND_POOL_SIZE; i++) {
3423 		v = dma_alloc_coherent(&priv->pci_dev->dev,
3424 				       sizeof(struct ipw2100_cmd_header), &p,
3425 				       GFP_KERNEL);
3426 		if (!v) {
3427 			printk(KERN_ERR DRV_NAME ": "
3428 			       "%s: PCI alloc failed for msg "
3429 			       "buffers.\n", priv->net_dev->name);
3430 			err = -ENOMEM;
3431 			break;
3432 		}
3433 
3434 		priv->msg_buffers[i].type = COMMAND;
3435 		priv->msg_buffers[i].info.c_struct.cmd =
3436 		    (struct ipw2100_cmd_header *)v;
3437 		priv->msg_buffers[i].info.c_struct.cmd_phys = p;
3438 	}
3439 
3440 	if (i == IPW_COMMAND_POOL_SIZE)
3441 		return 0;
3442 
3443 	for (j = 0; j < i; j++) {
3444 		dma_free_coherent(&priv->pci_dev->dev,
3445 				  sizeof(struct ipw2100_cmd_header),
3446 				  priv->msg_buffers[j].info.c_struct.cmd,
3447 				  priv->msg_buffers[j].info.c_struct.cmd_phys);
3448 	}
3449 
3450 	kfree(priv->msg_buffers);
3451 	priv->msg_buffers = NULL;
3452 
3453 	return err;
3454 }
3455 
3456 static int ipw2100_msg_initialize(struct ipw2100_priv *priv)
3457 {
3458 	int i;
3459 
3460 	INIT_LIST_HEAD(&priv->msg_free_list);
3461 	INIT_LIST_HEAD(&priv->msg_pend_list);
3462 
3463 	for (i = 0; i < IPW_COMMAND_POOL_SIZE; i++)
3464 		list_add_tail(&priv->msg_buffers[i].list, &priv->msg_free_list);
3465 	SET_STAT(&priv->msg_free_stat, i);
3466 
3467 	return 0;
3468 }
3469 
3470 static void ipw2100_msg_free(struct ipw2100_priv *priv)
3471 {
3472 	int i;
3473 
3474 	if (!priv->msg_buffers)
3475 		return;
3476 
3477 	for (i = 0; i < IPW_COMMAND_POOL_SIZE; i++) {
3478 		dma_free_coherent(&priv->pci_dev->dev,
3479 				  sizeof(struct ipw2100_cmd_header),
3480 				  priv->msg_buffers[i].info.c_struct.cmd,
3481 				  priv->msg_buffers[i].info.c_struct.cmd_phys);
3482 	}
3483 
3484 	kfree(priv->msg_buffers);
3485 	priv->msg_buffers = NULL;
3486 }
3487 
3488 static ssize_t pci_show(struct device *d, struct device_attribute *attr,
3489 			char *buf)
3490 {
3491 	struct pci_dev *pci_dev = to_pci_dev(d);
3492 	char *out = buf;
3493 	int i, j;
3494 	u32 val;
3495 
3496 	for (i = 0; i < 16; i++) {
3497 		out += sprintf(out, "[%08X] ", i * 16);
3498 		for (j = 0; j < 16; j += 4) {
3499 			pci_read_config_dword(pci_dev, i * 16 + j, &val);
3500 			out += sprintf(out, "%08X ", val);
3501 		}
3502 		out += sprintf(out, "\n");
3503 	}
3504 
3505 	return out - buf;
3506 }
3507 
3508 static DEVICE_ATTR_RO(pci);
3509 
3510 static ssize_t cfg_show(struct device *d, struct device_attribute *attr,
3511 			char *buf)
3512 {
3513 	struct ipw2100_priv *p = dev_get_drvdata(d);
3514 	return sprintf(buf, "0x%08x\n", (int)p->config);
3515 }
3516 
3517 static DEVICE_ATTR_RO(cfg);
3518 
3519 static ssize_t status_show(struct device *d, struct device_attribute *attr,
3520 			   char *buf)
3521 {
3522 	struct ipw2100_priv *p = dev_get_drvdata(d);
3523 	return sprintf(buf, "0x%08x\n", (int)p->status);
3524 }
3525 
3526 static DEVICE_ATTR_RO(status);
3527 
3528 static ssize_t capability_show(struct device *d, struct device_attribute *attr,
3529 			       char *buf)
3530 {
3531 	struct ipw2100_priv *p = dev_get_drvdata(d);
3532 	return sprintf(buf, "0x%08x\n", (int)p->capability);
3533 }
3534 
3535 static DEVICE_ATTR_RO(capability);
3536 
3537 #define IPW2100_REG(x) { IPW_ ##x, #x }
3538 static const struct {
3539 	u32 addr;
3540 	const char *name;
3541 } hw_data[] = {
3542 IPW2100_REG(REG_GP_CNTRL),
3543 	    IPW2100_REG(REG_GPIO),
3544 	    IPW2100_REG(REG_INTA),
3545 	    IPW2100_REG(REG_INTA_MASK), IPW2100_REG(REG_RESET_REG),};
3546 #define IPW2100_NIC(x, s) { x, #x, s }
3547 static const struct {
3548 	u32 addr;
3549 	const char *name;
3550 	size_t size;
3551 } nic_data[] = {
3552 IPW2100_NIC(IPW2100_CONTROL_REG, 2),
3553 	    IPW2100_NIC(0x210014, 1), IPW2100_NIC(0x210000, 1),};
3554 #define IPW2100_ORD(x, d) { IPW_ORD_ ##x, #x, d }
3555 static const struct {
3556 	u8 index;
3557 	const char *name;
3558 	const char *desc;
3559 } ord_data[] = {
3560 IPW2100_ORD(STAT_TX_HOST_REQUESTS, "requested Host Tx's (MSDU)"),
3561 	    IPW2100_ORD(STAT_TX_HOST_COMPLETE,
3562 				"successful Host Tx's (MSDU)"),
3563 	    IPW2100_ORD(STAT_TX_DIR_DATA,
3564 				"successful Directed Tx's (MSDU)"),
3565 	    IPW2100_ORD(STAT_TX_DIR_DATA1,
3566 				"successful Directed Tx's (MSDU) @ 1MB"),
3567 	    IPW2100_ORD(STAT_TX_DIR_DATA2,
3568 				"successful Directed Tx's (MSDU) @ 2MB"),
3569 	    IPW2100_ORD(STAT_TX_DIR_DATA5_5,
3570 				"successful Directed Tx's (MSDU) @ 5_5MB"),
3571 	    IPW2100_ORD(STAT_TX_DIR_DATA11,
3572 				"successful Directed Tx's (MSDU) @ 11MB"),
3573 	    IPW2100_ORD(STAT_TX_NODIR_DATA1,
3574 				"successful Non_Directed Tx's (MSDU) @ 1MB"),
3575 	    IPW2100_ORD(STAT_TX_NODIR_DATA2,
3576 				"successful Non_Directed Tx's (MSDU) @ 2MB"),
3577 	    IPW2100_ORD(STAT_TX_NODIR_DATA5_5,
3578 				"successful Non_Directed Tx's (MSDU) @ 5.5MB"),
3579 	    IPW2100_ORD(STAT_TX_NODIR_DATA11,
3580 				"successful Non_Directed Tx's (MSDU) @ 11MB"),
3581 	    IPW2100_ORD(STAT_NULL_DATA, "successful NULL data Tx's"),
3582 	    IPW2100_ORD(STAT_TX_RTS, "successful Tx RTS"),
3583 	    IPW2100_ORD(STAT_TX_CTS, "successful Tx CTS"),
3584 	    IPW2100_ORD(STAT_TX_ACK, "successful Tx ACK"),
3585 	    IPW2100_ORD(STAT_TX_ASSN, "successful Association Tx's"),
3586 	    IPW2100_ORD(STAT_TX_ASSN_RESP,
3587 				"successful Association response Tx's"),
3588 	    IPW2100_ORD(STAT_TX_REASSN,
3589 				"successful Reassociation Tx's"),
3590 	    IPW2100_ORD(STAT_TX_REASSN_RESP,
3591 				"successful Reassociation response Tx's"),
3592 	    IPW2100_ORD(STAT_TX_PROBE,
3593 				"probes successfully transmitted"),
3594 	    IPW2100_ORD(STAT_TX_PROBE_RESP,
3595 				"probe responses successfully transmitted"),
3596 	    IPW2100_ORD(STAT_TX_BEACON, "tx beacon"),
3597 	    IPW2100_ORD(STAT_TX_ATIM, "Tx ATIM"),
3598 	    IPW2100_ORD(STAT_TX_DISASSN,
3599 				"successful Disassociation TX"),
3600 	    IPW2100_ORD(STAT_TX_AUTH, "successful Authentication Tx"),
3601 	    IPW2100_ORD(STAT_TX_DEAUTH,
3602 				"successful Deauthentication TX"),
3603 	    IPW2100_ORD(STAT_TX_TOTAL_BYTES,
3604 				"Total successful Tx data bytes"),
3605 	    IPW2100_ORD(STAT_TX_RETRIES, "Tx retries"),
3606 	    IPW2100_ORD(STAT_TX_RETRY1, "Tx retries at 1MBPS"),
3607 	    IPW2100_ORD(STAT_TX_RETRY2, "Tx retries at 2MBPS"),
3608 	    IPW2100_ORD(STAT_TX_RETRY5_5, "Tx retries at 5.5MBPS"),
3609 	    IPW2100_ORD(STAT_TX_RETRY11, "Tx retries at 11MBPS"),
3610 	    IPW2100_ORD(STAT_TX_FAILURES, "Tx Failures"),
3611 	    IPW2100_ORD(STAT_TX_MAX_TRIES_IN_HOP,
3612 				"times max tries in a hop failed"),
3613 	    IPW2100_ORD(STAT_TX_DISASSN_FAIL,
3614 				"times disassociation failed"),
3615 	    IPW2100_ORD(STAT_TX_ERR_CTS, "missed/bad CTS frames"),
3616 	    IPW2100_ORD(STAT_TX_ERR_ACK, "tx err due to acks"),
3617 	    IPW2100_ORD(STAT_RX_HOST, "packets passed to host"),
3618 	    IPW2100_ORD(STAT_RX_DIR_DATA, "directed packets"),
3619 	    IPW2100_ORD(STAT_RX_DIR_DATA1, "directed packets at 1MB"),
3620 	    IPW2100_ORD(STAT_RX_DIR_DATA2, "directed packets at 2MB"),
3621 	    IPW2100_ORD(STAT_RX_DIR_DATA5_5,
3622 				"directed packets at 5.5MB"),
3623 	    IPW2100_ORD(STAT_RX_DIR_DATA11, "directed packets at 11MB"),
3624 	    IPW2100_ORD(STAT_RX_NODIR_DATA, "nondirected packets"),
3625 	    IPW2100_ORD(STAT_RX_NODIR_DATA1,
3626 				"nondirected packets at 1MB"),
3627 	    IPW2100_ORD(STAT_RX_NODIR_DATA2,
3628 				"nondirected packets at 2MB"),
3629 	    IPW2100_ORD(STAT_RX_NODIR_DATA5_5,
3630 				"nondirected packets at 5.5MB"),
3631 	    IPW2100_ORD(STAT_RX_NODIR_DATA11,
3632 				"nondirected packets at 11MB"),
3633 	    IPW2100_ORD(STAT_RX_NULL_DATA, "null data rx's"),
3634 	    IPW2100_ORD(STAT_RX_RTS, "Rx RTS"), IPW2100_ORD(STAT_RX_CTS,
3635 								    "Rx CTS"),
3636 	    IPW2100_ORD(STAT_RX_ACK, "Rx ACK"),
3637 	    IPW2100_ORD(STAT_RX_CFEND, "Rx CF End"),
3638 	    IPW2100_ORD(STAT_RX_CFEND_ACK, "Rx CF End + CF Ack"),
3639 	    IPW2100_ORD(STAT_RX_ASSN, "Association Rx's"),
3640 	    IPW2100_ORD(STAT_RX_ASSN_RESP, "Association response Rx's"),
3641 	    IPW2100_ORD(STAT_RX_REASSN, "Reassociation Rx's"),
3642 	    IPW2100_ORD(STAT_RX_REASSN_RESP,
3643 				"Reassociation response Rx's"),
3644 	    IPW2100_ORD(STAT_RX_PROBE, "probe Rx's"),
3645 	    IPW2100_ORD(STAT_RX_PROBE_RESP, "probe response Rx's"),
3646 	    IPW2100_ORD(STAT_RX_BEACON, "Rx beacon"),
3647 	    IPW2100_ORD(STAT_RX_ATIM, "Rx ATIM"),
3648 	    IPW2100_ORD(STAT_RX_DISASSN, "disassociation Rx"),
3649 	    IPW2100_ORD(STAT_RX_AUTH, "authentication Rx"),
3650 	    IPW2100_ORD(STAT_RX_DEAUTH, "deauthentication Rx"),
3651 	    IPW2100_ORD(STAT_RX_TOTAL_BYTES,
3652 				"Total rx data bytes received"),
3653 	    IPW2100_ORD(STAT_RX_ERR_CRC, "packets with Rx CRC error"),
3654 	    IPW2100_ORD(STAT_RX_ERR_CRC1, "Rx CRC errors at 1MB"),
3655 	    IPW2100_ORD(STAT_RX_ERR_CRC2, "Rx CRC errors at 2MB"),
3656 	    IPW2100_ORD(STAT_RX_ERR_CRC5_5, "Rx CRC errors at 5.5MB"),
3657 	    IPW2100_ORD(STAT_RX_ERR_CRC11, "Rx CRC errors at 11MB"),
3658 	    IPW2100_ORD(STAT_RX_DUPLICATE1,
3659 				"duplicate rx packets at 1MB"),
3660 	    IPW2100_ORD(STAT_RX_DUPLICATE2,
3661 				"duplicate rx packets at 2MB"),
3662 	    IPW2100_ORD(STAT_RX_DUPLICATE5_5,
3663 				"duplicate rx packets at 5.5MB"),
3664 	    IPW2100_ORD(STAT_RX_DUPLICATE11,
3665 				"duplicate rx packets at 11MB"),
3666 	    IPW2100_ORD(STAT_RX_DUPLICATE, "duplicate rx packets"),
3667 	    IPW2100_ORD(PERS_DB_LOCK, "locking fw permanent  db"),
3668 	    IPW2100_ORD(PERS_DB_SIZE, "size of fw permanent  db"),
3669 	    IPW2100_ORD(PERS_DB_ADDR, "address of fw permanent  db"),
3670 	    IPW2100_ORD(STAT_RX_INVALID_PROTOCOL,
3671 				"rx frames with invalid protocol"),
3672 	    IPW2100_ORD(SYS_BOOT_TIME, "Boot time"),
3673 	    IPW2100_ORD(STAT_RX_NO_BUFFER,
3674 				"rx frames rejected due to no buffer"),
3675 	    IPW2100_ORD(STAT_RX_MISSING_FRAG,
3676 				"rx frames dropped due to missing fragment"),
3677 	    IPW2100_ORD(STAT_RX_ORPHAN_FRAG,
3678 				"rx frames dropped due to non-sequential fragment"),
3679 	    IPW2100_ORD(STAT_RX_ORPHAN_FRAME,
3680 				"rx frames dropped due to unmatched 1st frame"),
3681 	    IPW2100_ORD(STAT_RX_FRAG_AGEOUT,
3682 				"rx frames dropped due to uncompleted frame"),
3683 	    IPW2100_ORD(STAT_RX_ICV_ERRORS,
3684 				"ICV errors during decryption"),
3685 	    IPW2100_ORD(STAT_PSP_SUSPENSION, "times adapter suspended"),
3686 	    IPW2100_ORD(STAT_PSP_BCN_TIMEOUT, "beacon timeout"),
3687 	    IPW2100_ORD(STAT_PSP_POLL_TIMEOUT,
3688 				"poll response timeouts"),
3689 	    IPW2100_ORD(STAT_PSP_NONDIR_TIMEOUT,
3690 				"timeouts waiting for last {broad,multi}cast pkt"),
3691 	    IPW2100_ORD(STAT_PSP_RX_DTIMS, "PSP DTIMs received"),
3692 	    IPW2100_ORD(STAT_PSP_RX_TIMS, "PSP TIMs received"),
3693 	    IPW2100_ORD(STAT_PSP_STATION_ID, "PSP Station ID"),
3694 	    IPW2100_ORD(LAST_ASSN_TIME, "RTC time of last association"),
3695 	    IPW2100_ORD(STAT_PERCENT_MISSED_BCNS,
3696 				"current calculation of % missed beacons"),
3697 	    IPW2100_ORD(STAT_PERCENT_RETRIES,
3698 				"current calculation of % missed tx retries"),
3699 	    IPW2100_ORD(ASSOCIATED_AP_PTR,
3700 				"0 if not associated, else pointer to AP table entry"),
3701 	    IPW2100_ORD(AVAILABLE_AP_CNT,
3702 				"AP's described in the AP table"),
3703 	    IPW2100_ORD(AP_LIST_PTR, "Ptr to list of available APs"),
3704 	    IPW2100_ORD(STAT_AP_ASSNS, "associations"),
3705 	    IPW2100_ORD(STAT_ASSN_FAIL, "association failures"),
3706 	    IPW2100_ORD(STAT_ASSN_RESP_FAIL,
3707 				"failures due to response fail"),
3708 	    IPW2100_ORD(STAT_FULL_SCANS, "full scans"),
3709 	    IPW2100_ORD(CARD_DISABLED, "Card Disabled"),
3710 	    IPW2100_ORD(STAT_ROAM_INHIBIT,
3711 				"times roaming was inhibited due to activity"),
3712 	    IPW2100_ORD(RSSI_AT_ASSN,
3713 				"RSSI of associated AP at time of association"),
3714 	    IPW2100_ORD(STAT_ASSN_CAUSE1,
3715 				"reassociation: no probe response or TX on hop"),
3716 	    IPW2100_ORD(STAT_ASSN_CAUSE2,
3717 				"reassociation: poor tx/rx quality"),
3718 	    IPW2100_ORD(STAT_ASSN_CAUSE3,
3719 				"reassociation: tx/rx quality (excessive AP load"),
3720 	    IPW2100_ORD(STAT_ASSN_CAUSE4,
3721 				"reassociation: AP RSSI level"),
3722 	    IPW2100_ORD(STAT_ASSN_CAUSE5,
3723 				"reassociations due to load leveling"),
3724 	    IPW2100_ORD(STAT_AUTH_FAIL, "times authentication failed"),
3725 	    IPW2100_ORD(STAT_AUTH_RESP_FAIL,
3726 				"times authentication response failed"),
3727 	    IPW2100_ORD(STATION_TABLE_CNT,
3728 				"entries in association table"),
3729 	    IPW2100_ORD(RSSI_AVG_CURR, "Current avg RSSI"),
3730 	    IPW2100_ORD(POWER_MGMT_MODE, "Power mode - 0=CAM, 1=PSP"),
3731 	    IPW2100_ORD(COUNTRY_CODE,
3732 				"IEEE country code as recv'd from beacon"),
3733 	    IPW2100_ORD(COUNTRY_CHANNELS,
3734 				"channels supported by country"),
3735 	    IPW2100_ORD(RESET_CNT, "adapter resets (warm)"),
3736 	    IPW2100_ORD(BEACON_INTERVAL, "Beacon interval"),
3737 	    IPW2100_ORD(ANTENNA_DIVERSITY,
3738 				"TRUE if antenna diversity is disabled"),
3739 	    IPW2100_ORD(DTIM_PERIOD, "beacon intervals between DTIMs"),
3740 	    IPW2100_ORD(OUR_FREQ,
3741 				"current radio freq lower digits - channel ID"),
3742 	    IPW2100_ORD(RTC_TIME, "current RTC time"),
3743 	    IPW2100_ORD(PORT_TYPE, "operating mode"),
3744 	    IPW2100_ORD(CURRENT_TX_RATE, "current tx rate"),
3745 	    IPW2100_ORD(SUPPORTED_RATES, "supported tx rates"),
3746 	    IPW2100_ORD(ATIM_WINDOW, "current ATIM Window"),
3747 	    IPW2100_ORD(BASIC_RATES, "basic tx rates"),
3748 	    IPW2100_ORD(NIC_HIGHEST_RATE, "NIC highest tx rate"),
3749 	    IPW2100_ORD(AP_HIGHEST_RATE, "AP highest tx rate"),
3750 	    IPW2100_ORD(CAPABILITIES,
3751 				"Management frame capability field"),
3752 	    IPW2100_ORD(AUTH_TYPE, "Type of authentication"),
3753 	    IPW2100_ORD(RADIO_TYPE, "Adapter card platform type"),
3754 	    IPW2100_ORD(RTS_THRESHOLD,
3755 				"Min packet length for RTS handshaking"),
3756 	    IPW2100_ORD(INT_MODE, "International mode"),
3757 	    IPW2100_ORD(FRAGMENTATION_THRESHOLD,
3758 				"protocol frag threshold"),
3759 	    IPW2100_ORD(EEPROM_SRAM_DB_BLOCK_START_ADDRESS,
3760 				"EEPROM offset in SRAM"),
3761 	    IPW2100_ORD(EEPROM_SRAM_DB_BLOCK_SIZE,
3762 				"EEPROM size in SRAM"),
3763 	    IPW2100_ORD(EEPROM_SKU_CAPABILITY, "EEPROM SKU Capability"),
3764 	    IPW2100_ORD(EEPROM_IBSS_11B_CHANNELS,
3765 				"EEPROM IBSS 11b channel set"),
3766 	    IPW2100_ORD(MAC_VERSION, "MAC Version"),
3767 	    IPW2100_ORD(MAC_REVISION, "MAC Revision"),
3768 	    IPW2100_ORD(RADIO_VERSION, "Radio Version"),
3769 	    IPW2100_ORD(NIC_MANF_DATE_TIME, "MANF Date/Time STAMP"),
3770 	    IPW2100_ORD(UCODE_VERSION, "Ucode Version"),};
3771 
3772 static ssize_t registers_show(struct device *d, struct device_attribute *attr,
3773 			      char *buf)
3774 {
3775 	int i;
3776 	struct ipw2100_priv *priv = dev_get_drvdata(d);
3777 	struct net_device *dev = priv->net_dev;
3778 	char *out = buf;
3779 	u32 val = 0;
3780 
3781 	out += sprintf(out, "%30s [Address ] : Hex\n", "Register");
3782 
3783 	for (i = 0; i < ARRAY_SIZE(hw_data); i++) {
3784 		read_register(dev, hw_data[i].addr, &val);
3785 		out += sprintf(out, "%30s [%08X] : %08X\n",
3786 			       hw_data[i].name, hw_data[i].addr, val);
3787 	}
3788 
3789 	return out - buf;
3790 }
3791 
3792 static DEVICE_ATTR_RO(registers);
3793 
3794 static ssize_t hardware_show(struct device *d, struct device_attribute *attr,
3795 			     char *buf)
3796 {
3797 	struct ipw2100_priv *priv = dev_get_drvdata(d);
3798 	struct net_device *dev = priv->net_dev;
3799 	char *out = buf;
3800 	int i;
3801 
3802 	out += sprintf(out, "%30s [Address ] : Hex\n", "NIC entry");
3803 
3804 	for (i = 0; i < ARRAY_SIZE(nic_data); i++) {
3805 		u8 tmp8;
3806 		u16 tmp16;
3807 		u32 tmp32;
3808 
3809 		switch (nic_data[i].size) {
3810 		case 1:
3811 			read_nic_byte(dev, nic_data[i].addr, &tmp8);
3812 			out += sprintf(out, "%30s [%08X] : %02X\n",
3813 				       nic_data[i].name, nic_data[i].addr,
3814 				       tmp8);
3815 			break;
3816 		case 2:
3817 			read_nic_word(dev, nic_data[i].addr, &tmp16);
3818 			out += sprintf(out, "%30s [%08X] : %04X\n",
3819 				       nic_data[i].name, nic_data[i].addr,
3820 				       tmp16);
3821 			break;
3822 		case 4:
3823 			read_nic_dword(dev, nic_data[i].addr, &tmp32);
3824 			out += sprintf(out, "%30s [%08X] : %08X\n",
3825 				       nic_data[i].name, nic_data[i].addr,
3826 				       tmp32);
3827 			break;
3828 		}
3829 	}
3830 	return out - buf;
3831 }
3832 
3833 static DEVICE_ATTR_RO(hardware);
3834 
3835 static ssize_t memory_show(struct device *d, struct device_attribute *attr,
3836 			   char *buf)
3837 {
3838 	struct ipw2100_priv *priv = dev_get_drvdata(d);
3839 	struct net_device *dev = priv->net_dev;
3840 	static unsigned long loop = 0;
3841 	int len = 0;
3842 	u32 buffer[4];
3843 	int i;
3844 	char line[81];
3845 
3846 	if (loop >= 0x30000)
3847 		loop = 0;
3848 
3849 	/* sysfs provides us PAGE_SIZE buffer */
3850 	while (len < PAGE_SIZE - 128 && loop < 0x30000) {
3851 
3852 		if (priv->snapshot[0])
3853 			for (i = 0; i < 4; i++)
3854 				buffer[i] =
3855 				    *(u32 *) SNAPSHOT_ADDR(loop + i * 4);
3856 		else
3857 			for (i = 0; i < 4; i++)
3858 				read_nic_dword(dev, loop + i * 4, &buffer[i]);
3859 
3860 		if (priv->dump_raw)
3861 			len += sprintf(buf + len,
3862 				       "%c%c%c%c"
3863 				       "%c%c%c%c"
3864 				       "%c%c%c%c"
3865 				       "%c%c%c%c",
3866 				       ((u8 *) buffer)[0x0],
3867 				       ((u8 *) buffer)[0x1],
3868 				       ((u8 *) buffer)[0x2],
3869 				       ((u8 *) buffer)[0x3],
3870 				       ((u8 *) buffer)[0x4],
3871 				       ((u8 *) buffer)[0x5],
3872 				       ((u8 *) buffer)[0x6],
3873 				       ((u8 *) buffer)[0x7],
3874 				       ((u8 *) buffer)[0x8],
3875 				       ((u8 *) buffer)[0x9],
3876 				       ((u8 *) buffer)[0xa],
3877 				       ((u8 *) buffer)[0xb],
3878 				       ((u8 *) buffer)[0xc],
3879 				       ((u8 *) buffer)[0xd],
3880 				       ((u8 *) buffer)[0xe],
3881 				       ((u8 *) buffer)[0xf]);
3882 		else
3883 			len += sprintf(buf + len, "%s\n",
3884 				       snprint_line(line, sizeof(line),
3885 						    (u8 *) buffer, 16, loop));
3886 		loop += 16;
3887 	}
3888 
3889 	return len;
3890 }
3891 
3892 static ssize_t memory_store(struct device *d, struct device_attribute *attr,
3893 			    const char *buf, size_t count)
3894 {
3895 	struct ipw2100_priv *priv = dev_get_drvdata(d);
3896 	struct net_device *dev = priv->net_dev;
3897 	const char *p = buf;
3898 
3899 	(void)dev;		/* kill unused-var warning for debug-only code */
3900 
3901 	if (count < 1)
3902 		return count;
3903 
3904 	if (p[0] == '1' ||
3905 	    (count >= 2 && tolower(p[0]) == 'o' && tolower(p[1]) == 'n')) {
3906 		IPW_DEBUG_INFO("%s: Setting memory dump to RAW mode.\n",
3907 			       dev->name);
3908 		priv->dump_raw = 1;
3909 
3910 	} else if (p[0] == '0' || (count >= 2 && tolower(p[0]) == 'o' &&
3911 				   tolower(p[1]) == 'f')) {
3912 		IPW_DEBUG_INFO("%s: Setting memory dump to HEX mode.\n",
3913 			       dev->name);
3914 		priv->dump_raw = 0;
3915 
3916 	} else if (tolower(p[0]) == 'r') {
3917 		IPW_DEBUG_INFO("%s: Resetting firmware snapshot.\n", dev->name);
3918 		ipw2100_snapshot_free(priv);
3919 
3920 	} else
3921 		IPW_DEBUG_INFO("%s: Usage: 0|on = HEX, 1|off = RAW, "
3922 			       "reset = clear memory snapshot\n", dev->name);
3923 
3924 	return count;
3925 }
3926 
3927 static DEVICE_ATTR_RW(memory);
3928 
3929 static ssize_t ordinals_show(struct device *d, struct device_attribute *attr,
3930 			     char *buf)
3931 {
3932 	struct ipw2100_priv *priv = dev_get_drvdata(d);
3933 	u32 val = 0;
3934 	int len = 0;
3935 	u32 val_len;
3936 	static int loop = 0;
3937 
3938 	if (priv->status & STATUS_RF_KILL_MASK)
3939 		return 0;
3940 
3941 	if (loop >= ARRAY_SIZE(ord_data))
3942 		loop = 0;
3943 
3944 	/* sysfs provides us PAGE_SIZE buffer */
3945 	while (len < PAGE_SIZE - 128 && loop < ARRAY_SIZE(ord_data)) {
3946 		val_len = sizeof(u32);
3947 
3948 		if (ipw2100_get_ordinal(priv, ord_data[loop].index, &val,
3949 					&val_len))
3950 			len += sprintf(buf + len, "[0x%02X] = ERROR    %s\n",
3951 				       ord_data[loop].index,
3952 				       ord_data[loop].desc);
3953 		else
3954 			len += sprintf(buf + len, "[0x%02X] = 0x%08X %s\n",
3955 				       ord_data[loop].index, val,
3956 				       ord_data[loop].desc);
3957 		loop++;
3958 	}
3959 
3960 	return len;
3961 }
3962 
3963 static DEVICE_ATTR_RO(ordinals);
3964 
3965 static ssize_t stats_show(struct device *d, struct device_attribute *attr,
3966 			  char *buf)
3967 {
3968 	struct ipw2100_priv *priv = dev_get_drvdata(d);
3969 	char *out = buf;
3970 
3971 	out += sprintf(out, "interrupts: %d {tx: %d, rx: %d, other: %d}\n",
3972 		       priv->interrupts, priv->tx_interrupts,
3973 		       priv->rx_interrupts, priv->inta_other);
3974 	out += sprintf(out, "firmware resets: %d\n", priv->resets);
3975 	out += sprintf(out, "firmware hangs: %d\n", priv->hangs);
3976 #ifdef CONFIG_IPW2100_DEBUG
3977 	out += sprintf(out, "packet mismatch image: %s\n",
3978 		       priv->snapshot[0] ? "YES" : "NO");
3979 #endif
3980 
3981 	return out - buf;
3982 }
3983 
3984 static DEVICE_ATTR_RO(stats);
3985 
3986 static int ipw2100_switch_mode(struct ipw2100_priv *priv, u32 mode)
3987 {
3988 	int err;
3989 
3990 	if (mode == priv->ieee->iw_mode)
3991 		return 0;
3992 
3993 	err = ipw2100_disable_adapter(priv);
3994 	if (err) {
3995 		printk(KERN_ERR DRV_NAME ": %s: Could not disable adapter %d\n",
3996 		       priv->net_dev->name, err);
3997 		return err;
3998 	}
3999 
4000 	switch (mode) {
4001 	case IW_MODE_INFRA:
4002 		priv->net_dev->type = ARPHRD_ETHER;
4003 		break;
4004 	case IW_MODE_ADHOC:
4005 		priv->net_dev->type = ARPHRD_ETHER;
4006 		break;
4007 #ifdef CONFIG_IPW2100_MONITOR
4008 	case IW_MODE_MONITOR:
4009 		priv->last_mode = priv->ieee->iw_mode;
4010 		priv->net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
4011 		break;
4012 #endif				/* CONFIG_IPW2100_MONITOR */
4013 	}
4014 
4015 	priv->ieee->iw_mode = mode;
4016 
4017 #ifdef CONFIG_PM
4018 	/* Indicate ipw2100_download_firmware download firmware
4019 	 * from disk instead of memory. */
4020 	ipw2100_firmware.version = 0;
4021 #endif
4022 
4023 	printk(KERN_INFO "%s: Resetting on mode change.\n", priv->net_dev->name);
4024 	priv->reset_backoff = 0;
4025 	schedule_reset(priv);
4026 
4027 	return 0;
4028 }
4029 
4030 static ssize_t internals_show(struct device *d, struct device_attribute *attr,
4031 			      char *buf)
4032 {
4033 	struct ipw2100_priv *priv = dev_get_drvdata(d);
4034 	int len = 0;
4035 
4036 #define DUMP_VAR(x,y) len += sprintf(buf + len, # x ": %" y "\n", priv-> x)
4037 
4038 	if (priv->status & STATUS_ASSOCIATED)
4039 		len += sprintf(buf + len, "connected: %llu\n",
4040 			       ktime_get_boottime_seconds() - priv->connect_start);
4041 	else
4042 		len += sprintf(buf + len, "not connected\n");
4043 
4044 	DUMP_VAR(ieee->crypt_info.crypt[priv->ieee->crypt_info.tx_keyidx], "p");
4045 	DUMP_VAR(status, "08lx");
4046 	DUMP_VAR(config, "08lx");
4047 	DUMP_VAR(capability, "08lx");
4048 
4049 	len +=
4050 	    sprintf(buf + len, "last_rtc: %lu\n",
4051 		    (unsigned long)priv->last_rtc);
4052 
4053 	DUMP_VAR(fatal_error, "d");
4054 	DUMP_VAR(stop_hang_check, "d");
4055 	DUMP_VAR(stop_rf_kill, "d");
4056 	DUMP_VAR(messages_sent, "d");
4057 
4058 	DUMP_VAR(tx_pend_stat.value, "d");
4059 	DUMP_VAR(tx_pend_stat.hi, "d");
4060 
4061 	DUMP_VAR(tx_free_stat.value, "d");
4062 	DUMP_VAR(tx_free_stat.lo, "d");
4063 
4064 	DUMP_VAR(msg_free_stat.value, "d");
4065 	DUMP_VAR(msg_free_stat.lo, "d");
4066 
4067 	DUMP_VAR(msg_pend_stat.value, "d");
4068 	DUMP_VAR(msg_pend_stat.hi, "d");
4069 
4070 	DUMP_VAR(fw_pend_stat.value, "d");
4071 	DUMP_VAR(fw_pend_stat.hi, "d");
4072 
4073 	DUMP_VAR(txq_stat.value, "d");
4074 	DUMP_VAR(txq_stat.lo, "d");
4075 
4076 	DUMP_VAR(ieee->scans, "d");
4077 	DUMP_VAR(reset_backoff, "lld");
4078 
4079 	return len;
4080 }
4081 
4082 static DEVICE_ATTR_RO(internals);
4083 
4084 static ssize_t bssinfo_show(struct device *d, struct device_attribute *attr,
4085 			    char *buf)
4086 {
4087 	struct ipw2100_priv *priv = dev_get_drvdata(d);
4088 	char essid[IW_ESSID_MAX_SIZE + 1];
4089 	u8 bssid[ETH_ALEN];
4090 	u32 chan = 0;
4091 	char *out = buf;
4092 	unsigned int length;
4093 	int ret;
4094 
4095 	if (priv->status & STATUS_RF_KILL_MASK)
4096 		return 0;
4097 
4098 	memset(essid, 0, sizeof(essid));
4099 	memset(bssid, 0, sizeof(bssid));
4100 
4101 	length = IW_ESSID_MAX_SIZE;
4102 	ret = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_SSID, essid, &length);
4103 	if (ret)
4104 		IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
4105 			       __LINE__);
4106 
4107 	length = sizeof(bssid);
4108 	ret = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_AP_BSSID,
4109 				  bssid, &length);
4110 	if (ret)
4111 		IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
4112 			       __LINE__);
4113 
4114 	length = sizeof(u32);
4115 	ret = ipw2100_get_ordinal(priv, IPW_ORD_OUR_FREQ, &chan, &length);
4116 	if (ret)
4117 		IPW_DEBUG_INFO("failed querying ordinals at line %d\n",
4118 			       __LINE__);
4119 
4120 	out += sprintf(out, "ESSID: %s\n", essid);
4121 	out += sprintf(out, "BSSID:   %pM\n", bssid);
4122 	out += sprintf(out, "Channel: %d\n", chan);
4123 
4124 	return out - buf;
4125 }
4126 
4127 static DEVICE_ATTR_RO(bssinfo);
4128 
4129 #ifdef CONFIG_IPW2100_DEBUG
4130 static ssize_t debug_level_show(struct device_driver *d, char *buf)
4131 {
4132 	return sprintf(buf, "0x%08X\n", ipw2100_debug_level);
4133 }
4134 
4135 static ssize_t debug_level_store(struct device_driver *d,
4136 				 const char *buf, size_t count)
4137 {
4138 	u32 val;
4139 	int ret;
4140 
4141 	ret = kstrtou32(buf, 0, &val);
4142 	if (ret)
4143 		IPW_DEBUG_INFO(": %s is not in hex or decimal form.\n", buf);
4144 	else
4145 		ipw2100_debug_level = val;
4146 
4147 	return strnlen(buf, count);
4148 }
4149 static DRIVER_ATTR_RW(debug_level);
4150 #endif				/* CONFIG_IPW2100_DEBUG */
4151 
4152 static ssize_t fatal_error_show(struct device *d,
4153 				struct device_attribute *attr, char *buf)
4154 {
4155 	struct ipw2100_priv *priv = dev_get_drvdata(d);
4156 	char *out = buf;
4157 	int i;
4158 
4159 	if (priv->fatal_error)
4160 		out += sprintf(out, "0x%08X\n", priv->fatal_error);
4161 	else
4162 		out += sprintf(out, "0\n");
4163 
4164 	for (i = 1; i <= IPW2100_ERROR_QUEUE; i++) {
4165 		if (!priv->fatal_errors[(priv->fatal_index - i) %
4166 					IPW2100_ERROR_QUEUE])
4167 			continue;
4168 
4169 		out += sprintf(out, "%d. 0x%08X\n", i,
4170 			       priv->fatal_errors[(priv->fatal_index - i) %
4171 						  IPW2100_ERROR_QUEUE]);
4172 	}
4173 
4174 	return out - buf;
4175 }
4176 
4177 static ssize_t fatal_error_store(struct device *d,
4178 				 struct device_attribute *attr, const char *buf,
4179 				 size_t count)
4180 {
4181 	struct ipw2100_priv *priv = dev_get_drvdata(d);
4182 	schedule_reset(priv);
4183 	return count;
4184 }
4185 
4186 static DEVICE_ATTR_RW(fatal_error);
4187 
4188 static ssize_t scan_age_show(struct device *d, struct device_attribute *attr,
4189 			     char *buf)
4190 {
4191 	struct ipw2100_priv *priv = dev_get_drvdata(d);
4192 	return sprintf(buf, "%d\n", priv->ieee->scan_age);
4193 }
4194 
4195 static ssize_t scan_age_store(struct device *d, struct device_attribute *attr,
4196 			      const char *buf, size_t count)
4197 {
4198 	struct ipw2100_priv *priv = dev_get_drvdata(d);
4199 	struct net_device *dev = priv->net_dev;
4200 	unsigned long val;
4201 	int ret;
4202 
4203 	(void)dev;		/* kill unused-var warning for debug-only code */
4204 
4205 	IPW_DEBUG_INFO("enter\n");
4206 
4207 	ret = kstrtoul(buf, 0, &val);
4208 	if (ret) {
4209 		IPW_DEBUG_INFO("%s: user supplied invalid value.\n", dev->name);
4210 	} else {
4211 		priv->ieee->scan_age = val;
4212 		IPW_DEBUG_INFO("set scan_age = %u\n", priv->ieee->scan_age);
4213 	}
4214 
4215 	IPW_DEBUG_INFO("exit\n");
4216 	return strnlen(buf, count);
4217 }
4218 
4219 static DEVICE_ATTR_RW(scan_age);
4220 
4221 static ssize_t rf_kill_show(struct device *d, struct device_attribute *attr,
4222 			    char *buf)
4223 {
4224 	/* 0 - RF kill not enabled
4225 	   1 - SW based RF kill active (sysfs)
4226 	   2 - HW based RF kill active
4227 	   3 - Both HW and SW baed RF kill active */
4228 	struct ipw2100_priv *priv = dev_get_drvdata(d);
4229 	int val = ((priv->status & STATUS_RF_KILL_SW) ? 0x1 : 0x0) |
4230 	    (rf_kill_active(priv) ? 0x2 : 0x0);
4231 	return sprintf(buf, "%i\n", val);
4232 }
4233 
4234 static int ipw_radio_kill_sw(struct ipw2100_priv *priv, int disable_radio)
4235 {
4236 	if ((disable_radio ? 1 : 0) ==
4237 	    (priv->status & STATUS_RF_KILL_SW ? 1 : 0))
4238 		return 0;
4239 
4240 	IPW_DEBUG_RF_KILL("Manual SW RF Kill set to: RADIO  %s\n",
4241 			  disable_radio ? "OFF" : "ON");
4242 
4243 	mutex_lock(&priv->action_mutex);
4244 
4245 	if (disable_radio) {
4246 		priv->status |= STATUS_RF_KILL_SW;
4247 		ipw2100_down(priv);
4248 	} else {
4249 		priv->status &= ~STATUS_RF_KILL_SW;
4250 		if (rf_kill_active(priv)) {
4251 			IPW_DEBUG_RF_KILL("Can not turn radio back on - "
4252 					  "disabled by HW switch\n");
4253 			/* Make sure the RF_KILL check timer is running */
4254 			priv->stop_rf_kill = 0;
4255 			mod_delayed_work(system_wq, &priv->rf_kill,
4256 					 round_jiffies_relative(HZ));
4257 		} else
4258 			schedule_reset(priv);
4259 	}
4260 
4261 	mutex_unlock(&priv->action_mutex);
4262 	return 1;
4263 }
4264 
4265 static ssize_t rf_kill_store(struct device *d, struct device_attribute *attr,
4266 			     const char *buf, size_t count)
4267 {
4268 	struct ipw2100_priv *priv = dev_get_drvdata(d);
4269 	ipw_radio_kill_sw(priv, buf[0] == '1');
4270 	return count;
4271 }
4272 
4273 static DEVICE_ATTR_RW(rf_kill);
4274 
4275 static struct attribute *ipw2100_sysfs_entries[] = {
4276 	&dev_attr_hardware.attr,
4277 	&dev_attr_registers.attr,
4278 	&dev_attr_ordinals.attr,
4279 	&dev_attr_pci.attr,
4280 	&dev_attr_stats.attr,
4281 	&dev_attr_internals.attr,
4282 	&dev_attr_bssinfo.attr,
4283 	&dev_attr_memory.attr,
4284 	&dev_attr_scan_age.attr,
4285 	&dev_attr_fatal_error.attr,
4286 	&dev_attr_rf_kill.attr,
4287 	&dev_attr_cfg.attr,
4288 	&dev_attr_status.attr,
4289 	&dev_attr_capability.attr,
4290 	NULL,
4291 };
4292 
4293 static const struct attribute_group ipw2100_attribute_group = {
4294 	.attrs = ipw2100_sysfs_entries,
4295 };
4296 
4297 static int status_queue_allocate(struct ipw2100_priv *priv, int entries)
4298 {
4299 	struct ipw2100_status_queue *q = &priv->status_queue;
4300 
4301 	IPW_DEBUG_INFO("enter\n");
4302 
4303 	q->size = entries * sizeof(struct ipw2100_status);
4304 	q->drv = dma_alloc_coherent(&priv->pci_dev->dev, q->size, &q->nic,
4305 				    GFP_KERNEL);
4306 	if (!q->drv) {
4307 		IPW_DEBUG_WARNING("Can not allocate status queue.\n");
4308 		return -ENOMEM;
4309 	}
4310 
4311 	IPW_DEBUG_INFO("exit\n");
4312 
4313 	return 0;
4314 }
4315 
4316 static void status_queue_free(struct ipw2100_priv *priv)
4317 {
4318 	IPW_DEBUG_INFO("enter\n");
4319 
4320 	if (priv->status_queue.drv) {
4321 		dma_free_coherent(&priv->pci_dev->dev,
4322 				  priv->status_queue.size,
4323 				  priv->status_queue.drv,
4324 				  priv->status_queue.nic);
4325 		priv->status_queue.drv = NULL;
4326 	}
4327 
4328 	IPW_DEBUG_INFO("exit\n");
4329 }
4330 
4331 static int bd_queue_allocate(struct ipw2100_priv *priv,
4332 			     struct ipw2100_bd_queue *q, int entries)
4333 {
4334 	IPW_DEBUG_INFO("enter\n");
4335 
4336 	memset(q, 0, sizeof(struct ipw2100_bd_queue));
4337 
4338 	q->entries = entries;
4339 	q->size = entries * sizeof(struct ipw2100_bd);
4340 	q->drv = dma_alloc_coherent(&priv->pci_dev->dev, q->size, &q->nic,
4341 				    GFP_KERNEL);
4342 	if (!q->drv) {
4343 		IPW_DEBUG_INFO
4344 		    ("can't allocate shared memory for buffer descriptors\n");
4345 		return -ENOMEM;
4346 	}
4347 
4348 	IPW_DEBUG_INFO("exit\n");
4349 
4350 	return 0;
4351 }
4352 
4353 static void bd_queue_free(struct ipw2100_priv *priv, struct ipw2100_bd_queue *q)
4354 {
4355 	IPW_DEBUG_INFO("enter\n");
4356 
4357 	if (!q)
4358 		return;
4359 
4360 	if (q->drv) {
4361 		dma_free_coherent(&priv->pci_dev->dev, q->size, q->drv,
4362 				  q->nic);
4363 		q->drv = NULL;
4364 	}
4365 
4366 	IPW_DEBUG_INFO("exit\n");
4367 }
4368 
4369 static void bd_queue_initialize(struct ipw2100_priv *priv,
4370 				struct ipw2100_bd_queue *q, u32 base, u32 size,
4371 				u32 r, u32 w)
4372 {
4373 	IPW_DEBUG_INFO("enter\n");
4374 
4375 	IPW_DEBUG_INFO("initializing bd queue at virt=%p, phys=%08x\n", q->drv,
4376 		       (u32) q->nic);
4377 
4378 	write_register(priv->net_dev, base, q->nic);
4379 	write_register(priv->net_dev, size, q->entries);
4380 	write_register(priv->net_dev, r, q->oldest);
4381 	write_register(priv->net_dev, w, q->next);
4382 
4383 	IPW_DEBUG_INFO("exit\n");
4384 }
4385 
4386 static void ipw2100_kill_works(struct ipw2100_priv *priv)
4387 {
4388 	priv->stop_rf_kill = 1;
4389 	priv->stop_hang_check = 1;
4390 	cancel_delayed_work_sync(&priv->reset_work);
4391 	cancel_delayed_work_sync(&priv->security_work);
4392 	cancel_delayed_work_sync(&priv->wx_event_work);
4393 	cancel_delayed_work_sync(&priv->hang_check);
4394 	cancel_delayed_work_sync(&priv->rf_kill);
4395 	cancel_delayed_work_sync(&priv->scan_event);
4396 }
4397 
4398 static int ipw2100_tx_allocate(struct ipw2100_priv *priv)
4399 {
4400 	int i, j, err;
4401 	void *v;
4402 	dma_addr_t p;
4403 
4404 	IPW_DEBUG_INFO("enter\n");
4405 
4406 	err = bd_queue_allocate(priv, &priv->tx_queue, TX_QUEUE_LENGTH);
4407 	if (err) {
4408 		IPW_DEBUG_ERROR("%s: failed bd_queue_allocate\n",
4409 				priv->net_dev->name);
4410 		return err;
4411 	}
4412 
4413 	priv->tx_buffers = kmalloc_array(TX_PENDED_QUEUE_LENGTH,
4414 					 sizeof(struct ipw2100_tx_packet),
4415 					 GFP_KERNEL);
4416 	if (!priv->tx_buffers) {
4417 		bd_queue_free(priv, &priv->tx_queue);
4418 		return -ENOMEM;
4419 	}
4420 
4421 	for (i = 0; i < TX_PENDED_QUEUE_LENGTH; i++) {
4422 		v = dma_alloc_coherent(&priv->pci_dev->dev,
4423 				       sizeof(struct ipw2100_data_header), &p,
4424 				       GFP_KERNEL);
4425 		if (!v) {
4426 			printk(KERN_ERR DRV_NAME
4427 			       ": %s: PCI alloc failed for tx " "buffers.\n",
4428 			       priv->net_dev->name);
4429 			err = -ENOMEM;
4430 			break;
4431 		}
4432 
4433 		priv->tx_buffers[i].type = DATA;
4434 		priv->tx_buffers[i].info.d_struct.data =
4435 		    (struct ipw2100_data_header *)v;
4436 		priv->tx_buffers[i].info.d_struct.data_phys = p;
4437 		priv->tx_buffers[i].info.d_struct.txb = NULL;
4438 	}
4439 
4440 	if (i == TX_PENDED_QUEUE_LENGTH)
4441 		return 0;
4442 
4443 	for (j = 0; j < i; j++) {
4444 		dma_free_coherent(&priv->pci_dev->dev,
4445 				  sizeof(struct ipw2100_data_header),
4446 				  priv->tx_buffers[j].info.d_struct.data,
4447 				  priv->tx_buffers[j].info.d_struct.data_phys);
4448 	}
4449 
4450 	kfree(priv->tx_buffers);
4451 	priv->tx_buffers = NULL;
4452 
4453 	return err;
4454 }
4455 
4456 static void ipw2100_tx_initialize(struct ipw2100_priv *priv)
4457 {
4458 	int i;
4459 
4460 	IPW_DEBUG_INFO("enter\n");
4461 
4462 	/*
4463 	 * reinitialize packet info lists
4464 	 */
4465 	INIT_LIST_HEAD(&priv->fw_pend_list);
4466 	INIT_STAT(&priv->fw_pend_stat);
4467 
4468 	/*
4469 	 * reinitialize lists
4470 	 */
4471 	INIT_LIST_HEAD(&priv->tx_pend_list);
4472 	INIT_LIST_HEAD(&priv->tx_free_list);
4473 	INIT_STAT(&priv->tx_pend_stat);
4474 	INIT_STAT(&priv->tx_free_stat);
4475 
4476 	for (i = 0; i < TX_PENDED_QUEUE_LENGTH; i++) {
4477 		/* We simply drop any SKBs that have been queued for
4478 		 * transmit */
4479 		if (priv->tx_buffers[i].info.d_struct.txb) {
4480 			libipw_txb_free(priv->tx_buffers[i].info.d_struct.
4481 					   txb);
4482 			priv->tx_buffers[i].info.d_struct.txb = NULL;
4483 		}
4484 
4485 		list_add_tail(&priv->tx_buffers[i].list, &priv->tx_free_list);
4486 	}
4487 
4488 	SET_STAT(&priv->tx_free_stat, i);
4489 
4490 	priv->tx_queue.oldest = 0;
4491 	priv->tx_queue.available = priv->tx_queue.entries;
4492 	priv->tx_queue.next = 0;
4493 	INIT_STAT(&priv->txq_stat);
4494 	SET_STAT(&priv->txq_stat, priv->tx_queue.available);
4495 
4496 	bd_queue_initialize(priv, &priv->tx_queue,
4497 			    IPW_MEM_HOST_SHARED_TX_QUEUE_BD_BASE,
4498 			    IPW_MEM_HOST_SHARED_TX_QUEUE_BD_SIZE,
4499 			    IPW_MEM_HOST_SHARED_TX_QUEUE_READ_INDEX,
4500 			    IPW_MEM_HOST_SHARED_TX_QUEUE_WRITE_INDEX);
4501 
4502 	IPW_DEBUG_INFO("exit\n");
4503 
4504 }
4505 
4506 static void ipw2100_tx_free(struct ipw2100_priv *priv)
4507 {
4508 	int i;
4509 
4510 	IPW_DEBUG_INFO("enter\n");
4511 
4512 	bd_queue_free(priv, &priv->tx_queue);
4513 
4514 	if (!priv->tx_buffers)
4515 		return;
4516 
4517 	for (i = 0; i < TX_PENDED_QUEUE_LENGTH; i++) {
4518 		if (priv->tx_buffers[i].info.d_struct.txb) {
4519 			libipw_txb_free(priv->tx_buffers[i].info.d_struct.
4520 					   txb);
4521 			priv->tx_buffers[i].info.d_struct.txb = NULL;
4522 		}
4523 		if (priv->tx_buffers[i].info.d_struct.data)
4524 			dma_free_coherent(&priv->pci_dev->dev,
4525 					  sizeof(struct ipw2100_data_header),
4526 					  priv->tx_buffers[i].info.d_struct.data,
4527 					  priv->tx_buffers[i].info.d_struct.data_phys);
4528 	}
4529 
4530 	kfree(priv->tx_buffers);
4531 	priv->tx_buffers = NULL;
4532 
4533 	IPW_DEBUG_INFO("exit\n");
4534 }
4535 
4536 static int ipw2100_rx_allocate(struct ipw2100_priv *priv)
4537 {
4538 	int i, j, err = -EINVAL;
4539 
4540 	IPW_DEBUG_INFO("enter\n");
4541 
4542 	err = bd_queue_allocate(priv, &priv->rx_queue, RX_QUEUE_LENGTH);
4543 	if (err) {
4544 		IPW_DEBUG_INFO("failed bd_queue_allocate\n");
4545 		return err;
4546 	}
4547 
4548 	err = status_queue_allocate(priv, RX_QUEUE_LENGTH);
4549 	if (err) {
4550 		IPW_DEBUG_INFO("failed status_queue_allocate\n");
4551 		bd_queue_free(priv, &priv->rx_queue);
4552 		return err;
4553 	}
4554 
4555 	/*
4556 	 * allocate packets
4557 	 */
4558 	priv->rx_buffers = kmalloc_array(RX_QUEUE_LENGTH,
4559 					 sizeof(struct ipw2100_rx_packet),
4560 					 GFP_KERNEL);
4561 	if (!priv->rx_buffers) {
4562 		IPW_DEBUG_INFO("can't allocate rx packet buffer table\n");
4563 
4564 		bd_queue_free(priv, &priv->rx_queue);
4565 
4566 		status_queue_free(priv);
4567 
4568 		return -ENOMEM;
4569 	}
4570 
4571 	for (i = 0; i < RX_QUEUE_LENGTH; i++) {
4572 		struct ipw2100_rx_packet *packet = &priv->rx_buffers[i];
4573 
4574 		err = ipw2100_alloc_skb(priv, packet);
4575 		if (unlikely(err)) {
4576 			err = -ENOMEM;
4577 			break;
4578 		}
4579 
4580 		/* The BD holds the cache aligned address */
4581 		priv->rx_queue.drv[i].host_addr = packet->dma_addr;
4582 		priv->rx_queue.drv[i].buf_length = IPW_RX_NIC_BUFFER_LENGTH;
4583 		priv->status_queue.drv[i].status_fields = 0;
4584 	}
4585 
4586 	if (i == RX_QUEUE_LENGTH)
4587 		return 0;
4588 
4589 	for (j = 0; j < i; j++) {
4590 		dma_unmap_single(&priv->pci_dev->dev,
4591 				 priv->rx_buffers[j].dma_addr,
4592 				 sizeof(struct ipw2100_rx_packet),
4593 				 DMA_FROM_DEVICE);
4594 		dev_kfree_skb(priv->rx_buffers[j].skb);
4595 	}
4596 
4597 	kfree(priv->rx_buffers);
4598 	priv->rx_buffers = NULL;
4599 
4600 	bd_queue_free(priv, &priv->rx_queue);
4601 
4602 	status_queue_free(priv);
4603 
4604 	return err;
4605 }
4606 
4607 static void ipw2100_rx_initialize(struct ipw2100_priv *priv)
4608 {
4609 	IPW_DEBUG_INFO("enter\n");
4610 
4611 	priv->rx_queue.oldest = 0;
4612 	priv->rx_queue.available = priv->rx_queue.entries - 1;
4613 	priv->rx_queue.next = priv->rx_queue.entries - 1;
4614 
4615 	INIT_STAT(&priv->rxq_stat);
4616 	SET_STAT(&priv->rxq_stat, priv->rx_queue.available);
4617 
4618 	bd_queue_initialize(priv, &priv->rx_queue,
4619 			    IPW_MEM_HOST_SHARED_RX_BD_BASE,
4620 			    IPW_MEM_HOST_SHARED_RX_BD_SIZE,
4621 			    IPW_MEM_HOST_SHARED_RX_READ_INDEX,
4622 			    IPW_MEM_HOST_SHARED_RX_WRITE_INDEX);
4623 
4624 	/* set up the status queue */
4625 	write_register(priv->net_dev, IPW_MEM_HOST_SHARED_RX_STATUS_BASE,
4626 		       priv->status_queue.nic);
4627 
4628 	IPW_DEBUG_INFO("exit\n");
4629 }
4630 
4631 static void ipw2100_rx_free(struct ipw2100_priv *priv)
4632 {
4633 	int i;
4634 
4635 	IPW_DEBUG_INFO("enter\n");
4636 
4637 	bd_queue_free(priv, &priv->rx_queue);
4638 	status_queue_free(priv);
4639 
4640 	if (!priv->rx_buffers)
4641 		return;
4642 
4643 	for (i = 0; i < RX_QUEUE_LENGTH; i++) {
4644 		if (priv->rx_buffers[i].rxp) {
4645 			dma_unmap_single(&priv->pci_dev->dev,
4646 					 priv->rx_buffers[i].dma_addr,
4647 					 sizeof(struct ipw2100_rx),
4648 					 DMA_FROM_DEVICE);
4649 			dev_kfree_skb(priv->rx_buffers[i].skb);
4650 		}
4651 	}
4652 
4653 	kfree(priv->rx_buffers);
4654 	priv->rx_buffers = NULL;
4655 
4656 	IPW_DEBUG_INFO("exit\n");
4657 }
4658 
4659 static int ipw2100_read_mac_address(struct ipw2100_priv *priv)
4660 {
4661 	u32 length = ETH_ALEN;
4662 	u8 addr[ETH_ALEN];
4663 
4664 	int err;
4665 
4666 	err = ipw2100_get_ordinal(priv, IPW_ORD_STAT_ADAPTER_MAC, addr, &length);
4667 	if (err) {
4668 		IPW_DEBUG_INFO("MAC address read failed\n");
4669 		return -EIO;
4670 	}
4671 
4672 	eth_hw_addr_set(priv->net_dev, addr);
4673 	IPW_DEBUG_INFO("card MAC is %pM\n", priv->net_dev->dev_addr);
4674 
4675 	return 0;
4676 }
4677 
4678 /********************************************************************
4679  *
4680  * Firmware Commands
4681  *
4682  ********************************************************************/
4683 
4684 static int ipw2100_set_mac_address(struct ipw2100_priv *priv, int batch_mode)
4685 {
4686 	struct host_command cmd = {
4687 		.host_command = ADAPTER_ADDRESS,
4688 		.host_command_sequence = 0,
4689 		.host_command_length = ETH_ALEN
4690 	};
4691 	int err;
4692 
4693 	IPW_DEBUG_HC("SET_MAC_ADDRESS\n");
4694 
4695 	IPW_DEBUG_INFO("enter\n");
4696 
4697 	if (priv->config & CFG_CUSTOM_MAC) {
4698 		memcpy(cmd.host_command_parameters, priv->mac_addr, ETH_ALEN);
4699 		eth_hw_addr_set(priv->net_dev, priv->mac_addr);
4700 	} else
4701 		memcpy(cmd.host_command_parameters, priv->net_dev->dev_addr,
4702 		       ETH_ALEN);
4703 
4704 	err = ipw2100_hw_send_command(priv, &cmd);
4705 
4706 	IPW_DEBUG_INFO("exit\n");
4707 	return err;
4708 }
4709 
4710 static int ipw2100_set_port_type(struct ipw2100_priv *priv, u32 port_type,
4711 				 int batch_mode)
4712 {
4713 	struct host_command cmd = {
4714 		.host_command = PORT_TYPE,
4715 		.host_command_sequence = 0,
4716 		.host_command_length = sizeof(u32)
4717 	};
4718 	int err;
4719 
4720 	switch (port_type) {
4721 	case IW_MODE_INFRA:
4722 		cmd.host_command_parameters[0] = IPW_BSS;
4723 		break;
4724 	case IW_MODE_ADHOC:
4725 		cmd.host_command_parameters[0] = IPW_IBSS;
4726 		break;
4727 	}
4728 
4729 	IPW_DEBUG_HC("PORT_TYPE: %s\n",
4730 		     port_type == IPW_IBSS ? "Ad-Hoc" : "Managed");
4731 
4732 	if (!batch_mode) {
4733 		err = ipw2100_disable_adapter(priv);
4734 		if (err) {
4735 			printk(KERN_ERR DRV_NAME
4736 			       ": %s: Could not disable adapter %d\n",
4737 			       priv->net_dev->name, err);
4738 			return err;
4739 		}
4740 	}
4741 
4742 	/* send cmd to firmware */
4743 	err = ipw2100_hw_send_command(priv, &cmd);
4744 
4745 	if (!batch_mode)
4746 		ipw2100_enable_adapter(priv);
4747 
4748 	return err;
4749 }
4750 
4751 static int ipw2100_set_channel(struct ipw2100_priv *priv, u32 channel,
4752 			       int batch_mode)
4753 {
4754 	struct host_command cmd = {
4755 		.host_command = CHANNEL,
4756 		.host_command_sequence = 0,
4757 		.host_command_length = sizeof(u32)
4758 	};
4759 	int err;
4760 
4761 	cmd.host_command_parameters[0] = channel;
4762 
4763 	IPW_DEBUG_HC("CHANNEL: %d\n", channel);
4764 
4765 	/* If BSS then we don't support channel selection */
4766 	if (priv->ieee->iw_mode == IW_MODE_INFRA)
4767 		return 0;
4768 
4769 	if ((channel != 0) &&
4770 	    ((channel < REG_MIN_CHANNEL) || (channel > REG_MAX_CHANNEL)))
4771 		return -EINVAL;
4772 
4773 	if (!batch_mode) {
4774 		err = ipw2100_disable_adapter(priv);
4775 		if (err)
4776 			return err;
4777 	}
4778 
4779 	err = ipw2100_hw_send_command(priv, &cmd);
4780 	if (err) {
4781 		IPW_DEBUG_INFO("Failed to set channel to %d", channel);
4782 		return err;
4783 	}
4784 
4785 	if (channel)
4786 		priv->config |= CFG_STATIC_CHANNEL;
4787 	else
4788 		priv->config &= ~CFG_STATIC_CHANNEL;
4789 
4790 	priv->channel = channel;
4791 
4792 	if (!batch_mode) {
4793 		err = ipw2100_enable_adapter(priv);
4794 		if (err)
4795 			return err;
4796 	}
4797 
4798 	return 0;
4799 }
4800 
4801 static int ipw2100_system_config(struct ipw2100_priv *priv, int batch_mode)
4802 {
4803 	struct host_command cmd = {
4804 		.host_command = SYSTEM_CONFIG,
4805 		.host_command_sequence = 0,
4806 		.host_command_length = 12,
4807 	};
4808 	u32 ibss_mask, len = sizeof(u32);
4809 	int err;
4810 
4811 	/* Set system configuration */
4812 
4813 	if (!batch_mode) {
4814 		err = ipw2100_disable_adapter(priv);
4815 		if (err)
4816 			return err;
4817 	}
4818 
4819 	if (priv->ieee->iw_mode == IW_MODE_ADHOC)
4820 		cmd.host_command_parameters[0] |= IPW_CFG_IBSS_AUTO_START;
4821 
4822 	cmd.host_command_parameters[0] |= IPW_CFG_IBSS_MASK |
4823 	    IPW_CFG_BSS_MASK | IPW_CFG_802_1x_ENABLE;
4824 
4825 	if (!(priv->config & CFG_LONG_PREAMBLE))
4826 		cmd.host_command_parameters[0] |= IPW_CFG_PREAMBLE_AUTO;
4827 
4828 	err = ipw2100_get_ordinal(priv,
4829 				  IPW_ORD_EEPROM_IBSS_11B_CHANNELS,
4830 				  &ibss_mask, &len);
4831 	if (err)
4832 		ibss_mask = IPW_IBSS_11B_DEFAULT_MASK;
4833 
4834 	cmd.host_command_parameters[1] = REG_CHANNEL_MASK;
4835 	cmd.host_command_parameters[2] = REG_CHANNEL_MASK & ibss_mask;
4836 
4837 	/* 11b only */
4838 	/*cmd.host_command_parameters[0] |= DIVERSITY_ANTENNA_A; */
4839 
4840 	err = ipw2100_hw_send_command(priv, &cmd);
4841 	if (err)
4842 		return err;
4843 
4844 /* If IPv6 is configured in the kernel then we don't want to filter out all
4845  * of the multicast packets as IPv6 needs some. */
4846 #if !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE)
4847 	cmd.host_command = ADD_MULTICAST;
4848 	cmd.host_command_sequence = 0;
4849 	cmd.host_command_length = 0;
4850 
4851 	ipw2100_hw_send_command(priv, &cmd);
4852 #endif
4853 	if (!batch_mode) {
4854 		err = ipw2100_enable_adapter(priv);
4855 		if (err)
4856 			return err;
4857 	}
4858 
4859 	return 0;
4860 }
4861 
4862 static int ipw2100_set_tx_rates(struct ipw2100_priv *priv, u32 rate,
4863 				int batch_mode)
4864 {
4865 	struct host_command cmd = {
4866 		.host_command = BASIC_TX_RATES,
4867 		.host_command_sequence = 0,
4868 		.host_command_length = 4
4869 	};
4870 	int err;
4871 
4872 	cmd.host_command_parameters[0] = rate & TX_RATE_MASK;
4873 
4874 	if (!batch_mode) {
4875 		err = ipw2100_disable_adapter(priv);
4876 		if (err)
4877 			return err;
4878 	}
4879 
4880 	/* Set BASIC TX Rate first */
4881 	ipw2100_hw_send_command(priv, &cmd);
4882 
4883 	/* Set TX Rate */
4884 	cmd.host_command = TX_RATES;
4885 	ipw2100_hw_send_command(priv, &cmd);
4886 
4887 	/* Set MSDU TX Rate */
4888 	cmd.host_command = MSDU_TX_RATES;
4889 	ipw2100_hw_send_command(priv, &cmd);
4890 
4891 	if (!batch_mode) {
4892 		err = ipw2100_enable_adapter(priv);
4893 		if (err)
4894 			return err;
4895 	}
4896 
4897 	priv->tx_rates = rate;
4898 
4899 	return 0;
4900 }
4901 
4902 static int ipw2100_set_power_mode(struct ipw2100_priv *priv, int power_level)
4903 {
4904 	struct host_command cmd = {
4905 		.host_command = POWER_MODE,
4906 		.host_command_sequence = 0,
4907 		.host_command_length = 4
4908 	};
4909 	int err;
4910 
4911 	cmd.host_command_parameters[0] = power_level;
4912 
4913 	err = ipw2100_hw_send_command(priv, &cmd);
4914 	if (err)
4915 		return err;
4916 
4917 	if (power_level == IPW_POWER_MODE_CAM)
4918 		priv->power_mode = IPW_POWER_LEVEL(priv->power_mode);
4919 	else
4920 		priv->power_mode = IPW_POWER_ENABLED | power_level;
4921 
4922 #ifdef IPW2100_TX_POWER
4923 	if (priv->port_type == IBSS && priv->adhoc_power != DFTL_IBSS_TX_POWER) {
4924 		/* Set beacon interval */
4925 		cmd.host_command = TX_POWER_INDEX;
4926 		cmd.host_command_parameters[0] = (u32) priv->adhoc_power;
4927 
4928 		err = ipw2100_hw_send_command(priv, &cmd);
4929 		if (err)
4930 			return err;
4931 	}
4932 #endif
4933 
4934 	return 0;
4935 }
4936 
4937 static int ipw2100_set_rts_threshold(struct ipw2100_priv *priv, u32 threshold)
4938 {
4939 	struct host_command cmd = {
4940 		.host_command = RTS_THRESHOLD,
4941 		.host_command_sequence = 0,
4942 		.host_command_length = 4
4943 	};
4944 	int err;
4945 
4946 	if (threshold & RTS_DISABLED)
4947 		cmd.host_command_parameters[0] = MAX_RTS_THRESHOLD;
4948 	else
4949 		cmd.host_command_parameters[0] = threshold & ~RTS_DISABLED;
4950 
4951 	err = ipw2100_hw_send_command(priv, &cmd);
4952 	if (err)
4953 		return err;
4954 
4955 	priv->rts_threshold = threshold;
4956 
4957 	return 0;
4958 }
4959 
4960 #if 0
4961 int ipw2100_set_fragmentation_threshold(struct ipw2100_priv *priv,
4962 					u32 threshold, int batch_mode)
4963 {
4964 	struct host_command cmd = {
4965 		.host_command = FRAG_THRESHOLD,
4966 		.host_command_sequence = 0,
4967 		.host_command_length = 4,
4968 		.host_command_parameters[0] = 0,
4969 	};
4970 	int err;
4971 
4972 	if (!batch_mode) {
4973 		err = ipw2100_disable_adapter(priv);
4974 		if (err)
4975 			return err;
4976 	}
4977 
4978 	if (threshold == 0)
4979 		threshold = DEFAULT_FRAG_THRESHOLD;
4980 	else {
4981 		threshold = max(threshold, MIN_FRAG_THRESHOLD);
4982 		threshold = min(threshold, MAX_FRAG_THRESHOLD);
4983 	}
4984 
4985 	cmd.host_command_parameters[0] = threshold;
4986 
4987 	IPW_DEBUG_HC("FRAG_THRESHOLD: %u\n", threshold);
4988 
4989 	err = ipw2100_hw_send_command(priv, &cmd);
4990 
4991 	if (!batch_mode)
4992 		ipw2100_enable_adapter(priv);
4993 
4994 	if (!err)
4995 		priv->frag_threshold = threshold;
4996 
4997 	return err;
4998 }
4999 #endif
5000 
5001 static int ipw2100_set_short_retry(struct ipw2100_priv *priv, u32 retry)
5002 {
5003 	struct host_command cmd = {
5004 		.host_command = SHORT_RETRY_LIMIT,
5005 		.host_command_sequence = 0,
5006 		.host_command_length = 4
5007 	};
5008 	int err;
5009 
5010 	cmd.host_command_parameters[0] = retry;
5011 
5012 	err = ipw2100_hw_send_command(priv, &cmd);
5013 	if (err)
5014 		return err;
5015 
5016 	priv->short_retry_limit = retry;
5017 
5018 	return 0;
5019 }
5020 
5021 static int ipw2100_set_long_retry(struct ipw2100_priv *priv, u32 retry)
5022 {
5023 	struct host_command cmd = {
5024 		.host_command = LONG_RETRY_LIMIT,
5025 		.host_command_sequence = 0,
5026 		.host_command_length = 4
5027 	};
5028 	int err;
5029 
5030 	cmd.host_command_parameters[0] = retry;
5031 
5032 	err = ipw2100_hw_send_command(priv, &cmd);
5033 	if (err)
5034 		return err;
5035 
5036 	priv->long_retry_limit = retry;
5037 
5038 	return 0;
5039 }
5040 
5041 static int ipw2100_set_mandatory_bssid(struct ipw2100_priv *priv, u8 * bssid,
5042 				       int batch_mode)
5043 {
5044 	struct host_command cmd = {
5045 		.host_command = MANDATORY_BSSID,
5046 		.host_command_sequence = 0,
5047 		.host_command_length = (bssid == NULL) ? 0 : ETH_ALEN
5048 	};
5049 	int err;
5050 
5051 #ifdef CONFIG_IPW2100_DEBUG
5052 	if (bssid != NULL)
5053 		IPW_DEBUG_HC("MANDATORY_BSSID: %pM\n", bssid);
5054 	else
5055 		IPW_DEBUG_HC("MANDATORY_BSSID: <clear>\n");
5056 #endif
5057 	/* if BSSID is empty then we disable mandatory bssid mode */
5058 	if (bssid != NULL)
5059 		memcpy(cmd.host_command_parameters, bssid, ETH_ALEN);
5060 
5061 	if (!batch_mode) {
5062 		err = ipw2100_disable_adapter(priv);
5063 		if (err)
5064 			return err;
5065 	}
5066 
5067 	err = ipw2100_hw_send_command(priv, &cmd);
5068 
5069 	if (!batch_mode)
5070 		ipw2100_enable_adapter(priv);
5071 
5072 	return err;
5073 }
5074 
5075 static int ipw2100_disassociate_bssid(struct ipw2100_priv *priv)
5076 {
5077 	struct host_command cmd = {
5078 		.host_command = DISASSOCIATION_BSSID,
5079 		.host_command_sequence = 0,
5080 		.host_command_length = ETH_ALEN
5081 	};
5082 	int err;
5083 
5084 	IPW_DEBUG_HC("DISASSOCIATION_BSSID\n");
5085 
5086 	/* The Firmware currently ignores the BSSID and just disassociates from
5087 	 * the currently associated AP -- but in the off chance that a future
5088 	 * firmware does use the BSSID provided here, we go ahead and try and
5089 	 * set it to the currently associated AP's BSSID */
5090 	memcpy(cmd.host_command_parameters, priv->bssid, ETH_ALEN);
5091 
5092 	err = ipw2100_hw_send_command(priv, &cmd);
5093 
5094 	return err;
5095 }
5096 
5097 static int ipw2100_set_wpa_ie(struct ipw2100_priv *,
5098 			      struct ipw2100_wpa_assoc_frame *, int)
5099     __attribute__ ((unused));
5100 
5101 static int ipw2100_set_wpa_ie(struct ipw2100_priv *priv,
5102 			      struct ipw2100_wpa_assoc_frame *wpa_frame,
5103 			      int batch_mode)
5104 {
5105 	struct host_command cmd = {
5106 		.host_command = SET_WPA_IE,
5107 		.host_command_sequence = 0,
5108 		.host_command_length = sizeof(struct ipw2100_wpa_assoc_frame),
5109 	};
5110 	int err;
5111 
5112 	IPW_DEBUG_HC("SET_WPA_IE\n");
5113 
5114 	if (!batch_mode) {
5115 		err = ipw2100_disable_adapter(priv);
5116 		if (err)
5117 			return err;
5118 	}
5119 
5120 	memcpy(cmd.host_command_parameters, wpa_frame,
5121 	       sizeof(struct ipw2100_wpa_assoc_frame));
5122 
5123 	err = ipw2100_hw_send_command(priv, &cmd);
5124 
5125 	if (!batch_mode) {
5126 		if (ipw2100_enable_adapter(priv))
5127 			err = -EIO;
5128 	}
5129 
5130 	return err;
5131 }
5132 
5133 struct security_info_params {
5134 	u32 allowed_ciphers;
5135 	u16 version;
5136 	u8 auth_mode;
5137 	u8 replay_counters_number;
5138 	u8 unicast_using_group;
5139 } __packed;
5140 
5141 static int ipw2100_set_security_information(struct ipw2100_priv *priv,
5142 					    int auth_mode,
5143 					    int security_level,
5144 					    int unicast_using_group,
5145 					    int batch_mode)
5146 {
5147 	struct host_command cmd = {
5148 		.host_command = SET_SECURITY_INFORMATION,
5149 		.host_command_sequence = 0,
5150 		.host_command_length = sizeof(struct security_info_params)
5151 	};
5152 	struct security_info_params *security =
5153 	    (struct security_info_params *)&cmd.host_command_parameters;
5154 	int err;
5155 	memset(security, 0, sizeof(*security));
5156 
5157 	/* If shared key AP authentication is turned on, then we need to
5158 	 * configure the firmware to try and use it.
5159 	 *
5160 	 * Actual data encryption/decryption is handled by the host. */
5161 	security->auth_mode = auth_mode;
5162 	security->unicast_using_group = unicast_using_group;
5163 
5164 	switch (security_level) {
5165 	default:
5166 	case SEC_LEVEL_0:
5167 		security->allowed_ciphers = IPW_NONE_CIPHER;
5168 		break;
5169 	case SEC_LEVEL_1:
5170 		security->allowed_ciphers = IPW_WEP40_CIPHER |
5171 		    IPW_WEP104_CIPHER;
5172 		break;
5173 	case SEC_LEVEL_2:
5174 		security->allowed_ciphers = IPW_WEP40_CIPHER |
5175 		    IPW_WEP104_CIPHER | IPW_TKIP_CIPHER;
5176 		break;
5177 	case SEC_LEVEL_2_CKIP:
5178 		security->allowed_ciphers = IPW_WEP40_CIPHER |
5179 		    IPW_WEP104_CIPHER | IPW_CKIP_CIPHER;
5180 		break;
5181 	case SEC_LEVEL_3:
5182 		security->allowed_ciphers = IPW_WEP40_CIPHER |
5183 		    IPW_WEP104_CIPHER | IPW_TKIP_CIPHER | IPW_CCMP_CIPHER;
5184 		break;
5185 	}
5186 
5187 	IPW_DEBUG_HC
5188 	    ("SET_SECURITY_INFORMATION: auth:%d cipher:0x%02X (level %d)\n",
5189 	     security->auth_mode, security->allowed_ciphers, security_level);
5190 
5191 	security->replay_counters_number = 0;
5192 
5193 	if (!batch_mode) {
5194 		err = ipw2100_disable_adapter(priv);
5195 		if (err)
5196 			return err;
5197 	}
5198 
5199 	err = ipw2100_hw_send_command(priv, &cmd);
5200 
5201 	if (!batch_mode)
5202 		ipw2100_enable_adapter(priv);
5203 
5204 	return err;
5205 }
5206 
5207 static int ipw2100_set_tx_power(struct ipw2100_priv *priv, u32 tx_power)
5208 {
5209 	struct host_command cmd = {
5210 		.host_command = TX_POWER_INDEX,
5211 		.host_command_sequence = 0,
5212 		.host_command_length = 4
5213 	};
5214 	int err = 0;
5215 	u32 tmp = tx_power;
5216 
5217 	if (tx_power != IPW_TX_POWER_DEFAULT)
5218 		tmp = (tx_power - IPW_TX_POWER_MIN_DBM) * 16 /
5219 		      (IPW_TX_POWER_MAX_DBM - IPW_TX_POWER_MIN_DBM);
5220 
5221 	cmd.host_command_parameters[0] = tmp;
5222 
5223 	if (priv->ieee->iw_mode == IW_MODE_ADHOC)
5224 		err = ipw2100_hw_send_command(priv, &cmd);
5225 	if (!err)
5226 		priv->tx_power = tx_power;
5227 
5228 	return 0;
5229 }
5230 
5231 static int ipw2100_set_ibss_beacon_interval(struct ipw2100_priv *priv,
5232 					    u32 interval, int batch_mode)
5233 {
5234 	struct host_command cmd = {
5235 		.host_command = BEACON_INTERVAL,
5236 		.host_command_sequence = 0,
5237 		.host_command_length = 4
5238 	};
5239 	int err;
5240 
5241 	cmd.host_command_parameters[0] = interval;
5242 
5243 	IPW_DEBUG_INFO("enter\n");
5244 
5245 	if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
5246 		if (!batch_mode) {
5247 			err = ipw2100_disable_adapter(priv);
5248 			if (err)
5249 				return err;
5250 		}
5251 
5252 		ipw2100_hw_send_command(priv, &cmd);
5253 
5254 		if (!batch_mode) {
5255 			err = ipw2100_enable_adapter(priv);
5256 			if (err)
5257 				return err;
5258 		}
5259 	}
5260 
5261 	IPW_DEBUG_INFO("exit\n");
5262 
5263 	return 0;
5264 }
5265 
5266 static void ipw2100_queues_initialize(struct ipw2100_priv *priv)
5267 {
5268 	ipw2100_tx_initialize(priv);
5269 	ipw2100_rx_initialize(priv);
5270 	ipw2100_msg_initialize(priv);
5271 }
5272 
5273 static void ipw2100_queues_free(struct ipw2100_priv *priv)
5274 {
5275 	ipw2100_tx_free(priv);
5276 	ipw2100_rx_free(priv);
5277 	ipw2100_msg_free(priv);
5278 }
5279 
5280 static int ipw2100_queues_allocate(struct ipw2100_priv *priv)
5281 {
5282 	if (ipw2100_tx_allocate(priv) ||
5283 	    ipw2100_rx_allocate(priv) || ipw2100_msg_allocate(priv))
5284 		goto fail;
5285 
5286 	return 0;
5287 
5288       fail:
5289 	ipw2100_tx_free(priv);
5290 	ipw2100_rx_free(priv);
5291 	ipw2100_msg_free(priv);
5292 	return -ENOMEM;
5293 }
5294 
5295 #define IPW_PRIVACY_CAPABLE 0x0008
5296 
5297 static int ipw2100_set_wep_flags(struct ipw2100_priv *priv, u32 flags,
5298 				 int batch_mode)
5299 {
5300 	struct host_command cmd = {
5301 		.host_command = WEP_FLAGS,
5302 		.host_command_sequence = 0,
5303 		.host_command_length = 4
5304 	};
5305 	int err;
5306 
5307 	cmd.host_command_parameters[0] = flags;
5308 
5309 	IPW_DEBUG_HC("WEP_FLAGS: flags = 0x%08X\n", flags);
5310 
5311 	if (!batch_mode) {
5312 		err = ipw2100_disable_adapter(priv);
5313 		if (err) {
5314 			printk(KERN_ERR DRV_NAME
5315 			       ": %s: Could not disable adapter %d\n",
5316 			       priv->net_dev->name, err);
5317 			return err;
5318 		}
5319 	}
5320 
5321 	/* send cmd to firmware */
5322 	err = ipw2100_hw_send_command(priv, &cmd);
5323 
5324 	if (!batch_mode)
5325 		ipw2100_enable_adapter(priv);
5326 
5327 	return err;
5328 }
5329 
5330 struct ipw2100_wep_key {
5331 	u8 idx;
5332 	u8 len;
5333 	u8 key[13];
5334 };
5335 
5336 /* Macros to ease up priting WEP keys */
5337 #define WEP_FMT_64  "%02X%02X%02X%02X-%02X"
5338 #define WEP_FMT_128 "%02X%02X%02X%02X-%02X%02X%02X%02X-%02X%02X%02X"
5339 #define WEP_STR_64(x) x[0],x[1],x[2],x[3],x[4]
5340 #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]
5341 
5342 /**
5343  * ipw2100_set_key() - Set a the wep key
5344  *
5345  * @priv: struct to work on
5346  * @idx: index of the key we want to set
5347  * @key: ptr to the key data to set
5348  * @len: length of the buffer at @key
5349  * @batch_mode: FIXME perform the operation in batch mode, not
5350  *              disabling the device.
5351  *
5352  * @returns 0 if OK, < 0 errno code on error.
5353  *
5354  * Fill out a command structure with the new wep key, length an
5355  * index and send it down the wire.
5356  */
5357 static int ipw2100_set_key(struct ipw2100_priv *priv,
5358 			   int idx, char *key, int len, int batch_mode)
5359 {
5360 	int keylen = len ? (len <= 5 ? 5 : 13) : 0;
5361 	struct host_command cmd = {
5362 		.host_command = WEP_KEY_INFO,
5363 		.host_command_sequence = 0,
5364 		.host_command_length = sizeof(struct ipw2100_wep_key),
5365 	};
5366 	struct ipw2100_wep_key *wep_key = (void *)cmd.host_command_parameters;
5367 	int err;
5368 
5369 	IPW_DEBUG_HC("WEP_KEY_INFO: index = %d, len = %d/%d\n",
5370 		     idx, keylen, len);
5371 
5372 	/* NOTE: We don't check cached values in case the firmware was reset
5373 	 * or some other problem is occurring.  If the user is setting the key,
5374 	 * then we push the change */
5375 
5376 	wep_key->idx = idx;
5377 	wep_key->len = keylen;
5378 
5379 	if (keylen) {
5380 		memcpy(wep_key->key, key, len);
5381 		memset(wep_key->key + len, 0, keylen - len);
5382 	}
5383 
5384 	/* Will be optimized out on debug not being configured in */
5385 	if (keylen == 0)
5386 		IPW_DEBUG_WEP("%s: Clearing key %d\n",
5387 			      priv->net_dev->name, wep_key->idx);
5388 	else if (keylen == 5)
5389 		IPW_DEBUG_WEP("%s: idx: %d, len: %d key: " WEP_FMT_64 "\n",
5390 			      priv->net_dev->name, wep_key->idx, wep_key->len,
5391 			      WEP_STR_64(wep_key->key));
5392 	else
5393 		IPW_DEBUG_WEP("%s: idx: %d, len: %d key: " WEP_FMT_128
5394 			      "\n",
5395 			      priv->net_dev->name, wep_key->idx, wep_key->len,
5396 			      WEP_STR_128(wep_key->key));
5397 
5398 	if (!batch_mode) {
5399 		err = ipw2100_disable_adapter(priv);
5400 		/* FIXME: IPG: shouldn't this prink be in _disable_adapter()? */
5401 		if (err) {
5402 			printk(KERN_ERR DRV_NAME
5403 			       ": %s: Could not disable adapter %d\n",
5404 			       priv->net_dev->name, err);
5405 			return err;
5406 		}
5407 	}
5408 
5409 	/* send cmd to firmware */
5410 	err = ipw2100_hw_send_command(priv, &cmd);
5411 
5412 	if (!batch_mode) {
5413 		int err2 = ipw2100_enable_adapter(priv);
5414 		if (err == 0)
5415 			err = err2;
5416 	}
5417 	return err;
5418 }
5419 
5420 static int ipw2100_set_key_index(struct ipw2100_priv *priv,
5421 				 int idx, int batch_mode)
5422 {
5423 	struct host_command cmd = {
5424 		.host_command = WEP_KEY_INDEX,
5425 		.host_command_sequence = 0,
5426 		.host_command_length = 4,
5427 		.host_command_parameters = {idx},
5428 	};
5429 	int err;
5430 
5431 	IPW_DEBUG_HC("WEP_KEY_INDEX: index = %d\n", idx);
5432 
5433 	if (idx < 0 || idx > 3)
5434 		return -EINVAL;
5435 
5436 	if (!batch_mode) {
5437 		err = ipw2100_disable_adapter(priv);
5438 		if (err) {
5439 			printk(KERN_ERR DRV_NAME
5440 			       ": %s: Could not disable adapter %d\n",
5441 			       priv->net_dev->name, err);
5442 			return err;
5443 		}
5444 	}
5445 
5446 	/* send cmd to firmware */
5447 	err = ipw2100_hw_send_command(priv, &cmd);
5448 
5449 	if (!batch_mode)
5450 		ipw2100_enable_adapter(priv);
5451 
5452 	return err;
5453 }
5454 
5455 static int ipw2100_configure_security(struct ipw2100_priv *priv, int batch_mode)
5456 {
5457 	int i, err, auth_mode, sec_level, use_group;
5458 
5459 	if (!(priv->status & STATUS_RUNNING))
5460 		return 0;
5461 
5462 	if (!batch_mode) {
5463 		err = ipw2100_disable_adapter(priv);
5464 		if (err)
5465 			return err;
5466 	}
5467 
5468 	if (!priv->ieee->sec.enabled) {
5469 		err =
5470 		    ipw2100_set_security_information(priv, IPW_AUTH_OPEN,
5471 						     SEC_LEVEL_0, 0, 1);
5472 	} else {
5473 		auth_mode = IPW_AUTH_OPEN;
5474 		if (priv->ieee->sec.flags & SEC_AUTH_MODE) {
5475 			if (priv->ieee->sec.auth_mode == WLAN_AUTH_SHARED_KEY)
5476 				auth_mode = IPW_AUTH_SHARED;
5477 			else if (priv->ieee->sec.auth_mode == WLAN_AUTH_LEAP)
5478 				auth_mode = IPW_AUTH_LEAP_CISCO_ID;
5479 		}
5480 
5481 		sec_level = SEC_LEVEL_0;
5482 		if (priv->ieee->sec.flags & SEC_LEVEL)
5483 			sec_level = priv->ieee->sec.level;
5484 
5485 		use_group = 0;
5486 		if (priv->ieee->sec.flags & SEC_UNICAST_GROUP)
5487 			use_group = priv->ieee->sec.unicast_uses_group;
5488 
5489 		err =
5490 		    ipw2100_set_security_information(priv, auth_mode, sec_level,
5491 						     use_group, 1);
5492 	}
5493 
5494 	if (err)
5495 		goto exit;
5496 
5497 	if (priv->ieee->sec.enabled) {
5498 		for (i = 0; i < 4; i++) {
5499 			if (!(priv->ieee->sec.flags & (1 << i))) {
5500 				memset(priv->ieee->sec.keys[i], 0, WEP_KEY_LEN);
5501 				priv->ieee->sec.key_sizes[i] = 0;
5502 			} else {
5503 				err = ipw2100_set_key(priv, i,
5504 						      priv->ieee->sec.keys[i],
5505 						      priv->ieee->sec.
5506 						      key_sizes[i], 1);
5507 				if (err)
5508 					goto exit;
5509 			}
5510 		}
5511 
5512 		ipw2100_set_key_index(priv, priv->ieee->crypt_info.tx_keyidx, 1);
5513 	}
5514 
5515 	/* Always enable privacy so the Host can filter WEP packets if
5516 	 * encrypted data is sent up */
5517 	err =
5518 	    ipw2100_set_wep_flags(priv,
5519 				  priv->ieee->sec.
5520 				  enabled ? IPW_PRIVACY_CAPABLE : 0, 1);
5521 	if (err)
5522 		goto exit;
5523 
5524 	priv->status &= ~STATUS_SECURITY_UPDATED;
5525 
5526       exit:
5527 	if (!batch_mode)
5528 		ipw2100_enable_adapter(priv);
5529 
5530 	return err;
5531 }
5532 
5533 static void ipw2100_security_work(struct work_struct *work)
5534 {
5535 	struct ipw2100_priv *priv =
5536 		container_of(work, struct ipw2100_priv, security_work.work);
5537 
5538 	/* If we happen to have reconnected before we get a chance to
5539 	 * process this, then update the security settings--which causes
5540 	 * a disassociation to occur */
5541 	if (!(priv->status & STATUS_ASSOCIATED) &&
5542 	    priv->status & STATUS_SECURITY_UPDATED)
5543 		ipw2100_configure_security(priv, 0);
5544 }
5545 
5546 static void shim__set_security(struct net_device *dev,
5547 			       struct libipw_security *sec)
5548 {
5549 	struct ipw2100_priv *priv = libipw_priv(dev);
5550 	int i;
5551 
5552 	mutex_lock(&priv->action_mutex);
5553 	if (!(priv->status & STATUS_INITIALIZED))
5554 		goto done;
5555 
5556 	for (i = 0; i < 4; i++) {
5557 		if (sec->flags & (1 << i)) {
5558 			priv->ieee->sec.key_sizes[i] = sec->key_sizes[i];
5559 			if (sec->key_sizes[i] == 0)
5560 				priv->ieee->sec.flags &= ~(1 << i);
5561 			else
5562 				memcpy(priv->ieee->sec.keys[i], sec->keys[i],
5563 				       sec->key_sizes[i]);
5564 			if (sec->level == SEC_LEVEL_1) {
5565 				priv->ieee->sec.flags |= (1 << i);
5566 				priv->status |= STATUS_SECURITY_UPDATED;
5567 			} else
5568 				priv->ieee->sec.flags &= ~(1 << i);
5569 		}
5570 	}
5571 
5572 	if ((sec->flags & SEC_ACTIVE_KEY) &&
5573 	    priv->ieee->sec.active_key != sec->active_key) {
5574 		priv->ieee->sec.active_key = sec->active_key;
5575 		priv->ieee->sec.flags |= SEC_ACTIVE_KEY;
5576 		priv->status |= STATUS_SECURITY_UPDATED;
5577 	}
5578 
5579 	if ((sec->flags & SEC_AUTH_MODE) &&
5580 	    (priv->ieee->sec.auth_mode != sec->auth_mode)) {
5581 		priv->ieee->sec.auth_mode = sec->auth_mode;
5582 		priv->ieee->sec.flags |= SEC_AUTH_MODE;
5583 		priv->status |= STATUS_SECURITY_UPDATED;
5584 	}
5585 
5586 	if (sec->flags & SEC_ENABLED && priv->ieee->sec.enabled != sec->enabled) {
5587 		priv->ieee->sec.flags |= SEC_ENABLED;
5588 		priv->ieee->sec.enabled = sec->enabled;
5589 		priv->status |= STATUS_SECURITY_UPDATED;
5590 	}
5591 
5592 	if (sec->flags & SEC_ENCRYPT)
5593 		priv->ieee->sec.encrypt = sec->encrypt;
5594 
5595 	if (sec->flags & SEC_LEVEL && priv->ieee->sec.level != sec->level) {
5596 		priv->ieee->sec.level = sec->level;
5597 		priv->ieee->sec.flags |= SEC_LEVEL;
5598 		priv->status |= STATUS_SECURITY_UPDATED;
5599 	}
5600 
5601 	IPW_DEBUG_WEP("Security flags: %c %c%c%c%c %c%c%c%c\n",
5602 		      priv->ieee->sec.flags & (1 << 8) ? '1' : '0',
5603 		      priv->ieee->sec.flags & (1 << 7) ? '1' : '0',
5604 		      priv->ieee->sec.flags & (1 << 6) ? '1' : '0',
5605 		      priv->ieee->sec.flags & (1 << 5) ? '1' : '0',
5606 		      priv->ieee->sec.flags & (1 << 4) ? '1' : '0',
5607 		      priv->ieee->sec.flags & (1 << 3) ? '1' : '0',
5608 		      priv->ieee->sec.flags & (1 << 2) ? '1' : '0',
5609 		      priv->ieee->sec.flags & (1 << 1) ? '1' : '0',
5610 		      priv->ieee->sec.flags & (1 << 0) ? '1' : '0');
5611 
5612 /* As a temporary work around to enable WPA until we figure out why
5613  * wpa_supplicant toggles the security capability of the driver, which
5614  * forces a disassociation with force_update...
5615  *
5616  *	if (force_update || !(priv->status & STATUS_ASSOCIATED))*/
5617 	if (!(priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)))
5618 		ipw2100_configure_security(priv, 0);
5619       done:
5620 	mutex_unlock(&priv->action_mutex);
5621 }
5622 
5623 static int ipw2100_adapter_setup(struct ipw2100_priv *priv)
5624 {
5625 	int err;
5626 	int batch_mode = 1;
5627 	u8 *bssid;
5628 
5629 	IPW_DEBUG_INFO("enter\n");
5630 
5631 	err = ipw2100_disable_adapter(priv);
5632 	if (err)
5633 		return err;
5634 #ifdef CONFIG_IPW2100_MONITOR
5635 	if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
5636 		err = ipw2100_set_channel(priv, priv->channel, batch_mode);
5637 		if (err)
5638 			return err;
5639 
5640 		IPW_DEBUG_INFO("exit\n");
5641 
5642 		return 0;
5643 	}
5644 #endif				/* CONFIG_IPW2100_MONITOR */
5645 
5646 	err = ipw2100_read_mac_address(priv);
5647 	if (err)
5648 		return -EIO;
5649 
5650 	err = ipw2100_set_mac_address(priv, batch_mode);
5651 	if (err)
5652 		return err;
5653 
5654 	err = ipw2100_set_port_type(priv, priv->ieee->iw_mode, batch_mode);
5655 	if (err)
5656 		return err;
5657 
5658 	if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
5659 		err = ipw2100_set_channel(priv, priv->channel, batch_mode);
5660 		if (err)
5661 			return err;
5662 	}
5663 
5664 	err = ipw2100_system_config(priv, batch_mode);
5665 	if (err)
5666 		return err;
5667 
5668 	err = ipw2100_set_tx_rates(priv, priv->tx_rates, batch_mode);
5669 	if (err)
5670 		return err;
5671 
5672 	/* Default to power mode OFF */
5673 	err = ipw2100_set_power_mode(priv, IPW_POWER_MODE_CAM);
5674 	if (err)
5675 		return err;
5676 
5677 	err = ipw2100_set_rts_threshold(priv, priv->rts_threshold);
5678 	if (err)
5679 		return err;
5680 
5681 	if (priv->config & CFG_STATIC_BSSID)
5682 		bssid = priv->bssid;
5683 	else
5684 		bssid = NULL;
5685 	err = ipw2100_set_mandatory_bssid(priv, bssid, batch_mode);
5686 	if (err)
5687 		return err;
5688 
5689 	if (priv->config & CFG_STATIC_ESSID)
5690 		err = ipw2100_set_essid(priv, priv->essid, priv->essid_len,
5691 					batch_mode);
5692 	else
5693 		err = ipw2100_set_essid(priv, NULL, 0, batch_mode);
5694 	if (err)
5695 		return err;
5696 
5697 	err = ipw2100_configure_security(priv, batch_mode);
5698 	if (err)
5699 		return err;
5700 
5701 	if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
5702 		err =
5703 		    ipw2100_set_ibss_beacon_interval(priv,
5704 						     priv->beacon_interval,
5705 						     batch_mode);
5706 		if (err)
5707 			return err;
5708 
5709 		err = ipw2100_set_tx_power(priv, priv->tx_power);
5710 		if (err)
5711 			return err;
5712 	}
5713 
5714 	/*
5715 	   err = ipw2100_set_fragmentation_threshold(
5716 	   priv, priv->frag_threshold, batch_mode);
5717 	   if (err)
5718 	   return err;
5719 	 */
5720 
5721 	IPW_DEBUG_INFO("exit\n");
5722 
5723 	return 0;
5724 }
5725 
5726 /*************************************************************************
5727  *
5728  * EXTERNALLY CALLED METHODS
5729  *
5730  *************************************************************************/
5731 
5732 /* This method is called by the network layer -- not to be confused with
5733  * ipw2100_set_mac_address() declared above called by this driver (and this
5734  * method as well) to talk to the firmware */
5735 static int ipw2100_set_address(struct net_device *dev, void *p)
5736 {
5737 	struct ipw2100_priv *priv = libipw_priv(dev);
5738 	struct sockaddr *addr = p;
5739 	int err = 0;
5740 
5741 	if (!is_valid_ether_addr(addr->sa_data))
5742 		return -EADDRNOTAVAIL;
5743 
5744 	mutex_lock(&priv->action_mutex);
5745 
5746 	priv->config |= CFG_CUSTOM_MAC;
5747 	memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
5748 
5749 	err = ipw2100_set_mac_address(priv, 0);
5750 	if (err)
5751 		goto done;
5752 
5753 	priv->reset_backoff = 0;
5754 	mutex_unlock(&priv->action_mutex);
5755 	ipw2100_reset_adapter(&priv->reset_work.work);
5756 	return 0;
5757 
5758       done:
5759 	mutex_unlock(&priv->action_mutex);
5760 	return err;
5761 }
5762 
5763 static int ipw2100_open(struct net_device *dev)
5764 {
5765 	struct ipw2100_priv *priv = libipw_priv(dev);
5766 	unsigned long flags;
5767 	IPW_DEBUG_INFO("dev->open\n");
5768 
5769 	spin_lock_irqsave(&priv->low_lock, flags);
5770 	if (priv->status & STATUS_ASSOCIATED) {
5771 		netif_carrier_on(dev);
5772 		netif_start_queue(dev);
5773 	}
5774 	spin_unlock_irqrestore(&priv->low_lock, flags);
5775 
5776 	return 0;
5777 }
5778 
5779 static int ipw2100_close(struct net_device *dev)
5780 {
5781 	struct ipw2100_priv *priv = libipw_priv(dev);
5782 	unsigned long flags;
5783 	struct list_head *element;
5784 	struct ipw2100_tx_packet *packet;
5785 
5786 	IPW_DEBUG_INFO("enter\n");
5787 
5788 	spin_lock_irqsave(&priv->low_lock, flags);
5789 
5790 	if (priv->status & STATUS_ASSOCIATED)
5791 		netif_carrier_off(dev);
5792 	netif_stop_queue(dev);
5793 
5794 	/* Flush the TX queue ... */
5795 	while (!list_empty(&priv->tx_pend_list)) {
5796 		element = priv->tx_pend_list.next;
5797 		packet = list_entry(element, struct ipw2100_tx_packet, list);
5798 
5799 		list_del(element);
5800 		DEC_STAT(&priv->tx_pend_stat);
5801 
5802 		libipw_txb_free(packet->info.d_struct.txb);
5803 		packet->info.d_struct.txb = NULL;
5804 
5805 		list_add_tail(element, &priv->tx_free_list);
5806 		INC_STAT(&priv->tx_free_stat);
5807 	}
5808 	spin_unlock_irqrestore(&priv->low_lock, flags);
5809 
5810 	IPW_DEBUG_INFO("exit\n");
5811 
5812 	return 0;
5813 }
5814 
5815 /*
5816  * TODO:  Fix this function... its just wrong
5817  */
5818 static void ipw2100_tx_timeout(struct net_device *dev, unsigned int txqueue)
5819 {
5820 	struct ipw2100_priv *priv = libipw_priv(dev);
5821 
5822 	dev->stats.tx_errors++;
5823 
5824 #ifdef CONFIG_IPW2100_MONITOR
5825 	if (priv->ieee->iw_mode == IW_MODE_MONITOR)
5826 		return;
5827 #endif
5828 
5829 	IPW_DEBUG_INFO("%s: TX timed out.  Scheduling firmware restart.\n",
5830 		       dev->name);
5831 	schedule_reset(priv);
5832 }
5833 
5834 static int ipw2100_wpa_enable(struct ipw2100_priv *priv, int value)
5835 {
5836 	/* This is called when wpa_supplicant loads and closes the driver
5837 	 * interface. */
5838 	priv->ieee->wpa_enabled = value;
5839 	return 0;
5840 }
5841 
5842 static int ipw2100_wpa_set_auth_algs(struct ipw2100_priv *priv, int value)
5843 {
5844 
5845 	struct libipw_device *ieee = priv->ieee;
5846 	struct libipw_security sec = {
5847 		.flags = SEC_AUTH_MODE,
5848 	};
5849 	int ret = 0;
5850 
5851 	if (value & IW_AUTH_ALG_SHARED_KEY) {
5852 		sec.auth_mode = WLAN_AUTH_SHARED_KEY;
5853 		ieee->open_wep = 0;
5854 	} else if (value & IW_AUTH_ALG_OPEN_SYSTEM) {
5855 		sec.auth_mode = WLAN_AUTH_OPEN;
5856 		ieee->open_wep = 1;
5857 	} else if (value & IW_AUTH_ALG_LEAP) {
5858 		sec.auth_mode = WLAN_AUTH_LEAP;
5859 		ieee->open_wep = 1;
5860 	} else
5861 		return -EINVAL;
5862 
5863 	if (ieee->set_security)
5864 		ieee->set_security(ieee->dev, &sec);
5865 	else
5866 		ret = -EOPNOTSUPP;
5867 
5868 	return ret;
5869 }
5870 
5871 static void ipw2100_wpa_assoc_frame(struct ipw2100_priv *priv,
5872 				    char *wpa_ie, int wpa_ie_len)
5873 {
5874 
5875 	struct ipw2100_wpa_assoc_frame frame;
5876 
5877 	frame.fixed_ie_mask = 0;
5878 
5879 	/* copy WPA IE */
5880 	memcpy(frame.var_ie, wpa_ie, wpa_ie_len);
5881 	frame.var_ie_len = wpa_ie_len;
5882 
5883 	/* make sure WPA is enabled */
5884 	ipw2100_wpa_enable(priv, 1);
5885 	ipw2100_set_wpa_ie(priv, &frame, 0);
5886 }
5887 
5888 static void ipw_ethtool_get_drvinfo(struct net_device *dev,
5889 				    struct ethtool_drvinfo *info)
5890 {
5891 	struct ipw2100_priv *priv = libipw_priv(dev);
5892 	char fw_ver[64];
5893 
5894 	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
5895 	strscpy(info->version, DRV_VERSION, sizeof(info->version));
5896 
5897 	ipw2100_get_fwversion(priv, fw_ver, sizeof(fw_ver));
5898 
5899 	strscpy(info->fw_version, fw_ver, sizeof(info->fw_version));
5900 	strscpy(info->bus_info, pci_name(priv->pci_dev),
5901 		sizeof(info->bus_info));
5902 }
5903 
5904 static u32 ipw2100_ethtool_get_link(struct net_device *dev)
5905 {
5906 	struct ipw2100_priv *priv = libipw_priv(dev);
5907 	return (priv->status & STATUS_ASSOCIATED) ? 1 : 0;
5908 }
5909 
5910 static const struct ethtool_ops ipw2100_ethtool_ops = {
5911 	.get_link = ipw2100_ethtool_get_link,
5912 	.get_drvinfo = ipw_ethtool_get_drvinfo,
5913 };
5914 
5915 static void ipw2100_hang_check(struct work_struct *work)
5916 {
5917 	struct ipw2100_priv *priv =
5918 		container_of(work, struct ipw2100_priv, hang_check.work);
5919 	unsigned long flags;
5920 	u32 rtc = 0xa5a5a5a5;
5921 	u32 len = sizeof(rtc);
5922 	int restart = 0;
5923 
5924 	spin_lock_irqsave(&priv->low_lock, flags);
5925 
5926 	if (priv->fatal_error != 0) {
5927 		/* If fatal_error is set then we need to restart */
5928 		IPW_DEBUG_INFO("%s: Hardware fatal error detected.\n",
5929 			       priv->net_dev->name);
5930 
5931 		restart = 1;
5932 	} else if (ipw2100_get_ordinal(priv, IPW_ORD_RTC_TIME, &rtc, &len) ||
5933 		   (rtc == priv->last_rtc)) {
5934 		/* Check if firmware is hung */
5935 		IPW_DEBUG_INFO("%s: Firmware RTC stalled.\n",
5936 			       priv->net_dev->name);
5937 
5938 		restart = 1;
5939 	}
5940 
5941 	if (restart) {
5942 		/* Kill timer */
5943 		priv->stop_hang_check = 1;
5944 		priv->hangs++;
5945 
5946 		/* Restart the NIC */
5947 		schedule_reset(priv);
5948 	}
5949 
5950 	priv->last_rtc = rtc;
5951 
5952 	if (!priv->stop_hang_check)
5953 		schedule_delayed_work(&priv->hang_check, HZ / 2);
5954 
5955 	spin_unlock_irqrestore(&priv->low_lock, flags);
5956 }
5957 
5958 static void ipw2100_rf_kill(struct work_struct *work)
5959 {
5960 	struct ipw2100_priv *priv =
5961 		container_of(work, struct ipw2100_priv, rf_kill.work);
5962 	unsigned long flags;
5963 
5964 	spin_lock_irqsave(&priv->low_lock, flags);
5965 
5966 	if (rf_kill_active(priv)) {
5967 		IPW_DEBUG_RF_KILL("RF Kill active, rescheduling GPIO check\n");
5968 		if (!priv->stop_rf_kill)
5969 			schedule_delayed_work(&priv->rf_kill,
5970 					      round_jiffies_relative(HZ));
5971 		goto exit_unlock;
5972 	}
5973 
5974 	/* RF Kill is now disabled, so bring the device back up */
5975 
5976 	if (!(priv->status & STATUS_RF_KILL_MASK)) {
5977 		IPW_DEBUG_RF_KILL("HW RF Kill no longer active, restarting "
5978 				  "device\n");
5979 		schedule_reset(priv);
5980 	} else
5981 		IPW_DEBUG_RF_KILL("HW RF Kill deactivated.  SW RF Kill still "
5982 				  "enabled\n");
5983 
5984       exit_unlock:
5985 	spin_unlock_irqrestore(&priv->low_lock, flags);
5986 }
5987 
5988 static void ipw2100_irq_tasklet(struct tasklet_struct *t);
5989 
5990 static const struct net_device_ops ipw2100_netdev_ops = {
5991 	.ndo_open		= ipw2100_open,
5992 	.ndo_stop		= ipw2100_close,
5993 	.ndo_start_xmit		= libipw_xmit,
5994 	.ndo_tx_timeout		= ipw2100_tx_timeout,
5995 	.ndo_set_mac_address	= ipw2100_set_address,
5996 	.ndo_validate_addr	= eth_validate_addr,
5997 };
5998 
5999 /* Look into using netdev destructor to shutdown libipw? */
6000 
6001 static struct net_device *ipw2100_alloc_device(struct pci_dev *pci_dev,
6002 					       void __iomem * ioaddr)
6003 {
6004 	struct ipw2100_priv *priv;
6005 	struct net_device *dev;
6006 
6007 	dev = alloc_libipw(sizeof(struct ipw2100_priv), 0);
6008 	if (!dev)
6009 		return NULL;
6010 	priv = libipw_priv(dev);
6011 	priv->ieee = netdev_priv(dev);
6012 	priv->pci_dev = pci_dev;
6013 	priv->net_dev = dev;
6014 	priv->ioaddr = ioaddr;
6015 
6016 	priv->ieee->hard_start_xmit = ipw2100_tx;
6017 	priv->ieee->set_security = shim__set_security;
6018 
6019 	priv->ieee->perfect_rssi = -20;
6020 	priv->ieee->worst_rssi = -85;
6021 
6022 	dev->netdev_ops = &ipw2100_netdev_ops;
6023 	dev->ethtool_ops = &ipw2100_ethtool_ops;
6024 	dev->wireless_handlers = &ipw2100_wx_handler_def;
6025 	dev->watchdog_timeo = 3 * HZ;
6026 	dev->irq = 0;
6027 	dev->min_mtu = 68;
6028 	dev->max_mtu = LIBIPW_DATA_LEN;
6029 
6030 	/* NOTE: We don't use the wireless_handlers hook
6031 	 * in dev as the system will start throwing WX requests
6032 	 * to us before we're actually initialized and it just
6033 	 * ends up causing problems.  So, we just handle
6034 	 * the WX extensions through the ipw2100_ioctl interface */
6035 
6036 	/* memset() puts everything to 0, so we only have explicitly set
6037 	 * those values that need to be something else */
6038 
6039 	/* If power management is turned on, default to AUTO mode */
6040 	priv->power_mode = IPW_POWER_AUTO;
6041 
6042 #ifdef CONFIG_IPW2100_MONITOR
6043 	priv->config |= CFG_CRC_CHECK;
6044 #endif
6045 	priv->ieee->wpa_enabled = 0;
6046 	priv->ieee->drop_unencrypted = 0;
6047 	priv->ieee->privacy_invoked = 0;
6048 	priv->ieee->ieee802_1x = 1;
6049 
6050 	/* Set module parameters */
6051 	switch (network_mode) {
6052 	case 1:
6053 		priv->ieee->iw_mode = IW_MODE_ADHOC;
6054 		break;
6055 #ifdef CONFIG_IPW2100_MONITOR
6056 	case 2:
6057 		priv->ieee->iw_mode = IW_MODE_MONITOR;
6058 		break;
6059 #endif
6060 	default:
6061 	case 0:
6062 		priv->ieee->iw_mode = IW_MODE_INFRA;
6063 		break;
6064 	}
6065 
6066 	if (disable == 1)
6067 		priv->status |= STATUS_RF_KILL_SW;
6068 
6069 	if (channel != 0 &&
6070 	    ((channel >= REG_MIN_CHANNEL) && (channel <= REG_MAX_CHANNEL))) {
6071 		priv->config |= CFG_STATIC_CHANNEL;
6072 		priv->channel = channel;
6073 	}
6074 
6075 	if (associate)
6076 		priv->config |= CFG_ASSOCIATE;
6077 
6078 	priv->beacon_interval = DEFAULT_BEACON_INTERVAL;
6079 	priv->short_retry_limit = DEFAULT_SHORT_RETRY_LIMIT;
6080 	priv->long_retry_limit = DEFAULT_LONG_RETRY_LIMIT;
6081 	priv->rts_threshold = DEFAULT_RTS_THRESHOLD | RTS_DISABLED;
6082 	priv->frag_threshold = DEFAULT_FTS | FRAG_DISABLED;
6083 	priv->tx_power = IPW_TX_POWER_DEFAULT;
6084 	priv->tx_rates = DEFAULT_TX_RATES;
6085 
6086 	strcpy(priv->nick, "ipw2100");
6087 
6088 	spin_lock_init(&priv->low_lock);
6089 	mutex_init(&priv->action_mutex);
6090 	mutex_init(&priv->adapter_mutex);
6091 
6092 	init_waitqueue_head(&priv->wait_command_queue);
6093 
6094 	netif_carrier_off(dev);
6095 
6096 	INIT_LIST_HEAD(&priv->msg_free_list);
6097 	INIT_LIST_HEAD(&priv->msg_pend_list);
6098 	INIT_STAT(&priv->msg_free_stat);
6099 	INIT_STAT(&priv->msg_pend_stat);
6100 
6101 	INIT_LIST_HEAD(&priv->tx_free_list);
6102 	INIT_LIST_HEAD(&priv->tx_pend_list);
6103 	INIT_STAT(&priv->tx_free_stat);
6104 	INIT_STAT(&priv->tx_pend_stat);
6105 
6106 	INIT_LIST_HEAD(&priv->fw_pend_list);
6107 	INIT_STAT(&priv->fw_pend_stat);
6108 
6109 	INIT_DELAYED_WORK(&priv->reset_work, ipw2100_reset_adapter);
6110 	INIT_DELAYED_WORK(&priv->security_work, ipw2100_security_work);
6111 	INIT_DELAYED_WORK(&priv->wx_event_work, ipw2100_wx_event_work);
6112 	INIT_DELAYED_WORK(&priv->hang_check, ipw2100_hang_check);
6113 	INIT_DELAYED_WORK(&priv->rf_kill, ipw2100_rf_kill);
6114 	INIT_DELAYED_WORK(&priv->scan_event, ipw2100_scan_event);
6115 
6116 	tasklet_setup(&priv->irq_tasklet, ipw2100_irq_tasklet);
6117 
6118 	/* NOTE:  We do not start the deferred work for status checks yet */
6119 	priv->stop_rf_kill = 1;
6120 	priv->stop_hang_check = 1;
6121 
6122 	return dev;
6123 }
6124 
6125 static int ipw2100_pci_init_one(struct pci_dev *pci_dev,
6126 				const struct pci_device_id *ent)
6127 {
6128 	void __iomem *ioaddr;
6129 	struct net_device *dev = NULL;
6130 	struct ipw2100_priv *priv = NULL;
6131 	int err = 0;
6132 	int registered = 0;
6133 	u32 val;
6134 
6135 	IPW_DEBUG_INFO("enter\n");
6136 
6137 	if (!(pci_resource_flags(pci_dev, 0) & IORESOURCE_MEM)) {
6138 		IPW_DEBUG_INFO("weird - resource type is not memory\n");
6139 		err = -ENODEV;
6140 		goto out;
6141 	}
6142 
6143 	ioaddr = pci_iomap(pci_dev, 0, 0);
6144 	if (!ioaddr) {
6145 		printk(KERN_WARNING DRV_NAME
6146 		       "Error calling ioremap.\n");
6147 		err = -EIO;
6148 		goto fail;
6149 	}
6150 
6151 	/* allocate and initialize our net_device */
6152 	dev = ipw2100_alloc_device(pci_dev, ioaddr);
6153 	if (!dev) {
6154 		printk(KERN_WARNING DRV_NAME
6155 		       "Error calling ipw2100_alloc_device.\n");
6156 		err = -ENOMEM;
6157 		goto fail;
6158 	}
6159 
6160 	/* set up PCI mappings for device */
6161 	err = pci_enable_device(pci_dev);
6162 	if (err) {
6163 		printk(KERN_WARNING DRV_NAME
6164 		       "Error calling pci_enable_device.\n");
6165 		return err;
6166 	}
6167 
6168 	priv = libipw_priv(dev);
6169 
6170 	pci_set_master(pci_dev);
6171 	pci_set_drvdata(pci_dev, priv);
6172 
6173 	err = dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32));
6174 	if (err) {
6175 		printk(KERN_WARNING DRV_NAME
6176 		       "Error calling pci_set_dma_mask.\n");
6177 		pci_disable_device(pci_dev);
6178 		return err;
6179 	}
6180 
6181 	err = pci_request_regions(pci_dev, DRV_NAME);
6182 	if (err) {
6183 		printk(KERN_WARNING DRV_NAME
6184 		       "Error calling pci_request_regions.\n");
6185 		pci_disable_device(pci_dev);
6186 		return err;
6187 	}
6188 
6189 	/* We disable the RETRY_TIMEOUT register (0x41) to keep
6190 	 * PCI Tx retries from interfering with C3 CPU state */
6191 	pci_read_config_dword(pci_dev, 0x40, &val);
6192 	if ((val & 0x0000ff00) != 0)
6193 		pci_write_config_dword(pci_dev, 0x40, val & 0xffff00ff);
6194 
6195 	if (!ipw2100_hw_is_adapter_in_system(dev)) {
6196 		printk(KERN_WARNING DRV_NAME
6197 		       "Device not found via register read.\n");
6198 		err = -ENODEV;
6199 		goto fail;
6200 	}
6201 
6202 	SET_NETDEV_DEV(dev, &pci_dev->dev);
6203 
6204 	/* Force interrupts to be shut off on the device */
6205 	priv->status |= STATUS_INT_ENABLED;
6206 	ipw2100_disable_interrupts(priv);
6207 
6208 	/* Allocate and initialize the Tx/Rx queues and lists */
6209 	if (ipw2100_queues_allocate(priv)) {
6210 		printk(KERN_WARNING DRV_NAME
6211 		       "Error calling ipw2100_queues_allocate.\n");
6212 		err = -ENOMEM;
6213 		goto fail;
6214 	}
6215 	ipw2100_queues_initialize(priv);
6216 
6217 	err = request_irq(pci_dev->irq,
6218 			  ipw2100_interrupt, IRQF_SHARED, dev->name, priv);
6219 	if (err) {
6220 		printk(KERN_WARNING DRV_NAME
6221 		       "Error calling request_irq: %d.\n", pci_dev->irq);
6222 		goto fail;
6223 	}
6224 	dev->irq = pci_dev->irq;
6225 
6226 	IPW_DEBUG_INFO("Attempting to register device...\n");
6227 
6228 	printk(KERN_INFO DRV_NAME
6229 	       ": Detected Intel PRO/Wireless 2100 Network Connection\n");
6230 
6231 	err = ipw2100_up(priv, 1);
6232 	if (err)
6233 		goto fail;
6234 
6235 	err = ipw2100_wdev_init(dev);
6236 	if (err)
6237 		goto fail;
6238 	registered = 1;
6239 
6240 	/* Bring up the interface.  Pre 0.46, after we registered the
6241 	 * network device we would call ipw2100_up.  This introduced a race
6242 	 * condition with newer hotplug configurations (network was coming
6243 	 * up and making calls before the device was initialized).
6244 	 */
6245 	err = register_netdev(dev);
6246 	if (err) {
6247 		printk(KERN_WARNING DRV_NAME
6248 		       "Error calling register_netdev.\n");
6249 		goto fail;
6250 	}
6251 	registered = 2;
6252 
6253 	mutex_lock(&priv->action_mutex);
6254 
6255 	IPW_DEBUG_INFO("%s: Bound to %s\n", dev->name, pci_name(pci_dev));
6256 
6257 	/* perform this after register_netdev so that dev->name is set */
6258 	err = sysfs_create_group(&pci_dev->dev.kobj, &ipw2100_attribute_group);
6259 	if (err)
6260 		goto fail_unlock;
6261 
6262 	/* If the RF Kill switch is disabled, go ahead and complete the
6263 	 * startup sequence */
6264 	if (!(priv->status & STATUS_RF_KILL_MASK)) {
6265 		/* Enable the adapter - sends HOST_COMPLETE */
6266 		if (ipw2100_enable_adapter(priv)) {
6267 			printk(KERN_WARNING DRV_NAME
6268 			       ": %s: failed in call to enable adapter.\n",
6269 			       priv->net_dev->name);
6270 			ipw2100_hw_stop_adapter(priv);
6271 			err = -EIO;
6272 			goto fail_unlock;
6273 		}
6274 
6275 		/* Start a scan . . . */
6276 		ipw2100_set_scan_options(priv);
6277 		ipw2100_start_scan(priv);
6278 	}
6279 
6280 	IPW_DEBUG_INFO("exit\n");
6281 
6282 	priv->status |= STATUS_INITIALIZED;
6283 
6284 	mutex_unlock(&priv->action_mutex);
6285 out:
6286 	return err;
6287 
6288       fail_unlock:
6289 	mutex_unlock(&priv->action_mutex);
6290       fail:
6291 	if (dev) {
6292 		if (registered >= 2)
6293 			unregister_netdev(dev);
6294 
6295 		if (registered) {
6296 			wiphy_unregister(priv->ieee->wdev.wiphy);
6297 			kfree(priv->ieee->bg_band.channels);
6298 		}
6299 
6300 		ipw2100_hw_stop_adapter(priv);
6301 
6302 		ipw2100_disable_interrupts(priv);
6303 
6304 		if (dev->irq)
6305 			free_irq(dev->irq, priv);
6306 
6307 		ipw2100_kill_works(priv);
6308 
6309 		/* These are safe to call even if they weren't allocated */
6310 		ipw2100_queues_free(priv);
6311 		sysfs_remove_group(&pci_dev->dev.kobj,
6312 				   &ipw2100_attribute_group);
6313 
6314 		free_libipw(dev, 0);
6315 	}
6316 
6317 	pci_iounmap(pci_dev, ioaddr);
6318 
6319 	pci_release_regions(pci_dev);
6320 	pci_disable_device(pci_dev);
6321 	goto out;
6322 }
6323 
6324 static void ipw2100_pci_remove_one(struct pci_dev *pci_dev)
6325 {
6326 	struct ipw2100_priv *priv = pci_get_drvdata(pci_dev);
6327 	struct net_device *dev = priv->net_dev;
6328 
6329 	mutex_lock(&priv->action_mutex);
6330 
6331 	priv->status &= ~STATUS_INITIALIZED;
6332 
6333 	sysfs_remove_group(&pci_dev->dev.kobj, &ipw2100_attribute_group);
6334 
6335 #ifdef CONFIG_PM
6336 	if (ipw2100_firmware.version)
6337 		ipw2100_release_firmware(priv, &ipw2100_firmware);
6338 #endif
6339 	/* Take down the hardware */
6340 	ipw2100_down(priv);
6341 
6342 	/* Release the mutex so that the network subsystem can
6343 	 * complete any needed calls into the driver... */
6344 	mutex_unlock(&priv->action_mutex);
6345 
6346 	/* Unregister the device first - this results in close()
6347 	 * being called if the device is open.  If we free storage
6348 	 * first, then close() will crash.
6349 	 * FIXME: remove the comment above. */
6350 	unregister_netdev(dev);
6351 
6352 	ipw2100_kill_works(priv);
6353 
6354 	ipw2100_queues_free(priv);
6355 
6356 	/* Free potential debugging firmware snapshot */
6357 	ipw2100_snapshot_free(priv);
6358 
6359 	free_irq(dev->irq, priv);
6360 
6361 	pci_iounmap(pci_dev, priv->ioaddr);
6362 
6363 	/* wiphy_unregister needs to be here, before free_libipw */
6364 	wiphy_unregister(priv->ieee->wdev.wiphy);
6365 	kfree(priv->ieee->bg_band.channels);
6366 	free_libipw(dev, 0);
6367 
6368 	pci_release_regions(pci_dev);
6369 	pci_disable_device(pci_dev);
6370 
6371 	IPW_DEBUG_INFO("exit\n");
6372 }
6373 
6374 static int __maybe_unused ipw2100_suspend(struct device *dev_d)
6375 {
6376 	struct ipw2100_priv *priv = dev_get_drvdata(dev_d);
6377 	struct net_device *dev = priv->net_dev;
6378 
6379 	IPW_DEBUG_INFO("%s: Going into suspend...\n", dev->name);
6380 
6381 	mutex_lock(&priv->action_mutex);
6382 	if (priv->status & STATUS_INITIALIZED) {
6383 		/* Take down the device; powers it off, etc. */
6384 		ipw2100_down(priv);
6385 	}
6386 
6387 	/* Remove the PRESENT state of the device */
6388 	netif_device_detach(dev);
6389 
6390 	priv->suspend_at = ktime_get_boottime_seconds();
6391 
6392 	mutex_unlock(&priv->action_mutex);
6393 
6394 	return 0;
6395 }
6396 
6397 static int __maybe_unused ipw2100_resume(struct device *dev_d)
6398 {
6399 	struct pci_dev *pci_dev = to_pci_dev(dev_d);
6400 	struct ipw2100_priv *priv = pci_get_drvdata(pci_dev);
6401 	struct net_device *dev = priv->net_dev;
6402 	u32 val;
6403 
6404 	if (IPW2100_PM_DISABLED)
6405 		return 0;
6406 
6407 	mutex_lock(&priv->action_mutex);
6408 
6409 	IPW_DEBUG_INFO("%s: Coming out of suspend...\n", dev->name);
6410 
6411 	/*
6412 	 * Suspend/Resume resets the PCI configuration space, so we have to
6413 	 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
6414 	 * from interfering with C3 CPU state. pci_restore_state won't help
6415 	 * here since it only restores the first 64 bytes pci config header.
6416 	 */
6417 	pci_read_config_dword(pci_dev, 0x40, &val);
6418 	if ((val & 0x0000ff00) != 0)
6419 		pci_write_config_dword(pci_dev, 0x40, val & 0xffff00ff);
6420 
6421 	/* Set the device back into the PRESENT state; this will also wake
6422 	 * the queue of needed */
6423 	netif_device_attach(dev);
6424 
6425 	priv->suspend_time = ktime_get_boottime_seconds() - priv->suspend_at;
6426 
6427 	/* Bring the device back up */
6428 	if (!(priv->status & STATUS_RF_KILL_SW))
6429 		ipw2100_up(priv, 0);
6430 
6431 	mutex_unlock(&priv->action_mutex);
6432 
6433 	return 0;
6434 }
6435 
6436 static void ipw2100_shutdown(struct pci_dev *pci_dev)
6437 {
6438 	struct ipw2100_priv *priv = pci_get_drvdata(pci_dev);
6439 
6440 	/* Take down the device; powers it off, etc. */
6441 	ipw2100_down(priv);
6442 
6443 	pci_disable_device(pci_dev);
6444 }
6445 
6446 #define IPW2100_DEV_ID(x) { PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, x }
6447 
6448 static const struct pci_device_id ipw2100_pci_id_table[] = {
6449 	IPW2100_DEV_ID(0x2520),	/* IN 2100A mPCI 3A */
6450 	IPW2100_DEV_ID(0x2521),	/* IN 2100A mPCI 3B */
6451 	IPW2100_DEV_ID(0x2524),	/* IN 2100A mPCI 3B */
6452 	IPW2100_DEV_ID(0x2525),	/* IN 2100A mPCI 3B */
6453 	IPW2100_DEV_ID(0x2526),	/* IN 2100A mPCI Gen A3 */
6454 	IPW2100_DEV_ID(0x2522),	/* IN 2100 mPCI 3B */
6455 	IPW2100_DEV_ID(0x2523),	/* IN 2100 mPCI 3A */
6456 	IPW2100_DEV_ID(0x2527),	/* IN 2100 mPCI 3B */
6457 	IPW2100_DEV_ID(0x2528),	/* IN 2100 mPCI 3B */
6458 	IPW2100_DEV_ID(0x2529),	/* IN 2100 mPCI 3B */
6459 	IPW2100_DEV_ID(0x252B),	/* IN 2100 mPCI 3A */
6460 	IPW2100_DEV_ID(0x252C),	/* IN 2100 mPCI 3A */
6461 	IPW2100_DEV_ID(0x252D),	/* IN 2100 mPCI 3A */
6462 
6463 	IPW2100_DEV_ID(0x2550),	/* IB 2100A mPCI 3B */
6464 	IPW2100_DEV_ID(0x2551),	/* IB 2100 mPCI 3B */
6465 	IPW2100_DEV_ID(0x2553),	/* IB 2100 mPCI 3B */
6466 	IPW2100_DEV_ID(0x2554),	/* IB 2100 mPCI 3B */
6467 	IPW2100_DEV_ID(0x2555),	/* IB 2100 mPCI 3B */
6468 
6469 	IPW2100_DEV_ID(0x2560),	/* DE 2100A mPCI 3A */
6470 	IPW2100_DEV_ID(0x2562),	/* DE 2100A mPCI 3A */
6471 	IPW2100_DEV_ID(0x2563),	/* DE 2100A mPCI 3A */
6472 	IPW2100_DEV_ID(0x2561),	/* DE 2100 mPCI 3A */
6473 	IPW2100_DEV_ID(0x2565),	/* DE 2100 mPCI 3A */
6474 	IPW2100_DEV_ID(0x2566),	/* DE 2100 mPCI 3A */
6475 	IPW2100_DEV_ID(0x2567),	/* DE 2100 mPCI 3A */
6476 
6477 	IPW2100_DEV_ID(0x2570),	/* GA 2100 mPCI 3B */
6478 
6479 	IPW2100_DEV_ID(0x2580),	/* TO 2100A mPCI 3B */
6480 	IPW2100_DEV_ID(0x2582),	/* TO 2100A mPCI 3B */
6481 	IPW2100_DEV_ID(0x2583),	/* TO 2100A mPCI 3B */
6482 	IPW2100_DEV_ID(0x2581),	/* TO 2100 mPCI 3B */
6483 	IPW2100_DEV_ID(0x2585),	/* TO 2100 mPCI 3B */
6484 	IPW2100_DEV_ID(0x2586),	/* TO 2100 mPCI 3B */
6485 	IPW2100_DEV_ID(0x2587),	/* TO 2100 mPCI 3B */
6486 
6487 	IPW2100_DEV_ID(0x2590),	/* SO 2100A mPCI 3B */
6488 	IPW2100_DEV_ID(0x2592),	/* SO 2100A mPCI 3B */
6489 	IPW2100_DEV_ID(0x2591),	/* SO 2100 mPCI 3B */
6490 	IPW2100_DEV_ID(0x2593),	/* SO 2100 mPCI 3B */
6491 	IPW2100_DEV_ID(0x2596),	/* SO 2100 mPCI 3B */
6492 	IPW2100_DEV_ID(0x2598),	/* SO 2100 mPCI 3B */
6493 
6494 	IPW2100_DEV_ID(0x25A0),	/* HP 2100 mPCI 3B */
6495 	{0,},
6496 };
6497 
6498 MODULE_DEVICE_TABLE(pci, ipw2100_pci_id_table);
6499 
6500 static SIMPLE_DEV_PM_OPS(ipw2100_pm_ops, ipw2100_suspend, ipw2100_resume);
6501 
6502 static struct pci_driver ipw2100_pci_driver = {
6503 	.name = DRV_NAME,
6504 	.id_table = ipw2100_pci_id_table,
6505 	.probe = ipw2100_pci_init_one,
6506 	.remove = ipw2100_pci_remove_one,
6507 	.driver.pm = &ipw2100_pm_ops,
6508 	.shutdown = ipw2100_shutdown,
6509 };
6510 
6511 /*
6512  * Initialize the ipw2100 driver/module
6513  *
6514  * @returns 0 if ok, < 0 errno node con error.
6515  *
6516  * Note: we cannot init the /proc stuff until the PCI driver is there,
6517  * or we risk an unlikely race condition on someone accessing
6518  * uninitialized data in the PCI dev struct through /proc.
6519  */
6520 static int __init ipw2100_init(void)
6521 {
6522 	int ret;
6523 
6524 	printk(KERN_INFO DRV_NAME ": %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
6525 	printk(KERN_INFO DRV_NAME ": %s\n", DRV_COPYRIGHT);
6526 
6527 	cpu_latency_qos_add_request(&ipw2100_pm_qos_req, PM_QOS_DEFAULT_VALUE);
6528 
6529 	ret = pci_register_driver(&ipw2100_pci_driver);
6530 	if (ret)
6531 		goto out;
6532 
6533 #ifdef CONFIG_IPW2100_DEBUG
6534 	ipw2100_debug_level = debug;
6535 	ret = driver_create_file(&ipw2100_pci_driver.driver,
6536 				 &driver_attr_debug_level);
6537 #endif
6538 
6539 out:
6540 	return ret;
6541 }
6542 
6543 /*
6544  * Cleanup ipw2100 driver registration
6545  */
6546 static void __exit ipw2100_exit(void)
6547 {
6548 	/* FIXME: IPG: check that we have no instances of the devices open */
6549 #ifdef CONFIG_IPW2100_DEBUG
6550 	driver_remove_file(&ipw2100_pci_driver.driver,
6551 			   &driver_attr_debug_level);
6552 #endif
6553 	pci_unregister_driver(&ipw2100_pci_driver);
6554 	cpu_latency_qos_remove_request(&ipw2100_pm_qos_req);
6555 }
6556 
6557 module_init(ipw2100_init);
6558 module_exit(ipw2100_exit);
6559 
6560 static int ipw2100_wx_get_name(struct net_device *dev,
6561 			       struct iw_request_info *info,
6562 			       union iwreq_data *wrqu, char *extra)
6563 {
6564 	/*
6565 	 * This can be called at any time.  No action lock required
6566 	 */
6567 
6568 	struct ipw2100_priv *priv = libipw_priv(dev);
6569 	if (!(priv->status & STATUS_ASSOCIATED))
6570 		strcpy(wrqu->name, "unassociated");
6571 	else
6572 		snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11b");
6573 
6574 	IPW_DEBUG_WX("Name: %s\n", wrqu->name);
6575 	return 0;
6576 }
6577 
6578 static int ipw2100_wx_set_freq(struct net_device *dev,
6579 			       struct iw_request_info *info,
6580 			       union iwreq_data *wrqu, char *extra)
6581 {
6582 	struct ipw2100_priv *priv = libipw_priv(dev);
6583 	struct iw_freq *fwrq = &wrqu->freq;
6584 	int err = 0;
6585 
6586 	if (priv->ieee->iw_mode == IW_MODE_INFRA)
6587 		return -EOPNOTSUPP;
6588 
6589 	mutex_lock(&priv->action_mutex);
6590 	if (!(priv->status & STATUS_INITIALIZED)) {
6591 		err = -EIO;
6592 		goto done;
6593 	}
6594 
6595 	/* if setting by freq convert to channel */
6596 	if (fwrq->e == 1) {
6597 		if ((fwrq->m >= (int)2.412e8 && fwrq->m <= (int)2.487e8)) {
6598 			int f = fwrq->m / 100000;
6599 			int c = 0;
6600 
6601 			while ((c < REG_MAX_CHANNEL) &&
6602 			       (f != ipw2100_frequencies[c]))
6603 				c++;
6604 
6605 			/* hack to fall through */
6606 			fwrq->e = 0;
6607 			fwrq->m = c + 1;
6608 		}
6609 	}
6610 
6611 	if (fwrq->e > 0 || fwrq->m > 1000) {
6612 		err = -EOPNOTSUPP;
6613 		goto done;
6614 	} else {		/* Set the channel */
6615 		IPW_DEBUG_WX("SET Freq/Channel -> %d\n", fwrq->m);
6616 		err = ipw2100_set_channel(priv, fwrq->m, 0);
6617 	}
6618 
6619       done:
6620 	mutex_unlock(&priv->action_mutex);
6621 	return err;
6622 }
6623 
6624 static int ipw2100_wx_get_freq(struct net_device *dev,
6625 			       struct iw_request_info *info,
6626 			       union iwreq_data *wrqu, char *extra)
6627 {
6628 	/*
6629 	 * This can be called at any time.  No action lock required
6630 	 */
6631 
6632 	struct ipw2100_priv *priv = libipw_priv(dev);
6633 
6634 	wrqu->freq.e = 0;
6635 
6636 	/* If we are associated, trying to associate, or have a statically
6637 	 * configured CHANNEL then return that; otherwise return ANY */
6638 	if (priv->config & CFG_STATIC_CHANNEL ||
6639 	    priv->status & STATUS_ASSOCIATED)
6640 		wrqu->freq.m = priv->channel;
6641 	else
6642 		wrqu->freq.m = 0;
6643 
6644 	IPW_DEBUG_WX("GET Freq/Channel -> %d\n", priv->channel);
6645 	return 0;
6646 
6647 }
6648 
6649 static int ipw2100_wx_set_mode(struct net_device *dev,
6650 			       struct iw_request_info *info,
6651 			       union iwreq_data *wrqu, char *extra)
6652 {
6653 	struct ipw2100_priv *priv = libipw_priv(dev);
6654 	int err = 0;
6655 
6656 	IPW_DEBUG_WX("SET Mode -> %d\n", wrqu->mode);
6657 
6658 	if (wrqu->mode == priv->ieee->iw_mode)
6659 		return 0;
6660 
6661 	mutex_lock(&priv->action_mutex);
6662 	if (!(priv->status & STATUS_INITIALIZED)) {
6663 		err = -EIO;
6664 		goto done;
6665 	}
6666 
6667 	switch (wrqu->mode) {
6668 #ifdef CONFIG_IPW2100_MONITOR
6669 	case IW_MODE_MONITOR:
6670 		err = ipw2100_switch_mode(priv, IW_MODE_MONITOR);
6671 		break;
6672 #endif				/* CONFIG_IPW2100_MONITOR */
6673 	case IW_MODE_ADHOC:
6674 		err = ipw2100_switch_mode(priv, IW_MODE_ADHOC);
6675 		break;
6676 	case IW_MODE_INFRA:
6677 	case IW_MODE_AUTO:
6678 	default:
6679 		err = ipw2100_switch_mode(priv, IW_MODE_INFRA);
6680 		break;
6681 	}
6682 
6683       done:
6684 	mutex_unlock(&priv->action_mutex);
6685 	return err;
6686 }
6687 
6688 static int ipw2100_wx_get_mode(struct net_device *dev,
6689 			       struct iw_request_info *info,
6690 			       union iwreq_data *wrqu, char *extra)
6691 {
6692 	/*
6693 	 * This can be called at any time.  No action lock required
6694 	 */
6695 
6696 	struct ipw2100_priv *priv = libipw_priv(dev);
6697 
6698 	wrqu->mode = priv->ieee->iw_mode;
6699 	IPW_DEBUG_WX("GET Mode -> %d\n", wrqu->mode);
6700 
6701 	return 0;
6702 }
6703 
6704 #define POWER_MODES 5
6705 
6706 /* Values are in microsecond */
6707 static const s32 timeout_duration[POWER_MODES] = {
6708 	350000,
6709 	250000,
6710 	75000,
6711 	37000,
6712 	25000,
6713 };
6714 
6715 static const s32 period_duration[POWER_MODES] = {
6716 	400000,
6717 	700000,
6718 	1000000,
6719 	1000000,
6720 	1000000
6721 };
6722 
6723 static int ipw2100_wx_get_range(struct net_device *dev,
6724 				struct iw_request_info *info,
6725 				union iwreq_data *wrqu, char *extra)
6726 {
6727 	/*
6728 	 * This can be called at any time.  No action lock required
6729 	 */
6730 
6731 	struct ipw2100_priv *priv = libipw_priv(dev);
6732 	struct iw_range *range = (struct iw_range *)extra;
6733 	u16 val;
6734 	int i, level;
6735 
6736 	wrqu->data.length = sizeof(*range);
6737 	memset(range, 0, sizeof(*range));
6738 
6739 	/* Let's try to keep this struct in the same order as in
6740 	 * linux/include/wireless.h
6741 	 */
6742 
6743 	/* TODO: See what values we can set, and remove the ones we can't
6744 	 * set, or fill them with some default data.
6745 	 */
6746 
6747 	/* ~5 Mb/s real (802.11b) */
6748 	range->throughput = 5 * 1000 * 1000;
6749 
6750 //      range->sensitivity;     /* signal level threshold range */
6751 
6752 	range->max_qual.qual = 100;
6753 	/* TODO: Find real max RSSI and stick here */
6754 	range->max_qual.level = 0;
6755 	range->max_qual.noise = 0;
6756 	range->max_qual.updated = 7;	/* Updated all three */
6757 
6758 	range->avg_qual.qual = 70;	/* > 8% missed beacons is 'bad' */
6759 	/* TODO: Find real 'good' to 'bad' threshold value for RSSI */
6760 	range->avg_qual.level = 20 + IPW2100_RSSI_TO_DBM;
6761 	range->avg_qual.noise = 0;
6762 	range->avg_qual.updated = 7;	/* Updated all three */
6763 
6764 	range->num_bitrates = RATE_COUNT;
6765 
6766 	for (i = 0; i < RATE_COUNT && i < IW_MAX_BITRATES; i++) {
6767 		range->bitrate[i] = ipw2100_bg_rates[i].bitrate * 100 * 1000;
6768 	}
6769 
6770 	range->min_rts = MIN_RTS_THRESHOLD;
6771 	range->max_rts = MAX_RTS_THRESHOLD;
6772 	range->min_frag = MIN_FRAG_THRESHOLD;
6773 	range->max_frag = MAX_FRAG_THRESHOLD;
6774 
6775 	range->min_pmp = period_duration[0];	/* Minimal PM period */
6776 	range->max_pmp = period_duration[POWER_MODES - 1];	/* Maximal PM period */
6777 	range->min_pmt = timeout_duration[POWER_MODES - 1];	/* Minimal PM timeout */
6778 	range->max_pmt = timeout_duration[0];	/* Maximal PM timeout */
6779 
6780 	/* How to decode max/min PM period */
6781 	range->pmp_flags = IW_POWER_PERIOD;
6782 	/* How to decode max/min PM period */
6783 	range->pmt_flags = IW_POWER_TIMEOUT;
6784 	/* What PM options are supported */
6785 	range->pm_capa = IW_POWER_TIMEOUT | IW_POWER_PERIOD;
6786 
6787 	range->encoding_size[0] = 5;
6788 	range->encoding_size[1] = 13;	/* Different token sizes */
6789 	range->num_encoding_sizes = 2;	/* Number of entry in the list */
6790 	range->max_encoding_tokens = WEP_KEYS;	/* Max number of tokens */
6791 //      range->encoding_login_index;            /* token index for login token */
6792 
6793 	if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
6794 		range->txpower_capa = IW_TXPOW_DBM;
6795 		range->num_txpower = IW_MAX_TXPOWER;
6796 		for (i = 0, level = (IPW_TX_POWER_MAX_DBM * 16);
6797 		     i < IW_MAX_TXPOWER;
6798 		     i++, level -=
6799 		     ((IPW_TX_POWER_MAX_DBM -
6800 		       IPW_TX_POWER_MIN_DBM) * 16) / (IW_MAX_TXPOWER - 1))
6801 			range->txpower[i] = level / 16;
6802 	} else {
6803 		range->txpower_capa = 0;
6804 		range->num_txpower = 0;
6805 	}
6806 
6807 	/* Set the Wireless Extension versions */
6808 	range->we_version_compiled = WIRELESS_EXT;
6809 	range->we_version_source = 18;
6810 
6811 //      range->retry_capa;      /* What retry options are supported */
6812 //      range->retry_flags;     /* How to decode max/min retry limit */
6813 //      range->r_time_flags;    /* How to decode max/min retry life */
6814 //      range->min_retry;       /* Minimal number of retries */
6815 //      range->max_retry;       /* Maximal number of retries */
6816 //      range->min_r_time;      /* Minimal retry lifetime */
6817 //      range->max_r_time;      /* Maximal retry lifetime */
6818 
6819 	range->num_channels = FREQ_COUNT;
6820 
6821 	val = 0;
6822 	for (i = 0; i < FREQ_COUNT; i++) {
6823 		// TODO: Include only legal frequencies for some countries
6824 //              if (local->channel_mask & (1 << i)) {
6825 		range->freq[val].i = i + 1;
6826 		range->freq[val].m = ipw2100_frequencies[i] * 100000;
6827 		range->freq[val].e = 1;
6828 		val++;
6829 //              }
6830 		if (val == IW_MAX_FREQUENCIES)
6831 			break;
6832 	}
6833 	range->num_frequency = val;
6834 
6835 	/* Event capability (kernel + driver) */
6836 	range->event_capa[0] = (IW_EVENT_CAPA_K_0 |
6837 				IW_EVENT_CAPA_MASK(SIOCGIWAP));
6838 	range->event_capa[1] = IW_EVENT_CAPA_K_1;
6839 
6840 	range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
6841 		IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
6842 
6843 	IPW_DEBUG_WX("GET Range\n");
6844 
6845 	return 0;
6846 }
6847 
6848 static int ipw2100_wx_set_wap(struct net_device *dev,
6849 			      struct iw_request_info *info,
6850 			      union iwreq_data *wrqu, char *extra)
6851 {
6852 	struct ipw2100_priv *priv = libipw_priv(dev);
6853 	int err = 0;
6854 
6855 	// sanity checks
6856 	if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
6857 		return -EINVAL;
6858 
6859 	mutex_lock(&priv->action_mutex);
6860 	if (!(priv->status & STATUS_INITIALIZED)) {
6861 		err = -EIO;
6862 		goto done;
6863 	}
6864 
6865 	if (is_broadcast_ether_addr(wrqu->ap_addr.sa_data) ||
6866 	    is_zero_ether_addr(wrqu->ap_addr.sa_data)) {
6867 		/* we disable mandatory BSSID association */
6868 		IPW_DEBUG_WX("exit - disable mandatory BSSID\n");
6869 		priv->config &= ~CFG_STATIC_BSSID;
6870 		err = ipw2100_set_mandatory_bssid(priv, NULL, 0);
6871 		goto done;
6872 	}
6873 
6874 	priv->config |= CFG_STATIC_BSSID;
6875 	memcpy(priv->mandatory_bssid_mac, wrqu->ap_addr.sa_data, ETH_ALEN);
6876 
6877 	err = ipw2100_set_mandatory_bssid(priv, wrqu->ap_addr.sa_data, 0);
6878 
6879 	IPW_DEBUG_WX("SET BSSID -> %pM\n", wrqu->ap_addr.sa_data);
6880 
6881       done:
6882 	mutex_unlock(&priv->action_mutex);
6883 	return err;
6884 }
6885 
6886 static int ipw2100_wx_get_wap(struct net_device *dev,
6887 			      struct iw_request_info *info,
6888 			      union iwreq_data *wrqu, char *extra)
6889 {
6890 	/*
6891 	 * This can be called at any time.  No action lock required
6892 	 */
6893 
6894 	struct ipw2100_priv *priv = libipw_priv(dev);
6895 
6896 	/* If we are associated, trying to associate, or have a statically
6897 	 * configured BSSID then return that; otherwise return ANY */
6898 	if (priv->config & CFG_STATIC_BSSID || priv->status & STATUS_ASSOCIATED) {
6899 		wrqu->ap_addr.sa_family = ARPHRD_ETHER;
6900 		memcpy(wrqu->ap_addr.sa_data, priv->bssid, ETH_ALEN);
6901 	} else
6902 		eth_zero_addr(wrqu->ap_addr.sa_data);
6903 
6904 	IPW_DEBUG_WX("Getting WAP BSSID: %pM\n", wrqu->ap_addr.sa_data);
6905 	return 0;
6906 }
6907 
6908 static int ipw2100_wx_set_essid(struct net_device *dev,
6909 				struct iw_request_info *info,
6910 				union iwreq_data *wrqu, char *extra)
6911 {
6912 	struct ipw2100_priv *priv = libipw_priv(dev);
6913 	char *essid = "";	/* ANY */
6914 	int length = 0;
6915 	int err = 0;
6916 
6917 	mutex_lock(&priv->action_mutex);
6918 	if (!(priv->status & STATUS_INITIALIZED)) {
6919 		err = -EIO;
6920 		goto done;
6921 	}
6922 
6923 	if (wrqu->essid.flags && wrqu->essid.length) {
6924 		length = wrqu->essid.length;
6925 		essid = extra;
6926 	}
6927 
6928 	if (length == 0) {
6929 		IPW_DEBUG_WX("Setting ESSID to ANY\n");
6930 		priv->config &= ~CFG_STATIC_ESSID;
6931 		err = ipw2100_set_essid(priv, NULL, 0, 0);
6932 		goto done;
6933 	}
6934 
6935 	length = min(length, IW_ESSID_MAX_SIZE);
6936 
6937 	priv->config |= CFG_STATIC_ESSID;
6938 
6939 	if (priv->essid_len == length && !memcmp(priv->essid, extra, length)) {
6940 		IPW_DEBUG_WX("ESSID set to current ESSID.\n");
6941 		err = 0;
6942 		goto done;
6943 	}
6944 
6945 	IPW_DEBUG_WX("Setting ESSID: '%*pE' (%d)\n", length, essid, length);
6946 
6947 	priv->essid_len = length;
6948 	memcpy(priv->essid, essid, priv->essid_len);
6949 
6950 	err = ipw2100_set_essid(priv, essid, length, 0);
6951 
6952       done:
6953 	mutex_unlock(&priv->action_mutex);
6954 	return err;
6955 }
6956 
6957 static int ipw2100_wx_get_essid(struct net_device *dev,
6958 				struct iw_request_info *info,
6959 				union iwreq_data *wrqu, char *extra)
6960 {
6961 	/*
6962 	 * This can be called at any time.  No action lock required
6963 	 */
6964 
6965 	struct ipw2100_priv *priv = libipw_priv(dev);
6966 
6967 	/* If we are associated, trying to associate, or have a statically
6968 	 * configured ESSID then return that; otherwise return ANY */
6969 	if (priv->config & CFG_STATIC_ESSID || priv->status & STATUS_ASSOCIATED) {
6970 		IPW_DEBUG_WX("Getting essid: '%*pE'\n",
6971 			     priv->essid_len, priv->essid);
6972 		memcpy(extra, priv->essid, priv->essid_len);
6973 		wrqu->essid.length = priv->essid_len;
6974 		wrqu->essid.flags = 1;	/* active */
6975 	} else {
6976 		IPW_DEBUG_WX("Getting essid: ANY\n");
6977 		wrqu->essid.length = 0;
6978 		wrqu->essid.flags = 0;	/* active */
6979 	}
6980 
6981 	return 0;
6982 }
6983 
6984 static int ipw2100_wx_set_nick(struct net_device *dev,
6985 			       struct iw_request_info *info,
6986 			       union iwreq_data *wrqu, char *extra)
6987 {
6988 	/*
6989 	 * This can be called at any time.  No action lock required
6990 	 */
6991 
6992 	struct ipw2100_priv *priv = libipw_priv(dev);
6993 
6994 	if (wrqu->data.length > IW_ESSID_MAX_SIZE)
6995 		return -E2BIG;
6996 
6997 	wrqu->data.length = min_t(size_t, wrqu->data.length, sizeof(priv->nick));
6998 	memset(priv->nick, 0, sizeof(priv->nick));
6999 	memcpy(priv->nick, extra, wrqu->data.length);
7000 
7001 	IPW_DEBUG_WX("SET Nickname -> %s\n", priv->nick);
7002 
7003 	return 0;
7004 }
7005 
7006 static int ipw2100_wx_get_nick(struct net_device *dev,
7007 			       struct iw_request_info *info,
7008 			       union iwreq_data *wrqu, char *extra)
7009 {
7010 	/*
7011 	 * This can be called at any time.  No action lock required
7012 	 */
7013 
7014 	struct ipw2100_priv *priv = libipw_priv(dev);
7015 
7016 	wrqu->data.length = strlen(priv->nick);
7017 	memcpy(extra, priv->nick, wrqu->data.length);
7018 	wrqu->data.flags = 1;	/* active */
7019 
7020 	IPW_DEBUG_WX("GET Nickname -> %s\n", extra);
7021 
7022 	return 0;
7023 }
7024 
7025 static int ipw2100_wx_set_rate(struct net_device *dev,
7026 			       struct iw_request_info *info,
7027 			       union iwreq_data *wrqu, char *extra)
7028 {
7029 	struct ipw2100_priv *priv = libipw_priv(dev);
7030 	u32 target_rate = wrqu->bitrate.value;
7031 	u32 rate;
7032 	int err = 0;
7033 
7034 	mutex_lock(&priv->action_mutex);
7035 	if (!(priv->status & STATUS_INITIALIZED)) {
7036 		err = -EIO;
7037 		goto done;
7038 	}
7039 
7040 	rate = 0;
7041 
7042 	if (target_rate == 1000000 ||
7043 	    (!wrqu->bitrate.fixed && target_rate > 1000000))
7044 		rate |= TX_RATE_1_MBIT;
7045 	if (target_rate == 2000000 ||
7046 	    (!wrqu->bitrate.fixed && target_rate > 2000000))
7047 		rate |= TX_RATE_2_MBIT;
7048 	if (target_rate == 5500000 ||
7049 	    (!wrqu->bitrate.fixed && target_rate > 5500000))
7050 		rate |= TX_RATE_5_5_MBIT;
7051 	if (target_rate == 11000000 ||
7052 	    (!wrqu->bitrate.fixed && target_rate > 11000000))
7053 		rate |= TX_RATE_11_MBIT;
7054 	if (rate == 0)
7055 		rate = DEFAULT_TX_RATES;
7056 
7057 	err = ipw2100_set_tx_rates(priv, rate, 0);
7058 
7059 	IPW_DEBUG_WX("SET Rate -> %04X\n", rate);
7060       done:
7061 	mutex_unlock(&priv->action_mutex);
7062 	return err;
7063 }
7064 
7065 static int ipw2100_wx_get_rate(struct net_device *dev,
7066 			       struct iw_request_info *info,
7067 			       union iwreq_data *wrqu, char *extra)
7068 {
7069 	struct ipw2100_priv *priv = libipw_priv(dev);
7070 	int val;
7071 	unsigned int len = sizeof(val);
7072 	int err = 0;
7073 
7074 	if (!(priv->status & STATUS_ENABLED) ||
7075 	    priv->status & STATUS_RF_KILL_MASK ||
7076 	    !(priv->status & STATUS_ASSOCIATED)) {
7077 		wrqu->bitrate.value = 0;
7078 		return 0;
7079 	}
7080 
7081 	mutex_lock(&priv->action_mutex);
7082 	if (!(priv->status & STATUS_INITIALIZED)) {
7083 		err = -EIO;
7084 		goto done;
7085 	}
7086 
7087 	err = ipw2100_get_ordinal(priv, IPW_ORD_CURRENT_TX_RATE, &val, &len);
7088 	if (err) {
7089 		IPW_DEBUG_WX("failed querying ordinals.\n");
7090 		goto done;
7091 	}
7092 
7093 	switch (val & TX_RATE_MASK) {
7094 	case TX_RATE_1_MBIT:
7095 		wrqu->bitrate.value = 1000000;
7096 		break;
7097 	case TX_RATE_2_MBIT:
7098 		wrqu->bitrate.value = 2000000;
7099 		break;
7100 	case TX_RATE_5_5_MBIT:
7101 		wrqu->bitrate.value = 5500000;
7102 		break;
7103 	case TX_RATE_11_MBIT:
7104 		wrqu->bitrate.value = 11000000;
7105 		break;
7106 	default:
7107 		wrqu->bitrate.value = 0;
7108 	}
7109 
7110 	IPW_DEBUG_WX("GET Rate -> %d\n", wrqu->bitrate.value);
7111 
7112       done:
7113 	mutex_unlock(&priv->action_mutex);
7114 	return err;
7115 }
7116 
7117 static int ipw2100_wx_set_rts(struct net_device *dev,
7118 			      struct iw_request_info *info,
7119 			      union iwreq_data *wrqu, char *extra)
7120 {
7121 	struct ipw2100_priv *priv = libipw_priv(dev);
7122 	int value, err;
7123 
7124 	/* Auto RTS not yet supported */
7125 	if (wrqu->rts.fixed == 0)
7126 		return -EINVAL;
7127 
7128 	mutex_lock(&priv->action_mutex);
7129 	if (!(priv->status & STATUS_INITIALIZED)) {
7130 		err = -EIO;
7131 		goto done;
7132 	}
7133 
7134 	if (wrqu->rts.disabled)
7135 		value = priv->rts_threshold | RTS_DISABLED;
7136 	else {
7137 		if (wrqu->rts.value < 1 || wrqu->rts.value > 2304) {
7138 			err = -EINVAL;
7139 			goto done;
7140 		}
7141 		value = wrqu->rts.value;
7142 	}
7143 
7144 	err = ipw2100_set_rts_threshold(priv, value);
7145 
7146 	IPW_DEBUG_WX("SET RTS Threshold -> 0x%08X\n", value);
7147       done:
7148 	mutex_unlock(&priv->action_mutex);
7149 	return err;
7150 }
7151 
7152 static int ipw2100_wx_get_rts(struct net_device *dev,
7153 			      struct iw_request_info *info,
7154 			      union iwreq_data *wrqu, char *extra)
7155 {
7156 	/*
7157 	 * This can be called at any time.  No action lock required
7158 	 */
7159 
7160 	struct ipw2100_priv *priv = libipw_priv(dev);
7161 
7162 	wrqu->rts.value = priv->rts_threshold & ~RTS_DISABLED;
7163 	wrqu->rts.fixed = 1;	/* no auto select */
7164 
7165 	/* If RTS is set to the default value, then it is disabled */
7166 	wrqu->rts.disabled = (priv->rts_threshold & RTS_DISABLED) ? 1 : 0;
7167 
7168 	IPW_DEBUG_WX("GET RTS Threshold -> 0x%08X\n", wrqu->rts.value);
7169 
7170 	return 0;
7171 }
7172 
7173 static int ipw2100_wx_set_txpow(struct net_device *dev,
7174 				struct iw_request_info *info,
7175 				union iwreq_data *wrqu, char *extra)
7176 {
7177 	struct ipw2100_priv *priv = libipw_priv(dev);
7178 	int err = 0, value;
7179 
7180 	if (ipw_radio_kill_sw(priv, wrqu->txpower.disabled))
7181 		return -EINPROGRESS;
7182 
7183 	if (priv->ieee->iw_mode != IW_MODE_ADHOC)
7184 		return 0;
7185 
7186 	if ((wrqu->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
7187 		return -EINVAL;
7188 
7189 	if (wrqu->txpower.fixed == 0)
7190 		value = IPW_TX_POWER_DEFAULT;
7191 	else {
7192 		if (wrqu->txpower.value < IPW_TX_POWER_MIN_DBM ||
7193 		    wrqu->txpower.value > IPW_TX_POWER_MAX_DBM)
7194 			return -EINVAL;
7195 
7196 		value = wrqu->txpower.value;
7197 	}
7198 
7199 	mutex_lock(&priv->action_mutex);
7200 	if (!(priv->status & STATUS_INITIALIZED)) {
7201 		err = -EIO;
7202 		goto done;
7203 	}
7204 
7205 	err = ipw2100_set_tx_power(priv, value);
7206 
7207 	IPW_DEBUG_WX("SET TX Power -> %d\n", value);
7208 
7209       done:
7210 	mutex_unlock(&priv->action_mutex);
7211 	return err;
7212 }
7213 
7214 static int ipw2100_wx_get_txpow(struct net_device *dev,
7215 				struct iw_request_info *info,
7216 				union iwreq_data *wrqu, char *extra)
7217 {
7218 	/*
7219 	 * This can be called at any time.  No action lock required
7220 	 */
7221 
7222 	struct ipw2100_priv *priv = libipw_priv(dev);
7223 
7224 	wrqu->txpower.disabled = (priv->status & STATUS_RF_KILL_MASK) ? 1 : 0;
7225 
7226 	if (priv->tx_power == IPW_TX_POWER_DEFAULT) {
7227 		wrqu->txpower.fixed = 0;
7228 		wrqu->txpower.value = IPW_TX_POWER_MAX_DBM;
7229 	} else {
7230 		wrqu->txpower.fixed = 1;
7231 		wrqu->txpower.value = priv->tx_power;
7232 	}
7233 
7234 	wrqu->txpower.flags = IW_TXPOW_DBM;
7235 
7236 	IPW_DEBUG_WX("GET TX Power -> %d\n", wrqu->txpower.value);
7237 
7238 	return 0;
7239 }
7240 
7241 static int ipw2100_wx_set_frag(struct net_device *dev,
7242 			       struct iw_request_info *info,
7243 			       union iwreq_data *wrqu, char *extra)
7244 {
7245 	/*
7246 	 * This can be called at any time.  No action lock required
7247 	 */
7248 
7249 	struct ipw2100_priv *priv = libipw_priv(dev);
7250 
7251 	if (!wrqu->frag.fixed)
7252 		return -EINVAL;
7253 
7254 	if (wrqu->frag.disabled) {
7255 		priv->frag_threshold |= FRAG_DISABLED;
7256 		priv->ieee->fts = DEFAULT_FTS;
7257 	} else {
7258 		if (wrqu->frag.value < MIN_FRAG_THRESHOLD ||
7259 		    wrqu->frag.value > MAX_FRAG_THRESHOLD)
7260 			return -EINVAL;
7261 
7262 		priv->ieee->fts = wrqu->frag.value & ~0x1;
7263 		priv->frag_threshold = priv->ieee->fts;
7264 	}
7265 
7266 	IPW_DEBUG_WX("SET Frag Threshold -> %d\n", priv->ieee->fts);
7267 
7268 	return 0;
7269 }
7270 
7271 static int ipw2100_wx_get_frag(struct net_device *dev,
7272 			       struct iw_request_info *info,
7273 			       union iwreq_data *wrqu, char *extra)
7274 {
7275 	/*
7276 	 * This can be called at any time.  No action lock required
7277 	 */
7278 
7279 	struct ipw2100_priv *priv = libipw_priv(dev);
7280 	wrqu->frag.value = priv->frag_threshold & ~FRAG_DISABLED;
7281 	wrqu->frag.fixed = 0;	/* no auto select */
7282 	wrqu->frag.disabled = (priv->frag_threshold & FRAG_DISABLED) ? 1 : 0;
7283 
7284 	IPW_DEBUG_WX("GET Frag Threshold -> %d\n", wrqu->frag.value);
7285 
7286 	return 0;
7287 }
7288 
7289 static int ipw2100_wx_set_retry(struct net_device *dev,
7290 				struct iw_request_info *info,
7291 				union iwreq_data *wrqu, char *extra)
7292 {
7293 	struct ipw2100_priv *priv = libipw_priv(dev);
7294 	int err = 0;
7295 
7296 	if (wrqu->retry.flags & IW_RETRY_LIFETIME || wrqu->retry.disabled)
7297 		return -EINVAL;
7298 
7299 	if (!(wrqu->retry.flags & IW_RETRY_LIMIT))
7300 		return 0;
7301 
7302 	mutex_lock(&priv->action_mutex);
7303 	if (!(priv->status & STATUS_INITIALIZED)) {
7304 		err = -EIO;
7305 		goto done;
7306 	}
7307 
7308 	if (wrqu->retry.flags & IW_RETRY_SHORT) {
7309 		err = ipw2100_set_short_retry(priv, wrqu->retry.value);
7310 		IPW_DEBUG_WX("SET Short Retry Limit -> %d\n",
7311 			     wrqu->retry.value);
7312 		goto done;
7313 	}
7314 
7315 	if (wrqu->retry.flags & IW_RETRY_LONG) {
7316 		err = ipw2100_set_long_retry(priv, wrqu->retry.value);
7317 		IPW_DEBUG_WX("SET Long Retry Limit -> %d\n",
7318 			     wrqu->retry.value);
7319 		goto done;
7320 	}
7321 
7322 	err = ipw2100_set_short_retry(priv, wrqu->retry.value);
7323 	if (!err)
7324 		err = ipw2100_set_long_retry(priv, wrqu->retry.value);
7325 
7326 	IPW_DEBUG_WX("SET Both Retry Limits -> %d\n", wrqu->retry.value);
7327 
7328       done:
7329 	mutex_unlock(&priv->action_mutex);
7330 	return err;
7331 }
7332 
7333 static int ipw2100_wx_get_retry(struct net_device *dev,
7334 				struct iw_request_info *info,
7335 				union iwreq_data *wrqu, char *extra)
7336 {
7337 	/*
7338 	 * This can be called at any time.  No action lock required
7339 	 */
7340 
7341 	struct ipw2100_priv *priv = libipw_priv(dev);
7342 
7343 	wrqu->retry.disabled = 0;	/* can't be disabled */
7344 
7345 	if ((wrqu->retry.flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME)
7346 		return -EINVAL;
7347 
7348 	if (wrqu->retry.flags & IW_RETRY_LONG) {
7349 		wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
7350 		wrqu->retry.value = priv->long_retry_limit;
7351 	} else {
7352 		wrqu->retry.flags =
7353 		    (priv->short_retry_limit !=
7354 		     priv->long_retry_limit) ?
7355 		    IW_RETRY_LIMIT | IW_RETRY_SHORT : IW_RETRY_LIMIT;
7356 
7357 		wrqu->retry.value = priv->short_retry_limit;
7358 	}
7359 
7360 	IPW_DEBUG_WX("GET Retry -> %d\n", wrqu->retry.value);
7361 
7362 	return 0;
7363 }
7364 
7365 static int ipw2100_wx_set_scan(struct net_device *dev,
7366 			       struct iw_request_info *info,
7367 			       union iwreq_data *wrqu, char *extra)
7368 {
7369 	struct ipw2100_priv *priv = libipw_priv(dev);
7370 	int err = 0;
7371 
7372 	mutex_lock(&priv->action_mutex);
7373 	if (!(priv->status & STATUS_INITIALIZED)) {
7374 		err = -EIO;
7375 		goto done;
7376 	}
7377 
7378 	IPW_DEBUG_WX("Initiating scan...\n");
7379 
7380 	priv->user_requested_scan = 1;
7381 	if (ipw2100_set_scan_options(priv) || ipw2100_start_scan(priv)) {
7382 		IPW_DEBUG_WX("Start scan failed.\n");
7383 
7384 		/* TODO: Mark a scan as pending so when hardware initialized
7385 		 *       a scan starts */
7386 	}
7387 
7388       done:
7389 	mutex_unlock(&priv->action_mutex);
7390 	return err;
7391 }
7392 
7393 static int ipw2100_wx_get_scan(struct net_device *dev,
7394 			       struct iw_request_info *info,
7395 			       union iwreq_data *wrqu, char *extra)
7396 {
7397 	/*
7398 	 * This can be called at any time.  No action lock required
7399 	 */
7400 
7401 	struct ipw2100_priv *priv = libipw_priv(dev);
7402 	return libipw_wx_get_scan(priv->ieee, info, wrqu, extra);
7403 }
7404 
7405 /*
7406  * Implementation based on code in hostap-driver v0.1.3 hostap_ioctl.c
7407  */
7408 static int ipw2100_wx_set_encode(struct net_device *dev,
7409 				 struct iw_request_info *info,
7410 				 union iwreq_data *wrqu, char *key)
7411 {
7412 	/*
7413 	 * No check of STATUS_INITIALIZED required
7414 	 */
7415 
7416 	struct ipw2100_priv *priv = libipw_priv(dev);
7417 	return libipw_wx_set_encode(priv->ieee, info, wrqu, key);
7418 }
7419 
7420 static int ipw2100_wx_get_encode(struct net_device *dev,
7421 				 struct iw_request_info *info,
7422 				 union iwreq_data *wrqu, char *key)
7423 {
7424 	/*
7425 	 * This can be called at any time.  No action lock required
7426 	 */
7427 
7428 	struct ipw2100_priv *priv = libipw_priv(dev);
7429 	return libipw_wx_get_encode(priv->ieee, info, wrqu, key);
7430 }
7431 
7432 static int ipw2100_wx_set_power(struct net_device *dev,
7433 				struct iw_request_info *info,
7434 				union iwreq_data *wrqu, char *extra)
7435 {
7436 	struct ipw2100_priv *priv = libipw_priv(dev);
7437 	int err = 0;
7438 
7439 	mutex_lock(&priv->action_mutex);
7440 	if (!(priv->status & STATUS_INITIALIZED)) {
7441 		err = -EIO;
7442 		goto done;
7443 	}
7444 
7445 	if (wrqu->power.disabled) {
7446 		priv->power_mode = IPW_POWER_LEVEL(priv->power_mode);
7447 		err = ipw2100_set_power_mode(priv, IPW_POWER_MODE_CAM);
7448 		IPW_DEBUG_WX("SET Power Management Mode -> off\n");
7449 		goto done;
7450 	}
7451 
7452 	switch (wrqu->power.flags & IW_POWER_MODE) {
7453 	case IW_POWER_ON:	/* If not specified */
7454 	case IW_POWER_MODE:	/* If set all mask */
7455 	case IW_POWER_ALL_R:	/* If explicitly state all */
7456 		break;
7457 	default:		/* Otherwise we don't support it */
7458 		IPW_DEBUG_WX("SET PM Mode: %X not supported.\n",
7459 			     wrqu->power.flags);
7460 		err = -EOPNOTSUPP;
7461 		goto done;
7462 	}
7463 
7464 	/* If the user hasn't specified a power management mode yet, default
7465 	 * to BATTERY */
7466 	priv->power_mode = IPW_POWER_ENABLED | priv->power_mode;
7467 	err = ipw2100_set_power_mode(priv, IPW_POWER_LEVEL(priv->power_mode));
7468 
7469 	IPW_DEBUG_WX("SET Power Management Mode -> 0x%02X\n", priv->power_mode);
7470 
7471       done:
7472 	mutex_unlock(&priv->action_mutex);
7473 	return err;
7474 
7475 }
7476 
7477 static int ipw2100_wx_get_power(struct net_device *dev,
7478 				struct iw_request_info *info,
7479 				union iwreq_data *wrqu, char *extra)
7480 {
7481 	/*
7482 	 * This can be called at any time.  No action lock required
7483 	 */
7484 
7485 	struct ipw2100_priv *priv = libipw_priv(dev);
7486 
7487 	if (!(priv->power_mode & IPW_POWER_ENABLED))
7488 		wrqu->power.disabled = 1;
7489 	else {
7490 		wrqu->power.disabled = 0;
7491 		wrqu->power.flags = 0;
7492 	}
7493 
7494 	IPW_DEBUG_WX("GET Power Management Mode -> %02X\n", priv->power_mode);
7495 
7496 	return 0;
7497 }
7498 
7499 /*
7500  * WE-18 WPA support
7501  */
7502 
7503 /* SIOCSIWGENIE */
7504 static int ipw2100_wx_set_genie(struct net_device *dev,
7505 				struct iw_request_info *info,
7506 				union iwreq_data *wrqu, char *extra)
7507 {
7508 
7509 	struct ipw2100_priv *priv = libipw_priv(dev);
7510 	struct libipw_device *ieee = priv->ieee;
7511 	u8 *buf;
7512 
7513 	if (!ieee->wpa_enabled)
7514 		return -EOPNOTSUPP;
7515 
7516 	if (wrqu->data.length > MAX_WPA_IE_LEN ||
7517 	    (wrqu->data.length && extra == NULL))
7518 		return -EINVAL;
7519 
7520 	if (wrqu->data.length) {
7521 		buf = kmemdup(extra, wrqu->data.length, GFP_KERNEL);
7522 		if (buf == NULL)
7523 			return -ENOMEM;
7524 
7525 		kfree(ieee->wpa_ie);
7526 		ieee->wpa_ie = buf;
7527 		ieee->wpa_ie_len = wrqu->data.length;
7528 	} else {
7529 		kfree(ieee->wpa_ie);
7530 		ieee->wpa_ie = NULL;
7531 		ieee->wpa_ie_len = 0;
7532 	}
7533 
7534 	ipw2100_wpa_assoc_frame(priv, ieee->wpa_ie, ieee->wpa_ie_len);
7535 
7536 	return 0;
7537 }
7538 
7539 /* SIOCGIWGENIE */
7540 static int ipw2100_wx_get_genie(struct net_device *dev,
7541 				struct iw_request_info *info,
7542 				union iwreq_data *wrqu, char *extra)
7543 {
7544 	struct ipw2100_priv *priv = libipw_priv(dev);
7545 	struct libipw_device *ieee = priv->ieee;
7546 
7547 	if (ieee->wpa_ie_len == 0 || ieee->wpa_ie == NULL) {
7548 		wrqu->data.length = 0;
7549 		return 0;
7550 	}
7551 
7552 	if (wrqu->data.length < ieee->wpa_ie_len)
7553 		return -E2BIG;
7554 
7555 	wrqu->data.length = ieee->wpa_ie_len;
7556 	memcpy(extra, ieee->wpa_ie, ieee->wpa_ie_len);
7557 
7558 	return 0;
7559 }
7560 
7561 /* SIOCSIWAUTH */
7562 static int ipw2100_wx_set_auth(struct net_device *dev,
7563 			       struct iw_request_info *info,
7564 			       union iwreq_data *wrqu, char *extra)
7565 {
7566 	struct ipw2100_priv *priv = libipw_priv(dev);
7567 	struct libipw_device *ieee = priv->ieee;
7568 	struct iw_param *param = &wrqu->param;
7569 	struct libipw_crypt_data *crypt;
7570 	unsigned long flags;
7571 	int ret = 0;
7572 
7573 	switch (param->flags & IW_AUTH_INDEX) {
7574 	case IW_AUTH_WPA_VERSION:
7575 	case IW_AUTH_CIPHER_PAIRWISE:
7576 	case IW_AUTH_CIPHER_GROUP:
7577 	case IW_AUTH_KEY_MGMT:
7578 		/*
7579 		 * ipw2200 does not use these parameters
7580 		 */
7581 		break;
7582 
7583 	case IW_AUTH_TKIP_COUNTERMEASURES:
7584 		crypt = priv->ieee->crypt_info.crypt[priv->ieee->crypt_info.tx_keyidx];
7585 		if (!crypt || !crypt->ops->set_flags || !crypt->ops->get_flags)
7586 			break;
7587 
7588 		flags = crypt->ops->get_flags(crypt->priv);
7589 
7590 		if (param->value)
7591 			flags |= IEEE80211_CRYPTO_TKIP_COUNTERMEASURES;
7592 		else
7593 			flags &= ~IEEE80211_CRYPTO_TKIP_COUNTERMEASURES;
7594 
7595 		crypt->ops->set_flags(flags, crypt->priv);
7596 
7597 		break;
7598 
7599 	case IW_AUTH_DROP_UNENCRYPTED:{
7600 			/* HACK:
7601 			 *
7602 			 * wpa_supplicant calls set_wpa_enabled when the driver
7603 			 * is loaded and unloaded, regardless of if WPA is being
7604 			 * used.  No other calls are made which can be used to
7605 			 * determine if encryption will be used or not prior to
7606 			 * association being expected.  If encryption is not being
7607 			 * used, drop_unencrypted is set to false, else true -- we
7608 			 * can use this to determine if the CAP_PRIVACY_ON bit should
7609 			 * be set.
7610 			 */
7611 			struct libipw_security sec = {
7612 				.flags = SEC_ENABLED,
7613 				.enabled = param->value,
7614 			};
7615 			priv->ieee->drop_unencrypted = param->value;
7616 			/* We only change SEC_LEVEL for open mode. Others
7617 			 * are set by ipw_wpa_set_encryption.
7618 			 */
7619 			if (!param->value) {
7620 				sec.flags |= SEC_LEVEL;
7621 				sec.level = SEC_LEVEL_0;
7622 			} else {
7623 				sec.flags |= SEC_LEVEL;
7624 				sec.level = SEC_LEVEL_1;
7625 			}
7626 			if (priv->ieee->set_security)
7627 				priv->ieee->set_security(priv->ieee->dev, &sec);
7628 			break;
7629 		}
7630 
7631 	case IW_AUTH_80211_AUTH_ALG:
7632 		ret = ipw2100_wpa_set_auth_algs(priv, param->value);
7633 		break;
7634 
7635 	case IW_AUTH_WPA_ENABLED:
7636 		ret = ipw2100_wpa_enable(priv, param->value);
7637 		break;
7638 
7639 	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
7640 		ieee->ieee802_1x = param->value;
7641 		break;
7642 
7643 		//case IW_AUTH_ROAMING_CONTROL:
7644 	case IW_AUTH_PRIVACY_INVOKED:
7645 		ieee->privacy_invoked = param->value;
7646 		break;
7647 
7648 	default:
7649 		return -EOPNOTSUPP;
7650 	}
7651 	return ret;
7652 }
7653 
7654 /* SIOCGIWAUTH */
7655 static int ipw2100_wx_get_auth(struct net_device *dev,
7656 			       struct iw_request_info *info,
7657 			       union iwreq_data *wrqu, char *extra)
7658 {
7659 	struct ipw2100_priv *priv = libipw_priv(dev);
7660 	struct libipw_device *ieee = priv->ieee;
7661 	struct libipw_crypt_data *crypt;
7662 	struct iw_param *param = &wrqu->param;
7663 
7664 	switch (param->flags & IW_AUTH_INDEX) {
7665 	case IW_AUTH_WPA_VERSION:
7666 	case IW_AUTH_CIPHER_PAIRWISE:
7667 	case IW_AUTH_CIPHER_GROUP:
7668 	case IW_AUTH_KEY_MGMT:
7669 		/*
7670 		 * wpa_supplicant will control these internally
7671 		 */
7672 		break;
7673 
7674 	case IW_AUTH_TKIP_COUNTERMEASURES:
7675 		crypt = priv->ieee->crypt_info.crypt[priv->ieee->crypt_info.tx_keyidx];
7676 		if (!crypt || !crypt->ops->get_flags) {
7677 			IPW_DEBUG_WARNING("Can't get TKIP countermeasures: "
7678 					  "crypt not set!\n");
7679 			break;
7680 		}
7681 
7682 		param->value = (crypt->ops->get_flags(crypt->priv) &
7683 				IEEE80211_CRYPTO_TKIP_COUNTERMEASURES) ? 1 : 0;
7684 
7685 		break;
7686 
7687 	case IW_AUTH_DROP_UNENCRYPTED:
7688 		param->value = ieee->drop_unencrypted;
7689 		break;
7690 
7691 	case IW_AUTH_80211_AUTH_ALG:
7692 		param->value = priv->ieee->sec.auth_mode;
7693 		break;
7694 
7695 	case IW_AUTH_WPA_ENABLED:
7696 		param->value = ieee->wpa_enabled;
7697 		break;
7698 
7699 	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
7700 		param->value = ieee->ieee802_1x;
7701 		break;
7702 
7703 	case IW_AUTH_ROAMING_CONTROL:
7704 	case IW_AUTH_PRIVACY_INVOKED:
7705 		param->value = ieee->privacy_invoked;
7706 		break;
7707 
7708 	default:
7709 		return -EOPNOTSUPP;
7710 	}
7711 	return 0;
7712 }
7713 
7714 /* SIOCSIWENCODEEXT */
7715 static int ipw2100_wx_set_encodeext(struct net_device *dev,
7716 				    struct iw_request_info *info,
7717 				    union iwreq_data *wrqu, char *extra)
7718 {
7719 	struct ipw2100_priv *priv = libipw_priv(dev);
7720 	return libipw_wx_set_encodeext(priv->ieee, info, wrqu, extra);
7721 }
7722 
7723 /* SIOCGIWENCODEEXT */
7724 static int ipw2100_wx_get_encodeext(struct net_device *dev,
7725 				    struct iw_request_info *info,
7726 				    union iwreq_data *wrqu, char *extra)
7727 {
7728 	struct ipw2100_priv *priv = libipw_priv(dev);
7729 	return libipw_wx_get_encodeext(priv->ieee, info, wrqu, extra);
7730 }
7731 
7732 /* SIOCSIWMLME */
7733 static int ipw2100_wx_set_mlme(struct net_device *dev,
7734 			       struct iw_request_info *info,
7735 			       union iwreq_data *wrqu, char *extra)
7736 {
7737 	struct ipw2100_priv *priv = libipw_priv(dev);
7738 	struct iw_mlme *mlme = (struct iw_mlme *)extra;
7739 
7740 	switch (mlme->cmd) {
7741 	case IW_MLME_DEAUTH:
7742 		// silently ignore
7743 		break;
7744 
7745 	case IW_MLME_DISASSOC:
7746 		ipw2100_disassociate_bssid(priv);
7747 		break;
7748 
7749 	default:
7750 		return -EOPNOTSUPP;
7751 	}
7752 	return 0;
7753 }
7754 
7755 /*
7756  *
7757  * IWPRIV handlers
7758  *
7759  */
7760 #ifdef CONFIG_IPW2100_MONITOR
7761 static int ipw2100_wx_set_promisc(struct net_device *dev,
7762 				  struct iw_request_info *info,
7763 				  union iwreq_data *wrqu, char *extra)
7764 {
7765 	struct ipw2100_priv *priv = libipw_priv(dev);
7766 	int *parms = (int *)extra;
7767 	int enable = (parms[0] > 0);
7768 	int err = 0;
7769 
7770 	mutex_lock(&priv->action_mutex);
7771 	if (!(priv->status & STATUS_INITIALIZED)) {
7772 		err = -EIO;
7773 		goto done;
7774 	}
7775 
7776 	if (enable) {
7777 		if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
7778 			err = ipw2100_set_channel(priv, parms[1], 0);
7779 			goto done;
7780 		}
7781 		priv->channel = parms[1];
7782 		err = ipw2100_switch_mode(priv, IW_MODE_MONITOR);
7783 	} else {
7784 		if (priv->ieee->iw_mode == IW_MODE_MONITOR)
7785 			err = ipw2100_switch_mode(priv, priv->last_mode);
7786 	}
7787       done:
7788 	mutex_unlock(&priv->action_mutex);
7789 	return err;
7790 }
7791 
7792 static int ipw2100_wx_reset(struct net_device *dev,
7793 			    struct iw_request_info *info,
7794 			    union iwreq_data *wrqu, char *extra)
7795 {
7796 	struct ipw2100_priv *priv = libipw_priv(dev);
7797 	if (priv->status & STATUS_INITIALIZED)
7798 		schedule_reset(priv);
7799 	return 0;
7800 }
7801 
7802 #endif
7803 
7804 static int ipw2100_wx_set_powermode(struct net_device *dev,
7805 				    struct iw_request_info *info,
7806 				    union iwreq_data *wrqu, char *extra)
7807 {
7808 	struct ipw2100_priv *priv = libipw_priv(dev);
7809 	int err = 0, mode = *(int *)extra;
7810 
7811 	mutex_lock(&priv->action_mutex);
7812 	if (!(priv->status & STATUS_INITIALIZED)) {
7813 		err = -EIO;
7814 		goto done;
7815 	}
7816 
7817 	if ((mode < 0) || (mode > POWER_MODES))
7818 		mode = IPW_POWER_AUTO;
7819 
7820 	if (IPW_POWER_LEVEL(priv->power_mode) != mode)
7821 		err = ipw2100_set_power_mode(priv, mode);
7822       done:
7823 	mutex_unlock(&priv->action_mutex);
7824 	return err;
7825 }
7826 
7827 #define MAX_POWER_STRING 80
7828 static int ipw2100_wx_get_powermode(struct net_device *dev,
7829 				    struct iw_request_info *info,
7830 				    union iwreq_data *wrqu, char *extra)
7831 {
7832 	/*
7833 	 * This can be called at any time.  No action lock required
7834 	 */
7835 
7836 	struct ipw2100_priv *priv = libipw_priv(dev);
7837 	int level = IPW_POWER_LEVEL(priv->power_mode);
7838 	s32 timeout, period;
7839 
7840 	if (!(priv->power_mode & IPW_POWER_ENABLED)) {
7841 		snprintf(extra, MAX_POWER_STRING,
7842 			 "Power save level: %d (Off)", level);
7843 	} else {
7844 		switch (level) {
7845 		case IPW_POWER_MODE_CAM:
7846 			snprintf(extra, MAX_POWER_STRING,
7847 				 "Power save level: %d (None)", level);
7848 			break;
7849 		case IPW_POWER_AUTO:
7850 			snprintf(extra, MAX_POWER_STRING,
7851 				 "Power save level: %d (Auto)", level);
7852 			break;
7853 		default:
7854 			timeout = timeout_duration[level - 1] / 1000;
7855 			period = period_duration[level - 1] / 1000;
7856 			snprintf(extra, MAX_POWER_STRING,
7857 				 "Power save level: %d "
7858 				 "(Timeout %dms, Period %dms)",
7859 				 level, timeout, period);
7860 		}
7861 	}
7862 
7863 	wrqu->data.length = strlen(extra) + 1;
7864 
7865 	return 0;
7866 }
7867 
7868 static int ipw2100_wx_set_preamble(struct net_device *dev,
7869 				   struct iw_request_info *info,
7870 				   union iwreq_data *wrqu, char *extra)
7871 {
7872 	struct ipw2100_priv *priv = libipw_priv(dev);
7873 	int err, mode = *(int *)extra;
7874 
7875 	mutex_lock(&priv->action_mutex);
7876 	if (!(priv->status & STATUS_INITIALIZED)) {
7877 		err = -EIO;
7878 		goto done;
7879 	}
7880 
7881 	if (mode == 1)
7882 		priv->config |= CFG_LONG_PREAMBLE;
7883 	else if (mode == 0)
7884 		priv->config &= ~CFG_LONG_PREAMBLE;
7885 	else {
7886 		err = -EINVAL;
7887 		goto done;
7888 	}
7889 
7890 	err = ipw2100_system_config(priv, 0);
7891 
7892       done:
7893 	mutex_unlock(&priv->action_mutex);
7894 	return err;
7895 }
7896 
7897 static int ipw2100_wx_get_preamble(struct net_device *dev,
7898 				   struct iw_request_info *info,
7899 				   union iwreq_data *wrqu, char *extra)
7900 {
7901 	/*
7902 	 * This can be called at any time.  No action lock required
7903 	 */
7904 
7905 	struct ipw2100_priv *priv = libipw_priv(dev);
7906 
7907 	if (priv->config & CFG_LONG_PREAMBLE)
7908 		snprintf(wrqu->name, IFNAMSIZ, "long (1)");
7909 	else
7910 		snprintf(wrqu->name, IFNAMSIZ, "auto (0)");
7911 
7912 	return 0;
7913 }
7914 
7915 #ifdef CONFIG_IPW2100_MONITOR
7916 static int ipw2100_wx_set_crc_check(struct net_device *dev,
7917 				    struct iw_request_info *info,
7918 				    union iwreq_data *wrqu, char *extra)
7919 {
7920 	struct ipw2100_priv *priv = libipw_priv(dev);
7921 	int err, mode = *(int *)extra;
7922 
7923 	mutex_lock(&priv->action_mutex);
7924 	if (!(priv->status & STATUS_INITIALIZED)) {
7925 		err = -EIO;
7926 		goto done;
7927 	}
7928 
7929 	if (mode == 1)
7930 		priv->config |= CFG_CRC_CHECK;
7931 	else if (mode == 0)
7932 		priv->config &= ~CFG_CRC_CHECK;
7933 	else {
7934 		err = -EINVAL;
7935 		goto done;
7936 	}
7937 	err = 0;
7938 
7939       done:
7940 	mutex_unlock(&priv->action_mutex);
7941 	return err;
7942 }
7943 
7944 static int ipw2100_wx_get_crc_check(struct net_device *dev,
7945 				    struct iw_request_info *info,
7946 				    union iwreq_data *wrqu, char *extra)
7947 {
7948 	/*
7949 	 * This can be called at any time.  No action lock required
7950 	 */
7951 
7952 	struct ipw2100_priv *priv = libipw_priv(dev);
7953 
7954 	if (priv->config & CFG_CRC_CHECK)
7955 		snprintf(wrqu->name, IFNAMSIZ, "CRC checked (1)");
7956 	else
7957 		snprintf(wrqu->name, IFNAMSIZ, "CRC ignored (0)");
7958 
7959 	return 0;
7960 }
7961 #endif				/* CONFIG_IPW2100_MONITOR */
7962 
7963 static iw_handler ipw2100_wx_handlers[] = {
7964 	IW_HANDLER(SIOCGIWNAME, ipw2100_wx_get_name),
7965 	IW_HANDLER(SIOCSIWFREQ, ipw2100_wx_set_freq),
7966 	IW_HANDLER(SIOCGIWFREQ, ipw2100_wx_get_freq),
7967 	IW_HANDLER(SIOCSIWMODE, ipw2100_wx_set_mode),
7968 	IW_HANDLER(SIOCGIWMODE, ipw2100_wx_get_mode),
7969 	IW_HANDLER(SIOCGIWRANGE, ipw2100_wx_get_range),
7970 	IW_HANDLER(SIOCSIWAP, ipw2100_wx_set_wap),
7971 	IW_HANDLER(SIOCGIWAP, ipw2100_wx_get_wap),
7972 	IW_HANDLER(SIOCSIWMLME, ipw2100_wx_set_mlme),
7973 	IW_HANDLER(SIOCSIWSCAN, ipw2100_wx_set_scan),
7974 	IW_HANDLER(SIOCGIWSCAN, ipw2100_wx_get_scan),
7975 	IW_HANDLER(SIOCSIWESSID, ipw2100_wx_set_essid),
7976 	IW_HANDLER(SIOCGIWESSID, ipw2100_wx_get_essid),
7977 	IW_HANDLER(SIOCSIWNICKN, ipw2100_wx_set_nick),
7978 	IW_HANDLER(SIOCGIWNICKN, ipw2100_wx_get_nick),
7979 	IW_HANDLER(SIOCSIWRATE, ipw2100_wx_set_rate),
7980 	IW_HANDLER(SIOCGIWRATE, ipw2100_wx_get_rate),
7981 	IW_HANDLER(SIOCSIWRTS, ipw2100_wx_set_rts),
7982 	IW_HANDLER(SIOCGIWRTS, ipw2100_wx_get_rts),
7983 	IW_HANDLER(SIOCSIWFRAG, ipw2100_wx_set_frag),
7984 	IW_HANDLER(SIOCGIWFRAG, ipw2100_wx_get_frag),
7985 	IW_HANDLER(SIOCSIWTXPOW, ipw2100_wx_set_txpow),
7986 	IW_HANDLER(SIOCGIWTXPOW, ipw2100_wx_get_txpow),
7987 	IW_HANDLER(SIOCSIWRETRY, ipw2100_wx_set_retry),
7988 	IW_HANDLER(SIOCGIWRETRY, ipw2100_wx_get_retry),
7989 	IW_HANDLER(SIOCSIWENCODE, ipw2100_wx_set_encode),
7990 	IW_HANDLER(SIOCGIWENCODE, ipw2100_wx_get_encode),
7991 	IW_HANDLER(SIOCSIWPOWER, ipw2100_wx_set_power),
7992 	IW_HANDLER(SIOCGIWPOWER, ipw2100_wx_get_power),
7993 	IW_HANDLER(SIOCSIWGENIE, ipw2100_wx_set_genie),
7994 	IW_HANDLER(SIOCGIWGENIE, ipw2100_wx_get_genie),
7995 	IW_HANDLER(SIOCSIWAUTH, ipw2100_wx_set_auth),
7996 	IW_HANDLER(SIOCGIWAUTH, ipw2100_wx_get_auth),
7997 	IW_HANDLER(SIOCSIWENCODEEXT, ipw2100_wx_set_encodeext),
7998 	IW_HANDLER(SIOCGIWENCODEEXT, ipw2100_wx_get_encodeext),
7999 };
8000 
8001 #define IPW2100_PRIV_SET_MONITOR	SIOCIWFIRSTPRIV
8002 #define IPW2100_PRIV_RESET		SIOCIWFIRSTPRIV+1
8003 #define IPW2100_PRIV_SET_POWER		SIOCIWFIRSTPRIV+2
8004 #define IPW2100_PRIV_GET_POWER		SIOCIWFIRSTPRIV+3
8005 #define IPW2100_PRIV_SET_LONGPREAMBLE	SIOCIWFIRSTPRIV+4
8006 #define IPW2100_PRIV_GET_LONGPREAMBLE	SIOCIWFIRSTPRIV+5
8007 #define IPW2100_PRIV_SET_CRC_CHECK	SIOCIWFIRSTPRIV+6
8008 #define IPW2100_PRIV_GET_CRC_CHECK	SIOCIWFIRSTPRIV+7
8009 
8010 static const struct iw_priv_args ipw2100_private_args[] = {
8011 
8012 #ifdef CONFIG_IPW2100_MONITOR
8013 	{
8014 	 IPW2100_PRIV_SET_MONITOR,
8015 	 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "monitor"},
8016 	{
8017 	 IPW2100_PRIV_RESET,
8018 	 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 0, 0, "reset"},
8019 #endif				/* CONFIG_IPW2100_MONITOR */
8020 
8021 	{
8022 	 IPW2100_PRIV_SET_POWER,
8023 	 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "set_power"},
8024 	{
8025 	 IPW2100_PRIV_GET_POWER,
8026 	 0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_POWER_STRING,
8027 	 "get_power"},
8028 	{
8029 	 IPW2100_PRIV_SET_LONGPREAMBLE,
8030 	 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "set_preamble"},
8031 	{
8032 	 IPW2100_PRIV_GET_LONGPREAMBLE,
8033 	 0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | IFNAMSIZ, "get_preamble"},
8034 #ifdef CONFIG_IPW2100_MONITOR
8035 	{
8036 	 IPW2100_PRIV_SET_CRC_CHECK,
8037 	 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "set_crc_check"},
8038 	{
8039 	 IPW2100_PRIV_GET_CRC_CHECK,
8040 	 0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | IFNAMSIZ, "get_crc_check"},
8041 #endif				/* CONFIG_IPW2100_MONITOR */
8042 };
8043 
8044 static iw_handler ipw2100_private_handler[] = {
8045 #ifdef CONFIG_IPW2100_MONITOR
8046 	ipw2100_wx_set_promisc,
8047 	ipw2100_wx_reset,
8048 #else				/* CONFIG_IPW2100_MONITOR */
8049 	NULL,
8050 	NULL,
8051 #endif				/* CONFIG_IPW2100_MONITOR */
8052 	ipw2100_wx_set_powermode,
8053 	ipw2100_wx_get_powermode,
8054 	ipw2100_wx_set_preamble,
8055 	ipw2100_wx_get_preamble,
8056 #ifdef CONFIG_IPW2100_MONITOR
8057 	ipw2100_wx_set_crc_check,
8058 	ipw2100_wx_get_crc_check,
8059 #else				/* CONFIG_IPW2100_MONITOR */
8060 	NULL,
8061 	NULL,
8062 #endif				/* CONFIG_IPW2100_MONITOR */
8063 };
8064 
8065 /*
8066  * Get wireless statistics.
8067  * Called by /proc/net/wireless
8068  * Also called by SIOCGIWSTATS
8069  */
8070 static struct iw_statistics *ipw2100_wx_wireless_stats(struct net_device *dev)
8071 {
8072 	enum {
8073 		POOR = 30,
8074 		FAIR = 60,
8075 		GOOD = 80,
8076 		VERY_GOOD = 90,
8077 		EXCELLENT = 95,
8078 		PERFECT = 100
8079 	};
8080 	int rssi_qual;
8081 	int tx_qual;
8082 	int beacon_qual;
8083 	int quality;
8084 
8085 	struct ipw2100_priv *priv = libipw_priv(dev);
8086 	struct iw_statistics *wstats;
8087 	u32 rssi, tx_retries, missed_beacons, tx_failures;
8088 	u32 ord_len = sizeof(u32);
8089 
8090 	if (!priv)
8091 		return (struct iw_statistics *)NULL;
8092 
8093 	wstats = &priv->wstats;
8094 
8095 	/* if hw is disabled, then ipw2100_get_ordinal() can't be called.
8096 	 * ipw2100_wx_wireless_stats seems to be called before fw is
8097 	 * initialized.  STATUS_ASSOCIATED will only be set if the hw is up
8098 	 * and associated; if not associcated, the values are all meaningless
8099 	 * anyway, so set them all to NULL and INVALID */
8100 	if (!(priv->status & STATUS_ASSOCIATED)) {
8101 		wstats->miss.beacon = 0;
8102 		wstats->discard.retries = 0;
8103 		wstats->qual.qual = 0;
8104 		wstats->qual.level = 0;
8105 		wstats->qual.noise = 0;
8106 		wstats->qual.updated = 7;
8107 		wstats->qual.updated |= IW_QUAL_NOISE_INVALID |
8108 		    IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID;
8109 		return wstats;
8110 	}
8111 
8112 	if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_PERCENT_MISSED_BCNS,
8113 				&missed_beacons, &ord_len))
8114 		goto fail_get_ordinal;
8115 
8116 	/* If we don't have a connection the quality and level is 0 */
8117 	if (!(priv->status & STATUS_ASSOCIATED)) {
8118 		wstats->qual.qual = 0;
8119 		wstats->qual.level = 0;
8120 	} else {
8121 		if (ipw2100_get_ordinal(priv, IPW_ORD_RSSI_AVG_CURR,
8122 					&rssi, &ord_len))
8123 			goto fail_get_ordinal;
8124 		wstats->qual.level = rssi + IPW2100_RSSI_TO_DBM;
8125 		if (rssi < 10)
8126 			rssi_qual = rssi * POOR / 10;
8127 		else if (rssi < 15)
8128 			rssi_qual = (rssi - 10) * (FAIR - POOR) / 5 + POOR;
8129 		else if (rssi < 20)
8130 			rssi_qual = (rssi - 15) * (GOOD - FAIR) / 5 + FAIR;
8131 		else if (rssi < 30)
8132 			rssi_qual = (rssi - 20) * (VERY_GOOD - GOOD) /
8133 			    10 + GOOD;
8134 		else
8135 			rssi_qual = (rssi - 30) * (PERFECT - VERY_GOOD) /
8136 			    10 + VERY_GOOD;
8137 
8138 		if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_PERCENT_RETRIES,
8139 					&tx_retries, &ord_len))
8140 			goto fail_get_ordinal;
8141 
8142 		if (tx_retries > 75)
8143 			tx_qual = (90 - tx_retries) * POOR / 15;
8144 		else if (tx_retries > 70)
8145 			tx_qual = (75 - tx_retries) * (FAIR - POOR) / 5 + POOR;
8146 		else if (tx_retries > 65)
8147 			tx_qual = (70 - tx_retries) * (GOOD - FAIR) / 5 + FAIR;
8148 		else if (tx_retries > 50)
8149 			tx_qual = (65 - tx_retries) * (VERY_GOOD - GOOD) /
8150 			    15 + GOOD;
8151 		else
8152 			tx_qual = (50 - tx_retries) *
8153 			    (PERFECT - VERY_GOOD) / 50 + VERY_GOOD;
8154 
8155 		if (missed_beacons > 50)
8156 			beacon_qual = (60 - missed_beacons) * POOR / 10;
8157 		else if (missed_beacons > 40)
8158 			beacon_qual = (50 - missed_beacons) * (FAIR - POOR) /
8159 			    10 + POOR;
8160 		else if (missed_beacons > 32)
8161 			beacon_qual = (40 - missed_beacons) * (GOOD - FAIR) /
8162 			    18 + FAIR;
8163 		else if (missed_beacons > 20)
8164 			beacon_qual = (32 - missed_beacons) *
8165 			    (VERY_GOOD - GOOD) / 20 + GOOD;
8166 		else
8167 			beacon_qual = (20 - missed_beacons) *
8168 			    (PERFECT - VERY_GOOD) / 20 + VERY_GOOD;
8169 
8170 		quality = min(tx_qual, rssi_qual);
8171 		quality = min(beacon_qual, quality);
8172 
8173 #ifdef CONFIG_IPW2100_DEBUG
8174 		if (beacon_qual == quality)
8175 			IPW_DEBUG_WX("Quality clamped by Missed Beacons\n");
8176 		else if (tx_qual == quality)
8177 			IPW_DEBUG_WX("Quality clamped by Tx Retries\n");
8178 		else if (quality != 100)
8179 			IPW_DEBUG_WX("Quality clamped by Signal Strength\n");
8180 		else
8181 			IPW_DEBUG_WX("Quality not clamped.\n");
8182 #endif
8183 
8184 		wstats->qual.qual = quality;
8185 		wstats->qual.level = rssi + IPW2100_RSSI_TO_DBM;
8186 	}
8187 
8188 	wstats->qual.noise = 0;
8189 	wstats->qual.updated = 7;
8190 	wstats->qual.updated |= IW_QUAL_NOISE_INVALID;
8191 
8192 	/* FIXME: this is percent and not a # */
8193 	wstats->miss.beacon = missed_beacons;
8194 
8195 	if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_TX_FAILURES,
8196 				&tx_failures, &ord_len))
8197 		goto fail_get_ordinal;
8198 	wstats->discard.retries = tx_failures;
8199 
8200 	return wstats;
8201 
8202       fail_get_ordinal:
8203 	IPW_DEBUG_WX("failed querying ordinals.\n");
8204 
8205 	return (struct iw_statistics *)NULL;
8206 }
8207 
8208 static const struct iw_handler_def ipw2100_wx_handler_def = {
8209 	.standard = ipw2100_wx_handlers,
8210 	.num_standard = ARRAY_SIZE(ipw2100_wx_handlers),
8211 	.num_private = ARRAY_SIZE(ipw2100_private_handler),
8212 	.num_private_args = ARRAY_SIZE(ipw2100_private_args),
8213 	.private = (iw_handler *) ipw2100_private_handler,
8214 	.private_args = (struct iw_priv_args *)ipw2100_private_args,
8215 	.get_wireless_stats = ipw2100_wx_wireless_stats,
8216 };
8217 
8218 static void ipw2100_wx_event_work(struct work_struct *work)
8219 {
8220 	struct ipw2100_priv *priv =
8221 		container_of(work, struct ipw2100_priv, wx_event_work.work);
8222 	union iwreq_data wrqu;
8223 	unsigned int len = ETH_ALEN;
8224 
8225 	if (priv->status & STATUS_STOPPING)
8226 		return;
8227 
8228 	mutex_lock(&priv->action_mutex);
8229 
8230 	IPW_DEBUG_WX("enter\n");
8231 
8232 	mutex_unlock(&priv->action_mutex);
8233 
8234 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
8235 
8236 	/* Fetch BSSID from the hardware */
8237 	if (!(priv->status & (STATUS_ASSOCIATING | STATUS_ASSOCIATED)) ||
8238 	    priv->status & STATUS_RF_KILL_MASK ||
8239 	    ipw2100_get_ordinal(priv, IPW_ORD_STAT_ASSN_AP_BSSID,
8240 				&priv->bssid, &len)) {
8241 		eth_zero_addr(wrqu.ap_addr.sa_data);
8242 	} else {
8243 		/* We now have the BSSID, so can finish setting to the full
8244 		 * associated state */
8245 		memcpy(wrqu.ap_addr.sa_data, priv->bssid, ETH_ALEN);
8246 		memcpy(priv->ieee->bssid, priv->bssid, ETH_ALEN);
8247 		priv->status &= ~STATUS_ASSOCIATING;
8248 		priv->status |= STATUS_ASSOCIATED;
8249 		netif_carrier_on(priv->net_dev);
8250 		netif_wake_queue(priv->net_dev);
8251 	}
8252 
8253 	if (!(priv->status & STATUS_ASSOCIATED)) {
8254 		IPW_DEBUG_WX("Configuring ESSID\n");
8255 		mutex_lock(&priv->action_mutex);
8256 		/* This is a disassociation event, so kick the firmware to
8257 		 * look for another AP */
8258 		if (priv->config & CFG_STATIC_ESSID)
8259 			ipw2100_set_essid(priv, priv->essid, priv->essid_len,
8260 					  0);
8261 		else
8262 			ipw2100_set_essid(priv, NULL, 0, 0);
8263 		mutex_unlock(&priv->action_mutex);
8264 	}
8265 
8266 	wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
8267 }
8268 
8269 #define IPW2100_FW_MAJOR_VERSION 1
8270 #define IPW2100_FW_MINOR_VERSION 3
8271 
8272 #define IPW2100_FW_MINOR(x) ((x & 0xff) >> 8)
8273 #define IPW2100_FW_MAJOR(x) (x & 0xff)
8274 
8275 #define IPW2100_FW_VERSION ((IPW2100_FW_MINOR_VERSION << 8) | \
8276                              IPW2100_FW_MAJOR_VERSION)
8277 
8278 #define IPW2100_FW_PREFIX "ipw2100-" __stringify(IPW2100_FW_MAJOR_VERSION) \
8279 "." __stringify(IPW2100_FW_MINOR_VERSION)
8280 
8281 #define IPW2100_FW_NAME(x) IPW2100_FW_PREFIX "" x ".fw"
8282 
8283 /*
8284 
8285 BINARY FIRMWARE HEADER FORMAT
8286 
8287 offset      length   desc
8288 0           2        version
8289 2           2        mode == 0:BSS,1:IBSS,2:MONITOR
8290 4           4        fw_len
8291 8           4        uc_len
8292 C           fw_len   firmware data
8293 12 + fw_len uc_len   microcode data
8294 
8295 */
8296 
8297 struct ipw2100_fw_header {
8298 	short version;
8299 	short mode;
8300 	unsigned int fw_size;
8301 	unsigned int uc_size;
8302 } __packed;
8303 
8304 static int ipw2100_mod_firmware_load(struct ipw2100_fw *fw)
8305 {
8306 	struct ipw2100_fw_header *h =
8307 	    (struct ipw2100_fw_header *)fw->fw_entry->data;
8308 
8309 	if (IPW2100_FW_MAJOR(h->version) != IPW2100_FW_MAJOR_VERSION) {
8310 		printk(KERN_WARNING DRV_NAME ": Firmware image not compatible "
8311 		       "(detected version id of %u). "
8312 		       "See Documentation/networking/device_drivers/wifi/intel/ipw2100.rst\n",
8313 		       h->version);
8314 		return 1;
8315 	}
8316 
8317 	fw->version = h->version;
8318 	fw->fw.data = fw->fw_entry->data + sizeof(struct ipw2100_fw_header);
8319 	fw->fw.size = h->fw_size;
8320 	fw->uc.data = fw->fw.data + h->fw_size;
8321 	fw->uc.size = h->uc_size;
8322 
8323 	return 0;
8324 }
8325 
8326 static int ipw2100_get_firmware(struct ipw2100_priv *priv,
8327 				struct ipw2100_fw *fw)
8328 {
8329 	char *fw_name;
8330 	int rc;
8331 
8332 	IPW_DEBUG_INFO("%s: Using hotplug firmware load.\n",
8333 		       priv->net_dev->name);
8334 
8335 	switch (priv->ieee->iw_mode) {
8336 	case IW_MODE_ADHOC:
8337 		fw_name = IPW2100_FW_NAME("-i");
8338 		break;
8339 #ifdef CONFIG_IPW2100_MONITOR
8340 	case IW_MODE_MONITOR:
8341 		fw_name = IPW2100_FW_NAME("-p");
8342 		break;
8343 #endif
8344 	case IW_MODE_INFRA:
8345 	default:
8346 		fw_name = IPW2100_FW_NAME("");
8347 		break;
8348 	}
8349 
8350 	rc = request_firmware(&fw->fw_entry, fw_name, &priv->pci_dev->dev);
8351 
8352 	if (rc < 0) {
8353 		printk(KERN_ERR DRV_NAME ": "
8354 		       "%s: Firmware '%s' not available or load failed.\n",
8355 		       priv->net_dev->name, fw_name);
8356 		return rc;
8357 	}
8358 	IPW_DEBUG_INFO("firmware data %p size %zd\n", fw->fw_entry->data,
8359 		       fw->fw_entry->size);
8360 
8361 	ipw2100_mod_firmware_load(fw);
8362 
8363 	return 0;
8364 }
8365 
8366 MODULE_FIRMWARE(IPW2100_FW_NAME("-i"));
8367 #ifdef CONFIG_IPW2100_MONITOR
8368 MODULE_FIRMWARE(IPW2100_FW_NAME("-p"));
8369 #endif
8370 MODULE_FIRMWARE(IPW2100_FW_NAME(""));
8371 
8372 static void ipw2100_release_firmware(struct ipw2100_priv *priv,
8373 				     struct ipw2100_fw *fw)
8374 {
8375 	fw->version = 0;
8376 	release_firmware(fw->fw_entry);
8377 	fw->fw_entry = NULL;
8378 }
8379 
8380 static int ipw2100_get_fwversion(struct ipw2100_priv *priv, char *buf,
8381 				 size_t max)
8382 {
8383 	char ver[MAX_FW_VERSION_LEN];
8384 	u32 len = MAX_FW_VERSION_LEN;
8385 	u32 tmp;
8386 	int i;
8387 	/* firmware version is an ascii string (max len of 14) */
8388 	if (ipw2100_get_ordinal(priv, IPW_ORD_STAT_FW_VER_NUM, ver, &len))
8389 		return -EIO;
8390 	tmp = max;
8391 	if (len >= max)
8392 		len = max - 1;
8393 	for (i = 0; i < len; i++)
8394 		buf[i] = ver[i];
8395 	buf[i] = '\0';
8396 	return tmp;
8397 }
8398 
8399 /*
8400  * On exit, the firmware will have been freed from the fw list
8401  */
8402 static int ipw2100_fw_download(struct ipw2100_priv *priv, struct ipw2100_fw *fw)
8403 {
8404 	/* firmware is constructed of N contiguous entries, each entry is
8405 	 * structured as:
8406 	 *
8407 	 * offset    sie         desc
8408 	 * 0         4           address to write to
8409 	 * 4         2           length of data run
8410 	 * 6         length      data
8411 	 */
8412 	unsigned int addr;
8413 	unsigned short len;
8414 
8415 	const unsigned char *firmware_data = fw->fw.data;
8416 	unsigned int firmware_data_left = fw->fw.size;
8417 
8418 	while (firmware_data_left > 0) {
8419 		addr = *(u32 *) (firmware_data);
8420 		firmware_data += 4;
8421 		firmware_data_left -= 4;
8422 
8423 		len = *(u16 *) (firmware_data);
8424 		firmware_data += 2;
8425 		firmware_data_left -= 2;
8426 
8427 		if (len > 32) {
8428 			printk(KERN_ERR DRV_NAME ": "
8429 			       "Invalid firmware run-length of %d bytes\n",
8430 			       len);
8431 			return -EINVAL;
8432 		}
8433 
8434 		write_nic_memory(priv->net_dev, addr, len, firmware_data);
8435 		firmware_data += len;
8436 		firmware_data_left -= len;
8437 	}
8438 
8439 	return 0;
8440 }
8441 
8442 struct symbol_alive_response {
8443 	u8 cmd_id;
8444 	u8 seq_num;
8445 	u8 ucode_rev;
8446 	u8 eeprom_valid;
8447 	u16 valid_flags;
8448 	u8 IEEE_addr[6];
8449 	u16 flags;
8450 	u16 pcb_rev;
8451 	u16 clock_settle_time;	// 1us LSB
8452 	u16 powerup_settle_time;	// 1us LSB
8453 	u16 hop_settle_time;	// 1us LSB
8454 	u8 date[3];		// month, day, year
8455 	u8 time[2];		// hours, minutes
8456 	u8 ucode_valid;
8457 };
8458 
8459 static int ipw2100_ucode_download(struct ipw2100_priv *priv,
8460 				  struct ipw2100_fw *fw)
8461 {
8462 	struct net_device *dev = priv->net_dev;
8463 	const unsigned char *microcode_data = fw->uc.data;
8464 	unsigned int microcode_data_left = fw->uc.size;
8465 	void __iomem *reg = priv->ioaddr;
8466 
8467 	struct symbol_alive_response response;
8468 	int i, j;
8469 	u8 data;
8470 
8471 	/* Symbol control */
8472 	write_nic_word(dev, IPW2100_CONTROL_REG, 0x703);
8473 	readl(reg);
8474 	write_nic_word(dev, IPW2100_CONTROL_REG, 0x707);
8475 	readl(reg);
8476 
8477 	/* HW config */
8478 	write_nic_byte(dev, 0x210014, 0x72);	/* fifo width =16 */
8479 	readl(reg);
8480 	write_nic_byte(dev, 0x210014, 0x72);	/* fifo width =16 */
8481 	readl(reg);
8482 
8483 	/* EN_CS_ACCESS bit to reset control store pointer */
8484 	write_nic_byte(dev, 0x210000, 0x40);
8485 	readl(reg);
8486 	write_nic_byte(dev, 0x210000, 0x0);
8487 	readl(reg);
8488 	write_nic_byte(dev, 0x210000, 0x40);
8489 	readl(reg);
8490 
8491 	/* copy microcode from buffer into Symbol */
8492 
8493 	while (microcode_data_left > 0) {
8494 		write_nic_byte(dev, 0x210010, *microcode_data++);
8495 		write_nic_byte(dev, 0x210010, *microcode_data++);
8496 		microcode_data_left -= 2;
8497 	}
8498 
8499 	/* EN_CS_ACCESS bit to reset the control store pointer */
8500 	write_nic_byte(dev, 0x210000, 0x0);
8501 	readl(reg);
8502 
8503 	/* Enable System (Reg 0)
8504 	 * first enable causes garbage in RX FIFO */
8505 	write_nic_byte(dev, 0x210000, 0x0);
8506 	readl(reg);
8507 	write_nic_byte(dev, 0x210000, 0x80);
8508 	readl(reg);
8509 
8510 	/* Reset External Baseband Reg */
8511 	write_nic_word(dev, IPW2100_CONTROL_REG, 0x703);
8512 	readl(reg);
8513 	write_nic_word(dev, IPW2100_CONTROL_REG, 0x707);
8514 	readl(reg);
8515 
8516 	/* HW Config (Reg 5) */
8517 	write_nic_byte(dev, 0x210014, 0x72);	// fifo width =16
8518 	readl(reg);
8519 	write_nic_byte(dev, 0x210014, 0x72);	// fifo width =16
8520 	readl(reg);
8521 
8522 	/* Enable System (Reg 0)
8523 	 * second enable should be OK */
8524 	write_nic_byte(dev, 0x210000, 0x00);	// clear enable system
8525 	readl(reg);
8526 	write_nic_byte(dev, 0x210000, 0x80);	// set enable system
8527 
8528 	/* check Symbol is enabled - upped this from 5 as it wasn't always
8529 	 * catching the update */
8530 	for (i = 0; i < 10; i++) {
8531 		udelay(10);
8532 
8533 		/* check Dino is enabled bit */
8534 		read_nic_byte(dev, 0x210000, &data);
8535 		if (data & 0x1)
8536 			break;
8537 	}
8538 
8539 	if (i == 10) {
8540 		printk(KERN_ERR DRV_NAME ": %s: Error initializing Symbol\n",
8541 		       dev->name);
8542 		return -EIO;
8543 	}
8544 
8545 	/* Get Symbol alive response */
8546 	for (i = 0; i < 30; i++) {
8547 		/* Read alive response structure */
8548 		for (j = 0;
8549 		     j < (sizeof(struct symbol_alive_response) >> 1); j++)
8550 			read_nic_word(dev, 0x210004, ((u16 *) & response) + j);
8551 
8552 		if ((response.cmd_id == 1) && (response.ucode_valid == 0x1))
8553 			break;
8554 		udelay(10);
8555 	}
8556 
8557 	if (i == 30) {
8558 		printk(KERN_ERR DRV_NAME
8559 		       ": %s: No response from Symbol - hw not alive\n",
8560 		       dev->name);
8561 		printk_buf(IPW_DL_ERROR, (u8 *) & response, sizeof(response));
8562 		return -EIO;
8563 	}
8564 
8565 	return 0;
8566 }
8567