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