xref: /linux/drivers/net/wireless/intel/ipw2x00/ipw2200.c (revision 50501936288d6a29d7ef78f25d00e33240fad45f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 
4   Copyright(c) 2003 - 2006 Intel Corporation. All rights reserved.
5 
6   802.11 status code portion of this file from ethereal-0.10.6:
7     Copyright 2000, Axis Communications AB
8     Ethereal - Network traffic analyzer
9     By Gerald Combs <gerald@ethereal.com>
10     Copyright 1998 Gerald Combs
11 
12 
13   Contact Information:
14   Intel Linux Wireless <ilw@linux.intel.com>
15   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
16 
17 ******************************************************************************/
18 
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <net/cfg80211-wext.h>
22 #include "ipw2200.h"
23 #include "ipw.h"
24 
25 
26 #ifndef KBUILD_EXTMOD
27 #define VK "k"
28 #else
29 #define VK
30 #endif
31 
32 #ifdef CONFIG_IPW2200_DEBUG
33 #define VD "d"
34 #else
35 #define VD
36 #endif
37 
38 #ifdef CONFIG_IPW2200_MONITOR
39 #define VM "m"
40 #else
41 #define VM
42 #endif
43 
44 #ifdef CONFIG_IPW2200_PROMISCUOUS
45 #define VP "p"
46 #else
47 #define VP
48 #endif
49 
50 #ifdef CONFIG_IPW2200_RADIOTAP
51 #define VR "r"
52 #else
53 #define VR
54 #endif
55 
56 #ifdef CONFIG_IPW2200_QOS
57 #define VQ "q"
58 #else
59 #define VQ
60 #endif
61 
62 #define IPW2200_VERSION "1.2.2" VK VD VM VP VR VQ
63 #define DRV_DESCRIPTION	"Intel(R) PRO/Wireless 2200/2915 Network Driver"
64 #define DRV_COPYRIGHT	"Copyright(c) 2003-2006 Intel Corporation"
65 #define DRV_VERSION     IPW2200_VERSION
66 
67 #define ETH_P_80211_STATS (ETH_P_80211_RAW + 1)
68 
69 MODULE_DESCRIPTION(DRV_DESCRIPTION);
70 MODULE_VERSION(DRV_VERSION);
71 MODULE_AUTHOR(DRV_COPYRIGHT);
72 MODULE_LICENSE("GPL");
73 MODULE_FIRMWARE("ipw2200-ibss.fw");
74 #ifdef CONFIG_IPW2200_MONITOR
75 MODULE_FIRMWARE("ipw2200-sniffer.fw");
76 #endif
77 MODULE_FIRMWARE("ipw2200-bss.fw");
78 
79 static int cmdlog = 0;
80 static int debug = 0;
81 static int default_channel = 0;
82 static int network_mode = 0;
83 
84 static u32 ipw_debug_level;
85 static int associate;
86 static int auto_create = 1;
87 static int led_support = 1;
88 static int disable = 0;
89 static int bt_coexist = 0;
90 static int hwcrypto = 0;
91 static int roaming = 1;
92 static const char ipw_modes[] = {
93 	'a', 'b', 'g', '?'
94 };
95 static int antenna = CFG_SYS_ANTENNA_BOTH;
96 
97 #ifdef CONFIG_IPW2200_PROMISCUOUS
98 static int rtap_iface = 0;     /* def: 0 -- do not create rtap interface */
99 #endif
100 
101 static struct ieee80211_rate ipw2200_rates[] = {
102 	{ .bitrate = 10 },
103 	{ .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
104 	{ .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
105 	{ .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
106 	{ .bitrate = 60 },
107 	{ .bitrate = 90 },
108 	{ .bitrate = 120 },
109 	{ .bitrate = 180 },
110 	{ .bitrate = 240 },
111 	{ .bitrate = 360 },
112 	{ .bitrate = 480 },
113 	{ .bitrate = 540 }
114 };
115 
116 #define ipw2200_a_rates		(ipw2200_rates + 4)
117 #define ipw2200_num_a_rates	8
118 #define ipw2200_bg_rates	(ipw2200_rates + 0)
119 #define ipw2200_num_bg_rates	12
120 
121 /* Ugly macro to convert literal channel numbers into their mhz equivalents
122  * There are certianly some conditions that will break this (like feeding it '30')
123  * but they shouldn't arise since nothing talks on channel 30. */
124 #define ieee80211chan2mhz(x) \
125 	(((x) <= 14) ? \
126 	(((x) == 14) ? 2484 : ((x) * 5) + 2407) : \
127 	((x) + 1000) * 5)
128 
129 #ifdef CONFIG_IPW2200_QOS
130 static int qos_enable = 0;
131 static int qos_burst_enable = 0;
132 static int qos_no_ack_mask = 0;
133 static int burst_duration_CCK = 0;
134 static int burst_duration_OFDM = 0;
135 
136 static struct libipw_qos_parameters def_qos_parameters_OFDM = {
137 	{QOS_TX0_CW_MIN_OFDM, QOS_TX1_CW_MIN_OFDM, QOS_TX2_CW_MIN_OFDM,
138 	 QOS_TX3_CW_MIN_OFDM},
139 	{QOS_TX0_CW_MAX_OFDM, QOS_TX1_CW_MAX_OFDM, QOS_TX2_CW_MAX_OFDM,
140 	 QOS_TX3_CW_MAX_OFDM},
141 	{QOS_TX0_AIFS, QOS_TX1_AIFS, QOS_TX2_AIFS, QOS_TX3_AIFS},
142 	{QOS_TX0_ACM, QOS_TX1_ACM, QOS_TX2_ACM, QOS_TX3_ACM},
143 	{QOS_TX0_TXOP_LIMIT_OFDM, QOS_TX1_TXOP_LIMIT_OFDM,
144 	 QOS_TX2_TXOP_LIMIT_OFDM, QOS_TX3_TXOP_LIMIT_OFDM}
145 };
146 
147 static struct libipw_qos_parameters def_qos_parameters_CCK = {
148 	{QOS_TX0_CW_MIN_CCK, QOS_TX1_CW_MIN_CCK, QOS_TX2_CW_MIN_CCK,
149 	 QOS_TX3_CW_MIN_CCK},
150 	{QOS_TX0_CW_MAX_CCK, QOS_TX1_CW_MAX_CCK, QOS_TX2_CW_MAX_CCK,
151 	 QOS_TX3_CW_MAX_CCK},
152 	{QOS_TX0_AIFS, QOS_TX1_AIFS, QOS_TX2_AIFS, QOS_TX3_AIFS},
153 	{QOS_TX0_ACM, QOS_TX1_ACM, QOS_TX2_ACM, QOS_TX3_ACM},
154 	{QOS_TX0_TXOP_LIMIT_CCK, QOS_TX1_TXOP_LIMIT_CCK, QOS_TX2_TXOP_LIMIT_CCK,
155 	 QOS_TX3_TXOP_LIMIT_CCK}
156 };
157 
158 static struct libipw_qos_parameters def_parameters_OFDM = {
159 	{DEF_TX0_CW_MIN_OFDM, DEF_TX1_CW_MIN_OFDM, DEF_TX2_CW_MIN_OFDM,
160 	 DEF_TX3_CW_MIN_OFDM},
161 	{DEF_TX0_CW_MAX_OFDM, DEF_TX1_CW_MAX_OFDM, DEF_TX2_CW_MAX_OFDM,
162 	 DEF_TX3_CW_MAX_OFDM},
163 	{DEF_TX0_AIFS, DEF_TX1_AIFS, DEF_TX2_AIFS, DEF_TX3_AIFS},
164 	{DEF_TX0_ACM, DEF_TX1_ACM, DEF_TX2_ACM, DEF_TX3_ACM},
165 	{DEF_TX0_TXOP_LIMIT_OFDM, DEF_TX1_TXOP_LIMIT_OFDM,
166 	 DEF_TX2_TXOP_LIMIT_OFDM, DEF_TX3_TXOP_LIMIT_OFDM}
167 };
168 
169 static struct libipw_qos_parameters def_parameters_CCK = {
170 	{DEF_TX0_CW_MIN_CCK, DEF_TX1_CW_MIN_CCK, DEF_TX2_CW_MIN_CCK,
171 	 DEF_TX3_CW_MIN_CCK},
172 	{DEF_TX0_CW_MAX_CCK, DEF_TX1_CW_MAX_CCK, DEF_TX2_CW_MAX_CCK,
173 	 DEF_TX3_CW_MAX_CCK},
174 	{DEF_TX0_AIFS, DEF_TX1_AIFS, DEF_TX2_AIFS, DEF_TX3_AIFS},
175 	{DEF_TX0_ACM, DEF_TX1_ACM, DEF_TX2_ACM, DEF_TX3_ACM},
176 	{DEF_TX0_TXOP_LIMIT_CCK, DEF_TX1_TXOP_LIMIT_CCK, DEF_TX2_TXOP_LIMIT_CCK,
177 	 DEF_TX3_TXOP_LIMIT_CCK}
178 };
179 
180 static u8 qos_oui[QOS_OUI_LEN] = { 0x00, 0x50, 0xF2 };
181 
182 static int from_priority_to_tx_queue[] = {
183 	IPW_TX_QUEUE_1, IPW_TX_QUEUE_2, IPW_TX_QUEUE_2, IPW_TX_QUEUE_1,
184 	IPW_TX_QUEUE_3, IPW_TX_QUEUE_3, IPW_TX_QUEUE_4, IPW_TX_QUEUE_4
185 };
186 
187 static u32 ipw_qos_get_burst_duration(struct ipw_priv *priv);
188 
189 static int ipw_send_qos_params_command(struct ipw_priv *priv, struct libipw_qos_parameters
190 				       *qos_param);
191 static int ipw_send_qos_info_command(struct ipw_priv *priv, struct libipw_qos_information_element
192 				     *qos_param);
193 #endif				/* CONFIG_IPW2200_QOS */
194 
195 static struct iw_statistics *ipw_get_wireless_stats(struct net_device *dev);
196 static void ipw_remove_current_network(struct ipw_priv *priv);
197 static void ipw_rx(struct ipw_priv *priv);
198 static int ipw_queue_tx_reclaim(struct ipw_priv *priv,
199 				struct clx2_tx_queue *txq, int qindex);
200 static int ipw_queue_reset(struct ipw_priv *priv);
201 
202 static int ipw_queue_tx_hcmd(struct ipw_priv *priv, int hcmd, const void *buf,
203 			     int len, int sync);
204 
205 static void ipw_tx_queue_free(struct ipw_priv *);
206 
207 static struct ipw_rx_queue *ipw_rx_queue_alloc(struct ipw_priv *);
208 static void ipw_rx_queue_free(struct ipw_priv *, struct ipw_rx_queue *);
209 static void ipw_rx_queue_replenish(void *);
210 static int ipw_up(struct ipw_priv *);
211 static void ipw_bg_up(struct work_struct *work);
212 static void ipw_down(struct ipw_priv *);
213 static void ipw_bg_down(struct work_struct *work);
214 static int ipw_config(struct ipw_priv *);
215 static int init_supported_rates(struct ipw_priv *priv,
216 				struct ipw_supported_rates *prates);
217 static void ipw_set_hwcrypto_keys(struct ipw_priv *);
218 static void ipw_send_wep_keys(struct ipw_priv *, int);
219 
220 static int snprint_line(char *buf, size_t count,
221 			const u8 * data, u32 len, u32 ofs)
222 {
223 	int out, i, j, l;
224 	char c;
225 
226 	out = scnprintf(buf, count, "%08X", ofs);
227 
228 	for (l = 0, i = 0; i < 2; i++) {
229 		out += scnprintf(buf + out, count - out, " ");
230 		for (j = 0; j < 8 && l < len; j++, l++)
231 			out += scnprintf(buf + out, count - out, "%02X ",
232 					data[(i * 8 + j)]);
233 		for (; j < 8; j++)
234 			out += scnprintf(buf + out, count - out, "   ");
235 	}
236 
237 	out += scnprintf(buf + out, count - out, " ");
238 	for (l = 0, i = 0; i < 2; i++) {
239 		out += scnprintf(buf + out, count - out, " ");
240 		for (j = 0; j < 8 && l < len; j++, l++) {
241 			c = data[(i * 8 + j)];
242 			if (!isascii(c) || !isprint(c))
243 				c = '.';
244 
245 			out += scnprintf(buf + out, count - out, "%c", c);
246 		}
247 
248 		for (; j < 8; j++)
249 			out += scnprintf(buf + out, count - out, " ");
250 	}
251 
252 	return out;
253 }
254 
255 static void printk_buf(int level, const u8 * data, u32 len)
256 {
257 	char line[81];
258 	u32 ofs = 0;
259 	if (!(ipw_debug_level & level))
260 		return;
261 
262 	while (len) {
263 		snprint_line(line, sizeof(line), &data[ofs],
264 			     min(len, 16U), ofs);
265 		printk(KERN_DEBUG "%s\n", line);
266 		ofs += 16;
267 		len -= min(len, 16U);
268 	}
269 }
270 
271 static int snprintk_buf(u8 * output, size_t size, const u8 * data, size_t len)
272 {
273 	size_t out = size;
274 	u32 ofs = 0;
275 	int total = 0;
276 
277 	while (size && len) {
278 		out = snprint_line(output, size, &data[ofs],
279 				   min_t(size_t, len, 16U), ofs);
280 
281 		ofs += 16;
282 		output += out;
283 		size -= out;
284 		len -= min_t(size_t, len, 16U);
285 		total += out;
286 	}
287 	return total;
288 }
289 
290 /* alias for 32-bit indirect read (for SRAM/reg above 4K), with debug wrapper */
291 static u32 _ipw_read_reg32(struct ipw_priv *priv, u32 reg);
292 #define ipw_read_reg32(a, b) _ipw_read_reg32(a, b)
293 
294 /* alias for 8-bit indirect read (for SRAM/reg above 4K), with debug wrapper */
295 static u8 _ipw_read_reg8(struct ipw_priv *ipw, u32 reg);
296 #define ipw_read_reg8(a, b) _ipw_read_reg8(a, b)
297 
298 /* 8-bit indirect write (for SRAM/reg above 4K), with debug wrapper */
299 static void _ipw_write_reg8(struct ipw_priv *priv, u32 reg, u8 value);
300 static inline void ipw_write_reg8(struct ipw_priv *a, u32 b, u8 c)
301 {
302 	IPW_DEBUG_IO("%s %d: write_indirect8(0x%08X, 0x%08X)\n", __FILE__,
303 		     __LINE__, (u32) (b), (u32) (c));
304 	_ipw_write_reg8(a, b, c);
305 }
306 
307 /* 16-bit indirect write (for SRAM/reg above 4K), with debug wrapper */
308 static void _ipw_write_reg16(struct ipw_priv *priv, u32 reg, u16 value);
309 static inline void ipw_write_reg16(struct ipw_priv *a, u32 b, u16 c)
310 {
311 	IPW_DEBUG_IO("%s %d: write_indirect16(0x%08X, 0x%08X)\n", __FILE__,
312 		     __LINE__, (u32) (b), (u32) (c));
313 	_ipw_write_reg16(a, b, c);
314 }
315 
316 /* 32-bit indirect write (for SRAM/reg above 4K), with debug wrapper */
317 static void _ipw_write_reg32(struct ipw_priv *priv, u32 reg, u32 value);
318 static inline void ipw_write_reg32(struct ipw_priv *a, u32 b, u32 c)
319 {
320 	IPW_DEBUG_IO("%s %d: write_indirect32(0x%08X, 0x%08X)\n", __FILE__,
321 		     __LINE__, (u32) (b), (u32) (c));
322 	_ipw_write_reg32(a, b, c);
323 }
324 
325 /* 8-bit direct write (low 4K) */
326 static inline void _ipw_write8(struct ipw_priv *ipw, unsigned long ofs,
327 		u8 val)
328 {
329 	writeb(val, ipw->hw_base + ofs);
330 }
331 
332 /* 8-bit direct write (for low 4K of SRAM/regs), with debug wrapper */
333 #define ipw_write8(ipw, ofs, val) do { \
334 	IPW_DEBUG_IO("%s %d: write_direct8(0x%08X, 0x%08X)\n", __FILE__, \
335 			__LINE__, (u32)(ofs), (u32)(val)); \
336 	_ipw_write8(ipw, ofs, val); \
337 } while (0)
338 
339 /* 16-bit direct write (low 4K) */
340 static inline void _ipw_write16(struct ipw_priv *ipw, unsigned long ofs,
341 		u16 val)
342 {
343 	writew(val, ipw->hw_base + ofs);
344 }
345 
346 /* 16-bit direct write (for low 4K of SRAM/regs), with debug wrapper */
347 #define ipw_write16(ipw, ofs, val) do { \
348 	IPW_DEBUG_IO("%s %d: write_direct16(0x%08X, 0x%08X)\n", __FILE__, \
349 			__LINE__, (u32)(ofs), (u32)(val)); \
350 	_ipw_write16(ipw, ofs, val); \
351 } while (0)
352 
353 /* 32-bit direct write (low 4K) */
354 static inline void _ipw_write32(struct ipw_priv *ipw, unsigned long ofs,
355 		u32 val)
356 {
357 	writel(val, ipw->hw_base + ofs);
358 }
359 
360 /* 32-bit direct write (for low 4K of SRAM/regs), with debug wrapper */
361 #define ipw_write32(ipw, ofs, val) do { \
362 	IPW_DEBUG_IO("%s %d: write_direct32(0x%08X, 0x%08X)\n", __FILE__, \
363 			__LINE__, (u32)(ofs), (u32)(val)); \
364 	_ipw_write32(ipw, ofs, val); \
365 } while (0)
366 
367 /* 8-bit direct read (low 4K) */
368 static inline u8 _ipw_read8(struct ipw_priv *ipw, unsigned long ofs)
369 {
370 	return readb(ipw->hw_base + ofs);
371 }
372 
373 /* alias to 8-bit direct read (low 4K of SRAM/regs), with debug wrapper */
374 #define ipw_read8(ipw, ofs) ({ \
375 	IPW_DEBUG_IO("%s %d: read_direct8(0x%08X)\n", __FILE__, __LINE__, \
376 			(u32)(ofs)); \
377 	_ipw_read8(ipw, ofs); \
378 })
379 
380 /* 32-bit direct read (low 4K) */
381 static inline u32 _ipw_read32(struct ipw_priv *ipw, unsigned long ofs)
382 {
383 	return readl(ipw->hw_base + ofs);
384 }
385 
386 /* alias to 32-bit direct read (low 4K of SRAM/regs), with debug wrapper */
387 #define ipw_read32(ipw, ofs) ({ \
388 	IPW_DEBUG_IO("%s %d: read_direct32(0x%08X)\n", __FILE__, __LINE__, \
389 			(u32)(ofs)); \
390 	_ipw_read32(ipw, ofs); \
391 })
392 
393 static void _ipw_read_indirect(struct ipw_priv *, u32, u8 *, int);
394 /* alias to multi-byte read (SRAM/regs above 4K), with debug wrapper */
395 #define ipw_read_indirect(a, b, c, d) ({ \
396 	IPW_DEBUG_IO("%s %d: read_indirect(0x%08X) %u bytes\n", __FILE__, \
397 			__LINE__, (u32)(b), (u32)(d)); \
398 	_ipw_read_indirect(a, b, c, d); \
399 })
400 
401 /* alias to multi-byte read (SRAM/regs above 4K), with debug wrapper */
402 static void _ipw_write_indirect(struct ipw_priv *priv, u32 addr, u8 * data,
403 				int num);
404 #define ipw_write_indirect(a, b, c, d) do { \
405 	IPW_DEBUG_IO("%s %d: write_indirect(0x%08X) %u bytes\n", __FILE__, \
406 			__LINE__, (u32)(b), (u32)(d)); \
407 	_ipw_write_indirect(a, b, c, d); \
408 } while (0)
409 
410 /* 32-bit indirect write (above 4K) */
411 static void _ipw_write_reg32(struct ipw_priv *priv, u32 reg, u32 value)
412 {
413 	IPW_DEBUG_IO(" %p : reg = 0x%8X : value = 0x%8X\n", priv, reg, value);
414 	_ipw_write32(priv, IPW_INDIRECT_ADDR, reg);
415 	_ipw_write32(priv, IPW_INDIRECT_DATA, value);
416 }
417 
418 /* 8-bit indirect write (above 4K) */
419 static void _ipw_write_reg8(struct ipw_priv *priv, u32 reg, u8 value)
420 {
421 	u32 aligned_addr = reg & IPW_INDIRECT_ADDR_MASK;	/* dword align */
422 	u32 dif_len = reg - aligned_addr;
423 
424 	IPW_DEBUG_IO(" reg = 0x%8X : value = 0x%8X\n", reg, value);
425 	_ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
426 	_ipw_write8(priv, IPW_INDIRECT_DATA + dif_len, value);
427 }
428 
429 /* 16-bit indirect write (above 4K) */
430 static void _ipw_write_reg16(struct ipw_priv *priv, u32 reg, u16 value)
431 {
432 	u32 aligned_addr = reg & IPW_INDIRECT_ADDR_MASK;	/* dword align */
433 	u32 dif_len = (reg - aligned_addr) & (~0x1ul);
434 
435 	IPW_DEBUG_IO(" reg = 0x%8X : value = 0x%8X\n", reg, value);
436 	_ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
437 	_ipw_write16(priv, IPW_INDIRECT_DATA + dif_len, value);
438 }
439 
440 /* 8-bit indirect read (above 4K) */
441 static u8 _ipw_read_reg8(struct ipw_priv *priv, u32 reg)
442 {
443 	u32 word;
444 	_ipw_write32(priv, IPW_INDIRECT_ADDR, reg & IPW_INDIRECT_ADDR_MASK);
445 	IPW_DEBUG_IO(" reg = 0x%8X :\n", reg);
446 	word = _ipw_read32(priv, IPW_INDIRECT_DATA);
447 	return (word >> ((reg & 0x3) * 8)) & 0xff;
448 }
449 
450 /* 32-bit indirect read (above 4K) */
451 static u32 _ipw_read_reg32(struct ipw_priv *priv, u32 reg)
452 {
453 	u32 value;
454 
455 	IPW_DEBUG_IO("%p : reg = 0x%08x\n", priv, reg);
456 
457 	_ipw_write32(priv, IPW_INDIRECT_ADDR, reg);
458 	value = _ipw_read32(priv, IPW_INDIRECT_DATA);
459 	IPW_DEBUG_IO(" reg = 0x%4X : value = 0x%4x\n", reg, value);
460 	return value;
461 }
462 
463 /* General purpose, no alignment requirement, iterative (multi-byte) read, */
464 /*    for area above 1st 4K of SRAM/reg space */
465 static void _ipw_read_indirect(struct ipw_priv *priv, u32 addr, u8 * buf,
466 			       int num)
467 {
468 	u32 aligned_addr = addr & IPW_INDIRECT_ADDR_MASK;	/* dword align */
469 	u32 dif_len = addr - aligned_addr;
470 	u32 i;
471 
472 	IPW_DEBUG_IO("addr = %i, buf = %p, num = %i\n", addr, buf, num);
473 
474 	if (num <= 0) {
475 		return;
476 	}
477 
478 	/* Read the first dword (or portion) byte by byte */
479 	if (unlikely(dif_len)) {
480 		_ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
481 		/* Start reading at aligned_addr + dif_len */
482 		for (i = dif_len; ((i < 4) && (num > 0)); i++, num--)
483 			*buf++ = _ipw_read8(priv, IPW_INDIRECT_DATA + i);
484 		aligned_addr += 4;
485 	}
486 
487 	/* Read all of the middle dwords as dwords, with auto-increment */
488 	_ipw_write32(priv, IPW_AUTOINC_ADDR, aligned_addr);
489 	for (; num >= 4; buf += 4, aligned_addr += 4, num -= 4)
490 		*(u32 *) buf = _ipw_read32(priv, IPW_AUTOINC_DATA);
491 
492 	/* Read the last dword (or portion) byte by byte */
493 	if (unlikely(num)) {
494 		_ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
495 		for (i = 0; num > 0; i++, num--)
496 			*buf++ = ipw_read8(priv, IPW_INDIRECT_DATA + i);
497 	}
498 }
499 
500 /* General purpose, no alignment requirement, iterative (multi-byte) write, */
501 /*    for area above 1st 4K of SRAM/reg space */
502 static void _ipw_write_indirect(struct ipw_priv *priv, u32 addr, u8 * buf,
503 				int num)
504 {
505 	u32 aligned_addr = addr & IPW_INDIRECT_ADDR_MASK;	/* dword align */
506 	u32 dif_len = addr - aligned_addr;
507 	u32 i;
508 
509 	IPW_DEBUG_IO("addr = %i, buf = %p, num = %i\n", addr, buf, num);
510 
511 	if (num <= 0) {
512 		return;
513 	}
514 
515 	/* Write the first dword (or portion) byte by byte */
516 	if (unlikely(dif_len)) {
517 		_ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
518 		/* Start writing at aligned_addr + dif_len */
519 		for (i = dif_len; ((i < 4) && (num > 0)); i++, num--, buf++)
520 			_ipw_write8(priv, IPW_INDIRECT_DATA + i, *buf);
521 		aligned_addr += 4;
522 	}
523 
524 	/* Write all of the middle dwords as dwords, with auto-increment */
525 	_ipw_write32(priv, IPW_AUTOINC_ADDR, aligned_addr);
526 	for (; num >= 4; buf += 4, aligned_addr += 4, num -= 4)
527 		_ipw_write32(priv, IPW_AUTOINC_DATA, *(u32 *) buf);
528 
529 	/* Write the last dword (or portion) byte by byte */
530 	if (unlikely(num)) {
531 		_ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
532 		for (i = 0; num > 0; i++, num--, buf++)
533 			_ipw_write8(priv, IPW_INDIRECT_DATA + i, *buf);
534 	}
535 }
536 
537 /* General purpose, no alignment requirement, iterative (multi-byte) write, */
538 /*    for 1st 4K of SRAM/regs space */
539 static void ipw_write_direct(struct ipw_priv *priv, u32 addr, void *buf,
540 			     int num)
541 {
542 	memcpy_toio((priv->hw_base + addr), buf, num);
543 }
544 
545 /* Set bit(s) in low 4K of SRAM/regs */
546 static inline void ipw_set_bit(struct ipw_priv *priv, u32 reg, u32 mask)
547 {
548 	ipw_write32(priv, reg, ipw_read32(priv, reg) | mask);
549 }
550 
551 /* Clear bit(s) in low 4K of SRAM/regs */
552 static inline void ipw_clear_bit(struct ipw_priv *priv, u32 reg, u32 mask)
553 {
554 	ipw_write32(priv, reg, ipw_read32(priv, reg) & ~mask);
555 }
556 
557 static inline void __ipw_enable_interrupts(struct ipw_priv *priv)
558 {
559 	if (priv->status & STATUS_INT_ENABLED)
560 		return;
561 	priv->status |= STATUS_INT_ENABLED;
562 	ipw_write32(priv, IPW_INTA_MASK_R, IPW_INTA_MASK_ALL);
563 }
564 
565 static inline void __ipw_disable_interrupts(struct ipw_priv *priv)
566 {
567 	if (!(priv->status & STATUS_INT_ENABLED))
568 		return;
569 	priv->status &= ~STATUS_INT_ENABLED;
570 	ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);
571 }
572 
573 static inline void ipw_enable_interrupts(struct ipw_priv *priv)
574 {
575 	unsigned long flags;
576 
577 	spin_lock_irqsave(&priv->irq_lock, flags);
578 	__ipw_enable_interrupts(priv);
579 	spin_unlock_irqrestore(&priv->irq_lock, flags);
580 }
581 
582 static inline void ipw_disable_interrupts(struct ipw_priv *priv)
583 {
584 	unsigned long flags;
585 
586 	spin_lock_irqsave(&priv->irq_lock, flags);
587 	__ipw_disable_interrupts(priv);
588 	spin_unlock_irqrestore(&priv->irq_lock, flags);
589 }
590 
591 static char *ipw_error_desc(u32 val)
592 {
593 	switch (val) {
594 	case IPW_FW_ERROR_OK:
595 		return "ERROR_OK";
596 	case IPW_FW_ERROR_FAIL:
597 		return "ERROR_FAIL";
598 	case IPW_FW_ERROR_MEMORY_UNDERFLOW:
599 		return "MEMORY_UNDERFLOW";
600 	case IPW_FW_ERROR_MEMORY_OVERFLOW:
601 		return "MEMORY_OVERFLOW";
602 	case IPW_FW_ERROR_BAD_PARAM:
603 		return "BAD_PARAM";
604 	case IPW_FW_ERROR_BAD_CHECKSUM:
605 		return "BAD_CHECKSUM";
606 	case IPW_FW_ERROR_NMI_INTERRUPT:
607 		return "NMI_INTERRUPT";
608 	case IPW_FW_ERROR_BAD_DATABASE:
609 		return "BAD_DATABASE";
610 	case IPW_FW_ERROR_ALLOC_FAIL:
611 		return "ALLOC_FAIL";
612 	case IPW_FW_ERROR_DMA_UNDERRUN:
613 		return "DMA_UNDERRUN";
614 	case IPW_FW_ERROR_DMA_STATUS:
615 		return "DMA_STATUS";
616 	case IPW_FW_ERROR_DINO_ERROR:
617 		return "DINO_ERROR";
618 	case IPW_FW_ERROR_EEPROM_ERROR:
619 		return "EEPROM_ERROR";
620 	case IPW_FW_ERROR_SYSASSERT:
621 		return "SYSASSERT";
622 	case IPW_FW_ERROR_FATAL_ERROR:
623 		return "FATAL_ERROR";
624 	default:
625 		return "UNKNOWN_ERROR";
626 	}
627 }
628 
629 static void ipw_dump_error_log(struct ipw_priv *priv,
630 			       struct ipw_fw_error *error)
631 {
632 	u32 i;
633 
634 	if (!error) {
635 		IPW_ERROR("Error allocating and capturing error log.  "
636 			  "Nothing to dump.\n");
637 		return;
638 	}
639 
640 	IPW_ERROR("Start IPW Error Log Dump:\n");
641 	IPW_ERROR("Status: 0x%08X, Config: %08X\n",
642 		  error->status, error->config);
643 
644 	for (i = 0; i < error->elem_len; i++)
645 		IPW_ERROR("%s %i 0x%08x  0x%08x  0x%08x  0x%08x  0x%08x\n",
646 			  ipw_error_desc(error->elem[i].desc),
647 			  error->elem[i].time,
648 			  error->elem[i].blink1,
649 			  error->elem[i].blink2,
650 			  error->elem[i].link1,
651 			  error->elem[i].link2, error->elem[i].data);
652 	for (i = 0; i < error->log_len; i++)
653 		IPW_ERROR("%i\t0x%08x\t%i\n",
654 			  error->log[i].time,
655 			  error->log[i].data, error->log[i].event);
656 }
657 
658 static inline int ipw_is_init(struct ipw_priv *priv)
659 {
660 	return (priv->status & STATUS_INIT) ? 1 : 0;
661 }
662 
663 static int ipw_get_ordinal(struct ipw_priv *priv, u32 ord, void *val, u32 * len)
664 {
665 	u32 addr, field_info, field_len, field_count, total_len;
666 
667 	IPW_DEBUG_ORD("ordinal = %i\n", ord);
668 
669 	if (!priv || !val || !len) {
670 		IPW_DEBUG_ORD("Invalid argument\n");
671 		return -EINVAL;
672 	}
673 
674 	/* verify device ordinal tables have been initialized */
675 	if (!priv->table0_addr || !priv->table1_addr || !priv->table2_addr) {
676 		IPW_DEBUG_ORD("Access ordinals before initialization\n");
677 		return -EINVAL;
678 	}
679 
680 	switch (IPW_ORD_TABLE_ID_MASK & ord) {
681 	case IPW_ORD_TABLE_0_MASK:
682 		/*
683 		 * TABLE 0: Direct access to a table of 32 bit values
684 		 *
685 		 * This is a very simple table with the data directly
686 		 * read from the table
687 		 */
688 
689 		/* remove the table id from the ordinal */
690 		ord &= IPW_ORD_TABLE_VALUE_MASK;
691 
692 		/* boundary check */
693 		if (ord > priv->table0_len) {
694 			IPW_DEBUG_ORD("ordinal value (%i) longer then "
695 				      "max (%i)\n", ord, priv->table0_len);
696 			return -EINVAL;
697 		}
698 
699 		/* verify we have enough room to store the value */
700 		if (*len < sizeof(u32)) {
701 			IPW_DEBUG_ORD("ordinal buffer length too small, "
702 				      "need %zd\n", sizeof(u32));
703 			return -EINVAL;
704 		}
705 
706 		IPW_DEBUG_ORD("Reading TABLE0[%i] from offset 0x%08x\n",
707 			      ord, priv->table0_addr + (ord << 2));
708 
709 		*len = sizeof(u32);
710 		ord <<= 2;
711 		*((u32 *) val) = ipw_read32(priv, priv->table0_addr + ord);
712 		break;
713 
714 	case IPW_ORD_TABLE_1_MASK:
715 		/*
716 		 * TABLE 1: Indirect access to a table of 32 bit values
717 		 *
718 		 * This is a fairly large table of u32 values each
719 		 * representing starting addr for the data (which is
720 		 * also a u32)
721 		 */
722 
723 		/* remove the table id from the ordinal */
724 		ord &= IPW_ORD_TABLE_VALUE_MASK;
725 
726 		/* boundary check */
727 		if (ord > priv->table1_len) {
728 			IPW_DEBUG_ORD("ordinal value too long\n");
729 			return -EINVAL;
730 		}
731 
732 		/* verify we have enough room to store the value */
733 		if (*len < sizeof(u32)) {
734 			IPW_DEBUG_ORD("ordinal buffer length too small, "
735 				      "need %zd\n", sizeof(u32));
736 			return -EINVAL;
737 		}
738 
739 		*((u32 *) val) =
740 		    ipw_read_reg32(priv, (priv->table1_addr + (ord << 2)));
741 		*len = sizeof(u32);
742 		break;
743 
744 	case IPW_ORD_TABLE_2_MASK:
745 		/*
746 		 * TABLE 2: Indirect access to a table of variable sized values
747 		 *
748 		 * This table consist of six values, each containing
749 		 *     - dword containing the starting offset of the data
750 		 *     - dword containing the lengh in the first 16bits
751 		 *       and the count in the second 16bits
752 		 */
753 
754 		/* remove the table id from the ordinal */
755 		ord &= IPW_ORD_TABLE_VALUE_MASK;
756 
757 		/* boundary check */
758 		if (ord > priv->table2_len) {
759 			IPW_DEBUG_ORD("ordinal value too long\n");
760 			return -EINVAL;
761 		}
762 
763 		/* get the address of statistic */
764 		addr = ipw_read_reg32(priv, priv->table2_addr + (ord << 3));
765 
766 		/* get the second DW of statistics ;
767 		 * two 16-bit words - first is length, second is count */
768 		field_info =
769 		    ipw_read_reg32(priv,
770 				   priv->table2_addr + (ord << 3) +
771 				   sizeof(u32));
772 
773 		/* get each entry length */
774 		field_len = *((u16 *) & field_info);
775 
776 		/* get number of entries */
777 		field_count = *(((u16 *) & field_info) + 1);
778 
779 		/* abort if not enough memory */
780 		total_len = field_len * field_count;
781 		if (total_len > *len) {
782 			*len = total_len;
783 			return -EINVAL;
784 		}
785 
786 		*len = total_len;
787 		if (!total_len)
788 			return 0;
789 
790 		IPW_DEBUG_ORD("addr = 0x%08x, total_len = %i, "
791 			      "field_info = 0x%08x\n",
792 			      addr, total_len, field_info);
793 		ipw_read_indirect(priv, addr, val, total_len);
794 		break;
795 
796 	default:
797 		IPW_DEBUG_ORD("Invalid ordinal!\n");
798 		return -EINVAL;
799 
800 	}
801 
802 	return 0;
803 }
804 
805 static void ipw_init_ordinals(struct ipw_priv *priv)
806 {
807 	priv->table0_addr = IPW_ORDINALS_TABLE_LOWER;
808 	priv->table0_len = ipw_read32(priv, priv->table0_addr);
809 
810 	IPW_DEBUG_ORD("table 0 offset at 0x%08x, len = %i\n",
811 		      priv->table0_addr, priv->table0_len);
812 
813 	priv->table1_addr = ipw_read32(priv, IPW_ORDINALS_TABLE_1);
814 	priv->table1_len = ipw_read_reg32(priv, priv->table1_addr);
815 
816 	IPW_DEBUG_ORD("table 1 offset at 0x%08x, len = %i\n",
817 		      priv->table1_addr, priv->table1_len);
818 
819 	priv->table2_addr = ipw_read32(priv, IPW_ORDINALS_TABLE_2);
820 	priv->table2_len = ipw_read_reg32(priv, priv->table2_addr);
821 	priv->table2_len &= 0x0000ffff;	/* use first two bytes */
822 
823 	IPW_DEBUG_ORD("table 2 offset at 0x%08x, len = %i\n",
824 		      priv->table2_addr, priv->table2_len);
825 
826 }
827 
828 static u32 ipw_register_toggle(u32 reg)
829 {
830 	reg &= ~IPW_START_STANDBY;
831 	if (reg & IPW_GATE_ODMA)
832 		reg &= ~IPW_GATE_ODMA;
833 	if (reg & IPW_GATE_IDMA)
834 		reg &= ~IPW_GATE_IDMA;
835 	if (reg & IPW_GATE_ADMA)
836 		reg &= ~IPW_GATE_ADMA;
837 	return reg;
838 }
839 
840 /*
841  * LED behavior:
842  * - On radio ON, turn on any LEDs that require to be on during start
843  * - On initialization, start unassociated blink
844  * - On association, disable unassociated blink
845  * - On disassociation, start unassociated blink
846  * - On radio OFF, turn off any LEDs started during radio on
847  *
848  */
849 #define LD_TIME_LINK_ON msecs_to_jiffies(300)
850 #define LD_TIME_LINK_OFF msecs_to_jiffies(2700)
851 #define LD_TIME_ACT_ON msecs_to_jiffies(250)
852 
853 static void ipw_led_link_on(struct ipw_priv *priv)
854 {
855 	unsigned long flags;
856 	u32 led;
857 
858 	/* If configured to not use LEDs, or nic_type is 1,
859 	 * then we don't toggle a LINK led */
860 	if (priv->config & CFG_NO_LED || priv->nic_type == EEPROM_NIC_TYPE_1)
861 		return;
862 
863 	spin_lock_irqsave(&priv->lock, flags);
864 
865 	if (!(priv->status & STATUS_RF_KILL_MASK) &&
866 	    !(priv->status & STATUS_LED_LINK_ON)) {
867 		IPW_DEBUG_LED("Link LED On\n");
868 		led = ipw_read_reg32(priv, IPW_EVENT_REG);
869 		led |= priv->led_association_on;
870 
871 		led = ipw_register_toggle(led);
872 
873 		IPW_DEBUG_LED("Reg: 0x%08X\n", led);
874 		ipw_write_reg32(priv, IPW_EVENT_REG, led);
875 
876 		priv->status |= STATUS_LED_LINK_ON;
877 
878 		/* If we aren't associated, schedule turning the LED off */
879 		if (!(priv->status & STATUS_ASSOCIATED))
880 			schedule_delayed_work(&priv->led_link_off,
881 					      LD_TIME_LINK_ON);
882 	}
883 
884 	spin_unlock_irqrestore(&priv->lock, flags);
885 }
886 
887 static void ipw_bg_led_link_on(struct work_struct *work)
888 {
889 	struct ipw_priv *priv =
890 		container_of(work, struct ipw_priv, led_link_on.work);
891 	mutex_lock(&priv->mutex);
892 	ipw_led_link_on(priv);
893 	mutex_unlock(&priv->mutex);
894 }
895 
896 static void ipw_led_link_off(struct ipw_priv *priv)
897 {
898 	unsigned long flags;
899 	u32 led;
900 
901 	/* If configured not to use LEDs, or nic type is 1,
902 	 * then we don't goggle the LINK led. */
903 	if (priv->config & CFG_NO_LED || priv->nic_type == EEPROM_NIC_TYPE_1)
904 		return;
905 
906 	spin_lock_irqsave(&priv->lock, flags);
907 
908 	if (priv->status & STATUS_LED_LINK_ON) {
909 		led = ipw_read_reg32(priv, IPW_EVENT_REG);
910 		led &= priv->led_association_off;
911 		led = ipw_register_toggle(led);
912 
913 		IPW_DEBUG_LED("Reg: 0x%08X\n", led);
914 		ipw_write_reg32(priv, IPW_EVENT_REG, led);
915 
916 		IPW_DEBUG_LED("Link LED Off\n");
917 
918 		priv->status &= ~STATUS_LED_LINK_ON;
919 
920 		/* If we aren't associated and the radio is on, schedule
921 		 * turning the LED on (blink while unassociated) */
922 		if (!(priv->status & STATUS_RF_KILL_MASK) &&
923 		    !(priv->status & STATUS_ASSOCIATED))
924 			schedule_delayed_work(&priv->led_link_on,
925 					      LD_TIME_LINK_OFF);
926 
927 	}
928 
929 	spin_unlock_irqrestore(&priv->lock, flags);
930 }
931 
932 static void ipw_bg_led_link_off(struct work_struct *work)
933 {
934 	struct ipw_priv *priv =
935 		container_of(work, struct ipw_priv, led_link_off.work);
936 	mutex_lock(&priv->mutex);
937 	ipw_led_link_off(priv);
938 	mutex_unlock(&priv->mutex);
939 }
940 
941 static void __ipw_led_activity_on(struct ipw_priv *priv)
942 {
943 	u32 led;
944 
945 	if (priv->config & CFG_NO_LED)
946 		return;
947 
948 	if (priv->status & STATUS_RF_KILL_MASK)
949 		return;
950 
951 	if (!(priv->status & STATUS_LED_ACT_ON)) {
952 		led = ipw_read_reg32(priv, IPW_EVENT_REG);
953 		led |= priv->led_activity_on;
954 
955 		led = ipw_register_toggle(led);
956 
957 		IPW_DEBUG_LED("Reg: 0x%08X\n", led);
958 		ipw_write_reg32(priv, IPW_EVENT_REG, led);
959 
960 		IPW_DEBUG_LED("Activity LED On\n");
961 
962 		priv->status |= STATUS_LED_ACT_ON;
963 
964 		cancel_delayed_work(&priv->led_act_off);
965 		schedule_delayed_work(&priv->led_act_off, LD_TIME_ACT_ON);
966 	} else {
967 		/* Reschedule LED off for full time period */
968 		cancel_delayed_work(&priv->led_act_off);
969 		schedule_delayed_work(&priv->led_act_off, LD_TIME_ACT_ON);
970 	}
971 }
972 
973 #if 0
974 void ipw_led_activity_on(struct ipw_priv *priv)
975 {
976 	unsigned long flags;
977 	spin_lock_irqsave(&priv->lock, flags);
978 	__ipw_led_activity_on(priv);
979 	spin_unlock_irqrestore(&priv->lock, flags);
980 }
981 #endif  /*  0  */
982 
983 static void ipw_led_activity_off(struct ipw_priv *priv)
984 {
985 	unsigned long flags;
986 	u32 led;
987 
988 	if (priv->config & CFG_NO_LED)
989 		return;
990 
991 	spin_lock_irqsave(&priv->lock, flags);
992 
993 	if (priv->status & STATUS_LED_ACT_ON) {
994 		led = ipw_read_reg32(priv, IPW_EVENT_REG);
995 		led &= priv->led_activity_off;
996 
997 		led = ipw_register_toggle(led);
998 
999 		IPW_DEBUG_LED("Reg: 0x%08X\n", led);
1000 		ipw_write_reg32(priv, IPW_EVENT_REG, led);
1001 
1002 		IPW_DEBUG_LED("Activity LED Off\n");
1003 
1004 		priv->status &= ~STATUS_LED_ACT_ON;
1005 	}
1006 
1007 	spin_unlock_irqrestore(&priv->lock, flags);
1008 }
1009 
1010 static void ipw_bg_led_activity_off(struct work_struct *work)
1011 {
1012 	struct ipw_priv *priv =
1013 		container_of(work, struct ipw_priv, led_act_off.work);
1014 	mutex_lock(&priv->mutex);
1015 	ipw_led_activity_off(priv);
1016 	mutex_unlock(&priv->mutex);
1017 }
1018 
1019 static void ipw_led_band_on(struct ipw_priv *priv)
1020 {
1021 	unsigned long flags;
1022 	u32 led;
1023 
1024 	/* Only nic type 1 supports mode LEDs */
1025 	if (priv->config & CFG_NO_LED ||
1026 	    priv->nic_type != EEPROM_NIC_TYPE_1 || !priv->assoc_network)
1027 		return;
1028 
1029 	spin_lock_irqsave(&priv->lock, flags);
1030 
1031 	led = ipw_read_reg32(priv, IPW_EVENT_REG);
1032 	if (priv->assoc_network->mode == IEEE_A) {
1033 		led |= priv->led_ofdm_on;
1034 		led &= priv->led_association_off;
1035 		IPW_DEBUG_LED("Mode LED On: 802.11a\n");
1036 	} else if (priv->assoc_network->mode == IEEE_G) {
1037 		led |= priv->led_ofdm_on;
1038 		led |= priv->led_association_on;
1039 		IPW_DEBUG_LED("Mode LED On: 802.11g\n");
1040 	} else {
1041 		led &= priv->led_ofdm_off;
1042 		led |= priv->led_association_on;
1043 		IPW_DEBUG_LED("Mode LED On: 802.11b\n");
1044 	}
1045 
1046 	led = ipw_register_toggle(led);
1047 
1048 	IPW_DEBUG_LED("Reg: 0x%08X\n", led);
1049 	ipw_write_reg32(priv, IPW_EVENT_REG, led);
1050 
1051 	spin_unlock_irqrestore(&priv->lock, flags);
1052 }
1053 
1054 static void ipw_led_band_off(struct ipw_priv *priv)
1055 {
1056 	unsigned long flags;
1057 	u32 led;
1058 
1059 	/* Only nic type 1 supports mode LEDs */
1060 	if (priv->config & CFG_NO_LED || priv->nic_type != EEPROM_NIC_TYPE_1)
1061 		return;
1062 
1063 	spin_lock_irqsave(&priv->lock, flags);
1064 
1065 	led = ipw_read_reg32(priv, IPW_EVENT_REG);
1066 	led &= priv->led_ofdm_off;
1067 	led &= priv->led_association_off;
1068 
1069 	led = ipw_register_toggle(led);
1070 
1071 	IPW_DEBUG_LED("Reg: 0x%08X\n", led);
1072 	ipw_write_reg32(priv, IPW_EVENT_REG, led);
1073 
1074 	spin_unlock_irqrestore(&priv->lock, flags);
1075 }
1076 
1077 static void ipw_led_radio_on(struct ipw_priv *priv)
1078 {
1079 	ipw_led_link_on(priv);
1080 }
1081 
1082 static void ipw_led_radio_off(struct ipw_priv *priv)
1083 {
1084 	ipw_led_activity_off(priv);
1085 	ipw_led_link_off(priv);
1086 }
1087 
1088 static void ipw_led_link_up(struct ipw_priv *priv)
1089 {
1090 	/* Set the Link Led on for all nic types */
1091 	ipw_led_link_on(priv);
1092 }
1093 
1094 static void ipw_led_link_down(struct ipw_priv *priv)
1095 {
1096 	ipw_led_activity_off(priv);
1097 	ipw_led_link_off(priv);
1098 
1099 	if (priv->status & STATUS_RF_KILL_MASK)
1100 		ipw_led_radio_off(priv);
1101 }
1102 
1103 static void ipw_led_init(struct ipw_priv *priv)
1104 {
1105 	priv->nic_type = priv->eeprom[EEPROM_NIC_TYPE];
1106 
1107 	/* Set the default PINs for the link and activity leds */
1108 	priv->led_activity_on = IPW_ACTIVITY_LED;
1109 	priv->led_activity_off = ~(IPW_ACTIVITY_LED);
1110 
1111 	priv->led_association_on = IPW_ASSOCIATED_LED;
1112 	priv->led_association_off = ~(IPW_ASSOCIATED_LED);
1113 
1114 	/* Set the default PINs for the OFDM leds */
1115 	priv->led_ofdm_on = IPW_OFDM_LED;
1116 	priv->led_ofdm_off = ~(IPW_OFDM_LED);
1117 
1118 	switch (priv->nic_type) {
1119 	case EEPROM_NIC_TYPE_1:
1120 		/* In this NIC type, the LEDs are reversed.... */
1121 		priv->led_activity_on = IPW_ASSOCIATED_LED;
1122 		priv->led_activity_off = ~(IPW_ASSOCIATED_LED);
1123 		priv->led_association_on = IPW_ACTIVITY_LED;
1124 		priv->led_association_off = ~(IPW_ACTIVITY_LED);
1125 
1126 		if (!(priv->config & CFG_NO_LED))
1127 			ipw_led_band_on(priv);
1128 
1129 		/* And we don't blink link LEDs for this nic, so
1130 		 * just return here */
1131 		return;
1132 
1133 	case EEPROM_NIC_TYPE_3:
1134 	case EEPROM_NIC_TYPE_2:
1135 	case EEPROM_NIC_TYPE_4:
1136 	case EEPROM_NIC_TYPE_0:
1137 		break;
1138 
1139 	default:
1140 		IPW_DEBUG_INFO("Unknown NIC type from EEPROM: %d\n",
1141 			       priv->nic_type);
1142 		priv->nic_type = EEPROM_NIC_TYPE_0;
1143 		break;
1144 	}
1145 
1146 	if (!(priv->config & CFG_NO_LED)) {
1147 		if (priv->status & STATUS_ASSOCIATED)
1148 			ipw_led_link_on(priv);
1149 		else
1150 			ipw_led_link_off(priv);
1151 	}
1152 }
1153 
1154 static void ipw_led_shutdown(struct ipw_priv *priv)
1155 {
1156 	ipw_led_activity_off(priv);
1157 	ipw_led_link_off(priv);
1158 	ipw_led_band_off(priv);
1159 	cancel_delayed_work(&priv->led_link_on);
1160 	cancel_delayed_work(&priv->led_link_off);
1161 	cancel_delayed_work(&priv->led_act_off);
1162 }
1163 
1164 /*
1165  * The following adds a new attribute to the sysfs representation
1166  * of this device driver (i.e. a new file in /sys/bus/pci/drivers/ipw/)
1167  * used for controlling the debug level.
1168  *
1169  * See the level definitions in ipw for details.
1170  */
1171 static ssize_t debug_level_show(struct device_driver *d, char *buf)
1172 {
1173 	return sprintf(buf, "0x%08X\n", ipw_debug_level);
1174 }
1175 
1176 static ssize_t debug_level_store(struct device_driver *d, const char *buf,
1177 				 size_t count)
1178 {
1179 	char *p = (char *)buf;
1180 	u32 val;
1181 
1182 	if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
1183 		p++;
1184 		if (p[0] == 'x' || p[0] == 'X')
1185 			p++;
1186 		val = simple_strtoul(p, &p, 16);
1187 	} else
1188 		val = simple_strtoul(p, &p, 10);
1189 	if (p == buf)
1190 		printk(KERN_INFO DRV_NAME
1191 		       ": %s is not in hex or decimal form.\n", buf);
1192 	else
1193 		ipw_debug_level = val;
1194 
1195 	return strnlen(buf, count);
1196 }
1197 static DRIVER_ATTR_RW(debug_level);
1198 
1199 static inline u32 ipw_get_event_log_len(struct ipw_priv *priv)
1200 {
1201 	/* length = 1st dword in log */
1202 	return ipw_read_reg32(priv, ipw_read32(priv, IPW_EVENT_LOG));
1203 }
1204 
1205 static void ipw_capture_event_log(struct ipw_priv *priv,
1206 				  u32 log_len, struct ipw_event *log)
1207 {
1208 	u32 base;
1209 
1210 	if (log_len) {
1211 		base = ipw_read32(priv, IPW_EVENT_LOG);
1212 		ipw_read_indirect(priv, base + sizeof(base) + sizeof(u32),
1213 				  (u8 *) log, sizeof(*log) * log_len);
1214 	}
1215 }
1216 
1217 static struct ipw_fw_error *ipw_alloc_error_log(struct ipw_priv *priv)
1218 {
1219 	struct ipw_fw_error *error;
1220 	u32 log_len = ipw_get_event_log_len(priv);
1221 	u32 base = ipw_read32(priv, IPW_ERROR_LOG);
1222 	u32 elem_len = ipw_read_reg32(priv, base);
1223 
1224 	error = kmalloc(size_add(struct_size(error, elem, elem_len),
1225 				 array_size(sizeof(*error->log), log_len)),
1226 			GFP_ATOMIC);
1227 	if (!error) {
1228 		IPW_ERROR("Memory allocation for firmware error log "
1229 			  "failed.\n");
1230 		return NULL;
1231 	}
1232 	error->jiffies = jiffies;
1233 	error->status = priv->status;
1234 	error->config = priv->config;
1235 	error->elem_len = elem_len;
1236 	error->log_len = log_len;
1237 	error->log = (struct ipw_event *)(error->elem + elem_len);
1238 
1239 	ipw_capture_event_log(priv, log_len, error->log);
1240 
1241 	if (elem_len)
1242 		ipw_read_indirect(priv, base + sizeof(base), (u8 *) error->elem,
1243 				  sizeof(*error->elem) * elem_len);
1244 
1245 	return error;
1246 }
1247 
1248 static ssize_t event_log_show(struct device *d,
1249 			      struct device_attribute *attr, char *buf)
1250 {
1251 	struct ipw_priv *priv = dev_get_drvdata(d);
1252 	u32 log_len = ipw_get_event_log_len(priv);
1253 	u32 log_size;
1254 	struct ipw_event *log;
1255 	u32 len = 0, i;
1256 
1257 	/* not using min() because of its strict type checking */
1258 	log_size = PAGE_SIZE / sizeof(*log) > log_len ?
1259 			sizeof(*log) * log_len : PAGE_SIZE;
1260 	log = kzalloc(log_size, GFP_KERNEL);
1261 	if (!log) {
1262 		IPW_ERROR("Unable to allocate memory for log\n");
1263 		return 0;
1264 	}
1265 	log_len = log_size / sizeof(*log);
1266 	ipw_capture_event_log(priv, log_len, log);
1267 
1268 	len += scnprintf(buf + len, PAGE_SIZE - len, "%08X", log_len);
1269 	for (i = 0; i < log_len; i++)
1270 		len += scnprintf(buf + len, PAGE_SIZE - len,
1271 				"\n%08X%08X%08X",
1272 				log[i].time, log[i].event, log[i].data);
1273 	len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
1274 	kfree(log);
1275 	return len;
1276 }
1277 
1278 static DEVICE_ATTR_RO(event_log);
1279 
1280 static ssize_t error_show(struct device *d,
1281 			  struct device_attribute *attr, char *buf)
1282 {
1283 	struct ipw_priv *priv = dev_get_drvdata(d);
1284 	u32 len = 0, i;
1285 	if (!priv->error)
1286 		return 0;
1287 	len += scnprintf(buf + len, PAGE_SIZE - len,
1288 			"%08lX%08X%08X%08X",
1289 			priv->error->jiffies,
1290 			priv->error->status,
1291 			priv->error->config, priv->error->elem_len);
1292 	for (i = 0; i < priv->error->elem_len; i++)
1293 		len += scnprintf(buf + len, PAGE_SIZE - len,
1294 				"\n%08X%08X%08X%08X%08X%08X%08X",
1295 				priv->error->elem[i].time,
1296 				priv->error->elem[i].desc,
1297 				priv->error->elem[i].blink1,
1298 				priv->error->elem[i].blink2,
1299 				priv->error->elem[i].link1,
1300 				priv->error->elem[i].link2,
1301 				priv->error->elem[i].data);
1302 
1303 	len += scnprintf(buf + len, PAGE_SIZE - len,
1304 			"\n%08X", priv->error->log_len);
1305 	for (i = 0; i < priv->error->log_len; i++)
1306 		len += scnprintf(buf + len, PAGE_SIZE - len,
1307 				"\n%08X%08X%08X",
1308 				priv->error->log[i].time,
1309 				priv->error->log[i].event,
1310 				priv->error->log[i].data);
1311 	len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
1312 	return len;
1313 }
1314 
1315 static ssize_t error_store(struct device *d,
1316 			   struct device_attribute *attr,
1317 			   const char *buf, size_t count)
1318 {
1319 	struct ipw_priv *priv = dev_get_drvdata(d);
1320 
1321 	kfree(priv->error);
1322 	priv->error = NULL;
1323 	return count;
1324 }
1325 
1326 static DEVICE_ATTR_RW(error);
1327 
1328 static ssize_t cmd_log_show(struct device *d,
1329 			    struct device_attribute *attr, char *buf)
1330 {
1331 	struct ipw_priv *priv = dev_get_drvdata(d);
1332 	u32 len = 0, i;
1333 	if (!priv->cmdlog)
1334 		return 0;
1335 	for (i = (priv->cmdlog_pos + 1) % priv->cmdlog_len;
1336 	     (i != priv->cmdlog_pos) && (len < PAGE_SIZE);
1337 	     i = (i + 1) % priv->cmdlog_len) {
1338 		len +=
1339 		    scnprintf(buf + len, PAGE_SIZE - len,
1340 			     "\n%08lX%08X%08X%08X\n", priv->cmdlog[i].jiffies,
1341 			     priv->cmdlog[i].retcode, priv->cmdlog[i].cmd.cmd,
1342 			     priv->cmdlog[i].cmd.len);
1343 		len +=
1344 		    snprintk_buf(buf + len, PAGE_SIZE - len,
1345 				 (u8 *) priv->cmdlog[i].cmd.param,
1346 				 priv->cmdlog[i].cmd.len);
1347 		len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
1348 	}
1349 	len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
1350 	return len;
1351 }
1352 
1353 static DEVICE_ATTR_RO(cmd_log);
1354 
1355 #ifdef CONFIG_IPW2200_PROMISCUOUS
1356 static void ipw_prom_free(struct ipw_priv *priv);
1357 static int ipw_prom_alloc(struct ipw_priv *priv);
1358 static ssize_t rtap_iface_store(struct device *d,
1359 			 struct device_attribute *attr,
1360 			 const char *buf, size_t count)
1361 {
1362 	struct ipw_priv *priv = dev_get_drvdata(d);
1363 	int rc = 0;
1364 
1365 	if (count < 1)
1366 		return -EINVAL;
1367 
1368 	switch (buf[0]) {
1369 	case '0':
1370 		if (!rtap_iface)
1371 			return count;
1372 
1373 		if (netif_running(priv->prom_net_dev)) {
1374 			IPW_WARNING("Interface is up.  Cannot unregister.\n");
1375 			return count;
1376 		}
1377 
1378 		ipw_prom_free(priv);
1379 		rtap_iface = 0;
1380 		break;
1381 
1382 	case '1':
1383 		if (rtap_iface)
1384 			return count;
1385 
1386 		rc = ipw_prom_alloc(priv);
1387 		if (!rc)
1388 			rtap_iface = 1;
1389 		break;
1390 
1391 	default:
1392 		return -EINVAL;
1393 	}
1394 
1395 	if (rc) {
1396 		IPW_ERROR("Failed to register promiscuous network "
1397 			  "device (error %d).\n", rc);
1398 	}
1399 
1400 	return count;
1401 }
1402 
1403 static ssize_t rtap_iface_show(struct device *d,
1404 			struct device_attribute *attr,
1405 			char *buf)
1406 {
1407 	struct ipw_priv *priv = dev_get_drvdata(d);
1408 	if (rtap_iface)
1409 		return sprintf(buf, "%s", priv->prom_net_dev->name);
1410 	else {
1411 		buf[0] = '-';
1412 		buf[1] = '1';
1413 		buf[2] = '\0';
1414 		return 3;
1415 	}
1416 }
1417 
1418 static DEVICE_ATTR_ADMIN_RW(rtap_iface);
1419 
1420 static ssize_t rtap_filter_store(struct device *d,
1421 			 struct device_attribute *attr,
1422 			 const char *buf, size_t count)
1423 {
1424 	struct ipw_priv *priv = dev_get_drvdata(d);
1425 
1426 	if (!priv->prom_priv) {
1427 		IPW_ERROR("Attempting to set filter without "
1428 			  "rtap_iface enabled.\n");
1429 		return -EPERM;
1430 	}
1431 
1432 	priv->prom_priv->filter = simple_strtol(buf, NULL, 0);
1433 
1434 	IPW_DEBUG_INFO("Setting rtap filter to " BIT_FMT16 "\n",
1435 		       BIT_ARG16(priv->prom_priv->filter));
1436 
1437 	return count;
1438 }
1439 
1440 static ssize_t rtap_filter_show(struct device *d,
1441 			struct device_attribute *attr,
1442 			char *buf)
1443 {
1444 	struct ipw_priv *priv = dev_get_drvdata(d);
1445 	return sprintf(buf, "0x%04X",
1446 		       priv->prom_priv ? priv->prom_priv->filter : 0);
1447 }
1448 
1449 static DEVICE_ATTR_ADMIN_RW(rtap_filter);
1450 #endif
1451 
1452 static ssize_t scan_age_show(struct device *d, struct device_attribute *attr,
1453 			     char *buf)
1454 {
1455 	struct ipw_priv *priv = dev_get_drvdata(d);
1456 	return sprintf(buf, "%d\n", priv->ieee->scan_age);
1457 }
1458 
1459 static ssize_t scan_age_store(struct device *d, struct device_attribute *attr,
1460 			      const char *buf, size_t count)
1461 {
1462 	struct ipw_priv *priv = dev_get_drvdata(d);
1463 	struct net_device *dev = priv->net_dev;
1464 	char buffer[] = "00000000";
1465 	unsigned long len =
1466 	    (sizeof(buffer) - 1) > count ? count : sizeof(buffer) - 1;
1467 	unsigned long val;
1468 	char *p = buffer;
1469 
1470 	IPW_DEBUG_INFO("enter\n");
1471 
1472 	strncpy(buffer, buf, len);
1473 	buffer[len] = 0;
1474 
1475 	if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
1476 		p++;
1477 		if (p[0] == 'x' || p[0] == 'X')
1478 			p++;
1479 		val = simple_strtoul(p, &p, 16);
1480 	} else
1481 		val = simple_strtoul(p, &p, 10);
1482 	if (p == buffer) {
1483 		IPW_DEBUG_INFO("%s: user supplied invalid value.\n", dev->name);
1484 	} else {
1485 		priv->ieee->scan_age = val;
1486 		IPW_DEBUG_INFO("set scan_age = %u\n", priv->ieee->scan_age);
1487 	}
1488 
1489 	IPW_DEBUG_INFO("exit\n");
1490 	return len;
1491 }
1492 
1493 static DEVICE_ATTR_RW(scan_age);
1494 
1495 static ssize_t led_show(struct device *d, struct device_attribute *attr,
1496 			char *buf)
1497 {
1498 	struct ipw_priv *priv = dev_get_drvdata(d);
1499 	return sprintf(buf, "%d\n", (priv->config & CFG_NO_LED) ? 0 : 1);
1500 }
1501 
1502 static ssize_t led_store(struct device *d, struct device_attribute *attr,
1503 			 const char *buf, size_t count)
1504 {
1505 	struct ipw_priv *priv = dev_get_drvdata(d);
1506 
1507 	IPW_DEBUG_INFO("enter\n");
1508 
1509 	if (count == 0)
1510 		return 0;
1511 
1512 	if (*buf == 0) {
1513 		IPW_DEBUG_LED("Disabling LED control.\n");
1514 		priv->config |= CFG_NO_LED;
1515 		ipw_led_shutdown(priv);
1516 	} else {
1517 		IPW_DEBUG_LED("Enabling LED control.\n");
1518 		priv->config &= ~CFG_NO_LED;
1519 		ipw_led_init(priv);
1520 	}
1521 
1522 	IPW_DEBUG_INFO("exit\n");
1523 	return count;
1524 }
1525 
1526 static DEVICE_ATTR_RW(led);
1527 
1528 static ssize_t status_show(struct device *d,
1529 			   struct device_attribute *attr, char *buf)
1530 {
1531 	struct ipw_priv *p = dev_get_drvdata(d);
1532 	return sprintf(buf, "0x%08x\n", (int)p->status);
1533 }
1534 
1535 static DEVICE_ATTR_RO(status);
1536 
1537 static ssize_t cfg_show(struct device *d, struct device_attribute *attr,
1538 			char *buf)
1539 {
1540 	struct ipw_priv *p = dev_get_drvdata(d);
1541 	return sprintf(buf, "0x%08x\n", (int)p->config);
1542 }
1543 
1544 static DEVICE_ATTR_RO(cfg);
1545 
1546 static ssize_t nic_type_show(struct device *d,
1547 			     struct device_attribute *attr, char *buf)
1548 {
1549 	struct ipw_priv *priv = dev_get_drvdata(d);
1550 	return sprintf(buf, "TYPE: %d\n", priv->nic_type);
1551 }
1552 
1553 static DEVICE_ATTR_RO(nic_type);
1554 
1555 static ssize_t ucode_version_show(struct device *d,
1556 				  struct device_attribute *attr, char *buf)
1557 {
1558 	u32 len = sizeof(u32), tmp = 0;
1559 	struct ipw_priv *p = dev_get_drvdata(d);
1560 
1561 	if (ipw_get_ordinal(p, IPW_ORD_STAT_UCODE_VERSION, &tmp, &len))
1562 		return 0;
1563 
1564 	return sprintf(buf, "0x%08x\n", tmp);
1565 }
1566 
1567 static DEVICE_ATTR_RO(ucode_version);
1568 
1569 static ssize_t rtc_show(struct device *d, struct device_attribute *attr,
1570 			char *buf)
1571 {
1572 	u32 len = sizeof(u32), tmp = 0;
1573 	struct ipw_priv *p = dev_get_drvdata(d);
1574 
1575 	if (ipw_get_ordinal(p, IPW_ORD_STAT_RTC, &tmp, &len))
1576 		return 0;
1577 
1578 	return sprintf(buf, "0x%08x\n", tmp);
1579 }
1580 
1581 static DEVICE_ATTR_RO(rtc);
1582 
1583 /*
1584  * Add a device attribute to view/control the delay between eeprom
1585  * operations.
1586  */
1587 static ssize_t eeprom_delay_show(struct device *d,
1588 				 struct device_attribute *attr, char *buf)
1589 {
1590 	struct ipw_priv *p = dev_get_drvdata(d);
1591 	int n = p->eeprom_delay;
1592 	return sprintf(buf, "%i\n", n);
1593 }
1594 static ssize_t eeprom_delay_store(struct device *d,
1595 				  struct device_attribute *attr,
1596 				  const char *buf, size_t count)
1597 {
1598 	struct ipw_priv *p = dev_get_drvdata(d);
1599 	sscanf(buf, "%i", &p->eeprom_delay);
1600 	return strnlen(buf, count);
1601 }
1602 
1603 static DEVICE_ATTR_RW(eeprom_delay);
1604 
1605 static ssize_t command_event_reg_show(struct device *d,
1606 				      struct device_attribute *attr, char *buf)
1607 {
1608 	u32 reg = 0;
1609 	struct ipw_priv *p = dev_get_drvdata(d);
1610 
1611 	reg = ipw_read_reg32(p, IPW_INTERNAL_CMD_EVENT);
1612 	return sprintf(buf, "0x%08x\n", reg);
1613 }
1614 static ssize_t command_event_reg_store(struct device *d,
1615 				       struct device_attribute *attr,
1616 				       const char *buf, size_t count)
1617 {
1618 	u32 reg;
1619 	struct ipw_priv *p = dev_get_drvdata(d);
1620 
1621 	sscanf(buf, "%x", &reg);
1622 	ipw_write_reg32(p, IPW_INTERNAL_CMD_EVENT, reg);
1623 	return strnlen(buf, count);
1624 }
1625 
1626 static DEVICE_ATTR_RW(command_event_reg);
1627 
1628 static ssize_t mem_gpio_reg_show(struct device *d,
1629 				 struct device_attribute *attr, char *buf)
1630 {
1631 	u32 reg = 0;
1632 	struct ipw_priv *p = dev_get_drvdata(d);
1633 
1634 	reg = ipw_read_reg32(p, 0x301100);
1635 	return sprintf(buf, "0x%08x\n", reg);
1636 }
1637 static ssize_t mem_gpio_reg_store(struct device *d,
1638 				  struct device_attribute *attr,
1639 				  const char *buf, size_t count)
1640 {
1641 	u32 reg;
1642 	struct ipw_priv *p = dev_get_drvdata(d);
1643 
1644 	sscanf(buf, "%x", &reg);
1645 	ipw_write_reg32(p, 0x301100, reg);
1646 	return strnlen(buf, count);
1647 }
1648 
1649 static DEVICE_ATTR_RW(mem_gpio_reg);
1650 
1651 static ssize_t indirect_dword_show(struct device *d,
1652 				   struct device_attribute *attr, char *buf)
1653 {
1654 	u32 reg = 0;
1655 	struct ipw_priv *priv = dev_get_drvdata(d);
1656 
1657 	if (priv->status & STATUS_INDIRECT_DWORD)
1658 		reg = ipw_read_reg32(priv, priv->indirect_dword);
1659 	else
1660 		reg = 0;
1661 
1662 	return sprintf(buf, "0x%08x\n", reg);
1663 }
1664 static ssize_t indirect_dword_store(struct device *d,
1665 				    struct device_attribute *attr,
1666 				    const char *buf, size_t count)
1667 {
1668 	struct ipw_priv *priv = dev_get_drvdata(d);
1669 
1670 	sscanf(buf, "%x", &priv->indirect_dword);
1671 	priv->status |= STATUS_INDIRECT_DWORD;
1672 	return strnlen(buf, count);
1673 }
1674 
1675 static DEVICE_ATTR_RW(indirect_dword);
1676 
1677 static ssize_t indirect_byte_show(struct device *d,
1678 				  struct device_attribute *attr, char *buf)
1679 {
1680 	u8 reg = 0;
1681 	struct ipw_priv *priv = dev_get_drvdata(d);
1682 
1683 	if (priv->status & STATUS_INDIRECT_BYTE)
1684 		reg = ipw_read_reg8(priv, priv->indirect_byte);
1685 	else
1686 		reg = 0;
1687 
1688 	return sprintf(buf, "0x%02x\n", reg);
1689 }
1690 static ssize_t indirect_byte_store(struct device *d,
1691 				   struct device_attribute *attr,
1692 				   const char *buf, size_t count)
1693 {
1694 	struct ipw_priv *priv = dev_get_drvdata(d);
1695 
1696 	sscanf(buf, "%x", &priv->indirect_byte);
1697 	priv->status |= STATUS_INDIRECT_BYTE;
1698 	return strnlen(buf, count);
1699 }
1700 
1701 static DEVICE_ATTR_RW(indirect_byte);
1702 
1703 static ssize_t direct_dword_show(struct device *d,
1704 				 struct device_attribute *attr, char *buf)
1705 {
1706 	u32 reg = 0;
1707 	struct ipw_priv *priv = dev_get_drvdata(d);
1708 
1709 	if (priv->status & STATUS_DIRECT_DWORD)
1710 		reg = ipw_read32(priv, priv->direct_dword);
1711 	else
1712 		reg = 0;
1713 
1714 	return sprintf(buf, "0x%08x\n", reg);
1715 }
1716 static ssize_t direct_dword_store(struct device *d,
1717 				  struct device_attribute *attr,
1718 				  const char *buf, size_t count)
1719 {
1720 	struct ipw_priv *priv = dev_get_drvdata(d);
1721 
1722 	sscanf(buf, "%x", &priv->direct_dword);
1723 	priv->status |= STATUS_DIRECT_DWORD;
1724 	return strnlen(buf, count);
1725 }
1726 
1727 static DEVICE_ATTR_RW(direct_dword);
1728 
1729 static int rf_kill_active(struct ipw_priv *priv)
1730 {
1731 	if (0 == (ipw_read32(priv, 0x30) & 0x10000)) {
1732 		priv->status |= STATUS_RF_KILL_HW;
1733 		wiphy_rfkill_set_hw_state(priv->ieee->wdev.wiphy, true);
1734 	} else {
1735 		priv->status &= ~STATUS_RF_KILL_HW;
1736 		wiphy_rfkill_set_hw_state(priv->ieee->wdev.wiphy, false);
1737 	}
1738 
1739 	return (priv->status & STATUS_RF_KILL_HW) ? 1 : 0;
1740 }
1741 
1742 static ssize_t rf_kill_show(struct device *d, struct device_attribute *attr,
1743 			    char *buf)
1744 {
1745 	/* 0 - RF kill not enabled
1746 	   1 - SW based RF kill active (sysfs)
1747 	   2 - HW based RF kill active
1748 	   3 - Both HW and SW baed RF kill active */
1749 	struct ipw_priv *priv = dev_get_drvdata(d);
1750 	int val = ((priv->status & STATUS_RF_KILL_SW) ? 0x1 : 0x0) |
1751 	    (rf_kill_active(priv) ? 0x2 : 0x0);
1752 	return sprintf(buf, "%i\n", val);
1753 }
1754 
1755 static int ipw_radio_kill_sw(struct ipw_priv *priv, int disable_radio)
1756 {
1757 	if ((disable_radio ? 1 : 0) ==
1758 	    ((priv->status & STATUS_RF_KILL_SW) ? 1 : 0))
1759 		return 0;
1760 
1761 	IPW_DEBUG_RF_KILL("Manual SW RF Kill set to: RADIO  %s\n",
1762 			  disable_radio ? "OFF" : "ON");
1763 
1764 	if (disable_radio) {
1765 		priv->status |= STATUS_RF_KILL_SW;
1766 
1767 		cancel_delayed_work(&priv->request_scan);
1768 		cancel_delayed_work(&priv->request_direct_scan);
1769 		cancel_delayed_work(&priv->request_passive_scan);
1770 		cancel_delayed_work(&priv->scan_event);
1771 		schedule_work(&priv->down);
1772 	} else {
1773 		priv->status &= ~STATUS_RF_KILL_SW;
1774 		if (rf_kill_active(priv)) {
1775 			IPW_DEBUG_RF_KILL("Can not turn radio back on - "
1776 					  "disabled by HW switch\n");
1777 			/* Make sure the RF_KILL check timer is running */
1778 			cancel_delayed_work(&priv->rf_kill);
1779 			schedule_delayed_work(&priv->rf_kill,
1780 					      round_jiffies_relative(2 * HZ));
1781 		} else
1782 			schedule_work(&priv->up);
1783 	}
1784 
1785 	return 1;
1786 }
1787 
1788 static ssize_t rf_kill_store(struct device *d, struct device_attribute *attr,
1789 			     const char *buf, size_t count)
1790 {
1791 	struct ipw_priv *priv = dev_get_drvdata(d);
1792 
1793 	ipw_radio_kill_sw(priv, buf[0] == '1');
1794 
1795 	return count;
1796 }
1797 
1798 static DEVICE_ATTR_RW(rf_kill);
1799 
1800 static ssize_t speed_scan_show(struct device *d, struct device_attribute *attr,
1801 			       char *buf)
1802 {
1803 	struct ipw_priv *priv = dev_get_drvdata(d);
1804 	int pos = 0, len = 0;
1805 	if (priv->config & CFG_SPEED_SCAN) {
1806 		while (priv->speed_scan[pos] != 0)
1807 			len += sprintf(&buf[len], "%d ",
1808 				       priv->speed_scan[pos++]);
1809 		return len + sprintf(&buf[len], "\n");
1810 	}
1811 
1812 	return sprintf(buf, "0\n");
1813 }
1814 
1815 static ssize_t speed_scan_store(struct device *d, struct device_attribute *attr,
1816 				const char *buf, size_t count)
1817 {
1818 	struct ipw_priv *priv = dev_get_drvdata(d);
1819 	int channel, pos = 0;
1820 	const char *p = buf;
1821 
1822 	/* list of space separated channels to scan, optionally ending with 0 */
1823 	while ((channel = simple_strtol(p, NULL, 0))) {
1824 		if (pos == MAX_SPEED_SCAN - 1) {
1825 			priv->speed_scan[pos] = 0;
1826 			break;
1827 		}
1828 
1829 		if (libipw_is_valid_channel(priv->ieee, channel))
1830 			priv->speed_scan[pos++] = channel;
1831 		else
1832 			IPW_WARNING("Skipping invalid channel request: %d\n",
1833 				    channel);
1834 		p = strchr(p, ' ');
1835 		if (!p)
1836 			break;
1837 		while (*p == ' ' || *p == '\t')
1838 			p++;
1839 	}
1840 
1841 	if (pos == 0)
1842 		priv->config &= ~CFG_SPEED_SCAN;
1843 	else {
1844 		priv->speed_scan_pos = 0;
1845 		priv->config |= CFG_SPEED_SCAN;
1846 	}
1847 
1848 	return count;
1849 }
1850 
1851 static DEVICE_ATTR_RW(speed_scan);
1852 
1853 static ssize_t net_stats_show(struct device *d, struct device_attribute *attr,
1854 			      char *buf)
1855 {
1856 	struct ipw_priv *priv = dev_get_drvdata(d);
1857 	return sprintf(buf, "%c\n", (priv->config & CFG_NET_STATS) ? '1' : '0');
1858 }
1859 
1860 static ssize_t net_stats_store(struct device *d, struct device_attribute *attr,
1861 			       const char *buf, size_t count)
1862 {
1863 	struct ipw_priv *priv = dev_get_drvdata(d);
1864 	if (buf[0] == '1')
1865 		priv->config |= CFG_NET_STATS;
1866 	else
1867 		priv->config &= ~CFG_NET_STATS;
1868 
1869 	return count;
1870 }
1871 
1872 static DEVICE_ATTR_RW(net_stats);
1873 
1874 static ssize_t channels_show(struct device *d,
1875 			     struct device_attribute *attr,
1876 			     char *buf)
1877 {
1878 	struct ipw_priv *priv = dev_get_drvdata(d);
1879 	const struct libipw_geo *geo = libipw_get_geo(priv->ieee);
1880 	int len = 0, i;
1881 
1882 	len = sprintf(&buf[len],
1883 		      "Displaying %d channels in 2.4Ghz band "
1884 		      "(802.11bg):\n", geo->bg_channels);
1885 
1886 	for (i = 0; i < geo->bg_channels; i++) {
1887 		len += sprintf(&buf[len], "%d: BSS%s%s, %s, Band %s.\n",
1888 			       geo->bg[i].channel,
1889 			       geo->bg[i].flags & LIBIPW_CH_RADAR_DETECT ?
1890 			       " (radar spectrum)" : "",
1891 			       ((geo->bg[i].flags & LIBIPW_CH_NO_IBSS) ||
1892 				(geo->bg[i].flags & LIBIPW_CH_RADAR_DETECT))
1893 			       ? "" : ", IBSS",
1894 			       geo->bg[i].flags & LIBIPW_CH_PASSIVE_ONLY ?
1895 			       "passive only" : "active/passive",
1896 			       geo->bg[i].flags & LIBIPW_CH_B_ONLY ?
1897 			       "B" : "B/G");
1898 	}
1899 
1900 	len += sprintf(&buf[len],
1901 		       "Displaying %d channels in 5.2Ghz band "
1902 		       "(802.11a):\n", geo->a_channels);
1903 	for (i = 0; i < geo->a_channels; i++) {
1904 		len += sprintf(&buf[len], "%d: BSS%s%s, %s.\n",
1905 			       geo->a[i].channel,
1906 			       geo->a[i].flags & LIBIPW_CH_RADAR_DETECT ?
1907 			       " (radar spectrum)" : "",
1908 			       ((geo->a[i].flags & LIBIPW_CH_NO_IBSS) ||
1909 				(geo->a[i].flags & LIBIPW_CH_RADAR_DETECT))
1910 			       ? "" : ", IBSS",
1911 			       geo->a[i].flags & LIBIPW_CH_PASSIVE_ONLY ?
1912 			       "passive only" : "active/passive");
1913 	}
1914 
1915 	return len;
1916 }
1917 
1918 static DEVICE_ATTR_ADMIN_RO(channels);
1919 
1920 static void notify_wx_assoc_event(struct ipw_priv *priv)
1921 {
1922 	union iwreq_data wrqu;
1923 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1924 	if (priv->status & STATUS_ASSOCIATED)
1925 		memcpy(wrqu.ap_addr.sa_data, priv->bssid, ETH_ALEN);
1926 	else
1927 		eth_zero_addr(wrqu.ap_addr.sa_data);
1928 	wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
1929 }
1930 
1931 static void ipw_irq_tasklet(struct tasklet_struct *t)
1932 {
1933 	struct ipw_priv *priv = from_tasklet(priv, t, irq_tasklet);
1934 	u32 inta, inta_mask, handled = 0;
1935 	unsigned long flags;
1936 
1937 	spin_lock_irqsave(&priv->irq_lock, flags);
1938 
1939 	inta = ipw_read32(priv, IPW_INTA_RW);
1940 	inta_mask = ipw_read32(priv, IPW_INTA_MASK_R);
1941 
1942 	if (inta == 0xFFFFFFFF) {
1943 		/* Hardware disappeared */
1944 		IPW_WARNING("TASKLET INTA == 0xFFFFFFFF\n");
1945 		/* Only handle the cached INTA values */
1946 		inta = 0;
1947 	}
1948 	inta &= (IPW_INTA_MASK_ALL & inta_mask);
1949 
1950 	/* Add any cached INTA values that need to be handled */
1951 	inta |= priv->isr_inta;
1952 
1953 	spin_unlock_irqrestore(&priv->irq_lock, flags);
1954 
1955 	spin_lock_irqsave(&priv->lock, flags);
1956 
1957 	/* handle all the justifications for the interrupt */
1958 	if (inta & IPW_INTA_BIT_RX_TRANSFER) {
1959 		ipw_rx(priv);
1960 		handled |= IPW_INTA_BIT_RX_TRANSFER;
1961 	}
1962 
1963 	if (inta & IPW_INTA_BIT_TX_CMD_QUEUE) {
1964 		IPW_DEBUG_HC("Command completed.\n");
1965 		ipw_queue_tx_reclaim(priv, &priv->txq_cmd, -1);
1966 		priv->status &= ~STATUS_HCMD_ACTIVE;
1967 		wake_up_interruptible(&priv->wait_command_queue);
1968 		handled |= IPW_INTA_BIT_TX_CMD_QUEUE;
1969 	}
1970 
1971 	if (inta & IPW_INTA_BIT_TX_QUEUE_1) {
1972 		IPW_DEBUG_TX("TX_QUEUE_1\n");
1973 		ipw_queue_tx_reclaim(priv, &priv->txq[0], 0);
1974 		handled |= IPW_INTA_BIT_TX_QUEUE_1;
1975 	}
1976 
1977 	if (inta & IPW_INTA_BIT_TX_QUEUE_2) {
1978 		IPW_DEBUG_TX("TX_QUEUE_2\n");
1979 		ipw_queue_tx_reclaim(priv, &priv->txq[1], 1);
1980 		handled |= IPW_INTA_BIT_TX_QUEUE_2;
1981 	}
1982 
1983 	if (inta & IPW_INTA_BIT_TX_QUEUE_3) {
1984 		IPW_DEBUG_TX("TX_QUEUE_3\n");
1985 		ipw_queue_tx_reclaim(priv, &priv->txq[2], 2);
1986 		handled |= IPW_INTA_BIT_TX_QUEUE_3;
1987 	}
1988 
1989 	if (inta & IPW_INTA_BIT_TX_QUEUE_4) {
1990 		IPW_DEBUG_TX("TX_QUEUE_4\n");
1991 		ipw_queue_tx_reclaim(priv, &priv->txq[3], 3);
1992 		handled |= IPW_INTA_BIT_TX_QUEUE_4;
1993 	}
1994 
1995 	if (inta & IPW_INTA_BIT_STATUS_CHANGE) {
1996 		IPW_WARNING("STATUS_CHANGE\n");
1997 		handled |= IPW_INTA_BIT_STATUS_CHANGE;
1998 	}
1999 
2000 	if (inta & IPW_INTA_BIT_BEACON_PERIOD_EXPIRED) {
2001 		IPW_WARNING("TX_PERIOD_EXPIRED\n");
2002 		handled |= IPW_INTA_BIT_BEACON_PERIOD_EXPIRED;
2003 	}
2004 
2005 	if (inta & IPW_INTA_BIT_SLAVE_MODE_HOST_CMD_DONE) {
2006 		IPW_WARNING("HOST_CMD_DONE\n");
2007 		handled |= IPW_INTA_BIT_SLAVE_MODE_HOST_CMD_DONE;
2008 	}
2009 
2010 	if (inta & IPW_INTA_BIT_FW_INITIALIZATION_DONE) {
2011 		IPW_WARNING("FW_INITIALIZATION_DONE\n");
2012 		handled |= IPW_INTA_BIT_FW_INITIALIZATION_DONE;
2013 	}
2014 
2015 	if (inta & IPW_INTA_BIT_FW_CARD_DISABLE_PHY_OFF_DONE) {
2016 		IPW_WARNING("PHY_OFF_DONE\n");
2017 		handled |= IPW_INTA_BIT_FW_CARD_DISABLE_PHY_OFF_DONE;
2018 	}
2019 
2020 	if (inta & IPW_INTA_BIT_RF_KILL_DONE) {
2021 		IPW_DEBUG_RF_KILL("RF_KILL_DONE\n");
2022 		priv->status |= STATUS_RF_KILL_HW;
2023 		wiphy_rfkill_set_hw_state(priv->ieee->wdev.wiphy, true);
2024 		wake_up_interruptible(&priv->wait_command_queue);
2025 		priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
2026 		cancel_delayed_work(&priv->request_scan);
2027 		cancel_delayed_work(&priv->request_direct_scan);
2028 		cancel_delayed_work(&priv->request_passive_scan);
2029 		cancel_delayed_work(&priv->scan_event);
2030 		schedule_work(&priv->link_down);
2031 		schedule_delayed_work(&priv->rf_kill, 2 * HZ);
2032 		handled |= IPW_INTA_BIT_RF_KILL_DONE;
2033 	}
2034 
2035 	if (inta & IPW_INTA_BIT_FATAL_ERROR) {
2036 		IPW_WARNING("Firmware error detected.  Restarting.\n");
2037 		if (priv->error) {
2038 			IPW_DEBUG_FW("Sysfs 'error' log already exists.\n");
2039 			if (ipw_debug_level & IPW_DL_FW_ERRORS) {
2040 				struct ipw_fw_error *error =
2041 				    ipw_alloc_error_log(priv);
2042 				ipw_dump_error_log(priv, error);
2043 				kfree(error);
2044 			}
2045 		} else {
2046 			priv->error = ipw_alloc_error_log(priv);
2047 			if (priv->error)
2048 				IPW_DEBUG_FW("Sysfs 'error' log captured.\n");
2049 			else
2050 				IPW_DEBUG_FW("Error allocating sysfs 'error' "
2051 					     "log.\n");
2052 			if (ipw_debug_level & IPW_DL_FW_ERRORS)
2053 				ipw_dump_error_log(priv, priv->error);
2054 		}
2055 
2056 		/* XXX: If hardware encryption is for WPA/WPA2,
2057 		 * we have to notify the supplicant. */
2058 		if (priv->ieee->sec.encrypt) {
2059 			priv->status &= ~STATUS_ASSOCIATED;
2060 			notify_wx_assoc_event(priv);
2061 		}
2062 
2063 		/* Keep the restart process from trying to send host
2064 		 * commands by clearing the INIT status bit */
2065 		priv->status &= ~STATUS_INIT;
2066 
2067 		/* Cancel currently queued command. */
2068 		priv->status &= ~STATUS_HCMD_ACTIVE;
2069 		wake_up_interruptible(&priv->wait_command_queue);
2070 
2071 		schedule_work(&priv->adapter_restart);
2072 		handled |= IPW_INTA_BIT_FATAL_ERROR;
2073 	}
2074 
2075 	if (inta & IPW_INTA_BIT_PARITY_ERROR) {
2076 		IPW_ERROR("Parity error\n");
2077 		handled |= IPW_INTA_BIT_PARITY_ERROR;
2078 	}
2079 
2080 	if (handled != inta) {
2081 		IPW_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
2082 	}
2083 
2084 	spin_unlock_irqrestore(&priv->lock, flags);
2085 
2086 	/* enable all interrupts */
2087 	ipw_enable_interrupts(priv);
2088 }
2089 
2090 #define IPW_CMD(x) case IPW_CMD_ ## x : return #x
2091 static char *get_cmd_string(u8 cmd)
2092 {
2093 	switch (cmd) {
2094 		IPW_CMD(HOST_COMPLETE);
2095 		IPW_CMD(POWER_DOWN);
2096 		IPW_CMD(SYSTEM_CONFIG);
2097 		IPW_CMD(MULTICAST_ADDRESS);
2098 		IPW_CMD(SSID);
2099 		IPW_CMD(ADAPTER_ADDRESS);
2100 		IPW_CMD(PORT_TYPE);
2101 		IPW_CMD(RTS_THRESHOLD);
2102 		IPW_CMD(FRAG_THRESHOLD);
2103 		IPW_CMD(POWER_MODE);
2104 		IPW_CMD(WEP_KEY);
2105 		IPW_CMD(TGI_TX_KEY);
2106 		IPW_CMD(SCAN_REQUEST);
2107 		IPW_CMD(SCAN_REQUEST_EXT);
2108 		IPW_CMD(ASSOCIATE);
2109 		IPW_CMD(SUPPORTED_RATES);
2110 		IPW_CMD(SCAN_ABORT);
2111 		IPW_CMD(TX_FLUSH);
2112 		IPW_CMD(QOS_PARAMETERS);
2113 		IPW_CMD(DINO_CONFIG);
2114 		IPW_CMD(RSN_CAPABILITIES);
2115 		IPW_CMD(RX_KEY);
2116 		IPW_CMD(CARD_DISABLE);
2117 		IPW_CMD(SEED_NUMBER);
2118 		IPW_CMD(TX_POWER);
2119 		IPW_CMD(COUNTRY_INFO);
2120 		IPW_CMD(AIRONET_INFO);
2121 		IPW_CMD(AP_TX_POWER);
2122 		IPW_CMD(CCKM_INFO);
2123 		IPW_CMD(CCX_VER_INFO);
2124 		IPW_CMD(SET_CALIBRATION);
2125 		IPW_CMD(SENSITIVITY_CALIB);
2126 		IPW_CMD(RETRY_LIMIT);
2127 		IPW_CMD(IPW_PRE_POWER_DOWN);
2128 		IPW_CMD(VAP_BEACON_TEMPLATE);
2129 		IPW_CMD(VAP_DTIM_PERIOD);
2130 		IPW_CMD(EXT_SUPPORTED_RATES);
2131 		IPW_CMD(VAP_LOCAL_TX_PWR_CONSTRAINT);
2132 		IPW_CMD(VAP_QUIET_INTERVALS);
2133 		IPW_CMD(VAP_CHANNEL_SWITCH);
2134 		IPW_CMD(VAP_MANDATORY_CHANNELS);
2135 		IPW_CMD(VAP_CELL_PWR_LIMIT);
2136 		IPW_CMD(VAP_CF_PARAM_SET);
2137 		IPW_CMD(VAP_SET_BEACONING_STATE);
2138 		IPW_CMD(MEASUREMENT);
2139 		IPW_CMD(POWER_CAPABILITY);
2140 		IPW_CMD(SUPPORTED_CHANNELS);
2141 		IPW_CMD(TPC_REPORT);
2142 		IPW_CMD(WME_INFO);
2143 		IPW_CMD(PRODUCTION_COMMAND);
2144 	default:
2145 		return "UNKNOWN";
2146 	}
2147 }
2148 
2149 #define HOST_COMPLETE_TIMEOUT HZ
2150 
2151 static int __ipw_send_cmd(struct ipw_priv *priv, struct host_cmd *cmd)
2152 {
2153 	int rc = 0;
2154 	unsigned long flags;
2155 	unsigned long now, end;
2156 
2157 	spin_lock_irqsave(&priv->lock, flags);
2158 	if (priv->status & STATUS_HCMD_ACTIVE) {
2159 		IPW_ERROR("Failed to send %s: Already sending a command.\n",
2160 			  get_cmd_string(cmd->cmd));
2161 		spin_unlock_irqrestore(&priv->lock, flags);
2162 		return -EAGAIN;
2163 	}
2164 
2165 	priv->status |= STATUS_HCMD_ACTIVE;
2166 
2167 	if (priv->cmdlog) {
2168 		priv->cmdlog[priv->cmdlog_pos].jiffies = jiffies;
2169 		priv->cmdlog[priv->cmdlog_pos].cmd.cmd = cmd->cmd;
2170 		priv->cmdlog[priv->cmdlog_pos].cmd.len = cmd->len;
2171 		memcpy(priv->cmdlog[priv->cmdlog_pos].cmd.param, cmd->param,
2172 		       cmd->len);
2173 		priv->cmdlog[priv->cmdlog_pos].retcode = -1;
2174 	}
2175 
2176 	IPW_DEBUG_HC("%s command (#%d) %d bytes: 0x%08X\n",
2177 		     get_cmd_string(cmd->cmd), cmd->cmd, cmd->len,
2178 		     priv->status);
2179 
2180 #ifndef DEBUG_CMD_WEP_KEY
2181 	if (cmd->cmd == IPW_CMD_WEP_KEY)
2182 		IPW_DEBUG_HC("WEP_KEY command masked out for secure.\n");
2183 	else
2184 #endif
2185 		printk_buf(IPW_DL_HOST_COMMAND, (u8 *) cmd->param, cmd->len);
2186 
2187 	rc = ipw_queue_tx_hcmd(priv, cmd->cmd, cmd->param, cmd->len, 0);
2188 	if (rc) {
2189 		priv->status &= ~STATUS_HCMD_ACTIVE;
2190 		IPW_ERROR("Failed to send %s: Reason %d\n",
2191 			  get_cmd_string(cmd->cmd), rc);
2192 		spin_unlock_irqrestore(&priv->lock, flags);
2193 		goto exit;
2194 	}
2195 	spin_unlock_irqrestore(&priv->lock, flags);
2196 
2197 	now = jiffies;
2198 	end = now + HOST_COMPLETE_TIMEOUT;
2199 again:
2200 	rc = wait_event_interruptible_timeout(priv->wait_command_queue,
2201 					      !(priv->
2202 						status & STATUS_HCMD_ACTIVE),
2203 					      end - now);
2204 	if (rc < 0) {
2205 		now = jiffies;
2206 		if (time_before(now, end))
2207 			goto again;
2208 		rc = 0;
2209 	}
2210 
2211 	if (rc == 0) {
2212 		spin_lock_irqsave(&priv->lock, flags);
2213 		if (priv->status & STATUS_HCMD_ACTIVE) {
2214 			IPW_ERROR("Failed to send %s: Command timed out.\n",
2215 				  get_cmd_string(cmd->cmd));
2216 			priv->status &= ~STATUS_HCMD_ACTIVE;
2217 			spin_unlock_irqrestore(&priv->lock, flags);
2218 			rc = -EIO;
2219 			goto exit;
2220 		}
2221 		spin_unlock_irqrestore(&priv->lock, flags);
2222 	} else
2223 		rc = 0;
2224 
2225 	if (priv->status & STATUS_RF_KILL_HW) {
2226 		IPW_ERROR("Failed to send %s: Aborted due to RF kill switch.\n",
2227 			  get_cmd_string(cmd->cmd));
2228 		rc = -EIO;
2229 		goto exit;
2230 	}
2231 
2232       exit:
2233 	if (priv->cmdlog) {
2234 		priv->cmdlog[priv->cmdlog_pos++].retcode = rc;
2235 		priv->cmdlog_pos %= priv->cmdlog_len;
2236 	}
2237 	return rc;
2238 }
2239 
2240 static int ipw_send_cmd_simple(struct ipw_priv *priv, u8 command)
2241 {
2242 	struct host_cmd cmd = {
2243 		.cmd = command,
2244 	};
2245 
2246 	return __ipw_send_cmd(priv, &cmd);
2247 }
2248 
2249 static int ipw_send_cmd_pdu(struct ipw_priv *priv, u8 command, u8 len,
2250 			    const void *data)
2251 {
2252 	struct host_cmd cmd = {
2253 		.cmd = command,
2254 		.len = len,
2255 		.param = data,
2256 	};
2257 
2258 	return __ipw_send_cmd(priv, &cmd);
2259 }
2260 
2261 static int ipw_send_host_complete(struct ipw_priv *priv)
2262 {
2263 	if (!priv) {
2264 		IPW_ERROR("Invalid args\n");
2265 		return -1;
2266 	}
2267 
2268 	return ipw_send_cmd_simple(priv, IPW_CMD_HOST_COMPLETE);
2269 }
2270 
2271 static int ipw_send_system_config(struct ipw_priv *priv)
2272 {
2273 	return ipw_send_cmd_pdu(priv, IPW_CMD_SYSTEM_CONFIG,
2274 				sizeof(priv->sys_config),
2275 				&priv->sys_config);
2276 }
2277 
2278 static int ipw_send_ssid(struct ipw_priv *priv, u8 * ssid, int len)
2279 {
2280 	if (!priv || !ssid) {
2281 		IPW_ERROR("Invalid args\n");
2282 		return -1;
2283 	}
2284 
2285 	return ipw_send_cmd_pdu(priv, IPW_CMD_SSID, min(len, IW_ESSID_MAX_SIZE),
2286 				ssid);
2287 }
2288 
2289 static int ipw_send_adapter_address(struct ipw_priv *priv, const u8 * mac)
2290 {
2291 	if (!priv || !mac) {
2292 		IPW_ERROR("Invalid args\n");
2293 		return -1;
2294 	}
2295 
2296 	IPW_DEBUG_INFO("%s: Setting MAC to %pM\n",
2297 		       priv->net_dev->name, mac);
2298 
2299 	return ipw_send_cmd_pdu(priv, IPW_CMD_ADAPTER_ADDRESS, ETH_ALEN, mac);
2300 }
2301 
2302 static void ipw_adapter_restart(void *adapter)
2303 {
2304 	struct ipw_priv *priv = adapter;
2305 
2306 	if (priv->status & STATUS_RF_KILL_MASK)
2307 		return;
2308 
2309 	ipw_down(priv);
2310 
2311 	if (priv->assoc_network &&
2312 	    (priv->assoc_network->capability & WLAN_CAPABILITY_IBSS))
2313 		ipw_remove_current_network(priv);
2314 
2315 	if (ipw_up(priv)) {
2316 		IPW_ERROR("Failed to up device\n");
2317 		return;
2318 	}
2319 }
2320 
2321 static void ipw_bg_adapter_restart(struct work_struct *work)
2322 {
2323 	struct ipw_priv *priv =
2324 		container_of(work, struct ipw_priv, adapter_restart);
2325 	mutex_lock(&priv->mutex);
2326 	ipw_adapter_restart(priv);
2327 	mutex_unlock(&priv->mutex);
2328 }
2329 
2330 static void ipw_abort_scan(struct ipw_priv *priv);
2331 
2332 #define IPW_SCAN_CHECK_WATCHDOG	(5 * HZ)
2333 
2334 static void ipw_scan_check(void *data)
2335 {
2336 	struct ipw_priv *priv = data;
2337 
2338 	if (priv->status & STATUS_SCAN_ABORTING) {
2339 		IPW_DEBUG_SCAN("Scan completion watchdog resetting "
2340 			       "adapter after (%dms).\n",
2341 			       jiffies_to_msecs(IPW_SCAN_CHECK_WATCHDOG));
2342 		schedule_work(&priv->adapter_restart);
2343 	} else if (priv->status & STATUS_SCANNING) {
2344 		IPW_DEBUG_SCAN("Scan completion watchdog aborting scan "
2345 			       "after (%dms).\n",
2346 			       jiffies_to_msecs(IPW_SCAN_CHECK_WATCHDOG));
2347 		ipw_abort_scan(priv);
2348 		schedule_delayed_work(&priv->scan_check, HZ);
2349 	}
2350 }
2351 
2352 static void ipw_bg_scan_check(struct work_struct *work)
2353 {
2354 	struct ipw_priv *priv =
2355 		container_of(work, struct ipw_priv, scan_check.work);
2356 	mutex_lock(&priv->mutex);
2357 	ipw_scan_check(priv);
2358 	mutex_unlock(&priv->mutex);
2359 }
2360 
2361 static int ipw_send_scan_request_ext(struct ipw_priv *priv,
2362 				     struct ipw_scan_request_ext *request)
2363 {
2364 	return ipw_send_cmd_pdu(priv, IPW_CMD_SCAN_REQUEST_EXT,
2365 				sizeof(*request), request);
2366 }
2367 
2368 static int ipw_send_scan_abort(struct ipw_priv *priv)
2369 {
2370 	if (!priv) {
2371 		IPW_ERROR("Invalid args\n");
2372 		return -1;
2373 	}
2374 
2375 	return ipw_send_cmd_simple(priv, IPW_CMD_SCAN_ABORT);
2376 }
2377 
2378 static int ipw_set_sensitivity(struct ipw_priv *priv, u16 sens)
2379 {
2380 	struct ipw_sensitivity_calib calib = {
2381 		.beacon_rssi_raw = cpu_to_le16(sens),
2382 	};
2383 
2384 	return ipw_send_cmd_pdu(priv, IPW_CMD_SENSITIVITY_CALIB, sizeof(calib),
2385 				&calib);
2386 }
2387 
2388 static int ipw_send_associate(struct ipw_priv *priv,
2389 			      struct ipw_associate *associate)
2390 {
2391 	if (!priv || !associate) {
2392 		IPW_ERROR("Invalid args\n");
2393 		return -1;
2394 	}
2395 
2396 	return ipw_send_cmd_pdu(priv, IPW_CMD_ASSOCIATE, sizeof(*associate),
2397 				associate);
2398 }
2399 
2400 static int ipw_send_supported_rates(struct ipw_priv *priv,
2401 				    struct ipw_supported_rates *rates)
2402 {
2403 	if (!priv || !rates) {
2404 		IPW_ERROR("Invalid args\n");
2405 		return -1;
2406 	}
2407 
2408 	return ipw_send_cmd_pdu(priv, IPW_CMD_SUPPORTED_RATES, sizeof(*rates),
2409 				rates);
2410 }
2411 
2412 static int ipw_set_random_seed(struct ipw_priv *priv)
2413 {
2414 	u32 val;
2415 
2416 	if (!priv) {
2417 		IPW_ERROR("Invalid args\n");
2418 		return -1;
2419 	}
2420 
2421 	get_random_bytes(&val, sizeof(val));
2422 
2423 	return ipw_send_cmd_pdu(priv, IPW_CMD_SEED_NUMBER, sizeof(val), &val);
2424 }
2425 
2426 static int ipw_send_card_disable(struct ipw_priv *priv, u32 phy_off)
2427 {
2428 	__le32 v = cpu_to_le32(phy_off);
2429 	if (!priv) {
2430 		IPW_ERROR("Invalid args\n");
2431 		return -1;
2432 	}
2433 
2434 	return ipw_send_cmd_pdu(priv, IPW_CMD_CARD_DISABLE, sizeof(v), &v);
2435 }
2436 
2437 static int ipw_send_tx_power(struct ipw_priv *priv, struct ipw_tx_power *power)
2438 {
2439 	if (!priv || !power) {
2440 		IPW_ERROR("Invalid args\n");
2441 		return -1;
2442 	}
2443 
2444 	return ipw_send_cmd_pdu(priv, IPW_CMD_TX_POWER, sizeof(*power), power);
2445 }
2446 
2447 static int ipw_set_tx_power(struct ipw_priv *priv)
2448 {
2449 	const struct libipw_geo *geo = libipw_get_geo(priv->ieee);
2450 	struct ipw_tx_power tx_power;
2451 	s8 max_power;
2452 	int i;
2453 
2454 	memset(&tx_power, 0, sizeof(tx_power));
2455 
2456 	/* configure device for 'G' band */
2457 	tx_power.ieee_mode = IPW_G_MODE;
2458 	tx_power.num_channels = geo->bg_channels;
2459 	for (i = 0; i < geo->bg_channels; i++) {
2460 		max_power = geo->bg[i].max_power;
2461 		tx_power.channels_tx_power[i].channel_number =
2462 		    geo->bg[i].channel;
2463 		tx_power.channels_tx_power[i].tx_power = max_power ?
2464 		    min(max_power, priv->tx_power) : priv->tx_power;
2465 	}
2466 	if (ipw_send_tx_power(priv, &tx_power))
2467 		return -EIO;
2468 
2469 	/* configure device to also handle 'B' band */
2470 	tx_power.ieee_mode = IPW_B_MODE;
2471 	if (ipw_send_tx_power(priv, &tx_power))
2472 		return -EIO;
2473 
2474 	/* configure device to also handle 'A' band */
2475 	if (priv->ieee->abg_true) {
2476 		tx_power.ieee_mode = IPW_A_MODE;
2477 		tx_power.num_channels = geo->a_channels;
2478 		for (i = 0; i < tx_power.num_channels; i++) {
2479 			max_power = geo->a[i].max_power;
2480 			tx_power.channels_tx_power[i].channel_number =
2481 			    geo->a[i].channel;
2482 			tx_power.channels_tx_power[i].tx_power = max_power ?
2483 			    min(max_power, priv->tx_power) : priv->tx_power;
2484 		}
2485 		if (ipw_send_tx_power(priv, &tx_power))
2486 			return -EIO;
2487 	}
2488 	return 0;
2489 }
2490 
2491 static int ipw_send_rts_threshold(struct ipw_priv *priv, u16 rts)
2492 {
2493 	struct ipw_rts_threshold rts_threshold = {
2494 		.rts_threshold = cpu_to_le16(rts),
2495 	};
2496 
2497 	if (!priv) {
2498 		IPW_ERROR("Invalid args\n");
2499 		return -1;
2500 	}
2501 
2502 	return ipw_send_cmd_pdu(priv, IPW_CMD_RTS_THRESHOLD,
2503 				sizeof(rts_threshold), &rts_threshold);
2504 }
2505 
2506 static int ipw_send_frag_threshold(struct ipw_priv *priv, u16 frag)
2507 {
2508 	struct ipw_frag_threshold frag_threshold = {
2509 		.frag_threshold = cpu_to_le16(frag),
2510 	};
2511 
2512 	if (!priv) {
2513 		IPW_ERROR("Invalid args\n");
2514 		return -1;
2515 	}
2516 
2517 	return ipw_send_cmd_pdu(priv, IPW_CMD_FRAG_THRESHOLD,
2518 				sizeof(frag_threshold), &frag_threshold);
2519 }
2520 
2521 static int ipw_send_power_mode(struct ipw_priv *priv, u32 mode)
2522 {
2523 	__le32 param;
2524 
2525 	if (!priv) {
2526 		IPW_ERROR("Invalid args\n");
2527 		return -1;
2528 	}
2529 
2530 	/* If on battery, set to 3, if AC set to CAM, else user
2531 	 * level */
2532 	switch (mode) {
2533 	case IPW_POWER_BATTERY:
2534 		param = cpu_to_le32(IPW_POWER_INDEX_3);
2535 		break;
2536 	case IPW_POWER_AC:
2537 		param = cpu_to_le32(IPW_POWER_MODE_CAM);
2538 		break;
2539 	default:
2540 		param = cpu_to_le32(mode);
2541 		break;
2542 	}
2543 
2544 	return ipw_send_cmd_pdu(priv, IPW_CMD_POWER_MODE, sizeof(param),
2545 				&param);
2546 }
2547 
2548 static int ipw_send_retry_limit(struct ipw_priv *priv, u8 slimit, u8 llimit)
2549 {
2550 	struct ipw_retry_limit retry_limit = {
2551 		.short_retry_limit = slimit,
2552 		.long_retry_limit = llimit
2553 	};
2554 
2555 	if (!priv) {
2556 		IPW_ERROR("Invalid args\n");
2557 		return -1;
2558 	}
2559 
2560 	return ipw_send_cmd_pdu(priv, IPW_CMD_RETRY_LIMIT, sizeof(retry_limit),
2561 				&retry_limit);
2562 }
2563 
2564 /*
2565  * The IPW device contains a Microwire compatible EEPROM that stores
2566  * various data like the MAC address.  Usually the firmware has exclusive
2567  * access to the eeprom, but during device initialization (before the
2568  * device driver has sent the HostComplete command to the firmware) the
2569  * device driver has read access to the EEPROM by way of indirect addressing
2570  * through a couple of memory mapped registers.
2571  *
2572  * The following is a simplified implementation for pulling data out of the
2573  * eeprom, along with some helper functions to find information in
2574  * the per device private data's copy of the eeprom.
2575  *
2576  * NOTE: To better understand how these functions work (i.e what is a chip
2577  *       select and why do have to keep driving the eeprom clock?), read
2578  *       just about any data sheet for a Microwire compatible EEPROM.
2579  */
2580 
2581 /* write a 32 bit value into the indirect accessor register */
2582 static inline void eeprom_write_reg(struct ipw_priv *p, u32 data)
2583 {
2584 	ipw_write_reg32(p, FW_MEM_REG_EEPROM_ACCESS, data);
2585 
2586 	/* the eeprom requires some time to complete the operation */
2587 	udelay(p->eeprom_delay);
2588 }
2589 
2590 /* perform a chip select operation */
2591 static void eeprom_cs(struct ipw_priv *priv)
2592 {
2593 	eeprom_write_reg(priv, 0);
2594 	eeprom_write_reg(priv, EEPROM_BIT_CS);
2595 	eeprom_write_reg(priv, EEPROM_BIT_CS | EEPROM_BIT_SK);
2596 	eeprom_write_reg(priv, EEPROM_BIT_CS);
2597 }
2598 
2599 /* perform a chip select operation */
2600 static void eeprom_disable_cs(struct ipw_priv *priv)
2601 {
2602 	eeprom_write_reg(priv, EEPROM_BIT_CS);
2603 	eeprom_write_reg(priv, 0);
2604 	eeprom_write_reg(priv, EEPROM_BIT_SK);
2605 }
2606 
2607 /* push a single bit down to the eeprom */
2608 static inline void eeprom_write_bit(struct ipw_priv *p, u8 bit)
2609 {
2610 	int d = (bit ? EEPROM_BIT_DI : 0);
2611 	eeprom_write_reg(p, EEPROM_BIT_CS | d);
2612 	eeprom_write_reg(p, EEPROM_BIT_CS | d | EEPROM_BIT_SK);
2613 }
2614 
2615 /* push an opcode followed by an address down to the eeprom */
2616 static void eeprom_op(struct ipw_priv *priv, u8 op, u8 addr)
2617 {
2618 	int i;
2619 
2620 	eeprom_cs(priv);
2621 	eeprom_write_bit(priv, 1);
2622 	eeprom_write_bit(priv, op & 2);
2623 	eeprom_write_bit(priv, op & 1);
2624 	for (i = 7; i >= 0; i--) {
2625 		eeprom_write_bit(priv, addr & (1 << i));
2626 	}
2627 }
2628 
2629 /* pull 16 bits off the eeprom, one bit at a time */
2630 static u16 eeprom_read_u16(struct ipw_priv *priv, u8 addr)
2631 {
2632 	int i;
2633 	u16 r = 0;
2634 
2635 	/* Send READ Opcode */
2636 	eeprom_op(priv, EEPROM_CMD_READ, addr);
2637 
2638 	/* Send dummy bit */
2639 	eeprom_write_reg(priv, EEPROM_BIT_CS);
2640 
2641 	/* Read the byte off the eeprom one bit at a time */
2642 	for (i = 0; i < 16; i++) {
2643 		u32 data = 0;
2644 		eeprom_write_reg(priv, EEPROM_BIT_CS | EEPROM_BIT_SK);
2645 		eeprom_write_reg(priv, EEPROM_BIT_CS);
2646 		data = ipw_read_reg32(priv, FW_MEM_REG_EEPROM_ACCESS);
2647 		r = (r << 1) | ((data & EEPROM_BIT_DO) ? 1 : 0);
2648 	}
2649 
2650 	/* Send another dummy bit */
2651 	eeprom_write_reg(priv, 0);
2652 	eeprom_disable_cs(priv);
2653 
2654 	return r;
2655 }
2656 
2657 /* helper function for pulling the mac address out of the private */
2658 /* data's copy of the eeprom data                                 */
2659 static void eeprom_parse_mac(struct ipw_priv *priv, u8 * mac)
2660 {
2661 	memcpy(mac, &priv->eeprom[EEPROM_MAC_ADDRESS], ETH_ALEN);
2662 }
2663 
2664 static void ipw_read_eeprom(struct ipw_priv *priv)
2665 {
2666 	int i;
2667 	__le16 *eeprom = (__le16 *) priv->eeprom;
2668 
2669 	IPW_DEBUG_TRACE(">>\n");
2670 
2671 	/* read entire contents of eeprom into private buffer */
2672 	for (i = 0; i < 128; i++)
2673 		eeprom[i] = cpu_to_le16(eeprom_read_u16(priv, (u8) i));
2674 
2675 	IPW_DEBUG_TRACE("<<\n");
2676 }
2677 
2678 /*
2679  * Either the device driver (i.e. the host) or the firmware can
2680  * load eeprom data into the designated region in SRAM.  If neither
2681  * happens then the FW will shutdown with a fatal error.
2682  *
2683  * In order to signal the FW to load the EEPROM, the EEPROM_LOAD_DISABLE
2684  * bit needs region of shared SRAM needs to be non-zero.
2685  */
2686 static void ipw_eeprom_init_sram(struct ipw_priv *priv)
2687 {
2688 	int i;
2689 
2690 	IPW_DEBUG_TRACE(">>\n");
2691 
2692 	/*
2693 	   If the data looks correct, then copy it to our private
2694 	   copy.  Otherwise let the firmware know to perform the operation
2695 	   on its own.
2696 	 */
2697 	if (priv->eeprom[EEPROM_VERSION] != 0) {
2698 		IPW_DEBUG_INFO("Writing EEPROM data into SRAM\n");
2699 
2700 		/* write the eeprom data to sram */
2701 		for (i = 0; i < IPW_EEPROM_IMAGE_SIZE; i++)
2702 			ipw_write8(priv, IPW_EEPROM_DATA + i, priv->eeprom[i]);
2703 
2704 		/* Do not load eeprom data on fatal error or suspend */
2705 		ipw_write32(priv, IPW_EEPROM_LOAD_DISABLE, 0);
2706 	} else {
2707 		IPW_DEBUG_INFO("Enabling FW initialization of SRAM\n");
2708 
2709 		/* Load eeprom data on fatal error or suspend */
2710 		ipw_write32(priv, IPW_EEPROM_LOAD_DISABLE, 1);
2711 	}
2712 
2713 	IPW_DEBUG_TRACE("<<\n");
2714 }
2715 
2716 static void ipw_zero_memory(struct ipw_priv *priv, u32 start, u32 count)
2717 {
2718 	count >>= 2;
2719 	if (!count)
2720 		return;
2721 	_ipw_write32(priv, IPW_AUTOINC_ADDR, start);
2722 	while (count--)
2723 		_ipw_write32(priv, IPW_AUTOINC_DATA, 0);
2724 }
2725 
2726 static inline void ipw_fw_dma_reset_command_blocks(struct ipw_priv *priv)
2727 {
2728 	ipw_zero_memory(priv, IPW_SHARED_SRAM_DMA_CONTROL,
2729 			CB_NUMBER_OF_ELEMENTS_SMALL *
2730 			sizeof(struct command_block));
2731 }
2732 
2733 static int ipw_fw_dma_enable(struct ipw_priv *priv)
2734 {				/* start dma engine but no transfers yet */
2735 
2736 	IPW_DEBUG_FW(">> :\n");
2737 
2738 	/* Start the dma */
2739 	ipw_fw_dma_reset_command_blocks(priv);
2740 
2741 	/* Write CB base address */
2742 	ipw_write_reg32(priv, IPW_DMA_I_CB_BASE, IPW_SHARED_SRAM_DMA_CONTROL);
2743 
2744 	IPW_DEBUG_FW("<< :\n");
2745 	return 0;
2746 }
2747 
2748 static void ipw_fw_dma_abort(struct ipw_priv *priv)
2749 {
2750 	u32 control = 0;
2751 
2752 	IPW_DEBUG_FW(">> :\n");
2753 
2754 	/* set the Stop and Abort bit */
2755 	control = DMA_CONTROL_SMALL_CB_CONST_VALUE | DMA_CB_STOP_AND_ABORT;
2756 	ipw_write_reg32(priv, IPW_DMA_I_DMA_CONTROL, control);
2757 	priv->sram_desc.last_cb_index = 0;
2758 
2759 	IPW_DEBUG_FW("<<\n");
2760 }
2761 
2762 static int ipw_fw_dma_write_command_block(struct ipw_priv *priv, int index,
2763 					  struct command_block *cb)
2764 {
2765 	u32 address =
2766 	    IPW_SHARED_SRAM_DMA_CONTROL +
2767 	    (sizeof(struct command_block) * index);
2768 	IPW_DEBUG_FW(">> :\n");
2769 
2770 	ipw_write_indirect(priv, address, (u8 *) cb,
2771 			   (int)sizeof(struct command_block));
2772 
2773 	IPW_DEBUG_FW("<< :\n");
2774 	return 0;
2775 
2776 }
2777 
2778 static int ipw_fw_dma_kick(struct ipw_priv *priv)
2779 {
2780 	u32 control = 0;
2781 	u32 index = 0;
2782 
2783 	IPW_DEBUG_FW(">> :\n");
2784 
2785 	for (index = 0; index < priv->sram_desc.last_cb_index; index++)
2786 		ipw_fw_dma_write_command_block(priv, index,
2787 					       &priv->sram_desc.cb_list[index]);
2788 
2789 	/* Enable the DMA in the CSR register */
2790 	ipw_clear_bit(priv, IPW_RESET_REG,
2791 		      IPW_RESET_REG_MASTER_DISABLED |
2792 		      IPW_RESET_REG_STOP_MASTER);
2793 
2794 	/* Set the Start bit. */
2795 	control = DMA_CONTROL_SMALL_CB_CONST_VALUE | DMA_CB_START;
2796 	ipw_write_reg32(priv, IPW_DMA_I_DMA_CONTROL, control);
2797 
2798 	IPW_DEBUG_FW("<< :\n");
2799 	return 0;
2800 }
2801 
2802 static void ipw_fw_dma_dump_command_block(struct ipw_priv *priv)
2803 {
2804 	u32 address;
2805 	u32 register_value = 0;
2806 	u32 cb_fields_address = 0;
2807 
2808 	IPW_DEBUG_FW(">> :\n");
2809 	address = ipw_read_reg32(priv, IPW_DMA_I_CURRENT_CB);
2810 	IPW_DEBUG_FW_INFO("Current CB is 0x%x\n", address);
2811 
2812 	/* Read the DMA Controlor register */
2813 	register_value = ipw_read_reg32(priv, IPW_DMA_I_DMA_CONTROL);
2814 	IPW_DEBUG_FW_INFO("IPW_DMA_I_DMA_CONTROL is 0x%x\n", register_value);
2815 
2816 	/* Print the CB values */
2817 	cb_fields_address = address;
2818 	register_value = ipw_read_reg32(priv, cb_fields_address);
2819 	IPW_DEBUG_FW_INFO("Current CB Control Field is 0x%x\n", register_value);
2820 
2821 	cb_fields_address += sizeof(u32);
2822 	register_value = ipw_read_reg32(priv, cb_fields_address);
2823 	IPW_DEBUG_FW_INFO("Current CB Source Field is 0x%x\n", register_value);
2824 
2825 	cb_fields_address += sizeof(u32);
2826 	register_value = ipw_read_reg32(priv, cb_fields_address);
2827 	IPW_DEBUG_FW_INFO("Current CB Destination Field is 0x%x\n",
2828 			  register_value);
2829 
2830 	cb_fields_address += sizeof(u32);
2831 	register_value = ipw_read_reg32(priv, cb_fields_address);
2832 	IPW_DEBUG_FW_INFO("Current CB Status Field is 0x%x\n", register_value);
2833 
2834 	IPW_DEBUG_FW(">> :\n");
2835 }
2836 
2837 static int ipw_fw_dma_command_block_index(struct ipw_priv *priv)
2838 {
2839 	u32 current_cb_address = 0;
2840 	u32 current_cb_index = 0;
2841 
2842 	IPW_DEBUG_FW("<< :\n");
2843 	current_cb_address = ipw_read_reg32(priv, IPW_DMA_I_CURRENT_CB);
2844 
2845 	current_cb_index = (current_cb_address - IPW_SHARED_SRAM_DMA_CONTROL) /
2846 	    sizeof(struct command_block);
2847 
2848 	IPW_DEBUG_FW_INFO("Current CB index 0x%x address = 0x%X\n",
2849 			  current_cb_index, current_cb_address);
2850 
2851 	IPW_DEBUG_FW(">> :\n");
2852 	return current_cb_index;
2853 
2854 }
2855 
2856 static int ipw_fw_dma_add_command_block(struct ipw_priv *priv,
2857 					u32 src_address,
2858 					u32 dest_address,
2859 					u32 length,
2860 					int interrupt_enabled, int is_last)
2861 {
2862 
2863 	u32 control = CB_VALID | CB_SRC_LE | CB_DEST_LE | CB_SRC_AUTOINC |
2864 	    CB_SRC_IO_GATED | CB_DEST_AUTOINC | CB_SRC_SIZE_LONG |
2865 	    CB_DEST_SIZE_LONG;
2866 	struct command_block *cb;
2867 	u32 last_cb_element = 0;
2868 
2869 	IPW_DEBUG_FW_INFO("src_address=0x%x dest_address=0x%x length=0x%x\n",
2870 			  src_address, dest_address, length);
2871 
2872 	if (priv->sram_desc.last_cb_index >= CB_NUMBER_OF_ELEMENTS_SMALL)
2873 		return -1;
2874 
2875 	last_cb_element = priv->sram_desc.last_cb_index;
2876 	cb = &priv->sram_desc.cb_list[last_cb_element];
2877 	priv->sram_desc.last_cb_index++;
2878 
2879 	/* Calculate the new CB control word */
2880 	if (interrupt_enabled)
2881 		control |= CB_INT_ENABLED;
2882 
2883 	if (is_last)
2884 		control |= CB_LAST_VALID;
2885 
2886 	control |= length;
2887 
2888 	/* Calculate the CB Element's checksum value */
2889 	cb->status = control ^ src_address ^ dest_address;
2890 
2891 	/* Copy the Source and Destination addresses */
2892 	cb->dest_addr = dest_address;
2893 	cb->source_addr = src_address;
2894 
2895 	/* Copy the Control Word last */
2896 	cb->control = control;
2897 
2898 	return 0;
2899 }
2900 
2901 static int ipw_fw_dma_add_buffer(struct ipw_priv *priv, dma_addr_t *src_address,
2902 				 int nr, u32 dest_address, u32 len)
2903 {
2904 	int ret, i;
2905 	u32 size;
2906 
2907 	IPW_DEBUG_FW(">>\n");
2908 	IPW_DEBUG_FW_INFO("nr=%d dest_address=0x%x len=0x%x\n",
2909 			  nr, dest_address, len);
2910 
2911 	for (i = 0; i < nr; i++) {
2912 		size = min_t(u32, len - i * CB_MAX_LENGTH, CB_MAX_LENGTH);
2913 		ret = ipw_fw_dma_add_command_block(priv, src_address[i],
2914 						   dest_address +
2915 						   i * CB_MAX_LENGTH, size,
2916 						   0, 0);
2917 		if (ret) {
2918 			IPW_DEBUG_FW_INFO(": Failed\n");
2919 			return -1;
2920 		} else
2921 			IPW_DEBUG_FW_INFO(": Added new cb\n");
2922 	}
2923 
2924 	IPW_DEBUG_FW("<<\n");
2925 	return 0;
2926 }
2927 
2928 static int ipw_fw_dma_wait(struct ipw_priv *priv)
2929 {
2930 	u32 current_index = 0, previous_index;
2931 	u32 watchdog = 0;
2932 
2933 	IPW_DEBUG_FW(">> :\n");
2934 
2935 	current_index = ipw_fw_dma_command_block_index(priv);
2936 	IPW_DEBUG_FW_INFO("sram_desc.last_cb_index:0x%08X\n",
2937 			  (int)priv->sram_desc.last_cb_index);
2938 
2939 	while (current_index < priv->sram_desc.last_cb_index) {
2940 		udelay(50);
2941 		previous_index = current_index;
2942 		current_index = ipw_fw_dma_command_block_index(priv);
2943 
2944 		if (previous_index < current_index) {
2945 			watchdog = 0;
2946 			continue;
2947 		}
2948 		if (++watchdog > 400) {
2949 			IPW_DEBUG_FW_INFO("Timeout\n");
2950 			ipw_fw_dma_dump_command_block(priv);
2951 			ipw_fw_dma_abort(priv);
2952 			return -1;
2953 		}
2954 	}
2955 
2956 	ipw_fw_dma_abort(priv);
2957 
2958 	/*Disable the DMA in the CSR register */
2959 	ipw_set_bit(priv, IPW_RESET_REG,
2960 		    IPW_RESET_REG_MASTER_DISABLED | IPW_RESET_REG_STOP_MASTER);
2961 
2962 	IPW_DEBUG_FW("<< dmaWaitSync\n");
2963 	return 0;
2964 }
2965 
2966 static void ipw_remove_current_network(struct ipw_priv *priv)
2967 {
2968 	struct list_head *element, *safe;
2969 	struct libipw_network *network = NULL;
2970 	unsigned long flags;
2971 
2972 	spin_lock_irqsave(&priv->ieee->lock, flags);
2973 	list_for_each_safe(element, safe, &priv->ieee->network_list) {
2974 		network = list_entry(element, struct libipw_network, list);
2975 		if (ether_addr_equal(network->bssid, priv->bssid)) {
2976 			list_del(element);
2977 			list_add_tail(&network->list,
2978 				      &priv->ieee->network_free_list);
2979 		}
2980 	}
2981 	spin_unlock_irqrestore(&priv->ieee->lock, flags);
2982 }
2983 
2984 /* timeout in msec, attempted in 10-msec quanta */
2985 static int ipw_poll_bit(struct ipw_priv *priv, u32 addr, u32 mask,
2986 			       int timeout)
2987 {
2988 	int i = 0;
2989 
2990 	do {
2991 		if ((ipw_read32(priv, addr) & mask) == mask)
2992 			return i;
2993 		mdelay(10);
2994 		i += 10;
2995 	} while (i < timeout);
2996 
2997 	return -ETIME;
2998 }
2999 
3000 /* These functions load the firmware and micro code for the operation of
3001  * the ipw hardware.  It assumes the buffer has all the bits for the
3002  * image and the caller is handling the memory allocation and clean up.
3003  */
3004 
3005 static int ipw_stop_master(struct ipw_priv *priv)
3006 {
3007 	int rc;
3008 
3009 	IPW_DEBUG_TRACE(">>\n");
3010 	/* stop master. typical delay - 0 */
3011 	ipw_set_bit(priv, IPW_RESET_REG, IPW_RESET_REG_STOP_MASTER);
3012 
3013 	/* timeout is in msec, polled in 10-msec quanta */
3014 	rc = ipw_poll_bit(priv, IPW_RESET_REG,
3015 			  IPW_RESET_REG_MASTER_DISABLED, 100);
3016 	if (rc < 0) {
3017 		IPW_ERROR("wait for stop master failed after 100ms\n");
3018 		return -1;
3019 	}
3020 
3021 	IPW_DEBUG_INFO("stop master %dms\n", rc);
3022 
3023 	return rc;
3024 }
3025 
3026 static void ipw_arc_release(struct ipw_priv *priv)
3027 {
3028 	IPW_DEBUG_TRACE(">>\n");
3029 	mdelay(5);
3030 
3031 	ipw_clear_bit(priv, IPW_RESET_REG, CBD_RESET_REG_PRINCETON_RESET);
3032 
3033 	/* no one knows timing, for safety add some delay */
3034 	mdelay(5);
3035 }
3036 
3037 struct fw_chunk {
3038 	__le32 address;
3039 	__le32 length;
3040 };
3041 
3042 static int ipw_load_ucode(struct ipw_priv *priv, u8 * data, size_t len)
3043 {
3044 	int rc = 0, i, addr;
3045 	u8 cr = 0;
3046 	__le16 *image;
3047 
3048 	image = (__le16 *) data;
3049 
3050 	IPW_DEBUG_TRACE(">>\n");
3051 
3052 	rc = ipw_stop_master(priv);
3053 
3054 	if (rc < 0)
3055 		return rc;
3056 
3057 	for (addr = IPW_SHARED_LOWER_BOUND;
3058 	     addr < IPW_REGISTER_DOMAIN1_END; addr += 4) {
3059 		ipw_write32(priv, addr, 0);
3060 	}
3061 
3062 	/* no ucode (yet) */
3063 	memset(&priv->dino_alive, 0, sizeof(priv->dino_alive));
3064 	/* destroy DMA queues */
3065 	/* reset sequence */
3066 
3067 	ipw_write_reg32(priv, IPW_MEM_HALT_AND_RESET, IPW_BIT_HALT_RESET_ON);
3068 	ipw_arc_release(priv);
3069 	ipw_write_reg32(priv, IPW_MEM_HALT_AND_RESET, IPW_BIT_HALT_RESET_OFF);
3070 	mdelay(1);
3071 
3072 	/* reset PHY */
3073 	ipw_write_reg32(priv, IPW_INTERNAL_CMD_EVENT, IPW_BASEBAND_POWER_DOWN);
3074 	mdelay(1);
3075 
3076 	ipw_write_reg32(priv, IPW_INTERNAL_CMD_EVENT, 0);
3077 	mdelay(1);
3078 
3079 	/* enable ucode store */
3080 	ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, 0x0);
3081 	ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, DINO_ENABLE_CS);
3082 	mdelay(1);
3083 
3084 	/* write ucode */
3085 	/*
3086 	 * @bug
3087 	 * Do NOT set indirect address register once and then
3088 	 * store data to indirect data register in the loop.
3089 	 * It seems very reasonable, but in this case DINO do not
3090 	 * accept ucode. It is essential to set address each time.
3091 	 */
3092 	/* load new ipw uCode */
3093 	for (i = 0; i < len / 2; i++)
3094 		ipw_write_reg16(priv, IPW_BASEBAND_CONTROL_STORE,
3095 				le16_to_cpu(image[i]));
3096 
3097 	/* enable DINO */
3098 	ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, 0);
3099 	ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, DINO_ENABLE_SYSTEM);
3100 
3101 	/* this is where the igx / win driver deveates from the VAP driver. */
3102 
3103 	/* wait for alive response */
3104 	for (i = 0; i < 100; i++) {
3105 		/* poll for incoming data */
3106 		cr = ipw_read_reg8(priv, IPW_BASEBAND_CONTROL_STATUS);
3107 		if (cr & DINO_RXFIFO_DATA)
3108 			break;
3109 		mdelay(1);
3110 	}
3111 
3112 	if (cr & DINO_RXFIFO_DATA) {
3113 		/* alive_command_responce size is NOT multiple of 4 */
3114 		__le32 response_buffer[(sizeof(priv->dino_alive) + 3) / 4];
3115 
3116 		for (i = 0; i < ARRAY_SIZE(response_buffer); i++)
3117 			response_buffer[i] =
3118 			    cpu_to_le32(ipw_read_reg32(priv,
3119 						       IPW_BASEBAND_RX_FIFO_READ));
3120 		memcpy(&priv->dino_alive, response_buffer,
3121 		       sizeof(priv->dino_alive));
3122 		if (priv->dino_alive.alive_command == 1
3123 		    && priv->dino_alive.ucode_valid == 1) {
3124 			rc = 0;
3125 			IPW_DEBUG_INFO
3126 			    ("Microcode OK, rev. %d (0x%x) dev. %d (0x%x) "
3127 			     "of %02d/%02d/%02d %02d:%02d\n",
3128 			     priv->dino_alive.software_revision,
3129 			     priv->dino_alive.software_revision,
3130 			     priv->dino_alive.device_identifier,
3131 			     priv->dino_alive.device_identifier,
3132 			     priv->dino_alive.time_stamp[0],
3133 			     priv->dino_alive.time_stamp[1],
3134 			     priv->dino_alive.time_stamp[2],
3135 			     priv->dino_alive.time_stamp[3],
3136 			     priv->dino_alive.time_stamp[4]);
3137 		} else {
3138 			IPW_DEBUG_INFO("Microcode is not alive\n");
3139 			rc = -EINVAL;
3140 		}
3141 	} else {
3142 		IPW_DEBUG_INFO("No alive response from DINO\n");
3143 		rc = -ETIME;
3144 	}
3145 
3146 	/* disable DINO, otherwise for some reason
3147 	   firmware have problem getting alive resp. */
3148 	ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, 0);
3149 
3150 	return rc;
3151 }
3152 
3153 static int ipw_load_firmware(struct ipw_priv *priv, u8 * data, size_t len)
3154 {
3155 	int ret = -1;
3156 	int offset = 0;
3157 	struct fw_chunk *chunk;
3158 	int total_nr = 0;
3159 	int i;
3160 	struct dma_pool *pool;
3161 	void **virts;
3162 	dma_addr_t *phys;
3163 
3164 	IPW_DEBUG_TRACE("<< :\n");
3165 
3166 	virts = kmalloc_array(CB_NUMBER_OF_ELEMENTS_SMALL, sizeof(void *),
3167 			      GFP_KERNEL);
3168 	if (!virts)
3169 		return -ENOMEM;
3170 
3171 	phys = kmalloc_array(CB_NUMBER_OF_ELEMENTS_SMALL, sizeof(dma_addr_t),
3172 			     GFP_KERNEL);
3173 	if (!phys) {
3174 		kfree(virts);
3175 		return -ENOMEM;
3176 	}
3177 	pool = dma_pool_create("ipw2200", &priv->pci_dev->dev, CB_MAX_LENGTH, 0,
3178 			       0);
3179 	if (!pool) {
3180 		IPW_ERROR("dma_pool_create failed\n");
3181 		kfree(phys);
3182 		kfree(virts);
3183 		return -ENOMEM;
3184 	}
3185 
3186 	/* Start the Dma */
3187 	ret = ipw_fw_dma_enable(priv);
3188 
3189 	/* the DMA is already ready this would be a bug. */
3190 	BUG_ON(priv->sram_desc.last_cb_index > 0);
3191 
3192 	do {
3193 		u32 chunk_len;
3194 		u8 *start;
3195 		int size;
3196 		int nr = 0;
3197 
3198 		chunk = (struct fw_chunk *)(data + offset);
3199 		offset += sizeof(struct fw_chunk);
3200 		chunk_len = le32_to_cpu(chunk->length);
3201 		start = data + offset;
3202 
3203 		nr = (chunk_len + CB_MAX_LENGTH - 1) / CB_MAX_LENGTH;
3204 		for (i = 0; i < nr; i++) {
3205 			virts[total_nr] = dma_pool_alloc(pool, GFP_KERNEL,
3206 							 &phys[total_nr]);
3207 			if (!virts[total_nr]) {
3208 				ret = -ENOMEM;
3209 				goto out;
3210 			}
3211 			size = min_t(u32, chunk_len - i * CB_MAX_LENGTH,
3212 				     CB_MAX_LENGTH);
3213 			memcpy(virts[total_nr], start, size);
3214 			start += size;
3215 			total_nr++;
3216 			/* We don't support fw chunk larger than 64*8K */
3217 			BUG_ON(total_nr > CB_NUMBER_OF_ELEMENTS_SMALL);
3218 		}
3219 
3220 		/* build DMA packet and queue up for sending */
3221 		/* dma to chunk->address, the chunk->length bytes from data +
3222 		 * offeset*/
3223 		/* Dma loading */
3224 		ret = ipw_fw_dma_add_buffer(priv, &phys[total_nr - nr],
3225 					    nr, le32_to_cpu(chunk->address),
3226 					    chunk_len);
3227 		if (ret) {
3228 			IPW_DEBUG_INFO("dmaAddBuffer Failed\n");
3229 			goto out;
3230 		}
3231 
3232 		offset += chunk_len;
3233 	} while (offset < len);
3234 
3235 	/* Run the DMA and wait for the answer */
3236 	ret = ipw_fw_dma_kick(priv);
3237 	if (ret) {
3238 		IPW_ERROR("dmaKick Failed\n");
3239 		goto out;
3240 	}
3241 
3242 	ret = ipw_fw_dma_wait(priv);
3243 	if (ret) {
3244 		IPW_ERROR("dmaWaitSync Failed\n");
3245 		goto out;
3246 	}
3247  out:
3248 	for (i = 0; i < total_nr; i++)
3249 		dma_pool_free(pool, virts[i], phys[i]);
3250 
3251 	dma_pool_destroy(pool);
3252 	kfree(phys);
3253 	kfree(virts);
3254 
3255 	return ret;
3256 }
3257 
3258 /* stop nic */
3259 static int ipw_stop_nic(struct ipw_priv *priv)
3260 {
3261 	int rc = 0;
3262 
3263 	/* stop */
3264 	ipw_write32(priv, IPW_RESET_REG, IPW_RESET_REG_STOP_MASTER);
3265 
3266 	rc = ipw_poll_bit(priv, IPW_RESET_REG,
3267 			  IPW_RESET_REG_MASTER_DISABLED, 500);
3268 	if (rc < 0) {
3269 		IPW_ERROR("wait for reg master disabled failed after 500ms\n");
3270 		return rc;
3271 	}
3272 
3273 	ipw_set_bit(priv, IPW_RESET_REG, CBD_RESET_REG_PRINCETON_RESET);
3274 
3275 	return rc;
3276 }
3277 
3278 static void ipw_start_nic(struct ipw_priv *priv)
3279 {
3280 	IPW_DEBUG_TRACE(">>\n");
3281 
3282 	/* prvHwStartNic  release ARC */
3283 	ipw_clear_bit(priv, IPW_RESET_REG,
3284 		      IPW_RESET_REG_MASTER_DISABLED |
3285 		      IPW_RESET_REG_STOP_MASTER |
3286 		      CBD_RESET_REG_PRINCETON_RESET);
3287 
3288 	/* enable power management */
3289 	ipw_set_bit(priv, IPW_GP_CNTRL_RW,
3290 		    IPW_GP_CNTRL_BIT_HOST_ALLOWS_STANDBY);
3291 
3292 	IPW_DEBUG_TRACE("<<\n");
3293 }
3294 
3295 static int ipw_init_nic(struct ipw_priv *priv)
3296 {
3297 	int rc;
3298 
3299 	IPW_DEBUG_TRACE(">>\n");
3300 	/* reset */
3301 	/*prvHwInitNic */
3302 	/* set "initialization complete" bit to move adapter to D0 state */
3303 	ipw_set_bit(priv, IPW_GP_CNTRL_RW, IPW_GP_CNTRL_BIT_INIT_DONE);
3304 
3305 	/* low-level PLL activation */
3306 	ipw_write32(priv, IPW_READ_INT_REGISTER,
3307 		    IPW_BIT_INT_HOST_SRAM_READ_INT_REGISTER);
3308 
3309 	/* wait for clock stabilization */
3310 	rc = ipw_poll_bit(priv, IPW_GP_CNTRL_RW,
3311 			  IPW_GP_CNTRL_BIT_CLOCK_READY, 250);
3312 	if (rc < 0)
3313 		IPW_DEBUG_INFO("FAILED wait for clock stablization\n");
3314 
3315 	/* assert SW reset */
3316 	ipw_set_bit(priv, IPW_RESET_REG, IPW_RESET_REG_SW_RESET);
3317 
3318 	udelay(10);
3319 
3320 	/* set "initialization complete" bit to move adapter to D0 state */
3321 	ipw_set_bit(priv, IPW_GP_CNTRL_RW, IPW_GP_CNTRL_BIT_INIT_DONE);
3322 
3323 	IPW_DEBUG_TRACE(">>\n");
3324 	return 0;
3325 }
3326 
3327 /* Call this function from process context, it will sleep in request_firmware.
3328  * Probe is an ok place to call this from.
3329  */
3330 static int ipw_reset_nic(struct ipw_priv *priv)
3331 {
3332 	int rc = 0;
3333 	unsigned long flags;
3334 
3335 	IPW_DEBUG_TRACE(">>\n");
3336 
3337 	rc = ipw_init_nic(priv);
3338 
3339 	spin_lock_irqsave(&priv->lock, flags);
3340 	/* Clear the 'host command active' bit... */
3341 	priv->status &= ~STATUS_HCMD_ACTIVE;
3342 	wake_up_interruptible(&priv->wait_command_queue);
3343 	priv->status &= ~(STATUS_SCANNING | STATUS_SCAN_ABORTING);
3344 	wake_up_interruptible(&priv->wait_state);
3345 	spin_unlock_irqrestore(&priv->lock, flags);
3346 
3347 	IPW_DEBUG_TRACE("<<\n");
3348 	return rc;
3349 }
3350 
3351 
3352 struct ipw_fw {
3353 	__le32 ver;
3354 	__le32 boot_size;
3355 	__le32 ucode_size;
3356 	__le32 fw_size;
3357 	u8 data[];
3358 };
3359 
3360 static int ipw_get_fw(struct ipw_priv *priv,
3361 		      const struct firmware **raw, const char *name)
3362 {
3363 	struct ipw_fw *fw;
3364 	int rc;
3365 
3366 	/* ask firmware_class module to get the boot firmware off disk */
3367 	rc = request_firmware(raw, name, &priv->pci_dev->dev);
3368 	if (rc < 0) {
3369 		IPW_ERROR("%s request_firmware failed: Reason %d\n", name, rc);
3370 		return rc;
3371 	}
3372 
3373 	if ((*raw)->size < sizeof(*fw)) {
3374 		IPW_ERROR("%s is too small (%zd)\n", name, (*raw)->size);
3375 		return -EINVAL;
3376 	}
3377 
3378 	fw = (void *)(*raw)->data;
3379 
3380 	if ((*raw)->size < sizeof(*fw) + le32_to_cpu(fw->boot_size) +
3381 	    le32_to_cpu(fw->ucode_size) + le32_to_cpu(fw->fw_size)) {
3382 		IPW_ERROR("%s is too small or corrupt (%zd)\n",
3383 			  name, (*raw)->size);
3384 		return -EINVAL;
3385 	}
3386 
3387 	IPW_DEBUG_INFO("Read firmware '%s' image v%d.%d (%zd bytes)\n",
3388 		       name,
3389 		       le32_to_cpu(fw->ver) >> 16,
3390 		       le32_to_cpu(fw->ver) & 0xff,
3391 		       (*raw)->size - sizeof(*fw));
3392 	return 0;
3393 }
3394 
3395 #define IPW_RX_BUF_SIZE (3000)
3396 
3397 static void ipw_rx_queue_reset(struct ipw_priv *priv,
3398 				      struct ipw_rx_queue *rxq)
3399 {
3400 	unsigned long flags;
3401 	int i;
3402 
3403 	spin_lock_irqsave(&rxq->lock, flags);
3404 
3405 	INIT_LIST_HEAD(&rxq->rx_free);
3406 	INIT_LIST_HEAD(&rxq->rx_used);
3407 
3408 	/* Fill the rx_used queue with _all_ of the Rx buffers */
3409 	for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3410 		/* In the reset function, these buffers may have been allocated
3411 		 * to an SKB, so we need to unmap and free potential storage */
3412 		if (rxq->pool[i].skb != NULL) {
3413 			dma_unmap_single(&priv->pci_dev->dev,
3414 					 rxq->pool[i].dma_addr,
3415 					 IPW_RX_BUF_SIZE, DMA_FROM_DEVICE);
3416 			dev_kfree_skb_irq(rxq->pool[i].skb);
3417 			rxq->pool[i].skb = NULL;
3418 		}
3419 		list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3420 	}
3421 
3422 	/* Set us so that we have processed and used all buffers, but have
3423 	 * not restocked the Rx queue with fresh buffers */
3424 	rxq->read = rxq->write = 0;
3425 	rxq->free_count = 0;
3426 	spin_unlock_irqrestore(&rxq->lock, flags);
3427 }
3428 
3429 #ifdef CONFIG_PM
3430 static int fw_loaded = 0;
3431 static const struct firmware *raw = NULL;
3432 
3433 static void free_firmware(void)
3434 {
3435 	if (fw_loaded) {
3436 		release_firmware(raw);
3437 		raw = NULL;
3438 		fw_loaded = 0;
3439 	}
3440 }
3441 #else
3442 #define free_firmware() do {} while (0)
3443 #endif
3444 
3445 static int ipw_load(struct ipw_priv *priv)
3446 {
3447 #ifndef CONFIG_PM
3448 	const struct firmware *raw = NULL;
3449 #endif
3450 	struct ipw_fw *fw;
3451 	u8 *boot_img, *ucode_img, *fw_img;
3452 	u8 *name = NULL;
3453 	int rc = 0, retries = 3;
3454 
3455 	switch (priv->ieee->iw_mode) {
3456 	case IW_MODE_ADHOC:
3457 		name = "ipw2200-ibss.fw";
3458 		break;
3459 #ifdef CONFIG_IPW2200_MONITOR
3460 	case IW_MODE_MONITOR:
3461 		name = "ipw2200-sniffer.fw";
3462 		break;
3463 #endif
3464 	case IW_MODE_INFRA:
3465 		name = "ipw2200-bss.fw";
3466 		break;
3467 	}
3468 
3469 	if (!name) {
3470 		rc = -EINVAL;
3471 		goto error;
3472 	}
3473 
3474 #ifdef CONFIG_PM
3475 	if (!fw_loaded) {
3476 #endif
3477 		rc = ipw_get_fw(priv, &raw, name);
3478 		if (rc < 0)
3479 			goto error;
3480 #ifdef CONFIG_PM
3481 	}
3482 #endif
3483 
3484 	fw = (void *)raw->data;
3485 	boot_img = &fw->data[0];
3486 	ucode_img = &fw->data[le32_to_cpu(fw->boot_size)];
3487 	fw_img = &fw->data[le32_to_cpu(fw->boot_size) +
3488 			   le32_to_cpu(fw->ucode_size)];
3489 
3490 	if (!priv->rxq)
3491 		priv->rxq = ipw_rx_queue_alloc(priv);
3492 	else
3493 		ipw_rx_queue_reset(priv, priv->rxq);
3494 	if (!priv->rxq) {
3495 		IPW_ERROR("Unable to initialize Rx queue\n");
3496 		rc = -ENOMEM;
3497 		goto error;
3498 	}
3499 
3500       retry:
3501 	/* Ensure interrupts are disabled */
3502 	ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);
3503 	priv->status &= ~STATUS_INT_ENABLED;
3504 
3505 	/* ack pending interrupts */
3506 	ipw_write32(priv, IPW_INTA_RW, IPW_INTA_MASK_ALL);
3507 
3508 	ipw_stop_nic(priv);
3509 
3510 	rc = ipw_reset_nic(priv);
3511 	if (rc < 0) {
3512 		IPW_ERROR("Unable to reset NIC\n");
3513 		goto error;
3514 	}
3515 
3516 	ipw_zero_memory(priv, IPW_NIC_SRAM_LOWER_BOUND,
3517 			IPW_NIC_SRAM_UPPER_BOUND - IPW_NIC_SRAM_LOWER_BOUND);
3518 
3519 	/* DMA the initial boot firmware into the device */
3520 	rc = ipw_load_firmware(priv, boot_img, le32_to_cpu(fw->boot_size));
3521 	if (rc < 0) {
3522 		IPW_ERROR("Unable to load boot firmware: %d\n", rc);
3523 		goto error;
3524 	}
3525 
3526 	/* kick start the device */
3527 	ipw_start_nic(priv);
3528 
3529 	/* wait for the device to finish its initial startup sequence */
3530 	rc = ipw_poll_bit(priv, IPW_INTA_RW,
3531 			  IPW_INTA_BIT_FW_INITIALIZATION_DONE, 500);
3532 	if (rc < 0) {
3533 		IPW_ERROR("device failed to boot initial fw image\n");
3534 		goto error;
3535 	}
3536 	IPW_DEBUG_INFO("initial device response after %dms\n", rc);
3537 
3538 	/* ack fw init done interrupt */
3539 	ipw_write32(priv, IPW_INTA_RW, IPW_INTA_BIT_FW_INITIALIZATION_DONE);
3540 
3541 	/* DMA the ucode into the device */
3542 	rc = ipw_load_ucode(priv, ucode_img, le32_to_cpu(fw->ucode_size));
3543 	if (rc < 0) {
3544 		IPW_ERROR("Unable to load ucode: %d\n", rc);
3545 		goto error;
3546 	}
3547 
3548 	/* stop nic */
3549 	ipw_stop_nic(priv);
3550 
3551 	/* DMA bss firmware into the device */
3552 	rc = ipw_load_firmware(priv, fw_img, le32_to_cpu(fw->fw_size));
3553 	if (rc < 0) {
3554 		IPW_ERROR("Unable to load firmware: %d\n", rc);
3555 		goto error;
3556 	}
3557 #ifdef CONFIG_PM
3558 	fw_loaded = 1;
3559 #endif
3560 
3561 	ipw_write32(priv, IPW_EEPROM_LOAD_DISABLE, 0);
3562 
3563 	rc = ipw_queue_reset(priv);
3564 	if (rc < 0) {
3565 		IPW_ERROR("Unable to initialize queues\n");
3566 		goto error;
3567 	}
3568 
3569 	/* Ensure interrupts are disabled */
3570 	ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);
3571 	/* ack pending interrupts */
3572 	ipw_write32(priv, IPW_INTA_RW, IPW_INTA_MASK_ALL);
3573 
3574 	/* kick start the device */
3575 	ipw_start_nic(priv);
3576 
3577 	if (ipw_read32(priv, IPW_INTA_RW) & IPW_INTA_BIT_PARITY_ERROR) {
3578 		if (retries > 0) {
3579 			IPW_WARNING("Parity error.  Retrying init.\n");
3580 			retries--;
3581 			goto retry;
3582 		}
3583 
3584 		IPW_ERROR("TODO: Handle parity error -- schedule restart?\n");
3585 		rc = -EIO;
3586 		goto error;
3587 	}
3588 
3589 	/* wait for the device */
3590 	rc = ipw_poll_bit(priv, IPW_INTA_RW,
3591 			  IPW_INTA_BIT_FW_INITIALIZATION_DONE, 500);
3592 	if (rc < 0) {
3593 		IPW_ERROR("device failed to start within 500ms\n");
3594 		goto error;
3595 	}
3596 	IPW_DEBUG_INFO("device response after %dms\n", rc);
3597 
3598 	/* ack fw init done interrupt */
3599 	ipw_write32(priv, IPW_INTA_RW, IPW_INTA_BIT_FW_INITIALIZATION_DONE);
3600 
3601 	/* read eeprom data */
3602 	priv->eeprom_delay = 1;
3603 	ipw_read_eeprom(priv);
3604 	/* initialize the eeprom region of sram */
3605 	ipw_eeprom_init_sram(priv);
3606 
3607 	/* enable interrupts */
3608 	ipw_enable_interrupts(priv);
3609 
3610 	/* Ensure our queue has valid packets */
3611 	ipw_rx_queue_replenish(priv);
3612 
3613 	ipw_write32(priv, IPW_RX_READ_INDEX, priv->rxq->read);
3614 
3615 	/* ack pending interrupts */
3616 	ipw_write32(priv, IPW_INTA_RW, IPW_INTA_MASK_ALL);
3617 
3618 #ifndef CONFIG_PM
3619 	release_firmware(raw);
3620 #endif
3621 	return 0;
3622 
3623       error:
3624 	if (priv->rxq) {
3625 		ipw_rx_queue_free(priv, priv->rxq);
3626 		priv->rxq = NULL;
3627 	}
3628 	ipw_tx_queue_free(priv);
3629 	release_firmware(raw);
3630 #ifdef CONFIG_PM
3631 	fw_loaded = 0;
3632 	raw = NULL;
3633 #endif
3634 
3635 	return rc;
3636 }
3637 
3638 /*
3639  * DMA services
3640  *
3641  * Theory of operation
3642  *
3643  * A queue is a circular buffers with 'Read' and 'Write' pointers.
3644  * 2 empty entries always kept in the buffer to protect from overflow.
3645  *
3646  * For Tx queue, there are low mark and high mark limits. If, after queuing
3647  * the packet for Tx, free space become < low mark, Tx queue stopped. When
3648  * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
3649  * Tx queue resumed.
3650  *
3651  * The IPW operates with six queues, one receive queue in the device's
3652  * sram, one transmit queue for sending commands to the device firmware,
3653  * and four transmit queues for data.
3654  *
3655  * The four transmit queues allow for performing quality of service (qos)
3656  * transmissions as per the 802.11 protocol.  Currently Linux does not
3657  * provide a mechanism to the user for utilizing prioritized queues, so
3658  * we only utilize the first data transmit queue (queue1).
3659  */
3660 
3661 /*
3662  * Driver allocates buffers of this size for Rx
3663  */
3664 
3665 /*
3666  * ipw_rx_queue_space - Return number of free slots available in queue.
3667  */
3668 static int ipw_rx_queue_space(const struct ipw_rx_queue *q)
3669 {
3670 	int s = q->read - q->write;
3671 	if (s <= 0)
3672 		s += RX_QUEUE_SIZE;
3673 	/* keep some buffer to not confuse full and empty queue */
3674 	s -= 2;
3675 	if (s < 0)
3676 		s = 0;
3677 	return s;
3678 }
3679 
3680 static inline int ipw_tx_queue_space(const struct clx2_queue *q)
3681 {
3682 	int s = q->last_used - q->first_empty;
3683 	if (s <= 0)
3684 		s += q->n_bd;
3685 	s -= 2;			/* keep some reserve to not confuse empty and full situations */
3686 	if (s < 0)
3687 		s = 0;
3688 	return s;
3689 }
3690 
3691 static inline int ipw_queue_inc_wrap(int index, int n_bd)
3692 {
3693 	return (++index == n_bd) ? 0 : index;
3694 }
3695 
3696 /*
3697  * Initialize common DMA queue structure
3698  *
3699  * @param q                queue to init
3700  * @param count            Number of BD's to allocate. Should be power of 2
3701  * @param read_register    Address for 'read' register
3702  *                         (not offset within BAR, full address)
3703  * @param write_register   Address for 'write' register
3704  *                         (not offset within BAR, full address)
3705  * @param base_register    Address for 'base' register
3706  *                         (not offset within BAR, full address)
3707  * @param size             Address for 'size' register
3708  *                         (not offset within BAR, full address)
3709  */
3710 static void ipw_queue_init(struct ipw_priv *priv, struct clx2_queue *q,
3711 			   int count, u32 read, u32 write, u32 base, u32 size)
3712 {
3713 	q->n_bd = count;
3714 
3715 	q->low_mark = q->n_bd / 4;
3716 	if (q->low_mark < 4)
3717 		q->low_mark = 4;
3718 
3719 	q->high_mark = q->n_bd / 8;
3720 	if (q->high_mark < 2)
3721 		q->high_mark = 2;
3722 
3723 	q->first_empty = q->last_used = 0;
3724 	q->reg_r = read;
3725 	q->reg_w = write;
3726 
3727 	ipw_write32(priv, base, q->dma_addr);
3728 	ipw_write32(priv, size, count);
3729 	ipw_write32(priv, read, 0);
3730 	ipw_write32(priv, write, 0);
3731 
3732 	_ipw_read32(priv, 0x90);
3733 }
3734 
3735 static int ipw_queue_tx_init(struct ipw_priv *priv,
3736 			     struct clx2_tx_queue *q,
3737 			     int count, u32 read, u32 write, u32 base, u32 size)
3738 {
3739 	struct pci_dev *dev = priv->pci_dev;
3740 
3741 	q->txb = kmalloc_array(count, sizeof(q->txb[0]), GFP_KERNEL);
3742 	if (!q->txb)
3743 		return -ENOMEM;
3744 
3745 	q->bd =
3746 	    dma_alloc_coherent(&dev->dev, sizeof(q->bd[0]) * count,
3747 			       &q->q.dma_addr, GFP_KERNEL);
3748 	if (!q->bd) {
3749 		IPW_ERROR("dma_alloc_coherent(%zd) failed\n",
3750 			  sizeof(q->bd[0]) * count);
3751 		kfree(q->txb);
3752 		q->txb = NULL;
3753 		return -ENOMEM;
3754 	}
3755 
3756 	ipw_queue_init(priv, &q->q, count, read, write, base, size);
3757 	return 0;
3758 }
3759 
3760 /*
3761  * Free one TFD, those at index [txq->q.last_used].
3762  * Do NOT advance any indexes
3763  *
3764  * @param dev
3765  * @param txq
3766  */
3767 static void ipw_queue_tx_free_tfd(struct ipw_priv *priv,
3768 				  struct clx2_tx_queue *txq)
3769 {
3770 	struct tfd_frame *bd = &txq->bd[txq->q.last_used];
3771 	struct pci_dev *dev = priv->pci_dev;
3772 	int i;
3773 
3774 	/* classify bd */
3775 	if (bd->control_flags.message_type == TX_HOST_COMMAND_TYPE)
3776 		/* nothing to cleanup after for host commands */
3777 		return;
3778 
3779 	/* sanity check */
3780 	if (le32_to_cpu(bd->u.data.num_chunks) > NUM_TFD_CHUNKS) {
3781 		IPW_ERROR("Too many chunks: %i\n",
3782 			  le32_to_cpu(bd->u.data.num_chunks));
3783 		/* @todo issue fatal error, it is quite serious situation */
3784 		return;
3785 	}
3786 
3787 	/* unmap chunks if any */
3788 	for (i = 0; i < le32_to_cpu(bd->u.data.num_chunks); i++) {
3789 		dma_unmap_single(&dev->dev,
3790 				 le32_to_cpu(bd->u.data.chunk_ptr[i]),
3791 				 le16_to_cpu(bd->u.data.chunk_len[i]),
3792 				 DMA_TO_DEVICE);
3793 		if (txq->txb[txq->q.last_used]) {
3794 			libipw_txb_free(txq->txb[txq->q.last_used]);
3795 			txq->txb[txq->q.last_used] = NULL;
3796 		}
3797 	}
3798 }
3799 
3800 /*
3801  * Deallocate DMA queue.
3802  *
3803  * Empty queue by removing and destroying all BD's.
3804  * Free all buffers.
3805  *
3806  * @param dev
3807  * @param q
3808  */
3809 static void ipw_queue_tx_free(struct ipw_priv *priv, struct clx2_tx_queue *txq)
3810 {
3811 	struct clx2_queue *q = &txq->q;
3812 	struct pci_dev *dev = priv->pci_dev;
3813 
3814 	if (q->n_bd == 0)
3815 		return;
3816 
3817 	/* first, empty all BD's */
3818 	for (; q->first_empty != q->last_used;
3819 	     q->last_used = ipw_queue_inc_wrap(q->last_used, q->n_bd)) {
3820 		ipw_queue_tx_free_tfd(priv, txq);
3821 	}
3822 
3823 	/* free buffers belonging to queue itself */
3824 	dma_free_coherent(&dev->dev, sizeof(txq->bd[0]) * q->n_bd, txq->bd,
3825 			  q->dma_addr);
3826 	kfree(txq->txb);
3827 
3828 	/* 0 fill whole structure */
3829 	memset(txq, 0, sizeof(*txq));
3830 }
3831 
3832 /*
3833  * Destroy all DMA queues and structures
3834  *
3835  * @param priv
3836  */
3837 static void ipw_tx_queue_free(struct ipw_priv *priv)
3838 {
3839 	/* Tx CMD queue */
3840 	ipw_queue_tx_free(priv, &priv->txq_cmd);
3841 
3842 	/* Tx queues */
3843 	ipw_queue_tx_free(priv, &priv->txq[0]);
3844 	ipw_queue_tx_free(priv, &priv->txq[1]);
3845 	ipw_queue_tx_free(priv, &priv->txq[2]);
3846 	ipw_queue_tx_free(priv, &priv->txq[3]);
3847 }
3848 
3849 static void ipw_create_bssid(struct ipw_priv *priv, u8 * bssid)
3850 {
3851 	/* First 3 bytes are manufacturer */
3852 	bssid[0] = priv->mac_addr[0];
3853 	bssid[1] = priv->mac_addr[1];
3854 	bssid[2] = priv->mac_addr[2];
3855 
3856 	/* Last bytes are random */
3857 	get_random_bytes(&bssid[3], ETH_ALEN - 3);
3858 
3859 	bssid[0] &= 0xfe;	/* clear multicast bit */
3860 	bssid[0] |= 0x02;	/* set local assignment bit (IEEE802) */
3861 }
3862 
3863 static u8 ipw_add_station(struct ipw_priv *priv, u8 * bssid)
3864 {
3865 	struct ipw_station_entry entry;
3866 	int i;
3867 
3868 	for (i = 0; i < priv->num_stations; i++) {
3869 		if (ether_addr_equal(priv->stations[i], bssid)) {
3870 			/* Another node is active in network */
3871 			priv->missed_adhoc_beacons = 0;
3872 			if (!(priv->config & CFG_STATIC_CHANNEL))
3873 				/* when other nodes drop out, we drop out */
3874 				priv->config &= ~CFG_ADHOC_PERSIST;
3875 
3876 			return i;
3877 		}
3878 	}
3879 
3880 	if (i == MAX_STATIONS)
3881 		return IPW_INVALID_STATION;
3882 
3883 	IPW_DEBUG_SCAN("Adding AdHoc station: %pM\n", bssid);
3884 
3885 	entry.reserved = 0;
3886 	entry.support_mode = 0;
3887 	memcpy(entry.mac_addr, bssid, ETH_ALEN);
3888 	memcpy(priv->stations[i], bssid, ETH_ALEN);
3889 	ipw_write_direct(priv, IPW_STATION_TABLE_LOWER + i * sizeof(entry),
3890 			 &entry, sizeof(entry));
3891 	priv->num_stations++;
3892 
3893 	return i;
3894 }
3895 
3896 static u8 ipw_find_station(struct ipw_priv *priv, u8 * bssid)
3897 {
3898 	int i;
3899 
3900 	for (i = 0; i < priv->num_stations; i++)
3901 		if (ether_addr_equal(priv->stations[i], bssid))
3902 			return i;
3903 
3904 	return IPW_INVALID_STATION;
3905 }
3906 
3907 static void ipw_send_disassociate(struct ipw_priv *priv, int quiet)
3908 {
3909 	int err;
3910 
3911 	if (priv->status & STATUS_ASSOCIATING) {
3912 		IPW_DEBUG_ASSOC("Disassociating while associating.\n");
3913 		schedule_work(&priv->disassociate);
3914 		return;
3915 	}
3916 
3917 	if (!(priv->status & STATUS_ASSOCIATED)) {
3918 		IPW_DEBUG_ASSOC("Disassociating while not associated.\n");
3919 		return;
3920 	}
3921 
3922 	IPW_DEBUG_ASSOC("Disassociation attempt from %pM "
3923 			"on channel %d.\n",
3924 			priv->assoc_request.bssid,
3925 			priv->assoc_request.channel);
3926 
3927 	priv->status &= ~(STATUS_ASSOCIATING | STATUS_ASSOCIATED);
3928 	priv->status |= STATUS_DISASSOCIATING;
3929 
3930 	if (quiet)
3931 		priv->assoc_request.assoc_type = HC_DISASSOC_QUIET;
3932 	else
3933 		priv->assoc_request.assoc_type = HC_DISASSOCIATE;
3934 
3935 	err = ipw_send_associate(priv, &priv->assoc_request);
3936 	if (err) {
3937 		IPW_DEBUG_HC("Attempt to send [dis]associate command "
3938 			     "failed.\n");
3939 		return;
3940 	}
3941 
3942 }
3943 
3944 static int ipw_disassociate(void *data)
3945 {
3946 	struct ipw_priv *priv = data;
3947 	if (!(priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)))
3948 		return 0;
3949 	ipw_send_disassociate(data, 0);
3950 	netif_carrier_off(priv->net_dev);
3951 	return 1;
3952 }
3953 
3954 static void ipw_bg_disassociate(struct work_struct *work)
3955 {
3956 	struct ipw_priv *priv =
3957 		container_of(work, struct ipw_priv, disassociate);
3958 	mutex_lock(&priv->mutex);
3959 	ipw_disassociate(priv);
3960 	mutex_unlock(&priv->mutex);
3961 }
3962 
3963 static void ipw_system_config(struct work_struct *work)
3964 {
3965 	struct ipw_priv *priv =
3966 		container_of(work, struct ipw_priv, system_config);
3967 
3968 #ifdef CONFIG_IPW2200_PROMISCUOUS
3969 	if (priv->prom_net_dev && netif_running(priv->prom_net_dev)) {
3970 		priv->sys_config.accept_all_data_frames = 1;
3971 		priv->sys_config.accept_non_directed_frames = 1;
3972 		priv->sys_config.accept_all_mgmt_bcpr = 1;
3973 		priv->sys_config.accept_all_mgmt_frames = 1;
3974 	}
3975 #endif
3976 
3977 	ipw_send_system_config(priv);
3978 }
3979 
3980 struct ipw_status_code {
3981 	u16 status;
3982 	const char *reason;
3983 };
3984 
3985 static const struct ipw_status_code ipw_status_codes[] = {
3986 	{0x00, "Successful"},
3987 	{0x01, "Unspecified failure"},
3988 	{0x0A, "Cannot support all requested capabilities in the "
3989 	 "Capability information field"},
3990 	{0x0B, "Reassociation denied due to inability to confirm that "
3991 	 "association exists"},
3992 	{0x0C, "Association denied due to reason outside the scope of this "
3993 	 "standard"},
3994 	{0x0D,
3995 	 "Responding station does not support the specified authentication "
3996 	 "algorithm"},
3997 	{0x0E,
3998 	 "Received an Authentication frame with authentication sequence "
3999 	 "transaction sequence number out of expected sequence"},
4000 	{0x0F, "Authentication rejected because of challenge failure"},
4001 	{0x10, "Authentication rejected due to timeout waiting for next "
4002 	 "frame in sequence"},
4003 	{0x11, "Association denied because AP is unable to handle additional "
4004 	 "associated stations"},
4005 	{0x12,
4006 	 "Association denied due to requesting station not supporting all "
4007 	 "of the datarates in the BSSBasicServiceSet Parameter"},
4008 	{0x13,
4009 	 "Association denied due to requesting station not supporting "
4010 	 "short preamble operation"},
4011 	{0x14,
4012 	 "Association denied due to requesting station not supporting "
4013 	 "PBCC encoding"},
4014 	{0x15,
4015 	 "Association denied due to requesting station not supporting "
4016 	 "channel agility"},
4017 	{0x19,
4018 	 "Association denied due to requesting station not supporting "
4019 	 "short slot operation"},
4020 	{0x1A,
4021 	 "Association denied due to requesting station not supporting "
4022 	 "DSSS-OFDM operation"},
4023 	{0x28, "Invalid Information Element"},
4024 	{0x29, "Group Cipher is not valid"},
4025 	{0x2A, "Pairwise Cipher is not valid"},
4026 	{0x2B, "AKMP is not valid"},
4027 	{0x2C, "Unsupported RSN IE version"},
4028 	{0x2D, "Invalid RSN IE Capabilities"},
4029 	{0x2E, "Cipher suite is rejected per security policy"},
4030 };
4031 
4032 static const char *ipw_get_status_code(u16 status)
4033 {
4034 	int i;
4035 	for (i = 0; i < ARRAY_SIZE(ipw_status_codes); i++)
4036 		if (ipw_status_codes[i].status == (status & 0xff))
4037 			return ipw_status_codes[i].reason;
4038 	return "Unknown status value.";
4039 }
4040 
4041 static inline void average_init(struct average *avg)
4042 {
4043 	memset(avg, 0, sizeof(*avg));
4044 }
4045 
4046 #define DEPTH_RSSI 8
4047 #define DEPTH_NOISE 16
4048 static s16 exponential_average(s16 prev_avg, s16 val, u8 depth)
4049 {
4050 	return ((depth-1)*prev_avg +  val)/depth;
4051 }
4052 
4053 static void average_add(struct average *avg, s16 val)
4054 {
4055 	avg->sum -= avg->entries[avg->pos];
4056 	avg->sum += val;
4057 	avg->entries[avg->pos++] = val;
4058 	if (unlikely(avg->pos == AVG_ENTRIES)) {
4059 		avg->init = 1;
4060 		avg->pos = 0;
4061 	}
4062 }
4063 
4064 static s16 average_value(struct average *avg)
4065 {
4066 	if (!unlikely(avg->init)) {
4067 		if (avg->pos)
4068 			return avg->sum / avg->pos;
4069 		return 0;
4070 	}
4071 
4072 	return avg->sum / AVG_ENTRIES;
4073 }
4074 
4075 static void ipw_reset_stats(struct ipw_priv *priv)
4076 {
4077 	u32 len = sizeof(u32);
4078 
4079 	priv->quality = 0;
4080 
4081 	average_init(&priv->average_missed_beacons);
4082 	priv->exp_avg_rssi = -60;
4083 	priv->exp_avg_noise = -85 + 0x100;
4084 
4085 	priv->last_rate = 0;
4086 	priv->last_missed_beacons = 0;
4087 	priv->last_rx_packets = 0;
4088 	priv->last_tx_packets = 0;
4089 	priv->last_tx_failures = 0;
4090 
4091 	/* Firmware managed, reset only when NIC is restarted, so we have to
4092 	 * normalize on the current value */
4093 	ipw_get_ordinal(priv, IPW_ORD_STAT_RX_ERR_CRC,
4094 			&priv->last_rx_err, &len);
4095 	ipw_get_ordinal(priv, IPW_ORD_STAT_TX_FAILURE,
4096 			&priv->last_tx_failures, &len);
4097 
4098 	/* Driver managed, reset with each association */
4099 	priv->missed_adhoc_beacons = 0;
4100 	priv->missed_beacons = 0;
4101 	priv->tx_packets = 0;
4102 	priv->rx_packets = 0;
4103 
4104 }
4105 
4106 static u32 ipw_get_max_rate(struct ipw_priv *priv)
4107 {
4108 	u32 i = 0x80000000;
4109 	u32 mask = priv->rates_mask;
4110 	/* If currently associated in B mode, restrict the maximum
4111 	 * rate match to B rates */
4112 	if (priv->assoc_request.ieee_mode == IPW_B_MODE)
4113 		mask &= LIBIPW_CCK_RATES_MASK;
4114 
4115 	/* TODO: Verify that the rate is supported by the current rates
4116 	 * list. */
4117 
4118 	while (i && !(mask & i))
4119 		i >>= 1;
4120 	switch (i) {
4121 	case LIBIPW_CCK_RATE_1MB_MASK:
4122 		return 1000000;
4123 	case LIBIPW_CCK_RATE_2MB_MASK:
4124 		return 2000000;
4125 	case LIBIPW_CCK_RATE_5MB_MASK:
4126 		return 5500000;
4127 	case LIBIPW_OFDM_RATE_6MB_MASK:
4128 		return 6000000;
4129 	case LIBIPW_OFDM_RATE_9MB_MASK:
4130 		return 9000000;
4131 	case LIBIPW_CCK_RATE_11MB_MASK:
4132 		return 11000000;
4133 	case LIBIPW_OFDM_RATE_12MB_MASK:
4134 		return 12000000;
4135 	case LIBIPW_OFDM_RATE_18MB_MASK:
4136 		return 18000000;
4137 	case LIBIPW_OFDM_RATE_24MB_MASK:
4138 		return 24000000;
4139 	case LIBIPW_OFDM_RATE_36MB_MASK:
4140 		return 36000000;
4141 	case LIBIPW_OFDM_RATE_48MB_MASK:
4142 		return 48000000;
4143 	case LIBIPW_OFDM_RATE_54MB_MASK:
4144 		return 54000000;
4145 	}
4146 
4147 	if (priv->ieee->mode == IEEE_B)
4148 		return 11000000;
4149 	else
4150 		return 54000000;
4151 }
4152 
4153 static u32 ipw_get_current_rate(struct ipw_priv *priv)
4154 {
4155 	u32 rate, len = sizeof(rate);
4156 	int err;
4157 
4158 	if (!(priv->status & STATUS_ASSOCIATED))
4159 		return 0;
4160 
4161 	if (priv->tx_packets > IPW_REAL_RATE_RX_PACKET_THRESHOLD) {
4162 		err = ipw_get_ordinal(priv, IPW_ORD_STAT_TX_CURR_RATE, &rate,
4163 				      &len);
4164 		if (err) {
4165 			IPW_DEBUG_INFO("failed querying ordinals.\n");
4166 			return 0;
4167 		}
4168 	} else
4169 		return ipw_get_max_rate(priv);
4170 
4171 	switch (rate) {
4172 	case IPW_TX_RATE_1MB:
4173 		return 1000000;
4174 	case IPW_TX_RATE_2MB:
4175 		return 2000000;
4176 	case IPW_TX_RATE_5MB:
4177 		return 5500000;
4178 	case IPW_TX_RATE_6MB:
4179 		return 6000000;
4180 	case IPW_TX_RATE_9MB:
4181 		return 9000000;
4182 	case IPW_TX_RATE_11MB:
4183 		return 11000000;
4184 	case IPW_TX_RATE_12MB:
4185 		return 12000000;
4186 	case IPW_TX_RATE_18MB:
4187 		return 18000000;
4188 	case IPW_TX_RATE_24MB:
4189 		return 24000000;
4190 	case IPW_TX_RATE_36MB:
4191 		return 36000000;
4192 	case IPW_TX_RATE_48MB:
4193 		return 48000000;
4194 	case IPW_TX_RATE_54MB:
4195 		return 54000000;
4196 	}
4197 
4198 	return 0;
4199 }
4200 
4201 #define IPW_STATS_INTERVAL (2 * HZ)
4202 static void ipw_gather_stats(struct ipw_priv *priv)
4203 {
4204 	u32 rx_err, rx_err_delta, rx_packets_delta;
4205 	u32 tx_failures, tx_failures_delta, tx_packets_delta;
4206 	u32 missed_beacons_percent, missed_beacons_delta;
4207 	u32 quality = 0;
4208 	u32 len = sizeof(u32);
4209 	s16 rssi;
4210 	u32 beacon_quality, signal_quality, tx_quality, rx_quality,
4211 	    rate_quality;
4212 	u32 max_rate;
4213 
4214 	if (!(priv->status & STATUS_ASSOCIATED)) {
4215 		priv->quality = 0;
4216 		return;
4217 	}
4218 
4219 	/* Update the statistics */
4220 	ipw_get_ordinal(priv, IPW_ORD_STAT_MISSED_BEACONS,
4221 			&priv->missed_beacons, &len);
4222 	missed_beacons_delta = priv->missed_beacons - priv->last_missed_beacons;
4223 	priv->last_missed_beacons = priv->missed_beacons;
4224 	if (priv->assoc_request.beacon_interval) {
4225 		missed_beacons_percent = missed_beacons_delta *
4226 		    (HZ * le16_to_cpu(priv->assoc_request.beacon_interval)) /
4227 		    (IPW_STATS_INTERVAL * 10);
4228 	} else {
4229 		missed_beacons_percent = 0;
4230 	}
4231 	average_add(&priv->average_missed_beacons, missed_beacons_percent);
4232 
4233 	ipw_get_ordinal(priv, IPW_ORD_STAT_RX_ERR_CRC, &rx_err, &len);
4234 	rx_err_delta = rx_err - priv->last_rx_err;
4235 	priv->last_rx_err = rx_err;
4236 
4237 	ipw_get_ordinal(priv, IPW_ORD_STAT_TX_FAILURE, &tx_failures, &len);
4238 	tx_failures_delta = tx_failures - priv->last_tx_failures;
4239 	priv->last_tx_failures = tx_failures;
4240 
4241 	rx_packets_delta = priv->rx_packets - priv->last_rx_packets;
4242 	priv->last_rx_packets = priv->rx_packets;
4243 
4244 	tx_packets_delta = priv->tx_packets - priv->last_tx_packets;
4245 	priv->last_tx_packets = priv->tx_packets;
4246 
4247 	/* Calculate quality based on the following:
4248 	 *
4249 	 * Missed beacon: 100% = 0, 0% = 70% missed
4250 	 * Rate: 60% = 1Mbs, 100% = Max
4251 	 * Rx and Tx errors represent a straight % of total Rx/Tx
4252 	 * RSSI: 100% = > -50,  0% = < -80
4253 	 * Rx errors: 100% = 0, 0% = 50% missed
4254 	 *
4255 	 * The lowest computed quality is used.
4256 	 *
4257 	 */
4258 #define BEACON_THRESHOLD 5
4259 	beacon_quality = 100 - missed_beacons_percent;
4260 	if (beacon_quality < BEACON_THRESHOLD)
4261 		beacon_quality = 0;
4262 	else
4263 		beacon_quality = (beacon_quality - BEACON_THRESHOLD) * 100 /
4264 		    (100 - BEACON_THRESHOLD);
4265 	IPW_DEBUG_STATS("Missed beacon: %3d%% (%d%%)\n",
4266 			beacon_quality, missed_beacons_percent);
4267 
4268 	priv->last_rate = ipw_get_current_rate(priv);
4269 	max_rate = ipw_get_max_rate(priv);
4270 	rate_quality = priv->last_rate * 40 / max_rate + 60;
4271 	IPW_DEBUG_STATS("Rate quality : %3d%% (%dMbs)\n",
4272 			rate_quality, priv->last_rate / 1000000);
4273 
4274 	if (rx_packets_delta > 100 && rx_packets_delta + rx_err_delta)
4275 		rx_quality = 100 - (rx_err_delta * 100) /
4276 		    (rx_packets_delta + rx_err_delta);
4277 	else
4278 		rx_quality = 100;
4279 	IPW_DEBUG_STATS("Rx quality   : %3d%% (%u errors, %u packets)\n",
4280 			rx_quality, rx_err_delta, rx_packets_delta);
4281 
4282 	if (tx_packets_delta > 100 && tx_packets_delta + tx_failures_delta)
4283 		tx_quality = 100 - (tx_failures_delta * 100) /
4284 		    (tx_packets_delta + tx_failures_delta);
4285 	else
4286 		tx_quality = 100;
4287 	IPW_DEBUG_STATS("Tx quality   : %3d%% (%u errors, %u packets)\n",
4288 			tx_quality, tx_failures_delta, tx_packets_delta);
4289 
4290 	rssi = priv->exp_avg_rssi;
4291 	signal_quality =
4292 	    (100 *
4293 	     (priv->ieee->perfect_rssi - priv->ieee->worst_rssi) *
4294 	     (priv->ieee->perfect_rssi - priv->ieee->worst_rssi) -
4295 	     (priv->ieee->perfect_rssi - rssi) *
4296 	     (15 * (priv->ieee->perfect_rssi - priv->ieee->worst_rssi) +
4297 	      62 * (priv->ieee->perfect_rssi - rssi))) /
4298 	    ((priv->ieee->perfect_rssi - priv->ieee->worst_rssi) *
4299 	     (priv->ieee->perfect_rssi - priv->ieee->worst_rssi));
4300 	if (signal_quality > 100)
4301 		signal_quality = 100;
4302 	else if (signal_quality < 1)
4303 		signal_quality = 0;
4304 
4305 	IPW_DEBUG_STATS("Signal level : %3d%% (%d dBm)\n",
4306 			signal_quality, rssi);
4307 
4308 	quality = min(rx_quality, signal_quality);
4309 	quality = min(tx_quality, quality);
4310 	quality = min(rate_quality, quality);
4311 	quality = min(beacon_quality, quality);
4312 	if (quality == beacon_quality)
4313 		IPW_DEBUG_STATS("Quality (%d%%): Clamped to missed beacons.\n",
4314 				quality);
4315 	if (quality == rate_quality)
4316 		IPW_DEBUG_STATS("Quality (%d%%): Clamped to rate quality.\n",
4317 				quality);
4318 	if (quality == tx_quality)
4319 		IPW_DEBUG_STATS("Quality (%d%%): Clamped to Tx quality.\n",
4320 				quality);
4321 	if (quality == rx_quality)
4322 		IPW_DEBUG_STATS("Quality (%d%%): Clamped to Rx quality.\n",
4323 				quality);
4324 	if (quality == signal_quality)
4325 		IPW_DEBUG_STATS("Quality (%d%%): Clamped to signal quality.\n",
4326 				quality);
4327 
4328 	priv->quality = quality;
4329 
4330 	schedule_delayed_work(&priv->gather_stats, IPW_STATS_INTERVAL);
4331 }
4332 
4333 static void ipw_bg_gather_stats(struct work_struct *work)
4334 {
4335 	struct ipw_priv *priv =
4336 		container_of(work, struct ipw_priv, gather_stats.work);
4337 	mutex_lock(&priv->mutex);
4338 	ipw_gather_stats(priv);
4339 	mutex_unlock(&priv->mutex);
4340 }
4341 
4342 /* Missed beacon behavior:
4343  * 1st missed -> roaming_threshold, just wait, don't do any scan/roam.
4344  * roaming_threshold -> disassociate_threshold, scan and roam for better signal.
4345  * Above disassociate threshold, give up and stop scanning.
4346  * Roaming is disabled if disassociate_threshold <= roaming_threshold  */
4347 static void ipw_handle_missed_beacon(struct ipw_priv *priv,
4348 					    int missed_count)
4349 {
4350 	priv->notif_missed_beacons = missed_count;
4351 
4352 	if (missed_count > priv->disassociate_threshold &&
4353 	    priv->status & STATUS_ASSOCIATED) {
4354 		/* If associated and we've hit the missed
4355 		 * beacon threshold, disassociate, turn
4356 		 * off roaming, and abort any active scans */
4357 		IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF |
4358 			  IPW_DL_STATE | IPW_DL_ASSOC,
4359 			  "Missed beacon: %d - disassociate\n", missed_count);
4360 		priv->status &= ~STATUS_ROAMING;
4361 		if (priv->status & STATUS_SCANNING) {
4362 			IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF |
4363 				  IPW_DL_STATE,
4364 				  "Aborting scan with missed beacon.\n");
4365 			schedule_work(&priv->abort_scan);
4366 		}
4367 
4368 		schedule_work(&priv->disassociate);
4369 		return;
4370 	}
4371 
4372 	if (priv->status & STATUS_ROAMING) {
4373 		/* If we are currently roaming, then just
4374 		 * print a debug statement... */
4375 		IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
4376 			  "Missed beacon: %d - roam in progress\n",
4377 			  missed_count);
4378 		return;
4379 	}
4380 
4381 	if (roaming &&
4382 	    (missed_count > priv->roaming_threshold &&
4383 	     missed_count <= priv->disassociate_threshold)) {
4384 		/* If we are not already roaming, set the ROAM
4385 		 * bit in the status and kick off a scan.
4386 		 * This can happen several times before we reach
4387 		 * disassociate_threshold. */
4388 		IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
4389 			  "Missed beacon: %d - initiate "
4390 			  "roaming\n", missed_count);
4391 		if (!(priv->status & STATUS_ROAMING)) {
4392 			priv->status |= STATUS_ROAMING;
4393 			if (!(priv->status & STATUS_SCANNING))
4394 				schedule_delayed_work(&priv->request_scan, 0);
4395 		}
4396 		return;
4397 	}
4398 
4399 	if (priv->status & STATUS_SCANNING &&
4400 	    missed_count > IPW_MB_SCAN_CANCEL_THRESHOLD) {
4401 		/* Stop scan to keep fw from getting
4402 		 * stuck (only if we aren't roaming --
4403 		 * otherwise we'll never scan more than 2 or 3
4404 		 * channels..) */
4405 		IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF | IPW_DL_STATE,
4406 			  "Aborting scan with missed beacon.\n");
4407 		schedule_work(&priv->abort_scan);
4408 	}
4409 
4410 	IPW_DEBUG_NOTIF("Missed beacon: %d\n", missed_count);
4411 }
4412 
4413 static void ipw_scan_event(struct work_struct *work)
4414 {
4415 	union iwreq_data wrqu;
4416 
4417 	struct ipw_priv *priv =
4418 		container_of(work, struct ipw_priv, scan_event.work);
4419 
4420 	wrqu.data.length = 0;
4421 	wrqu.data.flags = 0;
4422 	wireless_send_event(priv->net_dev, SIOCGIWSCAN, &wrqu, NULL);
4423 }
4424 
4425 static void handle_scan_event(struct ipw_priv *priv)
4426 {
4427 	/* Only userspace-requested scan completion events go out immediately */
4428 	if (!priv->user_requested_scan) {
4429 		schedule_delayed_work(&priv->scan_event,
4430 				      round_jiffies_relative(msecs_to_jiffies(4000)));
4431 	} else {
4432 		priv->user_requested_scan = 0;
4433 		mod_delayed_work(system_wq, &priv->scan_event, 0);
4434 	}
4435 }
4436 
4437 /*
4438  * Handle host notification packet.
4439  * Called from interrupt routine
4440  */
4441 static void ipw_rx_notification(struct ipw_priv *priv,
4442 				       struct ipw_rx_notification *notif)
4443 {
4444 	u16 size = le16_to_cpu(notif->size);
4445 
4446 	IPW_DEBUG_NOTIF("type = %i (%d bytes)\n", notif->subtype, size);
4447 
4448 	switch (notif->subtype) {
4449 	case HOST_NOTIFICATION_STATUS_ASSOCIATED:{
4450 			struct notif_association *assoc = &notif->u.assoc;
4451 
4452 			switch (assoc->state) {
4453 			case CMAS_ASSOCIATED:{
4454 					IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4455 						  IPW_DL_ASSOC,
4456 						  "associated: '%*pE' %pM\n",
4457 						  priv->essid_len, priv->essid,
4458 						  priv->bssid);
4459 
4460 					switch (priv->ieee->iw_mode) {
4461 					case IW_MODE_INFRA:
4462 						memcpy(priv->ieee->bssid,
4463 						       priv->bssid, ETH_ALEN);
4464 						break;
4465 
4466 					case IW_MODE_ADHOC:
4467 						memcpy(priv->ieee->bssid,
4468 						       priv->bssid, ETH_ALEN);
4469 
4470 						/* clear out the station table */
4471 						priv->num_stations = 0;
4472 
4473 						IPW_DEBUG_ASSOC
4474 						    ("queueing adhoc check\n");
4475 						schedule_delayed_work(
4476 							&priv->adhoc_check,
4477 							le16_to_cpu(priv->
4478 							assoc_request.
4479 							beacon_interval));
4480 						break;
4481 					}
4482 
4483 					priv->status &= ~STATUS_ASSOCIATING;
4484 					priv->status |= STATUS_ASSOCIATED;
4485 					schedule_work(&priv->system_config);
4486 
4487 #ifdef CONFIG_IPW2200_QOS
4488 #define IPW_GET_PACKET_STYPE(x) WLAN_FC_GET_STYPE( \
4489 			 le16_to_cpu(((struct ieee80211_hdr *)(x))->frame_control))
4490 					if ((priv->status & STATUS_AUTH) &&
4491 					    (IPW_GET_PACKET_STYPE(&notif->u.raw)
4492 					     == IEEE80211_STYPE_ASSOC_RESP)) {
4493 						if ((sizeof
4494 						     (struct
4495 						      libipw_assoc_response)
4496 						     <= size)
4497 						    && (size <= 2314)) {
4498 							struct
4499 							libipw_rx_stats
4500 							    stats = {
4501 								.len = size - 1,
4502 							};
4503 
4504 							IPW_DEBUG_QOS
4505 							    ("QoS Associate "
4506 							     "size %d\n", size);
4507 							libipw_rx_mgt(priv->
4508 									 ieee,
4509 									 (struct
4510 									  libipw_hdr_4addr
4511 									  *)
4512 									 &notif->u.raw, &stats);
4513 						}
4514 					}
4515 #endif
4516 
4517 					schedule_work(&priv->link_up);
4518 
4519 					break;
4520 				}
4521 
4522 			case CMAS_AUTHENTICATED:{
4523 					if (priv->
4524 					    status & (STATUS_ASSOCIATED |
4525 						      STATUS_AUTH)) {
4526 						struct notif_authenticate *auth
4527 						    = &notif->u.auth;
4528 						IPW_DEBUG(IPW_DL_NOTIF |
4529 							  IPW_DL_STATE |
4530 							  IPW_DL_ASSOC,
4531 							  "deauthenticated: '%*pE' %pM: (0x%04X) - %s\n",
4532 							  priv->essid_len,
4533 							  priv->essid,
4534 							  priv->bssid,
4535 							  le16_to_cpu(auth->status),
4536 							  ipw_get_status_code
4537 							  (le16_to_cpu
4538 							   (auth->status)));
4539 
4540 						priv->status &=
4541 						    ~(STATUS_ASSOCIATING |
4542 						      STATUS_AUTH |
4543 						      STATUS_ASSOCIATED);
4544 
4545 						schedule_work(&priv->link_down);
4546 						break;
4547 					}
4548 
4549 					IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4550 						  IPW_DL_ASSOC,
4551 						  "authenticated: '%*pE' %pM\n",
4552 						  priv->essid_len, priv->essid,
4553 						  priv->bssid);
4554 					break;
4555 				}
4556 
4557 			case CMAS_INIT:{
4558 					if (priv->status & STATUS_AUTH) {
4559 						struct
4560 						    libipw_assoc_response
4561 						*resp;
4562 						resp =
4563 						    (struct
4564 						     libipw_assoc_response
4565 						     *)&notif->u.raw;
4566 						IPW_DEBUG(IPW_DL_NOTIF |
4567 							  IPW_DL_STATE |
4568 							  IPW_DL_ASSOC,
4569 							  "association failed (0x%04X): %s\n",
4570 							  le16_to_cpu(resp->status),
4571 							  ipw_get_status_code
4572 							  (le16_to_cpu
4573 							   (resp->status)));
4574 					}
4575 
4576 					IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4577 						  IPW_DL_ASSOC,
4578 						  "disassociated: '%*pE' %pM\n",
4579 						  priv->essid_len, priv->essid,
4580 						  priv->bssid);
4581 
4582 					priv->status &=
4583 					    ~(STATUS_DISASSOCIATING |
4584 					      STATUS_ASSOCIATING |
4585 					      STATUS_ASSOCIATED | STATUS_AUTH);
4586 					if (priv->assoc_network
4587 					    && (priv->assoc_network->
4588 						capability &
4589 						WLAN_CAPABILITY_IBSS))
4590 						ipw_remove_current_network
4591 						    (priv);
4592 
4593 					schedule_work(&priv->link_down);
4594 
4595 					break;
4596 				}
4597 
4598 			case CMAS_RX_ASSOC_RESP:
4599 				break;
4600 
4601 			default:
4602 				IPW_ERROR("assoc: unknown (%d)\n",
4603 					  assoc->state);
4604 				break;
4605 			}
4606 
4607 			break;
4608 		}
4609 
4610 	case HOST_NOTIFICATION_STATUS_AUTHENTICATE:{
4611 			struct notif_authenticate *auth = &notif->u.auth;
4612 			switch (auth->state) {
4613 			case CMAS_AUTHENTICATED:
4614 				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
4615 					  "authenticated: '%*pE' %pM\n",
4616 					  priv->essid_len, priv->essid,
4617 					  priv->bssid);
4618 				priv->status |= STATUS_AUTH;
4619 				break;
4620 
4621 			case CMAS_INIT:
4622 				if (priv->status & STATUS_AUTH) {
4623 					IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4624 						  IPW_DL_ASSOC,
4625 						  "authentication failed (0x%04X): %s\n",
4626 						  le16_to_cpu(auth->status),
4627 						  ipw_get_status_code(le16_to_cpu
4628 								      (auth->
4629 								       status)));
4630 				}
4631 				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4632 					  IPW_DL_ASSOC,
4633 					  "deauthenticated: '%*pE' %pM\n",
4634 					  priv->essid_len, priv->essid,
4635 					  priv->bssid);
4636 
4637 				priv->status &= ~(STATUS_ASSOCIATING |
4638 						  STATUS_AUTH |
4639 						  STATUS_ASSOCIATED);
4640 
4641 				schedule_work(&priv->link_down);
4642 				break;
4643 
4644 			case CMAS_TX_AUTH_SEQ_1:
4645 				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4646 					  IPW_DL_ASSOC, "AUTH_SEQ_1\n");
4647 				break;
4648 			case CMAS_RX_AUTH_SEQ_2:
4649 				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4650 					  IPW_DL_ASSOC, "AUTH_SEQ_2\n");
4651 				break;
4652 			case CMAS_AUTH_SEQ_1_PASS:
4653 				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4654 					  IPW_DL_ASSOC, "AUTH_SEQ_1_PASS\n");
4655 				break;
4656 			case CMAS_AUTH_SEQ_1_FAIL:
4657 				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4658 					  IPW_DL_ASSOC, "AUTH_SEQ_1_FAIL\n");
4659 				break;
4660 			case CMAS_TX_AUTH_SEQ_3:
4661 				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4662 					  IPW_DL_ASSOC, "AUTH_SEQ_3\n");
4663 				break;
4664 			case CMAS_RX_AUTH_SEQ_4:
4665 				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4666 					  IPW_DL_ASSOC, "RX_AUTH_SEQ_4\n");
4667 				break;
4668 			case CMAS_AUTH_SEQ_2_PASS:
4669 				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4670 					  IPW_DL_ASSOC, "AUTH_SEQ_2_PASS\n");
4671 				break;
4672 			case CMAS_AUTH_SEQ_2_FAIL:
4673 				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4674 					  IPW_DL_ASSOC, "AUT_SEQ_2_FAIL\n");
4675 				break;
4676 			case CMAS_TX_ASSOC:
4677 				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4678 					  IPW_DL_ASSOC, "TX_ASSOC\n");
4679 				break;
4680 			case CMAS_RX_ASSOC_RESP:
4681 				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4682 					  IPW_DL_ASSOC, "RX_ASSOC_RESP\n");
4683 
4684 				break;
4685 			case CMAS_ASSOCIATED:
4686 				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4687 					  IPW_DL_ASSOC, "ASSOCIATED\n");
4688 				break;
4689 			default:
4690 				IPW_DEBUG_NOTIF("auth: failure - %d\n",
4691 						auth->state);
4692 				break;
4693 			}
4694 			break;
4695 		}
4696 
4697 	case HOST_NOTIFICATION_STATUS_SCAN_CHANNEL_RESULT:{
4698 			struct notif_channel_result *x =
4699 			    &notif->u.channel_result;
4700 
4701 			if (size == sizeof(*x)) {
4702 				IPW_DEBUG_SCAN("Scan result for channel %d\n",
4703 					       x->channel_num);
4704 			} else {
4705 				IPW_DEBUG_SCAN("Scan result of wrong size %d "
4706 					       "(should be %zd)\n",
4707 					       size, sizeof(*x));
4708 			}
4709 			break;
4710 		}
4711 
4712 	case HOST_NOTIFICATION_STATUS_SCAN_COMPLETED:{
4713 			struct notif_scan_complete *x = &notif->u.scan_complete;
4714 			if (size == sizeof(*x)) {
4715 				IPW_DEBUG_SCAN
4716 				    ("Scan completed: type %d, %d channels, "
4717 				     "%d status\n", x->scan_type,
4718 				     x->num_channels, x->status);
4719 			} else {
4720 				IPW_ERROR("Scan completed of wrong size %d "
4721 					  "(should be %zd)\n",
4722 					  size, sizeof(*x));
4723 			}
4724 
4725 			priv->status &=
4726 			    ~(STATUS_SCANNING | STATUS_SCAN_ABORTING);
4727 
4728 			wake_up_interruptible(&priv->wait_state);
4729 			cancel_delayed_work(&priv->scan_check);
4730 
4731 			if (priv->status & STATUS_EXIT_PENDING)
4732 				break;
4733 
4734 			priv->ieee->scans++;
4735 
4736 #ifdef CONFIG_IPW2200_MONITOR
4737 			if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
4738 				priv->status |= STATUS_SCAN_FORCED;
4739 				schedule_delayed_work(&priv->request_scan, 0);
4740 				break;
4741 			}
4742 			priv->status &= ~STATUS_SCAN_FORCED;
4743 #endif				/* CONFIG_IPW2200_MONITOR */
4744 
4745 			/* Do queued direct scans first */
4746 			if (priv->status & STATUS_DIRECT_SCAN_PENDING)
4747 				schedule_delayed_work(&priv->request_direct_scan, 0);
4748 
4749 			if (!(priv->status & (STATUS_ASSOCIATED |
4750 					      STATUS_ASSOCIATING |
4751 					      STATUS_ROAMING |
4752 					      STATUS_DISASSOCIATING)))
4753 				schedule_work(&priv->associate);
4754 			else if (priv->status & STATUS_ROAMING) {
4755 				if (x->status == SCAN_COMPLETED_STATUS_COMPLETE)
4756 					/* If a scan completed and we are in roam mode, then
4757 					 * the scan that completed was the one requested as a
4758 					 * result of entering roam... so, schedule the
4759 					 * roam work */
4760 					schedule_work(&priv->roam);
4761 				else
4762 					/* Don't schedule if we aborted the scan */
4763 					priv->status &= ~STATUS_ROAMING;
4764 			} else if (priv->status & STATUS_SCAN_PENDING)
4765 				schedule_delayed_work(&priv->request_scan, 0);
4766 			else if (priv->config & CFG_BACKGROUND_SCAN
4767 				 && priv->status & STATUS_ASSOCIATED)
4768 				schedule_delayed_work(&priv->request_scan,
4769 						      round_jiffies_relative(HZ));
4770 
4771 			/* Send an empty event to user space.
4772 			 * We don't send the received data on the event because
4773 			 * it would require us to do complex transcoding, and
4774 			 * we want to minimise the work done in the irq handler
4775 			 * Use a request to extract the data.
4776 			 * Also, we generate this even for any scan, regardless
4777 			 * on how the scan was initiated. User space can just
4778 			 * sync on periodic scan to get fresh data...
4779 			 * Jean II */
4780 			if (x->status == SCAN_COMPLETED_STATUS_COMPLETE)
4781 				handle_scan_event(priv);
4782 			break;
4783 		}
4784 
4785 	case HOST_NOTIFICATION_STATUS_FRAG_LENGTH:{
4786 			struct notif_frag_length *x = &notif->u.frag_len;
4787 
4788 			if (size == sizeof(*x))
4789 				IPW_ERROR("Frag length: %d\n",
4790 					  le16_to_cpu(x->frag_length));
4791 			else
4792 				IPW_ERROR("Frag length of wrong size %d "
4793 					  "(should be %zd)\n",
4794 					  size, sizeof(*x));
4795 			break;
4796 		}
4797 
4798 	case HOST_NOTIFICATION_STATUS_LINK_DETERIORATION:{
4799 			struct notif_link_deterioration *x =
4800 			    &notif->u.link_deterioration;
4801 
4802 			if (size == sizeof(*x)) {
4803 				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
4804 					"link deterioration: type %d, cnt %d\n",
4805 					x->silence_notification_type,
4806 					x->silence_count);
4807 				memcpy(&priv->last_link_deterioration, x,
4808 				       sizeof(*x));
4809 			} else {
4810 				IPW_ERROR("Link Deterioration of wrong size %d "
4811 					  "(should be %zd)\n",
4812 					  size, sizeof(*x));
4813 			}
4814 			break;
4815 		}
4816 
4817 	case HOST_NOTIFICATION_DINO_CONFIG_RESPONSE:{
4818 			IPW_ERROR("Dino config\n");
4819 			if (priv->hcmd
4820 			    && priv->hcmd->cmd != HOST_CMD_DINO_CONFIG)
4821 				IPW_ERROR("Unexpected DINO_CONFIG_RESPONSE\n");
4822 
4823 			break;
4824 		}
4825 
4826 	case HOST_NOTIFICATION_STATUS_BEACON_STATE:{
4827 			struct notif_beacon_state *x = &notif->u.beacon_state;
4828 			if (size != sizeof(*x)) {
4829 				IPW_ERROR
4830 				    ("Beacon state of wrong size %d (should "
4831 				     "be %zd)\n", size, sizeof(*x));
4832 				break;
4833 			}
4834 
4835 			if (le32_to_cpu(x->state) ==
4836 			    HOST_NOTIFICATION_STATUS_BEACON_MISSING)
4837 				ipw_handle_missed_beacon(priv,
4838 							 le32_to_cpu(x->
4839 								     number));
4840 
4841 			break;
4842 		}
4843 
4844 	case HOST_NOTIFICATION_STATUS_TGI_TX_KEY:{
4845 			struct notif_tgi_tx_key *x = &notif->u.tgi_tx_key;
4846 			if (size == sizeof(*x)) {
4847 				IPW_ERROR("TGi Tx Key: state 0x%02x sec type "
4848 					  "0x%02x station %d\n",
4849 					  x->key_state, x->security_type,
4850 					  x->station_index);
4851 				break;
4852 			}
4853 
4854 			IPW_ERROR
4855 			    ("TGi Tx Key of wrong size %d (should be %zd)\n",
4856 			     size, sizeof(*x));
4857 			break;
4858 		}
4859 
4860 	case HOST_NOTIFICATION_CALIB_KEEP_RESULTS:{
4861 			struct notif_calibration *x = &notif->u.calibration;
4862 
4863 			if (size == sizeof(*x)) {
4864 				memcpy(&priv->calib, x, sizeof(*x));
4865 				IPW_DEBUG_INFO("TODO: Calibration\n");
4866 				break;
4867 			}
4868 
4869 			IPW_ERROR
4870 			    ("Calibration of wrong size %d (should be %zd)\n",
4871 			     size, sizeof(*x));
4872 			break;
4873 		}
4874 
4875 	case HOST_NOTIFICATION_NOISE_STATS:{
4876 			if (size == sizeof(u32)) {
4877 				priv->exp_avg_noise =
4878 				    exponential_average(priv->exp_avg_noise,
4879 				    (u8) (le32_to_cpu(notif->u.noise.value) & 0xff),
4880 				    DEPTH_NOISE);
4881 				break;
4882 			}
4883 
4884 			IPW_ERROR
4885 			    ("Noise stat is wrong size %d (should be %zd)\n",
4886 			     size, sizeof(u32));
4887 			break;
4888 		}
4889 
4890 	default:
4891 		IPW_DEBUG_NOTIF("Unknown notification: "
4892 				"subtype=%d,flags=0x%2x,size=%d\n",
4893 				notif->subtype, notif->flags, size);
4894 	}
4895 }
4896 
4897 /*
4898  * Destroys all DMA structures and initialise them again
4899  *
4900  * @param priv
4901  * @return error code
4902  */
4903 static int ipw_queue_reset(struct ipw_priv *priv)
4904 {
4905 	int rc = 0;
4906 	/* @todo customize queue sizes */
4907 	int nTx = 64, nTxCmd = 8;
4908 	ipw_tx_queue_free(priv);
4909 	/* Tx CMD queue */
4910 	rc = ipw_queue_tx_init(priv, &priv->txq_cmd, nTxCmd,
4911 			       IPW_TX_CMD_QUEUE_READ_INDEX,
4912 			       IPW_TX_CMD_QUEUE_WRITE_INDEX,
4913 			       IPW_TX_CMD_QUEUE_BD_BASE,
4914 			       IPW_TX_CMD_QUEUE_BD_SIZE);
4915 	if (rc) {
4916 		IPW_ERROR("Tx Cmd queue init failed\n");
4917 		goto error;
4918 	}
4919 	/* Tx queue(s) */
4920 	rc = ipw_queue_tx_init(priv, &priv->txq[0], nTx,
4921 			       IPW_TX_QUEUE_0_READ_INDEX,
4922 			       IPW_TX_QUEUE_0_WRITE_INDEX,
4923 			       IPW_TX_QUEUE_0_BD_BASE, IPW_TX_QUEUE_0_BD_SIZE);
4924 	if (rc) {
4925 		IPW_ERROR("Tx 0 queue init failed\n");
4926 		goto error;
4927 	}
4928 	rc = ipw_queue_tx_init(priv, &priv->txq[1], nTx,
4929 			       IPW_TX_QUEUE_1_READ_INDEX,
4930 			       IPW_TX_QUEUE_1_WRITE_INDEX,
4931 			       IPW_TX_QUEUE_1_BD_BASE, IPW_TX_QUEUE_1_BD_SIZE);
4932 	if (rc) {
4933 		IPW_ERROR("Tx 1 queue init failed\n");
4934 		goto error;
4935 	}
4936 	rc = ipw_queue_tx_init(priv, &priv->txq[2], nTx,
4937 			       IPW_TX_QUEUE_2_READ_INDEX,
4938 			       IPW_TX_QUEUE_2_WRITE_INDEX,
4939 			       IPW_TX_QUEUE_2_BD_BASE, IPW_TX_QUEUE_2_BD_SIZE);
4940 	if (rc) {
4941 		IPW_ERROR("Tx 2 queue init failed\n");
4942 		goto error;
4943 	}
4944 	rc = ipw_queue_tx_init(priv, &priv->txq[3], nTx,
4945 			       IPW_TX_QUEUE_3_READ_INDEX,
4946 			       IPW_TX_QUEUE_3_WRITE_INDEX,
4947 			       IPW_TX_QUEUE_3_BD_BASE, IPW_TX_QUEUE_3_BD_SIZE);
4948 	if (rc) {
4949 		IPW_ERROR("Tx 3 queue init failed\n");
4950 		goto error;
4951 	}
4952 	/* statistics */
4953 	priv->rx_bufs_min = 0;
4954 	priv->rx_pend_max = 0;
4955 	return rc;
4956 
4957       error:
4958 	ipw_tx_queue_free(priv);
4959 	return rc;
4960 }
4961 
4962 /*
4963  * Reclaim Tx queue entries no more used by NIC.
4964  *
4965  * When FW advances 'R' index, all entries between old and
4966  * new 'R' index need to be reclaimed. As result, some free space
4967  * forms. If there is enough free space (> low mark), wake Tx queue.
4968  *
4969  * @note Need to protect against garbage in 'R' index
4970  * @param priv
4971  * @param txq
4972  * @param qindex
4973  * @return Number of used entries remains in the queue
4974  */
4975 static int ipw_queue_tx_reclaim(struct ipw_priv *priv,
4976 				struct clx2_tx_queue *txq, int qindex)
4977 {
4978 	u32 hw_tail;
4979 	int used;
4980 	struct clx2_queue *q = &txq->q;
4981 
4982 	hw_tail = ipw_read32(priv, q->reg_r);
4983 	if (hw_tail >= q->n_bd) {
4984 		IPW_ERROR
4985 		    ("Read index for DMA queue (%d) is out of range [0-%d)\n",
4986 		     hw_tail, q->n_bd);
4987 		goto done;
4988 	}
4989 	for (; q->last_used != hw_tail;
4990 	     q->last_used = ipw_queue_inc_wrap(q->last_used, q->n_bd)) {
4991 		ipw_queue_tx_free_tfd(priv, txq);
4992 		priv->tx_packets++;
4993 	}
4994       done:
4995 	if ((ipw_tx_queue_space(q) > q->low_mark) &&
4996 	    (qindex >= 0))
4997 		netif_wake_queue(priv->net_dev);
4998 	used = q->first_empty - q->last_used;
4999 	if (used < 0)
5000 		used += q->n_bd;
5001 
5002 	return used;
5003 }
5004 
5005 static int ipw_queue_tx_hcmd(struct ipw_priv *priv, int hcmd, const void *buf,
5006 			     int len, int sync)
5007 {
5008 	struct clx2_tx_queue *txq = &priv->txq_cmd;
5009 	struct clx2_queue *q = &txq->q;
5010 	struct tfd_frame *tfd;
5011 
5012 	if (ipw_tx_queue_space(q) < (sync ? 1 : 2)) {
5013 		IPW_ERROR("No space for Tx\n");
5014 		return -EBUSY;
5015 	}
5016 
5017 	tfd = &txq->bd[q->first_empty];
5018 	txq->txb[q->first_empty] = NULL;
5019 
5020 	memset(tfd, 0, sizeof(*tfd));
5021 	tfd->control_flags.message_type = TX_HOST_COMMAND_TYPE;
5022 	tfd->control_flags.control_bits = TFD_NEED_IRQ_MASK;
5023 	priv->hcmd_seq++;
5024 	tfd->u.cmd.index = hcmd;
5025 	tfd->u.cmd.length = len;
5026 	memcpy(tfd->u.cmd.payload, buf, len);
5027 	q->first_empty = ipw_queue_inc_wrap(q->first_empty, q->n_bd);
5028 	ipw_write32(priv, q->reg_w, q->first_empty);
5029 	_ipw_read32(priv, 0x90);
5030 
5031 	return 0;
5032 }
5033 
5034 /*
5035  * Rx theory of operation
5036  *
5037  * The host allocates 32 DMA target addresses and passes the host address
5038  * to the firmware at register IPW_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
5039  * 0 to 31
5040  *
5041  * Rx Queue Indexes
5042  * The host/firmware share two index registers for managing the Rx buffers.
5043  *
5044  * The READ index maps to the first position that the firmware may be writing
5045  * to -- the driver can read up to (but not including) this position and get
5046  * good data.
5047  * The READ index is managed by the firmware once the card is enabled.
5048  *
5049  * The WRITE index maps to the last position the driver has read from -- the
5050  * position preceding WRITE is the last slot the firmware can place a packet.
5051  *
5052  * The queue is empty (no good data) if WRITE = READ - 1, and is full if
5053  * WRITE = READ.
5054  *
5055  * During initialization the host sets up the READ queue position to the first
5056  * INDEX position, and WRITE to the last (READ - 1 wrapped)
5057  *
5058  * When the firmware places a packet in a buffer it will advance the READ index
5059  * and fire the RX interrupt.  The driver can then query the READ index and
5060  * process as many packets as possible, moving the WRITE index forward as it
5061  * resets the Rx queue buffers with new memory.
5062  *
5063  * The management in the driver is as follows:
5064  * + A list of pre-allocated SKBs is stored in ipw->rxq->rx_free.  When
5065  *   ipw->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
5066  *   to replensish the ipw->rxq->rx_free.
5067  * + In ipw_rx_queue_replenish (scheduled) if 'processed' != 'read' then the
5068  *   ipw->rxq is replenished and the READ INDEX is updated (updating the
5069  *   'processed' and 'read' driver indexes as well)
5070  * + A received packet is processed and handed to the kernel network stack,
5071  *   detached from the ipw->rxq.  The driver 'processed' index is updated.
5072  * + The Host/Firmware ipw->rxq is replenished at tasklet time from the rx_free
5073  *   list. If there are no allocated buffers in ipw->rxq->rx_free, the READ
5074  *   INDEX is not incremented and ipw->status(RX_STALLED) is set.  If there
5075  *   were enough free buffers and RX_STALLED is set it is cleared.
5076  *
5077  *
5078  * Driver sequence:
5079  *
5080  * ipw_rx_queue_alloc()       Allocates rx_free
5081  * ipw_rx_queue_replenish()   Replenishes rx_free list from rx_used, and calls
5082  *                            ipw_rx_queue_restock
5083  * ipw_rx_queue_restock()     Moves available buffers from rx_free into Rx
5084  *                            queue, updates firmware pointers, and updates
5085  *                            the WRITE index.  If insufficient rx_free buffers
5086  *                            are available, schedules ipw_rx_queue_replenish
5087  *
5088  * -- enable interrupts --
5089  * ISR - ipw_rx()             Detach ipw_rx_mem_buffers from pool up to the
5090  *                            READ INDEX, detaching the SKB from the pool.
5091  *                            Moves the packet buffer from queue to rx_used.
5092  *                            Calls ipw_rx_queue_restock to refill any empty
5093  *                            slots.
5094  * ...
5095  *
5096  */
5097 
5098 /*
5099  * If there are slots in the RX queue that  need to be restocked,
5100  * and we have free pre-allocated buffers, fill the ranks as much
5101  * as we can pulling from rx_free.
5102  *
5103  * This moves the 'write' index forward to catch up with 'processed', and
5104  * also updates the memory address in the firmware to reference the new
5105  * target buffer.
5106  */
5107 static void ipw_rx_queue_restock(struct ipw_priv *priv)
5108 {
5109 	struct ipw_rx_queue *rxq = priv->rxq;
5110 	struct list_head *element;
5111 	struct ipw_rx_mem_buffer *rxb;
5112 	unsigned long flags;
5113 	int write;
5114 
5115 	spin_lock_irqsave(&rxq->lock, flags);
5116 	write = rxq->write;
5117 	while ((ipw_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
5118 		element = rxq->rx_free.next;
5119 		rxb = list_entry(element, struct ipw_rx_mem_buffer, list);
5120 		list_del(element);
5121 
5122 		ipw_write32(priv, IPW_RFDS_TABLE_LOWER + rxq->write * RFD_SIZE,
5123 			    rxb->dma_addr);
5124 		rxq->queue[rxq->write] = rxb;
5125 		rxq->write = (rxq->write + 1) % RX_QUEUE_SIZE;
5126 		rxq->free_count--;
5127 	}
5128 	spin_unlock_irqrestore(&rxq->lock, flags);
5129 
5130 	/* If the pre-allocated buffer pool is dropping low, schedule to
5131 	 * refill it */
5132 	if (rxq->free_count <= RX_LOW_WATERMARK)
5133 		schedule_work(&priv->rx_replenish);
5134 
5135 	/* If we've added more space for the firmware to place data, tell it */
5136 	if (write != rxq->write)
5137 		ipw_write32(priv, IPW_RX_WRITE_INDEX, rxq->write);
5138 }
5139 
5140 /*
5141  * Move all used packet from rx_used to rx_free, allocating a new SKB for each.
5142  * Also restock the Rx queue via ipw_rx_queue_restock.
5143  *
5144  * This is called as a scheduled work item (except for during initialization)
5145  */
5146 static void ipw_rx_queue_replenish(void *data)
5147 {
5148 	struct ipw_priv *priv = data;
5149 	struct ipw_rx_queue *rxq = priv->rxq;
5150 	struct list_head *element;
5151 	struct ipw_rx_mem_buffer *rxb;
5152 	unsigned long flags;
5153 
5154 	spin_lock_irqsave(&rxq->lock, flags);
5155 	while (!list_empty(&rxq->rx_used)) {
5156 		element = rxq->rx_used.next;
5157 		rxb = list_entry(element, struct ipw_rx_mem_buffer, list);
5158 		rxb->skb = alloc_skb(IPW_RX_BUF_SIZE, GFP_ATOMIC);
5159 		if (!rxb->skb) {
5160 			printk(KERN_CRIT "%s: Can not allocate SKB buffers.\n",
5161 			       priv->net_dev->name);
5162 			/* We don't reschedule replenish work here -- we will
5163 			 * call the restock method and if it still needs
5164 			 * more buffers it will schedule replenish */
5165 			break;
5166 		}
5167 		list_del(element);
5168 
5169 		rxb->dma_addr =
5170 		    dma_map_single(&priv->pci_dev->dev, rxb->skb->data,
5171 				   IPW_RX_BUF_SIZE, DMA_FROM_DEVICE);
5172 
5173 		list_add_tail(&rxb->list, &rxq->rx_free);
5174 		rxq->free_count++;
5175 	}
5176 	spin_unlock_irqrestore(&rxq->lock, flags);
5177 
5178 	ipw_rx_queue_restock(priv);
5179 }
5180 
5181 static void ipw_bg_rx_queue_replenish(struct work_struct *work)
5182 {
5183 	struct ipw_priv *priv =
5184 		container_of(work, struct ipw_priv, rx_replenish);
5185 	mutex_lock(&priv->mutex);
5186 	ipw_rx_queue_replenish(priv);
5187 	mutex_unlock(&priv->mutex);
5188 }
5189 
5190 /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
5191  * If an SKB has been detached, the POOL needs to have its SKB set to NULL
5192  * This free routine walks the list of POOL entries and if SKB is set to
5193  * non NULL it is unmapped and freed
5194  */
5195 static void ipw_rx_queue_free(struct ipw_priv *priv, struct ipw_rx_queue *rxq)
5196 {
5197 	int i;
5198 
5199 	if (!rxq)
5200 		return;
5201 
5202 	for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
5203 		if (rxq->pool[i].skb != NULL) {
5204 			dma_unmap_single(&priv->pci_dev->dev,
5205 					 rxq->pool[i].dma_addr,
5206 					 IPW_RX_BUF_SIZE, DMA_FROM_DEVICE);
5207 			dev_kfree_skb(rxq->pool[i].skb);
5208 		}
5209 	}
5210 
5211 	kfree(rxq);
5212 }
5213 
5214 static struct ipw_rx_queue *ipw_rx_queue_alloc(struct ipw_priv *priv)
5215 {
5216 	struct ipw_rx_queue *rxq;
5217 	int i;
5218 
5219 	rxq = kzalloc(sizeof(*rxq), GFP_KERNEL);
5220 	if (unlikely(!rxq)) {
5221 		IPW_ERROR("memory allocation failed\n");
5222 		return NULL;
5223 	}
5224 	spin_lock_init(&rxq->lock);
5225 	INIT_LIST_HEAD(&rxq->rx_free);
5226 	INIT_LIST_HEAD(&rxq->rx_used);
5227 
5228 	/* Fill the rx_used queue with _all_ of the Rx buffers */
5229 	for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
5230 		list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
5231 
5232 	/* Set us so that we have processed and used all buffers, but have
5233 	 * not restocked the Rx queue with fresh buffers */
5234 	rxq->read = rxq->write = 0;
5235 	rxq->free_count = 0;
5236 
5237 	return rxq;
5238 }
5239 
5240 static int ipw_is_rate_in_mask(struct ipw_priv *priv, int ieee_mode, u8 rate)
5241 {
5242 	rate &= ~LIBIPW_BASIC_RATE_MASK;
5243 	if (ieee_mode == IEEE_A) {
5244 		switch (rate) {
5245 		case LIBIPW_OFDM_RATE_6MB:
5246 			return priv->rates_mask & LIBIPW_OFDM_RATE_6MB_MASK ?
5247 			    1 : 0;
5248 		case LIBIPW_OFDM_RATE_9MB:
5249 			return priv->rates_mask & LIBIPW_OFDM_RATE_9MB_MASK ?
5250 			    1 : 0;
5251 		case LIBIPW_OFDM_RATE_12MB:
5252 			return priv->
5253 			    rates_mask & LIBIPW_OFDM_RATE_12MB_MASK ? 1 : 0;
5254 		case LIBIPW_OFDM_RATE_18MB:
5255 			return priv->
5256 			    rates_mask & LIBIPW_OFDM_RATE_18MB_MASK ? 1 : 0;
5257 		case LIBIPW_OFDM_RATE_24MB:
5258 			return priv->
5259 			    rates_mask & LIBIPW_OFDM_RATE_24MB_MASK ? 1 : 0;
5260 		case LIBIPW_OFDM_RATE_36MB:
5261 			return priv->
5262 			    rates_mask & LIBIPW_OFDM_RATE_36MB_MASK ? 1 : 0;
5263 		case LIBIPW_OFDM_RATE_48MB:
5264 			return priv->
5265 			    rates_mask & LIBIPW_OFDM_RATE_48MB_MASK ? 1 : 0;
5266 		case LIBIPW_OFDM_RATE_54MB:
5267 			return priv->
5268 			    rates_mask & LIBIPW_OFDM_RATE_54MB_MASK ? 1 : 0;
5269 		default:
5270 			return 0;
5271 		}
5272 	}
5273 
5274 	/* B and G mixed */
5275 	switch (rate) {
5276 	case LIBIPW_CCK_RATE_1MB:
5277 		return priv->rates_mask & LIBIPW_CCK_RATE_1MB_MASK ? 1 : 0;
5278 	case LIBIPW_CCK_RATE_2MB:
5279 		return priv->rates_mask & LIBIPW_CCK_RATE_2MB_MASK ? 1 : 0;
5280 	case LIBIPW_CCK_RATE_5MB:
5281 		return priv->rates_mask & LIBIPW_CCK_RATE_5MB_MASK ? 1 : 0;
5282 	case LIBIPW_CCK_RATE_11MB:
5283 		return priv->rates_mask & LIBIPW_CCK_RATE_11MB_MASK ? 1 : 0;
5284 	}
5285 
5286 	/* If we are limited to B modulations, bail at this point */
5287 	if (ieee_mode == IEEE_B)
5288 		return 0;
5289 
5290 	/* G */
5291 	switch (rate) {
5292 	case LIBIPW_OFDM_RATE_6MB:
5293 		return priv->rates_mask & LIBIPW_OFDM_RATE_6MB_MASK ? 1 : 0;
5294 	case LIBIPW_OFDM_RATE_9MB:
5295 		return priv->rates_mask & LIBIPW_OFDM_RATE_9MB_MASK ? 1 : 0;
5296 	case LIBIPW_OFDM_RATE_12MB:
5297 		return priv->rates_mask & LIBIPW_OFDM_RATE_12MB_MASK ? 1 : 0;
5298 	case LIBIPW_OFDM_RATE_18MB:
5299 		return priv->rates_mask & LIBIPW_OFDM_RATE_18MB_MASK ? 1 : 0;
5300 	case LIBIPW_OFDM_RATE_24MB:
5301 		return priv->rates_mask & LIBIPW_OFDM_RATE_24MB_MASK ? 1 : 0;
5302 	case LIBIPW_OFDM_RATE_36MB:
5303 		return priv->rates_mask & LIBIPW_OFDM_RATE_36MB_MASK ? 1 : 0;
5304 	case LIBIPW_OFDM_RATE_48MB:
5305 		return priv->rates_mask & LIBIPW_OFDM_RATE_48MB_MASK ? 1 : 0;
5306 	case LIBIPW_OFDM_RATE_54MB:
5307 		return priv->rates_mask & LIBIPW_OFDM_RATE_54MB_MASK ? 1 : 0;
5308 	}
5309 
5310 	return 0;
5311 }
5312 
5313 static int ipw_compatible_rates(struct ipw_priv *priv,
5314 				const struct libipw_network *network,
5315 				struct ipw_supported_rates *rates)
5316 {
5317 	int num_rates, i;
5318 
5319 	memset(rates, 0, sizeof(*rates));
5320 	num_rates = min(network->rates_len, (u8) IPW_MAX_RATES);
5321 	rates->num_rates = 0;
5322 	for (i = 0; i < num_rates; i++) {
5323 		if (!ipw_is_rate_in_mask(priv, network->mode,
5324 					 network->rates[i])) {
5325 
5326 			if (network->rates[i] & LIBIPW_BASIC_RATE_MASK) {
5327 				IPW_DEBUG_SCAN("Adding masked mandatory "
5328 					       "rate %02X\n",
5329 					       network->rates[i]);
5330 				rates->supported_rates[rates->num_rates++] =
5331 				    network->rates[i];
5332 				continue;
5333 			}
5334 
5335 			IPW_DEBUG_SCAN("Rate %02X masked : 0x%08X\n",
5336 				       network->rates[i], priv->rates_mask);
5337 			continue;
5338 		}
5339 
5340 		rates->supported_rates[rates->num_rates++] = network->rates[i];
5341 	}
5342 
5343 	num_rates = min(network->rates_ex_len,
5344 			(u8) (IPW_MAX_RATES - num_rates));
5345 	for (i = 0; i < num_rates; i++) {
5346 		if (!ipw_is_rate_in_mask(priv, network->mode,
5347 					 network->rates_ex[i])) {
5348 			if (network->rates_ex[i] & LIBIPW_BASIC_RATE_MASK) {
5349 				IPW_DEBUG_SCAN("Adding masked mandatory "
5350 					       "rate %02X\n",
5351 					       network->rates_ex[i]);
5352 				rates->supported_rates[rates->num_rates++] =
5353 				    network->rates[i];
5354 				continue;
5355 			}
5356 
5357 			IPW_DEBUG_SCAN("Rate %02X masked : 0x%08X\n",
5358 				       network->rates_ex[i], priv->rates_mask);
5359 			continue;
5360 		}
5361 
5362 		rates->supported_rates[rates->num_rates++] =
5363 		    network->rates_ex[i];
5364 	}
5365 
5366 	return 1;
5367 }
5368 
5369 static void ipw_copy_rates(struct ipw_supported_rates *dest,
5370 				  const struct ipw_supported_rates *src)
5371 {
5372 	u8 i;
5373 	for (i = 0; i < src->num_rates; i++)
5374 		dest->supported_rates[i] = src->supported_rates[i];
5375 	dest->num_rates = src->num_rates;
5376 }
5377 
5378 /* TODO: Look at sniffed packets in the air to determine if the basic rate
5379  * mask should ever be used -- right now all callers to add the scan rates are
5380  * set with the modulation = CCK, so BASIC_RATE_MASK is never set... */
5381 static void ipw_add_cck_scan_rates(struct ipw_supported_rates *rates,
5382 				   u8 modulation, u32 rate_mask)
5383 {
5384 	u8 basic_mask = (LIBIPW_OFDM_MODULATION == modulation) ?
5385 	    LIBIPW_BASIC_RATE_MASK : 0;
5386 
5387 	if (rate_mask & LIBIPW_CCK_RATE_1MB_MASK)
5388 		rates->supported_rates[rates->num_rates++] =
5389 		    LIBIPW_BASIC_RATE_MASK | LIBIPW_CCK_RATE_1MB;
5390 
5391 	if (rate_mask & LIBIPW_CCK_RATE_2MB_MASK)
5392 		rates->supported_rates[rates->num_rates++] =
5393 		    LIBIPW_BASIC_RATE_MASK | LIBIPW_CCK_RATE_2MB;
5394 
5395 	if (rate_mask & LIBIPW_CCK_RATE_5MB_MASK)
5396 		rates->supported_rates[rates->num_rates++] = basic_mask |
5397 		    LIBIPW_CCK_RATE_5MB;
5398 
5399 	if (rate_mask & LIBIPW_CCK_RATE_11MB_MASK)
5400 		rates->supported_rates[rates->num_rates++] = basic_mask |
5401 		    LIBIPW_CCK_RATE_11MB;
5402 }
5403 
5404 static void ipw_add_ofdm_scan_rates(struct ipw_supported_rates *rates,
5405 				    u8 modulation, u32 rate_mask)
5406 {
5407 	u8 basic_mask = (LIBIPW_OFDM_MODULATION == modulation) ?
5408 	    LIBIPW_BASIC_RATE_MASK : 0;
5409 
5410 	if (rate_mask & LIBIPW_OFDM_RATE_6MB_MASK)
5411 		rates->supported_rates[rates->num_rates++] = basic_mask |
5412 		    LIBIPW_OFDM_RATE_6MB;
5413 
5414 	if (rate_mask & LIBIPW_OFDM_RATE_9MB_MASK)
5415 		rates->supported_rates[rates->num_rates++] =
5416 		    LIBIPW_OFDM_RATE_9MB;
5417 
5418 	if (rate_mask & LIBIPW_OFDM_RATE_12MB_MASK)
5419 		rates->supported_rates[rates->num_rates++] = basic_mask |
5420 		    LIBIPW_OFDM_RATE_12MB;
5421 
5422 	if (rate_mask & LIBIPW_OFDM_RATE_18MB_MASK)
5423 		rates->supported_rates[rates->num_rates++] =
5424 		    LIBIPW_OFDM_RATE_18MB;
5425 
5426 	if (rate_mask & LIBIPW_OFDM_RATE_24MB_MASK)
5427 		rates->supported_rates[rates->num_rates++] = basic_mask |
5428 		    LIBIPW_OFDM_RATE_24MB;
5429 
5430 	if (rate_mask & LIBIPW_OFDM_RATE_36MB_MASK)
5431 		rates->supported_rates[rates->num_rates++] =
5432 		    LIBIPW_OFDM_RATE_36MB;
5433 
5434 	if (rate_mask & LIBIPW_OFDM_RATE_48MB_MASK)
5435 		rates->supported_rates[rates->num_rates++] =
5436 		    LIBIPW_OFDM_RATE_48MB;
5437 
5438 	if (rate_mask & LIBIPW_OFDM_RATE_54MB_MASK)
5439 		rates->supported_rates[rates->num_rates++] =
5440 		    LIBIPW_OFDM_RATE_54MB;
5441 }
5442 
5443 struct ipw_network_match {
5444 	struct libipw_network *network;
5445 	struct ipw_supported_rates rates;
5446 };
5447 
5448 static int ipw_find_adhoc_network(struct ipw_priv *priv,
5449 				  struct ipw_network_match *match,
5450 				  struct libipw_network *network,
5451 				  int roaming)
5452 {
5453 	struct ipw_supported_rates rates;
5454 
5455 	/* Verify that this network's capability is compatible with the
5456 	 * current mode (AdHoc or Infrastructure) */
5457 	if ((priv->ieee->iw_mode == IW_MODE_ADHOC &&
5458 	     !(network->capability & WLAN_CAPABILITY_IBSS))) {
5459 		IPW_DEBUG_MERGE("Network '%*pE (%pM)' excluded due to capability mismatch.\n",
5460 				network->ssid_len, network->ssid,
5461 				network->bssid);
5462 		return 0;
5463 	}
5464 
5465 	if (unlikely(roaming)) {
5466 		/* If we are roaming, then ensure check if this is a valid
5467 		 * network to try and roam to */
5468 		if ((network->ssid_len != match->network->ssid_len) ||
5469 		    memcmp(network->ssid, match->network->ssid,
5470 			   network->ssid_len)) {
5471 			IPW_DEBUG_MERGE("Network '%*pE (%pM)' excluded because of non-network ESSID.\n",
5472 					network->ssid_len, network->ssid,
5473 					network->bssid);
5474 			return 0;
5475 		}
5476 	} else {
5477 		/* If an ESSID has been configured then compare the broadcast
5478 		 * ESSID to ours */
5479 		if ((priv->config & CFG_STATIC_ESSID) &&
5480 		    ((network->ssid_len != priv->essid_len) ||
5481 		     memcmp(network->ssid, priv->essid,
5482 			    min(network->ssid_len, priv->essid_len)))) {
5483 			IPW_DEBUG_MERGE("Network '%*pE (%pM)' excluded because of ESSID mismatch: '%*pE'.\n",
5484 					network->ssid_len, network->ssid,
5485 					network->bssid, priv->essid_len,
5486 					priv->essid);
5487 			return 0;
5488 		}
5489 	}
5490 
5491 	/* If the old network rate is better than this one, don't bother
5492 	 * testing everything else. */
5493 
5494 	if (network->time_stamp[0] < match->network->time_stamp[0]) {
5495 		IPW_DEBUG_MERGE("Network '%*pE excluded because newer than current network.\n",
5496 				match->network->ssid_len, match->network->ssid);
5497 		return 0;
5498 	} else if (network->time_stamp[1] < match->network->time_stamp[1]) {
5499 		IPW_DEBUG_MERGE("Network '%*pE excluded because newer than current network.\n",
5500 				match->network->ssid_len, match->network->ssid);
5501 		return 0;
5502 	}
5503 
5504 	/* Now go through and see if the requested network is valid... */
5505 	if (priv->ieee->scan_age != 0 &&
5506 	    time_after(jiffies, network->last_scanned + priv->ieee->scan_age)) {
5507 		IPW_DEBUG_MERGE("Network '%*pE (%pM)' excluded because of age: %ums.\n",
5508 				network->ssid_len, network->ssid,
5509 				network->bssid,
5510 				jiffies_to_msecs(jiffies -
5511 						 network->last_scanned));
5512 		return 0;
5513 	}
5514 
5515 	if ((priv->config & CFG_STATIC_CHANNEL) &&
5516 	    (network->channel != priv->channel)) {
5517 		IPW_DEBUG_MERGE("Network '%*pE (%pM)' excluded because of channel mismatch: %d != %d.\n",
5518 				network->ssid_len, network->ssid,
5519 				network->bssid,
5520 				network->channel, priv->channel);
5521 		return 0;
5522 	}
5523 
5524 	/* Verify privacy compatibility */
5525 	if (((priv->capability & CAP_PRIVACY_ON) ? 1 : 0) !=
5526 	    ((network->capability & WLAN_CAPABILITY_PRIVACY) ? 1 : 0)) {
5527 		IPW_DEBUG_MERGE("Network '%*pE (%pM)' excluded because of privacy mismatch: %s != %s.\n",
5528 				network->ssid_len, network->ssid,
5529 				network->bssid,
5530 				priv->
5531 				capability & CAP_PRIVACY_ON ? "on" : "off",
5532 				network->
5533 				capability & WLAN_CAPABILITY_PRIVACY ? "on" :
5534 				"off");
5535 		return 0;
5536 	}
5537 
5538 	if (ether_addr_equal(network->bssid, priv->bssid)) {
5539 		IPW_DEBUG_MERGE("Network '%*pE (%pM)' excluded because of the same BSSID match: %pM.\n",
5540 				network->ssid_len, network->ssid,
5541 				network->bssid, priv->bssid);
5542 		return 0;
5543 	}
5544 
5545 	/* Filter out any incompatible freq / mode combinations */
5546 	if (!libipw_is_valid_mode(priv->ieee, network->mode)) {
5547 		IPW_DEBUG_MERGE("Network '%*pE (%pM)' excluded because of invalid frequency/mode combination.\n",
5548 				network->ssid_len, network->ssid,
5549 				network->bssid);
5550 		return 0;
5551 	}
5552 
5553 	/* Ensure that the rates supported by the driver are compatible with
5554 	 * this AP, including verification of basic rates (mandatory) */
5555 	if (!ipw_compatible_rates(priv, network, &rates)) {
5556 		IPW_DEBUG_MERGE("Network '%*pE (%pM)' excluded because configured rate mask excludes AP mandatory rate.\n",
5557 				network->ssid_len, network->ssid,
5558 				network->bssid);
5559 		return 0;
5560 	}
5561 
5562 	if (rates.num_rates == 0) {
5563 		IPW_DEBUG_MERGE("Network '%*pE (%pM)' excluded because of no compatible rates.\n",
5564 				network->ssid_len, network->ssid,
5565 				network->bssid);
5566 		return 0;
5567 	}
5568 
5569 	/* TODO: Perform any further minimal comparititive tests.  We do not
5570 	 * want to put too much policy logic here; intelligent scan selection
5571 	 * should occur within a generic IEEE 802.11 user space tool.  */
5572 
5573 	/* Set up 'new' AP to this network */
5574 	ipw_copy_rates(&match->rates, &rates);
5575 	match->network = network;
5576 	IPW_DEBUG_MERGE("Network '%*pE (%pM)' is a viable match.\n",
5577 			network->ssid_len, network->ssid, network->bssid);
5578 
5579 	return 1;
5580 }
5581 
5582 static void ipw_merge_adhoc_network(struct work_struct *work)
5583 {
5584 	struct ipw_priv *priv =
5585 		container_of(work, struct ipw_priv, merge_networks);
5586 	struct libipw_network *network = NULL;
5587 	struct ipw_network_match match = {
5588 		.network = priv->assoc_network
5589 	};
5590 
5591 	if ((priv->status & STATUS_ASSOCIATED) &&
5592 	    (priv->ieee->iw_mode == IW_MODE_ADHOC)) {
5593 		/* First pass through ROAM process -- look for a better
5594 		 * network */
5595 		unsigned long flags;
5596 
5597 		spin_lock_irqsave(&priv->ieee->lock, flags);
5598 		list_for_each_entry(network, &priv->ieee->network_list, list) {
5599 			if (network != priv->assoc_network)
5600 				ipw_find_adhoc_network(priv, &match, network,
5601 						       1);
5602 		}
5603 		spin_unlock_irqrestore(&priv->ieee->lock, flags);
5604 
5605 		if (match.network == priv->assoc_network) {
5606 			IPW_DEBUG_MERGE("No better ADHOC in this network to "
5607 					"merge to.\n");
5608 			return;
5609 		}
5610 
5611 		mutex_lock(&priv->mutex);
5612 		if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
5613 			IPW_DEBUG_MERGE("remove network %*pE\n",
5614 					priv->essid_len, priv->essid);
5615 			ipw_remove_current_network(priv);
5616 		}
5617 
5618 		ipw_disassociate(priv);
5619 		priv->assoc_network = match.network;
5620 		mutex_unlock(&priv->mutex);
5621 		return;
5622 	}
5623 }
5624 
5625 static int ipw_best_network(struct ipw_priv *priv,
5626 			    struct ipw_network_match *match,
5627 			    struct libipw_network *network, int roaming)
5628 {
5629 	struct ipw_supported_rates rates;
5630 
5631 	/* Verify that this network's capability is compatible with the
5632 	 * current mode (AdHoc or Infrastructure) */
5633 	if ((priv->ieee->iw_mode == IW_MODE_INFRA &&
5634 	     !(network->capability & WLAN_CAPABILITY_ESS)) ||
5635 	    (priv->ieee->iw_mode == IW_MODE_ADHOC &&
5636 	     !(network->capability & WLAN_CAPABILITY_IBSS))) {
5637 		IPW_DEBUG_ASSOC("Network '%*pE (%pM)' excluded due to capability mismatch.\n",
5638 				network->ssid_len, network->ssid,
5639 				network->bssid);
5640 		return 0;
5641 	}
5642 
5643 	if (unlikely(roaming)) {
5644 		/* If we are roaming, then ensure check if this is a valid
5645 		 * network to try and roam to */
5646 		if ((network->ssid_len != match->network->ssid_len) ||
5647 		    memcmp(network->ssid, match->network->ssid,
5648 			   network->ssid_len)) {
5649 			IPW_DEBUG_ASSOC("Network '%*pE (%pM)' excluded because of non-network ESSID.\n",
5650 					network->ssid_len, network->ssid,
5651 					network->bssid);
5652 			return 0;
5653 		}
5654 	} else {
5655 		/* If an ESSID has been configured then compare the broadcast
5656 		 * ESSID to ours */
5657 		if ((priv->config & CFG_STATIC_ESSID) &&
5658 		    ((network->ssid_len != priv->essid_len) ||
5659 		     memcmp(network->ssid, priv->essid,
5660 			    min(network->ssid_len, priv->essid_len)))) {
5661 			IPW_DEBUG_ASSOC("Network '%*pE (%pM)' excluded because of ESSID mismatch: '%*pE'.\n",
5662 					network->ssid_len, network->ssid,
5663 					network->bssid, priv->essid_len,
5664 					priv->essid);
5665 			return 0;
5666 		}
5667 	}
5668 
5669 	/* If the old network rate is better than this one, don't bother
5670 	 * testing everything else. */
5671 	if (match->network && match->network->stats.rssi > network->stats.rssi) {
5672 		IPW_DEBUG_ASSOC("Network '%*pE (%pM)' excluded because '%*pE (%pM)' has a stronger signal.\n",
5673 				network->ssid_len, network->ssid,
5674 				network->bssid, match->network->ssid_len,
5675 				match->network->ssid, match->network->bssid);
5676 		return 0;
5677 	}
5678 
5679 	/* If this network has already had an association attempt within the
5680 	 * last 3 seconds, do not try and associate again... */
5681 	if (network->last_associate &&
5682 	    time_after(network->last_associate + (HZ * 3UL), jiffies)) {
5683 		IPW_DEBUG_ASSOC("Network '%*pE (%pM)' excluded because of storming (%ums since last assoc attempt).\n",
5684 				network->ssid_len, network->ssid,
5685 				network->bssid,
5686 				jiffies_to_msecs(jiffies -
5687 						 network->last_associate));
5688 		return 0;
5689 	}
5690 
5691 	/* Now go through and see if the requested network is valid... */
5692 	if (priv->ieee->scan_age != 0 &&
5693 	    time_after(jiffies, network->last_scanned + priv->ieee->scan_age)) {
5694 		IPW_DEBUG_ASSOC("Network '%*pE (%pM)' excluded because of age: %ums.\n",
5695 				network->ssid_len, network->ssid,
5696 				network->bssid,
5697 				jiffies_to_msecs(jiffies -
5698 						 network->last_scanned));
5699 		return 0;
5700 	}
5701 
5702 	if ((priv->config & CFG_STATIC_CHANNEL) &&
5703 	    (network->channel != priv->channel)) {
5704 		IPW_DEBUG_ASSOC("Network '%*pE (%pM)' excluded because of channel mismatch: %d != %d.\n",
5705 				network->ssid_len, network->ssid,
5706 				network->bssid,
5707 				network->channel, priv->channel);
5708 		return 0;
5709 	}
5710 
5711 	/* Verify privacy compatibility */
5712 	if (((priv->capability & CAP_PRIVACY_ON) ? 1 : 0) !=
5713 	    ((network->capability & WLAN_CAPABILITY_PRIVACY) ? 1 : 0)) {
5714 		IPW_DEBUG_ASSOC("Network '%*pE (%pM)' excluded because of privacy mismatch: %s != %s.\n",
5715 				network->ssid_len, network->ssid,
5716 				network->bssid,
5717 				priv->capability & CAP_PRIVACY_ON ? "on" :
5718 				"off",
5719 				network->capability &
5720 				WLAN_CAPABILITY_PRIVACY ? "on" : "off");
5721 		return 0;
5722 	}
5723 
5724 	if ((priv->config & CFG_STATIC_BSSID) &&
5725 	    !ether_addr_equal(network->bssid, priv->bssid)) {
5726 		IPW_DEBUG_ASSOC("Network '%*pE (%pM)' excluded because of BSSID mismatch: %pM.\n",
5727 				network->ssid_len, network->ssid,
5728 				network->bssid, priv->bssid);
5729 		return 0;
5730 	}
5731 
5732 	/* Filter out any incompatible freq / mode combinations */
5733 	if (!libipw_is_valid_mode(priv->ieee, network->mode)) {
5734 		IPW_DEBUG_ASSOC("Network '%*pE (%pM)' excluded because of invalid frequency/mode combination.\n",
5735 				network->ssid_len, network->ssid,
5736 				network->bssid);
5737 		return 0;
5738 	}
5739 
5740 	/* Filter out invalid channel in current GEO */
5741 	if (!libipw_is_valid_channel(priv->ieee, network->channel)) {
5742 		IPW_DEBUG_ASSOC("Network '%*pE (%pM)' excluded because of invalid channel in current GEO\n",
5743 				network->ssid_len, network->ssid,
5744 				network->bssid);
5745 		return 0;
5746 	}
5747 
5748 	/* Ensure that the rates supported by the driver are compatible with
5749 	 * this AP, including verification of basic rates (mandatory) */
5750 	if (!ipw_compatible_rates(priv, network, &rates)) {
5751 		IPW_DEBUG_ASSOC("Network '%*pE (%pM)' excluded because configured rate mask excludes AP mandatory rate.\n",
5752 				network->ssid_len, network->ssid,
5753 				network->bssid);
5754 		return 0;
5755 	}
5756 
5757 	if (rates.num_rates == 0) {
5758 		IPW_DEBUG_ASSOC("Network '%*pE (%pM)' excluded because of no compatible rates.\n",
5759 				network->ssid_len, network->ssid,
5760 				network->bssid);
5761 		return 0;
5762 	}
5763 
5764 	/* TODO: Perform any further minimal comparititive tests.  We do not
5765 	 * want to put too much policy logic here; intelligent scan selection
5766 	 * should occur within a generic IEEE 802.11 user space tool.  */
5767 
5768 	/* Set up 'new' AP to this network */
5769 	ipw_copy_rates(&match->rates, &rates);
5770 	match->network = network;
5771 
5772 	IPW_DEBUG_ASSOC("Network '%*pE (%pM)' is a viable match.\n",
5773 			network->ssid_len, network->ssid, network->bssid);
5774 
5775 	return 1;
5776 }
5777 
5778 static void ipw_adhoc_create(struct ipw_priv *priv,
5779 			     struct libipw_network *network)
5780 {
5781 	const struct libipw_geo *geo = libipw_get_geo(priv->ieee);
5782 	int i;
5783 
5784 	/*
5785 	 * For the purposes of scanning, we can set our wireless mode
5786 	 * to trigger scans across combinations of bands, but when it
5787 	 * comes to creating a new ad-hoc network, we have tell the FW
5788 	 * exactly which band to use.
5789 	 *
5790 	 * We also have the possibility of an invalid channel for the
5791 	 * chossen band.  Attempting to create a new ad-hoc network
5792 	 * with an invalid channel for wireless mode will trigger a
5793 	 * FW fatal error.
5794 	 *
5795 	 */
5796 	switch (libipw_is_valid_channel(priv->ieee, priv->channel)) {
5797 	case LIBIPW_52GHZ_BAND:
5798 		network->mode = IEEE_A;
5799 		i = libipw_channel_to_index(priv->ieee, priv->channel);
5800 		BUG_ON(i == -1);
5801 		if (geo->a[i].flags & LIBIPW_CH_PASSIVE_ONLY) {
5802 			IPW_WARNING("Overriding invalid channel\n");
5803 			priv->channel = geo->a[0].channel;
5804 		}
5805 		break;
5806 
5807 	case LIBIPW_24GHZ_BAND:
5808 		if (priv->ieee->mode & IEEE_G)
5809 			network->mode = IEEE_G;
5810 		else
5811 			network->mode = IEEE_B;
5812 		i = libipw_channel_to_index(priv->ieee, priv->channel);
5813 		BUG_ON(i == -1);
5814 		if (geo->bg[i].flags & LIBIPW_CH_PASSIVE_ONLY) {
5815 			IPW_WARNING("Overriding invalid channel\n");
5816 			priv->channel = geo->bg[0].channel;
5817 		}
5818 		break;
5819 
5820 	default:
5821 		IPW_WARNING("Overriding invalid channel\n");
5822 		if (priv->ieee->mode & IEEE_A) {
5823 			network->mode = IEEE_A;
5824 			priv->channel = geo->a[0].channel;
5825 		} else if (priv->ieee->mode & IEEE_G) {
5826 			network->mode = IEEE_G;
5827 			priv->channel = geo->bg[0].channel;
5828 		} else {
5829 			network->mode = IEEE_B;
5830 			priv->channel = geo->bg[0].channel;
5831 		}
5832 		break;
5833 	}
5834 
5835 	network->channel = priv->channel;
5836 	priv->config |= CFG_ADHOC_PERSIST;
5837 	ipw_create_bssid(priv, network->bssid);
5838 	network->ssid_len = priv->essid_len;
5839 	memcpy(network->ssid, priv->essid, priv->essid_len);
5840 	memset(&network->stats, 0, sizeof(network->stats));
5841 	network->capability = WLAN_CAPABILITY_IBSS;
5842 	if (!(priv->config & CFG_PREAMBLE_LONG))
5843 		network->capability |= WLAN_CAPABILITY_SHORT_PREAMBLE;
5844 	if (priv->capability & CAP_PRIVACY_ON)
5845 		network->capability |= WLAN_CAPABILITY_PRIVACY;
5846 	network->rates_len = min(priv->rates.num_rates, MAX_RATES_LENGTH);
5847 	memcpy(network->rates, priv->rates.supported_rates, network->rates_len);
5848 	network->rates_ex_len = priv->rates.num_rates - network->rates_len;
5849 	memcpy(network->rates_ex,
5850 	       &priv->rates.supported_rates[network->rates_len],
5851 	       network->rates_ex_len);
5852 	network->last_scanned = 0;
5853 	network->flags = 0;
5854 	network->last_associate = 0;
5855 	network->time_stamp[0] = 0;
5856 	network->time_stamp[1] = 0;
5857 	network->beacon_interval = 100;	/* Default */
5858 	network->listen_interval = 10;	/* Default */
5859 	network->atim_window = 0;	/* Default */
5860 	network->wpa_ie_len = 0;
5861 	network->rsn_ie_len = 0;
5862 }
5863 
5864 static void ipw_send_tgi_tx_key(struct ipw_priv *priv, int type, int index)
5865 {
5866 	struct ipw_tgi_tx_key key;
5867 
5868 	if (!(priv->ieee->sec.flags & (1 << index)))
5869 		return;
5870 
5871 	key.key_id = index;
5872 	memcpy(key.key, priv->ieee->sec.keys[index], SCM_TEMPORAL_KEY_LENGTH);
5873 	key.security_type = type;
5874 	key.station_index = 0;	/* always 0 for BSS */
5875 	key.flags = 0;
5876 	/* 0 for new key; previous value of counter (after fatal error) */
5877 	key.tx_counter[0] = cpu_to_le32(0);
5878 	key.tx_counter[1] = cpu_to_le32(0);
5879 
5880 	ipw_send_cmd_pdu(priv, IPW_CMD_TGI_TX_KEY, sizeof(key), &key);
5881 }
5882 
5883 static void ipw_send_wep_keys(struct ipw_priv *priv, int type)
5884 {
5885 	struct ipw_wep_key key;
5886 	int i;
5887 
5888 	key.cmd_id = DINO_CMD_WEP_KEY;
5889 	key.seq_num = 0;
5890 
5891 	/* Note: AES keys cannot be set for multiple times.
5892 	 * Only set it at the first time. */
5893 	for (i = 0; i < 4; i++) {
5894 		key.key_index = i | type;
5895 		if (!(priv->ieee->sec.flags & (1 << i))) {
5896 			key.key_size = 0;
5897 			continue;
5898 		}
5899 
5900 		key.key_size = priv->ieee->sec.key_sizes[i];
5901 		memcpy(key.key, priv->ieee->sec.keys[i], key.key_size);
5902 
5903 		ipw_send_cmd_pdu(priv, IPW_CMD_WEP_KEY, sizeof(key), &key);
5904 	}
5905 }
5906 
5907 static void ipw_set_hw_decrypt_unicast(struct ipw_priv *priv, int level)
5908 {
5909 	if (priv->ieee->host_encrypt)
5910 		return;
5911 
5912 	switch (level) {
5913 	case SEC_LEVEL_3:
5914 		priv->sys_config.disable_unicast_decryption = 0;
5915 		priv->ieee->host_decrypt = 0;
5916 		break;
5917 	case SEC_LEVEL_2:
5918 		priv->sys_config.disable_unicast_decryption = 1;
5919 		priv->ieee->host_decrypt = 1;
5920 		break;
5921 	case SEC_LEVEL_1:
5922 		priv->sys_config.disable_unicast_decryption = 0;
5923 		priv->ieee->host_decrypt = 0;
5924 		break;
5925 	case SEC_LEVEL_0:
5926 		priv->sys_config.disable_unicast_decryption = 1;
5927 		break;
5928 	default:
5929 		break;
5930 	}
5931 }
5932 
5933 static void ipw_set_hw_decrypt_multicast(struct ipw_priv *priv, int level)
5934 {
5935 	if (priv->ieee->host_encrypt)
5936 		return;
5937 
5938 	switch (level) {
5939 	case SEC_LEVEL_3:
5940 		priv->sys_config.disable_multicast_decryption = 0;
5941 		break;
5942 	case SEC_LEVEL_2:
5943 		priv->sys_config.disable_multicast_decryption = 1;
5944 		break;
5945 	case SEC_LEVEL_1:
5946 		priv->sys_config.disable_multicast_decryption = 0;
5947 		break;
5948 	case SEC_LEVEL_0:
5949 		priv->sys_config.disable_multicast_decryption = 1;
5950 		break;
5951 	default:
5952 		break;
5953 	}
5954 }
5955 
5956 static void ipw_set_hwcrypto_keys(struct ipw_priv *priv)
5957 {
5958 	switch (priv->ieee->sec.level) {
5959 	case SEC_LEVEL_3:
5960 		if (priv->ieee->sec.flags & SEC_ACTIVE_KEY)
5961 			ipw_send_tgi_tx_key(priv,
5962 					    DCT_FLAG_EXT_SECURITY_CCM,
5963 					    priv->ieee->sec.active_key);
5964 
5965 		if (!priv->ieee->host_mc_decrypt)
5966 			ipw_send_wep_keys(priv, DCW_WEP_KEY_SEC_TYPE_CCM);
5967 		break;
5968 	case SEC_LEVEL_2:
5969 		if (priv->ieee->sec.flags & SEC_ACTIVE_KEY)
5970 			ipw_send_tgi_tx_key(priv,
5971 					    DCT_FLAG_EXT_SECURITY_TKIP,
5972 					    priv->ieee->sec.active_key);
5973 		break;
5974 	case SEC_LEVEL_1:
5975 		ipw_send_wep_keys(priv, DCW_WEP_KEY_SEC_TYPE_WEP);
5976 		ipw_set_hw_decrypt_unicast(priv, priv->ieee->sec.level);
5977 		ipw_set_hw_decrypt_multicast(priv, priv->ieee->sec.level);
5978 		break;
5979 	case SEC_LEVEL_0:
5980 	default:
5981 		break;
5982 	}
5983 }
5984 
5985 static void ipw_adhoc_check(void *data)
5986 {
5987 	struct ipw_priv *priv = data;
5988 
5989 	if (priv->missed_adhoc_beacons++ > priv->disassociate_threshold &&
5990 	    !(priv->config & CFG_ADHOC_PERSIST)) {
5991 		IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF |
5992 			  IPW_DL_STATE | IPW_DL_ASSOC,
5993 			  "Missed beacon: %d - disassociate\n",
5994 			  priv->missed_adhoc_beacons);
5995 		ipw_remove_current_network(priv);
5996 		ipw_disassociate(priv);
5997 		return;
5998 	}
5999 
6000 	schedule_delayed_work(&priv->adhoc_check,
6001 			      le16_to_cpu(priv->assoc_request.beacon_interval));
6002 }
6003 
6004 static void ipw_bg_adhoc_check(struct work_struct *work)
6005 {
6006 	struct ipw_priv *priv =
6007 		container_of(work, struct ipw_priv, adhoc_check.work);
6008 	mutex_lock(&priv->mutex);
6009 	ipw_adhoc_check(priv);
6010 	mutex_unlock(&priv->mutex);
6011 }
6012 
6013 static void ipw_debug_config(struct ipw_priv *priv)
6014 {
6015 	IPW_DEBUG_INFO("Scan completed, no valid APs matched "
6016 		       "[CFG 0x%08X]\n", priv->config);
6017 	if (priv->config & CFG_STATIC_CHANNEL)
6018 		IPW_DEBUG_INFO("Channel locked to %d\n", priv->channel);
6019 	else
6020 		IPW_DEBUG_INFO("Channel unlocked.\n");
6021 	if (priv->config & CFG_STATIC_ESSID)
6022 		IPW_DEBUG_INFO("ESSID locked to '%*pE'\n",
6023 			       priv->essid_len, priv->essid);
6024 	else
6025 		IPW_DEBUG_INFO("ESSID unlocked.\n");
6026 	if (priv->config & CFG_STATIC_BSSID)
6027 		IPW_DEBUG_INFO("BSSID locked to %pM\n", priv->bssid);
6028 	else
6029 		IPW_DEBUG_INFO("BSSID unlocked.\n");
6030 	if (priv->capability & CAP_PRIVACY_ON)
6031 		IPW_DEBUG_INFO("PRIVACY on\n");
6032 	else
6033 		IPW_DEBUG_INFO("PRIVACY off\n");
6034 	IPW_DEBUG_INFO("RATE MASK: 0x%08X\n", priv->rates_mask);
6035 }
6036 
6037 static void ipw_set_fixed_rate(struct ipw_priv *priv, int mode)
6038 {
6039 	/* TODO: Verify that this works... */
6040 	struct ipw_fixed_rate fr;
6041 	u32 reg;
6042 	u16 mask = 0;
6043 	u16 new_tx_rates = priv->rates_mask;
6044 
6045 	/* Identify 'current FW band' and match it with the fixed
6046 	 * Tx rates */
6047 
6048 	switch (priv->ieee->freq_band) {
6049 	case LIBIPW_52GHZ_BAND:	/* A only */
6050 		/* IEEE_A */
6051 		if (priv->rates_mask & ~LIBIPW_OFDM_RATES_MASK) {
6052 			/* Invalid fixed rate mask */
6053 			IPW_DEBUG_WX
6054 			    ("invalid fixed rate mask in ipw_set_fixed_rate\n");
6055 			new_tx_rates = 0;
6056 			break;
6057 		}
6058 
6059 		new_tx_rates >>= LIBIPW_OFDM_SHIFT_MASK_A;
6060 		break;
6061 
6062 	default:		/* 2.4Ghz or Mixed */
6063 		/* IEEE_B */
6064 		if (mode == IEEE_B) {
6065 			if (new_tx_rates & ~LIBIPW_CCK_RATES_MASK) {
6066 				/* Invalid fixed rate mask */
6067 				IPW_DEBUG_WX
6068 				    ("invalid fixed rate mask in ipw_set_fixed_rate\n");
6069 				new_tx_rates = 0;
6070 			}
6071 			break;
6072 		}
6073 
6074 		/* IEEE_G */
6075 		if (new_tx_rates & ~(LIBIPW_CCK_RATES_MASK |
6076 				    LIBIPW_OFDM_RATES_MASK)) {
6077 			/* Invalid fixed rate mask */
6078 			IPW_DEBUG_WX
6079 			    ("invalid fixed rate mask in ipw_set_fixed_rate\n");
6080 			new_tx_rates = 0;
6081 			break;
6082 		}
6083 
6084 		if (LIBIPW_OFDM_RATE_6MB_MASK & new_tx_rates) {
6085 			mask |= (LIBIPW_OFDM_RATE_6MB_MASK >> 1);
6086 			new_tx_rates &= ~LIBIPW_OFDM_RATE_6MB_MASK;
6087 		}
6088 
6089 		if (LIBIPW_OFDM_RATE_9MB_MASK & new_tx_rates) {
6090 			mask |= (LIBIPW_OFDM_RATE_9MB_MASK >> 1);
6091 			new_tx_rates &= ~LIBIPW_OFDM_RATE_9MB_MASK;
6092 		}
6093 
6094 		if (LIBIPW_OFDM_RATE_12MB_MASK & new_tx_rates) {
6095 			mask |= (LIBIPW_OFDM_RATE_12MB_MASK >> 1);
6096 			new_tx_rates &= ~LIBIPW_OFDM_RATE_12MB_MASK;
6097 		}
6098 
6099 		new_tx_rates |= mask;
6100 		break;
6101 	}
6102 
6103 	fr.tx_rates = cpu_to_le16(new_tx_rates);
6104 
6105 	reg = ipw_read32(priv, IPW_MEM_FIXED_OVERRIDE);
6106 	ipw_write_reg32(priv, reg, *(u32 *) & fr);
6107 }
6108 
6109 static void ipw_abort_scan(struct ipw_priv *priv)
6110 {
6111 	int err;
6112 
6113 	if (priv->status & STATUS_SCAN_ABORTING) {
6114 		IPW_DEBUG_HC("Ignoring concurrent scan abort request.\n");
6115 		return;
6116 	}
6117 	priv->status |= STATUS_SCAN_ABORTING;
6118 
6119 	err = ipw_send_scan_abort(priv);
6120 	if (err)
6121 		IPW_DEBUG_HC("Request to abort scan failed.\n");
6122 }
6123 
6124 static void ipw_add_scan_channels(struct ipw_priv *priv,
6125 				  struct ipw_scan_request_ext *scan,
6126 				  int scan_type)
6127 {
6128 	int channel_index = 0;
6129 	const struct libipw_geo *geo;
6130 	int i;
6131 
6132 	geo = libipw_get_geo(priv->ieee);
6133 
6134 	if (priv->ieee->freq_band & LIBIPW_52GHZ_BAND) {
6135 		int start = channel_index;
6136 		for (i = 0; i < geo->a_channels; i++) {
6137 			if ((priv->status & STATUS_ASSOCIATED) &&
6138 			    geo->a[i].channel == priv->channel)
6139 				continue;
6140 			channel_index++;
6141 			scan->channels_list[channel_index] = geo->a[i].channel;
6142 			ipw_set_scan_type(scan, channel_index,
6143 					  geo->a[i].
6144 					  flags & LIBIPW_CH_PASSIVE_ONLY ?
6145 					  IPW_SCAN_PASSIVE_FULL_DWELL_SCAN :
6146 					  scan_type);
6147 		}
6148 
6149 		if (start != channel_index) {
6150 			scan->channels_list[start] = (u8) (IPW_A_MODE << 6) |
6151 			    (channel_index - start);
6152 			channel_index++;
6153 		}
6154 	}
6155 
6156 	if (priv->ieee->freq_band & LIBIPW_24GHZ_BAND) {
6157 		int start = channel_index;
6158 		if (priv->config & CFG_SPEED_SCAN) {
6159 			int index;
6160 			u8 channels[LIBIPW_24GHZ_CHANNELS] = {
6161 				/* nop out the list */
6162 				[0] = 0
6163 			};
6164 
6165 			u8 channel;
6166 			while (channel_index < IPW_SCAN_CHANNELS - 1) {
6167 				channel =
6168 				    priv->speed_scan[priv->speed_scan_pos];
6169 				if (channel == 0) {
6170 					priv->speed_scan_pos = 0;
6171 					channel = priv->speed_scan[0];
6172 				}
6173 				if ((priv->status & STATUS_ASSOCIATED) &&
6174 				    channel == priv->channel) {
6175 					priv->speed_scan_pos++;
6176 					continue;
6177 				}
6178 
6179 				/* If this channel has already been
6180 				 * added in scan, break from loop
6181 				 * and this will be the first channel
6182 				 * in the next scan.
6183 				 */
6184 				if (channels[channel - 1] != 0)
6185 					break;
6186 
6187 				channels[channel - 1] = 1;
6188 				priv->speed_scan_pos++;
6189 				channel_index++;
6190 				scan->channels_list[channel_index] = channel;
6191 				index =
6192 				    libipw_channel_to_index(priv->ieee, channel);
6193 				ipw_set_scan_type(scan, channel_index,
6194 						  geo->bg[index].
6195 						  flags &
6196 						  LIBIPW_CH_PASSIVE_ONLY ?
6197 						  IPW_SCAN_PASSIVE_FULL_DWELL_SCAN
6198 						  : scan_type);
6199 			}
6200 		} else {
6201 			for (i = 0; i < geo->bg_channels; i++) {
6202 				if ((priv->status & STATUS_ASSOCIATED) &&
6203 				    geo->bg[i].channel == priv->channel)
6204 					continue;
6205 				channel_index++;
6206 				scan->channels_list[channel_index] =
6207 				    geo->bg[i].channel;
6208 				ipw_set_scan_type(scan, channel_index,
6209 						  geo->bg[i].
6210 						  flags &
6211 						  LIBIPW_CH_PASSIVE_ONLY ?
6212 						  IPW_SCAN_PASSIVE_FULL_DWELL_SCAN
6213 						  : scan_type);
6214 			}
6215 		}
6216 
6217 		if (start != channel_index) {
6218 			scan->channels_list[start] = (u8) (IPW_B_MODE << 6) |
6219 			    (channel_index - start);
6220 		}
6221 	}
6222 }
6223 
6224 static int ipw_passive_dwell_time(struct ipw_priv *priv)
6225 {
6226 	/* staying on passive channels longer than the DTIM interval during a
6227 	 * scan, while associated, causes the firmware to cancel the scan
6228 	 * without notification. Hence, don't stay on passive channels longer
6229 	 * than the beacon interval.
6230 	 */
6231 	if (priv->status & STATUS_ASSOCIATED
6232 	    && priv->assoc_network->beacon_interval > 10)
6233 		return priv->assoc_network->beacon_interval - 10;
6234 	else
6235 		return 120;
6236 }
6237 
6238 static int ipw_request_scan_helper(struct ipw_priv *priv, int type, int direct)
6239 {
6240 	struct ipw_scan_request_ext scan;
6241 	int err = 0, scan_type;
6242 
6243 	if (!(priv->status & STATUS_INIT) ||
6244 	    (priv->status & STATUS_EXIT_PENDING))
6245 		return 0;
6246 
6247 	mutex_lock(&priv->mutex);
6248 
6249 	if (direct && (priv->direct_scan_ssid_len == 0)) {
6250 		IPW_DEBUG_HC("Direct scan requested but no SSID to scan for\n");
6251 		priv->status &= ~STATUS_DIRECT_SCAN_PENDING;
6252 		goto done;
6253 	}
6254 
6255 	if (priv->status & STATUS_SCANNING) {
6256 		IPW_DEBUG_HC("Concurrent scan requested.  Queuing.\n");
6257 		priv->status |= direct ? STATUS_DIRECT_SCAN_PENDING :
6258 					STATUS_SCAN_PENDING;
6259 		goto done;
6260 	}
6261 
6262 	if (!(priv->status & STATUS_SCAN_FORCED) &&
6263 	    priv->status & STATUS_SCAN_ABORTING) {
6264 		IPW_DEBUG_HC("Scan request while abort pending.  Queuing.\n");
6265 		priv->status |= direct ? STATUS_DIRECT_SCAN_PENDING :
6266 					STATUS_SCAN_PENDING;
6267 		goto done;
6268 	}
6269 
6270 	if (priv->status & STATUS_RF_KILL_MASK) {
6271 		IPW_DEBUG_HC("Queuing scan due to RF Kill activation\n");
6272 		priv->status |= direct ? STATUS_DIRECT_SCAN_PENDING :
6273 					STATUS_SCAN_PENDING;
6274 		goto done;
6275 	}
6276 
6277 	memset(&scan, 0, sizeof(scan));
6278 	scan.full_scan_index = cpu_to_le32(libipw_get_scans(priv->ieee));
6279 
6280 	if (type == IW_SCAN_TYPE_PASSIVE) {
6281 		IPW_DEBUG_WX("use passive scanning\n");
6282 		scan_type = IPW_SCAN_PASSIVE_FULL_DWELL_SCAN;
6283 		scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] =
6284 			cpu_to_le16(ipw_passive_dwell_time(priv));
6285 		ipw_add_scan_channels(priv, &scan, scan_type);
6286 		goto send_request;
6287 	}
6288 
6289 	/* Use active scan by default. */
6290 	if (priv->config & CFG_SPEED_SCAN)
6291 		scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_SCAN] =
6292 			cpu_to_le16(30);
6293 	else
6294 		scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_SCAN] =
6295 			cpu_to_le16(20);
6296 
6297 	scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN] =
6298 		cpu_to_le16(20);
6299 
6300 	scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] =
6301 		cpu_to_le16(ipw_passive_dwell_time(priv));
6302 	scan.dwell_time[IPW_SCAN_ACTIVE_DIRECT_SCAN] = cpu_to_le16(20);
6303 
6304 #ifdef CONFIG_IPW2200_MONITOR
6305 	if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
6306 		u8 channel;
6307 		u8 band = 0;
6308 
6309 		switch (libipw_is_valid_channel(priv->ieee, priv->channel)) {
6310 		case LIBIPW_52GHZ_BAND:
6311 			band = (u8) (IPW_A_MODE << 6) | 1;
6312 			channel = priv->channel;
6313 			break;
6314 
6315 		case LIBIPW_24GHZ_BAND:
6316 			band = (u8) (IPW_B_MODE << 6) | 1;
6317 			channel = priv->channel;
6318 			break;
6319 
6320 		default:
6321 			band = (u8) (IPW_B_MODE << 6) | 1;
6322 			channel = 9;
6323 			break;
6324 		}
6325 
6326 		scan.channels_list[0] = band;
6327 		scan.channels_list[1] = channel;
6328 		ipw_set_scan_type(&scan, 1, IPW_SCAN_PASSIVE_FULL_DWELL_SCAN);
6329 
6330 		/* NOTE:  The card will sit on this channel for this time
6331 		 * period.  Scan aborts are timing sensitive and frequently
6332 		 * result in firmware restarts.  As such, it is best to
6333 		 * set a small dwell_time here and just keep re-issuing
6334 		 * scans.  Otherwise fast channel hopping will not actually
6335 		 * hop channels.
6336 		 *
6337 		 * TODO: Move SPEED SCAN support to all modes and bands */
6338 		scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] =
6339 			cpu_to_le16(2000);
6340 	} else {
6341 #endif				/* CONFIG_IPW2200_MONITOR */
6342 		/* Honor direct scans first, otherwise if we are roaming make
6343 		 * this a direct scan for the current network.  Finally,
6344 		 * ensure that every other scan is a fast channel hop scan */
6345 		if (direct) {
6346 			err = ipw_send_ssid(priv, priv->direct_scan_ssid,
6347 			                    priv->direct_scan_ssid_len);
6348 			if (err) {
6349 				IPW_DEBUG_HC("Attempt to send SSID command  "
6350 					     "failed\n");
6351 				goto done;
6352 			}
6353 
6354 			scan_type = IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN;
6355 		} else if ((priv->status & STATUS_ROAMING)
6356 			   || (!(priv->status & STATUS_ASSOCIATED)
6357 			       && (priv->config & CFG_STATIC_ESSID)
6358 			       && (le32_to_cpu(scan.full_scan_index) % 2))) {
6359 			err = ipw_send_ssid(priv, priv->essid, priv->essid_len);
6360 			if (err) {
6361 				IPW_DEBUG_HC("Attempt to send SSID command "
6362 					     "failed.\n");
6363 				goto done;
6364 			}
6365 
6366 			scan_type = IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN;
6367 		} else
6368 			scan_type = IPW_SCAN_ACTIVE_BROADCAST_SCAN;
6369 
6370 		ipw_add_scan_channels(priv, &scan, scan_type);
6371 #ifdef CONFIG_IPW2200_MONITOR
6372 	}
6373 #endif
6374 
6375 send_request:
6376 	err = ipw_send_scan_request_ext(priv, &scan);
6377 	if (err) {
6378 		IPW_DEBUG_HC("Sending scan command failed: %08X\n", err);
6379 		goto done;
6380 	}
6381 
6382 	priv->status |= STATUS_SCANNING;
6383 	if (direct) {
6384 		priv->status &= ~STATUS_DIRECT_SCAN_PENDING;
6385 		priv->direct_scan_ssid_len = 0;
6386 	} else
6387 		priv->status &= ~STATUS_SCAN_PENDING;
6388 
6389 	schedule_delayed_work(&priv->scan_check, IPW_SCAN_CHECK_WATCHDOG);
6390 done:
6391 	mutex_unlock(&priv->mutex);
6392 	return err;
6393 }
6394 
6395 static void ipw_request_passive_scan(struct work_struct *work)
6396 {
6397 	struct ipw_priv *priv =
6398 		container_of(work, struct ipw_priv, request_passive_scan.work);
6399 	ipw_request_scan_helper(priv, IW_SCAN_TYPE_PASSIVE, 0);
6400 }
6401 
6402 static void ipw_request_scan(struct work_struct *work)
6403 {
6404 	struct ipw_priv *priv =
6405 		container_of(work, struct ipw_priv, request_scan.work);
6406 	ipw_request_scan_helper(priv, IW_SCAN_TYPE_ACTIVE, 0);
6407 }
6408 
6409 static void ipw_request_direct_scan(struct work_struct *work)
6410 {
6411 	struct ipw_priv *priv =
6412 		container_of(work, struct ipw_priv, request_direct_scan.work);
6413 	ipw_request_scan_helper(priv, IW_SCAN_TYPE_ACTIVE, 1);
6414 }
6415 
6416 static void ipw_bg_abort_scan(struct work_struct *work)
6417 {
6418 	struct ipw_priv *priv =
6419 		container_of(work, struct ipw_priv, abort_scan);
6420 	mutex_lock(&priv->mutex);
6421 	ipw_abort_scan(priv);
6422 	mutex_unlock(&priv->mutex);
6423 }
6424 
6425 static int ipw_wpa_enable(struct ipw_priv *priv, int value)
6426 {
6427 	/* This is called when wpa_supplicant loads and closes the driver
6428 	 * interface. */
6429 	priv->ieee->wpa_enabled = value;
6430 	return 0;
6431 }
6432 
6433 static int ipw_wpa_set_auth_algs(struct ipw_priv *priv, int value)
6434 {
6435 	struct libipw_device *ieee = priv->ieee;
6436 	struct libipw_security sec = {
6437 		.flags = SEC_AUTH_MODE,
6438 	};
6439 	int ret = 0;
6440 
6441 	if (value & IW_AUTH_ALG_SHARED_KEY) {
6442 		sec.auth_mode = WLAN_AUTH_SHARED_KEY;
6443 		ieee->open_wep = 0;
6444 	} else if (value & IW_AUTH_ALG_OPEN_SYSTEM) {
6445 		sec.auth_mode = WLAN_AUTH_OPEN;
6446 		ieee->open_wep = 1;
6447 	} else if (value & IW_AUTH_ALG_LEAP) {
6448 		sec.auth_mode = WLAN_AUTH_LEAP;
6449 		ieee->open_wep = 1;
6450 	} else
6451 		return -EINVAL;
6452 
6453 	if (ieee->set_security)
6454 		ieee->set_security(ieee->dev, &sec);
6455 	else
6456 		ret = -EOPNOTSUPP;
6457 
6458 	return ret;
6459 }
6460 
6461 static void ipw_wpa_assoc_frame(struct ipw_priv *priv, char *wpa_ie,
6462 				int wpa_ie_len)
6463 {
6464 	/* make sure WPA is enabled */
6465 	ipw_wpa_enable(priv, 1);
6466 }
6467 
6468 static int ipw_set_rsn_capa(struct ipw_priv *priv,
6469 			    char *capabilities, int length)
6470 {
6471 	IPW_DEBUG_HC("HOST_CMD_RSN_CAPABILITIES\n");
6472 
6473 	return ipw_send_cmd_pdu(priv, IPW_CMD_RSN_CAPABILITIES, length,
6474 				capabilities);
6475 }
6476 
6477 /*
6478  * WE-18 support
6479  */
6480 
6481 /* SIOCSIWGENIE */
6482 static int ipw_wx_set_genie(struct net_device *dev,
6483 			    struct iw_request_info *info,
6484 			    union iwreq_data *wrqu, char *extra)
6485 {
6486 	struct ipw_priv *priv = libipw_priv(dev);
6487 	struct libipw_device *ieee = priv->ieee;
6488 	u8 *buf;
6489 	int err = 0;
6490 
6491 	if (wrqu->data.length > MAX_WPA_IE_LEN ||
6492 	    (wrqu->data.length && extra == NULL))
6493 		return -EINVAL;
6494 
6495 	if (wrqu->data.length) {
6496 		buf = kmemdup(extra, wrqu->data.length, GFP_KERNEL);
6497 		if (buf == NULL) {
6498 			err = -ENOMEM;
6499 			goto out;
6500 		}
6501 
6502 		kfree(ieee->wpa_ie);
6503 		ieee->wpa_ie = buf;
6504 		ieee->wpa_ie_len = wrqu->data.length;
6505 	} else {
6506 		kfree(ieee->wpa_ie);
6507 		ieee->wpa_ie = NULL;
6508 		ieee->wpa_ie_len = 0;
6509 	}
6510 
6511 	ipw_wpa_assoc_frame(priv, ieee->wpa_ie, ieee->wpa_ie_len);
6512       out:
6513 	return err;
6514 }
6515 
6516 /* SIOCGIWGENIE */
6517 static int ipw_wx_get_genie(struct net_device *dev,
6518 			    struct iw_request_info *info,
6519 			    union iwreq_data *wrqu, char *extra)
6520 {
6521 	struct ipw_priv *priv = libipw_priv(dev);
6522 	struct libipw_device *ieee = priv->ieee;
6523 	int err = 0;
6524 
6525 	if (ieee->wpa_ie_len == 0 || ieee->wpa_ie == NULL) {
6526 		wrqu->data.length = 0;
6527 		goto out;
6528 	}
6529 
6530 	if (wrqu->data.length < ieee->wpa_ie_len) {
6531 		err = -E2BIG;
6532 		goto out;
6533 	}
6534 
6535 	wrqu->data.length = ieee->wpa_ie_len;
6536 	memcpy(extra, ieee->wpa_ie, ieee->wpa_ie_len);
6537 
6538       out:
6539 	return err;
6540 }
6541 
6542 static int wext_cipher2level(int cipher)
6543 {
6544 	switch (cipher) {
6545 	case IW_AUTH_CIPHER_NONE:
6546 		return SEC_LEVEL_0;
6547 	case IW_AUTH_CIPHER_WEP40:
6548 	case IW_AUTH_CIPHER_WEP104:
6549 		return SEC_LEVEL_1;
6550 	case IW_AUTH_CIPHER_TKIP:
6551 		return SEC_LEVEL_2;
6552 	case IW_AUTH_CIPHER_CCMP:
6553 		return SEC_LEVEL_3;
6554 	default:
6555 		return -1;
6556 	}
6557 }
6558 
6559 /* SIOCSIWAUTH */
6560 static int ipw_wx_set_auth(struct net_device *dev,
6561 			   struct iw_request_info *info,
6562 			   union iwreq_data *wrqu, char *extra)
6563 {
6564 	struct ipw_priv *priv = libipw_priv(dev);
6565 	struct libipw_device *ieee = priv->ieee;
6566 	struct iw_param *param = &wrqu->param;
6567 	struct lib80211_crypt_data *crypt;
6568 	unsigned long flags;
6569 	int ret = 0;
6570 
6571 	switch (param->flags & IW_AUTH_INDEX) {
6572 	case IW_AUTH_WPA_VERSION:
6573 		break;
6574 	case IW_AUTH_CIPHER_PAIRWISE:
6575 		ipw_set_hw_decrypt_unicast(priv,
6576 					   wext_cipher2level(param->value));
6577 		break;
6578 	case IW_AUTH_CIPHER_GROUP:
6579 		ipw_set_hw_decrypt_multicast(priv,
6580 					     wext_cipher2level(param->value));
6581 		break;
6582 	case IW_AUTH_KEY_MGMT:
6583 		/*
6584 		 * ipw2200 does not use these parameters
6585 		 */
6586 		break;
6587 
6588 	case IW_AUTH_TKIP_COUNTERMEASURES:
6589 		crypt = priv->ieee->crypt_info.crypt[priv->ieee->crypt_info.tx_keyidx];
6590 		if (!crypt || !crypt->ops->set_flags || !crypt->ops->get_flags)
6591 			break;
6592 
6593 		flags = crypt->ops->get_flags(crypt->priv);
6594 
6595 		if (param->value)
6596 			flags |= IEEE80211_CRYPTO_TKIP_COUNTERMEASURES;
6597 		else
6598 			flags &= ~IEEE80211_CRYPTO_TKIP_COUNTERMEASURES;
6599 
6600 		crypt->ops->set_flags(flags, crypt->priv);
6601 
6602 		break;
6603 
6604 	case IW_AUTH_DROP_UNENCRYPTED:{
6605 			/* HACK:
6606 			 *
6607 			 * wpa_supplicant calls set_wpa_enabled when the driver
6608 			 * is loaded and unloaded, regardless of if WPA is being
6609 			 * used.  No other calls are made which can be used to
6610 			 * determine if encryption will be used or not prior to
6611 			 * association being expected.  If encryption is not being
6612 			 * used, drop_unencrypted is set to false, else true -- we
6613 			 * can use this to determine if the CAP_PRIVACY_ON bit should
6614 			 * be set.
6615 			 */
6616 			struct libipw_security sec = {
6617 				.flags = SEC_ENABLED,
6618 				.enabled = param->value,
6619 			};
6620 			priv->ieee->drop_unencrypted = param->value;
6621 			/* We only change SEC_LEVEL for open mode. Others
6622 			 * are set by ipw_wpa_set_encryption.
6623 			 */
6624 			if (!param->value) {
6625 				sec.flags |= SEC_LEVEL;
6626 				sec.level = SEC_LEVEL_0;
6627 			} else {
6628 				sec.flags |= SEC_LEVEL;
6629 				sec.level = SEC_LEVEL_1;
6630 			}
6631 			if (priv->ieee->set_security)
6632 				priv->ieee->set_security(priv->ieee->dev, &sec);
6633 			break;
6634 		}
6635 
6636 	case IW_AUTH_80211_AUTH_ALG:
6637 		ret = ipw_wpa_set_auth_algs(priv, param->value);
6638 		break;
6639 
6640 	case IW_AUTH_WPA_ENABLED:
6641 		ret = ipw_wpa_enable(priv, param->value);
6642 		ipw_disassociate(priv);
6643 		break;
6644 
6645 	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
6646 		ieee->ieee802_1x = param->value;
6647 		break;
6648 
6649 	case IW_AUTH_PRIVACY_INVOKED:
6650 		ieee->privacy_invoked = param->value;
6651 		break;
6652 
6653 	default:
6654 		return -EOPNOTSUPP;
6655 	}
6656 	return ret;
6657 }
6658 
6659 /* SIOCGIWAUTH */
6660 static int ipw_wx_get_auth(struct net_device *dev,
6661 			   struct iw_request_info *info,
6662 			   union iwreq_data *wrqu, char *extra)
6663 {
6664 	struct ipw_priv *priv = libipw_priv(dev);
6665 	struct libipw_device *ieee = priv->ieee;
6666 	struct lib80211_crypt_data *crypt;
6667 	struct iw_param *param = &wrqu->param;
6668 
6669 	switch (param->flags & IW_AUTH_INDEX) {
6670 	case IW_AUTH_WPA_VERSION:
6671 	case IW_AUTH_CIPHER_PAIRWISE:
6672 	case IW_AUTH_CIPHER_GROUP:
6673 	case IW_AUTH_KEY_MGMT:
6674 		/*
6675 		 * wpa_supplicant will control these internally
6676 		 */
6677 		return -EOPNOTSUPP;
6678 
6679 	case IW_AUTH_TKIP_COUNTERMEASURES:
6680 		crypt = priv->ieee->crypt_info.crypt[priv->ieee->crypt_info.tx_keyidx];
6681 		if (!crypt || !crypt->ops->get_flags)
6682 			break;
6683 
6684 		param->value = (crypt->ops->get_flags(crypt->priv) &
6685 				IEEE80211_CRYPTO_TKIP_COUNTERMEASURES) ? 1 : 0;
6686 
6687 		break;
6688 
6689 	case IW_AUTH_DROP_UNENCRYPTED:
6690 		param->value = ieee->drop_unencrypted;
6691 		break;
6692 
6693 	case IW_AUTH_80211_AUTH_ALG:
6694 		param->value = ieee->sec.auth_mode;
6695 		break;
6696 
6697 	case IW_AUTH_WPA_ENABLED:
6698 		param->value = ieee->wpa_enabled;
6699 		break;
6700 
6701 	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
6702 		param->value = ieee->ieee802_1x;
6703 		break;
6704 
6705 	case IW_AUTH_ROAMING_CONTROL:
6706 	case IW_AUTH_PRIVACY_INVOKED:
6707 		param->value = ieee->privacy_invoked;
6708 		break;
6709 
6710 	default:
6711 		return -EOPNOTSUPP;
6712 	}
6713 	return 0;
6714 }
6715 
6716 /* SIOCSIWENCODEEXT */
6717 static int ipw_wx_set_encodeext(struct net_device *dev,
6718 				struct iw_request_info *info,
6719 				union iwreq_data *wrqu, char *extra)
6720 {
6721 	struct ipw_priv *priv = libipw_priv(dev);
6722 	struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
6723 
6724 	if (hwcrypto) {
6725 		if (ext->alg == IW_ENCODE_ALG_TKIP) {
6726 			/* IPW HW can't build TKIP MIC,
6727 			   host decryption still needed */
6728 			if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY)
6729 				priv->ieee->host_mc_decrypt = 1;
6730 			else {
6731 				priv->ieee->host_encrypt = 0;
6732 				priv->ieee->host_encrypt_msdu = 1;
6733 				priv->ieee->host_decrypt = 1;
6734 			}
6735 		} else {
6736 			priv->ieee->host_encrypt = 0;
6737 			priv->ieee->host_encrypt_msdu = 0;
6738 			priv->ieee->host_decrypt = 0;
6739 			priv->ieee->host_mc_decrypt = 0;
6740 		}
6741 	}
6742 
6743 	return libipw_wx_set_encodeext(priv->ieee, info, wrqu, extra);
6744 }
6745 
6746 /* SIOCGIWENCODEEXT */
6747 static int ipw_wx_get_encodeext(struct net_device *dev,
6748 				struct iw_request_info *info,
6749 				union iwreq_data *wrqu, char *extra)
6750 {
6751 	struct ipw_priv *priv = libipw_priv(dev);
6752 	return libipw_wx_get_encodeext(priv->ieee, info, wrqu, extra);
6753 }
6754 
6755 /* SIOCSIWMLME */
6756 static int ipw_wx_set_mlme(struct net_device *dev,
6757 			   struct iw_request_info *info,
6758 			   union iwreq_data *wrqu, char *extra)
6759 {
6760 	struct ipw_priv *priv = libipw_priv(dev);
6761 	struct iw_mlme *mlme = (struct iw_mlme *)extra;
6762 
6763 	switch (mlme->cmd) {
6764 	case IW_MLME_DEAUTH:
6765 		/* silently ignore */
6766 		break;
6767 
6768 	case IW_MLME_DISASSOC:
6769 		ipw_disassociate(priv);
6770 		break;
6771 
6772 	default:
6773 		return -EOPNOTSUPP;
6774 	}
6775 	return 0;
6776 }
6777 
6778 #ifdef CONFIG_IPW2200_QOS
6779 
6780 /* QoS */
6781 /*
6782 * get the modulation type of the current network or
6783 * the card current mode
6784 */
6785 static u8 ipw_qos_current_mode(struct ipw_priv * priv)
6786 {
6787 	u8 mode = 0;
6788 
6789 	if (priv->status & STATUS_ASSOCIATED) {
6790 		unsigned long flags;
6791 
6792 		spin_lock_irqsave(&priv->ieee->lock, flags);
6793 		mode = priv->assoc_network->mode;
6794 		spin_unlock_irqrestore(&priv->ieee->lock, flags);
6795 	} else {
6796 		mode = priv->ieee->mode;
6797 	}
6798 	IPW_DEBUG_QOS("QoS network/card mode %d\n", mode);
6799 	return mode;
6800 }
6801 
6802 /*
6803 * Handle management frame beacon and probe response
6804 */
6805 static int ipw_qos_handle_probe_response(struct ipw_priv *priv,
6806 					 int active_network,
6807 					 struct libipw_network *network)
6808 {
6809 	u32 size = sizeof(struct libipw_qos_parameters);
6810 
6811 	if (network->capability & WLAN_CAPABILITY_IBSS)
6812 		network->qos_data.active = network->qos_data.supported;
6813 
6814 	if (network->flags & NETWORK_HAS_QOS_MASK) {
6815 		if (active_network &&
6816 		    (network->flags & NETWORK_HAS_QOS_PARAMETERS))
6817 			network->qos_data.active = network->qos_data.supported;
6818 
6819 		if ((network->qos_data.active == 1) && (active_network == 1) &&
6820 		    (network->flags & NETWORK_HAS_QOS_PARAMETERS) &&
6821 		    (network->qos_data.old_param_count !=
6822 		     network->qos_data.param_count)) {
6823 			network->qos_data.old_param_count =
6824 			    network->qos_data.param_count;
6825 			schedule_work(&priv->qos_activate);
6826 			IPW_DEBUG_QOS("QoS parameters change call "
6827 				      "qos_activate\n");
6828 		}
6829 	} else {
6830 		if ((priv->ieee->mode == IEEE_B) || (network->mode == IEEE_B))
6831 			memcpy(&network->qos_data.parameters,
6832 			       &def_parameters_CCK, size);
6833 		else
6834 			memcpy(&network->qos_data.parameters,
6835 			       &def_parameters_OFDM, size);
6836 
6837 		if ((network->qos_data.active == 1) && (active_network == 1)) {
6838 			IPW_DEBUG_QOS("QoS was disabled call qos_activate\n");
6839 			schedule_work(&priv->qos_activate);
6840 		}
6841 
6842 		network->qos_data.active = 0;
6843 		network->qos_data.supported = 0;
6844 	}
6845 	if ((priv->status & STATUS_ASSOCIATED) &&
6846 	    (priv->ieee->iw_mode == IW_MODE_ADHOC) && (active_network == 0)) {
6847 		if (!ether_addr_equal(network->bssid, priv->bssid))
6848 			if (network->capability & WLAN_CAPABILITY_IBSS)
6849 				if ((network->ssid_len ==
6850 				     priv->assoc_network->ssid_len) &&
6851 				    !memcmp(network->ssid,
6852 					    priv->assoc_network->ssid,
6853 					    network->ssid_len)) {
6854 					schedule_work(&priv->merge_networks);
6855 				}
6856 	}
6857 
6858 	return 0;
6859 }
6860 
6861 /*
6862 * This function set up the firmware to support QoS. It sends
6863 * IPW_CMD_QOS_PARAMETERS and IPW_CMD_WME_INFO
6864 */
6865 static int ipw_qos_activate(struct ipw_priv *priv,
6866 			    struct libipw_qos_data *qos_network_data)
6867 {
6868 	int err;
6869 	struct libipw_qos_parameters qos_parameters[QOS_QOS_SETS];
6870 	struct libipw_qos_parameters *active_one = NULL;
6871 	u32 size = sizeof(struct libipw_qos_parameters);
6872 	u32 burst_duration;
6873 	int i;
6874 	u8 type;
6875 
6876 	type = ipw_qos_current_mode(priv);
6877 
6878 	active_one = &(qos_parameters[QOS_PARAM_SET_DEF_CCK]);
6879 	memcpy(active_one, priv->qos_data.def_qos_parm_CCK, size);
6880 	active_one = &(qos_parameters[QOS_PARAM_SET_DEF_OFDM]);
6881 	memcpy(active_one, priv->qos_data.def_qos_parm_OFDM, size);
6882 
6883 	if (qos_network_data == NULL) {
6884 		if (type == IEEE_B) {
6885 			IPW_DEBUG_QOS("QoS activate network mode %d\n", type);
6886 			active_one = &def_parameters_CCK;
6887 		} else
6888 			active_one = &def_parameters_OFDM;
6889 
6890 		memcpy(&qos_parameters[QOS_PARAM_SET_ACTIVE], active_one, size);
6891 		burst_duration = ipw_qos_get_burst_duration(priv);
6892 		for (i = 0; i < QOS_QUEUE_NUM; i++)
6893 			qos_parameters[QOS_PARAM_SET_ACTIVE].tx_op_limit[i] =
6894 			    cpu_to_le16(burst_duration);
6895 	} else if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
6896 		if (type == IEEE_B) {
6897 			IPW_DEBUG_QOS("QoS activate IBSS network mode %d\n",
6898 				      type);
6899 			if (priv->qos_data.qos_enable == 0)
6900 				active_one = &def_parameters_CCK;
6901 			else
6902 				active_one = priv->qos_data.def_qos_parm_CCK;
6903 		} else {
6904 			if (priv->qos_data.qos_enable == 0)
6905 				active_one = &def_parameters_OFDM;
6906 			else
6907 				active_one = priv->qos_data.def_qos_parm_OFDM;
6908 		}
6909 		memcpy(&qos_parameters[QOS_PARAM_SET_ACTIVE], active_one, size);
6910 	} else {
6911 		unsigned long flags;
6912 		int active;
6913 
6914 		spin_lock_irqsave(&priv->ieee->lock, flags);
6915 		active_one = &(qos_network_data->parameters);
6916 		qos_network_data->old_param_count =
6917 		    qos_network_data->param_count;
6918 		memcpy(&qos_parameters[QOS_PARAM_SET_ACTIVE], active_one, size);
6919 		active = qos_network_data->supported;
6920 		spin_unlock_irqrestore(&priv->ieee->lock, flags);
6921 
6922 		if (active == 0) {
6923 			burst_duration = ipw_qos_get_burst_duration(priv);
6924 			for (i = 0; i < QOS_QUEUE_NUM; i++)
6925 				qos_parameters[QOS_PARAM_SET_ACTIVE].
6926 				    tx_op_limit[i] = cpu_to_le16(burst_duration);
6927 		}
6928 	}
6929 
6930 	IPW_DEBUG_QOS("QoS sending IPW_CMD_QOS_PARAMETERS\n");
6931 	err = ipw_send_qos_params_command(priv, &qos_parameters[0]);
6932 	if (err)
6933 		IPW_DEBUG_QOS("QoS IPW_CMD_QOS_PARAMETERS failed\n");
6934 
6935 	return err;
6936 }
6937 
6938 /*
6939 * send IPW_CMD_WME_INFO to the firmware
6940 */
6941 static int ipw_qos_set_info_element(struct ipw_priv *priv)
6942 {
6943 	int ret = 0;
6944 	struct libipw_qos_information_element qos_info;
6945 
6946 	if (priv == NULL)
6947 		return -1;
6948 
6949 	qos_info.elementID = QOS_ELEMENT_ID;
6950 	qos_info.length = sizeof(struct libipw_qos_information_element) - 2;
6951 
6952 	qos_info.version = QOS_VERSION_1;
6953 	qos_info.ac_info = 0;
6954 
6955 	memcpy(qos_info.qui, qos_oui, QOS_OUI_LEN);
6956 	qos_info.qui_type = QOS_OUI_TYPE;
6957 	qos_info.qui_subtype = QOS_OUI_INFO_SUB_TYPE;
6958 
6959 	ret = ipw_send_qos_info_command(priv, &qos_info);
6960 	if (ret != 0) {
6961 		IPW_DEBUG_QOS("QoS error calling ipw_send_qos_info_command\n");
6962 	}
6963 	return ret;
6964 }
6965 
6966 /*
6967 * Set the QoS parameter with the association request structure
6968 */
6969 static int ipw_qos_association(struct ipw_priv *priv,
6970 			       struct libipw_network *network)
6971 {
6972 	int err = 0;
6973 	struct libipw_qos_data *qos_data = NULL;
6974 	struct libipw_qos_data ibss_data = {
6975 		.supported = 1,
6976 		.active = 1,
6977 	};
6978 
6979 	switch (priv->ieee->iw_mode) {
6980 	case IW_MODE_ADHOC:
6981 		BUG_ON(!(network->capability & WLAN_CAPABILITY_IBSS));
6982 
6983 		qos_data = &ibss_data;
6984 		break;
6985 
6986 	case IW_MODE_INFRA:
6987 		qos_data = &network->qos_data;
6988 		break;
6989 
6990 	default:
6991 		BUG();
6992 		break;
6993 	}
6994 
6995 	err = ipw_qos_activate(priv, qos_data);
6996 	if (err) {
6997 		priv->assoc_request.policy_support &= ~HC_QOS_SUPPORT_ASSOC;
6998 		return err;
6999 	}
7000 
7001 	if (priv->qos_data.qos_enable && qos_data->supported) {
7002 		IPW_DEBUG_QOS("QoS will be enabled for this association\n");
7003 		priv->assoc_request.policy_support |= HC_QOS_SUPPORT_ASSOC;
7004 		return ipw_qos_set_info_element(priv);
7005 	}
7006 
7007 	return 0;
7008 }
7009 
7010 /*
7011 * handling the beaconing responses. if we get different QoS setting
7012 * off the network from the associated setting, adjust the QoS
7013 * setting
7014 */
7015 static void ipw_qos_association_resp(struct ipw_priv *priv,
7016 				    struct libipw_network *network)
7017 {
7018 	unsigned long flags;
7019 	u32 size = sizeof(struct libipw_qos_parameters);
7020 	int set_qos_param = 0;
7021 
7022 	if ((priv == NULL) || (network == NULL) ||
7023 	    (priv->assoc_network == NULL))
7024 		return;
7025 
7026 	if (!(priv->status & STATUS_ASSOCIATED))
7027 		return;
7028 
7029 	if ((priv->ieee->iw_mode != IW_MODE_INFRA))
7030 		return;
7031 
7032 	spin_lock_irqsave(&priv->ieee->lock, flags);
7033 	if (network->flags & NETWORK_HAS_QOS_PARAMETERS) {
7034 		memcpy(&priv->assoc_network->qos_data, &network->qos_data,
7035 		       sizeof(struct libipw_qos_data));
7036 		priv->assoc_network->qos_data.active = 1;
7037 		if ((network->qos_data.old_param_count !=
7038 		     network->qos_data.param_count)) {
7039 			set_qos_param = 1;
7040 			network->qos_data.old_param_count =
7041 			    network->qos_data.param_count;
7042 		}
7043 
7044 	} else {
7045 		if ((network->mode == IEEE_B) || (priv->ieee->mode == IEEE_B))
7046 			memcpy(&priv->assoc_network->qos_data.parameters,
7047 			       &def_parameters_CCK, size);
7048 		else
7049 			memcpy(&priv->assoc_network->qos_data.parameters,
7050 			       &def_parameters_OFDM, size);
7051 		priv->assoc_network->qos_data.active = 0;
7052 		priv->assoc_network->qos_data.supported = 0;
7053 		set_qos_param = 1;
7054 	}
7055 
7056 	spin_unlock_irqrestore(&priv->ieee->lock, flags);
7057 
7058 	if (set_qos_param == 1)
7059 		schedule_work(&priv->qos_activate);
7060 }
7061 
7062 static u32 ipw_qos_get_burst_duration(struct ipw_priv *priv)
7063 {
7064 	u32 ret = 0;
7065 
7066 	if (!priv)
7067 		return 0;
7068 
7069 	if (!(priv->ieee->modulation & LIBIPW_OFDM_MODULATION))
7070 		ret = priv->qos_data.burst_duration_CCK;
7071 	else
7072 		ret = priv->qos_data.burst_duration_OFDM;
7073 
7074 	return ret;
7075 }
7076 
7077 /*
7078 * Initialize the setting of QoS global
7079 */
7080 static void ipw_qos_init(struct ipw_priv *priv, int enable,
7081 			 int burst_enable, u32 burst_duration_CCK,
7082 			 u32 burst_duration_OFDM)
7083 {
7084 	priv->qos_data.qos_enable = enable;
7085 
7086 	if (priv->qos_data.qos_enable) {
7087 		priv->qos_data.def_qos_parm_CCK = &def_qos_parameters_CCK;
7088 		priv->qos_data.def_qos_parm_OFDM = &def_qos_parameters_OFDM;
7089 		IPW_DEBUG_QOS("QoS is enabled\n");
7090 	} else {
7091 		priv->qos_data.def_qos_parm_CCK = &def_parameters_CCK;
7092 		priv->qos_data.def_qos_parm_OFDM = &def_parameters_OFDM;
7093 		IPW_DEBUG_QOS("QoS is not enabled\n");
7094 	}
7095 
7096 	priv->qos_data.burst_enable = burst_enable;
7097 
7098 	if (burst_enable) {
7099 		priv->qos_data.burst_duration_CCK = burst_duration_CCK;
7100 		priv->qos_data.burst_duration_OFDM = burst_duration_OFDM;
7101 	} else {
7102 		priv->qos_data.burst_duration_CCK = 0;
7103 		priv->qos_data.burst_duration_OFDM = 0;
7104 	}
7105 }
7106 
7107 /*
7108 * map the packet priority to the right TX Queue
7109 */
7110 static int ipw_get_tx_queue_number(struct ipw_priv *priv, u16 priority)
7111 {
7112 	if (priority > 7 || !priv->qos_data.qos_enable)
7113 		priority = 0;
7114 
7115 	return from_priority_to_tx_queue[priority] - 1;
7116 }
7117 
7118 static int ipw_is_qos_active(struct net_device *dev,
7119 			     struct sk_buff *skb)
7120 {
7121 	struct ipw_priv *priv = libipw_priv(dev);
7122 	struct libipw_qos_data *qos_data = NULL;
7123 	int active, supported;
7124 	u8 *daddr = skb->data + ETH_ALEN;
7125 	int unicast = !is_multicast_ether_addr(daddr);
7126 
7127 	if (!(priv->status & STATUS_ASSOCIATED))
7128 		return 0;
7129 
7130 	qos_data = &priv->assoc_network->qos_data;
7131 
7132 	if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
7133 		if (unicast == 0)
7134 			qos_data->active = 0;
7135 		else
7136 			qos_data->active = qos_data->supported;
7137 	}
7138 	active = qos_data->active;
7139 	supported = qos_data->supported;
7140 	IPW_DEBUG_QOS("QoS  %d network is QoS active %d  supported %d  "
7141 		      "unicast %d\n",
7142 		      priv->qos_data.qos_enable, active, supported, unicast);
7143 	if (active && priv->qos_data.qos_enable)
7144 		return 1;
7145 
7146 	return 0;
7147 
7148 }
7149 /*
7150 * add QoS parameter to the TX command
7151 */
7152 static int ipw_qos_set_tx_queue_command(struct ipw_priv *priv,
7153 					u16 priority,
7154 					struct tfd_data *tfd)
7155 {
7156 	int tx_queue_id = 0;
7157 
7158 
7159 	tx_queue_id = from_priority_to_tx_queue[priority] - 1;
7160 	tfd->tx_flags_ext |= DCT_FLAG_EXT_QOS_ENABLED;
7161 
7162 	if (priv->qos_data.qos_no_ack_mask & (1UL << tx_queue_id)) {
7163 		tfd->tx_flags &= ~DCT_FLAG_ACK_REQD;
7164 		tfd->tfd.tfd_26.mchdr.qos_ctrl |= cpu_to_le16(CTRL_QOS_NO_ACK);
7165 	}
7166 	return 0;
7167 }
7168 
7169 /*
7170 * background support to run QoS activate functionality
7171 */
7172 static void ipw_bg_qos_activate(struct work_struct *work)
7173 {
7174 	struct ipw_priv *priv =
7175 		container_of(work, struct ipw_priv, qos_activate);
7176 
7177 	mutex_lock(&priv->mutex);
7178 
7179 	if (priv->status & STATUS_ASSOCIATED)
7180 		ipw_qos_activate(priv, &(priv->assoc_network->qos_data));
7181 
7182 	mutex_unlock(&priv->mutex);
7183 }
7184 
7185 static int ipw_handle_probe_response(struct net_device *dev,
7186 				     struct libipw_probe_response *resp,
7187 				     struct libipw_network *network)
7188 {
7189 	struct ipw_priv *priv = libipw_priv(dev);
7190 	int active_network = ((priv->status & STATUS_ASSOCIATED) &&
7191 			      (network == priv->assoc_network));
7192 
7193 	ipw_qos_handle_probe_response(priv, active_network, network);
7194 
7195 	return 0;
7196 }
7197 
7198 static int ipw_handle_beacon(struct net_device *dev,
7199 			     struct libipw_beacon *resp,
7200 			     struct libipw_network *network)
7201 {
7202 	struct ipw_priv *priv = libipw_priv(dev);
7203 	int active_network = ((priv->status & STATUS_ASSOCIATED) &&
7204 			      (network == priv->assoc_network));
7205 
7206 	ipw_qos_handle_probe_response(priv, active_network, network);
7207 
7208 	return 0;
7209 }
7210 
7211 static int ipw_handle_assoc_response(struct net_device *dev,
7212 				     struct libipw_assoc_response *resp,
7213 				     struct libipw_network *network)
7214 {
7215 	struct ipw_priv *priv = libipw_priv(dev);
7216 	ipw_qos_association_resp(priv, network);
7217 	return 0;
7218 }
7219 
7220 static int ipw_send_qos_params_command(struct ipw_priv *priv, struct libipw_qos_parameters
7221 				       *qos_param)
7222 {
7223 	return ipw_send_cmd_pdu(priv, IPW_CMD_QOS_PARAMETERS,
7224 				sizeof(*qos_param) * 3, qos_param);
7225 }
7226 
7227 static int ipw_send_qos_info_command(struct ipw_priv *priv, struct libipw_qos_information_element
7228 				     *qos_param)
7229 {
7230 	return ipw_send_cmd_pdu(priv, IPW_CMD_WME_INFO, sizeof(*qos_param),
7231 				qos_param);
7232 }
7233 
7234 #endif				/* CONFIG_IPW2200_QOS */
7235 
7236 static int ipw_associate_network(struct ipw_priv *priv,
7237 				 struct libipw_network *network,
7238 				 struct ipw_supported_rates *rates, int roaming)
7239 {
7240 	int err;
7241 
7242 	if (priv->config & CFG_FIXED_RATE)
7243 		ipw_set_fixed_rate(priv, network->mode);
7244 
7245 	if (!(priv->config & CFG_STATIC_ESSID)) {
7246 		priv->essid_len = min(network->ssid_len,
7247 				      (u8) IW_ESSID_MAX_SIZE);
7248 		memcpy(priv->essid, network->ssid, priv->essid_len);
7249 	}
7250 
7251 	network->last_associate = jiffies;
7252 
7253 	memset(&priv->assoc_request, 0, sizeof(priv->assoc_request));
7254 	priv->assoc_request.channel = network->channel;
7255 	priv->assoc_request.auth_key = 0;
7256 
7257 	if ((priv->capability & CAP_PRIVACY_ON) &&
7258 	    (priv->ieee->sec.auth_mode == WLAN_AUTH_SHARED_KEY)) {
7259 		priv->assoc_request.auth_type = AUTH_SHARED_KEY;
7260 		priv->assoc_request.auth_key = priv->ieee->sec.active_key;
7261 
7262 		if (priv->ieee->sec.level == SEC_LEVEL_1)
7263 			ipw_send_wep_keys(priv, DCW_WEP_KEY_SEC_TYPE_WEP);
7264 
7265 	} else if ((priv->capability & CAP_PRIVACY_ON) &&
7266 		   (priv->ieee->sec.auth_mode == WLAN_AUTH_LEAP))
7267 		priv->assoc_request.auth_type = AUTH_LEAP;
7268 	else
7269 		priv->assoc_request.auth_type = AUTH_OPEN;
7270 
7271 	if (priv->ieee->wpa_ie_len) {
7272 		priv->assoc_request.policy_support = cpu_to_le16(0x02);	/* RSN active */
7273 		ipw_set_rsn_capa(priv, priv->ieee->wpa_ie,
7274 				 priv->ieee->wpa_ie_len);
7275 	}
7276 
7277 	/*
7278 	 * It is valid for our ieee device to support multiple modes, but
7279 	 * when it comes to associating to a given network we have to choose
7280 	 * just one mode.
7281 	 */
7282 	if (network->mode & priv->ieee->mode & IEEE_A)
7283 		priv->assoc_request.ieee_mode = IPW_A_MODE;
7284 	else if (network->mode & priv->ieee->mode & IEEE_G)
7285 		priv->assoc_request.ieee_mode = IPW_G_MODE;
7286 	else if (network->mode & priv->ieee->mode & IEEE_B)
7287 		priv->assoc_request.ieee_mode = IPW_B_MODE;
7288 
7289 	priv->assoc_request.capability = cpu_to_le16(network->capability);
7290 	if ((network->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7291 	    && !(priv->config & CFG_PREAMBLE_LONG)) {
7292 		priv->assoc_request.preamble_length = DCT_FLAG_SHORT_PREAMBLE;
7293 	} else {
7294 		priv->assoc_request.preamble_length = DCT_FLAG_LONG_PREAMBLE;
7295 
7296 		/* Clear the short preamble if we won't be supporting it */
7297 		priv->assoc_request.capability &=
7298 		    ~cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
7299 	}
7300 
7301 	/* Clear capability bits that aren't used in Ad Hoc */
7302 	if (priv->ieee->iw_mode == IW_MODE_ADHOC)
7303 		priv->assoc_request.capability &=
7304 		    ~cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
7305 
7306 	IPW_DEBUG_ASSOC("%ssociation attempt: '%*pE', channel %d, 802.11%c [%d], %s[:%s], enc=%s%s%s%c%c\n",
7307 			roaming ? "Rea" : "A",
7308 			priv->essid_len, priv->essid,
7309 			network->channel,
7310 			ipw_modes[priv->assoc_request.ieee_mode],
7311 			rates->num_rates,
7312 			(priv->assoc_request.preamble_length ==
7313 			 DCT_FLAG_LONG_PREAMBLE) ? "long" : "short",
7314 			network->capability &
7315 			WLAN_CAPABILITY_SHORT_PREAMBLE ? "short" : "long",
7316 			priv->capability & CAP_PRIVACY_ON ? "on " : "off",
7317 			priv->capability & CAP_PRIVACY_ON ?
7318 			(priv->capability & CAP_SHARED_KEY ? "(shared)" :
7319 			 "(open)") : "",
7320 			priv->capability & CAP_PRIVACY_ON ? " key=" : "",
7321 			priv->capability & CAP_PRIVACY_ON ?
7322 			'1' + priv->ieee->sec.active_key : '.',
7323 			priv->capability & CAP_PRIVACY_ON ? '.' : ' ');
7324 
7325 	priv->assoc_request.beacon_interval = cpu_to_le16(network->beacon_interval);
7326 	if ((priv->ieee->iw_mode == IW_MODE_ADHOC) &&
7327 	    (network->time_stamp[0] == 0) && (network->time_stamp[1] == 0)) {
7328 		priv->assoc_request.assoc_type = HC_IBSS_START;
7329 		priv->assoc_request.assoc_tsf_msw = 0;
7330 		priv->assoc_request.assoc_tsf_lsw = 0;
7331 	} else {
7332 		if (unlikely(roaming))
7333 			priv->assoc_request.assoc_type = HC_REASSOCIATE;
7334 		else
7335 			priv->assoc_request.assoc_type = HC_ASSOCIATE;
7336 		priv->assoc_request.assoc_tsf_msw = cpu_to_le32(network->time_stamp[1]);
7337 		priv->assoc_request.assoc_tsf_lsw = cpu_to_le32(network->time_stamp[0]);
7338 	}
7339 
7340 	memcpy(priv->assoc_request.bssid, network->bssid, ETH_ALEN);
7341 
7342 	if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
7343 		eth_broadcast_addr(priv->assoc_request.dest);
7344 		priv->assoc_request.atim_window = cpu_to_le16(network->atim_window);
7345 	} else {
7346 		memcpy(priv->assoc_request.dest, network->bssid, ETH_ALEN);
7347 		priv->assoc_request.atim_window = 0;
7348 	}
7349 
7350 	priv->assoc_request.listen_interval = cpu_to_le16(network->listen_interval);
7351 
7352 	err = ipw_send_ssid(priv, priv->essid, priv->essid_len);
7353 	if (err) {
7354 		IPW_DEBUG_HC("Attempt to send SSID command failed.\n");
7355 		return err;
7356 	}
7357 
7358 	rates->ieee_mode = priv->assoc_request.ieee_mode;
7359 	rates->purpose = IPW_RATE_CONNECT;
7360 	ipw_send_supported_rates(priv, rates);
7361 
7362 	if (priv->assoc_request.ieee_mode == IPW_G_MODE)
7363 		priv->sys_config.dot11g_auto_detection = 1;
7364 	else
7365 		priv->sys_config.dot11g_auto_detection = 0;
7366 
7367 	if (priv->ieee->iw_mode == IW_MODE_ADHOC)
7368 		priv->sys_config.answer_broadcast_ssid_probe = 1;
7369 	else
7370 		priv->sys_config.answer_broadcast_ssid_probe = 0;
7371 
7372 	err = ipw_send_system_config(priv);
7373 	if (err) {
7374 		IPW_DEBUG_HC("Attempt to send sys config command failed.\n");
7375 		return err;
7376 	}
7377 
7378 	IPW_DEBUG_ASSOC("Association sensitivity: %d\n", network->stats.rssi);
7379 	err = ipw_set_sensitivity(priv, network->stats.rssi + IPW_RSSI_TO_DBM);
7380 	if (err) {
7381 		IPW_DEBUG_HC("Attempt to send associate command failed.\n");
7382 		return err;
7383 	}
7384 
7385 	/*
7386 	 * If preemption is enabled, it is possible for the association
7387 	 * to complete before we return from ipw_send_associate.  Therefore
7388 	 * we have to be sure and update our priviate data first.
7389 	 */
7390 	priv->channel = network->channel;
7391 	memcpy(priv->bssid, network->bssid, ETH_ALEN);
7392 	priv->status |= STATUS_ASSOCIATING;
7393 	priv->status &= ~STATUS_SECURITY_UPDATED;
7394 
7395 	priv->assoc_network = network;
7396 
7397 #ifdef CONFIG_IPW2200_QOS
7398 	ipw_qos_association(priv, network);
7399 #endif
7400 
7401 	err = ipw_send_associate(priv, &priv->assoc_request);
7402 	if (err) {
7403 		IPW_DEBUG_HC("Attempt to send associate command failed.\n");
7404 		return err;
7405 	}
7406 
7407 	IPW_DEBUG(IPW_DL_STATE, "associating: '%*pE' %pM\n",
7408 		  priv->essid_len, priv->essid, priv->bssid);
7409 
7410 	return 0;
7411 }
7412 
7413 static void ipw_roam(void *data)
7414 {
7415 	struct ipw_priv *priv = data;
7416 	struct libipw_network *network = NULL;
7417 	struct ipw_network_match match = {
7418 		.network = priv->assoc_network
7419 	};
7420 
7421 	/* The roaming process is as follows:
7422 	 *
7423 	 * 1.  Missed beacon threshold triggers the roaming process by
7424 	 *     setting the status ROAM bit and requesting a scan.
7425 	 * 2.  When the scan completes, it schedules the ROAM work
7426 	 * 3.  The ROAM work looks at all of the known networks for one that
7427 	 *     is a better network than the currently associated.  If none
7428 	 *     found, the ROAM process is over (ROAM bit cleared)
7429 	 * 4.  If a better network is found, a disassociation request is
7430 	 *     sent.
7431 	 * 5.  When the disassociation completes, the roam work is again
7432 	 *     scheduled.  The second time through, the driver is no longer
7433 	 *     associated, and the newly selected network is sent an
7434 	 *     association request.
7435 	 * 6.  At this point ,the roaming process is complete and the ROAM
7436 	 *     status bit is cleared.
7437 	 */
7438 
7439 	/* If we are no longer associated, and the roaming bit is no longer
7440 	 * set, then we are not actively roaming, so just return */
7441 	if (!(priv->status & (STATUS_ASSOCIATED | STATUS_ROAMING)))
7442 		return;
7443 
7444 	if (priv->status & STATUS_ASSOCIATED) {
7445 		/* First pass through ROAM process -- look for a better
7446 		 * network */
7447 		unsigned long flags;
7448 		u8 rssi = priv->assoc_network->stats.rssi;
7449 		priv->assoc_network->stats.rssi = -128;
7450 		spin_lock_irqsave(&priv->ieee->lock, flags);
7451 		list_for_each_entry(network, &priv->ieee->network_list, list) {
7452 			if (network != priv->assoc_network)
7453 				ipw_best_network(priv, &match, network, 1);
7454 		}
7455 		spin_unlock_irqrestore(&priv->ieee->lock, flags);
7456 		priv->assoc_network->stats.rssi = rssi;
7457 
7458 		if (match.network == priv->assoc_network) {
7459 			IPW_DEBUG_ASSOC("No better APs in this network to "
7460 					"roam to.\n");
7461 			priv->status &= ~STATUS_ROAMING;
7462 			ipw_debug_config(priv);
7463 			return;
7464 		}
7465 
7466 		ipw_send_disassociate(priv, 1);
7467 		priv->assoc_network = match.network;
7468 
7469 		return;
7470 	}
7471 
7472 	/* Second pass through ROAM process -- request association */
7473 	ipw_compatible_rates(priv, priv->assoc_network, &match.rates);
7474 	ipw_associate_network(priv, priv->assoc_network, &match.rates, 1);
7475 	priv->status &= ~STATUS_ROAMING;
7476 }
7477 
7478 static void ipw_bg_roam(struct work_struct *work)
7479 {
7480 	struct ipw_priv *priv =
7481 		container_of(work, struct ipw_priv, roam);
7482 	mutex_lock(&priv->mutex);
7483 	ipw_roam(priv);
7484 	mutex_unlock(&priv->mutex);
7485 }
7486 
7487 static int ipw_associate(void *data)
7488 {
7489 	struct ipw_priv *priv = data;
7490 
7491 	struct libipw_network *network = NULL;
7492 	struct ipw_network_match match = {
7493 		.network = NULL
7494 	};
7495 	struct ipw_supported_rates *rates;
7496 	struct list_head *element;
7497 	unsigned long flags;
7498 
7499 	if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
7500 		IPW_DEBUG_ASSOC("Not attempting association (monitor mode)\n");
7501 		return 0;
7502 	}
7503 
7504 	if (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
7505 		IPW_DEBUG_ASSOC("Not attempting association (already in "
7506 				"progress)\n");
7507 		return 0;
7508 	}
7509 
7510 	if (priv->status & STATUS_DISASSOCIATING) {
7511 		IPW_DEBUG_ASSOC("Not attempting association (in disassociating)\n");
7512 		schedule_work(&priv->associate);
7513 		return 0;
7514 	}
7515 
7516 	if (!ipw_is_init(priv) || (priv->status & STATUS_SCANNING)) {
7517 		IPW_DEBUG_ASSOC("Not attempting association (scanning or not "
7518 				"initialized)\n");
7519 		return 0;
7520 	}
7521 
7522 	if (!(priv->config & CFG_ASSOCIATE) &&
7523 	    !(priv->config & (CFG_STATIC_ESSID | CFG_STATIC_BSSID))) {
7524 		IPW_DEBUG_ASSOC("Not attempting association (associate=0)\n");
7525 		return 0;
7526 	}
7527 
7528 	/* Protect our use of the network_list */
7529 	spin_lock_irqsave(&priv->ieee->lock, flags);
7530 	list_for_each_entry(network, &priv->ieee->network_list, list)
7531 	    ipw_best_network(priv, &match, network, 0);
7532 
7533 	network = match.network;
7534 	rates = &match.rates;
7535 
7536 	if (network == NULL &&
7537 	    priv->ieee->iw_mode == IW_MODE_ADHOC &&
7538 	    priv->config & CFG_ADHOC_CREATE &&
7539 	    priv->config & CFG_STATIC_ESSID &&
7540 	    priv->config & CFG_STATIC_CHANNEL) {
7541 		/* Use oldest network if the free list is empty */
7542 		if (list_empty(&priv->ieee->network_free_list)) {
7543 			struct libipw_network *oldest = NULL;
7544 			struct libipw_network *target;
7545 
7546 			list_for_each_entry(target, &priv->ieee->network_list, list) {
7547 				if ((oldest == NULL) ||
7548 				    (target->last_scanned < oldest->last_scanned))
7549 					oldest = target;
7550 			}
7551 
7552 			/* If there are no more slots, expire the oldest */
7553 			list_del(&oldest->list);
7554 			target = oldest;
7555 			IPW_DEBUG_ASSOC("Expired '%*pE' (%pM) from network list.\n",
7556 					target->ssid_len, target->ssid,
7557 					target->bssid);
7558 			list_add_tail(&target->list,
7559 				      &priv->ieee->network_free_list);
7560 		}
7561 
7562 		element = priv->ieee->network_free_list.next;
7563 		network = list_entry(element, struct libipw_network, list);
7564 		ipw_adhoc_create(priv, network);
7565 		rates = &priv->rates;
7566 		list_del(element);
7567 		list_add_tail(&network->list, &priv->ieee->network_list);
7568 	}
7569 	spin_unlock_irqrestore(&priv->ieee->lock, flags);
7570 
7571 	/* If we reached the end of the list, then we don't have any valid
7572 	 * matching APs */
7573 	if (!network) {
7574 		ipw_debug_config(priv);
7575 
7576 		if (!(priv->status & STATUS_SCANNING)) {
7577 			if (!(priv->config & CFG_SPEED_SCAN))
7578 				schedule_delayed_work(&priv->request_scan,
7579 						      SCAN_INTERVAL);
7580 			else
7581 				schedule_delayed_work(&priv->request_scan, 0);
7582 		}
7583 
7584 		return 0;
7585 	}
7586 
7587 	ipw_associate_network(priv, network, rates, 0);
7588 
7589 	return 1;
7590 }
7591 
7592 static void ipw_bg_associate(struct work_struct *work)
7593 {
7594 	struct ipw_priv *priv =
7595 		container_of(work, struct ipw_priv, associate);
7596 	mutex_lock(&priv->mutex);
7597 	ipw_associate(priv);
7598 	mutex_unlock(&priv->mutex);
7599 }
7600 
7601 static void ipw_rebuild_decrypted_skb(struct ipw_priv *priv,
7602 				      struct sk_buff *skb)
7603 {
7604 	struct ieee80211_hdr *hdr;
7605 	u16 fc;
7606 
7607 	hdr = (struct ieee80211_hdr *)skb->data;
7608 	fc = le16_to_cpu(hdr->frame_control);
7609 	if (!(fc & IEEE80211_FCTL_PROTECTED))
7610 		return;
7611 
7612 	fc &= ~IEEE80211_FCTL_PROTECTED;
7613 	hdr->frame_control = cpu_to_le16(fc);
7614 	switch (priv->ieee->sec.level) {
7615 	case SEC_LEVEL_3:
7616 		/* Remove CCMP HDR */
7617 		memmove(skb->data + LIBIPW_3ADDR_LEN,
7618 			skb->data + LIBIPW_3ADDR_LEN + 8,
7619 			skb->len - LIBIPW_3ADDR_LEN - 8);
7620 		skb_trim(skb, skb->len - 16);	/* CCMP_HDR_LEN + CCMP_MIC_LEN */
7621 		break;
7622 	case SEC_LEVEL_2:
7623 		break;
7624 	case SEC_LEVEL_1:
7625 		/* Remove IV */
7626 		memmove(skb->data + LIBIPW_3ADDR_LEN,
7627 			skb->data + LIBIPW_3ADDR_LEN + 4,
7628 			skb->len - LIBIPW_3ADDR_LEN - 4);
7629 		skb_trim(skb, skb->len - 8);	/* IV + ICV */
7630 		break;
7631 	case SEC_LEVEL_0:
7632 		break;
7633 	default:
7634 		printk(KERN_ERR "Unknown security level %d\n",
7635 		       priv->ieee->sec.level);
7636 		break;
7637 	}
7638 }
7639 
7640 static void ipw_handle_data_packet(struct ipw_priv *priv,
7641 				   struct ipw_rx_mem_buffer *rxb,
7642 				   struct libipw_rx_stats *stats)
7643 {
7644 	struct net_device *dev = priv->net_dev;
7645 	struct libipw_hdr_4addr *hdr;
7646 	struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)rxb->skb->data;
7647 
7648 	/* We received data from the HW, so stop the watchdog */
7649 	netif_trans_update(dev);
7650 
7651 	/* We only process data packets if the
7652 	 * interface is open */
7653 	if (unlikely((le16_to_cpu(pkt->u.frame.length) + IPW_RX_FRAME_SIZE) >
7654 		     skb_tailroom(rxb->skb))) {
7655 		dev->stats.rx_errors++;
7656 		priv->wstats.discard.misc++;
7657 		IPW_DEBUG_DROP("Corruption detected! Oh no!\n");
7658 		return;
7659 	} else if (unlikely(!netif_running(priv->net_dev))) {
7660 		dev->stats.rx_dropped++;
7661 		priv->wstats.discard.misc++;
7662 		IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
7663 		return;
7664 	}
7665 
7666 	/* Advance skb->data to the start of the actual payload */
7667 	skb_reserve(rxb->skb, offsetof(struct ipw_rx_packet, u.frame.data));
7668 
7669 	/* Set the size of the skb to the size of the frame */
7670 	skb_put(rxb->skb, le16_to_cpu(pkt->u.frame.length));
7671 
7672 	IPW_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
7673 
7674 	/* HW decrypt will not clear the WEP bit, MIC, PN, etc. */
7675 	hdr = (struct libipw_hdr_4addr *)rxb->skb->data;
7676 	if (priv->ieee->iw_mode != IW_MODE_MONITOR &&
7677 	    (is_multicast_ether_addr(hdr->addr1) ?
7678 	     !priv->ieee->host_mc_decrypt : !priv->ieee->host_decrypt))
7679 		ipw_rebuild_decrypted_skb(priv, rxb->skb);
7680 
7681 	if (!libipw_rx(priv->ieee, rxb->skb, stats))
7682 		dev->stats.rx_errors++;
7683 	else {			/* libipw_rx succeeded, so it now owns the SKB */
7684 		rxb->skb = NULL;
7685 		__ipw_led_activity_on(priv);
7686 	}
7687 }
7688 
7689 #ifdef CONFIG_IPW2200_RADIOTAP
7690 static void ipw_handle_data_packet_monitor(struct ipw_priv *priv,
7691 					   struct ipw_rx_mem_buffer *rxb,
7692 					   struct libipw_rx_stats *stats)
7693 {
7694 	struct net_device *dev = priv->net_dev;
7695 	struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)rxb->skb->data;
7696 	struct ipw_rx_frame *frame = &pkt->u.frame;
7697 
7698 	/* initial pull of some data */
7699 	u16 received_channel = frame->received_channel;
7700 	u8 antennaAndPhy = frame->antennaAndPhy;
7701 	s8 antsignal = frame->rssi_dbm - IPW_RSSI_TO_DBM;	/* call it signed anyhow */
7702 	u16 pktrate = frame->rate;
7703 
7704 	/* Magic struct that slots into the radiotap header -- no reason
7705 	 * to build this manually element by element, we can write it much
7706 	 * more efficiently than we can parse it. ORDER MATTERS HERE */
7707 	struct ipw_rt_hdr *ipw_rt;
7708 
7709 	unsigned short len = le16_to_cpu(pkt->u.frame.length);
7710 
7711 	/* We received data from the HW, so stop the watchdog */
7712 	netif_trans_update(dev);
7713 
7714 	/* We only process data packets if the
7715 	 * interface is open */
7716 	if (unlikely((le16_to_cpu(pkt->u.frame.length) + IPW_RX_FRAME_SIZE) >
7717 		     skb_tailroom(rxb->skb))) {
7718 		dev->stats.rx_errors++;
7719 		priv->wstats.discard.misc++;
7720 		IPW_DEBUG_DROP("Corruption detected! Oh no!\n");
7721 		return;
7722 	} else if (unlikely(!netif_running(priv->net_dev))) {
7723 		dev->stats.rx_dropped++;
7724 		priv->wstats.discard.misc++;
7725 		IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
7726 		return;
7727 	}
7728 
7729 	/* Libpcap 0.9.3+ can handle variable length radiotap, so we'll use
7730 	 * that now */
7731 	if (len > IPW_RX_BUF_SIZE - sizeof(struct ipw_rt_hdr)) {
7732 		/* FIXME: Should alloc bigger skb instead */
7733 		dev->stats.rx_dropped++;
7734 		priv->wstats.discard.misc++;
7735 		IPW_DEBUG_DROP("Dropping too large packet in monitor\n");
7736 		return;
7737 	}
7738 
7739 	/* copy the frame itself */
7740 	memmove(rxb->skb->data + sizeof(struct ipw_rt_hdr),
7741 		rxb->skb->data + IPW_RX_FRAME_SIZE, len);
7742 
7743 	ipw_rt = (struct ipw_rt_hdr *)rxb->skb->data;
7744 
7745 	ipw_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
7746 	ipw_rt->rt_hdr.it_pad = 0;	/* always good to zero */
7747 	ipw_rt->rt_hdr.it_len = cpu_to_le16(sizeof(struct ipw_rt_hdr));	/* total header+data */
7748 
7749 	/* Big bitfield of all the fields we provide in radiotap */
7750 	ipw_rt->rt_hdr.it_present = cpu_to_le32(
7751 	     (1 << IEEE80211_RADIOTAP_TSFT) |
7752 	     (1 << IEEE80211_RADIOTAP_FLAGS) |
7753 	     (1 << IEEE80211_RADIOTAP_RATE) |
7754 	     (1 << IEEE80211_RADIOTAP_CHANNEL) |
7755 	     (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
7756 	     (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
7757 	     (1 << IEEE80211_RADIOTAP_ANTENNA));
7758 
7759 	/* Zero the flags, we'll add to them as we go */
7760 	ipw_rt->rt_flags = 0;
7761 	ipw_rt->rt_tsf = (u64)(frame->parent_tsf[3] << 24 |
7762 			       frame->parent_tsf[2] << 16 |
7763 			       frame->parent_tsf[1] << 8  |
7764 			       frame->parent_tsf[0]);
7765 
7766 	/* Convert signal to DBM */
7767 	ipw_rt->rt_dbmsignal = antsignal;
7768 	ipw_rt->rt_dbmnoise = (s8) le16_to_cpu(frame->noise);
7769 
7770 	/* Convert the channel data and set the flags */
7771 	ipw_rt->rt_channel = cpu_to_le16(ieee80211chan2mhz(received_channel));
7772 	if (received_channel > 14) {	/* 802.11a */
7773 		ipw_rt->rt_chbitmask =
7774 		    cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
7775 	} else if (antennaAndPhy & 32) {	/* 802.11b */
7776 		ipw_rt->rt_chbitmask =
7777 		    cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
7778 	} else {		/* 802.11g */
7779 		ipw_rt->rt_chbitmask =
7780 		    cpu_to_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ);
7781 	}
7782 
7783 	/* set the rate in multiples of 500k/s */
7784 	switch (pktrate) {
7785 	case IPW_TX_RATE_1MB:
7786 		ipw_rt->rt_rate = 2;
7787 		break;
7788 	case IPW_TX_RATE_2MB:
7789 		ipw_rt->rt_rate = 4;
7790 		break;
7791 	case IPW_TX_RATE_5MB:
7792 		ipw_rt->rt_rate = 10;
7793 		break;
7794 	case IPW_TX_RATE_6MB:
7795 		ipw_rt->rt_rate = 12;
7796 		break;
7797 	case IPW_TX_RATE_9MB:
7798 		ipw_rt->rt_rate = 18;
7799 		break;
7800 	case IPW_TX_RATE_11MB:
7801 		ipw_rt->rt_rate = 22;
7802 		break;
7803 	case IPW_TX_RATE_12MB:
7804 		ipw_rt->rt_rate = 24;
7805 		break;
7806 	case IPW_TX_RATE_18MB:
7807 		ipw_rt->rt_rate = 36;
7808 		break;
7809 	case IPW_TX_RATE_24MB:
7810 		ipw_rt->rt_rate = 48;
7811 		break;
7812 	case IPW_TX_RATE_36MB:
7813 		ipw_rt->rt_rate = 72;
7814 		break;
7815 	case IPW_TX_RATE_48MB:
7816 		ipw_rt->rt_rate = 96;
7817 		break;
7818 	case IPW_TX_RATE_54MB:
7819 		ipw_rt->rt_rate = 108;
7820 		break;
7821 	default:
7822 		ipw_rt->rt_rate = 0;
7823 		break;
7824 	}
7825 
7826 	/* antenna number */
7827 	ipw_rt->rt_antenna = (antennaAndPhy & 3);	/* Is this right? */
7828 
7829 	/* set the preamble flag if we have it */
7830 	if ((antennaAndPhy & 64))
7831 		ipw_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
7832 
7833 	/* Set the size of the skb to the size of the frame */
7834 	skb_put(rxb->skb, len + sizeof(struct ipw_rt_hdr));
7835 
7836 	IPW_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
7837 
7838 	if (!libipw_rx(priv->ieee, rxb->skb, stats))
7839 		dev->stats.rx_errors++;
7840 	else {			/* libipw_rx succeeded, so it now owns the SKB */
7841 		rxb->skb = NULL;
7842 		/* no LED during capture */
7843 	}
7844 }
7845 #endif
7846 
7847 #ifdef CONFIG_IPW2200_PROMISCUOUS
7848 #define libipw_is_probe_response(fc) \
7849    ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT && \
7850     (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP )
7851 
7852 #define libipw_is_management(fc) \
7853    ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
7854 
7855 #define libipw_is_control(fc) \
7856    ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
7857 
7858 #define libipw_is_data(fc) \
7859    ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
7860 
7861 #define libipw_is_assoc_request(fc) \
7862    ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ)
7863 
7864 #define libipw_is_reassoc_request(fc) \
7865    ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
7866 
7867 static void ipw_handle_promiscuous_rx(struct ipw_priv *priv,
7868 				      struct ipw_rx_mem_buffer *rxb,
7869 				      struct libipw_rx_stats *stats)
7870 {
7871 	struct net_device *dev = priv->prom_net_dev;
7872 	struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)rxb->skb->data;
7873 	struct ipw_rx_frame *frame = &pkt->u.frame;
7874 	struct ipw_rt_hdr *ipw_rt;
7875 
7876 	/* First cache any information we need before we overwrite
7877 	 * the information provided in the skb from the hardware */
7878 	struct ieee80211_hdr *hdr;
7879 	u16 channel = frame->received_channel;
7880 	u8 phy_flags = frame->antennaAndPhy;
7881 	s8 signal = frame->rssi_dbm - IPW_RSSI_TO_DBM;
7882 	s8 noise = (s8) le16_to_cpu(frame->noise);
7883 	u8 rate = frame->rate;
7884 	unsigned short len = le16_to_cpu(pkt->u.frame.length);
7885 	struct sk_buff *skb;
7886 	int hdr_only = 0;
7887 	u16 filter = priv->prom_priv->filter;
7888 
7889 	/* If the filter is set to not include Rx frames then return */
7890 	if (filter & IPW_PROM_NO_RX)
7891 		return;
7892 
7893 	/* We received data from the HW, so stop the watchdog */
7894 	netif_trans_update(dev);
7895 
7896 	if (unlikely((len + IPW_RX_FRAME_SIZE) > skb_tailroom(rxb->skb))) {
7897 		dev->stats.rx_errors++;
7898 		IPW_DEBUG_DROP("Corruption detected! Oh no!\n");
7899 		return;
7900 	}
7901 
7902 	/* We only process data packets if the interface is open */
7903 	if (unlikely(!netif_running(dev))) {
7904 		dev->stats.rx_dropped++;
7905 		IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
7906 		return;
7907 	}
7908 
7909 	/* Libpcap 0.9.3+ can handle variable length radiotap, so we'll use
7910 	 * that now */
7911 	if (len > IPW_RX_BUF_SIZE - sizeof(struct ipw_rt_hdr)) {
7912 		/* FIXME: Should alloc bigger skb instead */
7913 		dev->stats.rx_dropped++;
7914 		IPW_DEBUG_DROP("Dropping too large packet in monitor\n");
7915 		return;
7916 	}
7917 
7918 	hdr = (void *)rxb->skb->data + IPW_RX_FRAME_SIZE;
7919 	if (libipw_is_management(le16_to_cpu(hdr->frame_control))) {
7920 		if (filter & IPW_PROM_NO_MGMT)
7921 			return;
7922 		if (filter & IPW_PROM_MGMT_HEADER_ONLY)
7923 			hdr_only = 1;
7924 	} else if (libipw_is_control(le16_to_cpu(hdr->frame_control))) {
7925 		if (filter & IPW_PROM_NO_CTL)
7926 			return;
7927 		if (filter & IPW_PROM_CTL_HEADER_ONLY)
7928 			hdr_only = 1;
7929 	} else if (libipw_is_data(le16_to_cpu(hdr->frame_control))) {
7930 		if (filter & IPW_PROM_NO_DATA)
7931 			return;
7932 		if (filter & IPW_PROM_DATA_HEADER_ONLY)
7933 			hdr_only = 1;
7934 	}
7935 
7936 	/* Copy the SKB since this is for the promiscuous side */
7937 	skb = skb_copy(rxb->skb, GFP_ATOMIC);
7938 	if (skb == NULL) {
7939 		IPW_ERROR("skb_clone failed for promiscuous copy.\n");
7940 		return;
7941 	}
7942 
7943 	/* copy the frame data to write after where the radiotap header goes */
7944 	ipw_rt = (void *)skb->data;
7945 
7946 	if (hdr_only)
7947 		len = libipw_get_hdrlen(le16_to_cpu(hdr->frame_control));
7948 
7949 	memcpy(ipw_rt->payload, hdr, len);
7950 
7951 	ipw_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
7952 	ipw_rt->rt_hdr.it_pad = 0;	/* always good to zero */
7953 	ipw_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*ipw_rt));	/* total header+data */
7954 
7955 	/* Set the size of the skb to the size of the frame */
7956 	skb_put(skb, sizeof(*ipw_rt) + len);
7957 
7958 	/* Big bitfield of all the fields we provide in radiotap */
7959 	ipw_rt->rt_hdr.it_present = cpu_to_le32(
7960 	     (1 << IEEE80211_RADIOTAP_TSFT) |
7961 	     (1 << IEEE80211_RADIOTAP_FLAGS) |
7962 	     (1 << IEEE80211_RADIOTAP_RATE) |
7963 	     (1 << IEEE80211_RADIOTAP_CHANNEL) |
7964 	     (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
7965 	     (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
7966 	     (1 << IEEE80211_RADIOTAP_ANTENNA));
7967 
7968 	/* Zero the flags, we'll add to them as we go */
7969 	ipw_rt->rt_flags = 0;
7970 	ipw_rt->rt_tsf = (u64)(frame->parent_tsf[3] << 24 |
7971 			       frame->parent_tsf[2] << 16 |
7972 			       frame->parent_tsf[1] << 8  |
7973 			       frame->parent_tsf[0]);
7974 
7975 	/* Convert to DBM */
7976 	ipw_rt->rt_dbmsignal = signal;
7977 	ipw_rt->rt_dbmnoise = noise;
7978 
7979 	/* Convert the channel data and set the flags */
7980 	ipw_rt->rt_channel = cpu_to_le16(ieee80211chan2mhz(channel));
7981 	if (channel > 14) {	/* 802.11a */
7982 		ipw_rt->rt_chbitmask =
7983 		    cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
7984 	} else if (phy_flags & (1 << 5)) {	/* 802.11b */
7985 		ipw_rt->rt_chbitmask =
7986 		    cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
7987 	} else {		/* 802.11g */
7988 		ipw_rt->rt_chbitmask =
7989 		    cpu_to_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ);
7990 	}
7991 
7992 	/* set the rate in multiples of 500k/s */
7993 	switch (rate) {
7994 	case IPW_TX_RATE_1MB:
7995 		ipw_rt->rt_rate = 2;
7996 		break;
7997 	case IPW_TX_RATE_2MB:
7998 		ipw_rt->rt_rate = 4;
7999 		break;
8000 	case IPW_TX_RATE_5MB:
8001 		ipw_rt->rt_rate = 10;
8002 		break;
8003 	case IPW_TX_RATE_6MB:
8004 		ipw_rt->rt_rate = 12;
8005 		break;
8006 	case IPW_TX_RATE_9MB:
8007 		ipw_rt->rt_rate = 18;
8008 		break;
8009 	case IPW_TX_RATE_11MB:
8010 		ipw_rt->rt_rate = 22;
8011 		break;
8012 	case IPW_TX_RATE_12MB:
8013 		ipw_rt->rt_rate = 24;
8014 		break;
8015 	case IPW_TX_RATE_18MB:
8016 		ipw_rt->rt_rate = 36;
8017 		break;
8018 	case IPW_TX_RATE_24MB:
8019 		ipw_rt->rt_rate = 48;
8020 		break;
8021 	case IPW_TX_RATE_36MB:
8022 		ipw_rt->rt_rate = 72;
8023 		break;
8024 	case IPW_TX_RATE_48MB:
8025 		ipw_rt->rt_rate = 96;
8026 		break;
8027 	case IPW_TX_RATE_54MB:
8028 		ipw_rt->rt_rate = 108;
8029 		break;
8030 	default:
8031 		ipw_rt->rt_rate = 0;
8032 		break;
8033 	}
8034 
8035 	/* antenna number */
8036 	ipw_rt->rt_antenna = (phy_flags & 3);
8037 
8038 	/* set the preamble flag if we have it */
8039 	if (phy_flags & (1 << 6))
8040 		ipw_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
8041 
8042 	IPW_DEBUG_RX("Rx packet of %d bytes.\n", skb->len);
8043 
8044 	if (!libipw_rx(priv->prom_priv->ieee, skb, stats)) {
8045 		dev->stats.rx_errors++;
8046 		dev_kfree_skb_any(skb);
8047 	}
8048 }
8049 #endif
8050 
8051 static int is_network_packet(struct ipw_priv *priv,
8052 				    struct libipw_hdr_4addr *header)
8053 {
8054 	/* Filter incoming packets to determine if they are targeted toward
8055 	 * this network, discarding packets coming from ourselves */
8056 	switch (priv->ieee->iw_mode) {
8057 	case IW_MODE_ADHOC:	/* Header: Dest. | Source    | BSSID */
8058 		/* packets from our adapter are dropped (echo) */
8059 		if (ether_addr_equal(header->addr2, priv->net_dev->dev_addr))
8060 			return 0;
8061 
8062 		/* {broad,multi}cast packets to our BSSID go through */
8063 		if (is_multicast_ether_addr(header->addr1))
8064 			return ether_addr_equal(header->addr3, priv->bssid);
8065 
8066 		/* packets to our adapter go through */
8067 		return ether_addr_equal(header->addr1,
8068 					priv->net_dev->dev_addr);
8069 
8070 	case IW_MODE_INFRA:	/* Header: Dest. | BSSID | Source */
8071 		/* packets from our adapter are dropped (echo) */
8072 		if (ether_addr_equal(header->addr3, priv->net_dev->dev_addr))
8073 			return 0;
8074 
8075 		/* {broad,multi}cast packets to our BSS go through */
8076 		if (is_multicast_ether_addr(header->addr1))
8077 			return ether_addr_equal(header->addr2, priv->bssid);
8078 
8079 		/* packets to our adapter go through */
8080 		return ether_addr_equal(header->addr1,
8081 					priv->net_dev->dev_addr);
8082 	}
8083 
8084 	return 1;
8085 }
8086 
8087 #define IPW_PACKET_RETRY_TIME HZ
8088 
8089 static  int is_duplicate_packet(struct ipw_priv *priv,
8090 				      struct libipw_hdr_4addr *header)
8091 {
8092 	u16 sc = le16_to_cpu(header->seq_ctl);
8093 	u16 seq = WLAN_GET_SEQ_SEQ(sc);
8094 	u16 frag = WLAN_GET_SEQ_FRAG(sc);
8095 	u16 *last_seq, *last_frag;
8096 	unsigned long *last_time;
8097 
8098 	switch (priv->ieee->iw_mode) {
8099 	case IW_MODE_ADHOC:
8100 		{
8101 			struct list_head *p;
8102 			struct ipw_ibss_seq *entry = NULL;
8103 			u8 *mac = header->addr2;
8104 			int index = mac[5] % IPW_IBSS_MAC_HASH_SIZE;
8105 
8106 			list_for_each(p, &priv->ibss_mac_hash[index]) {
8107 				entry =
8108 				    list_entry(p, struct ipw_ibss_seq, list);
8109 				if (ether_addr_equal(entry->mac, mac))
8110 					break;
8111 			}
8112 			if (p == &priv->ibss_mac_hash[index]) {
8113 				entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
8114 				if (!entry) {
8115 					IPW_ERROR
8116 					    ("Cannot malloc new mac entry\n");
8117 					return 0;
8118 				}
8119 				memcpy(entry->mac, mac, ETH_ALEN);
8120 				entry->seq_num = seq;
8121 				entry->frag_num = frag;
8122 				entry->packet_time = jiffies;
8123 				list_add(&entry->list,
8124 					 &priv->ibss_mac_hash[index]);
8125 				return 0;
8126 			}
8127 			last_seq = &entry->seq_num;
8128 			last_frag = &entry->frag_num;
8129 			last_time = &entry->packet_time;
8130 			break;
8131 		}
8132 	case IW_MODE_INFRA:
8133 		last_seq = &priv->last_seq_num;
8134 		last_frag = &priv->last_frag_num;
8135 		last_time = &priv->last_packet_time;
8136 		break;
8137 	default:
8138 		return 0;
8139 	}
8140 	if ((*last_seq == seq) &&
8141 	    time_after(*last_time + IPW_PACKET_RETRY_TIME, jiffies)) {
8142 		if (*last_frag == frag)
8143 			goto drop;
8144 		if (*last_frag + 1 != frag)
8145 			/* out-of-order fragment */
8146 			goto drop;
8147 	} else
8148 		*last_seq = seq;
8149 
8150 	*last_frag = frag;
8151 	*last_time = jiffies;
8152 	return 0;
8153 
8154       drop:
8155 	/* Comment this line now since we observed the card receives
8156 	 * duplicate packets but the FCTL_RETRY bit is not set in the
8157 	 * IBSS mode with fragmentation enabled.
8158 	 BUG_ON(!(le16_to_cpu(header->frame_control) & IEEE80211_FCTL_RETRY)); */
8159 	return 1;
8160 }
8161 
8162 static void ipw_handle_mgmt_packet(struct ipw_priv *priv,
8163 				   struct ipw_rx_mem_buffer *rxb,
8164 				   struct libipw_rx_stats *stats)
8165 {
8166 	struct sk_buff *skb = rxb->skb;
8167 	struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)skb->data;
8168 	struct libipw_hdr_4addr *header = (struct libipw_hdr_4addr *)
8169 	    (skb->data + IPW_RX_FRAME_SIZE);
8170 
8171 	libipw_rx_mgt(priv->ieee, header, stats);
8172 
8173 	if (priv->ieee->iw_mode == IW_MODE_ADHOC &&
8174 	    ((WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)) ==
8175 	      IEEE80211_STYPE_PROBE_RESP) ||
8176 	     (WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)) ==
8177 	      IEEE80211_STYPE_BEACON))) {
8178 		if (ether_addr_equal(header->addr3, priv->bssid))
8179 			ipw_add_station(priv, header->addr2);
8180 	}
8181 
8182 	if (priv->config & CFG_NET_STATS) {
8183 		IPW_DEBUG_HC("sending stat packet\n");
8184 
8185 		/* Set the size of the skb to the size of the full
8186 		 * ipw header and 802.11 frame */
8187 		skb_put(skb, le16_to_cpu(pkt->u.frame.length) +
8188 			IPW_RX_FRAME_SIZE);
8189 
8190 		/* Advance past the ipw packet header to the 802.11 frame */
8191 		skb_pull(skb, IPW_RX_FRAME_SIZE);
8192 
8193 		/* Push the libipw_rx_stats before the 802.11 frame */
8194 		memcpy(skb_push(skb, sizeof(*stats)), stats, sizeof(*stats));
8195 
8196 		skb->dev = priv->ieee->dev;
8197 
8198 		/* Point raw at the libipw_stats */
8199 		skb_reset_mac_header(skb);
8200 
8201 		skb->pkt_type = PACKET_OTHERHOST;
8202 		skb->protocol = cpu_to_be16(ETH_P_80211_STATS);
8203 		memset(skb->cb, 0, sizeof(rxb->skb->cb));
8204 		netif_rx(skb);
8205 		rxb->skb = NULL;
8206 	}
8207 }
8208 
8209 /*
8210  * Main entry function for receiving a packet with 80211 headers.  This
8211  * should be called when ever the FW has notified us that there is a new
8212  * skb in the receive queue.
8213  */
8214 static void ipw_rx(struct ipw_priv *priv)
8215 {
8216 	struct ipw_rx_mem_buffer *rxb;
8217 	struct ipw_rx_packet *pkt;
8218 	struct libipw_hdr_4addr *header;
8219 	u32 r, i;
8220 	u8 network_packet;
8221 	u8 fill_rx = 0;
8222 
8223 	r = ipw_read32(priv, IPW_RX_READ_INDEX);
8224 	ipw_read32(priv, IPW_RX_WRITE_INDEX);
8225 	i = priv->rxq->read;
8226 
8227 	if (ipw_rx_queue_space (priv->rxq) > (RX_QUEUE_SIZE / 2))
8228 		fill_rx = 1;
8229 
8230 	while (i != r) {
8231 		rxb = priv->rxq->queue[i];
8232 		if (unlikely(rxb == NULL)) {
8233 			printk(KERN_CRIT "Queue not allocated!\n");
8234 			break;
8235 		}
8236 		priv->rxq->queue[i] = NULL;
8237 
8238 		dma_sync_single_for_cpu(&priv->pci_dev->dev, rxb->dma_addr,
8239 					IPW_RX_BUF_SIZE, DMA_FROM_DEVICE);
8240 
8241 		pkt = (struct ipw_rx_packet *)rxb->skb->data;
8242 		IPW_DEBUG_RX("Packet: type=%02X seq=%02X bits=%02X\n",
8243 			     pkt->header.message_type,
8244 			     pkt->header.rx_seq_num, pkt->header.control_bits);
8245 
8246 		switch (pkt->header.message_type) {
8247 		case RX_FRAME_TYPE:	/* 802.11 frame */  {
8248 				struct libipw_rx_stats stats = {
8249 					.rssi = pkt->u.frame.rssi_dbm -
8250 					    IPW_RSSI_TO_DBM,
8251 					.signal =
8252 					    pkt->u.frame.rssi_dbm -
8253 					    IPW_RSSI_TO_DBM + 0x100,
8254 					.noise =
8255 					    le16_to_cpu(pkt->u.frame.noise),
8256 					.rate = pkt->u.frame.rate,
8257 					.mac_time = jiffies,
8258 					.received_channel =
8259 					    pkt->u.frame.received_channel,
8260 					.freq =
8261 					    (pkt->u.frame.
8262 					     control & (1 << 0)) ?
8263 					    LIBIPW_24GHZ_BAND :
8264 					    LIBIPW_52GHZ_BAND,
8265 					.len = le16_to_cpu(pkt->u.frame.length),
8266 				};
8267 
8268 				if (stats.rssi != 0)
8269 					stats.mask |= LIBIPW_STATMASK_RSSI;
8270 				if (stats.signal != 0)
8271 					stats.mask |= LIBIPW_STATMASK_SIGNAL;
8272 				if (stats.noise != 0)
8273 					stats.mask |= LIBIPW_STATMASK_NOISE;
8274 				if (stats.rate != 0)
8275 					stats.mask |= LIBIPW_STATMASK_RATE;
8276 
8277 				priv->rx_packets++;
8278 
8279 #ifdef CONFIG_IPW2200_PROMISCUOUS
8280 	if (priv->prom_net_dev && netif_running(priv->prom_net_dev))
8281 		ipw_handle_promiscuous_rx(priv, rxb, &stats);
8282 #endif
8283 
8284 #ifdef CONFIG_IPW2200_MONITOR
8285 				if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
8286 #ifdef CONFIG_IPW2200_RADIOTAP
8287 
8288                 ipw_handle_data_packet_monitor(priv,
8289 					       rxb,
8290 					       &stats);
8291 #else
8292 		ipw_handle_data_packet(priv, rxb,
8293 				       &stats);
8294 #endif
8295 					break;
8296 				}
8297 #endif
8298 
8299 				header =
8300 				    (struct libipw_hdr_4addr *)(rxb->skb->
8301 								   data +
8302 								   IPW_RX_FRAME_SIZE);
8303 				/* TODO: Check Ad-Hoc dest/source and make sure
8304 				 * that we are actually parsing these packets
8305 				 * correctly -- we should probably use the
8306 				 * frame control of the packet and disregard
8307 				 * the current iw_mode */
8308 
8309 				network_packet =
8310 				    is_network_packet(priv, header);
8311 				if (network_packet && priv->assoc_network) {
8312 					priv->assoc_network->stats.rssi =
8313 					    stats.rssi;
8314 					priv->exp_avg_rssi =
8315 					    exponential_average(priv->exp_avg_rssi,
8316 					    stats.rssi, DEPTH_RSSI);
8317 				}
8318 
8319 				IPW_DEBUG_RX("Frame: len=%u\n",
8320 					     le16_to_cpu(pkt->u.frame.length));
8321 
8322 				if (le16_to_cpu(pkt->u.frame.length) <
8323 				    libipw_get_hdrlen(le16_to_cpu(
8324 						    header->frame_ctl))) {
8325 					IPW_DEBUG_DROP
8326 					    ("Received packet is too small. "
8327 					     "Dropping.\n");
8328 					priv->net_dev->stats.rx_errors++;
8329 					priv->wstats.discard.misc++;
8330 					break;
8331 				}
8332 
8333 				switch (WLAN_FC_GET_TYPE
8334 					(le16_to_cpu(header->frame_ctl))) {
8335 
8336 				case IEEE80211_FTYPE_MGMT:
8337 					ipw_handle_mgmt_packet(priv, rxb,
8338 							       &stats);
8339 					break;
8340 
8341 				case IEEE80211_FTYPE_CTL:
8342 					break;
8343 
8344 				case IEEE80211_FTYPE_DATA:
8345 					if (unlikely(!network_packet ||
8346 						     is_duplicate_packet(priv,
8347 									 header)))
8348 					{
8349 						IPW_DEBUG_DROP("Dropping: "
8350 							       "%pM, "
8351 							       "%pM, "
8352 							       "%pM\n",
8353 							       header->addr1,
8354 							       header->addr2,
8355 							       header->addr3);
8356 						break;
8357 					}
8358 
8359 					ipw_handle_data_packet(priv, rxb,
8360 							       &stats);
8361 
8362 					break;
8363 				}
8364 				break;
8365 			}
8366 
8367 		case RX_HOST_NOTIFICATION_TYPE:{
8368 				IPW_DEBUG_RX
8369 				    ("Notification: subtype=%02X flags=%02X size=%d\n",
8370 				     pkt->u.notification.subtype,
8371 				     pkt->u.notification.flags,
8372 				     le16_to_cpu(pkt->u.notification.size));
8373 				ipw_rx_notification(priv, &pkt->u.notification);
8374 				break;
8375 			}
8376 
8377 		default:
8378 			IPW_DEBUG_RX("Bad Rx packet of type %d\n",
8379 				     pkt->header.message_type);
8380 			break;
8381 		}
8382 
8383 		/* For now we just don't re-use anything.  We can tweak this
8384 		 * later to try and re-use notification packets and SKBs that
8385 		 * fail to Rx correctly */
8386 		if (rxb->skb != NULL) {
8387 			dev_kfree_skb_any(rxb->skb);
8388 			rxb->skb = NULL;
8389 		}
8390 
8391 		dma_unmap_single(&priv->pci_dev->dev, rxb->dma_addr,
8392 				 IPW_RX_BUF_SIZE, DMA_FROM_DEVICE);
8393 		list_add_tail(&rxb->list, &priv->rxq->rx_used);
8394 
8395 		i = (i + 1) % RX_QUEUE_SIZE;
8396 
8397 		/* If there are a lot of unsued frames, restock the Rx queue
8398 		 * so the ucode won't assert */
8399 		if (fill_rx) {
8400 			priv->rxq->read = i;
8401 			ipw_rx_queue_replenish(priv);
8402 		}
8403 	}
8404 
8405 	/* Backtrack one entry */
8406 	priv->rxq->read = i;
8407 	ipw_rx_queue_restock(priv);
8408 }
8409 
8410 #define DEFAULT_RTS_THRESHOLD     2304U
8411 #define MIN_RTS_THRESHOLD         1U
8412 #define MAX_RTS_THRESHOLD         2304U
8413 #define DEFAULT_BEACON_INTERVAL   100U
8414 #define	DEFAULT_SHORT_RETRY_LIMIT 7U
8415 #define	DEFAULT_LONG_RETRY_LIMIT  4U
8416 
8417 /*
8418  * ipw_sw_reset
8419  * @option: options to control different reset behaviour
8420  * 	    0 = reset everything except the 'disable' module_param
8421  * 	    1 = reset everything and print out driver info (for probe only)
8422  * 	    2 = reset everything
8423  */
8424 static int ipw_sw_reset(struct ipw_priv *priv, int option)
8425 {
8426 	int band, modulation;
8427 	int old_mode = priv->ieee->iw_mode;
8428 
8429 	/* Initialize module parameter values here */
8430 	priv->config = 0;
8431 
8432 	/* We default to disabling the LED code as right now it causes
8433 	 * too many systems to lock up... */
8434 	if (!led_support)
8435 		priv->config |= CFG_NO_LED;
8436 
8437 	if (associate)
8438 		priv->config |= CFG_ASSOCIATE;
8439 	else
8440 		IPW_DEBUG_INFO("Auto associate disabled.\n");
8441 
8442 	if (auto_create)
8443 		priv->config |= CFG_ADHOC_CREATE;
8444 	else
8445 		IPW_DEBUG_INFO("Auto adhoc creation disabled.\n");
8446 
8447 	priv->config &= ~CFG_STATIC_ESSID;
8448 	priv->essid_len = 0;
8449 	memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
8450 
8451 	if (disable && option) {
8452 		priv->status |= STATUS_RF_KILL_SW;
8453 		IPW_DEBUG_INFO("Radio disabled.\n");
8454 	}
8455 
8456 	if (default_channel != 0) {
8457 		priv->config |= CFG_STATIC_CHANNEL;
8458 		priv->channel = default_channel;
8459 		IPW_DEBUG_INFO("Bind to static channel %d\n", default_channel);
8460 		/* TODO: Validate that provided channel is in range */
8461 	}
8462 #ifdef CONFIG_IPW2200_QOS
8463 	ipw_qos_init(priv, qos_enable, qos_burst_enable,
8464 		     burst_duration_CCK, burst_duration_OFDM);
8465 #endif				/* CONFIG_IPW2200_QOS */
8466 
8467 	switch (network_mode) {
8468 	case 1:
8469 		priv->ieee->iw_mode = IW_MODE_ADHOC;
8470 		priv->net_dev->type = ARPHRD_ETHER;
8471 
8472 		break;
8473 #ifdef CONFIG_IPW2200_MONITOR
8474 	case 2:
8475 		priv->ieee->iw_mode = IW_MODE_MONITOR;
8476 #ifdef CONFIG_IPW2200_RADIOTAP
8477 		priv->net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
8478 #else
8479 		priv->net_dev->type = ARPHRD_IEEE80211;
8480 #endif
8481 		break;
8482 #endif
8483 	default:
8484 	case 0:
8485 		priv->net_dev->type = ARPHRD_ETHER;
8486 		priv->ieee->iw_mode = IW_MODE_INFRA;
8487 		break;
8488 	}
8489 
8490 	if (hwcrypto) {
8491 		priv->ieee->host_encrypt = 0;
8492 		priv->ieee->host_encrypt_msdu = 0;
8493 		priv->ieee->host_decrypt = 0;
8494 		priv->ieee->host_mc_decrypt = 0;
8495 	}
8496 	IPW_DEBUG_INFO("Hardware crypto [%s]\n", hwcrypto ? "on" : "off");
8497 
8498 	/* IPW2200/2915 is abled to do hardware fragmentation. */
8499 	priv->ieee->host_open_frag = 0;
8500 
8501 	if ((priv->pci_dev->device == 0x4223) ||
8502 	    (priv->pci_dev->device == 0x4224)) {
8503 		if (option == 1)
8504 			printk(KERN_INFO DRV_NAME
8505 			       ": Detected Intel PRO/Wireless 2915ABG Network "
8506 			       "Connection\n");
8507 		priv->ieee->abg_true = 1;
8508 		band = LIBIPW_52GHZ_BAND | LIBIPW_24GHZ_BAND;
8509 		modulation = LIBIPW_OFDM_MODULATION |
8510 		    LIBIPW_CCK_MODULATION;
8511 		priv->adapter = IPW_2915ABG;
8512 		priv->ieee->mode = IEEE_A | IEEE_G | IEEE_B;
8513 	} else {
8514 		if (option == 1)
8515 			printk(KERN_INFO DRV_NAME
8516 			       ": Detected Intel PRO/Wireless 2200BG Network "
8517 			       "Connection\n");
8518 
8519 		priv->ieee->abg_true = 0;
8520 		band = LIBIPW_24GHZ_BAND;
8521 		modulation = LIBIPW_OFDM_MODULATION |
8522 		    LIBIPW_CCK_MODULATION;
8523 		priv->adapter = IPW_2200BG;
8524 		priv->ieee->mode = IEEE_G | IEEE_B;
8525 	}
8526 
8527 	priv->ieee->freq_band = band;
8528 	priv->ieee->modulation = modulation;
8529 
8530 	priv->rates_mask = LIBIPW_DEFAULT_RATES_MASK;
8531 
8532 	priv->disassociate_threshold = IPW_MB_DISASSOCIATE_THRESHOLD_DEFAULT;
8533 	priv->roaming_threshold = IPW_MB_ROAMING_THRESHOLD_DEFAULT;
8534 
8535 	priv->rts_threshold = DEFAULT_RTS_THRESHOLD;
8536 	priv->short_retry_limit = DEFAULT_SHORT_RETRY_LIMIT;
8537 	priv->long_retry_limit = DEFAULT_LONG_RETRY_LIMIT;
8538 
8539 	/* If power management is turned on, default to AC mode */
8540 	priv->power_mode = IPW_POWER_AC;
8541 	priv->tx_power = IPW_TX_POWER_DEFAULT;
8542 
8543 	return old_mode == priv->ieee->iw_mode;
8544 }
8545 
8546 /*
8547  * This file defines the Wireless Extension handlers.  It does not
8548  * define any methods of hardware manipulation and relies on the
8549  * functions defined in ipw_main to provide the HW interaction.
8550  *
8551  * The exception to this is the use of the ipw_get_ordinal()
8552  * function used to poll the hardware vs. making unnecessary calls.
8553  *
8554  */
8555 
8556 static int ipw_set_channel(struct ipw_priv *priv, u8 channel)
8557 {
8558 	if (channel == 0) {
8559 		IPW_DEBUG_INFO("Setting channel to ANY (0)\n");
8560 		priv->config &= ~CFG_STATIC_CHANNEL;
8561 		IPW_DEBUG_ASSOC("Attempting to associate with new "
8562 				"parameters.\n");
8563 		ipw_associate(priv);
8564 		return 0;
8565 	}
8566 
8567 	priv->config |= CFG_STATIC_CHANNEL;
8568 
8569 	if (priv->channel == channel) {
8570 		IPW_DEBUG_INFO("Request to set channel to current value (%d)\n",
8571 			       channel);
8572 		return 0;
8573 	}
8574 
8575 	IPW_DEBUG_INFO("Setting channel to %i\n", (int)channel);
8576 	priv->channel = channel;
8577 
8578 #ifdef CONFIG_IPW2200_MONITOR
8579 	if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
8580 		int i;
8581 		if (priv->status & STATUS_SCANNING) {
8582 			IPW_DEBUG_SCAN("Scan abort triggered due to "
8583 				       "channel change.\n");
8584 			ipw_abort_scan(priv);
8585 		}
8586 
8587 		for (i = 1000; i && (priv->status & STATUS_SCANNING); i--)
8588 			udelay(10);
8589 
8590 		if (priv->status & STATUS_SCANNING)
8591 			IPW_DEBUG_SCAN("Still scanning...\n");
8592 		else
8593 			IPW_DEBUG_SCAN("Took %dms to abort current scan\n",
8594 				       1000 - i);
8595 
8596 		return 0;
8597 	}
8598 #endif				/* CONFIG_IPW2200_MONITOR */
8599 
8600 	/* Network configuration changed -- force [re]association */
8601 	IPW_DEBUG_ASSOC("[re]association triggered due to channel change.\n");
8602 	if (!ipw_disassociate(priv))
8603 		ipw_associate(priv);
8604 
8605 	return 0;
8606 }
8607 
8608 static int ipw_wx_set_freq(struct net_device *dev,
8609 			   struct iw_request_info *info,
8610 			   union iwreq_data *wrqu, char *extra)
8611 {
8612 	struct ipw_priv *priv = libipw_priv(dev);
8613 	const struct libipw_geo *geo = libipw_get_geo(priv->ieee);
8614 	struct iw_freq *fwrq = &wrqu->freq;
8615 	int ret = 0, i;
8616 	u8 channel, flags;
8617 	int band;
8618 
8619 	if (fwrq->m == 0) {
8620 		IPW_DEBUG_WX("SET Freq/Channel -> any\n");
8621 		mutex_lock(&priv->mutex);
8622 		ret = ipw_set_channel(priv, 0);
8623 		mutex_unlock(&priv->mutex);
8624 		return ret;
8625 	}
8626 	/* if setting by freq convert to channel */
8627 	if (fwrq->e == 1) {
8628 		channel = libipw_freq_to_channel(priv->ieee, fwrq->m);
8629 		if (channel == 0)
8630 			return -EINVAL;
8631 	} else
8632 		channel = fwrq->m;
8633 
8634 	if (!(band = libipw_is_valid_channel(priv->ieee, channel)))
8635 		return -EINVAL;
8636 
8637 	if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
8638 		i = libipw_channel_to_index(priv->ieee, channel);
8639 		if (i == -1)
8640 			return -EINVAL;
8641 
8642 		flags = (band == LIBIPW_24GHZ_BAND) ?
8643 		    geo->bg[i].flags : geo->a[i].flags;
8644 		if (flags & LIBIPW_CH_PASSIVE_ONLY) {
8645 			IPW_DEBUG_WX("Invalid Ad-Hoc channel for 802.11a\n");
8646 			return -EINVAL;
8647 		}
8648 	}
8649 
8650 	IPW_DEBUG_WX("SET Freq/Channel -> %d\n", fwrq->m);
8651 	mutex_lock(&priv->mutex);
8652 	ret = ipw_set_channel(priv, channel);
8653 	mutex_unlock(&priv->mutex);
8654 	return ret;
8655 }
8656 
8657 static int ipw_wx_get_freq(struct net_device *dev,
8658 			   struct iw_request_info *info,
8659 			   union iwreq_data *wrqu, char *extra)
8660 {
8661 	struct ipw_priv *priv = libipw_priv(dev);
8662 
8663 	wrqu->freq.e = 0;
8664 
8665 	/* If we are associated, trying to associate, or have a statically
8666 	 * configured CHANNEL then return that; otherwise return ANY */
8667 	mutex_lock(&priv->mutex);
8668 	if (priv->config & CFG_STATIC_CHANNEL ||
8669 	    priv->status & (STATUS_ASSOCIATING | STATUS_ASSOCIATED)) {
8670 		int i;
8671 
8672 		i = libipw_channel_to_index(priv->ieee, priv->channel);
8673 		BUG_ON(i == -1);
8674 		wrqu->freq.e = 1;
8675 
8676 		switch (libipw_is_valid_channel(priv->ieee, priv->channel)) {
8677 		case LIBIPW_52GHZ_BAND:
8678 			wrqu->freq.m = priv->ieee->geo.a[i].freq * 100000;
8679 			break;
8680 
8681 		case LIBIPW_24GHZ_BAND:
8682 			wrqu->freq.m = priv->ieee->geo.bg[i].freq * 100000;
8683 			break;
8684 
8685 		default:
8686 			BUG();
8687 		}
8688 	} else
8689 		wrqu->freq.m = 0;
8690 
8691 	mutex_unlock(&priv->mutex);
8692 	IPW_DEBUG_WX("GET Freq/Channel -> %d\n", priv->channel);
8693 	return 0;
8694 }
8695 
8696 static int ipw_wx_set_mode(struct net_device *dev,
8697 			   struct iw_request_info *info,
8698 			   union iwreq_data *wrqu, char *extra)
8699 {
8700 	struct ipw_priv *priv = libipw_priv(dev);
8701 	int err = 0;
8702 
8703 	IPW_DEBUG_WX("Set MODE: %d\n", wrqu->mode);
8704 
8705 	switch (wrqu->mode) {
8706 #ifdef CONFIG_IPW2200_MONITOR
8707 	case IW_MODE_MONITOR:
8708 #endif
8709 	case IW_MODE_ADHOC:
8710 	case IW_MODE_INFRA:
8711 		break;
8712 	case IW_MODE_AUTO:
8713 		wrqu->mode = IW_MODE_INFRA;
8714 		break;
8715 	default:
8716 		return -EINVAL;
8717 	}
8718 	if (wrqu->mode == priv->ieee->iw_mode)
8719 		return 0;
8720 
8721 	mutex_lock(&priv->mutex);
8722 
8723 	ipw_sw_reset(priv, 0);
8724 
8725 #ifdef CONFIG_IPW2200_MONITOR
8726 	if (priv->ieee->iw_mode == IW_MODE_MONITOR)
8727 		priv->net_dev->type = ARPHRD_ETHER;
8728 
8729 	if (wrqu->mode == IW_MODE_MONITOR)
8730 #ifdef CONFIG_IPW2200_RADIOTAP
8731 		priv->net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
8732 #else
8733 		priv->net_dev->type = ARPHRD_IEEE80211;
8734 #endif
8735 #endif				/* CONFIG_IPW2200_MONITOR */
8736 
8737 	/* Free the existing firmware and reset the fw_loaded
8738 	 * flag so ipw_load() will bring in the new firmware */
8739 	free_firmware();
8740 
8741 	priv->ieee->iw_mode = wrqu->mode;
8742 
8743 	schedule_work(&priv->adapter_restart);
8744 	mutex_unlock(&priv->mutex);
8745 	return err;
8746 }
8747 
8748 static int ipw_wx_get_mode(struct net_device *dev,
8749 			   struct iw_request_info *info,
8750 			   union iwreq_data *wrqu, char *extra)
8751 {
8752 	struct ipw_priv *priv = libipw_priv(dev);
8753 	mutex_lock(&priv->mutex);
8754 	wrqu->mode = priv->ieee->iw_mode;
8755 	IPW_DEBUG_WX("Get MODE -> %d\n", wrqu->mode);
8756 	mutex_unlock(&priv->mutex);
8757 	return 0;
8758 }
8759 
8760 /* Values are in microsecond */
8761 static const s32 timeout_duration[] = {
8762 	350000,
8763 	250000,
8764 	75000,
8765 	37000,
8766 	25000,
8767 };
8768 
8769 static const s32 period_duration[] = {
8770 	400000,
8771 	700000,
8772 	1000000,
8773 	1000000,
8774 	1000000
8775 };
8776 
8777 static int ipw_wx_get_range(struct net_device *dev,
8778 			    struct iw_request_info *info,
8779 			    union iwreq_data *wrqu, char *extra)
8780 {
8781 	struct ipw_priv *priv = libipw_priv(dev);
8782 	struct iw_range *range = (struct iw_range *)extra;
8783 	const struct libipw_geo *geo = libipw_get_geo(priv->ieee);
8784 	int i = 0, j;
8785 
8786 	wrqu->data.length = sizeof(*range);
8787 	memset(range, 0, sizeof(*range));
8788 
8789 	/* 54Mbs == ~27 Mb/s real (802.11g) */
8790 	range->throughput = 27 * 1000 * 1000;
8791 
8792 	range->max_qual.qual = 100;
8793 	/* TODO: Find real max RSSI and stick here */
8794 	range->max_qual.level = 0;
8795 	range->max_qual.noise = 0;
8796 	range->max_qual.updated = 7;	/* Updated all three */
8797 
8798 	range->avg_qual.qual = 70;
8799 	/* TODO: Find real 'good' to 'bad' threshold value for RSSI */
8800 	range->avg_qual.level = 0;	/* FIXME to real average level */
8801 	range->avg_qual.noise = 0;
8802 	range->avg_qual.updated = 7;	/* Updated all three */
8803 	mutex_lock(&priv->mutex);
8804 	range->num_bitrates = min(priv->rates.num_rates, (u8) IW_MAX_BITRATES);
8805 
8806 	for (i = 0; i < range->num_bitrates; i++)
8807 		range->bitrate[i] = (priv->rates.supported_rates[i] & 0x7F) *
8808 		    500000;
8809 
8810 	range->max_rts = DEFAULT_RTS_THRESHOLD;
8811 	range->min_frag = MIN_FRAG_THRESHOLD;
8812 	range->max_frag = MAX_FRAG_THRESHOLD;
8813 
8814 	range->encoding_size[0] = 5;
8815 	range->encoding_size[1] = 13;
8816 	range->num_encoding_sizes = 2;
8817 	range->max_encoding_tokens = WEP_KEYS;
8818 
8819 	/* Set the Wireless Extension versions */
8820 	range->we_version_compiled = WIRELESS_EXT;
8821 	range->we_version_source = 18;
8822 
8823 	i = 0;
8824 	if (priv->ieee->mode & (IEEE_B | IEEE_G)) {
8825 		for (j = 0; j < geo->bg_channels && i < IW_MAX_FREQUENCIES; j++) {
8826 			if ((priv->ieee->iw_mode == IW_MODE_ADHOC) &&
8827 			    (geo->bg[j].flags & LIBIPW_CH_PASSIVE_ONLY))
8828 				continue;
8829 
8830 			range->freq[i].i = geo->bg[j].channel;
8831 			range->freq[i].m = geo->bg[j].freq * 100000;
8832 			range->freq[i].e = 1;
8833 			i++;
8834 		}
8835 	}
8836 
8837 	if (priv->ieee->mode & IEEE_A) {
8838 		for (j = 0; j < geo->a_channels && i < IW_MAX_FREQUENCIES; j++) {
8839 			if ((priv->ieee->iw_mode == IW_MODE_ADHOC) &&
8840 			    (geo->a[j].flags & LIBIPW_CH_PASSIVE_ONLY))
8841 				continue;
8842 
8843 			range->freq[i].i = geo->a[j].channel;
8844 			range->freq[i].m = geo->a[j].freq * 100000;
8845 			range->freq[i].e = 1;
8846 			i++;
8847 		}
8848 	}
8849 
8850 	range->num_channels = i;
8851 	range->num_frequency = i;
8852 
8853 	mutex_unlock(&priv->mutex);
8854 
8855 	/* Event capability (kernel + driver) */
8856 	range->event_capa[0] = (IW_EVENT_CAPA_K_0 |
8857 				IW_EVENT_CAPA_MASK(SIOCGIWTHRSPY) |
8858 				IW_EVENT_CAPA_MASK(SIOCGIWAP) |
8859 				IW_EVENT_CAPA_MASK(SIOCGIWSCAN));
8860 	range->event_capa[1] = IW_EVENT_CAPA_K_1;
8861 
8862 	range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
8863 		IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
8864 
8865 	range->scan_capa = IW_SCAN_CAPA_ESSID | IW_SCAN_CAPA_TYPE;
8866 
8867 	IPW_DEBUG_WX("GET Range\n");
8868 	return 0;
8869 }
8870 
8871 static int ipw_wx_set_wap(struct net_device *dev,
8872 			  struct iw_request_info *info,
8873 			  union iwreq_data *wrqu, char *extra)
8874 {
8875 	struct ipw_priv *priv = libipw_priv(dev);
8876 
8877 	if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
8878 		return -EINVAL;
8879 	mutex_lock(&priv->mutex);
8880 	if (is_broadcast_ether_addr(wrqu->ap_addr.sa_data) ||
8881 	    is_zero_ether_addr(wrqu->ap_addr.sa_data)) {
8882 		/* we disable mandatory BSSID association */
8883 		IPW_DEBUG_WX("Setting AP BSSID to ANY\n");
8884 		priv->config &= ~CFG_STATIC_BSSID;
8885 		IPW_DEBUG_ASSOC("Attempting to associate with new "
8886 				"parameters.\n");
8887 		ipw_associate(priv);
8888 		mutex_unlock(&priv->mutex);
8889 		return 0;
8890 	}
8891 
8892 	priv->config |= CFG_STATIC_BSSID;
8893 	if (ether_addr_equal(priv->bssid, wrqu->ap_addr.sa_data)) {
8894 		IPW_DEBUG_WX("BSSID set to current BSSID.\n");
8895 		mutex_unlock(&priv->mutex);
8896 		return 0;
8897 	}
8898 
8899 	IPW_DEBUG_WX("Setting mandatory BSSID to %pM\n",
8900 		     wrqu->ap_addr.sa_data);
8901 
8902 	memcpy(priv->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
8903 
8904 	/* Network configuration changed -- force [re]association */
8905 	IPW_DEBUG_ASSOC("[re]association triggered due to BSSID change.\n");
8906 	if (!ipw_disassociate(priv))
8907 		ipw_associate(priv);
8908 
8909 	mutex_unlock(&priv->mutex);
8910 	return 0;
8911 }
8912 
8913 static int ipw_wx_get_wap(struct net_device *dev,
8914 			  struct iw_request_info *info,
8915 			  union iwreq_data *wrqu, char *extra)
8916 {
8917 	struct ipw_priv *priv = libipw_priv(dev);
8918 
8919 	/* If we are associated, trying to associate, or have a statically
8920 	 * configured BSSID then return that; otherwise return ANY */
8921 	mutex_lock(&priv->mutex);
8922 	if (priv->config & CFG_STATIC_BSSID ||
8923 	    priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
8924 		wrqu->ap_addr.sa_family = ARPHRD_ETHER;
8925 		memcpy(wrqu->ap_addr.sa_data, priv->bssid, ETH_ALEN);
8926 	} else
8927 		eth_zero_addr(wrqu->ap_addr.sa_data);
8928 
8929 	IPW_DEBUG_WX("Getting WAP BSSID: %pM\n",
8930 		     wrqu->ap_addr.sa_data);
8931 	mutex_unlock(&priv->mutex);
8932 	return 0;
8933 }
8934 
8935 static int ipw_wx_set_essid(struct net_device *dev,
8936 			    struct iw_request_info *info,
8937 			    union iwreq_data *wrqu, char *extra)
8938 {
8939 	struct ipw_priv *priv = libipw_priv(dev);
8940         int length;
8941 
8942         mutex_lock(&priv->mutex);
8943 
8944         if (!wrqu->essid.flags)
8945         {
8946                 IPW_DEBUG_WX("Setting ESSID to ANY\n");
8947                 ipw_disassociate(priv);
8948                 priv->config &= ~CFG_STATIC_ESSID;
8949                 ipw_associate(priv);
8950                 mutex_unlock(&priv->mutex);
8951                 return 0;
8952         }
8953 
8954 	length = min((int)wrqu->essid.length, IW_ESSID_MAX_SIZE);
8955 
8956 	priv->config |= CFG_STATIC_ESSID;
8957 
8958 	if (priv->essid_len == length && !memcmp(priv->essid, extra, length)
8959 	    && (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING))) {
8960 		IPW_DEBUG_WX("ESSID set to current ESSID.\n");
8961 		mutex_unlock(&priv->mutex);
8962 		return 0;
8963 	}
8964 
8965 	IPW_DEBUG_WX("Setting ESSID: '%*pE' (%d)\n", length, extra, length);
8966 
8967 	priv->essid_len = length;
8968 	memcpy(priv->essid, extra, priv->essid_len);
8969 
8970 	/* Network configuration changed -- force [re]association */
8971 	IPW_DEBUG_ASSOC("[re]association triggered due to ESSID change.\n");
8972 	if (!ipw_disassociate(priv))
8973 		ipw_associate(priv);
8974 
8975 	mutex_unlock(&priv->mutex);
8976 	return 0;
8977 }
8978 
8979 static int ipw_wx_get_essid(struct net_device *dev,
8980 			    struct iw_request_info *info,
8981 			    union iwreq_data *wrqu, char *extra)
8982 {
8983 	struct ipw_priv *priv = libipw_priv(dev);
8984 
8985 	/* If we are associated, trying to associate, or have a statically
8986 	 * configured ESSID then return that; otherwise return ANY */
8987 	mutex_lock(&priv->mutex);
8988 	if (priv->config & CFG_STATIC_ESSID ||
8989 	    priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
8990 		IPW_DEBUG_WX("Getting essid: '%*pE'\n",
8991 			     priv->essid_len, priv->essid);
8992 		memcpy(extra, priv->essid, priv->essid_len);
8993 		wrqu->essid.length = priv->essid_len;
8994 		wrqu->essid.flags = 1;	/* active */
8995 	} else {
8996 		IPW_DEBUG_WX("Getting essid: ANY\n");
8997 		wrqu->essid.length = 0;
8998 		wrqu->essid.flags = 0;	/* active */
8999 	}
9000 	mutex_unlock(&priv->mutex);
9001 	return 0;
9002 }
9003 
9004 static int ipw_wx_set_nick(struct net_device *dev,
9005 			   struct iw_request_info *info,
9006 			   union iwreq_data *wrqu, char *extra)
9007 {
9008 	struct ipw_priv *priv = libipw_priv(dev);
9009 
9010 	IPW_DEBUG_WX("Setting nick to '%s'\n", extra);
9011 	if (wrqu->data.length > IW_ESSID_MAX_SIZE)
9012 		return -E2BIG;
9013 	mutex_lock(&priv->mutex);
9014 	wrqu->data.length = min_t(size_t, wrqu->data.length, sizeof(priv->nick));
9015 	memset(priv->nick, 0, sizeof(priv->nick));
9016 	memcpy(priv->nick, extra, wrqu->data.length);
9017 	IPW_DEBUG_TRACE("<<\n");
9018 	mutex_unlock(&priv->mutex);
9019 	return 0;
9020 
9021 }
9022 
9023 static int ipw_wx_get_nick(struct net_device *dev,
9024 			   struct iw_request_info *info,
9025 			   union iwreq_data *wrqu, char *extra)
9026 {
9027 	struct ipw_priv *priv = libipw_priv(dev);
9028 	IPW_DEBUG_WX("Getting nick\n");
9029 	mutex_lock(&priv->mutex);
9030 	wrqu->data.length = strlen(priv->nick);
9031 	memcpy(extra, priv->nick, wrqu->data.length);
9032 	wrqu->data.flags = 1;	/* active */
9033 	mutex_unlock(&priv->mutex);
9034 	return 0;
9035 }
9036 
9037 static int ipw_wx_set_sens(struct net_device *dev,
9038 			    struct iw_request_info *info,
9039 			    union iwreq_data *wrqu, char *extra)
9040 {
9041 	struct ipw_priv *priv = libipw_priv(dev);
9042 	int err = 0;
9043 
9044 	IPW_DEBUG_WX("Setting roaming threshold to %d\n", wrqu->sens.value);
9045 	IPW_DEBUG_WX("Setting disassociate threshold to %d\n", 3*wrqu->sens.value);
9046 	mutex_lock(&priv->mutex);
9047 
9048 	if (wrqu->sens.fixed == 0)
9049 	{
9050 		priv->roaming_threshold = IPW_MB_ROAMING_THRESHOLD_DEFAULT;
9051 		priv->disassociate_threshold = IPW_MB_DISASSOCIATE_THRESHOLD_DEFAULT;
9052 		goto out;
9053 	}
9054 	if ((wrqu->sens.value > IPW_MB_ROAMING_THRESHOLD_MAX) ||
9055 	    (wrqu->sens.value < IPW_MB_ROAMING_THRESHOLD_MIN)) {
9056 		err = -EINVAL;
9057 		goto out;
9058 	}
9059 
9060 	priv->roaming_threshold = wrqu->sens.value;
9061 	priv->disassociate_threshold = 3*wrqu->sens.value;
9062       out:
9063 	mutex_unlock(&priv->mutex);
9064 	return err;
9065 }
9066 
9067 static int ipw_wx_get_sens(struct net_device *dev,
9068 			    struct iw_request_info *info,
9069 			    union iwreq_data *wrqu, char *extra)
9070 {
9071 	struct ipw_priv *priv = libipw_priv(dev);
9072 	mutex_lock(&priv->mutex);
9073 	wrqu->sens.fixed = 1;
9074 	wrqu->sens.value = priv->roaming_threshold;
9075 	mutex_unlock(&priv->mutex);
9076 
9077 	IPW_DEBUG_WX("GET roaming threshold -> %s %d\n",
9078 		     wrqu->power.disabled ? "OFF" : "ON", wrqu->power.value);
9079 
9080 	return 0;
9081 }
9082 
9083 static int ipw_wx_set_rate(struct net_device *dev,
9084 			   struct iw_request_info *info,
9085 			   union iwreq_data *wrqu, char *extra)
9086 {
9087 	/* TODO: We should use semaphores or locks for access to priv */
9088 	struct ipw_priv *priv = libipw_priv(dev);
9089 	u32 target_rate = wrqu->bitrate.value;
9090 	u32 fixed, mask;
9091 
9092 	/* value = -1, fixed = 0 means auto only, so we should use all rates offered by AP */
9093 	/* value = X, fixed = 1 means only rate X */
9094 	/* value = X, fixed = 0 means all rates lower equal X */
9095 
9096 	if (target_rate == -1) {
9097 		fixed = 0;
9098 		mask = LIBIPW_DEFAULT_RATES_MASK;
9099 		/* Now we should reassociate */
9100 		goto apply;
9101 	}
9102 
9103 	mask = 0;
9104 	fixed = wrqu->bitrate.fixed;
9105 
9106 	if (target_rate == 1000000 || !fixed)
9107 		mask |= LIBIPW_CCK_RATE_1MB_MASK;
9108 	if (target_rate == 1000000)
9109 		goto apply;
9110 
9111 	if (target_rate == 2000000 || !fixed)
9112 		mask |= LIBIPW_CCK_RATE_2MB_MASK;
9113 	if (target_rate == 2000000)
9114 		goto apply;
9115 
9116 	if (target_rate == 5500000 || !fixed)
9117 		mask |= LIBIPW_CCK_RATE_5MB_MASK;
9118 	if (target_rate == 5500000)
9119 		goto apply;
9120 
9121 	if (target_rate == 6000000 || !fixed)
9122 		mask |= LIBIPW_OFDM_RATE_6MB_MASK;
9123 	if (target_rate == 6000000)
9124 		goto apply;
9125 
9126 	if (target_rate == 9000000 || !fixed)
9127 		mask |= LIBIPW_OFDM_RATE_9MB_MASK;
9128 	if (target_rate == 9000000)
9129 		goto apply;
9130 
9131 	if (target_rate == 11000000 || !fixed)
9132 		mask |= LIBIPW_CCK_RATE_11MB_MASK;
9133 	if (target_rate == 11000000)
9134 		goto apply;
9135 
9136 	if (target_rate == 12000000 || !fixed)
9137 		mask |= LIBIPW_OFDM_RATE_12MB_MASK;
9138 	if (target_rate == 12000000)
9139 		goto apply;
9140 
9141 	if (target_rate == 18000000 || !fixed)
9142 		mask |= LIBIPW_OFDM_RATE_18MB_MASK;
9143 	if (target_rate == 18000000)
9144 		goto apply;
9145 
9146 	if (target_rate == 24000000 || !fixed)
9147 		mask |= LIBIPW_OFDM_RATE_24MB_MASK;
9148 	if (target_rate == 24000000)
9149 		goto apply;
9150 
9151 	if (target_rate == 36000000 || !fixed)
9152 		mask |= LIBIPW_OFDM_RATE_36MB_MASK;
9153 	if (target_rate == 36000000)
9154 		goto apply;
9155 
9156 	if (target_rate == 48000000 || !fixed)
9157 		mask |= LIBIPW_OFDM_RATE_48MB_MASK;
9158 	if (target_rate == 48000000)
9159 		goto apply;
9160 
9161 	if (target_rate == 54000000 || !fixed)
9162 		mask |= LIBIPW_OFDM_RATE_54MB_MASK;
9163 	if (target_rate == 54000000)
9164 		goto apply;
9165 
9166 	IPW_DEBUG_WX("invalid rate specified, returning error\n");
9167 	return -EINVAL;
9168 
9169       apply:
9170 	IPW_DEBUG_WX("Setting rate mask to 0x%08X [%s]\n",
9171 		     mask, fixed ? "fixed" : "sub-rates");
9172 	mutex_lock(&priv->mutex);
9173 	if (mask == LIBIPW_DEFAULT_RATES_MASK) {
9174 		priv->config &= ~CFG_FIXED_RATE;
9175 		ipw_set_fixed_rate(priv, priv->ieee->mode);
9176 	} else
9177 		priv->config |= CFG_FIXED_RATE;
9178 
9179 	if (priv->rates_mask == mask) {
9180 		IPW_DEBUG_WX("Mask set to current mask.\n");
9181 		mutex_unlock(&priv->mutex);
9182 		return 0;
9183 	}
9184 
9185 	priv->rates_mask = mask;
9186 
9187 	/* Network configuration changed -- force [re]association */
9188 	IPW_DEBUG_ASSOC("[re]association triggered due to rates change.\n");
9189 	if (!ipw_disassociate(priv))
9190 		ipw_associate(priv);
9191 
9192 	mutex_unlock(&priv->mutex);
9193 	return 0;
9194 }
9195 
9196 static int ipw_wx_get_rate(struct net_device *dev,
9197 			   struct iw_request_info *info,
9198 			   union iwreq_data *wrqu, char *extra)
9199 {
9200 	struct ipw_priv *priv = libipw_priv(dev);
9201 	mutex_lock(&priv->mutex);
9202 	wrqu->bitrate.value = priv->last_rate;
9203 	wrqu->bitrate.fixed = (priv->config & CFG_FIXED_RATE) ? 1 : 0;
9204 	mutex_unlock(&priv->mutex);
9205 	IPW_DEBUG_WX("GET Rate -> %d\n", wrqu->bitrate.value);
9206 	return 0;
9207 }
9208 
9209 static int ipw_wx_set_rts(struct net_device *dev,
9210 			  struct iw_request_info *info,
9211 			  union iwreq_data *wrqu, char *extra)
9212 {
9213 	struct ipw_priv *priv = libipw_priv(dev);
9214 	mutex_lock(&priv->mutex);
9215 	if (wrqu->rts.disabled || !wrqu->rts.fixed)
9216 		priv->rts_threshold = DEFAULT_RTS_THRESHOLD;
9217 	else {
9218 		if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
9219 		    wrqu->rts.value > MAX_RTS_THRESHOLD) {
9220 			mutex_unlock(&priv->mutex);
9221 			return -EINVAL;
9222 		}
9223 		priv->rts_threshold = wrqu->rts.value;
9224 	}
9225 
9226 	ipw_send_rts_threshold(priv, priv->rts_threshold);
9227 	mutex_unlock(&priv->mutex);
9228 	IPW_DEBUG_WX("SET RTS Threshold -> %d\n", priv->rts_threshold);
9229 	return 0;
9230 }
9231 
9232 static int ipw_wx_get_rts(struct net_device *dev,
9233 			  struct iw_request_info *info,
9234 			  union iwreq_data *wrqu, char *extra)
9235 {
9236 	struct ipw_priv *priv = libipw_priv(dev);
9237 	mutex_lock(&priv->mutex);
9238 	wrqu->rts.value = priv->rts_threshold;
9239 	wrqu->rts.fixed = 0;	/* no auto select */
9240 	wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
9241 	mutex_unlock(&priv->mutex);
9242 	IPW_DEBUG_WX("GET RTS Threshold -> %d\n", wrqu->rts.value);
9243 	return 0;
9244 }
9245 
9246 static int ipw_wx_set_txpow(struct net_device *dev,
9247 			    struct iw_request_info *info,
9248 			    union iwreq_data *wrqu, char *extra)
9249 {
9250 	struct ipw_priv *priv = libipw_priv(dev);
9251 	int err = 0;
9252 
9253 	mutex_lock(&priv->mutex);
9254 	if (ipw_radio_kill_sw(priv, wrqu->power.disabled)) {
9255 		err = -EINPROGRESS;
9256 		goto out;
9257 	}
9258 
9259 	if (!wrqu->power.fixed)
9260 		wrqu->power.value = IPW_TX_POWER_DEFAULT;
9261 
9262 	if (wrqu->power.flags != IW_TXPOW_DBM) {
9263 		err = -EINVAL;
9264 		goto out;
9265 	}
9266 
9267 	if ((wrqu->power.value > IPW_TX_POWER_MAX) ||
9268 	    (wrqu->power.value < IPW_TX_POWER_MIN)) {
9269 		err = -EINVAL;
9270 		goto out;
9271 	}
9272 
9273 	priv->tx_power = wrqu->power.value;
9274 	err = ipw_set_tx_power(priv);
9275       out:
9276 	mutex_unlock(&priv->mutex);
9277 	return err;
9278 }
9279 
9280 static int ipw_wx_get_txpow(struct net_device *dev,
9281 			    struct iw_request_info *info,
9282 			    union iwreq_data *wrqu, char *extra)
9283 {
9284 	struct ipw_priv *priv = libipw_priv(dev);
9285 	mutex_lock(&priv->mutex);
9286 	wrqu->power.value = priv->tx_power;
9287 	wrqu->power.fixed = 1;
9288 	wrqu->power.flags = IW_TXPOW_DBM;
9289 	wrqu->power.disabled = (priv->status & STATUS_RF_KILL_MASK) ? 1 : 0;
9290 	mutex_unlock(&priv->mutex);
9291 
9292 	IPW_DEBUG_WX("GET TX Power -> %s %d\n",
9293 		     wrqu->power.disabled ? "OFF" : "ON", wrqu->power.value);
9294 
9295 	return 0;
9296 }
9297 
9298 static int ipw_wx_set_frag(struct net_device *dev,
9299 			   struct iw_request_info *info,
9300 			   union iwreq_data *wrqu, char *extra)
9301 {
9302 	struct ipw_priv *priv = libipw_priv(dev);
9303 	mutex_lock(&priv->mutex);
9304 	if (wrqu->frag.disabled || !wrqu->frag.fixed)
9305 		priv->ieee->fts = DEFAULT_FTS;
9306 	else {
9307 		if (wrqu->frag.value < MIN_FRAG_THRESHOLD ||
9308 		    wrqu->frag.value > MAX_FRAG_THRESHOLD) {
9309 			mutex_unlock(&priv->mutex);
9310 			return -EINVAL;
9311 		}
9312 
9313 		priv->ieee->fts = wrqu->frag.value & ~0x1;
9314 	}
9315 
9316 	ipw_send_frag_threshold(priv, wrqu->frag.value);
9317 	mutex_unlock(&priv->mutex);
9318 	IPW_DEBUG_WX("SET Frag Threshold -> %d\n", wrqu->frag.value);
9319 	return 0;
9320 }
9321 
9322 static int ipw_wx_get_frag(struct net_device *dev,
9323 			   struct iw_request_info *info,
9324 			   union iwreq_data *wrqu, char *extra)
9325 {
9326 	struct ipw_priv *priv = libipw_priv(dev);
9327 	mutex_lock(&priv->mutex);
9328 	wrqu->frag.value = priv->ieee->fts;
9329 	wrqu->frag.fixed = 0;	/* no auto select */
9330 	wrqu->frag.disabled = (wrqu->frag.value == DEFAULT_FTS);
9331 	mutex_unlock(&priv->mutex);
9332 	IPW_DEBUG_WX("GET Frag Threshold -> %d\n", wrqu->frag.value);
9333 
9334 	return 0;
9335 }
9336 
9337 static int ipw_wx_set_retry(struct net_device *dev,
9338 			    struct iw_request_info *info,
9339 			    union iwreq_data *wrqu, char *extra)
9340 {
9341 	struct ipw_priv *priv = libipw_priv(dev);
9342 
9343 	if (wrqu->retry.flags & IW_RETRY_LIFETIME || wrqu->retry.disabled)
9344 		return -EINVAL;
9345 
9346 	if (!(wrqu->retry.flags & IW_RETRY_LIMIT))
9347 		return 0;
9348 
9349 	if (wrqu->retry.value < 0 || wrqu->retry.value >= 255)
9350 		return -EINVAL;
9351 
9352 	mutex_lock(&priv->mutex);
9353 	if (wrqu->retry.flags & IW_RETRY_SHORT)
9354 		priv->short_retry_limit = (u8) wrqu->retry.value;
9355 	else if (wrqu->retry.flags & IW_RETRY_LONG)
9356 		priv->long_retry_limit = (u8) wrqu->retry.value;
9357 	else {
9358 		priv->short_retry_limit = (u8) wrqu->retry.value;
9359 		priv->long_retry_limit = (u8) wrqu->retry.value;
9360 	}
9361 
9362 	ipw_send_retry_limit(priv, priv->short_retry_limit,
9363 			     priv->long_retry_limit);
9364 	mutex_unlock(&priv->mutex);
9365 	IPW_DEBUG_WX("SET retry limit -> short:%d long:%d\n",
9366 		     priv->short_retry_limit, priv->long_retry_limit);
9367 	return 0;
9368 }
9369 
9370 static int ipw_wx_get_retry(struct net_device *dev,
9371 			    struct iw_request_info *info,
9372 			    union iwreq_data *wrqu, char *extra)
9373 {
9374 	struct ipw_priv *priv = libipw_priv(dev);
9375 
9376 	mutex_lock(&priv->mutex);
9377 	wrqu->retry.disabled = 0;
9378 
9379 	if ((wrqu->retry.flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
9380 		mutex_unlock(&priv->mutex);
9381 		return -EINVAL;
9382 	}
9383 
9384 	if (wrqu->retry.flags & IW_RETRY_LONG) {
9385 		wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
9386 		wrqu->retry.value = priv->long_retry_limit;
9387 	} else if (wrqu->retry.flags & IW_RETRY_SHORT) {
9388 		wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_SHORT;
9389 		wrqu->retry.value = priv->short_retry_limit;
9390 	} else {
9391 		wrqu->retry.flags = IW_RETRY_LIMIT;
9392 		wrqu->retry.value = priv->short_retry_limit;
9393 	}
9394 	mutex_unlock(&priv->mutex);
9395 
9396 	IPW_DEBUG_WX("GET retry -> %d\n", wrqu->retry.value);
9397 
9398 	return 0;
9399 }
9400 
9401 static int ipw_wx_set_scan(struct net_device *dev,
9402 			   struct iw_request_info *info,
9403 			   union iwreq_data *wrqu, char *extra)
9404 {
9405 	struct ipw_priv *priv = libipw_priv(dev);
9406 	struct iw_scan_req *req = (struct iw_scan_req *)extra;
9407 	struct delayed_work *work = NULL;
9408 
9409 	mutex_lock(&priv->mutex);
9410 
9411 	priv->user_requested_scan = 1;
9412 
9413 	if (wrqu->data.length == sizeof(struct iw_scan_req)) {
9414 		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
9415 			int len = min((int)req->essid_len,
9416 			              (int)sizeof(priv->direct_scan_ssid));
9417 			memcpy(priv->direct_scan_ssid, req->essid, len);
9418 			priv->direct_scan_ssid_len = len;
9419 			work = &priv->request_direct_scan;
9420 		} else if (req->scan_type == IW_SCAN_TYPE_PASSIVE) {
9421 			work = &priv->request_passive_scan;
9422 		}
9423 	} else {
9424 		/* Normal active broadcast scan */
9425 		work = &priv->request_scan;
9426 	}
9427 
9428 	mutex_unlock(&priv->mutex);
9429 
9430 	IPW_DEBUG_WX("Start scan\n");
9431 
9432 	schedule_delayed_work(work, 0);
9433 
9434 	return 0;
9435 }
9436 
9437 static int ipw_wx_get_scan(struct net_device *dev,
9438 			   struct iw_request_info *info,
9439 			   union iwreq_data *wrqu, char *extra)
9440 {
9441 	struct ipw_priv *priv = libipw_priv(dev);
9442 	return libipw_wx_get_scan(priv->ieee, info, wrqu, extra);
9443 }
9444 
9445 static int ipw_wx_set_encode(struct net_device *dev,
9446 			     struct iw_request_info *info,
9447 			     union iwreq_data *wrqu, char *key)
9448 {
9449 	struct ipw_priv *priv = libipw_priv(dev);
9450 	int ret;
9451 	u32 cap = priv->capability;
9452 
9453 	mutex_lock(&priv->mutex);
9454 	ret = libipw_wx_set_encode(priv->ieee, info, wrqu, key);
9455 
9456 	/* In IBSS mode, we need to notify the firmware to update
9457 	 * the beacon info after we changed the capability. */
9458 	if (cap != priv->capability &&
9459 	    priv->ieee->iw_mode == IW_MODE_ADHOC &&
9460 	    priv->status & STATUS_ASSOCIATED)
9461 		ipw_disassociate(priv);
9462 
9463 	mutex_unlock(&priv->mutex);
9464 	return ret;
9465 }
9466 
9467 static int ipw_wx_get_encode(struct net_device *dev,
9468 			     struct iw_request_info *info,
9469 			     union iwreq_data *wrqu, char *key)
9470 {
9471 	struct ipw_priv *priv = libipw_priv(dev);
9472 	return libipw_wx_get_encode(priv->ieee, info, wrqu, key);
9473 }
9474 
9475 static int ipw_wx_set_power(struct net_device *dev,
9476 			    struct iw_request_info *info,
9477 			    union iwreq_data *wrqu, char *extra)
9478 {
9479 	struct ipw_priv *priv = libipw_priv(dev);
9480 	int err;
9481 	mutex_lock(&priv->mutex);
9482 	if (wrqu->power.disabled) {
9483 		priv->power_mode = IPW_POWER_LEVEL(priv->power_mode);
9484 		err = ipw_send_power_mode(priv, IPW_POWER_MODE_CAM);
9485 		if (err) {
9486 			IPW_DEBUG_WX("failed setting power mode.\n");
9487 			mutex_unlock(&priv->mutex);
9488 			return err;
9489 		}
9490 		IPW_DEBUG_WX("SET Power Management Mode -> off\n");
9491 		mutex_unlock(&priv->mutex);
9492 		return 0;
9493 	}
9494 
9495 	switch (wrqu->power.flags & IW_POWER_MODE) {
9496 	case IW_POWER_ON:	/* If not specified */
9497 	case IW_POWER_MODE:	/* If set all mask */
9498 	case IW_POWER_ALL_R:	/* If explicitly state all */
9499 		break;
9500 	default:		/* Otherwise we don't support it */
9501 		IPW_DEBUG_WX("SET PM Mode: %X not supported.\n",
9502 			     wrqu->power.flags);
9503 		mutex_unlock(&priv->mutex);
9504 		return -EOPNOTSUPP;
9505 	}
9506 
9507 	/* If the user hasn't specified a power management mode yet, default
9508 	 * to BATTERY */
9509 	if (IPW_POWER_LEVEL(priv->power_mode) == IPW_POWER_AC)
9510 		priv->power_mode = IPW_POWER_ENABLED | IPW_POWER_BATTERY;
9511 	else
9512 		priv->power_mode = IPW_POWER_ENABLED | priv->power_mode;
9513 
9514 	err = ipw_send_power_mode(priv, IPW_POWER_LEVEL(priv->power_mode));
9515 	if (err) {
9516 		IPW_DEBUG_WX("failed setting power mode.\n");
9517 		mutex_unlock(&priv->mutex);
9518 		return err;
9519 	}
9520 
9521 	IPW_DEBUG_WX("SET Power Management Mode -> 0x%02X\n", priv->power_mode);
9522 	mutex_unlock(&priv->mutex);
9523 	return 0;
9524 }
9525 
9526 static int ipw_wx_get_power(struct net_device *dev,
9527 			    struct iw_request_info *info,
9528 			    union iwreq_data *wrqu, char *extra)
9529 {
9530 	struct ipw_priv *priv = libipw_priv(dev);
9531 	mutex_lock(&priv->mutex);
9532 	if (!(priv->power_mode & IPW_POWER_ENABLED))
9533 		wrqu->power.disabled = 1;
9534 	else
9535 		wrqu->power.disabled = 0;
9536 
9537 	mutex_unlock(&priv->mutex);
9538 	IPW_DEBUG_WX("GET Power Management Mode -> %02X\n", priv->power_mode);
9539 
9540 	return 0;
9541 }
9542 
9543 static int ipw_wx_set_powermode(struct net_device *dev,
9544 				struct iw_request_info *info,
9545 				union iwreq_data *wrqu, char *extra)
9546 {
9547 	struct ipw_priv *priv = libipw_priv(dev);
9548 	int mode = *(int *)extra;
9549 	int err;
9550 
9551 	mutex_lock(&priv->mutex);
9552 	if ((mode < 1) || (mode > IPW_POWER_LIMIT))
9553 		mode = IPW_POWER_AC;
9554 
9555 	if (IPW_POWER_LEVEL(priv->power_mode) != mode) {
9556 		err = ipw_send_power_mode(priv, mode);
9557 		if (err) {
9558 			IPW_DEBUG_WX("failed setting power mode.\n");
9559 			mutex_unlock(&priv->mutex);
9560 			return err;
9561 		}
9562 		priv->power_mode = IPW_POWER_ENABLED | mode;
9563 	}
9564 	mutex_unlock(&priv->mutex);
9565 	return 0;
9566 }
9567 
9568 #define MAX_WX_STRING 80
9569 static int ipw_wx_get_powermode(struct net_device *dev,
9570 				struct iw_request_info *info,
9571 				union iwreq_data *wrqu, char *extra)
9572 {
9573 	struct ipw_priv *priv = libipw_priv(dev);
9574 	int level = IPW_POWER_LEVEL(priv->power_mode);
9575 	char *p = extra;
9576 
9577 	p += scnprintf(p, MAX_WX_STRING, "Power save level: %d ", level);
9578 
9579 	switch (level) {
9580 	case IPW_POWER_AC:
9581 		p += scnprintf(p, MAX_WX_STRING - (p - extra), "(AC)");
9582 		break;
9583 	case IPW_POWER_BATTERY:
9584 		p += scnprintf(p, MAX_WX_STRING - (p - extra), "(BATTERY)");
9585 		break;
9586 	default:
9587 		p += scnprintf(p, MAX_WX_STRING - (p - extra),
9588 			      "(Timeout %dms, Period %dms)",
9589 			      timeout_duration[level - 1] / 1000,
9590 			      period_duration[level - 1] / 1000);
9591 	}
9592 
9593 	if (!(priv->power_mode & IPW_POWER_ENABLED))
9594 		p += scnprintf(p, MAX_WX_STRING - (p - extra), " OFF");
9595 
9596 	wrqu->data.length = p - extra + 1;
9597 
9598 	return 0;
9599 }
9600 
9601 static int ipw_wx_set_wireless_mode(struct net_device *dev,
9602 				    struct iw_request_info *info,
9603 				    union iwreq_data *wrqu, char *extra)
9604 {
9605 	struct ipw_priv *priv = libipw_priv(dev);
9606 	int mode = *(int *)extra;
9607 	u8 band = 0, modulation = 0;
9608 
9609 	if (mode == 0 || mode & ~IEEE_MODE_MASK) {
9610 		IPW_WARNING("Attempt to set invalid wireless mode: %d\n", mode);
9611 		return -EINVAL;
9612 	}
9613 	mutex_lock(&priv->mutex);
9614 	if (priv->adapter == IPW_2915ABG) {
9615 		priv->ieee->abg_true = 1;
9616 		if (mode & IEEE_A) {
9617 			band |= LIBIPW_52GHZ_BAND;
9618 			modulation |= LIBIPW_OFDM_MODULATION;
9619 		} else
9620 			priv->ieee->abg_true = 0;
9621 	} else {
9622 		if (mode & IEEE_A) {
9623 			IPW_WARNING("Attempt to set 2200BG into "
9624 				    "802.11a mode\n");
9625 			mutex_unlock(&priv->mutex);
9626 			return -EINVAL;
9627 		}
9628 
9629 		priv->ieee->abg_true = 0;
9630 	}
9631 
9632 	if (mode & IEEE_B) {
9633 		band |= LIBIPW_24GHZ_BAND;
9634 		modulation |= LIBIPW_CCK_MODULATION;
9635 	} else
9636 		priv->ieee->abg_true = 0;
9637 
9638 	if (mode & IEEE_G) {
9639 		band |= LIBIPW_24GHZ_BAND;
9640 		modulation |= LIBIPW_OFDM_MODULATION;
9641 	} else
9642 		priv->ieee->abg_true = 0;
9643 
9644 	priv->ieee->mode = mode;
9645 	priv->ieee->freq_band = band;
9646 	priv->ieee->modulation = modulation;
9647 	init_supported_rates(priv, &priv->rates);
9648 
9649 	/* Network configuration changed -- force [re]association */
9650 	IPW_DEBUG_ASSOC("[re]association triggered due to mode change.\n");
9651 	if (!ipw_disassociate(priv)) {
9652 		ipw_send_supported_rates(priv, &priv->rates);
9653 		ipw_associate(priv);
9654 	}
9655 
9656 	/* Update the band LEDs */
9657 	ipw_led_band_on(priv);
9658 
9659 	IPW_DEBUG_WX("PRIV SET MODE: %c%c%c\n",
9660 		     mode & IEEE_A ? 'a' : '.',
9661 		     mode & IEEE_B ? 'b' : '.', mode & IEEE_G ? 'g' : '.');
9662 	mutex_unlock(&priv->mutex);
9663 	return 0;
9664 }
9665 
9666 static int ipw_wx_get_wireless_mode(struct net_device *dev,
9667 				    struct iw_request_info *info,
9668 				    union iwreq_data *wrqu, char *extra)
9669 {
9670 	struct ipw_priv *priv = libipw_priv(dev);
9671 	mutex_lock(&priv->mutex);
9672 	switch (priv->ieee->mode) {
9673 	case IEEE_A:
9674 		strncpy(extra, "802.11a (1)", MAX_WX_STRING);
9675 		break;
9676 	case IEEE_B:
9677 		strncpy(extra, "802.11b (2)", MAX_WX_STRING);
9678 		break;
9679 	case IEEE_A | IEEE_B:
9680 		strncpy(extra, "802.11ab (3)", MAX_WX_STRING);
9681 		break;
9682 	case IEEE_G:
9683 		strncpy(extra, "802.11g (4)", MAX_WX_STRING);
9684 		break;
9685 	case IEEE_A | IEEE_G:
9686 		strncpy(extra, "802.11ag (5)", MAX_WX_STRING);
9687 		break;
9688 	case IEEE_B | IEEE_G:
9689 		strncpy(extra, "802.11bg (6)", MAX_WX_STRING);
9690 		break;
9691 	case IEEE_A | IEEE_B | IEEE_G:
9692 		strncpy(extra, "802.11abg (7)", MAX_WX_STRING);
9693 		break;
9694 	default:
9695 		strncpy(extra, "unknown", MAX_WX_STRING);
9696 		break;
9697 	}
9698 	extra[MAX_WX_STRING - 1] = '\0';
9699 
9700 	IPW_DEBUG_WX("PRIV GET MODE: %s\n", extra);
9701 
9702 	wrqu->data.length = strlen(extra) + 1;
9703 	mutex_unlock(&priv->mutex);
9704 
9705 	return 0;
9706 }
9707 
9708 static int ipw_wx_set_preamble(struct net_device *dev,
9709 			       struct iw_request_info *info,
9710 			       union iwreq_data *wrqu, char *extra)
9711 {
9712 	struct ipw_priv *priv = libipw_priv(dev);
9713 	int mode = *(int *)extra;
9714 	mutex_lock(&priv->mutex);
9715 	/* Switching from SHORT -> LONG requires a disassociation */
9716 	if (mode == 1) {
9717 		if (!(priv->config & CFG_PREAMBLE_LONG)) {
9718 			priv->config |= CFG_PREAMBLE_LONG;
9719 
9720 			/* Network configuration changed -- force [re]association */
9721 			IPW_DEBUG_ASSOC
9722 			    ("[re]association triggered due to preamble change.\n");
9723 			if (!ipw_disassociate(priv))
9724 				ipw_associate(priv);
9725 		}
9726 		goto done;
9727 	}
9728 
9729 	if (mode == 0) {
9730 		priv->config &= ~CFG_PREAMBLE_LONG;
9731 		goto done;
9732 	}
9733 	mutex_unlock(&priv->mutex);
9734 	return -EINVAL;
9735 
9736       done:
9737 	mutex_unlock(&priv->mutex);
9738 	return 0;
9739 }
9740 
9741 static int ipw_wx_get_preamble(struct net_device *dev,
9742 			       struct iw_request_info *info,
9743 			       union iwreq_data *wrqu, char *extra)
9744 {
9745 	struct ipw_priv *priv = libipw_priv(dev);
9746 	mutex_lock(&priv->mutex);
9747 	if (priv->config & CFG_PREAMBLE_LONG)
9748 		snprintf(wrqu->name, IFNAMSIZ, "long (1)");
9749 	else
9750 		snprintf(wrqu->name, IFNAMSIZ, "auto (0)");
9751 	mutex_unlock(&priv->mutex);
9752 	return 0;
9753 }
9754 
9755 #ifdef CONFIG_IPW2200_MONITOR
9756 static int ipw_wx_set_monitor(struct net_device *dev,
9757 			      struct iw_request_info *info,
9758 			      union iwreq_data *wrqu, char *extra)
9759 {
9760 	struct ipw_priv *priv = libipw_priv(dev);
9761 	int *parms = (int *)extra;
9762 	int enable = (parms[0] > 0);
9763 	mutex_lock(&priv->mutex);
9764 	IPW_DEBUG_WX("SET MONITOR: %d %d\n", enable, parms[1]);
9765 	if (enable) {
9766 		if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
9767 #ifdef CONFIG_IPW2200_RADIOTAP
9768 			priv->net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
9769 #else
9770 			priv->net_dev->type = ARPHRD_IEEE80211;
9771 #endif
9772 			schedule_work(&priv->adapter_restart);
9773 		}
9774 
9775 		ipw_set_channel(priv, parms[1]);
9776 	} else {
9777 		if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
9778 			mutex_unlock(&priv->mutex);
9779 			return 0;
9780 		}
9781 		priv->net_dev->type = ARPHRD_ETHER;
9782 		schedule_work(&priv->adapter_restart);
9783 	}
9784 	mutex_unlock(&priv->mutex);
9785 	return 0;
9786 }
9787 
9788 #endif				/* CONFIG_IPW2200_MONITOR */
9789 
9790 static int ipw_wx_reset(struct net_device *dev,
9791 			struct iw_request_info *info,
9792 			union iwreq_data *wrqu, char *extra)
9793 {
9794 	struct ipw_priv *priv = libipw_priv(dev);
9795 	IPW_DEBUG_WX("RESET\n");
9796 	schedule_work(&priv->adapter_restart);
9797 	return 0;
9798 }
9799 
9800 static int ipw_wx_sw_reset(struct net_device *dev,
9801 			   struct iw_request_info *info,
9802 			   union iwreq_data *wrqu, char *extra)
9803 {
9804 	struct ipw_priv *priv = libipw_priv(dev);
9805 	union iwreq_data wrqu_sec = {
9806 		.encoding = {
9807 			     .flags = IW_ENCODE_DISABLED,
9808 			     },
9809 	};
9810 	int ret;
9811 
9812 	IPW_DEBUG_WX("SW_RESET\n");
9813 
9814 	mutex_lock(&priv->mutex);
9815 
9816 	ret = ipw_sw_reset(priv, 2);
9817 	if (!ret) {
9818 		free_firmware();
9819 		ipw_adapter_restart(priv);
9820 	}
9821 
9822 	/* The SW reset bit might have been toggled on by the 'disable'
9823 	 * module parameter, so take appropriate action */
9824 	ipw_radio_kill_sw(priv, priv->status & STATUS_RF_KILL_SW);
9825 
9826 	mutex_unlock(&priv->mutex);
9827 	libipw_wx_set_encode(priv->ieee, info, &wrqu_sec, NULL);
9828 	mutex_lock(&priv->mutex);
9829 
9830 	if (!(priv->status & STATUS_RF_KILL_MASK)) {
9831 		/* Configuration likely changed -- force [re]association */
9832 		IPW_DEBUG_ASSOC("[re]association triggered due to sw "
9833 				"reset.\n");
9834 		if (!ipw_disassociate(priv))
9835 			ipw_associate(priv);
9836 	}
9837 
9838 	mutex_unlock(&priv->mutex);
9839 
9840 	return 0;
9841 }
9842 
9843 /* Rebase the WE IOCTLs to zero for the handler array */
9844 static iw_handler ipw_wx_handlers[] = {
9845 	IW_HANDLER(SIOCGIWNAME, cfg80211_wext_giwname),
9846 	IW_HANDLER(SIOCSIWFREQ, ipw_wx_set_freq),
9847 	IW_HANDLER(SIOCGIWFREQ, ipw_wx_get_freq),
9848 	IW_HANDLER(SIOCSIWMODE, ipw_wx_set_mode),
9849 	IW_HANDLER(SIOCGIWMODE, ipw_wx_get_mode),
9850 	IW_HANDLER(SIOCSIWSENS, ipw_wx_set_sens),
9851 	IW_HANDLER(SIOCGIWSENS, ipw_wx_get_sens),
9852 	IW_HANDLER(SIOCGIWRANGE, ipw_wx_get_range),
9853 	IW_HANDLER(SIOCSIWAP, ipw_wx_set_wap),
9854 	IW_HANDLER(SIOCGIWAP, ipw_wx_get_wap),
9855 	IW_HANDLER(SIOCSIWSCAN, ipw_wx_set_scan),
9856 	IW_HANDLER(SIOCGIWSCAN, ipw_wx_get_scan),
9857 	IW_HANDLER(SIOCSIWESSID, ipw_wx_set_essid),
9858 	IW_HANDLER(SIOCGIWESSID, ipw_wx_get_essid),
9859 	IW_HANDLER(SIOCSIWNICKN, ipw_wx_set_nick),
9860 	IW_HANDLER(SIOCGIWNICKN, ipw_wx_get_nick),
9861 	IW_HANDLER(SIOCSIWRATE, ipw_wx_set_rate),
9862 	IW_HANDLER(SIOCGIWRATE, ipw_wx_get_rate),
9863 	IW_HANDLER(SIOCSIWRTS, ipw_wx_set_rts),
9864 	IW_HANDLER(SIOCGIWRTS, ipw_wx_get_rts),
9865 	IW_HANDLER(SIOCSIWFRAG, ipw_wx_set_frag),
9866 	IW_HANDLER(SIOCGIWFRAG, ipw_wx_get_frag),
9867 	IW_HANDLER(SIOCSIWTXPOW, ipw_wx_set_txpow),
9868 	IW_HANDLER(SIOCGIWTXPOW, ipw_wx_get_txpow),
9869 	IW_HANDLER(SIOCSIWRETRY, ipw_wx_set_retry),
9870 	IW_HANDLER(SIOCGIWRETRY, ipw_wx_get_retry),
9871 	IW_HANDLER(SIOCSIWENCODE, ipw_wx_set_encode),
9872 	IW_HANDLER(SIOCGIWENCODE, ipw_wx_get_encode),
9873 	IW_HANDLER(SIOCSIWPOWER, ipw_wx_set_power),
9874 	IW_HANDLER(SIOCGIWPOWER, ipw_wx_get_power),
9875 	IW_HANDLER(SIOCSIWSPY, iw_handler_set_spy),
9876 	IW_HANDLER(SIOCGIWSPY, iw_handler_get_spy),
9877 	IW_HANDLER(SIOCSIWTHRSPY, iw_handler_set_thrspy),
9878 	IW_HANDLER(SIOCGIWTHRSPY, iw_handler_get_thrspy),
9879 	IW_HANDLER(SIOCSIWGENIE, ipw_wx_set_genie),
9880 	IW_HANDLER(SIOCGIWGENIE, ipw_wx_get_genie),
9881 	IW_HANDLER(SIOCSIWMLME, ipw_wx_set_mlme),
9882 	IW_HANDLER(SIOCSIWAUTH, ipw_wx_set_auth),
9883 	IW_HANDLER(SIOCGIWAUTH, ipw_wx_get_auth),
9884 	IW_HANDLER(SIOCSIWENCODEEXT, ipw_wx_set_encodeext),
9885 	IW_HANDLER(SIOCGIWENCODEEXT, ipw_wx_get_encodeext),
9886 };
9887 
9888 enum {
9889 	IPW_PRIV_SET_POWER = SIOCIWFIRSTPRIV,
9890 	IPW_PRIV_GET_POWER,
9891 	IPW_PRIV_SET_MODE,
9892 	IPW_PRIV_GET_MODE,
9893 	IPW_PRIV_SET_PREAMBLE,
9894 	IPW_PRIV_GET_PREAMBLE,
9895 	IPW_PRIV_RESET,
9896 	IPW_PRIV_SW_RESET,
9897 #ifdef CONFIG_IPW2200_MONITOR
9898 	IPW_PRIV_SET_MONITOR,
9899 #endif
9900 };
9901 
9902 static struct iw_priv_args ipw_priv_args[] = {
9903 	{
9904 	 .cmd = IPW_PRIV_SET_POWER,
9905 	 .set_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
9906 	 .name = "set_power"},
9907 	{
9908 	 .cmd = IPW_PRIV_GET_POWER,
9909 	 .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
9910 	 .name = "get_power"},
9911 	{
9912 	 .cmd = IPW_PRIV_SET_MODE,
9913 	 .set_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
9914 	 .name = "set_mode"},
9915 	{
9916 	 .cmd = IPW_PRIV_GET_MODE,
9917 	 .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
9918 	 .name = "get_mode"},
9919 	{
9920 	 .cmd = IPW_PRIV_SET_PREAMBLE,
9921 	 .set_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
9922 	 .name = "set_preamble"},
9923 	{
9924 	 .cmd = IPW_PRIV_GET_PREAMBLE,
9925 	 .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | IFNAMSIZ,
9926 	 .name = "get_preamble"},
9927 	{
9928 	 IPW_PRIV_RESET,
9929 	 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 0, 0, "reset"},
9930 	{
9931 	 IPW_PRIV_SW_RESET,
9932 	 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 0, 0, "sw_reset"},
9933 #ifdef CONFIG_IPW2200_MONITOR
9934 	{
9935 	 IPW_PRIV_SET_MONITOR,
9936 	 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "monitor"},
9937 #endif				/* CONFIG_IPW2200_MONITOR */
9938 };
9939 
9940 static iw_handler ipw_priv_handler[] = {
9941 	ipw_wx_set_powermode,
9942 	ipw_wx_get_powermode,
9943 	ipw_wx_set_wireless_mode,
9944 	ipw_wx_get_wireless_mode,
9945 	ipw_wx_set_preamble,
9946 	ipw_wx_get_preamble,
9947 	ipw_wx_reset,
9948 	ipw_wx_sw_reset,
9949 #ifdef CONFIG_IPW2200_MONITOR
9950 	ipw_wx_set_monitor,
9951 #endif
9952 };
9953 
9954 static const struct iw_handler_def ipw_wx_handler_def = {
9955 	.standard = ipw_wx_handlers,
9956 	.num_standard = ARRAY_SIZE(ipw_wx_handlers),
9957 	.num_private = ARRAY_SIZE(ipw_priv_handler),
9958 	.num_private_args = ARRAY_SIZE(ipw_priv_args),
9959 	.private = ipw_priv_handler,
9960 	.private_args = ipw_priv_args,
9961 	.get_wireless_stats = ipw_get_wireless_stats,
9962 };
9963 
9964 /*
9965  * Get wireless statistics.
9966  * Called by /proc/net/wireless
9967  * Also called by SIOCGIWSTATS
9968  */
9969 static struct iw_statistics *ipw_get_wireless_stats(struct net_device *dev)
9970 {
9971 	struct ipw_priv *priv = libipw_priv(dev);
9972 	struct iw_statistics *wstats;
9973 
9974 	wstats = &priv->wstats;
9975 
9976 	/* if hw is disabled, then ipw_get_ordinal() can't be called.
9977 	 * netdev->get_wireless_stats seems to be called before fw is
9978 	 * initialized.  STATUS_ASSOCIATED will only be set if the hw is up
9979 	 * and associated; if not associcated, the values are all meaningless
9980 	 * anyway, so set them all to NULL and INVALID */
9981 	if (!(priv->status & STATUS_ASSOCIATED)) {
9982 		wstats->miss.beacon = 0;
9983 		wstats->discard.retries = 0;
9984 		wstats->qual.qual = 0;
9985 		wstats->qual.level = 0;
9986 		wstats->qual.noise = 0;
9987 		wstats->qual.updated = 7;
9988 		wstats->qual.updated |= IW_QUAL_NOISE_INVALID |
9989 		    IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID;
9990 		return wstats;
9991 	}
9992 
9993 	wstats->qual.qual = priv->quality;
9994 	wstats->qual.level = priv->exp_avg_rssi;
9995 	wstats->qual.noise = priv->exp_avg_noise;
9996 	wstats->qual.updated = IW_QUAL_QUAL_UPDATED | IW_QUAL_LEVEL_UPDATED |
9997 	    IW_QUAL_NOISE_UPDATED | IW_QUAL_DBM;
9998 
9999 	wstats->miss.beacon = average_value(&priv->average_missed_beacons);
10000 	wstats->discard.retries = priv->last_tx_failures;
10001 	wstats->discard.code = priv->ieee->ieee_stats.rx_discards_undecryptable;
10002 
10003 /*	if (ipw_get_ordinal(priv, IPW_ORD_STAT_TX_RETRY, &tx_retry, &len))
10004 	goto fail_get_ordinal;
10005 	wstats->discard.retries += tx_retry; */
10006 
10007 	return wstats;
10008 }
10009 
10010 /* net device stuff */
10011 
10012 static  void init_sys_config(struct ipw_sys_config *sys_config)
10013 {
10014 	memset(sys_config, 0, sizeof(struct ipw_sys_config));
10015 	sys_config->bt_coexistence = 0;
10016 	sys_config->answer_broadcast_ssid_probe = 0;
10017 	sys_config->accept_all_data_frames = 0;
10018 	sys_config->accept_non_directed_frames = 1;
10019 	sys_config->exclude_unicast_unencrypted = 0;
10020 	sys_config->disable_unicast_decryption = 1;
10021 	sys_config->exclude_multicast_unencrypted = 0;
10022 	sys_config->disable_multicast_decryption = 1;
10023 	if (antenna < CFG_SYS_ANTENNA_BOTH || antenna > CFG_SYS_ANTENNA_B)
10024 		antenna = CFG_SYS_ANTENNA_BOTH;
10025 	sys_config->antenna_diversity = antenna;
10026 	sys_config->pass_crc_to_host = 0;	/* TODO: See if 1 gives us FCS */
10027 	sys_config->dot11g_auto_detection = 0;
10028 	sys_config->enable_cts_to_self = 0;
10029 	sys_config->bt_coexist_collision_thr = 0;
10030 	sys_config->pass_noise_stats_to_host = 1;	/* 1 -- fix for 256 */
10031 	sys_config->silence_threshold = 0x1e;
10032 }
10033 
10034 static int ipw_net_open(struct net_device *dev)
10035 {
10036 	IPW_DEBUG_INFO("dev->open\n");
10037 	netif_start_queue(dev);
10038 	return 0;
10039 }
10040 
10041 static int ipw_net_stop(struct net_device *dev)
10042 {
10043 	IPW_DEBUG_INFO("dev->close\n");
10044 	netif_stop_queue(dev);
10045 	return 0;
10046 }
10047 
10048 /*
10049 todo:
10050 
10051 modify to send one tfd per fragment instead of using chunking.  otherwise
10052 we need to heavily modify the libipw_skb_to_txb.
10053 */
10054 
10055 static int ipw_tx_skb(struct ipw_priv *priv, struct libipw_txb *txb,
10056 			     int pri)
10057 {
10058 	struct libipw_hdr_3addrqos *hdr = (struct libipw_hdr_3addrqos *)
10059 	    txb->fragments[0]->data;
10060 	int i = 0;
10061 	struct tfd_frame *tfd;
10062 #ifdef CONFIG_IPW2200_QOS
10063 	int tx_id = ipw_get_tx_queue_number(priv, pri);
10064 	struct clx2_tx_queue *txq = &priv->txq[tx_id];
10065 #else
10066 	struct clx2_tx_queue *txq = &priv->txq[0];
10067 #endif
10068 	struct clx2_queue *q = &txq->q;
10069 	u8 id, hdr_len, unicast;
10070 	int fc;
10071 
10072 	if (!(priv->status & STATUS_ASSOCIATED))
10073 		goto drop;
10074 
10075 	hdr_len = libipw_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
10076 	switch (priv->ieee->iw_mode) {
10077 	case IW_MODE_ADHOC:
10078 		unicast = !is_multicast_ether_addr(hdr->addr1);
10079 		id = ipw_find_station(priv, hdr->addr1);
10080 		if (id == IPW_INVALID_STATION) {
10081 			id = ipw_add_station(priv, hdr->addr1);
10082 			if (id == IPW_INVALID_STATION) {
10083 				IPW_WARNING("Attempt to send data to "
10084 					    "invalid cell: %pM\n",
10085 					    hdr->addr1);
10086 				goto drop;
10087 			}
10088 		}
10089 		break;
10090 
10091 	case IW_MODE_INFRA:
10092 	default:
10093 		unicast = !is_multicast_ether_addr(hdr->addr3);
10094 		id = 0;
10095 		break;
10096 	}
10097 
10098 	tfd = &txq->bd[q->first_empty];
10099 	txq->txb[q->first_empty] = txb;
10100 	memset(tfd, 0, sizeof(*tfd));
10101 	tfd->u.data.station_number = id;
10102 
10103 	tfd->control_flags.message_type = TX_FRAME_TYPE;
10104 	tfd->control_flags.control_bits = TFD_NEED_IRQ_MASK;
10105 
10106 	tfd->u.data.cmd_id = DINO_CMD_TX;
10107 	tfd->u.data.len = cpu_to_le16(txb->payload_size);
10108 
10109 	if (priv->assoc_request.ieee_mode == IPW_B_MODE)
10110 		tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_MODE_CCK;
10111 	else
10112 		tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_MODE_OFDM;
10113 
10114 	if (priv->assoc_request.preamble_length == DCT_FLAG_SHORT_PREAMBLE)
10115 		tfd->u.data.tx_flags |= DCT_FLAG_SHORT_PREAMBLE;
10116 
10117 	fc = le16_to_cpu(hdr->frame_ctl);
10118 	hdr->frame_ctl = cpu_to_le16(fc & ~IEEE80211_FCTL_MOREFRAGS);
10119 
10120 	memcpy(&tfd->u.data.tfd.tfd_24.mchdr, hdr, hdr_len);
10121 
10122 	if (likely(unicast))
10123 		tfd->u.data.tx_flags |= DCT_FLAG_ACK_REQD;
10124 
10125 	if (txb->encrypted && !priv->ieee->host_encrypt) {
10126 		switch (priv->ieee->sec.level) {
10127 		case SEC_LEVEL_3:
10128 			tfd->u.data.tfd.tfd_24.mchdr.frame_ctl |=
10129 			    cpu_to_le16(IEEE80211_FCTL_PROTECTED);
10130 			/* XXX: ACK flag must be set for CCMP even if it
10131 			 * is a multicast/broadcast packet, because CCMP
10132 			 * group communication encrypted by GTK is
10133 			 * actually done by the AP. */
10134 			if (!unicast)
10135 				tfd->u.data.tx_flags |= DCT_FLAG_ACK_REQD;
10136 
10137 			tfd->u.data.tx_flags &= ~DCT_FLAG_NO_WEP;
10138 			tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_SECURITY_CCM;
10139 			tfd->u.data.key_index = 0;
10140 			tfd->u.data.key_index |= DCT_WEP_INDEX_USE_IMMEDIATE;
10141 			break;
10142 		case SEC_LEVEL_2:
10143 			tfd->u.data.tfd.tfd_24.mchdr.frame_ctl |=
10144 			    cpu_to_le16(IEEE80211_FCTL_PROTECTED);
10145 			tfd->u.data.tx_flags &= ~DCT_FLAG_NO_WEP;
10146 			tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_SECURITY_TKIP;
10147 			tfd->u.data.key_index = DCT_WEP_INDEX_USE_IMMEDIATE;
10148 			break;
10149 		case SEC_LEVEL_1:
10150 			tfd->u.data.tfd.tfd_24.mchdr.frame_ctl |=
10151 			    cpu_to_le16(IEEE80211_FCTL_PROTECTED);
10152 			tfd->u.data.key_index = priv->ieee->crypt_info.tx_keyidx;
10153 			if (priv->ieee->sec.key_sizes[priv->ieee->crypt_info.tx_keyidx] <=
10154 			    40)
10155 				tfd->u.data.key_index |= DCT_WEP_KEY_64Bit;
10156 			else
10157 				tfd->u.data.key_index |= DCT_WEP_KEY_128Bit;
10158 			break;
10159 		case SEC_LEVEL_0:
10160 			break;
10161 		default:
10162 			printk(KERN_ERR "Unknown security level %d\n",
10163 			       priv->ieee->sec.level);
10164 			break;
10165 		}
10166 	} else
10167 		/* No hardware encryption */
10168 		tfd->u.data.tx_flags |= DCT_FLAG_NO_WEP;
10169 
10170 #ifdef CONFIG_IPW2200_QOS
10171 	if (fc & IEEE80211_STYPE_QOS_DATA)
10172 		ipw_qos_set_tx_queue_command(priv, pri, &(tfd->u.data));
10173 #endif				/* CONFIG_IPW2200_QOS */
10174 
10175 	/* payload */
10176 	tfd->u.data.num_chunks = cpu_to_le32(min((u8) (NUM_TFD_CHUNKS - 2),
10177 						 txb->nr_frags));
10178 	IPW_DEBUG_FRAG("%i fragments being sent as %i chunks.\n",
10179 		       txb->nr_frags, le32_to_cpu(tfd->u.data.num_chunks));
10180 	for (i = 0; i < le32_to_cpu(tfd->u.data.num_chunks); i++) {
10181 		IPW_DEBUG_FRAG("Adding fragment %i of %i (%d bytes).\n",
10182 			       i, le32_to_cpu(tfd->u.data.num_chunks),
10183 			       txb->fragments[i]->len - hdr_len);
10184 		IPW_DEBUG_TX("Dumping TX packet frag %i of %i (%d bytes):\n",
10185 			     i, tfd->u.data.num_chunks,
10186 			     txb->fragments[i]->len - hdr_len);
10187 		printk_buf(IPW_DL_TX, txb->fragments[i]->data + hdr_len,
10188 			   txb->fragments[i]->len - hdr_len);
10189 
10190 		tfd->u.data.chunk_ptr[i] =
10191 		    cpu_to_le32(dma_map_single(&priv->pci_dev->dev,
10192 					       txb->fragments[i]->data + hdr_len,
10193 					       txb->fragments[i]->len - hdr_len,
10194 					       DMA_TO_DEVICE));
10195 		tfd->u.data.chunk_len[i] =
10196 		    cpu_to_le16(txb->fragments[i]->len - hdr_len);
10197 	}
10198 
10199 	if (i != txb->nr_frags) {
10200 		struct sk_buff *skb;
10201 		u16 remaining_bytes = 0;
10202 		int j;
10203 
10204 		for (j = i; j < txb->nr_frags; j++)
10205 			remaining_bytes += txb->fragments[j]->len - hdr_len;
10206 
10207 		printk(KERN_INFO "Trying to reallocate for %d bytes\n",
10208 		       remaining_bytes);
10209 		skb = alloc_skb(remaining_bytes, GFP_ATOMIC);
10210 		if (skb != NULL) {
10211 			tfd->u.data.chunk_len[i] = cpu_to_le16(remaining_bytes);
10212 			for (j = i; j < txb->nr_frags; j++) {
10213 				int size = txb->fragments[j]->len - hdr_len;
10214 
10215 				printk(KERN_INFO "Adding frag %d %d...\n",
10216 				       j, size);
10217 				skb_put_data(skb,
10218 					     txb->fragments[j]->data + hdr_len,
10219 					     size);
10220 			}
10221 			dev_kfree_skb_any(txb->fragments[i]);
10222 			txb->fragments[i] = skb;
10223 			tfd->u.data.chunk_ptr[i] =
10224 			    cpu_to_le32(dma_map_single(&priv->pci_dev->dev,
10225 						       skb->data,
10226 						       remaining_bytes,
10227 						       DMA_TO_DEVICE));
10228 
10229 			le32_add_cpu(&tfd->u.data.num_chunks, 1);
10230 		}
10231 	}
10232 
10233 	/* kick DMA */
10234 	q->first_empty = ipw_queue_inc_wrap(q->first_empty, q->n_bd);
10235 	ipw_write32(priv, q->reg_w, q->first_empty);
10236 
10237 	if (ipw_tx_queue_space(q) < q->high_mark)
10238 		netif_stop_queue(priv->net_dev);
10239 
10240 	return NETDEV_TX_OK;
10241 
10242       drop:
10243 	IPW_DEBUG_DROP("Silently dropping Tx packet.\n");
10244 	libipw_txb_free(txb);
10245 	return NETDEV_TX_OK;
10246 }
10247 
10248 static int ipw_net_is_queue_full(struct net_device *dev, int pri)
10249 {
10250 	struct ipw_priv *priv = libipw_priv(dev);
10251 #ifdef CONFIG_IPW2200_QOS
10252 	int tx_id = ipw_get_tx_queue_number(priv, pri);
10253 	struct clx2_tx_queue *txq = &priv->txq[tx_id];
10254 #else
10255 	struct clx2_tx_queue *txq = &priv->txq[0];
10256 #endif				/* CONFIG_IPW2200_QOS */
10257 
10258 	if (ipw_tx_queue_space(&txq->q) < txq->q.high_mark)
10259 		return 1;
10260 
10261 	return 0;
10262 }
10263 
10264 #ifdef CONFIG_IPW2200_PROMISCUOUS
10265 static void ipw_handle_promiscuous_tx(struct ipw_priv *priv,
10266 				      struct libipw_txb *txb)
10267 {
10268 	struct libipw_rx_stats dummystats;
10269 	struct ieee80211_hdr *hdr;
10270 	u8 n;
10271 	u16 filter = priv->prom_priv->filter;
10272 	int hdr_only = 0;
10273 
10274 	if (filter & IPW_PROM_NO_TX)
10275 		return;
10276 
10277 	memset(&dummystats, 0, sizeof(dummystats));
10278 
10279 	/* Filtering of fragment chains is done against the first fragment */
10280 	hdr = (void *)txb->fragments[0]->data;
10281 	if (libipw_is_management(le16_to_cpu(hdr->frame_control))) {
10282 		if (filter & IPW_PROM_NO_MGMT)
10283 			return;
10284 		if (filter & IPW_PROM_MGMT_HEADER_ONLY)
10285 			hdr_only = 1;
10286 	} else if (libipw_is_control(le16_to_cpu(hdr->frame_control))) {
10287 		if (filter & IPW_PROM_NO_CTL)
10288 			return;
10289 		if (filter & IPW_PROM_CTL_HEADER_ONLY)
10290 			hdr_only = 1;
10291 	} else if (libipw_is_data(le16_to_cpu(hdr->frame_control))) {
10292 		if (filter & IPW_PROM_NO_DATA)
10293 			return;
10294 		if (filter & IPW_PROM_DATA_HEADER_ONLY)
10295 			hdr_only = 1;
10296 	}
10297 
10298 	for(n=0; n<txb->nr_frags; ++n) {
10299 		struct sk_buff *src = txb->fragments[n];
10300 		struct sk_buff *dst;
10301 		struct ieee80211_radiotap_header *rt_hdr;
10302 		int len;
10303 
10304 		if (hdr_only) {
10305 			hdr = (void *)src->data;
10306 			len = libipw_get_hdrlen(le16_to_cpu(hdr->frame_control));
10307 		} else
10308 			len = src->len;
10309 
10310 		dst = alloc_skb(len + sizeof(*rt_hdr) + sizeof(u16)*2, GFP_ATOMIC);
10311 		if (!dst)
10312 			continue;
10313 
10314 		rt_hdr = skb_put(dst, sizeof(*rt_hdr));
10315 
10316 		rt_hdr->it_version = PKTHDR_RADIOTAP_VERSION;
10317 		rt_hdr->it_pad = 0;
10318 		rt_hdr->it_present = 0; /* after all, it's just an idea */
10319 		rt_hdr->it_present |=  cpu_to_le32(1 << IEEE80211_RADIOTAP_CHANNEL);
10320 
10321 		*(__le16*)skb_put(dst, sizeof(u16)) = cpu_to_le16(
10322 			ieee80211chan2mhz(priv->channel));
10323 		if (priv->channel > 14) 	/* 802.11a */
10324 			*(__le16*)skb_put(dst, sizeof(u16)) =
10325 				cpu_to_le16(IEEE80211_CHAN_OFDM |
10326 					     IEEE80211_CHAN_5GHZ);
10327 		else if (priv->ieee->mode == IEEE_B) /* 802.11b */
10328 			*(__le16*)skb_put(dst, sizeof(u16)) =
10329 				cpu_to_le16(IEEE80211_CHAN_CCK |
10330 					     IEEE80211_CHAN_2GHZ);
10331 		else 		/* 802.11g */
10332 			*(__le16*)skb_put(dst, sizeof(u16)) =
10333 				cpu_to_le16(IEEE80211_CHAN_OFDM |
10334 				 IEEE80211_CHAN_2GHZ);
10335 
10336 		rt_hdr->it_len = cpu_to_le16(dst->len);
10337 
10338 		skb_copy_from_linear_data(src, skb_put(dst, len), len);
10339 
10340 		if (!libipw_rx(priv->prom_priv->ieee, dst, &dummystats))
10341 			dev_kfree_skb_any(dst);
10342 	}
10343 }
10344 #endif
10345 
10346 static netdev_tx_t ipw_net_hard_start_xmit(struct libipw_txb *txb,
10347 					   struct net_device *dev, int pri)
10348 {
10349 	struct ipw_priv *priv = libipw_priv(dev);
10350 	unsigned long flags;
10351 	netdev_tx_t ret;
10352 
10353 	IPW_DEBUG_TX("dev->xmit(%d bytes)\n", txb->payload_size);
10354 	spin_lock_irqsave(&priv->lock, flags);
10355 
10356 #ifdef CONFIG_IPW2200_PROMISCUOUS
10357 	if (rtap_iface && netif_running(priv->prom_net_dev))
10358 		ipw_handle_promiscuous_tx(priv, txb);
10359 #endif
10360 
10361 	ret = ipw_tx_skb(priv, txb, pri);
10362 	if (ret == NETDEV_TX_OK)
10363 		__ipw_led_activity_on(priv);
10364 	spin_unlock_irqrestore(&priv->lock, flags);
10365 
10366 	return ret;
10367 }
10368 
10369 static void ipw_net_set_multicast_list(struct net_device *dev)
10370 {
10371 
10372 }
10373 
10374 static int ipw_net_set_mac_address(struct net_device *dev, void *p)
10375 {
10376 	struct ipw_priv *priv = libipw_priv(dev);
10377 	struct sockaddr *addr = p;
10378 
10379 	if (!is_valid_ether_addr(addr->sa_data))
10380 		return -EADDRNOTAVAIL;
10381 	mutex_lock(&priv->mutex);
10382 	priv->config |= CFG_CUSTOM_MAC;
10383 	memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
10384 	printk(KERN_INFO "%s: Setting MAC to %pM\n",
10385 	       priv->net_dev->name, priv->mac_addr);
10386 	schedule_work(&priv->adapter_restart);
10387 	mutex_unlock(&priv->mutex);
10388 	return 0;
10389 }
10390 
10391 static void ipw_ethtool_get_drvinfo(struct net_device *dev,
10392 				    struct ethtool_drvinfo *info)
10393 {
10394 	struct ipw_priv *p = libipw_priv(dev);
10395 	char vers[64];
10396 	char date[32];
10397 	u32 len;
10398 
10399 	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
10400 	strscpy(info->version, DRV_VERSION, sizeof(info->version));
10401 
10402 	len = sizeof(vers);
10403 	ipw_get_ordinal(p, IPW_ORD_STAT_FW_VERSION, vers, &len);
10404 	len = sizeof(date);
10405 	ipw_get_ordinal(p, IPW_ORD_STAT_FW_DATE, date, &len);
10406 
10407 	snprintf(info->fw_version, sizeof(info->fw_version), "%s (%s)",
10408 		 vers, date);
10409 	strscpy(info->bus_info, pci_name(p->pci_dev),
10410 		sizeof(info->bus_info));
10411 }
10412 
10413 static u32 ipw_ethtool_get_link(struct net_device *dev)
10414 {
10415 	struct ipw_priv *priv = libipw_priv(dev);
10416 	return (priv->status & STATUS_ASSOCIATED) != 0;
10417 }
10418 
10419 static int ipw_ethtool_get_eeprom_len(struct net_device *dev)
10420 {
10421 	return IPW_EEPROM_IMAGE_SIZE;
10422 }
10423 
10424 static int ipw_ethtool_get_eeprom(struct net_device *dev,
10425 				  struct ethtool_eeprom *eeprom, u8 * bytes)
10426 {
10427 	struct ipw_priv *p = libipw_priv(dev);
10428 
10429 	if (eeprom->offset + eeprom->len > IPW_EEPROM_IMAGE_SIZE)
10430 		return -EINVAL;
10431 	mutex_lock(&p->mutex);
10432 	memcpy(bytes, &p->eeprom[eeprom->offset], eeprom->len);
10433 	mutex_unlock(&p->mutex);
10434 	return 0;
10435 }
10436 
10437 static int ipw_ethtool_set_eeprom(struct net_device *dev,
10438 				  struct ethtool_eeprom *eeprom, u8 * bytes)
10439 {
10440 	struct ipw_priv *p = libipw_priv(dev);
10441 	int i;
10442 
10443 	if (eeprom->offset + eeprom->len > IPW_EEPROM_IMAGE_SIZE)
10444 		return -EINVAL;
10445 	mutex_lock(&p->mutex);
10446 	memcpy(&p->eeprom[eeprom->offset], bytes, eeprom->len);
10447 	for (i = 0; i < IPW_EEPROM_IMAGE_SIZE; i++)
10448 		ipw_write8(p, i + IPW_EEPROM_DATA, p->eeprom[i]);
10449 	mutex_unlock(&p->mutex);
10450 	return 0;
10451 }
10452 
10453 static const struct ethtool_ops ipw_ethtool_ops = {
10454 	.get_link = ipw_ethtool_get_link,
10455 	.get_drvinfo = ipw_ethtool_get_drvinfo,
10456 	.get_eeprom_len = ipw_ethtool_get_eeprom_len,
10457 	.get_eeprom = ipw_ethtool_get_eeprom,
10458 	.set_eeprom = ipw_ethtool_set_eeprom,
10459 };
10460 
10461 static irqreturn_t ipw_isr(int irq, void *data)
10462 {
10463 	struct ipw_priv *priv = data;
10464 	u32 inta, inta_mask;
10465 
10466 	if (!priv)
10467 		return IRQ_NONE;
10468 
10469 	spin_lock(&priv->irq_lock);
10470 
10471 	if (!(priv->status & STATUS_INT_ENABLED)) {
10472 		/* IRQ is disabled */
10473 		goto none;
10474 	}
10475 
10476 	inta = ipw_read32(priv, IPW_INTA_RW);
10477 	inta_mask = ipw_read32(priv, IPW_INTA_MASK_R);
10478 
10479 	if (inta == 0xFFFFFFFF) {
10480 		/* Hardware disappeared */
10481 		IPW_WARNING("IRQ INTA == 0xFFFFFFFF\n");
10482 		goto none;
10483 	}
10484 
10485 	if (!(inta & (IPW_INTA_MASK_ALL & inta_mask))) {
10486 		/* Shared interrupt */
10487 		goto none;
10488 	}
10489 
10490 	/* tell the device to stop sending interrupts */
10491 	__ipw_disable_interrupts(priv);
10492 
10493 	/* ack current interrupts */
10494 	inta &= (IPW_INTA_MASK_ALL & inta_mask);
10495 	ipw_write32(priv, IPW_INTA_RW, inta);
10496 
10497 	/* Cache INTA value for our tasklet */
10498 	priv->isr_inta = inta;
10499 
10500 	tasklet_schedule(&priv->irq_tasklet);
10501 
10502 	spin_unlock(&priv->irq_lock);
10503 
10504 	return IRQ_HANDLED;
10505       none:
10506 	spin_unlock(&priv->irq_lock);
10507 	return IRQ_NONE;
10508 }
10509 
10510 static void ipw_rf_kill(void *adapter)
10511 {
10512 	struct ipw_priv *priv = adapter;
10513 	unsigned long flags;
10514 
10515 	spin_lock_irqsave(&priv->lock, flags);
10516 
10517 	if (rf_kill_active(priv)) {
10518 		IPW_DEBUG_RF_KILL("RF Kill active, rescheduling GPIO check\n");
10519 		schedule_delayed_work(&priv->rf_kill, 2 * HZ);
10520 		goto exit_unlock;
10521 	}
10522 
10523 	/* RF Kill is now disabled, so bring the device back up */
10524 
10525 	if (!(priv->status & STATUS_RF_KILL_MASK)) {
10526 		IPW_DEBUG_RF_KILL("HW RF Kill no longer active, restarting "
10527 				  "device\n");
10528 
10529 		/* we can not do an adapter restart while inside an irq lock */
10530 		schedule_work(&priv->adapter_restart);
10531 	} else
10532 		IPW_DEBUG_RF_KILL("HW RF Kill deactivated.  SW RF Kill still "
10533 				  "enabled\n");
10534 
10535       exit_unlock:
10536 	spin_unlock_irqrestore(&priv->lock, flags);
10537 }
10538 
10539 static void ipw_bg_rf_kill(struct work_struct *work)
10540 {
10541 	struct ipw_priv *priv =
10542 		container_of(work, struct ipw_priv, rf_kill.work);
10543 	mutex_lock(&priv->mutex);
10544 	ipw_rf_kill(priv);
10545 	mutex_unlock(&priv->mutex);
10546 }
10547 
10548 static void ipw_link_up(struct ipw_priv *priv)
10549 {
10550 	priv->last_seq_num = -1;
10551 	priv->last_frag_num = -1;
10552 	priv->last_packet_time = 0;
10553 
10554 	netif_carrier_on(priv->net_dev);
10555 
10556 	cancel_delayed_work(&priv->request_scan);
10557 	cancel_delayed_work(&priv->request_direct_scan);
10558 	cancel_delayed_work(&priv->request_passive_scan);
10559 	cancel_delayed_work(&priv->scan_event);
10560 	ipw_reset_stats(priv);
10561 	/* Ensure the rate is updated immediately */
10562 	priv->last_rate = ipw_get_current_rate(priv);
10563 	ipw_gather_stats(priv);
10564 	ipw_led_link_up(priv);
10565 	notify_wx_assoc_event(priv);
10566 
10567 	if (priv->config & CFG_BACKGROUND_SCAN)
10568 		schedule_delayed_work(&priv->request_scan, HZ);
10569 }
10570 
10571 static void ipw_bg_link_up(struct work_struct *work)
10572 {
10573 	struct ipw_priv *priv =
10574 		container_of(work, struct ipw_priv, link_up);
10575 	mutex_lock(&priv->mutex);
10576 	ipw_link_up(priv);
10577 	mutex_unlock(&priv->mutex);
10578 }
10579 
10580 static void ipw_link_down(struct ipw_priv *priv)
10581 {
10582 	ipw_led_link_down(priv);
10583 	netif_carrier_off(priv->net_dev);
10584 	notify_wx_assoc_event(priv);
10585 
10586 	/* Cancel any queued work ... */
10587 	cancel_delayed_work(&priv->request_scan);
10588 	cancel_delayed_work(&priv->request_direct_scan);
10589 	cancel_delayed_work(&priv->request_passive_scan);
10590 	cancel_delayed_work(&priv->adhoc_check);
10591 	cancel_delayed_work(&priv->gather_stats);
10592 
10593 	ipw_reset_stats(priv);
10594 
10595 	if (!(priv->status & STATUS_EXIT_PENDING)) {
10596 		/* Queue up another scan... */
10597 		schedule_delayed_work(&priv->request_scan, 0);
10598 	} else
10599 		cancel_delayed_work(&priv->scan_event);
10600 }
10601 
10602 static void ipw_bg_link_down(struct work_struct *work)
10603 {
10604 	struct ipw_priv *priv =
10605 		container_of(work, struct ipw_priv, link_down);
10606 	mutex_lock(&priv->mutex);
10607 	ipw_link_down(priv);
10608 	mutex_unlock(&priv->mutex);
10609 }
10610 
10611 static void ipw_setup_deferred_work(struct ipw_priv *priv)
10612 {
10613 	init_waitqueue_head(&priv->wait_command_queue);
10614 	init_waitqueue_head(&priv->wait_state);
10615 
10616 	INIT_DELAYED_WORK(&priv->adhoc_check, ipw_bg_adhoc_check);
10617 	INIT_WORK(&priv->associate, ipw_bg_associate);
10618 	INIT_WORK(&priv->disassociate, ipw_bg_disassociate);
10619 	INIT_WORK(&priv->system_config, ipw_system_config);
10620 	INIT_WORK(&priv->rx_replenish, ipw_bg_rx_queue_replenish);
10621 	INIT_WORK(&priv->adapter_restart, ipw_bg_adapter_restart);
10622 	INIT_DELAYED_WORK(&priv->rf_kill, ipw_bg_rf_kill);
10623 	INIT_WORK(&priv->up, ipw_bg_up);
10624 	INIT_WORK(&priv->down, ipw_bg_down);
10625 	INIT_DELAYED_WORK(&priv->request_scan, ipw_request_scan);
10626 	INIT_DELAYED_WORK(&priv->request_direct_scan, ipw_request_direct_scan);
10627 	INIT_DELAYED_WORK(&priv->request_passive_scan, ipw_request_passive_scan);
10628 	INIT_DELAYED_WORK(&priv->scan_event, ipw_scan_event);
10629 	INIT_DELAYED_WORK(&priv->gather_stats, ipw_bg_gather_stats);
10630 	INIT_WORK(&priv->abort_scan, ipw_bg_abort_scan);
10631 	INIT_WORK(&priv->roam, ipw_bg_roam);
10632 	INIT_DELAYED_WORK(&priv->scan_check, ipw_bg_scan_check);
10633 	INIT_WORK(&priv->link_up, ipw_bg_link_up);
10634 	INIT_WORK(&priv->link_down, ipw_bg_link_down);
10635 	INIT_DELAYED_WORK(&priv->led_link_on, ipw_bg_led_link_on);
10636 	INIT_DELAYED_WORK(&priv->led_link_off, ipw_bg_led_link_off);
10637 	INIT_DELAYED_WORK(&priv->led_act_off, ipw_bg_led_activity_off);
10638 	INIT_WORK(&priv->merge_networks, ipw_merge_adhoc_network);
10639 
10640 #ifdef CONFIG_IPW2200_QOS
10641 	INIT_WORK(&priv->qos_activate, ipw_bg_qos_activate);
10642 #endif				/* CONFIG_IPW2200_QOS */
10643 
10644 	tasklet_setup(&priv->irq_tasklet, ipw_irq_tasklet);
10645 }
10646 
10647 static void shim__set_security(struct net_device *dev,
10648 			       struct libipw_security *sec)
10649 {
10650 	struct ipw_priv *priv = libipw_priv(dev);
10651 	int i;
10652 	for (i = 0; i < 4; i++) {
10653 		if (sec->flags & (1 << i)) {
10654 			priv->ieee->sec.encode_alg[i] = sec->encode_alg[i];
10655 			priv->ieee->sec.key_sizes[i] = sec->key_sizes[i];
10656 			if (sec->key_sizes[i] == 0)
10657 				priv->ieee->sec.flags &= ~(1 << i);
10658 			else {
10659 				memcpy(priv->ieee->sec.keys[i], sec->keys[i],
10660 				       sec->key_sizes[i]);
10661 				priv->ieee->sec.flags |= (1 << i);
10662 			}
10663 			priv->status |= STATUS_SECURITY_UPDATED;
10664 		} else if (sec->level != SEC_LEVEL_1)
10665 			priv->ieee->sec.flags &= ~(1 << i);
10666 	}
10667 
10668 	if (sec->flags & SEC_ACTIVE_KEY) {
10669 		priv->ieee->sec.active_key = sec->active_key;
10670 		priv->ieee->sec.flags |= SEC_ACTIVE_KEY;
10671 		priv->status |= STATUS_SECURITY_UPDATED;
10672 	} else
10673 		priv->ieee->sec.flags &= ~SEC_ACTIVE_KEY;
10674 
10675 	if ((sec->flags & SEC_AUTH_MODE) &&
10676 	    (priv->ieee->sec.auth_mode != sec->auth_mode)) {
10677 		priv->ieee->sec.auth_mode = sec->auth_mode;
10678 		priv->ieee->sec.flags |= SEC_AUTH_MODE;
10679 		if (sec->auth_mode == WLAN_AUTH_SHARED_KEY)
10680 			priv->capability |= CAP_SHARED_KEY;
10681 		else
10682 			priv->capability &= ~CAP_SHARED_KEY;
10683 		priv->status |= STATUS_SECURITY_UPDATED;
10684 	}
10685 
10686 	if (sec->flags & SEC_ENABLED && priv->ieee->sec.enabled != sec->enabled) {
10687 		priv->ieee->sec.flags |= SEC_ENABLED;
10688 		priv->ieee->sec.enabled = sec->enabled;
10689 		priv->status |= STATUS_SECURITY_UPDATED;
10690 		if (sec->enabled)
10691 			priv->capability |= CAP_PRIVACY_ON;
10692 		else
10693 			priv->capability &= ~CAP_PRIVACY_ON;
10694 	}
10695 
10696 	if (sec->flags & SEC_ENCRYPT)
10697 		priv->ieee->sec.encrypt = sec->encrypt;
10698 
10699 	if (sec->flags & SEC_LEVEL && priv->ieee->sec.level != sec->level) {
10700 		priv->ieee->sec.level = sec->level;
10701 		priv->ieee->sec.flags |= SEC_LEVEL;
10702 		priv->status |= STATUS_SECURITY_UPDATED;
10703 	}
10704 
10705 	if (!priv->ieee->host_encrypt && (sec->flags & SEC_ENCRYPT))
10706 		ipw_set_hwcrypto_keys(priv);
10707 
10708 	/* To match current functionality of ipw2100 (which works well w/
10709 	 * various supplicants, we don't force a disassociate if the
10710 	 * privacy capability changes ... */
10711 #if 0
10712 	if ((priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) &&
10713 	    (((priv->assoc_request.capability &
10714 	       cpu_to_le16(WLAN_CAPABILITY_PRIVACY)) && !sec->enabled) ||
10715 	     (!(priv->assoc_request.capability &
10716 		cpu_to_le16(WLAN_CAPABILITY_PRIVACY)) && sec->enabled))) {
10717 		IPW_DEBUG_ASSOC("Disassociating due to capability "
10718 				"change.\n");
10719 		ipw_disassociate(priv);
10720 	}
10721 #endif
10722 }
10723 
10724 static int init_supported_rates(struct ipw_priv *priv,
10725 				struct ipw_supported_rates *rates)
10726 {
10727 	/* TODO: Mask out rates based on priv->rates_mask */
10728 
10729 	memset(rates, 0, sizeof(*rates));
10730 	/* configure supported rates */
10731 	switch (priv->ieee->freq_band) {
10732 	case LIBIPW_52GHZ_BAND:
10733 		rates->ieee_mode = IPW_A_MODE;
10734 		rates->purpose = IPW_RATE_CAPABILITIES;
10735 		ipw_add_ofdm_scan_rates(rates, LIBIPW_CCK_MODULATION,
10736 					LIBIPW_OFDM_DEFAULT_RATES_MASK);
10737 		break;
10738 
10739 	default:		/* Mixed or 2.4Ghz */
10740 		rates->ieee_mode = IPW_G_MODE;
10741 		rates->purpose = IPW_RATE_CAPABILITIES;
10742 		ipw_add_cck_scan_rates(rates, LIBIPW_CCK_MODULATION,
10743 				       LIBIPW_CCK_DEFAULT_RATES_MASK);
10744 		if (priv->ieee->modulation & LIBIPW_OFDM_MODULATION) {
10745 			ipw_add_ofdm_scan_rates(rates, LIBIPW_CCK_MODULATION,
10746 						LIBIPW_OFDM_DEFAULT_RATES_MASK);
10747 		}
10748 		break;
10749 	}
10750 
10751 	return 0;
10752 }
10753 
10754 static int ipw_config(struct ipw_priv *priv)
10755 {
10756 	/* This is only called from ipw_up, which resets/reloads the firmware
10757 	   so, we don't need to first disable the card before we configure
10758 	   it */
10759 	if (ipw_set_tx_power(priv))
10760 		goto error;
10761 
10762 	/* initialize adapter address */
10763 	if (ipw_send_adapter_address(priv, priv->net_dev->dev_addr))
10764 		goto error;
10765 
10766 	/* set basic system config settings */
10767 	init_sys_config(&priv->sys_config);
10768 
10769 	/* Support Bluetooth if we have BT h/w on board, and user wants to.
10770 	 * Does not support BT priority yet (don't abort or defer our Tx) */
10771 	if (bt_coexist) {
10772 		unsigned char bt_caps = priv->eeprom[EEPROM_SKU_CAPABILITY];
10773 
10774 		if (bt_caps & EEPROM_SKU_CAP_BT_CHANNEL_SIG)
10775 			priv->sys_config.bt_coexistence
10776 			    |= CFG_BT_COEXISTENCE_SIGNAL_CHNL;
10777 		if (bt_caps & EEPROM_SKU_CAP_BT_OOB)
10778 			priv->sys_config.bt_coexistence
10779 			    |= CFG_BT_COEXISTENCE_OOB;
10780 	}
10781 
10782 #ifdef CONFIG_IPW2200_PROMISCUOUS
10783 	if (priv->prom_net_dev && netif_running(priv->prom_net_dev)) {
10784 		priv->sys_config.accept_all_data_frames = 1;
10785 		priv->sys_config.accept_non_directed_frames = 1;
10786 		priv->sys_config.accept_all_mgmt_bcpr = 1;
10787 		priv->sys_config.accept_all_mgmt_frames = 1;
10788 	}
10789 #endif
10790 
10791 	if (priv->ieee->iw_mode == IW_MODE_ADHOC)
10792 		priv->sys_config.answer_broadcast_ssid_probe = 1;
10793 	else
10794 		priv->sys_config.answer_broadcast_ssid_probe = 0;
10795 
10796 	if (ipw_send_system_config(priv))
10797 		goto error;
10798 
10799 	init_supported_rates(priv, &priv->rates);
10800 	if (ipw_send_supported_rates(priv, &priv->rates))
10801 		goto error;
10802 
10803 	/* Set request-to-send threshold */
10804 	if (priv->rts_threshold) {
10805 		if (ipw_send_rts_threshold(priv, priv->rts_threshold))
10806 			goto error;
10807 	}
10808 #ifdef CONFIG_IPW2200_QOS
10809 	IPW_DEBUG_QOS("QoS: call ipw_qos_activate\n");
10810 	ipw_qos_activate(priv, NULL);
10811 #endif				/* CONFIG_IPW2200_QOS */
10812 
10813 	if (ipw_set_random_seed(priv))
10814 		goto error;
10815 
10816 	/* final state transition to the RUN state */
10817 	if (ipw_send_host_complete(priv))
10818 		goto error;
10819 
10820 	priv->status |= STATUS_INIT;
10821 
10822 	ipw_led_init(priv);
10823 	ipw_led_radio_on(priv);
10824 	priv->notif_missed_beacons = 0;
10825 
10826 	/* Set hardware WEP key if it is configured. */
10827 	if ((priv->capability & CAP_PRIVACY_ON) &&
10828 	    (priv->ieee->sec.level == SEC_LEVEL_1) &&
10829 	    !(priv->ieee->host_encrypt || priv->ieee->host_decrypt))
10830 		ipw_set_hwcrypto_keys(priv);
10831 
10832 	return 0;
10833 
10834       error:
10835 	return -EIO;
10836 }
10837 
10838 /*
10839  * NOTE:
10840  *
10841  * These tables have been tested in conjunction with the
10842  * Intel PRO/Wireless 2200BG and 2915ABG Network Connection Adapters.
10843  *
10844  * Altering this values, using it on other hardware, or in geographies
10845  * not intended for resale of the above mentioned Intel adapters has
10846  * not been tested.
10847  *
10848  * Remember to update the table in README.ipw2200 when changing this
10849  * table.
10850  *
10851  */
10852 static const struct libipw_geo ipw_geos[] = {
10853 	{			/* Restricted */
10854 	 "---",
10855 	 .bg_channels = 11,
10856 	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10857 		{2427, 4}, {2432, 5}, {2437, 6},
10858 		{2442, 7}, {2447, 8}, {2452, 9},
10859 		{2457, 10}, {2462, 11}},
10860 	 },
10861 
10862 	{			/* Custom US/Canada */
10863 	 "ZZF",
10864 	 .bg_channels = 11,
10865 	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10866 		{2427, 4}, {2432, 5}, {2437, 6},
10867 		{2442, 7}, {2447, 8}, {2452, 9},
10868 		{2457, 10}, {2462, 11}},
10869 	 .a_channels = 8,
10870 	 .a = {{5180, 36},
10871 	       {5200, 40},
10872 	       {5220, 44},
10873 	       {5240, 48},
10874 	       {5260, 52, LIBIPW_CH_PASSIVE_ONLY},
10875 	       {5280, 56, LIBIPW_CH_PASSIVE_ONLY},
10876 	       {5300, 60, LIBIPW_CH_PASSIVE_ONLY},
10877 	       {5320, 64, LIBIPW_CH_PASSIVE_ONLY}},
10878 	 },
10879 
10880 	{			/* Rest of World */
10881 	 "ZZD",
10882 	 .bg_channels = 13,
10883 	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10884 		{2427, 4}, {2432, 5}, {2437, 6},
10885 		{2442, 7}, {2447, 8}, {2452, 9},
10886 		{2457, 10}, {2462, 11}, {2467, 12},
10887 		{2472, 13}},
10888 	 },
10889 
10890 	{			/* Custom USA & Europe & High */
10891 	 "ZZA",
10892 	 .bg_channels = 11,
10893 	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10894 		{2427, 4}, {2432, 5}, {2437, 6},
10895 		{2442, 7}, {2447, 8}, {2452, 9},
10896 		{2457, 10}, {2462, 11}},
10897 	 .a_channels = 13,
10898 	 .a = {{5180, 36},
10899 	       {5200, 40},
10900 	       {5220, 44},
10901 	       {5240, 48},
10902 	       {5260, 52, LIBIPW_CH_PASSIVE_ONLY},
10903 	       {5280, 56, LIBIPW_CH_PASSIVE_ONLY},
10904 	       {5300, 60, LIBIPW_CH_PASSIVE_ONLY},
10905 	       {5320, 64, LIBIPW_CH_PASSIVE_ONLY},
10906 	       {5745, 149},
10907 	       {5765, 153},
10908 	       {5785, 157},
10909 	       {5805, 161},
10910 	       {5825, 165}},
10911 	 },
10912 
10913 	{			/* Custom NA & Europe */
10914 	 "ZZB",
10915 	 .bg_channels = 11,
10916 	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10917 		{2427, 4}, {2432, 5}, {2437, 6},
10918 		{2442, 7}, {2447, 8}, {2452, 9},
10919 		{2457, 10}, {2462, 11}},
10920 	 .a_channels = 13,
10921 	 .a = {{5180, 36},
10922 	       {5200, 40},
10923 	       {5220, 44},
10924 	       {5240, 48},
10925 	       {5260, 52, LIBIPW_CH_PASSIVE_ONLY},
10926 	       {5280, 56, LIBIPW_CH_PASSIVE_ONLY},
10927 	       {5300, 60, LIBIPW_CH_PASSIVE_ONLY},
10928 	       {5320, 64, LIBIPW_CH_PASSIVE_ONLY},
10929 	       {5745, 149, LIBIPW_CH_PASSIVE_ONLY},
10930 	       {5765, 153, LIBIPW_CH_PASSIVE_ONLY},
10931 	       {5785, 157, LIBIPW_CH_PASSIVE_ONLY},
10932 	       {5805, 161, LIBIPW_CH_PASSIVE_ONLY},
10933 	       {5825, 165, LIBIPW_CH_PASSIVE_ONLY}},
10934 	 },
10935 
10936 	{			/* Custom Japan */
10937 	 "ZZC",
10938 	 .bg_channels = 11,
10939 	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10940 		{2427, 4}, {2432, 5}, {2437, 6},
10941 		{2442, 7}, {2447, 8}, {2452, 9},
10942 		{2457, 10}, {2462, 11}},
10943 	 .a_channels = 4,
10944 	 .a = {{5170, 34}, {5190, 38},
10945 	       {5210, 42}, {5230, 46}},
10946 	 },
10947 
10948 	{			/* Custom */
10949 	 "ZZM",
10950 	 .bg_channels = 11,
10951 	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10952 		{2427, 4}, {2432, 5}, {2437, 6},
10953 		{2442, 7}, {2447, 8}, {2452, 9},
10954 		{2457, 10}, {2462, 11}},
10955 	 },
10956 
10957 	{			/* Europe */
10958 	 "ZZE",
10959 	 .bg_channels = 13,
10960 	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10961 		{2427, 4}, {2432, 5}, {2437, 6},
10962 		{2442, 7}, {2447, 8}, {2452, 9},
10963 		{2457, 10}, {2462, 11}, {2467, 12},
10964 		{2472, 13}},
10965 	 .a_channels = 19,
10966 	 .a = {{5180, 36},
10967 	       {5200, 40},
10968 	       {5220, 44},
10969 	       {5240, 48},
10970 	       {5260, 52, LIBIPW_CH_PASSIVE_ONLY},
10971 	       {5280, 56, LIBIPW_CH_PASSIVE_ONLY},
10972 	       {5300, 60, LIBIPW_CH_PASSIVE_ONLY},
10973 	       {5320, 64, LIBIPW_CH_PASSIVE_ONLY},
10974 	       {5500, 100, LIBIPW_CH_PASSIVE_ONLY},
10975 	       {5520, 104, LIBIPW_CH_PASSIVE_ONLY},
10976 	       {5540, 108, LIBIPW_CH_PASSIVE_ONLY},
10977 	       {5560, 112, LIBIPW_CH_PASSIVE_ONLY},
10978 	       {5580, 116, LIBIPW_CH_PASSIVE_ONLY},
10979 	       {5600, 120, LIBIPW_CH_PASSIVE_ONLY},
10980 	       {5620, 124, LIBIPW_CH_PASSIVE_ONLY},
10981 	       {5640, 128, LIBIPW_CH_PASSIVE_ONLY},
10982 	       {5660, 132, LIBIPW_CH_PASSIVE_ONLY},
10983 	       {5680, 136, LIBIPW_CH_PASSIVE_ONLY},
10984 	       {5700, 140, LIBIPW_CH_PASSIVE_ONLY}},
10985 	 },
10986 
10987 	{			/* Custom Japan */
10988 	 "ZZJ",
10989 	 .bg_channels = 14,
10990 	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10991 		{2427, 4}, {2432, 5}, {2437, 6},
10992 		{2442, 7}, {2447, 8}, {2452, 9},
10993 		{2457, 10}, {2462, 11}, {2467, 12},
10994 		{2472, 13}, {2484, 14, LIBIPW_CH_B_ONLY}},
10995 	 .a_channels = 4,
10996 	 .a = {{5170, 34}, {5190, 38},
10997 	       {5210, 42}, {5230, 46}},
10998 	 },
10999 
11000 	{			/* Rest of World */
11001 	 "ZZR",
11002 	 .bg_channels = 14,
11003 	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
11004 		{2427, 4}, {2432, 5}, {2437, 6},
11005 		{2442, 7}, {2447, 8}, {2452, 9},
11006 		{2457, 10}, {2462, 11}, {2467, 12},
11007 		{2472, 13}, {2484, 14, LIBIPW_CH_B_ONLY |
11008 			     LIBIPW_CH_PASSIVE_ONLY}},
11009 	 },
11010 
11011 	{			/* High Band */
11012 	 "ZZH",
11013 	 .bg_channels = 13,
11014 	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
11015 		{2427, 4}, {2432, 5}, {2437, 6},
11016 		{2442, 7}, {2447, 8}, {2452, 9},
11017 		{2457, 10}, {2462, 11},
11018 		{2467, 12, LIBIPW_CH_PASSIVE_ONLY},
11019 		{2472, 13, LIBIPW_CH_PASSIVE_ONLY}},
11020 	 .a_channels = 4,
11021 	 .a = {{5745, 149}, {5765, 153},
11022 	       {5785, 157}, {5805, 161}},
11023 	 },
11024 
11025 	{			/* Custom Europe */
11026 	 "ZZG",
11027 	 .bg_channels = 13,
11028 	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
11029 		{2427, 4}, {2432, 5}, {2437, 6},
11030 		{2442, 7}, {2447, 8}, {2452, 9},
11031 		{2457, 10}, {2462, 11},
11032 		{2467, 12}, {2472, 13}},
11033 	 .a_channels = 4,
11034 	 .a = {{5180, 36}, {5200, 40},
11035 	       {5220, 44}, {5240, 48}},
11036 	 },
11037 
11038 	{			/* Europe */
11039 	 "ZZK",
11040 	 .bg_channels = 13,
11041 	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
11042 		{2427, 4}, {2432, 5}, {2437, 6},
11043 		{2442, 7}, {2447, 8}, {2452, 9},
11044 		{2457, 10}, {2462, 11},
11045 		{2467, 12, LIBIPW_CH_PASSIVE_ONLY},
11046 		{2472, 13, LIBIPW_CH_PASSIVE_ONLY}},
11047 	 .a_channels = 24,
11048 	 .a = {{5180, 36, LIBIPW_CH_PASSIVE_ONLY},
11049 	       {5200, 40, LIBIPW_CH_PASSIVE_ONLY},
11050 	       {5220, 44, LIBIPW_CH_PASSIVE_ONLY},
11051 	       {5240, 48, LIBIPW_CH_PASSIVE_ONLY},
11052 	       {5260, 52, LIBIPW_CH_PASSIVE_ONLY},
11053 	       {5280, 56, LIBIPW_CH_PASSIVE_ONLY},
11054 	       {5300, 60, LIBIPW_CH_PASSIVE_ONLY},
11055 	       {5320, 64, LIBIPW_CH_PASSIVE_ONLY},
11056 	       {5500, 100, LIBIPW_CH_PASSIVE_ONLY},
11057 	       {5520, 104, LIBIPW_CH_PASSIVE_ONLY},
11058 	       {5540, 108, LIBIPW_CH_PASSIVE_ONLY},
11059 	       {5560, 112, LIBIPW_CH_PASSIVE_ONLY},
11060 	       {5580, 116, LIBIPW_CH_PASSIVE_ONLY},
11061 	       {5600, 120, LIBIPW_CH_PASSIVE_ONLY},
11062 	       {5620, 124, LIBIPW_CH_PASSIVE_ONLY},
11063 	       {5640, 128, LIBIPW_CH_PASSIVE_ONLY},
11064 	       {5660, 132, LIBIPW_CH_PASSIVE_ONLY},
11065 	       {5680, 136, LIBIPW_CH_PASSIVE_ONLY},
11066 	       {5700, 140, LIBIPW_CH_PASSIVE_ONLY},
11067 	       {5745, 149, LIBIPW_CH_PASSIVE_ONLY},
11068 	       {5765, 153, LIBIPW_CH_PASSIVE_ONLY},
11069 	       {5785, 157, LIBIPW_CH_PASSIVE_ONLY},
11070 	       {5805, 161, LIBIPW_CH_PASSIVE_ONLY},
11071 	       {5825, 165, LIBIPW_CH_PASSIVE_ONLY}},
11072 	 },
11073 
11074 	{			/* Europe */
11075 	 "ZZL",
11076 	 .bg_channels = 11,
11077 	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
11078 		{2427, 4}, {2432, 5}, {2437, 6},
11079 		{2442, 7}, {2447, 8}, {2452, 9},
11080 		{2457, 10}, {2462, 11}},
11081 	 .a_channels = 13,
11082 	 .a = {{5180, 36, LIBIPW_CH_PASSIVE_ONLY},
11083 	       {5200, 40, LIBIPW_CH_PASSIVE_ONLY},
11084 	       {5220, 44, LIBIPW_CH_PASSIVE_ONLY},
11085 	       {5240, 48, LIBIPW_CH_PASSIVE_ONLY},
11086 	       {5260, 52, LIBIPW_CH_PASSIVE_ONLY},
11087 	       {5280, 56, LIBIPW_CH_PASSIVE_ONLY},
11088 	       {5300, 60, LIBIPW_CH_PASSIVE_ONLY},
11089 	       {5320, 64, LIBIPW_CH_PASSIVE_ONLY},
11090 	       {5745, 149, LIBIPW_CH_PASSIVE_ONLY},
11091 	       {5765, 153, LIBIPW_CH_PASSIVE_ONLY},
11092 	       {5785, 157, LIBIPW_CH_PASSIVE_ONLY},
11093 	       {5805, 161, LIBIPW_CH_PASSIVE_ONLY},
11094 	       {5825, 165, LIBIPW_CH_PASSIVE_ONLY}},
11095 	 }
11096 };
11097 
11098 static void ipw_set_geo(struct ipw_priv *priv)
11099 {
11100 	int j;
11101 
11102 	for (j = 0; j < ARRAY_SIZE(ipw_geos); j++) {
11103 		if (!memcmp(&priv->eeprom[EEPROM_COUNTRY_CODE],
11104 			    ipw_geos[j].name, 3))
11105 			break;
11106 	}
11107 
11108 	if (j == ARRAY_SIZE(ipw_geos)) {
11109 		IPW_WARNING("SKU [%c%c%c] not recognized.\n",
11110 			    priv->eeprom[EEPROM_COUNTRY_CODE + 0],
11111 			    priv->eeprom[EEPROM_COUNTRY_CODE + 1],
11112 			    priv->eeprom[EEPROM_COUNTRY_CODE + 2]);
11113 		j = 0;
11114 	}
11115 
11116 	libipw_set_geo(priv->ieee, &ipw_geos[j]);
11117 }
11118 
11119 #define MAX_HW_RESTARTS 5
11120 static int ipw_up(struct ipw_priv *priv)
11121 {
11122 	int rc, i;
11123 
11124 	/* Age scan list entries found before suspend */
11125 	if (priv->suspend_time) {
11126 		libipw_networks_age(priv->ieee, priv->suspend_time);
11127 		priv->suspend_time = 0;
11128 	}
11129 
11130 	if (priv->status & STATUS_EXIT_PENDING)
11131 		return -EIO;
11132 
11133 	if (cmdlog && !priv->cmdlog) {
11134 		priv->cmdlog = kcalloc(cmdlog, sizeof(*priv->cmdlog),
11135 				       GFP_KERNEL);
11136 		if (priv->cmdlog == NULL) {
11137 			IPW_ERROR("Error allocating %d command log entries.\n",
11138 				  cmdlog);
11139 			return -ENOMEM;
11140 		} else {
11141 			priv->cmdlog_len = cmdlog;
11142 		}
11143 	}
11144 
11145 	for (i = 0; i < MAX_HW_RESTARTS; i++) {
11146 		/* Load the microcode, firmware, and eeprom.
11147 		 * Also start the clocks. */
11148 		rc = ipw_load(priv);
11149 		if (rc) {
11150 			IPW_ERROR("Unable to load firmware: %d\n", rc);
11151 			return rc;
11152 		}
11153 
11154 		ipw_init_ordinals(priv);
11155 		if (!(priv->config & CFG_CUSTOM_MAC))
11156 			eeprom_parse_mac(priv, priv->mac_addr);
11157 		eth_hw_addr_set(priv->net_dev, priv->mac_addr);
11158 
11159 		ipw_set_geo(priv);
11160 
11161 		if (priv->status & STATUS_RF_KILL_SW) {
11162 			IPW_WARNING("Radio disabled by module parameter.\n");
11163 			return 0;
11164 		} else if (rf_kill_active(priv)) {
11165 			IPW_WARNING("Radio Frequency Kill Switch is On:\n"
11166 				    "Kill switch must be turned off for "
11167 				    "wireless networking to work.\n");
11168 			schedule_delayed_work(&priv->rf_kill, 2 * HZ);
11169 			return 0;
11170 		}
11171 
11172 		rc = ipw_config(priv);
11173 		if (!rc) {
11174 			IPW_DEBUG_INFO("Configured device on count %i\n", i);
11175 
11176 			/* If configure to try and auto-associate, kick
11177 			 * off a scan. */
11178 			schedule_delayed_work(&priv->request_scan, 0);
11179 
11180 			return 0;
11181 		}
11182 
11183 		IPW_DEBUG_INFO("Device configuration failed: 0x%08X\n", rc);
11184 		IPW_DEBUG_INFO("Failed to config device on retry %d of %d\n",
11185 			       i, MAX_HW_RESTARTS);
11186 
11187 		/* We had an error bringing up the hardware, so take it
11188 		 * all the way back down so we can try again */
11189 		ipw_down(priv);
11190 	}
11191 
11192 	/* tried to restart and config the device for as long as our
11193 	 * patience could withstand */
11194 	IPW_ERROR("Unable to initialize device after %d attempts.\n", i);
11195 
11196 	return -EIO;
11197 }
11198 
11199 static void ipw_bg_up(struct work_struct *work)
11200 {
11201 	struct ipw_priv *priv =
11202 		container_of(work, struct ipw_priv, up);
11203 	mutex_lock(&priv->mutex);
11204 	ipw_up(priv);
11205 	mutex_unlock(&priv->mutex);
11206 }
11207 
11208 static void ipw_deinit(struct ipw_priv *priv)
11209 {
11210 	int i;
11211 
11212 	if (priv->status & STATUS_SCANNING) {
11213 		IPW_DEBUG_INFO("Aborting scan during shutdown.\n");
11214 		ipw_abort_scan(priv);
11215 	}
11216 
11217 	if (priv->status & STATUS_ASSOCIATED) {
11218 		IPW_DEBUG_INFO("Disassociating during shutdown.\n");
11219 		ipw_disassociate(priv);
11220 	}
11221 
11222 	ipw_led_shutdown(priv);
11223 
11224 	/* Wait up to 1s for status to change to not scanning and not
11225 	 * associated (disassociation can take a while for a ful 802.11
11226 	 * exchange */
11227 	for (i = 1000; i && (priv->status &
11228 			     (STATUS_DISASSOCIATING |
11229 			      STATUS_ASSOCIATED | STATUS_SCANNING)); i--)
11230 		udelay(10);
11231 
11232 	if (priv->status & (STATUS_DISASSOCIATING |
11233 			    STATUS_ASSOCIATED | STATUS_SCANNING))
11234 		IPW_DEBUG_INFO("Still associated or scanning...\n");
11235 	else
11236 		IPW_DEBUG_INFO("Took %dms to de-init\n", 1000 - i);
11237 
11238 	/* Attempt to disable the card */
11239 	ipw_send_card_disable(priv, 0);
11240 
11241 	priv->status &= ~STATUS_INIT;
11242 }
11243 
11244 static void ipw_down(struct ipw_priv *priv)
11245 {
11246 	int exit_pending = priv->status & STATUS_EXIT_PENDING;
11247 
11248 	priv->status |= STATUS_EXIT_PENDING;
11249 
11250 	if (ipw_is_init(priv))
11251 		ipw_deinit(priv);
11252 
11253 	/* Wipe out the EXIT_PENDING status bit if we are not actually
11254 	 * exiting the module */
11255 	if (!exit_pending)
11256 		priv->status &= ~STATUS_EXIT_PENDING;
11257 
11258 	/* tell the device to stop sending interrupts */
11259 	ipw_disable_interrupts(priv);
11260 
11261 	/* Clear all bits but the RF Kill */
11262 	priv->status &= STATUS_RF_KILL_MASK | STATUS_EXIT_PENDING;
11263 	netif_carrier_off(priv->net_dev);
11264 
11265 	ipw_stop_nic(priv);
11266 
11267 	ipw_led_radio_off(priv);
11268 }
11269 
11270 static void ipw_bg_down(struct work_struct *work)
11271 {
11272 	struct ipw_priv *priv =
11273 		container_of(work, struct ipw_priv, down);
11274 	mutex_lock(&priv->mutex);
11275 	ipw_down(priv);
11276 	mutex_unlock(&priv->mutex);
11277 }
11278 
11279 static int ipw_wdev_init(struct net_device *dev)
11280 {
11281 	int i, rc = 0;
11282 	struct ipw_priv *priv = libipw_priv(dev);
11283 	const struct libipw_geo *geo = libipw_get_geo(priv->ieee);
11284 	struct wireless_dev *wdev = &priv->ieee->wdev;
11285 
11286 	memcpy(wdev->wiphy->perm_addr, priv->mac_addr, ETH_ALEN);
11287 
11288 	/* fill-out priv->ieee->bg_band */
11289 	if (geo->bg_channels) {
11290 		struct ieee80211_supported_band *bg_band = &priv->ieee->bg_band;
11291 
11292 		bg_band->band = NL80211_BAND_2GHZ;
11293 		bg_band->n_channels = geo->bg_channels;
11294 		bg_band->channels = kcalloc(geo->bg_channels,
11295 					    sizeof(struct ieee80211_channel),
11296 					    GFP_KERNEL);
11297 		if (!bg_band->channels) {
11298 			rc = -ENOMEM;
11299 			goto out;
11300 		}
11301 		/* translate geo->bg to bg_band.channels */
11302 		for (i = 0; i < geo->bg_channels; i++) {
11303 			bg_band->channels[i].band = NL80211_BAND_2GHZ;
11304 			bg_band->channels[i].center_freq = geo->bg[i].freq;
11305 			bg_band->channels[i].hw_value = geo->bg[i].channel;
11306 			bg_band->channels[i].max_power = geo->bg[i].max_power;
11307 			if (geo->bg[i].flags & LIBIPW_CH_PASSIVE_ONLY)
11308 				bg_band->channels[i].flags |=
11309 					IEEE80211_CHAN_NO_IR;
11310 			if (geo->bg[i].flags & LIBIPW_CH_NO_IBSS)
11311 				bg_band->channels[i].flags |=
11312 					IEEE80211_CHAN_NO_IR;
11313 			if (geo->bg[i].flags & LIBIPW_CH_RADAR_DETECT)
11314 				bg_band->channels[i].flags |=
11315 					IEEE80211_CHAN_RADAR;
11316 			/* No equivalent for LIBIPW_CH_80211H_RULES,
11317 			   LIBIPW_CH_UNIFORM_SPREADING, or
11318 			   LIBIPW_CH_B_ONLY... */
11319 		}
11320 		/* point at bitrate info */
11321 		bg_band->bitrates = ipw2200_bg_rates;
11322 		bg_band->n_bitrates = ipw2200_num_bg_rates;
11323 
11324 		wdev->wiphy->bands[NL80211_BAND_2GHZ] = bg_band;
11325 	}
11326 
11327 	/* fill-out priv->ieee->a_band */
11328 	if (geo->a_channels) {
11329 		struct ieee80211_supported_band *a_band = &priv->ieee->a_band;
11330 
11331 		a_band->band = NL80211_BAND_5GHZ;
11332 		a_band->n_channels = geo->a_channels;
11333 		a_band->channels = kcalloc(geo->a_channels,
11334 					   sizeof(struct ieee80211_channel),
11335 					   GFP_KERNEL);
11336 		if (!a_band->channels) {
11337 			rc = -ENOMEM;
11338 			goto out;
11339 		}
11340 		/* translate geo->a to a_band.channels */
11341 		for (i = 0; i < geo->a_channels; i++) {
11342 			a_band->channels[i].band = NL80211_BAND_5GHZ;
11343 			a_band->channels[i].center_freq = geo->a[i].freq;
11344 			a_band->channels[i].hw_value = geo->a[i].channel;
11345 			a_band->channels[i].max_power = geo->a[i].max_power;
11346 			if (geo->a[i].flags & LIBIPW_CH_PASSIVE_ONLY)
11347 				a_band->channels[i].flags |=
11348 					IEEE80211_CHAN_NO_IR;
11349 			if (geo->a[i].flags & LIBIPW_CH_NO_IBSS)
11350 				a_band->channels[i].flags |=
11351 					IEEE80211_CHAN_NO_IR;
11352 			if (geo->a[i].flags & LIBIPW_CH_RADAR_DETECT)
11353 				a_band->channels[i].flags |=
11354 					IEEE80211_CHAN_RADAR;
11355 			/* No equivalent for LIBIPW_CH_80211H_RULES,
11356 			   LIBIPW_CH_UNIFORM_SPREADING, or
11357 			   LIBIPW_CH_B_ONLY... */
11358 		}
11359 		/* point at bitrate info */
11360 		a_band->bitrates = ipw2200_a_rates;
11361 		a_band->n_bitrates = ipw2200_num_a_rates;
11362 
11363 		wdev->wiphy->bands[NL80211_BAND_5GHZ] = a_band;
11364 	}
11365 
11366 	wdev->wiphy->cipher_suites = ipw_cipher_suites;
11367 	wdev->wiphy->n_cipher_suites = ARRAY_SIZE(ipw_cipher_suites);
11368 
11369 	set_wiphy_dev(wdev->wiphy, &priv->pci_dev->dev);
11370 
11371 	/* With that information in place, we can now register the wiphy... */
11372 	rc = wiphy_register(wdev->wiphy);
11373 	if (rc)
11374 		goto out;
11375 
11376 	return 0;
11377 out:
11378 	kfree(priv->ieee->a_band.channels);
11379 	kfree(priv->ieee->bg_band.channels);
11380 	return rc;
11381 }
11382 
11383 /* PCI driver stuff */
11384 static const struct pci_device_id card_ids[] = {
11385 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2701, 0, 0, 0},
11386 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2702, 0, 0, 0},
11387 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2711, 0, 0, 0},
11388 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2712, 0, 0, 0},
11389 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2721, 0, 0, 0},
11390 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2722, 0, 0, 0},
11391 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2731, 0, 0, 0},
11392 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2732, 0, 0, 0},
11393 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2741, 0, 0, 0},
11394 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x103c, 0x2741, 0, 0, 0},
11395 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2742, 0, 0, 0},
11396 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2751, 0, 0, 0},
11397 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2752, 0, 0, 0},
11398 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2753, 0, 0, 0},
11399 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2754, 0, 0, 0},
11400 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2761, 0, 0, 0},
11401 	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2762, 0, 0, 0},
11402 	{PCI_VDEVICE(INTEL, 0x104f), 0},
11403 	{PCI_VDEVICE(INTEL, 0x4220), 0},	/* BG */
11404 	{PCI_VDEVICE(INTEL, 0x4221), 0},	/* BG */
11405 	{PCI_VDEVICE(INTEL, 0x4223), 0},	/* ABG */
11406 	{PCI_VDEVICE(INTEL, 0x4224), 0},	/* ABG */
11407 
11408 	/* required last entry */
11409 	{0,}
11410 };
11411 
11412 MODULE_DEVICE_TABLE(pci, card_ids);
11413 
11414 static struct attribute *ipw_sysfs_entries[] = {
11415 	&dev_attr_rf_kill.attr,
11416 	&dev_attr_direct_dword.attr,
11417 	&dev_attr_indirect_byte.attr,
11418 	&dev_attr_indirect_dword.attr,
11419 	&dev_attr_mem_gpio_reg.attr,
11420 	&dev_attr_command_event_reg.attr,
11421 	&dev_attr_nic_type.attr,
11422 	&dev_attr_status.attr,
11423 	&dev_attr_cfg.attr,
11424 	&dev_attr_error.attr,
11425 	&dev_attr_event_log.attr,
11426 	&dev_attr_cmd_log.attr,
11427 	&dev_attr_eeprom_delay.attr,
11428 	&dev_attr_ucode_version.attr,
11429 	&dev_attr_rtc.attr,
11430 	&dev_attr_scan_age.attr,
11431 	&dev_attr_led.attr,
11432 	&dev_attr_speed_scan.attr,
11433 	&dev_attr_net_stats.attr,
11434 	&dev_attr_channels.attr,
11435 #ifdef CONFIG_IPW2200_PROMISCUOUS
11436 	&dev_attr_rtap_iface.attr,
11437 	&dev_attr_rtap_filter.attr,
11438 #endif
11439 	NULL
11440 };
11441 
11442 static const struct attribute_group ipw_attribute_group = {
11443 	.name = NULL,		/* put in device directory */
11444 	.attrs = ipw_sysfs_entries,
11445 };
11446 
11447 #ifdef CONFIG_IPW2200_PROMISCUOUS
11448 static int ipw_prom_open(struct net_device *dev)
11449 {
11450 	struct ipw_prom_priv *prom_priv = libipw_priv(dev);
11451 	struct ipw_priv *priv = prom_priv->priv;
11452 
11453 	IPW_DEBUG_INFO("prom dev->open\n");
11454 	netif_carrier_off(dev);
11455 
11456 	if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
11457 		priv->sys_config.accept_all_data_frames = 1;
11458 		priv->sys_config.accept_non_directed_frames = 1;
11459 		priv->sys_config.accept_all_mgmt_bcpr = 1;
11460 		priv->sys_config.accept_all_mgmt_frames = 1;
11461 
11462 		ipw_send_system_config(priv);
11463 	}
11464 
11465 	return 0;
11466 }
11467 
11468 static int ipw_prom_stop(struct net_device *dev)
11469 {
11470 	struct ipw_prom_priv *prom_priv = libipw_priv(dev);
11471 	struct ipw_priv *priv = prom_priv->priv;
11472 
11473 	IPW_DEBUG_INFO("prom dev->stop\n");
11474 
11475 	if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
11476 		priv->sys_config.accept_all_data_frames = 0;
11477 		priv->sys_config.accept_non_directed_frames = 0;
11478 		priv->sys_config.accept_all_mgmt_bcpr = 0;
11479 		priv->sys_config.accept_all_mgmt_frames = 0;
11480 
11481 		ipw_send_system_config(priv);
11482 	}
11483 
11484 	return 0;
11485 }
11486 
11487 static netdev_tx_t ipw_prom_hard_start_xmit(struct sk_buff *skb,
11488 					    struct net_device *dev)
11489 {
11490 	IPW_DEBUG_INFO("prom dev->xmit\n");
11491 	dev_kfree_skb(skb);
11492 	return NETDEV_TX_OK;
11493 }
11494 
11495 static const struct net_device_ops ipw_prom_netdev_ops = {
11496 	.ndo_open 		= ipw_prom_open,
11497 	.ndo_stop		= ipw_prom_stop,
11498 	.ndo_start_xmit		= ipw_prom_hard_start_xmit,
11499 	.ndo_set_mac_address 	= eth_mac_addr,
11500 	.ndo_validate_addr	= eth_validate_addr,
11501 };
11502 
11503 static int ipw_prom_alloc(struct ipw_priv *priv)
11504 {
11505 	int rc = 0;
11506 
11507 	if (priv->prom_net_dev)
11508 		return -EPERM;
11509 
11510 	priv->prom_net_dev = alloc_libipw(sizeof(struct ipw_prom_priv), 1);
11511 	if (priv->prom_net_dev == NULL)
11512 		return -ENOMEM;
11513 
11514 	priv->prom_priv = libipw_priv(priv->prom_net_dev);
11515 	priv->prom_priv->ieee = netdev_priv(priv->prom_net_dev);
11516 	priv->prom_priv->priv = priv;
11517 
11518 	strcpy(priv->prom_net_dev->name, "rtap%d");
11519 	eth_hw_addr_set(priv->prom_net_dev, priv->mac_addr);
11520 
11521 	priv->prom_net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
11522 	priv->prom_net_dev->netdev_ops = &ipw_prom_netdev_ops;
11523 
11524 	priv->prom_net_dev->min_mtu = 68;
11525 	priv->prom_net_dev->max_mtu = LIBIPW_DATA_LEN;
11526 
11527 	priv->prom_priv->ieee->iw_mode = IW_MODE_MONITOR;
11528 	SET_NETDEV_DEV(priv->prom_net_dev, &priv->pci_dev->dev);
11529 
11530 	rc = register_netdev(priv->prom_net_dev);
11531 	if (rc) {
11532 		free_libipw(priv->prom_net_dev, 1);
11533 		priv->prom_net_dev = NULL;
11534 		return rc;
11535 	}
11536 
11537 	return 0;
11538 }
11539 
11540 static void ipw_prom_free(struct ipw_priv *priv)
11541 {
11542 	if (!priv->prom_net_dev)
11543 		return;
11544 
11545 	unregister_netdev(priv->prom_net_dev);
11546 	free_libipw(priv->prom_net_dev, 1);
11547 
11548 	priv->prom_net_dev = NULL;
11549 }
11550 
11551 #endif
11552 
11553 static const struct net_device_ops ipw_netdev_ops = {
11554 	.ndo_open		= ipw_net_open,
11555 	.ndo_stop		= ipw_net_stop,
11556 	.ndo_set_rx_mode	= ipw_net_set_multicast_list,
11557 	.ndo_set_mac_address	= ipw_net_set_mac_address,
11558 	.ndo_start_xmit		= libipw_xmit,
11559 	.ndo_validate_addr	= eth_validate_addr,
11560 };
11561 
11562 static int ipw_pci_probe(struct pci_dev *pdev,
11563 				   const struct pci_device_id *ent)
11564 {
11565 	int err = 0;
11566 	struct net_device *net_dev;
11567 	void __iomem *base;
11568 	u32 length, val;
11569 	struct ipw_priv *priv;
11570 	int i;
11571 
11572 	net_dev = alloc_libipw(sizeof(struct ipw_priv), 0);
11573 	if (net_dev == NULL) {
11574 		err = -ENOMEM;
11575 		goto out;
11576 	}
11577 
11578 	priv = libipw_priv(net_dev);
11579 	priv->ieee = netdev_priv(net_dev);
11580 
11581 	priv->net_dev = net_dev;
11582 	priv->pci_dev = pdev;
11583 	ipw_debug_level = debug;
11584 	spin_lock_init(&priv->irq_lock);
11585 	spin_lock_init(&priv->lock);
11586 	for (i = 0; i < IPW_IBSS_MAC_HASH_SIZE; i++)
11587 		INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
11588 
11589 	mutex_init(&priv->mutex);
11590 	if (pci_enable_device(pdev)) {
11591 		err = -ENODEV;
11592 		goto out_free_libipw;
11593 	}
11594 
11595 	pci_set_master(pdev);
11596 
11597 	err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
11598 	if (!err)
11599 		err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
11600 	if (err) {
11601 		printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
11602 		goto out_pci_disable_device;
11603 	}
11604 
11605 	pci_set_drvdata(pdev, priv);
11606 
11607 	err = pci_request_regions(pdev, DRV_NAME);
11608 	if (err)
11609 		goto out_pci_disable_device;
11610 
11611 	/* We disable the RETRY_TIMEOUT register (0x41) to keep
11612 	 * PCI Tx retries from interfering with C3 CPU state */
11613 	pci_read_config_dword(pdev, 0x40, &val);
11614 	if ((val & 0x0000ff00) != 0)
11615 		pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
11616 
11617 	length = pci_resource_len(pdev, 0);
11618 	priv->hw_len = length;
11619 
11620 	base = pci_ioremap_bar(pdev, 0);
11621 	if (!base) {
11622 		err = -ENODEV;
11623 		goto out_pci_release_regions;
11624 	}
11625 
11626 	priv->hw_base = base;
11627 	IPW_DEBUG_INFO("pci_resource_len = 0x%08x\n", length);
11628 	IPW_DEBUG_INFO("pci_resource_base = %p\n", base);
11629 
11630 	ipw_setup_deferred_work(priv);
11631 
11632 	ipw_sw_reset(priv, 1);
11633 
11634 	err = request_irq(pdev->irq, ipw_isr, IRQF_SHARED, DRV_NAME, priv);
11635 	if (err) {
11636 		IPW_ERROR("Error allocating IRQ %d\n", pdev->irq);
11637 		goto out_iounmap;
11638 	}
11639 
11640 	SET_NETDEV_DEV(net_dev, &pdev->dev);
11641 
11642 	mutex_lock(&priv->mutex);
11643 
11644 	priv->ieee->hard_start_xmit = ipw_net_hard_start_xmit;
11645 	priv->ieee->set_security = shim__set_security;
11646 	priv->ieee->is_queue_full = ipw_net_is_queue_full;
11647 
11648 #ifdef CONFIG_IPW2200_QOS
11649 	priv->ieee->is_qos_active = ipw_is_qos_active;
11650 	priv->ieee->handle_probe_response = ipw_handle_beacon;
11651 	priv->ieee->handle_beacon = ipw_handle_probe_response;
11652 	priv->ieee->handle_assoc_response = ipw_handle_assoc_response;
11653 #endif				/* CONFIG_IPW2200_QOS */
11654 
11655 	priv->ieee->perfect_rssi = -20;
11656 	priv->ieee->worst_rssi = -85;
11657 
11658 	net_dev->netdev_ops = &ipw_netdev_ops;
11659 	priv->wireless_data.spy_data = &priv->ieee->spy_data;
11660 	net_dev->wireless_data = &priv->wireless_data;
11661 	net_dev->wireless_handlers = &ipw_wx_handler_def;
11662 	net_dev->ethtool_ops = &ipw_ethtool_ops;
11663 
11664 	net_dev->min_mtu = 68;
11665 	net_dev->max_mtu = LIBIPW_DATA_LEN;
11666 
11667 	err = sysfs_create_group(&pdev->dev.kobj, &ipw_attribute_group);
11668 	if (err) {
11669 		IPW_ERROR("failed to create sysfs device attributes\n");
11670 		mutex_unlock(&priv->mutex);
11671 		goto out_release_irq;
11672 	}
11673 
11674 	if (ipw_up(priv)) {
11675 		mutex_unlock(&priv->mutex);
11676 		err = -EIO;
11677 		goto out_remove_sysfs;
11678 	}
11679 
11680 	mutex_unlock(&priv->mutex);
11681 
11682 	err = ipw_wdev_init(net_dev);
11683 	if (err) {
11684 		IPW_ERROR("failed to register wireless device\n");
11685 		goto out_remove_sysfs;
11686 	}
11687 
11688 	err = register_netdev(net_dev);
11689 	if (err) {
11690 		IPW_ERROR("failed to register network device\n");
11691 		goto out_unregister_wiphy;
11692 	}
11693 
11694 #ifdef CONFIG_IPW2200_PROMISCUOUS
11695 	if (rtap_iface) {
11696 	        err = ipw_prom_alloc(priv);
11697 		if (err) {
11698 			IPW_ERROR("Failed to register promiscuous network "
11699 				  "device (error %d).\n", err);
11700 			unregister_netdev(priv->net_dev);
11701 			goto out_unregister_wiphy;
11702 		}
11703 	}
11704 #endif
11705 
11706 	printk(KERN_INFO DRV_NAME ": Detected geography %s (%d 802.11bg "
11707 	       "channels, %d 802.11a channels)\n",
11708 	       priv->ieee->geo.name, priv->ieee->geo.bg_channels,
11709 	       priv->ieee->geo.a_channels);
11710 
11711 	return 0;
11712 
11713       out_unregister_wiphy:
11714 	wiphy_unregister(priv->ieee->wdev.wiphy);
11715 	kfree(priv->ieee->a_band.channels);
11716 	kfree(priv->ieee->bg_band.channels);
11717       out_remove_sysfs:
11718 	sysfs_remove_group(&pdev->dev.kobj, &ipw_attribute_group);
11719       out_release_irq:
11720 	free_irq(pdev->irq, priv);
11721       out_iounmap:
11722 	iounmap(priv->hw_base);
11723       out_pci_release_regions:
11724 	pci_release_regions(pdev);
11725       out_pci_disable_device:
11726 	pci_disable_device(pdev);
11727       out_free_libipw:
11728 	free_libipw(priv->net_dev, 0);
11729       out:
11730 	return err;
11731 }
11732 
11733 static void ipw_pci_remove(struct pci_dev *pdev)
11734 {
11735 	struct ipw_priv *priv = pci_get_drvdata(pdev);
11736 	struct list_head *p, *q;
11737 	int i;
11738 
11739 	if (!priv)
11740 		return;
11741 
11742 	mutex_lock(&priv->mutex);
11743 
11744 	priv->status |= STATUS_EXIT_PENDING;
11745 	ipw_down(priv);
11746 	sysfs_remove_group(&pdev->dev.kobj, &ipw_attribute_group);
11747 
11748 	mutex_unlock(&priv->mutex);
11749 
11750 	unregister_netdev(priv->net_dev);
11751 
11752 	if (priv->rxq) {
11753 		ipw_rx_queue_free(priv, priv->rxq);
11754 		priv->rxq = NULL;
11755 	}
11756 	ipw_tx_queue_free(priv);
11757 
11758 	if (priv->cmdlog) {
11759 		kfree(priv->cmdlog);
11760 		priv->cmdlog = NULL;
11761 	}
11762 
11763 	/* make sure all works are inactive */
11764 	cancel_delayed_work_sync(&priv->adhoc_check);
11765 	cancel_work_sync(&priv->associate);
11766 	cancel_work_sync(&priv->disassociate);
11767 	cancel_work_sync(&priv->system_config);
11768 	cancel_work_sync(&priv->rx_replenish);
11769 	cancel_work_sync(&priv->adapter_restart);
11770 	cancel_delayed_work_sync(&priv->rf_kill);
11771 	cancel_work_sync(&priv->up);
11772 	cancel_work_sync(&priv->down);
11773 	cancel_delayed_work_sync(&priv->request_scan);
11774 	cancel_delayed_work_sync(&priv->request_direct_scan);
11775 	cancel_delayed_work_sync(&priv->request_passive_scan);
11776 	cancel_delayed_work_sync(&priv->scan_event);
11777 	cancel_delayed_work_sync(&priv->gather_stats);
11778 	cancel_work_sync(&priv->abort_scan);
11779 	cancel_work_sync(&priv->roam);
11780 	cancel_delayed_work_sync(&priv->scan_check);
11781 	cancel_work_sync(&priv->link_up);
11782 	cancel_work_sync(&priv->link_down);
11783 	cancel_delayed_work_sync(&priv->led_link_on);
11784 	cancel_delayed_work_sync(&priv->led_link_off);
11785 	cancel_delayed_work_sync(&priv->led_act_off);
11786 	cancel_work_sync(&priv->merge_networks);
11787 
11788 	/* Free MAC hash list for ADHOC */
11789 	for (i = 0; i < IPW_IBSS_MAC_HASH_SIZE; i++) {
11790 		list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
11791 			list_del(p);
11792 			kfree(list_entry(p, struct ipw_ibss_seq, list));
11793 		}
11794 	}
11795 
11796 	kfree(priv->error);
11797 	priv->error = NULL;
11798 
11799 #ifdef CONFIG_IPW2200_PROMISCUOUS
11800 	ipw_prom_free(priv);
11801 #endif
11802 
11803 	free_irq(pdev->irq, priv);
11804 	iounmap(priv->hw_base);
11805 	pci_release_regions(pdev);
11806 	pci_disable_device(pdev);
11807 	/* wiphy_unregister needs to be here, before free_libipw */
11808 	wiphy_unregister(priv->ieee->wdev.wiphy);
11809 	kfree(priv->ieee->a_band.channels);
11810 	kfree(priv->ieee->bg_band.channels);
11811 	free_libipw(priv->net_dev, 0);
11812 	free_firmware();
11813 }
11814 
11815 static int __maybe_unused ipw_pci_suspend(struct device *dev_d)
11816 {
11817 	struct ipw_priv *priv = dev_get_drvdata(dev_d);
11818 	struct net_device *dev = priv->net_dev;
11819 
11820 	printk(KERN_INFO "%s: Going into suspend...\n", dev->name);
11821 
11822 	/* Take down the device; powers it off, etc. */
11823 	ipw_down(priv);
11824 
11825 	/* Remove the PRESENT state of the device */
11826 	netif_device_detach(dev);
11827 
11828 	priv->suspend_at = ktime_get_boottime_seconds();
11829 
11830 	return 0;
11831 }
11832 
11833 static int __maybe_unused ipw_pci_resume(struct device *dev_d)
11834 {
11835 	struct pci_dev *pdev = to_pci_dev(dev_d);
11836 	struct ipw_priv *priv = pci_get_drvdata(pdev);
11837 	struct net_device *dev = priv->net_dev;
11838 	u32 val;
11839 
11840 	printk(KERN_INFO "%s: Coming out of suspend...\n", dev->name);
11841 
11842 	/*
11843 	 * Suspend/Resume resets the PCI configuration space, so we have to
11844 	 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
11845 	 * from interfering with C3 CPU state. pci_restore_state won't help
11846 	 * here since it only restores the first 64 bytes pci config header.
11847 	 */
11848 	pci_read_config_dword(pdev, 0x40, &val);
11849 	if ((val & 0x0000ff00) != 0)
11850 		pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
11851 
11852 	/* Set the device back into the PRESENT state; this will also wake
11853 	 * the queue of needed */
11854 	netif_device_attach(dev);
11855 
11856 	priv->suspend_time = ktime_get_boottime_seconds() - priv->suspend_at;
11857 
11858 	/* Bring the device back up */
11859 	schedule_work(&priv->up);
11860 
11861 	return 0;
11862 }
11863 
11864 static void ipw_pci_shutdown(struct pci_dev *pdev)
11865 {
11866 	struct ipw_priv *priv = pci_get_drvdata(pdev);
11867 
11868 	/* Take down the device; powers it off, etc. */
11869 	ipw_down(priv);
11870 
11871 	pci_disable_device(pdev);
11872 }
11873 
11874 static SIMPLE_DEV_PM_OPS(ipw_pci_pm_ops, ipw_pci_suspend, ipw_pci_resume);
11875 
11876 /* driver initialization stuff */
11877 static struct pci_driver ipw_driver = {
11878 	.name = DRV_NAME,
11879 	.id_table = card_ids,
11880 	.probe = ipw_pci_probe,
11881 	.remove = ipw_pci_remove,
11882 	.driver.pm = &ipw_pci_pm_ops,
11883 	.shutdown = ipw_pci_shutdown,
11884 };
11885 
11886 static int __init ipw_init(void)
11887 {
11888 	int ret;
11889 
11890 	printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
11891 	printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
11892 
11893 	ret = pci_register_driver(&ipw_driver);
11894 	if (ret) {
11895 		IPW_ERROR("Unable to initialize PCI module\n");
11896 		return ret;
11897 	}
11898 
11899 	ret = driver_create_file(&ipw_driver.driver, &driver_attr_debug_level);
11900 	if (ret) {
11901 		IPW_ERROR("Unable to create driver sysfs file\n");
11902 		pci_unregister_driver(&ipw_driver);
11903 		return ret;
11904 	}
11905 
11906 	return ret;
11907 }
11908 
11909 static void __exit ipw_exit(void)
11910 {
11911 	driver_remove_file(&ipw_driver.driver, &driver_attr_debug_level);
11912 	pci_unregister_driver(&ipw_driver);
11913 }
11914 
11915 module_param(disable, int, 0444);
11916 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
11917 
11918 module_param(associate, int, 0444);
11919 MODULE_PARM_DESC(associate, "auto associate when scanning (default off)");
11920 
11921 module_param(auto_create, int, 0444);
11922 MODULE_PARM_DESC(auto_create, "auto create adhoc network (default on)");
11923 
11924 module_param_named(led, led_support, int, 0444);
11925 MODULE_PARM_DESC(led, "enable led control on some systems (default 1 on)");
11926 
11927 module_param(debug, int, 0444);
11928 MODULE_PARM_DESC(debug, "debug output mask");
11929 
11930 module_param_named(channel, default_channel, int, 0444);
11931 MODULE_PARM_DESC(channel, "channel to limit associate to (default 0 [ANY])");
11932 
11933 #ifdef CONFIG_IPW2200_PROMISCUOUS
11934 module_param(rtap_iface, int, 0444);
11935 MODULE_PARM_DESC(rtap_iface, "create the rtap interface (1 - create, default 0)");
11936 #endif
11937 
11938 #ifdef CONFIG_IPW2200_QOS
11939 module_param(qos_enable, int, 0444);
11940 MODULE_PARM_DESC(qos_enable, "enable all QoS functionalities");
11941 
11942 module_param(qos_burst_enable, int, 0444);
11943 MODULE_PARM_DESC(qos_burst_enable, "enable QoS burst mode");
11944 
11945 module_param(qos_no_ack_mask, int, 0444);
11946 MODULE_PARM_DESC(qos_no_ack_mask, "mask Tx_Queue to no ack");
11947 
11948 module_param(burst_duration_CCK, int, 0444);
11949 MODULE_PARM_DESC(burst_duration_CCK, "set CCK burst value");
11950 
11951 module_param(burst_duration_OFDM, int, 0444);
11952 MODULE_PARM_DESC(burst_duration_OFDM, "set OFDM burst value");
11953 #endif				/* CONFIG_IPW2200_QOS */
11954 
11955 #ifdef CONFIG_IPW2200_MONITOR
11956 module_param_named(mode, network_mode, int, 0444);
11957 MODULE_PARM_DESC(mode, "network mode (0=BSS,1=IBSS,2=Monitor)");
11958 #else
11959 module_param_named(mode, network_mode, int, 0444);
11960 MODULE_PARM_DESC(mode, "network mode (0=BSS,1=IBSS)");
11961 #endif
11962 
11963 module_param(bt_coexist, int, 0444);
11964 MODULE_PARM_DESC(bt_coexist, "enable bluetooth coexistence (default off)");
11965 
11966 module_param(hwcrypto, int, 0444);
11967 MODULE_PARM_DESC(hwcrypto, "enable hardware crypto (default off)");
11968 
11969 module_param(cmdlog, int, 0444);
11970 MODULE_PARM_DESC(cmdlog,
11971 		 "allocate a ring buffer for logging firmware commands");
11972 
11973 module_param(roaming, int, 0444);
11974 MODULE_PARM_DESC(roaming, "enable roaming support (default on)");
11975 
11976 module_param(antenna, int, 0444);
11977 MODULE_PARM_DESC(antenna, "select antenna 1=Main, 3=Aux, default 0 [both], 2=slow_diversity (choose the one with lower background noise)");
11978 
11979 module_exit(ipw_exit);
11980 module_init(ipw_init);
11981