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