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