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