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