Catch ipw2200 up to equivelancy with v1.0.1
[pandora-kernel.git] / drivers / net / wireless / ipw2200.c
1 /******************************************************************************
2
3   Copyright(c) 2003 - 2004 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.1"
36 #define DRV_DESCRIPTION "Intel(R) PRO/Wireless 2200/2915 Network Driver"
37 #define DRV_COPYRIGHT   "Copyright(c) 2003-2004 Intel Corporation"
38 #define DRV_VERSION     IPW2200_VERSION
39
40 MODULE_DESCRIPTION(DRV_DESCRIPTION);
41 MODULE_VERSION(DRV_VERSION);
42 MODULE_AUTHOR(DRV_COPYRIGHT);
43 MODULE_LICENSE("GPL");
44
45 static int debug = 0;
46 static int channel = 0;
47 static int mode = 0;
48
49 static u32 ipw_debug_level;
50 static int associate = 1;
51 static int auto_create = 1;
52 static int disable = 0;
53 static const char ipw_modes[] = {
54         'a', 'b', 'g', '?'
55 };
56
57 static void ipw_rx(struct ipw_priv *priv);
58 static int ipw_queue_tx_reclaim(struct ipw_priv *priv,
59                                 struct clx2_tx_queue *txq, int qindex);
60 static int ipw_queue_reset(struct ipw_priv *priv);
61
62 static int ipw_queue_tx_hcmd(struct ipw_priv *priv, int hcmd, void *buf,
63                              int len, int sync);
64
65 static void ipw_tx_queue_free(struct ipw_priv *);
66
67 static struct ipw_rx_queue *ipw_rx_queue_alloc(struct ipw_priv *);
68 static void ipw_rx_queue_free(struct ipw_priv *, struct ipw_rx_queue *);
69 static void ipw_rx_queue_replenish(void *);
70
71 static int ipw_up(struct ipw_priv *);
72 static void ipw_down(struct ipw_priv *);
73 static int ipw_config(struct ipw_priv *);
74 static int init_supported_rates(struct ipw_priv *priv,
75                                 struct ipw_supported_rates *prates);
76
77 static u8 band_b_active_channel[MAX_B_CHANNELS] = {
78         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0
79 };
80 static u8 band_a_active_channel[MAX_A_CHANNELS] = {
81         36, 40, 44, 48, 149, 153, 157, 161, 165, 52, 56, 60, 64, 0
82 };
83
84 static int is_valid_channel(int mode_mask, int channel)
85 {
86         int i;
87
88         if (!channel)
89                 return 0;
90
91         if (mode_mask & IEEE_A)
92                 for (i = 0; i < MAX_A_CHANNELS; i++)
93                         if (band_a_active_channel[i] == channel)
94                                 return IEEE_A;
95
96         if (mode_mask & (IEEE_B | IEEE_G))
97                 for (i = 0; i < MAX_B_CHANNELS; i++)
98                         if (band_b_active_channel[i] == channel)
99                                 return mode_mask & (IEEE_B | IEEE_G);
100
101         return 0;
102 }
103
104 static char *snprint_line(char *buf, size_t count,
105                           const u8 * data, u32 len, u32 ofs)
106 {
107         int out, i, j, l;
108         char c;
109
110         out = snprintf(buf, count, "%08X", ofs);
111
112         for (l = 0, i = 0; i < 2; i++) {
113                 out += snprintf(buf + out, count - out, " ");
114                 for (j = 0; j < 8 && l < len; j++, l++)
115                         out += snprintf(buf + out, count - out, "%02X ",
116                                         data[(i * 8 + j)]);
117                 for (; j < 8; j++)
118                         out += snprintf(buf + out, count - out, "   ");
119         }
120
121         out += snprintf(buf + out, count - out, " ");
122         for (l = 0, i = 0; i < 2; i++) {
123                 out += snprintf(buf + out, count - out, " ");
124                 for (j = 0; j < 8 && l < len; j++, l++) {
125                         c = data[(i * 8 + j)];
126                         if (!isascii(c) || !isprint(c))
127                                 c = '.';
128
129                         out += snprintf(buf + out, count - out, "%c", c);
130                 }
131
132                 for (; j < 8; j++)
133                         out += snprintf(buf + out, count - out, " ");
134         }
135
136         return buf;
137 }
138
139 static void printk_buf(int level, const u8 * data, u32 len)
140 {
141         char line[81];
142         u32 ofs = 0;
143         if (!(ipw_debug_level & level))
144                 return;
145
146         while (len) {
147                 printk(KERN_DEBUG "%s\n",
148                        snprint_line(line, sizeof(line), &data[ofs],
149                                     min(len, 16U), ofs));
150                 ofs += 16;
151                 len -= min(len, 16U);
152         }
153 }
154
155 static u32 _ipw_read_reg32(struct ipw_priv *priv, u32 reg);
156 #define ipw_read_reg32(a, b) _ipw_read_reg32(a, b)
157
158 static u8 _ipw_read_reg8(struct ipw_priv *ipw, u32 reg);
159 #define ipw_read_reg8(a, b) _ipw_read_reg8(a, b)
160
161 static void _ipw_write_reg8(struct ipw_priv *priv, u32 reg, u8 value);
162 static inline void ipw_write_reg8(struct ipw_priv *a, u32 b, u8 c)
163 {
164         IPW_DEBUG_IO("%s %d: write_indirect8(0x%08X, 0x%08X)\n", __FILE__,
165                      __LINE__, (u32) (b), (u32) (c));
166         _ipw_write_reg8(a, b, c);
167 }
168
169 static void _ipw_write_reg16(struct ipw_priv *priv, u32 reg, u16 value);
170 static inline void ipw_write_reg16(struct ipw_priv *a, u32 b, u16 c)
171 {
172         IPW_DEBUG_IO("%s %d: write_indirect16(0x%08X, 0x%08X)\n", __FILE__,
173                      __LINE__, (u32) (b), (u32) (c));
174         _ipw_write_reg16(a, b, c);
175 }
176
177 static void _ipw_write_reg32(struct ipw_priv *priv, u32 reg, u32 value);
178 static inline void ipw_write_reg32(struct ipw_priv *a, u32 b, u32 c)
179 {
180         IPW_DEBUG_IO("%s %d: write_indirect32(0x%08X, 0x%08X)\n", __FILE__,
181                      __LINE__, (u32) (b), (u32) (c));
182         _ipw_write_reg32(a, b, c);
183 }
184
185 #define _ipw_write8(ipw, ofs, val) writeb((val), (ipw)->hw_base + (ofs))
186 #define ipw_write8(ipw, ofs, val) \
187  IPW_DEBUG_IO("%s %d: write_direct8(0x%08X, 0x%08X)\n", __FILE__, __LINE__, (u32)(ofs), (u32)(val)); \
188  _ipw_write8(ipw, ofs, val)
189
190 #define _ipw_write16(ipw, ofs, val) writew((val), (ipw)->hw_base + (ofs))
191 #define ipw_write16(ipw, ofs, val) \
192  IPW_DEBUG_IO("%s %d: write_direct16(0x%08X, 0x%08X)\n", __FILE__, __LINE__, (u32)(ofs), (u32)(val)); \
193  _ipw_write16(ipw, ofs, val)
194
195 #define _ipw_write32(ipw, ofs, val) writel((val), (ipw)->hw_base + (ofs))
196 #define ipw_write32(ipw, ofs, val) \
197  IPW_DEBUG_IO("%s %d: write_direct32(0x%08X, 0x%08X)\n", __FILE__, __LINE__, (u32)(ofs), (u32)(val)); \
198  _ipw_write32(ipw, ofs, val)
199
200 #define _ipw_read8(ipw, ofs) readb((ipw)->hw_base + (ofs))
201 static inline u8 __ipw_read8(char *f, u32 l, struct ipw_priv *ipw, u32 ofs)
202 {
203         IPW_DEBUG_IO("%s %d: read_direct8(0x%08X)\n", f, l, (u32) (ofs));
204         return _ipw_read8(ipw, ofs);
205 }
206
207 #define ipw_read8(ipw, ofs) __ipw_read8(__FILE__, __LINE__, ipw, ofs)
208
209 #define _ipw_read16(ipw, ofs) readw((ipw)->hw_base + (ofs))
210 static inline u16 __ipw_read16(char *f, u32 l, struct ipw_priv *ipw, u32 ofs)
211 {
212         IPW_DEBUG_IO("%s %d: read_direct16(0x%08X)\n", f, l, (u32) (ofs));
213         return _ipw_read16(ipw, ofs);
214 }
215
216 #define ipw_read16(ipw, ofs) __ipw_read16(__FILE__, __LINE__, ipw, ofs)
217
218 #define _ipw_read32(ipw, ofs) readl((ipw)->hw_base + (ofs))
219 static inline u32 __ipw_read32(char *f, u32 l, struct ipw_priv *ipw, u32 ofs)
220 {
221         IPW_DEBUG_IO("%s %d: read_direct32(0x%08X)\n", f, l, (u32) (ofs));
222         return _ipw_read32(ipw, ofs);
223 }
224
225 #define ipw_read32(ipw, ofs) __ipw_read32(__FILE__, __LINE__, ipw, ofs)
226
227 static void _ipw_read_indirect(struct ipw_priv *, u32, u8 *, int);
228 #define ipw_read_indirect(a, b, c, d) \
229         IPW_DEBUG_IO("%s %d: read_inddirect(0x%08X) %d bytes\n", __FILE__, __LINE__, (u32)(b), d); \
230         _ipw_read_indirect(a, b, c, d)
231
232 static void _ipw_write_indirect(struct ipw_priv *priv, u32 addr, u8 * data,
233                                 int num);
234 #define ipw_write_indirect(a, b, c, d) \
235         IPW_DEBUG_IO("%s %d: write_indirect(0x%08X) %d bytes\n", __FILE__, __LINE__, (u32)(b), d); \
236         _ipw_write_indirect(a, b, c, d)
237
238 /* indirect write s */
239 static void _ipw_write_reg32(struct ipw_priv *priv, u32 reg, u32 value)
240 {
241         IPW_DEBUG_IO(" %p : reg = 0x%8X : value = 0x%8X\n", priv, reg, value);
242         _ipw_write32(priv, CX2_INDIRECT_ADDR, reg);
243         _ipw_write32(priv, CX2_INDIRECT_DATA, value);
244 }
245
246 static void _ipw_write_reg8(struct ipw_priv *priv, u32 reg, u8 value)
247 {
248         IPW_DEBUG_IO(" reg = 0x%8X : value = 0x%8X\n", reg, value);
249         _ipw_write32(priv, CX2_INDIRECT_ADDR, reg & CX2_INDIRECT_ADDR_MASK);
250         _ipw_write8(priv, CX2_INDIRECT_DATA, value);
251         IPW_DEBUG_IO(" reg = 0x%8lX : value = 0x%8X\n",
252                      (unsigned long)(priv->hw_base + CX2_INDIRECT_DATA), value);
253 }
254
255 static void _ipw_write_reg16(struct ipw_priv *priv, u32 reg, u16 value)
256 {
257         IPW_DEBUG_IO(" reg = 0x%8X : value = 0x%8X\n", reg, value);
258         _ipw_write32(priv, CX2_INDIRECT_ADDR, reg & CX2_INDIRECT_ADDR_MASK);
259         _ipw_write16(priv, CX2_INDIRECT_DATA, value);
260 }
261
262 /* indirect read s */
263
264 static u8 _ipw_read_reg8(struct ipw_priv *priv, u32 reg)
265 {
266         u32 word;
267         _ipw_write32(priv, CX2_INDIRECT_ADDR, reg & CX2_INDIRECT_ADDR_MASK);
268         IPW_DEBUG_IO(" reg = 0x%8X : \n", reg);
269         word = _ipw_read32(priv, CX2_INDIRECT_DATA);
270         return (word >> ((reg & 0x3) * 8)) & 0xff;
271 }
272
273 static u32 _ipw_read_reg32(struct ipw_priv *priv, u32 reg)
274 {
275         u32 value;
276
277         IPW_DEBUG_IO("%p : reg = 0x%08x\n", priv, reg);
278
279         _ipw_write32(priv, CX2_INDIRECT_ADDR, reg);
280         value = _ipw_read32(priv, CX2_INDIRECT_DATA);
281         IPW_DEBUG_IO(" reg = 0x%4X : value = 0x%4x \n", reg, value);
282         return value;
283 }
284
285 /* iterative/auto-increment 32 bit reads and writes */
286 static void _ipw_read_indirect(struct ipw_priv *priv, u32 addr, u8 * buf,
287                                int num)
288 {
289         u32 aligned_addr = addr & CX2_INDIRECT_ADDR_MASK;
290         u32 dif_len = addr - aligned_addr;
291         u32 i;
292
293         IPW_DEBUG_IO("addr = %i, buf = %p, num = %i\n", addr, buf, num);
294
295         if (num <= 0) {
296                 return;
297         }
298
299         /* Read the first nibble byte by byte */
300         if (unlikely(dif_len)) {
301                 _ipw_write32(priv, CX2_INDIRECT_ADDR, aligned_addr);
302                 /* Start reading at aligned_addr + dif_len */
303                 for (i = dif_len; ((i < 4) && (num > 0)); i++, num--)
304                         *buf++ = _ipw_read8(priv, CX2_INDIRECT_DATA + i);
305                 aligned_addr += 4;
306         }
307
308         _ipw_write32(priv, CX2_AUTOINC_ADDR, aligned_addr);
309         for (; num >= 4; buf += 4, aligned_addr += 4, num -= 4)
310                 *(u32 *) buf = _ipw_read32(priv, CX2_AUTOINC_DATA);
311
312         /* Copy the last nibble */
313         if (unlikely(num)) {
314                 _ipw_write32(priv, CX2_INDIRECT_ADDR, aligned_addr);
315                 for (i = 0; num > 0; i++, num--)
316                         *buf++ = ipw_read8(priv, CX2_INDIRECT_DATA + i);
317         }
318 }
319
320 static void _ipw_write_indirect(struct ipw_priv *priv, u32 addr, u8 * buf,
321                                 int num)
322 {
323         u32 aligned_addr = addr & CX2_INDIRECT_ADDR_MASK;
324         u32 dif_len = addr - aligned_addr;
325         u32 i;
326
327         IPW_DEBUG_IO("addr = %i, buf = %p, num = %i\n", addr, buf, num);
328
329         if (num <= 0) {
330                 return;
331         }
332
333         /* Write the first nibble byte by byte */
334         if (unlikely(dif_len)) {
335                 _ipw_write32(priv, CX2_INDIRECT_ADDR, aligned_addr);
336                 /* Start reading at aligned_addr + dif_len */
337                 for (i = dif_len; ((i < 4) && (num > 0)); i++, num--, buf++)
338                         _ipw_write8(priv, CX2_INDIRECT_DATA + i, *buf);
339                 aligned_addr += 4;
340         }
341
342         _ipw_write32(priv, CX2_AUTOINC_ADDR, aligned_addr);
343         for (; num >= 4; buf += 4, aligned_addr += 4, num -= 4)
344                 _ipw_write32(priv, CX2_AUTOINC_DATA, *(u32 *) buf);
345
346         /* Copy the last nibble */
347         if (unlikely(num)) {
348                 _ipw_write32(priv, CX2_INDIRECT_ADDR, aligned_addr);
349                 for (i = 0; num > 0; i++, num--, buf++)
350                         _ipw_write8(priv, CX2_INDIRECT_DATA + i, *buf);
351         }
352 }
353
354 static void ipw_write_direct(struct ipw_priv *priv, u32 addr, void *buf,
355                              int num)
356 {
357         memcpy_toio((priv->hw_base + addr), buf, num);
358 }
359
360 static inline void ipw_set_bit(struct ipw_priv *priv, u32 reg, u32 mask)
361 {
362         ipw_write32(priv, reg, ipw_read32(priv, reg) | mask);
363 }
364
365 static inline void ipw_clear_bit(struct ipw_priv *priv, u32 reg, u32 mask)
366 {
367         ipw_write32(priv, reg, ipw_read32(priv, reg) & ~mask);
368 }
369
370 static inline void ipw_enable_interrupts(struct ipw_priv *priv)
371 {
372         if (priv->status & STATUS_INT_ENABLED)
373                 return;
374         priv->status |= STATUS_INT_ENABLED;
375         ipw_write32(priv, CX2_INTA_MASK_R, CX2_INTA_MASK_ALL);
376 }
377
378 static inline void ipw_disable_interrupts(struct ipw_priv *priv)
379 {
380         if (!(priv->status & STATUS_INT_ENABLED))
381                 return;
382         priv->status &= ~STATUS_INT_ENABLED;
383         ipw_write32(priv, CX2_INTA_MASK_R, ~CX2_INTA_MASK_ALL);
384 }
385
386 static char *ipw_error_desc(u32 val)
387 {
388         switch (val) {
389         case IPW_FW_ERROR_OK:
390                 return "ERROR_OK";
391         case IPW_FW_ERROR_FAIL:
392                 return "ERROR_FAIL";
393         case IPW_FW_ERROR_MEMORY_UNDERFLOW:
394                 return "MEMORY_UNDERFLOW";
395         case IPW_FW_ERROR_MEMORY_OVERFLOW:
396                 return "MEMORY_OVERFLOW";
397         case IPW_FW_ERROR_BAD_PARAM:
398                 return "ERROR_BAD_PARAM";
399         case IPW_FW_ERROR_BAD_CHECKSUM:
400                 return "ERROR_BAD_CHECKSUM";
401         case IPW_FW_ERROR_NMI_INTERRUPT:
402                 return "ERROR_NMI_INTERRUPT";
403         case IPW_FW_ERROR_BAD_DATABASE:
404                 return "ERROR_BAD_DATABASE";
405         case IPW_FW_ERROR_ALLOC_FAIL:
406                 return "ERROR_ALLOC_FAIL";
407         case IPW_FW_ERROR_DMA_UNDERRUN:
408                 return "ERROR_DMA_UNDERRUN";
409         case IPW_FW_ERROR_DMA_STATUS:
410                 return "ERROR_DMA_STATUS";
411         case IPW_FW_ERROR_DINOSTATUS_ERROR:
412                 return "ERROR_DINOSTATUS_ERROR";
413         case IPW_FW_ERROR_EEPROMSTATUS_ERROR:
414                 return "ERROR_EEPROMSTATUS_ERROR";
415         case IPW_FW_ERROR_SYSASSERT:
416                 return "ERROR_SYSASSERT";
417         case IPW_FW_ERROR_FATAL_ERROR:
418                 return "ERROR_FATALSTATUS_ERROR";
419         default:
420                 return "UNKNOWNSTATUS_ERROR";
421         }
422 }
423
424 static void ipw_dump_nic_error_log(struct ipw_priv *priv)
425 {
426         u32 desc, time, blink1, blink2, ilink1, ilink2, idata, i, count, base;
427
428         base = ipw_read32(priv, IPWSTATUS_ERROR_LOG);
429         count = ipw_read_reg32(priv, base);
430
431         if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
432                 IPW_ERROR("Start IPW Error Log Dump:\n");
433                 IPW_ERROR("Status: 0x%08X, Config: %08X\n",
434                           priv->status, priv->config);
435         }
436
437         for (i = ERROR_START_OFFSET;
438              i <= count * ERROR_ELEM_SIZE; i += ERROR_ELEM_SIZE) {
439                 desc = ipw_read_reg32(priv, base + i);
440                 time = ipw_read_reg32(priv, base + i + 1 * sizeof(u32));
441                 blink1 = ipw_read_reg32(priv, base + i + 2 * sizeof(u32));
442                 blink2 = ipw_read_reg32(priv, base + i + 3 * sizeof(u32));
443                 ilink1 = ipw_read_reg32(priv, base + i + 4 * sizeof(u32));
444                 ilink2 = ipw_read_reg32(priv, base + i + 5 * sizeof(u32));
445                 idata = ipw_read_reg32(priv, base + i + 6 * sizeof(u32));
446
447                 IPW_ERROR("%s %i 0x%08x  0x%08x  0x%08x  0x%08x  0x%08x\n",
448                           ipw_error_desc(desc), time, blink1, blink2,
449                           ilink1, ilink2, idata);
450         }
451 }
452
453 static void ipw_dump_nic_event_log(struct ipw_priv *priv)
454 {
455         u32 ev, time, data, i, count, base;
456
457         base = ipw_read32(priv, IPW_EVENT_LOG);
458         count = ipw_read_reg32(priv, base);
459
460         if (EVENT_START_OFFSET <= count * EVENT_ELEM_SIZE)
461                 IPW_ERROR("Start IPW Event Log Dump:\n");
462
463         for (i = EVENT_START_OFFSET;
464              i <= count * EVENT_ELEM_SIZE; i += EVENT_ELEM_SIZE) {
465                 ev = ipw_read_reg32(priv, base + i);
466                 time = ipw_read_reg32(priv, base + i + 1 * sizeof(u32));
467                 data = ipw_read_reg32(priv, base + i + 2 * sizeof(u32));
468
469 #ifdef CONFIG_IPW_DEBUG
470                 IPW_ERROR("%i\t0x%08x\t%i\n", time, data, ev);
471 #endif
472         }
473 }
474
475 static int ipw_get_ordinal(struct ipw_priv *priv, u32 ord, void *val, u32 * len)
476 {
477         u32 addr, field_info, field_len, field_count, total_len;
478
479         IPW_DEBUG_ORD("ordinal = %i\n", ord);
480
481         if (!priv || !val || !len) {
482                 IPW_DEBUG_ORD("Invalid argument\n");
483                 return -EINVAL;
484         }
485
486         /* verify device ordinal tables have been initialized */
487         if (!priv->table0_addr || !priv->table1_addr || !priv->table2_addr) {
488                 IPW_DEBUG_ORD("Access ordinals before initialization\n");
489                 return -EINVAL;
490         }
491
492         switch (IPW_ORD_TABLE_ID_MASK & ord) {
493         case IPW_ORD_TABLE_0_MASK:
494                 /*
495                  * TABLE 0: Direct access to a table of 32 bit values
496                  *
497                  * This is a very simple table with the data directly
498                  * read from the table
499                  */
500
501                 /* remove the table id from the ordinal */
502                 ord &= IPW_ORD_TABLE_VALUE_MASK;
503
504                 /* boundary check */
505                 if (ord > priv->table0_len) {
506                         IPW_DEBUG_ORD("ordinal value (%i) longer then "
507                                       "max (%i)\n", ord, priv->table0_len);
508                         return -EINVAL;
509                 }
510
511                 /* verify we have enough room to store the value */
512                 if (*len < sizeof(u32)) {
513                         IPW_DEBUG_ORD("ordinal buffer length too small, "
514                                       "need %zd\n", sizeof(u32));
515                         return -EINVAL;
516                 }
517
518                 IPW_DEBUG_ORD("Reading TABLE0[%i] from offset 0x%08x\n",
519                               ord, priv->table0_addr + (ord << 2));
520
521                 *len = sizeof(u32);
522                 ord <<= 2;
523                 *((u32 *) val) = ipw_read32(priv, priv->table0_addr + ord);
524                 break;
525
526         case IPW_ORD_TABLE_1_MASK:
527                 /*
528                  * TABLE 1: Indirect access to a table of 32 bit values
529                  *
530                  * This is a fairly large table of u32 values each
531                  * representing starting addr for the data (which is
532                  * also a u32)
533                  */
534
535                 /* remove the table id from the ordinal */
536                 ord &= IPW_ORD_TABLE_VALUE_MASK;
537
538                 /* boundary check */
539                 if (ord > priv->table1_len) {
540                         IPW_DEBUG_ORD("ordinal value too long\n");
541                         return -EINVAL;
542                 }
543
544                 /* verify we have enough room to store the value */
545                 if (*len < sizeof(u32)) {
546                         IPW_DEBUG_ORD("ordinal buffer length too small, "
547                                       "need %zd\n", sizeof(u32));
548                         return -EINVAL;
549                 }
550
551                 *((u32 *) val) =
552                     ipw_read_reg32(priv, (priv->table1_addr + (ord << 2)));
553                 *len = sizeof(u32);
554                 break;
555
556         case IPW_ORD_TABLE_2_MASK:
557                 /*
558                  * TABLE 2: Indirect access to a table of variable sized values
559                  *
560                  * This table consist of six values, each containing
561                  *     - dword containing the starting offset of the data
562                  *     - dword containing the lengh in the first 16bits
563                  *       and the count in the second 16bits
564                  */
565
566                 /* remove the table id from the ordinal */
567                 ord &= IPW_ORD_TABLE_VALUE_MASK;
568
569                 /* boundary check */
570                 if (ord > priv->table2_len) {
571                         IPW_DEBUG_ORD("ordinal value too long\n");
572                         return -EINVAL;
573                 }
574
575                 /* get the address of statistic */
576                 addr = ipw_read_reg32(priv, priv->table2_addr + (ord << 3));
577
578                 /* get the second DW of statistics ;
579                  * two 16-bit words - first is length, second is count */
580                 field_info =
581                     ipw_read_reg32(priv,
582                                    priv->table2_addr + (ord << 3) +
583                                    sizeof(u32));
584
585                 /* get each entry length */
586                 field_len = *((u16 *) & field_info);
587
588                 /* get number of entries */
589                 field_count = *(((u16 *) & field_info) + 1);
590
591                 /* abort if not enought memory */
592                 total_len = field_len * field_count;
593                 if (total_len > *len) {
594                         *len = total_len;
595                         return -EINVAL;
596                 }
597
598                 *len = total_len;
599                 if (!total_len)
600                         return 0;
601
602                 IPW_DEBUG_ORD("addr = 0x%08x, total_len = %i, "
603                               "field_info = 0x%08x\n",
604                               addr, total_len, field_info);
605                 ipw_read_indirect(priv, addr, val, total_len);
606                 break;
607
608         default:
609                 IPW_DEBUG_ORD("Invalid ordinal!\n");
610                 return -EINVAL;
611
612         }
613
614         return 0;
615 }
616
617 static void ipw_init_ordinals(struct ipw_priv *priv)
618 {
619         priv->table0_addr = IPW_ORDINALS_TABLE_LOWER;
620         priv->table0_len = ipw_read32(priv, priv->table0_addr);
621
622         IPW_DEBUG_ORD("table 0 offset at 0x%08x, len = %i\n",
623                       priv->table0_addr, priv->table0_len);
624
625         priv->table1_addr = ipw_read32(priv, IPW_ORDINALS_TABLE_1);
626         priv->table1_len = ipw_read_reg32(priv, priv->table1_addr);
627
628         IPW_DEBUG_ORD("table 1 offset at 0x%08x, len = %i\n",
629                       priv->table1_addr, priv->table1_len);
630
631         priv->table2_addr = ipw_read32(priv, IPW_ORDINALS_TABLE_2);
632         priv->table2_len = ipw_read_reg32(priv, priv->table2_addr);
633         priv->table2_len &= 0x0000ffff; /* use first two bytes */
634
635         IPW_DEBUG_ORD("table 2 offset at 0x%08x, len = %i\n",
636                       priv->table2_addr, priv->table2_len);
637
638 }
639
640 /*
641  * The following adds a new attribute to the sysfs representation
642  * of this device driver (i.e. a new file in /sys/bus/pci/drivers/ipw/)
643  * used for controling the debug level.
644  *
645  * See the level definitions in ipw for details.
646  */
647 static ssize_t show_debug_level(struct device_driver *d, char *buf)
648 {
649         return sprintf(buf, "0x%08X\n", ipw_debug_level);
650 }
651 static ssize_t store_debug_level(struct device_driver *d,
652                                  const char *buf, size_t count)
653 {
654         char *p = (char *)buf;
655         u32 val;
656
657         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
658                 p++;
659                 if (p[0] == 'x' || p[0] == 'X')
660                         p++;
661                 val = simple_strtoul(p, &p, 16);
662         } else
663                 val = simple_strtoul(p, &p, 10);
664         if (p == buf)
665                 printk(KERN_INFO DRV_NAME
666                        ": %s is not in hex or decimal form.\n", buf);
667         else
668                 ipw_debug_level = val;
669
670         return strnlen(buf, count);
671 }
672
673 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
674                    show_debug_level, store_debug_level);
675
676 static ssize_t show_status(struct device *d,
677                            struct device_attribute *attr, char *buf)
678 {
679         struct ipw_priv *p = d->driver_data;
680         return sprintf(buf, "0x%08x\n", (int)p->status);
681 }
682
683 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
684
685 static ssize_t show_cfg(struct device *d, struct device_attribute *attr,
686                         char *buf)
687 {
688         struct ipw_priv *p = d->driver_data;
689         return sprintf(buf, "0x%08x\n", (int)p->config);
690 }
691
692 static DEVICE_ATTR(cfg, S_IRUGO, show_cfg, NULL);
693
694 static ssize_t show_nic_type(struct device *d,
695                              struct device_attribute *attr, char *buf)
696 {
697         struct ipw_priv *p = d->driver_data;
698         u8 type = p->eeprom[EEPROM_NIC_TYPE];
699
700         switch (type) {
701         case EEPROM_NIC_TYPE_STANDARD:
702                 return sprintf(buf, "STANDARD\n");
703         case EEPROM_NIC_TYPE_DELL:
704                 return sprintf(buf, "DELL\n");
705         case EEPROM_NIC_TYPE_FUJITSU:
706                 return sprintf(buf, "FUJITSU\n");
707         case EEPROM_NIC_TYPE_IBM:
708                 return sprintf(buf, "IBM\n");
709         case EEPROM_NIC_TYPE_HP:
710                 return sprintf(buf, "HP\n");
711         }
712
713         return sprintf(buf, "UNKNOWN\n");
714 }
715
716 static DEVICE_ATTR(nic_type, S_IRUGO, show_nic_type, NULL);
717
718 static ssize_t dump_error_log(struct device *d,
719                               struct device_attribute *attr, const char *buf,
720                               size_t count)
721 {
722         char *p = (char *)buf;
723
724         if (p[0] == '1')
725                 ipw_dump_nic_error_log((struct ipw_priv *)d->driver_data);
726
727         return strnlen(buf, count);
728 }
729
730 static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
731
732 static ssize_t dump_event_log(struct device *d,
733                               struct device_attribute *attr, const char *buf,
734                               size_t count)
735 {
736         char *p = (char *)buf;
737
738         if (p[0] == '1')
739                 ipw_dump_nic_event_log((struct ipw_priv *)d->driver_data);
740
741         return strnlen(buf, count);
742 }
743
744 static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
745
746 static ssize_t show_ucode_version(struct device *d,
747                                   struct device_attribute *attr, char *buf)
748 {
749         u32 len = sizeof(u32), tmp = 0;
750         struct ipw_priv *p = d->driver_data;
751
752         if (ipw_get_ordinal(p, IPW_ORD_STAT_UCODE_VERSION, &tmp, &len))
753                 return 0;
754
755         return sprintf(buf, "0x%08x\n", tmp);
756 }
757
758 static DEVICE_ATTR(ucode_version, S_IWUSR | S_IRUGO, show_ucode_version, NULL);
759
760 static ssize_t show_rtc(struct device *d, struct device_attribute *attr,
761                         char *buf)
762 {
763         u32 len = sizeof(u32), tmp = 0;
764         struct ipw_priv *p = d->driver_data;
765
766         if (ipw_get_ordinal(p, IPW_ORD_STAT_RTC, &tmp, &len))
767                 return 0;
768
769         return sprintf(buf, "0x%08x\n", tmp);
770 }
771
772 static DEVICE_ATTR(rtc, S_IWUSR | S_IRUGO, show_rtc, NULL);
773
774 /*
775  * Add a device attribute to view/control the delay between eeprom
776  * operations.
777  */
778 static ssize_t show_eeprom_delay(struct device *d,
779                                  struct device_attribute *attr, char *buf)
780 {
781         int n = ((struct ipw_priv *)d->driver_data)->eeprom_delay;
782         return sprintf(buf, "%i\n", n);
783 }
784 static ssize_t store_eeprom_delay(struct device *d,
785                                   struct device_attribute *attr,
786                                   const char *buf, size_t count)
787 {
788         struct ipw_priv *p = d->driver_data;
789         sscanf(buf, "%i", &p->eeprom_delay);
790         return strnlen(buf, count);
791 }
792
793 static DEVICE_ATTR(eeprom_delay, S_IWUSR | S_IRUGO,
794                    show_eeprom_delay, store_eeprom_delay);
795
796 static ssize_t show_command_event_reg(struct device *d,
797                                       struct device_attribute *attr, char *buf)
798 {
799         u32 reg = 0;
800         struct ipw_priv *p = d->driver_data;
801
802         reg = ipw_read_reg32(p, CX2_INTERNAL_CMD_EVENT);
803         return sprintf(buf, "0x%08x\n", reg);
804 }
805 static ssize_t store_command_event_reg(struct device *d,
806                                        struct device_attribute *attr,
807                                        const char *buf, size_t count)
808 {
809         u32 reg;
810         struct ipw_priv *p = d->driver_data;
811
812         sscanf(buf, "%x", &reg);
813         ipw_write_reg32(p, CX2_INTERNAL_CMD_EVENT, reg);
814         return strnlen(buf, count);
815 }
816
817 static DEVICE_ATTR(command_event_reg, S_IWUSR | S_IRUGO,
818                    show_command_event_reg, store_command_event_reg);
819
820 static ssize_t show_mem_gpio_reg(struct device *d,
821                                  struct device_attribute *attr, char *buf)
822 {
823         u32 reg = 0;
824         struct ipw_priv *p = d->driver_data;
825
826         reg = ipw_read_reg32(p, 0x301100);
827         return sprintf(buf, "0x%08x\n", reg);
828 }
829 static ssize_t store_mem_gpio_reg(struct device *d,
830                                   struct device_attribute *attr,
831                                   const char *buf, size_t count)
832 {
833         u32 reg;
834         struct ipw_priv *p = d->driver_data;
835
836         sscanf(buf, "%x", &reg);
837         ipw_write_reg32(p, 0x301100, reg);
838         return strnlen(buf, count);
839 }
840
841 static DEVICE_ATTR(mem_gpio_reg, S_IWUSR | S_IRUGO,
842                    show_mem_gpio_reg, store_mem_gpio_reg);
843
844 static ssize_t show_indirect_dword(struct device *d,
845                                    struct device_attribute *attr, char *buf)
846 {
847         u32 reg = 0;
848         struct ipw_priv *priv = d->driver_data;
849         if (priv->status & STATUS_INDIRECT_DWORD)
850                 reg = ipw_read_reg32(priv, priv->indirect_dword);
851         else
852                 reg = 0;
853
854         return sprintf(buf, "0x%08x\n", reg);
855 }
856 static ssize_t store_indirect_dword(struct device *d,
857                                     struct device_attribute *attr,
858                                     const char *buf, size_t count)
859 {
860         struct ipw_priv *priv = d->driver_data;
861
862         sscanf(buf, "%x", &priv->indirect_dword);
863         priv->status |= STATUS_INDIRECT_DWORD;
864         return strnlen(buf, count);
865 }
866
867 static DEVICE_ATTR(indirect_dword, S_IWUSR | S_IRUGO,
868                    show_indirect_dword, store_indirect_dword);
869
870 static ssize_t show_indirect_byte(struct device *d,
871                                   struct device_attribute *attr, char *buf)
872 {
873         u8 reg = 0;
874         struct ipw_priv *priv = d->driver_data;
875         if (priv->status & STATUS_INDIRECT_BYTE)
876                 reg = ipw_read_reg8(priv, priv->indirect_byte);
877         else
878                 reg = 0;
879
880         return sprintf(buf, "0x%02x\n", reg);
881 }
882 static ssize_t store_indirect_byte(struct device *d,
883                                    struct device_attribute *attr,
884                                    const char *buf, size_t count)
885 {
886         struct ipw_priv *priv = d->driver_data;
887
888         sscanf(buf, "%x", &priv->indirect_byte);
889         priv->status |= STATUS_INDIRECT_BYTE;
890         return strnlen(buf, count);
891 }
892
893 static DEVICE_ATTR(indirect_byte, S_IWUSR | S_IRUGO,
894                    show_indirect_byte, store_indirect_byte);
895
896 static ssize_t show_direct_dword(struct device *d,
897                                  struct device_attribute *attr, char *buf)
898 {
899         u32 reg = 0;
900         struct ipw_priv *priv = d->driver_data;
901
902         if (priv->status & STATUS_DIRECT_DWORD)
903                 reg = ipw_read32(priv, priv->direct_dword);
904         else
905                 reg = 0;
906
907         return sprintf(buf, "0x%08x\n", reg);
908 }
909 static ssize_t store_direct_dword(struct device *d,
910                                   struct device_attribute *attr,
911                                   const char *buf, size_t count)
912 {
913         struct ipw_priv *priv = d->driver_data;
914
915         sscanf(buf, "%x", &priv->direct_dword);
916         priv->status |= STATUS_DIRECT_DWORD;
917         return strnlen(buf, count);
918 }
919
920 static DEVICE_ATTR(direct_dword, S_IWUSR | S_IRUGO,
921                    show_direct_dword, store_direct_dword);
922
923 static inline int rf_kill_active(struct ipw_priv *priv)
924 {
925         if (0 == (ipw_read32(priv, 0x30) & 0x10000))
926                 priv->status |= STATUS_RF_KILL_HW;
927         else
928                 priv->status &= ~STATUS_RF_KILL_HW;
929
930         return (priv->status & STATUS_RF_KILL_HW) ? 1 : 0;
931 }
932
933 static ssize_t show_rf_kill(struct device *d, struct device_attribute *attr,
934                             char *buf)
935 {
936         /* 0 - RF kill not enabled
937            1 - SW based RF kill active (sysfs)
938            2 - HW based RF kill active
939            3 - Both HW and SW baed RF kill active */
940         struct ipw_priv *priv = d->driver_data;
941         int val = ((priv->status & STATUS_RF_KILL_SW) ? 0x1 : 0x0) |
942             (rf_kill_active(priv) ? 0x2 : 0x0);
943         return sprintf(buf, "%i\n", val);
944 }
945
946 static int ipw_radio_kill_sw(struct ipw_priv *priv, int disable_radio)
947 {
948         if ((disable_radio ? 1 : 0) ==
949             ((priv->status & STATUS_RF_KILL_SW) ? 1 : 0))
950                 return 0;
951
952         IPW_DEBUG_RF_KILL("Manual SW RF Kill set to: RADIO  %s\n",
953                           disable_radio ? "OFF" : "ON");
954
955         if (disable_radio) {
956                 priv->status |= STATUS_RF_KILL_SW;
957
958                 if (priv->workqueue) {
959                         cancel_delayed_work(&priv->request_scan);
960                 }
961                 wake_up_interruptible(&priv->wait_command_queue);
962                 queue_work(priv->workqueue, &priv->down);
963         } else {
964                 priv->status &= ~STATUS_RF_KILL_SW;
965                 if (rf_kill_active(priv)) {
966                         IPW_DEBUG_RF_KILL("Can not turn radio back on - "
967                                           "disabled by HW switch\n");
968                         /* Make sure the RF_KILL check timer is running */
969                         cancel_delayed_work(&priv->rf_kill);
970                         queue_delayed_work(priv->workqueue, &priv->rf_kill,
971                                            2 * HZ);
972                 } else
973                         queue_work(priv->workqueue, &priv->up);
974         }
975
976         return 1;
977 }
978
979 static ssize_t store_rf_kill(struct device *d, struct device_attribute *attr,
980                              const char *buf, size_t count)
981 {
982         struct ipw_priv *priv = d->driver_data;
983
984         ipw_radio_kill_sw(priv, buf[0] == '1');
985
986         return count;
987 }
988
989 static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
990
991 static void notify_wx_assoc_event(struct ipw_priv *priv)
992 {
993         union iwreq_data wrqu;
994         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
995         if (priv->status & STATUS_ASSOCIATED)
996                 memcpy(wrqu.ap_addr.sa_data, priv->bssid, ETH_ALEN);
997         else
998                 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
999         wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
1000 }
1001
1002 static void ipw_irq_tasklet(struct ipw_priv *priv)
1003 {
1004         u32 inta, inta_mask, handled = 0;
1005         unsigned long flags;
1006         int rc = 0;
1007
1008         spin_lock_irqsave(&priv->lock, flags);
1009
1010         inta = ipw_read32(priv, CX2_INTA_RW);
1011         inta_mask = ipw_read32(priv, CX2_INTA_MASK_R);
1012         inta &= (CX2_INTA_MASK_ALL & inta_mask);
1013
1014         /* Add any cached INTA values that need to be handled */
1015         inta |= priv->isr_inta;
1016
1017         /* handle all the justifications for the interrupt */
1018         if (inta & CX2_INTA_BIT_RX_TRANSFER) {
1019                 ipw_rx(priv);
1020                 handled |= CX2_INTA_BIT_RX_TRANSFER;
1021         }
1022
1023         if (inta & CX2_INTA_BIT_TX_CMD_QUEUE) {
1024                 IPW_DEBUG_HC("Command completed.\n");
1025                 rc = ipw_queue_tx_reclaim(priv, &priv->txq_cmd, -1);
1026                 priv->status &= ~STATUS_HCMD_ACTIVE;
1027                 wake_up_interruptible(&priv->wait_command_queue);
1028                 handled |= CX2_INTA_BIT_TX_CMD_QUEUE;
1029         }
1030
1031         if (inta & CX2_INTA_BIT_TX_QUEUE_1) {
1032                 IPW_DEBUG_TX("TX_QUEUE_1\n");
1033                 rc = ipw_queue_tx_reclaim(priv, &priv->txq[0], 0);
1034                 handled |= CX2_INTA_BIT_TX_QUEUE_1;
1035         }
1036
1037         if (inta & CX2_INTA_BIT_TX_QUEUE_2) {
1038                 IPW_DEBUG_TX("TX_QUEUE_2\n");
1039                 rc = ipw_queue_tx_reclaim(priv, &priv->txq[1], 1);
1040                 handled |= CX2_INTA_BIT_TX_QUEUE_2;
1041         }
1042
1043         if (inta & CX2_INTA_BIT_TX_QUEUE_3) {
1044                 IPW_DEBUG_TX("TX_QUEUE_3\n");
1045                 rc = ipw_queue_tx_reclaim(priv, &priv->txq[2], 2);
1046                 handled |= CX2_INTA_BIT_TX_QUEUE_3;
1047         }
1048
1049         if (inta & CX2_INTA_BIT_TX_QUEUE_4) {
1050                 IPW_DEBUG_TX("TX_QUEUE_4\n");
1051                 rc = ipw_queue_tx_reclaim(priv, &priv->txq[3], 3);
1052                 handled |= CX2_INTA_BIT_TX_QUEUE_4;
1053         }
1054
1055         if (inta & CX2_INTA_BIT_STATUS_CHANGE) {
1056                 IPW_WARNING("STATUS_CHANGE\n");
1057                 handled |= CX2_INTA_BIT_STATUS_CHANGE;
1058         }
1059
1060         if (inta & CX2_INTA_BIT_BEACON_PERIOD_EXPIRED) {
1061                 IPW_WARNING("TX_PERIOD_EXPIRED\n");
1062                 handled |= CX2_INTA_BIT_BEACON_PERIOD_EXPIRED;
1063         }
1064
1065         if (inta & CX2_INTA_BIT_SLAVE_MODE_HOST_CMD_DONE) {
1066                 IPW_WARNING("HOST_CMD_DONE\n");
1067                 handled |= CX2_INTA_BIT_SLAVE_MODE_HOST_CMD_DONE;
1068         }
1069
1070         if (inta & CX2_INTA_BIT_FW_INITIALIZATION_DONE) {
1071                 IPW_WARNING("FW_INITIALIZATION_DONE\n");
1072                 handled |= CX2_INTA_BIT_FW_INITIALIZATION_DONE;
1073         }
1074
1075         if (inta & CX2_INTA_BIT_FW_CARD_DISABLE_PHY_OFF_DONE) {
1076                 IPW_WARNING("PHY_OFF_DONE\n");
1077                 handled |= CX2_INTA_BIT_FW_CARD_DISABLE_PHY_OFF_DONE;
1078         }
1079
1080         if (inta & CX2_INTA_BIT_RF_KILL_DONE) {
1081                 IPW_DEBUG_RF_KILL("RF_KILL_DONE\n");
1082                 priv->status |= STATUS_RF_KILL_HW;
1083                 wake_up_interruptible(&priv->wait_command_queue);
1084                 netif_carrier_off(priv->net_dev);
1085                 netif_stop_queue(priv->net_dev);
1086                 priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
1087                 notify_wx_assoc_event(priv);
1088                 cancel_delayed_work(&priv->request_scan);
1089                 queue_delayed_work(priv->workqueue, &priv->rf_kill, 2 * HZ);
1090                 handled |= CX2_INTA_BIT_RF_KILL_DONE;
1091         }
1092
1093         if (inta & CX2_INTA_BIT_FATAL_ERROR) {
1094                 IPW_ERROR("Firmware error detected.  Restarting.\n");
1095 #ifdef CONFIG_IPW_DEBUG
1096                 if (ipw_debug_level & IPW_DL_FW_ERRORS) {
1097                         ipw_dump_nic_error_log(priv);
1098                         ipw_dump_nic_event_log(priv);
1099                 }
1100 #endif
1101                 queue_work(priv->workqueue, &priv->adapter_restart);
1102                 handled |= CX2_INTA_BIT_FATAL_ERROR;
1103         }
1104
1105         if (inta & CX2_INTA_BIT_PARITY_ERROR) {
1106                 IPW_ERROR("Parity error\n");
1107                 handled |= CX2_INTA_BIT_PARITY_ERROR;
1108         }
1109
1110         if (handled != inta) {
1111                 IPW_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
1112         }
1113
1114         /* enable all interrupts */
1115         ipw_enable_interrupts(priv);
1116
1117         spin_unlock_irqrestore(&priv->lock, flags);
1118 }
1119
1120 #ifdef CONFIG_IPW_DEBUG
1121 #define IPW_CMD(x) case IPW_CMD_ ## x : return #x
1122 static char *get_cmd_string(u8 cmd)
1123 {
1124         switch (cmd) {
1125                 IPW_CMD(HOST_COMPLETE);
1126                 IPW_CMD(POWER_DOWN);
1127                 IPW_CMD(SYSTEM_CONFIG);
1128                 IPW_CMD(MULTICAST_ADDRESS);
1129                 IPW_CMD(SSID);
1130                 IPW_CMD(ADAPTER_ADDRESS);
1131                 IPW_CMD(PORT_TYPE);
1132                 IPW_CMD(RTS_THRESHOLD);
1133                 IPW_CMD(FRAG_THRESHOLD);
1134                 IPW_CMD(POWER_MODE);
1135                 IPW_CMD(WEP_KEY);
1136                 IPW_CMD(TGI_TX_KEY);
1137                 IPW_CMD(SCAN_REQUEST);
1138                 IPW_CMD(SCAN_REQUEST_EXT);
1139                 IPW_CMD(ASSOCIATE);
1140                 IPW_CMD(SUPPORTED_RATES);
1141                 IPW_CMD(SCAN_ABORT);
1142                 IPW_CMD(TX_FLUSH);
1143                 IPW_CMD(QOS_PARAMETERS);
1144                 IPW_CMD(DINO_CONFIG);
1145                 IPW_CMD(RSN_CAPABILITIES);
1146                 IPW_CMD(RX_KEY);
1147                 IPW_CMD(CARD_DISABLE);
1148                 IPW_CMD(SEED_NUMBER);
1149                 IPW_CMD(TX_POWER);
1150                 IPW_CMD(COUNTRY_INFO);
1151                 IPW_CMD(AIRONET_INFO);
1152                 IPW_CMD(AP_TX_POWER);
1153                 IPW_CMD(CCKM_INFO);
1154                 IPW_CMD(CCX_VER_INFO);
1155                 IPW_CMD(SET_CALIBRATION);
1156                 IPW_CMD(SENSITIVITY_CALIB);
1157                 IPW_CMD(RETRY_LIMIT);
1158                 IPW_CMD(IPW_PRE_POWER_DOWN);
1159                 IPW_CMD(VAP_BEACON_TEMPLATE);
1160                 IPW_CMD(VAP_DTIM_PERIOD);
1161                 IPW_CMD(EXT_SUPPORTED_RATES);
1162                 IPW_CMD(VAP_LOCAL_TX_PWR_CONSTRAINT);
1163                 IPW_CMD(VAP_QUIET_INTERVALS);
1164                 IPW_CMD(VAP_CHANNEL_SWITCH);
1165                 IPW_CMD(VAP_MANDATORY_CHANNELS);
1166                 IPW_CMD(VAP_CELL_PWR_LIMIT);
1167                 IPW_CMD(VAP_CF_PARAM_SET);
1168                 IPW_CMD(VAP_SET_BEACONING_STATE);
1169                 IPW_CMD(MEASUREMENT);
1170                 IPW_CMD(POWER_CAPABILITY);
1171                 IPW_CMD(SUPPORTED_CHANNELS);
1172                 IPW_CMD(TPC_REPORT);
1173                 IPW_CMD(WME_INFO);
1174                 IPW_CMD(PRODUCTION_COMMAND);
1175         default:
1176                 return "UNKNOWN";
1177         }
1178 }
1179 #endif
1180
1181 #define HOST_COMPLETE_TIMEOUT HZ
1182 static int ipw_send_cmd(struct ipw_priv *priv, struct host_cmd *cmd)
1183 {
1184         int rc = 0;
1185
1186         if (priv->status & STATUS_HCMD_ACTIVE) {
1187                 IPW_ERROR("Already sending a command\n");
1188                 return -1;
1189         }
1190
1191         priv->status |= STATUS_HCMD_ACTIVE;
1192
1193         IPW_DEBUG_HC("Sending %s command (#%d), %d bytes\n",
1194                      get_cmd_string(cmd->cmd), cmd->cmd, cmd->len);
1195         printk_buf(IPW_DL_HOST_COMMAND, (u8 *) cmd->param, cmd->len);
1196
1197         rc = ipw_queue_tx_hcmd(priv, cmd->cmd, &cmd->param, cmd->len, 0);
1198         if (rc)
1199                 return rc;
1200
1201         rc = wait_event_interruptible_timeout(priv->wait_command_queue,
1202                                               !(priv->
1203                                                 status & STATUS_HCMD_ACTIVE),
1204                                               HOST_COMPLETE_TIMEOUT);
1205         if (rc == 0) {
1206                 IPW_DEBUG_INFO("Command completion failed out after %dms.\n",
1207                                jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
1208                 priv->status &= ~STATUS_HCMD_ACTIVE;
1209                 return -EIO;
1210         }
1211         if (priv->status & STATUS_RF_KILL_MASK) {
1212                 IPW_DEBUG_INFO("Command aborted due to RF Kill Switch\n");
1213                 return -EIO;
1214         }
1215
1216         return 0;
1217 }
1218
1219 static int ipw_send_host_complete(struct ipw_priv *priv)
1220 {
1221         struct host_cmd cmd = {
1222                 .cmd = IPW_CMD_HOST_COMPLETE,
1223                 .len = 0
1224         };
1225
1226         if (!priv) {
1227                 IPW_ERROR("Invalid args\n");
1228                 return -1;
1229         }
1230
1231         if (ipw_send_cmd(priv, &cmd)) {
1232                 IPW_ERROR("failed to send HOST_COMPLETE command\n");
1233                 return -1;
1234         }
1235
1236         return 0;
1237 }
1238
1239 static int ipw_send_system_config(struct ipw_priv *priv,
1240                                   struct ipw_sys_config *config)
1241 {
1242         struct host_cmd cmd = {
1243                 .cmd = IPW_CMD_SYSTEM_CONFIG,
1244                 .len = sizeof(*config)
1245         };
1246
1247         if (!priv || !config) {
1248                 IPW_ERROR("Invalid args\n");
1249                 return -1;
1250         }
1251
1252         memcpy(&cmd.param, config, sizeof(*config));
1253         if (ipw_send_cmd(priv, &cmd)) {
1254                 IPW_ERROR("failed to send SYSTEM_CONFIG command\n");
1255                 return -1;
1256         }
1257
1258         return 0;
1259 }
1260
1261 static int ipw_send_ssid(struct ipw_priv *priv, u8 * ssid, int len)
1262 {
1263         struct host_cmd cmd = {
1264                 .cmd = IPW_CMD_SSID,
1265                 .len = min(len, IW_ESSID_MAX_SIZE)
1266         };
1267
1268         if (!priv || !ssid) {
1269                 IPW_ERROR("Invalid args\n");
1270                 return -1;
1271         }
1272
1273         memcpy(&cmd.param, ssid, cmd.len);
1274         if (ipw_send_cmd(priv, &cmd)) {
1275                 IPW_ERROR("failed to send SSID command\n");
1276                 return -1;
1277         }
1278
1279         return 0;
1280 }
1281
1282 static int ipw_send_adapter_address(struct ipw_priv *priv, u8 * mac)
1283 {
1284         struct host_cmd cmd = {
1285                 .cmd = IPW_CMD_ADAPTER_ADDRESS,
1286                 .len = ETH_ALEN
1287         };
1288
1289         if (!priv || !mac) {
1290                 IPW_ERROR("Invalid args\n");
1291                 return -1;
1292         }
1293
1294         IPW_DEBUG_INFO("%s: Setting MAC to " MAC_FMT "\n",
1295                        priv->net_dev->name, MAC_ARG(mac));
1296
1297         memcpy(&cmd.param, mac, ETH_ALEN);
1298
1299         if (ipw_send_cmd(priv, &cmd)) {
1300                 IPW_ERROR("failed to send ADAPTER_ADDRESS command\n");
1301                 return -1;
1302         }
1303
1304         return 0;
1305 }
1306
1307 static void ipw_adapter_restart(void *adapter)
1308 {
1309         struct ipw_priv *priv = adapter;
1310
1311         if (priv->status & STATUS_RF_KILL_MASK)
1312                 return;
1313
1314         ipw_down(priv);
1315         if (ipw_up(priv)) {
1316                 IPW_ERROR("Failed to up device\n");
1317                 return;
1318         }
1319 }
1320
1321 #define IPW_SCAN_CHECK_WATCHDOG (5 * HZ)
1322
1323 static void ipw_scan_check(void *data)
1324 {
1325         struct ipw_priv *priv = data;
1326         if (priv->status & (STATUS_SCANNING | STATUS_SCAN_ABORTING)) {
1327                 IPW_DEBUG_SCAN("Scan completion watchdog resetting "
1328                                "adapter (%dms).\n",
1329                                IPW_SCAN_CHECK_WATCHDOG / 100);
1330                 ipw_adapter_restart(priv);
1331         }
1332 }
1333
1334 static int ipw_send_scan_request_ext(struct ipw_priv *priv,
1335                                      struct ipw_scan_request_ext *request)
1336 {
1337         struct host_cmd cmd = {
1338                 .cmd = IPW_CMD_SCAN_REQUEST_EXT,
1339                 .len = sizeof(*request)
1340         };
1341
1342         if (!priv || !request) {
1343                 IPW_ERROR("Invalid args\n");
1344                 return -1;
1345         }
1346
1347         memcpy(&cmd.param, request, sizeof(*request));
1348         if (ipw_send_cmd(priv, &cmd)) {
1349                 IPW_ERROR("failed to send SCAN_REQUEST_EXT command\n");
1350                 return -1;
1351         }
1352
1353         queue_delayed_work(priv->workqueue, &priv->scan_check,
1354                            IPW_SCAN_CHECK_WATCHDOG);
1355         return 0;
1356 }
1357
1358 static int ipw_send_scan_abort(struct ipw_priv *priv)
1359 {
1360         struct host_cmd cmd = {
1361                 .cmd = IPW_CMD_SCAN_ABORT,
1362                 .len = 0
1363         };
1364
1365         if (!priv) {
1366                 IPW_ERROR("Invalid args\n");
1367                 return -1;
1368         }
1369
1370         if (ipw_send_cmd(priv, &cmd)) {
1371                 IPW_ERROR("failed to send SCAN_ABORT command\n");
1372                 return -1;
1373         }
1374
1375         return 0;
1376 }
1377
1378 static int ipw_set_sensitivity(struct ipw_priv *priv, u16 sens)
1379 {
1380         struct host_cmd cmd = {
1381                 .cmd = IPW_CMD_SENSITIVITY_CALIB,
1382                 .len = sizeof(struct ipw_sensitivity_calib)
1383         };
1384         struct ipw_sensitivity_calib *calib = (struct ipw_sensitivity_calib *)
1385             &cmd.param;
1386         calib->beacon_rssi_raw = sens;
1387         if (ipw_send_cmd(priv, &cmd)) {
1388                 IPW_ERROR("failed to send SENSITIVITY CALIB command\n");
1389                 return -1;
1390         }
1391
1392         return 0;
1393 }
1394
1395 static int ipw_send_associate(struct ipw_priv *priv,
1396                               struct ipw_associate *associate)
1397 {
1398         struct host_cmd cmd = {
1399                 .cmd = IPW_CMD_ASSOCIATE,
1400                 .len = sizeof(*associate)
1401         };
1402
1403         if (!priv || !associate) {
1404                 IPW_ERROR("Invalid args\n");
1405                 return -1;
1406         }
1407
1408         memcpy(&cmd.param, associate, sizeof(*associate));
1409         if (ipw_send_cmd(priv, &cmd)) {
1410                 IPW_ERROR("failed to send ASSOCIATE command\n");
1411                 return -1;
1412         }
1413
1414         return 0;
1415 }
1416
1417 static int ipw_send_supported_rates(struct ipw_priv *priv,
1418                                     struct ipw_supported_rates *rates)
1419 {
1420         struct host_cmd cmd = {
1421                 .cmd = IPW_CMD_SUPPORTED_RATES,
1422                 .len = sizeof(*rates)
1423         };
1424
1425         if (!priv || !rates) {
1426                 IPW_ERROR("Invalid args\n");
1427                 return -1;
1428         }
1429
1430         memcpy(&cmd.param, rates, sizeof(*rates));
1431         if (ipw_send_cmd(priv, &cmd)) {
1432                 IPW_ERROR("failed to send SUPPORTED_RATES command\n");
1433                 return -1;
1434         }
1435
1436         return 0;
1437 }
1438
1439 static int ipw_set_random_seed(struct ipw_priv *priv)
1440 {
1441         struct host_cmd cmd = {
1442                 .cmd = IPW_CMD_SEED_NUMBER,
1443                 .len = sizeof(u32)
1444         };
1445
1446         if (!priv) {
1447                 IPW_ERROR("Invalid args\n");
1448                 return -1;
1449         }
1450
1451         get_random_bytes(&cmd.param, sizeof(u32));
1452
1453         if (ipw_send_cmd(priv, &cmd)) {
1454                 IPW_ERROR("failed to send SEED_NUMBER command\n");
1455                 return -1;
1456         }
1457
1458         return 0;
1459 }
1460
1461 #if 0
1462 static int ipw_send_card_disable(struct ipw_priv *priv, u32 phy_off)
1463 {
1464         struct host_cmd cmd = {
1465                 .cmd = IPW_CMD_CARD_DISABLE,
1466                 .len = sizeof(u32)
1467         };
1468
1469         if (!priv) {
1470                 IPW_ERROR("Invalid args\n");
1471                 return -1;
1472         }
1473
1474         *((u32 *) & cmd.param) = phy_off;
1475
1476         if (ipw_send_cmd(priv, &cmd)) {
1477                 IPW_ERROR("failed to send CARD_DISABLE command\n");
1478                 return -1;
1479         }
1480
1481         return 0;
1482 }
1483 #endif
1484
1485 static int ipw_send_tx_power(struct ipw_priv *priv, struct ipw_tx_power *power)
1486 {
1487         struct host_cmd cmd = {
1488                 .cmd = IPW_CMD_TX_POWER,
1489                 .len = sizeof(*power)
1490         };
1491
1492         if (!priv || !power) {
1493                 IPW_ERROR("Invalid args\n");
1494                 return -1;
1495         }
1496
1497         memcpy(&cmd.param, power, sizeof(*power));
1498         if (ipw_send_cmd(priv, &cmd)) {
1499                 IPW_ERROR("failed to send TX_POWER command\n");
1500                 return -1;
1501         }
1502
1503         return 0;
1504 }
1505
1506 static int ipw_send_rts_threshold(struct ipw_priv *priv, u16 rts)
1507 {
1508         struct ipw_rts_threshold rts_threshold = {
1509                 .rts_threshold = rts,
1510         };
1511         struct host_cmd cmd = {
1512                 .cmd = IPW_CMD_RTS_THRESHOLD,
1513                 .len = sizeof(rts_threshold)
1514         };
1515
1516         if (!priv) {
1517                 IPW_ERROR("Invalid args\n");
1518                 return -1;
1519         }
1520
1521         memcpy(&cmd.param, &rts_threshold, sizeof(rts_threshold));
1522         if (ipw_send_cmd(priv, &cmd)) {
1523                 IPW_ERROR("failed to send RTS_THRESHOLD command\n");
1524                 return -1;
1525         }
1526
1527         return 0;
1528 }
1529
1530 static int ipw_send_frag_threshold(struct ipw_priv *priv, u16 frag)
1531 {
1532         struct ipw_frag_threshold frag_threshold = {
1533                 .frag_threshold = frag,
1534         };
1535         struct host_cmd cmd = {
1536                 .cmd = IPW_CMD_FRAG_THRESHOLD,
1537                 .len = sizeof(frag_threshold)
1538         };
1539
1540         if (!priv) {
1541                 IPW_ERROR("Invalid args\n");
1542                 return -1;
1543         }
1544
1545         memcpy(&cmd.param, &frag_threshold, sizeof(frag_threshold));
1546         if (ipw_send_cmd(priv, &cmd)) {
1547                 IPW_ERROR("failed to send FRAG_THRESHOLD command\n");
1548                 return -1;
1549         }
1550
1551         return 0;
1552 }
1553
1554 static int ipw_send_power_mode(struct ipw_priv *priv, u32 mode)
1555 {
1556         struct host_cmd cmd = {
1557                 .cmd = IPW_CMD_POWER_MODE,
1558                 .len = sizeof(u32)
1559         };
1560         u32 *param = (u32 *) (&cmd.param);
1561
1562         if (!priv) {
1563                 IPW_ERROR("Invalid args\n");
1564                 return -1;
1565         }
1566
1567         /* If on battery, set to 3, if AC set to CAM, else user
1568          * level */
1569         switch (mode) {
1570         case IPW_POWER_BATTERY:
1571                 *param = IPW_POWER_INDEX_3;
1572                 break;
1573         case IPW_POWER_AC:
1574                 *param = IPW_POWER_MODE_CAM;
1575                 break;
1576         default:
1577                 *param = mode;
1578                 break;
1579         }
1580
1581         if (ipw_send_cmd(priv, &cmd)) {
1582                 IPW_ERROR("failed to send POWER_MODE command\n");
1583                 return -1;
1584         }
1585
1586         return 0;
1587 }
1588
1589 /*
1590  * The IPW device contains a Microwire compatible EEPROM that stores
1591  * various data like the MAC address.  Usually the firmware has exclusive
1592  * access to the eeprom, but during device initialization (before the
1593  * device driver has sent the HostComplete command to the firmware) the
1594  * device driver has read access to the EEPROM by way of indirect addressing
1595  * through a couple of memory mapped registers.
1596  *
1597  * The following is a simplified implementation for pulling data out of the
1598  * the eeprom, along with some helper functions to find information in
1599  * the per device private data's copy of the eeprom.
1600  *
1601  * NOTE: To better understand how these functions work (i.e what is a chip
1602  *       select and why do have to keep driving the eeprom clock?), read
1603  *       just about any data sheet for a Microwire compatible EEPROM.
1604  */
1605
1606 /* write a 32 bit value into the indirect accessor register */
1607 static inline void eeprom_write_reg(struct ipw_priv *p, u32 data)
1608 {
1609         ipw_write_reg32(p, FW_MEM_REG_EEPROM_ACCESS, data);
1610
1611         /* the eeprom requires some time to complete the operation */
1612         udelay(p->eeprom_delay);
1613
1614         return;
1615 }
1616
1617 /* perform a chip select operation */
1618 static inline void eeprom_cs(struct ipw_priv *priv)
1619 {
1620         eeprom_write_reg(priv, 0);
1621         eeprom_write_reg(priv, EEPROM_BIT_CS);
1622         eeprom_write_reg(priv, EEPROM_BIT_CS | EEPROM_BIT_SK);
1623         eeprom_write_reg(priv, EEPROM_BIT_CS);
1624 }
1625
1626 /* perform a chip select operation */
1627 static inline void eeprom_disable_cs(struct ipw_priv *priv)
1628 {
1629         eeprom_write_reg(priv, EEPROM_BIT_CS);
1630         eeprom_write_reg(priv, 0);
1631         eeprom_write_reg(priv, EEPROM_BIT_SK);
1632 }
1633
1634 /* push a single bit down to the eeprom */
1635 static inline void eeprom_write_bit(struct ipw_priv *p, u8 bit)
1636 {
1637         int d = (bit ? EEPROM_BIT_DI : 0);
1638         eeprom_write_reg(p, EEPROM_BIT_CS | d);
1639         eeprom_write_reg(p, EEPROM_BIT_CS | d | EEPROM_BIT_SK);
1640 }
1641
1642 /* push an opcode followed by an address down to the eeprom */
1643 static void eeprom_op(struct ipw_priv *priv, u8 op, u8 addr)
1644 {
1645         int i;
1646
1647         eeprom_cs(priv);
1648         eeprom_write_bit(priv, 1);
1649         eeprom_write_bit(priv, op & 2);
1650         eeprom_write_bit(priv, op & 1);
1651         for (i = 7; i >= 0; i--) {
1652                 eeprom_write_bit(priv, addr & (1 << i));
1653         }
1654 }
1655
1656 /* pull 16 bits off the eeprom, one bit at a time */
1657 static u16 eeprom_read_u16(struct ipw_priv *priv, u8 addr)
1658 {
1659         int i;
1660         u16 r = 0;
1661
1662         /* Send READ Opcode */
1663         eeprom_op(priv, EEPROM_CMD_READ, addr);
1664
1665         /* Send dummy bit */
1666         eeprom_write_reg(priv, EEPROM_BIT_CS);
1667
1668         /* Read the byte off the eeprom one bit at a time */
1669         for (i = 0; i < 16; i++) {
1670                 u32 data = 0;
1671                 eeprom_write_reg(priv, EEPROM_BIT_CS | EEPROM_BIT_SK);
1672                 eeprom_write_reg(priv, EEPROM_BIT_CS);
1673                 data = ipw_read_reg32(priv, FW_MEM_REG_EEPROM_ACCESS);
1674                 r = (r << 1) | ((data & EEPROM_BIT_DO) ? 1 : 0);
1675         }
1676
1677         /* Send another dummy bit */
1678         eeprom_write_reg(priv, 0);
1679         eeprom_disable_cs(priv);
1680
1681         return r;
1682 }
1683
1684 /* helper function for pulling the mac address out of the private */
1685 /* data's copy of the eeprom data                                 */
1686 static void eeprom_parse_mac(struct ipw_priv *priv, u8 * mac)
1687 {
1688         u8 *ee = (u8 *) priv->eeprom;
1689         memcpy(mac, &ee[EEPROM_MAC_ADDRESS], 6);
1690 }
1691
1692 /*
1693  * Either the device driver (i.e. the host) or the firmware can
1694  * load eeprom data into the designated region in SRAM.  If neither
1695  * happens then the FW will shutdown with a fatal error.
1696  *
1697  * In order to signal the FW to load the EEPROM, the EEPROM_LOAD_DISABLE
1698  * bit needs region of shared SRAM needs to be non-zero.
1699  */
1700 static void ipw_eeprom_init_sram(struct ipw_priv *priv)
1701 {
1702         int i;
1703         u16 *eeprom = (u16 *) priv->eeprom;
1704
1705         IPW_DEBUG_TRACE(">>\n");
1706
1707         /* read entire contents of eeprom into private buffer */
1708         for (i = 0; i < 128; i++)
1709                 eeprom[i] = eeprom_read_u16(priv, (u8) i);
1710
1711         /*
1712            If the data looks correct, then copy it to our private
1713            copy.  Otherwise let the firmware know to perform the operation
1714            on it's own
1715          */
1716         if ((priv->eeprom + EEPROM_VERSION) != 0) {
1717                 IPW_DEBUG_INFO("Writing EEPROM data into SRAM\n");
1718
1719                 /* write the eeprom data to sram */
1720                 for (i = 0; i < CX2_EEPROM_IMAGE_SIZE; i++)
1721                         ipw_write8(priv, IPW_EEPROM_DATA + i, priv->eeprom[i]);
1722
1723                 /* Do not load eeprom data on fatal error or suspend */
1724                 ipw_write32(priv, IPW_EEPROM_LOAD_DISABLE, 0);
1725         } else {
1726                 IPW_DEBUG_INFO("Enabling FW initializationg of SRAM\n");
1727
1728                 /* Load eeprom data on fatal error or suspend */
1729                 ipw_write32(priv, IPW_EEPROM_LOAD_DISABLE, 1);
1730         }
1731
1732         IPW_DEBUG_TRACE("<<\n");
1733 }
1734
1735 static inline void ipw_zero_memory(struct ipw_priv *priv, u32 start, u32 count)
1736 {
1737         count >>= 2;
1738         if (!count)
1739                 return;
1740         _ipw_write32(priv, CX2_AUTOINC_ADDR, start);
1741         while (count--)
1742                 _ipw_write32(priv, CX2_AUTOINC_DATA, 0);
1743 }
1744
1745 static inline void ipw_fw_dma_reset_command_blocks(struct ipw_priv *priv)
1746 {
1747         ipw_zero_memory(priv, CX2_SHARED_SRAM_DMA_CONTROL,
1748                         CB_NUMBER_OF_ELEMENTS_SMALL *
1749                         sizeof(struct command_block));
1750 }
1751
1752 static int ipw_fw_dma_enable(struct ipw_priv *priv)
1753 {                               /* start dma engine but no transfers yet */
1754
1755         IPW_DEBUG_FW(">> : \n");
1756
1757         /* Start the dma */
1758         ipw_fw_dma_reset_command_blocks(priv);
1759
1760         /* Write CB base address */
1761         ipw_write_reg32(priv, CX2_DMA_I_CB_BASE, CX2_SHARED_SRAM_DMA_CONTROL);
1762
1763         IPW_DEBUG_FW("<< : \n");
1764         return 0;
1765 }
1766
1767 static void ipw_fw_dma_abort(struct ipw_priv *priv)
1768 {
1769         u32 control = 0;
1770
1771         IPW_DEBUG_FW(">> :\n");
1772
1773         //set the Stop and Abort bit
1774         control = DMA_CONTROL_SMALL_CB_CONST_VALUE | DMA_CB_STOP_AND_ABORT;
1775         ipw_write_reg32(priv, CX2_DMA_I_DMA_CONTROL, control);
1776         priv->sram_desc.last_cb_index = 0;
1777
1778         IPW_DEBUG_FW("<< \n");
1779 }
1780
1781 static int ipw_fw_dma_write_command_block(struct ipw_priv *priv, int index,
1782                                           struct command_block *cb)
1783 {
1784         u32 address =
1785             CX2_SHARED_SRAM_DMA_CONTROL +
1786             (sizeof(struct command_block) * index);
1787         IPW_DEBUG_FW(">> :\n");
1788
1789         ipw_write_indirect(priv, address, (u8 *) cb,
1790                            (int)sizeof(struct command_block));
1791
1792         IPW_DEBUG_FW("<< :\n");
1793         return 0;
1794
1795 }
1796
1797 static int ipw_fw_dma_kick(struct ipw_priv *priv)
1798 {
1799         u32 control = 0;
1800         u32 index = 0;
1801
1802         IPW_DEBUG_FW(">> :\n");
1803
1804         for (index = 0; index < priv->sram_desc.last_cb_index; index++)
1805                 ipw_fw_dma_write_command_block(priv, index,
1806                                                &priv->sram_desc.cb_list[index]);
1807
1808         /* Enable the DMA in the CSR register */
1809         ipw_clear_bit(priv, CX2_RESET_REG,
1810                       CX2_RESET_REG_MASTER_DISABLED |
1811                       CX2_RESET_REG_STOP_MASTER);
1812
1813         /* Set the Start bit. */
1814         control = DMA_CONTROL_SMALL_CB_CONST_VALUE | DMA_CB_START;
1815         ipw_write_reg32(priv, CX2_DMA_I_DMA_CONTROL, control);
1816
1817         IPW_DEBUG_FW("<< :\n");
1818         return 0;
1819 }
1820
1821 static void ipw_fw_dma_dump_command_block(struct ipw_priv *priv)
1822 {
1823         u32 address;
1824         u32 register_value = 0;
1825         u32 cb_fields_address = 0;
1826
1827         IPW_DEBUG_FW(">> :\n");
1828         address = ipw_read_reg32(priv, CX2_DMA_I_CURRENT_CB);
1829         IPW_DEBUG_FW_INFO("Current CB is 0x%x \n", address);
1830
1831         /* Read the DMA Controlor register */
1832         register_value = ipw_read_reg32(priv, CX2_DMA_I_DMA_CONTROL);
1833         IPW_DEBUG_FW_INFO("CX2_DMA_I_DMA_CONTROL is 0x%x \n", register_value);
1834
1835         /* Print the CB values */
1836         cb_fields_address = address;
1837         register_value = ipw_read_reg32(priv, cb_fields_address);
1838         IPW_DEBUG_FW_INFO("Current CB ControlField is 0x%x \n", register_value);
1839
1840         cb_fields_address += sizeof(u32);
1841         register_value = ipw_read_reg32(priv, cb_fields_address);
1842         IPW_DEBUG_FW_INFO("Current CB Source Field is 0x%x \n", register_value);
1843
1844         cb_fields_address += sizeof(u32);
1845         register_value = ipw_read_reg32(priv, cb_fields_address);
1846         IPW_DEBUG_FW_INFO("Current CB Destination Field is 0x%x \n",
1847                           register_value);
1848
1849         cb_fields_address += sizeof(u32);
1850         register_value = ipw_read_reg32(priv, cb_fields_address);
1851         IPW_DEBUG_FW_INFO("Current CB Status Field is 0x%x \n", register_value);
1852
1853         IPW_DEBUG_FW(">> :\n");
1854 }
1855
1856 static int ipw_fw_dma_command_block_index(struct ipw_priv *priv)
1857 {
1858         u32 current_cb_address = 0;
1859         u32 current_cb_index = 0;
1860
1861         IPW_DEBUG_FW("<< :\n");
1862         current_cb_address = ipw_read_reg32(priv, CX2_DMA_I_CURRENT_CB);
1863
1864         current_cb_index = (current_cb_address - CX2_SHARED_SRAM_DMA_CONTROL) /
1865             sizeof(struct command_block);
1866
1867         IPW_DEBUG_FW_INFO("Current CB index 0x%x address = 0x%X \n",
1868                           current_cb_index, current_cb_address);
1869
1870         IPW_DEBUG_FW(">> :\n");
1871         return current_cb_index;
1872
1873 }
1874
1875 static int ipw_fw_dma_add_command_block(struct ipw_priv *priv,
1876                                         u32 src_address,
1877                                         u32 dest_address,
1878                                         u32 length,
1879                                         int interrupt_enabled, int is_last)
1880 {
1881
1882         u32 control = CB_VALID | CB_SRC_LE | CB_DEST_LE | CB_SRC_AUTOINC |
1883             CB_SRC_IO_GATED | CB_DEST_AUTOINC | CB_SRC_SIZE_LONG |
1884             CB_DEST_SIZE_LONG;
1885         struct command_block *cb;
1886         u32 last_cb_element = 0;
1887
1888         IPW_DEBUG_FW_INFO("src_address=0x%x dest_address=0x%x length=0x%x\n",
1889                           src_address, dest_address, length);
1890
1891         if (priv->sram_desc.last_cb_index >= CB_NUMBER_OF_ELEMENTS_SMALL)
1892                 return -1;
1893
1894         last_cb_element = priv->sram_desc.last_cb_index;
1895         cb = &priv->sram_desc.cb_list[last_cb_element];
1896         priv->sram_desc.last_cb_index++;
1897
1898         /* Calculate the new CB control word */
1899         if (interrupt_enabled)
1900                 control |= CB_INT_ENABLED;
1901
1902         if (is_last)
1903                 control |= CB_LAST_VALID;
1904
1905         control |= length;
1906
1907         /* Calculate the CB Element's checksum value */
1908         cb->status = control ^ src_address ^ dest_address;
1909
1910         /* Copy the Source and Destination addresses */
1911         cb->dest_addr = dest_address;
1912         cb->source_addr = src_address;
1913
1914         /* Copy the Control Word last */
1915         cb->control = control;
1916
1917         return 0;
1918 }
1919
1920 static int ipw_fw_dma_add_buffer(struct ipw_priv *priv,
1921                                  u32 src_phys, u32 dest_address, u32 length)
1922 {
1923         u32 bytes_left = length;
1924         u32 src_offset = 0;
1925         u32 dest_offset = 0;
1926         int status = 0;
1927         IPW_DEBUG_FW(">> \n");
1928         IPW_DEBUG_FW_INFO("src_phys=0x%x dest_address=0x%x length=0x%x\n",
1929                           src_phys, dest_address, length);
1930         while (bytes_left > CB_MAX_LENGTH) {
1931                 status = ipw_fw_dma_add_command_block(priv,
1932                                                       src_phys + src_offset,
1933                                                       dest_address +
1934                                                       dest_offset,
1935                                                       CB_MAX_LENGTH, 0, 0);
1936                 if (status) {
1937                         IPW_DEBUG_FW_INFO(": Failed\n");
1938                         return -1;
1939                 } else
1940                         IPW_DEBUG_FW_INFO(": Added new cb\n");
1941
1942                 src_offset += CB_MAX_LENGTH;
1943                 dest_offset += CB_MAX_LENGTH;
1944                 bytes_left -= CB_MAX_LENGTH;
1945         }
1946
1947         /* add the buffer tail */
1948         if (bytes_left > 0) {
1949                 status =
1950                     ipw_fw_dma_add_command_block(priv, src_phys + src_offset,
1951                                                  dest_address + dest_offset,
1952                                                  bytes_left, 0, 0);
1953                 if (status) {
1954                         IPW_DEBUG_FW_INFO(": Failed on the buffer tail\n");
1955                         return -1;
1956                 } else
1957                         IPW_DEBUG_FW_INFO
1958                             (": Adding new cb - the buffer tail\n");
1959         }
1960
1961         IPW_DEBUG_FW("<< \n");
1962         return 0;
1963 }
1964
1965 static int ipw_fw_dma_wait(struct ipw_priv *priv)
1966 {
1967         u32 current_index = 0;
1968         u32 watchdog = 0;
1969
1970         IPW_DEBUG_FW(">> : \n");
1971
1972         current_index = ipw_fw_dma_command_block_index(priv);
1973         IPW_DEBUG_FW_INFO("sram_desc.last_cb_index:0x%8X\n",
1974                           (int)priv->sram_desc.last_cb_index);
1975
1976         while (current_index < priv->sram_desc.last_cb_index) {
1977                 udelay(50);
1978                 current_index = ipw_fw_dma_command_block_index(priv);
1979
1980                 watchdog++;
1981
1982                 if (watchdog > 400) {
1983                         IPW_DEBUG_FW_INFO("Timeout\n");
1984                         ipw_fw_dma_dump_command_block(priv);
1985                         ipw_fw_dma_abort(priv);
1986                         return -1;
1987                 }
1988         }
1989
1990         ipw_fw_dma_abort(priv);
1991
1992         /*Disable the DMA in the CSR register */
1993         ipw_set_bit(priv, CX2_RESET_REG,
1994                     CX2_RESET_REG_MASTER_DISABLED | CX2_RESET_REG_STOP_MASTER);
1995
1996         IPW_DEBUG_FW("<< dmaWaitSync \n");
1997         return 0;
1998 }
1999
2000 static void ipw_remove_current_network(struct ipw_priv *priv)
2001 {
2002         struct list_head *element, *safe;
2003         struct ieee80211_network *network = NULL;
2004         list_for_each_safe(element, safe, &priv->ieee->network_list) {
2005                 network = list_entry(element, struct ieee80211_network, list);
2006                 if (!memcmp(network->bssid, priv->bssid, ETH_ALEN)) {
2007                         list_del(element);
2008                         list_add_tail(&network->list,
2009                                       &priv->ieee->network_free_list);
2010                 }
2011         }
2012 }
2013
2014 /**
2015  * Check that card is still alive.
2016  * Reads debug register from domain0.
2017  * If card is present, pre-defined value should
2018  * be found there.
2019  *
2020  * @param priv
2021  * @return 1 if card is present, 0 otherwise
2022  */
2023 static inline int ipw_alive(struct ipw_priv *priv)
2024 {
2025         return ipw_read32(priv, 0x90) == 0xd55555d5;
2026 }
2027
2028 static inline int ipw_poll_bit(struct ipw_priv *priv, u32 addr, u32 mask,
2029                                int timeout)
2030 {
2031         int i = 0;
2032
2033         do {
2034                 if ((ipw_read32(priv, addr) & mask) == mask)
2035                         return i;
2036                 mdelay(10);
2037                 i += 10;
2038         } while (i < timeout);
2039
2040         return -ETIME;
2041 }
2042
2043 /* These functions load the firmware and micro code for the operation of
2044  * the ipw hardware.  It assumes the buffer has all the bits for the
2045  * image and the caller is handling the memory allocation and clean up.
2046  */
2047
2048 static int ipw_stop_master(struct ipw_priv *priv)
2049 {
2050         int rc;
2051
2052         IPW_DEBUG_TRACE(">> \n");
2053         /* stop master. typical delay - 0 */
2054         ipw_set_bit(priv, CX2_RESET_REG, CX2_RESET_REG_STOP_MASTER);
2055
2056         rc = ipw_poll_bit(priv, CX2_RESET_REG,
2057                           CX2_RESET_REG_MASTER_DISABLED, 100);
2058         if (rc < 0) {
2059                 IPW_ERROR("stop master failed in 10ms\n");
2060                 return -1;
2061         }
2062
2063         IPW_DEBUG_INFO("stop master %dms\n", rc);
2064
2065         return rc;
2066 }
2067
2068 static void ipw_arc_release(struct ipw_priv *priv)
2069 {
2070         IPW_DEBUG_TRACE(">> \n");
2071         mdelay(5);
2072
2073         ipw_clear_bit(priv, CX2_RESET_REG, CBD_RESET_REG_PRINCETON_RESET);
2074
2075         /* no one knows timing, for safety add some delay */
2076         mdelay(5);
2077 }
2078
2079 struct fw_header {
2080         u32 version;
2081         u32 mode;
2082 };
2083
2084 struct fw_chunk {
2085         u32 address;
2086         u32 length;
2087 };
2088
2089 #define IPW_FW_MAJOR_VERSION 2
2090 #define IPW_FW_MINOR_VERSION 2
2091
2092 #define IPW_FW_MINOR(x) ((x & 0xff) >> 8)
2093 #define IPW_FW_MAJOR(x) (x & 0xff)
2094
2095 #define IPW_FW_VERSION ((IPW_FW_MINOR_VERSION << 8) | \
2096                          IPW_FW_MAJOR_VERSION)
2097
2098 #define IPW_FW_PREFIX "ipw-" __stringify(IPW_FW_MAJOR_VERSION) \
2099 "." __stringify(IPW_FW_MINOR_VERSION) "-"
2100
2101 #if IPW_FW_MAJOR_VERSION >= 2 && IPW_FW_MINOR_VERSION > 0
2102 #define IPW_FW_NAME(x) IPW_FW_PREFIX "" x ".fw"
2103 #else
2104 #define IPW_FW_NAME(x) "ipw2200_" x ".fw"
2105 #endif
2106
2107 static int ipw_load_ucode(struct ipw_priv *priv, u8 * data, size_t len)
2108 {
2109         int rc = 0, i, addr;
2110         u8 cr = 0;
2111         u16 *image;
2112
2113         image = (u16 *) data;
2114
2115         IPW_DEBUG_TRACE(">> \n");
2116
2117         rc = ipw_stop_master(priv);
2118
2119         if (rc < 0)
2120                 return rc;
2121
2122 //      spin_lock_irqsave(&priv->lock, flags);
2123
2124         for (addr = CX2_SHARED_LOWER_BOUND;
2125              addr < CX2_REGISTER_DOMAIN1_END; addr += 4) {
2126                 ipw_write32(priv, addr, 0);
2127         }
2128
2129         /* no ucode (yet) */
2130         memset(&priv->dino_alive, 0, sizeof(priv->dino_alive));
2131         /* destroy DMA queues */
2132         /* reset sequence */
2133
2134         ipw_write_reg32(priv, CX2_MEM_HALT_AND_RESET, CX2_BIT_HALT_RESET_ON);
2135         ipw_arc_release(priv);
2136         ipw_write_reg32(priv, CX2_MEM_HALT_AND_RESET, CX2_BIT_HALT_RESET_OFF);
2137         mdelay(1);
2138
2139         /* reset PHY */
2140         ipw_write_reg32(priv, CX2_INTERNAL_CMD_EVENT, CX2_BASEBAND_POWER_DOWN);
2141         mdelay(1);
2142
2143         ipw_write_reg32(priv, CX2_INTERNAL_CMD_EVENT, 0);
2144         mdelay(1);
2145
2146         /* enable ucode store */
2147         ipw_write_reg8(priv, DINO_CONTROL_REG, 0x0);
2148         ipw_write_reg8(priv, DINO_CONTROL_REG, DINO_ENABLE_CS);
2149         mdelay(1);
2150
2151         /* write ucode */
2152         /**
2153          * @bug
2154          * Do NOT set indirect address register once and then
2155          * store data to indirect data register in the loop.
2156          * It seems very reasonable, but in this case DINO do not
2157          * accept ucode. It is essential to set address each time.
2158          */
2159         /* load new ipw uCode */
2160         for (i = 0; i < len / 2; i++)
2161                 ipw_write_reg16(priv, CX2_BASEBAND_CONTROL_STORE, image[i]);
2162
2163         /* enable DINO */
2164         ipw_write_reg8(priv, CX2_BASEBAND_CONTROL_STATUS, 0);
2165         ipw_write_reg8(priv, CX2_BASEBAND_CONTROL_STATUS, DINO_ENABLE_SYSTEM);
2166
2167         /* this is where the igx / win driver deveates from the VAP driver. */
2168
2169         /* wait for alive response */
2170         for (i = 0; i < 100; i++) {
2171                 /* poll for incoming data */
2172                 cr = ipw_read_reg8(priv, CX2_BASEBAND_CONTROL_STATUS);
2173                 if (cr & DINO_RXFIFO_DATA)
2174                         break;
2175                 mdelay(1);
2176         }
2177
2178         if (cr & DINO_RXFIFO_DATA) {
2179                 /* alive_command_responce size is NOT multiple of 4 */
2180                 u32 response_buffer[(sizeof(priv->dino_alive) + 3) / 4];
2181
2182                 for (i = 0; i < ARRAY_SIZE(response_buffer); i++)
2183                         response_buffer[i] =
2184                             ipw_read_reg32(priv, CX2_BASEBAND_RX_FIFO_READ);
2185                 memcpy(&priv->dino_alive, response_buffer,
2186                        sizeof(priv->dino_alive));
2187                 if (priv->dino_alive.alive_command == 1
2188                     && priv->dino_alive.ucode_valid == 1) {
2189                         rc = 0;
2190                         IPW_DEBUG_INFO
2191                             ("Microcode OK, rev. %d (0x%x) dev. %d (0x%x) "
2192                              "of %02d/%02d/%02d %02d:%02d\n",
2193                              priv->dino_alive.software_revision,
2194                              priv->dino_alive.software_revision,
2195                              priv->dino_alive.device_identifier,
2196                              priv->dino_alive.device_identifier,
2197                              priv->dino_alive.time_stamp[0],
2198                              priv->dino_alive.time_stamp[1],
2199                              priv->dino_alive.time_stamp[2],
2200                              priv->dino_alive.time_stamp[3],
2201                              priv->dino_alive.time_stamp[4]);
2202                 } else {
2203                         IPW_DEBUG_INFO("Microcode is not alive\n");
2204                         rc = -EINVAL;
2205                 }
2206         } else {
2207                 IPW_DEBUG_INFO("No alive response from DINO\n");
2208                 rc = -ETIME;
2209         }
2210
2211         /* disable DINO, otherwise for some reason
2212            firmware have problem getting alive resp. */
2213         ipw_write_reg8(priv, CX2_BASEBAND_CONTROL_STATUS, 0);
2214
2215 //      spin_unlock_irqrestore(&priv->lock, flags);
2216
2217         return rc;
2218 }
2219
2220 static int ipw_load_firmware(struct ipw_priv *priv, u8 * data, size_t len)
2221 {
2222         int rc = -1;
2223         int offset = 0;
2224         struct fw_chunk *chunk;
2225         dma_addr_t shared_phys;
2226         u8 *shared_virt;
2227
2228         IPW_DEBUG_TRACE("<< : \n");
2229         shared_virt = pci_alloc_consistent(priv->pci_dev, len, &shared_phys);
2230
2231         if (!shared_virt)
2232                 return -ENOMEM;
2233
2234         memmove(shared_virt, data, len);
2235
2236         /* Start the Dma */
2237         rc = ipw_fw_dma_enable(priv);
2238
2239         if (priv->sram_desc.last_cb_index > 0) {
2240                 /* the DMA is already ready this would be a bug. */
2241                 BUG();
2242                 goto out;
2243         }
2244
2245         do {
2246                 chunk = (struct fw_chunk *)(data + offset);
2247                 offset += sizeof(struct fw_chunk);
2248                 /* build DMA packet and queue up for sending */
2249                 /* dma to chunk->address, the chunk->length bytes from data +
2250                  * offeset*/
2251                 /* Dma loading */
2252                 rc = ipw_fw_dma_add_buffer(priv, shared_phys + offset,
2253                                            chunk->address, chunk->length);
2254                 if (rc) {
2255                         IPW_DEBUG_INFO("dmaAddBuffer Failed\n");
2256                         goto out;
2257                 }
2258
2259                 offset += chunk->length;
2260         } while (offset < len);
2261
2262         /* Run the DMA and wait for the answer */
2263         rc = ipw_fw_dma_kick(priv);
2264         if (rc) {
2265                 IPW_ERROR("dmaKick Failed\n");
2266                 goto out;
2267         }
2268
2269         rc = ipw_fw_dma_wait(priv);
2270         if (rc) {
2271                 IPW_ERROR("dmaWaitSync Failed\n");
2272                 goto out;
2273         }
2274       out:
2275         pci_free_consistent(priv->pci_dev, len, shared_virt, shared_phys);
2276         return rc;
2277 }
2278
2279 /* stop nic */
2280 static int ipw_stop_nic(struct ipw_priv *priv)
2281 {
2282         int rc = 0;
2283
2284         /* stop */
2285         ipw_write32(priv, CX2_RESET_REG, CX2_RESET_REG_STOP_MASTER);
2286
2287         rc = ipw_poll_bit(priv, CX2_RESET_REG,
2288                           CX2_RESET_REG_MASTER_DISABLED, 500);
2289         if (rc < 0) {
2290                 IPW_ERROR("wait for reg master disabled failed\n");
2291                 return rc;
2292         }
2293
2294         ipw_set_bit(priv, CX2_RESET_REG, CBD_RESET_REG_PRINCETON_RESET);
2295
2296         return rc;
2297 }
2298
2299 static void ipw_start_nic(struct ipw_priv *priv)
2300 {
2301         IPW_DEBUG_TRACE(">>\n");
2302
2303         /* prvHwStartNic  release ARC */
2304         ipw_clear_bit(priv, CX2_RESET_REG,
2305                       CX2_RESET_REG_MASTER_DISABLED |
2306                       CX2_RESET_REG_STOP_MASTER |
2307                       CBD_RESET_REG_PRINCETON_RESET);
2308
2309         /* enable power management */
2310         ipw_set_bit(priv, CX2_GP_CNTRL_RW,
2311                     CX2_GP_CNTRL_BIT_HOST_ALLOWS_STANDBY);
2312
2313         IPW_DEBUG_TRACE("<<\n");
2314 }
2315
2316 static int ipw_init_nic(struct ipw_priv *priv)
2317 {
2318         int rc;
2319
2320         IPW_DEBUG_TRACE(">>\n");
2321         /* reset */
2322         /*prvHwInitNic */
2323         /* set "initialization complete" bit to move adapter to D0 state */
2324         ipw_set_bit(priv, CX2_GP_CNTRL_RW, CX2_GP_CNTRL_BIT_INIT_DONE);
2325
2326         /* low-level PLL activation */
2327         ipw_write32(priv, CX2_READ_INT_REGISTER,
2328                     CX2_BIT_INT_HOST_SRAM_READ_INT_REGISTER);
2329
2330         /* wait for clock stabilization */
2331         rc = ipw_poll_bit(priv, CX2_GP_CNTRL_RW,
2332                           CX2_GP_CNTRL_BIT_CLOCK_READY, 250);
2333         if (rc < 0)
2334                 IPW_DEBUG_INFO("FAILED wait for clock stablization\n");
2335
2336         /* assert SW reset */
2337         ipw_set_bit(priv, CX2_RESET_REG, CX2_RESET_REG_SW_RESET);
2338
2339         udelay(10);
2340
2341         /* set "initialization complete" bit to move adapter to D0 state */
2342         ipw_set_bit(priv, CX2_GP_CNTRL_RW, CX2_GP_CNTRL_BIT_INIT_DONE);
2343
2344         IPW_DEBUG_TRACE(">>\n");
2345         return 0;
2346 }
2347
2348 /* Call this function from process context, it will sleep in request_firmware.
2349  * Probe is an ok place to call this from.
2350  */
2351 static int ipw_reset_nic(struct ipw_priv *priv)
2352 {
2353         int rc = 0;
2354
2355         IPW_DEBUG_TRACE(">>\n");
2356
2357         rc = ipw_init_nic(priv);
2358
2359         /* Clear the 'host command active' bit... */
2360         priv->status &= ~STATUS_HCMD_ACTIVE;
2361         wake_up_interruptible(&priv->wait_command_queue);
2362
2363         IPW_DEBUG_TRACE("<<\n");
2364         return rc;
2365 }
2366
2367 static int ipw_get_fw(struct ipw_priv *priv,
2368                       const struct firmware **fw, const char *name)
2369 {
2370         struct fw_header *header;
2371         int rc;
2372
2373         /* ask firmware_class module to get the boot firmware off disk */
2374         rc = request_firmware(fw, name, &priv->pci_dev->dev);
2375         if (rc < 0) {
2376                 IPW_ERROR("%s load failed: Reason %d\n", name, rc);
2377                 return rc;
2378         }
2379
2380         header = (struct fw_header *)(*fw)->data;
2381         if (IPW_FW_MAJOR(header->version) != IPW_FW_MAJOR_VERSION) {
2382                 IPW_ERROR("'%s' firmware version not compatible (%d != %d)\n",
2383                           name,
2384                           IPW_FW_MAJOR(header->version), IPW_FW_MAJOR_VERSION);
2385                 return -EINVAL;
2386         }
2387
2388         IPW_DEBUG_INFO("Loading firmware '%s' file v%d.%d (%zd bytes)\n",
2389                        name,
2390                        IPW_FW_MAJOR(header->version),
2391                        IPW_FW_MINOR(header->version),
2392                        (*fw)->size - sizeof(struct fw_header));
2393         return 0;
2394 }
2395
2396 #define CX2_RX_BUF_SIZE (3000)
2397
2398 static inline void ipw_rx_queue_reset(struct ipw_priv *priv,
2399                                       struct ipw_rx_queue *rxq)
2400 {
2401         unsigned long flags;
2402         int i;
2403
2404         spin_lock_irqsave(&rxq->lock, flags);
2405
2406         INIT_LIST_HEAD(&rxq->rx_free);
2407         INIT_LIST_HEAD(&rxq->rx_used);
2408
2409         /* Fill the rx_used queue with _all_ of the Rx buffers */
2410         for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
2411                 /* In the reset function, these buffers may have been allocated
2412                  * to an SKB, so we need to unmap and free potential storage */
2413                 if (rxq->pool[i].skb != NULL) {
2414                         pci_unmap_single(priv->pci_dev, rxq->pool[i].dma_addr,
2415                                          CX2_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
2416                         dev_kfree_skb(rxq->pool[i].skb);
2417                 }
2418                 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
2419         }
2420
2421         /* Set us so that we have processed and used all buffers, but have
2422          * not restocked the Rx queue with fresh buffers */
2423         rxq->read = rxq->write = 0;
2424         rxq->processed = RX_QUEUE_SIZE - 1;
2425         rxq->free_count = 0;
2426         spin_unlock_irqrestore(&rxq->lock, flags);
2427 }
2428
2429 #ifdef CONFIG_PM
2430 static int fw_loaded = 0;
2431 static const struct firmware *bootfw = NULL;
2432 static const struct firmware *firmware = NULL;
2433 static const struct firmware *ucode = NULL;
2434 #endif
2435
2436 static int ipw_load(struct ipw_priv *priv)
2437 {
2438 #ifndef CONFIG_PM
2439         const struct firmware *bootfw = NULL;
2440         const struct firmware *firmware = NULL;
2441         const struct firmware *ucode = NULL;
2442 #endif
2443         int rc = 0, retries = 3;
2444
2445 #ifdef CONFIG_PM
2446         if (!fw_loaded) {
2447 #endif
2448                 rc = ipw_get_fw(priv, &bootfw, IPW_FW_NAME("boot"));
2449                 if (rc)
2450                         goto error;
2451
2452                 switch (priv->ieee->iw_mode) {
2453                 case IW_MODE_ADHOC:
2454                         rc = ipw_get_fw(priv, &ucode,
2455                                         IPW_FW_NAME("ibss_ucode"));
2456                         if (rc)
2457                                 goto error;
2458
2459                         rc = ipw_get_fw(priv, &firmware, IPW_FW_NAME("ibss"));
2460                         break;
2461
2462 #ifdef CONFIG_IPW_MONITOR
2463                 case IW_MODE_MONITOR:
2464                         rc = ipw_get_fw(priv, &ucode,
2465                                         IPW_FW_NAME("sniffer_ucode"));
2466                         if (rc)
2467                                 goto error;
2468
2469                         rc = ipw_get_fw(priv, &firmware,
2470                                         IPW_FW_NAME("sniffer"));
2471                         break;
2472 #endif
2473                 case IW_MODE_INFRA:
2474                         rc = ipw_get_fw(priv, &ucode, IPW_FW_NAME("bss_ucode"));
2475                         if (rc)
2476                                 goto error;
2477
2478                         rc = ipw_get_fw(priv, &firmware, IPW_FW_NAME("bss"));
2479                         break;
2480
2481                 default:
2482                         rc = -EINVAL;
2483                 }
2484
2485                 if (rc)
2486                         goto error;
2487
2488 #ifdef CONFIG_PM
2489                 fw_loaded = 1;
2490         }
2491 #endif
2492
2493         if (!priv->rxq)
2494                 priv->rxq = ipw_rx_queue_alloc(priv);
2495         else
2496                 ipw_rx_queue_reset(priv, priv->rxq);
2497         if (!priv->rxq) {
2498                 IPW_ERROR("Unable to initialize Rx queue\n");
2499                 goto error;
2500         }
2501
2502       retry:
2503         /* Ensure interrupts are disabled */
2504         ipw_write32(priv, CX2_INTA_MASK_R, ~CX2_INTA_MASK_ALL);
2505         priv->status &= ~STATUS_INT_ENABLED;
2506
2507         /* ack pending interrupts */
2508         ipw_write32(priv, CX2_INTA_RW, CX2_INTA_MASK_ALL);
2509
2510         ipw_stop_nic(priv);
2511
2512         rc = ipw_reset_nic(priv);
2513         if (rc) {
2514                 IPW_ERROR("Unable to reset NIC\n");
2515                 goto error;
2516         }
2517
2518         ipw_zero_memory(priv, CX2_NIC_SRAM_LOWER_BOUND,
2519                         CX2_NIC_SRAM_UPPER_BOUND - CX2_NIC_SRAM_LOWER_BOUND);
2520
2521         /* DMA the initial boot firmware into the device */
2522         rc = ipw_load_firmware(priv, bootfw->data + sizeof(struct fw_header),
2523                                bootfw->size - sizeof(struct fw_header));
2524         if (rc < 0) {
2525                 IPW_ERROR("Unable to load boot firmware\n");
2526                 goto error;
2527         }
2528
2529         /* kick start the device */
2530         ipw_start_nic(priv);
2531
2532         /* wait for the device to finish it's initial startup sequence */
2533         rc = ipw_poll_bit(priv, CX2_INTA_RW,
2534                           CX2_INTA_BIT_FW_INITIALIZATION_DONE, 500);
2535         if (rc < 0) {
2536                 IPW_ERROR("device failed to boot initial fw image\n");
2537                 goto error;
2538         }
2539         IPW_DEBUG_INFO("initial device response after %dms\n", rc);
2540
2541         /* ack fw init done interrupt */
2542         ipw_write32(priv, CX2_INTA_RW, CX2_INTA_BIT_FW_INITIALIZATION_DONE);
2543
2544         /* DMA the ucode into the device */
2545         rc = ipw_load_ucode(priv, ucode->data + sizeof(struct fw_header),
2546                             ucode->size - sizeof(struct fw_header));
2547         if (rc < 0) {
2548                 IPW_ERROR("Unable to load ucode\n");
2549                 goto error;
2550         }
2551
2552         /* stop nic */
2553         ipw_stop_nic(priv);
2554
2555         /* DMA bss firmware into the device */
2556         rc = ipw_load_firmware(priv, firmware->data +
2557                                sizeof(struct fw_header),
2558                                firmware->size - sizeof(struct fw_header));
2559         if (rc < 0) {
2560                 IPW_ERROR("Unable to load firmware\n");
2561                 goto error;
2562         }
2563
2564         ipw_write32(priv, IPW_EEPROM_LOAD_DISABLE, 0);
2565
2566         rc = ipw_queue_reset(priv);
2567         if (rc) {
2568                 IPW_ERROR("Unable to initialize queues\n");
2569                 goto error;
2570         }
2571
2572         /* Ensure interrupts are disabled */
2573         ipw_write32(priv, CX2_INTA_MASK_R, ~CX2_INTA_MASK_ALL);
2574
2575         /* kick start the device */
2576         ipw_start_nic(priv);
2577
2578         if (ipw_read32(priv, CX2_INTA_RW) & CX2_INTA_BIT_PARITY_ERROR) {
2579                 if (retries > 0) {
2580                         IPW_WARNING("Parity error.  Retrying init.\n");
2581                         retries--;
2582                         goto retry;
2583                 }
2584
2585                 IPW_ERROR("TODO: Handle parity error -- schedule restart?\n");
2586                 rc = -EIO;
2587                 goto error;
2588         }
2589
2590         /* wait for the device */
2591         rc = ipw_poll_bit(priv, CX2_INTA_RW,
2592                           CX2_INTA_BIT_FW_INITIALIZATION_DONE, 500);
2593         if (rc < 0) {
2594                 IPW_ERROR("device failed to start after 500ms\n");
2595                 goto error;
2596         }
2597         IPW_DEBUG_INFO("device response after %dms\n", rc);
2598
2599         /* ack fw init done interrupt */
2600         ipw_write32(priv, CX2_INTA_RW, CX2_INTA_BIT_FW_INITIALIZATION_DONE);
2601
2602         /* read eeprom data and initialize the eeprom region of sram */
2603         priv->eeprom_delay = 1;
2604         ipw_eeprom_init_sram(priv);
2605
2606         /* enable interrupts */
2607         ipw_enable_interrupts(priv);
2608
2609         /* Ensure our queue has valid packets */
2610         ipw_rx_queue_replenish(priv);
2611
2612         ipw_write32(priv, CX2_RX_READ_INDEX, priv->rxq->read);
2613
2614         /* ack pending interrupts */
2615         ipw_write32(priv, CX2_INTA_RW, CX2_INTA_MASK_ALL);
2616
2617 #ifndef CONFIG_PM
2618         release_firmware(bootfw);
2619         release_firmware(ucode);
2620         release_firmware(firmware);
2621 #endif
2622         return 0;
2623
2624       error:
2625         if (priv->rxq) {
2626                 ipw_rx_queue_free(priv, priv->rxq);
2627                 priv->rxq = NULL;
2628         }
2629         ipw_tx_queue_free(priv);
2630         if (bootfw)
2631                 release_firmware(bootfw);
2632         if (ucode)
2633                 release_firmware(ucode);
2634         if (firmware)
2635                 release_firmware(firmware);
2636 #ifdef CONFIG_PM
2637         fw_loaded = 0;
2638         bootfw = ucode = firmware = NULL;
2639 #endif
2640
2641         return rc;
2642 }
2643
2644 /**
2645  * DMA services
2646  *
2647  * Theory of operation
2648  *
2649  * A queue is a circular buffers with 'Read' and 'Write' pointers.
2650  * 2 empty entries always kept in the buffer to protect from overflow.
2651  *
2652  * For Tx queue, there are low mark and high mark limits. If, after queuing
2653  * the packet for Tx, free space become < low mark, Tx queue stopped. When
2654  * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
2655  * Tx queue resumed.
2656  *
2657  * The IPW operates with six queues, one receive queue in the device's
2658  * sram, one transmit queue for sending commands to the device firmware,
2659  * and four transmit queues for data.
2660  *
2661  * The four transmit queues allow for performing quality of service (qos)
2662  * transmissions as per the 802.11 protocol.  Currently Linux does not
2663  * provide a mechanism to the user for utilizing prioritized queues, so
2664  * we only utilize the first data transmit queue (queue1).
2665  */
2666
2667 /**
2668  * Driver allocates buffers of this size for Rx
2669  */
2670
2671 static inline int ipw_queue_space(const struct clx2_queue *q)
2672 {
2673         int s = q->last_used - q->first_empty;
2674         if (s <= 0)
2675                 s += q->n_bd;
2676         s -= 2;                 /* keep some reserve to not confuse empty and full situations */
2677         if (s < 0)
2678                 s = 0;
2679         return s;
2680 }
2681
2682 static inline int ipw_queue_inc_wrap(int index, int n_bd)
2683 {
2684         return (++index == n_bd) ? 0 : index;
2685 }
2686
2687 /**
2688  * Initialize common DMA queue structure
2689  *
2690  * @param q                queue to init
2691  * @param count            Number of BD's to allocate. Should be power of 2
2692  * @param read_register    Address for 'read' register
2693  *                         (not offset within BAR, full address)
2694  * @param write_register   Address for 'write' register
2695  *                         (not offset within BAR, full address)
2696  * @param base_register    Address for 'base' register
2697  *                         (not offset within BAR, full address)
2698  * @param size             Address for 'size' register
2699  *                         (not offset within BAR, full address)
2700  */
2701 static void ipw_queue_init(struct ipw_priv *priv, struct clx2_queue *q,
2702                            int count, u32 read, u32 write, u32 base, u32 size)
2703 {
2704         q->n_bd = count;
2705
2706         q->low_mark = q->n_bd / 4;
2707         if (q->low_mark < 4)
2708                 q->low_mark = 4;
2709
2710         q->high_mark = q->n_bd / 8;
2711         if (q->high_mark < 2)
2712                 q->high_mark = 2;
2713
2714         q->first_empty = q->last_used = 0;
2715         q->reg_r = read;
2716         q->reg_w = write;
2717
2718         ipw_write32(priv, base, q->dma_addr);
2719         ipw_write32(priv, size, count);
2720         ipw_write32(priv, read, 0);
2721         ipw_write32(priv, write, 0);
2722
2723         _ipw_read32(priv, 0x90);
2724 }
2725
2726 static int ipw_queue_tx_init(struct ipw_priv *priv,
2727                              struct clx2_tx_queue *q,
2728                              int count, u32 read, u32 write, u32 base, u32 size)
2729 {
2730         struct pci_dev *dev = priv->pci_dev;
2731
2732         q->txb = kmalloc(sizeof(q->txb[0]) * count, GFP_KERNEL);
2733         if (!q->txb) {
2734                 IPW_ERROR("vmalloc for auxilary BD structures failed\n");
2735                 return -ENOMEM;
2736         }
2737
2738         q->bd =
2739             pci_alloc_consistent(dev, sizeof(q->bd[0]) * count, &q->q.dma_addr);
2740         if (!q->bd) {
2741                 IPW_ERROR("pci_alloc_consistent(%zd) failed\n",
2742                           sizeof(q->bd[0]) * count);
2743                 kfree(q->txb);
2744                 q->txb = NULL;
2745                 return -ENOMEM;
2746         }
2747
2748         ipw_queue_init(priv, &q->q, count, read, write, base, size);
2749         return 0;
2750 }
2751
2752 /**
2753  * Free one TFD, those at index [txq->q.last_used].
2754  * Do NOT advance any indexes
2755  *
2756  * @param dev
2757  * @param txq
2758  */
2759 static void ipw_queue_tx_free_tfd(struct ipw_priv *priv,
2760                                   struct clx2_tx_queue *txq)
2761 {
2762         struct tfd_frame *bd = &txq->bd[txq->q.last_used];
2763         struct pci_dev *dev = priv->pci_dev;
2764         int i;
2765
2766         /* classify bd */
2767         if (bd->control_flags.message_type == TX_HOST_COMMAND_TYPE)
2768                 /* nothing to cleanup after for host commands */
2769                 return;
2770
2771         /* sanity check */
2772         if (bd->u.data.num_chunks > NUM_TFD_CHUNKS) {
2773                 IPW_ERROR("Too many chunks: %i\n", bd->u.data.num_chunks);
2774                 /** @todo issue fatal error, it is quite serious situation */
2775                 return;
2776         }
2777
2778         /* unmap chunks if any */
2779         for (i = 0; i < bd->u.data.num_chunks; i++) {
2780                 pci_unmap_single(dev, bd->u.data.chunk_ptr[i],
2781                                  bd->u.data.chunk_len[i], PCI_DMA_TODEVICE);
2782                 if (txq->txb[txq->q.last_used]) {
2783                         ieee80211_txb_free(txq->txb[txq->q.last_used]);
2784                         txq->txb[txq->q.last_used] = NULL;
2785                 }
2786         }
2787 }
2788
2789 /**
2790  * Deallocate DMA queue.
2791  *
2792  * Empty queue by removing and destroying all BD's.
2793  * Free all buffers.
2794  *
2795  * @param dev
2796  * @param q
2797  */
2798 static void ipw_queue_tx_free(struct ipw_priv *priv, struct clx2_tx_queue *txq)
2799 {
2800         struct clx2_queue *q = &txq->q;
2801         struct pci_dev *dev = priv->pci_dev;
2802
2803         if (q->n_bd == 0)
2804                 return;
2805
2806         /* first, empty all BD's */
2807         for (; q->first_empty != q->last_used;
2808              q->last_used = ipw_queue_inc_wrap(q->last_used, q->n_bd)) {
2809                 ipw_queue_tx_free_tfd(priv, txq);
2810         }
2811
2812         /* free buffers belonging to queue itself */
2813         pci_free_consistent(dev, sizeof(txq->bd[0]) * q->n_bd, txq->bd,
2814                             q->dma_addr);
2815         kfree(txq->txb);
2816
2817         /* 0 fill whole structure */
2818         memset(txq, 0, sizeof(*txq));
2819 }
2820
2821 /**
2822  * Destroy all DMA queues and structures
2823  *
2824  * @param priv
2825  */
2826 static void ipw_tx_queue_free(struct ipw_priv *priv)
2827 {
2828         /* Tx CMD queue */
2829         ipw_queue_tx_free(priv, &priv->txq_cmd);
2830
2831         /* Tx queues */
2832         ipw_queue_tx_free(priv, &priv->txq[0]);
2833         ipw_queue_tx_free(priv, &priv->txq[1]);
2834         ipw_queue_tx_free(priv, &priv->txq[2]);
2835         ipw_queue_tx_free(priv, &priv->txq[3]);
2836 }
2837
2838 static void inline __maybe_wake_tx(struct ipw_priv *priv)
2839 {
2840         if (netif_running(priv->net_dev)) {
2841                 switch (priv->port_type) {
2842                 case DCR_TYPE_MU_BSS:
2843                 case DCR_TYPE_MU_IBSS:
2844                         if (!(priv->status & STATUS_ASSOCIATED)) {
2845                                 return;
2846                         }
2847                 }
2848                 netif_wake_queue(priv->net_dev);
2849         }
2850
2851 }
2852
2853 static inline void ipw_create_bssid(struct ipw_priv *priv, u8 * bssid)
2854 {
2855         /* First 3 bytes are manufacturer */
2856         bssid[0] = priv->mac_addr[0];
2857         bssid[1] = priv->mac_addr[1];
2858         bssid[2] = priv->mac_addr[2];
2859
2860         /* Last bytes are random */
2861         get_random_bytes(&bssid[3], ETH_ALEN - 3);
2862
2863         bssid[0] &= 0xfe;       /* clear multicast bit */
2864         bssid[0] |= 0x02;       /* set local assignment bit (IEEE802) */
2865 }
2866
2867 static inline u8 ipw_add_station(struct ipw_priv *priv, u8 * bssid)
2868 {
2869         struct ipw_station_entry entry;
2870         int i;
2871
2872         for (i = 0; i < priv->num_stations; i++) {
2873                 if (!memcmp(priv->stations[i], bssid, ETH_ALEN)) {
2874                         /* Another node is active in network */
2875                         priv->missed_adhoc_beacons = 0;
2876                         if (!(priv->config & CFG_STATIC_CHANNEL))
2877                                 /* when other nodes drop out, we drop out */
2878                                 priv->config &= ~CFG_ADHOC_PERSIST;
2879
2880                         return i;
2881                 }
2882         }
2883
2884         if (i == MAX_STATIONS)
2885                 return IPW_INVALID_STATION;
2886
2887         IPW_DEBUG_SCAN("Adding AdHoc station: " MAC_FMT "\n", MAC_ARG(bssid));
2888
2889         entry.reserved = 0;
2890         entry.support_mode = 0;
2891         memcpy(entry.mac_addr, bssid, ETH_ALEN);
2892         memcpy(priv->stations[i], bssid, ETH_ALEN);
2893         ipw_write_direct(priv, IPW_STATION_TABLE_LOWER + i * sizeof(entry),
2894                          &entry, sizeof(entry));
2895         priv->num_stations++;
2896
2897         return i;
2898 }
2899
2900 static inline u8 ipw_find_station(struct ipw_priv *priv, u8 * bssid)
2901 {
2902         int i;
2903
2904         for (i = 0; i < priv->num_stations; i++)
2905                 if (!memcmp(priv->stations[i], bssid, ETH_ALEN))
2906                         return i;
2907
2908         return IPW_INVALID_STATION;
2909 }
2910
2911 static void ipw_send_disassociate(struct ipw_priv *priv, int quiet)
2912 {
2913         int err;
2914
2915         if (!(priv->status & (STATUS_ASSOCIATING | STATUS_ASSOCIATED))) {
2916                 IPW_DEBUG_ASSOC("Disassociating while not associated.\n");
2917                 return;
2918         }
2919
2920         IPW_DEBUG_ASSOC("Disassocation attempt from " MAC_FMT " "
2921                         "on channel %d.\n",
2922                         MAC_ARG(priv->assoc_request.bssid),
2923                         priv->assoc_request.channel);
2924
2925         priv->status &= ~(STATUS_ASSOCIATING | STATUS_ASSOCIATED);
2926         priv->status |= STATUS_DISASSOCIATING;
2927
2928         if (quiet)
2929                 priv->assoc_request.assoc_type = HC_DISASSOC_QUIET;
2930         else
2931                 priv->assoc_request.assoc_type = HC_DISASSOCIATE;
2932         err = ipw_send_associate(priv, &priv->assoc_request);
2933         if (err) {
2934                 IPW_DEBUG_HC("Attempt to send [dis]associate command "
2935                              "failed.\n");
2936                 return;
2937         }
2938
2939 }
2940
2941 static void ipw_disassociate(void *data)
2942 {
2943         ipw_send_disassociate(data, 0);
2944 }
2945
2946 struct ipw_status_code {
2947         u16 status;
2948         const char *reason;
2949 };
2950
2951 static const struct ipw_status_code ipw_status_codes[] = {
2952         {0x00, "Successful"},
2953         {0x01, "Unspecified failure"},
2954         {0x0A, "Cannot support all requested capabilities in the "
2955          "Capability information field"},
2956         {0x0B, "Reassociation denied due to inability to confirm that "
2957          "association exists"},
2958         {0x0C, "Association denied due to reason outside the scope of this "
2959          "standard"},
2960         {0x0D,
2961          "Responding station does not support the specified authentication "
2962          "algorithm"},
2963         {0x0E,
2964          "Received an Authentication frame with authentication sequence "
2965          "transaction sequence number out of expected sequence"},
2966         {0x0F, "Authentication rejected because of challenge failure"},
2967         {0x10, "Authentication rejected due to timeout waiting for next "
2968          "frame in sequence"},
2969         {0x11, "Association denied because AP is unable to handle additional "
2970          "associated stations"},
2971         {0x12,
2972          "Association denied due to requesting station not supporting all "
2973          "of the datarates in the BSSBasicServiceSet Parameter"},
2974         {0x13,
2975          "Association denied due to requesting station not supporting "
2976          "short preamble operation"},
2977         {0x14,
2978          "Association denied due to requesting station not supporting "
2979          "PBCC encoding"},
2980         {0x15,
2981          "Association denied due to requesting station not supporting "
2982          "channel agility"},
2983         {0x19,
2984          "Association denied due to requesting station not supporting "
2985          "short slot operation"},
2986         {0x1A,
2987          "Association denied due to requesting station not supporting "
2988          "DSSS-OFDM operation"},
2989         {0x28, "Invalid Information Element"},
2990         {0x29, "Group Cipher is not valid"},
2991         {0x2A, "Pairwise Cipher is not valid"},
2992         {0x2B, "AKMP is not valid"},
2993         {0x2C, "Unsupported RSN IE version"},
2994         {0x2D, "Invalid RSN IE Capabilities"},
2995         {0x2E, "Cipher suite is rejected per security policy"},
2996 };
2997
2998 #ifdef CONFIG_IPW_DEBUG
2999 static const char *ipw_get_status_code(u16 status)
3000 {
3001         int i;
3002         for (i = 0; i < ARRAY_SIZE(ipw_status_codes); i++)
3003                 if (ipw_status_codes[i].status == (status & 0xff))
3004                         return ipw_status_codes[i].reason;
3005         return "Unknown status value.";
3006 }
3007 #endif
3008
3009 static void inline average_init(struct average *avg)
3010 {
3011         memset(avg, 0, sizeof(*avg));
3012 }
3013
3014 static void inline average_add(struct average *avg, s16 val)
3015 {
3016         avg->sum -= avg->entries[avg->pos];
3017         avg->sum += val;
3018         avg->entries[avg->pos++] = val;
3019         if (unlikely(avg->pos == AVG_ENTRIES)) {
3020                 avg->init = 1;
3021                 avg->pos = 0;
3022         }
3023 }
3024
3025 static s16 inline average_value(struct average *avg)
3026 {
3027         if (!unlikely(avg->init)) {
3028                 if (avg->pos)
3029                         return avg->sum / avg->pos;
3030                 return 0;
3031         }
3032
3033         return avg->sum / AVG_ENTRIES;
3034 }
3035
3036 static void ipw_reset_stats(struct ipw_priv *priv)
3037 {
3038         u32 len = sizeof(u32);
3039
3040         priv->quality = 0;
3041
3042         average_init(&priv->average_missed_beacons);
3043         average_init(&priv->average_rssi);
3044         average_init(&priv->average_noise);
3045
3046         priv->last_rate = 0;
3047         priv->last_missed_beacons = 0;
3048         priv->last_rx_packets = 0;
3049         priv->last_tx_packets = 0;
3050         priv->last_tx_failures = 0;
3051
3052         /* Firmware managed, reset only when NIC is restarted, so we have to
3053          * normalize on the current value */
3054         ipw_get_ordinal(priv, IPW_ORD_STAT_RX_ERR_CRC,
3055                         &priv->last_rx_err, &len);
3056         ipw_get_ordinal(priv, IPW_ORD_STAT_TX_FAILURE,
3057                         &priv->last_tx_failures, &len);
3058
3059         /* Driver managed, reset with each association */
3060         priv->missed_adhoc_beacons = 0;
3061         priv->missed_beacons = 0;
3062         priv->tx_packets = 0;
3063         priv->rx_packets = 0;
3064
3065 }
3066
3067 static inline u32 ipw_get_max_rate(struct ipw_priv *priv)
3068 {
3069         u32 i = 0x80000000;
3070         u32 mask = priv->rates_mask;
3071         /* If currently associated in B mode, restrict the maximum
3072          * rate match to B rates */
3073         if (priv->assoc_request.ieee_mode == IPW_B_MODE)
3074                 mask &= IEEE80211_CCK_RATES_MASK;
3075
3076         /* TODO: Verify that the rate is supported by the current rates
3077          * list. */
3078
3079         while (i && !(mask & i))
3080                 i >>= 1;
3081         switch (i) {
3082         case IEEE80211_CCK_RATE_1MB_MASK:
3083                 return 1000000;
3084         case IEEE80211_CCK_RATE_2MB_MASK:
3085                 return 2000000;
3086         case IEEE80211_CCK_RATE_5MB_MASK:
3087                 return 5500000;
3088         case IEEE80211_OFDM_RATE_6MB_MASK:
3089                 return 6000000;
3090         case IEEE80211_OFDM_RATE_9MB_MASK:
3091                 return 9000000;
3092         case IEEE80211_CCK_RATE_11MB_MASK:
3093                 return 11000000;
3094         case IEEE80211_OFDM_RATE_12MB_MASK:
3095                 return 12000000;
3096         case IEEE80211_OFDM_RATE_18MB_MASK:
3097                 return 18000000;
3098         case IEEE80211_OFDM_RATE_24MB_MASK:
3099                 return 24000000;
3100         case IEEE80211_OFDM_RATE_36MB_MASK:
3101                 return 36000000;
3102         case IEEE80211_OFDM_RATE_48MB_MASK:
3103                 return 48000000;
3104         case IEEE80211_OFDM_RATE_54MB_MASK:
3105                 return 54000000;
3106         }
3107
3108         if (priv->ieee->mode == IEEE_B)
3109                 return 11000000;
3110         else
3111                 return 54000000;
3112 }
3113
3114 static u32 ipw_get_current_rate(struct ipw_priv *priv)
3115 {
3116         u32 rate, len = sizeof(rate);
3117         int err;
3118
3119         if (!(priv->status & STATUS_ASSOCIATED))
3120                 return 0;
3121
3122         if (priv->tx_packets > IPW_REAL_RATE_RX_PACKET_THRESHOLD) {
3123                 err = ipw_get_ordinal(priv, IPW_ORD_STAT_TX_CURR_RATE, &rate,
3124                                       &len);
3125                 if (err) {
3126                         IPW_DEBUG_INFO("failed querying ordinals.\n");
3127                         return 0;
3128                 }
3129         } else
3130                 return ipw_get_max_rate(priv);
3131
3132         switch (rate) {
3133         case IPW_TX_RATE_1MB:
3134                 return 1000000;
3135         case IPW_TX_RATE_2MB:
3136                 return 2000000;
3137         case IPW_TX_RATE_5MB:
3138                 return 5500000;
3139         case IPW_TX_RATE_6MB:
3140                 return 6000000;
3141         case IPW_TX_RATE_9MB:
3142                 return 9000000;
3143         case IPW_TX_RATE_11MB:
3144                 return 11000000;
3145         case IPW_TX_RATE_12MB:
3146                 return 12000000;
3147         case IPW_TX_RATE_18MB:
3148                 return 18000000;
3149         case IPW_TX_RATE_24MB:
3150                 return 24000000;
3151         case IPW_TX_RATE_36MB:
3152                 return 36000000;
3153         case IPW_TX_RATE_48MB:
3154                 return 48000000;
3155         case IPW_TX_RATE_54MB:
3156                 return 54000000;
3157         }
3158
3159         return 0;
3160 }
3161
3162 #define PERFECT_RSSI (-20)
3163 #define WORST_RSSI   (-85)
3164 #define IPW_STATS_INTERVAL (2 * HZ)
3165 static void ipw_gather_stats(struct ipw_priv *priv)
3166 {
3167         u32 rx_err, rx_err_delta, rx_packets_delta;
3168         u32 tx_failures, tx_failures_delta, tx_packets_delta;
3169         u32 missed_beacons_percent, missed_beacons_delta;
3170         u32 quality = 0;
3171         u32 len = sizeof(u32);
3172         s16 rssi;
3173         u32 beacon_quality, signal_quality, tx_quality, rx_quality,
3174             rate_quality;
3175         u32 max_rate;
3176
3177         if (!(priv->status & STATUS_ASSOCIATED)) {
3178                 priv->quality = 0;
3179                 return;
3180         }
3181
3182         /* Update the statistics */
3183         ipw_get_ordinal(priv, IPW_ORD_STAT_MISSED_BEACONS,
3184                         &priv->missed_beacons, &len);
3185         missed_beacons_delta = priv->missed_beacons - priv->last_missed_beacons;
3186         priv->last_missed_beacons = priv->missed_beacons;
3187         if (priv->assoc_request.beacon_interval) {
3188                 missed_beacons_percent = missed_beacons_delta *
3189                     (HZ * priv->assoc_request.beacon_interval) /
3190                     (IPW_STATS_INTERVAL * 10);
3191         } else {
3192                 missed_beacons_percent = 0;
3193         }
3194         average_add(&priv->average_missed_beacons, missed_beacons_percent);
3195
3196         ipw_get_ordinal(priv, IPW_ORD_STAT_RX_ERR_CRC, &rx_err, &len);
3197         rx_err_delta = rx_err - priv->last_rx_err;
3198         priv->last_rx_err = rx_err;
3199
3200         ipw_get_ordinal(priv, IPW_ORD_STAT_TX_FAILURE, &tx_failures, &len);
3201         tx_failures_delta = tx_failures - priv->last_tx_failures;
3202         priv->last_tx_failures = tx_failures;
3203
3204         rx_packets_delta = priv->rx_packets - priv->last_rx_packets;
3205         priv->last_rx_packets = priv->rx_packets;
3206
3207         tx_packets_delta = priv->tx_packets - priv->last_tx_packets;
3208         priv->last_tx_packets = priv->tx_packets;
3209
3210         /* Calculate quality based on the following:
3211          *
3212          * Missed beacon: 100% = 0, 0% = 70% missed
3213          * Rate: 60% = 1Mbs, 100% = Max
3214          * Rx and Tx errors represent a straight % of total Rx/Tx
3215          * RSSI: 100% = > -50,  0% = < -80
3216          * Rx errors: 100% = 0, 0% = 50% missed
3217          *
3218          * The lowest computed quality is used.
3219          *
3220          */
3221 #define BEACON_THRESHOLD 5
3222         beacon_quality = 100 - missed_beacons_percent;
3223         if (beacon_quality < BEACON_THRESHOLD)
3224                 beacon_quality = 0;
3225         else
3226                 beacon_quality = (beacon_quality - BEACON_THRESHOLD) * 100 /
3227                     (100 - BEACON_THRESHOLD);
3228         IPW_DEBUG_STATS("Missed beacon: %3d%% (%d%%)\n",
3229                         beacon_quality, missed_beacons_percent);
3230
3231         priv->last_rate = ipw_get_current_rate(priv);
3232         max_rate = ipw_get_max_rate(priv);
3233         rate_quality = priv->last_rate * 40 / max_rate + 60;
3234         IPW_DEBUG_STATS("Rate quality : %3d%% (%dMbs)\n",
3235                         rate_quality, priv->last_rate / 1000000);
3236
3237         if (rx_packets_delta > 100 && rx_packets_delta + rx_err_delta)
3238                 rx_quality = 100 - (rx_err_delta * 100) /
3239                     (rx_packets_delta + rx_err_delta);
3240         else
3241                 rx_quality = 100;
3242         IPW_DEBUG_STATS("Rx quality   : %3d%% (%u errors, %u packets)\n",
3243                         rx_quality, rx_err_delta, rx_packets_delta);
3244
3245         if (tx_packets_delta > 100 && tx_packets_delta + tx_failures_delta)
3246                 tx_quality = 100 - (tx_failures_delta * 100) /
3247                     (tx_packets_delta + tx_failures_delta);
3248         else
3249                 tx_quality = 100;
3250         IPW_DEBUG_STATS("Tx quality   : %3d%% (%u errors, %u packets)\n",
3251                         tx_quality, tx_failures_delta, tx_packets_delta);
3252
3253         rssi = average_value(&priv->average_rssi);
3254         if (rssi > PERFECT_RSSI)
3255                 signal_quality = 100;
3256         else if (rssi < WORST_RSSI)
3257                 signal_quality = 0;
3258         else                    /* qual = 100a^2 - 15ab + 62b^2 / a^2 */
3259                 signal_quality =
3260                     (100 *
3261                      (PERFECT_RSSI - WORST_RSSI) *
3262                      (PERFECT_RSSI - WORST_RSSI) -
3263                      (PERFECT_RSSI - rssi) *
3264                      (15 * (PERFECT_RSSI - WORST_RSSI) +
3265                       62 * (PERFECT_RSSI - rssi))) /
3266                     ((PERFECT_RSSI - WORST_RSSI) * (PERFECT_RSSI - WORST_RSSI));
3267
3268         IPW_DEBUG_STATS("Signal level : %3d%% (%d dBm)\n",
3269                         signal_quality, rssi);
3270
3271         quality = min(beacon_quality,
3272                       min(rate_quality,
3273                           min(tx_quality, min(rx_quality, signal_quality))));
3274         if (quality == beacon_quality)
3275                 IPW_DEBUG_STATS("Quality (%d%%): Clamped to missed beacons.\n",
3276                                 quality);
3277         if (quality == rate_quality)
3278                 IPW_DEBUG_STATS("Quality (%d%%): Clamped to rate quality.\n",
3279                                 quality);
3280         if (quality == tx_quality)
3281                 IPW_DEBUG_STATS("Quality (%d%%): Clamped to Tx quality.\n",
3282                                 quality);
3283         if (quality == rx_quality)
3284                 IPW_DEBUG_STATS("Quality (%d%%): Clamped to Rx quality.\n",
3285                                 quality);
3286         if (quality == signal_quality)
3287                 IPW_DEBUG_STATS("Quality (%d%%): Clamped to signal quality.\n",
3288                                 quality);
3289
3290         priv->quality = quality;
3291
3292         queue_delayed_work(priv->workqueue, &priv->gather_stats,
3293                            IPW_STATS_INTERVAL);
3294 }
3295
3296 static inline void ipw_handle_missed_beacon(struct ipw_priv *priv,
3297                                             int missed_count)
3298 {
3299         priv->notif_missed_beacons = missed_count;
3300
3301         if (missed_count > priv->missed_beacon_threshold &&
3302             priv->status & STATUS_ASSOCIATED) {
3303                 /* If associated and we've hit the missed
3304                  * beacon threshold, disassociate, turn
3305                  * off roaming, and abort any active scans */
3306                 IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF |
3307                           IPW_DL_STATE,
3308                           "Missed beacon: %d - disassociate\n", missed_count);
3309                 priv->status &= ~STATUS_ROAMING;
3310                 if (priv->status & STATUS_SCANNING)
3311                         queue_work(priv->workqueue, &priv->abort_scan);
3312                 queue_work(priv->workqueue, &priv->disassociate);
3313                 return;
3314         }
3315
3316         if (priv->status & STATUS_ROAMING) {
3317                 /* If we are currently roaming, then just
3318                  * print a debug statement... */
3319                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
3320                           "Missed beacon: %d - roam in progress\n",
3321                           missed_count);
3322                 return;
3323         }
3324
3325         if (missed_count > priv->roaming_threshold) {
3326                 /* If we are not already roaming, set the ROAM
3327                  * bit in the status and kick off a scan */
3328                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
3329                           "Missed beacon: %d - initiate "
3330                           "roaming\n", missed_count);
3331                 if (!(priv->status & STATUS_ROAMING)) {
3332                         priv->status |= STATUS_ROAMING;
3333                         if (!(priv->status & STATUS_SCANNING))
3334                                 queue_work(priv->workqueue,
3335                                            &priv->request_scan);
3336                 }
3337                 return;
3338         }
3339
3340         if (priv->status & STATUS_SCANNING) {
3341                 /* Stop scan to keep fw from getting
3342                  * stuck (only if we aren't roaming --
3343                  * otherwise we'll never scan more than 2 or 3
3344                  * channels..) */
3345                 queue_work(priv->workqueue, &priv->abort_scan);
3346         }
3347
3348         IPW_DEBUG_NOTIF("Missed beacon: %d\n", missed_count);
3349
3350 }
3351
3352 /**
3353  * Handle host notification packet.
3354  * Called from interrupt routine
3355  */
3356 static inline void ipw_rx_notification(struct ipw_priv *priv,
3357                                        struct ipw_rx_notification *notif)
3358 {
3359         IPW_DEBUG_NOTIF("type = %i (%d bytes)\n", notif->subtype, notif->size);
3360
3361         switch (notif->subtype) {
3362         case HOST_NOTIFICATION_STATUS_ASSOCIATED:{
3363                         struct notif_association *assoc = &notif->u.assoc;
3364
3365                         switch (assoc->state) {
3366                         case CMAS_ASSOCIATED:{
3367                                         IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3368                                                   IPW_DL_ASSOC,
3369                                                   "associated: '%s' " MAC_FMT
3370                                                   " \n",
3371                                                   escape_essid(priv->essid,
3372                                                                priv->essid_len),
3373                                                   MAC_ARG(priv->bssid));
3374
3375                                         switch (priv->ieee->iw_mode) {
3376                                         case IW_MODE_INFRA:
3377                                                 memcpy(priv->ieee->bssid,
3378                                                        priv->bssid, ETH_ALEN);
3379                                                 break;
3380
3381                                         case IW_MODE_ADHOC:
3382                                                 memcpy(priv->ieee->bssid,
3383                                                        priv->bssid, ETH_ALEN);
3384
3385                                                 /* clear out the station table */
3386                                                 priv->num_stations = 0;
3387
3388                                                 IPW_DEBUG_ASSOC
3389                                                     ("queueing adhoc check\n");
3390                                                 queue_delayed_work(priv->
3391                                                                    workqueue,
3392                                                                    &priv->
3393                                                                    adhoc_check,
3394                                                                    priv->
3395                                                                    assoc_request.
3396                                                                    beacon_interval);
3397                                                 break;
3398                                         }
3399
3400                                         priv->status &= ~STATUS_ASSOCIATING;
3401                                         priv->status |= STATUS_ASSOCIATED;
3402
3403                                         netif_carrier_on(priv->net_dev);
3404                                         if (netif_queue_stopped(priv->net_dev)) {
3405                                                 IPW_DEBUG_NOTIF
3406                                                     ("waking queue\n");
3407                                                 netif_wake_queue(priv->net_dev);
3408                                         } else {
3409                                                 IPW_DEBUG_NOTIF
3410                                                     ("starting queue\n");
3411                                                 netif_start_queue(priv->
3412                                                                   net_dev);
3413                                         }
3414
3415                                         ipw_reset_stats(priv);
3416                                         /* Ensure the rate is updated immediately */
3417                                         priv->last_rate =
3418                                             ipw_get_current_rate(priv);
3419                                         schedule_work(&priv->gather_stats);
3420                                         notify_wx_assoc_event(priv);
3421
3422 /*                      queue_delayed_work(priv->workqueue,
3423                                            &priv->request_scan,
3424                                            SCAN_ASSOCIATED_INTERVAL);
3425 */
3426                                         break;
3427                                 }
3428
3429                         case CMAS_AUTHENTICATED:{
3430                                         if (priv->
3431                                             status & (STATUS_ASSOCIATED |
3432                                                       STATUS_AUTH)) {
3433 #ifdef CONFIG_IPW_DEBUG
3434                                                 struct notif_authenticate *auth
3435                                                     = &notif->u.auth;
3436                                                 IPW_DEBUG(IPW_DL_NOTIF |
3437                                                           IPW_DL_STATE |
3438                                                           IPW_DL_ASSOC,
3439                                                           "deauthenticated: '%s' "
3440                                                           MAC_FMT
3441                                                           ": (0x%04X) - %s \n",
3442                                                           escape_essid(priv->
3443                                                                        essid,
3444                                                                        priv->
3445                                                                        essid_len),
3446                                                           MAC_ARG(priv->bssid),
3447                                                           ntohs(auth->status),
3448                                                           ipw_get_status_code
3449                                                           (ntohs
3450                                                            (auth->status)));
3451 #endif
3452
3453                                                 priv->status &=
3454                                                     ~(STATUS_ASSOCIATING |
3455                                                       STATUS_AUTH |
3456                                                       STATUS_ASSOCIATED);
3457
3458                                                 netif_carrier_off(priv->
3459                                                                   net_dev);
3460                                                 netif_stop_queue(priv->net_dev);
3461                                                 queue_work(priv->workqueue,
3462                                                            &priv->request_scan);
3463                                                 notify_wx_assoc_event(priv);
3464                                                 break;
3465                                         }
3466
3467                                         IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3468                                                   IPW_DL_ASSOC,
3469                                                   "authenticated: '%s' " MAC_FMT
3470                                                   "\n",
3471                                                   escape_essid(priv->essid,
3472                                                                priv->essid_len),
3473                                                   MAC_ARG(priv->bssid));
3474                                         break;
3475                                 }
3476
3477                         case CMAS_INIT:{
3478                                         if (priv->status & STATUS_AUTH) {
3479                                                 struct
3480                                                     ieee80211_assoc_response
3481                                                 *resp;
3482                                                 resp =
3483                                                     (struct
3484                                                      ieee80211_assoc_response
3485                                                      *)&notif->u.raw;
3486                                                 IPW_DEBUG(IPW_DL_NOTIF |
3487                                                           IPW_DL_STATE |
3488                                                           IPW_DL_ASSOC,
3489                                                           "association failed (0x%04X): %s\n",
3490                                                           ntohs(resp->status),
3491                                                           ipw_get_status_code
3492                                                           (ntohs
3493                                                            (resp->status)));
3494                                         }
3495
3496                                         IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3497                                                   IPW_DL_ASSOC,
3498                                                   "disassociated: '%s' " MAC_FMT
3499                                                   " \n",
3500                                                   escape_essid(priv->essid,
3501                                                                priv->essid_len),
3502                                                   MAC_ARG(priv->bssid));
3503
3504                                         priv->status &=
3505                                             ~(STATUS_DISASSOCIATING |
3506                                               STATUS_ASSOCIATING |
3507                                               STATUS_ASSOCIATED | STATUS_AUTH);
3508
3509                                         netif_stop_queue(priv->net_dev);
3510                                         if (!(priv->status & STATUS_ROAMING)) {
3511                                                 netif_carrier_off(priv->
3512                                                                   net_dev);
3513                                                 notify_wx_assoc_event(priv);
3514
3515                                                 /* Cancel any queued work ... */
3516                                                 cancel_delayed_work(&priv->
3517                                                                     request_scan);
3518                                                 cancel_delayed_work(&priv->
3519                                                                     adhoc_check);
3520
3521                                                 /* Queue up another scan... */
3522                                                 queue_work(priv->workqueue,
3523                                                            &priv->request_scan);
3524
3525                                                 cancel_delayed_work(&priv->
3526                                                                     gather_stats);
3527                                         } else {
3528                                                 priv->status |= STATUS_ROAMING;
3529                                                 queue_work(priv->workqueue,
3530                                                            &priv->request_scan);
3531                                         }
3532
3533                                         ipw_reset_stats(priv);
3534                                         break;
3535                                 }
3536
3537                         default:
3538                                 IPW_ERROR("assoc: unknown (%d)\n",
3539                                           assoc->state);
3540                                 break;
3541                         }
3542
3543                         break;
3544                 }
3545
3546         case HOST_NOTIFICATION_STATUS_AUTHENTICATE:{
3547                         struct notif_authenticate *auth = &notif->u.auth;
3548                         switch (auth->state) {
3549                         case CMAS_AUTHENTICATED:
3550                                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
3551                                           "authenticated: '%s' " MAC_FMT " \n",
3552                                           escape_essid(priv->essid,
3553                                                        priv->essid_len),
3554                                           MAC_ARG(priv->bssid));
3555                                 priv->status |= STATUS_AUTH;
3556                                 break;
3557
3558                         case CMAS_INIT:
3559                                 if (priv->status & STATUS_AUTH) {
3560                                         IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3561                                                   IPW_DL_ASSOC,
3562                                                   "authentication failed (0x%04X): %s\n",
3563                                                   ntohs(auth->status),
3564                                                   ipw_get_status_code(ntohs
3565                                                                       (auth->
3566                                                                        status)));
3567                                 }
3568                                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3569                                           IPW_DL_ASSOC,
3570                                           "deauthenticated: '%s' " MAC_FMT "\n",
3571                                           escape_essid(priv->essid,
3572                                                        priv->essid_len),
3573                                           MAC_ARG(priv->bssid));
3574
3575                                 priv->status &= ~(STATUS_ASSOCIATING |
3576                                                   STATUS_AUTH |
3577                                                   STATUS_ASSOCIATED);
3578
3579                                 netif_carrier_off(priv->net_dev);
3580                                 netif_stop_queue(priv->net_dev);
3581                                 queue_work(priv->workqueue,
3582                                            &priv->request_scan);
3583                                 notify_wx_assoc_event(priv);
3584                                 break;
3585
3586                         case CMAS_TX_AUTH_SEQ_1:
3587                                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3588                                           IPW_DL_ASSOC, "AUTH_SEQ_1\n");
3589                                 break;
3590                         case CMAS_RX_AUTH_SEQ_2:
3591                                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3592                                           IPW_DL_ASSOC, "AUTH_SEQ_2\n");
3593                                 break;
3594                         case CMAS_AUTH_SEQ_1_PASS:
3595                                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3596                                           IPW_DL_ASSOC, "AUTH_SEQ_1_PASS\n");
3597                                 break;
3598                         case CMAS_AUTH_SEQ_1_FAIL:
3599                                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3600                                           IPW_DL_ASSOC, "AUTH_SEQ_1_FAIL\n");
3601                                 break;
3602                         case CMAS_TX_AUTH_SEQ_3:
3603                                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3604                                           IPW_DL_ASSOC, "AUTH_SEQ_3\n");
3605                                 break;
3606                         case CMAS_RX_AUTH_SEQ_4:
3607                                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3608                                           IPW_DL_ASSOC, "RX_AUTH_SEQ_4\n");
3609                                 break;
3610                         case CMAS_AUTH_SEQ_2_PASS:
3611                                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3612                                           IPW_DL_ASSOC, "AUTH_SEQ_2_PASS\n");
3613                                 break;
3614                         case CMAS_AUTH_SEQ_2_FAIL:
3615                                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3616                                           IPW_DL_ASSOC, "AUT_SEQ_2_FAIL\n");
3617                                 break;
3618                         case CMAS_TX_ASSOC:
3619                                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3620                                           IPW_DL_ASSOC, "TX_ASSOC\n");
3621                                 break;
3622                         case CMAS_RX_ASSOC_RESP:
3623                                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3624                                           IPW_DL_ASSOC, "RX_ASSOC_RESP\n");
3625                                 break;
3626                         case CMAS_ASSOCIATED:
3627                                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
3628                                           IPW_DL_ASSOC, "ASSOCIATED\n");
3629                                 break;
3630                         default:
3631                                 IPW_DEBUG_NOTIF("auth: failure - %d\n",
3632                                                 auth->state);
3633                                 break;
3634                         }
3635                         break;
3636                 }
3637
3638         case HOST_NOTIFICATION_STATUS_SCAN_CHANNEL_RESULT:{
3639                         struct notif_channel_result *x =
3640                             &notif->u.channel_result;
3641
3642                         if (notif->size == sizeof(*x)) {
3643                                 IPW_DEBUG_SCAN("Scan result for channel %d\n",
3644                                                x->channel_num);
3645                         } else {
3646                                 IPW_DEBUG_SCAN("Scan result of wrong size %d "
3647                                                "(should be %zd)\n",
3648                                                notif->size, sizeof(*x));
3649                         }
3650                         break;
3651                 }
3652
3653         case HOST_NOTIFICATION_STATUS_SCAN_COMPLETED:{
3654                         struct notif_scan_complete *x = &notif->u.scan_complete;
3655                         if (notif->size == sizeof(*x)) {
3656                                 IPW_DEBUG_SCAN
3657                                     ("Scan completed: type %d, %d channels, "
3658                                      "%d status\n", x->scan_type,
3659                                      x->num_channels, x->status);
3660                         } else {
3661                                 IPW_ERROR("Scan completed of wrong size %d "
3662                                           "(should be %zd)\n",
3663                                           notif->size, sizeof(*x));
3664                         }
3665
3666                         priv->status &=
3667                             ~(STATUS_SCANNING | STATUS_SCAN_ABORTING);
3668
3669                         cancel_delayed_work(&priv->scan_check);
3670
3671                         if (!(priv->status & (STATUS_ASSOCIATED |
3672                                               STATUS_ASSOCIATING |
3673                                               STATUS_ROAMING |
3674                                               STATUS_DISASSOCIATING)))
3675                                 queue_work(priv->workqueue, &priv->associate);
3676                         else if (priv->status & STATUS_ROAMING) {
3677                                 /* If a scan completed and we are in roam mode, then
3678                                  * the scan that completed was the one requested as a
3679                                  * result of entering roam... so, schedule the
3680                                  * roam work */
3681                                 queue_work(priv->workqueue, &priv->roam);
3682                         } else if (priv->status & STATUS_SCAN_PENDING)
3683                                 queue_work(priv->workqueue,
3684                                            &priv->request_scan);
3685
3686                         priv->ieee->scans++;
3687                         break;
3688                 }
3689
3690         case HOST_NOTIFICATION_STATUS_FRAG_LENGTH:{
3691                         struct notif_frag_length *x = &notif->u.frag_len;
3692
3693                         if (notif->size == sizeof(*x)) {
3694                                 IPW_ERROR("Frag length: %d\n", x->frag_length);
3695                         } else {
3696                                 IPW_ERROR("Frag length of wrong size %d "
3697                                           "(should be %zd)\n",
3698                                           notif->size, sizeof(*x));
3699                         }
3700                         break;
3701                 }
3702
3703         case HOST_NOTIFICATION_STATUS_LINK_DETERIORATION:{
3704                         struct notif_link_deterioration *x =
3705                             &notif->u.link_deterioration;
3706                         if (notif->size == sizeof(*x)) {
3707                                 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
3708                                           "link deterioration: '%s' " MAC_FMT
3709                                           " \n", escape_essid(priv->essid,
3710                                                               priv->essid_len),
3711                                           MAC_ARG(priv->bssid));
3712                                 memcpy(&priv->last_link_deterioration, x,
3713                                        sizeof(*x));
3714                         } else {
3715                                 IPW_ERROR("Link Deterioration of wrong size %d "
3716                                           "(should be %zd)\n",
3717                                           notif->size, sizeof(*x));
3718                         }
3719                         break;
3720                 }
3721
3722         case HOST_NOTIFICATION_DINO_CONFIG_RESPONSE:{
3723                         IPW_ERROR("Dino config\n");
3724                         if (priv->hcmd
3725                             && priv->hcmd->cmd == HOST_CMD_DINO_CONFIG) {
3726                                 /* TODO: Do anything special? */
3727                         } else {
3728                                 IPW_ERROR("Unexpected DINO_CONFIG_RESPONSE\n");
3729                         }
3730                         break;
3731                 }
3732
3733         case HOST_NOTIFICATION_STATUS_BEACON_STATE:{
3734                         struct notif_beacon_state *x = &notif->u.beacon_state;
3735                         if (notif->size != sizeof(*x)) {
3736                                 IPW_ERROR
3737                                     ("Beacon state of wrong size %d (should "
3738                                      "be %zd)\n", notif->size, sizeof(*x));
3739                                 break;
3740                         }
3741
3742                         if (x->state == HOST_NOTIFICATION_STATUS_BEACON_MISSING)
3743                                 ipw_handle_missed_beacon(priv, x->number);
3744
3745                         break;
3746                 }
3747
3748         case HOST_NOTIFICATION_STATUS_TGI_TX_KEY:{
3749                         struct notif_tgi_tx_key *x = &notif->u.tgi_tx_key;
3750                         if (notif->size == sizeof(*x)) {
3751                                 IPW_ERROR("TGi Tx Key: state 0x%02x sec type "
3752                                           "0x%02x station %d\n",
3753                                           x->key_state, x->security_type,
3754                                           x->station_index);
3755                                 break;
3756                         }
3757
3758                         IPW_ERROR
3759                             ("TGi Tx Key of wrong size %d (should be %zd)\n",
3760                              notif->size, sizeof(*x));
3761                         break;
3762                 }
3763
3764         case HOST_NOTIFICATION_CALIB_KEEP_RESULTS:{
3765                         struct notif_calibration *x = &notif->u.calibration;
3766
3767                         if (notif->size == sizeof(*x)) {
3768                                 memcpy(&priv->calib, x, sizeof(*x));
3769                                 IPW_DEBUG_INFO("TODO: Calibration\n");
3770                                 break;
3771                         }
3772
3773                         IPW_ERROR
3774                             ("Calibration of wrong size %d (should be %zd)\n",
3775                              notif->size, sizeof(*x));
3776                         break;
3777                 }
3778
3779         case HOST_NOTIFICATION_NOISE_STATS:{
3780                         if (notif->size == sizeof(u32)) {
3781                                 priv->last_noise =
3782                                     (u8) (notif->u.noise.value & 0xff);
3783                                 average_add(&priv->average_noise,
3784                                             priv->last_noise);
3785                                 break;
3786                         }
3787
3788                         IPW_ERROR
3789                             ("Noise stat is wrong size %d (should be %zd)\n",
3790                              notif->size, sizeof(u32));
3791                         break;
3792                 }
3793
3794         default:
3795                 IPW_ERROR("Unknown notification: "
3796                           "subtype=%d,flags=0x%2x,size=%d\n",
3797                           notif->subtype, notif->flags, notif->size);
3798         }
3799 }
3800
3801 /**
3802  * Destroys all DMA structures and initialise them again
3803  *
3804  * @param priv
3805  * @return error code
3806  */
3807 static int ipw_queue_reset(struct ipw_priv *priv)
3808 {
3809         int rc = 0;
3810         /** @todo customize queue sizes */
3811         int nTx = 64, nTxCmd = 8;
3812         ipw_tx_queue_free(priv);
3813         /* Tx CMD queue */
3814         rc = ipw_queue_tx_init(priv, &priv->txq_cmd, nTxCmd,
3815                                CX2_TX_CMD_QUEUE_READ_INDEX,
3816                                CX2_TX_CMD_QUEUE_WRITE_INDEX,
3817                                CX2_TX_CMD_QUEUE_BD_BASE,
3818                                CX2_TX_CMD_QUEUE_BD_SIZE);
3819         if (rc) {
3820                 IPW_ERROR("Tx Cmd queue init failed\n");
3821                 goto error;
3822         }
3823         /* Tx queue(s) */
3824         rc = ipw_queue_tx_init(priv, &priv->txq[0], nTx,
3825                                CX2_TX_QUEUE_0_READ_INDEX,
3826                                CX2_TX_QUEUE_0_WRITE_INDEX,
3827                                CX2_TX_QUEUE_0_BD_BASE, CX2_TX_QUEUE_0_BD_SIZE);
3828         if (rc) {
3829                 IPW_ERROR("Tx 0 queue init failed\n");
3830                 goto error;
3831         }
3832         rc = ipw_queue_tx_init(priv, &priv->txq[1], nTx,
3833                                CX2_TX_QUEUE_1_READ_INDEX,
3834                                CX2_TX_QUEUE_1_WRITE_INDEX,
3835                                CX2_TX_QUEUE_1_BD_BASE, CX2_TX_QUEUE_1_BD_SIZE);
3836         if (rc) {
3837                 IPW_ERROR("Tx 1 queue init failed\n");
3838                 goto error;
3839         }
3840         rc = ipw_queue_tx_init(priv, &priv->txq[2], nTx,
3841                                CX2_TX_QUEUE_2_READ_INDEX,
3842                                CX2_TX_QUEUE_2_WRITE_INDEX,
3843                                CX2_TX_QUEUE_2_BD_BASE, CX2_TX_QUEUE_2_BD_SIZE);
3844         if (rc) {
3845                 IPW_ERROR("Tx 2 queue init failed\n");
3846                 goto error;
3847         }
3848         rc = ipw_queue_tx_init(priv, &priv->txq[3], nTx,
3849                                CX2_TX_QUEUE_3_READ_INDEX,
3850                                CX2_TX_QUEUE_3_WRITE_INDEX,
3851                                CX2_TX_QUEUE_3_BD_BASE, CX2_TX_QUEUE_3_BD_SIZE);
3852         if (rc) {
3853                 IPW_ERROR("Tx 3 queue init failed\n");
3854                 goto error;
3855         }
3856         /* statistics */
3857         priv->rx_bufs_min = 0;
3858         priv->rx_pend_max = 0;
3859         return rc;
3860
3861       error:
3862         ipw_tx_queue_free(priv);
3863         return rc;
3864 }
3865
3866 /**
3867  * Reclaim Tx queue entries no more used by NIC.
3868  *
3869  * When FW adwances 'R' index, all entries between old and
3870  * new 'R' index need to be reclaimed. As result, some free space
3871  * forms. If there is enough free space (> low mark), wake Tx queue.
3872  *
3873  * @note Need to protect against garbage in 'R' index
3874  * @param priv
3875  * @param txq
3876  * @param qindex
3877  * @return Number of used entries remains in the queue
3878  */
3879 static int ipw_queue_tx_reclaim(struct ipw_priv *priv,
3880                                 struct clx2_tx_queue *txq, int qindex)
3881 {
3882         u32 hw_tail;
3883         int used;
3884         struct clx2_queue *q = &txq->q;
3885
3886         hw_tail = ipw_read32(priv, q->reg_r);
3887         if (hw_tail >= q->n_bd) {
3888                 IPW_ERROR
3889                     ("Read index for DMA queue (%d) is out of range [0-%d)\n",
3890                      hw_tail, q->n_bd);
3891                 goto done;
3892         }
3893         for (; q->last_used != hw_tail;
3894              q->last_used = ipw_queue_inc_wrap(q->last_used, q->n_bd)) {
3895                 ipw_queue_tx_free_tfd(priv, txq);
3896                 priv->tx_packets++;
3897         }
3898       done:
3899         if (ipw_queue_space(q) > q->low_mark && qindex >= 0) {
3900                 __maybe_wake_tx(priv);
3901         }
3902         used = q->first_empty - q->last_used;
3903         if (used < 0)
3904                 used += q->n_bd;
3905
3906         return used;
3907 }
3908
3909 static int ipw_queue_tx_hcmd(struct ipw_priv *priv, int hcmd, void *buf,
3910                              int len, int sync)
3911 {
3912         struct clx2_tx_queue *txq = &priv->txq_cmd;
3913         struct clx2_queue *q = &txq->q;
3914         struct tfd_frame *tfd;
3915
3916         if (ipw_queue_space(q) < (sync ? 1 : 2)) {
3917                 IPW_ERROR("No space for Tx\n");
3918                 return -EBUSY;
3919         }
3920
3921         tfd = &txq->bd[q->first_empty];
3922         txq->txb[q->first_empty] = NULL;
3923
3924         memset(tfd, 0, sizeof(*tfd));
3925         tfd->control_flags.message_type = TX_HOST_COMMAND_TYPE;
3926         tfd->control_flags.control_bits = TFD_NEED_IRQ_MASK;
3927         priv->hcmd_seq++;
3928         tfd->u.cmd.index = hcmd;
3929         tfd->u.cmd.length = len;
3930         memcpy(tfd->u.cmd.payload, buf, len);
3931         q->first_empty = ipw_queue_inc_wrap(q->first_empty, q->n_bd);
3932         ipw_write32(priv, q->reg_w, q->first_empty);
3933         _ipw_read32(priv, 0x90);
3934
3935         return 0;
3936 }
3937
3938 /*
3939  * Rx theory of operation
3940  *
3941  * The host allocates 32 DMA target addresses and passes the host address
3942  * to the firmware at register CX2_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3943  * 0 to 31
3944  *
3945  * Rx Queue Indexes
3946  * The host/firmware share two index registers for managing the Rx buffers.
3947  *
3948  * The READ index maps to the first position that the firmware may be writing
3949  * to -- the driver can read up to (but not including) this position and get
3950  * good data.
3951  * The READ index is managed by the firmware once the card is enabled.
3952  *
3953  * The WRITE index maps to the last position the driver has read from -- the
3954  * position preceding WRITE is the last slot the firmware can place a packet.
3955  *
3956  * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3957  * WRITE = READ.
3958  *
3959  * During initialization the host sets up the READ queue position to the first
3960  * INDEX position, and WRITE to the last (READ - 1 wrapped)
3961  *
3962  * When the firmware places a packet in a buffer it will advance the READ index
3963  * and fire the RX interrupt.  The driver can then query the READ index and
3964  * process as many packets as possible, moving the WRITE index forward as it
3965  * resets the Rx queue buffers with new memory.
3966  *
3967  * The management in the driver is as follows:
3968  * + A list of pre-allocated SKBs is stored in ipw->rxq->rx_free.  When
3969  *   ipw->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
3970  *   to replensish the ipw->rxq->rx_free.
3971  * + In ipw_rx_queue_replenish (scheduled) if 'processed' != 'read' then the
3972  *   ipw->rxq is replenished and the READ INDEX is updated (updating the
3973  *   'processed' and 'read' driver indexes as well)
3974  * + A received packet is processed and handed to the kernel network stack,
3975  *   detached from the ipw->rxq.  The driver 'processed' index is updated.
3976  * + The Host/Firmware ipw->rxq is replenished at tasklet time from the rx_free
3977  *   list. If there are no allocated buffers in ipw->rxq->rx_free, the READ
3978  *   INDEX is not incremented and ipw->status(RX_STALLED) is set.  If there
3979  *   were enough free buffers and RX_STALLED is set it is cleared.
3980  *
3981  *
3982  * Driver sequence:
3983  *
3984  * ipw_rx_queue_alloc()       Allocates rx_free
3985  * ipw_rx_queue_replenish()   Replenishes rx_free list from rx_used, and calls
3986  *                            ipw_rx_queue_restock
3987  * ipw_rx_queue_restock()     Moves available buffers from rx_free into Rx
3988  *                            queue, updates firmware pointers, and updates
3989  *                            the WRITE index.  If insufficient rx_free buffers
3990  *                            are available, schedules ipw_rx_queue_replenish
3991  *
3992  * -- enable interrupts --
3993  * ISR - ipw_rx()             Detach ipw_rx_mem_buffers from pool up to the
3994  *                            READ INDEX, detaching the SKB from the pool.
3995  *                            Moves the packet buffer from queue to rx_used.
3996  *                            Calls ipw_rx_queue_restock to refill any empty
3997  *                            slots.
3998  * ...
3999  *
4000  */
4001
4002 /*
4003  * If there are slots in the RX queue that  need to be restocked,
4004  * and we have free pre-allocated buffers, fill the ranks as much
4005  * as we can pulling from rx_free.
4006  *
4007  * This moves the 'write' index forward to catch up with 'processed', and
4008  * also updates the memory address in the firmware to reference the new
4009  * target buffer.
4010  */
4011 static void ipw_rx_queue_restock(struct ipw_priv *priv)
4012 {
4013         struct ipw_rx_queue *rxq = priv->rxq;
4014         struct list_head *element;
4015         struct ipw_rx_mem_buffer *rxb;
4016         unsigned long flags;
4017         int write;
4018
4019         spin_lock_irqsave(&rxq->lock, flags);
4020         write = rxq->write;
4021         while ((rxq->write != rxq->processed) && (rxq->free_count)) {
4022                 element = rxq->rx_free.next;
4023                 rxb = list_entry(element, struct ipw_rx_mem_buffer, list);
4024                 list_del(element);
4025
4026                 ipw_write32(priv, CX2_RFDS_TABLE_LOWER + rxq->write * RFD_SIZE,
4027                             rxb->dma_addr);
4028                 rxq->queue[rxq->write] = rxb;
4029                 rxq->write = (rxq->write + 1) % RX_QUEUE_SIZE;
4030                 rxq->free_count--;
4031         }
4032         spin_unlock_irqrestore(&rxq->lock, flags);
4033
4034         /* If the pre-allocated buffer pool is dropping low, schedule to
4035          * refill it */
4036         if (rxq->free_count <= RX_LOW_WATERMARK)
4037                 queue_work(priv->workqueue, &priv->rx_replenish);
4038
4039         /* If we've added more space for the firmware to place data, tell it */
4040         if (write != rxq->write)
4041                 ipw_write32(priv, CX2_RX_WRITE_INDEX, rxq->write);
4042 }
4043
4044 /*
4045  * Move all used packet from rx_used to rx_free, allocating a new SKB for each.
4046  * Also restock the Rx queue via ipw_rx_queue_restock.
4047  *
4048  * This is called as a scheduled work item (except for during intialization)
4049  */
4050 static void ipw_rx_queue_replenish(void *data)
4051 {
4052         struct ipw_priv *priv = data;
4053         struct ipw_rx_queue *rxq = priv->rxq;
4054         struct list_head *element;
4055         struct ipw_rx_mem_buffer *rxb;
4056         unsigned long flags;
4057
4058         spin_lock_irqsave(&rxq->lock, flags);
4059         while (!list_empty(&rxq->rx_used)) {
4060                 element = rxq->rx_used.next;
4061                 rxb = list_entry(element, struct ipw_rx_mem_buffer, list);
4062                 rxb->skb = alloc_skb(CX2_RX_BUF_SIZE, GFP_ATOMIC);
4063                 if (!rxb->skb) {
4064                         printk(KERN_CRIT "%s: Can not allocate SKB buffers.\n",
4065                                priv->net_dev->name);
4066                         /* We don't reschedule replenish work here -- we will
4067                          * call the restock method and if it still needs
4068                          * more buffers it will schedule replenish */
4069                         break;
4070                 }
4071                 list_del(element);
4072
4073                 rxb->rxb = (struct ipw_rx_buffer *)rxb->skb->data;
4074                 rxb->dma_addr =
4075                     pci_map_single(priv->pci_dev, rxb->skb->data,
4076                                    CX2_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4077
4078                 list_add_tail(&rxb->list, &rxq->rx_free);
4079                 rxq->free_count++;
4080         }
4081         spin_unlock_irqrestore(&rxq->lock, flags);
4082
4083         ipw_rx_queue_restock(priv);
4084 }
4085
4086 /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
4087  * If an SKB has been detached, the POOL needs to have it's SKB set to NULL
4088  * This free routine walks the list of POOL entries and if SKB is set to
4089  * non NULL it is unmapped and freed
4090  */
4091 static void ipw_rx_queue_free(struct ipw_priv *priv, struct ipw_rx_queue *rxq)
4092 {
4093         int i;
4094
4095         if (!rxq)
4096                 return;
4097
4098         for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4099                 if (rxq->pool[i].skb != NULL) {
4100                         pci_unmap_single(priv->pci_dev, rxq->pool[i].dma_addr,
4101                                          CX2_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4102                         dev_kfree_skb(rxq->pool[i].skb);
4103                 }
4104         }
4105
4106         kfree(rxq);
4107 }
4108
4109 static struct ipw_rx_queue *ipw_rx_queue_alloc(struct ipw_priv *priv)
4110 {
4111         struct ipw_rx_queue *rxq;
4112         int i;
4113
4114         rxq = (struct ipw_rx_queue *)kmalloc(sizeof(*rxq), GFP_KERNEL);
4115         if (unlikely(!rxq)) {
4116                 IPW_ERROR("memory allocation failed\n");
4117                 return NULL;
4118         }
4119         memset(rxq, 0, sizeof(*rxq));
4120         spin_lock_init(&rxq->lock);
4121         INIT_LIST_HEAD(&rxq->rx_free);
4122         INIT_LIST_HEAD(&rxq->rx_used);
4123
4124         /* Fill the rx_used queue with _all_ of the Rx buffers */
4125         for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4126                 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4127
4128         /* Set us so that we have processed and used all buffers, but have
4129          * not restocked the Rx queue with fresh buffers */
4130         rxq->read = rxq->write = 0;
4131         rxq->processed = RX_QUEUE_SIZE - 1;
4132         rxq->free_count = 0;
4133
4134         return rxq;
4135 }
4136
4137 static int ipw_is_rate_in_mask(struct ipw_priv *priv, int ieee_mode, u8 rate)
4138 {
4139         rate &= ~IEEE80211_BASIC_RATE_MASK;
4140         if (ieee_mode == IEEE_A) {
4141                 switch (rate) {
4142                 case IEEE80211_OFDM_RATE_6MB:
4143                         return priv->rates_mask & IEEE80211_OFDM_RATE_6MB_MASK ?
4144                             1 : 0;
4145                 case IEEE80211_OFDM_RATE_9MB:
4146                         return priv->rates_mask & IEEE80211_OFDM_RATE_9MB_MASK ?
4147                             1 : 0;
4148                 case IEEE80211_OFDM_RATE_12MB:
4149                         return priv->
4150                             rates_mask & IEEE80211_OFDM_RATE_12MB_MASK ? 1 : 0;
4151                 case IEEE80211_OFDM_RATE_18MB:
4152                         return priv->
4153                             rates_mask & IEEE80211_OFDM_RATE_18MB_MASK ? 1 : 0;
4154                 case IEEE80211_OFDM_RATE_24MB:
4155                         return priv->
4156                             rates_mask & IEEE80211_OFDM_RATE_24MB_MASK ? 1 : 0;
4157                 case IEEE80211_OFDM_RATE_36MB:
4158                         return priv->
4159                             rates_mask & IEEE80211_OFDM_RATE_36MB_MASK ? 1 : 0;
4160                 case IEEE80211_OFDM_RATE_48MB:
4161                         return priv->
4162                             rates_mask & IEEE80211_OFDM_RATE_48MB_MASK ? 1 : 0;
4163                 case IEEE80211_OFDM_RATE_54MB:
4164                         return priv->
4165                             rates_mask & IEEE80211_OFDM_RATE_54MB_MASK ? 1 : 0;
4166                 default:
4167                         return 0;
4168                 }
4169         }
4170
4171         /* B and G mixed */
4172         switch (rate) {
4173         case IEEE80211_CCK_RATE_1MB:
4174                 return priv->rates_mask & IEEE80211_CCK_RATE_1MB_MASK ? 1 : 0;
4175         case IEEE80211_CCK_RATE_2MB:
4176                 return priv->rates_mask & IEEE80211_CCK_RATE_2MB_MASK ? 1 : 0;
4177         case IEEE80211_CCK_RATE_5MB:
4178                 return priv->rates_mask & IEEE80211_CCK_RATE_5MB_MASK ? 1 : 0;
4179         case IEEE80211_CCK_RATE_11MB:
4180                 return priv->rates_mask & IEEE80211_CCK_RATE_11MB_MASK ? 1 : 0;
4181         }
4182
4183         /* If we are limited to B modulations, bail at this point */
4184         if (ieee_mode == IEEE_B)
4185                 return 0;
4186
4187         /* G */
4188         switch (rate) {
4189         case IEEE80211_OFDM_RATE_6MB:
4190                 return priv->rates_mask & IEEE80211_OFDM_RATE_6MB_MASK ? 1 : 0;
4191         case IEEE80211_OFDM_RATE_9MB:
4192                 return priv->rates_mask & IEEE80211_OFDM_RATE_9MB_MASK ? 1 : 0;
4193         case IEEE80211_OFDM_RATE_12MB:
4194                 return priv->rates_mask & IEEE80211_OFDM_RATE_12MB_MASK ? 1 : 0;
4195         case IEEE80211_OFDM_RATE_18MB:
4196                 return priv->rates_mask & IEEE80211_OFDM_RATE_18MB_MASK ? 1 : 0;
4197         case IEEE80211_OFDM_RATE_24MB:
4198                 return priv->rates_mask & IEEE80211_OFDM_RATE_24MB_MASK ? 1 : 0;
4199         case IEEE80211_OFDM_RATE_36MB:
4200                 return priv->rates_mask & IEEE80211_OFDM_RATE_36MB_MASK ? 1 : 0;
4201         case IEEE80211_OFDM_RATE_48MB:
4202                 return priv->rates_mask & IEEE80211_OFDM_RATE_48MB_MASK ? 1 : 0;
4203         case IEEE80211_OFDM_RATE_54MB:
4204                 return priv->rates_mask & IEEE80211_OFDM_RATE_54MB_MASK ? 1 : 0;
4205         }
4206
4207         return 0;
4208 }
4209
4210 static int ipw_compatible_rates(struct ipw_priv *priv,
4211                                 const struct ieee80211_network *network,
4212                                 struct ipw_supported_rates *rates)
4213 {
4214         int num_rates, i;
4215
4216         memset(rates, 0, sizeof(*rates));
4217         num_rates = min(network->rates_len, (u8) IPW_MAX_RATES);
4218         rates->num_rates = 0;
4219         for (i = 0; i < num_rates; i++) {
4220                 if (!ipw_is_rate_in_mask
4221                     (priv, network->mode, network->rates[i])) {
4222                         if (network->rates[i] & IEEE80211_BASIC_RATE_MASK) {
4223                                 IPW_DEBUG_SCAN
4224                                     ("Basic rate %02X masked: 0x%08X\n",
4225                                      network->rates[i], priv->rates_mask);
4226                                 return 0;
4227                         }
4228
4229                         IPW_DEBUG_SCAN("Rate %02X masked : 0x%08X\n",
4230                                        network->rates[i], priv->rates_mask);
4231                         continue;
4232                 }
4233
4234                 rates->supported_rates[rates->num_rates++] = network->rates[i];
4235         }
4236
4237         num_rates =
4238             min(network->rates_ex_len, (u8) (IPW_MAX_RATES - num_rates));
4239         for (i = 0; i < num_rates; i++) {
4240                 if (!ipw_is_rate_in_mask
4241                     (priv, network->mode, network->rates_ex[i])) {
4242                         if (network->rates_ex[i] & IEEE80211_BASIC_RATE_MASK) {
4243                                 IPW_DEBUG_SCAN
4244                                     ("Basic rate %02X masked: 0x%08X\n",
4245                                      network->rates_ex[i], priv->rates_mask);
4246                                 return 0;
4247                         }
4248
4249                         IPW_DEBUG_SCAN("Rate %02X masked : 0x%08X\n",
4250                                        network->rates_ex[i], priv->rates_mask);
4251                         continue;
4252                 }
4253
4254                 rates->supported_rates[rates->num_rates++] =
4255                     network->rates_ex[i];
4256         }
4257
4258         return 1;
4259 }
4260
4261 static inline void ipw_copy_rates(struct ipw_supported_rates *dest,
4262                                   const struct ipw_supported_rates *src)
4263 {
4264         u8 i;
4265         for (i = 0; i < src->num_rates; i++)
4266                 dest->supported_rates[i] = src->supported_rates[i];
4267         dest->num_rates = src->num_rates;
4268 }
4269
4270 /* TODO: Look at sniffed packets in the air to determine if the basic rate
4271  * mask should ever be used -- right now all callers to add the scan rates are
4272  * set with the modulation = CCK, so BASIC_RATE_MASK is never set... */
4273 static void ipw_add_cck_scan_rates(struct ipw_supported_rates *rates,
4274                                    u8 modulation, u32 rate_mask)
4275 {
4276         u8 basic_mask = (IEEE80211_OFDM_MODULATION == modulation) ?
4277             IEEE80211_BASIC_RATE_MASK : 0;
4278
4279         if (rate_mask & IEEE80211_CCK_RATE_1MB_MASK)
4280                 rates->supported_rates[rates->num_rates++] =
4281                     IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
4282
4283         if (rate_mask & IEEE80211_CCK_RATE_2MB_MASK)
4284                 rates->supported_rates[rates->num_rates++] =
4285                     IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
4286
4287         if (rate_mask & IEEE80211_CCK_RATE_5MB_MASK)
4288                 rates->supported_rates[rates->num_rates++] = basic_mask |
4289                     IEEE80211_CCK_RATE_5MB;
4290
4291         if (rate_mask & IEEE80211_CCK_RATE_11MB_MASK)
4292                 rates->supported_rates[rates->num_rates++] = basic_mask |
4293                     IEEE80211_CCK_RATE_11MB;
4294 }
4295
4296 static void ipw_add_ofdm_scan_rates(struct ipw_supported_rates *rates,
4297                                     u8 modulation, u32 rate_mask)
4298 {
4299         u8 basic_mask = (IEEE80211_OFDM_MODULATION == modulation) ?
4300             IEEE80211_BASIC_RATE_MASK : 0;
4301
4302         if (rate_mask & IEEE80211_OFDM_RATE_6MB_MASK)
4303                 rates->supported_rates[rates->num_rates++] = basic_mask |
4304                     IEEE80211_OFDM_RATE_6MB;
4305
4306         if (rate_mask & IEEE80211_OFDM_RATE_9MB_MASK)
4307                 rates->supported_rates[rates->num_rates++] =
4308                     IEEE80211_OFDM_RATE_9MB;
4309
4310         if (rate_mask & IEEE80211_OFDM_RATE_12MB_MASK)
4311                 rates->supported_rates[rates->num_rates++] = basic_mask |
4312                     IEEE80211_OFDM_RATE_12MB;
4313
4314         if (rate_mask & IEEE80211_OFDM_RATE_18MB_MASK)
4315                 rates->supported_rates[rates->num_rates++] =
4316                     IEEE80211_OFDM_RATE_18MB;
4317
4318         if (rate_mask & IEEE80211_OFDM_RATE_24MB_MASK)
4319                 rates->supported_rates[rates->num_rates++] = basic_mask |
4320                     IEEE80211_OFDM_RATE_24MB;
4321
4322         if (rate_mask & IEEE80211_OFDM_RATE_36MB_MASK)
4323                 rates->supported_rates[rates->num_rates++] =
4324                     IEEE80211_OFDM_RATE_36MB;
4325
4326         if (rate_mask & IEEE80211_OFDM_RATE_48MB_MASK)
4327                 rates->supported_rates[rates->num_rates++] =
4328                     IEEE80211_OFDM_RATE_48MB;
4329
4330         if (rate_mask & IEEE80211_OFDM_RATE_54MB_MASK)
4331                 rates->supported_rates[rates->num_rates++] =
4332                     IEEE80211_OFDM_RATE_54MB;
4333 }
4334
4335 struct ipw_network_match {
4336         struct ieee80211_network *network;
4337         struct ipw_supported_rates rates;
4338 };
4339
4340 static int ipw_best_network(struct ipw_priv *priv,
4341                             struct ipw_network_match *match,
4342                             struct ieee80211_network *network, int roaming)
4343 {
4344         struct ipw_supported_rates rates;
4345
4346         /* Verify that this network's capability is compatible with the
4347          * current mode (AdHoc or Infrastructure) */
4348         if ((priv->ieee->iw_mode == IW_MODE_INFRA &&
4349              !(network->capability & WLAN_CAPABILITY_ESS)) ||
4350             (priv->ieee->iw_mode == IW_MODE_ADHOC &&
4351              !(network->capability & WLAN_CAPABILITY_IBSS))) {
4352                 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded due to "
4353                                 "capability mismatch.\n",
4354                                 escape_essid(network->ssid, network->ssid_len),
4355                                 MAC_ARG(network->bssid));
4356                 return 0;
4357         }
4358
4359         /* If we do not have an ESSID for this AP, we can not associate with
4360          * it */
4361         if (network->flags & NETWORK_EMPTY_ESSID) {
4362                 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
4363                                 "because of hidden ESSID.\n",
4364                                 escape_essid(network->ssid, network->ssid_len),
4365                                 MAC_ARG(network->bssid));
4366                 return 0;
4367         }
4368
4369         if (unlikely(roaming)) {
4370                 /* If we are roaming, then ensure check if this is a valid
4371                  * network to try and roam to */
4372                 if ((network->ssid_len != match->network->ssid_len) ||
4373                     memcmp(network->ssid, match->network->ssid,
4374                            network->ssid_len)) {
4375                         IPW_DEBUG_ASSOC("Netowrk '%s (" MAC_FMT ")' excluded "
4376                                         "because of non-network ESSID.\n",
4377                                         escape_essid(network->ssid,
4378                                                      network->ssid_len),
4379                                         MAC_ARG(network->bssid));
4380                         return 0;
4381                 }
4382         } else {
4383                 /* If an ESSID has been configured then compare the broadcast
4384                  * ESSID to ours */
4385                 if ((priv->config & CFG_STATIC_ESSID) &&
4386                     ((network->ssid_len != priv->essid_len) ||
4387                      memcmp(network->ssid, priv->essid,
4388                             min(network->ssid_len, priv->essid_len)))) {
4389                         char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
4390                         strncpy(escaped,
4391                                 escape_essid(network->ssid, network->ssid_len),
4392                                 sizeof(escaped));
4393                         IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
4394                                         "because of ESSID mismatch: '%s'.\n",
4395                                         escaped, MAC_ARG(network->bssid),
4396                                         escape_essid(priv->essid,
4397                                                      priv->essid_len));
4398                         return 0;
4399                 }
4400         }
4401
4402         /* If the old network rate is better than this one, don't bother
4403          * testing everything else. */
4404         if (match->network && match->network->stats.rssi > network->stats.rssi) {
4405                 char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
4406                 strncpy(escaped,
4407                         escape_essid(network->ssid, network->ssid_len),
4408                         sizeof(escaped));
4409                 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded because "
4410                                 "'%s (" MAC_FMT ")' has a stronger signal.\n",
4411                                 escaped, MAC_ARG(network->bssid),
4412                                 escape_essid(match->network->ssid,
4413                                              match->network->ssid_len),
4414                                 MAC_ARG(match->network->bssid));
4415                 return 0;
4416         }
4417
4418         /* If this network has already had an association attempt within the
4419          * last 3 seconds, do not try and associate again... */
4420         if (network->last_associate &&
4421             time_after(network->last_associate + (HZ * 3UL), jiffies)) {
4422                 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
4423                                 "because of storming (%lu since last "
4424                                 "assoc attempt).\n",
4425                                 escape_essid(network->ssid, network->ssid_len),
4426                                 MAC_ARG(network->bssid),
4427                                 (jiffies - network->last_associate) / HZ);
4428                 return 0;
4429         }
4430
4431         /* Now go through and see if the requested network is valid... */
4432         if (priv->ieee->scan_age != 0 &&
4433             time_after(jiffies, network->last_scanned + priv->ieee->scan_age)) {
4434                 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
4435                                 "because of age: %lums.\n",
4436                                 escape_essid(network->ssid, network->ssid_len),
4437                                 MAC_ARG(network->bssid),
4438                                 (jiffies - network->last_scanned) / (HZ / 100));
4439                 return 0;
4440         }
4441
4442         if ((priv->config & CFG_STATIC_CHANNEL) &&
4443             (network->channel != priv->channel)) {
4444                 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
4445                                 "because of channel mismatch: %d != %d.\n",
4446                                 escape_essid(network->ssid, network->ssid_len),
4447                                 MAC_ARG(network->bssid),
4448                                 network->channel, priv->channel);
4449                 return 0;
4450         }
4451
4452         /* Verify privacy compatability */
4453         if (((priv->capability & CAP_PRIVACY_ON) ? 1 : 0) !=
4454             ((network->capability & WLAN_CAPABILITY_PRIVACY) ? 1 : 0)) {
4455                 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
4456                                 "because of privacy mismatch: %s != %s.\n",
4457                                 escape_essid(network->ssid, network->ssid_len),
4458                                 MAC_ARG(network->bssid),
4459                                 priv->capability & CAP_PRIVACY_ON ? "on" :
4460                                 "off",
4461                                 network->capability &
4462                                 WLAN_CAPABILITY_PRIVACY ? "on" : "off");
4463                 return 0;
4464         }
4465
4466         if ((priv->config & CFG_STATIC_BSSID) &&
4467             memcmp(network->bssid, priv->bssid, ETH_ALEN)) {
4468                 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
4469                                 "because of BSSID mismatch: " MAC_FMT ".\n",
4470                                 escape_essid(network->ssid, network->ssid_len),
4471                                 MAC_ARG(network->bssid), MAC_ARG(priv->bssid));
4472                 return 0;
4473         }
4474
4475         /* Filter out any incompatible freq / mode combinations */
4476         if (!ieee80211_is_valid_mode(priv->ieee, network->mode)) {
4477                 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
4478                                 "because of invalid frequency/mode "
4479                                 "combination.\n",
4480                                 escape_essid(network->ssid, network->ssid_len),
4481                                 MAC_ARG(network->bssid));
4482                 return 0;
4483         }
4484
4485         /* Ensure that the rates supported by the driver are compatible with
4486          * this AP, including verification of basic rates (mandatory) */
4487         if (!ipw_compatible_rates(priv, network, &rates)) {
4488                 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
4489                                 "because configured rate mask excludes "
4490                                 "AP mandatory rate.\n",
4491                                 escape_essid(network->ssid, network->ssid_len),
4492                                 MAC_ARG(network->bssid));
4493                 return 0;
4494         }
4495
4496         if (rates.num_rates == 0) {
4497                 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
4498                                 "because of no compatible rates.\n",
4499                                 escape_essid(network->ssid, network->ssid_len),
4500                                 MAC_ARG(network->bssid));
4501                 return 0;
4502         }
4503
4504         /* TODO: Perform any further minimal comparititive tests.  We do not
4505          * want to put too much policy logic here; intelligent scan selection
4506          * should occur within a generic IEEE 802.11 user space tool.  */
4507
4508         /* Set up 'new' AP to this network */
4509         ipw_copy_rates(&match->rates, &rates);
4510         match->network = network;
4511
4512         IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' is a viable match.\n",
4513                         escape_essid(network->ssid, network->ssid_len),
4514                         MAC_ARG(network->bssid));
4515
4516         return 1;
4517 }
4518
4519 static void ipw_adhoc_create(struct ipw_priv *priv,
4520                              struct ieee80211_network *network)
4521 {
4522         /*
4523          * For the purposes of scanning, we can set our wireless mode
4524          * to trigger scans across combinations of bands, but when it
4525          * comes to creating a new ad-hoc network, we have tell the FW
4526          * exactly which band to use.
4527          *
4528          * We also have the possibility of an invalid channel for the
4529          * chossen band.  Attempting to create a new ad-hoc network
4530          * with an invalid channel for wireless mode will trigger a
4531          * FW fatal error.
4532          */
4533         network->mode = is_valid_channel(priv->ieee->mode, priv->channel);
4534         if (network->mode) {
4535                 network->channel = priv->channel;
4536         } else {
4537                 IPW_WARNING("Overriding invalid channel\n");
4538                 if (priv->ieee->mode & IEEE_A) {
4539                         network->mode = IEEE_A;
4540                         priv->channel = band_a_active_channel[0];
4541                 } else if (priv->ieee->mode & IEEE_G) {
4542                         network->mode = IEEE_G;
4543                         priv->channel = band_b_active_channel[0];
4544                 } else {
4545                         network->mode = IEEE_B;
4546                         priv->channel = band_b_active_channel[0];
4547                 }
4548         }
4549
4550         network->channel = priv->channel;
4551         priv->config |= CFG_ADHOC_PERSIST;
4552         ipw_create_bssid(priv, network->bssid);
4553         network->ssid_len = priv->essid_len;
4554         memcpy(network->ssid, priv->essid, priv->essid_len);
4555         memset(&network->stats, 0, sizeof(network->stats));
4556         network->capability = WLAN_CAPABILITY_IBSS;
4557         if (!(priv->config & CFG_PREAMBLE_LONG))
4558                 network->capability |= WLAN_CAPABILITY_SHORT_PREAMBLE;
4559         if (priv->capability & CAP_PRIVACY_ON)
4560                 network->capability |= WLAN_CAPABILITY_PRIVACY;
4561         network->rates_len = min(priv->rates.num_rates, MAX_RATES_LENGTH);
4562         memcpy(network->rates, priv->rates.supported_rates, network->rates_len);
4563         network->rates_ex_len = priv->rates.num_rates - network->rates_len;
4564         memcpy(network->rates_ex,
4565                &priv->rates.supported_rates[network->rates_len],
4566                network->rates_ex_len);
4567         network->last_scanned = 0;
4568         network->flags = 0;
4569         network->last_associate = 0;
4570         network->time_stamp[0] = 0;
4571         network->time_stamp[1] = 0;
4572         network->beacon_interval = 100; /* Default */
4573         network->listen_interval = 10;  /* Default */
4574         network->atim_window = 0;       /* Default */
4575 #ifdef CONFIG_IEEE80211_WPA
4576         network->wpa_ie_len = 0;
4577         network->rsn_ie_len = 0;
4578 #endif                          /* CONFIG_IEEE80211_WPA */
4579 }
4580
4581 static void ipw_send_wep_keys(struct ipw_priv *priv)
4582 {
4583         struct ipw_wep_key *key;
4584         int i;
4585         struct host_cmd cmd = {
4586                 .cmd = IPW_CMD_WEP_KEY,
4587                 .len = sizeof(*key)
4588         };
4589
4590         key = (struct ipw_wep_key *)&cmd.param;
4591         key->cmd_id = DINO_CMD_WEP_KEY;
4592         key->seq_num = 0;
4593
4594         for (i = 0; i < 4; i++) {
4595                 key->key_index = i;
4596                 if (!(priv->sec.flags & (1 << i))) {
4597                         key->key_size = 0;
4598                 } else {
4599                         key->key_size = priv->sec.key_sizes[i];
4600                         memcpy(key->key, priv->sec.keys[i], key->key_size);
4601                 }
4602
4603                 if (ipw_send_cmd(priv, &cmd)) {
4604                         IPW_ERROR("failed to send WEP_KEY command\n");
4605                         return;
4606                 }
4607         }
4608 }
4609
4610 static void ipw_adhoc_check(void *data)
4611 {
4612         struct ipw_priv *priv = data;
4613
4614         if (priv->missed_adhoc_beacons++ > priv->missed_beacon_threshold &&
4615             !(priv->config & CFG_ADHOC_PERSIST)) {
4616                 IPW_DEBUG_SCAN("Disassociating due to missed beacons\n");
4617                 ipw_remove_current_network(priv);
4618                 ipw_disassociate(priv);
4619                 return;
4620         }
4621
4622         queue_delayed_work(priv->workqueue, &priv->adhoc_check,
4623                            priv->assoc_request.beacon_interval);
4624 }
4625
4626 #ifdef CONFIG_IPW_DEBUG
4627 static void ipw_debug_config(struct ipw_priv *priv)
4628 {
4629         IPW_DEBUG_INFO("Scan completed, no valid APs matched "
4630                        "[CFG 0x%08X]\n", priv->config);
4631         if (priv->config & CFG_STATIC_CHANNEL)
4632                 IPW_DEBUG_INFO("Channel locked to %d\n", priv->channel);
4633         else
4634                 IPW_DEBUG_INFO("Channel unlocked.\n");
4635         if (priv->config & CFG_STATIC_ESSID)
4636                 IPW_DEBUG_INFO("ESSID locked to '%s'\n",
4637                                escape_essid(priv->essid, priv->essid_len));
4638         else
4639                 IPW_DEBUG_INFO("ESSID unlocked.\n");
4640         if (priv->config & CFG_STATIC_BSSID)
4641                 IPW_DEBUG_INFO("BSSID locked to " MAC_FMT "\n",
4642                                MAC_ARG(priv->bssid));
4643         else
4644                 IPW_DEBUG_INFO("BSSID unlocked.\n");
4645         if (priv->capability & CAP_PRIVACY_ON)
4646                 IPW_DEBUG_INFO("PRIVACY on\n");
4647         else
4648                 IPW_DEBUG_INFO("PRIVACY off\n");
4649         IPW_DEBUG_INFO("RATE MASK: 0x%08X\n", priv->rates_mask);
4650 }
4651 #else
4652 #define ipw_debug_config(x) do {} while (0)
4653 #endif
4654
4655 static inline void ipw_set_fixed_rate(struct ipw_priv *priv,
4656                                       struct ieee80211_network *network)
4657 {
4658         /* TODO: Verify that this works... */
4659         struct ipw_fixed_rate fr = {
4660                 .tx_rates = priv->rates_mask
4661         };
4662         u32 reg;
4663         u16 mask = 0;
4664
4665         /* Identify 'current FW band' and match it with the fixed
4666          * Tx rates */
4667
4668         switch (priv->ieee->freq_band) {
4669         case IEEE80211_52GHZ_BAND:      /* A only */
4670                 /* IEEE_A */
4671                 if (priv->rates_mask & ~IEEE80211_OFDM_RATES_MASK) {
4672                         /* Invalid fixed rate mask */
4673                         IPW_DEBUG_WX
4674                             ("invalid fixed rate mask in ipw_set_fixed_rate\n");
4675                         fr.tx_rates = 0;
4676                         break;
4677                 }
4678
4679                 fr.tx_rates >>= IEEE80211_OFDM_SHIFT_MASK_A;
4680                 break;
4681
4682         default:                /* 2.4Ghz or Mixed */
4683                 /* IEEE_B */
4684                 if (network->mode == IEEE_B) {
4685                         if (fr.tx_rates & ~IEEE80211_CCK_RATES_MASK) {
4686                                 /* Invalid fixed rate mask */
4687                                 IPW_DEBUG_WX
4688                                     ("invalid fixed rate mask in ipw_set_fixed_rate\n");
4689                                 fr.tx_rates = 0;
4690                         }
4691                         break;
4692                 }
4693
4694                 /* IEEE_G */
4695                 if (fr.tx_rates & ~(IEEE80211_CCK_RATES_MASK |
4696                                     IEEE80211_OFDM_RATES_MASK)) {
4697                         /* Invalid fixed rate mask */
4698                         IPW_DEBUG_WX
4699                             ("invalid fixed rate mask in ipw_set_fixed_rate\n");
4700                         fr.tx_rates = 0;
4701                         break;
4702                 }
4703
4704                 if (IEEE80211_OFDM_RATE_6MB_MASK & fr.tx_rates) {
4705                         mask |= (IEEE80211_OFDM_RATE_6MB_MASK >> 1);
4706                         fr.tx_rates &= ~IEEE80211_OFDM_RATE_6MB_MASK;
4707                 }
4708
4709                 if (IEEE80211_OFDM_RATE_9MB_MASK & fr.tx_rates) {
4710                         mask |= (IEEE80211_OFDM_RATE_9MB_MASK >> 1);
4711                         fr.tx_rates &= ~IEEE80211_OFDM_RATE_9MB_MASK;
4712                 }
4713
4714                 if (IEEE80211_OFDM_RATE_12MB_MASK & fr.tx_rates) {
4715                         mask |= (IEEE80211_OFDM_RATE_12MB_MASK >> 1);
4716                         fr.tx_rates &= ~IEEE80211_OFDM_RATE_12MB_MASK;
4717                 }
4718
4719                 fr.tx_rates |= mask;
4720                 break;
4721         }
4722
4723         reg = ipw_read32(priv, IPW_MEM_FIXED_OVERRIDE);
4724         ipw_write_reg32(priv, reg, *(u32 *) & fr);
4725 }
4726
4727 static void ipw_abort_scan(struct ipw_priv *priv)
4728 {
4729         int err;
4730
4731         if (priv->status & STATUS_SCAN_ABORTING) {
4732                 IPW_DEBUG_HC("Ignoring concurrent scan abort request.\n");
4733                 return;
4734         }
4735         priv->status |= STATUS_SCAN_ABORTING;
4736
4737         err = ipw_send_scan_abort(priv);
4738         if (err)
4739                 IPW_DEBUG_HC("Request to abort scan failed.\n");
4740 }
4741
4742 static int ipw_request_scan(struct ipw_priv *priv)
4743 {
4744         struct ipw_scan_request_ext scan;
4745         int channel_index = 0;
4746         int i, err, scan_type;
4747
4748         if (priv->status & STATUS_EXIT_PENDING) {
4749                 IPW_DEBUG_SCAN("Aborting scan due to device shutdown\n");
4750                 priv->status |= STATUS_SCAN_PENDING;
4751                 return 0;
4752         }
4753
4754         if (priv->status & STATUS_SCANNING) {
4755                 IPW_DEBUG_HC("Concurrent scan requested.  Aborting first.\n");
4756                 priv->status |= STATUS_SCAN_PENDING;
4757                 ipw_abort_scan(priv);
4758                 return 0;
4759         }
4760
4761         if (priv->status & STATUS_SCAN_ABORTING) {
4762                 IPW_DEBUG_HC("Scan request while abort pending.  Queuing.\n");
4763                 priv->status |= STATUS_SCAN_PENDING;
4764                 return 0;
4765         }
4766
4767         if (priv->status & STATUS_RF_KILL_MASK) {
4768                 IPW_DEBUG_HC("Aborting scan due to RF Kill activation\n");
4769                 priv->status |= STATUS_SCAN_PENDING;
4770                 return 0;
4771         }
4772
4773         memset(&scan, 0, sizeof(scan));
4774
4775         scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_SCAN] = 20;
4776         scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN] = 20;
4777         scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] = 20;
4778
4779         scan.full_scan_index = ieee80211_get_scans(priv->ieee);
4780
4781 #ifdef CONFIG_IPW_MONITOR
4782         if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
4783                 u8 band = 0, channel = priv->channel;
4784
4785                 if (is_valid_channel(IEEE_A, channel))
4786                         band = (u8) (IPW_A_MODE << 6) | 1;
4787
4788                 if (is_valid_channel(IEEE_B | IEEE_G, channel))
4789                         band = (u8) (IPW_B_MODE << 6) | 1;
4790
4791                 if (band == 0) {
4792                         band = (u8) (IPW_B_MODE << 6) | 1;
4793                         channel = 9;
4794                 }
4795
4796                 scan.channels_list[channel_index++] = band;
4797                 scan.channels_list[channel_index] = channel;
4798                 ipw_set_scan_type(&scan, channel_index,
4799                                   IPW_SCAN_PASSIVE_FULL_DWELL_SCAN);
4800
4801                 scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] = 2000;
4802         } else {
4803 #endif                          /* CONFIG_IPW_MONITOR */
4804                 /* If we are roaming, then make this a directed scan for the current
4805                  * network.  Otherwise, ensure that every other scan is a fast
4806                  * channel hop scan */
4807                 if ((priv->status & STATUS_ROAMING)
4808                     || (!(priv->status & STATUS_ASSOCIATED)
4809                         && (priv->config & CFG_STATIC_ESSID)
4810                         && (scan.full_scan_index % 2))) {
4811                         err = ipw_send_ssid(priv, priv->essid, priv->essid_len);
4812                         if (err) {
4813                                 IPW_DEBUG_HC
4814                                     ("Attempt to send SSID command failed.\n");
4815                                 return err;
4816                         }
4817
4818                         scan_type = IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN;
4819                 } else {
4820                         scan_type = IPW_SCAN_ACTIVE_BROADCAST_SCAN;
4821                 }
4822
4823                 if (priv->ieee->freq_band & IEEE80211_52GHZ_BAND) {
4824                         int start = channel_index;
4825                         for (i = 0; i < MAX_A_CHANNELS; i++) {
4826                                 if (band_a_active_channel[i] == 0)
4827                                         break;
4828                                 if ((priv->status & STATUS_ASSOCIATED) &&
4829                                     band_a_active_channel[i] == priv->channel)
4830                                         continue;
4831                                 channel_index++;
4832                                 scan.channels_list[channel_index] =
4833                                     band_a_active_channel[i];
4834                                 ipw_set_scan_type(&scan, channel_index,
4835                                                   scan_type);
4836                         }
4837
4838                         if (start != channel_index) {
4839                                 scan.channels_list[start] =
4840                                     (u8) (IPW_A_MODE << 6) | (channel_index -
4841                                                               start);
4842                                 channel_index++;
4843                         }
4844                 }
4845
4846                 if (priv->ieee->freq_band & IEEE80211_24GHZ_BAND) {
4847                         int start = channel_index;
4848                         for (i = 0; i < MAX_B_CHANNELS; i++) {
4849                                 if (band_b_active_channel[i] == 0)
4850                                         break;
4851                                 if ((priv->status & STATUS_ASSOCIATED) &&
4852                                     band_b_active_channel[i] == priv->channel)
4853                                         continue;
4854                                 channel_index++;
4855                                 scan.channels_list[channel_index] =
4856                                     band_b_active_channel[i];
4857                                 ipw_set_scan_type(&scan, channel_index,
4858                                                   scan_type);
4859                         }
4860
4861                         if (start != channel_index) {
4862                                 scan.channels_list[start] =
4863                                     (u8) (IPW_B_MODE << 6) | (channel_index -
4864                                                               start);
4865                         }
4866                 }
4867 #ifdef CONFIG_IPW_MONITOR
4868         }
4869 #endif
4870
4871         err = ipw_send_scan_request_ext(priv, &scan);
4872         if (err) {
4873                 IPW_DEBUG_HC("Sending scan command failed: %08X\n", err);
4874                 return -EIO;
4875         }
4876
4877         priv->status |= STATUS_SCANNING;
4878         priv->status &= ~STATUS_SCAN_PENDING;
4879
4880         return 0;
4881 }
4882
4883 /* Support for wpa_supplicant. Will be replaced with WEXT once
4884  * they get WPA support. */
4885 #ifdef CONFIG_IEEE80211_WPA
4886
4887 /* following definitions must match definitions in driver_ipw.c */
4888
4889 #define IPW_IOCTL_WPA_SUPPLICANT                SIOCIWFIRSTPRIV+30
4890
4891 #define IPW_CMD_SET_WPA_PARAM                   1
4892 #define IPW_CMD_SET_WPA_IE                      2
4893 #define IPW_CMD_SET_ENCRYPTION                  3
4894 #define IPW_CMD_MLME                            4
4895
4896 #define IPW_PARAM_WPA_ENABLED                   1
4897 #define IPW_PARAM_TKIP_COUNTERMEASURES          2
4898 #define IPW_PARAM_DROP_UNENCRYPTED              3
4899 #define IPW_PARAM_PRIVACY_INVOKED               4
4900 #define IPW_PARAM_AUTH_ALGS                     5
4901 #define IPW_PARAM_IEEE_802_1X                   6
4902
4903 #define IPW_MLME_STA_DEAUTH                     1
4904 #define IPW_MLME_STA_DISASSOC                   2
4905
4906 #define IPW_CRYPT_ERR_UNKNOWN_ALG               2
4907 #define IPW_CRYPT_ERR_UNKNOWN_ADDR              3
4908 #define IPW_CRYPT_ERR_CRYPT_INIT_FAILED         4
4909 #define IPW_CRYPT_ERR_KEY_SET_FAILED            5
4910 #define IPW_CRYPT_ERR_TX_KEY_SET_FAILED         6
4911 #define IPW_CRYPT_ERR_CARD_CONF_FAILED          7
4912
4913 #define IPW_CRYPT_ALG_NAME_LEN                  16
4914
4915 struct ipw_param {
4916         u32 cmd;
4917         u8 sta_addr[ETH_ALEN];
4918         union {
4919                 struct {
4920                         u8 name;
4921                         u32 value;
4922                 } wpa_param;
4923                 struct {
4924                         u32 len;
4925                         u8 *data;
4926                 } wpa_ie;
4927                 struct {
4928                         int command;
4929                         int reason_code;
4930                 } mlme;
4931                 struct {
4932                         u8 alg[IPW_CRYPT_ALG_NAME_LEN];
4933                         u8 set_tx;
4934                         u32 err;
4935                         u8 idx;
4936                         u8 seq[8];      /* sequence counter (set: RX, get: TX) */
4937                         u16 key_len;
4938                         u8 key[0];
4939                 } crypt;
4940
4941         } u;
4942 };
4943
4944 /* end of driver_ipw.c code */
4945
4946 static int ipw_wpa_enable(struct ipw_priv *priv, int value)
4947 {
4948         struct ieee80211_device *ieee = priv->ieee;
4949         struct ieee80211_security sec = {
4950                 .flags = SEC_LEVEL | SEC_ENABLED,
4951         };
4952         int ret = 0;
4953
4954         ieee->wpa_enabled = value;
4955
4956         if (value) {
4957                 sec.level = SEC_LEVEL_3;
4958                 sec.enabled = 1;
4959         } else {
4960                 sec.level = SEC_LEVEL_0;
4961                 sec.enabled = 0;
4962         }
4963
4964         if (ieee->set_security)
4965                 ieee->set_security(ieee->dev, &sec);
4966         else
4967                 ret = -EOPNOTSUPP;
4968
4969         return ret;
4970 }
4971
4972 #define AUTH_ALG_OPEN_SYSTEM                    0x1
4973 #define AUTH_ALG_SHARED_KEY                     0x2
4974
4975 static int ipw_wpa_set_auth_algs(struct ipw_priv *priv, int value)
4976 {
4977         struct ieee80211_device *ieee = priv->ieee;
4978         struct ieee80211_security sec = {
4979                 .flags = SEC_AUTH_MODE,
4980         };
4981         int ret = 0;
4982
4983         if (value & AUTH_ALG_SHARED_KEY) {
4984                 sec.auth_mode = WLAN_AUTH_SHARED_KEY;
4985                 ieee->open_wep = 0;
4986         } else {
4987                 sec.auth_mode = WLAN_AUTH_OPEN;
4988                 ieee->open_wep = 1;
4989         }
4990
4991         if (ieee->set_security)
4992                 ieee->set_security(ieee->dev, &sec);
4993         else
4994                 ret = -EOPNOTSUPP;
4995
4996         return ret;
4997 }
4998
4999 static int ipw_wpa_set_param(struct net_device *dev, u8 name, u32 value)
5000 {
5001         struct ipw_priv *priv = ieee80211_priv(dev);
5002         int ret = 0;
5003
5004         switch (name) {
5005         case IPW_PARAM_WPA_ENABLED:
5006                 ret = ipw_wpa_enable(priv, value);
5007                 break;
5008
5009         case IPW_PARAM_TKIP_COUNTERMEASURES:
5010                 priv->ieee->tkip_countermeasures = value;
5011                 break;
5012
5013         case IPW_PARAM_DROP_UNENCRYPTED:
5014                 priv->ieee->drop_unencrypted = value;
5015                 break;
5016
5017         case IPW_PARAM_PRIVACY_INVOKED:
5018                 priv->ieee->privacy_invoked = value;
5019                 break;
5020
5021         case IPW_PARAM_AUTH_ALGS:
5022                 ret = ipw_wpa_set_auth_algs(priv, value);
5023                 break;
5024
5025         case IPW_PARAM_IEEE_802_1X:
5026                 priv->ieee->ieee802_1x = value;
5027                 break;
5028
5029         default:
5030                 IPW_ERROR("%s: Unknown WPA param: %d\n", dev->name, name);
5031                 ret = -EOPNOTSUPP;
5032         }
5033
5034         return ret;
5035 }
5036
5037 static int ipw_wpa_mlme(struct net_device *dev, int command, int reason)
5038 {
5039         struct ipw_priv *priv = ieee80211_priv(dev);
5040         int ret = 0;
5041
5042         switch (command) {
5043         case IPW_MLME_STA_DEAUTH:
5044                 // silently ignore
5045                 break;
5046
5047         case IPW_MLME_STA_DISASSOC:
5048                 ipw_disassociate(priv);
5049                 break;
5050
5051         default:
5052                 IPW_ERROR("%s: Unknown MLME request: %d\n", dev->name, command);
5053                 ret = -EOPNOTSUPP;
5054         }
5055
5056         return ret;
5057 }
5058
5059 static int ipw_set_rsn_capa(struct ipw_priv *priv,
5060                             char *capabilities, int length)
5061 {
5062         struct host_cmd cmd = {
5063                 .cmd = IPW_CMD_RSN_CAPABILITIES,
5064                 .len = length,
5065         };
5066
5067         IPW_DEBUG_HC("HOST_CMD_RSN_CAPABILITIES\n");
5068
5069         memcpy(&cmd.param, capabilities, length);
5070         if (ipw_send_cmd(priv, &cmd)) {
5071                 IPW_ERROR("failed to send HOST_CMD_RSN_CAPABILITIES command\n");
5072                 return -1;
5073         }
5074         return 0;
5075 }
5076
5077 void ipw_wpa_assoc_frame(struct ipw_priv *priv, char *wpa_ie, int wpa_ie_len)
5078 {
5079         /* make sure WPA is enabled */
5080         ipw_wpa_enable(priv, 1);
5081
5082         if (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING))
5083                 ipw_disassociate(priv);
5084 }
5085
5086 static int ipw_wpa_set_wpa_ie(struct net_device *dev,
5087                               struct ipw_param *param, int plen)
5088 {
5089         struct ipw_priv *priv = ieee80211_priv(dev);
5090         struct ieee80211_device *ieee = priv->ieee;
5091         u8 *buf;
5092
5093         if (!ieee->wpa_enabled)
5094                 return -EOPNOTSUPP;
5095
5096         if (param->u.wpa_ie.len > MAX_WPA_IE_LEN ||
5097             (param->u.wpa_ie.len && param->u.wpa_ie.data == NULL))
5098                 return -EINVAL;
5099
5100         if (param->u.wpa_ie.len) {
5101                 buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL);
5102                 if (buf == NULL)
5103                         return -ENOMEM;
5104
5105                 memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len);
5106                 kfree(ieee->wpa_ie);
5107                 ieee->wpa_ie = buf;
5108                 ieee->wpa_ie_len = param->u.wpa_ie.len;
5109         } else {
5110                 kfree(ieee->wpa_ie);
5111                 ieee->wpa_ie = NULL;
5112                 ieee->wpa_ie_len = 0;
5113         }
5114
5115         ipw_wpa_assoc_frame(priv, ieee->wpa_ie, ieee->wpa_ie_len);
5116         return 0;
5117 }
5118
5119 /* implementation borrowed from hostap driver */
5120
5121 static int ipw_wpa_set_encryption(struct net_device *dev,
5122                                   struct ipw_param *param, int param_len)
5123 {
5124         int ret = 0;
5125         struct ipw_priv *priv = ieee80211_priv(dev);
5126         struct ieee80211_device *ieee = priv->ieee;
5127         struct ieee80211_crypto_ops *ops;
5128         struct ieee80211_crypt_data **crypt;
5129
5130         struct ieee80211_security sec = {
5131                 .flags = 0,
5132         };
5133
5134         param->u.crypt.err = 0;
5135         param->u.crypt.alg[IPW_CRYPT_ALG_NAME_LEN - 1] = '\0';
5136
5137         if (param_len !=
5138             (int)((char *)param->u.crypt.key - (char *)param) +
5139             param->u.crypt.key_len) {
5140                 IPW_DEBUG_INFO("Len mismatch %d, %d\n", param_len,
5141                                param->u.crypt.key_len);
5142                 return -EINVAL;
5143         }
5144         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
5145             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
5146             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
5147                 if (param->u.crypt.idx >= WEP_KEYS)
5148                         return -EINVAL;
5149                 crypt = &ieee->crypt[param->u.crypt.idx];
5150         } else {
5151                 return -EINVAL;
5152         }
5153
5154         if (strcmp(param->u.crypt.alg, "none") == 0) {
5155                 if (crypt) {
5156                         sec.enabled = 0;
5157                         sec.level = SEC_LEVEL_0;
5158                         sec.flags |= SEC_ENABLED | SEC_LEVEL;
5159                         ieee80211_crypt_delayed_deinit(ieee, crypt);
5160                 }
5161                 goto done;
5162         }
5163         sec.enabled = 1;
5164         sec.flags |= SEC_ENABLED;
5165
5166         ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
5167         if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0) {
5168                 request_module("ieee80211_crypt_wep");
5169                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
5170         } else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0) {
5171                 request_module("ieee80211_crypt_tkip");
5172                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
5173         } else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0) {
5174                 request_module("ieee80211_crypt_ccmp");
5175                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
5176         }
5177         if (ops == NULL) {
5178                 IPW_DEBUG_INFO("%s: unknown crypto alg '%s'\n",
5179                                dev->name, param->u.crypt.alg);
5180                 param->u.crypt.err = IPW_CRYPT_ERR_UNKNOWN_ALG;
5181                 ret = -EINVAL;
5182                 goto done;
5183         }
5184
5185         if (*crypt == NULL || (*crypt)->ops != ops) {
5186                 struct ieee80211_crypt_data *new_crypt;
5187
5188                 ieee80211_crypt_delayed_deinit(ieee, crypt);
5189
5190                 new_crypt = (struct ieee80211_crypt_data *)
5191                     kmalloc(sizeof(*new_crypt), GFP_KERNEL);
5192                 if (new_crypt == NULL) {
5193                         ret = -ENOMEM;
5194                         goto done;
5195                 }
5196                 memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
5197                 new_crypt->ops = ops;
5198                 if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
5199                         new_crypt->priv =
5200                             new_crypt->ops->init(param->u.crypt.idx);
5201
5202                 if (new_crypt->priv == NULL) {
5203                         kfree(new_crypt);
5204                         param->u.crypt.err = IPW_CRYPT_ERR_CRYPT_INIT_FAILED;
5205                         ret = -EINVAL;
5206                         goto done;
5207                 }
5208
5209                 *crypt = new_crypt;
5210         }
5211
5212         if (param->u.crypt.key_len > 0 && (*crypt)->ops->set_key &&
5213             (*crypt)->ops->set_key(param->u.crypt.key,
5214                                    param->u.crypt.key_len, param->u.crypt.seq,
5215                                    (*crypt)->priv) < 0) {
5216                 IPW_DEBUG_INFO("%s: key setting failed\n", dev->name);
5217                 param->u.crypt.err = IPW_CRYPT_ERR_KEY_SET_FAILED;
5218                 ret = -EINVAL;
5219                 goto done;
5220         }
5221
5222         if (param->u.crypt.set_tx) {
5223                 ieee->tx_keyidx = param->u.crypt.idx;
5224                 sec.active_key = param->u.crypt.idx;
5225                 sec.flags |= SEC_ACTIVE_KEY;
5226         }
5227
5228         if (ops->name != NULL) {
5229                 if (strcmp(ops->name, "WEP") == 0) {
5230                         memcpy(sec.keys[param->u.crypt.idx],
5231                                param->u.crypt.key, param->u.crypt.key_len);
5232                         sec.key_sizes[param->u.crypt.idx] =
5233                             param->u.crypt.key_len;
5234                         sec.flags |= (1 << param->u.crypt.idx);
5235                         sec.flags |= SEC_LEVEL;
5236                         sec.level = SEC_LEVEL_1;
5237                 } else if (strcmp(ops->name, "TKIP") == 0) {
5238                         sec.flags |= SEC_LEVEL;
5239                         sec.level = SEC_LEVEL_2;
5240                 } else if (strcmp(ops->name, "CCMP") == 0) {
5241                         sec.flags |= SEC_LEVEL;
5242                         sec.level = SEC_LEVEL_3;
5243                 }
5244         }
5245       done:
5246         if (ieee->set_security)
5247                 ieee->set_security(ieee->dev, &sec);
5248
5249         /* Do not reset port if card is in Managed mode since resetting will
5250          * generate new IEEE 802.11 authentication which may end up in looping
5251          * with IEEE 802.1X.  If your hardware requires a reset after WEP
5252          * configuration (for example... Prism2), implement the reset_port in
5253          * the callbacks structures used to initialize the 802.11 stack. */
5254         if (ieee->reset_on_keychange &&
5255             ieee->iw_mode != IW_MODE_INFRA &&
5256             ieee->reset_port && ieee->reset_port(dev)) {
5257                 IPW_DEBUG_INFO("%s: reset_port failed\n", dev->name);
5258                 param->u.crypt.err = IPW_CRYPT_ERR_CARD_CONF_FAILED;
5259                 return -EINVAL;
5260         }
5261
5262         return ret;
5263 }
5264
5265 static int ipw_wpa_supplicant(struct net_device *dev, struct iw_point *p)
5266 {
5267         struct ipw_param *param;
5268         int ret = 0;
5269
5270         IPW_DEBUG_INFO("wpa_supplicant: len=%d\n", p->length);
5271
5272         if (p->length < sizeof(struct ipw_param) || !p->pointer)
5273                 return -EINVAL;
5274
5275         param = (struct ipw_param *)kmalloc(p->length, GFP_KERNEL);
5276         if (param == NULL)
5277                 return -ENOMEM;
5278
5279         if (copy_from_user(param, p->pointer, p->length)) {
5280                 kfree(param);
5281                 return -EFAULT;
5282         }
5283
5284         switch (param->cmd) {
5285
5286         case IPW_CMD_SET_WPA_PARAM:
5287                 ret = ipw_wpa_set_param(dev, param->u.wpa_param.name,
5288                                         param->u.wpa_param.value);
5289                 break;
5290
5291         case IPW_CMD_SET_WPA_IE:
5292                 ret = ipw_wpa_set_wpa_ie(dev, param, p->length);
5293                 break;
5294
5295         case IPW_CMD_SET_ENCRYPTION:
5296                 ret = ipw_wpa_set_encryption(dev, param, p->length);
5297                 break;
5298
5299         case IPW_CMD_MLME:
5300                 ret = ipw_wpa_mlme(dev, param->u.mlme.command,
5301                                    param->u.mlme.reason_code);
5302                 break;
5303
5304         default:
5305                 IPW_ERROR("%s: Unknown WPA supplicant request: %d\n",
5306                           dev->name, param->cmd);
5307                 ret = -EOPNOTSUPP;
5308         }
5309
5310         if (ret == 0 && copy_to_user(p->pointer, param, p->length))
5311                 ret = -EFAULT;
5312
5313         kfree(param);
5314         return ret;
5315 }
5316 #endif                          /* CONFIG_IEEE80211_WPA */
5317
5318 static int ipw_associate_network(struct ipw_priv *priv,
5319                                  struct ieee80211_network *network,
5320                                  struct ipw_supported_rates *rates, int roaming)
5321 {
5322         int err;
5323
5324         if (priv->config & CFG_FIXED_RATE)
5325                 ipw_set_fixed_rate(priv, network);
5326
5327         if (!(priv->config & CFG_STATIC_ESSID)) {
5328                 priv->essid_len = min(network->ssid_len,
5329                                       (u8) IW_ESSID_MAX_SIZE);
5330                 memcpy(priv->essid, network->ssid, priv->essid_len);
5331         }
5332
5333         network->last_associate = jiffies;
5334
5335         memset(&priv->assoc_request, 0, sizeof(priv->assoc_request));
5336         priv->assoc_request.channel = network->channel;
5337         if ((priv->capability & CAP_PRIVACY_ON) &&
5338             (priv->capability & CAP_SHARED_KEY)) {
5339                 priv->assoc_request.auth_type = AUTH_SHARED_KEY;
5340                 priv->assoc_request.auth_key = priv->sec.active_key;
5341         } else {
5342                 priv->assoc_request.auth_type = AUTH_OPEN;
5343                 priv->assoc_request.auth_key = 0;
5344         }
5345
5346         if (priv->capability & CAP_PRIVACY_ON)
5347                 ipw_send_wep_keys(priv);
5348
5349 #ifdef CONFIG_IEEE80211_WPA
5350         if (priv->ieee->wpa_enabled) {
5351                 priv->assoc_request.policy_support = 0x02;      /* RSN active */
5352                 ipw_set_rsn_capa(priv, priv->ieee->wpa_ie,
5353                                  priv->ieee->wpa_ie_len);
5354         }
5355 #endif
5356
5357         /*
5358          * It is valid for our ieee device to support multiple modes, but
5359          * when it comes to associating to a given network we have to choose
5360          * just one mode.
5361          */
5362         if (network->mode & priv->ieee->mode & IEEE_A)
5363                 priv->assoc_request.ieee_mode = IPW_A_MODE;
5364         else if (network->mode & priv->ieee->mode & IEEE_G)
5365                 priv->assoc_request.ieee_mode = IPW_G_MODE;
5366         else if (network->mode & priv->ieee->mode & IEEE_B)
5367                 priv->assoc_request.ieee_mode = IPW_B_MODE;
5368
5369         priv->assoc_request.capability = network->capability;
5370         if ((network->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
5371             && !(priv->config & CFG_PREAMBLE_LONG)) {
5372                 priv->assoc_request.preamble_length = DCT_FLAG_SHORT_PREAMBLE;
5373         } else {
5374                 priv->assoc_request.preamble_length = DCT_FLAG_LONG_PREAMBLE;
5375
5376                 /* Clear the short preamble if we won't be supporting it */
5377                 priv->assoc_request.capability &=
5378                     ~WLAN_CAPABILITY_SHORT_PREAMBLE;
5379         }
5380
5381         IPW_DEBUG_ASSOC("%sssocation attempt: '%s', channel %d, "
5382                         "802.11%c [%d], %s[:%s], enc=%s%s%s%c%c\n",
5383                         roaming ? "Rea" : "A",
5384                         escape_essid(priv->essid, priv->essid_len),
5385                         network->channel,
5386                         ipw_modes[priv->assoc_request.ieee_mode],
5387                         rates->num_rates,
5388                         (priv->assoc_request.preamble_length ==
5389                          DCT_FLAG_LONG_PREAMBLE) ? "long" : "short",
5390                         network->capability &
5391                         WLAN_CAPABILITY_SHORT_PREAMBLE ? "short" : "long",
5392                         priv->capability & CAP_PRIVACY_ON ? "on " : "off",
5393                         priv->capability & CAP_PRIVACY_ON ?
5394                         (priv->capability & CAP_SHARED_KEY ? "(shared)" :
5395                          "(open)") : "",
5396                         priv->capability & CAP_PRIVACY_ON ? " key=" : "",
5397                         priv->capability & CAP_PRIVACY_ON ?
5398                         '1' + priv->sec.active_key : '.',
5399                         priv->capability & CAP_PRIVACY_ON ? '.' : ' ');
5400
5401         priv->assoc_request.beacon_interval = network->beacon_interval;
5402         if ((priv->ieee->iw_mode == IW_MODE_ADHOC) &&
5403             (network->time_stamp[0] == 0) && (network->time_stamp[1] == 0)) {
5404                 priv->assoc_request.assoc_type = HC_IBSS_START;
5405                 priv->assoc_request.assoc_tsf_msw = 0;
5406                 priv->assoc_request.assoc_tsf_lsw = 0;
5407         } else {
5408                 if (unlikely(roaming))
5409                         priv->assoc_request.assoc_type = HC_REASSOCIATE;
5410                 else
5411                         priv->assoc_request.assoc_type = HC_ASSOCIATE;
5412                 priv->assoc_request.assoc_tsf_msw = network->time_stamp[1];
5413                 priv->assoc_request.assoc_tsf_lsw = network->time_stamp[0];
5414         }
5415
5416         memcpy(&priv->assoc_request.bssid, network->bssid, ETH_ALEN);
5417
5418         if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
5419                 memset(&priv->assoc_request.dest, 0xFF, ETH_ALEN);
5420                 priv->assoc_request.atim_window = network->atim_window;
5421         } else {
5422                 memcpy(&priv->assoc_request.dest, network->bssid, ETH_ALEN);
5423                 priv->assoc_request.atim_window = 0;
5424         }
5425
5426         priv->assoc_request.listen_interval = network->listen_interval;
5427
5428         err = ipw_send_ssid(priv, priv->essid, priv->essid_len);
5429         if (err) {
5430                 IPW_DEBUG_HC("Attempt to send SSID command failed.\n");
5431                 return err;
5432         }
5433
5434         rates->ieee_mode = priv->assoc_request.ieee_mode;
5435         rates->purpose = IPW_RATE_CONNECT;
5436         ipw_send_supported_rates(priv, rates);
5437
5438         if (priv->assoc_request.ieee_mode == IPW_G_MODE)
5439                 priv->sys_config.dot11g_auto_detection = 1;
5440         else
5441                 priv->sys_config.dot11g_auto_detection = 0;
5442         err = ipw_send_system_config(priv, &priv->sys_config);
5443         if (err) {
5444                 IPW_DEBUG_HC("Attempt to send sys config command failed.\n");
5445                 return err;
5446         }
5447
5448         IPW_DEBUG_ASSOC("Association sensitivity: %d\n", network->stats.rssi);
5449         err = ipw_set_sensitivity(priv, network->stats.rssi + IPW_RSSI_TO_DBM);
5450         if (err) {
5451                 IPW_DEBUG_HC("Attempt to send associate command failed.\n");
5452                 return err;
5453         }
5454
5455         /*
5456          * If preemption is enabled, it is possible for the association
5457          * to complete before we return from ipw_send_associate.  Therefore
5458          * we have to be sure and update our priviate data first.
5459          */
5460         priv->channel = network->channel;
5461         memcpy(priv->bssid, network->bssid, ETH_ALEN);
5462         priv->status |= STATUS_ASSOCIATING;
5463         priv->status &= ~STATUS_SECURITY_UPDATED;
5464
5465         priv->assoc_network = network;
5466
5467         err = ipw_send_associate(priv, &priv->assoc_request);
5468         if (err) {
5469                 IPW_DEBUG_HC("Attempt to send associate command failed.\n");
5470                 return err;
5471         }
5472
5473         IPW_DEBUG(IPW_DL_STATE, "associating: '%s' " MAC_FMT " \n",
5474                   escape_essid(priv->essid, priv->essid_len),
5475                   MAC_ARG(priv->bssid));
5476
5477         return 0;
5478 }
5479
5480 static void ipw_roam(void *data)
5481 {
5482         struct ipw_priv *priv = data;
5483         struct ieee80211_network *network = NULL;
5484         struct ipw_network_match match = {
5485                 .network = priv->assoc_network
5486         };
5487
5488         /* The roaming process is as follows:
5489          *
5490          * 1.  Missed beacon threshold triggers the roaming process by
5491          *     setting the status ROAM bit and requesting a scan.
5492          * 2.  When the scan completes, it schedules the ROAM work
5493          * 3.  The ROAM work looks at all of the known networks for one that
5494          *     is a better network than the currently associated.  If none
5495          *     found, the ROAM process is over (ROAM bit cleared)
5496          * 4.  If a better network is found, a disassociation request is
5497          *     sent.
5498          * 5.  When the disassociation completes, the roam work is again
5499          *     scheduled.  The second time through, the driver is no longer
5500          *     associated, and the newly selected network is sent an
5501          *     association request.
5502          * 6.  At this point ,the roaming process is complete and the ROAM
5503          *     status bit is cleared.
5504          */
5505
5506         /* If we are no longer associated, and the roaming bit is no longer
5507          * set, then we are not actively roaming, so just return */
5508         if (!(priv->status & (STATUS_ASSOCIATED | STATUS_ROAMING)))
5509                 return;
5510
5511         if (priv->status & STATUS_ASSOCIATED) {
5512                 /* First pass through ROAM process -- look for a better
5513                  * network */
5514                 u8 rssi = priv->assoc_network->stats.rssi;
5515                 priv->assoc_network->stats.rssi = -128;
5516                 list_for_each_entry(network, &priv->ieee->network_list, list) {
5517                         if (network != priv->assoc_network)
5518                                 ipw_best_network(priv, &match, network, 1);
5519                 }
5520                 priv->assoc_network->stats.rssi = rssi;
5521
5522                 if (match.network == priv->assoc_network) {
5523                         IPW_DEBUG_ASSOC("No better APs in this network to "
5524                                         "roam to.\n");
5525                         priv->status &= ~STATUS_ROAMING;
5526                         ipw_debug_config(priv);
5527                         return;
5528                 }
5529
5530                 ipw_send_disassociate(priv, 1);
5531                 priv->assoc_network = match.network;
5532
5533                 return;
5534         }
5535
5536         /* Second pass through ROAM process -- request association */
5537         ipw_compatible_rates(priv, priv->assoc_network, &match.rates);
5538         ipw_associate_network(priv, priv->assoc_network, &match.rates, 1);
5539         priv->status &= ~STATUS_ROAMING;
5540 }
5541
5542 static void ipw_associate(void *data)
5543 {
5544         struct ipw_priv *priv = data;
5545
5546         struct ieee80211_network *network = NULL;
5547         struct ipw_network_match match = {
5548                 .network = NULL
5549         };
5550         struct ipw_supported_rates *rates;
5551         struct list_head *element;
5552
5553         if (!(priv->config & CFG_ASSOCIATE) &&
5554             !(priv->config & (CFG_STATIC_ESSID |
5555                               CFG_STATIC_CHANNEL | CFG_STATIC_BSSID))) {
5556                 IPW_DEBUG_ASSOC("Not attempting association (associate=0)\n");
5557                 return;
5558         }
5559
5560         list_for_each_entry(network, &priv->ieee->network_list, list)
5561             ipw_best_network(priv, &match, network, 0);
5562
5563         network = match.network;
5564         rates = &match.rates;
5565
5566         if (network == NULL &&
5567             priv->ieee->iw_mode == IW_MODE_ADHOC &&
5568             priv->config & CFG_ADHOC_CREATE &&
5569             priv->config & CFG_STATIC_ESSID &&
5570             !list_empty(&priv->ieee->network_free_list)) {
5571                 element = priv->ieee->network_free_list.next;
5572                 network = list_entry(element, struct ieee80211_network, list);
5573                 ipw_adhoc_create(priv, network);
5574                 rates = &priv->rates;
5575                 list_del(element);
5576                 list_add_tail(&network->list, &priv->ieee->network_list);
5577         }
5578
5579         /* If we reached the end of the list, then we don't have any valid
5580          * matching APs */
5581         if (!network) {
5582                 ipw_debug_config(priv);
5583
5584                 queue_delayed_work(priv->workqueue, &priv->request_scan,
5585                                    SCAN_INTERVAL);
5586
5587                 return;
5588         }
5589
5590         ipw_associate_network(priv, network, rates, 0);
5591 }
5592
5593 static inline void ipw_handle_data_packet(struct ipw_priv *priv,
5594                                           struct ipw_rx_mem_buffer *rxb,
5595                                           struct ieee80211_rx_stats *stats)
5596 {
5597         struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)rxb->skb->data;
5598
5599         /* We received data from the HW, so stop the watchdog */
5600         priv->net_dev->trans_start = jiffies;
5601
5602         /* We only process data packets if the
5603          * interface is open */
5604         if (unlikely((pkt->u.frame.length + IPW_RX_FRAME_SIZE) >
5605                      skb_tailroom(rxb->skb))) {
5606                 priv->ieee->stats.rx_errors++;
5607                 priv->wstats.discard.misc++;
5608                 IPW_DEBUG_DROP("Corruption detected! Oh no!\n");
5609                 return;
5610         } else if (unlikely(!netif_running(priv->net_dev))) {
5611                 priv->ieee->stats.rx_dropped++;
5612                 priv->wstats.discard.misc++;
5613                 IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
5614                 return;
5615         }
5616
5617         /* Advance skb->data to the start of the actual payload */
5618         skb_reserve(rxb->skb, offsetof(struct ipw_rx_packet, u.frame.data));
5619
5620         /* Set the size of the skb to the size of the frame */
5621         skb_put(rxb->skb, pkt->u.frame.length);
5622
5623         IPW_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
5624
5625         if (!ieee80211_rx(priv->ieee, rxb->skb, stats))
5626                 priv->ieee->stats.rx_errors++;
5627         else                    /* ieee80211_rx succeeded, so it now owns the SKB */
5628                 rxb->skb = NULL;
5629 }
5630
5631 static inline int is_network_packet(struct ipw_priv *priv,
5632                                     struct ieee80211_hdr_4addr *header)
5633 {
5634         /* Filter incoming packets to determine if they are targetted toward
5635          * this network, discarding packets coming from ourselves */
5636         switch (priv->ieee->iw_mode) {
5637         case IW_MODE_ADHOC:
5638                 if (is_broadcast_ether_addr(header->addr1) ||
5639                     is_multicast_ether_addr(header->addr1))
5640                         return !memcmp(header->addr3, priv->bssid, ETH_ALEN);
5641                 else
5642                         return memcmp(header->addr1, priv->net_dev->dev_addr,
5643                                       ETH_ALEN);
5644                 break;
5645         case IW_MODE_INFRA:
5646                 if (is_broadcast_ether_addr(header->addr3) ||
5647                     is_multicast_ether_addr(header->addr3))
5648                         return !memcmp(header->addr1, priv->bssid, ETH_ALEN);
5649                 else
5650                         return memcmp(header->addr3, priv->net_dev->dev_addr,
5651                                       ETH_ALEN);
5652                 break;
5653         }
5654         return 1;
5655 }
5656
5657 /*
5658  * Main entry function for recieving a packet with 80211 headers.  This
5659  * should be called when ever the FW has notified us that there is a new
5660  * skb in the recieve queue.
5661  */
5662 static void ipw_rx(struct ipw_priv *priv)
5663 {
5664         struct ipw_rx_mem_buffer *rxb;
5665         struct ipw_rx_packet *pkt;
5666         struct ieee80211_hdr_4addr *header;
5667         u32 r, w, i;
5668         u8 network_packet;
5669
5670         r = ipw_read32(priv, CX2_RX_READ_INDEX);
5671         w = ipw_read32(priv, CX2_RX_WRITE_INDEX);
5672         i = (priv->rxq->processed + 1) % RX_QUEUE_SIZE;
5673
5674         while (i != r) {
5675                 rxb = priv->rxq->queue[i];
5676 #ifdef CONFIG_IPW_DEBUG
5677                 if (unlikely(rxb == NULL)) {
5678                         printk(KERN_CRIT "Queue not allocated!\n");
5679                         break;
5680                 }
5681 #endif
5682                 priv->rxq->queue[i] = NULL;
5683
5684                 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
5685                                             CX2_RX_BUF_SIZE,
5686                                             PCI_DMA_FROMDEVICE);
5687
5688                 pkt = (struct ipw_rx_packet *)rxb->skb->data;
5689                 IPW_DEBUG_RX("Packet: type=%02X seq=%02X bits=%02X\n",
5690                              pkt->header.message_type,
5691                              pkt->header.rx_seq_num, pkt->header.control_bits);
5692
5693                 switch (pkt->header.message_type) {
5694                 case RX_FRAME_TYPE:     /* 802.11 frame */  {
5695                                 struct ieee80211_rx_stats stats = {
5696                                         .rssi = pkt->u.frame.rssi_dbm -
5697                                             IPW_RSSI_TO_DBM,
5698                                         .signal = pkt->u.frame.signal,
5699                                         .rate = pkt->u.frame.rate,
5700                                         .mac_time = jiffies,
5701                                         .received_channel =
5702                                             pkt->u.frame.received_channel,
5703                                         .freq =
5704                                             (pkt->u.frame.
5705                                              control & (1 << 0)) ?
5706                                             IEEE80211_24GHZ_BAND :
5707                                             IEEE80211_52GHZ_BAND,
5708                                         .len = pkt->u.frame.length,
5709                                 };
5710
5711                                 if (stats.rssi != 0)
5712                                         stats.mask |= IEEE80211_STATMASK_RSSI;
5713                                 if (stats.signal != 0)
5714                                         stats.mask |= IEEE80211_STATMASK_SIGNAL;
5715                                 if (stats.rate != 0)
5716                                         stats.mask |= IEEE80211_STATMASK_RATE;
5717
5718                                 priv->rx_packets++;
5719
5720 #ifdef CONFIG_IPW_MONITOR
5721                                 if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
5722                                         ipw_handle_data_packet(priv, rxb,
5723                                                                &stats);
5724                                         break;
5725                                 }
5726 #endif
5727
5728                                 header =
5729                                     (struct ieee80211_hdr_4addr *)(rxb->skb->
5730                                                                    data +
5731                                                                    IPW_RX_FRAME_SIZE);
5732                                 /* TODO: Check Ad-Hoc dest/source and make sure
5733                                  * that we are actually parsing these packets
5734                                  * correctly -- we should probably use the
5735                                  * frame control of the packet and disregard
5736                                  * the current iw_mode */
5737
5738                                 network_packet =
5739                                     is_network_packet(priv, header);
5740                                 if (network_packet && priv->assoc_network) {
5741                                         priv->assoc_network->stats.rssi =
5742                                             stats.rssi;
5743                                         average_add(&priv->average_rssi,
5744                                                     stats.rssi);
5745                                         priv->last_rx_rssi = stats.rssi;
5746                                 }
5747
5748                                 IPW_DEBUG_RX("Frame: len=%u\n",
5749                                              pkt->u.frame.length);
5750
5751                                 if (pkt->u.frame.length < frame_hdr_len(header)) {
5752                                         IPW_DEBUG_DROP
5753                                             ("Received packet is too small. "
5754                                              "Dropping.\n");
5755                                         priv->ieee->stats.rx_errors++;
5756                                         priv->wstats.discard.misc++;
5757                                         break;
5758                                 }
5759
5760                                 switch (WLAN_FC_GET_TYPE(header->frame_ctl)) {
5761                                 case IEEE80211_FTYPE_MGMT:
5762                                         ieee80211_rx_mgt(priv->ieee, header,
5763                                                          &stats);
5764                                         if (priv->ieee->iw_mode == IW_MODE_ADHOC
5765                                             &&
5766                                             ((WLAN_FC_GET_STYPE
5767                                               (header->frame_ctl) ==
5768                                               IEEE80211_STYPE_PROBE_RESP)
5769                                              ||
5770                                              (WLAN_FC_GET_STYPE
5771                                               (header->frame_ctl) ==
5772                                               IEEE80211_STYPE_BEACON))
5773                                             && !memcmp(header->addr3,
5774                                                        priv->bssid, ETH_ALEN))
5775                                                 ipw_add_station(priv,
5776                                                                 header->addr2);
5777                                         break;
5778
5779                                 case IEEE80211_FTYPE_CTL:
5780                                         break;
5781
5782                                 case IEEE80211_FTYPE_DATA:
5783                                         if (network_packet)
5784                                                 ipw_handle_data_packet(priv,
5785                                                                        rxb,
5786                                                                        &stats);
5787                                         else
5788                                                 IPW_DEBUG_DROP("Dropping: "
5789                                                                MAC_FMT ", "
5790                                                                MAC_FMT ", "
5791                                                                MAC_FMT "\n",
5792                                                                MAC_ARG(header->
5793                                                                        addr1),
5794                                                                MAC_ARG(header->
5795                                                                        addr2),
5796                                                                MAC_ARG(header->
5797                                                                        addr3));
5798                                         break;
5799                                 }
5800                                 break;
5801                         }
5802
5803                 case RX_HOST_NOTIFICATION_TYPE:{
5804                                 IPW_DEBUG_RX
5805                                     ("Notification: subtype=%02X flags=%02X size=%d\n",
5806                                      pkt->u.notification.subtype,
5807                                      pkt->u.notification.flags,
5808                                      pkt->u.notification.size);
5809                                 ipw_rx_notification(priv, &pkt->u.notification);
5810                                 break;
5811                         }
5812
5813                 default:
5814                         IPW_DEBUG_RX("Bad Rx packet of type %d\n",
5815                                      pkt->header.message_type);
5816                         break;
5817                 }
5818
5819                 /* For now we just don't re-use anything.  We can tweak this
5820                  * later to try and re-use notification packets and SKBs that
5821                  * fail to Rx correctly */
5822                 if (rxb->skb != NULL) {
5823                         dev_kfree_skb_any(rxb->skb);
5824                         rxb->skb = NULL;
5825                 }
5826
5827                 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
5828                                  CX2_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
5829                 list_add_tail(&rxb->list, &priv->rxq->rx_used);
5830
5831                 i = (i + 1) % RX_QUEUE_SIZE;
5832         }
5833
5834         /* Backtrack one entry */
5835         priv->rxq->processed = (i ? i : RX_QUEUE_SIZE) - 1;
5836
5837         ipw_rx_queue_restock(priv);
5838 }
5839
5840 /*
5841  * This file defines the Wireless Extension handlers.  It does not
5842  * define any methods of hardware manipulation and relies on the
5843  * functions defined in ipw_main to provide the HW interaction.
5844  *
5845  * The exception to this is the use of the ipw_get_ordinal()
5846  * function used to poll the hardware vs. making unecessary calls.
5847  *
5848  */
5849
5850 static int ipw_wx_get_name(struct net_device *dev,
5851                            struct iw_request_info *info,
5852                            union iwreq_data *wrqu, char *extra)
5853 {
5854         struct ipw_priv *priv = ieee80211_priv(dev);
5855         if (!(priv->status & STATUS_ASSOCIATED))
5856                 strcpy(wrqu->name, "unassociated");
5857         else
5858                 snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11%c",
5859                          ipw_modes[priv->assoc_request.ieee_mode]);
5860         IPW_DEBUG_WX("Name: %s\n", wrqu->name);
5861         return 0;
5862 }
5863
5864 static int ipw_set_channel(struct ipw_priv *priv, u8 channel)
5865 {
5866         if (channel == 0) {
5867                 IPW_DEBUG_INFO("Setting channel to ANY (0)\n");
5868                 priv->config &= ~CFG_STATIC_CHANNEL;
5869                 if (!(priv->status & (STATUS_SCANNING | STATUS_ASSOCIATED |
5870                                       STATUS_ASSOCIATING))) {
5871                         IPW_DEBUG_ASSOC("Attempting to associate with new "
5872                                         "parameters.\n");
5873                         ipw_associate(priv);
5874                 }
5875
5876                 return 0;
5877         }
5878
5879         priv->config |= CFG_STATIC_CHANNEL;
5880
5881         if (priv->channel == channel) {
5882                 IPW_DEBUG_INFO("Request to set channel to current value (%d)\n",
5883                                channel);
5884                 return 0;
5885         }
5886
5887         IPW_DEBUG_INFO("Setting channel to %i\n", (int)channel);
5888         priv->channel = channel;
5889
5890         /* If we are currently associated, or trying to associate
5891          * then see if this is a new channel (causing us to disassociate) */
5892         if (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
5893                 IPW_DEBUG_ASSOC("Disassociating due to channel change.\n");
5894                 ipw_disassociate(priv);
5895         } else {
5896                 ipw_associate(priv);
5897         }
5898
5899         return 0;
5900 }
5901
5902 static int ipw_wx_set_freq(struct net_device *dev,
5903                            struct iw_request_info *info,
5904                            union iwreq_data *wrqu, char *extra)
5905 {
5906         struct ipw_priv *priv = ieee80211_priv(dev);
5907         struct iw_freq *fwrq = &wrqu->freq;
5908
5909         /* if setting by freq convert to channel */
5910         if (fwrq->e == 1) {
5911                 if ((fwrq->m >= (int)2.412e8 && fwrq->m <= (int)2.487e8)) {
5912                         int f = fwrq->m / 100000;
5913                         int c = 0;
5914
5915                         while ((c < REG_MAX_CHANNEL) &&
5916                                (f != ipw_frequencies[c]))
5917                                 c++;
5918
5919                         /* hack to fall through */
5920                         fwrq->e = 0;
5921                         fwrq->m = c + 1;
5922                 }
5923         }
5924
5925         if (fwrq->e > 0 || fwrq->m > 1000)
5926                 return -EOPNOTSUPP;
5927
5928         IPW_DEBUG_WX("SET Freq/Channel -> %d \n", fwrq->m);
5929         return ipw_set_channel(priv, (u8) fwrq->m);
5930 }
5931
5932 static int ipw_wx_get_freq(struct net_device *dev,
5933                            struct iw_request_info *info,
5934                            union iwreq_data *wrqu, char *extra)
5935 {
5936         struct ipw_priv *priv = ieee80211_priv(dev);
5937
5938         wrqu->freq.e = 0;
5939
5940         /* If we are associated, trying to associate, or have a statically
5941          * configured CHANNEL then return that; otherwise return ANY */
5942         if (priv->config & CFG_STATIC_CHANNEL ||
5943             priv->status & (STATUS_ASSOCIATING | STATUS_ASSOCIATED))
5944                 wrqu->freq.m = priv->channel;
5945         else
5946                 wrqu->freq.m = 0;
5947
5948         IPW_DEBUG_WX("GET Freq/Channel -> %d \n", priv->channel);
5949         return 0;
5950 }
5951
5952 static int ipw_wx_set_mode(struct net_device *dev,
5953                            struct iw_request_info *info,
5954                            union iwreq_data *wrqu, char *extra)
5955 {
5956         struct ipw_priv *priv = ieee80211_priv(dev);
5957         int err = 0;
5958
5959         IPW_DEBUG_WX("Set MODE: %d\n", wrqu->mode);
5960
5961         if (wrqu->mode == priv->ieee->iw_mode)
5962                 return 0;
5963
5964         switch (wrqu->mode) {
5965 #ifdef CONFIG_IPW_MONITOR
5966         case IW_MODE_MONITOR:
5967 #endif
5968         case IW_MODE_ADHOC:
5969         case IW_MODE_INFRA:
5970                 break;
5971         case IW_MODE_AUTO:
5972                 wrqu->mode = IW_MODE_INFRA;
5973                 break;
5974         default:
5975                 return -EINVAL;
5976         }
5977
5978 #ifdef CONFIG_IPW_MONITOR
5979         if (priv->ieee->iw_mode == IW_MODE_MONITOR)
5980                 priv->net_dev->type = ARPHRD_ETHER;
5981
5982         if (wrqu->mode == IW_MODE_MONITOR)
5983                 priv->net_dev->type = ARPHRD_IEEE80211;
5984 #endif                          /* CONFIG_IPW_MONITOR */
5985
5986 #ifdef CONFIG_PM
5987         /* Free the existing firmware and reset the fw_loaded
5988          * flag so ipw_load() will bring in the new firmawre */
5989         if (fw_loaded) {
5990                 fw_loaded = 0;
5991         }
5992
5993         release_firmware(bootfw);
5994         release_firmware(ucode);
5995         release_firmware(firmware);
5996         bootfw = ucode = firmware = NULL;
5997 #endif
5998
5999         priv->ieee->iw_mode = wrqu->mode;
6000         ipw_adapter_restart(priv);
6001
6002         return err;
6003 }
6004
6005 static int ipw_wx_get_mode(struct net_device *dev,
6006                            struct iw_request_info *info,
6007                            union iwreq_data *wrqu, char *extra)
6008 {
6009         struct ipw_priv *priv = ieee80211_priv(dev);
6010
6011         wrqu->mode = priv->ieee->iw_mode;
6012         IPW_DEBUG_WX("Get MODE -> %d\n", wrqu->mode);
6013
6014         return 0;
6015 }
6016
6017 #define DEFAULT_RTS_THRESHOLD     2304U
6018 #define MIN_RTS_THRESHOLD         1U
6019 #define MAX_RTS_THRESHOLD         2304U
6020 #define DEFAULT_BEACON_INTERVAL   100U
6021 #define DEFAULT_SHORT_RETRY_LIMIT 7U
6022 #define DEFAULT_LONG_RETRY_LIMIT  4U
6023
6024 /* Values are in microsecond */
6025 static const s32 timeout_duration[] = {
6026         350000,
6027         250000,
6028         75000,
6029         37000,
6030         25000,
6031 };
6032
6033 static const s32 period_duration[] = {
6034         400000,
6035         700000,
6036         1000000,
6037         1000000,
6038         1000000
6039 };
6040
6041 static int ipw_wx_get_range(struct net_device *dev,
6042                             struct iw_request_info *info,
6043                             union iwreq_data *wrqu, char *extra)
6044 {
6045         struct ipw_priv *priv = ieee80211_priv(dev);
6046         struct iw_range *range = (struct iw_range *)extra;
6047         u16 val;
6048         int i;
6049
6050         wrqu->data.length = sizeof(*range);
6051         memset(range, 0, sizeof(*range));
6052
6053         /* 54Mbs == ~27 Mb/s real (802.11g) */
6054         range->throughput = 27 * 1000 * 1000;
6055
6056         range->max_qual.qual = 100;
6057         /* TODO: Find real max RSSI and stick here */
6058         range->max_qual.level = 0;
6059         range->max_qual.noise = 0;
6060         range->max_qual.updated = 7;    /* Updated all three */
6061
6062         range->avg_qual.qual = 70;
6063         /* TODO: Find real 'good' to 'bad' threshol value for RSSI */
6064         range->avg_qual.level = 0;      /* FIXME to real average level */
6065         range->avg_qual.noise = 0;
6066         range->avg_qual.updated = 7;    /* Updated all three */
6067
6068         range->num_bitrates = min(priv->rates.num_rates, (u8) IW_MAX_BITRATES);
6069
6070         for (i = 0; i < range->num_bitrates; i++)
6071                 range->bitrate[i] = (priv->rates.supported_rates[i] & 0x7F) *
6072                     500000;
6073
6074         range->max_rts = DEFAULT_RTS_THRESHOLD;
6075         range->min_frag = MIN_FRAG_THRESHOLD;
6076         range->max_frag = MAX_FRAG_THRESHOLD;
6077
6078         range->encoding_size[0] = 5;
6079         range->encoding_size[1] = 13;
6080         range->num_encoding_sizes = 2;
6081         range->max_encoding_tokens = WEP_KEYS;
6082
6083         /* Set the Wireless Extension versions */
6084         range->we_version_compiled = WIRELESS_EXT;
6085         range->we_version_source = 16;
6086
6087         range->num_channels = FREQ_COUNT;
6088
6089         val = 0;
6090         for (i = 0; i < FREQ_COUNT; i++) {
6091                 range->freq[val].i = i + 1;
6092                 range->freq[val].m = ipw_frequencies[i] * 100000;
6093                 range->freq[val].e = 1;
6094                 val++;
6095
6096                 if (val == IW_MAX_FREQUENCIES)
6097                         break;
6098         }
6099         range->num_frequency = val;
6100
6101         IPW_DEBUG_WX("GET Range\n");
6102         return 0;
6103 }
6104
6105 static int ipw_wx_set_wap(struct net_device *dev,
6106                           struct iw_request_info *info,
6107                           union iwreq_data *wrqu, char *extra)
6108 {
6109         struct ipw_priv *priv = ieee80211_priv(dev);
6110
6111         static const unsigned char any[] = {
6112                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
6113         };
6114         static const unsigned char off[] = {
6115                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
6116         };
6117
6118         if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
6119                 return -EINVAL;
6120
6121         if (!memcmp(any, wrqu->ap_addr.sa_data, ETH_ALEN) ||
6122             !memcmp(off, wrqu->ap_addr.sa_data, ETH_ALEN)) {
6123                 /* we disable mandatory BSSID association */
6124                 IPW_DEBUG_WX("Setting AP BSSID to ANY\n");
6125                 priv->config &= ~CFG_STATIC_BSSID;
6126                 if (!(priv->status & (STATUS_SCANNING | STATUS_ASSOCIATED |
6127                                       STATUS_ASSOCIATING))) {
6128                         IPW_DEBUG_ASSOC("Attempting to associate with new "
6129                                         "parameters.\n");
6130                         ipw_associate(priv);
6131                 }
6132
6133                 return 0;
6134         }
6135
6136         priv->config |= CFG_STATIC_BSSID;
6137         if (!memcmp(priv->bssid, wrqu->ap_addr.sa_data, ETH_ALEN)) {
6138                 IPW_DEBUG_WX("BSSID set to current BSSID.\n");
6139                 return 0;
6140         }
6141
6142         IPW_DEBUG_WX("Setting mandatory BSSID to " MAC_FMT "\n",
6143                      MAC_ARG(wrqu->ap_addr.sa_data));
6144
6145         memcpy(priv->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
6146
6147         /* If we are currently associated, or trying to associate
6148          * then see if this is a new BSSID (causing us to disassociate) */
6149         if (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
6150                 IPW_DEBUG_ASSOC("Disassociating due to BSSID change.\n");
6151                 ipw_disassociate(priv);
6152         } else {
6153                 ipw_associate(priv);
6154         }
6155
6156         return 0;
6157 }
6158
6159 static int ipw_wx_get_wap(struct net_device *dev,
6160                           struct iw_request_info *info,
6161                           union iwreq_data *wrqu, char *extra)
6162 {
6163         struct ipw_priv *priv = ieee80211_priv(dev);
6164         /* If we are associated, trying to associate, or have a statically
6165          * configured BSSID then return that; otherwise return ANY */
6166         if (priv->config & CFG_STATIC_BSSID ||
6167             priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
6168                 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
6169                 memcpy(wrqu->ap_addr.sa_data, &priv->bssid, ETH_ALEN);
6170         } else
6171                 memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);
6172
6173         IPW_DEBUG_WX("Getting WAP BSSID: " MAC_FMT "\n",
6174                      MAC_ARG(wrqu->ap_addr.sa_data));
6175         return 0;
6176 }
6177
6178 static int ipw_wx_set_essid(struct net_device *dev,
6179                             struct iw_request_info *info,
6180                             union iwreq_data *wrqu, char *extra)
6181 {
6182         struct ipw_priv *priv = ieee80211_priv(dev);
6183         char *essid = "";       /* ANY */
6184         int length = 0;
6185
6186         if (wrqu->essid.flags && wrqu->essid.length) {
6187                 length = wrqu->essid.length - 1;
6188                 essid = extra;
6189         }
6190         if (length == 0) {
6191                 IPW_DEBUG_WX("Setting ESSID to ANY\n");
6192                 priv->config &= ~CFG_STATIC_ESSID;
6193                 if (!(priv->status & (STATUS_SCANNING | STATUS_ASSOCIATED |
6194                                       STATUS_ASSOCIATING))) {
6195                         IPW_DEBUG_ASSOC("Attempting to associate with new "
6196                                         "parameters.\n");
6197                         ipw_associate(priv);
6198                 }
6199
6200                 return 0;
6201         }
6202
6203         length = min(length, IW_ESSID_MAX_SIZE);
6204
6205         priv->config |= CFG_STATIC_ESSID;
6206
6207         if (priv->essid_len == length && !memcmp(priv->essid, extra, length)) {
6208                 IPW_DEBUG_WX("ESSID set to current ESSID.\n");
6209                 return 0;
6210         }
6211
6212         IPW_DEBUG_WX("Setting ESSID: '%s' (%d)\n", escape_essid(essid, length),
6213                      length);
6214
6215         priv->essid_len = length;
6216         memcpy(priv->essid, essid, priv->essid_len);
6217
6218         /* If we are currently associated, or trying to associate
6219          * then see if this is a new ESSID (causing us to disassociate) */
6220         if (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
6221                 IPW_DEBUG_ASSOC("Disassociating due to ESSID change.\n");
6222                 ipw_disassociate(priv);
6223         } else {
6224                 ipw_associate(priv);
6225         }
6226
6227         return 0;
6228 }
6229
6230 static int ipw_wx_get_essid(struct net_device *dev,
6231                             struct iw_request_info *info,
6232                             union iwreq_data *wrqu, char *extra)
6233 {
6234         struct ipw_priv *priv = ieee80211_priv(dev);
6235
6236         /* If we are associated, trying to associate, or have a statically
6237          * configured ESSID then return that; otherwise return ANY */
6238         if (priv->config & CFG_STATIC_ESSID ||
6239             priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
6240                 IPW_DEBUG_WX("Getting essid: '%s'\n",
6241                              escape_essid(priv->essid, priv->essid_len));
6242                 memcpy(extra, priv->essid, priv->essid_len);
6243                 wrqu->essid.length = priv->essid_len;
6244                 wrqu->essid.flags = 1;  /* active */
6245         } else {
6246                 IPW_DEBUG_WX("Getting essid: ANY\n");
6247                 wrqu->essid.length = 0;
6248                 wrqu->essid.flags = 0;  /* active */
6249         }
6250
6251         return 0;
6252 }
6253
6254 static int ipw_wx_set_nick(struct net_device *dev,
6255                            struct iw_request_info *info,
6256                            union iwreq_data *wrqu, char *extra)
6257 {
6258         struct ipw_priv *priv = ieee80211_priv(dev);
6259
6260         IPW_DEBUG_WX("Setting nick to '%s'\n", extra);
6261         if (wrqu->data.length > IW_ESSID_MAX_SIZE)
6262                 return -E2BIG;
6263
6264         wrqu->data.length = min((size_t) wrqu->data.length, sizeof(priv->nick));
6265         memset(priv->nick, 0, sizeof(priv->nick));
6266         memcpy(priv->nick, extra, wrqu->data.length);
6267         IPW_DEBUG_TRACE("<<\n");
6268         return 0;
6269
6270 }
6271
6272 static int ipw_wx_get_nick(struct net_device *dev,
6273                            struct iw_request_info *info,
6274                            union iwreq_data *wrqu, char *extra)
6275 {
6276         struct ipw_priv *priv = ieee80211_priv(dev);
6277         IPW_DEBUG_WX("Getting nick\n");
6278         wrqu->data.length = strlen(priv->nick) + 1;
6279         memcpy(extra, priv->nick, wrqu->data.length);
6280         wrqu->data.flags = 1;   /* active */
6281         return 0;
6282 }
6283
6284 static int ipw_wx_set_rate(struct net_device *dev,
6285                            struct iw_request_info *info,
6286                            union iwreq_data *wrqu, char *extra)
6287 {
6288         /* TODO: We should use semaphores or locks for access to priv */
6289         struct ipw_priv *priv = ieee80211_priv(dev);
6290         u32 target_rate = wrqu->bitrate.value;
6291         u32 fixed, mask;
6292
6293         /* value = -1, fixed = 0 means auto only, so we should use all rates offered by AP */
6294         /* value = X, fixed = 1 means only rate X */
6295         /* value = X, fixed = 0 means all rates lower equal X */
6296
6297         if (target_rate == -1) {
6298                 fixed = 0;
6299                 mask = IEEE80211_DEFAULT_RATES_MASK;
6300                 /* Now we should reassociate */
6301                 goto apply;
6302         }
6303
6304         mask = 0;
6305         fixed = wrqu->bitrate.fixed;
6306
6307         if (target_rate == 1000000 || !fixed)
6308                 mask |= IEEE80211_CCK_RATE_1MB_MASK;
6309         if (target_rate == 1000000)
6310                 goto apply;
6311
6312         if (target_rate == 2000000 || !fixed)
6313                 mask |= IEEE80211_CCK_RATE_2MB_MASK;
6314         if (target_rate == 2000000)
6315                 goto apply;
6316
6317         if (target_rate == 5500000 || !fixed)
6318                 mask |= IEEE80211_CCK_RATE_5MB_MASK;
6319         if (target_rate == 5500000)
6320                 goto apply;
6321
6322         if (target_rate == 6000000 || !fixed)
6323                 mask |= IEEE80211_OFDM_RATE_6MB_MASK;
6324         if (target_rate == 6000000)
6325                 goto apply;
6326
6327         if (target_rate == 9000000 || !fixed)
6328                 mask |= IEEE80211_OFDM_RATE_9MB_MASK;
6329         if (target_rate == 9000000)
6330                 goto apply;
6331
6332         if (target_rate == 11000000 || !fixed)
6333                 mask |= IEEE80211_CCK_RATE_11MB_MASK;
6334         if (target_rate == 11000000)
6335                 goto apply;
6336
6337         if (target_rate == 12000000 || !fixed)
6338                 mask |= IEEE80211_OFDM_RATE_12MB_MASK;
6339         if (target_rate == 12000000)
6340                 goto apply;
6341
6342         if (target_rate == 18000000 || !fixed)
6343                 mask |= IEEE80211_OFDM_RATE_18MB_MASK;
6344         if (target_rate == 18000000)
6345                 goto apply;
6346
6347         if (target_rate == 24000000 || !fixed)
6348                 mask |= IEEE80211_OFDM_RATE_24MB_MASK;
6349         if (target_rate == 24000000)
6350                 goto apply;
6351
6352         if (target_rate == 36000000 || !fixed)
6353                 mask |= IEEE80211_OFDM_RATE_36MB_MASK;
6354         if (target_rate == 36000000)
6355                 goto apply;
6356
6357         if (target_rate == 48000000 || !fixed)
6358                 mask |= IEEE80211_OFDM_RATE_48MB_MASK;
6359         if (target_rate == 48000000)
6360                 goto apply;
6361
6362         if (target_rate == 54000000 || !fixed)
6363                 mask |= IEEE80211_OFDM_RATE_54MB_MASK;
6364         if (target_rate == 54000000)
6365                 goto apply;
6366
6367         IPW_DEBUG_WX("invalid rate specified, returning error\n");
6368         return -EINVAL;
6369
6370       apply:
6371         IPW_DEBUG_WX("Setting rate mask to 0x%08X [%s]\n",
6372                      mask, fixed ? "fixed" : "sub-rates");
6373
6374         if (mask == IEEE80211_DEFAULT_RATES_MASK)
6375                 priv->config &= ~CFG_FIXED_RATE;
6376         else
6377                 priv->config |= CFG_FIXED_RATE;
6378
6379         if (priv->rates_mask != mask) {
6380                 priv->rates_mask = mask;
6381                 /* If we are already associated or are currently trying to
6382                  * associate, disassociate and try again */
6383                 if ((priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING))) {
6384                         IPW_DEBUG_ASSOC("Disassociating due to RATE change.\n");
6385                         ipw_disassociate(priv);
6386                 }
6387         } else {
6388                 /* We are not yet associated, so kick one off... */
6389                 ipw_associate(priv);
6390         }
6391
6392         return 0;
6393 }
6394
6395 static int ipw_wx_get_rate(struct net_device *dev,
6396                            struct iw_request_info *info,
6397                            union iwreq_data *wrqu, char *extra)
6398 {
6399         struct ipw_priv *priv = ieee80211_priv(dev);
6400         wrqu->bitrate.value = priv->last_rate;
6401
6402         IPW_DEBUG_WX("GET Rate -> %d \n", wrqu->bitrate.value);
6403         return 0;
6404 }
6405
6406 static int ipw_wx_set_rts(struct net_device *dev,
6407                           struct iw_request_info *info,
6408                           union iwreq_data *wrqu, char *extra)
6409 {
6410         struct ipw_priv *priv = ieee80211_priv(dev);
6411
6412         if (wrqu->rts.disabled)
6413                 priv->rts_threshold = DEFAULT_RTS_THRESHOLD;
6414         else {
6415                 if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
6416                     wrqu->rts.value > MAX_RTS_THRESHOLD)
6417                         return -EINVAL;
6418
6419                 priv->rts_threshold = wrqu->rts.value;
6420         }
6421
6422         ipw_send_rts_threshold(priv, priv->rts_threshold);
6423         IPW_DEBUG_WX("SET RTS Threshold -> %d \n", priv->rts_threshold);
6424         return 0;
6425 }
6426
6427 static int ipw_wx_get_rts(struct net_device *dev,
6428                           struct iw_request_info *info,
6429                           union iwreq_data *wrqu, char *extra)
6430 {
6431         struct ipw_priv *priv = ieee80211_priv(dev);
6432         wrqu->rts.value = priv->rts_threshold;
6433         wrqu->rts.fixed = 0;    /* no auto select */
6434         wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
6435
6436         IPW_DEBUG_WX("GET RTS Threshold -> %d \n", wrqu->rts.value);
6437         return 0;
6438 }
6439
6440 static int ipw_wx_set_txpow(struct net_device *dev,
6441                             struct iw_request_info *info,
6442                             union iwreq_data *wrqu, char *extra)
6443 {
6444         struct ipw_priv *priv = ieee80211_priv(dev);
6445         struct ipw_tx_power tx_power;
6446         int i;
6447
6448         if (ipw_radio_kill_sw(priv, wrqu->power.disabled))
6449                 return -EINPROGRESS;
6450
6451         if (wrqu->power.flags != IW_TXPOW_DBM)
6452                 return -EINVAL;
6453
6454         if ((wrqu->power.value > 20) || (wrqu->power.value < -12))
6455                 return -EINVAL;
6456
6457         priv->tx_power = wrqu->power.value;
6458
6459         memset(&tx_power, 0, sizeof(tx_power));
6460
6461         /* configure device for 'G' band */
6462         tx_power.ieee_mode = IPW_G_MODE;
6463         tx_power.num_channels = 11;
6464         for (i = 0; i < 11; i++) {
6465                 tx_power.channels_tx_power[i].channel_number = i + 1;
6466                 tx_power.channels_tx_power[i].tx_power = priv->tx_power;
6467         }
6468         if (ipw_send_tx_power(priv, &tx_power))
6469                 goto error;
6470
6471         /* configure device to also handle 'B' band */
6472         tx_power.ieee_mode = IPW_B_MODE;
6473         if (ipw_send_tx_power(priv, &tx_power))
6474                 goto error;
6475
6476         return 0;
6477
6478       error:
6479         return -EIO;
6480 }
6481
6482 static int ipw_wx_get_txpow(struct net_device *dev,
6483                             struct iw_request_info *info,
6484                             union iwreq_data *wrqu, char *extra)
6485 {
6486         struct ipw_priv *priv = ieee80211_priv(dev);
6487
6488         wrqu->power.value = priv->tx_power;
6489         wrqu->power.fixed = 1;
6490         wrqu->power.flags = IW_TXPOW_DBM;
6491         wrqu->power.disabled = (priv->status & STATUS_RF_KILL_MASK) ? 1 : 0;
6492
6493         IPW_DEBUG_WX("GET TX Power -> %s %d \n",
6494                      wrqu->power.disabled ? "ON" : "OFF", wrqu->power.value);
6495
6496         return 0;
6497 }
6498
6499 static int ipw_wx_set_frag(struct net_device *dev,
6500                            struct iw_request_info *info,
6501                            union iwreq_data *wrqu, char *extra)
6502 {
6503         struct ipw_priv *priv = ieee80211_priv(dev);
6504
6505         if (wrqu->frag.disabled)
6506                 priv->ieee->fts = DEFAULT_FTS;
6507         else {
6508                 if (wrqu->frag.value < MIN_FRAG_THRESHOLD ||
6509                     wrqu->frag.value > MAX_FRAG_THRESHOLD)
6510                         return -EINVAL;
6511
6512                 priv->ieee->fts = wrqu->frag.value & ~0x1;
6513         }
6514
6515         ipw_send_frag_threshold(priv, wrqu->frag.value);
6516         IPW_DEBUG_WX("SET Frag Threshold -> %d \n", wrqu->frag.value);
6517         return 0;
6518 }
6519
6520 static int ipw_wx_get_frag(struct net_device *dev,
6521                            struct iw_request_info *info,
6522                            union iwreq_data *wrqu, char *extra)
6523 {
6524         struct ipw_priv *priv = ieee80211_priv(dev);
6525         wrqu->frag.value = priv->ieee->fts;
6526         wrqu->frag.fixed = 0;   /* no auto select */
6527         wrqu->frag.disabled = (wrqu->frag.value == DEFAULT_FTS);
6528
6529         IPW_DEBUG_WX("GET Frag Threshold -> %d \n", wrqu->frag.value);
6530
6531         return 0;
6532 }
6533
6534 static int ipw_wx_set_retry(struct net_device *dev,
6535                             struct iw_request_info *info,
6536                             union iwreq_data *wrqu, char *extra)
6537 {
6538         IPW_DEBUG_WX("0x%p, 0x%p, 0x%p\n", dev, info, wrqu);
6539         return -EOPNOTSUPP;
6540 }
6541
6542 static int ipw_wx_get_retry(struct net_device *dev,
6543                             struct iw_request_info *info,
6544                             union iwreq_data *wrqu, char *extra)
6545 {
6546         IPW_DEBUG_WX("0x%p, 0x%p, 0x%p\n", dev, info, wrqu);
6547         return -EOPNOTSUPP;
6548 }
6549
6550 static int ipw_wx_set_scan(struct net_device *dev,
6551                            struct iw_request_info *info,
6552                            union iwreq_data *wrqu, char *extra)
6553 {
6554         struct ipw_priv *priv = ieee80211_priv(dev);
6555         IPW_DEBUG_WX("Start scan\n");
6556         if (ipw_request_scan(priv))
6557                 return -EIO;
6558         return 0;
6559 }
6560
6561 static int ipw_wx_get_scan(struct net_device *dev,
6562                            struct iw_request_info *info,
6563                            union iwreq_data *wrqu, char *extra)
6564 {
6565         struct ipw_priv *priv = ieee80211_priv(dev);
6566         return ieee80211_wx_get_scan(priv->ieee, info, wrqu, extra);
6567 }
6568
6569 static int ipw_wx_set_encode(struct net_device *dev,
6570                              struct iw_request_info *info,
6571                              union iwreq_data *wrqu, char *key)
6572 {
6573         struct ipw_priv *priv = ieee80211_priv(dev);
6574         return ieee80211_wx_set_encode(priv->ieee, info, wrqu, key);
6575 }
6576
6577 static int ipw_wx_get_encode(struct net_device *dev,
6578                              struct iw_request_info *info,
6579                              union iwreq_data *wrqu, char *key)
6580 {
6581         struct ipw_priv *priv = ieee80211_priv(dev);
6582         return ieee80211_wx_get_encode(priv->ieee, info, wrqu, key);
6583 }
6584
6585 static int ipw_wx_set_power(struct net_device *dev,
6586                             struct iw_request_info *info,
6587                             union iwreq_data *wrqu, char *extra)
6588 {
6589         struct ipw_priv *priv = ieee80211_priv(dev);
6590         int err;
6591
6592         if (wrqu->power.disabled) {
6593                 priv->power_mode = IPW_POWER_LEVEL(priv->power_mode);
6594                 err = ipw_send_power_mode(priv, IPW_POWER_MODE_CAM);
6595                 if (err) {
6596                         IPW_DEBUG_WX("failed setting power mode.\n");
6597                         return err;
6598                 }
6599                 IPW_DEBUG_WX("SET Power Management Mode -> off\n");
6600
6601                 return 0;
6602         }
6603
6604         switch (wrqu->power.flags & IW_POWER_MODE) {
6605         case IW_POWER_ON:       /* If not specified */
6606         case IW_POWER_MODE:     /* If set all mask */
6607         case IW_POWER_ALL_R:    /* If explicitely state all */
6608                 break;
6609         default:                /* Otherwise we don't support it */
6610                 IPW_DEBUG_WX("SET PM Mode: %X not supported.\n",
6611                              wrqu->power.flags);
6612                 return -EOPNOTSUPP;
6613         }
6614
6615         /* If the user hasn't specified a power management mode yet, default
6616          * to BATTERY */
6617         if (IPW_POWER_LEVEL(priv->power_mode) == IPW_POWER_AC)
6618                 priv->power_mode = IPW_POWER_ENABLED | IPW_POWER_BATTERY;
6619         else
6620                 priv->power_mode = IPW_POWER_ENABLED | priv->power_mode;
6621         err = ipw_send_power_mode(priv, IPW_POWER_LEVEL(priv->power_mode));
6622         if (err) {
6623                 IPW_DEBUG_WX("failed setting power mode.\n");
6624                 return err;
6625         }
6626
6627         IPW_DEBUG_WX("SET Power Management Mode -> 0x%02X\n", priv->power_mode);
6628
6629         return 0;
6630 }
6631
6632 static int ipw_wx_get_power(struct net_device *dev,
6633                             struct iw_request_info *info,
6634                             union iwreq_data *wrqu, char *extra)
6635 {
6636         struct ipw_priv *priv = ieee80211_priv(dev);
6637
6638         if (!(priv->power_mode & IPW_POWER_ENABLED)) {
6639                 wrqu->power.disabled = 1;
6640         } else {
6641                 wrqu->power.disabled = 0;
6642         }
6643
6644         IPW_DEBUG_WX("GET Power Management Mode -> %02X\n", priv->power_mode);
6645
6646         return 0;
6647 }
6648
6649 static int ipw_wx_set_powermode(struct net_device *dev,
6650                                 struct iw_request_info *info,
6651                                 union iwreq_data *wrqu, char *extra)
6652 {
6653         struct ipw_priv *priv = ieee80211_priv(dev);
6654         int mode = *(int *)extra;
6655         int err;
6656
6657         if ((mode < 1) || (mode > IPW_POWER_LIMIT)) {
6658                 mode = IPW_POWER_AC;
6659                 priv->power_mode = mode;
6660         } else {
6661                 priv->power_mode = IPW_POWER_ENABLED | mode;
6662         }
6663
6664         if (priv->power_mode != mode) {
6665                 err = ipw_send_power_mode(priv, mode);
6666
6667                 if (err) {
6668                         IPW_DEBUG_WX("failed setting power mode.\n");
6669                         return err;
6670                 }
6671         }
6672
6673         return 0;
6674 }
6675
6676 #define MAX_WX_STRING 80
6677 static int ipw_wx_get_powermode(struct net_device *dev,
6678                                 struct iw_request_info *info,
6679                                 union iwreq_data *wrqu, char *extra)
6680 {
6681         struct ipw_priv *priv = ieee80211_priv(dev);
6682         int level = IPW_POWER_LEVEL(priv->power_mode);
6683         char *p = extra;
6684
6685         p += snprintf(p, MAX_WX_STRING, "Power save level: %d ", level);
6686
6687         switch (level) {
6688         case IPW_POWER_AC:
6689                 p += snprintf(p, MAX_WX_STRING - (p - extra), "(AC)");
6690                 break;
6691         case IPW_POWER_BATTERY:
6692                 p += snprintf(p, MAX_WX_STRING - (p - extra), "(BATTERY)");
6693                 break;
6694         default:
6695                 p += snprintf(p, MAX_WX_STRING - (p - extra),
6696                               "(Timeout %dms, Period %dms)",
6697                               timeout_duration[level - 1] / 1000,
6698                               period_duration[level - 1] / 1000);
6699         }
6700
6701         if (!(priv->power_mode & IPW_POWER_ENABLED))
6702                 p += snprintf(p, MAX_WX_STRING - (p - extra), " OFF");
6703
6704         wrqu->data.length = p - extra + 1;
6705
6706         return 0;
6707 }
6708
6709 static int ipw_wx_set_wireless_mode(struct net_device *dev,
6710                                     struct iw_request_info *info,
6711                                     union iwreq_data *wrqu, char *extra)
6712 {
6713         struct ipw_priv *priv = ieee80211_priv(dev);
6714         int mode = *(int *)extra;
6715         u8 band = 0, modulation = 0;
6716
6717         if (mode == 0 || mode & ~IEEE_MODE_MASK) {
6718                 IPW_WARNING("Attempt to set invalid wireless mode: %d\n", mode);
6719                 return -EINVAL;
6720         }
6721
6722         if (priv->adapter == IPW_2915ABG) {
6723                 priv->ieee->abg_true = 1;
6724                 if (mode & IEEE_A) {
6725                         band |= IEEE80211_52GHZ_BAND;
6726                         modulation |= IEEE80211_OFDM_MODULATION;
6727                 } else
6728                         priv->ieee->abg_true = 0;
6729         } else {
6730                 if (mode & IEEE_A) {
6731                         IPW_WARNING("Attempt to set 2200BG into "
6732                                     "802.11a mode\n");
6733                         return -EINVAL;
6734                 }
6735
6736                 priv->ieee->abg_true = 0;
6737         }
6738
6739         if (mode & IEEE_B) {
6740                 band |= IEEE80211_24GHZ_BAND;
6741                 modulation |= IEEE80211_CCK_MODULATION;
6742         } else
6743                 priv->ieee->abg_true = 0;
6744
6745         if (mode & IEEE_G) {
6746                 band |= IEEE80211_24GHZ_BAND;
6747                 modulation |= IEEE80211_OFDM_MODULATION;
6748         } else
6749                 priv->ieee->abg_true = 0;
6750
6751         priv->ieee->mode = mode;
6752         priv->ieee->freq_band = band;
6753         priv->ieee->modulation = modulation;
6754         init_supported_rates(priv, &priv->rates);
6755
6756         /* If we are currently associated, or trying to associate
6757          * then see if this is a new configuration (causing us to
6758          * disassociate) */
6759         if (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
6760                 /* The resulting association will trigger
6761                  * the new rates to be sent to the device */
6762                 IPW_DEBUG_ASSOC("Disassociating due to mode change.\n");
6763                 ipw_disassociate(priv);
6764         } else
6765                 ipw_send_supported_rates(priv, &priv->rates);
6766
6767         IPW_DEBUG_WX("PRIV SET MODE: %c%c%c\n",
6768                      mode & IEEE_A ? 'a' : '.',
6769                      mode & IEEE_B ? 'b' : '.', mode & IEEE_G ? 'g' : '.');
6770         return 0;
6771 }
6772
6773 static int ipw_wx_get_wireless_mode(struct net_device *dev,
6774                                     struct iw_request_info *info,
6775                                     union iwreq_data *wrqu, char *extra)
6776 {
6777         struct ipw_priv *priv = ieee80211_priv(dev);
6778
6779         switch (priv->ieee->mode) {
6780         case IEEE_A:
6781                 strncpy(extra, "802.11a (1)", MAX_WX_STRING);
6782                 break;
6783         case IEEE_B:
6784                 strncpy(extra, "802.11b (2)", MAX_WX_STRING);
6785                 break;
6786         case IEEE_A | IEEE_B:
6787                 strncpy(extra, "802.11ab (3)", MAX_WX_STRING);
6788                 break;
6789         case IEEE_G:
6790                 strncpy(extra, "802.11g (4)", MAX_WX_STRING);
6791                 break;
6792         case IEEE_A | IEEE_G:
6793                 strncpy(extra, "802.11ag (5)", MAX_WX_STRING);
6794                 break;
6795         case IEEE_B | IEEE_G:
6796                 strncpy(extra, "802.11bg (6)", MAX_WX_STRING);
6797                 break;
6798         case IEEE_A | IEEE_B | IEEE_G:
6799                 strncpy(extra, "802.11abg (7)", MAX_WX_STRING);
6800                 break;
6801         default:
6802                 strncpy(extra, "unknown", MAX_WX_STRING);
6803                 break;
6804         }
6805
6806         IPW_DEBUG_WX("PRIV GET MODE: %s\n", extra);
6807
6808         wrqu->data.length = strlen(extra) + 1;
6809
6810         return 0;
6811 }
6812
6813 static int ipw_wx_set_preamble(struct net_device *dev,
6814                                struct iw_request_info *info,
6815                                union iwreq_data *wrqu, char *extra)
6816 {
6817         struct ipw_priv *priv = ieee80211_priv(dev);
6818         int mode = *(int *)extra;
6819
6820         /* Switching from SHORT -> LONG requires a disassociation */
6821         if (mode == 1) {
6822                 if (!(priv->config & CFG_PREAMBLE_LONG)) {
6823                         priv->config |= CFG_PREAMBLE_LONG;
6824                         if (priv->status &
6825                             (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
6826                                 IPW_DEBUG_ASSOC
6827                                     ("Disassociating due to preamble "
6828                                      "change.\n");
6829                                 ipw_disassociate(priv);
6830                         }
6831                 }
6832                 goto done;
6833         }
6834
6835         if (mode == 0) {
6836                 priv->config &= ~CFG_PREAMBLE_LONG;
6837                 goto done;
6838         }
6839
6840         return -EINVAL;
6841
6842       done:
6843         return 0;
6844 }
6845
6846 static int ipw_wx_get_preamble(struct net_device *dev,
6847                                struct iw_request_info *info,
6848                                union iwreq_data *wrqu, char *extra)
6849 {
6850         struct ipw_priv *priv = ieee80211_priv(dev);
6851
6852         if (priv->config & CFG_PREAMBLE_LONG)
6853                 snprintf(wrqu->name, IFNAMSIZ, "long (1)");
6854         else
6855                 snprintf(wrqu->name, IFNAMSIZ, "auto (0)");
6856
6857         return 0;
6858 }
6859
6860 #ifdef CONFIG_IPW_MONITOR
6861 static int ipw_wx_set_monitor(struct net_device *dev,
6862                               struct iw_request_info *info,
6863                               union iwreq_data *wrqu, char *extra)
6864 {
6865         struct ipw_priv *priv = ieee80211_priv(dev);
6866         int *parms = (int *)extra;
6867         int enable = (parms[0] > 0);
6868
6869         IPW_DEBUG_WX("SET MONITOR: %d %d\n", enable, parms[1]);
6870         if (enable) {
6871                 if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
6872                         priv->net_dev->type = ARPHRD_IEEE80211;
6873                         ipw_adapter_restart(priv);
6874                 }
6875
6876                 ipw_set_channel(priv, parms[1]);
6877         } else {
6878                 if (priv->ieee->iw_mode != IW_MODE_MONITOR)
6879                         return 0;
6880                 priv->net_dev->type = ARPHRD_ETHER;
6881                 ipw_adapter_restart(priv);
6882         }
6883         return 0;
6884 }
6885
6886 static int ipw_wx_reset(struct net_device *dev,
6887                         struct iw_request_info *info,
6888                         union iwreq_data *wrqu, char *extra)
6889 {
6890         struct ipw_priv *priv = ieee80211_priv(dev);
6891         IPW_DEBUG_WX("RESET\n");
6892         ipw_adapter_restart(priv);
6893         return 0;
6894 }
6895 #endif                          // CONFIG_IPW_MONITOR
6896
6897 /* Rebase the WE IOCTLs to zero for the handler array */
6898 #define IW_IOCTL(x) [(x)-SIOCSIWCOMMIT]
6899 static iw_handler ipw_wx_handlers[] = {
6900         IW_IOCTL(SIOCGIWNAME) = ipw_wx_get_name,
6901         IW_IOCTL(SIOCSIWFREQ) = ipw_wx_set_freq,
6902         IW_IOCTL(SIOCGIWFREQ) = ipw_wx_get_freq,
6903         IW_IOCTL(SIOCSIWMODE) = ipw_wx_set_mode,
6904         IW_IOCTL(SIOCGIWMODE) = ipw_wx_get_mode,
6905         IW_IOCTL(SIOCGIWRANGE) = ipw_wx_get_range,
6906         IW_IOCTL(SIOCSIWAP) = ipw_wx_set_wap,
6907         IW_IOCTL(SIOCGIWAP) = ipw_wx_get_wap,
6908         IW_IOCTL(SIOCSIWSCAN) = ipw_wx_set_scan,
6909         IW_IOCTL(SIOCGIWSCAN) = ipw_wx_get_scan,
6910         IW_IOCTL(SIOCSIWESSID) = ipw_wx_set_essid,
6911         IW_IOCTL(SIOCGIWESSID) = ipw_wx_get_essid,
6912         IW_IOCTL(SIOCSIWNICKN) = ipw_wx_set_nick,
6913         IW_IOCTL(SIOCGIWNICKN) = ipw_wx_get_nick,
6914         IW_IOCTL(SIOCSIWRATE) = ipw_wx_set_rate,
6915         IW_IOCTL(SIOCGIWRATE) = ipw_wx_get_rate,
6916         IW_IOCTL(SIOCSIWRTS) = ipw_wx_set_rts,
6917         IW_IOCTL(SIOCGIWRTS) = ipw_wx_get_rts,
6918         IW_IOCTL(SIOCSIWFRAG) = ipw_wx_set_frag,
6919         IW_IOCTL(SIOCGIWFRAG) = ipw_wx_get_frag,
6920         IW_IOCTL(SIOCSIWTXPOW) = ipw_wx_set_txpow,
6921         IW_IOCTL(SIOCGIWTXPOW) = ipw_wx_get_txpow,
6922         IW_IOCTL(SIOCSIWRETRY) = ipw_wx_set_retry,
6923         IW_IOCTL(SIOCGIWRETRY) = ipw_wx_get_retry,
6924         IW_IOCTL(SIOCSIWENCODE) = ipw_wx_set_encode,
6925         IW_IOCTL(SIOCGIWENCODE) = ipw_wx_get_encode,
6926         IW_IOCTL(SIOCSIWPOWER) = ipw_wx_set_power,
6927         IW_IOCTL(SIOCGIWPOWER) = ipw_wx_get_power,
6928 };
6929
6930 #define IPW_PRIV_SET_POWER      SIOCIWFIRSTPRIV
6931 #define IPW_PRIV_GET_POWER      SIOCIWFIRSTPRIV+1
6932 #define IPW_PRIV_SET_MODE       SIOCIWFIRSTPRIV+2
6933 #define IPW_PRIV_GET_MODE       SIOCIWFIRSTPRIV+3
6934 #define IPW_PRIV_SET_PREAMBLE   SIOCIWFIRSTPRIV+4
6935 #define IPW_PRIV_GET_PREAMBLE   SIOCIWFIRSTPRIV+5
6936 #define IPW_PRIV_SET_MONITOR    SIOCIWFIRSTPRIV+6
6937 #define IPW_PRIV_RESET          SIOCIWFIRSTPRIV+7
6938
6939 static struct iw_priv_args ipw_priv_args[] = {
6940         {
6941          .cmd = IPW_PRIV_SET_POWER,
6942          .set_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6943          .name = "set_power"},
6944         {
6945          .cmd = IPW_PRIV_GET_POWER,
6946          .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
6947          .name = "get_power"},
6948         {
6949          .cmd = IPW_PRIV_SET_MODE,
6950          .set_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6951          .name = "set_mode"},
6952         {
6953          .cmd = IPW_PRIV_GET_MODE,
6954          .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
6955          .name = "get_mode"},
6956         {
6957          .cmd = IPW_PRIV_SET_PREAMBLE,
6958          .set_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6959          .name = "set_preamble"},
6960         {
6961          .cmd = IPW_PRIV_GET_PREAMBLE,
6962          .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | IFNAMSIZ,
6963          .name = "get_preamble"},
6964 #ifdef CONFIG_IPW_MONITOR
6965         {
6966          IPW_PRIV_SET_MONITOR,
6967          IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "monitor"},
6968         {
6969          IPW_PRIV_RESET,
6970          IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 0, 0, "reset"},
6971 #endif                          /* CONFIG_IPW_MONITOR */
6972 };
6973
6974 static iw_handler ipw_priv_handler[] = {
6975         ipw_wx_set_powermode,
6976         ipw_wx_get_powermode,
6977         ipw_wx_set_wireless_mode,
6978         ipw_wx_get_wireless_mode,
6979         ipw_wx_set_preamble,
6980         ipw_wx_get_preamble,
6981 #ifdef CONFIG_IPW_MONITOR
6982         ipw_wx_set_monitor,
6983         ipw_wx_reset,
6984 #endif
6985 };
6986
6987 static struct iw_handler_def ipw_wx_handler_def = {
6988         .standard = ipw_wx_handlers,
6989         .num_standard = ARRAY_SIZE(ipw_wx_handlers),
6990         .num_private = ARRAY_SIZE(ipw_priv_handler),
6991         .num_private_args = ARRAY_SIZE(ipw_priv_args),
6992         .private = ipw_priv_handler,
6993         .private_args = ipw_priv_args,
6994 };
6995
6996 /*
6997  * Get wireless statistics.
6998  * Called by /proc/net/wireless
6999  * Also called by SIOCGIWSTATS
7000  */
7001 static struct iw_statistics *ipw_get_wireless_stats(struct net_device *dev)
7002 {
7003         struct ipw_priv *priv = ieee80211_priv(dev);
7004         struct iw_statistics *wstats;
7005
7006         wstats = &priv->wstats;
7007
7008         /* if hw is disabled, then ipw_get_ordinal() can't be called.
7009          * ipw2100_wx_wireless_stats seems to be called before fw is
7010          * initialized.  STATUS_ASSOCIATED will only be set if the hw is up
7011          * and associated; if not associcated, the values are all meaningless
7012          * anyway, so set them all to NULL and INVALID */
7013         if (!(priv->status & STATUS_ASSOCIATED)) {
7014                 wstats->miss.beacon = 0;
7015                 wstats->discard.retries = 0;
7016                 wstats->qual.qual = 0;
7017                 wstats->qual.level = 0;
7018                 wstats->qual.noise = 0;
7019                 wstats->qual.updated = 7;
7020                 wstats->qual.updated |= IW_QUAL_NOISE_INVALID |
7021                     IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID;
7022                 return wstats;
7023         }
7024
7025         wstats->qual.qual = priv->quality;
7026         wstats->qual.level = average_value(&priv->average_rssi);
7027         wstats->qual.noise = average_value(&priv->average_noise);
7028         wstats->qual.updated = IW_QUAL_QUAL_UPDATED | IW_QUAL_LEVEL_UPDATED |
7029             IW_QUAL_NOISE_UPDATED;
7030
7031         wstats->miss.beacon = average_value(&priv->average_missed_beacons);
7032         wstats->discard.retries = priv->last_tx_failures;
7033         wstats->discard.code = priv->ieee->ieee_stats.rx_discards_undecryptable;
7034
7035 /*      if (ipw_get_ordinal(priv, IPW_ORD_STAT_TX_RETRY, &tx_retry, &len))
7036         goto fail_get_ordinal;
7037         wstats->discard.retries += tx_retry; */
7038
7039         return wstats;
7040 }
7041
7042 /* net device stuff */
7043
7044 static inline void init_sys_config(struct ipw_sys_config *sys_config)
7045 {
7046         memset(sys_config, 0, sizeof(struct ipw_sys_config));
7047         sys_config->bt_coexistence = 1; /* We may need to look into prvStaBtConfig */
7048         sys_config->answer_broadcast_ssid_probe = 0;
7049         sys_config->accept_all_data_frames = 0;
7050         sys_config->accept_non_directed_frames = 1;
7051         sys_config->exclude_unicast_unencrypted = 0;
7052         sys_config->disable_unicast_decryption = 1;
7053         sys_config->exclude_multicast_unencrypted = 0;
7054         sys_config->disable_multicast_decryption = 1;
7055         sys_config->antenna_diversity = CFG_SYS_ANTENNA_BOTH;
7056         sys_config->pass_crc_to_host = 0;       /* TODO: See if 1 gives us FCS */
7057         sys_config->dot11g_auto_detection = 0;
7058         sys_config->enable_cts_to_self = 0;
7059         sys_config->bt_coexist_collision_thr = 0;
7060         sys_config->pass_noise_stats_to_host = 1;
7061 }
7062
7063 static int ipw_net_open(struct net_device *dev)
7064 {
7065         struct ipw_priv *priv = ieee80211_priv(dev);
7066         IPW_DEBUG_INFO("dev->open\n");
7067         /* we should be verifying the device is ready to be opened */
7068         if (!(priv->status & STATUS_RF_KILL_MASK) &&
7069             (priv->status & STATUS_ASSOCIATED))
7070                 netif_start_queue(dev);
7071         return 0;
7072 }
7073
7074 static int ipw_net_stop(struct net_device *dev)
7075 {
7076         IPW_DEBUG_INFO("dev->close\n");
7077         netif_stop_queue(dev);
7078         return 0;
7079 }
7080
7081 /*
7082 todo:
7083
7084 modify to send one tfd per fragment instead of using chunking.  otherwise
7085 we need to heavily modify the ieee80211_skb_to_txb.
7086 */
7087
7088 static inline void ipw_tx_skb(struct ipw_priv *priv, struct ieee80211_txb *txb)
7089 {
7090         struct ieee80211_hdr_3addr *hdr = (struct ieee80211_hdr_3addr *)
7091             txb->fragments[0]->data;
7092         int i = 0;
7093         struct tfd_frame *tfd;
7094         struct clx2_tx_queue *txq = &priv->txq[0];
7095         struct clx2_queue *q = &txq->q;
7096         u8 id, hdr_len, unicast;
7097         u16 remaining_bytes;
7098
7099         switch (priv->ieee->iw_mode) {
7100         case IW_MODE_ADHOC:
7101                 hdr_len = IEEE80211_3ADDR_LEN;
7102                 unicast = !is_broadcast_ether_addr(hdr->addr1) &&
7103                     !is_multicast_ether_addr(hdr->addr1);
7104                 id = ipw_find_station(priv, hdr->addr1);
7105                 if (id == IPW_INVALID_STATION) {
7106                         id = ipw_add_station(priv, hdr->addr1);
7107                         if (id == IPW_INVALID_STATION) {
7108                                 IPW_WARNING("Attempt to send data to "
7109                                             "invalid cell: " MAC_FMT "\n",
7110                                             MAC_ARG(hdr->addr1));
7111                                 goto drop;
7112                         }
7113                 }
7114                 break;
7115
7116         case IW_MODE_INFRA:
7117         default:
7118                 unicast = !is_broadcast_ether_addr(hdr->addr3) &&
7119                     !is_multicast_ether_addr(hdr->addr3);
7120                 hdr_len = IEEE80211_3ADDR_LEN;
7121                 id = 0;
7122                 break;
7123         }
7124
7125         tfd = &txq->bd[q->first_empty];
7126         txq->txb[q->first_empty] = txb;
7127         memset(tfd, 0, sizeof(*tfd));
7128         tfd->u.data.station_number = id;
7129
7130         tfd->control_flags.message_type = TX_FRAME_TYPE;
7131         tfd->control_flags.control_bits = TFD_NEED_IRQ_MASK;
7132
7133         tfd->u.data.cmd_id = DINO_CMD_TX;
7134         tfd->u.data.len = txb->payload_size;
7135         remaining_bytes = txb->payload_size;
7136         if (unlikely(!unicast))
7137                 tfd->u.data.tx_flags = DCT_FLAG_NO_WEP;
7138         else
7139                 tfd->u.data.tx_flags = DCT_FLAG_NO_WEP | DCT_FLAG_ACK_REQD;
7140
7141         if (priv->assoc_request.ieee_mode == IPW_B_MODE)
7142                 tfd->u.data.tx_flags_ext = DCT_FLAG_EXT_MODE_CCK;
7143         else
7144                 tfd->u.data.tx_flags_ext = DCT_FLAG_EXT_MODE_OFDM;
7145
7146         if (priv->assoc_request.preamble_length == DCT_FLAG_SHORT_PREAMBLE)
7147                 tfd->u.data.tx_flags |= DCT_FLAG_SHORT_PREAMBLE;
7148
7149         memcpy(&tfd->u.data.tfd.tfd_24.mchdr, hdr, hdr_len);
7150
7151         /* payload */
7152         tfd->u.data.num_chunks = min((u8) (NUM_TFD_CHUNKS - 2), txb->nr_frags);
7153         for (i = 0; i < tfd->u.data.num_chunks; i++) {
7154                 IPW_DEBUG_TX("Dumping TX packet frag %i of %i (%d bytes):\n",
7155                              i, tfd->u.data.num_chunks,
7156                              txb->fragments[i]->len - hdr_len);
7157                 printk_buf(IPW_DL_TX, txb->fragments[i]->data + hdr_len,
7158                            txb->fragments[i]->len - hdr_len);
7159
7160                 tfd->u.data.chunk_ptr[i] =
7161                     pci_map_single(priv->pci_dev,
7162                                    txb->fragments[i]->data + hdr_len,
7163                                    txb->fragments[i]->len - hdr_len,
7164                                    PCI_DMA_TODEVICE);
7165                 tfd->u.data.chunk_len[i] = txb->fragments[i]->len - hdr_len;
7166         }
7167
7168         if (i != txb->nr_frags) {
7169                 struct sk_buff *skb;
7170                 u16 remaining_bytes = 0;
7171                 int j;
7172
7173                 for (j = i; j < txb->nr_frags; j++)
7174                         remaining_bytes += txb->fragments[j]->len - hdr_len;
7175
7176                 printk(KERN_INFO "Trying to reallocate for %d bytes\n",
7177                        remaining_bytes);
7178                 skb = alloc_skb(remaining_bytes, GFP_ATOMIC);
7179                 if (skb != NULL) {
7180                         tfd->u.data.chunk_len[i] = remaining_bytes;
7181                         for (j = i; j < txb->nr_frags; j++) {
7182                                 int size = txb->fragments[j]->len - hdr_len;
7183                                 printk(KERN_INFO "Adding frag %d %d...\n",
7184                                        j, size);
7185                                 memcpy(skb_put(skb, size),
7186                                        txb->fragments[j]->data + hdr_len, size);
7187                         }
7188                         dev_kfree_skb_any(txb->fragments[i]);
7189                         txb->fragments[i] = skb;
7190                         tfd->u.data.chunk_ptr[i] =
7191                             pci_map_single(priv->pci_dev, skb->data,
7192                                            tfd->u.data.chunk_len[i],
7193                                            PCI_DMA_TODEVICE);
7194                         tfd->u.data.num_chunks++;
7195                 }
7196         }
7197
7198         /* kick DMA */
7199         q->first_empty = ipw_queue_inc_wrap(q->first_empty, q->n_bd);
7200         ipw_write32(priv, q->reg_w, q->first_empty);
7201
7202         if (ipw_queue_space(q) < q->high_mark)
7203                 netif_stop_queue(priv->net_dev);
7204
7205         return;
7206
7207       drop:
7208         IPW_DEBUG_DROP("Silently dropping Tx packet.\n");
7209         ieee80211_txb_free(txb);
7210 }
7211
7212 static int ipw_net_hard_start_xmit(struct ieee80211_txb *txb,
7213                                    struct net_device *dev, int pri)
7214 {
7215         struct ipw_priv *priv = ieee80211_priv(dev);
7216         unsigned long flags;
7217
7218         IPW_DEBUG_TX("dev->xmit(%d bytes)\n", txb->payload_size);
7219
7220         spin_lock_irqsave(&priv->lock, flags);
7221
7222         if (!(priv->status & STATUS_ASSOCIATED)) {
7223                 IPW_DEBUG_INFO("Tx attempt while not associated.\n");
7224                 priv->ieee->stats.tx_carrier_errors++;
7225                 netif_stop_queue(dev);
7226                 goto fail_unlock;
7227         }
7228
7229         ipw_tx_skb(priv, txb);
7230
7231         spin_unlock_irqrestore(&priv->lock, flags);
7232         return 0;
7233
7234       fail_unlock:
7235         spin_unlock_irqrestore(&priv->lock, flags);
7236         return 1;
7237 }
7238
7239 static struct net_device_stats *ipw_net_get_stats(struct net_device *dev)
7240 {
7241         struct ipw_priv *priv = ieee80211_priv(dev);
7242
7243         priv->ieee->stats.tx_packets = priv->tx_packets;
7244         priv->ieee->stats.rx_packets = priv->rx_packets;
7245         return &priv->ieee->stats;
7246 }
7247
7248 static void ipw_net_set_multicast_list(struct net_device *dev)
7249 {
7250
7251 }
7252
7253 static int ipw_net_set_mac_address(struct net_device *dev, void *p)
7254 {
7255         struct ipw_priv *priv = ieee80211_priv(dev);
7256         struct sockaddr *addr = p;
7257         if (!is_valid_ether_addr(addr->sa_data))
7258                 return -EADDRNOTAVAIL;
7259         priv->config |= CFG_CUSTOM_MAC;
7260         memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
7261         printk(KERN_INFO "%s: Setting MAC to " MAC_FMT "\n",
7262                priv->net_dev->name, MAC_ARG(priv->mac_addr));
7263         ipw_adapter_restart(priv);
7264         return 0;
7265 }
7266
7267 static void ipw_ethtool_get_drvinfo(struct net_device *dev,
7268                                     struct ethtool_drvinfo *info)
7269 {
7270         struct ipw_priv *p = ieee80211_priv(dev);
7271         char vers[64];
7272         char date[32];
7273         u32 len;
7274
7275         strcpy(info->driver, DRV_NAME);
7276         strcpy(info->version, DRV_VERSION);
7277
7278         len = sizeof(vers);
7279         ipw_get_ordinal(p, IPW_ORD_STAT_FW_VERSION, vers, &len);
7280         len = sizeof(date);
7281         ipw_get_ordinal(p, IPW_ORD_STAT_FW_DATE, date, &len);
7282
7283         snprintf(info->fw_version, sizeof(info->fw_version), "%s (%s)",
7284                  vers, date);
7285         strcpy(info->bus_info, pci_name(p->pci_dev));
7286         info->eedump_len = CX2_EEPROM_IMAGE_SIZE;
7287 }
7288
7289 static u32 ipw_ethtool_get_link(struct net_device *dev)
7290 {
7291         struct ipw_priv *priv = ieee80211_priv(dev);
7292         return (priv->status & STATUS_ASSOCIATED) != 0;
7293 }
7294
7295 static int ipw_ethtool_get_eeprom_len(struct net_device *dev)
7296 {
7297         return CX2_EEPROM_IMAGE_SIZE;
7298 }
7299
7300 static int ipw_ethtool_get_eeprom(struct net_device *dev,
7301                                   struct ethtool_eeprom *eeprom, u8 * bytes)
7302 {
7303         struct ipw_priv *p = ieee80211_priv(dev);
7304
7305         if (eeprom->offset + eeprom->len > CX2_EEPROM_IMAGE_SIZE)
7306                 return -EINVAL;
7307
7308         memcpy(bytes, &((u8 *) p->eeprom)[eeprom->offset], eeprom->len);
7309         return 0;
7310 }
7311
7312 static int ipw_ethtool_set_eeprom(struct net_device *dev,
7313                                   struct ethtool_eeprom *eeprom, u8 * bytes)
7314 {
7315         struct ipw_priv *p = ieee80211_priv(dev);
7316         int i;
7317
7318         if (eeprom->offset + eeprom->len > CX2_EEPROM_IMAGE_SIZE)
7319                 return -EINVAL;
7320
7321         memcpy(&((u8 *) p->eeprom)[eeprom->offset], bytes, eeprom->len);
7322         for (i = IPW_EEPROM_DATA;
7323              i < IPW_EEPROM_DATA + CX2_EEPROM_IMAGE_SIZE; i++)
7324                 ipw_write8(p, i, p->eeprom[i]);
7325
7326         return 0;
7327 }
7328
7329 static struct ethtool_ops ipw_ethtool_ops = {
7330         .get_link = ipw_ethtool_get_link,
7331         .get_drvinfo = ipw_ethtool_get_drvinfo,
7332         .get_eeprom_len = ipw_ethtool_get_eeprom_len,
7333         .get_eeprom = ipw_ethtool_get_eeprom,
7334         .set_eeprom = ipw_ethtool_set_eeprom,
7335 };
7336
7337 static irqreturn_t ipw_isr(int irq, void *data, struct pt_regs *regs)
7338 {
7339         struct ipw_priv *priv = data;
7340         u32 inta, inta_mask;
7341
7342         if (!priv)
7343                 return IRQ_NONE;
7344
7345         spin_lock(&priv->lock);
7346
7347         if (!(priv->status & STATUS_INT_ENABLED)) {
7348                 /* Shared IRQ */
7349                 goto none;
7350         }
7351
7352         inta = ipw_read32(priv, CX2_INTA_RW);
7353         inta_mask = ipw_read32(priv, CX2_INTA_MASK_R);
7354
7355         if (inta == 0xFFFFFFFF) {
7356                 /* Hardware disappeared */
7357                 IPW_WARNING("IRQ INTA == 0xFFFFFFFF\n");
7358                 goto none;
7359         }
7360
7361         if (!(inta & (CX2_INTA_MASK_ALL & inta_mask))) {
7362                 /* Shared interrupt */
7363                 goto none;
7364         }
7365
7366         /* tell the device to stop sending interrupts */
7367         ipw_disable_interrupts(priv);
7368
7369         /* ack current interrupts */
7370         inta &= (CX2_INTA_MASK_ALL & inta_mask);
7371         ipw_write32(priv, CX2_INTA_RW, inta);
7372
7373         /* Cache INTA value for our tasklet */
7374         priv->isr_inta = inta;
7375
7376         tasklet_schedule(&priv->irq_tasklet);
7377
7378         spin_unlock(&priv->lock);
7379
7380         return IRQ_HANDLED;
7381       none:
7382         spin_unlock(&priv->lock);
7383         return IRQ_NONE;
7384 }
7385
7386 static void ipw_rf_kill(void *adapter)
7387 {
7388         struct ipw_priv *priv = adapter;
7389         unsigned long flags;
7390
7391         spin_lock_irqsave(&priv->lock, flags);
7392
7393         if (rf_kill_active(priv)) {
7394                 IPW_DEBUG_RF_KILL("RF Kill active, rescheduling GPIO check\n");
7395                 if (priv->workqueue)
7396                         queue_delayed_work(priv->workqueue,
7397                                            &priv->rf_kill, 2 * HZ);
7398                 goto exit_unlock;
7399         }
7400
7401         /* RF Kill is now disabled, so bring the device back up */
7402
7403         if (!(priv->status & STATUS_RF_KILL_MASK)) {
7404                 IPW_DEBUG_RF_KILL("HW RF Kill no longer active, restarting "
7405                                   "device\n");
7406
7407                 /* we can not do an adapter restart while inside an irq lock */
7408                 queue_work(priv->workqueue, &priv->adapter_restart);
7409         } else
7410                 IPW_DEBUG_RF_KILL("HW RF Kill deactivated.  SW RF Kill still "
7411                                   "enabled\n");
7412
7413       exit_unlock:
7414         spin_unlock_irqrestore(&priv->lock, flags);
7415 }
7416
7417 static int ipw_setup_deferred_work(struct ipw_priv *priv)
7418 {
7419         int ret = 0;
7420
7421         priv->workqueue = create_workqueue(DRV_NAME);
7422         init_waitqueue_head(&priv->wait_command_queue);
7423
7424         INIT_WORK(&priv->adhoc_check, ipw_adhoc_check, priv);
7425         INIT_WORK(&priv->associate, ipw_associate, priv);
7426         INIT_WORK(&priv->disassociate, ipw_disassociate, priv);
7427         INIT_WORK(&priv->rx_replenish, ipw_rx_queue_replenish, priv);
7428         INIT_WORK(&priv->adapter_restart, ipw_adapter_restart, priv);
7429         INIT_WORK(&priv->rf_kill, ipw_rf_kill, priv);
7430         INIT_WORK(&priv->up, (void (*)(void *))ipw_up, priv);
7431         INIT_WORK(&priv->down, (void (*)(void *))ipw_down, priv);
7432         INIT_WORK(&priv->request_scan,
7433                   (void (*)(void *))ipw_request_scan, priv);
7434         INIT_WORK(&priv->gather_stats,
7435                   (void (*)(void *))ipw_gather_stats, priv);
7436         INIT_WORK(&priv->abort_scan, (void (*)(void *))ipw_abort_scan, priv);
7437         INIT_WORK(&priv->roam, ipw_roam, priv);
7438         INIT_WORK(&priv->scan_check, ipw_scan_check, priv);
7439
7440         tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
7441                      ipw_irq_tasklet, (unsigned long)priv);
7442
7443         return ret;
7444 }
7445
7446 static void shim__set_security(struct net_device *dev,
7447                                struct ieee80211_security *sec)
7448 {
7449         struct ipw_priv *priv = ieee80211_priv(dev);
7450         int i;
7451
7452         for (i = 0; i < 4; i++) {
7453                 if (sec->flags & (1 << i)) {
7454                         priv->sec.key_sizes[i] = sec->key_sizes[i];
7455                         if (sec->key_sizes[i] == 0)
7456                                 priv->sec.flags &= ~(1 << i);
7457                         else
7458                                 memcpy(priv->sec.keys[i], sec->keys[i],
7459                                        sec->key_sizes[i]);
7460                         priv->sec.flags |= (1 << i);
7461                         priv->status |= STATUS_SECURITY_UPDATED;
7462                 }
7463         }
7464
7465         if ((sec->flags & SEC_ACTIVE_KEY) &&
7466             priv->sec.active_key != sec->active_key) {
7467                 if (sec->active_key <= 3) {
7468                         priv->sec.active_key = sec->active_key;
7469                         priv->sec.flags |= SEC_ACTIVE_KEY;
7470                 } else
7471                         priv->sec.flags &= ~SEC_ACTIVE_KEY;
7472                 priv->status |= STATUS_SECURITY_UPDATED;
7473         }
7474
7475         if ((sec->flags & SEC_AUTH_MODE) &&
7476             (priv->sec.auth_mode != sec->auth_mode)) {
7477                 priv->sec.auth_mode = sec->auth_mode;
7478                 priv->sec.flags |= SEC_AUTH_MODE;
7479                 if (sec->auth_mode == WLAN_AUTH_SHARED_KEY)
7480                         priv->capability |= CAP_SHARED_KEY;
7481                 else
7482                         priv->capability &= ~CAP_SHARED_KEY;
7483                 priv->status |= STATUS_SECURITY_UPDATED;
7484         }
7485
7486         if (sec->flags & SEC_ENABLED && priv->sec.enabled != sec->enabled) {
7487                 priv->sec.flags |= SEC_ENABLED;
7488                 priv->sec.enabled = sec->enabled;
7489                 priv->status |= STATUS_SECURITY_UPDATED;
7490                 if (sec->enabled)
7491                         priv->capability |= CAP_PRIVACY_ON;
7492                 else
7493                         priv->capability &= ~CAP_PRIVACY_ON;
7494         }
7495
7496         if (sec->flags & SEC_LEVEL && priv->sec.level != sec->level) {
7497                 priv->sec.level = sec->level;
7498                 priv->sec.flags |= SEC_LEVEL;
7499                 priv->status |= STATUS_SECURITY_UPDATED;
7500         }
7501
7502         /* To match current functionality of ipw2100 (which works well w/
7503          * various supplicants, we don't force a disassociate if the
7504          * privacy capability changes ... */
7505 #if 0
7506         if ((priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) &&
7507             (((priv->assoc_request.capability &
7508                WLAN_CAPABILITY_PRIVACY) && !sec->enabled) ||
7509              (!(priv->assoc_request.capability &
7510                 WLAN_CAPABILITY_PRIVACY) && sec->enabled))) {
7511                 IPW_DEBUG_ASSOC("Disassociating due to capability "
7512                                 "change.\n");
7513                 ipw_disassociate(priv);
7514         }
7515 #endif
7516 }
7517
7518 static int init_supported_rates(struct ipw_priv *priv,
7519                                 struct ipw_supported_rates *rates)
7520 {
7521         /* TODO: Mask out rates based on priv->rates_mask */
7522
7523         memset(rates, 0, sizeof(*rates));
7524         /* configure supported rates */
7525         switch (priv->ieee->freq_band) {
7526         case IEEE80211_52GHZ_BAND:
7527                 rates->ieee_mode = IPW_A_MODE;
7528                 rates->purpose = IPW_RATE_CAPABILITIES;
7529                 ipw_add_ofdm_scan_rates(rates, IEEE80211_CCK_MODULATION,
7530                                         IEEE80211_OFDM_DEFAULT_RATES_MASK);
7531                 break;
7532
7533         default:                /* Mixed or 2.4Ghz */
7534                 rates->ieee_mode = IPW_G_MODE;
7535                 rates->purpose = IPW_RATE_CAPABILITIES;
7536                 ipw_add_cck_scan_rates(rates, IEEE80211_CCK_MODULATION,
7537                                        IEEE80211_CCK_DEFAULT_RATES_MASK);
7538                 if (priv->ieee->modulation & IEEE80211_OFDM_MODULATION) {
7539                         ipw_add_ofdm_scan_rates(rates, IEEE80211_CCK_MODULATION,
7540                                                 IEEE80211_OFDM_DEFAULT_RATES_MASK);
7541                 }
7542                 break;
7543         }
7544
7545         return 0;
7546 }
7547
7548 static int ipw_config(struct ipw_priv *priv)
7549 {
7550         int i;
7551         struct ipw_tx_power tx_power;
7552
7553         memset(&priv->sys_config, 0, sizeof(priv->sys_config));
7554         memset(&tx_power, 0, sizeof(tx_power));
7555
7556         /* This is only called from ipw_up, which resets/reloads the firmware
7557            so, we don't need to first disable the card before we configure
7558            it */
7559
7560         /* configure device for 'G' band */
7561         tx_power.ieee_mode = IPW_G_MODE;
7562         tx_power.num_channels = 11;
7563         for (i = 0; i < 11; i++) {
7564                 tx_power.channels_tx_power[i].channel_number = i + 1;
7565                 tx_power.channels_tx_power[i].tx_power = priv->tx_power;
7566         }
7567         if (ipw_send_tx_power(priv, &tx_power))
7568                 goto error;
7569
7570         /* configure device to also handle 'B' band */
7571         tx_power.ieee_mode = IPW_B_MODE;
7572         if (ipw_send_tx_power(priv, &tx_power))
7573                 goto error;
7574
7575         /* initialize adapter address */
7576         if (ipw_send_adapter_address(priv, priv->net_dev->dev_addr))
7577                 goto error;
7578
7579         /* set basic system config settings */
7580         init_sys_config(&priv->sys_config);
7581         if (ipw_send_system_config(priv, &priv->sys_config))
7582                 goto error;
7583
7584         init_supported_rates(priv, &priv->rates);
7585         if (ipw_send_supported_rates(priv, &priv->rates))
7586                 goto error;
7587
7588         /* Set request-to-send threshold */
7589         if (priv->rts_threshold) {
7590                 if (ipw_send_rts_threshold(priv, priv->rts_threshold))
7591                         goto error;
7592         }
7593
7594         if (ipw_set_random_seed(priv))
7595                 goto error;
7596
7597         /* final state transition to the RUN state */
7598         if (ipw_send_host_complete(priv))
7599                 goto error;
7600
7601         /* If configured to try and auto-associate, kick off a scan */
7602         if ((priv->config & CFG_ASSOCIATE) && ipw_request_scan(priv))
7603                 goto error;
7604
7605         return 0;
7606
7607       error:
7608         return -EIO;
7609 }
7610
7611 #define MAX_HW_RESTARTS 5
7612 static int ipw_up(struct ipw_priv *priv)
7613 {
7614         int rc, i;
7615
7616         if (priv->status & STATUS_EXIT_PENDING)
7617                 return -EIO;
7618
7619         for (i = 0; i < MAX_HW_RESTARTS; i++) {
7620                 /* Load the microcode, firmware, and eeprom.
7621                  * Also start the clocks. */
7622                 rc = ipw_load(priv);
7623                 if (rc) {
7624                         IPW_ERROR("Unable to load firmware: 0x%08X\n", rc);
7625                         return rc;
7626                 }
7627
7628                 ipw_init_ordinals(priv);
7629                 if (!(priv->config & CFG_CUSTOM_MAC))
7630                         eeprom_parse_mac(priv, priv->mac_addr);
7631                 memcpy(priv->net_dev->dev_addr, priv->mac_addr, ETH_ALEN);
7632
7633                 if (priv->status & STATUS_RF_KILL_MASK)
7634                         return 0;
7635
7636                 rc = ipw_config(priv);
7637                 if (!rc) {
7638                         IPW_DEBUG_INFO("Configured device on count %i\n", i);
7639                         priv->notif_missed_beacons = 0;
7640                         netif_start_queue(priv->net_dev);
7641                         return 0;
7642                 } else {
7643                         IPW_DEBUG_INFO("Device configuration failed: 0x%08X\n",
7644                                        rc);
7645                 }
7646
7647                 IPW_DEBUG_INFO("Failed to config device on retry %d of %d\n",
7648                                i, MAX_HW_RESTARTS);
7649
7650                 /* We had an error bringing up the hardware, so take it
7651                  * all the way back down so we can try again */
7652                 ipw_down(priv);
7653         }
7654
7655         /* tried to restart and config the device for as long as our
7656          * patience could withstand */
7657         IPW_ERROR("Unable to initialize device after %d attempts.\n", i);
7658         return -EIO;
7659 }
7660
7661 static void ipw_down(struct ipw_priv *priv)
7662 {
7663         /* Attempt to disable the card */
7664 #if 0
7665         ipw_send_card_disable(priv, 0);
7666 #endif
7667
7668         /* tell the device to stop sending interrupts */
7669         ipw_disable_interrupts(priv);
7670
7671         /* Clear all bits but the RF Kill */
7672         priv->status &= STATUS_RF_KILL_MASK;
7673
7674         netif_carrier_off(priv->net_dev);
7675         netif_stop_queue(priv->net_dev);
7676
7677         ipw_stop_nic(priv);
7678 }
7679
7680 static int ipw_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
7681 {
7682 #ifdef CONFIG_IEEE80211_WPA
7683         struct iwreq *wrq = (struct iwreq *)rq;
7684         int ret = -1;
7685         switch (cmd) {
7686         case IPW_IOCTL_WPA_SUPPLICANT:
7687                 ret = ipw_wpa_supplicant(dev, &wrq->u.data);
7688                 return ret;
7689
7690         default:
7691                 return -EOPNOTSUPP;
7692         }
7693
7694 #endif                          /* CONFIG_IEEE80211_WPA */
7695
7696         return -EOPNOTSUPP;
7697 }
7698
7699 /* Called by register_netdev() */
7700 static int ipw_net_init(struct net_device *dev)
7701 {
7702         struct ipw_priv *priv = ieee80211_priv(dev);
7703
7704         if (priv->status & STATUS_RF_KILL_SW) {
7705                 IPW_WARNING("Radio disabled by module parameter.\n");
7706                 return 0;
7707         } else if (rf_kill_active(priv)) {
7708                 IPW_WARNING("Radio Frequency Kill Switch is On:\n"
7709                             "Kill switch must be turned off for "
7710                             "wireless networking to work.\n");
7711                 queue_delayed_work(priv->workqueue, &priv->rf_kill, 2 * HZ);
7712                 return 0;
7713         }
7714
7715         if (ipw_up(priv))
7716                 return -EIO;
7717
7718         return 0;
7719 }
7720
7721 /* PCI driver stuff */
7722 static struct pci_device_id card_ids[] = {
7723         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2701, 0, 0, 0},
7724         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2702, 0, 0, 0},
7725         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2711, 0, 0, 0},
7726         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2712, 0, 0, 0},
7727         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2721, 0, 0, 0},
7728         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2722, 0, 0, 0},
7729         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2731, 0, 0, 0},
7730         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2732, 0, 0, 0},
7731         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2741, 0, 0, 0},
7732         {PCI_VENDOR_ID_INTEL, 0x1043, 0x103c, 0x2741, 0, 0, 0},
7733         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2742, 0, 0, 0},
7734         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2751, 0, 0, 0},
7735         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2752, 0, 0, 0},
7736         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2753, 0, 0, 0},
7737         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2754, 0, 0, 0},
7738         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2761, 0, 0, 0},
7739         {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2762, 0, 0, 0},
7740         {PCI_VENDOR_ID_INTEL, 0x104f, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
7741         {PCI_VENDOR_ID_INTEL, 0x4220, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, /* BG */
7742         {PCI_VENDOR_ID_INTEL, 0x4221, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, /* 2225BG */
7743         {PCI_VENDOR_ID_INTEL, 0x4223, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, /* ABG */
7744         {PCI_VENDOR_ID_INTEL, 0x4224, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, /* ABG */
7745
7746         /* required last entry */
7747         {0,}
7748 };
7749
7750 MODULE_DEVICE_TABLE(pci, card_ids);
7751
7752 static struct attribute *ipw_sysfs_entries[] = {
7753         &dev_attr_rf_kill.attr,
7754         &dev_attr_direct_dword.attr,
7755         &dev_attr_indirect_byte.attr,
7756         &dev_attr_indirect_dword.attr,
7757         &dev_attr_mem_gpio_reg.attr,
7758         &dev_attr_command_event_reg.attr,
7759         &dev_attr_nic_type.attr,
7760         &dev_attr_status.attr,
7761         &dev_attr_cfg.attr,
7762         &dev_attr_dump_errors.attr,
7763         &dev_attr_dump_events.attr,
7764         &dev_attr_eeprom_delay.attr,
7765         &dev_attr_ucode_version.attr,
7766         &dev_attr_rtc.attr,
7767         NULL
7768 };
7769
7770 static struct attribute_group ipw_attribute_group = {
7771         .name = NULL,           /* put in device directory */
7772         .attrs = ipw_sysfs_entries,
7773 };
7774
7775 static int ipw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
7776 {
7777         int err = 0;
7778         struct net_device *net_dev;
7779         void __iomem *base;
7780         u32 length, val;
7781         struct ipw_priv *priv;
7782         int band, modulation;
7783
7784         net_dev = alloc_ieee80211(sizeof(struct ipw_priv));
7785         if (net_dev == NULL) {
7786                 err = -ENOMEM;
7787                 goto out;
7788         }
7789
7790         priv = ieee80211_priv(net_dev);
7791         priv->ieee = netdev_priv(net_dev);
7792         priv->net_dev = net_dev;
7793         priv->pci_dev = pdev;
7794 #ifdef CONFIG_IPW_DEBUG
7795         ipw_debug_level = debug;
7796 #endif
7797         spin_lock_init(&priv->lock);
7798
7799         if (pci_enable_device(pdev)) {
7800                 err = -ENODEV;
7801                 goto out_free_ieee80211;
7802         }
7803
7804         pci_set_master(pdev);
7805
7806         err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7807         if (!err)
7808                 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7809         if (err) {
7810                 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
7811                 goto out_pci_disable_device;
7812         }
7813
7814         pci_set_drvdata(pdev, priv);
7815
7816         err = pci_request_regions(pdev, DRV_NAME);
7817         if (err)
7818                 goto out_pci_disable_device;
7819
7820         /* We disable the RETRY_TIMEOUT register (0x41) to keep
7821          * PCI Tx retries from interfering with C3 CPU state */
7822         pci_read_config_dword(pdev, 0x40, &val);
7823         if ((val & 0x0000ff00) != 0)
7824                 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
7825
7826         length = pci_resource_len(pdev, 0);
7827         priv->hw_len = length;
7828
7829         base = ioremap_nocache(pci_resource_start(pdev, 0), length);
7830         if (!base) {
7831                 err = -ENODEV;
7832                 goto out_pci_release_regions;
7833         }
7834
7835         priv->hw_base = base;
7836         IPW_DEBUG_INFO("pci_resource_len = 0x%08x\n", length);
7837         IPW_DEBUG_INFO("pci_resource_base = %p\n", base);
7838
7839         err = ipw_setup_deferred_work(priv);
7840         if (err) {
7841                 IPW_ERROR("Unable to setup deferred work\n");
7842                 goto out_iounmap;
7843         }
7844
7845         /* Initialize module parameter values here */
7846         if (associate)
7847                 priv->config |= CFG_ASSOCIATE;
7848         else
7849                 IPW_DEBUG_INFO("Auto associate disabled.\n");
7850
7851         if (auto_create)
7852                 priv->config |= CFG_ADHOC_CREATE;
7853         else
7854                 IPW_DEBUG_INFO("Auto adhoc creation disabled.\n");
7855
7856         if (disable) {
7857                 priv->status |= STATUS_RF_KILL_SW;
7858                 IPW_DEBUG_INFO("Radio disabled.\n");
7859         }
7860
7861         if (channel != 0) {
7862                 priv->config |= CFG_STATIC_CHANNEL;
7863                 priv->channel = channel;
7864                 IPW_DEBUG_INFO("Bind to static channel %d\n", channel);
7865                 IPW_DEBUG_INFO("Bind to static channel %d\n", channel);
7866                 /* TODO: Validate that provided channel is in range */
7867         }
7868
7869         switch (mode) {
7870         case 1:
7871                 priv->ieee->iw_mode = IW_MODE_ADHOC;
7872                 break;
7873 #ifdef CONFIG_IPW_MONITOR
7874         case 2:
7875                 priv->ieee->iw_mode = IW_MODE_MONITOR;
7876                 break;
7877 #endif
7878         default:
7879         case 0:
7880                 priv->ieee->iw_mode = IW_MODE_INFRA;
7881                 break;
7882         }
7883
7884         if ((priv->pci_dev->device == 0x4223) ||
7885             (priv->pci_dev->device == 0x4224)) {
7886                 printk(KERN_INFO DRV_NAME
7887                        ": Detected Intel PRO/Wireless 2915ABG Network "
7888                        "Connection\n");
7889                 priv->ieee->abg_true = 1;
7890                 band = IEEE80211_52GHZ_BAND | IEEE80211_24GHZ_BAND;
7891                 modulation = IEEE80211_OFDM_MODULATION |
7892                     IEEE80211_CCK_MODULATION;
7893                 priv->adapter = IPW_2915ABG;
7894                 priv->ieee->mode = IEEE_A | IEEE_G | IEEE_B;
7895         } else {
7896                 if (priv->pci_dev->device == 0x4221)
7897                         printk(KERN_INFO DRV_NAME
7898                                ": Detected Intel PRO/Wireless 2225BG Network "
7899                                "Connection\n");
7900                 else
7901                         printk(KERN_INFO DRV_NAME
7902                                ": Detected Intel PRO/Wireless 2200BG Network "
7903                                "Connection\n");
7904
7905                 priv->ieee->abg_true = 0;
7906                 band = IEEE80211_24GHZ_BAND;
7907                 modulation = IEEE80211_OFDM_MODULATION |
7908                     IEEE80211_CCK_MODULATION;
7909                 priv->adapter = IPW_2200BG;
7910                 priv->ieee->mode = IEEE_G | IEEE_B;
7911         }
7912
7913         priv->ieee->freq_band = band;
7914         priv->ieee->modulation = modulation;
7915
7916         priv->rates_mask = IEEE80211_DEFAULT_RATES_MASK;
7917
7918         priv->missed_beacon_threshold = IPW_MB_DISASSOCIATE_THRESHOLD_DEFAULT;
7919         priv->roaming_threshold = IPW_MB_ROAMING_THRESHOLD_DEFAULT;
7920
7921         priv->rts_threshold = DEFAULT_RTS_THRESHOLD;
7922
7923         /* If power management is turned on, default to AC mode */
7924         priv->power_mode = IPW_POWER_AC;
7925         priv->tx_power = IPW_DEFAULT_TX_POWER;
7926
7927         err = request_irq(pdev->irq, ipw_isr, SA_SHIRQ, DRV_NAME, priv);
7928         if (err) {
7929                 IPW_ERROR("Error allocating IRQ %d\n", pdev->irq);
7930                 goto out_destroy_workqueue;
7931         }
7932
7933         SET_MODULE_OWNER(net_dev);
7934         SET_NETDEV_DEV(net_dev, &pdev->dev);
7935
7936         priv->ieee->hard_start_xmit = ipw_net_hard_start_xmit;
7937         priv->ieee->set_security = shim__set_security;
7938
7939         net_dev->open = ipw_net_open;
7940         net_dev->stop = ipw_net_stop;
7941         net_dev->init = ipw_net_init;
7942         net_dev->do_ioctl = ipw_ioctl;
7943         net_dev->get_stats = ipw_net_get_stats;
7944         net_dev->set_multicast_list = ipw_net_set_multicast_list;
7945         net_dev->set_mac_address = ipw_net_set_mac_address;
7946         net_dev->get_wireless_stats = ipw_get_wireless_stats;
7947         net_dev->wireless_handlers = &ipw_wx_handler_def;
7948         net_dev->ethtool_ops = &ipw_ethtool_ops;
7949         net_dev->irq = pdev->irq;
7950         net_dev->base_addr = (unsigned long)priv->hw_base;
7951         net_dev->mem_start = pci_resource_start(pdev, 0);
7952         net_dev->mem_end = net_dev->mem_start + pci_resource_len(pdev, 0) - 1;
7953
7954         err = sysfs_create_group(&pdev->dev.kobj, &ipw_attribute_group);
7955         if (err) {
7956                 IPW_ERROR("failed to create sysfs device attributes\n");
7957                 goto out_release_irq;
7958         }
7959
7960         err = register_netdev(net_dev);
7961         if (err) {
7962                 IPW_ERROR("failed to register network device\n");
7963                 goto out_remove_group;
7964         }
7965
7966         return 0;
7967
7968       out_remove_group:
7969         sysfs_remove_group(&pdev->dev.kobj, &ipw_attribute_group);
7970       out_release_irq:
7971         free_irq(pdev->irq, priv);
7972       out_destroy_workqueue:
7973         destroy_workqueue(priv->workqueue);
7974         priv->workqueue = NULL;
7975       out_iounmap:
7976         iounmap(priv->hw_base);
7977       out_pci_release_regions:
7978         pci_release_regions(pdev);
7979       out_pci_disable_device:
7980         pci_disable_device(pdev);
7981         pci_set_drvdata(pdev, NULL);
7982       out_free_ieee80211:
7983         free_ieee80211(priv->net_dev);
7984       out:
7985         return err;
7986 }
7987
7988 static void ipw_pci_remove(struct pci_dev *pdev)
7989 {
7990         struct ipw_priv *priv = pci_get_drvdata(pdev);
7991         if (!priv)
7992                 return;
7993
7994         priv->status |= STATUS_EXIT_PENDING;
7995
7996         sysfs_remove_group(&pdev->dev.kobj, &ipw_attribute_group);
7997
7998         ipw_down(priv);
7999
8000         unregister_netdev(priv->net_dev);
8001
8002         if (priv->rxq) {
8003                 ipw_rx_queue_free(priv, priv->rxq);
8004                 priv->rxq = NULL;
8005         }
8006         ipw_tx_queue_free(priv);
8007
8008         /* ipw_down will ensure that there is no more pending work
8009          * in the workqueue's, so we can safely remove them now. */
8010         if (priv->workqueue) {
8011                 cancel_delayed_work(&priv->adhoc_check);
8012                 cancel_delayed_work(&priv->gather_stats);
8013                 cancel_delayed_work(&priv->request_scan);
8014                 cancel_delayed_work(&priv->rf_kill);
8015                 cancel_delayed_work(&priv->scan_check);
8016                 destroy_workqueue(priv->workqueue);
8017                 priv->workqueue = NULL;
8018         }
8019
8020         free_irq(pdev->irq, priv);
8021         iounmap(priv->hw_base);
8022         pci_release_regions(pdev);
8023         pci_disable_device(pdev);
8024         pci_set_drvdata(pdev, NULL);
8025         free_ieee80211(priv->net_dev);
8026
8027 #ifdef CONFIG_PM
8028         if (fw_loaded) {
8029                 release_firmware(bootfw);
8030                 release_firmware(ucode);
8031                 release_firmware(firmware);
8032                 fw_loaded = 0;
8033         }
8034 #endif
8035 }
8036
8037 #ifdef CONFIG_PM
8038 static int ipw_pci_suspend(struct pci_dev *pdev, pm_message_t state)
8039 {
8040         struct ipw_priv *priv = pci_get_drvdata(pdev);
8041         struct net_device *dev = priv->net_dev;
8042
8043         printk(KERN_INFO "%s: Going into suspend...\n", dev->name);
8044
8045         /* Take down the device; powers it off, etc. */
8046         ipw_down(priv);
8047
8048         /* Remove the PRESENT state of the device */
8049         netif_device_detach(dev);
8050
8051         pci_save_state(pdev);
8052         pci_disable_device(pdev);
8053         pci_set_power_state(pdev, pci_choose_state(pdev, state));
8054
8055         return 0;
8056 }
8057
8058 static int ipw_pci_resume(struct pci_dev *pdev)
8059 {
8060         struct ipw_priv *priv = pci_get_drvdata(pdev);
8061         struct net_device *dev = priv->net_dev;
8062         u32 val;
8063
8064         printk(KERN_INFO "%s: Coming out of suspend...\n", dev->name);
8065
8066         pci_set_power_state(pdev, PCI_D0);
8067         pci_enable_device(pdev);
8068         pci_restore_state(pdev);
8069
8070         /*
8071          * Suspend/Resume resets the PCI configuration space, so we have to
8072          * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
8073          * from interfering with C3 CPU state. pci_restore_state won't help
8074          * here since it only restores the first 64 bytes pci config header.
8075          */
8076         pci_read_config_dword(pdev, 0x40, &val);
8077         if ((val & 0x0000ff00) != 0)
8078                 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
8079
8080         /* Set the device back into the PRESENT state; this will also wake
8081          * the queue of needed */
8082         netif_device_attach(dev);
8083
8084         /* Bring the device back up */
8085         queue_work(priv->workqueue, &priv->up);
8086
8087         return 0;
8088 }
8089 #endif
8090
8091 /* driver initialization stuff */
8092 static struct pci_driver ipw_driver = {
8093         .name = DRV_NAME,
8094         .id_table = card_ids,
8095         .probe = ipw_pci_probe,
8096         .remove = __devexit_p(ipw_pci_remove),
8097 #ifdef CONFIG_PM
8098         .suspend = ipw_pci_suspend,
8099         .resume = ipw_pci_resume,
8100 #endif
8101 };
8102
8103 static int __init ipw_init(void)
8104 {
8105         int ret;
8106
8107         printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8108         printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
8109
8110         ret = pci_module_init(&ipw_driver);
8111         if (ret) {
8112                 IPW_ERROR("Unable to initialize PCI module\n");
8113                 return ret;
8114         }
8115
8116         ret = driver_create_file(&ipw_driver.driver, &driver_attr_debug_level);
8117         if (ret) {
8118                 IPW_ERROR("Unable to create driver sysfs file\n");
8119                 pci_unregister_driver(&ipw_driver);
8120                 return ret;
8121         }
8122
8123         return ret;
8124 }
8125
8126 static void __exit ipw_exit(void)
8127 {
8128         driver_remove_file(&ipw_driver.driver, &driver_attr_debug_level);
8129         pci_unregister_driver(&ipw_driver);
8130 }
8131
8132 module_param(disable, int, 0444);
8133 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
8134
8135 module_param(associate, int, 0444);
8136 MODULE_PARM_DESC(associate, "auto associate when scanning (default on)");
8137
8138 module_param(auto_create, int, 0444);
8139 MODULE_PARM_DESC(auto_create, "auto create adhoc network (default on)");
8140
8141 module_param(debug, int, 0444);
8142 MODULE_PARM_DESC(debug, "debug output mask");
8143
8144 module_param(channel, int, 0444);
8145 MODULE_PARM_DESC(channel, "channel to limit associate to (default 0 [ANY])");
8146
8147 #ifdef CONFIG_IPW_MONITOR
8148 module_param(mode, int, 0444);
8149 MODULE_PARM_DESC(mode, "network mode (0=BSS,1=IBSS,2=Monitor)");
8150 #else
8151 module_param(mode, int, 0444);
8152 MODULE_PARM_DESC(mode, "network mode (0=BSS,1=IBSS)");
8153 #endif
8154
8155 module_exit(ipw_exit);
8156 module_init(ipw_init);