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