ipw2200.c 323.5 KB
Newer Older
J
James Ketrenos 已提交
1
/******************************************************************************
2

3
  Copyright(c) 2003 - 2006 Intel Corporation. All rights reserved.
J
James Ketrenos 已提交
4 5 6 7 8 9 10

  802.11 status code portion of this file from ethereal-0.10.6:
    Copyright 2000, Axis Communications AB
    Ethereal - Network traffic analyzer
    By Gerald Combs <gerald@ethereal.com>
    Copyright 1998 Gerald Combs

11 12
  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2 of the GNU General Public License as
J
James Ketrenos 已提交
13
  published by the Free Software Foundation.
14 15 16 17

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
J
James Ketrenos 已提交
18
  more details.
19

J
James Ketrenos 已提交
20
  You should have received a copy of the GNU General Public License along with
21
  this program; if not, write to the Free Software Foundation, Inc., 59
J
James Ketrenos 已提交
22
  Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23

J
James Ketrenos 已提交
24 25
  The full GNU General Public License is included in this distribution in the
  file called LICENSE.
26

J
James Ketrenos 已提交
27 28 29 30 31 32 33 34
  Contact Information:
  James P. Ketrenos <ipw2100-admin@linux.intel.com>
  Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497

******************************************************************************/

#include "ipw2200.h"

Z
Zhu Yi 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

#ifndef KBUILD_EXTMOD
#define VK "k"
#else
#define VK
#endif

#ifdef CONFIG_IPW2200_DEBUG
#define VD "d"
#else
#define VD
#endif

#ifdef CONFIG_IPW2200_MONITOR
#define VM "m"
#else
#define VM
#endif

#ifdef CONFIG_IPW2200_PROMISCUOUS
#define VP "p"
#else
#define VP
#endif

60
#ifdef CONFIG_IPW2200_RADIOTAP
Z
Zhu Yi 已提交
61 62 63 64 65 66 67 68 69 70 71
#define VR "r"
#else
#define VR
#endif

#ifdef CONFIG_IPW2200_QOS
#define VQ "q"
#else
#define VQ
#endif

72
#define IPW2200_VERSION "1.2.2" VK VD VM VP VR VQ
J
James Ketrenos 已提交
73
#define DRV_DESCRIPTION	"Intel(R) PRO/Wireless 2200/2915 Network Driver"
74
#define DRV_COPYRIGHT	"Copyright(c) 2003-2006 Intel Corporation"
J
James Ketrenos 已提交
75 76
#define DRV_VERSION     IPW2200_VERSION

77 78
#define ETH_P_80211_STATS (ETH_P_80211_RAW + 1)

J
James Ketrenos 已提交
79 80 81 82 83
MODULE_DESCRIPTION(DRV_DESCRIPTION);
MODULE_VERSION(DRV_VERSION);
MODULE_AUTHOR(DRV_COPYRIGHT);
MODULE_LICENSE("GPL");

84
static int cmdlog = 0;
J
James Ketrenos 已提交
85 86 87 88 89
static int debug = 0;
static int channel = 0;
static int mode = 0;

static u32 ipw_debug_level;
90
static int associate;
J
James Ketrenos 已提交
91
static int auto_create = 1;
92
static int led = 0;
J
James Ketrenos 已提交
93
static int disable = 0;
94
static int bt_coexist = 0;
95
static int hwcrypto = 0;
96
static int roaming = 1;
J
James Ketrenos 已提交
97 98 99
static const char ipw_modes[] = {
	'a', 'b', 'g', '?'
};
100
static int antenna = CFG_SYS_ANTENNA_BOTH;
J
James Ketrenos 已提交
101

102 103 104 105 106
#ifdef CONFIG_IPW2200_PROMISCUOUS
static int rtap_iface = 0;     /* def: 0 -- do not create rtap interface */
#endif


107
#ifdef CONFIG_IPW2200_QOS
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
static int qos_enable = 0;
static int qos_burst_enable = 0;
static int qos_no_ack_mask = 0;
static int burst_duration_CCK = 0;
static int burst_duration_OFDM = 0;

static struct ieee80211_qos_parameters def_qos_parameters_OFDM = {
	{QOS_TX0_CW_MIN_OFDM, QOS_TX1_CW_MIN_OFDM, QOS_TX2_CW_MIN_OFDM,
	 QOS_TX3_CW_MIN_OFDM},
	{QOS_TX0_CW_MAX_OFDM, QOS_TX1_CW_MAX_OFDM, QOS_TX2_CW_MAX_OFDM,
	 QOS_TX3_CW_MAX_OFDM},
	{QOS_TX0_AIFS, QOS_TX1_AIFS, QOS_TX2_AIFS, QOS_TX3_AIFS},
	{QOS_TX0_ACM, QOS_TX1_ACM, QOS_TX2_ACM, QOS_TX3_ACM},
	{QOS_TX0_TXOP_LIMIT_OFDM, QOS_TX1_TXOP_LIMIT_OFDM,
	 QOS_TX2_TXOP_LIMIT_OFDM, QOS_TX3_TXOP_LIMIT_OFDM}
};

static struct ieee80211_qos_parameters def_qos_parameters_CCK = {
	{QOS_TX0_CW_MIN_CCK, QOS_TX1_CW_MIN_CCK, QOS_TX2_CW_MIN_CCK,
	 QOS_TX3_CW_MIN_CCK},
	{QOS_TX0_CW_MAX_CCK, QOS_TX1_CW_MAX_CCK, QOS_TX2_CW_MAX_CCK,
	 QOS_TX3_CW_MAX_CCK},
	{QOS_TX0_AIFS, QOS_TX1_AIFS, QOS_TX2_AIFS, QOS_TX3_AIFS},
	{QOS_TX0_ACM, QOS_TX1_ACM, QOS_TX2_ACM, QOS_TX3_ACM},
	{QOS_TX0_TXOP_LIMIT_CCK, QOS_TX1_TXOP_LIMIT_CCK, QOS_TX2_TXOP_LIMIT_CCK,
	 QOS_TX3_TXOP_LIMIT_CCK}
};

static struct ieee80211_qos_parameters def_parameters_OFDM = {
	{DEF_TX0_CW_MIN_OFDM, DEF_TX1_CW_MIN_OFDM, DEF_TX2_CW_MIN_OFDM,
	 DEF_TX3_CW_MIN_OFDM},
	{DEF_TX0_CW_MAX_OFDM, DEF_TX1_CW_MAX_OFDM, DEF_TX2_CW_MAX_OFDM,
	 DEF_TX3_CW_MAX_OFDM},
	{DEF_TX0_AIFS, DEF_TX1_AIFS, DEF_TX2_AIFS, DEF_TX3_AIFS},
	{DEF_TX0_ACM, DEF_TX1_ACM, DEF_TX2_ACM, DEF_TX3_ACM},
	{DEF_TX0_TXOP_LIMIT_OFDM, DEF_TX1_TXOP_LIMIT_OFDM,
	 DEF_TX2_TXOP_LIMIT_OFDM, DEF_TX3_TXOP_LIMIT_OFDM}
};

static struct ieee80211_qos_parameters def_parameters_CCK = {
	{DEF_TX0_CW_MIN_CCK, DEF_TX1_CW_MIN_CCK, DEF_TX2_CW_MIN_CCK,
	 DEF_TX3_CW_MIN_CCK},
	{DEF_TX0_CW_MAX_CCK, DEF_TX1_CW_MAX_CCK, DEF_TX2_CW_MAX_CCK,
	 DEF_TX3_CW_MAX_CCK},
	{DEF_TX0_AIFS, DEF_TX1_AIFS, DEF_TX2_AIFS, DEF_TX3_AIFS},
	{DEF_TX0_ACM, DEF_TX1_ACM, DEF_TX2_ACM, DEF_TX3_ACM},
	{DEF_TX0_TXOP_LIMIT_CCK, DEF_TX1_TXOP_LIMIT_CCK, DEF_TX2_TXOP_LIMIT_CCK,
	 DEF_TX3_TXOP_LIMIT_CCK}
};

static u8 qos_oui[QOS_OUI_LEN] = { 0x00, 0x50, 0xF2 };

static int from_priority_to_tx_queue[] = {
	IPW_TX_QUEUE_1, IPW_TX_QUEUE_2, IPW_TX_QUEUE_2, IPW_TX_QUEUE_1,
	IPW_TX_QUEUE_3, IPW_TX_QUEUE_3, IPW_TX_QUEUE_4, IPW_TX_QUEUE_4
};

static u32 ipw_qos_get_burst_duration(struct ipw_priv *priv);

static int ipw_send_qos_params_command(struct ipw_priv *priv, struct ieee80211_qos_parameters
				       *qos_param);
static int ipw_send_qos_info_command(struct ipw_priv *priv, struct ieee80211_qos_information_element
				     *qos_param);
171
#endif				/* CONFIG_IPW2200_QOS */
172

173
static struct iw_statistics *ipw_get_wireless_stats(struct net_device *dev);
174
static void ipw_remove_current_network(struct ipw_priv *priv);
J
James Ketrenos 已提交
175
static void ipw_rx(struct ipw_priv *priv);
176
static int ipw_queue_tx_reclaim(struct ipw_priv *priv,
J
James Ketrenos 已提交
177 178 179 180 181 182 183 184 185 186 187 188
				struct clx2_tx_queue *txq, int qindex);
static int ipw_queue_reset(struct ipw_priv *priv);

static int ipw_queue_tx_hcmd(struct ipw_priv *priv, int hcmd, void *buf,
			     int len, int sync);

static void ipw_tx_queue_free(struct ipw_priv *);

static struct ipw_rx_queue *ipw_rx_queue_alloc(struct ipw_priv *);
static void ipw_rx_queue_free(struct ipw_priv *, struct ipw_rx_queue *);
static void ipw_rx_queue_replenish(void *);
static int ipw_up(struct ipw_priv *);
D
David Howells 已提交
189
static void ipw_bg_up(struct work_struct *work);
J
James Ketrenos 已提交
190
static void ipw_down(struct ipw_priv *);
D
David Howells 已提交
191
static void ipw_bg_down(struct work_struct *work);
J
James Ketrenos 已提交
192
static int ipw_config(struct ipw_priv *);
193 194
static int init_supported_rates(struct ipw_priv *priv,
				struct ipw_supported_rates *prates);
195 196
static void ipw_set_hwcrypto_keys(struct ipw_priv *);
static void ipw_send_wep_keys(struct ipw_priv *, int);
J
James Ketrenos 已提交
197

198 199
static int snprint_line(char *buf, size_t count,
			const u8 * data, u32 len, u32 ofs)
J
James Ketrenos 已提交
200 201 202
{
	int out, i, j, l;
	char c;
203

J
James Ketrenos 已提交
204 205 206 207
	out = snprintf(buf, count, "%08X", ofs);

	for (l = 0, i = 0; i < 2; i++) {
		out += snprintf(buf + out, count - out, " ");
208 209
		for (j = 0; j < 8 && l < len; j++, l++)
			out += snprintf(buf + out, count - out, "%02X ",
J
James Ketrenos 已提交
210 211 212 213
					data[(i * 8 + j)]);
		for (; j < 8; j++)
			out += snprintf(buf + out, count - out, "   ");
	}
214

J
James Ketrenos 已提交
215 216 217 218 219 220 221
	out += snprintf(buf + out, count - out, " ");
	for (l = 0, i = 0; i < 2; i++) {
		out += snprintf(buf + out, count - out, " ");
		for (j = 0; j < 8 && l < len; j++, l++) {
			c = data[(i * 8 + j)];
			if (!isascii(c) || !isprint(c))
				c = '.';
222

J
James Ketrenos 已提交
223 224 225 226 227 228
			out += snprintf(buf + out, count - out, "%c", c);
		}

		for (; j < 8; j++)
			out += snprintf(buf + out, count - out, " ");
	}
229

230
	return out;
J
James Ketrenos 已提交
231 232
}

233
static void printk_buf(int level, const u8 * data, u32 len)
J
James Ketrenos 已提交
234 235 236 237 238 239 240
{
	char line[81];
	u32 ofs = 0;
	if (!(ipw_debug_level & level))
		return;

	while (len) {
241 242 243
		snprint_line(line, sizeof(line), &data[ofs],
			     min(len, 16U), ofs);
		printk(KERN_DEBUG "%s\n", line);
J
James Ketrenos 已提交
244 245 246 247 248
		ofs += 16;
		len -= min(len, 16U);
	}
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
static int snprintk_buf(u8 * output, size_t size, const u8 * data, size_t len)
{
	size_t out = size;
	u32 ofs = 0;
	int total = 0;

	while (size && len) {
		out = snprint_line(output, size, &data[ofs],
				   min_t(size_t, len, 16U), ofs);

		ofs += 16;
		output += out;
		size -= out;
		len -= min_t(size_t, len, 16U);
		total += out;
	}
	return total;
}

268
/* alias for 32-bit indirect read (for SRAM/reg above 4K), with debug wrapper */
J
James Ketrenos 已提交
269 270 271
static u32 _ipw_read_reg32(struct ipw_priv *priv, u32 reg);
#define ipw_read_reg32(a, b) _ipw_read_reg32(a, b)

272
/* alias for 8-bit indirect read (for SRAM/reg above 4K), with debug wrapper */
J
James Ketrenos 已提交
273 274 275
static u8 _ipw_read_reg8(struct ipw_priv *ipw, u32 reg);
#define ipw_read_reg8(a, b) _ipw_read_reg8(a, b)

276
/* 8-bit indirect write (for SRAM/reg above 4K), with debug wrapper */
J
James Ketrenos 已提交
277 278 279
static void _ipw_write_reg8(struct ipw_priv *priv, u32 reg, u8 value);
static inline void ipw_write_reg8(struct ipw_priv *a, u32 b, u8 c)
{
280 281
	IPW_DEBUG_IO("%s %d: write_indirect8(0x%08X, 0x%08X)\n", __FILE__,
		     __LINE__, (u32) (b), (u32) (c));
J
James Ketrenos 已提交
282 283 284
	_ipw_write_reg8(a, b, c);
}

285
/* 16-bit indirect write (for SRAM/reg above 4K), with debug wrapper */
J
James Ketrenos 已提交
286 287 288
static void _ipw_write_reg16(struct ipw_priv *priv, u32 reg, u16 value);
static inline void ipw_write_reg16(struct ipw_priv *a, u32 b, u16 c)
{
289 290
	IPW_DEBUG_IO("%s %d: write_indirect16(0x%08X, 0x%08X)\n", __FILE__,
		     __LINE__, (u32) (b), (u32) (c));
J
James Ketrenos 已提交
291 292 293
	_ipw_write_reg16(a, b, c);
}

294
/* 32-bit indirect write (for SRAM/reg above 4K), with debug wrapper */
J
James Ketrenos 已提交
295 296 297
static void _ipw_write_reg32(struct ipw_priv *priv, u32 reg, u32 value);
static inline void ipw_write_reg32(struct ipw_priv *a, u32 b, u32 c)
{
298 299
	IPW_DEBUG_IO("%s %d: write_indirect32(0x%08X, 0x%08X)\n", __FILE__,
		     __LINE__, (u32) (b), (u32) (c));
J
James Ketrenos 已提交
300 301 302
	_ipw_write_reg32(a, b, c);
}

303
/* 8-bit direct write (low 4K) */
J
Jiri Slaby 已提交
304 305 306 307 308
static inline void _ipw_write8(struct ipw_priv *ipw, unsigned long ofs,
		u8 val)
{
	writeb(val, ipw->hw_base + ofs);
}
309 310

/* 8-bit direct write (for low 4K of SRAM/regs), with debug wrapper */
311
#define ipw_write8(ipw, ofs, val) do { \
J
Jiri Slaby 已提交
312 313 314 315
	IPW_DEBUG_IO("%s %d: write_direct8(0x%08X, 0x%08X)\n", __FILE__, \
			__LINE__, (u32)(ofs), (u32)(val)); \
	_ipw_write8(ipw, ofs, val); \
} while (0)
J
James Ketrenos 已提交
316

317
/* 16-bit direct write (low 4K) */
J
Jiri Slaby 已提交
318 319 320 321 322
static inline void _ipw_write16(struct ipw_priv *ipw, unsigned long ofs,
		u16 val)
{
	writew(val, ipw->hw_base + ofs);
}
323 324

/* 16-bit direct write (for low 4K of SRAM/regs), with debug wrapper */
J
Jiri Slaby 已提交
325 326 327 328 329
#define ipw_write16(ipw, ofs, val) do { \
	IPW_DEBUG_IO("%s %d: write_direct16(0x%08X, 0x%08X)\n", __FILE__, \
			__LINE__, (u32)(ofs), (u32)(val)); \
	_ipw_write16(ipw, ofs, val); \
} while (0)
J
James Ketrenos 已提交
330

331
/* 32-bit direct write (low 4K) */
J
Jiri Slaby 已提交
332 333 334 335 336
static inline void _ipw_write32(struct ipw_priv *ipw, unsigned long ofs,
		u32 val)
{
	writel(val, ipw->hw_base + ofs);
}
337 338

/* 32-bit direct write (for low 4K of SRAM/regs), with debug wrapper */
J
Jiri Slaby 已提交
339 340 341 342 343
#define ipw_write32(ipw, ofs, val) do { \
	IPW_DEBUG_IO("%s %d: write_direct32(0x%08X, 0x%08X)\n", __FILE__, \
			__LINE__, (u32)(ofs), (u32)(val)); \
	_ipw_write32(ipw, ofs, val); \
} while (0)
J
James Ketrenos 已提交
344

345
/* 8-bit direct read (low 4K) */
J
Jiri Slaby 已提交
346
static inline u8 _ipw_read8(struct ipw_priv *ipw, unsigned long ofs)
347
{
J
Jiri Slaby 已提交
348
	return readb(ipw->hw_base + ofs);
J
James Ketrenos 已提交
349
}
350

351
/* alias to 8-bit direct read (low 4K of SRAM/regs), with debug wrapper */
J
Jiri Slaby 已提交
352 353 354 355 356
#define ipw_read8(ipw, ofs) ({ \
	IPW_DEBUG_IO("%s %d: read_direct8(0x%08X)\n", __FILE__, __LINE__, \
			(u32)(ofs)); \
	_ipw_read8(ipw, ofs); \
})
J
James Ketrenos 已提交
357

358
/* 16-bit direct read (low 4K) */
J
Jiri Slaby 已提交
359
static inline u16 _ipw_read16(struct ipw_priv *ipw, unsigned long ofs)
360
{
J
Jiri Slaby 已提交
361
	return readw(ipw->hw_base + ofs);
J
James Ketrenos 已提交
362
}
363

364
/* alias to 16-bit direct read (low 4K of SRAM/regs), with debug wrapper */
J
Jiri Slaby 已提交
365 366 367 368 369
#define ipw_read16(ipw, ofs) ({ \
	IPW_DEBUG_IO("%s %d: read_direct16(0x%08X)\n", __FILE__, __LINE__, \
			(u32)(ofs)); \
	_ipw_read16(ipw, ofs); \
})
J
James Ketrenos 已提交
370

371
/* 32-bit direct read (low 4K) */
J
Jiri Slaby 已提交
372
static inline u32 _ipw_read32(struct ipw_priv *ipw, unsigned long ofs)
373
{
J
Jiri Slaby 已提交
374
	return readl(ipw->hw_base + ofs);
J
James Ketrenos 已提交
375
}
376

377
/* alias to 32-bit direct read (low 4K of SRAM/regs), with debug wrapper */
J
Jiri Slaby 已提交
378 379 380 381 382
#define ipw_read32(ipw, ofs) ({ \
	IPW_DEBUG_IO("%s %d: read_direct32(0x%08X)\n", __FILE__, __LINE__, \
			(u32)(ofs)); \
	_ipw_read32(ipw, ofs); \
})
J
James Ketrenos 已提交
383 384

static void _ipw_read_indirect(struct ipw_priv *, u32, u8 *, int);
385
/* alias to multi-byte read (SRAM/regs above 4K), with debug wrapper */
J
Jiri Slaby 已提交
386 387 388 389 390
#define ipw_read_indirect(a, b, c, d) ({ \
	IPW_DEBUG_IO("%s %d: read_indirect(0x%08X) %u bytes\n", __FILE__, \
			__LINE__, (u32)(b), (u32)(d)); \
	_ipw_read_indirect(a, b, c, d); \
})
J
James Ketrenos 已提交
391

392
/* alias to multi-byte read (SRAM/regs above 4K), with debug wrapper */
393 394
static void _ipw_write_indirect(struct ipw_priv *priv, u32 addr, u8 * data,
				int num);
J
Jiri Slaby 已提交
395 396 397 398 399
#define ipw_write_indirect(a, b, c, d) do { \
	IPW_DEBUG_IO("%s %d: write_indirect(0x%08X) %u bytes\n", __FILE__, \
			__LINE__, (u32)(b), (u32)(d)); \
	_ipw_write_indirect(a, b, c, d); \
} while (0)
J
James Ketrenos 已提交
400

401
/* 32-bit indirect write (above 4K) */
402
static void _ipw_write_reg32(struct ipw_priv *priv, u32 reg, u32 value)
J
James Ketrenos 已提交
403
{
404
	IPW_DEBUG_IO(" %p : reg = 0x%8X : value = 0x%8X\n", priv, reg, value);
405 406
	_ipw_write32(priv, IPW_INDIRECT_ADDR, reg);
	_ipw_write32(priv, IPW_INDIRECT_DATA, value);
J
James Ketrenos 已提交
407 408
}

409
/* 8-bit indirect write (above 4K) */
J
James Ketrenos 已提交
410 411
static void _ipw_write_reg8(struct ipw_priv *priv, u32 reg, u8 value)
{
412
	u32 aligned_addr = reg & IPW_INDIRECT_ADDR_MASK;	/* dword align */
413 414
	u32 dif_len = reg - aligned_addr;

J
James Ketrenos 已提交
415
	IPW_DEBUG_IO(" reg = 0x%8X : value = 0x%8X\n", reg, value);
416 417
	_ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
	_ipw_write8(priv, IPW_INDIRECT_DATA + dif_len, value);
J
James Ketrenos 已提交
418 419
}

420
/* 16-bit indirect write (above 4K) */
421
static void _ipw_write_reg16(struct ipw_priv *priv, u32 reg, u16 value)
J
James Ketrenos 已提交
422
{
423
	u32 aligned_addr = reg & IPW_INDIRECT_ADDR_MASK;	/* dword align */
424 425
	u32 dif_len = (reg - aligned_addr) & (~0x1ul);

J
James Ketrenos 已提交
426
	IPW_DEBUG_IO(" reg = 0x%8X : value = 0x%8X\n", reg, value);
427 428
	_ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
	_ipw_write16(priv, IPW_INDIRECT_DATA + dif_len, value);
J
James Ketrenos 已提交
429 430
}

431
/* 8-bit indirect read (above 4K) */
J
James Ketrenos 已提交
432 433 434
static u8 _ipw_read_reg8(struct ipw_priv *priv, u32 reg)
{
	u32 word;
435
	_ipw_write32(priv, IPW_INDIRECT_ADDR, reg & IPW_INDIRECT_ADDR_MASK);
J
James Ketrenos 已提交
436
	IPW_DEBUG_IO(" reg = 0x%8X : \n", reg);
437
	word = _ipw_read32(priv, IPW_INDIRECT_DATA);
438
	return (word >> ((reg & 0x3) * 8)) & 0xff;
J
James Ketrenos 已提交
439 440
}

441
/* 32-bit indirect read (above 4K) */
J
James Ketrenos 已提交
442 443 444 445 446 447
static u32 _ipw_read_reg32(struct ipw_priv *priv, u32 reg)
{
	u32 value;

	IPW_DEBUG_IO("%p : reg = 0x%08x\n", priv, reg);

448 449
	_ipw_write32(priv, IPW_INDIRECT_ADDR, reg);
	value = _ipw_read32(priv, IPW_INDIRECT_DATA);
J
James Ketrenos 已提交
450 451 452 453
	IPW_DEBUG_IO(" reg = 0x%4X : value = 0x%4x \n", reg, value);
	return value;
}

454 455
/* General purpose, no alignment requirement, iterative (multi-byte) read, */
/*    for area above 1st 4K of SRAM/reg space */
J
James Ketrenos 已提交
456 457 458
static void _ipw_read_indirect(struct ipw_priv *priv, u32 addr, u8 * buf,
			       int num)
{
459
	u32 aligned_addr = addr & IPW_INDIRECT_ADDR_MASK;	/* dword align */
J
James Ketrenos 已提交
460 461
	u32 dif_len = addr - aligned_addr;
	u32 i;
462

J
James Ketrenos 已提交
463 464
	IPW_DEBUG_IO("addr = %i, buf = %p, num = %i\n", addr, buf, num);

465 466 467 468
	if (num <= 0) {
		return;
	}

469
	/* Read the first dword (or portion) byte by byte */
J
James Ketrenos 已提交
470
	if (unlikely(dif_len)) {
471
		_ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
J
James Ketrenos 已提交
472
		/* Start reading at aligned_addr + dif_len */
473
		for (i = dif_len; ((i < 4) && (num > 0)); i++, num--)
474
			*buf++ = _ipw_read8(priv, IPW_INDIRECT_DATA + i);
J
James Ketrenos 已提交
475 476 477
		aligned_addr += 4;
	}

478
	/* Read all of the middle dwords as dwords, with auto-increment */
479
	_ipw_write32(priv, IPW_AUTOINC_ADDR, aligned_addr);
480
	for (; num >= 4; buf += 4, aligned_addr += 4, num -= 4)
481
		*(u32 *) buf = _ipw_read32(priv, IPW_AUTOINC_DATA);
482

483
	/* Read the last dword (or portion) byte by byte */
484
	if (unlikely(num)) {
485
		_ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
486
		for (i = 0; num > 0; i++, num--)
487
			*buf++ = ipw_read8(priv, IPW_INDIRECT_DATA + i);
488
	}
J
James Ketrenos 已提交
489 490
}

491 492
/* General purpose, no alignment requirement, iterative (multi-byte) write, */
/*    for area above 1st 4K of SRAM/reg space */
493
static void _ipw_write_indirect(struct ipw_priv *priv, u32 addr, u8 * buf,
J
James Ketrenos 已提交
494 495
				int num)
{
496
	u32 aligned_addr = addr & IPW_INDIRECT_ADDR_MASK;	/* dword align */
J
James Ketrenos 已提交
497 498
	u32 dif_len = addr - aligned_addr;
	u32 i;
499

J
James Ketrenos 已提交
500
	IPW_DEBUG_IO("addr = %i, buf = %p, num = %i\n", addr, buf, num);
501

502 503 504 505
	if (num <= 0) {
		return;
	}

506
	/* Write the first dword (or portion) byte by byte */
J
James Ketrenos 已提交
507
	if (unlikely(dif_len)) {
508
		_ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
509
		/* Start writing at aligned_addr + dif_len */
510
		for (i = dif_len; ((i < 4) && (num > 0)); i++, num--, buf++)
511
			_ipw_write8(priv, IPW_INDIRECT_DATA + i, *buf);
J
James Ketrenos 已提交
512 513
		aligned_addr += 4;
	}
514

515
	/* Write all of the middle dwords as dwords, with auto-increment */
516
	_ipw_write32(priv, IPW_AUTOINC_ADDR, aligned_addr);
517
	for (; num >= 4; buf += 4, aligned_addr += 4, num -= 4)
518
		_ipw_write32(priv, IPW_AUTOINC_DATA, *(u32 *) buf);
519

520
	/* Write the last dword (or portion) byte by byte */
521
	if (unlikely(num)) {
522
		_ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
523
		for (i = 0; num > 0; i++, num--, buf++)
524
			_ipw_write8(priv, IPW_INDIRECT_DATA + i, *buf);
525
	}
J
James Ketrenos 已提交
526 527
}

528 529
/* General purpose, no alignment requirement, iterative (multi-byte) write, */
/*    for 1st 4K of SRAM/regs space */
530
static void ipw_write_direct(struct ipw_priv *priv, u32 addr, void *buf,
J
James Ketrenos 已提交
531 532 533 534 535
			     int num)
{
	memcpy_toio((priv->hw_base + addr), buf, num);
}

536
/* Set bit(s) in low 4K of SRAM/regs */
J
James Ketrenos 已提交
537 538 539 540 541
static inline void ipw_set_bit(struct ipw_priv *priv, u32 reg, u32 mask)
{
	ipw_write32(priv, reg, ipw_read32(priv, reg) | mask);
}

542
/* Clear bit(s) in low 4K of SRAM/regs */
J
James Ketrenos 已提交
543 544 545 546 547
static inline void ipw_clear_bit(struct ipw_priv *priv, u32 reg, u32 mask)
{
	ipw_write32(priv, reg, ipw_read32(priv, reg) & ~mask);
}

Z
Zhu Yi 已提交
548
static inline void __ipw_enable_interrupts(struct ipw_priv *priv)
J
James Ketrenos 已提交
549 550 551 552
{
	if (priv->status & STATUS_INT_ENABLED)
		return;
	priv->status |= STATUS_INT_ENABLED;
553
	ipw_write32(priv, IPW_INTA_MASK_R, IPW_INTA_MASK_ALL);
J
James Ketrenos 已提交
554 555
}

Z
Zhu Yi 已提交
556
static inline void __ipw_disable_interrupts(struct ipw_priv *priv)
J
James Ketrenos 已提交
557 558 559 560
{
	if (!(priv->status & STATUS_INT_ENABLED))
		return;
	priv->status &= ~STATUS_INT_ENABLED;
561
	ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);
J
James Ketrenos 已提交
562 563
}

Z
Zhu Yi 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
static inline void ipw_enable_interrupts(struct ipw_priv *priv)
{
	unsigned long flags;

	spin_lock_irqsave(&priv->irq_lock, flags);
	__ipw_enable_interrupts(priv);
	spin_unlock_irqrestore(&priv->irq_lock, flags);
}

static inline void ipw_disable_interrupts(struct ipw_priv *priv)
{
	unsigned long flags;

	spin_lock_irqsave(&priv->irq_lock, flags);
	__ipw_disable_interrupts(priv);
	spin_unlock_irqrestore(&priv->irq_lock, flags);
}

J
James Ketrenos 已提交
582 583 584
static char *ipw_error_desc(u32 val)
{
	switch (val) {
585
	case IPW_FW_ERROR_OK:
J
James Ketrenos 已提交
586
		return "ERROR_OK";
587
	case IPW_FW_ERROR_FAIL:
J
James Ketrenos 已提交
588
		return "ERROR_FAIL";
589
	case IPW_FW_ERROR_MEMORY_UNDERFLOW:
J
James Ketrenos 已提交
590
		return "MEMORY_UNDERFLOW";
591
	case IPW_FW_ERROR_MEMORY_OVERFLOW:
J
James Ketrenos 已提交
592
		return "MEMORY_OVERFLOW";
593
	case IPW_FW_ERROR_BAD_PARAM:
594
		return "BAD_PARAM";
595
	case IPW_FW_ERROR_BAD_CHECKSUM:
596
		return "BAD_CHECKSUM";
597
	case IPW_FW_ERROR_NMI_INTERRUPT:
598
		return "NMI_INTERRUPT";
599
	case IPW_FW_ERROR_BAD_DATABASE:
600
		return "BAD_DATABASE";
601
	case IPW_FW_ERROR_ALLOC_FAIL:
602
		return "ALLOC_FAIL";
603
	case IPW_FW_ERROR_DMA_UNDERRUN:
604
		return "DMA_UNDERRUN";
605
	case IPW_FW_ERROR_DMA_STATUS:
606 607 608 609 610
		return "DMA_STATUS";
	case IPW_FW_ERROR_DINO_ERROR:
		return "DINO_ERROR";
	case IPW_FW_ERROR_EEPROM_ERROR:
		return "EEPROM_ERROR";
611
	case IPW_FW_ERROR_SYSASSERT:
612
		return "SYSASSERT";
613
	case IPW_FW_ERROR_FATAL_ERROR:
614
		return "FATAL_ERROR";
615
	default:
616
		return "UNKNOWN_ERROR";
J
James Ketrenos 已提交
617 618 619
	}
}

620 621
static void ipw_dump_error_log(struct ipw_priv *priv,
			       struct ipw_fw_error *error)
J
James Ketrenos 已提交
622
{
623
	u32 i;
624

625 626 627 628
	if (!error) {
		IPW_ERROR("Error allocating and capturing error log.  "
			  "Nothing to dump.\n");
		return;
J
James Ketrenos 已提交
629 630
	}

631 632 633
	IPW_ERROR("Start IPW Error Log Dump:\n");
	IPW_ERROR("Status: 0x%08X, Config: %08X\n",
		  error->status, error->config);
J
James Ketrenos 已提交
634

635
	for (i = 0; i < error->elem_len; i++)
636
		IPW_ERROR("%s %i 0x%08x  0x%08x  0x%08x  0x%08x  0x%08x\n",
637 638 639 640 641 642 643 644 645
			  ipw_error_desc(error->elem[i].desc),
			  error->elem[i].time,
			  error->elem[i].blink1,
			  error->elem[i].blink2,
			  error->elem[i].link1,
			  error->elem[i].link2, error->elem[i].data);
	for (i = 0; i < error->log_len; i++)
		IPW_ERROR("%i\t0x%08x\t%i\n",
			  error->log[i].time,
646
			  error->log[i].data, error->log[i].event);
J
James Ketrenos 已提交
647 648
}

649
static inline int ipw_is_init(struct ipw_priv *priv)
J
James Ketrenos 已提交
650
{
651
	return (priv->status & STATUS_INIT) ? 1 : 0;
J
James Ketrenos 已提交
652 653
}

654
static int ipw_get_ordinal(struct ipw_priv *priv, u32 ord, void *val, u32 * len)
J
James Ketrenos 已提交
655 656 657 658 659 660 661 662 663
{
	u32 addr, field_info, field_len, field_count, total_len;

	IPW_DEBUG_ORD("ordinal = %i\n", ord);

	if (!priv || !val || !len) {
		IPW_DEBUG_ORD("Invalid argument\n");
		return -EINVAL;
	}
664

J
James Ketrenos 已提交
665 666 667 668 669 670 671 672 673 674 675
	/* verify device ordinal tables have been initialized */
	if (!priv->table0_addr || !priv->table1_addr || !priv->table2_addr) {
		IPW_DEBUG_ORD("Access ordinals before initialization\n");
		return -EINVAL;
	}

	switch (IPW_ORD_TABLE_ID_MASK & ord) {
	case IPW_ORD_TABLE_0_MASK:
		/*
		 * TABLE 0: Direct access to a table of 32 bit values
		 *
676
		 * This is a very simple table with the data directly
J
James Ketrenos 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
		 * read from the table
		 */

		/* remove the table id from the ordinal */
		ord &= IPW_ORD_TABLE_VALUE_MASK;

		/* boundary check */
		if (ord > priv->table0_len) {
			IPW_DEBUG_ORD("ordinal value (%i) longer then "
				      "max (%i)\n", ord, priv->table0_len);
			return -EINVAL;
		}

		/* verify we have enough room to store the value */
		if (*len < sizeof(u32)) {
			IPW_DEBUG_ORD("ordinal buffer length too small, "
693
				      "need %zd\n", sizeof(u32));
J
James Ketrenos 已提交
694 695 696 697
			return -EINVAL;
		}

		IPW_DEBUG_ORD("Reading TABLE0[%i] from offset 0x%08x\n",
698
			      ord, priv->table0_addr + (ord << 2));
J
James Ketrenos 已提交
699 700 701

		*len = sizeof(u32);
		ord <<= 2;
702
		*((u32 *) val) = ipw_read32(priv, priv->table0_addr + ord);
J
James Ketrenos 已提交
703 704 705 706 707
		break;

	case IPW_ORD_TABLE_1_MASK:
		/*
		 * TABLE 1: Indirect access to a table of 32 bit values
708 709
		 *
		 * This is a fairly large table of u32 values each
J
James Ketrenos 已提交
710 711 712 713 714 715
		 * representing starting addr for the data (which is
		 * also a u32)
		 */

		/* remove the table id from the ordinal */
		ord &= IPW_ORD_TABLE_VALUE_MASK;
716

J
James Ketrenos 已提交
717 718 719 720 721 722 723 724 725
		/* boundary check */
		if (ord > priv->table1_len) {
			IPW_DEBUG_ORD("ordinal value too long\n");
			return -EINVAL;
		}

		/* verify we have enough room to store the value */
		if (*len < sizeof(u32)) {
			IPW_DEBUG_ORD("ordinal buffer length too small, "
726
				      "need %zd\n", sizeof(u32));
J
James Ketrenos 已提交
727 728 729
			return -EINVAL;
		}

730 731
		*((u32 *) val) =
		    ipw_read_reg32(priv, (priv->table1_addr + (ord << 2)));
J
James Ketrenos 已提交
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
		*len = sizeof(u32);
		break;

	case IPW_ORD_TABLE_2_MASK:
		/*
		 * TABLE 2: Indirect access to a table of variable sized values
		 *
		 * This table consist of six values, each containing
		 *     - dword containing the starting offset of the data
		 *     - dword containing the lengh in the first 16bits
		 *       and the count in the second 16bits
		 */

		/* remove the table id from the ordinal */
		ord &= IPW_ORD_TABLE_VALUE_MASK;

		/* boundary check */
		if (ord > priv->table2_len) {
			IPW_DEBUG_ORD("ordinal value too long\n");
			return -EINVAL;
		}

		/* get the address of statistic */
		addr = ipw_read_reg32(priv, priv->table2_addr + (ord << 3));
756 757

		/* get the second DW of statistics ;
J
James Ketrenos 已提交
758
		 * two 16-bit words - first is length, second is count */
759 760 761 762
		field_info =
		    ipw_read_reg32(priv,
				   priv->table2_addr + (ord << 3) +
				   sizeof(u32));
763

J
James Ketrenos 已提交
764
		/* get each entry length */
765
		field_len = *((u16 *) & field_info);
766

J
James Ketrenos 已提交
767
		/* get number of entries */
768
		field_count = *(((u16 *) & field_info) + 1);
769

J
James Ketrenos 已提交
770 771 772 773 774 775
		/* abort if not enought memory */
		total_len = field_len * field_count;
		if (total_len > *len) {
			*len = total_len;
			return -EINVAL;
		}
776

J
James Ketrenos 已提交
777 778 779 780 781
		*len = total_len;
		if (!total_len)
			return 0;

		IPW_DEBUG_ORD("addr = 0x%08x, total_len = %i, "
782
			      "field_info = 0x%08x\n",
J
James Ketrenos 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
			      addr, total_len, field_info);
		ipw_read_indirect(priv, addr, val, total_len);
		break;

	default:
		IPW_DEBUG_ORD("Invalid ordinal!\n");
		return -EINVAL;

	}

	return 0;
}

static void ipw_init_ordinals(struct ipw_priv *priv)
{
	priv->table0_addr = IPW_ORDINALS_TABLE_LOWER;
799
	priv->table0_len = ipw_read32(priv, priv->table0_addr);
J
James Ketrenos 已提交
800 801 802 803 804 805 806 807 808 809 810 811

	IPW_DEBUG_ORD("table 0 offset at 0x%08x, len = %i\n",
		      priv->table0_addr, priv->table0_len);

	priv->table1_addr = ipw_read32(priv, IPW_ORDINALS_TABLE_1);
	priv->table1_len = ipw_read_reg32(priv, priv->table1_addr);

	IPW_DEBUG_ORD("table 1 offset at 0x%08x, len = %i\n",
		      priv->table1_addr, priv->table1_len);

	priv->table2_addr = ipw_read32(priv, IPW_ORDINALS_TABLE_2);
	priv->table2_len = ipw_read_reg32(priv, priv->table2_addr);
812
	priv->table2_len &= 0x0000ffff;	/* use first two bytes */
J
James Ketrenos 已提交
813 814 815 816 817 818

	IPW_DEBUG_ORD("table 2 offset at 0x%08x, len = %i\n",
		      priv->table2_addr, priv->table2_len);

}

819
static u32 ipw_register_toggle(u32 reg)
820
{
821 822 823 824 825 826 827
	reg &= ~IPW_START_STANDBY;
	if (reg & IPW_GATE_ODMA)
		reg &= ~IPW_GATE_ODMA;
	if (reg & IPW_GATE_IDMA)
		reg &= ~IPW_GATE_IDMA;
	if (reg & IPW_GATE_ADMA)
		reg &= ~IPW_GATE_ADMA;
828 829 830 831 832 833 834 835 836 837 838 839
	return reg;
}

/*
 * LED behavior:
 * - On radio ON, turn on any LEDs that require to be on during start
 * - On initialization, start unassociated blink
 * - On association, disable unassociated blink
 * - On disassociation, start unassociated blink
 * - On radio OFF, turn off any LEDs started during radio on
 *
 */
840 841 842
#define LD_TIME_LINK_ON msecs_to_jiffies(300)
#define LD_TIME_LINK_OFF msecs_to_jiffies(2700)
#define LD_TIME_ACT_ON msecs_to_jiffies(250)
843

844
static void ipw_led_link_on(struct ipw_priv *priv)
845 846 847 848 849 850 851 852 853 854 855 856 857 858
{
	unsigned long flags;
	u32 led;

	/* If configured to not use LEDs, or nic_type is 1,
	 * then we don't toggle a LINK led */
	if (priv->config & CFG_NO_LED || priv->nic_type == EEPROM_NIC_TYPE_1)
		return;

	spin_lock_irqsave(&priv->lock, flags);

	if (!(priv->status & STATUS_RF_KILL_MASK) &&
	    !(priv->status & STATUS_LED_LINK_ON)) {
		IPW_DEBUG_LED("Link LED On\n");
859
		led = ipw_read_reg32(priv, IPW_EVENT_REG);
860 861 862 863 864
		led |= priv->led_association_on;

		led = ipw_register_toggle(led);

		IPW_DEBUG_LED("Reg: 0x%08X\n", led);
865
		ipw_write_reg32(priv, IPW_EVENT_REG, led);
866 867 868 869 870 871 872 873 874 875 876 877 878

		priv->status |= STATUS_LED_LINK_ON;

		/* If we aren't associated, schedule turning the LED off */
		if (!(priv->status & STATUS_ASSOCIATED))
			queue_delayed_work(priv->workqueue,
					   &priv->led_link_off,
					   LD_TIME_LINK_ON);
	}

	spin_unlock_irqrestore(&priv->lock, flags);
}

D
David Howells 已提交
879
static void ipw_bg_led_link_on(struct work_struct *work)
880
{
D
David Howells 已提交
881 882
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, led_link_on.work);
883
	mutex_lock(&priv->mutex);
D
David Howells 已提交
884
	ipw_led_link_on(priv);
885
	mutex_unlock(&priv->mutex);
886 887
}

888
static void ipw_led_link_off(struct ipw_priv *priv)
889 890 891 892 893 894 895 896 897 898 899 900
{
	unsigned long flags;
	u32 led;

	/* If configured not to use LEDs, or nic type is 1,
	 * then we don't goggle the LINK led. */
	if (priv->config & CFG_NO_LED || priv->nic_type == EEPROM_NIC_TYPE_1)
		return;

	spin_lock_irqsave(&priv->lock, flags);

	if (priv->status & STATUS_LED_LINK_ON) {
901
		led = ipw_read_reg32(priv, IPW_EVENT_REG);
902 903 904 905
		led &= priv->led_association_off;
		led = ipw_register_toggle(led);

		IPW_DEBUG_LED("Reg: 0x%08X\n", led);
906
		ipw_write_reg32(priv, IPW_EVENT_REG, led);
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923

		IPW_DEBUG_LED("Link LED Off\n");

		priv->status &= ~STATUS_LED_LINK_ON;

		/* If we aren't associated and the radio is on, schedule
		 * turning the LED on (blink while unassociated) */
		if (!(priv->status & STATUS_RF_KILL_MASK) &&
		    !(priv->status & STATUS_ASSOCIATED))
			queue_delayed_work(priv->workqueue, &priv->led_link_on,
					   LD_TIME_LINK_OFF);

	}

	spin_unlock_irqrestore(&priv->lock, flags);
}

D
David Howells 已提交
924
static void ipw_bg_led_link_off(struct work_struct *work)
925
{
D
David Howells 已提交
926 927
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, led_link_off.work);
928
	mutex_lock(&priv->mutex);
D
David Howells 已提交
929
	ipw_led_link_off(priv);
930
	mutex_unlock(&priv->mutex);
931 932
}

933
static void __ipw_led_activity_on(struct ipw_priv *priv)
934 935 936 937 938 939
{
	u32 led;

	if (priv->config & CFG_NO_LED)
		return;

940
	if (priv->status & STATUS_RF_KILL_MASK)
941 942 943
		return;

	if (!(priv->status & STATUS_LED_ACT_ON)) {
944
		led = ipw_read_reg32(priv, IPW_EVENT_REG);
945 946 947 948 949
		led |= priv->led_activity_on;

		led = ipw_register_toggle(led);

		IPW_DEBUG_LED("Reg: 0x%08X\n", led);
950
		ipw_write_reg32(priv, IPW_EVENT_REG, led);
951 952 953 954 955

		IPW_DEBUG_LED("Activity LED On\n");

		priv->status |= STATUS_LED_ACT_ON;

956
		cancel_delayed_work(&priv->led_act_off);
957 958 959 960 961 962 963 964
		queue_delayed_work(priv->workqueue, &priv->led_act_off,
				   LD_TIME_ACT_ON);
	} else {
		/* Reschedule LED off for full time period */
		cancel_delayed_work(&priv->led_act_off);
		queue_delayed_work(priv->workqueue, &priv->led_act_off,
				   LD_TIME_ACT_ON);
	}
965
}
966

967
#if 0
968 969 970 971 972
void ipw_led_activity_on(struct ipw_priv *priv)
{
	unsigned long flags;
	spin_lock_irqsave(&priv->lock, flags);
	__ipw_led_activity_on(priv);
973 974
	spin_unlock_irqrestore(&priv->lock, flags);
}
975
#endif  /*  0  */
976

977
static void ipw_led_activity_off(struct ipw_priv *priv)
978 979 980 981 982 983 984 985 986 987
{
	unsigned long flags;
	u32 led;

	if (priv->config & CFG_NO_LED)
		return;

	spin_lock_irqsave(&priv->lock, flags);

	if (priv->status & STATUS_LED_ACT_ON) {
988
		led = ipw_read_reg32(priv, IPW_EVENT_REG);
989 990 991 992 993
		led &= priv->led_activity_off;

		led = ipw_register_toggle(led);

		IPW_DEBUG_LED("Reg: 0x%08X\n", led);
994
		ipw_write_reg32(priv, IPW_EVENT_REG, led);
995 996 997 998 999 1000 1001 1002 1003

		IPW_DEBUG_LED("Activity LED Off\n");

		priv->status &= ~STATUS_LED_ACT_ON;
	}

	spin_unlock_irqrestore(&priv->lock, flags);
}

D
David Howells 已提交
1004
static void ipw_bg_led_activity_off(struct work_struct *work)
1005
{
D
David Howells 已提交
1006 1007
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, led_act_off.work);
1008
	mutex_lock(&priv->mutex);
D
David Howells 已提交
1009
	ipw_led_activity_off(priv);
1010
	mutex_unlock(&priv->mutex);
1011 1012
}

1013
static void ipw_led_band_on(struct ipw_priv *priv)
1014 1015 1016 1017 1018
{
	unsigned long flags;
	u32 led;

	/* Only nic type 1 supports mode LEDs */
1019 1020
	if (priv->config & CFG_NO_LED ||
	    priv->nic_type != EEPROM_NIC_TYPE_1 || !priv->assoc_network)
1021 1022 1023 1024
		return;

	spin_lock_irqsave(&priv->lock, flags);

1025
	led = ipw_read_reg32(priv, IPW_EVENT_REG);
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
	if (priv->assoc_network->mode == IEEE_A) {
		led |= priv->led_ofdm_on;
		led &= priv->led_association_off;
		IPW_DEBUG_LED("Mode LED On: 802.11a\n");
	} else if (priv->assoc_network->mode == IEEE_G) {
		led |= priv->led_ofdm_on;
		led |= priv->led_association_on;
		IPW_DEBUG_LED("Mode LED On: 802.11g\n");
	} else {
		led &= priv->led_ofdm_off;
		led |= priv->led_association_on;
		IPW_DEBUG_LED("Mode LED On: 802.11b\n");
	}

	led = ipw_register_toggle(led);

	IPW_DEBUG_LED("Reg: 0x%08X\n", led);
1043
	ipw_write_reg32(priv, IPW_EVENT_REG, led);
1044 1045 1046 1047

	spin_unlock_irqrestore(&priv->lock, flags);
}

1048
static void ipw_led_band_off(struct ipw_priv *priv)
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
{
	unsigned long flags;
	u32 led;

	/* Only nic type 1 supports mode LEDs */
	if (priv->config & CFG_NO_LED || priv->nic_type != EEPROM_NIC_TYPE_1)
		return;

	spin_lock_irqsave(&priv->lock, flags);

1059
	led = ipw_read_reg32(priv, IPW_EVENT_REG);
1060 1061 1062 1063 1064 1065
	led &= priv->led_ofdm_off;
	led &= priv->led_association_off;

	led = ipw_register_toggle(led);

	IPW_DEBUG_LED("Reg: 0x%08X\n", led);
1066
	ipw_write_reg32(priv, IPW_EVENT_REG, led);
1067 1068 1069 1070

	spin_unlock_irqrestore(&priv->lock, flags);
}

1071
static void ipw_led_radio_on(struct ipw_priv *priv)
1072 1073 1074 1075
{
	ipw_led_link_on(priv);
}

1076
static void ipw_led_radio_off(struct ipw_priv *priv)
1077 1078 1079 1080 1081
{
	ipw_led_activity_off(priv);
	ipw_led_link_off(priv);
}

1082
static void ipw_led_link_up(struct ipw_priv *priv)
1083 1084 1085 1086 1087
{
	/* Set the Link Led on for all nic types */
	ipw_led_link_on(priv);
}

1088
static void ipw_led_link_down(struct ipw_priv *priv)
1089 1090 1091 1092 1093 1094 1095 1096
{
	ipw_led_activity_off(priv);
	ipw_led_link_off(priv);

	if (priv->status & STATUS_RF_KILL_MASK)
		ipw_led_radio_off(priv);
}

1097
static void ipw_led_init(struct ipw_priv *priv)
1098 1099 1100 1101
{
	priv->nic_type = priv->eeprom[EEPROM_NIC_TYPE];

	/* Set the default PINs for the link and activity leds */
1102 1103
	priv->led_activity_on = IPW_ACTIVITY_LED;
	priv->led_activity_off = ~(IPW_ACTIVITY_LED);
1104

1105 1106
	priv->led_association_on = IPW_ASSOCIATED_LED;
	priv->led_association_off = ~(IPW_ASSOCIATED_LED);
1107 1108

	/* Set the default PINs for the OFDM leds */
1109 1110
	priv->led_ofdm_on = IPW_OFDM_LED;
	priv->led_ofdm_off = ~(IPW_OFDM_LED);
1111 1112 1113 1114

	switch (priv->nic_type) {
	case EEPROM_NIC_TYPE_1:
		/* In this NIC type, the LEDs are reversed.... */
1115 1116 1117 1118
		priv->led_activity_on = IPW_ASSOCIATED_LED;
		priv->led_activity_off = ~(IPW_ASSOCIATED_LED);
		priv->led_association_on = IPW_ACTIVITY_LED;
		priv->led_association_off = ~(IPW_ACTIVITY_LED);
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147

		if (!(priv->config & CFG_NO_LED))
			ipw_led_band_on(priv);

		/* And we don't blink link LEDs for this nic, so
		 * just return here */
		return;

	case EEPROM_NIC_TYPE_3:
	case EEPROM_NIC_TYPE_2:
	case EEPROM_NIC_TYPE_4:
	case EEPROM_NIC_TYPE_0:
		break;

	default:
		IPW_DEBUG_INFO("Unknown NIC type from EEPROM: %d\n",
			       priv->nic_type);
		priv->nic_type = EEPROM_NIC_TYPE_0;
		break;
	}

	if (!(priv->config & CFG_NO_LED)) {
		if (priv->status & STATUS_ASSOCIATED)
			ipw_led_link_on(priv);
		else
			ipw_led_link_off(priv);
	}
}

1148
static void ipw_led_shutdown(struct ipw_priv *priv)
1149 1150 1151 1152
{
	ipw_led_activity_off(priv);
	ipw_led_link_off(priv);
	ipw_led_band_off(priv);
1153 1154 1155
	cancel_delayed_work(&priv->led_link_on);
	cancel_delayed_work(&priv->led_link_off);
	cancel_delayed_work(&priv->led_act_off);
1156 1157
}

J
James Ketrenos 已提交
1158 1159 1160 1161
/*
 * The following adds a new attribute to the sysfs representation
 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/ipw/)
 * used for controling the debug level.
1162
 *
J
James Ketrenos 已提交
1163 1164 1165 1166 1167 1168
 * See the level definitions in ipw for details.
 */
static ssize_t show_debug_level(struct device_driver *d, char *buf)
{
	return sprintf(buf, "0x%08X\n", ipw_debug_level);
}
1169 1170 1171

static ssize_t store_debug_level(struct device_driver *d, const char *buf,
				 size_t count)
J
James Ketrenos 已提交
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
{
	char *p = (char *)buf;
	u32 val;

	if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
		p++;
		if (p[0] == 'x' || p[0] == 'X')
			p++;
		val = simple_strtoul(p, &p, 16);
	} else
		val = simple_strtoul(p, &p, 10);
1183 1184
	if (p == buf)
		printk(KERN_INFO DRV_NAME
J
James Ketrenos 已提交
1185 1186 1187 1188 1189 1190 1191
		       ": %s is not in hex or decimal form.\n", buf);
	else
		ipw_debug_level = val;

	return strnlen(buf, count);
}

1192
static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
J
James Ketrenos 已提交
1193 1194
		   show_debug_level, store_debug_level);

1195
static inline u32 ipw_get_event_log_len(struct ipw_priv *priv)
J
James Ketrenos 已提交
1196
{
1197
	/* length = 1st dword in log */
1198
	return ipw_read_reg32(priv, ipw_read32(priv, IPW_EVENT_LOG));
J
James Ketrenos 已提交
1199
}
1200

1201 1202
static void ipw_capture_event_log(struct ipw_priv *priv,
				  u32 log_len, struct ipw_event *log)
J
James Ketrenos 已提交
1203
{
1204
	u32 base;
1205

1206 1207 1208 1209 1210 1211
	if (log_len) {
		base = ipw_read32(priv, IPW_EVENT_LOG);
		ipw_read_indirect(priv, base + sizeof(base) + sizeof(u32),
				  (u8 *) log, sizeof(*log) * log_len);
	}
}
J
James Ketrenos 已提交
1212

1213
static struct ipw_fw_error *ipw_alloc_error_log(struct ipw_priv *priv)
J
James Ketrenos 已提交
1214
{
1215 1216 1217 1218
	struct ipw_fw_error *error;
	u32 log_len = ipw_get_event_log_len(priv);
	u32 base = ipw_read32(priv, IPW_ERROR_LOG);
	u32 elem_len = ipw_read_reg32(priv, base);
J
James Ketrenos 已提交
1219

1220 1221 1222 1223 1224 1225 1226
	error = kmalloc(sizeof(*error) +
			sizeof(*error->elem) * elem_len +
			sizeof(*error->log) * log_len, GFP_ATOMIC);
	if (!error) {
		IPW_ERROR("Memory allocation for firmware error log "
			  "failed.\n");
		return NULL;
J
James Ketrenos 已提交
1227
	}
1228
	error->jiffies = jiffies;
1229 1230 1231 1232 1233
	error->status = priv->status;
	error->config = priv->config;
	error->elem_len = elem_len;
	error->log_len = log_len;
	error->elem = (struct ipw_error_elem *)error->payload;
1234
	error->log = (struct ipw_event *)(error->elem + elem_len);
1235 1236

	ipw_capture_event_log(priv, log_len, error->log);
1237

1238 1239 1240 1241 1242
	if (elem_len)
		ipw_read_indirect(priv, base + sizeof(base), (u8 *) error->elem,
				  sizeof(*error->elem) * elem_len);

	return error;
J
James Ketrenos 已提交
1243
}
1244

1245 1246
static ssize_t show_event_log(struct device *d,
			      struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
1247
{
1248 1249
	struct ipw_priv *priv = dev_get_drvdata(d);
	u32 log_len = ipw_get_event_log_len(priv);
1250 1251
	u32 log_size;
	struct ipw_event *log;
1252
	u32 len = 0, i;
J
James Ketrenos 已提交
1253

1254 1255 1256 1257 1258 1259 1260 1261 1262
	/* not using min() because of its strict type checking */
	log_size = PAGE_SIZE / sizeof(*log) > log_len ?
			sizeof(*log) * log_len : PAGE_SIZE;
	log = kzalloc(log_size, GFP_KERNEL);
	if (!log) {
		IPW_ERROR("Unable to allocate memory for log\n");
		return 0;
	}
	log_len = log_size / sizeof(*log);
1263
	ipw_capture_event_log(priv, log_len, log);
J
James Ketrenos 已提交
1264

1265 1266 1267 1268 1269 1270
	len += snprintf(buf + len, PAGE_SIZE - len, "%08X", log_len);
	for (i = 0; i < log_len; i++)
		len += snprintf(buf + len, PAGE_SIZE - len,
				"\n%08X%08X%08X",
				log[i].time, log[i].event, log[i].data);
	len += snprintf(buf + len, PAGE_SIZE - len, "\n");
1271
	kfree(log);
1272
	return len;
J
James Ketrenos 已提交
1273
}
1274

1275
static DEVICE_ATTR(event_log, S_IRUGO, show_event_log, NULL);
J
James Ketrenos 已提交
1276

1277 1278
static ssize_t show_error(struct device *d,
			  struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
1279
{
1280 1281 1282 1283 1284
	struct ipw_priv *priv = dev_get_drvdata(d);
	u32 len = 0, i;
	if (!priv->error)
		return 0;
	len += snprintf(buf + len, PAGE_SIZE - len,
1285 1286
			"%08lX%08X%08X%08X",
			priv->error->jiffies,
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
			priv->error->status,
			priv->error->config, priv->error->elem_len);
	for (i = 0; i < priv->error->elem_len; i++)
		len += snprintf(buf + len, PAGE_SIZE - len,
				"\n%08X%08X%08X%08X%08X%08X%08X",
				priv->error->elem[i].time,
				priv->error->elem[i].desc,
				priv->error->elem[i].blink1,
				priv->error->elem[i].blink2,
				priv->error->elem[i].link1,
				priv->error->elem[i].link2,
				priv->error->elem[i].data);

	len += snprintf(buf + len, PAGE_SIZE - len,
			"\n%08X", priv->error->log_len);
	for (i = 0; i < priv->error->log_len; i++)
		len += snprintf(buf + len, PAGE_SIZE - len,
				"\n%08X%08X%08X",
				priv->error->log[i].time,
				priv->error->log[i].event,
				priv->error->log[i].data);
	len += snprintf(buf + len, PAGE_SIZE - len, "\n");
	return len;
}

static ssize_t clear_error(struct device *d,
			   struct device_attribute *attr,
			   const char *buf, size_t count)
{
	struct ipw_priv *priv = dev_get_drvdata(d);
1317 1318 1319

	kfree(priv->error);
	priv->error = NULL;
1320 1321
	return count;
}
J
James Ketrenos 已提交
1322

1323
static DEVICE_ATTR(error, S_IRUGO | S_IWUSR, show_error, clear_error);
J
James Ketrenos 已提交
1324

1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
static ssize_t show_cmd_log(struct device *d,
			    struct device_attribute *attr, char *buf)
{
	struct ipw_priv *priv = dev_get_drvdata(d);
	u32 len = 0, i;
	if (!priv->cmdlog)
		return 0;
	for (i = (priv->cmdlog_pos + 1) % priv->cmdlog_len;
	     (i != priv->cmdlog_pos) && (PAGE_SIZE - len);
	     i = (i + 1) % priv->cmdlog_len) {
		len +=
		    snprintf(buf + len, PAGE_SIZE - len,
			     "\n%08lX%08X%08X%08X\n", priv->cmdlog[i].jiffies,
			     priv->cmdlog[i].retcode, priv->cmdlog[i].cmd.cmd,
			     priv->cmdlog[i].cmd.len);
		len +=
		    snprintk_buf(buf + len, PAGE_SIZE - len,
				 (u8 *) priv->cmdlog[i].cmd.param,
				 priv->cmdlog[i].cmd.len);
		len += snprintf(buf + len, PAGE_SIZE - len, "\n");
	}
	len += snprintf(buf + len, PAGE_SIZE - len, "\n");
	return len;
J
James Ketrenos 已提交
1348
}
1349

1350
static DEVICE_ATTR(cmd_log, S_IRUGO, show_cmd_log, NULL);
J
James Ketrenos 已提交
1351

1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
#ifdef CONFIG_IPW2200_PROMISCUOUS
static void ipw_prom_free(struct ipw_priv *priv);
static int ipw_prom_alloc(struct ipw_priv *priv);
static ssize_t store_rtap_iface(struct device *d,
			 struct device_attribute *attr,
			 const char *buf, size_t count)
{
	struct ipw_priv *priv = dev_get_drvdata(d);
	int rc = 0;

	if (count < 1)
		return -EINVAL;

	switch (buf[0]) {
	case '0':
		if (!rtap_iface)
			return count;

		if (netif_running(priv->prom_net_dev)) {
			IPW_WARNING("Interface is up.  Cannot unregister.\n");
			return count;
		}

		ipw_prom_free(priv);
		rtap_iface = 0;
		break;

	case '1':
		if (rtap_iface)
			return count;

		rc = ipw_prom_alloc(priv);
		if (!rc)
			rtap_iface = 1;
		break;

	default:
		return -EINVAL;
	}

	if (rc) {
		IPW_ERROR("Failed to register promiscuous network "
			  "device (error %d).\n", rc);
	}

	return count;
}

static ssize_t show_rtap_iface(struct device *d,
			struct device_attribute *attr,
			char *buf)
{
	struct ipw_priv *priv = dev_get_drvdata(d);
	if (rtap_iface)
		return sprintf(buf, "%s", priv->prom_net_dev->name);
	else {
		buf[0] = '-';
		buf[1] = '1';
		buf[2] = '\0';
		return 3;
	}
}

static DEVICE_ATTR(rtap_iface, S_IWUSR | S_IRUSR, show_rtap_iface,
		   store_rtap_iface);

static ssize_t store_rtap_filter(struct device *d,
			 struct device_attribute *attr,
			 const char *buf, size_t count)
{
	struct ipw_priv *priv = dev_get_drvdata(d);

	if (!priv->prom_priv) {
		IPW_ERROR("Attempting to set filter without "
			  "rtap_iface enabled.\n");
		return -EPERM;
	}

	priv->prom_priv->filter = simple_strtol(buf, NULL, 0);

	IPW_DEBUG_INFO("Setting rtap filter to " BIT_FMT16 "\n",
		       BIT_ARG16(priv->prom_priv->filter));

	return count;
}

static ssize_t show_rtap_filter(struct device *d,
			struct device_attribute *attr,
			char *buf)
{
	struct ipw_priv *priv = dev_get_drvdata(d);
	return sprintf(buf, "0x%04X",
		       priv->prom_priv ? priv->prom_priv->filter : 0);
}

static DEVICE_ATTR(rtap_filter, S_IWUSR | S_IRUSR, show_rtap_filter,
		   store_rtap_filter);
#endif

1451 1452
static ssize_t show_scan_age(struct device *d, struct device_attribute *attr,
			     char *buf)
J
James Ketrenos 已提交
1453
{
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
	struct ipw_priv *priv = dev_get_drvdata(d);
	return sprintf(buf, "%d\n", priv->ieee->scan_age);
}

static ssize_t store_scan_age(struct device *d, struct device_attribute *attr,
			      const char *buf, size_t count)
{
	struct ipw_priv *priv = dev_get_drvdata(d);
	struct net_device *dev = priv->net_dev;
	char buffer[] = "00000000";
	unsigned long len =
	    (sizeof(buffer) - 1) > count ? count : sizeof(buffer) - 1;
	unsigned long val;
	char *p = buffer;

	IPW_DEBUG_INFO("enter\n");

	strncpy(buffer, buf, len);
	buffer[len] = 0;

	if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
		p++;
		if (p[0] == 'x' || p[0] == 'X')
			p++;
		val = simple_strtoul(p, &p, 16);
	} else
		val = simple_strtoul(p, &p, 10);
	if (p == buffer) {
		IPW_DEBUG_INFO("%s: user supplied invalid value.\n", dev->name);
	} else {
		priv->ieee->scan_age = val;
		IPW_DEBUG_INFO("set scan_age = %u\n", priv->ieee->scan_age);
	}

	IPW_DEBUG_INFO("exit\n");
	return len;
}

static DEVICE_ATTR(scan_age, S_IWUSR | S_IRUGO, show_scan_age, store_scan_age);

static ssize_t show_led(struct device *d, struct device_attribute *attr,
			char *buf)
{
	struct ipw_priv *priv = dev_get_drvdata(d);
	return sprintf(buf, "%d\n", (priv->config & CFG_NO_LED) ? 0 : 1);
}

static ssize_t store_led(struct device *d, struct device_attribute *attr,
			 const char *buf, size_t count)
{
	struct ipw_priv *priv = dev_get_drvdata(d);

	IPW_DEBUG_INFO("enter\n");

	if (count == 0)
		return 0;

	if (*buf == 0) {
		IPW_DEBUG_LED("Disabling LED control.\n");
		priv->config |= CFG_NO_LED;
		ipw_led_shutdown(priv);
	} else {
		IPW_DEBUG_LED("Enabling LED control.\n");
		priv->config &= ~CFG_NO_LED;
		ipw_led_init(priv);
	}

	IPW_DEBUG_INFO("exit\n");
	return count;
}

static DEVICE_ATTR(led, S_IWUSR | S_IRUGO, show_led, store_led);

1527
static ssize_t show_status(struct device *d,
1528
			   struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
1529
{
1530
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
1531 1532
	return sprintf(buf, "0x%08x\n", (int)p->status);
}
1533

J
James Ketrenos 已提交
1534 1535
static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);

1536 1537
static ssize_t show_cfg(struct device *d, struct device_attribute *attr,
			char *buf)
J
James Ketrenos 已提交
1538
{
1539
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
1540 1541
	return sprintf(buf, "0x%08x\n", (int)p->config);
}
1542

J
James Ketrenos 已提交
1543 1544
static DEVICE_ATTR(cfg, S_IRUGO, show_cfg, NULL);

1545
static ssize_t show_nic_type(struct device *d,
1546
			     struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
1547
{
1548 1549
	struct ipw_priv *priv = d->driver_data;
	return sprintf(buf, "TYPE: %d\n", priv->nic_type);
J
James Ketrenos 已提交
1550
}
1551

J
James Ketrenos 已提交
1552 1553
static DEVICE_ATTR(nic_type, S_IRUGO, show_nic_type, NULL);

1554
static ssize_t show_ucode_version(struct device *d,
1555
				  struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
1556 1557
{
	u32 len = sizeof(u32), tmp = 0;
1558
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
1559

1560
	if (ipw_get_ordinal(p, IPW_ORD_STAT_UCODE_VERSION, &tmp, &len))
J
James Ketrenos 已提交
1561 1562 1563 1564
		return 0;

	return sprintf(buf, "0x%08x\n", tmp);
}
1565 1566

static DEVICE_ATTR(ucode_version, S_IWUSR | S_IRUGO, show_ucode_version, NULL);
J
James Ketrenos 已提交
1567

1568 1569
static ssize_t show_rtc(struct device *d, struct device_attribute *attr,
			char *buf)
J
James Ketrenos 已提交
1570 1571
{
	u32 len = sizeof(u32), tmp = 0;
1572
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
1573

1574
	if (ipw_get_ordinal(p, IPW_ORD_STAT_RTC, &tmp, &len))
J
James Ketrenos 已提交
1575 1576 1577 1578
		return 0;

	return sprintf(buf, "0x%08x\n", tmp);
}
1579 1580

static DEVICE_ATTR(rtc, S_IWUSR | S_IRUGO, show_rtc, NULL);
J
James Ketrenos 已提交
1581 1582 1583 1584 1585

/*
 * Add a device attribute to view/control the delay between eeprom
 * operations.
 */
1586
static ssize_t show_eeprom_delay(struct device *d,
1587
				 struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
1588
{
1589
	int n = ((struct ipw_priv *)d->driver_data)->eeprom_delay;
J
James Ketrenos 已提交
1590 1591
	return sprintf(buf, "%i\n", n);
}
1592
static ssize_t store_eeprom_delay(struct device *d,
1593 1594
				  struct device_attribute *attr,
				  const char *buf, size_t count)
J
James Ketrenos 已提交
1595
{
1596
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
1597 1598 1599
	sscanf(buf, "%i", &p->eeprom_delay);
	return strnlen(buf, count);
}
1600 1601 1602

static DEVICE_ATTR(eeprom_delay, S_IWUSR | S_IRUGO,
		   show_eeprom_delay, store_eeprom_delay);
J
James Ketrenos 已提交
1603

1604
static ssize_t show_command_event_reg(struct device *d,
1605
				      struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
1606 1607
{
	u32 reg = 0;
1608
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
1609

1610
	reg = ipw_read_reg32(p, IPW_INTERNAL_CMD_EVENT);
J
James Ketrenos 已提交
1611 1612
	return sprintf(buf, "0x%08x\n", reg);
}
1613
static ssize_t store_command_event_reg(struct device *d,
1614 1615
				       struct device_attribute *attr,
				       const char *buf, size_t count)
J
James Ketrenos 已提交
1616 1617
{
	u32 reg;
1618
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
1619 1620

	sscanf(buf, "%x", &reg);
1621
	ipw_write_reg32(p, IPW_INTERNAL_CMD_EVENT, reg);
J
James Ketrenos 已提交
1622 1623
	return strnlen(buf, count);
}
1624 1625 1626

static DEVICE_ATTR(command_event_reg, S_IWUSR | S_IRUGO,
		   show_command_event_reg, store_command_event_reg);
J
James Ketrenos 已提交
1627

1628
static ssize_t show_mem_gpio_reg(struct device *d,
1629
				 struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
1630 1631
{
	u32 reg = 0;
1632
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
1633 1634 1635 1636

	reg = ipw_read_reg32(p, 0x301100);
	return sprintf(buf, "0x%08x\n", reg);
}
1637
static ssize_t store_mem_gpio_reg(struct device *d,
1638 1639
				  struct device_attribute *attr,
				  const char *buf, size_t count)
J
James Ketrenos 已提交
1640 1641
{
	u32 reg;
1642
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
1643 1644 1645 1646 1647

	sscanf(buf, "%x", &reg);
	ipw_write_reg32(p, 0x301100, reg);
	return strnlen(buf, count);
}
1648 1649 1650

static DEVICE_ATTR(mem_gpio_reg, S_IWUSR | S_IRUGO,
		   show_mem_gpio_reg, store_mem_gpio_reg);
J
James Ketrenos 已提交
1651

1652
static ssize_t show_indirect_dword(struct device *d,
1653
				   struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
1654 1655
{
	u32 reg = 0;
1656
	struct ipw_priv *priv = d->driver_data;
1657

1658
	if (priv->status & STATUS_INDIRECT_DWORD)
J
James Ketrenos 已提交
1659
		reg = ipw_read_reg32(priv, priv->indirect_dword);
1660
	else
J
James Ketrenos 已提交
1661
		reg = 0;
1662

J
James Ketrenos 已提交
1663 1664
	return sprintf(buf, "0x%08x\n", reg);
}
1665
static ssize_t store_indirect_dword(struct device *d,
1666 1667
				    struct device_attribute *attr,
				    const char *buf, size_t count)
J
James Ketrenos 已提交
1668
{
1669
	struct ipw_priv *priv = d->driver_data;
J
James Ketrenos 已提交
1670 1671 1672 1673 1674

	sscanf(buf, "%x", &priv->indirect_dword);
	priv->status |= STATUS_INDIRECT_DWORD;
	return strnlen(buf, count);
}
1675 1676 1677

static DEVICE_ATTR(indirect_dword, S_IWUSR | S_IRUGO,
		   show_indirect_dword, store_indirect_dword);
J
James Ketrenos 已提交
1678

1679
static ssize_t show_indirect_byte(struct device *d,
1680
				  struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
1681 1682
{
	u8 reg = 0;
1683
	struct ipw_priv *priv = d->driver_data;
1684

1685
	if (priv->status & STATUS_INDIRECT_BYTE)
J
James Ketrenos 已提交
1686
		reg = ipw_read_reg8(priv, priv->indirect_byte);
1687
	else
J
James Ketrenos 已提交
1688 1689 1690 1691
		reg = 0;

	return sprintf(buf, "0x%02x\n", reg);
}
1692
static ssize_t store_indirect_byte(struct device *d,
1693 1694
				   struct device_attribute *attr,
				   const char *buf, size_t count)
J
James Ketrenos 已提交
1695
{
1696
	struct ipw_priv *priv = d->driver_data;
J
James Ketrenos 已提交
1697 1698 1699 1700 1701

	sscanf(buf, "%x", &priv->indirect_byte);
	priv->status |= STATUS_INDIRECT_BYTE;
	return strnlen(buf, count);
}
1702 1703

static DEVICE_ATTR(indirect_byte, S_IWUSR | S_IRUGO,
J
James Ketrenos 已提交
1704 1705
		   show_indirect_byte, store_indirect_byte);

1706
static ssize_t show_direct_dword(struct device *d,
1707
				 struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
1708 1709
{
	u32 reg = 0;
1710
	struct ipw_priv *priv = d->driver_data;
J
James Ketrenos 已提交
1711

1712
	if (priv->status & STATUS_DIRECT_DWORD)
J
James Ketrenos 已提交
1713
		reg = ipw_read32(priv, priv->direct_dword);
1714
	else
J
James Ketrenos 已提交
1715 1716 1717 1718
		reg = 0;

	return sprintf(buf, "0x%08x\n", reg);
}
1719
static ssize_t store_direct_dword(struct device *d,
1720 1721
				  struct device_attribute *attr,
				  const char *buf, size_t count)
J
James Ketrenos 已提交
1722
{
1723
	struct ipw_priv *priv = d->driver_data;
J
James Ketrenos 已提交
1724 1725 1726 1727 1728 1729

	sscanf(buf, "%x", &priv->direct_dword);
	priv->status |= STATUS_DIRECT_DWORD;
	return strnlen(buf, count);
}

1730 1731
static DEVICE_ATTR(direct_dword, S_IWUSR | S_IRUGO,
		   show_direct_dword, store_direct_dword);
J
James Ketrenos 已提交
1732

1733
static int rf_kill_active(struct ipw_priv *priv)
J
James Ketrenos 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742
{
	if (0 == (ipw_read32(priv, 0x30) & 0x10000))
		priv->status |= STATUS_RF_KILL_HW;
	else
		priv->status &= ~STATUS_RF_KILL_HW;

	return (priv->status & STATUS_RF_KILL_HW) ? 1 : 0;
}

1743
static ssize_t show_rf_kill(struct device *d, struct device_attribute *attr,
1744
			    char *buf)
J
James Ketrenos 已提交
1745 1746
{
	/* 0 - RF kill not enabled
1747
	   1 - SW based RF kill active (sysfs)
J
James Ketrenos 已提交
1748 1749
	   2 - HW based RF kill active
	   3 - Both HW and SW baed RF kill active */
1750
	struct ipw_priv *priv = d->driver_data;
J
James Ketrenos 已提交
1751
	int val = ((priv->status & STATUS_RF_KILL_SW) ? 0x1 : 0x0) |
1752
	    (rf_kill_active(priv) ? 0x2 : 0x0);
J
James Ketrenos 已提交
1753 1754 1755 1756 1757
	return sprintf(buf, "%i\n", val);
}

static int ipw_radio_kill_sw(struct ipw_priv *priv, int disable_radio)
{
1758
	if ((disable_radio ? 1 : 0) ==
1759
	    ((priv->status & STATUS_RF_KILL_SW) ? 1 : 0))
1760
		return 0;
J
James Ketrenos 已提交
1761 1762 1763 1764 1765 1766 1767

	IPW_DEBUG_RF_KILL("Manual SW RF Kill set to: RADIO  %s\n",
			  disable_radio ? "OFF" : "ON");

	if (disable_radio) {
		priv->status |= STATUS_RF_KILL_SW;

1768
		if (priv->workqueue) {
J
James Ketrenos 已提交
1769
			cancel_delayed_work(&priv->request_scan);
D
Dan Williams 已提交
1770 1771
			cancel_delayed_work(&priv->request_direct_scan);
			cancel_delayed_work(&priv->request_passive_scan);
1772 1773
			cancel_delayed_work(&priv->scan_event);
		}
J
James Ketrenos 已提交
1774 1775 1776 1777 1778 1779 1780 1781
		queue_work(priv->workqueue, &priv->down);
	} else {
		priv->status &= ~STATUS_RF_KILL_SW;
		if (rf_kill_active(priv)) {
			IPW_DEBUG_RF_KILL("Can not turn radio back on - "
					  "disabled by HW switch\n");
			/* Make sure the RF_KILL check timer is running */
			cancel_delayed_work(&priv->rf_kill);
1782
			queue_delayed_work(priv->workqueue, &priv->rf_kill,
1783
					   round_jiffies_relative(2 * HZ));
1784
		} else
J
James Ketrenos 已提交
1785 1786 1787 1788 1789 1790
			queue_work(priv->workqueue, &priv->up);
	}

	return 1;
}

1791 1792
static ssize_t store_rf_kill(struct device *d, struct device_attribute *attr,
			     const char *buf, size_t count)
J
James Ketrenos 已提交
1793
{
1794
	struct ipw_priv *priv = d->driver_data;
1795

J
James Ketrenos 已提交
1796 1797 1798 1799
	ipw_radio_kill_sw(priv, buf[0] == '1');

	return count;
}
1800 1801

static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
J
James Ketrenos 已提交
1802

1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
static ssize_t show_speed_scan(struct device *d, struct device_attribute *attr,
			       char *buf)
{
	struct ipw_priv *priv = (struct ipw_priv *)d->driver_data;
	int pos = 0, len = 0;
	if (priv->config & CFG_SPEED_SCAN) {
		while (priv->speed_scan[pos] != 0)
			len += sprintf(&buf[len], "%d ",
				       priv->speed_scan[pos++]);
		return len + sprintf(&buf[len], "\n");
	}

	return sprintf(buf, "0\n");
}

static ssize_t store_speed_scan(struct device *d, struct device_attribute *attr,
				const char *buf, size_t count)
{
	struct ipw_priv *priv = (struct ipw_priv *)d->driver_data;
	int channel, pos = 0;
	const char *p = buf;

	/* list of space separated channels to scan, optionally ending with 0 */
	while ((channel = simple_strtol(p, NULL, 0))) {
		if (pos == MAX_SPEED_SCAN - 1) {
			priv->speed_scan[pos] = 0;
			break;
		}

1832
		if (ieee80211_is_valid_channel(priv->ieee, channel))
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
			priv->speed_scan[pos++] = channel;
		else
			IPW_WARNING("Skipping invalid channel request: %d\n",
				    channel);
		p = strchr(p, ' ');
		if (!p)
			break;
		while (*p == ' ' || *p == '\t')
			p++;
	}

	if (pos == 0)
		priv->config &= ~CFG_SPEED_SCAN;
	else {
		priv->speed_scan_pos = 0;
		priv->config |= CFG_SPEED_SCAN;
	}

	return count;
}

static DEVICE_ATTR(speed_scan, S_IWUSR | S_IRUGO, show_speed_scan,
		   store_speed_scan);

static ssize_t show_net_stats(struct device *d, struct device_attribute *attr,
			      char *buf)
{
	struct ipw_priv *priv = (struct ipw_priv *)d->driver_data;
	return sprintf(buf, "%c\n", (priv->config & CFG_NET_STATS) ? '1' : '0');
}

static ssize_t store_net_stats(struct device *d, struct device_attribute *attr,
			       const char *buf, size_t count)
{
	struct ipw_priv *priv = (struct ipw_priv *)d->driver_data;
	if (buf[0] == '1')
		priv->config |= CFG_NET_STATS;
	else
		priv->config &= ~CFG_NET_STATS;

	return count;
}

1876 1877
static DEVICE_ATTR(net_stats, S_IWUSR | S_IRUGO,
		   show_net_stats, store_net_stats);
1878

1879 1880 1881 1882 1883
static ssize_t show_channels(struct device *d,
			     struct device_attribute *attr,
			     char *buf)
{
	struct ipw_priv *priv = dev_get_drvdata(d);
1884
	const struct ieee80211_geo *geo = ieee80211_get_geo(priv->ieee);
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
	int len = 0, i;

	len = sprintf(&buf[len],
		      "Displaying %d channels in 2.4Ghz band "
		      "(802.11bg):\n", geo->bg_channels);

	for (i = 0; i < geo->bg_channels; i++) {
		len += sprintf(&buf[len], "%d: BSS%s%s, %s, Band %s.\n",
			       geo->bg[i].channel,
			       geo->bg[i].flags & IEEE80211_CH_RADAR_DETECT ?
			       " (radar spectrum)" : "",
			       ((geo->bg[i].flags & IEEE80211_CH_NO_IBSS) ||
				(geo->bg[i].flags & IEEE80211_CH_RADAR_DETECT))
			       ? "" : ", IBSS",
			       geo->bg[i].flags & IEEE80211_CH_PASSIVE_ONLY ?
			       "passive only" : "active/passive",
			       geo->bg[i].flags & IEEE80211_CH_B_ONLY ?
			       "B" : "B/G");
	}

	len += sprintf(&buf[len],
		       "Displaying %d channels in 5.2Ghz band "
		       "(802.11a):\n", geo->a_channels);
	for (i = 0; i < geo->a_channels; i++) {
		len += sprintf(&buf[len], "%d: BSS%s%s, %s.\n",
			       geo->a[i].channel,
			       geo->a[i].flags & IEEE80211_CH_RADAR_DETECT ?
			       " (radar spectrum)" : "",
			       ((geo->a[i].flags & IEEE80211_CH_NO_IBSS) ||
				(geo->a[i].flags & IEEE80211_CH_RADAR_DETECT))
			       ? "" : ", IBSS",
			       geo->a[i].flags & IEEE80211_CH_PASSIVE_ONLY ?
			       "passive only" : "active/passive");
	}

	return len;
}

static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);

1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
static void notify_wx_assoc_event(struct ipw_priv *priv)
{
	union iwreq_data wrqu;
	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
	if (priv->status & STATUS_ASSOCIATED)
		memcpy(wrqu.ap_addr.sa_data, priv->bssid, ETH_ALEN);
	else
		memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
	wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
}

J
James Ketrenos 已提交
1936 1937 1938 1939 1940 1941
static void ipw_irq_tasklet(struct ipw_priv *priv)
{
	u32 inta, inta_mask, handled = 0;
	unsigned long flags;
	int rc = 0;

Z
Zhu Yi 已提交
1942
	spin_lock_irqsave(&priv->irq_lock, flags);
J
James Ketrenos 已提交
1943

1944 1945 1946
	inta = ipw_read32(priv, IPW_INTA_RW);
	inta_mask = ipw_read32(priv, IPW_INTA_MASK_R);
	inta &= (IPW_INTA_MASK_ALL & inta_mask);
J
James Ketrenos 已提交
1947 1948 1949 1950

	/* Add any cached INTA values that need to be handled */
	inta |= priv->isr_inta;

Z
Zhu Yi 已提交
1951 1952 1953 1954
	spin_unlock_irqrestore(&priv->irq_lock, flags);

	spin_lock_irqsave(&priv->lock, flags);

J
James Ketrenos 已提交
1955
	/* handle all the justifications for the interrupt */
1956
	if (inta & IPW_INTA_BIT_RX_TRANSFER) {
J
James Ketrenos 已提交
1957
		ipw_rx(priv);
1958
		handled |= IPW_INTA_BIT_RX_TRANSFER;
J
James Ketrenos 已提交
1959 1960
	}

1961
	if (inta & IPW_INTA_BIT_TX_CMD_QUEUE) {
J
James Ketrenos 已提交
1962
		IPW_DEBUG_HC("Command completed.\n");
1963
		rc = ipw_queue_tx_reclaim(priv, &priv->txq_cmd, -1);
J
James Ketrenos 已提交
1964 1965
		priv->status &= ~STATUS_HCMD_ACTIVE;
		wake_up_interruptible(&priv->wait_command_queue);
1966
		handled |= IPW_INTA_BIT_TX_CMD_QUEUE;
J
James Ketrenos 已提交
1967 1968
	}

1969
	if (inta & IPW_INTA_BIT_TX_QUEUE_1) {
J
James Ketrenos 已提交
1970
		IPW_DEBUG_TX("TX_QUEUE_1\n");
1971
		rc = ipw_queue_tx_reclaim(priv, &priv->txq[0], 0);
1972
		handled |= IPW_INTA_BIT_TX_QUEUE_1;
J
James Ketrenos 已提交
1973 1974
	}

1975
	if (inta & IPW_INTA_BIT_TX_QUEUE_2) {
J
James Ketrenos 已提交
1976
		IPW_DEBUG_TX("TX_QUEUE_2\n");
1977
		rc = ipw_queue_tx_reclaim(priv, &priv->txq[1], 1);
1978
		handled |= IPW_INTA_BIT_TX_QUEUE_2;
J
James Ketrenos 已提交
1979 1980
	}

1981
	if (inta & IPW_INTA_BIT_TX_QUEUE_3) {
J
James Ketrenos 已提交
1982
		IPW_DEBUG_TX("TX_QUEUE_3\n");
1983
		rc = ipw_queue_tx_reclaim(priv, &priv->txq[2], 2);
1984
		handled |= IPW_INTA_BIT_TX_QUEUE_3;
J
James Ketrenos 已提交
1985 1986
	}

1987
	if (inta & IPW_INTA_BIT_TX_QUEUE_4) {
J
James Ketrenos 已提交
1988
		IPW_DEBUG_TX("TX_QUEUE_4\n");
1989
		rc = ipw_queue_tx_reclaim(priv, &priv->txq[3], 3);
1990
		handled |= IPW_INTA_BIT_TX_QUEUE_4;
J
James Ketrenos 已提交
1991 1992
	}

1993
	if (inta & IPW_INTA_BIT_STATUS_CHANGE) {
J
James Ketrenos 已提交
1994
		IPW_WARNING("STATUS_CHANGE\n");
1995
		handled |= IPW_INTA_BIT_STATUS_CHANGE;
J
James Ketrenos 已提交
1996 1997
	}

1998
	if (inta & IPW_INTA_BIT_BEACON_PERIOD_EXPIRED) {
J
James Ketrenos 已提交
1999
		IPW_WARNING("TX_PERIOD_EXPIRED\n");
2000
		handled |= IPW_INTA_BIT_BEACON_PERIOD_EXPIRED;
J
James Ketrenos 已提交
2001 2002
	}

2003
	if (inta & IPW_INTA_BIT_SLAVE_MODE_HOST_CMD_DONE) {
J
James Ketrenos 已提交
2004
		IPW_WARNING("HOST_CMD_DONE\n");
2005
		handled |= IPW_INTA_BIT_SLAVE_MODE_HOST_CMD_DONE;
J
James Ketrenos 已提交
2006 2007
	}

2008
	if (inta & IPW_INTA_BIT_FW_INITIALIZATION_DONE) {
J
James Ketrenos 已提交
2009
		IPW_WARNING("FW_INITIALIZATION_DONE\n");
2010
		handled |= IPW_INTA_BIT_FW_INITIALIZATION_DONE;
J
James Ketrenos 已提交
2011 2012
	}

2013
	if (inta & IPW_INTA_BIT_FW_CARD_DISABLE_PHY_OFF_DONE) {
J
James Ketrenos 已提交
2014
		IPW_WARNING("PHY_OFF_DONE\n");
2015
		handled |= IPW_INTA_BIT_FW_CARD_DISABLE_PHY_OFF_DONE;
J
James Ketrenos 已提交
2016 2017
	}

2018
	if (inta & IPW_INTA_BIT_RF_KILL_DONE) {
J
James Ketrenos 已提交
2019 2020 2021
		IPW_DEBUG_RF_KILL("RF_KILL_DONE\n");
		priv->status |= STATUS_RF_KILL_HW;
		wake_up_interruptible(&priv->wait_command_queue);
2022
		priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
J
James Ketrenos 已提交
2023
		cancel_delayed_work(&priv->request_scan);
D
Dan Williams 已提交
2024 2025
		cancel_delayed_work(&priv->request_direct_scan);
		cancel_delayed_work(&priv->request_passive_scan);
2026
		cancel_delayed_work(&priv->scan_event);
2027
		schedule_work(&priv->link_down);
J
James Ketrenos 已提交
2028
		queue_delayed_work(priv->workqueue, &priv->rf_kill, 2 * HZ);
2029
		handled |= IPW_INTA_BIT_RF_KILL_DONE;
J
James Ketrenos 已提交
2030
	}
2031

2032
	if (inta & IPW_INTA_BIT_FATAL_ERROR) {
2033
		IPW_WARNING("Firmware error detected.  Restarting.\n");
2034
		if (priv->error) {
2035
			IPW_DEBUG_FW("Sysfs 'error' log already exists.\n");
2036 2037 2038 2039
			if (ipw_debug_level & IPW_DL_FW_ERRORS) {
				struct ipw_fw_error *error =
				    ipw_alloc_error_log(priv);
				ipw_dump_error_log(priv, error);
2040
				kfree(error);
2041 2042 2043 2044
			}
		} else {
			priv->error = ipw_alloc_error_log(priv);
			if (priv->error)
2045
				IPW_DEBUG_FW("Sysfs 'error' log captured.\n");
2046
			else
2047 2048
				IPW_DEBUG_FW("Error allocating sysfs 'error' "
					     "log.\n");
2049 2050 2051 2052
			if (ipw_debug_level & IPW_DL_FW_ERRORS)
				ipw_dump_error_log(priv, priv->error);
		}

2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
		/* XXX: If hardware encryption is for WPA/WPA2,
		 * we have to notify the supplicant. */
		if (priv->ieee->sec.encrypt) {
			priv->status &= ~STATUS_ASSOCIATED;
			notify_wx_assoc_event(priv);
		}

		/* Keep the restart process from trying to send host
		 * commands by clearing the INIT status bit */
		priv->status &= ~STATUS_INIT;
2063 2064 2065 2066 2067

		/* Cancel currently queued command. */
		priv->status &= ~STATUS_HCMD_ACTIVE;
		wake_up_interruptible(&priv->wait_command_queue);

J
James Ketrenos 已提交
2068
		queue_work(priv->workqueue, &priv->adapter_restart);
2069
		handled |= IPW_INTA_BIT_FATAL_ERROR;
J
James Ketrenos 已提交
2070 2071
	}

2072
	if (inta & IPW_INTA_BIT_PARITY_ERROR) {
J
James Ketrenos 已提交
2073
		IPW_ERROR("Parity error\n");
2074
		handled |= IPW_INTA_BIT_PARITY_ERROR;
J
James Ketrenos 已提交
2075 2076 2077
	}

	if (handled != inta) {
2078
		IPW_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
J
James Ketrenos 已提交
2079 2080
	}

Z
Zhu Yi 已提交
2081 2082
	spin_unlock_irqrestore(&priv->lock, flags);

J
James Ketrenos 已提交
2083 2084 2085
	/* enable all interrupts */
	ipw_enable_interrupts(priv);
}
2086

J
James Ketrenos 已提交
2087 2088 2089 2090 2091
#define IPW_CMD(x) case IPW_CMD_ ## x : return #x
static char *get_cmd_string(u8 cmd)
{
	switch (cmd) {
		IPW_CMD(HOST_COMPLETE);
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141
		IPW_CMD(POWER_DOWN);
		IPW_CMD(SYSTEM_CONFIG);
		IPW_CMD(MULTICAST_ADDRESS);
		IPW_CMD(SSID);
		IPW_CMD(ADAPTER_ADDRESS);
		IPW_CMD(PORT_TYPE);
		IPW_CMD(RTS_THRESHOLD);
		IPW_CMD(FRAG_THRESHOLD);
		IPW_CMD(POWER_MODE);
		IPW_CMD(WEP_KEY);
		IPW_CMD(TGI_TX_KEY);
		IPW_CMD(SCAN_REQUEST);
		IPW_CMD(SCAN_REQUEST_EXT);
		IPW_CMD(ASSOCIATE);
		IPW_CMD(SUPPORTED_RATES);
		IPW_CMD(SCAN_ABORT);
		IPW_CMD(TX_FLUSH);
		IPW_CMD(QOS_PARAMETERS);
		IPW_CMD(DINO_CONFIG);
		IPW_CMD(RSN_CAPABILITIES);
		IPW_CMD(RX_KEY);
		IPW_CMD(CARD_DISABLE);
		IPW_CMD(SEED_NUMBER);
		IPW_CMD(TX_POWER);
		IPW_CMD(COUNTRY_INFO);
		IPW_CMD(AIRONET_INFO);
		IPW_CMD(AP_TX_POWER);
		IPW_CMD(CCKM_INFO);
		IPW_CMD(CCX_VER_INFO);
		IPW_CMD(SET_CALIBRATION);
		IPW_CMD(SENSITIVITY_CALIB);
		IPW_CMD(RETRY_LIMIT);
		IPW_CMD(IPW_PRE_POWER_DOWN);
		IPW_CMD(VAP_BEACON_TEMPLATE);
		IPW_CMD(VAP_DTIM_PERIOD);
		IPW_CMD(EXT_SUPPORTED_RATES);
		IPW_CMD(VAP_LOCAL_TX_PWR_CONSTRAINT);
		IPW_CMD(VAP_QUIET_INTERVALS);
		IPW_CMD(VAP_CHANNEL_SWITCH);
		IPW_CMD(VAP_MANDATORY_CHANNELS);
		IPW_CMD(VAP_CELL_PWR_LIMIT);
		IPW_CMD(VAP_CF_PARAM_SET);
		IPW_CMD(VAP_SET_BEACONING_STATE);
		IPW_CMD(MEASUREMENT);
		IPW_CMD(POWER_CAPABILITY);
		IPW_CMD(SUPPORTED_CHANNELS);
		IPW_CMD(TPC_REPORT);
		IPW_CMD(WME_INFO);
		IPW_CMD(PRODUCTION_COMMAND);
	default:
J
James Ketrenos 已提交
2142 2143 2144 2145 2146
		return "UNKNOWN";
	}
}

#define HOST_COMPLETE_TIMEOUT HZ
Z
Zhu Yi 已提交
2147 2148

static int __ipw_send_cmd(struct ipw_priv *priv, struct host_cmd *cmd)
J
James Ketrenos 已提交
2149 2150
{
	int rc = 0;
2151
	unsigned long flags;
J
James Ketrenos 已提交
2152

2153
	spin_lock_irqsave(&priv->lock, flags);
J
James Ketrenos 已提交
2154
	if (priv->status & STATUS_HCMD_ACTIVE) {
2155 2156
		IPW_ERROR("Failed to send %s: Already sending a command.\n",
			  get_cmd_string(cmd->cmd));
2157
		spin_unlock_irqrestore(&priv->lock, flags);
2158
		return -EAGAIN;
J
James Ketrenos 已提交
2159 2160 2161
	}

	priv->status |= STATUS_HCMD_ACTIVE;
2162

2163 2164 2165 2166 2167 2168 2169 2170 2171
	if (priv->cmdlog) {
		priv->cmdlog[priv->cmdlog_pos].jiffies = jiffies;
		priv->cmdlog[priv->cmdlog_pos].cmd.cmd = cmd->cmd;
		priv->cmdlog[priv->cmdlog_pos].cmd.len = cmd->len;
		memcpy(priv->cmdlog[priv->cmdlog_pos].cmd.param, cmd->param,
		       cmd->len);
		priv->cmdlog[priv->cmdlog_pos].retcode = -1;
	}

2172 2173 2174
	IPW_DEBUG_HC("%s command (#%d) %d bytes: 0x%08X\n",
		     get_cmd_string(cmd->cmd), cmd->cmd, cmd->len,
		     priv->status);
2175 2176 2177 2178 2179 2180 2181 2182

#ifndef DEBUG_CMD_WEP_KEY
	if (cmd->cmd == IPW_CMD_WEP_KEY)
		IPW_DEBUG_HC("WEP_KEY command masked out for secure.\n");
	else
#endif
		printk_buf(IPW_DL_HOST_COMMAND, (u8 *) cmd->param, cmd->len);

Z
Zhu Yi 已提交
2183
	rc = ipw_queue_tx_hcmd(priv, cmd->cmd, cmd->param, cmd->len, 0);
2184 2185
	if (rc) {
		priv->status &= ~STATUS_HCMD_ACTIVE;
2186 2187
		IPW_ERROR("Failed to send %s: Reason %d\n",
			  get_cmd_string(cmd->cmd), rc);
2188
		spin_unlock_irqrestore(&priv->lock, flags);
2189
		goto exit;
2190 2191
	}
	spin_unlock_irqrestore(&priv->lock, flags);
J
James Ketrenos 已提交
2192

2193 2194 2195 2196
	rc = wait_event_interruptible_timeout(priv->wait_command_queue,
					      !(priv->
						status & STATUS_HCMD_ACTIVE),
					      HOST_COMPLETE_TIMEOUT);
J
James Ketrenos 已提交
2197
	if (rc == 0) {
2198 2199
		spin_lock_irqsave(&priv->lock, flags);
		if (priv->status & STATUS_HCMD_ACTIVE) {
2200 2201
			IPW_ERROR("Failed to send %s: Command timed out.\n",
				  get_cmd_string(cmd->cmd));
2202 2203
			priv->status &= ~STATUS_HCMD_ACTIVE;
			spin_unlock_irqrestore(&priv->lock, flags);
2204 2205
			rc = -EIO;
			goto exit;
2206 2207
		}
		spin_unlock_irqrestore(&priv->lock, flags);
2208 2209
	} else
		rc = 0;
2210

2211
	if (priv->status & STATUS_RF_KILL_HW) {
2212 2213
		IPW_ERROR("Failed to send %s: Aborted due to RF kill switch.\n",
			  get_cmd_string(cmd->cmd));
2214 2215
		rc = -EIO;
		goto exit;
J
James Ketrenos 已提交
2216 2217
	}

2218
      exit:
2219 2220 2221 2222 2223
	if (priv->cmdlog) {
		priv->cmdlog[priv->cmdlog_pos++].retcode = rc;
		priv->cmdlog_pos %= priv->cmdlog_len;
	}
	return rc;
J
James Ketrenos 已提交
2224 2225
}

Z
Zhu Yi 已提交
2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
static int ipw_send_cmd_simple(struct ipw_priv *priv, u8 command)
{
	struct host_cmd cmd = {
		.cmd = command,
	};

	return __ipw_send_cmd(priv, &cmd);
}

static int ipw_send_cmd_pdu(struct ipw_priv *priv, u8 command, u8 len,
			    void *data)
J
James Ketrenos 已提交
2237 2238
{
	struct host_cmd cmd = {
Z
Zhu Yi 已提交
2239 2240 2241
		.cmd = command,
		.len = len,
		.param = data,
J
James Ketrenos 已提交
2242 2243
	};

Z
Zhu Yi 已提交
2244 2245 2246 2247 2248
	return __ipw_send_cmd(priv, &cmd);
}

static int ipw_send_host_complete(struct ipw_priv *priv)
{
J
James Ketrenos 已提交
2249 2250 2251 2252 2253
	if (!priv) {
		IPW_ERROR("Invalid args\n");
		return -1;
	}

Z
Zhu Yi 已提交
2254
	return ipw_send_cmd_simple(priv, IPW_CMD_HOST_COMPLETE);
J
James Ketrenos 已提交
2255 2256
}

2257
static int ipw_send_system_config(struct ipw_priv *priv)
J
James Ketrenos 已提交
2258
{
2259 2260 2261
	return ipw_send_cmd_pdu(priv, IPW_CMD_SYSTEM_CONFIG,
				sizeof(priv->sys_config),
				&priv->sys_config);
J
James Ketrenos 已提交
2262 2263
}

2264
static int ipw_send_ssid(struct ipw_priv *priv, u8 * ssid, int len)
J
James Ketrenos 已提交
2265 2266 2267 2268 2269 2270
{
	if (!priv || !ssid) {
		IPW_ERROR("Invalid args\n");
		return -1;
	}

Z
Zhu Yi 已提交
2271
	return ipw_send_cmd_pdu(priv, IPW_CMD_SSID, min(len, IW_ESSID_MAX_SIZE),
2272
				ssid);
J
James Ketrenos 已提交
2273 2274
}

2275
static int ipw_send_adapter_address(struct ipw_priv *priv, u8 * mac)
J
James Ketrenos 已提交
2276 2277 2278 2279 2280 2281
{
	if (!priv || !mac) {
		IPW_ERROR("Invalid args\n");
		return -1;
	}

J
Johannes Berg 已提交
2282 2283
	IPW_DEBUG_INFO("%s: Setting MAC to %pM\n",
		       priv->net_dev->name, mac);
J
James Ketrenos 已提交
2284

2285
	return ipw_send_cmd_pdu(priv, IPW_CMD_ADAPTER_ADDRESS, ETH_ALEN, mac);
J
James Ketrenos 已提交
2286 2287
}

2288 2289 2290 2291 2292
/*
 * NOTE: This must be executed from our workqueue as it results in udelay
 * being called which may corrupt the keyboard if executed on default
 * workqueue
 */
J
James Ketrenos 已提交
2293 2294 2295 2296 2297 2298 2299 2300
static void ipw_adapter_restart(void *adapter)
{
	struct ipw_priv *priv = adapter;

	if (priv->status & STATUS_RF_KILL_MASK)
		return;

	ipw_down(priv);
2301 2302 2303 2304 2305

	if (priv->assoc_network &&
	    (priv->assoc_network->capability & WLAN_CAPABILITY_IBSS))
		ipw_remove_current_network(priv);

J
James Ketrenos 已提交
2306 2307 2308 2309 2310 2311
	if (ipw_up(priv)) {
		IPW_ERROR("Failed to up device\n");
		return;
	}
}

D
David Howells 已提交
2312
static void ipw_bg_adapter_restart(struct work_struct *work)
2313
{
D
David Howells 已提交
2314 2315
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, adapter_restart);
2316
	mutex_lock(&priv->mutex);
D
David Howells 已提交
2317
	ipw_adapter_restart(priv);
2318
	mutex_unlock(&priv->mutex);
2319 2320
}

J
James Ketrenos 已提交
2321 2322 2323 2324 2325 2326 2327
#define IPW_SCAN_CHECK_WATCHDOG (5 * HZ)

static void ipw_scan_check(void *data)
{
	struct ipw_priv *priv = data;
	if (priv->status & (STATUS_SCANNING | STATUS_SCAN_ABORTING)) {
		IPW_DEBUG_SCAN("Scan completion watchdog resetting "
2328 2329
			       "adapter after (%dms).\n",
			       jiffies_to_msecs(IPW_SCAN_CHECK_WATCHDOG));
2330
		queue_work(priv->workqueue, &priv->adapter_restart);
J
James Ketrenos 已提交
2331 2332 2333
	}
}

D
David Howells 已提交
2334
static void ipw_bg_scan_check(struct work_struct *work)
2335
{
D
David Howells 已提交
2336 2337
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, scan_check.work);
2338
	mutex_lock(&priv->mutex);
D
David Howells 已提交
2339
	ipw_scan_check(priv);
2340
	mutex_unlock(&priv->mutex);
2341 2342
}

J
James Ketrenos 已提交
2343 2344 2345
static int ipw_send_scan_request_ext(struct ipw_priv *priv,
				     struct ipw_scan_request_ext *request)
{
Z
Zhu Yi 已提交
2346
	return ipw_send_cmd_pdu(priv, IPW_CMD_SCAN_REQUEST_EXT,
2347
				sizeof(*request), request);
J
James Ketrenos 已提交
2348 2349 2350 2351 2352 2353 2354 2355 2356
}

static int ipw_send_scan_abort(struct ipw_priv *priv)
{
	if (!priv) {
		IPW_ERROR("Invalid args\n");
		return -1;
	}

Z
Zhu Yi 已提交
2357
	return ipw_send_cmd_simple(priv, IPW_CMD_SCAN_ABORT);
J
James Ketrenos 已提交
2358 2359 2360 2361
}

static int ipw_set_sensitivity(struct ipw_priv *priv, u16 sens)
{
Z
Zhu Yi 已提交
2362
	struct ipw_sensitivity_calib calib = {
2363
		.beacon_rssi_raw = cpu_to_le16(sens),
J
James Ketrenos 已提交
2364
	};
Z
Zhu Yi 已提交
2365 2366

	return ipw_send_cmd_pdu(priv, IPW_CMD_SENSITIVITY_CALIB, sizeof(calib),
2367
				&calib);
J
James Ketrenos 已提交
2368 2369 2370 2371 2372
}

static int ipw_send_associate(struct ipw_priv *priv,
			      struct ipw_associate *associate)
{
Z
Zhu Yi 已提交
2373 2374 2375 2376 2377
	if (!priv || !associate) {
		IPW_ERROR("Invalid args\n");
		return -1;
	}

2378 2379
	return ipw_send_cmd_pdu(priv, IPW_CMD_ASSOCIATE, sizeof(*associate),
				associate);
J
James Ketrenos 已提交
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389
}

static int ipw_send_supported_rates(struct ipw_priv *priv,
				    struct ipw_supported_rates *rates)
{
	if (!priv || !rates) {
		IPW_ERROR("Invalid args\n");
		return -1;
	}

Z
Zhu Yi 已提交
2390
	return ipw_send_cmd_pdu(priv, IPW_CMD_SUPPORTED_RATES, sizeof(*rates),
2391
				rates);
J
James Ketrenos 已提交
2392 2393 2394 2395
}

static int ipw_set_random_seed(struct ipw_priv *priv)
{
Z
Zhu Yi 已提交
2396
	u32 val;
J
James Ketrenos 已提交
2397 2398 2399 2400 2401 2402

	if (!priv) {
		IPW_ERROR("Invalid args\n");
		return -1;
	}

Z
Zhu Yi 已提交
2403
	get_random_bytes(&val, sizeof(val));
J
James Ketrenos 已提交
2404

Z
Zhu Yi 已提交
2405
	return ipw_send_cmd_pdu(priv, IPW_CMD_SEED_NUMBER, sizeof(val), &val);
J
James Ketrenos 已提交
2406 2407 2408 2409
}

static int ipw_send_card_disable(struct ipw_priv *priv, u32 phy_off)
{
A
Al Viro 已提交
2410
	__le32 v = cpu_to_le32(phy_off);
J
James Ketrenos 已提交
2411 2412 2413 2414 2415
	if (!priv) {
		IPW_ERROR("Invalid args\n");
		return -1;
	}

A
Al Viro 已提交
2416
	return ipw_send_cmd_pdu(priv, IPW_CMD_CARD_DISABLE, sizeof(v), &v);
J
James Ketrenos 已提交
2417 2418
}

2419
static int ipw_send_tx_power(struct ipw_priv *priv, struct ipw_tx_power *power)
J
James Ketrenos 已提交
2420 2421 2422 2423 2424 2425
{
	if (!priv || !power) {
		IPW_ERROR("Invalid args\n");
		return -1;
	}

2426
	return ipw_send_cmd_pdu(priv, IPW_CMD_TX_POWER, sizeof(*power), power);
J
James Ketrenos 已提交
2427 2428
}

Z
Zhu Yi 已提交
2429 2430
static int ipw_set_tx_power(struct ipw_priv *priv)
{
2431
	const struct ieee80211_geo *geo = ieee80211_get_geo(priv->ieee);
Z
Zhu Yi 已提交
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446
	struct ipw_tx_power tx_power;
	s8 max_power;
	int i;

	memset(&tx_power, 0, sizeof(tx_power));

	/* configure device for 'G' band */
	tx_power.ieee_mode = IPW_G_MODE;
	tx_power.num_channels = geo->bg_channels;
	for (i = 0; i < geo->bg_channels; i++) {
		max_power = geo->bg[i].max_power;
		tx_power.channels_tx_power[i].channel_number =
		    geo->bg[i].channel;
		tx_power.channels_tx_power[i].tx_power = max_power ?
		    min(max_power, priv->tx_power) : priv->tx_power;
J
James Ketrenos 已提交
2447
	}
Z
Zhu Yi 已提交
2448 2449 2450 2451 2452 2453 2454
	if (ipw_send_tx_power(priv, &tx_power))
		return -EIO;

	/* configure device to also handle 'B' band */
	tx_power.ieee_mode = IPW_B_MODE;
	if (ipw_send_tx_power(priv, &tx_power))
		return -EIO;
2455

Z
Zhu Yi 已提交
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469
	/* configure device to also handle 'A' band */
	if (priv->ieee->abg_true) {
		tx_power.ieee_mode = IPW_A_MODE;
		tx_power.num_channels = geo->a_channels;
		for (i = 0; i < tx_power.num_channels; i++) {
			max_power = geo->a[i].max_power;
			tx_power.channels_tx_power[i].channel_number =
			    geo->a[i].channel;
			tx_power.channels_tx_power[i].tx_power = max_power ?
			    min(max_power, priv->tx_power) : priv->tx_power;
		}
		if (ipw_send_tx_power(priv, &tx_power))
			return -EIO;
	}
J
James Ketrenos 已提交
2470 2471 2472 2473 2474 2475
	return 0;
}

static int ipw_send_rts_threshold(struct ipw_priv *priv, u16 rts)
{
	struct ipw_rts_threshold rts_threshold = {
2476
		.rts_threshold = cpu_to_le16(rts),
J
James Ketrenos 已提交
2477 2478 2479 2480 2481 2482 2483
	};

	if (!priv) {
		IPW_ERROR("Invalid args\n");
		return -1;
	}

Z
Zhu Yi 已提交
2484 2485
	return ipw_send_cmd_pdu(priv, IPW_CMD_RTS_THRESHOLD,
				sizeof(rts_threshold), &rts_threshold);
J
James Ketrenos 已提交
2486 2487 2488 2489 2490
}

static int ipw_send_frag_threshold(struct ipw_priv *priv, u16 frag)
{
	struct ipw_frag_threshold frag_threshold = {
2491
		.frag_threshold = cpu_to_le16(frag),
J
James Ketrenos 已提交
2492 2493 2494 2495 2496 2497 2498
	};

	if (!priv) {
		IPW_ERROR("Invalid args\n");
		return -1;
	}

Z
Zhu Yi 已提交
2499 2500
	return ipw_send_cmd_pdu(priv, IPW_CMD_FRAG_THRESHOLD,
				sizeof(frag_threshold), &frag_threshold);
J
James Ketrenos 已提交
2501 2502 2503 2504
}

static int ipw_send_power_mode(struct ipw_priv *priv, u32 mode)
{
A
Al Viro 已提交
2505
	__le32 param;
J
James Ketrenos 已提交
2506 2507 2508 2509 2510

	if (!priv) {
		IPW_ERROR("Invalid args\n");
		return -1;
	}
2511

J
James Ketrenos 已提交
2512 2513 2514 2515
	/* If on battery, set to 3, if AC set to CAM, else user
	 * level */
	switch (mode) {
	case IPW_POWER_BATTERY:
A
Al Viro 已提交
2516
		param = cpu_to_le32(IPW_POWER_INDEX_3);
J
James Ketrenos 已提交
2517 2518
		break;
	case IPW_POWER_AC:
A
Al Viro 已提交
2519
		param = cpu_to_le32(IPW_POWER_MODE_CAM);
J
James Ketrenos 已提交
2520 2521
		break;
	default:
A
Al Viro 已提交
2522
		param = cpu_to_le32(mode);
J
James Ketrenos 已提交
2523 2524 2525
		break;
	}

Z
Zhu Yi 已提交
2526
	return ipw_send_cmd_pdu(priv, IPW_CMD_POWER_MODE, sizeof(param),
2527
				&param);
J
James Ketrenos 已提交
2528 2529
}

2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
static int ipw_send_retry_limit(struct ipw_priv *priv, u8 slimit, u8 llimit)
{
	struct ipw_retry_limit retry_limit = {
		.short_retry_limit = slimit,
		.long_retry_limit = llimit
	};

	if (!priv) {
		IPW_ERROR("Invalid args\n");
		return -1;
	}

Z
Zhu Yi 已提交
2542
	return ipw_send_cmd_pdu(priv, IPW_CMD_RETRY_LIMIT, sizeof(retry_limit),
2543
				&retry_limit);
2544 2545
}

J
James Ketrenos 已提交
2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566
/*
 * The IPW device contains a Microwire compatible EEPROM that stores
 * various data like the MAC address.  Usually the firmware has exclusive
 * access to the eeprom, but during device initialization (before the
 * device driver has sent the HostComplete command to the firmware) the
 * device driver has read access to the EEPROM by way of indirect addressing
 * through a couple of memory mapped registers.
 *
 * The following is a simplified implementation for pulling data out of the
 * the eeprom, along with some helper functions to find information in
 * the per device private data's copy of the eeprom.
 *
 * NOTE: To better understand how these functions work (i.e what is a chip
 *       select and why do have to keep driving the eeprom clock?), read
 *       just about any data sheet for a Microwire compatible EEPROM.
 */

/* write a 32 bit value into the indirect accessor register */
static inline void eeprom_write_reg(struct ipw_priv *p, u32 data)
{
	ipw_write_reg32(p, FW_MEM_REG_EEPROM_ACCESS, data);
2567

J
James Ketrenos 已提交
2568 2569 2570 2571 2572 2573 2574
	/* the eeprom requires some time to complete the operation */
	udelay(p->eeprom_delay);

	return;
}

/* perform a chip select operation */
2575
static void eeprom_cs(struct ipw_priv *priv)
J
James Ketrenos 已提交
2576
{
2577 2578 2579 2580
	eeprom_write_reg(priv, 0);
	eeprom_write_reg(priv, EEPROM_BIT_CS);
	eeprom_write_reg(priv, EEPROM_BIT_CS | EEPROM_BIT_SK);
	eeprom_write_reg(priv, EEPROM_BIT_CS);
J
James Ketrenos 已提交
2581 2582 2583
}

/* perform a chip select operation */
2584
static void eeprom_disable_cs(struct ipw_priv *priv)
J
James Ketrenos 已提交
2585
{
2586 2587 2588
	eeprom_write_reg(priv, EEPROM_BIT_CS);
	eeprom_write_reg(priv, 0);
	eeprom_write_reg(priv, EEPROM_BIT_SK);
J
James Ketrenos 已提交
2589 2590 2591
}

/* push a single bit down to the eeprom */
2592
static inline void eeprom_write_bit(struct ipw_priv *p, u8 bit)
J
James Ketrenos 已提交
2593
{
2594 2595 2596
	int d = (bit ? EEPROM_BIT_DI : 0);
	eeprom_write_reg(p, EEPROM_BIT_CS | d);
	eeprom_write_reg(p, EEPROM_BIT_CS | d | EEPROM_BIT_SK);
J
James Ketrenos 已提交
2597 2598 2599
}

/* push an opcode followed by an address down to the eeprom */
2600
static void eeprom_op(struct ipw_priv *priv, u8 op, u8 addr)
J
James Ketrenos 已提交
2601 2602 2603 2604
{
	int i;

	eeprom_cs(priv);
2605 2606 2607 2608 2609
	eeprom_write_bit(priv, 1);
	eeprom_write_bit(priv, op & 2);
	eeprom_write_bit(priv, op & 1);
	for (i = 7; i >= 0; i--) {
		eeprom_write_bit(priv, addr & (1 << i));
J
James Ketrenos 已提交
2610 2611 2612 2613
	}
}

/* pull 16 bits off the eeprom, one bit at a time */
2614
static u16 eeprom_read_u16(struct ipw_priv *priv, u8 addr)
J
James Ketrenos 已提交
2615 2616
{
	int i;
2617
	u16 r = 0;
2618

J
James Ketrenos 已提交
2619
	/* Send READ Opcode */
2620
	eeprom_op(priv, EEPROM_CMD_READ, addr);
J
James Ketrenos 已提交
2621 2622

	/* Send dummy bit */
2623
	eeprom_write_reg(priv, EEPROM_BIT_CS);
J
James Ketrenos 已提交
2624 2625

	/* Read the byte off the eeprom one bit at a time */
2626
	for (i = 0; i < 16; i++) {
J
James Ketrenos 已提交
2627
		u32 data = 0;
2628 2629 2630 2631
		eeprom_write_reg(priv, EEPROM_BIT_CS | EEPROM_BIT_SK);
		eeprom_write_reg(priv, EEPROM_BIT_CS);
		data = ipw_read_reg32(priv, FW_MEM_REG_EEPROM_ACCESS);
		r = (r << 1) | ((data & EEPROM_BIT_DO) ? 1 : 0);
J
James Ketrenos 已提交
2632
	}
2633

J
James Ketrenos 已提交
2634
	/* Send another dummy bit */
2635
	eeprom_write_reg(priv, 0);
J
James Ketrenos 已提交
2636
	eeprom_disable_cs(priv);
2637

J
James Ketrenos 已提交
2638 2639 2640 2641 2642
	return r;
}

/* helper function for pulling the mac address out of the private */
/* data's copy of the eeprom data                                 */
2643
static void eeprom_parse_mac(struct ipw_priv *priv, u8 * mac)
J
James Ketrenos 已提交
2644
{
2645
	memcpy(mac, &priv->eeprom[EEPROM_MAC_ADDRESS], 6);
J
James Ketrenos 已提交
2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658
}

/*
 * Either the device driver (i.e. the host) or the firmware can
 * load eeprom data into the designated region in SRAM.  If neither
 * happens then the FW will shutdown with a fatal error.
 *
 * In order to signal the FW to load the EEPROM, the EEPROM_LOAD_DISABLE
 * bit needs region of shared SRAM needs to be non-zero.
 */
static void ipw_eeprom_init_sram(struct ipw_priv *priv)
{
	int i;
A
Al Viro 已提交
2659
	__le16 *eeprom = (__le16 *) priv->eeprom;
2660

J
James Ketrenos 已提交
2661 2662 2663
	IPW_DEBUG_TRACE(">>\n");

	/* read entire contents of eeprom into private buffer */
2664
	for (i = 0; i < 128; i++)
A
Al Viro 已提交
2665
		eeprom[i] = cpu_to_le16(eeprom_read_u16(priv, (u8) i));
J
James Ketrenos 已提交
2666

2667 2668
	/*
	   If the data looks correct, then copy it to our private
J
James Ketrenos 已提交
2669
	   copy.  Otherwise let the firmware know to perform the operation
2670
	   on its own.
2671
	 */
2672
	if (priv->eeprom[EEPROM_VERSION] != 0) {
J
James Ketrenos 已提交
2673 2674 2675
		IPW_DEBUG_INFO("Writing EEPROM data into SRAM\n");

		/* write the eeprom data to sram */
2676
		for (i = 0; i < IPW_EEPROM_IMAGE_SIZE; i++)
2677
			ipw_write8(priv, IPW_EEPROM_DATA + i, priv->eeprom[i]);
J
James Ketrenos 已提交
2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690

		/* Do not load eeprom data on fatal error or suspend */
		ipw_write32(priv, IPW_EEPROM_LOAD_DISABLE, 0);
	} else {
		IPW_DEBUG_INFO("Enabling FW initializationg of SRAM\n");

		/* Load eeprom data on fatal error or suspend */
		ipw_write32(priv, IPW_EEPROM_LOAD_DISABLE, 1);
	}

	IPW_DEBUG_TRACE("<<\n");
}

2691
static void ipw_zero_memory(struct ipw_priv *priv, u32 start, u32 count)
J
James Ketrenos 已提交
2692 2693
{
	count >>= 2;
2694 2695
	if (!count)
		return;
2696
	_ipw_write32(priv, IPW_AUTOINC_ADDR, start);
2697
	while (count--)
2698
		_ipw_write32(priv, IPW_AUTOINC_DATA, 0);
J
James Ketrenos 已提交
2699 2700 2701 2702
}

static inline void ipw_fw_dma_reset_command_blocks(struct ipw_priv *priv)
{
2703
	ipw_zero_memory(priv, IPW_SHARED_SRAM_DMA_CONTROL,
2704
			CB_NUMBER_OF_ELEMENTS_SMALL *
J
James Ketrenos 已提交
2705 2706 2707 2708
			sizeof(struct command_block));
}

static int ipw_fw_dma_enable(struct ipw_priv *priv)
2709
{				/* start dma engine but no transfers yet */
J
James Ketrenos 已提交
2710 2711

	IPW_DEBUG_FW(">> : \n");
2712

J
James Ketrenos 已提交
2713 2714
	/* Start the dma */
	ipw_fw_dma_reset_command_blocks(priv);
2715

J
James Ketrenos 已提交
2716
	/* Write CB base address */
2717
	ipw_write_reg32(priv, IPW_DMA_I_CB_BASE, IPW_SHARED_SRAM_DMA_CONTROL);
J
James Ketrenos 已提交
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727

	IPW_DEBUG_FW("<< : \n");
	return 0;
}

static void ipw_fw_dma_abort(struct ipw_priv *priv)
{
	u32 control = 0;

	IPW_DEBUG_FW(">> :\n");
2728

2729
	/* set the Stop and Abort bit */
J
James Ketrenos 已提交
2730
	control = DMA_CONTROL_SMALL_CB_CONST_VALUE | DMA_CB_STOP_AND_ABORT;
2731
	ipw_write_reg32(priv, IPW_DMA_I_DMA_CONTROL, control);
J
James Ketrenos 已提交
2732
	priv->sram_desc.last_cb_index = 0;
2733

J
James Ketrenos 已提交
2734 2735 2736
	IPW_DEBUG_FW("<< \n");
}

2737 2738
static int ipw_fw_dma_write_command_block(struct ipw_priv *priv, int index,
					  struct command_block *cb)
J
James Ketrenos 已提交
2739
{
2740
	u32 address =
2741
	    IPW_SHARED_SRAM_DMA_CONTROL +
2742
	    (sizeof(struct command_block) * index);
J
James Ketrenos 已提交
2743 2744
	IPW_DEBUG_FW(">> :\n");

2745 2746
	ipw_write_indirect(priv, address, (u8 *) cb,
			   (int)sizeof(struct command_block));
J
James Ketrenos 已提交
2747 2748 2749 2750 2751 2752 2753 2754 2755

	IPW_DEBUG_FW("<< :\n");
	return 0;

}

static int ipw_fw_dma_kick(struct ipw_priv *priv)
{
	u32 control = 0;
2756
	u32 index = 0;
J
James Ketrenos 已提交
2757 2758

	IPW_DEBUG_FW(">> :\n");
2759

J
James Ketrenos 已提交
2760
	for (index = 0; index < priv->sram_desc.last_cb_index; index++)
2761 2762
		ipw_fw_dma_write_command_block(priv, index,
					       &priv->sram_desc.cb_list[index]);
J
James Ketrenos 已提交
2763 2764

	/* Enable the DMA in the CSR register */
2765 2766 2767
	ipw_clear_bit(priv, IPW_RESET_REG,
		      IPW_RESET_REG_MASTER_DISABLED |
		      IPW_RESET_REG_STOP_MASTER);
2768

2769
	/* Set the Start bit. */
J
James Ketrenos 已提交
2770
	control = DMA_CONTROL_SMALL_CB_CONST_VALUE | DMA_CB_START;
2771
	ipw_write_reg32(priv, IPW_DMA_I_DMA_CONTROL, control);
J
James Ketrenos 已提交
2772 2773 2774 2775 2776 2777 2778 2779

	IPW_DEBUG_FW("<< :\n");
	return 0;
}

static void ipw_fw_dma_dump_command_block(struct ipw_priv *priv)
{
	u32 address;
2780 2781
	u32 register_value = 0;
	u32 cb_fields_address = 0;
J
James Ketrenos 已提交
2782 2783

	IPW_DEBUG_FW(">> :\n");
2784
	address = ipw_read_reg32(priv, IPW_DMA_I_CURRENT_CB);
2785
	IPW_DEBUG_FW_INFO("Current CB is 0x%x \n", address);
J
James Ketrenos 已提交
2786 2787

	/* Read the DMA Controlor register */
2788 2789
	register_value = ipw_read_reg32(priv, IPW_DMA_I_DMA_CONTROL);
	IPW_DEBUG_FW_INFO("IPW_DMA_I_DMA_CONTROL is 0x%x \n", register_value);
J
James Ketrenos 已提交
2790

2791
	/* Print the CB values */
J
James Ketrenos 已提交
2792 2793
	cb_fields_address = address;
	register_value = ipw_read_reg32(priv, cb_fields_address);
2794
	IPW_DEBUG_FW_INFO("Current CB ControlField is 0x%x \n", register_value);
J
James Ketrenos 已提交
2795 2796 2797

	cb_fields_address += sizeof(u32);
	register_value = ipw_read_reg32(priv, cb_fields_address);
2798
	IPW_DEBUG_FW_INFO("Current CB Source Field is 0x%x \n", register_value);
J
James Ketrenos 已提交
2799 2800 2801 2802 2803 2804 2805 2806

	cb_fields_address += sizeof(u32);
	register_value = ipw_read_reg32(priv, cb_fields_address);
	IPW_DEBUG_FW_INFO("Current CB Destination Field is 0x%x \n",
			  register_value);

	cb_fields_address += sizeof(u32);
	register_value = ipw_read_reg32(priv, cb_fields_address);
2807
	IPW_DEBUG_FW_INFO("Current CB Status Field is 0x%x \n", register_value);
J
James Ketrenos 已提交
2808 2809 2810 2811 2812 2813 2814 2815 2816 2817

	IPW_DEBUG_FW(">> :\n");
}

static int ipw_fw_dma_command_block_index(struct ipw_priv *priv)
{
	u32 current_cb_address = 0;
	u32 current_cb_index = 0;

	IPW_DEBUG_FW("<< :\n");
2818
	current_cb_address = ipw_read_reg32(priv, IPW_DMA_I_CURRENT_CB);
2819

2820
	current_cb_index = (current_cb_address - IPW_SHARED_SRAM_DMA_CONTROL) /
2821
	    sizeof(struct command_block);
2822

J
James Ketrenos 已提交
2823
	IPW_DEBUG_FW_INFO("Current CB index 0x%x address = 0x%X \n",
2824
			  current_cb_index, current_cb_address);
J
James Ketrenos 已提交
2825 2826 2827 2828 2829 2830 2831 2832 2833 2834

	IPW_DEBUG_FW(">> :\n");
	return current_cb_index;

}

static int ipw_fw_dma_add_command_block(struct ipw_priv *priv,
					u32 src_address,
					u32 dest_address,
					u32 length,
2835
					int interrupt_enabled, int is_last)
J
James Ketrenos 已提交
2836 2837
{

2838
	u32 control = CB_VALID | CB_SRC_LE | CB_DEST_LE | CB_SRC_AUTOINC |
2839 2840
	    CB_SRC_IO_GATED | CB_DEST_AUTOINC | CB_SRC_SIZE_LONG |
	    CB_DEST_SIZE_LONG;
J
James Ketrenos 已提交
2841
	struct command_block *cb;
2842
	u32 last_cb_element = 0;
J
James Ketrenos 已提交
2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854

	IPW_DEBUG_FW_INFO("src_address=0x%x dest_address=0x%x length=0x%x\n",
			  src_address, dest_address, length);

	if (priv->sram_desc.last_cb_index >= CB_NUMBER_OF_ELEMENTS_SMALL)
		return -1;

	last_cb_element = priv->sram_desc.last_cb_index;
	cb = &priv->sram_desc.cb_list[last_cb_element];
	priv->sram_desc.last_cb_index++;

	/* Calculate the new CB control word */
2855
	if (interrupt_enabled)
J
James Ketrenos 已提交
2856 2857 2858 2859
		control |= CB_INT_ENABLED;

	if (is_last)
		control |= CB_LAST_VALID;
2860

J
James Ketrenos 已提交
2861 2862 2863
	control |= length;

	/* Calculate the CB Element's checksum value */
2864
	cb->status = control ^ src_address ^ dest_address;
J
James Ketrenos 已提交
2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876

	/* Copy the Source and Destination addresses */
	cb->dest_addr = dest_address;
	cb->source_addr = src_address;

	/* Copy the Control Word last */
	cb->control = control;

	return 0;
}

static int ipw_fw_dma_add_buffer(struct ipw_priv *priv,
2877
				 u32 src_phys, u32 dest_address, u32 length)
J
James Ketrenos 已提交
2878 2879
{
	u32 bytes_left = length;
2880 2881
	u32 src_offset = 0;
	u32 dest_offset = 0;
J
James Ketrenos 已提交
2882 2883 2884 2885 2886
	int status = 0;
	IPW_DEBUG_FW(">> \n");
	IPW_DEBUG_FW_INFO("src_phys=0x%x dest_address=0x%x length=0x%x\n",
			  src_phys, dest_address, length);
	while (bytes_left > CB_MAX_LENGTH) {
2887 2888 2889 2890 2891
		status = ipw_fw_dma_add_command_block(priv,
						      src_phys + src_offset,
						      dest_address +
						      dest_offset,
						      CB_MAX_LENGTH, 0, 0);
J
James Ketrenos 已提交
2892 2893 2894
		if (status) {
			IPW_DEBUG_FW_INFO(": Failed\n");
			return -1;
2895
		} else
J
James Ketrenos 已提交
2896 2897 2898 2899 2900 2901 2902 2903 2904
			IPW_DEBUG_FW_INFO(": Added new cb\n");

		src_offset += CB_MAX_LENGTH;
		dest_offset += CB_MAX_LENGTH;
		bytes_left -= CB_MAX_LENGTH;
	}

	/* add the buffer tail */
	if (bytes_left > 0) {
2905 2906 2907 2908
		status =
		    ipw_fw_dma_add_command_block(priv, src_phys + src_offset,
						 dest_address + dest_offset,
						 bytes_left, 0, 0);
J
James Ketrenos 已提交
2909 2910 2911
		if (status) {
			IPW_DEBUG_FW_INFO(": Failed on the buffer tail\n");
			return -1;
2912
		} else
2913 2914
			IPW_DEBUG_FW_INFO
			    (": Adding new cb - the buffer tail\n");
J
James Ketrenos 已提交
2915
	}
2916

J
James Ketrenos 已提交
2917 2918 2919 2920 2921 2922
	IPW_DEBUG_FW("<< \n");
	return 0;
}

static int ipw_fw_dma_wait(struct ipw_priv *priv)
{
2923
	u32 current_index = 0, previous_index;
J
James Ketrenos 已提交
2924 2925 2926 2927 2928
	u32 watchdog = 0;

	IPW_DEBUG_FW(">> : \n");

	current_index = ipw_fw_dma_command_block_index(priv);
2929
	IPW_DEBUG_FW_INFO("sram_desc.last_cb_index:0x%08X\n",
2930
			  (int)priv->sram_desc.last_cb_index);
J
James Ketrenos 已提交
2931 2932 2933

	while (current_index < priv->sram_desc.last_cb_index) {
		udelay(50);
2934
		previous_index = current_index;
J
James Ketrenos 已提交
2935 2936
		current_index = ipw_fw_dma_command_block_index(priv);

2937 2938 2939 2940 2941
		if (previous_index < current_index) {
			watchdog = 0;
			continue;
		}
		if (++watchdog > 400) {
J
James Ketrenos 已提交
2942 2943 2944 2945 2946 2947 2948 2949 2950
			IPW_DEBUG_FW_INFO("Timeout\n");
			ipw_fw_dma_dump_command_block(priv);
			ipw_fw_dma_abort(priv);
			return -1;
		}
	}

	ipw_fw_dma_abort(priv);

2951
	/*Disable the DMA in the CSR register */
2952 2953
	ipw_set_bit(priv, IPW_RESET_REG,
		    IPW_RESET_REG_MASTER_DISABLED | IPW_RESET_REG_STOP_MASTER);
J
James Ketrenos 已提交
2954 2955 2956 2957 2958

	IPW_DEBUG_FW("<< dmaWaitSync \n");
	return 0;
}

2959
static void ipw_remove_current_network(struct ipw_priv *priv)
J
James Ketrenos 已提交
2960 2961
{
	struct list_head *element, *safe;
2962
	struct ieee80211_network *network = NULL;
2963 2964 2965
	unsigned long flags;

	spin_lock_irqsave(&priv->ieee->lock, flags);
J
James Ketrenos 已提交
2966 2967 2968 2969
	list_for_each_safe(element, safe, &priv->ieee->network_list) {
		network = list_entry(element, struct ieee80211_network, list);
		if (!memcmp(network->bssid, priv->bssid, ETH_ALEN)) {
			list_del(element);
2970
			list_add_tail(&network->list,
J
James Ketrenos 已提交
2971 2972 2973
				      &priv->ieee->network_free_list);
		}
	}
2974
	spin_unlock_irqrestore(&priv->ieee->lock, flags);
J
James Ketrenos 已提交
2975 2976 2977
}

/**
2978
 * Check that card is still alive.
J
James Ketrenos 已提交
2979 2980 2981
 * Reads debug register from domain0.
 * If card is present, pre-defined value should
 * be found there.
2982
 *
J
James Ketrenos 已提交
2983 2984 2985 2986 2987 2988 2989 2990
 * @param priv
 * @return 1 if card is present, 0 otherwise
 */
static inline int ipw_alive(struct ipw_priv *priv)
{
	return ipw_read32(priv, 0x90) == 0xd55555d5;
}

2991
/* timeout in msec, attempted in 10-msec quanta */
2992
static int ipw_poll_bit(struct ipw_priv *priv, u32 addr, u32 mask,
J
James Ketrenos 已提交
2993 2994 2995 2996 2997
			       int timeout)
{
	int i = 0;

	do {
2998
		if ((ipw_read32(priv, addr) & mask) == mask)
J
James Ketrenos 已提交
2999 3000 3001 3002
			return i;
		mdelay(10);
		i += 10;
	} while (i < timeout);
3003

J
James Ketrenos 已提交
3004 3005 3006
	return -ETIME;
}

3007
/* These functions load the firmware and micro code for the operation of
J
James Ketrenos 已提交
3008 3009 3010 3011
 * the ipw hardware.  It assumes the buffer has all the bits for the
 * image and the caller is handling the memory allocation and clean up.
 */

3012
static int ipw_stop_master(struct ipw_priv *priv)
J
James Ketrenos 已提交
3013 3014
{
	int rc;
3015

J
James Ketrenos 已提交
3016 3017
	IPW_DEBUG_TRACE(">> \n");
	/* stop master. typical delay - 0 */
3018
	ipw_set_bit(priv, IPW_RESET_REG, IPW_RESET_REG_STOP_MASTER);
J
James Ketrenos 已提交
3019

3020
	/* timeout is in msec, polled in 10-msec quanta */
3021 3022
	rc = ipw_poll_bit(priv, IPW_RESET_REG,
			  IPW_RESET_REG_MASTER_DISABLED, 100);
J
James Ketrenos 已提交
3023
	if (rc < 0) {
3024
		IPW_ERROR("wait for stop master failed after 100ms\n");
J
James Ketrenos 已提交
3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037
		return -1;
	}

	IPW_DEBUG_INFO("stop master %dms\n", rc);

	return rc;
}

static void ipw_arc_release(struct ipw_priv *priv)
{
	IPW_DEBUG_TRACE(">> \n");
	mdelay(5);

3038
	ipw_clear_bit(priv, IPW_RESET_REG, CBD_RESET_REG_PRINCETON_RESET);
J
James Ketrenos 已提交
3039 3040 3041 3042 3043 3044

	/* no one knows timing, for safety add some delay */
	mdelay(5);
}

struct fw_chunk {
A
Al Viro 已提交
3045 3046
	__le32 address;
	__le32 length;
J
James Ketrenos 已提交
3047 3048
};

3049
static int ipw_load_ucode(struct ipw_priv *priv, u8 * data, size_t len)
J
James Ketrenos 已提交
3050 3051 3052
{
	int rc = 0, i, addr;
	u8 cr = 0;
A
Al Viro 已提交
3053
	__le16 *image;
J
James Ketrenos 已提交
3054

A
Al Viro 已提交
3055
	image = (__le16 *) data;
3056

J
James Ketrenos 已提交
3057 3058 3059 3060 3061 3062
	IPW_DEBUG_TRACE(">> \n");

	rc = ipw_stop_master(priv);

	if (rc < 0)
		return rc;
3063

3064 3065
	for (addr = IPW_SHARED_LOWER_BOUND;
	     addr < IPW_REGISTER_DOMAIN1_END; addr += 4) {
J
James Ketrenos 已提交
3066 3067 3068 3069 3070 3071 3072 3073
		ipw_write32(priv, addr, 0);
	}

	/* no ucode (yet) */
	memset(&priv->dino_alive, 0, sizeof(priv->dino_alive));
	/* destroy DMA queues */
	/* reset sequence */

3074
	ipw_write_reg32(priv, IPW_MEM_HALT_AND_RESET, IPW_BIT_HALT_RESET_ON);
J
James Ketrenos 已提交
3075
	ipw_arc_release(priv);
3076
	ipw_write_reg32(priv, IPW_MEM_HALT_AND_RESET, IPW_BIT_HALT_RESET_OFF);
J
James Ketrenos 已提交
3077 3078 3079
	mdelay(1);

	/* reset PHY */
3080
	ipw_write_reg32(priv, IPW_INTERNAL_CMD_EVENT, IPW_BASEBAND_POWER_DOWN);
J
James Ketrenos 已提交
3081
	mdelay(1);
3082

3083
	ipw_write_reg32(priv, IPW_INTERNAL_CMD_EVENT, 0);
J
James Ketrenos 已提交
3084
	mdelay(1);
3085

J
James Ketrenos 已提交
3086
	/* enable ucode store */
3087 3088
	ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, 0x0);
	ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, DINO_ENABLE_CS);
J
James Ketrenos 已提交
3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100
	mdelay(1);

	/* write ucode */
	/**
	 * @bug
	 * Do NOT set indirect address register once and then
	 * store data to indirect data register in the loop.
	 * It seems very reasonable, but in this case DINO do not
	 * accept ucode. It is essential to set address each time.
	 */
	/* load new ipw uCode */
	for (i = 0; i < len / 2; i++)
3101
		ipw_write_reg16(priv, IPW_BASEBAND_CONTROL_STORE,
A
Al Viro 已提交
3102
				le16_to_cpu(image[i]));
J
James Ketrenos 已提交
3103 3104

	/* enable DINO */
3105 3106
	ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, 0);
	ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, DINO_ENABLE_SYSTEM);
J
James Ketrenos 已提交
3107

3108
	/* this is where the igx / win driver deveates from the VAP driver. */
J
James Ketrenos 已提交
3109 3110 3111 3112

	/* wait for alive response */
	for (i = 0; i < 100; i++) {
		/* poll for incoming data */
3113
		cr = ipw_read_reg8(priv, IPW_BASEBAND_CONTROL_STATUS);
J
James Ketrenos 已提交
3114 3115 3116 3117 3118 3119 3120
		if (cr & DINO_RXFIFO_DATA)
			break;
		mdelay(1);
	}

	if (cr & DINO_RXFIFO_DATA) {
		/* alive_command_responce size is NOT multiple of 4 */
A
Al Viro 已提交
3121
		__le32 response_buffer[(sizeof(priv->dino_alive) + 3) / 4];
3122 3123

		for (i = 0; i < ARRAY_SIZE(response_buffer); i++)
J
James Ketrenos 已提交
3124
			response_buffer[i] =
A
Al Viro 已提交
3125
			    cpu_to_le32(ipw_read_reg32(priv,
3126
						       IPW_BASEBAND_RX_FIFO_READ));
J
James Ketrenos 已提交
3127 3128 3129 3130 3131
		memcpy(&priv->dino_alive, response_buffer,
		       sizeof(priv->dino_alive));
		if (priv->dino_alive.alive_command == 1
		    && priv->dino_alive.ucode_valid == 1) {
			rc = 0;
3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
			IPW_DEBUG_INFO
			    ("Microcode OK, rev. %d (0x%x) dev. %d (0x%x) "
			     "of %02d/%02d/%02d %02d:%02d\n",
			     priv->dino_alive.software_revision,
			     priv->dino_alive.software_revision,
			     priv->dino_alive.device_identifier,
			     priv->dino_alive.device_identifier,
			     priv->dino_alive.time_stamp[0],
			     priv->dino_alive.time_stamp[1],
			     priv->dino_alive.time_stamp[2],
			     priv->dino_alive.time_stamp[3],
			     priv->dino_alive.time_stamp[4]);
J
James Ketrenos 已提交
3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154
		} else {
			IPW_DEBUG_INFO("Microcode is not alive\n");
			rc = -EINVAL;
		}
	} else {
		IPW_DEBUG_INFO("No alive response from DINO\n");
		rc = -ETIME;
	}

	/* disable DINO, otherwise for some reason
	   firmware have problem getting alive resp. */
3155
	ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, 0);
J
James Ketrenos 已提交
3156 3157 3158 3159

	return rc;
}

3160
static int ipw_load_firmware(struct ipw_priv *priv, u8 * data, size_t len)
J
James Ketrenos 已提交
3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178
{
	int rc = -1;
	int offset = 0;
	struct fw_chunk *chunk;
	dma_addr_t shared_phys;
	u8 *shared_virt;

	IPW_DEBUG_TRACE("<< : \n");
	shared_virt = pci_alloc_consistent(priv->pci_dev, len, &shared_phys);

	if (!shared_virt)
		return -ENOMEM;

	memmove(shared_virt, data, len);

	/* Start the Dma */
	rc = ipw_fw_dma_enable(priv);

3179 3180
	/* the DMA is already ready this would be a bug. */
	BUG_ON(priv->sram_desc.last_cb_index > 0);
J
James Ketrenos 已提交
3181 3182 3183 3184 3185

	do {
		chunk = (struct fw_chunk *)(data + offset);
		offset += sizeof(struct fw_chunk);
		/* build DMA packet and queue up for sending */
3186
		/* dma to chunk->address, the chunk->length bytes from data +
J
James Ketrenos 已提交
3187 3188 3189
		 * offeset*/
		/* Dma loading */
		rc = ipw_fw_dma_add_buffer(priv, shared_phys + offset,
3190 3191
					   le32_to_cpu(chunk->address),
					   le32_to_cpu(chunk->length));
J
James Ketrenos 已提交
3192 3193 3194 3195
		if (rc) {
			IPW_DEBUG_INFO("dmaAddBuffer Failed\n");
			goto out;
		}
3196

3197
		offset += le32_to_cpu(chunk->length);
J
James Ketrenos 已提交
3198 3199
	} while (offset < len);

3200
	/* Run the DMA and wait for the answer */
J
James Ketrenos 已提交
3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211
	rc = ipw_fw_dma_kick(priv);
	if (rc) {
		IPW_ERROR("dmaKick Failed\n");
		goto out;
	}

	rc = ipw_fw_dma_wait(priv);
	if (rc) {
		IPW_ERROR("dmaWaitSync Failed\n");
		goto out;
	}
3212 3213
      out:
	pci_free_consistent(priv->pci_dev, len, shared_virt, shared_phys);
J
James Ketrenos 已提交
3214 3215 3216 3217 3218 3219 3220 3221
	return rc;
}

/* stop nic */
static int ipw_stop_nic(struct ipw_priv *priv)
{
	int rc = 0;

3222
	/* stop */
3223
	ipw_write32(priv, IPW_RESET_REG, IPW_RESET_REG_STOP_MASTER);
3224

3225 3226
	rc = ipw_poll_bit(priv, IPW_RESET_REG,
			  IPW_RESET_REG_MASTER_DISABLED, 500);
J
James Ketrenos 已提交
3227
	if (rc < 0) {
3228
		IPW_ERROR("wait for reg master disabled failed after 500ms\n");
J
James Ketrenos 已提交
3229
		return rc;
3230
	}
J
James Ketrenos 已提交
3231

3232
	ipw_set_bit(priv, IPW_RESET_REG, CBD_RESET_REG_PRINCETON_RESET);
3233

J
James Ketrenos 已提交
3234 3235 3236 3237 3238 3239 3240
	return rc;
}

static void ipw_start_nic(struct ipw_priv *priv)
{
	IPW_DEBUG_TRACE(">>\n");

3241
	/* prvHwStartNic  release ARC */
3242 3243 3244
	ipw_clear_bit(priv, IPW_RESET_REG,
		      IPW_RESET_REG_MASTER_DISABLED |
		      IPW_RESET_REG_STOP_MASTER |
J
James Ketrenos 已提交
3245
		      CBD_RESET_REG_PRINCETON_RESET);
3246

J
James Ketrenos 已提交
3247
	/* enable power management */
3248 3249
	ipw_set_bit(priv, IPW_GP_CNTRL_RW,
		    IPW_GP_CNTRL_BIT_HOST_ALLOWS_STANDBY);
J
James Ketrenos 已提交
3250 3251 3252

	IPW_DEBUG_TRACE("<<\n");
}
3253

J
James Ketrenos 已提交
3254 3255 3256 3257 3258
static int ipw_init_nic(struct ipw_priv *priv)
{
	int rc;

	IPW_DEBUG_TRACE(">>\n");
3259
	/* reset */
J
James Ketrenos 已提交
3260 3261
	/*prvHwInitNic */
	/* set "initialization complete" bit to move adapter to D0 state */
3262
	ipw_set_bit(priv, IPW_GP_CNTRL_RW, IPW_GP_CNTRL_BIT_INIT_DONE);
J
James Ketrenos 已提交
3263 3264

	/* low-level PLL activation */
3265 3266
	ipw_write32(priv, IPW_READ_INT_REGISTER,
		    IPW_BIT_INT_HOST_SRAM_READ_INT_REGISTER);
J
James Ketrenos 已提交
3267 3268

	/* wait for clock stabilization */
3269 3270
	rc = ipw_poll_bit(priv, IPW_GP_CNTRL_RW,
			  IPW_GP_CNTRL_BIT_CLOCK_READY, 250);
3271
	if (rc < 0)
J
James Ketrenos 已提交
3272 3273 3274
		IPW_DEBUG_INFO("FAILED wait for clock stablization\n");

	/* assert SW reset */
3275
	ipw_set_bit(priv, IPW_RESET_REG, IPW_RESET_REG_SW_RESET);
J
James Ketrenos 已提交
3276 3277 3278 3279

	udelay(10);

	/* set "initialization complete" bit to move adapter to D0 state */
3280
	ipw_set_bit(priv, IPW_GP_CNTRL_RW, IPW_GP_CNTRL_BIT_INIT_DONE);
J
James Ketrenos 已提交
3281 3282 3283 3284 3285

	IPW_DEBUG_TRACE(">>\n");
	return 0;
}

3286
/* Call this function from process context, it will sleep in request_firmware.
J
James Ketrenos 已提交
3287 3288 3289 3290 3291
 * Probe is an ok place to call this from.
 */
static int ipw_reset_nic(struct ipw_priv *priv)
{
	int rc = 0;
3292
	unsigned long flags;
J
James Ketrenos 已提交
3293 3294

	IPW_DEBUG_TRACE(">>\n");
3295

J
James Ketrenos 已提交
3296
	rc = ipw_init_nic(priv);
3297

3298
	spin_lock_irqsave(&priv->lock, flags);
J
James Ketrenos 已提交
3299 3300 3301
	/* Clear the 'host command active' bit... */
	priv->status &= ~STATUS_HCMD_ACTIVE;
	wake_up_interruptible(&priv->wait_command_queue);
3302 3303
	priv->status &= ~(STATUS_SCANNING | STATUS_SCAN_ABORTING);
	wake_up_interruptible(&priv->wait_state);
3304
	spin_unlock_irqrestore(&priv->lock, flags);
J
James Ketrenos 已提交
3305 3306 3307

	IPW_DEBUG_TRACE("<<\n");
	return rc;
3308
}
J
James Ketrenos 已提交
3309

3310 3311

struct ipw_fw {
3312 3313 3314 3315
	__le32 ver;
	__le32 boot_size;
	__le32 ucode_size;
	__le32 fw_size;
3316 3317 3318
	u8 data[0];
};

3319
static int ipw_get_fw(struct ipw_priv *priv,
3320
		      const struct firmware **raw, const char *name)
J
James Ketrenos 已提交
3321
{
3322
	struct ipw_fw *fw;
J
James Ketrenos 已提交
3323 3324 3325
	int rc;

	/* ask firmware_class module to get the boot firmware off disk */
3326
	rc = request_firmware(raw, name, &priv->pci_dev->dev);
J
James Ketrenos 已提交
3327
	if (rc < 0) {
3328
		IPW_ERROR("%s request_firmware failed: Reason %d\n", name, rc);
J
James Ketrenos 已提交
3329
		return rc;
3330
	}
J
James Ketrenos 已提交
3331

3332 3333 3334 3335 3336 3337 3338
	if ((*raw)->size < sizeof(*fw)) {
		IPW_ERROR("%s is too small (%zd)\n", name, (*raw)->size);
		return -EINVAL;
	}

	fw = (void *)(*raw)->data;

3339 3340
	if ((*raw)->size < sizeof(*fw) + le32_to_cpu(fw->boot_size) +
	    le32_to_cpu(fw->ucode_size) + le32_to_cpu(fw->fw_size)) {
3341 3342
		IPW_ERROR("%s is too small or corrupt (%zd)\n",
			  name, (*raw)->size);
J
James Ketrenos 已提交
3343 3344 3345
		return -EINVAL;
	}

3346
	IPW_DEBUG_INFO("Read firmware '%s' image v%d.%d (%zd bytes)\n",
J
James Ketrenos 已提交
3347
		       name,
3348 3349 3350
		       le32_to_cpu(fw->ver) >> 16,
		       le32_to_cpu(fw->ver) & 0xff,
		       (*raw)->size - sizeof(*fw));
J
James Ketrenos 已提交
3351 3352 3353
	return 0;
}

3354
#define IPW_RX_BUF_SIZE (3000)
J
James Ketrenos 已提交
3355

3356
static void ipw_rx_queue_reset(struct ipw_priv *priv,
J
James Ketrenos 已提交
3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372
				      struct ipw_rx_queue *rxq)
{
	unsigned long flags;
	int i;

	spin_lock_irqsave(&rxq->lock, flags);

	INIT_LIST_HEAD(&rxq->rx_free);
	INIT_LIST_HEAD(&rxq->rx_used);

	/* Fill the rx_used queue with _all_ of the Rx buffers */
	for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
		/* In the reset function, these buffers may have been allocated
		 * to an SKB, so we need to unmap and free potential storage */
		if (rxq->pool[i].skb != NULL) {
			pci_unmap_single(priv->pci_dev, rxq->pool[i].dma_addr,
3373
					 IPW_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
J
James Ketrenos 已提交
3374
			dev_kfree_skb(rxq->pool[i].skb);
3375
			rxq->pool[i].skb = NULL;
J
James Ketrenos 已提交
3376 3377 3378
		}
		list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
	}
3379

J
James Ketrenos 已提交
3380 3381 3382 3383 3384 3385 3386 3387 3388
	/* Set us so that we have processed and used all buffers, but have
	 * not restocked the Rx queue with fresh buffers */
	rxq->read = rxq->write = 0;
	rxq->free_count = 0;
	spin_unlock_irqrestore(&rxq->lock, flags);
}

#ifdef CONFIG_PM
static int fw_loaded = 0;
3389
static const struct firmware *raw = NULL;
3390 3391 3392 3393

static void free_firmware(void)
{
	if (fw_loaded) {
3394 3395
		release_firmware(raw);
		raw = NULL;
3396 3397 3398 3399 3400
		fw_loaded = 0;
	}
}
#else
#define free_firmware() do {} while (0)
J
James Ketrenos 已提交
3401 3402 3403 3404 3405
#endif

static int ipw_load(struct ipw_priv *priv)
{
#ifndef CONFIG_PM
3406
	const struct firmware *raw = NULL;
J
James Ketrenos 已提交
3407
#endif
3408 3409 3410
	struct ipw_fw *fw;
	u8 *boot_img, *ucode_img, *fw_img;
	u8 *name = NULL;
J
James Ketrenos 已提交
3411 3412
	int rc = 0, retries = 3;

3413 3414
	switch (priv->ieee->iw_mode) {
	case IW_MODE_ADHOC:
3415
		name = "ipw2200-ibss.fw";
3416
		break;
3417
#ifdef CONFIG_IPW2200_MONITOR
3418
	case IW_MODE_MONITOR:
3419
		name = "ipw2200-sniffer.fw";
3420
		break;
J
James Ketrenos 已提交
3421
#endif
3422
	case IW_MODE_INFRA:
3423
		name = "ipw2200-bss.fw";
3424
		break;
3425 3426 3427
	}

	if (!name) {
3428
		rc = -EINVAL;
3429 3430 3431 3432 3433 3434 3435 3436 3437 3438
		goto error;
	}

#ifdef CONFIG_PM
	if (!fw_loaded) {
#endif
		rc = ipw_get_fw(priv, &raw, name);
		if (rc < 0)
			goto error;
#ifdef CONFIG_PM
J
James Ketrenos 已提交
3439
	}
3440 3441 3442 3443
#endif

	fw = (void *)raw->data;
	boot_img = &fw->data[0];
3444 3445 3446
	ucode_img = &fw->data[le32_to_cpu(fw->boot_size)];
	fw_img = &fw->data[le32_to_cpu(fw->boot_size) +
			   le32_to_cpu(fw->ucode_size)];
3447 3448 3449

	if (rc < 0)
		goto error;
J
James Ketrenos 已提交
3450 3451 3452 3453 3454 3455 3456 3457 3458 3459

	if (!priv->rxq)
		priv->rxq = ipw_rx_queue_alloc(priv);
	else
		ipw_rx_queue_reset(priv, priv->rxq);
	if (!priv->rxq) {
		IPW_ERROR("Unable to initialize Rx queue\n");
		goto error;
	}

3460
      retry:
J
James Ketrenos 已提交
3461
	/* Ensure interrupts are disabled */
3462
	ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);
J
James Ketrenos 已提交
3463 3464 3465
	priv->status &= ~STATUS_INT_ENABLED;

	/* ack pending interrupts */
3466
	ipw_write32(priv, IPW_INTA_RW, IPW_INTA_MASK_ALL);
3467

J
James Ketrenos 已提交
3468 3469 3470
	ipw_stop_nic(priv);

	rc = ipw_reset_nic(priv);
3471
	if (rc < 0) {
J
James Ketrenos 已提交
3472 3473 3474 3475
		IPW_ERROR("Unable to reset NIC\n");
		goto error;
	}

3476 3477
	ipw_zero_memory(priv, IPW_NIC_SRAM_LOWER_BOUND,
			IPW_NIC_SRAM_UPPER_BOUND - IPW_NIC_SRAM_LOWER_BOUND);
J
James Ketrenos 已提交
3478 3479

	/* DMA the initial boot firmware into the device */
3480
	rc = ipw_load_firmware(priv, boot_img, le32_to_cpu(fw->boot_size));
J
James Ketrenos 已提交
3481
	if (rc < 0) {
3482
		IPW_ERROR("Unable to load boot firmware: %d\n", rc);
J
James Ketrenos 已提交
3483 3484 3485 3486 3487 3488
		goto error;
	}

	/* kick start the device */
	ipw_start_nic(priv);

3489
	/* wait for the device to finish its initial startup sequence */
3490 3491
	rc = ipw_poll_bit(priv, IPW_INTA_RW,
			  IPW_INTA_BIT_FW_INITIALIZATION_DONE, 500);
J
James Ketrenos 已提交
3492 3493 3494 3495 3496 3497
	if (rc < 0) {
		IPW_ERROR("device failed to boot initial fw image\n");
		goto error;
	}
	IPW_DEBUG_INFO("initial device response after %dms\n", rc);

3498
	/* ack fw init done interrupt */
3499
	ipw_write32(priv, IPW_INTA_RW, IPW_INTA_BIT_FW_INITIALIZATION_DONE);
J
James Ketrenos 已提交
3500 3501

	/* DMA the ucode into the device */
3502
	rc = ipw_load_ucode(priv, ucode_img, le32_to_cpu(fw->ucode_size));
J
James Ketrenos 已提交
3503
	if (rc < 0) {
3504
		IPW_ERROR("Unable to load ucode: %d\n", rc);
J
James Ketrenos 已提交
3505 3506
		goto error;
	}
3507

J
James Ketrenos 已提交
3508 3509 3510 3511
	/* stop nic */
	ipw_stop_nic(priv);

	/* DMA bss firmware into the device */
3512
	rc = ipw_load_firmware(priv, fw_img, le32_to_cpu(fw->fw_size));
3513
	if (rc < 0) {
3514
		IPW_ERROR("Unable to load firmware: %d\n", rc);
J
James Ketrenos 已提交
3515 3516
		goto error;
	}
3517 3518 3519 3520
#ifdef CONFIG_PM
	fw_loaded = 1;
#endif

J
James Ketrenos 已提交
3521 3522 3523
	ipw_write32(priv, IPW_EEPROM_LOAD_DISABLE, 0);

	rc = ipw_queue_reset(priv);
3524
	if (rc < 0) {
J
James Ketrenos 已提交
3525 3526 3527 3528 3529
		IPW_ERROR("Unable to initialize queues\n");
		goto error;
	}

	/* Ensure interrupts are disabled */
3530
	ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);
3531
	/* ack pending interrupts */
3532
	ipw_write32(priv, IPW_INTA_RW, IPW_INTA_MASK_ALL);
3533

J
James Ketrenos 已提交
3534 3535 3536
	/* kick start the device */
	ipw_start_nic(priv);

3537
	if (ipw_read32(priv, IPW_INTA_RW) & IPW_INTA_BIT_PARITY_ERROR) {
J
James Ketrenos 已提交
3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549
		if (retries > 0) {
			IPW_WARNING("Parity error.  Retrying init.\n");
			retries--;
			goto retry;
		}

		IPW_ERROR("TODO: Handle parity error -- schedule restart?\n");
		rc = -EIO;
		goto error;
	}

	/* wait for the device */
3550 3551
	rc = ipw_poll_bit(priv, IPW_INTA_RW,
			  IPW_INTA_BIT_FW_INITIALIZATION_DONE, 500);
J
James Ketrenos 已提交
3552
	if (rc < 0) {
3553
		IPW_ERROR("device failed to start within 500ms\n");
J
James Ketrenos 已提交
3554 3555 3556 3557 3558
		goto error;
	}
	IPW_DEBUG_INFO("device response after %dms\n", rc);

	/* ack fw init done interrupt */
3559
	ipw_write32(priv, IPW_INTA_RW, IPW_INTA_BIT_FW_INITIALIZATION_DONE);
J
James Ketrenos 已提交
3560 3561 3562

	/* read eeprom data and initialize the eeprom region of sram */
	priv->eeprom_delay = 1;
3563
	ipw_eeprom_init_sram(priv);
J
James Ketrenos 已提交
3564 3565 3566 3567 3568 3569 3570

	/* enable interrupts */
	ipw_enable_interrupts(priv);

	/* Ensure our queue has valid packets */
	ipw_rx_queue_replenish(priv);

3571
	ipw_write32(priv, IPW_RX_READ_INDEX, priv->rxq->read);
J
James Ketrenos 已提交
3572 3573

	/* ack pending interrupts */
3574
	ipw_write32(priv, IPW_INTA_RW, IPW_INTA_MASK_ALL);
J
James Ketrenos 已提交
3575 3576

#ifndef CONFIG_PM
3577
	release_firmware(raw);
J
James Ketrenos 已提交
3578 3579 3580
#endif
	return 0;

3581
      error:
J
James Ketrenos 已提交
3582 3583 3584 3585 3586
	if (priv->rxq) {
		ipw_rx_queue_free(priv, priv->rxq);
		priv->rxq = NULL;
	}
	ipw_tx_queue_free(priv);
3587 3588
	if (raw)
		release_firmware(raw);
J
James Ketrenos 已提交
3589 3590
#ifdef CONFIG_PM
	fw_loaded = 0;
3591
	raw = NULL;
J
James Ketrenos 已提交
3592 3593 3594 3595 3596
#endif

	return rc;
}

3597
/**
J
James Ketrenos 已提交
3598 3599 3600 3601 3602 3603 3604 3605
 * DMA services
 *
 * Theory of operation
 *
 * A queue is a circular buffers with 'Read' and 'Write' pointers.
 * 2 empty entries always kept in the buffer to protect from overflow.
 *
 * For Tx queue, there are low mark and high mark limits. If, after queuing
3606 3607
 * the packet for Tx, free space become < low mark, Tx queue stopped. When
 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
J
James Ketrenos 已提交
3608 3609 3610 3611
 * Tx queue resumed.
 *
 * The IPW operates with six queues, one receive queue in the device's
 * sram, one transmit queue for sending commands to the device firmware,
3612
 * and four transmit queues for data.
J
James Ketrenos 已提交
3613
 *
3614
 * The four transmit queues allow for performing quality of service (qos)
J
James Ketrenos 已提交
3615
 * transmissions as per the 802.11 protocol.  Currently Linux does not
3616
 * provide a mechanism to the user for utilizing prioritized queues, so
J
James Ketrenos 已提交
3617 3618 3619 3620 3621 3622 3623
 * we only utilize the first data transmit queue (queue1).
 */

/**
 * Driver allocates buffers of this size for Rx
 */

3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639
/**
 * ipw_rx_queue_space - Return number of free slots available in queue.
 */
static int ipw_rx_queue_space(const struct ipw_rx_queue *q)
{
	int s = q->read - q->write;
	if (s <= 0)
		s += RX_QUEUE_SIZE;
	/* keep some buffer to not confuse full and empty queue */
	s -= 2;
	if (s < 0)
		s = 0;
	return s;
}

static inline int ipw_tx_queue_space(const struct clx2_queue *q)
J
James Ketrenos 已提交
3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656
{
	int s = q->last_used - q->first_empty;
	if (s <= 0)
		s += q->n_bd;
	s -= 2;			/* keep some reserve to not confuse empty and full situations */
	if (s < 0)
		s = 0;
	return s;
}

static inline int ipw_queue_inc_wrap(int index, int n_bd)
{
	return (++index == n_bd) ? 0 : index;
}

/**
 * Initialize common DMA queue structure
3657
 *
J
James Ketrenos 已提交
3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668
 * @param q                queue to init
 * @param count            Number of BD's to allocate. Should be power of 2
 * @param read_register    Address for 'read' register
 *                         (not offset within BAR, full address)
 * @param write_register   Address for 'write' register
 *                         (not offset within BAR, full address)
 * @param base_register    Address for 'base' register
 *                         (not offset within BAR, full address)
 * @param size             Address for 'size' register
 *                         (not offset within BAR, full address)
 */
3669
static void ipw_queue_init(struct ipw_priv *priv, struct clx2_queue *q,
3670
			   int count, u32 read, u32 write, u32 base, u32 size)
J
James Ketrenos 已提交
3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693
{
	q->n_bd = count;

	q->low_mark = q->n_bd / 4;
	if (q->low_mark < 4)
		q->low_mark = 4;

	q->high_mark = q->n_bd / 8;
	if (q->high_mark < 2)
		q->high_mark = 2;

	q->first_empty = q->last_used = 0;
	q->reg_r = read;
	q->reg_w = write;

	ipw_write32(priv, base, q->dma_addr);
	ipw_write32(priv, size, count);
	ipw_write32(priv, read, 0);
	ipw_write32(priv, write, 0);

	_ipw_read32(priv, 0x90);
}

3694
static int ipw_queue_tx_init(struct ipw_priv *priv,
J
James Ketrenos 已提交
3695
			     struct clx2_tx_queue *q,
3696
			     int count, u32 read, u32 write, u32 base, u32 size)
J
James Ketrenos 已提交
3697 3698 3699 3700 3701 3702 3703 3704 3705
{
	struct pci_dev *dev = priv->pci_dev;

	q->txb = kmalloc(sizeof(q->txb[0]) * count, GFP_KERNEL);
	if (!q->txb) {
		IPW_ERROR("vmalloc for auxilary BD structures failed\n");
		return -ENOMEM;
	}

3706 3707
	q->bd =
	    pci_alloc_consistent(dev, sizeof(q->bd[0]) * count, &q->q.dma_addr);
J
James Ketrenos 已提交
3708
	if (!q->bd) {
3709
		IPW_ERROR("pci_alloc_consistent(%zd) failed\n",
3710
			  sizeof(q->bd[0]) * count);
J
James Ketrenos 已提交
3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722
		kfree(q->txb);
		q->txb = NULL;
		return -ENOMEM;
	}

	ipw_queue_init(priv, &q->q, count, read, write, base, size);
	return 0;
}

/**
 * Free one TFD, those at index [txq->q.last_used].
 * Do NOT advance any indexes
3723
 *
J
James Ketrenos 已提交
3724 3725 3726 3727 3728 3729 3730 3731 3732
 * @param dev
 * @param txq
 */
static void ipw_queue_tx_free_tfd(struct ipw_priv *priv,
				  struct clx2_tx_queue *txq)
{
	struct tfd_frame *bd = &txq->bd[txq->q.last_used];
	struct pci_dev *dev = priv->pci_dev;
	int i;
3733

J
James Ketrenos 已提交
3734 3735 3736 3737 3738 3739
	/* classify bd */
	if (bd->control_flags.message_type == TX_HOST_COMMAND_TYPE)
		/* nothing to cleanup after for host commands */
		return;

	/* sanity check */
3740 3741 3742
	if (le32_to_cpu(bd->u.data.num_chunks) > NUM_TFD_CHUNKS) {
		IPW_ERROR("Too many chunks: %i\n",
			  le32_to_cpu(bd->u.data.num_chunks));
J
James Ketrenos 已提交
3743 3744 3745 3746 3747
		/** @todo issue fatal error, it is quite serious situation */
		return;
	}

	/* unmap chunks if any */
3748 3749 3750 3751
	for (i = 0; i < le32_to_cpu(bd->u.data.num_chunks); i++) {
		pci_unmap_single(dev, le32_to_cpu(bd->u.data.chunk_ptr[i]),
				 le16_to_cpu(bd->u.data.chunk_len[i]),
				 PCI_DMA_TODEVICE);
J
James Ketrenos 已提交
3752 3753 3754 3755 3756 3757 3758 3759 3760
		if (txq->txb[txq->q.last_used]) {
			ieee80211_txb_free(txq->txb[txq->q.last_used]);
			txq->txb[txq->q.last_used] = NULL;
		}
	}
}

/**
 * Deallocate DMA queue.
3761
 *
J
James Ketrenos 已提交
3762 3763
 * Empty queue by removing and destroying all BD's.
 * Free all buffers.
3764
 *
J
James Ketrenos 已提交
3765 3766 3767
 * @param dev
 * @param q
 */
3768
static void ipw_queue_tx_free(struct ipw_priv *priv, struct clx2_tx_queue *txq)
J
James Ketrenos 已提交
3769 3770 3771 3772
{
	struct clx2_queue *q = &txq->q;
	struct pci_dev *dev = priv->pci_dev;

3773 3774
	if (q->n_bd == 0)
		return;
J
James Ketrenos 已提交
3775 3776 3777 3778 3779 3780

	/* first, empty all BD's */
	for (; q->first_empty != q->last_used;
	     q->last_used = ipw_queue_inc_wrap(q->last_used, q->n_bd)) {
		ipw_queue_tx_free_tfd(priv, txq);
	}
3781

J
James Ketrenos 已提交
3782
	/* free buffers belonging to queue itself */
3783
	pci_free_consistent(dev, sizeof(txq->bd[0]) * q->n_bd, txq->bd,
J
James Ketrenos 已提交
3784 3785 3786 3787 3788 3789 3790 3791 3792
			    q->dma_addr);
	kfree(txq->txb);

	/* 0 fill whole structure */
	memset(txq, 0, sizeof(*txq));
}

/**
 * Destroy all DMA queues and structures
3793
 *
J
James Ketrenos 已提交
3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807
 * @param priv
 */
static void ipw_tx_queue_free(struct ipw_priv *priv)
{
	/* Tx CMD queue */
	ipw_queue_tx_free(priv, &priv->txq_cmd);

	/* Tx queues */
	ipw_queue_tx_free(priv, &priv->txq[0]);
	ipw_queue_tx_free(priv, &priv->txq[1]);
	ipw_queue_tx_free(priv, &priv->txq[2]);
	ipw_queue_tx_free(priv, &priv->txq[3]);
}

3808
static void ipw_create_bssid(struct ipw_priv *priv, u8 * bssid)
J
James Ketrenos 已提交
3809 3810 3811 3812 3813 3814 3815
{
	/* First 3 bytes are manufacturer */
	bssid[0] = priv->mac_addr[0];
	bssid[1] = priv->mac_addr[1];
	bssid[2] = priv->mac_addr[2];

	/* Last bytes are random */
3816
	get_random_bytes(&bssid[3], ETH_ALEN - 3);
J
James Ketrenos 已提交
3817

3818 3819
	bssid[0] &= 0xfe;	/* clear multicast bit */
	bssid[0] |= 0x02;	/* set local assignment bit (IEEE802) */
J
James Ketrenos 已提交
3820 3821
}

3822
static u8 ipw_add_station(struct ipw_priv *priv, u8 * bssid)
J
James Ketrenos 已提交
3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841
{
	struct ipw_station_entry entry;
	int i;

	for (i = 0; i < priv->num_stations; i++) {
		if (!memcmp(priv->stations[i], bssid, ETH_ALEN)) {
			/* Another node is active in network */
			priv->missed_adhoc_beacons = 0;
			if (!(priv->config & CFG_STATIC_CHANNEL))
				/* when other nodes drop out, we drop out */
				priv->config &= ~CFG_ADHOC_PERSIST;

			return i;
		}
	}

	if (i == MAX_STATIONS)
		return IPW_INVALID_STATION;

J
Johannes Berg 已提交
3842
	IPW_DEBUG_SCAN("Adding AdHoc station: %pM\n", bssid);
J
James Ketrenos 已提交
3843 3844 3845 3846 3847 3848

	entry.reserved = 0;
	entry.support_mode = 0;
	memcpy(entry.mac_addr, bssid, ETH_ALEN);
	memcpy(priv->stations[i], bssid, ETH_ALEN);
	ipw_write_direct(priv, IPW_STATION_TABLE_LOWER + i * sizeof(entry),
3849
			 &entry, sizeof(entry));
J
James Ketrenos 已提交
3850 3851 3852 3853 3854
	priv->num_stations++;

	return i;
}

3855
static u8 ipw_find_station(struct ipw_priv *priv, u8 * bssid)
J
James Ketrenos 已提交
3856 3857 3858
{
	int i;

3859 3860
	for (i = 0; i < priv->num_stations; i++)
		if (!memcmp(priv->stations[i], bssid, ETH_ALEN))
J
James Ketrenos 已提交
3861 3862 3863 3864 3865 3866 3867 3868 3869
			return i;

	return IPW_INVALID_STATION;
}

static void ipw_send_disassociate(struct ipw_priv *priv, int quiet)
{
	int err;

3870 3871 3872 3873 3874 3875 3876
	if (priv->status & STATUS_ASSOCIATING) {
		IPW_DEBUG_ASSOC("Disassociating while associating.\n");
		queue_work(priv->workqueue, &priv->disassociate);
		return;
	}

	if (!(priv->status & STATUS_ASSOCIATED)) {
J
James Ketrenos 已提交
3877 3878 3879 3880
		IPW_DEBUG_ASSOC("Disassociating while not associated.\n");
		return;
	}

J
Johannes Berg 已提交
3881
	IPW_DEBUG_ASSOC("Disassocation attempt from %pM "
J
James Ketrenos 已提交
3882
			"on channel %d.\n",
J
Johannes Berg 已提交
3883
			priv->assoc_request.bssid,
J
James Ketrenos 已提交
3884 3885 3886 3887 3888 3889 3890 3891 3892
			priv->assoc_request.channel);

	priv->status &= ~(STATUS_ASSOCIATING | STATUS_ASSOCIATED);
	priv->status |= STATUS_DISASSOCIATING;

	if (quiet)
		priv->assoc_request.assoc_type = HC_DISASSOC_QUIET;
	else
		priv->assoc_request.assoc_type = HC_DISASSOCIATE;
H
Hong Liu 已提交
3893

J
James Ketrenos 已提交
3894 3895 3896 3897 3898 3899 3900 3901 3902
	err = ipw_send_associate(priv, &priv->assoc_request);
	if (err) {
		IPW_DEBUG_HC("Attempt to send [dis]associate command "
			     "failed.\n");
		return;
	}

}

3903
static int ipw_disassociate(void *data)
J
James Ketrenos 已提交
3904
{
3905 3906 3907
	struct ipw_priv *priv = data;
	if (!(priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)))
		return 0;
J
James Ketrenos 已提交
3908
	ipw_send_disassociate(data, 0);
3909
	netif_carrier_off(priv->net_dev);
3910
	return 1;
J
James Ketrenos 已提交
3911 3912
}

D
David Howells 已提交
3913
static void ipw_bg_disassociate(struct work_struct *work)
J
James Ketrenos 已提交
3914
{
D
David Howells 已提交
3915 3916
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, disassociate);
3917
	mutex_lock(&priv->mutex);
D
David Howells 已提交
3918
	ipw_disassociate(priv);
3919
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
3920 3921
}

D
David Howells 已提交
3922
static void ipw_system_config(struct work_struct *work)
3923
{
D
David Howells 已提交
3924 3925
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, system_config);
3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936

#ifdef CONFIG_IPW2200_PROMISCUOUS
	if (priv->prom_net_dev && netif_running(priv->prom_net_dev)) {
		priv->sys_config.accept_all_data_frames = 1;
		priv->sys_config.accept_non_directed_frames = 1;
		priv->sys_config.accept_all_mgmt_bcpr = 1;
		priv->sys_config.accept_all_mgmt_frames = 1;
	}
#endif

	ipw_send_system_config(priv);
J
James Ketrenos 已提交
3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952
}

struct ipw_status_code {
	u16 status;
	const char *reason;
};

static const struct ipw_status_code ipw_status_codes[] = {
	{0x00, "Successful"},
	{0x01, "Unspecified failure"},
	{0x0A, "Cannot support all requested capabilities in the "
	 "Capability information field"},
	{0x0B, "Reassociation denied due to inability to confirm that "
	 "association exists"},
	{0x0C, "Association denied due to reason outside the scope of this "
	 "standard"},
3953 3954
	{0x0D,
	 "Responding station does not support the specified authentication "
J
James Ketrenos 已提交
3955
	 "algorithm"},
3956 3957
	{0x0E,
	 "Received an Authentication frame with authentication sequence "
J
James Ketrenos 已提交
3958 3959 3960 3961 3962 3963
	 "transaction sequence number out of expected sequence"},
	{0x0F, "Authentication rejected because of challenge failure"},
	{0x10, "Authentication rejected due to timeout waiting for next "
	 "frame in sequence"},
	{0x11, "Association denied because AP is unable to handle additional "
	 "associated stations"},
3964 3965
	{0x12,
	 "Association denied due to requesting station not supporting all "
J
James Ketrenos 已提交
3966
	 "of the datarates in the BSSBasicServiceSet Parameter"},
3967 3968
	{0x13,
	 "Association denied due to requesting station not supporting "
J
James Ketrenos 已提交
3969
	 "short preamble operation"},
3970 3971
	{0x14,
	 "Association denied due to requesting station not supporting "
J
James Ketrenos 已提交
3972
	 "PBCC encoding"},
3973 3974
	{0x15,
	 "Association denied due to requesting station not supporting "
J
James Ketrenos 已提交
3975
	 "channel agility"},
3976 3977
	{0x19,
	 "Association denied due to requesting station not supporting "
J
James Ketrenos 已提交
3978
	 "short slot operation"},
3979 3980
	{0x1A,
	 "Association denied due to requesting station not supporting "
J
James Ketrenos 已提交
3981 3982 3983 3984 3985 3986 3987 3988 3989 3990
	 "DSSS-OFDM operation"},
	{0x28, "Invalid Information Element"},
	{0x29, "Group Cipher is not valid"},
	{0x2A, "Pairwise Cipher is not valid"},
	{0x2B, "AKMP is not valid"},
	{0x2C, "Unsupported RSN IE version"},
	{0x2D, "Invalid RSN IE Capabilities"},
	{0x2E, "Cipher suite is rejected per security policy"},
};

3991
static const char *ipw_get_status_code(u16 status)
J
James Ketrenos 已提交
3992 3993
{
	int i;
3994
	for (i = 0; i < ARRAY_SIZE(ipw_status_codes); i++)
3995
		if (ipw_status_codes[i].status == (status & 0xff))
J
James Ketrenos 已提交
3996 3997 3998 3999 4000 4001 4002 4003 4004
			return ipw_status_codes[i].reason;
	return "Unknown status value.";
}

static void inline average_init(struct average *avg)
{
	memset(avg, 0, sizeof(*avg));
}

4005 4006 4007 4008 4009 4010 4011
#define DEPTH_RSSI 8
#define DEPTH_NOISE 16
static s16 exponential_average(s16 prev_avg, s16 val, u8 depth)
{
	return ((depth-1)*prev_avg +  val)/depth;
}

4012
static void average_add(struct average *avg, s16 val)
J
James Ketrenos 已提交
4013 4014 4015 4016 4017 4018 4019 4020 4021 4022
{
	avg->sum -= avg->entries[avg->pos];
	avg->sum += val;
	avg->entries[avg->pos++] = val;
	if (unlikely(avg->pos == AVG_ENTRIES)) {
		avg->init = 1;
		avg->pos = 0;
	}
}

4023
static s16 average_value(struct average *avg)
J
James Ketrenos 已提交
4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040
{
	if (!unlikely(avg->init)) {
		if (avg->pos)
			return avg->sum / avg->pos;
		return 0;
	}

	return avg->sum / AVG_ENTRIES;
}

static void ipw_reset_stats(struct ipw_priv *priv)
{
	u32 len = sizeof(u32);

	priv->quality = 0;

	average_init(&priv->average_missed_beacons);
4041 4042
	priv->exp_avg_rssi = -60;
	priv->exp_avg_noise = -85 + 0x100;
J
James Ketrenos 已提交
4043 4044 4045 4046 4047 4048

	priv->last_rate = 0;
	priv->last_missed_beacons = 0;
	priv->last_rx_packets = 0;
	priv->last_tx_packets = 0;
	priv->last_tx_failures = 0;
4049

J
James Ketrenos 已提交
4050 4051
	/* Firmware managed, reset only when NIC is restarted, so we have to
	 * normalize on the current value */
4052
	ipw_get_ordinal(priv, IPW_ORD_STAT_RX_ERR_CRC,
J
James Ketrenos 已提交
4053
			&priv->last_rx_err, &len);
4054
	ipw_get_ordinal(priv, IPW_ORD_STAT_TX_FAILURE,
J
James Ketrenos 已提交
4055 4056 4057 4058 4059 4060 4061 4062 4063 4064
			&priv->last_tx_failures, &len);

	/* Driver managed, reset with each association */
	priv->missed_adhoc_beacons = 0;
	priv->missed_beacons = 0;
	priv->tx_packets = 0;
	priv->rx_packets = 0;

}

4065
static u32 ipw_get_max_rate(struct ipw_priv *priv)
J
James Ketrenos 已提交
4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076
{
	u32 i = 0x80000000;
	u32 mask = priv->rates_mask;
	/* If currently associated in B mode, restrict the maximum
	 * rate match to B rates */
	if (priv->assoc_request.ieee_mode == IPW_B_MODE)
		mask &= IEEE80211_CCK_RATES_MASK;

	/* TODO: Verify that the rate is supported by the current rates
	 * list. */

4077 4078
	while (i && !(mask & i))
		i >>= 1;
J
James Ketrenos 已提交
4079
	switch (i) {
4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103
	case IEEE80211_CCK_RATE_1MB_MASK:
		return 1000000;
	case IEEE80211_CCK_RATE_2MB_MASK:
		return 2000000;
	case IEEE80211_CCK_RATE_5MB_MASK:
		return 5500000;
	case IEEE80211_OFDM_RATE_6MB_MASK:
		return 6000000;
	case IEEE80211_OFDM_RATE_9MB_MASK:
		return 9000000;
	case IEEE80211_CCK_RATE_11MB_MASK:
		return 11000000;
	case IEEE80211_OFDM_RATE_12MB_MASK:
		return 12000000;
	case IEEE80211_OFDM_RATE_18MB_MASK:
		return 18000000;
	case IEEE80211_OFDM_RATE_24MB_MASK:
		return 24000000;
	case IEEE80211_OFDM_RATE_36MB_MASK:
		return 36000000;
	case IEEE80211_OFDM_RATE_48MB_MASK:
		return 48000000;
	case IEEE80211_OFDM_RATE_54MB_MASK:
		return 54000000;
J
James Ketrenos 已提交
4104 4105
	}

4106
	if (priv->ieee->mode == IEEE_B)
J
James Ketrenos 已提交
4107 4108 4109 4110 4111 4112 4113 4114 4115 4116
		return 11000000;
	else
		return 54000000;
}

static u32 ipw_get_current_rate(struct ipw_priv *priv)
{
	u32 rate, len = sizeof(rate);
	int err;

4117
	if (!(priv->status & STATUS_ASSOCIATED))
J
James Ketrenos 已提交
4118 4119 4120
		return 0;

	if (priv->tx_packets > IPW_REAL_RATE_RX_PACKET_THRESHOLD) {
4121
		err = ipw_get_ordinal(priv, IPW_ORD_STAT_TX_CURR_RATE, &rate,
J
James Ketrenos 已提交
4122 4123 4124 4125 4126
				      &len);
		if (err) {
			IPW_DEBUG_INFO("failed querying ordinals.\n");
			return 0;
		}
4127
	} else
J
James Ketrenos 已提交
4128 4129 4130
		return ipw_get_max_rate(priv);

	switch (rate) {
4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154
	case IPW_TX_RATE_1MB:
		return 1000000;
	case IPW_TX_RATE_2MB:
		return 2000000;
	case IPW_TX_RATE_5MB:
		return 5500000;
	case IPW_TX_RATE_6MB:
		return 6000000;
	case IPW_TX_RATE_9MB:
		return 9000000;
	case IPW_TX_RATE_11MB:
		return 11000000;
	case IPW_TX_RATE_12MB:
		return 12000000;
	case IPW_TX_RATE_18MB:
		return 18000000;
	case IPW_TX_RATE_24MB:
		return 24000000;
	case IPW_TX_RATE_36MB:
		return 36000000;
	case IPW_TX_RATE_48MB:
		return 48000000;
	case IPW_TX_RATE_54MB:
		return 54000000;
J
James Ketrenos 已提交
4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168
	}

	return 0;
}

#define IPW_STATS_INTERVAL (2 * HZ)
static void ipw_gather_stats(struct ipw_priv *priv)
{
	u32 rx_err, rx_err_delta, rx_packets_delta;
	u32 tx_failures, tx_failures_delta, tx_packets_delta;
	u32 missed_beacons_percent, missed_beacons_delta;
	u32 quality = 0;
	u32 len = sizeof(u32);
	s16 rssi;
4169
	u32 beacon_quality, signal_quality, tx_quality, rx_quality,
4170
	    rate_quality;
4171
	u32 max_rate;
J
James Ketrenos 已提交
4172 4173 4174 4175 4176 4177 4178

	if (!(priv->status & STATUS_ASSOCIATED)) {
		priv->quality = 0;
		return;
	}

	/* Update the statistics */
4179
	ipw_get_ordinal(priv, IPW_ORD_STAT_MISSED_BEACONS,
J
James Ketrenos 已提交
4180
			&priv->missed_beacons, &len);
4181
	missed_beacons_delta = priv->missed_beacons - priv->last_missed_beacons;
J
James Ketrenos 已提交
4182 4183 4184
	priv->last_missed_beacons = priv->missed_beacons;
	if (priv->assoc_request.beacon_interval) {
		missed_beacons_percent = missed_beacons_delta *
4185
		    (HZ * le16_to_cpu(priv->assoc_request.beacon_interval)) /
4186
		    (IPW_STATS_INTERVAL * 10);
J
James Ketrenos 已提交
4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206
	} else {
		missed_beacons_percent = 0;
	}
	average_add(&priv->average_missed_beacons, missed_beacons_percent);

	ipw_get_ordinal(priv, IPW_ORD_STAT_RX_ERR_CRC, &rx_err, &len);
	rx_err_delta = rx_err - priv->last_rx_err;
	priv->last_rx_err = rx_err;

	ipw_get_ordinal(priv, IPW_ORD_STAT_TX_FAILURE, &tx_failures, &len);
	tx_failures_delta = tx_failures - priv->last_tx_failures;
	priv->last_tx_failures = tx_failures;

	rx_packets_delta = priv->rx_packets - priv->last_rx_packets;
	priv->last_rx_packets = priv->rx_packets;

	tx_packets_delta = priv->tx_packets - priv->last_tx_packets;
	priv->last_tx_packets = priv->tx_packets;

	/* Calculate quality based on the following:
4207
	 *
J
James Ketrenos 已提交
4208 4209 4210 4211 4212
	 * Missed beacon: 100% = 0, 0% = 70% missed
	 * Rate: 60% = 1Mbs, 100% = Max
	 * Rx and Tx errors represent a straight % of total Rx/Tx
	 * RSSI: 100% = > -50,  0% = < -80
	 * Rx errors: 100% = 0, 0% = 50% missed
4213
	 *
J
James Ketrenos 已提交
4214 4215 4216 4217 4218 4219 4220 4221
	 * The lowest computed quality is used.
	 *
	 */
#define BEACON_THRESHOLD 5
	beacon_quality = 100 - missed_beacons_percent;
	if (beacon_quality < BEACON_THRESHOLD)
		beacon_quality = 0;
	else
4222
		beacon_quality = (beacon_quality - BEACON_THRESHOLD) * 100 /
4223
		    (100 - BEACON_THRESHOLD);
4224
	IPW_DEBUG_STATS("Missed beacon: %3d%% (%d%%)\n",
J
James Ketrenos 已提交
4225
			beacon_quality, missed_beacons_percent);
4226

J
James Ketrenos 已提交
4227
	priv->last_rate = ipw_get_current_rate(priv);
4228 4229
	max_rate = ipw_get_max_rate(priv);
	rate_quality = priv->last_rate * 40 / max_rate + 60;
J
James Ketrenos 已提交
4230 4231
	IPW_DEBUG_STATS("Rate quality : %3d%% (%dMbs)\n",
			rate_quality, priv->last_rate / 1000000);
4232

4233
	if (rx_packets_delta > 100 && rx_packets_delta + rx_err_delta)
4234
		rx_quality = 100 - (rx_err_delta * 100) /
4235
		    (rx_packets_delta + rx_err_delta);
J
James Ketrenos 已提交
4236 4237 4238 4239
	else
		rx_quality = 100;
	IPW_DEBUG_STATS("Rx quality   : %3d%% (%u errors, %u packets)\n",
			rx_quality, rx_err_delta, rx_packets_delta);
4240

4241
	if (tx_packets_delta > 100 && tx_packets_delta + tx_failures_delta)
4242
		tx_quality = 100 - (tx_failures_delta * 100) /
4243
		    (tx_packets_delta + tx_failures_delta);
J
James Ketrenos 已提交
4244 4245 4246 4247
	else
		tx_quality = 100;
	IPW_DEBUG_STATS("Tx quality   : %3d%% (%u errors, %u packets)\n",
			tx_quality, tx_failures_delta, tx_packets_delta);
4248

4249
	rssi = priv->exp_avg_rssi;
4250 4251 4252 4253 4254 4255 4256 4257 4258 4259
	signal_quality =
	    (100 *
	     (priv->ieee->perfect_rssi - priv->ieee->worst_rssi) *
	     (priv->ieee->perfect_rssi - priv->ieee->worst_rssi) -
	     (priv->ieee->perfect_rssi - rssi) *
	     (15 * (priv->ieee->perfect_rssi - priv->ieee->worst_rssi) +
	      62 * (priv->ieee->perfect_rssi - rssi))) /
	    ((priv->ieee->perfect_rssi - priv->ieee->worst_rssi) *
	     (priv->ieee->perfect_rssi - priv->ieee->worst_rssi));
	if (signal_quality > 100)
J
James Ketrenos 已提交
4260
		signal_quality = 100;
4261
	else if (signal_quality < 1)
J
James Ketrenos 已提交
4262
		signal_quality = 0;
4263

4264
	IPW_DEBUG_STATS("Signal level : %3d%% (%d dBm)\n",
J
James Ketrenos 已提交
4265
			signal_quality, rssi);
4266 4267

	quality = min(beacon_quality,
J
James Ketrenos 已提交
4268 4269 4270
		      min(rate_quality,
			  min(tx_quality, min(rx_quality, signal_quality))));
	if (quality == beacon_quality)
4271 4272
		IPW_DEBUG_STATS("Quality (%d%%): Clamped to missed beacons.\n",
				quality);
J
James Ketrenos 已提交
4273
	if (quality == rate_quality)
4274 4275
		IPW_DEBUG_STATS("Quality (%d%%): Clamped to rate quality.\n",
				quality);
J
James Ketrenos 已提交
4276
	if (quality == tx_quality)
4277 4278
		IPW_DEBUG_STATS("Quality (%d%%): Clamped to Tx quality.\n",
				quality);
J
James Ketrenos 已提交
4279
	if (quality == rx_quality)
4280 4281
		IPW_DEBUG_STATS("Quality (%d%%): Clamped to Rx quality.\n",
				quality);
J
James Ketrenos 已提交
4282
	if (quality == signal_quality)
4283 4284
		IPW_DEBUG_STATS("Quality (%d%%): Clamped to signal quality.\n",
				quality);
J
James Ketrenos 已提交
4285 4286

	priv->quality = quality;
4287 4288

	queue_delayed_work(priv->workqueue, &priv->gather_stats,
J
James Ketrenos 已提交
4289 4290 4291
			   IPW_STATS_INTERVAL);
}

D
David Howells 已提交
4292
static void ipw_bg_gather_stats(struct work_struct *work)
4293
{
D
David Howells 已提交
4294 4295
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, gather_stats.work);
4296
	mutex_lock(&priv->mutex);
D
David Howells 已提交
4297
	ipw_gather_stats(priv);
4298
	mutex_unlock(&priv->mutex);
4299 4300
}

4301 4302 4303 4304 4305
/* Missed beacon behavior:
 * 1st missed -> roaming_threshold, just wait, don't do any scan/roam.
 * roaming_threshold -> disassociate_threshold, scan and roam for better signal.
 * Above disassociate threshold, give up and stop scanning.
 * Roaming is disabled if disassociate_threshold <= roaming_threshold  */
4306
static void ipw_handle_missed_beacon(struct ipw_priv *priv,
4307 4308 4309 4310
					    int missed_count)
{
	priv->notif_missed_beacons = missed_count;

4311
	if (missed_count > priv->disassociate_threshold &&
4312 4313 4314 4315 4316
	    priv->status & STATUS_ASSOCIATED) {
		/* If associated and we've hit the missed
		 * beacon threshold, disassociate, turn
		 * off roaming, and abort any active scans */
		IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF |
4317
			  IPW_DL_STATE | IPW_DL_ASSOC,
4318 4319
			  "Missed beacon: %d - disassociate\n", missed_count);
		priv->status &= ~STATUS_ROAMING;
4320 4321 4322 4323
		if (priv->status & STATUS_SCANNING) {
			IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF |
				  IPW_DL_STATE,
				  "Aborting scan with missed beacon.\n");
4324
			queue_work(priv->workqueue, &priv->abort_scan);
4325 4326
		}

4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339
		queue_work(priv->workqueue, &priv->disassociate);
		return;
	}

	if (priv->status & STATUS_ROAMING) {
		/* If we are currently roaming, then just
		 * print a debug statement... */
		IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
			  "Missed beacon: %d - roam in progress\n",
			  missed_count);
		return;
	}

4340 4341 4342
	if (roaming &&
	    (missed_count > priv->roaming_threshold &&
	     missed_count <= priv->disassociate_threshold)) {
4343
		/* If we are not already roaming, set the ROAM
4344 4345 4346
		 * bit in the status and kick off a scan.
		 * This can happen several times before we reach
		 * disassociate_threshold. */
4347 4348 4349 4350 4351 4352
		IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
			  "Missed beacon: %d - initiate "
			  "roaming\n", missed_count);
		if (!(priv->status & STATUS_ROAMING)) {
			priv->status |= STATUS_ROAMING;
			if (!(priv->status & STATUS_SCANNING))
D
David Howells 已提交
4353 4354
				queue_delayed_work(priv->workqueue,
						   &priv->request_scan, 0);
4355 4356 4357 4358
		}
		return;
	}

4359 4360
	if (priv->status & STATUS_SCANNING &&
	    missed_count > IPW_MB_SCAN_CANCEL_THRESHOLD) {
4361 4362 4363 4364
		/* Stop scan to keep fw from getting
		 * stuck (only if we aren't roaming --
		 * otherwise we'll never scan more than 2 or 3
		 * channels..) */
4365 4366
		IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF | IPW_DL_STATE,
			  "Aborting scan with missed beacon.\n");
4367 4368 4369 4370 4371 4372
		queue_work(priv->workqueue, &priv->abort_scan);
	}

	IPW_DEBUG_NOTIF("Missed beacon: %d\n", missed_count);
}

4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390
static void ipw_scan_event(struct work_struct *work)
{
	union iwreq_data wrqu;

	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, scan_event.work);

	wrqu.data.length = 0;
	wrqu.data.flags = 0;
	wireless_send_event(priv->net_dev, SIOCGIWSCAN, &wrqu, NULL);
}

static void handle_scan_event(struct ipw_priv *priv)
{
	/* Only userspace-requested scan completion events go out immediately */
	if (!priv->user_requested_scan) {
		if (!delayed_work_pending(&priv->scan_event))
			queue_delayed_work(priv->workqueue, &priv->scan_event,
4391
					 round_jiffies_relative(msecs_to_jiffies(4000)));
4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403
	} else {
		union iwreq_data wrqu;

		priv->user_requested_scan = 0;
		cancel_delayed_work(&priv->scan_event);

		wrqu.data.length = 0;
		wrqu.data.flags = 0;
		wireless_send_event(priv->net_dev, SIOCGIWSCAN, &wrqu, NULL);
	}
}

J
James Ketrenos 已提交
4404 4405 4406 4407
/**
 * Handle host notification packet.
 * Called from interrupt routine
 */
4408
static void ipw_rx_notification(struct ipw_priv *priv,
J
James Ketrenos 已提交
4409 4410
				       struct ipw_rx_notification *notif)
{
4411
	DECLARE_SSID_BUF(ssid);
A
Al Viro 已提交
4412
	u16 size = le16_to_cpu(notif->size);
4413 4414
	notif->size = le16_to_cpu(notif->size);

A
Al Viro 已提交
4415
	IPW_DEBUG_NOTIF("type = %i (%d bytes)\n", notif->subtype, size);
4416

J
James Ketrenos 已提交
4417
	switch (notif->subtype) {
4418 4419 4420 4421 4422 4423 4424
	case HOST_NOTIFICATION_STATUS_ASSOCIATED:{
			struct notif_association *assoc = &notif->u.assoc;

			switch (assoc->state) {
			case CMAS_ASSOCIATED:{
					IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
						  IPW_DL_ASSOC,
J
Johannes Berg 已提交
4425
						  "associated: '%s' %pM \n",
4426 4427
						  print_ssid(ssid, priv->essid,
							     priv->essid_len),
J
Johannes Berg 已提交
4428
						  priv->bssid);
4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448

					switch (priv->ieee->iw_mode) {
					case IW_MODE_INFRA:
						memcpy(priv->ieee->bssid,
						       priv->bssid, ETH_ALEN);
						break;

					case IW_MODE_ADHOC:
						memcpy(priv->ieee->bssid,
						       priv->bssid, ETH_ALEN);

						/* clear out the station table */
						priv->num_stations = 0;

						IPW_DEBUG_ASSOC
						    ("queueing adhoc check\n");
						queue_delayed_work(priv->
								   workqueue,
								   &priv->
								   adhoc_check,
4449
								   le16_to_cpu(priv->
4450
								   assoc_request.
4451
								   beacon_interval));
4452 4453 4454 4455 4456
						break;
					}

					priv->status &= ~STATUS_ASSOCIATING;
					priv->status |= STATUS_ASSOCIATED;
4457 4458
					queue_work(priv->workqueue,
						   &priv->system_config);
4459

4460
#ifdef CONFIG_IPW2200_QOS
4461
#define IPW_GET_PACKET_STYPE(x) WLAN_FC_GET_STYPE( \
4462
			 le16_to_cpu(((struct ieee80211_hdr *)(x))->frame_control))
4463 4464 4465
					if ((priv->status & STATUS_AUTH) &&
					    (IPW_GET_PACKET_STYPE(&notif->u.raw)
					     == IEEE80211_STYPE_ASSOC_RESP)) {
4466 4467
						if ((sizeof
						     (struct
4468
						      ieee80211_assoc_response)
A
Al Viro 已提交
4469 4470
						     <= size)
						    && (size <= 2314)) {
4471 4472 4473
							struct
							ieee80211_rx_stats
							    stats = {
A
Al Viro 已提交
4474
								.len = size - 1,
4475 4476 4477 4478
							};

							IPW_DEBUG_QOS
							    ("QoS Associate "
A
Al Viro 已提交
4479
							     "size %d\n", size);
4480 4481 4482
							ieee80211_rx_mgt(priv->
									 ieee,
									 (struct
4483
									  ieee80211_hdr_4addr
4484 4485 4486
									  *)
									 &notif->u.raw, &stats);
						}
4487
					}
4488
#endif
4489

4490
					schedule_work(&priv->link_up);
J
James Ketrenos 已提交
4491

4492 4493
					break;
				}
4494

4495 4496 4497 4498 4499 4500 4501 4502 4503 4504
			case CMAS_AUTHENTICATED:{
					if (priv->
					    status & (STATUS_ASSOCIATED |
						      STATUS_AUTH)) {
						struct notif_authenticate *auth
						    = &notif->u.auth;
						IPW_DEBUG(IPW_DL_NOTIF |
							  IPW_DL_STATE |
							  IPW_DL_ASSOC,
							  "deauthenticated: '%s' "
J
Johannes Berg 已提交
4505
							  "%pM"
4506
							  ": (0x%04X) - %s \n",
4507 4508 4509 4510 4511
							  print_ssid(ssid,
								     priv->
								     essid,
								     priv->
								     essid_len),
J
Johannes Berg 已提交
4512
							  priv->bssid,
A
Al Viro 已提交
4513
							  le16_to_cpu(auth->status),
4514
							  ipw_get_status_code
A
Al Viro 已提交
4515
							  (le16_to_cpu
4516
							   (auth->status)));
J
James Ketrenos 已提交
4517

4518 4519 4520 4521 4522
						priv->status &=
						    ~(STATUS_ASSOCIATING |
						      STATUS_AUTH |
						      STATUS_ASSOCIATED);

4523
						schedule_work(&priv->link_down);
4524 4525 4526 4527 4528
						break;
					}

					IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
						  IPW_DL_ASSOC,
J
Johannes Berg 已提交
4529
						  "authenticated: '%s' %pM\n",
4530 4531
						  print_ssid(ssid, priv->essid,
							     priv->essid_len),
J
Johannes Berg 已提交
4532
						  priv->bssid);
4533 4534 4535 4536
					break;
				}

			case CMAS_INIT:{
4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548
					if (priv->status & STATUS_AUTH) {
						struct
						    ieee80211_assoc_response
						*resp;
						resp =
						    (struct
						     ieee80211_assoc_response
						     *)&notif->u.raw;
						IPW_DEBUG(IPW_DL_NOTIF |
							  IPW_DL_STATE |
							  IPW_DL_ASSOC,
							  "association failed (0x%04X): %s\n",
A
Al Viro 已提交
4549
							  le16_to_cpu(resp->status),
4550
							  ipw_get_status_code
A
Al Viro 已提交
4551
							  (le16_to_cpu
4552 4553 4554
							   (resp->status)));
					}

4555 4556
					IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
						  IPW_DL_ASSOC,
J
Johannes Berg 已提交
4557
						  "disassociated: '%s' %pM \n",
4558 4559
						  print_ssid(ssid, priv->essid,
							     priv->essid_len),
J
Johannes Berg 已提交
4560
						  priv->bssid);
4561 4562 4563 4564 4565

					priv->status &=
					    ~(STATUS_DISASSOCIATING |
					      STATUS_ASSOCIATING |
					      STATUS_ASSOCIATED | STATUS_AUTH);
4566 4567 4568 4569 4570 4571
					if (priv->assoc_network
					    && (priv->assoc_network->
						capability &
						WLAN_CAPABILITY_IBSS))
						ipw_remove_current_network
						    (priv);
4572

4573
					schedule_work(&priv->link_down);
4574 4575 4576

					break;
				}
J
James Ketrenos 已提交
4577

4578 4579 4580
			case CMAS_RX_ASSOC_RESP:
				break;

4581 4582 4583
			default:
				IPW_ERROR("assoc: unknown (%d)\n",
					  assoc->state);
J
James Ketrenos 已提交
4584
				break;
4585
			}
J
James Ketrenos 已提交
4586 4587 4588

			break;
		}
4589

4590 4591 4592 4593 4594
	case HOST_NOTIFICATION_STATUS_AUTHENTICATE:{
			struct notif_authenticate *auth = &notif->u.auth;
			switch (auth->state) {
			case CMAS_AUTHENTICATED:
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
J
Johannes Berg 已提交
4595
					  "authenticated: '%s' %pM \n",
4596 4597
					  print_ssid(ssid, priv->essid,
						     priv->essid_len),
J
Johannes Berg 已提交
4598
					  priv->bssid);
4599 4600
				priv->status |= STATUS_AUTH;
				break;
J
James Ketrenos 已提交
4601

4602 4603 4604 4605 4606
			case CMAS_INIT:
				if (priv->status & STATUS_AUTH) {
					IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
						  IPW_DL_ASSOC,
						  "authentication failed (0x%04X): %s\n",
A
Al Viro 已提交
4607 4608
						  le16_to_cpu(auth->status),
						  ipw_get_status_code(le16_to_cpu
4609 4610 4611 4612 4613
								      (auth->
								       status)));
				}
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
					  IPW_DL_ASSOC,
J
Johannes Berg 已提交
4614
					  "deauthenticated: '%s' %pM\n",
4615 4616
					  print_ssid(ssid, priv->essid,
						     priv->essid_len),
J
Johannes Berg 已提交
4617
					  priv->bssid);
4618

4619 4620 4621
				priv->status &= ~(STATUS_ASSOCIATING |
						  STATUS_AUTH |
						  STATUS_ASSOCIATED);
J
James Ketrenos 已提交
4622

4623
				schedule_work(&priv->link_down);
4624
				break;
J
James Ketrenos 已提交
4625

4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664
			case CMAS_TX_AUTH_SEQ_1:
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
					  IPW_DL_ASSOC, "AUTH_SEQ_1\n");
				break;
			case CMAS_RX_AUTH_SEQ_2:
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
					  IPW_DL_ASSOC, "AUTH_SEQ_2\n");
				break;
			case CMAS_AUTH_SEQ_1_PASS:
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
					  IPW_DL_ASSOC, "AUTH_SEQ_1_PASS\n");
				break;
			case CMAS_AUTH_SEQ_1_FAIL:
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
					  IPW_DL_ASSOC, "AUTH_SEQ_1_FAIL\n");
				break;
			case CMAS_TX_AUTH_SEQ_3:
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
					  IPW_DL_ASSOC, "AUTH_SEQ_3\n");
				break;
			case CMAS_RX_AUTH_SEQ_4:
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
					  IPW_DL_ASSOC, "RX_AUTH_SEQ_4\n");
				break;
			case CMAS_AUTH_SEQ_2_PASS:
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
					  IPW_DL_ASSOC, "AUTH_SEQ_2_PASS\n");
				break;
			case CMAS_AUTH_SEQ_2_FAIL:
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
					  IPW_DL_ASSOC, "AUT_SEQ_2_FAIL\n");
				break;
			case CMAS_TX_ASSOC:
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
					  IPW_DL_ASSOC, "TX_ASSOC\n");
				break;
			case CMAS_RX_ASSOC_RESP:
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
					  IPW_DL_ASSOC, "RX_ASSOC_RESP\n");
4665

4666 4667 4668 4669 4670 4671 4672 4673 4674
				break;
			case CMAS_ASSOCIATED:
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
					  IPW_DL_ASSOC, "ASSOCIATED\n");
				break;
			default:
				IPW_DEBUG_NOTIF("auth: failure - %d\n",
						auth->state);
				break;
J
James Ketrenos 已提交
4675 4676 4677 4678
			}
			break;
		}

4679 4680 4681
	case HOST_NOTIFICATION_STATUS_SCAN_CHANNEL_RESULT:{
			struct notif_channel_result *x =
			    &notif->u.channel_result;
J
James Ketrenos 已提交
4682

A
Al Viro 已提交
4683
			if (size == sizeof(*x)) {
4684 4685 4686 4687 4688
				IPW_DEBUG_SCAN("Scan result for channel %d\n",
					       x->channel_num);
			} else {
				IPW_DEBUG_SCAN("Scan result of wrong size %d "
					       "(should be %zd)\n",
A
Al Viro 已提交
4689
					       size, sizeof(*x));
4690
			}
J
James Ketrenos 已提交
4691 4692 4693
			break;
		}

4694 4695
	case HOST_NOTIFICATION_STATUS_SCAN_COMPLETED:{
			struct notif_scan_complete *x = &notif->u.scan_complete;
A
Al Viro 已提交
4696
			if (size == sizeof(*x)) {
4697 4698 4699 4700 4701 4702 4703
				IPW_DEBUG_SCAN
				    ("Scan completed: type %d, %d channels, "
				     "%d status\n", x->scan_type,
				     x->num_channels, x->status);
			} else {
				IPW_ERROR("Scan completed of wrong size %d "
					  "(should be %zd)\n",
A
Al Viro 已提交
4704
					  size, sizeof(*x));
4705
			}
J
James Ketrenos 已提交
4706

4707 4708 4709
			priv->status &=
			    ~(STATUS_SCANNING | STATUS_SCAN_ABORTING);

4710
			wake_up_interruptible(&priv->wait_state);
4711 4712
			cancel_delayed_work(&priv->scan_check);

4713 4714 4715 4716 4717 4718 4719
			if (priv->status & STATUS_EXIT_PENDING)
				break;

			priv->ieee->scans++;

#ifdef CONFIG_IPW2200_MONITOR
			if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
4720
				priv->status |= STATUS_SCAN_FORCED;
D
David Howells 已提交
4721 4722
				queue_delayed_work(priv->workqueue,
						   &priv->request_scan, 0);
4723 4724
				break;
			}
4725
			priv->status &= ~STATUS_SCAN_FORCED;
4726 4727
#endif				/* CONFIG_IPW2200_MONITOR */

D
Dan Williams 已提交
4728 4729 4730 4731 4732 4733
			/* Do queued direct scans first */
			if (priv->status & STATUS_DIRECT_SCAN_PENDING) {
				queue_delayed_work(priv->workqueue,
						   &priv->request_direct_scan, 0);
			}

4734 4735 4736 4737 4738 4739
			if (!(priv->status & (STATUS_ASSOCIATED |
					      STATUS_ASSOCIATING |
					      STATUS_ROAMING |
					      STATUS_DISASSOCIATING)))
				queue_work(priv->workqueue, &priv->associate);
			else if (priv->status & STATUS_ROAMING) {
4740 4741 4742 4743 4744 4745 4746 4747 4748 4749
				if (x->status == SCAN_COMPLETED_STATUS_COMPLETE)
					/* If a scan completed and we are in roam mode, then
					 * the scan that completed was the one requested as a
					 * result of entering roam... so, schedule the
					 * roam work */
					queue_work(priv->workqueue,
						   &priv->roam);
				else
					/* Don't schedule if we aborted the scan */
					priv->status &= ~STATUS_ROAMING;
4750
			} else if (priv->status & STATUS_SCAN_PENDING)
D
David Howells 已提交
4751 4752
				queue_delayed_work(priv->workqueue,
						   &priv->request_scan, 0);
4753 4754 4755
			else if (priv->config & CFG_BACKGROUND_SCAN
				 && priv->status & STATUS_ASSOCIATED)
				queue_delayed_work(priv->workqueue,
4756
						   &priv->request_scan,
4757
						   round_jiffies_relative(HZ));
4758 4759 4760 4761 4762 4763 4764 4765 4766 4767

			/* Send an empty event to user space.
			 * We don't send the received data on the event because
			 * it would require us to do complex transcoding, and
			 * we want to minimise the work done in the irq handler
			 * Use a request to extract the data.
			 * Also, we generate this even for any scan, regardless
			 * on how the scan was initiated. User space can just
			 * sync on periodic scan to get fresh data...
			 * Jean II */
4768 4769
			if (x->status == SCAN_COMPLETED_STATUS_COMPLETE)
				handle_scan_event(priv);
4770
			break;
J
James Ketrenos 已提交
4771 4772
		}

4773 4774
	case HOST_NOTIFICATION_STATUS_FRAG_LENGTH:{
			struct notif_frag_length *x = &notif->u.frag_len;
J
James Ketrenos 已提交
4775

A
Al Viro 已提交
4776
			if (size == sizeof(*x))
4777 4778 4779
				IPW_ERROR("Frag length: %d\n",
					  le16_to_cpu(x->frag_length));
			else
4780 4781
				IPW_ERROR("Frag length of wrong size %d "
					  "(should be %zd)\n",
A
Al Viro 已提交
4782
					  size, sizeof(*x));
4783
			break;
J
James Ketrenos 已提交
4784 4785
		}

4786 4787 4788
	case HOST_NOTIFICATION_STATUS_LINK_DETERIORATION:{
			struct notif_link_deterioration *x =
			    &notif->u.link_deterioration;
4789

A
Al Viro 已提交
4790
			if (size == sizeof(*x)) {
4791
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
4792 4793 4794
					"link deterioration: type %d, cnt %d\n",
					x->silence_notification_type,
					x->silence_count);
4795 4796 4797 4798 4799
				memcpy(&priv->last_link_deterioration, x,
				       sizeof(*x));
			} else {
				IPW_ERROR("Link Deterioration of wrong size %d "
					  "(should be %zd)\n",
A
Al Viro 已提交
4800
					  size, sizeof(*x));
4801
			}
J
James Ketrenos 已提交
4802 4803 4804
			break;
		}

4805 4806 4807
	case HOST_NOTIFICATION_DINO_CONFIG_RESPONSE:{
			IPW_ERROR("Dino config\n");
			if (priv->hcmd
4808
			    && priv->hcmd->cmd != HOST_CMD_DINO_CONFIG)
4809
				IPW_ERROR("Unexpected DINO_CONFIG_RESPONSE\n");
4810

4811 4812
			break;
		}
J
James Ketrenos 已提交
4813

4814 4815
	case HOST_NOTIFICATION_STATUS_BEACON_STATE:{
			struct notif_beacon_state *x = &notif->u.beacon_state;
A
Al Viro 已提交
4816
			if (size != sizeof(*x)) {
4817 4818
				IPW_ERROR
				    ("Beacon state of wrong size %d (should "
A
Al Viro 已提交
4819
				     "be %zd)\n", size, sizeof(*x));
4820
				break;
J
James Ketrenos 已提交
4821 4822
			}

4823 4824 4825 4826 4827
			if (le32_to_cpu(x->state) ==
			    HOST_NOTIFICATION_STATUS_BEACON_MISSING)
				ipw_handle_missed_beacon(priv,
							 le32_to_cpu(x->
								     number));
J
James Ketrenos 已提交
4828

4829 4830
			break;
		}
J
James Ketrenos 已提交
4831

4832 4833
	case HOST_NOTIFICATION_STATUS_TGI_TX_KEY:{
			struct notif_tgi_tx_key *x = &notif->u.tgi_tx_key;
A
Al Viro 已提交
4834
			if (size == sizeof(*x)) {
4835 4836 4837 4838 4839 4840
				IPW_ERROR("TGi Tx Key: state 0x%02x sec type "
					  "0x%02x station %d\n",
					  x->key_state, x->security_type,
					  x->station_index);
				break;
			}
J
James Ketrenos 已提交
4841

4842 4843
			IPW_ERROR
			    ("TGi Tx Key of wrong size %d (should be %zd)\n",
A
Al Viro 已提交
4844
			     size, sizeof(*x));
J
James Ketrenos 已提交
4845
			break;
4846
		}
J
James Ketrenos 已提交
4847

4848 4849
	case HOST_NOTIFICATION_CALIB_KEEP_RESULTS:{
			struct notif_calibration *x = &notif->u.calibration;
J
James Ketrenos 已提交
4850

A
Al Viro 已提交
4851
			if (size == sizeof(*x)) {
4852 4853 4854 4855
				memcpy(&priv->calib, x, sizeof(*x));
				IPW_DEBUG_INFO("TODO: Calibration\n");
				break;
			}
J
James Ketrenos 已提交
4856

4857 4858
			IPW_ERROR
			    ("Calibration of wrong size %d (should be %zd)\n",
A
Al Viro 已提交
4859
			     size, sizeof(*x));
J
James Ketrenos 已提交
4860
			break;
4861 4862
		}

4863
	case HOST_NOTIFICATION_NOISE_STATS:{
A
Al Viro 已提交
4864
			if (size == sizeof(u32)) {
4865 4866 4867 4868
				priv->exp_avg_noise =
				    exponential_average(priv->exp_avg_noise,
				    (u8) (le32_to_cpu(notif->u.noise.value) & 0xff),
				    DEPTH_NOISE);
4869 4870
				break;
			}
J
James Ketrenos 已提交
4871

4872 4873
			IPW_ERROR
			    ("Noise stat is wrong size %d (should be %zd)\n",
A
Al Viro 已提交
4874
			     size, sizeof(u32));
J
James Ketrenos 已提交
4875 4876 4877 4878
			break;
		}

	default:
4879 4880
		IPW_DEBUG_NOTIF("Unknown notification: "
				"subtype=%d,flags=0x%2x,size=%d\n",
A
Al Viro 已提交
4881
				notif->subtype, notif->flags, size);
J
James Ketrenos 已提交
4882 4883 4884 4885 4886
	}
}

/**
 * Destroys all DMA structures and initialise them again
4887
 *
J
James Ketrenos 已提交
4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898
 * @param priv
 * @return error code
 */
static int ipw_queue_reset(struct ipw_priv *priv)
{
	int rc = 0;
	/** @todo customize queue sizes */
	int nTx = 64, nTxCmd = 8;
	ipw_tx_queue_free(priv);
	/* Tx CMD queue */
	rc = ipw_queue_tx_init(priv, &priv->txq_cmd, nTxCmd,
4899 4900 4901 4902
			       IPW_TX_CMD_QUEUE_READ_INDEX,
			       IPW_TX_CMD_QUEUE_WRITE_INDEX,
			       IPW_TX_CMD_QUEUE_BD_BASE,
			       IPW_TX_CMD_QUEUE_BD_SIZE);
J
James Ketrenos 已提交
4903 4904 4905 4906 4907 4908
	if (rc) {
		IPW_ERROR("Tx Cmd queue init failed\n");
		goto error;
	}
	/* Tx queue(s) */
	rc = ipw_queue_tx_init(priv, &priv->txq[0], nTx,
4909 4910 4911
			       IPW_TX_QUEUE_0_READ_INDEX,
			       IPW_TX_QUEUE_0_WRITE_INDEX,
			       IPW_TX_QUEUE_0_BD_BASE, IPW_TX_QUEUE_0_BD_SIZE);
J
James Ketrenos 已提交
4912 4913 4914 4915 4916
	if (rc) {
		IPW_ERROR("Tx 0 queue init failed\n");
		goto error;
	}
	rc = ipw_queue_tx_init(priv, &priv->txq[1], nTx,
4917 4918 4919
			       IPW_TX_QUEUE_1_READ_INDEX,
			       IPW_TX_QUEUE_1_WRITE_INDEX,
			       IPW_TX_QUEUE_1_BD_BASE, IPW_TX_QUEUE_1_BD_SIZE);
J
James Ketrenos 已提交
4920 4921 4922 4923 4924
	if (rc) {
		IPW_ERROR("Tx 1 queue init failed\n");
		goto error;
	}
	rc = ipw_queue_tx_init(priv, &priv->txq[2], nTx,
4925 4926 4927
			       IPW_TX_QUEUE_2_READ_INDEX,
			       IPW_TX_QUEUE_2_WRITE_INDEX,
			       IPW_TX_QUEUE_2_BD_BASE, IPW_TX_QUEUE_2_BD_SIZE);
J
James Ketrenos 已提交
4928 4929 4930 4931 4932
	if (rc) {
		IPW_ERROR("Tx 2 queue init failed\n");
		goto error;
	}
	rc = ipw_queue_tx_init(priv, &priv->txq[3], nTx,
4933 4934 4935
			       IPW_TX_QUEUE_3_READ_INDEX,
			       IPW_TX_QUEUE_3_WRITE_INDEX,
			       IPW_TX_QUEUE_3_BD_BASE, IPW_TX_QUEUE_3_BD_SIZE);
J
James Ketrenos 已提交
4936 4937 4938 4939 4940 4941 4942 4943 4944
	if (rc) {
		IPW_ERROR("Tx 3 queue init failed\n");
		goto error;
	}
	/* statistics */
	priv->rx_bufs_min = 0;
	priv->rx_pend_max = 0;
	return rc;

4945
      error:
J
James Ketrenos 已提交
4946 4947 4948 4949 4950 4951
	ipw_tx_queue_free(priv);
	return rc;
}

/**
 * Reclaim Tx queue entries no more used by NIC.
4952
 *
S
Stefano Brivio 已提交
4953
 * When FW advances 'R' index, all entries between old and
J
James Ketrenos 已提交
4954 4955
 * new 'R' index need to be reclaimed. As result, some free space
 * forms. If there is enough free space (> low mark), wake Tx queue.
4956
 *
J
James Ketrenos 已提交
4957 4958 4959 4960 4961 4962
 * @note Need to protect against garbage in 'R' index
 * @param priv
 * @param txq
 * @param qindex
 * @return Number of used entries remains in the queue
 */
4963
static int ipw_queue_tx_reclaim(struct ipw_priv *priv,
J
James Ketrenos 已提交
4964 4965 4966 4967 4968 4969 4970 4971 4972
				struct clx2_tx_queue *txq, int qindex)
{
	u32 hw_tail;
	int used;
	struct clx2_queue *q = &txq->q;

	hw_tail = ipw_read32(priv, q->reg_r);
	if (hw_tail >= q->n_bd) {
		IPW_ERROR
4973 4974
		    ("Read index for DMA queue (%d) is out of range [0-%d)\n",
		     hw_tail, q->n_bd);
J
James Ketrenos 已提交
4975 4976 4977 4978 4979 4980 4981
		goto done;
	}
	for (; q->last_used != hw_tail;
	     q->last_used = ipw_queue_inc_wrap(q->last_used, q->n_bd)) {
		ipw_queue_tx_free_tfd(priv, txq);
		priv->tx_packets++;
	}
4982
      done:
4983
	if ((ipw_tx_queue_space(q) > q->low_mark) &&
4984
	    (qindex >= 0))
4985
		netif_wake_queue(priv->net_dev);
J
James Ketrenos 已提交
4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999
	used = q->first_empty - q->last_used;
	if (used < 0)
		used += q->n_bd;

	return used;
}

static int ipw_queue_tx_hcmd(struct ipw_priv *priv, int hcmd, void *buf,
			     int len, int sync)
{
	struct clx2_tx_queue *txq = &priv->txq_cmd;
	struct clx2_queue *q = &txq->q;
	struct tfd_frame *tfd;

5000
	if (ipw_tx_queue_space(q) < (sync ? 1 : 2)) {
J
James Ketrenos 已提交
5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021
		IPW_ERROR("No space for Tx\n");
		return -EBUSY;
	}

	tfd = &txq->bd[q->first_empty];
	txq->txb[q->first_empty] = NULL;

	memset(tfd, 0, sizeof(*tfd));
	tfd->control_flags.message_type = TX_HOST_COMMAND_TYPE;
	tfd->control_flags.control_bits = TFD_NEED_IRQ_MASK;
	priv->hcmd_seq++;
	tfd->u.cmd.index = hcmd;
	tfd->u.cmd.length = len;
	memcpy(tfd->u.cmd.payload, buf, len);
	q->first_empty = ipw_queue_inc_wrap(q->first_empty, q->n_bd);
	ipw_write32(priv, q->reg_w, q->first_empty);
	_ipw_read32(priv, 0x90);

	return 0;
}

5022
/*
J
James Ketrenos 已提交
5023 5024 5025
 * Rx theory of operation
 *
 * The host allocates 32 DMA target addresses and passes the host address
5026
 * to the firmware at register IPW_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
J
James Ketrenos 已提交
5027 5028 5029 5030 5031
 * 0 to 31
 *
 * Rx Queue Indexes
 * The host/firmware share two index registers for managing the Rx buffers.
 *
5032 5033 5034
 * The READ index maps to the first position that the firmware may be writing
 * to -- the driver can read up to (but not including) this position and get
 * good data.
J
James Ketrenos 已提交
5035 5036 5037 5038 5039 5040
 * The READ index is managed by the firmware once the card is enabled.
 *
 * The WRITE index maps to the last position the driver has read from -- the
 * position preceding WRITE is the last slot the firmware can place a packet.
 *
 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
5041
 * WRITE = READ.
J
James Ketrenos 已提交
5042
 *
5043
 * During initialization the host sets up the READ queue position to the first
J
James Ketrenos 已提交
5044 5045 5046 5047 5048 5049
 * INDEX position, and WRITE to the last (READ - 1 wrapped)
 *
 * When the firmware places a packet in a buffer it will advance the READ index
 * and fire the RX interrupt.  The driver can then query the READ index and
 * process as many packets as possible, moving the WRITE index forward as it
 * resets the Rx queue buffers with new memory.
5050
 *
J
James Ketrenos 已提交
5051
 * The management in the driver is as follows:
5052
 * + A list of pre-allocated SKBs is stored in ipw->rxq->rx_free.  When
J
James Ketrenos 已提交
5053
 *   ipw->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
5054
 *   to replensish the ipw->rxq->rx_free.
J
James Ketrenos 已提交
5055 5056 5057 5058 5059 5060
 * + In ipw_rx_queue_replenish (scheduled) if 'processed' != 'read' then the
 *   ipw->rxq is replenished and the READ INDEX is updated (updating the
 *   'processed' and 'read' driver indexes as well)
 * + A received packet is processed and handed to the kernel network stack,
 *   detached from the ipw->rxq.  The driver 'processed' index is updated.
 * + The Host/Firmware ipw->rxq is replenished at tasklet time from the rx_free
5061 5062
 *   list. If there are no allocated buffers in ipw->rxq->rx_free, the READ
 *   INDEX is not incremented and ipw->status(RX_STALLED) is set.  If there
J
James Ketrenos 已提交
5063 5064 5065 5066 5067
 *   were enough free buffers and RX_STALLED is set it is cleared.
 *
 *
 * Driver sequence:
 *
5068
 * ipw_rx_queue_alloc()       Allocates rx_free
J
James Ketrenos 已提交
5069 5070 5071 5072 5073 5074 5075 5076 5077
 * ipw_rx_queue_replenish()   Replenishes rx_free list from rx_used, and calls
 *                            ipw_rx_queue_restock
 * ipw_rx_queue_restock()     Moves available buffers from rx_free into Rx
 *                            queue, updates firmware pointers, and updates
 *                            the WRITE index.  If insufficient rx_free buffers
 *                            are available, schedules ipw_rx_queue_replenish
 *
 * -- enable interrupts --
 * ISR - ipw_rx()             Detach ipw_rx_mem_buffers from pool up to the
5078
 *                            READ INDEX, detaching the SKB from the pool.
J
James Ketrenos 已提交
5079 5080 5081 5082 5083 5084 5085
 *                            Moves the packet buffer from queue to rx_used.
 *                            Calls ipw_rx_queue_restock to refill any empty
 *                            slots.
 * ...
 *
 */

5086
/*
J
James Ketrenos 已提交
5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104
 * If there are slots in the RX queue that  need to be restocked,
 * and we have free pre-allocated buffers, fill the ranks as much
 * as we can pulling from rx_free.
 *
 * This moves the 'write' index forward to catch up with 'processed', and
 * also updates the memory address in the firmware to reference the new
 * target buffer.
 */
static void ipw_rx_queue_restock(struct ipw_priv *priv)
{
	struct ipw_rx_queue *rxq = priv->rxq;
	struct list_head *element;
	struct ipw_rx_mem_buffer *rxb;
	unsigned long flags;
	int write;

	spin_lock_irqsave(&rxq->lock, flags);
	write = rxq->write;
5105
	while ((ipw_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
J
James Ketrenos 已提交
5106 5107 5108 5109
		element = rxq->rx_free.next;
		rxb = list_entry(element, struct ipw_rx_mem_buffer, list);
		list_del(element);

5110
		ipw_write32(priv, IPW_RFDS_TABLE_LOWER + rxq->write * RFD_SIZE,
J
James Ketrenos 已提交
5111 5112 5113 5114 5115 5116 5117
			    rxb->dma_addr);
		rxq->queue[rxq->write] = rxb;
		rxq->write = (rxq->write + 1) % RX_QUEUE_SIZE;
		rxq->free_count--;
	}
	spin_unlock_irqrestore(&rxq->lock, flags);

5118
	/* If the pre-allocated buffer pool is dropping low, schedule to
J
James Ketrenos 已提交
5119 5120 5121 5122 5123
	 * refill it */
	if (rxq->free_count <= RX_LOW_WATERMARK)
		queue_work(priv->workqueue, &priv->rx_replenish);

	/* If we've added more space for the firmware to place data, tell it */
5124
	if (write != rxq->write)
5125
		ipw_write32(priv, IPW_RX_WRITE_INDEX, rxq->write);
J
James Ketrenos 已提交
5126 5127 5128 5129
}

/*
 * Move all used packet from rx_used to rx_free, allocating a new SKB for each.
5130 5131
 * Also restock the Rx queue via ipw_rx_queue_restock.
 *
J
James Ketrenos 已提交
5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145
 * This is called as a scheduled work item (except for during intialization)
 */
static void ipw_rx_queue_replenish(void *data)
{
	struct ipw_priv *priv = data;
	struct ipw_rx_queue *rxq = priv->rxq;
	struct list_head *element;
	struct ipw_rx_mem_buffer *rxb;
	unsigned long flags;

	spin_lock_irqsave(&rxq->lock, flags);
	while (!list_empty(&rxq->rx_used)) {
		element = rxq->rx_used.next;
		rxb = list_entry(element, struct ipw_rx_mem_buffer, list);
5146
		rxb->skb = alloc_skb(IPW_RX_BUF_SIZE, GFP_ATOMIC);
J
James Ketrenos 已提交
5147 5148 5149 5150 5151 5152 5153 5154 5155
		if (!rxb->skb) {
			printk(KERN_CRIT "%s: Can not allocate SKB buffers.\n",
			       priv->net_dev->name);
			/* We don't reschedule replenish work here -- we will
			 * call the restock method and if it still needs
			 * more buffers it will schedule replenish */
			break;
		}
		list_del(element);
5156

5157 5158
		rxb->dma_addr =
		    pci_map_single(priv->pci_dev, rxb->skb->data,
5159
				   IPW_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
5160

J
James Ketrenos 已提交
5161 5162 5163 5164 5165 5166 5167 5168
		list_add_tail(&rxb->list, &rxq->rx_free);
		rxq->free_count++;
	}
	spin_unlock_irqrestore(&rxq->lock, flags);

	ipw_rx_queue_restock(priv);
}

D
David Howells 已提交
5169
static void ipw_bg_rx_queue_replenish(struct work_struct *work)
5170
{
D
David Howells 已提交
5171 5172
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, rx_replenish);
5173
	mutex_lock(&priv->mutex);
D
David Howells 已提交
5174
	ipw_rx_queue_replenish(priv);
5175
	mutex_unlock(&priv->mutex);
5176 5177
}

J
James Ketrenos 已提交
5178
/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
5179
 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
5180
 * This free routine walks the list of POOL entries and if SKB is set to
J
James Ketrenos 已提交
5181 5182
 * non NULL it is unmapped and freed
 */
5183
static void ipw_rx_queue_free(struct ipw_priv *priv, struct ipw_rx_queue *rxq)
J
James Ketrenos 已提交
5184 5185 5186 5187 5188
{
	int i;

	if (!rxq)
		return;
5189

J
James Ketrenos 已提交
5190 5191 5192
	for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
		if (rxq->pool[i].skb != NULL) {
			pci_unmap_single(priv->pci_dev, rxq->pool[i].dma_addr,
5193
					 IPW_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
J
James Ketrenos 已提交
5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205
			dev_kfree_skb(rxq->pool[i].skb);
		}
	}

	kfree(rxq);
}

static struct ipw_rx_queue *ipw_rx_queue_alloc(struct ipw_priv *priv)
{
	struct ipw_rx_queue *rxq;
	int i;

5206
	rxq = kzalloc(sizeof(*rxq), GFP_KERNEL);
5207 5208 5209 5210
	if (unlikely(!rxq)) {
		IPW_ERROR("memory allocation failed\n");
		return NULL;
	}
J
James Ketrenos 已提交
5211 5212 5213 5214 5215
	spin_lock_init(&rxq->lock);
	INIT_LIST_HEAD(&rxq->rx_free);
	INIT_LIST_HEAD(&rxq->rx_used);

	/* Fill the rx_used queue with _all_ of the Rx buffers */
5216
	for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
J
James Ketrenos 已提交
5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231
		list_add_tail(&rxq->pool[i].list, &rxq->rx_used);

	/* Set us so that we have processed and used all buffers, but have
	 * not restocked the Rx queue with fresh buffers */
	rxq->read = rxq->write = 0;
	rxq->free_count = 0;

	return rxq;
}

static int ipw_is_rate_in_mask(struct ipw_priv *priv, int ieee_mode, u8 rate)
{
	rate &= ~IEEE80211_BASIC_RATE_MASK;
	if (ieee_mode == IEEE_A) {
		switch (rate) {
5232 5233
		case IEEE80211_OFDM_RATE_6MB:
			return priv->rates_mask & IEEE80211_OFDM_RATE_6MB_MASK ?
5234
			    1 : 0;
5235 5236
		case IEEE80211_OFDM_RATE_9MB:
			return priv->rates_mask & IEEE80211_OFDM_RATE_9MB_MASK ?
5237
			    1 : 0;
5238
		case IEEE80211_OFDM_RATE_12MB:
5239 5240
			return priv->
			    rates_mask & IEEE80211_OFDM_RATE_12MB_MASK ? 1 : 0;
5241
		case IEEE80211_OFDM_RATE_18MB:
5242 5243
			return priv->
			    rates_mask & IEEE80211_OFDM_RATE_18MB_MASK ? 1 : 0;
5244
		case IEEE80211_OFDM_RATE_24MB:
5245 5246
			return priv->
			    rates_mask & IEEE80211_OFDM_RATE_24MB_MASK ? 1 : 0;
5247
		case IEEE80211_OFDM_RATE_36MB:
5248 5249
			return priv->
			    rates_mask & IEEE80211_OFDM_RATE_36MB_MASK ? 1 : 0;
5250
		case IEEE80211_OFDM_RATE_48MB:
5251 5252
			return priv->
			    rates_mask & IEEE80211_OFDM_RATE_48MB_MASK ? 1 : 0;
5253
		case IEEE80211_OFDM_RATE_54MB:
5254 5255
			return priv->
			    rates_mask & IEEE80211_OFDM_RATE_54MB_MASK ? 1 : 0;
J
James Ketrenos 已提交
5256 5257 5258 5259
		default:
			return 0;
		}
	}
5260

J
James Ketrenos 已提交
5261 5262
	/* B and G mixed */
	switch (rate) {
5263
	case IEEE80211_CCK_RATE_1MB:
J
James Ketrenos 已提交
5264
		return priv->rates_mask & IEEE80211_CCK_RATE_1MB_MASK ? 1 : 0;
5265
	case IEEE80211_CCK_RATE_2MB:
J
James Ketrenos 已提交
5266
		return priv->rates_mask & IEEE80211_CCK_RATE_2MB_MASK ? 1 : 0;
5267
	case IEEE80211_CCK_RATE_5MB:
J
James Ketrenos 已提交
5268
		return priv->rates_mask & IEEE80211_CCK_RATE_5MB_MASK ? 1 : 0;
5269
	case IEEE80211_CCK_RATE_11MB:
J
James Ketrenos 已提交
5270 5271 5272 5273 5274 5275 5276 5277 5278
		return priv->rates_mask & IEEE80211_CCK_RATE_11MB_MASK ? 1 : 0;
	}

	/* If we are limited to B modulations, bail at this point */
	if (ieee_mode == IEEE_B)
		return 0;

	/* G */
	switch (rate) {
5279
	case IEEE80211_OFDM_RATE_6MB:
J
James Ketrenos 已提交
5280
		return priv->rates_mask & IEEE80211_OFDM_RATE_6MB_MASK ? 1 : 0;
5281
	case IEEE80211_OFDM_RATE_9MB:
J
James Ketrenos 已提交
5282
		return priv->rates_mask & IEEE80211_OFDM_RATE_9MB_MASK ? 1 : 0;
5283
	case IEEE80211_OFDM_RATE_12MB:
J
James Ketrenos 已提交
5284
		return priv->rates_mask & IEEE80211_OFDM_RATE_12MB_MASK ? 1 : 0;
5285
	case IEEE80211_OFDM_RATE_18MB:
J
James Ketrenos 已提交
5286
		return priv->rates_mask & IEEE80211_OFDM_RATE_18MB_MASK ? 1 : 0;
5287
	case IEEE80211_OFDM_RATE_24MB:
J
James Ketrenos 已提交
5288
		return priv->rates_mask & IEEE80211_OFDM_RATE_24MB_MASK ? 1 : 0;
5289
	case IEEE80211_OFDM_RATE_36MB:
J
James Ketrenos 已提交
5290
		return priv->rates_mask & IEEE80211_OFDM_RATE_36MB_MASK ? 1 : 0;
5291
	case IEEE80211_OFDM_RATE_48MB:
J
James Ketrenos 已提交
5292
		return priv->rates_mask & IEEE80211_OFDM_RATE_48MB_MASK ? 1 : 0;
5293
	case IEEE80211_OFDM_RATE_54MB:
J
James Ketrenos 已提交
5294 5295 5296 5297 5298 5299
		return priv->rates_mask & IEEE80211_OFDM_RATE_54MB_MASK ? 1 : 0;
	}

	return 0;
}

5300
static int ipw_compatible_rates(struct ipw_priv *priv,
J
James Ketrenos 已提交
5301 5302 5303 5304 5305 5306
				const struct ieee80211_network *network,
				struct ipw_supported_rates *rates)
{
	int num_rates, i;

	memset(rates, 0, sizeof(*rates));
5307
	num_rates = min(network->rates_len, (u8) IPW_MAX_RATES);
J
James Ketrenos 已提交
5308 5309
	rates->num_rates = 0;
	for (i = 0; i < num_rates; i++) {
5310 5311 5312
		if (!ipw_is_rate_in_mask(priv, network->mode,
					 network->rates[i])) {

5313
			if (network->rates[i] & IEEE80211_BASIC_RATE_MASK) {
5314 5315 5316 5317 5318 5319
				IPW_DEBUG_SCAN("Adding masked mandatory "
					       "rate %02X\n",
					       network->rates[i]);
				rates->supported_rates[rates->num_rates++] =
				    network->rates[i];
				continue;
5320 5321
			}

J
James Ketrenos 已提交
5322 5323 5324 5325
			IPW_DEBUG_SCAN("Rate %02X masked : 0x%08X\n",
				       network->rates[i], priv->rates_mask);
			continue;
		}
5326

J
James Ketrenos 已提交
5327 5328 5329
		rates->supported_rates[rates->num_rates++] = network->rates[i];
	}

5330 5331
	num_rates = min(network->rates_ex_len,
			(u8) (IPW_MAX_RATES - num_rates));
J
James Ketrenos 已提交
5332
	for (i = 0; i < num_rates; i++) {
5333 5334
		if (!ipw_is_rate_in_mask(priv, network->mode,
					 network->rates_ex[i])) {
5335
			if (network->rates_ex[i] & IEEE80211_BASIC_RATE_MASK) {
5336 5337 5338 5339 5340 5341
				IPW_DEBUG_SCAN("Adding masked mandatory "
					       "rate %02X\n",
					       network->rates_ex[i]);
				rates->supported_rates[rates->num_rates++] =
				    network->rates[i];
				continue;
5342 5343
			}

J
James Ketrenos 已提交
5344 5345 5346 5347
			IPW_DEBUG_SCAN("Rate %02X masked : 0x%08X\n",
				       network->rates_ex[i], priv->rates_mask);
			continue;
		}
5348

5349 5350
		rates->supported_rates[rates->num_rates++] =
		    network->rates_ex[i];
J
James Ketrenos 已提交
5351 5352
	}

5353
	return 1;
J
James Ketrenos 已提交
5354 5355
}

5356
static void ipw_copy_rates(struct ipw_supported_rates *dest,
J
James Ketrenos 已提交
5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368
				  const struct ipw_supported_rates *src)
{
	u8 i;
	for (i = 0; i < src->num_rates; i++)
		dest->supported_rates[i] = src->supported_rates[i];
	dest->num_rates = src->num_rates;
}

/* TODO: Look at sniffed packets in the air to determine if the basic rate
 * mask should ever be used -- right now all callers to add the scan rates are
 * set with the modulation = CCK, so BASIC_RATE_MASK is never set... */
static void ipw_add_cck_scan_rates(struct ipw_supported_rates *rates,
5369
				   u8 modulation, u32 rate_mask)
J
James Ketrenos 已提交
5370
{
5371
	u8 basic_mask = (IEEE80211_OFDM_MODULATION == modulation) ?
5372
	    IEEE80211_BASIC_RATE_MASK : 0;
5373

J
James Ketrenos 已提交
5374
	if (rate_mask & IEEE80211_CCK_RATE_1MB_MASK)
5375
		rates->supported_rates[rates->num_rates++] =
5376
		    IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
J
James Ketrenos 已提交
5377 5378

	if (rate_mask & IEEE80211_CCK_RATE_2MB_MASK)
5379
		rates->supported_rates[rates->num_rates++] =
5380
		    IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
J
James Ketrenos 已提交
5381 5382

	if (rate_mask & IEEE80211_CCK_RATE_5MB_MASK)
5383
		rates->supported_rates[rates->num_rates++] = basic_mask |
5384
		    IEEE80211_CCK_RATE_5MB;
J
James Ketrenos 已提交
5385 5386

	if (rate_mask & IEEE80211_CCK_RATE_11MB_MASK)
5387
		rates->supported_rates[rates->num_rates++] = basic_mask |
5388
		    IEEE80211_CCK_RATE_11MB;
J
James Ketrenos 已提交
5389 5390 5391
}

static void ipw_add_ofdm_scan_rates(struct ipw_supported_rates *rates,
5392
				    u8 modulation, u32 rate_mask)
J
James Ketrenos 已提交
5393
{
5394
	u8 basic_mask = (IEEE80211_OFDM_MODULATION == modulation) ?
5395
	    IEEE80211_BASIC_RATE_MASK : 0;
J
James Ketrenos 已提交
5396 5397

	if (rate_mask & IEEE80211_OFDM_RATE_6MB_MASK)
5398
		rates->supported_rates[rates->num_rates++] = basic_mask |
5399
		    IEEE80211_OFDM_RATE_6MB;
J
James Ketrenos 已提交
5400 5401

	if (rate_mask & IEEE80211_OFDM_RATE_9MB_MASK)
5402
		rates->supported_rates[rates->num_rates++] =
5403
		    IEEE80211_OFDM_RATE_9MB;
J
James Ketrenos 已提交
5404 5405

	if (rate_mask & IEEE80211_OFDM_RATE_12MB_MASK)
5406
		rates->supported_rates[rates->num_rates++] = basic_mask |
5407
		    IEEE80211_OFDM_RATE_12MB;
J
James Ketrenos 已提交
5408 5409

	if (rate_mask & IEEE80211_OFDM_RATE_18MB_MASK)
5410
		rates->supported_rates[rates->num_rates++] =
5411
		    IEEE80211_OFDM_RATE_18MB;
J
James Ketrenos 已提交
5412 5413

	if (rate_mask & IEEE80211_OFDM_RATE_24MB_MASK)
5414
		rates->supported_rates[rates->num_rates++] = basic_mask |
5415
		    IEEE80211_OFDM_RATE_24MB;
J
James Ketrenos 已提交
5416 5417

	if (rate_mask & IEEE80211_OFDM_RATE_36MB_MASK)
5418
		rates->supported_rates[rates->num_rates++] =
5419
		    IEEE80211_OFDM_RATE_36MB;
J
James Ketrenos 已提交
5420 5421

	if (rate_mask & IEEE80211_OFDM_RATE_48MB_MASK)
5422
		rates->supported_rates[rates->num_rates++] =
5423
		    IEEE80211_OFDM_RATE_48MB;
J
James Ketrenos 已提交
5424 5425

	if (rate_mask & IEEE80211_OFDM_RATE_54MB_MASK)
5426
		rates->supported_rates[rates->num_rates++] =
5427
		    IEEE80211_OFDM_RATE_54MB;
J
James Ketrenos 已提交
5428 5429 5430 5431 5432 5433 5434
}

struct ipw_network_match {
	struct ieee80211_network *network;
	struct ipw_supported_rates rates;
};

5435 5436 5437 5438
static int ipw_find_adhoc_network(struct ipw_priv *priv,
				  struct ipw_network_match *match,
				  struct ieee80211_network *network,
				  int roaming)
J
James Ketrenos 已提交
5439 5440
{
	struct ipw_supported_rates rates;
5441
	DECLARE_SSID_BUF(ssid);
J
James Ketrenos 已提交
5442 5443 5444

	/* Verify that this network's capability is compatible with the
	 * current mode (AdHoc or Infrastructure) */
5445
	if ((priv->ieee->iw_mode == IW_MODE_ADHOC &&
J
James Ketrenos 已提交
5446
	     !(network->capability & WLAN_CAPABILITY_IBSS))) {
J
Johannes Berg 已提交
5447
		IPW_DEBUG_MERGE("Network '%s (%pM)' excluded due to "
5448
				"capability mismatch.\n",
5449 5450
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5451
				network->bssid);
J
James Ketrenos 已提交
5452 5453 5454 5455 5456 5457 5458
		return 0;
	}

	if (unlikely(roaming)) {
		/* If we are roaming, then ensure check if this is a valid
		 * network to try and roam to */
		if ((network->ssid_len != match->network->ssid_len) ||
5459
		    memcmp(network->ssid, match->network->ssid,
J
James Ketrenos 已提交
5460
			   network->ssid_len)) {
J
Johannes Berg 已提交
5461
			IPW_DEBUG_MERGE("Network '%s (%pM)' excluded "
J
James Ketrenos 已提交
5462
					"because of non-network ESSID.\n",
5463 5464
					print_ssid(ssid, network->ssid,
						   network->ssid_len),
J
Johannes Berg 已提交
5465
					network->bssid);
J
James Ketrenos 已提交
5466 5467 5468
			return 0;
		}
	} else {
5469 5470 5471
		/* If an ESSID has been configured then compare the broadcast
		 * ESSID to ours */
		if ((priv->config & CFG_STATIC_ESSID) &&
J
James Ketrenos 已提交
5472
		    ((network->ssid_len != priv->essid_len) ||
5473
		     memcmp(network->ssid, priv->essid,
J
James Ketrenos 已提交
5474 5475
			    min(network->ssid_len, priv->essid_len)))) {
			char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
5476

5477
			strncpy(escaped,
5478 5479
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
James Ketrenos 已提交
5480
				sizeof(escaped));
J
Johannes Berg 已提交
5481
			IPW_DEBUG_MERGE("Network '%s (%pM)' excluded "
5482
					"because of ESSID mismatch: '%s'.\n",
J
Johannes Berg 已提交
5483
					escaped, network->bssid,
5484 5485
					print_ssid(ssid, priv->essid,
						   priv->essid_len));
J
James Ketrenos 已提交
5486 5487 5488 5489 5490 5491
			return 0;
		}
	}

	/* If the old network rate is better than this one, don't bother
	 * testing everything else. */
5492 5493

	if (network->time_stamp[0] < match->network->time_stamp[0]) {
5494 5495
		IPW_DEBUG_MERGE("Network '%s excluded because newer than "
				"current network.\n",
5496 5497
				print_ssid(ssid, match->network->ssid,
					   match->network->ssid_len));
J
James Ketrenos 已提交
5498
		return 0;
5499
	} else if (network->time_stamp[1] < match->network->time_stamp[1]) {
5500 5501
		IPW_DEBUG_MERGE("Network '%s excluded because newer than "
				"current network.\n",
5502 5503
				print_ssid(ssid, match->network->ssid,
					   match->network->ssid_len));
J
James Ketrenos 已提交
5504 5505 5506 5507
		return 0;
	}

	/* Now go through and see if the requested network is valid... */
5508
	if (priv->ieee->scan_age != 0 &&
5509
	    time_after(jiffies, network->last_scanned + priv->ieee->scan_age)) {
J
Johannes Berg 已提交
5510
		IPW_DEBUG_MERGE("Network '%s (%pM)' excluded "
5511
				"because of age: %ums.\n",
5512 5513
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5514
				network->bssid,
5515 5516
				jiffies_to_msecs(jiffies -
						 network->last_scanned));
J
James Ketrenos 已提交
5517
		return 0;
5518
	}
J
James Ketrenos 已提交
5519

5520
	if ((priv->config & CFG_STATIC_CHANNEL) &&
J
James Ketrenos 已提交
5521
	    (network->channel != priv->channel)) {
J
Johannes Berg 已提交
5522
		IPW_DEBUG_MERGE("Network '%s (%pM)' excluded "
J
James Ketrenos 已提交
5523
				"because of channel mismatch: %d != %d.\n",
5524 5525
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5526
				network->bssid,
J
James Ketrenos 已提交
5527 5528 5529
				network->channel, priv->channel);
		return 0;
	}
5530

J
James Ketrenos 已提交
5531
	/* Verify privacy compatability */
5532
	if (((priv->capability & CAP_PRIVACY_ON) ? 1 : 0) !=
J
James Ketrenos 已提交
5533
	    ((network->capability & WLAN_CAPABILITY_PRIVACY) ? 1 : 0)) {
J
Johannes Berg 已提交
5534
		IPW_DEBUG_MERGE("Network '%s (%pM)' excluded "
J
James Ketrenos 已提交
5535
				"because of privacy mismatch: %s != %s.\n",
5536 5537
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5538
				network->bssid,
5539 5540 5541 5542 5543
				priv->
				capability & CAP_PRIVACY_ON ? "on" : "off",
				network->
				capability & WLAN_CAPABILITY_PRIVACY ? "on" :
				"off");
J
James Ketrenos 已提交
5544 5545
		return 0;
	}
5546

5547
	if (!memcmp(network->bssid, priv->bssid, ETH_ALEN)) {
J
Johannes Berg 已提交
5548 5549
		IPW_DEBUG_MERGE("Network '%s (%pM)' excluded "
				"because of the same BSSID match: %pM"
5550 5551
				".\n", print_ssid(ssid, network->ssid,
						  network->ssid_len),
J
Johannes Berg 已提交
5552 5553
				network->bssid,
				priv->bssid);
J
James Ketrenos 已提交
5554 5555
		return 0;
	}
5556

J
James Ketrenos 已提交
5557 5558
	/* Filter out any incompatible freq / mode combinations */
	if (!ieee80211_is_valid_mode(priv->ieee, network->mode)) {
J
Johannes Berg 已提交
5559
		IPW_DEBUG_MERGE("Network '%s (%pM)' excluded "
J
James Ketrenos 已提交
5560 5561
				"because of invalid frequency/mode "
				"combination.\n",
5562 5563
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5564
				network->bssid);
J
James Ketrenos 已提交
5565 5566
		return 0;
	}
5567

5568 5569 5570
	/* Ensure that the rates supported by the driver are compatible with
	 * this AP, including verification of basic rates (mandatory) */
	if (!ipw_compatible_rates(priv, network, &rates)) {
J
Johannes Berg 已提交
5571
		IPW_DEBUG_MERGE("Network '%s (%pM)' excluded "
5572 5573
				"because configured rate mask excludes "
				"AP mandatory rate.\n",
5574 5575
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5576
				network->bssid);
5577 5578 5579
		return 0;
	}

J
James Ketrenos 已提交
5580
	if (rates.num_rates == 0) {
J
Johannes Berg 已提交
5581
		IPW_DEBUG_MERGE("Network '%s (%pM)' excluded "
J
James Ketrenos 已提交
5582
				"because of no compatible rates.\n",
5583 5584
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5585
				network->bssid);
J
James Ketrenos 已提交
5586 5587
		return 0;
	}
5588

J
James Ketrenos 已提交
5589 5590 5591 5592 5593 5594 5595
	/* TODO: Perform any further minimal comparititive tests.  We do not
	 * want to put too much policy logic here; intelligent scan selection
	 * should occur within a generic IEEE 802.11 user space tool.  */

	/* Set up 'new' AP to this network */
	ipw_copy_rates(&match->rates, &rates);
	match->network = network;
J
Johannes Berg 已提交
5596
	IPW_DEBUG_MERGE("Network '%s (%pM)' is a viable match.\n",
5597
			print_ssid(ssid, network->ssid, network->ssid_len),
J
Johannes Berg 已提交
5598
			network->bssid);
J
James Ketrenos 已提交
5599 5600 5601 5602

	return 1;
}

D
David Howells 已提交
5603
static void ipw_merge_adhoc_network(struct work_struct *work)
J
James Ketrenos 已提交
5604
{
5605
	DECLARE_SSID_BUF(ssid);
D
David Howells 已提交
5606 5607
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, merge_networks);
5608 5609 5610 5611 5612
	struct ieee80211_network *network = NULL;
	struct ipw_network_match match = {
		.network = priv->assoc_network
	};

5613 5614
	if ((priv->status & STATUS_ASSOCIATED) &&
	    (priv->ieee->iw_mode == IW_MODE_ADHOC)) {
5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632
		/* First pass through ROAM process -- look for a better
		 * network */
		unsigned long flags;

		spin_lock_irqsave(&priv->ieee->lock, flags);
		list_for_each_entry(network, &priv->ieee->network_list, list) {
			if (network != priv->assoc_network)
				ipw_find_adhoc_network(priv, &match, network,
						       1);
		}
		spin_unlock_irqrestore(&priv->ieee->lock, flags);

		if (match.network == priv->assoc_network) {
			IPW_DEBUG_MERGE("No better ADHOC in this network to "
					"merge to.\n");
			return;
		}

5633
		mutex_lock(&priv->mutex);
5634 5635
		if ((priv->ieee->iw_mode == IW_MODE_ADHOC)) {
			IPW_DEBUG_MERGE("remove network %s\n",
5636 5637
					print_ssid(ssid, priv->essid,
						   priv->essid_len));
5638
			ipw_remove_current_network(priv);
J
James Ketrenos 已提交
5639
		}
5640 5641 5642

		ipw_disassociate(priv);
		priv->assoc_network = match.network;
5643
		mutex_unlock(&priv->mutex);
5644
		return;
J
James Ketrenos 已提交
5645
	}
5646
}
J
James Ketrenos 已提交
5647

5648 5649 5650
static int ipw_best_network(struct ipw_priv *priv,
			    struct ipw_network_match *match,
			    struct ieee80211_network *network, int roaming)
J
James Ketrenos 已提交
5651 5652
{
	struct ipw_supported_rates rates;
5653
	DECLARE_SSID_BUF(ssid);
J
James Ketrenos 已提交
5654 5655 5656 5657

	/* Verify that this network's capability is compatible with the
	 * current mode (AdHoc or Infrastructure) */
	if ((priv->ieee->iw_mode == IW_MODE_INFRA &&
5658
	     !(network->capability & WLAN_CAPABILITY_ESS)) ||
J
James Ketrenos 已提交
5659 5660
	    (priv->ieee->iw_mode == IW_MODE_ADHOC &&
	     !(network->capability & WLAN_CAPABILITY_IBSS))) {
J
Johannes Berg 已提交
5661
		IPW_DEBUG_ASSOC("Network '%s (%pM)' excluded due to "
5662
				"capability mismatch.\n",
5663 5664
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5665
				network->bssid);
J
James Ketrenos 已提交
5666 5667 5668 5669 5670 5671 5672
		return 0;
	}

	if (unlikely(roaming)) {
		/* If we are roaming, then ensure check if this is a valid
		 * network to try and roam to */
		if ((network->ssid_len != match->network->ssid_len) ||
5673
		    memcmp(network->ssid, match->network->ssid,
J
James Ketrenos 已提交
5674
			   network->ssid_len)) {
J
Johannes Berg 已提交
5675
			IPW_DEBUG_ASSOC("Network '%s (%pM)' excluded "
J
James Ketrenos 已提交
5676
					"because of non-network ESSID.\n",
5677 5678
					print_ssid(ssid, network->ssid,
						   network->ssid_len),
J
Johannes Berg 已提交
5679
					network->bssid);
J
James Ketrenos 已提交
5680 5681 5682
			return 0;
		}
	} else {
5683 5684 5685
		/* If an ESSID has been configured then compare the broadcast
		 * ESSID to ours */
		if ((priv->config & CFG_STATIC_ESSID) &&
J
James Ketrenos 已提交
5686
		    ((network->ssid_len != priv->essid_len) ||
5687
		     memcmp(network->ssid, priv->essid,
J
James Ketrenos 已提交
5688 5689
			    min(network->ssid_len, priv->essid_len)))) {
			char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
5690
			strncpy(escaped,
5691 5692
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
James Ketrenos 已提交
5693
				sizeof(escaped));
J
Johannes Berg 已提交
5694
			IPW_DEBUG_ASSOC("Network '%s (%pM)' excluded "
5695
					"because of ESSID mismatch: '%s'.\n",
J
Johannes Berg 已提交
5696
					escaped, network->bssid,
5697 5698
					print_ssid(ssid, priv->essid,
						   priv->essid_len));
J
James Ketrenos 已提交
5699 5700 5701 5702 5703 5704
			return 0;
		}
	}

	/* If the old network rate is better than this one, don't bother
	 * testing everything else. */
5705
	if (match->network && match->network->stats.rssi > network->stats.rssi) {
J
James Ketrenos 已提交
5706
		char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
5707
		strncpy(escaped,
5708
			print_ssid(ssid, network->ssid, network->ssid_len),
J
James Ketrenos 已提交
5709
			sizeof(escaped));
J
Johannes Berg 已提交
5710 5711 5712
		IPW_DEBUG_ASSOC("Network '%s (%pM)' excluded because "
				"'%s (%pM)' has a stronger signal.\n",
				escaped, network->bssid,
5713 5714
				print_ssid(ssid, match->network->ssid,
					   match->network->ssid_len),
J
Johannes Berg 已提交
5715
				match->network->bssid);
J
James Ketrenos 已提交
5716 5717
		return 0;
	}
5718

J
James Ketrenos 已提交
5719 5720 5721
	/* If this network has already had an association attempt within the
	 * last 3 seconds, do not try and associate again... */
	if (network->last_associate &&
5722
	    time_after(network->last_associate + (HZ * 3UL), jiffies)) {
J
Johannes Berg 已提交
5723
		IPW_DEBUG_ASSOC("Network '%s (%pM)' excluded "
5724
				"because of storming (%ums since last "
J
James Ketrenos 已提交
5725
				"assoc attempt).\n",
5726 5727
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5728
				network->bssid,
5729 5730
				jiffies_to_msecs(jiffies -
						 network->last_associate));
J
James Ketrenos 已提交
5731 5732 5733 5734
		return 0;
	}

	/* Now go through and see if the requested network is valid... */
5735
	if (priv->ieee->scan_age != 0 &&
5736
	    time_after(jiffies, network->last_scanned + priv->ieee->scan_age)) {
J
Johannes Berg 已提交
5737
		IPW_DEBUG_ASSOC("Network '%s (%pM)' excluded "
5738
				"because of age: %ums.\n",
5739 5740
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5741
				network->bssid,
5742 5743
				jiffies_to_msecs(jiffies -
						 network->last_scanned));
J
James Ketrenos 已提交
5744
		return 0;
5745
	}
J
James Ketrenos 已提交
5746

5747
	if ((priv->config & CFG_STATIC_CHANNEL) &&
J
James Ketrenos 已提交
5748
	    (network->channel != priv->channel)) {
J
Johannes Berg 已提交
5749
		IPW_DEBUG_ASSOC("Network '%s (%pM)' excluded "
J
James Ketrenos 已提交
5750
				"because of channel mismatch: %d != %d.\n",
5751 5752
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5753
				network->bssid,
J
James Ketrenos 已提交
5754 5755 5756
				network->channel, priv->channel);
		return 0;
	}
5757

J
James Ketrenos 已提交
5758
	/* Verify privacy compatability */
5759
	if (((priv->capability & CAP_PRIVACY_ON) ? 1 : 0) !=
J
James Ketrenos 已提交
5760
	    ((network->capability & WLAN_CAPABILITY_PRIVACY) ? 1 : 0)) {
J
Johannes Berg 已提交
5761
		IPW_DEBUG_ASSOC("Network '%s (%pM)' excluded "
J
James Ketrenos 已提交
5762
				"because of privacy mismatch: %s != %s.\n",
5763 5764
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5765
				network->bssid,
5766
				priv->capability & CAP_PRIVACY_ON ? "on" :
J
James Ketrenos 已提交
5767
				"off",
5768
				network->capability &
5769
				WLAN_CAPABILITY_PRIVACY ? "on" : "off");
J
James Ketrenos 已提交
5770 5771
		return 0;
	}
5772 5773

	if ((priv->config & CFG_STATIC_BSSID) &&
J
James Ketrenos 已提交
5774
	    memcmp(network->bssid, priv->bssid, ETH_ALEN)) {
J
Johannes Berg 已提交
5775 5776
		IPW_DEBUG_ASSOC("Network '%s (%pM)' excluded "
				"because of BSSID mismatch: %pM.\n",
5777 5778
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5779
				network->bssid, priv->bssid);
J
James Ketrenos 已提交
5780 5781
		return 0;
	}
5782

J
James Ketrenos 已提交
5783 5784
	/* Filter out any incompatible freq / mode combinations */
	if (!ieee80211_is_valid_mode(priv->ieee, network->mode)) {
J
Johannes Berg 已提交
5785
		IPW_DEBUG_ASSOC("Network '%s (%pM)' excluded "
J
James Ketrenos 已提交
5786 5787
				"because of invalid frequency/mode "
				"combination.\n",
5788 5789
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5790
				network->bssid);
J
James Ketrenos 已提交
5791 5792
		return 0;
	}
5793

5794
	/* Filter out invalid channel in current GEO */
5795
	if (!ieee80211_is_valid_channel(priv->ieee, network->channel)) {
J
Johannes Berg 已提交
5796
		IPW_DEBUG_ASSOC("Network '%s (%pM)' excluded "
5797
				"because of invalid channel in current GEO\n",
5798 5799
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5800
				network->bssid);
5801 5802 5803
		return 0;
	}

5804 5805 5806
	/* Ensure that the rates supported by the driver are compatible with
	 * this AP, including verification of basic rates (mandatory) */
	if (!ipw_compatible_rates(priv, network, &rates)) {
J
Johannes Berg 已提交
5807
		IPW_DEBUG_ASSOC("Network '%s (%pM)' excluded "
5808 5809
				"because configured rate mask excludes "
				"AP mandatory rate.\n",
5810 5811
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5812
				network->bssid);
5813 5814 5815
		return 0;
	}

J
James Ketrenos 已提交
5816
	if (rates.num_rates == 0) {
J
Johannes Berg 已提交
5817
		IPW_DEBUG_ASSOC("Network '%s (%pM)' excluded "
J
James Ketrenos 已提交
5818
				"because of no compatible rates.\n",
5819 5820
				print_ssid(ssid, network->ssid,
					   network->ssid_len),
J
Johannes Berg 已提交
5821
				network->bssid);
J
James Ketrenos 已提交
5822 5823
		return 0;
	}
5824

J
James Ketrenos 已提交
5825 5826 5827 5828 5829 5830 5831 5832
	/* TODO: Perform any further minimal comparititive tests.  We do not
	 * want to put too much policy logic here; intelligent scan selection
	 * should occur within a generic IEEE 802.11 user space tool.  */

	/* Set up 'new' AP to this network */
	ipw_copy_rates(&match->rates, &rates);
	match->network = network;

J
Johannes Berg 已提交
5833
	IPW_DEBUG_ASSOC("Network '%s (%pM)' is a viable match.\n",
5834
			print_ssid(ssid, network->ssid, network->ssid_len),
J
Johannes Berg 已提交
5835
			network->bssid);
J
James Ketrenos 已提交
5836 5837 5838 5839

	return 1;
}

5840
static void ipw_adhoc_create(struct ipw_priv *priv,
5841
			     struct ieee80211_network *network)
J
James Ketrenos 已提交
5842
{
5843
	const struct ieee80211_geo *geo = ieee80211_get_geo(priv->ieee);
5844 5845
	int i;

J
James Ketrenos 已提交
5846 5847 5848 5849 5850 5851
	/*
	 * For the purposes of scanning, we can set our wireless mode
	 * to trigger scans across combinations of bands, but when it
	 * comes to creating a new ad-hoc network, we have tell the FW
	 * exactly which band to use.
	 *
5852
	 * We also have the possibility of an invalid channel for the
J
James Ketrenos 已提交
5853 5854 5855
	 * chossen band.  Attempting to create a new ad-hoc network
	 * with an invalid channel for wireless mode will trigger a
	 * FW fatal error.
5856
	 *
J
James Ketrenos 已提交
5857
	 */
5858
	switch (ieee80211_is_valid_channel(priv->ieee, priv->channel)) {
5859 5860
	case IEEE80211_52GHZ_BAND:
		network->mode = IEEE_A;
5861
		i = ieee80211_channel_to_index(priv->ieee, priv->channel);
5862
		BUG_ON(i == -1);
5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873
		if (geo->a[i].flags & IEEE80211_CH_PASSIVE_ONLY) {
			IPW_WARNING("Overriding invalid channel\n");
			priv->channel = geo->a[0].channel;
		}
		break;

	case IEEE80211_24GHZ_BAND:
		if (priv->ieee->mode & IEEE_G)
			network->mode = IEEE_G;
		else
			network->mode = IEEE_B;
5874
		i = ieee80211_channel_to_index(priv->ieee, priv->channel);
5875
		BUG_ON(i == -1);
5876 5877 5878 5879
		if (geo->bg[i].flags & IEEE80211_CH_PASSIVE_ONLY) {
			IPW_WARNING("Overriding invalid channel\n");
			priv->channel = geo->bg[0].channel;
		}
5880 5881 5882
		break;

	default:
J
James Ketrenos 已提交
5883 5884 5885
		IPW_WARNING("Overriding invalid channel\n");
		if (priv->ieee->mode & IEEE_A) {
			network->mode = IEEE_A;
5886
			priv->channel = geo->a[0].channel;
J
James Ketrenos 已提交
5887 5888
		} else if (priv->ieee->mode & IEEE_G) {
			network->mode = IEEE_G;
5889
			priv->channel = geo->bg[0].channel;
J
James Ketrenos 已提交
5890 5891
		} else {
			network->mode = IEEE_B;
5892
			priv->channel = geo->bg[0].channel;
J
James Ketrenos 已提交
5893
		}
5894 5895
		break;
	}
J
James Ketrenos 已提交
5896 5897 5898 5899 5900 5901 5902 5903

	network->channel = priv->channel;
	priv->config |= CFG_ADHOC_PERSIST;
	ipw_create_bssid(priv, network->bssid);
	network->ssid_len = priv->essid_len;
	memcpy(network->ssid, priv->essid, priv->essid_len);
	memset(&network->stats, 0, sizeof(network->stats));
	network->capability = WLAN_CAPABILITY_IBSS;
5904 5905
	if (!(priv->config & CFG_PREAMBLE_LONG))
		network->capability |= WLAN_CAPABILITY_SHORT_PREAMBLE;
J
James Ketrenos 已提交
5906 5907 5908
	if (priv->capability & CAP_PRIVACY_ON)
		network->capability |= WLAN_CAPABILITY_PRIVACY;
	network->rates_len = min(priv->rates.num_rates, MAX_RATES_LENGTH);
5909
	memcpy(network->rates, priv->rates.supported_rates, network->rates_len);
J
James Ketrenos 已提交
5910
	network->rates_ex_len = priv->rates.num_rates - network->rates_len;
5911
	memcpy(network->rates_ex,
J
James Ketrenos 已提交
5912 5913 5914 5915 5916 5917 5918
	       &priv->rates.supported_rates[network->rates_len],
	       network->rates_ex_len);
	network->last_scanned = 0;
	network->flags = 0;
	network->last_associate = 0;
	network->time_stamp[0] = 0;
	network->time_stamp[1] = 0;
5919 5920 5921
	network->beacon_interval = 100;	/* Default */
	network->listen_interval = 10;	/* Default */
	network->atim_window = 0;	/* Default */
J
James Ketrenos 已提交
5922 5923 5924 5925
	network->wpa_ie_len = 0;
	network->rsn_ie_len = 0;
}

5926 5927
static void ipw_send_tgi_tx_key(struct ipw_priv *priv, int type, int index)
{
Z
Zhu Yi 已提交
5928
	struct ipw_tgi_tx_key key;
5929 5930 5931 5932

	if (!(priv->ieee->sec.flags & (1 << index)))
		return;

Z
Zhu Yi 已提交
5933 5934 5935 5936 5937
	key.key_id = index;
	memcpy(key.key, priv->ieee->sec.keys[index], SCM_TEMPORAL_KEY_LENGTH);
	key.security_type = type;
	key.station_index = 0;	/* always 0 for BSS */
	key.flags = 0;
5938
	/* 0 for new key; previous value of counter (after fatal error) */
5939 5940
	key.tx_counter[0] = cpu_to_le32(0);
	key.tx_counter[1] = cpu_to_le32(0);
5941

Z
Zhu Yi 已提交
5942
	ipw_send_cmd_pdu(priv, IPW_CMD_TGI_TX_KEY, sizeof(key), &key);
5943 5944 5945
}

static void ipw_send_wep_keys(struct ipw_priv *priv, int type)
J
James Ketrenos 已提交
5946
{
Z
Zhu Yi 已提交
5947
	struct ipw_wep_key key;
J
James Ketrenos 已提交
5948 5949
	int i;

Z
Zhu Yi 已提交
5950 5951
	key.cmd_id = DINO_CMD_WEP_KEY;
	key.seq_num = 0;
J
James Ketrenos 已提交
5952

5953 5954
	/* Note: AES keys cannot be set for multiple times.
	 * Only set it at the first time. */
5955
	for (i = 0; i < 4; i++) {
Z
Zhu Yi 已提交
5956
		key.key_index = i | type;
5957
		if (!(priv->ieee->sec.flags & (1 << i))) {
Z
Zhu Yi 已提交
5958
			key.key_size = 0;
5959
			continue;
J
James Ketrenos 已提交
5960 5961
		}

Z
Zhu Yi 已提交
5962 5963
		key.key_size = priv->ieee->sec.key_sizes[i];
		memcpy(key.key, priv->ieee->sec.keys[i], key.key_size);
5964

Z
Zhu Yi 已提交
5965
		ipw_send_cmd_pdu(priv, IPW_CMD_WEP_KEY, sizeof(key), &key);
5966
	}
J
James Ketrenos 已提交
5967 5968
}

5969
static void ipw_set_hw_decrypt_unicast(struct ipw_priv *priv, int level)
J
James Ketrenos 已提交
5970
{
5971
	if (priv->ieee->host_encrypt)
J
James Ketrenos 已提交
5972 5973
		return;

5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017
	switch (level) {
	case SEC_LEVEL_3:
		priv->sys_config.disable_unicast_decryption = 0;
		priv->ieee->host_decrypt = 0;
		break;
	case SEC_LEVEL_2:
		priv->sys_config.disable_unicast_decryption = 1;
		priv->ieee->host_decrypt = 1;
		break;
	case SEC_LEVEL_1:
		priv->sys_config.disable_unicast_decryption = 0;
		priv->ieee->host_decrypt = 0;
		break;
	case SEC_LEVEL_0:
		priv->sys_config.disable_unicast_decryption = 1;
		break;
	default:
		break;
	}
}

static void ipw_set_hw_decrypt_multicast(struct ipw_priv *priv, int level)
{
	if (priv->ieee->host_encrypt)
		return;

	switch (level) {
	case SEC_LEVEL_3:
		priv->sys_config.disable_multicast_decryption = 0;
		break;
	case SEC_LEVEL_2:
		priv->sys_config.disable_multicast_decryption = 1;
		break;
	case SEC_LEVEL_1:
		priv->sys_config.disable_multicast_decryption = 0;
		break;
	case SEC_LEVEL_0:
		priv->sys_config.disable_multicast_decryption = 1;
		break;
	default:
		break;
	}
}

6018 6019 6020 6021
static void ipw_set_hwcrypto_keys(struct ipw_priv *priv)
{
	switch (priv->ieee->sec.level) {
	case SEC_LEVEL_3:
6022 6023 6024 6025
		if (priv->ieee->sec.flags & SEC_ACTIVE_KEY)
			ipw_send_tgi_tx_key(priv,
					    DCT_FLAG_EXT_SECURITY_CCM,
					    priv->ieee->sec.active_key);
6026

H
Hong Liu 已提交
6027 6028
		if (!priv->ieee->host_mc_decrypt)
			ipw_send_wep_keys(priv, DCW_WEP_KEY_SEC_TYPE_CCM);
6029 6030
		break;
	case SEC_LEVEL_2:
6031 6032 6033 6034
		if (priv->ieee->sec.flags & SEC_ACTIVE_KEY)
			ipw_send_tgi_tx_key(priv,
					    DCT_FLAG_EXT_SECURITY_TKIP,
					    priv->ieee->sec.active_key);
6035 6036 6037
		break;
	case SEC_LEVEL_1:
		ipw_send_wep_keys(priv, DCW_WEP_KEY_SEC_TYPE_WEP);
6038 6039
		ipw_set_hw_decrypt_unicast(priv, priv->ieee->sec.level);
		ipw_set_hw_decrypt_multicast(priv, priv->ieee->sec.level);
6040 6041 6042 6043 6044 6045 6046
		break;
	case SEC_LEVEL_0:
	default:
		break;
	}
}

J
James Ketrenos 已提交
6047 6048 6049
static void ipw_adhoc_check(void *data)
{
	struct ipw_priv *priv = data;
6050

6051
	if (priv->missed_adhoc_beacons++ > priv->disassociate_threshold &&
J
James Ketrenos 已提交
6052
	    !(priv->config & CFG_ADHOC_PERSIST)) {
6053 6054 6055 6056
		IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF |
			  IPW_DL_STATE | IPW_DL_ASSOC,
			  "Missed beacon: %d - disassociate\n",
			  priv->missed_adhoc_beacons);
J
James Ketrenos 已提交
6057 6058 6059 6060 6061
		ipw_remove_current_network(priv);
		ipw_disassociate(priv);
		return;
	}

6062
	queue_delayed_work(priv->workqueue, &priv->adhoc_check,
6063
			   le16_to_cpu(priv->assoc_request.beacon_interval));
J
James Ketrenos 已提交
6064 6065
}

D
David Howells 已提交
6066
static void ipw_bg_adhoc_check(struct work_struct *work)
6067
{
D
David Howells 已提交
6068 6069
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, adhoc_check.work);
6070
	mutex_lock(&priv->mutex);
D
David Howells 已提交
6071
	ipw_adhoc_check(priv);
6072
	mutex_unlock(&priv->mutex);
6073 6074
}

J
James Ketrenos 已提交
6075 6076
static void ipw_debug_config(struct ipw_priv *priv)
{
6077
	DECLARE_SSID_BUF(ssid);
J
James Ketrenos 已提交
6078 6079 6080
	IPW_DEBUG_INFO("Scan completed, no valid APs matched "
		       "[CFG 0x%08X]\n", priv->config);
	if (priv->config & CFG_STATIC_CHANNEL)
6081
		IPW_DEBUG_INFO("Channel locked to %d\n", priv->channel);
J
James Ketrenos 已提交
6082 6083 6084
	else
		IPW_DEBUG_INFO("Channel unlocked.\n");
	if (priv->config & CFG_STATIC_ESSID)
6085
		IPW_DEBUG_INFO("ESSID locked to '%s'\n",
6086
			       print_ssid(ssid, priv->essid, priv->essid_len));
J
James Ketrenos 已提交
6087 6088 6089
	else
		IPW_DEBUG_INFO("ESSID unlocked.\n");
	if (priv->config & CFG_STATIC_BSSID)
J
Johannes Berg 已提交
6090
		IPW_DEBUG_INFO("BSSID locked to %pM\n", priv->bssid);
J
James Ketrenos 已提交
6091 6092 6093 6094 6095 6096 6097 6098 6099
	else
		IPW_DEBUG_INFO("BSSID unlocked.\n");
	if (priv->capability & CAP_PRIVACY_ON)
		IPW_DEBUG_INFO("PRIVACY on\n");
	else
		IPW_DEBUG_INFO("PRIVACY off\n");
	IPW_DEBUG_INFO("RATE MASK: 0x%08X\n", priv->rates_mask);
}

6100
static void ipw_set_fixed_rate(struct ipw_priv *priv, int mode)
J
James Ketrenos 已提交
6101 6102 6103 6104 6105 6106 6107 6108
{
	/* TODO: Verify that this works... */
	struct ipw_fixed_rate fr = {
		.tx_rates = priv->rates_mask
	};
	u32 reg;
	u16 mask = 0;

6109
	/* Identify 'current FW band' and match it with the fixed
J
James Ketrenos 已提交
6110
	 * Tx rates */
6111

J
James Ketrenos 已提交
6112
	switch (priv->ieee->freq_band) {
6113
	case IEEE80211_52GHZ_BAND:	/* A only */
J
James Ketrenos 已提交
6114 6115 6116
		/* IEEE_A */
		if (priv->rates_mask & ~IEEE80211_OFDM_RATES_MASK) {
			/* Invalid fixed rate mask */
6117 6118
			IPW_DEBUG_WX
			    ("invalid fixed rate mask in ipw_set_fixed_rate\n");
J
James Ketrenos 已提交
6119 6120 6121
			fr.tx_rates = 0;
			break;
		}
6122

J
James Ketrenos 已提交
6123 6124 6125
		fr.tx_rates >>= IEEE80211_OFDM_SHIFT_MASK_A;
		break;

6126
	default:		/* 2.4Ghz or Mixed */
J
James Ketrenos 已提交
6127
		/* IEEE_B */
6128
		if (mode == IEEE_B) {
J
James Ketrenos 已提交
6129 6130
			if (fr.tx_rates & ~IEEE80211_CCK_RATES_MASK) {
				/* Invalid fixed rate mask */
6131 6132
				IPW_DEBUG_WX
				    ("invalid fixed rate mask in ipw_set_fixed_rate\n");
J
James Ketrenos 已提交
6133 6134 6135
				fr.tx_rates = 0;
			}
			break;
6136
		}
J
James Ketrenos 已提交
6137 6138 6139 6140 6141

		/* IEEE_G */
		if (fr.tx_rates & ~(IEEE80211_CCK_RATES_MASK |
				    IEEE80211_OFDM_RATES_MASK)) {
			/* Invalid fixed rate mask */
6142 6143
			IPW_DEBUG_WX
			    ("invalid fixed rate mask in ipw_set_fixed_rate\n");
J
James Ketrenos 已提交
6144 6145 6146
			fr.tx_rates = 0;
			break;
		}
6147

J
James Ketrenos 已提交
6148 6149 6150 6151
		if (IEEE80211_OFDM_RATE_6MB_MASK & fr.tx_rates) {
			mask |= (IEEE80211_OFDM_RATE_6MB_MASK >> 1);
			fr.tx_rates &= ~IEEE80211_OFDM_RATE_6MB_MASK;
		}
6152

J
James Ketrenos 已提交
6153 6154 6155 6156
		if (IEEE80211_OFDM_RATE_9MB_MASK & fr.tx_rates) {
			mask |= (IEEE80211_OFDM_RATE_9MB_MASK >> 1);
			fr.tx_rates &= ~IEEE80211_OFDM_RATE_9MB_MASK;
		}
6157

J
James Ketrenos 已提交
6158 6159 6160 6161
		if (IEEE80211_OFDM_RATE_12MB_MASK & fr.tx_rates) {
			mask |= (IEEE80211_OFDM_RATE_12MB_MASK >> 1);
			fr.tx_rates &= ~IEEE80211_OFDM_RATE_12MB_MASK;
		}
6162

J
James Ketrenos 已提交
6163 6164 6165 6166 6167
		fr.tx_rates |= mask;
		break;
	}

	reg = ipw_read32(priv, IPW_MEM_FIXED_OVERRIDE);
6168
	ipw_write_reg32(priv, reg, *(u32 *) & fr);
J
James Ketrenos 已提交
6169 6170
}

6171
static void ipw_abort_scan(struct ipw_priv *priv)
J
James Ketrenos 已提交
6172 6173 6174
{
	int err;

6175 6176 6177 6178 6179
	if (priv->status & STATUS_SCAN_ABORTING) {
		IPW_DEBUG_HC("Ignoring concurrent scan abort request.\n");
		return;
	}
	priv->status |= STATUS_SCAN_ABORTING;
J
James Ketrenos 已提交
6180

6181 6182 6183 6184 6185
	err = ipw_send_scan_abort(priv);
	if (err)
		IPW_DEBUG_HC("Request to abort scan failed.\n");
}

6186 6187 6188
static void ipw_add_scan_channels(struct ipw_priv *priv,
				  struct ipw_scan_request_ext *scan,
				  int scan_type)
6189 6190
{
	int channel_index = 0;
6191
	const struct ieee80211_geo *geo;
6192
	int i;
6193

6194
	geo = ieee80211_get_geo(priv->ieee);
J
James Ketrenos 已提交
6195

6196 6197 6198 6199 6200 6201 6202 6203
	if (priv->ieee->freq_band & IEEE80211_52GHZ_BAND) {
		int start = channel_index;
		for (i = 0; i < geo->a_channels; i++) {
			if ((priv->status & STATUS_ASSOCIATED) &&
			    geo->a[i].channel == priv->channel)
				continue;
			channel_index++;
			scan->channels_list[channel_index] = geo->a[i].channel;
6204 6205 6206 6207 6208
			ipw_set_scan_type(scan, channel_index,
					  geo->a[i].
					  flags & IEEE80211_CH_PASSIVE_ONLY ?
					  IPW_SCAN_PASSIVE_FULL_DWELL_SCAN :
					  scan_type);
6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220
		}

		if (start != channel_index) {
			scan->channels_list[start] = (u8) (IPW_A_MODE << 6) |
			    (channel_index - start);
			channel_index++;
		}
	}

	if (priv->ieee->freq_band & IEEE80211_24GHZ_BAND) {
		int start = channel_index;
		if (priv->config & CFG_SPEED_SCAN) {
6221
			int index;
6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252
			u8 channels[IEEE80211_24GHZ_CHANNELS] = {
				/* nop out the list */
				[0] = 0
			};

			u8 channel;
			while (channel_index < IPW_SCAN_CHANNELS) {
				channel =
				    priv->speed_scan[priv->speed_scan_pos];
				if (channel == 0) {
					priv->speed_scan_pos = 0;
					channel = priv->speed_scan[0];
				}
				if ((priv->status & STATUS_ASSOCIATED) &&
				    channel == priv->channel) {
					priv->speed_scan_pos++;
					continue;
				}

				/* If this channel has already been
				 * added in scan, break from loop
				 * and this will be the first channel
				 * in the next scan.
				 */
				if (channels[channel - 1] != 0)
					break;

				channels[channel - 1] = 1;
				priv->speed_scan_pos++;
				channel_index++;
				scan->channels_list[channel_index] = channel;
6253
				index =
6254
				    ieee80211_channel_to_index(priv->ieee, channel);
6255
				ipw_set_scan_type(scan, channel_index,
6256 6257 6258 6259 6260
						  geo->bg[index].
						  flags &
						  IEEE80211_CH_PASSIVE_ONLY ?
						  IPW_SCAN_PASSIVE_FULL_DWELL_SCAN
						  : scan_type);
6261 6262 6263 6264 6265 6266 6267 6268 6269 6270
			}
		} else {
			for (i = 0; i < geo->bg_channels; i++) {
				if ((priv->status & STATUS_ASSOCIATED) &&
				    geo->bg[i].channel == priv->channel)
					continue;
				channel_index++;
				scan->channels_list[channel_index] =
				    geo->bg[i].channel;
				ipw_set_scan_type(scan, channel_index,
6271 6272 6273 6274 6275
						  geo->bg[i].
						  flags &
						  IEEE80211_CH_PASSIVE_ONLY ?
						  IPW_SCAN_PASSIVE_FULL_DWELL_SCAN
						  : scan_type);
6276 6277 6278 6279 6280 6281 6282 6283 6284 6285
			}
		}

		if (start != channel_index) {
			scan->channels_list[start] = (u8) (IPW_B_MODE << 6) |
			    (channel_index - start);
		}
	}
}

6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299
static int ipw_passive_dwell_time(struct ipw_priv *priv)
{
	/* staying on passive channels longer than the DTIM interval during a
	 * scan, while associated, causes the firmware to cancel the scan
	 * without notification. Hence, don't stay on passive channels longer
	 * than the beacon interval.
	 */
	if (priv->status & STATUS_ASSOCIATED
	    && priv->assoc_network->beacon_interval > 10)
		return priv->assoc_network->beacon_interval - 10;
	else
		return 120;
}

D
Dan Williams 已提交
6300
static int ipw_request_scan_helper(struct ipw_priv *priv, int type, int direct)
6301 6302 6303 6304 6305 6306 6307 6308
{
	struct ipw_scan_request_ext scan;
	int err = 0, scan_type;

	if (!(priv->status & STATUS_INIT) ||
	    (priv->status & STATUS_EXIT_PENDING))
		return 0;

6309
	mutex_lock(&priv->mutex);
6310

D
Dan Williams 已提交
6311 6312 6313 6314 6315 6316
	if (direct && (priv->direct_scan_ssid_len == 0)) {
		IPW_DEBUG_HC("Direct scan requested but no SSID to scan for\n");
		priv->status &= ~STATUS_DIRECT_SCAN_PENDING;
		goto done;
	}

6317
	if (priv->status & STATUS_SCANNING) {
D
Dan Williams 已提交
6318 6319 6320
		IPW_DEBUG_HC("Concurrent scan requested.  Queuing.\n");
		priv->status |= direct ? STATUS_DIRECT_SCAN_PENDING :
					STATUS_SCAN_PENDING;
6321
		goto done;
6322
	}
J
James Ketrenos 已提交
6323

6324 6325
	if (!(priv->status & STATUS_SCAN_FORCED) &&
	    priv->status & STATUS_SCAN_ABORTING) {
6326
		IPW_DEBUG_HC("Scan request while abort pending.  Queuing.\n");
D
Dan Williams 已提交
6327 6328
		priv->status |= direct ? STATUS_DIRECT_SCAN_PENDING :
					STATUS_SCAN_PENDING;
6329
		goto done;
J
James Ketrenos 已提交
6330 6331
	}

6332
	if (priv->status & STATUS_RF_KILL_MASK) {
D
Dan Williams 已提交
6333 6334 6335
		IPW_DEBUG_HC("Queuing scan due to RF Kill activation\n");
		priv->status |= direct ? STATUS_DIRECT_SCAN_PENDING :
					STATUS_SCAN_PENDING;
6336
		goto done;
6337
	}
J
James Ketrenos 已提交
6338

6339
	memset(&scan, 0, sizeof(scan));
6340
	scan.full_scan_index = cpu_to_le32(ieee80211_get_scans(priv->ieee));
J
James Ketrenos 已提交
6341

6342
	if (type == IW_SCAN_TYPE_PASSIVE) {
6343 6344
		IPW_DEBUG_WX("use passive scanning\n");
		scan_type = IPW_SCAN_PASSIVE_FULL_DWELL_SCAN;
6345
		scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] =
6346
			cpu_to_le16(ipw_passive_dwell_time(priv));
6347 6348 6349 6350 6351
		ipw_add_scan_channels(priv, &scan, scan_type);
		goto send_request;
	}

	/* Use active scan by default. */
6352
	if (priv->config & CFG_SPEED_SCAN)
6353
		scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_SCAN] =
6354
			cpu_to_le16(30);
6355 6356
	else
		scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_SCAN] =
6357
			cpu_to_le16(20);
6358

6359
	scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN] =
6360
		cpu_to_le16(20);
J
James Ketrenos 已提交
6361

6362 6363
	scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] =
		cpu_to_le16(ipw_passive_dwell_time(priv));
D
Dan Williams 已提交
6364
	scan.dwell_time[IPW_SCAN_ACTIVE_DIRECT_SCAN] = cpu_to_le16(20);
J
James Ketrenos 已提交
6365

6366
#ifdef CONFIG_IPW2200_MONITOR
6367
	if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
6368
		u8 channel;
6369
		u8 band = 0;
J
James Ketrenos 已提交
6370

6371
		switch (ieee80211_is_valid_channel(priv->ieee, priv->channel)) {
6372
		case IEEE80211_52GHZ_BAND:
6373
			band = (u8) (IPW_A_MODE << 6) | 1;
6374 6375
			channel = priv->channel;
			break;
6376

6377
		case IEEE80211_24GHZ_BAND:
6378
			band = (u8) (IPW_B_MODE << 6) | 1;
6379 6380
			channel = priv->channel;
			break;
6381

6382
		default:
6383 6384
			band = (u8) (IPW_B_MODE << 6) | 1;
			channel = 9;
6385
			break;
6386 6387
		}

6388 6389 6390
		scan.channels_list[0] = band;
		scan.channels_list[1] = channel;
		ipw_set_scan_type(&scan, 1, IPW_SCAN_PASSIVE_FULL_DWELL_SCAN);
6391

6392 6393 6394 6395 6396 6397 6398 6399
		/* NOTE:  The card will sit on this channel for this time
		 * period.  Scan aborts are timing sensitive and frequently
		 * result in firmware restarts.  As such, it is best to
		 * set a small dwell_time here and just keep re-issuing
		 * scans.  Otherwise fast channel hopping will not actually
		 * hop channels.
		 *
		 * TODO: Move SPEED SCAN support to all modes and bands */
6400
		scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] =
6401
			cpu_to_le16(2000);
J
James Ketrenos 已提交
6402
	} else {
6403
#endif				/* CONFIG_IPW2200_MONITOR */
D
Dan Williams 已提交
6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420
		/* Honor direct scans first, otherwise if we are roaming make
		 * this a direct scan for the current network.  Finally,
		 * ensure that every other scan is a fast channel hop scan */
		if (direct) {
			err = ipw_send_ssid(priv, priv->direct_scan_ssid,
			                    priv->direct_scan_ssid_len);
			if (err) {
				IPW_DEBUG_HC("Attempt to send SSID command  "
					     "failed\n");
				goto done;
			}

			scan_type = IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN;
		} else if ((priv->status & STATUS_ROAMING)
			   || (!(priv->status & STATUS_ASSOCIATED)
			       && (priv->config & CFG_STATIC_ESSID)
			       && (le32_to_cpu(scan.full_scan_index) % 2))) {
6421 6422
			err = ipw_send_ssid(priv, priv->essid, priv->essid_len);
			if (err) {
6423 6424 6425
				IPW_DEBUG_HC("Attempt to send SSID command "
					     "failed.\n");
				goto done;
6426
			}
J
James Ketrenos 已提交
6427

6428
			scan_type = IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN;
6429
		} else
6430 6431
			scan_type = IPW_SCAN_ACTIVE_BROADCAST_SCAN;

6432
		ipw_add_scan_channels(priv, &scan, scan_type);
6433
#ifdef CONFIG_IPW2200_MONITOR
J
James Ketrenos 已提交
6434
	}
6435
#endif
6436

6437
send_request:
6438
	err = ipw_send_scan_request_ext(priv, &scan);
J
James Ketrenos 已提交
6439
	if (err) {
6440
		IPW_DEBUG_HC("Sending scan command failed: %08X\n", err);
6441
		goto done;
J
James Ketrenos 已提交
6442 6443
	}

6444
	priv->status |= STATUS_SCANNING;
D
Dan Williams 已提交
6445 6446 6447 6448 6449 6450
	if (direct) {
		priv->status &= ~STATUS_DIRECT_SCAN_PENDING;
		priv->direct_scan_ssid_len = 0;
	} else
		priv->status &= ~STATUS_SCAN_PENDING;

6451 6452
	queue_delayed_work(priv->workqueue, &priv->scan_check,
			   IPW_SCAN_CHECK_WATCHDOG);
6453
done:
6454
	mutex_unlock(&priv->mutex);
6455
	return err;
6456 6457
}

D
David Howells 已提交
6458 6459 6460
static void ipw_request_passive_scan(struct work_struct *work)
{
	struct ipw_priv *priv =
D
Dan Williams 已提交
6461 6462
		container_of(work, struct ipw_priv, request_passive_scan.work);
	ipw_request_scan_helper(priv, IW_SCAN_TYPE_PASSIVE, 0);
6463 6464
}

D
David Howells 已提交
6465 6466 6467 6468
static void ipw_request_scan(struct work_struct *work)
{
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, request_scan.work);
D
Dan Williams 已提交
6469 6470 6471 6472 6473 6474 6475 6476
	ipw_request_scan_helper(priv, IW_SCAN_TYPE_ACTIVE, 0);
}

static void ipw_request_direct_scan(struct work_struct *work)
{
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, request_direct_scan.work);
	ipw_request_scan_helper(priv, IW_SCAN_TYPE_ACTIVE, 1);
6477 6478
}

D
David Howells 已提交
6479
static void ipw_bg_abort_scan(struct work_struct *work)
6480
{
D
David Howells 已提交
6481 6482
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, abort_scan);
6483
	mutex_lock(&priv->mutex);
D
David Howells 已提交
6484
	ipw_abort_scan(priv);
6485
	mutex_unlock(&priv->mutex);
6486 6487
}

6488 6489
static int ipw_wpa_enable(struct ipw_priv *priv, int value)
{
6490 6491
	/* This is called when wpa_supplicant loads and closes the driver
	 * interface. */
6492
	priv->ieee->wpa_enabled = value;
6493
	return 0;
6494 6495 6496 6497 6498 6499 6500 6501 6502 6503
}

static int ipw_wpa_set_auth_algs(struct ipw_priv *priv, int value)
{
	struct ieee80211_device *ieee = priv->ieee;
	struct ieee80211_security sec = {
		.flags = SEC_AUTH_MODE,
	};
	int ret = 0;

6504
	if (value & IW_AUTH_ALG_SHARED_KEY) {
6505 6506
		sec.auth_mode = WLAN_AUTH_SHARED_KEY;
		ieee->open_wep = 0;
6507
	} else if (value & IW_AUTH_ALG_OPEN_SYSTEM) {
6508 6509
		sec.auth_mode = WLAN_AUTH_OPEN;
		ieee->open_wep = 1;
6510 6511 6512
	} else if (value & IW_AUTH_ALG_LEAP) {
		sec.auth_mode = WLAN_AUTH_LEAP;
		ieee->open_wep = 1;
6513 6514
	} else
		return -EINVAL;
6515 6516 6517 6518 6519 6520 6521 6522 6523

	if (ieee->set_security)
		ieee->set_security(ieee->dev, &sec);
	else
		ret = -EOPNOTSUPP;

	return ret;
}

6524 6525
static void ipw_wpa_assoc_frame(struct ipw_priv *priv, char *wpa_ie,
				int wpa_ie_len)
6526 6527 6528 6529 6530 6531 6532 6533 6534 6535
{
	/* make sure WPA is enabled */
	ipw_wpa_enable(priv, 1);
}

static int ipw_set_rsn_capa(struct ipw_priv *priv,
			    char *capabilities, int length)
{
	IPW_DEBUG_HC("HOST_CMD_RSN_CAPABILITIES\n");

Z
Zhu Yi 已提交
6536
	return ipw_send_cmd_pdu(priv, IPW_CMD_RSN_CAPABILITIES, length,
6537
				capabilities);
6538 6539
}

6540
/*
6541 6542 6543 6544 6545 6546 6547
 * WE-18 support
 */

/* SIOCSIWGENIE */
static int ipw_wx_set_genie(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu, char *extra)
6548
{
6549 6550 6551 6552
	struct ipw_priv *priv = ieee80211_priv(dev);
	struct ieee80211_device *ieee = priv->ieee;
	u8 *buf;
	int err = 0;
6553

6554 6555 6556
	if (wrqu->data.length > MAX_WPA_IE_LEN ||
	    (wrqu->data.length && extra == NULL))
		return -EINVAL;
6557

6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568
	if (wrqu->data.length) {
		buf = kmalloc(wrqu->data.length, GFP_KERNEL);
		if (buf == NULL) {
			err = -ENOMEM;
			goto out;
		}

		memcpy(buf, extra, wrqu->data.length);
		kfree(ieee->wpa_ie);
		ieee->wpa_ie = buf;
		ieee->wpa_ie_len = wrqu->data.length;
6569
	} else {
6570 6571 6572
		kfree(ieee->wpa_ie);
		ieee->wpa_ie = NULL;
		ieee->wpa_ie_len = 0;
6573
	}
6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605

	ipw_wpa_assoc_frame(priv, ieee->wpa_ie, ieee->wpa_ie_len);
      out:
	return err;
}

/* SIOCGIWGENIE */
static int ipw_wx_get_genie(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	struct ieee80211_device *ieee = priv->ieee;
	int err = 0;

	if (ieee->wpa_ie_len == 0 || ieee->wpa_ie == NULL) {
		wrqu->data.length = 0;
		goto out;
	}

	if (wrqu->data.length < ieee->wpa_ie_len) {
		err = -E2BIG;
		goto out;
	}

	wrqu->data.length = ieee->wpa_ie_len;
	memcpy(extra, ieee->wpa_ie, ieee->wpa_ie_len);

      out:
	return err;
}

6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622
static int wext_cipher2level(int cipher)
{
	switch (cipher) {
	case IW_AUTH_CIPHER_NONE:
		return SEC_LEVEL_0;
	case IW_AUTH_CIPHER_WEP40:
	case IW_AUTH_CIPHER_WEP104:
		return SEC_LEVEL_1;
	case IW_AUTH_CIPHER_TKIP:
		return SEC_LEVEL_2;
	case IW_AUTH_CIPHER_CCMP:
		return SEC_LEVEL_3;
	default:
		return -1;
	}
}

6623 6624 6625 6626 6627 6628 6629 6630
/* SIOCSIWAUTH */
static int ipw_wx_set_auth(struct net_device *dev,
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	struct ieee80211_device *ieee = priv->ieee;
	struct iw_param *param = &wrqu->param;
6631
	struct lib80211_crypt_data *crypt;
6632 6633 6634 6635 6636
	unsigned long flags;
	int ret = 0;

	switch (param->flags & IW_AUTH_INDEX) {
	case IW_AUTH_WPA_VERSION:
6637
		break;
6638
	case IW_AUTH_CIPHER_PAIRWISE:
6639 6640 6641
		ipw_set_hw_decrypt_unicast(priv,
					   wext_cipher2level(param->value));
		break;
6642
	case IW_AUTH_CIPHER_GROUP:
6643 6644 6645
		ipw_set_hw_decrypt_multicast(priv,
					     wext_cipher2level(param->value));
		break;
6646 6647 6648 6649 6650 6651 6652
	case IW_AUTH_KEY_MGMT:
		/*
		 * ipw2200 does not use these parameters
		 */
		break;

	case IW_AUTH_TKIP_COUNTERMEASURES:
6653
		crypt = priv->ieee->crypt_info.crypt[priv->ieee->crypt_info.tx_keyidx];
6654
		if (!crypt || !crypt->ops->set_flags || !crypt->ops->get_flags)
6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705
			break;

		flags = crypt->ops->get_flags(crypt->priv);

		if (param->value)
			flags |= IEEE80211_CRYPTO_TKIP_COUNTERMEASURES;
		else
			flags &= ~IEEE80211_CRYPTO_TKIP_COUNTERMEASURES;

		crypt->ops->set_flags(flags, crypt->priv);

		break;

	case IW_AUTH_DROP_UNENCRYPTED:{
			/* HACK:
			 *
			 * wpa_supplicant calls set_wpa_enabled when the driver
			 * is loaded and unloaded, regardless of if WPA is being
			 * used.  No other calls are made which can be used to
			 * determine if encryption will be used or not prior to
			 * association being expected.  If encryption is not being
			 * used, drop_unencrypted is set to false, else true -- we
			 * can use this to determine if the CAP_PRIVACY_ON bit should
			 * be set.
			 */
			struct ieee80211_security sec = {
				.flags = SEC_ENABLED,
				.enabled = param->value,
			};
			priv->ieee->drop_unencrypted = param->value;
			/* We only change SEC_LEVEL for open mode. Others
			 * are set by ipw_wpa_set_encryption.
			 */
			if (!param->value) {
				sec.flags |= SEC_LEVEL;
				sec.level = SEC_LEVEL_0;
			} else {
				sec.flags |= SEC_LEVEL;
				sec.level = SEC_LEVEL_1;
			}
			if (priv->ieee->set_security)
				priv->ieee->set_security(priv->ieee->dev, &sec);
			break;
		}

	case IW_AUTH_80211_AUTH_ALG:
		ret = ipw_wpa_set_auth_algs(priv, param->value);
		break;

	case IW_AUTH_WPA_ENABLED:
		ret = ipw_wpa_enable(priv, param->value);
6706
		ipw_disassociate(priv);
6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729
		break;

	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
		ieee->ieee802_1x = param->value;
		break;

	case IW_AUTH_PRIVACY_INVOKED:
		ieee->privacy_invoked = param->value;
		break;

	default:
		return -EOPNOTSUPP;
	}
	return ret;
}

/* SIOCGIWAUTH */
static int ipw_wx_get_auth(struct net_device *dev,
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	struct ieee80211_device *ieee = priv->ieee;
6730
	struct lib80211_crypt_data *crypt;
6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745
	struct iw_param *param = &wrqu->param;
	int ret = 0;

	switch (param->flags & IW_AUTH_INDEX) {
	case IW_AUTH_WPA_VERSION:
	case IW_AUTH_CIPHER_PAIRWISE:
	case IW_AUTH_CIPHER_GROUP:
	case IW_AUTH_KEY_MGMT:
		/*
		 * wpa_supplicant will control these internally
		 */
		ret = -EOPNOTSUPP;
		break;

	case IW_AUTH_TKIP_COUNTERMEASURES:
6746
		crypt = priv->ieee->crypt_info.crypt[priv->ieee->crypt_info.tx_keyidx];
6747
		if (!crypt || !crypt->ops->get_flags)
6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791
			break;

		param->value = (crypt->ops->get_flags(crypt->priv) &
				IEEE80211_CRYPTO_TKIP_COUNTERMEASURES) ? 1 : 0;

		break;

	case IW_AUTH_DROP_UNENCRYPTED:
		param->value = ieee->drop_unencrypted;
		break;

	case IW_AUTH_80211_AUTH_ALG:
		param->value = ieee->sec.auth_mode;
		break;

	case IW_AUTH_WPA_ENABLED:
		param->value = ieee->wpa_enabled;
		break;

	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
		param->value = ieee->ieee802_1x;
		break;

	case IW_AUTH_ROAMING_CONTROL:
	case IW_AUTH_PRIVACY_INVOKED:
		param->value = ieee->privacy_invoked;
		break;

	default:
		return -EOPNOTSUPP;
	}
	return 0;
}

/* SIOCSIWENCODEEXT */
static int ipw_wx_set_encodeext(struct net_device *dev,
				struct iw_request_info *info,
				union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;

	if (hwcrypto) {
		if (ext->alg == IW_ENCODE_ALG_TKIP) {
H
Hong Liu 已提交
6792 6793 6794 6795 6796 6797 6798 6799 6800
			/* IPW HW can't build TKIP MIC,
			   host decryption still needed */
			if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY)
				priv->ieee->host_mc_decrypt = 1;
			else {
				priv->ieee->host_encrypt = 0;
				priv->ieee->host_encrypt_msdu = 1;
				priv->ieee->host_decrypt = 1;
			}
6801 6802 6803 6804
		} else {
			priv->ieee->host_encrypt = 0;
			priv->ieee->host_encrypt_msdu = 0;
			priv->ieee->host_decrypt = 0;
H
Hong Liu 已提交
6805
			priv->ieee->host_mc_decrypt = 0;
6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827
		}
	}

	return ieee80211_wx_set_encodeext(priv->ieee, info, wrqu, extra);
}

/* SIOCGIWENCODEEXT */
static int ipw_wx_get_encodeext(struct net_device *dev,
				struct iw_request_info *info,
				union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	return ieee80211_wx_get_encodeext(priv->ieee, info, wrqu, extra);
}

/* SIOCSIWMLME */
static int ipw_wx_set_mlme(struct net_device *dev,
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	struct iw_mlme *mlme = (struct iw_mlme *)extra;
A
Al Viro 已提交
6828
	__le16 reason;
6829 6830 6831 6832 6833

	reason = cpu_to_le16(mlme->reason_code);

	switch (mlme->cmd) {
	case IW_MLME_DEAUTH:
6834
		/* silently ignore */
6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846
		break;

	case IW_MLME_DISASSOC:
		ipw_disassociate(priv);
		break;

	default:
		return -EOPNOTSUPP;
	}
	return 0;
}

6847
#ifdef CONFIG_IPW2200_QOS
6848 6849 6850 6851 6852 6853

/* QoS */
/*
* get the modulation type of the current network or
* the card current mode
*/
6854
static u8 ipw_qos_current_mode(struct ipw_priv * priv)
6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868
{
	u8 mode = 0;

	if (priv->status & STATUS_ASSOCIATED) {
		unsigned long flags;

		spin_lock_irqsave(&priv->ieee->lock, flags);
		mode = priv->assoc_network->mode;
		spin_unlock_irqrestore(&priv->ieee->lock, flags);
	} else {
		mode = priv->ieee->mode;
	}
	IPW_DEBUG_QOS("QoS network/card mode %d \n", mode);
	return mode;
6869
}
6870

6871 6872 6873
/*
* Handle management frame beacon and probe response
*/
6874 6875 6876
static int ipw_qos_handle_probe_response(struct ipw_priv *priv,
					 int active_network,
					 struct ieee80211_network *network)
6877 6878 6879
{
	u32 size = sizeof(struct ieee80211_qos_parameters);

6880
	if (network->capability & WLAN_CAPABILITY_IBSS)
6881 6882 6883
		network->qos_data.active = network->qos_data.supported;

	if (network->flags & NETWORK_HAS_QOS_MASK) {
6884 6885
		if (active_network &&
		    (network->flags & NETWORK_HAS_QOS_PARAMETERS))
6886 6887 6888 6889 6890 6891 6892 6893 6894
			network->qos_data.active = network->qos_data.supported;

		if ((network->qos_data.active == 1) && (active_network == 1) &&
		    (network->flags & NETWORK_HAS_QOS_PARAMETERS) &&
		    (network->qos_data.old_param_count !=
		     network->qos_data.param_count)) {
			network->qos_data.old_param_count =
			    network->qos_data.param_count;
			schedule_work(&priv->qos_activate);
6895 6896
			IPW_DEBUG_QOS("QoS parameters change call "
				      "qos_activate\n");
6897
		}
6898
	} else {
6899 6900
		if ((priv->ieee->mode == IEEE_B) || (network->mode == IEEE_B))
			memcpy(&network->qos_data.parameters,
6901
			       &def_parameters_CCK, size);
6902 6903
		else
			memcpy(&network->qos_data.parameters,
6904
			       &def_parameters_OFDM, size);
6905

6906 6907 6908 6909 6910 6911 6912
		if ((network->qos_data.active == 1) && (active_network == 1)) {
			IPW_DEBUG_QOS("QoS was disabled call qos_activate \n");
			schedule_work(&priv->qos_activate);
		}

		network->qos_data.active = 0;
		network->qos_data.supported = 0;
6913
	}
6914 6915 6916
	if ((priv->status & STATUS_ASSOCIATED) &&
	    (priv->ieee->iw_mode == IW_MODE_ADHOC) && (active_network == 0)) {
		if (memcmp(network->bssid, priv->bssid, ETH_ALEN))
6917
			if (network->capability & WLAN_CAPABILITY_IBSS)
6918
				if ((network->ssid_len ==
6919 6920 6921 6922
				     priv->assoc_network->ssid_len) &&
				    !memcmp(network->ssid,
					    priv->assoc_network->ssid,
					    network->ssid_len)) {
6923 6924 6925 6926
					queue_work(priv->workqueue,
						   &priv->merge_networks);
				}
	}
6927

6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959
	return 0;
}

/*
* This function set up the firmware to support QoS. It sends
* IPW_CMD_QOS_PARAMETERS and IPW_CMD_WME_INFO
*/
static int ipw_qos_activate(struct ipw_priv *priv,
			    struct ieee80211_qos_data *qos_network_data)
{
	int err;
	struct ieee80211_qos_parameters qos_parameters[QOS_QOS_SETS];
	struct ieee80211_qos_parameters *active_one = NULL;
	u32 size = sizeof(struct ieee80211_qos_parameters);
	u32 burst_duration;
	int i;
	u8 type;

	type = ipw_qos_current_mode(priv);

	active_one = &(qos_parameters[QOS_PARAM_SET_DEF_CCK]);
	memcpy(active_one, priv->qos_data.def_qos_parm_CCK, size);
	active_one = &(qos_parameters[QOS_PARAM_SET_DEF_OFDM]);
	memcpy(active_one, priv->qos_data.def_qos_parm_OFDM, size);

	if (qos_network_data == NULL) {
		if (type == IEEE_B) {
			IPW_DEBUG_QOS("QoS activate network mode %d\n", type);
			active_one = &def_parameters_CCK;
		} else
			active_one = &def_parameters_OFDM;

6960
		memcpy(&qos_parameters[QOS_PARAM_SET_ACTIVE], active_one, size);
6961 6962
		burst_duration = ipw_qos_get_burst_duration(priv);
		for (i = 0; i < QOS_QUEUE_NUM; i++)
6963
			qos_parameters[QOS_PARAM_SET_ACTIVE].tx_op_limit[i] =
6964
			    cpu_to_le16(burst_duration);
6965
	} else if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978
		if (type == IEEE_B) {
			IPW_DEBUG_QOS("QoS activate IBSS nework mode %d\n",
				      type);
			if (priv->qos_data.qos_enable == 0)
				active_one = &def_parameters_CCK;
			else
				active_one = priv->qos_data.def_qos_parm_CCK;
		} else {
			if (priv->qos_data.qos_enable == 0)
				active_one = &def_parameters_OFDM;
			else
				active_one = priv->qos_data.def_qos_parm_OFDM;
		}
6979
		memcpy(&qos_parameters[QOS_PARAM_SET_ACTIVE], active_one, size);
6980 6981 6982 6983 6984 6985 6986 6987
	} else {
		unsigned long flags;
		int active;

		spin_lock_irqsave(&priv->ieee->lock, flags);
		active_one = &(qos_network_data->parameters);
		qos_network_data->old_param_count =
		    qos_network_data->param_count;
6988
		memcpy(&qos_parameters[QOS_PARAM_SET_ACTIVE], active_one, size);
6989 6990 6991 6992 6993 6994 6995
		active = qos_network_data->supported;
		spin_unlock_irqrestore(&priv->ieee->lock, flags);

		if (active == 0) {
			burst_duration = ipw_qos_get_burst_duration(priv);
			for (i = 0; i < QOS_QUEUE_NUM; i++)
				qos_parameters[QOS_PARAM_SET_ACTIVE].
6996
				    tx_op_limit[i] = cpu_to_le16(burst_duration);
6997 6998 6999 7000
		}
	}

	IPW_DEBUG_QOS("QoS sending IPW_CMD_QOS_PARAMETERS\n");
7001 7002 7003
	err = ipw_send_qos_params_command(priv,
					  (struct ieee80211_qos_parameters *)
					  &(qos_parameters[0]));
7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052
	if (err)
		IPW_DEBUG_QOS("QoS IPW_CMD_QOS_PARAMETERS failed\n");

	return err;
}

/*
* send IPW_CMD_WME_INFO to the firmware
*/
static int ipw_qos_set_info_element(struct ipw_priv *priv)
{
	int ret = 0;
	struct ieee80211_qos_information_element qos_info;

	if (priv == NULL)
		return -1;

	qos_info.elementID = QOS_ELEMENT_ID;
	qos_info.length = sizeof(struct ieee80211_qos_information_element) - 2;

	qos_info.version = QOS_VERSION_1;
	qos_info.ac_info = 0;

	memcpy(qos_info.qui, qos_oui, QOS_OUI_LEN);
	qos_info.qui_type = QOS_OUI_TYPE;
	qos_info.qui_subtype = QOS_OUI_INFO_SUB_TYPE;

	ret = ipw_send_qos_info_command(priv, &qos_info);
	if (ret != 0) {
		IPW_DEBUG_QOS("QoS error calling ipw_send_qos_info_command\n");
	}
	return ret;
}

/*
* Set the QoS parameter with the association request structure
*/
static int ipw_qos_association(struct ipw_priv *priv,
			       struct ieee80211_network *network)
{
	int err = 0;
	struct ieee80211_qos_data *qos_data = NULL;
	struct ieee80211_qos_data ibss_data = {
		.supported = 1,
		.active = 1,
	};

	switch (priv->ieee->iw_mode) {
	case IW_MODE_ADHOC:
7053
		BUG_ON(!(network->capability & WLAN_CAPABILITY_IBSS));
7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082

		qos_data = &ibss_data;
		break;

	case IW_MODE_INFRA:
		qos_data = &network->qos_data;
		break;

	default:
		BUG();
		break;
	}

	err = ipw_qos_activate(priv, qos_data);
	if (err) {
		priv->assoc_request.policy_support &= ~HC_QOS_SUPPORT_ASSOC;
		return err;
	}

	if (priv->qos_data.qos_enable && qos_data->supported) {
		IPW_DEBUG_QOS("QoS will be enabled for this association\n");
		priv->assoc_request.policy_support |= HC_QOS_SUPPORT_ASSOC;
		return ipw_qos_set_info_element(priv);
	}

	return 0;
}

/*
M
Matt LaPlante 已提交
7083 7084
* handling the beaconing responses. if we get different QoS setting
* off the network from the associated setting, adjust the QoS
7085 7086 7087 7088 7089 7090 7091 7092 7093 7094
* setting
*/
static int ipw_qos_association_resp(struct ipw_priv *priv,
				    struct ieee80211_network *network)
{
	int ret = 0;
	unsigned long flags;
	u32 size = sizeof(struct ieee80211_qos_parameters);
	int set_qos_param = 0;

7095 7096
	if ((priv == NULL) || (network == NULL) ||
	    (priv->assoc_network == NULL))
7097 7098 7099 7100 7101
		return ret;

	if (!(priv->status & STATUS_ASSOCIATED))
		return ret;

7102
	if ((priv->ieee->iw_mode != IW_MODE_INFRA))
7103 7104 7105 7106
		return ret;

	spin_lock_irqsave(&priv->ieee->lock, flags);
	if (network->flags & NETWORK_HAS_QOS_PARAMETERS) {
7107
		memcpy(&priv->assoc_network->qos_data, &network->qos_data,
7108 7109 7110 7111 7112 7113 7114 7115 7116 7117
		       sizeof(struct ieee80211_qos_data));
		priv->assoc_network->qos_data.active = 1;
		if ((network->qos_data.old_param_count !=
		     network->qos_data.param_count)) {
			set_qos_param = 1;
			network->qos_data.old_param_count =
			    network->qos_data.param_count;
		}

	} else {
7118 7119
		if ((network->mode == IEEE_B) || (priv->ieee->mode == IEEE_B))
			memcpy(&priv->assoc_network->qos_data.parameters,
7120
			       &def_parameters_CCK, size);
7121 7122
		else
			memcpy(&priv->assoc_network->qos_data.parameters,
7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143
			       &def_parameters_OFDM, size);
		priv->assoc_network->qos_data.active = 0;
		priv->assoc_network->qos_data.supported = 0;
		set_qos_param = 1;
	}

	spin_unlock_irqrestore(&priv->ieee->lock, flags);

	if (set_qos_param == 1)
		schedule_work(&priv->qos_activate);

	return ret;
}

static u32 ipw_qos_get_burst_duration(struct ipw_priv *priv)
{
	u32 ret = 0;

	if ((priv == NULL))
		return 0;

7144
	if (!(priv->ieee->modulation & IEEE80211_OFDM_MODULATION))
7145
		ret = priv->qos_data.burst_duration_CCK;
7146
	else
7147
		ret = priv->qos_data.burst_duration_OFDM;
7148

7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192
	return ret;
}

/*
* Initialize the setting of QoS global
*/
static void ipw_qos_init(struct ipw_priv *priv, int enable,
			 int burst_enable, u32 burst_duration_CCK,
			 u32 burst_duration_OFDM)
{
	priv->qos_data.qos_enable = enable;

	if (priv->qos_data.qos_enable) {
		priv->qos_data.def_qos_parm_CCK = &def_qos_parameters_CCK;
		priv->qos_data.def_qos_parm_OFDM = &def_qos_parameters_OFDM;
		IPW_DEBUG_QOS("QoS is enabled\n");
	} else {
		priv->qos_data.def_qos_parm_CCK = &def_parameters_CCK;
		priv->qos_data.def_qos_parm_OFDM = &def_parameters_OFDM;
		IPW_DEBUG_QOS("QoS is not enabled\n");
	}

	priv->qos_data.burst_enable = burst_enable;

	if (burst_enable) {
		priv->qos_data.burst_duration_CCK = burst_duration_CCK;
		priv->qos_data.burst_duration_OFDM = burst_duration_OFDM;
	} else {
		priv->qos_data.burst_duration_CCK = 0;
		priv->qos_data.burst_duration_OFDM = 0;
	}
}

/*
* map the packet priority to the right TX Queue
*/
static int ipw_get_tx_queue_number(struct ipw_priv *priv, u16 priority)
{
	if (priority > 7 || !priv->qos_data.qos_enable)
		priority = 0;

	return from_priority_to_tx_queue[priority] - 1;
}

7193 7194
static int ipw_is_qos_active(struct net_device *dev,
			     struct sk_buff *skb)
7195
{
7196
	struct ipw_priv *priv = ieee80211_priv(dev);
7197 7198
	struct ieee80211_qos_data *qos_data = NULL;
	int active, supported;
7199 7200
	u8 *daddr = skb->data + ETH_ALEN;
	int unicast = !is_multicast_ether_addr(daddr);
7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214

	if (!(priv->status & STATUS_ASSOCIATED))
		return 0;

	qos_data = &priv->assoc_network->qos_data;

	if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
		if (unicast == 0)
			qos_data->active = 0;
		else
			qos_data->active = qos_data->supported;
	}
	active = qos_data->active;
	supported = qos_data->supported;
7215 7216 7217
	IPW_DEBUG_QOS("QoS  %d network is QoS active %d  supported %d  "
		      "unicast %d\n",
		      priv->qos_data.qos_enable, active, supported, unicast);
7218 7219
	if (active && priv->qos_data.qos_enable)
		return 1;
7220

7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238
	return 0;

}
/*
* add QoS parameter to the TX command
*/
static int ipw_qos_set_tx_queue_command(struct ipw_priv *priv,
					u16 priority,
					struct tfd_data *tfd)
{
	int tx_queue_id = 0;


	tx_queue_id = from_priority_to_tx_queue[priority] - 1;
	tfd->tx_flags_ext |= DCT_FLAG_EXT_QOS_ENABLED;

	if (priv->qos_data.qos_no_ack_mask & (1UL << tx_queue_id)) {
		tfd->tx_flags &= ~DCT_FLAG_ACK_REQD;
7239
		tfd->tfd.tfd_26.mchdr.qos_ctrl |= cpu_to_le16(CTRL_QOS_NO_ACK);
7240 7241
	}
	return 0;
7242 7243 7244 7245 7246
}

/*
* background support to run QoS activate functionality
*/
D
David Howells 已提交
7247
static void ipw_bg_qos_activate(struct work_struct *work)
7248
{
D
David Howells 已提交
7249 7250
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, qos_activate);
7251 7252 7253 7254

	if (priv == NULL)
		return;

7255
	mutex_lock(&priv->mutex);
7256 7257 7258 7259

	if (priv->status & STATUS_ASSOCIATED)
		ipw_qos_activate(priv, &(priv->assoc_network->qos_data));

7260
	mutex_unlock(&priv->mutex);
7261 7262
}

7263 7264 7265
static int ipw_handle_probe_response(struct net_device *dev,
				     struct ieee80211_probe_response *resp,
				     struct ieee80211_network *network)
7266 7267
{
	struct ipw_priv *priv = ieee80211_priv(dev);
7268 7269
	int active_network = ((priv->status & STATUS_ASSOCIATED) &&
			      (network == priv->assoc_network));
J
James Ketrenos 已提交
7270

7271
	ipw_qos_handle_probe_response(priv, active_network, network);
J
James Ketrenos 已提交
7272

7273 7274
	return 0;
}
J
James Ketrenos 已提交
7275

7276 7277 7278 7279 7280 7281 7282
static int ipw_handle_beacon(struct net_device *dev,
			     struct ieee80211_beacon *resp,
			     struct ieee80211_network *network)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	int active_network = ((priv->status & STATUS_ASSOCIATED) &&
			      (network == priv->assoc_network));
7283

7284
	ipw_qos_handle_probe_response(priv, active_network, network);
7285

7286 7287
	return 0;
}
7288

7289 7290 7291 7292 7293 7294 7295 7296
static int ipw_handle_assoc_response(struct net_device *dev,
				     struct ieee80211_assoc_response *resp,
				     struct ieee80211_network *network)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	ipw_qos_association_resp(priv, network);
	return 0;
}
J
James Ketrenos 已提交
7297

7298 7299 7300
static int ipw_send_qos_params_command(struct ipw_priv *priv, struct ieee80211_qos_parameters
				       *qos_param)
{
7301 7302
	return ipw_send_cmd_pdu(priv, IPW_CMD_QOS_PARAMETERS,
				sizeof(*qos_param) * 3, qos_param);
7303 7304 7305 7306 7307
}

static int ipw_send_qos_info_command(struct ipw_priv *priv, struct ieee80211_qos_information_element
				     *qos_param)
{
7308 7309
	return ipw_send_cmd_pdu(priv, IPW_CMD_WME_INFO, sizeof(*qos_param),
				qos_param);
J
James Ketrenos 已提交
7310 7311
}

7312
#endif				/* CONFIG_IPW2200_QOS */
7313

J
James Ketrenos 已提交
7314 7315
static int ipw_associate_network(struct ipw_priv *priv,
				 struct ieee80211_network *network,
7316
				 struct ipw_supported_rates *rates, int roaming)
J
James Ketrenos 已提交
7317 7318
{
	int err;
7319
	DECLARE_SSID_BUF(ssid);
J
James Ketrenos 已提交
7320 7321

	if (priv->config & CFG_FIXED_RATE)
7322
		ipw_set_fixed_rate(priv, network->mode);
J
James Ketrenos 已提交
7323 7324

	if (!(priv->config & CFG_STATIC_ESSID)) {
7325
		priv->essid_len = min(network->ssid_len,
7326
				      (u8) IW_ESSID_MAX_SIZE);
J
James Ketrenos 已提交
7327 7328 7329 7330 7331 7332 7333
		memcpy(priv->essid, network->ssid, priv->essid_len);
	}

	network->last_associate = jiffies;

	memset(&priv->assoc_request, 0, sizeof(priv->assoc_request));
	priv->assoc_request.channel = network->channel;
7334 7335
	priv->assoc_request.auth_key = 0;

J
James Ketrenos 已提交
7336
	if ((priv->capability & CAP_PRIVACY_ON) &&
7337
	    (priv->ieee->sec.auth_mode == WLAN_AUTH_SHARED_KEY)) {
J
James Ketrenos 已提交
7338
		priv->assoc_request.auth_type = AUTH_SHARED_KEY;
7339 7340
		priv->assoc_request.auth_key = priv->ieee->sec.active_key;

7341
		if (priv->ieee->sec.level == SEC_LEVEL_1)
7342
			ipw_send_wep_keys(priv, DCW_WEP_KEY_SEC_TYPE_WEP);
7343 7344 7345 7346 7347

	} else if ((priv->capability & CAP_PRIVACY_ON) &&
		   (priv->ieee->sec.auth_mode == WLAN_AUTH_LEAP))
		priv->assoc_request.auth_type = AUTH_LEAP;
	else
J
James Ketrenos 已提交
7348 7349
		priv->assoc_request.auth_type = AUTH_OPEN;

7350
	if (priv->ieee->wpa_ie_len) {
7351
		priv->assoc_request.policy_support = cpu_to_le16(0x02);	/* RSN active */
7352 7353 7354
		ipw_set_rsn_capa(priv, priv->ieee->wpa_ie,
				 priv->ieee->wpa_ie_len);
	}
J
James Ketrenos 已提交
7355

7356 7357 7358
	/*
	 * It is valid for our ieee device to support multiple modes, but
	 * when it comes to associating to a given network we have to choose
J
James Ketrenos 已提交
7359 7360 7361 7362 7363 7364 7365 7366 7367
	 * just one mode.
	 */
	if (network->mode & priv->ieee->mode & IEEE_A)
		priv->assoc_request.ieee_mode = IPW_A_MODE;
	else if (network->mode & priv->ieee->mode & IEEE_G)
		priv->assoc_request.ieee_mode = IPW_G_MODE;
	else if (network->mode & priv->ieee->mode & IEEE_B)
		priv->assoc_request.ieee_mode = IPW_B_MODE;

7368
	priv->assoc_request.capability = cpu_to_le16(network->capability);
7369 7370 7371 7372 7373 7374 7375 7376
	if ((network->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
	    && !(priv->config & CFG_PREAMBLE_LONG)) {
		priv->assoc_request.preamble_length = DCT_FLAG_SHORT_PREAMBLE;
	} else {
		priv->assoc_request.preamble_length = DCT_FLAG_LONG_PREAMBLE;

		/* Clear the short preamble if we won't be supporting it */
		priv->assoc_request.capability &=
7377
		    ~cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
7378 7379
	}

7380 7381 7382
	/* Clear capability bits that aren't used in Ad Hoc */
	if (priv->ieee->iw_mode == IW_MODE_ADHOC)
		priv->assoc_request.capability &=
7383
		    ~cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
7384

J
James Ketrenos 已提交
7385
	IPW_DEBUG_ASSOC("%sssocation attempt: '%s', channel %d, "
7386
			"802.11%c [%d], %s[:%s], enc=%s%s%s%c%c\n",
J
James Ketrenos 已提交
7387
			roaming ? "Rea" : "A",
7388
			print_ssid(ssid, priv->essid, priv->essid_len),
7389 7390 7391
			network->channel,
			ipw_modes[priv->assoc_request.ieee_mode],
			rates->num_rates,
7392 7393 7394 7395
			(priv->assoc_request.preamble_length ==
			 DCT_FLAG_LONG_PREAMBLE) ? "long" : "short",
			network->capability &
			WLAN_CAPABILITY_SHORT_PREAMBLE ? "short" : "long",
J
James Ketrenos 已提交
7396
			priv->capability & CAP_PRIVACY_ON ? "on " : "off",
7397 7398
			priv->capability & CAP_PRIVACY_ON ?
			(priv->capability & CAP_SHARED_KEY ? "(shared)" :
J
James Ketrenos 已提交
7399 7400
			 "(open)") : "",
			priv->capability & CAP_PRIVACY_ON ? " key=" : "",
7401
			priv->capability & CAP_PRIVACY_ON ?
7402
			'1' + priv->ieee->sec.active_key : '.',
7403
			priv->capability & CAP_PRIVACY_ON ? '.' : ' ');
J
James Ketrenos 已提交
7404

7405
	priv->assoc_request.beacon_interval = cpu_to_le16(network->beacon_interval);
J
James Ketrenos 已提交
7406
	if ((priv->ieee->iw_mode == IW_MODE_ADHOC) &&
7407
	    (network->time_stamp[0] == 0) && (network->time_stamp[1] == 0)) {
J
James Ketrenos 已提交
7408 7409 7410 7411 7412 7413 7414 7415
		priv->assoc_request.assoc_type = HC_IBSS_START;
		priv->assoc_request.assoc_tsf_msw = 0;
		priv->assoc_request.assoc_tsf_lsw = 0;
	} else {
		if (unlikely(roaming))
			priv->assoc_request.assoc_type = HC_REASSOCIATE;
		else
			priv->assoc_request.assoc_type = HC_ASSOCIATE;
7416 7417
		priv->assoc_request.assoc_tsf_msw = cpu_to_le32(network->time_stamp[1]);
		priv->assoc_request.assoc_tsf_lsw = cpu_to_le32(network->time_stamp[0]);
J
James Ketrenos 已提交
7418 7419
	}

7420
	memcpy(priv->assoc_request.bssid, network->bssid, ETH_ALEN);
J
James Ketrenos 已提交
7421 7422 7423

	if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
		memset(&priv->assoc_request.dest, 0xFF, ETH_ALEN);
7424
		priv->assoc_request.atim_window = cpu_to_le16(network->atim_window);
J
James Ketrenos 已提交
7425
	} else {
7426
		memcpy(priv->assoc_request.dest, network->bssid, ETH_ALEN);
J
James Ketrenos 已提交
7427 7428 7429
		priv->assoc_request.atim_window = 0;
	}

7430
	priv->assoc_request.listen_interval = cpu_to_le16(network->listen_interval);
7431

J
James Ketrenos 已提交
7432 7433 7434 7435 7436 7437 7438 7439 7440
	err = ipw_send_ssid(priv, priv->essid, priv->essid_len);
	if (err) {
		IPW_DEBUG_HC("Attempt to send SSID command failed.\n");
		return err;
	}

	rates->ieee_mode = priv->assoc_request.ieee_mode;
	rates->purpose = IPW_RATE_CONNECT;
	ipw_send_supported_rates(priv, rates);
7441

J
James Ketrenos 已提交
7442 7443 7444 7445
	if (priv->assoc_request.ieee_mode == IPW_G_MODE)
		priv->sys_config.dot11g_auto_detection = 1;
	else
		priv->sys_config.dot11g_auto_detection = 0;
7446 7447 7448 7449 7450 7451

	if (priv->ieee->iw_mode == IW_MODE_ADHOC)
		priv->sys_config.answer_broadcast_ssid_probe = 1;
	else
		priv->sys_config.answer_broadcast_ssid_probe = 0;

7452
	err = ipw_send_system_config(priv);
J
James Ketrenos 已提交
7453 7454 7455 7456
	if (err) {
		IPW_DEBUG_HC("Attempt to send sys config command failed.\n");
		return err;
	}
7457

J
James Ketrenos 已提交
7458
	IPW_DEBUG_ASSOC("Association sensitivity: %d\n", network->stats.rssi);
7459
	err = ipw_set_sensitivity(priv, network->stats.rssi + IPW_RSSI_TO_DBM);
J
James Ketrenos 已提交
7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471
	if (err) {
		IPW_DEBUG_HC("Attempt to send associate command failed.\n");
		return err;
	}

	/*
	 * If preemption is enabled, it is possible for the association
	 * to complete before we return from ipw_send_associate.  Therefore
	 * we have to be sure and update our priviate data first.
	 */
	priv->channel = network->channel;
	memcpy(priv->bssid, network->bssid, ETH_ALEN);
7472
	priv->status |= STATUS_ASSOCIATING;
J
James Ketrenos 已提交
7473 7474 7475 7476
	priv->status &= ~STATUS_SECURITY_UPDATED;

	priv->assoc_network = network;

7477
#ifdef CONFIG_IPW2200_QOS
7478 7479 7480
	ipw_qos_association(priv, network);
#endif

J
James Ketrenos 已提交
7481 7482 7483 7484 7485
	err = ipw_send_associate(priv, &priv->assoc_request);
	if (err) {
		IPW_DEBUG_HC("Attempt to send associate command failed.\n");
		return err;
	}
7486

J
Johannes Berg 已提交
7487
	IPW_DEBUG(IPW_DL_STATE, "associating: '%s' %pM \n",
7488
		  print_ssid(ssid, priv->essid, priv->essid_len),
J
Johannes Berg 已提交
7489
		  priv->bssid);
J
James Ketrenos 已提交
7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502

	return 0;
}

static void ipw_roam(void *data)
{
	struct ipw_priv *priv = data;
	struct ieee80211_network *network = NULL;
	struct ipw_network_match match = {
		.network = priv->assoc_network
	};

	/* The roaming process is as follows:
7503 7504
	 *
	 * 1.  Missed beacon threshold triggers the roaming process by
J
James Ketrenos 已提交
7505 7506 7507 7508 7509 7510 7511 7512 7513 7514
	 *     setting the status ROAM bit and requesting a scan.
	 * 2.  When the scan completes, it schedules the ROAM work
	 * 3.  The ROAM work looks at all of the known networks for one that
	 *     is a better network than the currently associated.  If none
	 *     found, the ROAM process is over (ROAM bit cleared)
	 * 4.  If a better network is found, a disassociation request is
	 *     sent.
	 * 5.  When the disassociation completes, the roam work is again
	 *     scheduled.  The second time through, the driver is no longer
	 *     associated, and the newly selected network is sent an
7515
	 *     association request.
J
James Ketrenos 已提交
7516 7517 7518 7519 7520 7521 7522 7523
	 * 6.  At this point ,the roaming process is complete and the ROAM
	 *     status bit is cleared.
	 */

	/* If we are no longer associated, and the roaming bit is no longer
	 * set, then we are not actively roaming, so just return */
	if (!(priv->status & (STATUS_ASSOCIATED | STATUS_ROAMING)))
		return;
7524

J
James Ketrenos 已提交
7525
	if (priv->status & STATUS_ASSOCIATED) {
7526
		/* First pass through ROAM process -- look for a better
J
James Ketrenos 已提交
7527
		 * network */
7528
		unsigned long flags;
J
James Ketrenos 已提交
7529 7530
		u8 rssi = priv->assoc_network->stats.rssi;
		priv->assoc_network->stats.rssi = -128;
7531
		spin_lock_irqsave(&priv->ieee->lock, flags);
J
James Ketrenos 已提交
7532 7533 7534 7535
		list_for_each_entry(network, &priv->ieee->network_list, list) {
			if (network != priv->assoc_network)
				ipw_best_network(priv, &match, network, 1);
		}
7536
		spin_unlock_irqrestore(&priv->ieee->lock, flags);
J
James Ketrenos 已提交
7537
		priv->assoc_network->stats.rssi = rssi;
7538

J
James Ketrenos 已提交
7539 7540 7541 7542 7543 7544 7545
		if (match.network == priv->assoc_network) {
			IPW_DEBUG_ASSOC("No better APs in this network to "
					"roam to.\n");
			priv->status &= ~STATUS_ROAMING;
			ipw_debug_config(priv);
			return;
		}
7546

J
James Ketrenos 已提交
7547 7548 7549 7550
		ipw_send_disassociate(priv, 1);
		priv->assoc_network = match.network;

		return;
7551
	}
J
James Ketrenos 已提交
7552 7553 7554 7555 7556 7557 7558

	/* Second pass through ROAM process -- request association */
	ipw_compatible_rates(priv, priv->assoc_network, &match.rates);
	ipw_associate_network(priv, priv->assoc_network, &match.rates, 1);
	priv->status &= ~STATUS_ROAMING;
}

D
David Howells 已提交
7559
static void ipw_bg_roam(struct work_struct *work)
7560
{
D
David Howells 已提交
7561 7562
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, roam);
7563
	mutex_lock(&priv->mutex);
D
David Howells 已提交
7564
	ipw_roam(priv);
7565
	mutex_unlock(&priv->mutex);
7566 7567 7568
}

static int ipw_associate(void *data)
J
James Ketrenos 已提交
7569 7570 7571 7572 7573 7574 7575 7576 7577
{
	struct ipw_priv *priv = data;

	struct ieee80211_network *network = NULL;
	struct ipw_network_match match = {
		.network = NULL
	};
	struct ipw_supported_rates *rates;
	struct list_head *element;
7578
	unsigned long flags;
7579
	DECLARE_SSID_BUF(ssid);
J
James Ketrenos 已提交
7580

7581 7582 7583 7584 7585
	if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
		IPW_DEBUG_ASSOC("Not attempting association (monitor mode)\n");
		return 0;
	}

7586
	if (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
7587 7588
		IPW_DEBUG_ASSOC("Not attempting association (already in "
				"progress)\n");
7589 7590 7591
		return 0;
	}

H
Hong Liu 已提交
7592 7593 7594 7595 7596 7597 7598
	if (priv->status & STATUS_DISASSOCIATING) {
		IPW_DEBUG_ASSOC("Not attempting association (in "
				"disassociating)\n ");
		queue_work(priv->workqueue, &priv->associate);
		return 0;
	}

7599
	if (!ipw_is_init(priv) || (priv->status & STATUS_SCANNING)) {
7600 7601
		IPW_DEBUG_ASSOC("Not attempting association (scanning or not "
				"initialized)\n");
7602 7603
		return 0;
	}
J
James Ketrenos 已提交
7604 7605

	if (!(priv->config & CFG_ASSOCIATE) &&
7606
	    !(priv->config & (CFG_STATIC_ESSID | CFG_STATIC_BSSID))) {
J
James Ketrenos 已提交
7607
		IPW_DEBUG_ASSOC("Not attempting association (associate=0)\n");
7608
		return 0;
J
James Ketrenos 已提交
7609 7610
	}

7611 7612
	/* Protect our use of the network_list */
	spin_lock_irqsave(&priv->ieee->lock, flags);
7613
	list_for_each_entry(network, &priv->ieee->network_list, list)
7614
	    ipw_best_network(priv, &match, network, 0);
J
James Ketrenos 已提交
7615 7616 7617 7618 7619 7620 7621 7622

	network = match.network;
	rates = &match.rates;

	if (network == NULL &&
	    priv->ieee->iw_mode == IW_MODE_ADHOC &&
	    priv->config & CFG_ADHOC_CREATE &&
	    priv->config & CFG_STATIC_ESSID &&
7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637
	    priv->config & CFG_STATIC_CHANNEL) {
		/* Use oldest network if the free list is empty */
		if (list_empty(&priv->ieee->network_free_list)) {
			struct ieee80211_network *oldest = NULL;
			struct ieee80211_network *target;

			list_for_each_entry(target, &priv->ieee->network_list, list) {
				if ((oldest == NULL) ||
				    (target->last_scanned < oldest->last_scanned))
					oldest = target;
			}

			/* If there are no more slots, expire the oldest */
			list_del(&oldest->list);
			target = oldest;
J
Johannes Berg 已提交
7638
			IPW_DEBUG_ASSOC("Expired '%s' (%pM) from "
7639
					"network list.\n",
7640 7641
					print_ssid(ssid, target->ssid,
						   target->ssid_len),
J
Johannes Berg 已提交
7642
					target->bssid);
7643 7644 7645 7646
			list_add_tail(&target->list,
				      &priv->ieee->network_free_list);
		}

J
James Ketrenos 已提交
7647
		element = priv->ieee->network_free_list.next;
7648
		network = list_entry(element, struct ieee80211_network, list);
J
James Ketrenos 已提交
7649 7650 7651 7652 7653
		ipw_adhoc_create(priv, network);
		rates = &priv->rates;
		list_del(element);
		list_add_tail(&network->list, &priv->ieee->network_list);
	}
7654
	spin_unlock_irqrestore(&priv->ieee->lock, flags);
7655

J
James Ketrenos 已提交
7656 7657 7658 7659 7660
	/* If we reached the end of the list, then we don't have any valid
	 * matching APs */
	if (!network) {
		ipw_debug_config(priv);

7661 7662 7663 7664 7665 7666
		if (!(priv->status & STATUS_SCANNING)) {
			if (!(priv->config & CFG_SPEED_SCAN))
				queue_delayed_work(priv->workqueue,
						   &priv->request_scan,
						   SCAN_INTERVAL);
			else
D
David Howells 已提交
7667 7668
				queue_delayed_work(priv->workqueue,
						   &priv->request_scan, 0);
7669
		}
7670

7671
		return 0;
J
James Ketrenos 已提交
7672 7673 7674
	}

	ipw_associate_network(priv, network, rates, 0);
7675 7676 7677 7678

	return 1;
}

D
David Howells 已提交
7679
static void ipw_bg_associate(struct work_struct *work)
7680
{
D
David Howells 已提交
7681 7682
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, associate);
7683
	mutex_lock(&priv->mutex);
D
David Howells 已提交
7684
	ipw_associate(priv);
7685
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
7686
}
7687

7688 7689 7690 7691 7692 7693 7694
static void ipw_rebuild_decrypted_skb(struct ipw_priv *priv,
				      struct sk_buff *skb)
{
	struct ieee80211_hdr *hdr;
	u16 fc;

	hdr = (struct ieee80211_hdr *)skb->data;
7695
	fc = le16_to_cpu(hdr->frame_control);
7696 7697 7698 7699
	if (!(fc & IEEE80211_FCTL_PROTECTED))
		return;

	fc &= ~IEEE80211_FCTL_PROTECTED;
7700
	hdr->frame_control = cpu_to_le16(fc);
7701 7702 7703 7704 7705 7706
	switch (priv->ieee->sec.level) {
	case SEC_LEVEL_3:
		/* Remove CCMP HDR */
		memmove(skb->data + IEEE80211_3ADDR_LEN,
			skb->data + IEEE80211_3ADDR_LEN + 8,
			skb->len - IEEE80211_3ADDR_LEN - 8);
7707
		skb_trim(skb, skb->len - 16);	/* CCMP_HDR_LEN + CCMP_MIC_LEN */
7708 7709 7710 7711 7712 7713 7714 7715
		break;
	case SEC_LEVEL_2:
		break;
	case SEC_LEVEL_1:
		/* Remove IV */
		memmove(skb->data + IEEE80211_3ADDR_LEN,
			skb->data + IEEE80211_3ADDR_LEN + 4,
			skb->len - IEEE80211_3ADDR_LEN - 4);
7716
		skb_trim(skb, skb->len - 8);	/* IV + ICV */
7717 7718 7719 7720 7721 7722 7723 7724
		break;
	case SEC_LEVEL_0:
		break;
	default:
		printk(KERN_ERR "Unknow security level %d\n",
		       priv->ieee->sec.level);
		break;
	}
J
James Ketrenos 已提交
7725
}
7726

7727 7728 7729
static void ipw_handle_data_packet(struct ipw_priv *priv,
				   struct ipw_rx_mem_buffer *rxb,
				   struct ieee80211_rx_stats *stats)
J
James Ketrenos 已提交
7730
{
7731
	struct net_device *dev = priv->net_dev;
H
Hong Liu 已提交
7732
	struct ieee80211_hdr_4addr *hdr;
J
James Ketrenos 已提交
7733 7734 7735
	struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)rxb->skb->data;

	/* We received data from the HW, so stop the watchdog */
7736
	dev->trans_start = jiffies;
J
James Ketrenos 已提交
7737

7738
	/* We only process data packets if the
J
James Ketrenos 已提交
7739
	 * interface is open */
7740
	if (unlikely((le16_to_cpu(pkt->u.frame.length) + IPW_RX_FRAME_SIZE) >
J
James Ketrenos 已提交
7741
		     skb_tailroom(rxb->skb))) {
7742
		dev->stats.rx_errors++;
J
James Ketrenos 已提交
7743 7744 7745 7746
		priv->wstats.discard.misc++;
		IPW_DEBUG_DROP("Corruption detected! Oh no!\n");
		return;
	} else if (unlikely(!netif_running(priv->net_dev))) {
7747
		dev->stats.rx_dropped++;
J
James Ketrenos 已提交
7748 7749 7750 7751 7752 7753
		priv->wstats.discard.misc++;
		IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
		return;
	}

	/* Advance skb->data to the start of the actual payload */
7754
	skb_reserve(rxb->skb, offsetof(struct ipw_rx_packet, u.frame.data));
J
James Ketrenos 已提交
7755 7756

	/* Set the size of the skb to the size of the frame */
7757
	skb_put(rxb->skb, le16_to_cpu(pkt->u.frame.length));
J
James Ketrenos 已提交
7758 7759 7760

	IPW_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);

7761
	/* HW decrypt will not clear the WEP bit, MIC, PN, etc. */
H
Hong Liu 已提交
7762 7763
	hdr = (struct ieee80211_hdr_4addr *)rxb->skb->data;
	if (priv->ieee->iw_mode != IW_MODE_MONITOR &&
7764
	    (is_multicast_ether_addr(hdr->addr1) ?
H
Hong Liu 已提交
7765
	     !priv->ieee->host_mc_decrypt : !priv->ieee->host_decrypt))
7766 7767
		ipw_rebuild_decrypted_skb(priv, rxb->skb);

7768
	if (!ieee80211_rx(priv->ieee, rxb->skb, stats))
7769
		dev->stats.rx_errors++;
7770
	else {			/* ieee80211_rx succeeded, so it now owns the SKB */
J
James Ketrenos 已提交
7771
		rxb->skb = NULL;
7772
		__ipw_led_activity_on(priv);
7773
	}
J
James Ketrenos 已提交
7774 7775
}

7776
#ifdef CONFIG_IPW2200_RADIOTAP
7777 7778 7779 7780
static void ipw_handle_data_packet_monitor(struct ipw_priv *priv,
					   struct ipw_rx_mem_buffer *rxb,
					   struct ieee80211_rx_stats *stats)
{
7781
	struct net_device *dev = priv->net_dev;
7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793
	struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)rxb->skb->data;
	struct ipw_rx_frame *frame = &pkt->u.frame;

	/* initial pull of some data */
	u16 received_channel = frame->received_channel;
	u8 antennaAndPhy = frame->antennaAndPhy;
	s8 antsignal = frame->rssi_dbm - IPW_RSSI_TO_DBM;	/* call it signed anyhow */
	u16 pktrate = frame->rate;

	/* Magic struct that slots into the radiotap header -- no reason
	 * to build this manually element by element, we can write it much
	 * more efficiently than we can parse it. ORDER MATTERS HERE */
7794
	struct ipw_rt_hdr *ipw_rt;
7795 7796 7797 7798

	short len = le16_to_cpu(pkt->u.frame.length);

	/* We received data from the HW, so stop the watchdog */
7799
	dev->trans_start = jiffies;
7800 7801 7802 7803 7804

	/* We only process data packets if the
	 * interface is open */
	if (unlikely((le16_to_cpu(pkt->u.frame.length) + IPW_RX_FRAME_SIZE) >
		     skb_tailroom(rxb->skb))) {
7805
		dev->stats.rx_errors++;
7806 7807 7808 7809
		priv->wstats.discard.misc++;
		IPW_DEBUG_DROP("Corruption detected! Oh no!\n");
		return;
	} else if (unlikely(!netif_running(priv->net_dev))) {
7810
		dev->stats.rx_dropped++;
7811 7812 7813 7814 7815 7816 7817 7818 7819
		priv->wstats.discard.misc++;
		IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
		return;
	}

	/* Libpcap 0.9.3+ can handle variable length radiotap, so we'll use
	 * that now */
	if (len > IPW_RX_BUF_SIZE - sizeof(struct ipw_rt_hdr)) {
		/* FIXME: Should alloc bigger skb instead */
7820
		dev->stats.rx_dropped++;
7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833
		priv->wstats.discard.misc++;
		IPW_DEBUG_DROP("Dropping too large packet in monitor\n");
		return;
	}

	/* copy the frame itself */
	memmove(rxb->skb->data + sizeof(struct ipw_rt_hdr),
		rxb->skb->data + IPW_RX_FRAME_SIZE, len);

	ipw_rt = (struct ipw_rt_hdr *)rxb->skb->data;

	ipw_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
	ipw_rt->rt_hdr.it_pad = 0;	/* always good to zero */
7834
	ipw_rt->rt_hdr.it_len = cpu_to_le16(sizeof(struct ipw_rt_hdr));	/* total header+data */
7835 7836

	/* Big bitfield of all the fields we provide in radiotap */
7837 7838
	ipw_rt->rt_hdr.it_present = cpu_to_le32(
	     (1 << IEEE80211_RADIOTAP_TSFT) |
7839
	     (1 << IEEE80211_RADIOTAP_FLAGS) |
7840 7841 7842
	     (1 << IEEE80211_RADIOTAP_RATE) |
	     (1 << IEEE80211_RADIOTAP_CHANNEL) |
	     (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
7843
	     (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
7844 7845 7846 7847
	     (1 << IEEE80211_RADIOTAP_ANTENNA));

	/* Zero the flags, we'll add to them as we go */
	ipw_rt->rt_flags = 0;
7848 7849 7850 7851
	ipw_rt->rt_tsf = (u64)(frame->parent_tsf[3] << 24 |
			       frame->parent_tsf[2] << 16 |
			       frame->parent_tsf[1] << 8  |
			       frame->parent_tsf[0]);
7852 7853 7854

	/* Convert signal to DBM */
	ipw_rt->rt_dbmsignal = antsignal;
7855
	ipw_rt->rt_dbmnoise = frame->noise;
7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866

	/* Convert the channel data and set the flags */
	ipw_rt->rt_channel = cpu_to_le16(ieee80211chan2mhz(received_channel));
	if (received_channel > 14) {	/* 802.11a */
		ipw_rt->rt_chbitmask =
		    cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
	} else if (antennaAndPhy & 32) {	/* 802.11b */
		ipw_rt->rt_chbitmask =
		    cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
	} else {		/* 802.11g */
		ipw_rt->rt_chbitmask =
A
Al Viro 已提交
7867
		    cpu_to_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ);
7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921
	}

	/* set the rate in multiples of 500k/s */
	switch (pktrate) {
	case IPW_TX_RATE_1MB:
		ipw_rt->rt_rate = 2;
		break;
	case IPW_TX_RATE_2MB:
		ipw_rt->rt_rate = 4;
		break;
	case IPW_TX_RATE_5MB:
		ipw_rt->rt_rate = 10;
		break;
	case IPW_TX_RATE_6MB:
		ipw_rt->rt_rate = 12;
		break;
	case IPW_TX_RATE_9MB:
		ipw_rt->rt_rate = 18;
		break;
	case IPW_TX_RATE_11MB:
		ipw_rt->rt_rate = 22;
		break;
	case IPW_TX_RATE_12MB:
		ipw_rt->rt_rate = 24;
		break;
	case IPW_TX_RATE_18MB:
		ipw_rt->rt_rate = 36;
		break;
	case IPW_TX_RATE_24MB:
		ipw_rt->rt_rate = 48;
		break;
	case IPW_TX_RATE_36MB:
		ipw_rt->rt_rate = 72;
		break;
	case IPW_TX_RATE_48MB:
		ipw_rt->rt_rate = 96;
		break;
	case IPW_TX_RATE_54MB:
		ipw_rt->rt_rate = 108;
		break;
	default:
		ipw_rt->rt_rate = 0;
		break;
	}

	/* antenna number */
	ipw_rt->rt_antenna = (antennaAndPhy & 3);	/* Is this right? */

	/* set the preamble flag if we have it */
	if ((antennaAndPhy & 64))
		ipw_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;

	/* Set the size of the skb to the size of the frame */
	skb_put(rxb->skb, len + sizeof(struct ipw_rt_hdr));
J
James Ketrenos 已提交
7922 7923 7924

	IPW_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);

7925
	if (!ieee80211_rx(priv->ieee, rxb->skb, stats))
7926
		dev->stats.rx_errors++;
7927 7928 7929 7930 7931 7932 7933
	else {			/* ieee80211_rx succeeded, so it now owns the SKB */
		rxb->skb = NULL;
		/* no LED during capture */
	}
}
#endif

7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957
#ifdef CONFIG_IPW2200_PROMISCUOUS
#define ieee80211_is_probe_response(fc) \
   ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT && \
    (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP )

#define ieee80211_is_management(fc) \
   ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)

#define ieee80211_is_control(fc) \
   ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)

#define ieee80211_is_data(fc) \
   ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)

#define ieee80211_is_assoc_request(fc) \
   ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ)

#define ieee80211_is_reassoc_request(fc) \
   ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)

static void ipw_handle_promiscuous_rx(struct ipw_priv *priv,
				      struct ipw_rx_mem_buffer *rxb,
				      struct ieee80211_rx_stats *stats)
{
7958
	struct net_device *dev = priv->prom_net_dev;
7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980
	struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)rxb->skb->data;
	struct ipw_rx_frame *frame = &pkt->u.frame;
	struct ipw_rt_hdr *ipw_rt;

	/* First cache any information we need before we overwrite
	 * the information provided in the skb from the hardware */
	struct ieee80211_hdr *hdr;
	u16 channel = frame->received_channel;
	u8 phy_flags = frame->antennaAndPhy;
	s8 signal = frame->rssi_dbm - IPW_RSSI_TO_DBM;
	s8 noise = frame->noise;
	u8 rate = frame->rate;
	short len = le16_to_cpu(pkt->u.frame.length);
	struct sk_buff *skb;
	int hdr_only = 0;
	u16 filter = priv->prom_priv->filter;

	/* If the filter is set to not include Rx frames then return */
	if (filter & IPW_PROM_NO_RX)
		return;

	/* We received data from the HW, so stop the watchdog */
7981
	dev->trans_start = jiffies;
7982 7983

	if (unlikely((len + IPW_RX_FRAME_SIZE) > skb_tailroom(rxb->skb))) {
7984
		dev->stats.rx_errors++;
7985 7986 7987 7988 7989
		IPW_DEBUG_DROP("Corruption detected! Oh no!\n");
		return;
	}

	/* We only process data packets if the interface is open */
7990 7991
	if (unlikely(!netif_running(dev))) {
		dev->stats.rx_dropped++;
7992 7993 7994 7995 7996 7997 7998 7999
		IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
		return;
	}

	/* Libpcap 0.9.3+ can handle variable length radiotap, so we'll use
	 * that now */
	if (len > IPW_RX_BUF_SIZE - sizeof(struct ipw_rt_hdr)) {
		/* FIXME: Should alloc bigger skb instead */
8000
		dev->stats.rx_dropped++;
8001 8002 8003 8004 8005
		IPW_DEBUG_DROP("Dropping too large packet in monitor\n");
		return;
	}

	hdr = (void *)rxb->skb->data + IPW_RX_FRAME_SIZE;
8006
	if (ieee80211_is_management(le16_to_cpu(hdr->frame_control))) {
8007 8008 8009 8010
		if (filter & IPW_PROM_NO_MGMT)
			return;
		if (filter & IPW_PROM_MGMT_HEADER_ONLY)
			hdr_only = 1;
8011
	} else if (ieee80211_is_control(le16_to_cpu(hdr->frame_control))) {
8012 8013 8014 8015
		if (filter & IPW_PROM_NO_CTL)
			return;
		if (filter & IPW_PROM_CTL_HEADER_ONLY)
			hdr_only = 1;
8016
	} else if (ieee80211_is_data(le16_to_cpu(hdr->frame_control))) {
8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033
		if (filter & IPW_PROM_NO_DATA)
			return;
		if (filter & IPW_PROM_DATA_HEADER_ONLY)
			hdr_only = 1;
	}

	/* Copy the SKB since this is for the promiscuous side */
	skb = skb_copy(rxb->skb, GFP_ATOMIC);
	if (skb == NULL) {
		IPW_ERROR("skb_clone failed for promiscuous copy.\n");
		return;
	}

	/* copy the frame data to write after where the radiotap header goes */
	ipw_rt = (void *)skb->data;

	if (hdr_only)
8034
		len = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
8035 8036 8037 8038 8039

	memcpy(ipw_rt->payload, hdr, len);

	ipw_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
	ipw_rt->rt_hdr.it_pad = 0;	/* always good to zero */
8040
	ipw_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*ipw_rt));	/* total header+data */
8041 8042

	/* Set the size of the skb to the size of the frame */
8043
	skb_put(skb, sizeof(*ipw_rt) + len);
8044 8045

	/* Big bitfield of all the fields we provide in radiotap */
8046 8047
	ipw_rt->rt_hdr.it_present = cpu_to_le32(
	     (1 << IEEE80211_RADIOTAP_TSFT) |
8048
	     (1 << IEEE80211_RADIOTAP_FLAGS) |
8049 8050 8051 8052 8053 8054 8055 8056
	     (1 << IEEE80211_RADIOTAP_RATE) |
	     (1 << IEEE80211_RADIOTAP_CHANNEL) |
	     (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
	     (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
	     (1 << IEEE80211_RADIOTAP_ANTENNA));

	/* Zero the flags, we'll add to them as we go */
	ipw_rt->rt_flags = 0;
8057 8058 8059 8060
	ipw_rt->rt_tsf = (u64)(frame->parent_tsf[3] << 24 |
			       frame->parent_tsf[2] << 16 |
			       frame->parent_tsf[1] << 8  |
			       frame->parent_tsf[0]);
8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075

	/* Convert to DBM */
	ipw_rt->rt_dbmsignal = signal;
	ipw_rt->rt_dbmnoise = noise;

	/* Convert the channel data and set the flags */
	ipw_rt->rt_channel = cpu_to_le16(ieee80211chan2mhz(channel));
	if (channel > 14) {	/* 802.11a */
		ipw_rt->rt_chbitmask =
		    cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
	} else if (phy_flags & (1 << 5)) {	/* 802.11b */
		ipw_rt->rt_chbitmask =
		    cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
	} else {		/* 802.11g */
		ipw_rt->rt_chbitmask =
A
Al Viro 已提交
8076
		    cpu_to_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ);
8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131
	}

	/* set the rate in multiples of 500k/s */
	switch (rate) {
	case IPW_TX_RATE_1MB:
		ipw_rt->rt_rate = 2;
		break;
	case IPW_TX_RATE_2MB:
		ipw_rt->rt_rate = 4;
		break;
	case IPW_TX_RATE_5MB:
		ipw_rt->rt_rate = 10;
		break;
	case IPW_TX_RATE_6MB:
		ipw_rt->rt_rate = 12;
		break;
	case IPW_TX_RATE_9MB:
		ipw_rt->rt_rate = 18;
		break;
	case IPW_TX_RATE_11MB:
		ipw_rt->rt_rate = 22;
		break;
	case IPW_TX_RATE_12MB:
		ipw_rt->rt_rate = 24;
		break;
	case IPW_TX_RATE_18MB:
		ipw_rt->rt_rate = 36;
		break;
	case IPW_TX_RATE_24MB:
		ipw_rt->rt_rate = 48;
		break;
	case IPW_TX_RATE_36MB:
		ipw_rt->rt_rate = 72;
		break;
	case IPW_TX_RATE_48MB:
		ipw_rt->rt_rate = 96;
		break;
	case IPW_TX_RATE_54MB:
		ipw_rt->rt_rate = 108;
		break;
	default:
		ipw_rt->rt_rate = 0;
		break;
	}

	/* antenna number */
	ipw_rt->rt_antenna = (phy_flags & 3);

	/* set the preamble flag if we have it */
	if (phy_flags & (1 << 6))
		ipw_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;

	IPW_DEBUG_RX("Rx packet of %d bytes.\n", skb->len);

	if (!ieee80211_rx(priv->prom_priv->ieee, skb, stats)) {
8132
		dev->stats.rx_errors++;
8133 8134 8135 8136 8137
		dev_kfree_skb_any(skb);
	}
}
#endif

8138
static int is_network_packet(struct ipw_priv *priv,
8139 8140 8141 8142 8143
				    struct ieee80211_hdr_4addr *header)
{
	/* Filter incoming packets to determine if they are targetted toward
	 * this network, discarding packets coming from ourselves */
	switch (priv->ieee->iw_mode) {
8144
	case IW_MODE_ADHOC:	/* Header: Dest. | Source    | BSSID */
8145 8146 8147 8148
		/* packets from our adapter are dropped (echo) */
		if (!memcmp(header->addr2, priv->net_dev->dev_addr, ETH_ALEN))
			return 0;

8149
		/* {broad,multi}cast packets to our BSSID go through */
8150
		if (is_multicast_ether_addr(header->addr1))
8151
			return !memcmp(header->addr3, priv->bssid, ETH_ALEN);
8152 8153 8154 8155 8156

		/* packets to our adapter go through */
		return !memcmp(header->addr1, priv->net_dev->dev_addr,
			       ETH_ALEN);

8157
	case IW_MODE_INFRA:	/* Header: Dest. | BSSID | Source */
8158 8159 8160 8161
		/* packets from our adapter are dropped (echo) */
		if (!memcmp(header->addr3, priv->net_dev->dev_addr, ETH_ALEN))
			return 0;

8162
		/* {broad,multi}cast packets to our BSS go through */
8163
		if (is_multicast_ether_addr(header->addr1))
8164 8165 8166 8167 8168
			return !memcmp(header->addr2, priv->bssid, ETH_ALEN);

		/* packets to our adapter go through */
		return !memcmp(header->addr1, priv->net_dev->dev_addr,
			       ETH_ALEN);
8169
	}
8170

8171 8172 8173
	return 1;
}

8174 8175
#define IPW_PACKET_RETRY_TIME HZ

8176
static  int is_duplicate_packet(struct ipw_priv *priv,
8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236
				      struct ieee80211_hdr_4addr *header)
{
	u16 sc = le16_to_cpu(header->seq_ctl);
	u16 seq = WLAN_GET_SEQ_SEQ(sc);
	u16 frag = WLAN_GET_SEQ_FRAG(sc);
	u16 *last_seq, *last_frag;
	unsigned long *last_time;

	switch (priv->ieee->iw_mode) {
	case IW_MODE_ADHOC:
		{
			struct list_head *p;
			struct ipw_ibss_seq *entry = NULL;
			u8 *mac = header->addr2;
			int index = mac[5] % IPW_IBSS_MAC_HASH_SIZE;

			__list_for_each(p, &priv->ibss_mac_hash[index]) {
				entry =
				    list_entry(p, struct ipw_ibss_seq, list);
				if (!memcmp(entry->mac, mac, ETH_ALEN))
					break;
			}
			if (p == &priv->ibss_mac_hash[index]) {
				entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
				if (!entry) {
					IPW_ERROR
					    ("Cannot malloc new mac entry\n");
					return 0;
				}
				memcpy(entry->mac, mac, ETH_ALEN);
				entry->seq_num = seq;
				entry->frag_num = frag;
				entry->packet_time = jiffies;
				list_add(&entry->list,
					 &priv->ibss_mac_hash[index]);
				return 0;
			}
			last_seq = &entry->seq_num;
			last_frag = &entry->frag_num;
			last_time = &entry->packet_time;
			break;
		}
	case IW_MODE_INFRA:
		last_seq = &priv->last_seq_num;
		last_frag = &priv->last_frag_num;
		last_time = &priv->last_packet_time;
		break;
	default:
		return 0;
	}
	if ((*last_seq == seq) &&
	    time_after(*last_time + IPW_PACKET_RETRY_TIME, jiffies)) {
		if (*last_frag == frag)
			goto drop;
		if (*last_frag + 1 != frag)
			/* out-of-order fragment */
			goto drop;
	} else
		*last_seq = seq;

8237
	*last_frag = frag;
8238 8239 8240 8241
	*last_time = jiffies;
	return 0;

      drop:
8242 8243 8244
	/* Comment this line now since we observed the card receives
	 * duplicate packets but the FCTL_RETRY bit is not set in the
	 * IBSS mode with fragmentation enabled.
8245
	 BUG_ON(!(le16_to_cpu(header->frame_control) & IEEE80211_FCTL_RETRY)); */
8246 8247 8248
	return 1;
}

8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285
static void ipw_handle_mgmt_packet(struct ipw_priv *priv,
				   struct ipw_rx_mem_buffer *rxb,
				   struct ieee80211_rx_stats *stats)
{
	struct sk_buff *skb = rxb->skb;
	struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)skb->data;
	struct ieee80211_hdr_4addr *header = (struct ieee80211_hdr_4addr *)
	    (skb->data + IPW_RX_FRAME_SIZE);

	ieee80211_rx_mgt(priv->ieee, header, stats);

	if (priv->ieee->iw_mode == IW_MODE_ADHOC &&
	    ((WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)) ==
	      IEEE80211_STYPE_PROBE_RESP) ||
	     (WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)) ==
	      IEEE80211_STYPE_BEACON))) {
		if (!memcmp(header->addr3, priv->bssid, ETH_ALEN))
			ipw_add_station(priv, header->addr2);
	}

	if (priv->config & CFG_NET_STATS) {
		IPW_DEBUG_HC("sending stat packet\n");

		/* Set the size of the skb to the size of the full
		 * ipw header and 802.11 frame */
		skb_put(skb, le16_to_cpu(pkt->u.frame.length) +
			IPW_RX_FRAME_SIZE);

		/* Advance past the ipw packet header to the 802.11 frame */
		skb_pull(skb, IPW_RX_FRAME_SIZE);

		/* Push the ieee80211_rx_stats before the 802.11 frame */
		memcpy(skb_push(skb, sizeof(*stats)), stats, sizeof(*stats));

		skb->dev = priv->ieee->dev;

		/* Point raw at the ieee80211_stats */
8286
		skb_reset_mac_header(skb);
8287 8288

		skb->pkt_type = PACKET_OTHERHOST;
8289
		skb->protocol = cpu_to_be16(ETH_P_80211_STATS);
8290 8291
		memset(skb->cb, 0, sizeof(rxb->skb->cb));
		netif_rx(skb);
J
James Ketrenos 已提交
8292
		rxb->skb = NULL;
8293
	}
J
James Ketrenos 已提交
8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304
}

/*
 * Main entry function for recieving a packet with 80211 headers.  This
 * should be called when ever the FW has notified us that there is a new
 * skb in the recieve queue.
 */
static void ipw_rx(struct ipw_priv *priv)
{
	struct ipw_rx_mem_buffer *rxb;
	struct ipw_rx_packet *pkt;
8305
	struct ieee80211_hdr_4addr *header;
J
James Ketrenos 已提交
8306 8307
	u32 r, w, i;
	u8 network_packet;
8308
	u8 fill_rx = 0;
J
James Ketrenos 已提交
8309

8310 8311
	r = ipw_read32(priv, IPW_RX_READ_INDEX);
	w = ipw_read32(priv, IPW_RX_WRITE_INDEX);
8312 8313 8314 8315
	i = priv->rxq->read;

	if (ipw_rx_queue_space (priv->rxq) > (RX_QUEUE_SIZE / 2))
		fill_rx = 1;
J
James Ketrenos 已提交
8316 8317 8318 8319 8320 8321 8322 8323 8324 8325

	while (i != r) {
		rxb = priv->rxq->queue[i];
		if (unlikely(rxb == NULL)) {
			printk(KERN_CRIT "Queue not allocated!\n");
			break;
		}
		priv->rxq->queue[i] = NULL;

		pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
8326
					    IPW_RX_BUF_SIZE,
J
James Ketrenos 已提交
8327 8328 8329 8330 8331
					    PCI_DMA_FROMDEVICE);

		pkt = (struct ipw_rx_packet *)rxb->skb->data;
		IPW_DEBUG_RX("Packet: type=%02X seq=%02X bits=%02X\n",
			     pkt->header.message_type,
8332
			     pkt->header.rx_seq_num, pkt->header.control_bits);
J
James Ketrenos 已提交
8333 8334

		switch (pkt->header.message_type) {
8335 8336
		case RX_FRAME_TYPE:	/* 802.11 frame */  {
				struct ieee80211_rx_stats stats = {
8337
					.rssi = pkt->u.frame.rssi_dbm -
8338
					    IPW_RSSI_TO_DBM,
8339
					.signal =
8340 8341
					    le16_to_cpu(pkt->u.frame.rssi_dbm) -
					    IPW_RSSI_TO_DBM + 0x100,
8342 8343
					.noise =
					    le16_to_cpu(pkt->u.frame.noise),
8344 8345 8346 8347 8348 8349 8350 8351 8352
					.rate = pkt->u.frame.rate,
					.mac_time = jiffies,
					.received_channel =
					    pkt->u.frame.received_channel,
					.freq =
					    (pkt->u.frame.
					     control & (1 << 0)) ?
					    IEEE80211_24GHZ_BAND :
					    IEEE80211_52GHZ_BAND,
8353
					.len = le16_to_cpu(pkt->u.frame.length),
8354 8355 8356 8357 8358 8359
				};

				if (stats.rssi != 0)
					stats.mask |= IEEE80211_STATMASK_RSSI;
				if (stats.signal != 0)
					stats.mask |= IEEE80211_STATMASK_SIGNAL;
8360 8361
				if (stats.noise != 0)
					stats.mask |= IEEE80211_STATMASK_NOISE;
8362 8363 8364 8365
				if (stats.rate != 0)
					stats.mask |= IEEE80211_STATMASK_RATE;

				priv->rx_packets++;
J
James Ketrenos 已提交
8366

8367 8368 8369 8370 8371
#ifdef CONFIG_IPW2200_PROMISCUOUS
	if (priv->prom_net_dev && netif_running(priv->prom_net_dev))
		ipw_handle_promiscuous_rx(priv, rxb, &stats);
#endif

8372
#ifdef CONFIG_IPW2200_MONITOR
8373
				if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
8374
#ifdef CONFIG_IPW2200_RADIOTAP
8375 8376 8377 8378

                ipw_handle_data_packet_monitor(priv,
					       rxb,
					       &stats);
8379
#else
8380 8381
		ipw_handle_data_packet(priv, rxb,
				       &stats);
8382
#endif
8383 8384
					break;
				}
J
James Ketrenos 已提交
8385
#endif
8386

8387
				header =
8388 8389 8390
				    (struct ieee80211_hdr_4addr *)(rxb->skb->
								   data +
								   IPW_RX_FRAME_SIZE);
J
James Ketrenos 已提交
8391 8392
				/* TODO: Check Ad-Hoc dest/source and make sure
				 * that we are actually parsing these packets
8393
				 * correctly -- we should probably use the
J
James Ketrenos 已提交
8394 8395
				 * frame control of the packet and disregard
				 * the current iw_mode */
8396

8397 8398
				network_packet =
				    is_network_packet(priv, header);
8399 8400 8401
				if (network_packet && priv->assoc_network) {
					priv->assoc_network->stats.rssi =
					    stats.rssi;
8402 8403 8404
					priv->exp_avg_rssi =
					    exponential_average(priv->exp_avg_rssi,
					    stats.rssi, DEPTH_RSSI);
8405 8406 8407
				}

				IPW_DEBUG_RX("Frame: len=%u\n",
8408
					     le16_to_cpu(pkt->u.frame.length));
8409

8410
				if (le16_to_cpu(pkt->u.frame.length) <
8411 8412
				    ieee80211_get_hdrlen(le16_to_cpu(
						    header->frame_ctl))) {
8413 8414 8415
					IPW_DEBUG_DROP
					    ("Received packet is too small. "
					     "Dropping.\n");
8416
					priv->net_dev->stats.rx_errors++;
8417 8418 8419 8420
					priv->wstats.discard.misc++;
					break;
				}

8421 8422
				switch (WLAN_FC_GET_TYPE
					(le16_to_cpu(header->frame_ctl))) {
8423

8424
				case IEEE80211_FTYPE_MGMT:
8425 8426
					ipw_handle_mgmt_packet(priv, rxb,
							       &stats);
8427 8428 8429 8430 8431 8432
					break;

				case IEEE80211_FTYPE_CTL:
					break;

				case IEEE80211_FTYPE_DATA:
8433 8434 8435 8436
					if (unlikely(!network_packet ||
						     is_duplicate_packet(priv,
									 header)))
					{
8437
						IPW_DEBUG_DROP("Dropping: "
J
Johannes Berg 已提交
8438 8439 8440 8441 8442 8443
							       "%pM, "
							       "%pM, "
							       "%pM\n",
							       header->addr1,
							       header->addr2,
							       header->addr3);
8444 8445 8446 8447 8448 8449
						break;
					}

					ipw_handle_data_packet(priv, rxb,
							       &stats);

8450 8451
					break;
				}
J
James Ketrenos 已提交
8452 8453
				break;
			}
8454

8455 8456 8457
		case RX_HOST_NOTIFICATION_TYPE:{
				IPW_DEBUG_RX
				    ("Notification: subtype=%02X flags=%02X size=%d\n",
J
James Ketrenos 已提交
8458 8459
				     pkt->u.notification.subtype,
				     pkt->u.notification.flags,
8460
				     le16_to_cpu(pkt->u.notification.size));
8461 8462 8463
				ipw_rx_notification(priv, &pkt->u.notification);
				break;
			}
J
James Ketrenos 已提交
8464 8465 8466 8467 8468 8469

		default:
			IPW_DEBUG_RX("Bad Rx packet of type %d\n",
				     pkt->header.message_type);
			break;
		}
8470 8471 8472

		/* For now we just don't re-use anything.  We can tweak this
		 * later to try and re-use notification packets and SKBs that
J
James Ketrenos 已提交
8473 8474 8475 8476 8477
		 * fail to Rx correctly */
		if (rxb->skb != NULL) {
			dev_kfree_skb_any(rxb->skb);
			rxb->skb = NULL;
		}
8478

J
James Ketrenos 已提交
8479
		pci_unmap_single(priv->pci_dev, rxb->dma_addr,
8480
				 IPW_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
J
James Ketrenos 已提交
8481
		list_add_tail(&rxb->list, &priv->rxq->rx_used);
8482

J
James Ketrenos 已提交
8483
		i = (i + 1) % RX_QUEUE_SIZE;
8484 8485 8486 8487 8488 8489 8490

		/* If there are a lot of unsued frames, restock the Rx queue
		 * so the ucode won't assert */
		if (fill_rx) {
			priv->rxq->read = i;
			ipw_rx_queue_replenish(priv);
		}
J
James Ketrenos 已提交
8491 8492 8493
	}

	/* Backtrack one entry */
8494
	priv->rxq->read = i;
J
James Ketrenos 已提交
8495 8496 8497
	ipw_rx_queue_restock(priv);
}

8498 8499 8500 8501 8502 8503 8504
#define DEFAULT_RTS_THRESHOLD     2304U
#define MIN_RTS_THRESHOLD         1U
#define MAX_RTS_THRESHOLD         2304U
#define DEFAULT_BEACON_INTERVAL   100U
#define	DEFAULT_SHORT_RETRY_LIMIT 7U
#define	DEFAULT_LONG_RETRY_LIMIT  4U

8505 8506 8507 8508 8509 8510 8511 8512
/**
 * ipw_sw_reset
 * @option: options to control different reset behaviour
 * 	    0 = reset everything except the 'disable' module_param
 * 	    1 = reset everything and print out driver info (for probe only)
 * 	    2 = reset everything
 */
static int ipw_sw_reset(struct ipw_priv *priv, int option)
J
James Ketrenos 已提交
8513
{
8514 8515
	int band, modulation;
	int old_mode = priv->ieee->iw_mode;
J
James Ketrenos 已提交
8516

8517 8518
	/* Initialize module parameter values here */
	priv->config = 0;
J
James Ketrenos 已提交
8519

8520 8521 8522 8523
	/* We default to disabling the LED code as right now it causes
	 * too many systems to lock up... */
	if (!led)
		priv->config |= CFG_NO_LED;
J
James Ketrenos 已提交
8524

8525 8526 8527 8528
	if (associate)
		priv->config |= CFG_ASSOCIATE;
	else
		IPW_DEBUG_INFO("Auto associate disabled.\n");
8529

8530 8531 8532 8533
	if (auto_create)
		priv->config |= CFG_ADHOC_CREATE;
	else
		IPW_DEBUG_INFO("Auto adhoc creation disabled.\n");
J
James Ketrenos 已提交
8534

8535 8536 8537 8538
	priv->config &= ~CFG_STATIC_ESSID;
	priv->essid_len = 0;
	memset(priv->essid, 0, IW_ESSID_MAX_SIZE);

8539
	if (disable && option) {
8540 8541
		priv->status |= STATUS_RF_KILL_SW;
		IPW_DEBUG_INFO("Radio disabled.\n");
J
James Ketrenos 已提交
8542
	}
8543

8544 8545 8546 8547 8548
	if (channel != 0) {
		priv->config |= CFG_STATIC_CHANNEL;
		priv->channel = channel;
		IPW_DEBUG_INFO("Bind to static channel %d\n", channel);
		/* TODO: Validate that provided channel is in range */
J
James Ketrenos 已提交
8549
	}
8550
#ifdef CONFIG_IPW2200_QOS
8551 8552
	ipw_qos_init(priv, qos_enable, qos_burst_enable,
		     burst_duration_CCK, burst_duration_OFDM);
8553
#endif				/* CONFIG_IPW2200_QOS */
J
James Ketrenos 已提交
8554

8555 8556 8557 8558 8559 8560 8561 8562 8563
	switch (mode) {
	case 1:
		priv->ieee->iw_mode = IW_MODE_ADHOC;
		priv->net_dev->type = ARPHRD_ETHER;

		break;
#ifdef CONFIG_IPW2200_MONITOR
	case 2:
		priv->ieee->iw_mode = IW_MODE_MONITOR;
8564
#ifdef CONFIG_IPW2200_RADIOTAP
8565 8566
		priv->net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
#else
8567
		priv->net_dev->type = ARPHRD_IEEE80211;
8568
#endif
8569 8570 8571 8572 8573 8574 8575
		break;
#endif
	default:
	case 0:
		priv->net_dev->type = ARPHRD_ETHER;
		priv->ieee->iw_mode = IW_MODE_INFRA;
		break;
J
James Ketrenos 已提交
8576 8577
	}

8578 8579 8580 8581
	if (hwcrypto) {
		priv->ieee->host_encrypt = 0;
		priv->ieee->host_encrypt_msdu = 0;
		priv->ieee->host_decrypt = 0;
H
Hong Liu 已提交
8582
		priv->ieee->host_mc_decrypt = 0;
8583 8584
	}
	IPW_DEBUG_INFO("Hardware crypto [%s]\n", hwcrypto ? "on" : "off");
J
James Ketrenos 已提交
8585

8586 8587
	/* IPW2200/2915 is abled to do hardware fragmentation. */
	priv->ieee->host_open_frag = 0;
8588

8589 8590
	if ((priv->pci_dev->device == 0x4223) ||
	    (priv->pci_dev->device == 0x4224)) {
8591
		if (option == 1)
8592 8593 8594 8595 8596 8597 8598 8599 8600
			printk(KERN_INFO DRV_NAME
			       ": Detected Intel PRO/Wireless 2915ABG Network "
			       "Connection\n");
		priv->ieee->abg_true = 1;
		band = IEEE80211_52GHZ_BAND | IEEE80211_24GHZ_BAND;
		modulation = IEEE80211_OFDM_MODULATION |
		    IEEE80211_CCK_MODULATION;
		priv->adapter = IPW_2915ABG;
		priv->ieee->mode = IEEE_A | IEEE_G | IEEE_B;
J
James Ketrenos 已提交
8601
	} else {
8602
		if (option == 1)
8603 8604 8605
			printk(KERN_INFO DRV_NAME
			       ": Detected Intel PRO/Wireless 2200BG Network "
			       "Connection\n");
8606

8607 8608 8609 8610 8611 8612
		priv->ieee->abg_true = 0;
		band = IEEE80211_24GHZ_BAND;
		modulation = IEEE80211_OFDM_MODULATION |
		    IEEE80211_CCK_MODULATION;
		priv->adapter = IPW_2200BG;
		priv->ieee->mode = IEEE_G | IEEE_B;
J
James Ketrenos 已提交
8613 8614
	}

8615 8616
	priv->ieee->freq_band = band;
	priv->ieee->modulation = modulation;
J
James Ketrenos 已提交
8617

8618
	priv->rates_mask = IEEE80211_DEFAULT_RATES_MASK;
8619

8620 8621
	priv->disassociate_threshold = IPW_MB_DISASSOCIATE_THRESHOLD_DEFAULT;
	priv->roaming_threshold = IPW_MB_ROAMING_THRESHOLD_DEFAULT;
J
James Ketrenos 已提交
8622

8623 8624 8625
	priv->rts_threshold = DEFAULT_RTS_THRESHOLD;
	priv->short_retry_limit = DEFAULT_SHORT_RETRY_LIMIT;
	priv->long_retry_limit = DEFAULT_LONG_RETRY_LIMIT;
J
James Ketrenos 已提交
8626

8627 8628 8629 8630
	/* If power management is turned on, default to AC mode */
	priv->power_mode = IPW_POWER_AC;
	priv->tx_power = IPW_TX_POWER_DEFAULT;

8631
	return old_mode == priv->ieee->iw_mode;
J
James Ketrenos 已提交
8632 8633 8634 8635 8636 8637
}

/*
 * This file defines the Wireless Extension handlers.  It does not
 * define any methods of hardware manipulation and relies on the
 * functions defined in ipw_main to provide the HW interaction.
8638 8639
 *
 * The exception to this is the use of the ipw_get_ordinal()
J
James Ketrenos 已提交
8640 8641 8642 8643
 * function used to poll the hardware vs. making unecessary calls.
 *
 */

8644 8645
static int ipw_wx_get_name(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
8646 8647 8648
			   union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
8649
	mutex_lock(&priv->mutex);
8650
	if (priv->status & STATUS_RF_KILL_MASK)
8651
		strcpy(wrqu->name, "radio off");
8652
	else if (!(priv->status & STATUS_ASSOCIATED))
J
James Ketrenos 已提交
8653
		strcpy(wrqu->name, "unassociated");
8654
	else
J
James Ketrenos 已提交
8655 8656 8657
		snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11%c",
			 ipw_modes[priv->assoc_request.ieee_mode]);
	IPW_DEBUG_WX("Name: %s\n", wrqu->name);
8658
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
8659 8660 8661 8662 8663 8664 8665 8666
	return 0;
}

static int ipw_set_channel(struct ipw_priv *priv, u8 channel)
{
	if (channel == 0) {
		IPW_DEBUG_INFO("Setting channel to ANY (0)\n");
		priv->config &= ~CFG_STATIC_CHANNEL;
8667 8668 8669
		IPW_DEBUG_ASSOC("Attempting to associate with new "
				"parameters.\n");
		ipw_associate(priv);
J
James Ketrenos 已提交
8670 8671 8672 8673 8674 8675
		return 0;
	}

	priv->config |= CFG_STATIC_CHANNEL;

	if (priv->channel == channel) {
8676 8677
		IPW_DEBUG_INFO("Request to set channel to current value (%d)\n",
			       channel);
J
James Ketrenos 已提交
8678 8679 8680 8681 8682 8683
		return 0;
	}

	IPW_DEBUG_INFO("Setting channel to %i\n", (int)channel);
	priv->channel = channel;

8684 8685
#ifdef CONFIG_IPW2200_MONITOR
	if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
8686
		int i;
8687
		if (priv->status & STATUS_SCANNING) {
8688
			IPW_DEBUG_SCAN("Scan abort triggered due to "
8689
				       "channel change.\n");
8690
			ipw_abort_scan(priv);
8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702
		}

		for (i = 1000; i && (priv->status & STATUS_SCANNING); i--)
			udelay(10);

		if (priv->status & STATUS_SCANNING)
			IPW_DEBUG_SCAN("Still scanning...\n");
		else
			IPW_DEBUG_SCAN("Took %dms to abort current scan\n",
				       1000 - i);

		return 0;
J
James Ketrenos 已提交
8703
	}
8704 8705
#endif				/* CONFIG_IPW2200_MONITOR */

8706 8707 8708
	/* Network configuration changed -- force [re]association */
	IPW_DEBUG_ASSOC("[re]association triggered due to channel change.\n");
	if (!ipw_disassociate(priv))
J
James Ketrenos 已提交
8709 8710 8711 8712 8713
		ipw_associate(priv);

	return 0;
}

8714 8715 8716
static int ipw_wx_set_freq(struct net_device *dev,
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
8717 8718
{
	struct ipw_priv *priv = ieee80211_priv(dev);
8719
	const struct ieee80211_geo *geo = ieee80211_get_geo(priv->ieee);
J
James Ketrenos 已提交
8720
	struct iw_freq *fwrq = &wrqu->freq;
8721
	int ret = 0, i;
8722 8723
	u8 channel, flags;
	int band;
8724 8725 8726

	if (fwrq->m == 0) {
		IPW_DEBUG_WX("SET Freq/Channel -> any\n");
8727
		mutex_lock(&priv->mutex);
8728
		ret = ipw_set_channel(priv, 0);
8729
		mutex_unlock(&priv->mutex);
8730 8731
		return ret;
	}
J
James Ketrenos 已提交
8732 8733
	/* if setting by freq convert to channel */
	if (fwrq->e == 1) {
8734
		channel = ieee80211_freq_to_channel(priv->ieee, fwrq->m);
8735 8736 8737 8738
		if (channel == 0)
			return -EINVAL;
	} else
		channel = fwrq->m;
8739

8740
	if (!(band = ieee80211_is_valid_channel(priv->ieee, channel)))
8741
		return -EINVAL;
J
James Ketrenos 已提交
8742

8743
	if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
8744
		i = ieee80211_channel_to_index(priv->ieee, channel);
8745 8746
		if (i == -1)
			return -EINVAL;
8747

8748 8749 8750
		flags = (band == IEEE80211_24GHZ_BAND) ?
		    geo->bg[i].flags : geo->a[i].flags;
		if (flags & IEEE80211_CH_PASSIVE_ONLY) {
8751 8752
			IPW_DEBUG_WX("Invalid Ad-Hoc channel for 802.11a\n");
			return -EINVAL;
J
James Ketrenos 已提交
8753 8754
		}
	}
8755

J
James Ketrenos 已提交
8756
	IPW_DEBUG_WX("SET Freq/Channel -> %d \n", fwrq->m);
8757
	mutex_lock(&priv->mutex);
8758
	ret = ipw_set_channel(priv, channel);
8759
	mutex_unlock(&priv->mutex);
8760
	return ret;
J
James Ketrenos 已提交
8761 8762
}

8763 8764
static int ipw_wx_get_freq(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
8765 8766 8767 8768 8769 8770 8771 8772
			   union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);

	wrqu->freq.e = 0;

	/* If we are associated, trying to associate, or have a statically
	 * configured CHANNEL then return that; otherwise return ANY */
8773
	mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
8774
	if (priv->config & CFG_STATIC_CHANNEL ||
8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794
	    priv->status & (STATUS_ASSOCIATING | STATUS_ASSOCIATED)) {
		int i;

		i = ieee80211_channel_to_index(priv->ieee, priv->channel);
		BUG_ON(i == -1);
		wrqu->freq.e = 1;

		switch (ieee80211_is_valid_channel(priv->ieee, priv->channel)) {
		case IEEE80211_52GHZ_BAND:
			wrqu->freq.m = priv->ieee->geo.a[i].freq * 100000;
			break;

		case IEEE80211_24GHZ_BAND:
			wrqu->freq.m = priv->ieee->geo.bg[i].freq * 100000;
			break;

		default:
			BUG();
		}
	} else
J
James Ketrenos 已提交
8795 8796
		wrqu->freq.m = 0;

8797
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
8798 8799 8800 8801
	IPW_DEBUG_WX("GET Freq/Channel -> %d \n", priv->channel);
	return 0;
}

8802 8803
static int ipw_wx_set_mode(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
8804 8805 8806 8807 8808 8809 8810 8811
			   union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	int err = 0;

	IPW_DEBUG_WX("Set MODE: %d\n", wrqu->mode);

	switch (wrqu->mode) {
8812
#ifdef CONFIG_IPW2200_MONITOR
J
James Ketrenos 已提交
8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823
	case IW_MODE_MONITOR:
#endif
	case IW_MODE_ADHOC:
	case IW_MODE_INFRA:
		break;
	case IW_MODE_AUTO:
		wrqu->mode = IW_MODE_INFRA;
		break;
	default:
		return -EINVAL;
	}
8824 8825
	if (wrqu->mode == priv->ieee->iw_mode)
		return 0;
J
James Ketrenos 已提交
8826

8827
	mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
8828

8829 8830
	ipw_sw_reset(priv, 0);

8831
#ifdef CONFIG_IPW2200_MONITOR
8832
	if (priv->ieee->iw_mode == IW_MODE_MONITOR)
J
James Ketrenos 已提交
8833
		priv->net_dev->type = ARPHRD_ETHER;
8834 8835

	if (wrqu->mode == IW_MODE_MONITOR)
8836
#ifdef CONFIG_IPW2200_RADIOTAP
8837 8838
		priv->net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
#else
J
James Ketrenos 已提交
8839
		priv->net_dev->type = ARPHRD_IEEE80211;
8840
#endif
8841
#endif				/* CONFIG_IPW2200_MONITOR */
8842 8843

	/* Free the existing firmware and reset the fw_loaded
8844
	 * flag so ipw_load() will bring in the new firmware */
8845
	free_firmware();
J
James Ketrenos 已提交
8846 8847

	priv->ieee->iw_mode = wrqu->mode;
8848

8849
	queue_work(priv->workqueue, &priv->adapter_restart);
8850
	mutex_unlock(&priv->mutex);
8851
	return err;
J
James Ketrenos 已提交
8852 8853
}

8854
static int ipw_wx_get_mode(struct net_device *dev,
8855 8856
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
8857 8858
{
	struct ipw_priv *priv = ieee80211_priv(dev);
8859
	mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
8860 8861
	wrqu->mode = priv->ieee->iw_mode;
	IPW_DEBUG_WX("Get MODE -> %d\n", wrqu->mode);
8862
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882
	return 0;
}

/* Values are in microsecond */
static const s32 timeout_duration[] = {
	350000,
	250000,
	75000,
	37000,
	25000,
};

static const s32 period_duration[] = {
	400000,
	700000,
	1000000,
	1000000,
	1000000
};

8883 8884
static int ipw_wx_get_range(struct net_device *dev,
			    struct iw_request_info *info,
J
James Ketrenos 已提交
8885 8886 8887 8888
			    union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	struct iw_range *range = (struct iw_range *)extra;
8889
	const struct ieee80211_geo *geo = ieee80211_get_geo(priv->ieee);
8890
	int i = 0, j;
J
James Ketrenos 已提交
8891 8892 8893 8894 8895

	wrqu->data.length = sizeof(*range);
	memset(range, 0, sizeof(*range));

	/* 54Mbs == ~27 Mb/s real (802.11g) */
8896
	range->throughput = 27 * 1000 * 1000;
J
James Ketrenos 已提交
8897 8898 8899 8900

	range->max_qual.qual = 100;
	/* TODO: Find real max RSSI and stick here */
	range->max_qual.level = 0;
8901
	range->max_qual.noise = 0;
8902
	range->max_qual.updated = 7;	/* Updated all three */
J
James Ketrenos 已提交
8903 8904 8905

	range->avg_qual.qual = 70;
	/* TODO: Find real 'good' to 'bad' threshol value for RSSI */
8906
	range->avg_qual.level = 0;	/* FIXME to real average level */
J
James Ketrenos 已提交
8907
	range->avg_qual.noise = 0;
8908
	range->avg_qual.updated = 7;	/* Updated all three */
8909
	mutex_lock(&priv->mutex);
8910
	range->num_bitrates = min(priv->rates.num_rates, (u8) IW_MAX_BITRATES);
J
James Ketrenos 已提交
8911

8912 8913
	for (i = 0; i < range->num_bitrates; i++)
		range->bitrate[i] = (priv->rates.supported_rates[i] & 0x7F) *
8914
		    500000;
8915

J
James Ketrenos 已提交
8916 8917 8918 8919 8920
	range->max_rts = DEFAULT_RTS_THRESHOLD;
	range->min_frag = MIN_FRAG_THRESHOLD;
	range->max_frag = MAX_FRAG_THRESHOLD;

	range->encoding_size[0] = 5;
8921
	range->encoding_size[1] = 13;
J
James Ketrenos 已提交
8922 8923 8924 8925 8926
	range->num_encoding_sizes = 2;
	range->max_encoding_tokens = WEP_KEYS;

	/* Set the Wireless Extension versions */
	range->we_version_compiled = WIRELESS_EXT;
8927
	range->we_version_source = 18;
J
James Ketrenos 已提交
8928

8929 8930
	i = 0;
	if (priv->ieee->mode & (IEEE_B | IEEE_G)) {
8931 8932 8933 8934 8935
		for (j = 0; j < geo->bg_channels && i < IW_MAX_FREQUENCIES; j++) {
			if ((priv->ieee->iw_mode == IW_MODE_ADHOC) &&
			    (geo->bg[j].flags & IEEE80211_CH_PASSIVE_ONLY))
				continue;

8936 8937 8938
			range->freq[i].i = geo->bg[j].channel;
			range->freq[i].m = geo->bg[j].freq * 100000;
			range->freq[i].e = 1;
8939
			i++;
8940 8941
		}
	}
J
James Ketrenos 已提交
8942

8943
	if (priv->ieee->mode & IEEE_A) {
8944 8945 8946 8947 8948
		for (j = 0; j < geo->a_channels && i < IW_MAX_FREQUENCIES; j++) {
			if ((priv->ieee->iw_mode == IW_MODE_ADHOC) &&
			    (geo->a[j].flags & IEEE80211_CH_PASSIVE_ONLY))
				continue;

8949 8950 8951
			range->freq[i].i = geo->a[j].channel;
			range->freq[i].m = geo->a[j].freq * 100000;
			range->freq[i].e = 1;
8952
			i++;
8953
		}
J
James Ketrenos 已提交
8954
	}
8955 8956 8957 8958

	range->num_channels = i;
	range->num_frequency = i;

8959
	mutex_unlock(&priv->mutex);
8960 8961 8962 8963

	/* Event capability (kernel + driver) */
	range->event_capa[0] = (IW_EVENT_CAPA_K_0 |
				IW_EVENT_CAPA_MASK(SIOCGIWTHRSPY) |
8964 8965
				IW_EVENT_CAPA_MASK(SIOCGIWAP) |
				IW_EVENT_CAPA_MASK(SIOCGIWSCAN));
8966
	range->event_capa[1] = IW_EVENT_CAPA_K_1;
J
James Ketrenos 已提交
8967

8968 8969 8970
	range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
		IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;

D
Dan Williams 已提交
8971 8972
	range->scan_capa = IW_SCAN_CAPA_ESSID | IW_SCAN_CAPA_TYPE;

J
James Ketrenos 已提交
8973 8974 8975 8976
	IPW_DEBUG_WX("GET Range\n");
	return 0;
}

8977 8978
static int ipw_wx_set_wap(struct net_device *dev,
			  struct iw_request_info *info,
J
James Ketrenos 已提交
8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989
			  union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);

	static const unsigned char any[] = {
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
	};
	static const unsigned char off[] = {
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};

8990
	if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
J
James Ketrenos 已提交
8991
		return -EINVAL;
8992
	mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
8993 8994 8995 8996 8997
	if (!memcmp(any, wrqu->ap_addr.sa_data, ETH_ALEN) ||
	    !memcmp(off, wrqu->ap_addr.sa_data, ETH_ALEN)) {
		/* we disable mandatory BSSID association */
		IPW_DEBUG_WX("Setting AP BSSID to ANY\n");
		priv->config &= ~CFG_STATIC_BSSID;
8998 8999 9000
		IPW_DEBUG_ASSOC("Attempting to associate with new "
				"parameters.\n");
		ipw_associate(priv);
9001
		mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9002 9003 9004 9005 9006 9007
		return 0;
	}

	priv->config |= CFG_STATIC_BSSID;
	if (!memcmp(priv->bssid, wrqu->ap_addr.sa_data, ETH_ALEN)) {
		IPW_DEBUG_WX("BSSID set to current BSSID.\n");
9008
		mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9009 9010 9011
		return 0;
	}

J
Johannes Berg 已提交
9012 9013
	IPW_DEBUG_WX("Setting mandatory BSSID to %pM\n",
		     wrqu->ap_addr.sa_data);
J
James Ketrenos 已提交
9014 9015 9016

	memcpy(priv->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);

9017 9018 9019
	/* Network configuration changed -- force [re]association */
	IPW_DEBUG_ASSOC("[re]association triggered due to BSSID change.\n");
	if (!ipw_disassociate(priv))
J
James Ketrenos 已提交
9020 9021
		ipw_associate(priv);

9022
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9023 9024 9025
	return 0;
}

9026 9027
static int ipw_wx_get_wap(struct net_device *dev,
			  struct iw_request_info *info,
J
James Ketrenos 已提交
9028 9029 9030
			  union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
9031

J
James Ketrenos 已提交
9032 9033
	/* If we are associated, trying to associate, or have a statically
	 * configured BSSID then return that; otherwise return ANY */
9034
	mutex_lock(&priv->mutex);
9035
	if (priv->config & CFG_STATIC_BSSID ||
J
James Ketrenos 已提交
9036 9037
	    priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
		wrqu->ap_addr.sa_family = ARPHRD_ETHER;
9038
		memcpy(wrqu->ap_addr.sa_data, priv->bssid, ETH_ALEN);
J
James Ketrenos 已提交
9039 9040 9041
	} else
		memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);

J
Johannes Berg 已提交
9042 9043
	IPW_DEBUG_WX("Getting WAP BSSID: %pM\n",
		     wrqu->ap_addr.sa_data);
9044
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9045 9046 9047
	return 0;
}

9048 9049
static int ipw_wx_set_essid(struct net_device *dev,
			    struct iw_request_info *info,
J
James Ketrenos 已提交
9050 9051 9052
			    union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
Z
Zhu Yi 已提交
9053
        int length;
9054
	DECLARE_SSID_BUF(ssid);
Z
Zhu Yi 已提交
9055 9056

        mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
9057

Z
Zhu Yi 已提交
9058 9059 9060 9061 9062 9063 9064 9065 9066
        if (!wrqu->essid.flags)
        {
                IPW_DEBUG_WX("Setting ESSID to ANY\n");
                ipw_disassociate(priv);
                priv->config &= ~CFG_STATIC_ESSID;
                ipw_associate(priv);
                mutex_unlock(&priv->mutex);
                return 0;
        }
J
James Ketrenos 已提交
9067

9068
	length = min((int)wrqu->essid.length, IW_ESSID_MAX_SIZE);
J
James Ketrenos 已提交
9069 9070 9071

	priv->config |= CFG_STATIC_ESSID;

9072 9073
	if (priv->essid_len == length && !memcmp(priv->essid, extra, length)
	    && (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING))) {
J
James Ketrenos 已提交
9074
		IPW_DEBUG_WX("ESSID set to current ESSID.\n");
9075
		mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9076 9077 9078
		return 0;
	}

9079 9080
	IPW_DEBUG_WX("Setting ESSID: '%s' (%d)\n",
		     print_ssid(ssid, extra, length), length);
J
James Ketrenos 已提交
9081 9082

	priv->essid_len = length;
9083
	memcpy(priv->essid, extra, priv->essid_len);
9084

9085 9086 9087
	/* Network configuration changed -- force [re]association */
	IPW_DEBUG_ASSOC("[re]association triggered due to ESSID change.\n");
	if (!ipw_disassociate(priv))
J
James Ketrenos 已提交
9088 9089
		ipw_associate(priv);

9090
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9091 9092 9093
	return 0;
}

9094 9095
static int ipw_wx_get_essid(struct net_device *dev,
			    struct iw_request_info *info,
J
James Ketrenos 已提交
9096 9097 9098
			    union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
9099
	DECLARE_SSID_BUF(ssid);
J
James Ketrenos 已提交
9100 9101 9102

	/* If we are associated, trying to associate, or have a statically
	 * configured ESSID then return that; otherwise return ANY */
9103
	mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
9104
	if (priv->config & CFG_STATIC_ESSID ||
9105 9106
	    priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
		IPW_DEBUG_WX("Getting essid: '%s'\n",
9107
			     print_ssid(ssid, priv->essid, priv->essid_len));
9108
		memcpy(extra, priv->essid, priv->essid_len);
J
James Ketrenos 已提交
9109
		wrqu->essid.length = priv->essid_len;
9110
		wrqu->essid.flags = 1;	/* active */
J
James Ketrenos 已提交
9111 9112 9113
	} else {
		IPW_DEBUG_WX("Getting essid: ANY\n");
		wrqu->essid.length = 0;
9114
		wrqu->essid.flags = 0;	/* active */
J
James Ketrenos 已提交
9115
	}
9116
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9117 9118 9119
	return 0;
}

9120 9121
static int ipw_wx_set_nick(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
9122
			   union iwreq_data *wrqu, char *extra)
9123
{
J
James Ketrenos 已提交
9124 9125 9126 9127 9128
	struct ipw_priv *priv = ieee80211_priv(dev);

	IPW_DEBUG_WX("Setting nick to '%s'\n", extra);
	if (wrqu->data.length > IW_ESSID_MAX_SIZE)
		return -E2BIG;
9129
	mutex_lock(&priv->mutex);
9130
	wrqu->data.length = min((size_t) wrqu->data.length, sizeof(priv->nick));
J
James Ketrenos 已提交
9131
	memset(priv->nick, 0, sizeof(priv->nick));
9132
	memcpy(priv->nick, extra, wrqu->data.length);
J
James Ketrenos 已提交
9133
	IPW_DEBUG_TRACE("<<\n");
9134
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9135 9136 9137 9138
	return 0;

}

9139 9140
static int ipw_wx_get_nick(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
9141
			   union iwreq_data *wrqu, char *extra)
9142
{
J
James Ketrenos 已提交
9143 9144
	struct ipw_priv *priv = ieee80211_priv(dev);
	IPW_DEBUG_WX("Getting nick\n");
9145
	mutex_lock(&priv->mutex);
J
Jean Tourrilhes 已提交
9146
	wrqu->data.length = strlen(priv->nick);
J
James Ketrenos 已提交
9147
	memcpy(extra, priv->nick, wrqu->data.length);
9148
	wrqu->data.flags = 1;	/* active */
9149
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9150 9151 9152
	return 0;
}

9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198
static int ipw_wx_set_sens(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	int err = 0;

	IPW_DEBUG_WX("Setting roaming threshold to %d\n", wrqu->sens.value);
	IPW_DEBUG_WX("Setting disassociate threshold to %d\n", 3*wrqu->sens.value);
	mutex_lock(&priv->mutex);

	if (wrqu->sens.fixed == 0)
	{
		priv->roaming_threshold = IPW_MB_ROAMING_THRESHOLD_DEFAULT;
		priv->disassociate_threshold = IPW_MB_DISASSOCIATE_THRESHOLD_DEFAULT;
		goto out;
	}
	if ((wrqu->sens.value > IPW_MB_ROAMING_THRESHOLD_MAX) ||
	    (wrqu->sens.value < IPW_MB_ROAMING_THRESHOLD_MIN)) {
		err = -EINVAL;
		goto out;
	}

	priv->roaming_threshold = wrqu->sens.value;
	priv->disassociate_threshold = 3*wrqu->sens.value;
      out:
	mutex_unlock(&priv->mutex);
	return err;
}

static int ipw_wx_get_sens(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	mutex_lock(&priv->mutex);
	wrqu->sens.fixed = 1;
	wrqu->sens.value = priv->roaming_threshold;
	mutex_unlock(&priv->mutex);

	IPW_DEBUG_WX("GET roaming threshold -> %s %d \n",
		     wrqu->power.disabled ? "OFF" : "ON", wrqu->power.value);

	return 0;
}

J
James Ketrenos 已提交
9199 9200 9201
static int ipw_wx_set_rate(struct net_device *dev,
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
9202
{
9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287
	/* TODO: We should use semaphores or locks for access to priv */
	struct ipw_priv *priv = ieee80211_priv(dev);
	u32 target_rate = wrqu->bitrate.value;
	u32 fixed, mask;

	/* value = -1, fixed = 0 means auto only, so we should use all rates offered by AP */
	/* value = X, fixed = 1 means only rate X */
	/* value = X, fixed = 0 means all rates lower equal X */

	if (target_rate == -1) {
		fixed = 0;
		mask = IEEE80211_DEFAULT_RATES_MASK;
		/* Now we should reassociate */
		goto apply;
	}

	mask = 0;
	fixed = wrqu->bitrate.fixed;

	if (target_rate == 1000000 || !fixed)
		mask |= IEEE80211_CCK_RATE_1MB_MASK;
	if (target_rate == 1000000)
		goto apply;

	if (target_rate == 2000000 || !fixed)
		mask |= IEEE80211_CCK_RATE_2MB_MASK;
	if (target_rate == 2000000)
		goto apply;

	if (target_rate == 5500000 || !fixed)
		mask |= IEEE80211_CCK_RATE_5MB_MASK;
	if (target_rate == 5500000)
		goto apply;

	if (target_rate == 6000000 || !fixed)
		mask |= IEEE80211_OFDM_RATE_6MB_MASK;
	if (target_rate == 6000000)
		goto apply;

	if (target_rate == 9000000 || !fixed)
		mask |= IEEE80211_OFDM_RATE_9MB_MASK;
	if (target_rate == 9000000)
		goto apply;

	if (target_rate == 11000000 || !fixed)
		mask |= IEEE80211_CCK_RATE_11MB_MASK;
	if (target_rate == 11000000)
		goto apply;

	if (target_rate == 12000000 || !fixed)
		mask |= IEEE80211_OFDM_RATE_12MB_MASK;
	if (target_rate == 12000000)
		goto apply;

	if (target_rate == 18000000 || !fixed)
		mask |= IEEE80211_OFDM_RATE_18MB_MASK;
	if (target_rate == 18000000)
		goto apply;

	if (target_rate == 24000000 || !fixed)
		mask |= IEEE80211_OFDM_RATE_24MB_MASK;
	if (target_rate == 24000000)
		goto apply;

	if (target_rate == 36000000 || !fixed)
		mask |= IEEE80211_OFDM_RATE_36MB_MASK;
	if (target_rate == 36000000)
		goto apply;

	if (target_rate == 48000000 || !fixed)
		mask |= IEEE80211_OFDM_RATE_48MB_MASK;
	if (target_rate == 48000000)
		goto apply;

	if (target_rate == 54000000 || !fixed)
		mask |= IEEE80211_OFDM_RATE_54MB_MASK;
	if (target_rate == 54000000)
		goto apply;

	IPW_DEBUG_WX("invalid rate specified, returning error\n");
	return -EINVAL;

      apply:
	IPW_DEBUG_WX("Setting rate mask to 0x%08X [%s]\n",
		     mask, fixed ? "fixed" : "sub-rates");
9288
	mutex_lock(&priv->mutex);
9289
	if (mask == IEEE80211_DEFAULT_RATES_MASK) {
9290
		priv->config &= ~CFG_FIXED_RATE;
9291 9292
		ipw_set_fixed_rate(priv, priv->ieee->mode);
	} else
9293 9294
		priv->config |= CFG_FIXED_RATE;

9295 9296
	if (priv->rates_mask == mask) {
		IPW_DEBUG_WX("Mask set to current mask.\n");
9297
		mutex_unlock(&priv->mutex);
9298
		return 0;
9299 9300
	}

9301 9302 9303 9304 9305 9306 9307
	priv->rates_mask = mask;

	/* Network configuration changed -- force [re]association */
	IPW_DEBUG_ASSOC("[re]association triggered due to rates change.\n");
	if (!ipw_disassociate(priv))
		ipw_associate(priv);

9308
	mutex_unlock(&priv->mutex);
9309
	return 0;
J
James Ketrenos 已提交
9310 9311
}

9312 9313
static int ipw_wx_get_rate(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
9314
			   union iwreq_data *wrqu, char *extra)
9315
{
9316
	struct ipw_priv *priv = ieee80211_priv(dev);
9317
	mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
9318
	wrqu->bitrate.value = priv->last_rate;
9319
	wrqu->bitrate.fixed = (priv->config & CFG_FIXED_RATE) ? 1 : 0;
9320
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9321 9322 9323 9324
	IPW_DEBUG_WX("GET Rate -> %d \n", wrqu->bitrate.value);
	return 0;
}

9325 9326
static int ipw_wx_set_rts(struct net_device *dev,
			  struct iw_request_info *info,
J
James Ketrenos 已提交
9327
			  union iwreq_data *wrqu, char *extra)
9328
{
J
James Ketrenos 已提交
9329
	struct ipw_priv *priv = ieee80211_priv(dev);
9330
	mutex_lock(&priv->mutex);
9331
	if (wrqu->rts.disabled || !wrqu->rts.fixed)
J
James Ketrenos 已提交
9332 9333 9334
		priv->rts_threshold = DEFAULT_RTS_THRESHOLD;
	else {
		if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
9335
		    wrqu->rts.value > MAX_RTS_THRESHOLD) {
9336
			mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9337
			return -EINVAL;
9338
		}
J
James Ketrenos 已提交
9339 9340 9341 9342
		priv->rts_threshold = wrqu->rts.value;
	}

	ipw_send_rts_threshold(priv, priv->rts_threshold);
9343
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9344 9345 9346 9347
	IPW_DEBUG_WX("SET RTS Threshold -> %d \n", priv->rts_threshold);
	return 0;
}

9348 9349
static int ipw_wx_get_rts(struct net_device *dev,
			  struct iw_request_info *info,
J
James Ketrenos 已提交
9350
			  union iwreq_data *wrqu, char *extra)
9351
{
J
James Ketrenos 已提交
9352
	struct ipw_priv *priv = ieee80211_priv(dev);
9353
	mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
9354 9355
	wrqu->rts.value = priv->rts_threshold;
	wrqu->rts.fixed = 0;	/* no auto select */
9356
	wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
9357
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9358 9359 9360 9361
	IPW_DEBUG_WX("GET RTS Threshold -> %d \n", wrqu->rts.value);
	return 0;
}

9362 9363
static int ipw_wx_set_txpow(struct net_device *dev,
			    struct iw_request_info *info,
J
James Ketrenos 已提交
9364
			    union iwreq_data *wrqu, char *extra)
9365
{
J
James Ketrenos 已提交
9366
	struct ipw_priv *priv = ieee80211_priv(dev);
Z
Zhu Yi 已提交
9367
	int err = 0;
J
James Ketrenos 已提交
9368

9369
	mutex_lock(&priv->mutex);
9370
	if (ipw_radio_kill_sw(priv, wrqu->power.disabled)) {
Z
Zhu Yi 已提交
9371 9372
		err = -EINPROGRESS;
		goto out;
J
James Ketrenos 已提交
9373 9374
	}

9375 9376 9377
	if (!wrqu->power.fixed)
		wrqu->power.value = IPW_TX_POWER_DEFAULT;

9378
	if (wrqu->power.flags != IW_TXPOW_DBM) {
Z
Zhu Yi 已提交
9379 9380
		err = -EINVAL;
		goto out;
9381
	}
J
James Ketrenos 已提交
9382

9383
	if ((wrqu->power.value > IPW_TX_POWER_MAX) ||
9384
	    (wrqu->power.value < IPW_TX_POWER_MIN)) {
Z
Zhu Yi 已提交
9385 9386
		err = -EINVAL;
		goto out;
9387
	}
J
James Ketrenos 已提交
9388 9389

	priv->tx_power = wrqu->power.value;
Z
Zhu Yi 已提交
9390 9391
	err = ipw_set_tx_power(priv);
      out:
9392
	mutex_unlock(&priv->mutex);
Z
Zhu Yi 已提交
9393
	return err;
J
James Ketrenos 已提交
9394 9395
}

9396 9397
static int ipw_wx_get_txpow(struct net_device *dev,
			    struct iw_request_info *info,
J
James Ketrenos 已提交
9398
			    union iwreq_data *wrqu, char *extra)
9399
{
J
James Ketrenos 已提交
9400
	struct ipw_priv *priv = ieee80211_priv(dev);
9401
	mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
9402 9403 9404 9405
	wrqu->power.value = priv->tx_power;
	wrqu->power.fixed = 1;
	wrqu->power.flags = IW_TXPOW_DBM;
	wrqu->power.disabled = (priv->status & STATUS_RF_KILL_MASK) ? 1 : 0;
9406
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9407

9408
	IPW_DEBUG_WX("GET TX Power -> %s %d \n",
9409
		     wrqu->power.disabled ? "OFF" : "ON", wrqu->power.value);
J
James Ketrenos 已提交
9410 9411 9412 9413

	return 0;
}

9414
static int ipw_wx_set_frag(struct net_device *dev,
9415 9416
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
9417 9418
{
	struct ipw_priv *priv = ieee80211_priv(dev);
9419
	mutex_lock(&priv->mutex);
9420
	if (wrqu->frag.disabled || !wrqu->frag.fixed)
J
James Ketrenos 已提交
9421 9422 9423
		priv->ieee->fts = DEFAULT_FTS;
	else {
		if (wrqu->frag.value < MIN_FRAG_THRESHOLD ||
9424
		    wrqu->frag.value > MAX_FRAG_THRESHOLD) {
9425
			mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9426
			return -EINVAL;
9427
		}
9428

J
James Ketrenos 已提交
9429 9430 9431 9432
		priv->ieee->fts = wrqu->frag.value & ~0x1;
	}

	ipw_send_frag_threshold(priv, wrqu->frag.value);
9433
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9434 9435 9436 9437
	IPW_DEBUG_WX("SET Frag Threshold -> %d \n", wrqu->frag.value);
	return 0;
}

9438
static int ipw_wx_get_frag(struct net_device *dev,
9439 9440
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
9441 9442
{
	struct ipw_priv *priv = ieee80211_priv(dev);
9443
	mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
9444 9445
	wrqu->frag.value = priv->ieee->fts;
	wrqu->frag.fixed = 0;	/* no auto select */
9446
	wrqu->frag.disabled = (wrqu->frag.value == DEFAULT_FTS);
9447
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9448 9449 9450 9451 9452
	IPW_DEBUG_WX("GET Frag Threshold -> %d \n", wrqu->frag.value);

	return 0;
}

9453 9454
static int ipw_wx_set_retry(struct net_device *dev,
			    struct iw_request_info *info,
J
James Ketrenos 已提交
9455
			    union iwreq_data *wrqu, char *extra)
9456
{
9457 9458 9459 9460 9461 9462 9463 9464
	struct ipw_priv *priv = ieee80211_priv(dev);

	if (wrqu->retry.flags & IW_RETRY_LIFETIME || wrqu->retry.disabled)
		return -EINVAL;

	if (!(wrqu->retry.flags & IW_RETRY_LIMIT))
		return 0;

9465
	if (wrqu->retry.value < 0 || wrqu->retry.value >= 255)
9466 9467
		return -EINVAL;

9468
	mutex_lock(&priv->mutex);
J
Jean Tourrilhes 已提交
9469
	if (wrqu->retry.flags & IW_RETRY_SHORT)
9470
		priv->short_retry_limit = (u8) wrqu->retry.value;
J
Jean Tourrilhes 已提交
9471
	else if (wrqu->retry.flags & IW_RETRY_LONG)
9472 9473 9474 9475 9476 9477 9478 9479
		priv->long_retry_limit = (u8) wrqu->retry.value;
	else {
		priv->short_retry_limit = (u8) wrqu->retry.value;
		priv->long_retry_limit = (u8) wrqu->retry.value;
	}

	ipw_send_retry_limit(priv, priv->short_retry_limit,
			     priv->long_retry_limit);
9480
	mutex_unlock(&priv->mutex);
9481 9482 9483
	IPW_DEBUG_WX("SET retry limit -> short:%d long:%d\n",
		     priv->short_retry_limit, priv->long_retry_limit);
	return 0;
J
James Ketrenos 已提交
9484 9485
}

9486 9487
static int ipw_wx_get_retry(struct net_device *dev,
			    struct iw_request_info *info,
J
James Ketrenos 已提交
9488
			    union iwreq_data *wrqu, char *extra)
9489
{
9490 9491
	struct ipw_priv *priv = ieee80211_priv(dev);

9492
	mutex_lock(&priv->mutex);
9493 9494 9495
	wrqu->retry.disabled = 0;

	if ((wrqu->retry.flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
9496
		mutex_unlock(&priv->mutex);
9497 9498 9499
		return -EINVAL;
	}

J
Jean Tourrilhes 已提交
9500 9501
	if (wrqu->retry.flags & IW_RETRY_LONG) {
		wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
9502
		wrqu->retry.value = priv->long_retry_limit;
J
Jean Tourrilhes 已提交
9503 9504
	} else if (wrqu->retry.flags & IW_RETRY_SHORT) {
		wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_SHORT;
9505 9506 9507 9508 9509
		wrqu->retry.value = priv->short_retry_limit;
	} else {
		wrqu->retry.flags = IW_RETRY_LIMIT;
		wrqu->retry.value = priv->short_retry_limit;
	}
9510
	mutex_unlock(&priv->mutex);
9511 9512 9513 9514 9515 9516

	IPW_DEBUG_WX("GET retry -> %d \n", wrqu->retry.value);

	return 0;
}

9517 9518
static int ipw_wx_set_scan(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
9519 9520 9521
			   union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
9522
	struct iw_scan_req *req = (struct iw_scan_req *)extra;
D
Dan Williams 已提交
9523
	struct delayed_work *work = NULL;
9524

9525
	mutex_lock(&priv->mutex);
D
Dan Williams 已提交
9526

9527 9528
	priv->user_requested_scan = 1;

9529
	if (wrqu->data.length == sizeof(struct iw_scan_req)) {
9530
		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
D
Dan Williams 已提交
9531 9532 9533 9534 9535 9536 9537
			int len = min((int)req->essid_len,
			              (int)sizeof(priv->direct_scan_ssid));
			memcpy(priv->direct_scan_ssid, req->essid, len);
			priv->direct_scan_ssid_len = len;
			work = &priv->request_direct_scan;
		} else if (req->scan_type == IW_SCAN_TYPE_PASSIVE) {
			work = &priv->request_passive_scan;
9538
		}
D
Dan Williams 已提交
9539 9540 9541
	} else {
		/* Normal active broadcast scan */
		work = &priv->request_scan;
9542
	}
9543

D
Dan Williams 已提交
9544 9545
	mutex_unlock(&priv->mutex);

J
James Ketrenos 已提交
9546
	IPW_DEBUG_WX("Start scan\n");
9547

D
Dan Williams 已提交
9548
	queue_delayed_work(priv->workqueue, work, 0);
9549

J
James Ketrenos 已提交
9550 9551 9552
	return 0;
}

9553 9554
static int ipw_wx_get_scan(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
9555
			   union iwreq_data *wrqu, char *extra)
9556
{
J
James Ketrenos 已提交
9557 9558 9559 9560
	struct ipw_priv *priv = ieee80211_priv(dev);
	return ieee80211_wx_get_scan(priv->ieee, info, wrqu, extra);
}

9561
static int ipw_wx_set_encode(struct net_device *dev,
9562 9563
			     struct iw_request_info *info,
			     union iwreq_data *wrqu, char *key)
J
James Ketrenos 已提交
9564 9565
{
	struct ipw_priv *priv = ieee80211_priv(dev);
9566
	int ret;
9567
	u32 cap = priv->capability;
9568

9569
	mutex_lock(&priv->mutex);
9570 9571
	ret = ieee80211_wx_set_encode(priv->ieee, info, wrqu, key);

9572 9573 9574 9575 9576 9577 9578
	/* In IBSS mode, we need to notify the firmware to update
	 * the beacon info after we changed the capability. */
	if (cap != priv->capability &&
	    priv->ieee->iw_mode == IW_MODE_ADHOC &&
	    priv->status & STATUS_ASSOCIATED)
		ipw_disassociate(priv);

9579
	mutex_unlock(&priv->mutex);
9580
	return ret;
J
James Ketrenos 已提交
9581 9582
}

9583
static int ipw_wx_get_encode(struct net_device *dev,
9584 9585
			     struct iw_request_info *info,
			     union iwreq_data *wrqu, char *key)
J
James Ketrenos 已提交
9586 9587 9588 9589 9590
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	return ieee80211_wx_get_encode(priv->ieee, info, wrqu, key);
}

9591
static int ipw_wx_set_power(struct net_device *dev,
9592 9593
			    struct iw_request_info *info,
			    union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
9594 9595 9596
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	int err;
9597
	mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
9598 9599 9600 9601 9602
	if (wrqu->power.disabled) {
		priv->power_mode = IPW_POWER_LEVEL(priv->power_mode);
		err = ipw_send_power_mode(priv, IPW_POWER_MODE_CAM);
		if (err) {
			IPW_DEBUG_WX("failed setting power mode.\n");
9603
			mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9604 9605 9606
			return err;
		}
		IPW_DEBUG_WX("SET Power Management Mode -> off\n");
9607
		mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9608
		return 0;
9609
	}
J
James Ketrenos 已提交
9610 9611

	switch (wrqu->power.flags & IW_POWER_MODE) {
9612 9613
	case IW_POWER_ON:	/* If not specified */
	case IW_POWER_MODE:	/* If set all mask */
J
Jean Delvare 已提交
9614
	case IW_POWER_ALL_R:	/* If explicitly state all */
J
James Ketrenos 已提交
9615
		break;
9616
	default:		/* Otherwise we don't support it */
J
James Ketrenos 已提交
9617 9618
		IPW_DEBUG_WX("SET PM Mode: %X not supported.\n",
			     wrqu->power.flags);
9619
		mutex_unlock(&priv->mutex);
9620
		return -EOPNOTSUPP;
J
James Ketrenos 已提交
9621
	}
9622

J
James Ketrenos 已提交
9623 9624
	/* If the user hasn't specified a power management mode yet, default
	 * to BATTERY */
9625
	if (IPW_POWER_LEVEL(priv->power_mode) == IPW_POWER_AC)
J
James Ketrenos 已提交
9626
		priv->power_mode = IPW_POWER_ENABLED | IPW_POWER_BATTERY;
9627
	else
J
James Ketrenos 已提交
9628
		priv->power_mode = IPW_POWER_ENABLED | priv->power_mode;
9629

J
James Ketrenos 已提交
9630 9631 9632
	err = ipw_send_power_mode(priv, IPW_POWER_LEVEL(priv->power_mode));
	if (err) {
		IPW_DEBUG_WX("failed setting power mode.\n");
9633
		mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9634 9635 9636
		return err;
	}

9637
	IPW_DEBUG_WX("SET Power Management Mode -> 0x%02X\n", priv->power_mode);
9638
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9639 9640 9641
	return 0;
}

9642
static int ipw_wx_get_power(struct net_device *dev,
9643 9644
			    struct iw_request_info *info,
			    union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
9645 9646
{
	struct ipw_priv *priv = ieee80211_priv(dev);
9647
	mutex_lock(&priv->mutex);
9648
	if (!(priv->power_mode & IPW_POWER_ENABLED))
J
James Ketrenos 已提交
9649
		wrqu->power.disabled = 1;
9650
	else
J
James Ketrenos 已提交
9651 9652
		wrqu->power.disabled = 0;

9653
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9654
	IPW_DEBUG_WX("GET Power Management Mode -> %02X\n", priv->power_mode);
9655

J
James Ketrenos 已提交
9656 9657 9658
	return 0;
}

9659
static int ipw_wx_set_powermode(struct net_device *dev,
9660 9661
				struct iw_request_info *info,
				union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
9662 9663 9664 9665
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	int mode = *(int *)extra;
	int err;
9666

9667
	mutex_lock(&priv->mutex);
9668
	if ((mode < 1) || (mode > IPW_POWER_LIMIT))
J
James Ketrenos 已提交
9669
		mode = IPW_POWER_AC;
9670

9671
	if (IPW_POWER_LEVEL(priv->power_mode) != mode) {
J
James Ketrenos 已提交
9672 9673 9674
		err = ipw_send_power_mode(priv, mode);
		if (err) {
			IPW_DEBUG_WX("failed setting power mode.\n");
9675
			mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9676 9677
			return err;
		}
9678
		priv->power_mode = IPW_POWER_ENABLED | mode;
J
James Ketrenos 已提交
9679
	}
9680
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9681 9682 9683 9684
	return 0;
}

#define MAX_WX_STRING 80
9685
static int ipw_wx_get_powermode(struct net_device *dev,
9686 9687
				struct iw_request_info *info,
				union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	int level = IPW_POWER_LEVEL(priv->power_mode);
	char *p = extra;

	p += snprintf(p, MAX_WX_STRING, "Power save level: %d ", level);

	switch (level) {
	case IPW_POWER_AC:
		p += snprintf(p, MAX_WX_STRING - (p - extra), "(AC)");
		break;
	case IPW_POWER_BATTERY:
		p += snprintf(p, MAX_WX_STRING - (p - extra), "(BATTERY)");
		break;
	default:
		p += snprintf(p, MAX_WX_STRING - (p - extra),
9704
			      "(Timeout %dms, Period %dms)",
J
James Ketrenos 已提交
9705 9706 9707 9708 9709
			      timeout_duration[level - 1] / 1000,
			      period_duration[level - 1] / 1000);
	}

	if (!(priv->power_mode & IPW_POWER_ENABLED))
9710
		p += snprintf(p, MAX_WX_STRING - (p - extra), " OFF");
J
James Ketrenos 已提交
9711 9712 9713 9714 9715 9716 9717

	wrqu->data.length = p - extra + 1;

	return 0;
}

static int ipw_wx_set_wireless_mode(struct net_device *dev,
9718 9719
				    struct iw_request_info *info,
				    union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
9720
{
9721
	struct ipw_priv *priv = ieee80211_priv(dev);
J
James Ketrenos 已提交
9722 9723 9724 9725
	int mode = *(int *)extra;
	u8 band = 0, modulation = 0;

	if (mode == 0 || mode & ~IEEE_MODE_MASK) {
9726
		IPW_WARNING("Attempt to set invalid wireless mode: %d\n", mode);
J
James Ketrenos 已提交
9727 9728
		return -EINVAL;
	}
9729
	mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
9730
	if (priv->adapter == IPW_2915ABG) {
9731
		priv->ieee->abg_true = 1;
J
James Ketrenos 已提交
9732 9733 9734 9735
		if (mode & IEEE_A) {
			band |= IEEE80211_52GHZ_BAND;
			modulation |= IEEE80211_OFDM_MODULATION;
		} else
9736
			priv->ieee->abg_true = 0;
J
James Ketrenos 已提交
9737 9738 9739 9740
	} else {
		if (mode & IEEE_A) {
			IPW_WARNING("Attempt to set 2200BG into "
				    "802.11a mode\n");
9741
			mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9742 9743 9744
			return -EINVAL;
		}

9745
		priv->ieee->abg_true = 0;
J
James Ketrenos 已提交
9746 9747 9748 9749 9750 9751
	}

	if (mode & IEEE_B) {
		band |= IEEE80211_24GHZ_BAND;
		modulation |= IEEE80211_CCK_MODULATION;
	} else
9752
		priv->ieee->abg_true = 0;
9753

J
James Ketrenos 已提交
9754 9755 9756 9757
	if (mode & IEEE_G) {
		band |= IEEE80211_24GHZ_BAND;
		modulation |= IEEE80211_OFDM_MODULATION;
	} else
9758
		priv->ieee->abg_true = 0;
J
James Ketrenos 已提交
9759 9760 9761 9762

	priv->ieee->mode = mode;
	priv->ieee->freq_band = band;
	priv->ieee->modulation = modulation;
9763
	init_supported_rates(priv, &priv->rates);
J
James Ketrenos 已提交
9764

9765 9766 9767
	/* Network configuration changed -- force [re]association */
	IPW_DEBUG_ASSOC("[re]association triggered due to mode change.\n");
	if (!ipw_disassociate(priv)) {
J
James Ketrenos 已提交
9768
		ipw_send_supported_rates(priv, &priv->rates);
9769 9770
		ipw_associate(priv);
	}
J
James Ketrenos 已提交
9771

9772 9773
	/* Update the band LEDs */
	ipw_led_band_on(priv);
J
James Ketrenos 已提交
9774

9775
	IPW_DEBUG_WX("PRIV SET MODE: %c%c%c\n",
J
James Ketrenos 已提交
9776
		     mode & IEEE_A ? 'a' : '.',
9777
		     mode & IEEE_B ? 'b' : '.', mode & IEEE_G ? 'g' : '.');
9778
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9779 9780 9781 9782
	return 0;
}

static int ipw_wx_get_wireless_mode(struct net_device *dev,
9783 9784
				    struct iw_request_info *info,
				    union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
9785
{
9786
	struct ipw_priv *priv = ieee80211_priv(dev);
9787
	mutex_lock(&priv->mutex);
9788 9789
	switch (priv->ieee->mode) {
	case IEEE_A:
J
James Ketrenos 已提交
9790 9791
		strncpy(extra, "802.11a (1)", MAX_WX_STRING);
		break;
9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811
	case IEEE_B:
		strncpy(extra, "802.11b (2)", MAX_WX_STRING);
		break;
	case IEEE_A | IEEE_B:
		strncpy(extra, "802.11ab (3)", MAX_WX_STRING);
		break;
	case IEEE_G:
		strncpy(extra, "802.11g (4)", MAX_WX_STRING);
		break;
	case IEEE_A | IEEE_G:
		strncpy(extra, "802.11ag (5)", MAX_WX_STRING);
		break;
	case IEEE_B | IEEE_G:
		strncpy(extra, "802.11bg (6)", MAX_WX_STRING);
		break;
	case IEEE_A | IEEE_B | IEEE_G:
		strncpy(extra, "802.11abg (7)", MAX_WX_STRING);
		break;
	default:
		strncpy(extra, "unknown", MAX_WX_STRING);
J
James Ketrenos 已提交
9812
		break;
9813 9814
	}

J
James Ketrenos 已提交
9815 9816
	IPW_DEBUG_WX("PRIV GET MODE: %s\n", extra);

9817
	wrqu->data.length = strlen(extra) + 1;
9818
	mutex_unlock(&priv->mutex);
9819 9820 9821 9822 9823 9824 9825 9826 9827 9828

	return 0;
}

static int ipw_wx_set_preamble(struct net_device *dev,
			       struct iw_request_info *info,
			       union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	int mode = *(int *)extra;
9829
	mutex_lock(&priv->mutex);
9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842
	/* Switching from SHORT -> LONG requires a disassociation */
	if (mode == 1) {
		if (!(priv->config & CFG_PREAMBLE_LONG)) {
			priv->config |= CFG_PREAMBLE_LONG;

			/* Network configuration changed -- force [re]association */
			IPW_DEBUG_ASSOC
			    ("[re]association triggered due to preamble change.\n");
			if (!ipw_disassociate(priv))
				ipw_associate(priv);
		}
		goto done;
	}
J
James Ketrenos 已提交
9843

9844 9845 9846 9847
	if (mode == 0) {
		priv->config &= ~CFG_PREAMBLE_LONG;
		goto done;
	}
9848
	mutex_unlock(&priv->mutex);
9849 9850 9851
	return -EINVAL;

      done:
9852
	mutex_unlock(&priv->mutex);
9853 9854 9855 9856 9857 9858 9859 9860
	return 0;
}

static int ipw_wx_get_preamble(struct net_device *dev,
			       struct iw_request_info *info,
			       union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
9861
	mutex_lock(&priv->mutex);
9862 9863 9864 9865
	if (priv->config & CFG_PREAMBLE_LONG)
		snprintf(wrqu->name, IFNAMSIZ, "long (1)");
	else
		snprintf(wrqu->name, IFNAMSIZ, "auto (0)");
9866
	mutex_unlock(&priv->mutex);
9867
	return 0;
J
James Ketrenos 已提交
9868 9869
}

9870 9871
#ifdef CONFIG_IPW2200_MONITOR
static int ipw_wx_set_monitor(struct net_device *dev,
9872
			      struct iw_request_info *info,
J
James Ketrenos 已提交
9873
			      union iwreq_data *wrqu, char *extra)
9874
{
J
James Ketrenos 已提交
9875 9876 9877
	struct ipw_priv *priv = ieee80211_priv(dev);
	int *parms = (int *)extra;
	int enable = (parms[0] > 0);
9878
	mutex_lock(&priv->mutex);
9879
	IPW_DEBUG_WX("SET MONITOR: %d %d\n", enable, parms[1]);
J
James Ketrenos 已提交
9880 9881
	if (enable) {
		if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
9882
#ifdef CONFIG_IPW2200_RADIOTAP
9883 9884
			priv->net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
#else
J
James Ketrenos 已提交
9885
			priv->net_dev->type = ARPHRD_IEEE80211;
9886
#endif
9887
			queue_work(priv->workqueue, &priv->adapter_restart);
J
James Ketrenos 已提交
9888
		}
9889

J
James Ketrenos 已提交
9890 9891
		ipw_set_channel(priv, parms[1]);
	} else {
9892
		if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
9893
			mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9894
			return 0;
9895
		}
J
James Ketrenos 已提交
9896
		priv->net_dev->type = ARPHRD_ETHER;
9897
		queue_work(priv->workqueue, &priv->adapter_restart);
J
James Ketrenos 已提交
9898
	}
9899
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9900 9901 9902
	return 0;
}

9903
#endif				/* CONFIG_IPW2200_MONITOR */
9904

9905 9906
static int ipw_wx_reset(struct net_device *dev,
			struct iw_request_info *info,
J
James Ketrenos 已提交
9907
			union iwreq_data *wrqu, char *extra)
9908
{
J
James Ketrenos 已提交
9909 9910
	struct ipw_priv *priv = ieee80211_priv(dev);
	IPW_DEBUG_WX("RESET\n");
9911 9912 9913 9914 9915 9916 9917
	queue_work(priv->workqueue, &priv->adapter_restart);
	return 0;
}

static int ipw_wx_sw_reset(struct net_device *dev,
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
9918 9919
{
	struct ipw_priv *priv = ieee80211_priv(dev);
9920 9921 9922 9923 9924
	union iwreq_data wrqu_sec = {
		.encoding = {
			     .flags = IW_ENCODE_DISABLED,
			     },
	};
9925
	int ret;
9926

9927
	IPW_DEBUG_WX("SW_RESET\n");
9928

9929
	mutex_lock(&priv->mutex);
9930

9931
	ret = ipw_sw_reset(priv, 2);
9932 9933 9934 9935
	if (!ret) {
		free_firmware();
		ipw_adapter_restart(priv);
	}
9936

9937 9938 9939
	/* The SW reset bit might have been toggled on by the 'disable'
	 * module parameter, so take appropriate action */
	ipw_radio_kill_sw(priv, priv->status & STATUS_RF_KILL_SW);
9940

9941
	mutex_unlock(&priv->mutex);
9942
	ieee80211_wx_set_encode(priv->ieee, info, &wrqu_sec, NULL);
9943
	mutex_lock(&priv->mutex);
9944

9945 9946 9947 9948 9949 9950
	if (!(priv->status & STATUS_RF_KILL_MASK)) {
		/* Configuration likely changed -- force [re]association */
		IPW_DEBUG_ASSOC("[re]association triggered due to sw "
				"reset.\n");
		if (!ipw_disassociate(priv))
			ipw_associate(priv);
J
James Ketrenos 已提交
9951
	}
9952

9953
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
9954 9955 9956 9957 9958 9959

	return 0;
}

/* Rebase the WE IOCTLs to zero for the handler array */
#define IW_IOCTL(x) [(x)-SIOCSIWCOMMIT]
9960
static iw_handler ipw_wx_handlers[] = {
9961 9962 9963 9964 9965
	IW_IOCTL(SIOCGIWNAME) = ipw_wx_get_name,
	IW_IOCTL(SIOCSIWFREQ) = ipw_wx_set_freq,
	IW_IOCTL(SIOCGIWFREQ) = ipw_wx_get_freq,
	IW_IOCTL(SIOCSIWMODE) = ipw_wx_set_mode,
	IW_IOCTL(SIOCGIWMODE) = ipw_wx_get_mode,
9966 9967
	IW_IOCTL(SIOCSIWSENS) = ipw_wx_set_sens,
	IW_IOCTL(SIOCGIWSENS) = ipw_wx_get_sens,
9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990
	IW_IOCTL(SIOCGIWRANGE) = ipw_wx_get_range,
	IW_IOCTL(SIOCSIWAP) = ipw_wx_set_wap,
	IW_IOCTL(SIOCGIWAP) = ipw_wx_get_wap,
	IW_IOCTL(SIOCSIWSCAN) = ipw_wx_set_scan,
	IW_IOCTL(SIOCGIWSCAN) = ipw_wx_get_scan,
	IW_IOCTL(SIOCSIWESSID) = ipw_wx_set_essid,
	IW_IOCTL(SIOCGIWESSID) = ipw_wx_get_essid,
	IW_IOCTL(SIOCSIWNICKN) = ipw_wx_set_nick,
	IW_IOCTL(SIOCGIWNICKN) = ipw_wx_get_nick,
	IW_IOCTL(SIOCSIWRATE) = ipw_wx_set_rate,
	IW_IOCTL(SIOCGIWRATE) = ipw_wx_get_rate,
	IW_IOCTL(SIOCSIWRTS) = ipw_wx_set_rts,
	IW_IOCTL(SIOCGIWRTS) = ipw_wx_get_rts,
	IW_IOCTL(SIOCSIWFRAG) = ipw_wx_set_frag,
	IW_IOCTL(SIOCGIWFRAG) = ipw_wx_get_frag,
	IW_IOCTL(SIOCSIWTXPOW) = ipw_wx_set_txpow,
	IW_IOCTL(SIOCGIWTXPOW) = ipw_wx_get_txpow,
	IW_IOCTL(SIOCSIWRETRY) = ipw_wx_set_retry,
	IW_IOCTL(SIOCGIWRETRY) = ipw_wx_get_retry,
	IW_IOCTL(SIOCSIWENCODE) = ipw_wx_set_encode,
	IW_IOCTL(SIOCGIWENCODE) = ipw_wx_get_encode,
	IW_IOCTL(SIOCSIWPOWER) = ipw_wx_set_power,
	IW_IOCTL(SIOCGIWPOWER) = ipw_wx_get_power,
9991 9992 9993 9994
	IW_IOCTL(SIOCSIWSPY) = iw_handler_set_spy,
	IW_IOCTL(SIOCGIWSPY) = iw_handler_get_spy,
	IW_IOCTL(SIOCSIWTHRSPY) = iw_handler_set_thrspy,
	IW_IOCTL(SIOCGIWTHRSPY) = iw_handler_get_thrspy,
9995 9996 9997 9998 9999 10000 10001
	IW_IOCTL(SIOCSIWGENIE) = ipw_wx_set_genie,
	IW_IOCTL(SIOCGIWGENIE) = ipw_wx_get_genie,
	IW_IOCTL(SIOCSIWMLME) = ipw_wx_set_mlme,
	IW_IOCTL(SIOCSIWAUTH) = ipw_wx_set_auth,
	IW_IOCTL(SIOCGIWAUTH) = ipw_wx_get_auth,
	IW_IOCTL(SIOCSIWENCODEEXT) = ipw_wx_set_encodeext,
	IW_IOCTL(SIOCGIWENCODEEXT) = ipw_wx_get_encodeext,
J
James Ketrenos 已提交
10002 10003
};

10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016
enum {
	IPW_PRIV_SET_POWER = SIOCIWFIRSTPRIV,
	IPW_PRIV_GET_POWER,
	IPW_PRIV_SET_MODE,
	IPW_PRIV_GET_MODE,
	IPW_PRIV_SET_PREAMBLE,
	IPW_PRIV_GET_PREAMBLE,
	IPW_PRIV_RESET,
	IPW_PRIV_SW_RESET,
#ifdef CONFIG_IPW2200_MONITOR
	IPW_PRIV_SET_MONITOR,
#endif
};
J
James Ketrenos 已提交
10017

10018
static struct iw_priv_args ipw_priv_args[] = {
J
James Ketrenos 已提交
10019
	{
10020 10021 10022
	 .cmd = IPW_PRIV_SET_POWER,
	 .set_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
	 .name = "set_power"},
J
James Ketrenos 已提交
10023
	{
10024 10025 10026
	 .cmd = IPW_PRIV_GET_POWER,
	 .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
	 .name = "get_power"},
J
James Ketrenos 已提交
10027
	{
10028 10029 10030
	 .cmd = IPW_PRIV_SET_MODE,
	 .set_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
	 .name = "set_mode"},
J
James Ketrenos 已提交
10031
	{
10032 10033 10034
	 .cmd = IPW_PRIV_GET_MODE,
	 .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
	 .name = "get_mode"},
J
James Ketrenos 已提交
10035
	{
10036 10037 10038 10039 10040 10041 10042
	 .cmd = IPW_PRIV_SET_PREAMBLE,
	 .set_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
	 .name = "set_preamble"},
	{
	 .cmd = IPW_PRIV_GET_PREAMBLE,
	 .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | IFNAMSIZ,
	 .name = "get_preamble"},
J
James Ketrenos 已提交
10043
	{
10044 10045
	 IPW_PRIV_RESET,
	 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 0, 0, "reset"},
10046 10047 10048 10049 10050 10051 10052 10053
	{
	 IPW_PRIV_SW_RESET,
	 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 0, 0, "sw_reset"},
#ifdef CONFIG_IPW2200_MONITOR
	{
	 IPW_PRIV_SET_MONITOR,
	 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "monitor"},
#endif				/* CONFIG_IPW2200_MONITOR */
J
James Ketrenos 已提交
10054 10055 10056 10057 10058 10059 10060
};

static iw_handler ipw_priv_handler[] = {
	ipw_wx_set_powermode,
	ipw_wx_get_powermode,
	ipw_wx_set_wireless_mode,
	ipw_wx_get_wireless_mode,
10061 10062
	ipw_wx_set_preamble,
	ipw_wx_get_preamble,
10063
	ipw_wx_reset,
10064 10065 10066
	ipw_wx_sw_reset,
#ifdef CONFIG_IPW2200_MONITOR
	ipw_wx_set_monitor,
J
James Ketrenos 已提交
10067 10068 10069
#endif
};

10070
static struct iw_handler_def ipw_wx_handler_def = {
10071 10072 10073 10074 10075 10076
	.standard = ipw_wx_handlers,
	.num_standard = ARRAY_SIZE(ipw_wx_handlers),
	.num_private = ARRAY_SIZE(ipw_priv_handler),
	.num_private_args = ARRAY_SIZE(ipw_priv_args),
	.private = ipw_priv_handler,
	.private_args = ipw_priv_args,
10077
	.get_wireless_stats = ipw_get_wireless_stats,
J
James Ketrenos 已提交
10078 10079 10080 10081 10082 10083 10084
};

/*
 * Get wireless statistics.
 * Called by /proc/net/wireless
 * Also called by SIOCGIWSTATS
 */
10085
static struct iw_statistics *ipw_get_wireless_stats(struct net_device *dev)
J
James Ketrenos 已提交
10086 10087 10088
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	struct iw_statistics *wstats;
10089

J
James Ketrenos 已提交
10090 10091
	wstats = &priv->wstats;

10092
	/* if hw is disabled, then ipw_get_ordinal() can't be called.
10093
	 * netdev->get_wireless_stats seems to be called before fw is
J
James Ketrenos 已提交
10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104
	 * initialized.  STATUS_ASSOCIATED will only be set if the hw is up
	 * and associated; if not associcated, the values are all meaningless
	 * anyway, so set them all to NULL and INVALID */
	if (!(priv->status & STATUS_ASSOCIATED)) {
		wstats->miss.beacon = 0;
		wstats->discard.retries = 0;
		wstats->qual.qual = 0;
		wstats->qual.level = 0;
		wstats->qual.noise = 0;
		wstats->qual.updated = 7;
		wstats->qual.updated |= IW_QUAL_NOISE_INVALID |
10105
		    IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID;
J
James Ketrenos 已提交
10106
		return wstats;
10107
	}
J
James Ketrenos 已提交
10108 10109

	wstats->qual.qual = priv->quality;
10110 10111
	wstats->qual.level = priv->exp_avg_rssi;
	wstats->qual.noise = priv->exp_avg_noise;
J
James Ketrenos 已提交
10112
	wstats->qual.updated = IW_QUAL_QUAL_UPDATED | IW_QUAL_LEVEL_UPDATED |
10113
	    IW_QUAL_NOISE_UPDATED | IW_QUAL_DBM;
J
James Ketrenos 已提交
10114 10115 10116 10117

	wstats->miss.beacon = average_value(&priv->average_missed_beacons);
	wstats->discard.retries = priv->last_tx_failures;
	wstats->discard.code = priv->ieee->ieee_stats.rx_discards_undecryptable;
10118

J
James Ketrenos 已提交
10119 10120 10121
/*	if (ipw_get_ordinal(priv, IPW_ORD_STAT_TX_RETRY, &tx_retry, &len))
	goto fail_get_ordinal;
	wstats->discard.retries += tx_retry; */
10122

J
James Ketrenos 已提交
10123 10124 10125 10126 10127
	return wstats;
}

/* net device stuff */

10128
static  void init_sys_config(struct ipw_sys_config *sys_config)
J
James Ketrenos 已提交
10129
{
10130
	memset(sys_config, 0, sizeof(struct ipw_sys_config));
10131
	sys_config->bt_coexistence = 0;
J
James Ketrenos 已提交
10132 10133 10134 10135 10136 10137 10138
	sys_config->answer_broadcast_ssid_probe = 0;
	sys_config->accept_all_data_frames = 0;
	sys_config->accept_non_directed_frames = 1;
	sys_config->exclude_unicast_unencrypted = 0;
	sys_config->disable_unicast_decryption = 1;
	sys_config->exclude_multicast_unencrypted = 0;
	sys_config->disable_multicast_decryption = 1;
10139 10140 10141
	if (antenna < CFG_SYS_ANTENNA_BOTH || antenna > CFG_SYS_ANTENNA_B)
		antenna = CFG_SYS_ANTENNA_BOTH;
	sys_config->antenna_diversity = antenna;
10142
	sys_config->pass_crc_to_host = 0;	/* TODO: See if 1 gives us FCS */
J
James Ketrenos 已提交
10143
	sys_config->dot11g_auto_detection = 0;
10144
	sys_config->enable_cts_to_self = 0;
J
James Ketrenos 已提交
10145
	sys_config->bt_coexist_collision_thr = 0;
10146
	sys_config->pass_noise_stats_to_host = 1;	/* 1 -- fix for 256 */
10147
	sys_config->silence_threshold = 0x1e;
J
James Ketrenos 已提交
10148 10149 10150 10151 10152
}

static int ipw_net_open(struct net_device *dev)
{
	IPW_DEBUG_INFO("dev->open\n");
10153
	netif_start_queue(dev);
J
James Ketrenos 已提交
10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170
	return 0;
}

static int ipw_net_stop(struct net_device *dev)
{
	IPW_DEBUG_INFO("dev->close\n");
	netif_stop_queue(dev);
	return 0;
}

/*
todo:

modify to send one tfd per fragment instead of using chunking.  otherwise
we need to heavily modify the ieee80211_skb_to_txb.
*/

10171
static int ipw_tx_skb(struct ipw_priv *priv, struct ieee80211_txb *txb,
10172
			     int pri)
J
James Ketrenos 已提交
10173
{
10174
	struct ieee80211_hdr_3addrqos *hdr = (struct ieee80211_hdr_3addrqos *)
10175
	    txb->fragments[0]->data;
J
James Ketrenos 已提交
10176 10177
	int i = 0;
	struct tfd_frame *tfd;
10178
#ifdef CONFIG_IPW2200_QOS
10179 10180 10181
	int tx_id = ipw_get_tx_queue_number(priv, pri);
	struct clx2_tx_queue *txq = &priv->txq[tx_id];
#else
J
James Ketrenos 已提交
10182
	struct clx2_tx_queue *txq = &priv->txq[0];
10183
#endif
J
James Ketrenos 已提交
10184 10185 10186
	struct clx2_queue *q = &txq->q;
	u8 id, hdr_len, unicast;
	u16 remaining_bytes;
10187
	int fc;
J
James Ketrenos 已提交
10188

10189 10190 10191
	if (!(priv->status & STATUS_ASSOCIATED))
		goto drop;

10192
	hdr_len = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
J
James Ketrenos 已提交
10193 10194
	switch (priv->ieee->iw_mode) {
	case IW_MODE_ADHOC:
10195
		unicast = !is_multicast_ether_addr(hdr->addr1);
J
James Ketrenos 已提交
10196 10197 10198 10199 10200
		id = ipw_find_station(priv, hdr->addr1);
		if (id == IPW_INVALID_STATION) {
			id = ipw_add_station(priv, hdr->addr1);
			if (id == IPW_INVALID_STATION) {
				IPW_WARNING("Attempt to send data to "
J
Johannes Berg 已提交
10201 10202
					    "invalid cell: %pM\n",
					    hdr->addr1);
J
James Ketrenos 已提交
10203 10204 10205 10206 10207 10208 10209
				goto drop;
			}
		}
		break;

	case IW_MODE_INFRA:
	default:
10210
		unicast = !is_multicast_ether_addr(hdr->addr3);
J
James Ketrenos 已提交
10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223
		id = 0;
		break;
	}

	tfd = &txq->bd[q->first_empty];
	txq->txb[q->first_empty] = txb;
	memset(tfd, 0, sizeof(*tfd));
	tfd->u.data.station_number = id;

	tfd->control_flags.message_type = TX_FRAME_TYPE;
	tfd->control_flags.control_bits = TFD_NEED_IRQ_MASK;

	tfd->u.data.cmd_id = DINO_CMD_TX;
10224
	tfd->u.data.len = cpu_to_le16(txb->payload_size);
J
James Ketrenos 已提交
10225
	remaining_bytes = txb->payload_size;
10226

J
James Ketrenos 已提交
10227
	if (priv->assoc_request.ieee_mode == IPW_B_MODE)
10228
		tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_MODE_CCK;
J
James Ketrenos 已提交
10229
	else
10230
		tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_MODE_OFDM;
J
James Ketrenos 已提交
10231

10232 10233
	if (priv->assoc_request.preamble_length == DCT_FLAG_SHORT_PREAMBLE)
		tfd->u.data.tx_flags |= DCT_FLAG_SHORT_PREAMBLE;
J
James Ketrenos 已提交
10234

10235 10236
	fc = le16_to_cpu(hdr->frame_ctl);
	hdr->frame_ctl = cpu_to_le16(fc & ~IEEE80211_FCTL_MOREFRAGS);
J
James Ketrenos 已提交
10237 10238 10239

	memcpy(&tfd->u.data.tfd.tfd_24.mchdr, hdr, hdr_len);

10240 10241 10242 10243 10244 10245 10246
	if (likely(unicast))
		tfd->u.data.tx_flags |= DCT_FLAG_ACK_REQD;

	if (txb->encrypted && !priv->ieee->host_encrypt) {
		switch (priv->ieee->sec.level) {
		case SEC_LEVEL_3:
			tfd->u.data.tfd.tfd_24.mchdr.frame_ctl |=
10247
			    cpu_to_le16(IEEE80211_FCTL_PROTECTED);
10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261
			/* XXX: ACK flag must be set for CCMP even if it
			 * is a multicast/broadcast packet, because CCMP
			 * group communication encrypted by GTK is
			 * actually done by the AP. */
			if (!unicast)
				tfd->u.data.tx_flags |= DCT_FLAG_ACK_REQD;

			tfd->u.data.tx_flags &= ~DCT_FLAG_NO_WEP;
			tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_SECURITY_CCM;
			tfd->u.data.key_index = 0;
			tfd->u.data.key_index |= DCT_WEP_INDEX_USE_IMMEDIATE;
			break;
		case SEC_LEVEL_2:
			tfd->u.data.tfd.tfd_24.mchdr.frame_ctl |=
10262
			    cpu_to_le16(IEEE80211_FCTL_PROTECTED);
10263 10264 10265 10266 10267 10268
			tfd->u.data.tx_flags &= ~DCT_FLAG_NO_WEP;
			tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_SECURITY_TKIP;
			tfd->u.data.key_index = DCT_WEP_INDEX_USE_IMMEDIATE;
			break;
		case SEC_LEVEL_1:
			tfd->u.data.tfd.tfd_24.mchdr.frame_ctl |=
10269
			    cpu_to_le16(IEEE80211_FCTL_PROTECTED);
10270 10271
			tfd->u.data.key_index = priv->ieee->crypt_info.tx_keyidx;
			if (priv->ieee->sec.key_sizes[priv->ieee->crypt_info.tx_keyidx] <=
10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287
			    40)
				tfd->u.data.key_index |= DCT_WEP_KEY_64Bit;
			else
				tfd->u.data.key_index |= DCT_WEP_KEY_128Bit;
			break;
		case SEC_LEVEL_0:
			break;
		default:
			printk(KERN_ERR "Unknow security level %d\n",
			       priv->ieee->sec.level);
			break;
		}
	} else
		/* No hardware encryption */
		tfd->u.data.tx_flags |= DCT_FLAG_NO_WEP;

10288
#ifdef CONFIG_IPW2200_QOS
10289 10290
	if (fc & IEEE80211_STYPE_QOS_DATA)
		ipw_qos_set_tx_queue_command(priv, pri, &(tfd->u.data));
10291
#endif				/* CONFIG_IPW2200_QOS */
10292

J
James Ketrenos 已提交
10293
	/* payload */
10294 10295 10296 10297 10298 10299 10300 10301
	tfd->u.data.num_chunks = cpu_to_le32(min((u8) (NUM_TFD_CHUNKS - 2),
						 txb->nr_frags));
	IPW_DEBUG_FRAG("%i fragments being sent as %i chunks.\n",
		       txb->nr_frags, le32_to_cpu(tfd->u.data.num_chunks));
	for (i = 0; i < le32_to_cpu(tfd->u.data.num_chunks); i++) {
		IPW_DEBUG_FRAG("Adding fragment %i of %i (%d bytes).\n",
			       i, le32_to_cpu(tfd->u.data.num_chunks),
			       txb->fragments[i]->len - hdr_len);
10302
		IPW_DEBUG_TX("Dumping TX packet frag %i of %i (%d bytes):\n",
J
James Ketrenos 已提交
10303 10304
			     i, tfd->u.data.num_chunks,
			     txb->fragments[i]->len - hdr_len);
10305
		printk_buf(IPW_DL_TX, txb->fragments[i]->data + hdr_len,
J
James Ketrenos 已提交
10306 10307
			   txb->fragments[i]->len - hdr_len);

10308
		tfd->u.data.chunk_ptr[i] =
10309 10310 10311 10312 10313 10314 10315
		    cpu_to_le32(pci_map_single
				(priv->pci_dev,
				 txb->fragments[i]->data + hdr_len,
				 txb->fragments[i]->len - hdr_len,
				 PCI_DMA_TODEVICE));
		tfd->u.data.chunk_len[i] =
		    cpu_to_le16(txb->fragments[i]->len - hdr_len);
J
James Ketrenos 已提交
10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329
	}

	if (i != txb->nr_frags) {
		struct sk_buff *skb;
		u16 remaining_bytes = 0;
		int j;

		for (j = i; j < txb->nr_frags; j++)
			remaining_bytes += txb->fragments[j]->len - hdr_len;

		printk(KERN_INFO "Trying to reallocate for %d bytes\n",
		       remaining_bytes);
		skb = alloc_skb(remaining_bytes, GFP_ATOMIC);
		if (skb != NULL) {
10330
			tfd->u.data.chunk_len[i] = cpu_to_le16(remaining_bytes);
J
James Ketrenos 已提交
10331 10332
			for (j = i; j < txb->nr_frags; j++) {
				int size = txb->fragments[j]->len - hdr_len;
10333

J
James Ketrenos 已提交
10334
				printk(KERN_INFO "Adding frag %d %d...\n",
10335
				       j, size);
J
James Ketrenos 已提交
10336
				memcpy(skb_put(skb, size),
10337
				       txb->fragments[j]->data + hdr_len, size);
J
James Ketrenos 已提交
10338 10339 10340
			}
			dev_kfree_skb_any(txb->fragments[i]);
			txb->fragments[i] = skb;
10341
			tfd->u.data.chunk_ptr[i] =
10342 10343
			    cpu_to_le32(pci_map_single
					(priv->pci_dev, skb->data,
A
Al Viro 已提交
10344
					 remaining_bytes,
10345 10346
					 PCI_DMA_TODEVICE));

M
Marcin Slusarz 已提交
10347
			le32_add_cpu(&tfd->u.data.num_chunks, 1);
10348
		}
J
James Ketrenos 已提交
10349 10350 10351 10352 10353 10354
	}

	/* kick DMA */
	q->first_empty = ipw_queue_inc_wrap(q->first_empty, q->n_bd);
	ipw_write32(priv, q->reg_w, q->first_empty);

10355
	if (ipw_tx_queue_space(q) < q->high_mark)
10356 10357
		netif_stop_queue(priv->net_dev);

10358
	return NETDEV_TX_OK;
J
James Ketrenos 已提交
10359

10360
      drop:
J
James Ketrenos 已提交
10361 10362
	IPW_DEBUG_DROP("Silently dropping Tx packet.\n");
	ieee80211_txb_free(txb);
10363 10364 10365 10366 10367 10368
	return NETDEV_TX_OK;
}

static int ipw_net_is_queue_full(struct net_device *dev, int pri)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
10369
#ifdef CONFIG_IPW2200_QOS
10370 10371 10372 10373
	int tx_id = ipw_get_tx_queue_number(priv, pri);
	struct clx2_tx_queue *txq = &priv->txq[tx_id];
#else
	struct clx2_tx_queue *txq = &priv->txq[0];
10374
#endif				/* CONFIG_IPW2200_QOS */
10375

10376
	if (ipw_tx_queue_space(&txq->q) < txq->q.high_mark)
10377 10378 10379
		return 1;

	return 0;
J
James Ketrenos 已提交
10380 10381
}

10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398
#ifdef CONFIG_IPW2200_PROMISCUOUS
static void ipw_handle_promiscuous_tx(struct ipw_priv *priv,
				      struct ieee80211_txb *txb)
{
	struct ieee80211_rx_stats dummystats;
	struct ieee80211_hdr *hdr;
	u8 n;
	u16 filter = priv->prom_priv->filter;
	int hdr_only = 0;

	if (filter & IPW_PROM_NO_TX)
		return;

	memset(&dummystats, 0, sizeof(dummystats));

	/* Filtering of fragment chains is done agains the first fragment */
	hdr = (void *)txb->fragments[0]->data;
10399
	if (ieee80211_is_management(le16_to_cpu(hdr->frame_control))) {
10400 10401 10402 10403
		if (filter & IPW_PROM_NO_MGMT)
			return;
		if (filter & IPW_PROM_MGMT_HEADER_ONLY)
			hdr_only = 1;
10404
	} else if (ieee80211_is_control(le16_to_cpu(hdr->frame_control))) {
10405 10406 10407 10408
		if (filter & IPW_PROM_NO_CTL)
			return;
		if (filter & IPW_PROM_CTL_HEADER_ONLY)
			hdr_only = 1;
10409
	} else if (ieee80211_is_data(le16_to_cpu(hdr->frame_control))) {
10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423
		if (filter & IPW_PROM_NO_DATA)
			return;
		if (filter & IPW_PROM_DATA_HEADER_ONLY)
			hdr_only = 1;
	}

	for(n=0; n<txb->nr_frags; ++n) {
		struct sk_buff *src = txb->fragments[n];
		struct sk_buff *dst;
		struct ieee80211_radiotap_header *rt_hdr;
		int len;

		if (hdr_only) {
			hdr = (void *)src->data;
10424
			len = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
10425 10426 10427
		} else
			len = src->len;

J
Johannes Berg 已提交
10428 10429 10430
		dst = alloc_skb(len + sizeof(*rt_hdr), GFP_ATOMIC);
		if (!dst)
			continue;
10431 10432 10433 10434 10435 10436

		rt_hdr = (void *)skb_put(dst, sizeof(*rt_hdr));

		rt_hdr->it_version = PKTHDR_RADIOTAP_VERSION;
		rt_hdr->it_pad = 0;
		rt_hdr->it_present = 0; /* after all, it's just an idea */
10437
		rt_hdr->it_present |=  cpu_to_le32(1 << IEEE80211_RADIOTAP_CHANNEL);
10438

A
Al Viro 已提交
10439
		*(__le16*)skb_put(dst, sizeof(u16)) = cpu_to_le16(
10440 10441
			ieee80211chan2mhz(priv->channel));
		if (priv->channel > 14) 	/* 802.11a */
A
Al Viro 已提交
10442
			*(__le16*)skb_put(dst, sizeof(u16)) =
10443 10444 10445
				cpu_to_le16(IEEE80211_CHAN_OFDM |
					     IEEE80211_CHAN_5GHZ);
		else if (priv->ieee->mode == IEEE_B) /* 802.11b */
A
Al Viro 已提交
10446
			*(__le16*)skb_put(dst, sizeof(u16)) =
10447 10448 10449
				cpu_to_le16(IEEE80211_CHAN_CCK |
					     IEEE80211_CHAN_2GHZ);
		else 		/* 802.11g */
A
Al Viro 已提交
10450
			*(__le16*)skb_put(dst, sizeof(u16)) =
10451 10452 10453
				cpu_to_le16(IEEE80211_CHAN_OFDM |
				 IEEE80211_CHAN_2GHZ);

10454
		rt_hdr->it_len = cpu_to_le16(dst->len);
10455

10456
		skb_copy_from_linear_data(src, skb_put(dst, len), len);
10457 10458 10459 10460 10461 10462 10463

		if (!ieee80211_rx(priv->prom_priv->ieee, dst, &dummystats))
			dev_kfree_skb_any(dst);
	}
}
#endif

J
James Ketrenos 已提交
10464
static int ipw_net_hard_start_xmit(struct ieee80211_txb *txb,
10465
				   struct net_device *dev, int pri)
J
James Ketrenos 已提交
10466 10467 10468
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	unsigned long flags;
10469
	int ret;
J
James Ketrenos 已提交
10470 10471 10472 10473

	IPW_DEBUG_TX("dev->xmit(%d bytes)\n", txb->payload_size);
	spin_lock_irqsave(&priv->lock, flags);

10474 10475 10476 10477 10478
#ifdef CONFIG_IPW2200_PROMISCUOUS
	if (rtap_iface && netif_running(priv->prom_net_dev))
		ipw_handle_promiscuous_tx(priv, txb);
#endif

10479 10480 10481
	ret = ipw_tx_skb(priv, txb, pri);
	if (ret == NETDEV_TX_OK)
		__ipw_led_activity_on(priv);
J
James Ketrenos 已提交
10482 10483
	spin_unlock_irqrestore(&priv->lock, flags);

10484
	return ret;
J
James Ketrenos 已提交
10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495
}

static void ipw_net_set_multicast_list(struct net_device *dev)
{

}

static int ipw_net_set_mac_address(struct net_device *dev, void *p)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	struct sockaddr *addr = p;
10496

J
James Ketrenos 已提交
10497 10498
	if (!is_valid_ether_addr(addr->sa_data))
		return -EADDRNOTAVAIL;
10499
	mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
10500 10501
	priv->config |= CFG_CUSTOM_MAC;
	memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
J
Johannes Berg 已提交
10502 10503
	printk(KERN_INFO "%s: Setting MAC to %pM\n",
	       priv->net_dev->name, priv->mac_addr);
10504
	queue_work(priv->workqueue, &priv->adapter_restart);
10505
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
10506 10507 10508
	return 0;
}

10509
static void ipw_ethtool_get_drvinfo(struct net_device *dev,
J
James Ketrenos 已提交
10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524
				    struct ethtool_drvinfo *info)
{
	struct ipw_priv *p = ieee80211_priv(dev);
	char vers[64];
	char date[32];
	u32 len;

	strcpy(info->driver, DRV_NAME);
	strcpy(info->version, DRV_VERSION);

	len = sizeof(vers);
	ipw_get_ordinal(p, IPW_ORD_STAT_FW_VERSION, vers, &len);
	len = sizeof(date);
	ipw_get_ordinal(p, IPW_ORD_STAT_FW_DATE, date, &len);

10525
	snprintf(info->fw_version, sizeof(info->fw_version), "%s (%s)",
J
James Ketrenos 已提交
10526 10527
		 vers, date);
	strcpy(info->bus_info, pci_name(p->pci_dev));
10528
	info->eedump_len = IPW_EEPROM_IMAGE_SIZE;
J
James Ketrenos 已提交
10529 10530 10531 10532 10533 10534 10535 10536 10537 10538
}

static u32 ipw_ethtool_get_link(struct net_device *dev)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	return (priv->status & STATUS_ASSOCIATED) != 0;
}

static int ipw_ethtool_get_eeprom_len(struct net_device *dev)
{
10539
	return IPW_EEPROM_IMAGE_SIZE;
J
James Ketrenos 已提交
10540 10541 10542
}

static int ipw_ethtool_get_eeprom(struct net_device *dev,
10543
				  struct ethtool_eeprom *eeprom, u8 * bytes)
J
James Ketrenos 已提交
10544 10545 10546
{
	struct ipw_priv *p = ieee80211_priv(dev);

10547
	if (eeprom->offset + eeprom->len > IPW_EEPROM_IMAGE_SIZE)
J
James Ketrenos 已提交
10548
		return -EINVAL;
10549
	mutex_lock(&p->mutex);
10550
	memcpy(bytes, &p->eeprom[eeprom->offset], eeprom->len);
10551
	mutex_unlock(&p->mutex);
J
James Ketrenos 已提交
10552 10553 10554 10555
	return 0;
}

static int ipw_ethtool_set_eeprom(struct net_device *dev,
10556
				  struct ethtool_eeprom *eeprom, u8 * bytes)
J
James Ketrenos 已提交
10557 10558 10559 10560
{
	struct ipw_priv *p = ieee80211_priv(dev);
	int i;

10561
	if (eeprom->offset + eeprom->len > IPW_EEPROM_IMAGE_SIZE)
J
James Ketrenos 已提交
10562
		return -EINVAL;
10563
	mutex_lock(&p->mutex);
10564
	memcpy(&p->eeprom[eeprom->offset], bytes, eeprom->len);
10565 10566
	for (i = 0; i < IPW_EEPROM_IMAGE_SIZE; i++)
		ipw_write8(p, i + IPW_EEPROM_DATA, p->eeprom[i]);
10567
	mutex_unlock(&p->mutex);
J
James Ketrenos 已提交
10568 10569 10570
	return 0;
}

10571
static const struct ethtool_ops ipw_ethtool_ops = {
10572 10573 10574 10575 10576
	.get_link = ipw_ethtool_get_link,
	.get_drvinfo = ipw_ethtool_get_drvinfo,
	.get_eeprom_len = ipw_ethtool_get_eeprom_len,
	.get_eeprom = ipw_ethtool_get_eeprom,
	.set_eeprom = ipw_ethtool_set_eeprom,
J
James Ketrenos 已提交
10577 10578
};

10579
static irqreturn_t ipw_isr(int irq, void *data)
J
James Ketrenos 已提交
10580 10581 10582
{
	struct ipw_priv *priv = data;
	u32 inta, inta_mask;
10583

J
James Ketrenos 已提交
10584 10585 10586
	if (!priv)
		return IRQ_NONE;

Z
Zhu Yi 已提交
10587
	spin_lock(&priv->irq_lock);
J
James Ketrenos 已提交
10588 10589

	if (!(priv->status & STATUS_INT_ENABLED)) {
10590
		/* IRQ is disabled */
J
James Ketrenos 已提交
10591 10592 10593
		goto none;
	}

10594 10595
	inta = ipw_read32(priv, IPW_INTA_RW);
	inta_mask = ipw_read32(priv, IPW_INTA_MASK_R);
10596

J
James Ketrenos 已提交
10597 10598 10599 10600 10601 10602
	if (inta == 0xFFFFFFFF) {
		/* Hardware disappeared */
		IPW_WARNING("IRQ INTA == 0xFFFFFFFF\n");
		goto none;
	}

10603
	if (!(inta & (IPW_INTA_MASK_ALL & inta_mask))) {
J
James Ketrenos 已提交
10604 10605 10606 10607 10608
		/* Shared interrupt */
		goto none;
	}

	/* tell the device to stop sending interrupts */
Z
Zhu Yi 已提交
10609
	__ipw_disable_interrupts(priv);
10610

J
James Ketrenos 已提交
10611
	/* ack current interrupts */
10612 10613
	inta &= (IPW_INTA_MASK_ALL & inta_mask);
	ipw_write32(priv, IPW_INTA_RW, inta);
10614

J
James Ketrenos 已提交
10615 10616 10617 10618 10619
	/* Cache INTA value for our tasklet */
	priv->isr_inta = inta;

	tasklet_schedule(&priv->irq_tasklet);

Z
Zhu Yi 已提交
10620
	spin_unlock(&priv->irq_lock);
J
James Ketrenos 已提交
10621 10622

	return IRQ_HANDLED;
10623
      none:
Z
Zhu Yi 已提交
10624
	spin_unlock(&priv->irq_lock);
J
James Ketrenos 已提交
10625 10626 10627 10628 10629 10630 10631
	return IRQ_NONE;
}

static void ipw_rf_kill(void *adapter)
{
	struct ipw_priv *priv = adapter;
	unsigned long flags;
10632

J
James Ketrenos 已提交
10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650
	spin_lock_irqsave(&priv->lock, flags);

	if (rf_kill_active(priv)) {
		IPW_DEBUG_RF_KILL("RF Kill active, rescheduling GPIO check\n");
		if (priv->workqueue)
			queue_delayed_work(priv->workqueue,
					   &priv->rf_kill, 2 * HZ);
		goto exit_unlock;
	}

	/* RF Kill is now disabled, so bring the device back up */

	if (!(priv->status & STATUS_RF_KILL_MASK)) {
		IPW_DEBUG_RF_KILL("HW RF Kill no longer active, restarting "
				  "device\n");

		/* we can not do an adapter restart while inside an irq lock */
		queue_work(priv->workqueue, &priv->adapter_restart);
10651
	} else
J
James Ketrenos 已提交
10652 10653 10654
		IPW_DEBUG_RF_KILL("HW RF Kill deactivated.  SW RF Kill still "
				  "enabled\n");

10655
      exit_unlock:
J
James Ketrenos 已提交
10656 10657 10658
	spin_unlock_irqrestore(&priv->lock, flags);
}

D
David Howells 已提交
10659
static void ipw_bg_rf_kill(struct work_struct *work)
10660
{
D
David Howells 已提交
10661 10662
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, rf_kill.work);
10663
	mutex_lock(&priv->mutex);
D
David Howells 已提交
10664
	ipw_rf_kill(priv);
10665
	mutex_unlock(&priv->mutex);
10666 10667
}

10668
static void ipw_link_up(struct ipw_priv *priv)
10669
{
10670 10671 10672 10673
	priv->last_seq_num = -1;
	priv->last_frag_num = -1;
	priv->last_packet_time = 0;

10674 10675
	netif_carrier_on(priv->net_dev);

10676
	cancel_delayed_work(&priv->request_scan);
D
Dan Williams 已提交
10677 10678
	cancel_delayed_work(&priv->request_direct_scan);
	cancel_delayed_work(&priv->request_passive_scan);
10679
	cancel_delayed_work(&priv->scan_event);
10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690
	ipw_reset_stats(priv);
	/* Ensure the rate is updated immediately */
	priv->last_rate = ipw_get_current_rate(priv);
	ipw_gather_stats(priv);
	ipw_led_link_up(priv);
	notify_wx_assoc_event(priv);

	if (priv->config & CFG_BACKGROUND_SCAN)
		queue_delayed_work(priv->workqueue, &priv->request_scan, HZ);
}

D
David Howells 已提交
10691
static void ipw_bg_link_up(struct work_struct *work)
10692
{
D
David Howells 已提交
10693 10694
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, link_up);
10695
	mutex_lock(&priv->mutex);
D
David Howells 已提交
10696
	ipw_link_up(priv);
10697
	mutex_unlock(&priv->mutex);
10698 10699
}

10700
static void ipw_link_down(struct ipw_priv *priv)
10701 10702 10703 10704 10705 10706 10707
{
	ipw_led_link_down(priv);
	netif_carrier_off(priv->net_dev);
	notify_wx_assoc_event(priv);

	/* Cancel any queued work ... */
	cancel_delayed_work(&priv->request_scan);
D
Dan Williams 已提交
10708 10709
	cancel_delayed_work(&priv->request_direct_scan);
	cancel_delayed_work(&priv->request_passive_scan);
10710 10711 10712 10713 10714
	cancel_delayed_work(&priv->adhoc_check);
	cancel_delayed_work(&priv->gather_stats);

	ipw_reset_stats(priv);

10715 10716
	if (!(priv->status & STATUS_EXIT_PENDING)) {
		/* Queue up another scan... */
D
David Howells 已提交
10717
		queue_delayed_work(priv->workqueue, &priv->request_scan, 0);
10718 10719
	} else
		cancel_delayed_work(&priv->scan_event);
10720 10721
}

D
David Howells 已提交
10722
static void ipw_bg_link_down(struct work_struct *work)
10723
{
D
David Howells 已提交
10724 10725
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, link_down);
10726
	mutex_lock(&priv->mutex);
D
David Howells 已提交
10727
	ipw_link_down(priv);
10728
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
10729 10730
}

10731
static int __devinit ipw_setup_deferred_work(struct ipw_priv *priv)
J
James Ketrenos 已提交
10732 10733 10734 10735 10736
{
	int ret = 0;

	priv->workqueue = create_workqueue(DRV_NAME);
	init_waitqueue_head(&priv->wait_command_queue);
10737
	init_waitqueue_head(&priv->wait_state);
J
James Ketrenos 已提交
10738

D
David Howells 已提交
10739 10740 10741 10742 10743 10744 10745 10746 10747 10748
	INIT_DELAYED_WORK(&priv->adhoc_check, ipw_bg_adhoc_check);
	INIT_WORK(&priv->associate, ipw_bg_associate);
	INIT_WORK(&priv->disassociate, ipw_bg_disassociate);
	INIT_WORK(&priv->system_config, ipw_system_config);
	INIT_WORK(&priv->rx_replenish, ipw_bg_rx_queue_replenish);
	INIT_WORK(&priv->adapter_restart, ipw_bg_adapter_restart);
	INIT_DELAYED_WORK(&priv->rf_kill, ipw_bg_rf_kill);
	INIT_WORK(&priv->up, ipw_bg_up);
	INIT_WORK(&priv->down, ipw_bg_down);
	INIT_DELAYED_WORK(&priv->request_scan, ipw_request_scan);
D
Dan Williams 已提交
10749 10750
	INIT_DELAYED_WORK(&priv->request_direct_scan, ipw_request_direct_scan);
	INIT_DELAYED_WORK(&priv->request_passive_scan, ipw_request_passive_scan);
10751
	INIT_DELAYED_WORK(&priv->scan_event, ipw_scan_event);
D
David Howells 已提交
10752 10753 10754 10755 10756 10757 10758 10759 10760 10761
	INIT_DELAYED_WORK(&priv->gather_stats, ipw_bg_gather_stats);
	INIT_WORK(&priv->abort_scan, ipw_bg_abort_scan);
	INIT_WORK(&priv->roam, ipw_bg_roam);
	INIT_DELAYED_WORK(&priv->scan_check, ipw_bg_scan_check);
	INIT_WORK(&priv->link_up, ipw_bg_link_up);
	INIT_WORK(&priv->link_down, ipw_bg_link_down);
	INIT_DELAYED_WORK(&priv->led_link_on, ipw_bg_led_link_on);
	INIT_DELAYED_WORK(&priv->led_link_off, ipw_bg_led_link_off);
	INIT_DELAYED_WORK(&priv->led_act_off, ipw_bg_led_activity_off);
	INIT_WORK(&priv->merge_networks, ipw_merge_adhoc_network);
J
James Ketrenos 已提交
10762

10763
#ifdef CONFIG_IPW2200_QOS
D
David Howells 已提交
10764
	INIT_WORK(&priv->qos_activate, ipw_bg_qos_activate);
10765
#endif				/* CONFIG_IPW2200_QOS */
J
James Ketrenos 已提交
10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777

	tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
		     ipw_irq_tasklet, (unsigned long)priv);

	return ret;
}

static void shim__set_security(struct net_device *dev,
			       struct ieee80211_security *sec)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	int i;
10778
	for (i = 0; i < 4; i++) {
J
James Ketrenos 已提交
10779
		if (sec->flags & (1 << i)) {
10780
			priv->ieee->sec.encode_alg[i] = sec->encode_alg[i];
10781
			priv->ieee->sec.key_sizes[i] = sec->key_sizes[i];
J
James Ketrenos 已提交
10782
			if (sec->key_sizes[i] == 0)
10783 10784 10785
				priv->ieee->sec.flags &= ~(1 << i);
			else {
				memcpy(priv->ieee->sec.keys[i], sec->keys[i],
J
James Ketrenos 已提交
10786
				       sec->key_sizes[i]);
10787 10788
				priv->ieee->sec.flags |= (1 << i);
			}
J
James Ketrenos 已提交
10789
			priv->status |= STATUS_SECURITY_UPDATED;
10790 10791
		} else if (sec->level != SEC_LEVEL_1)
			priv->ieee->sec.flags &= ~(1 << i);
J
James Ketrenos 已提交
10792 10793
	}

10794
	if (sec->flags & SEC_ACTIVE_KEY) {
J
James Ketrenos 已提交
10795
		if (sec->active_key <= 3) {
10796 10797
			priv->ieee->sec.active_key = sec->active_key;
			priv->ieee->sec.flags |= SEC_ACTIVE_KEY;
10798
		} else
10799
			priv->ieee->sec.flags &= ~SEC_ACTIVE_KEY;
J
James Ketrenos 已提交
10800
		priv->status |= STATUS_SECURITY_UPDATED;
10801 10802
	} else
		priv->ieee->sec.flags &= ~SEC_ACTIVE_KEY;
J
James Ketrenos 已提交
10803 10804

	if ((sec->flags & SEC_AUTH_MODE) &&
10805 10806 10807
	    (priv->ieee->sec.auth_mode != sec->auth_mode)) {
		priv->ieee->sec.auth_mode = sec->auth_mode;
		priv->ieee->sec.flags |= SEC_AUTH_MODE;
J
James Ketrenos 已提交
10808 10809 10810 10811 10812 10813
		if (sec->auth_mode == WLAN_AUTH_SHARED_KEY)
			priv->capability |= CAP_SHARED_KEY;
		else
			priv->capability &= ~CAP_SHARED_KEY;
		priv->status |= STATUS_SECURITY_UPDATED;
	}
10814

10815 10816 10817
	if (sec->flags & SEC_ENABLED && priv->ieee->sec.enabled != sec->enabled) {
		priv->ieee->sec.flags |= SEC_ENABLED;
		priv->ieee->sec.enabled = sec->enabled;
J
James Ketrenos 已提交
10818
		priv->status |= STATUS_SECURITY_UPDATED;
10819
		if (sec->enabled)
J
James Ketrenos 已提交
10820 10821 10822 10823
			priv->capability |= CAP_PRIVACY_ON;
		else
			priv->capability &= ~CAP_PRIVACY_ON;
	}
10824

10825 10826
	if (sec->flags & SEC_ENCRYPT)
		priv->ieee->sec.encrypt = sec->encrypt;
10827

10828 10829 10830
	if (sec->flags & SEC_LEVEL && priv->ieee->sec.level != sec->level) {
		priv->ieee->sec.level = sec->level;
		priv->ieee->sec.flags |= SEC_LEVEL;
J
James Ketrenos 已提交
10831 10832 10833
		priv->status |= STATUS_SECURITY_UPDATED;
	}

10834 10835 10836
	if (!priv->ieee->host_encrypt && (sec->flags & SEC_ENCRYPT))
		ipw_set_hwcrypto_keys(priv);

10837 10838
	/* To match current functionality of ipw2100 (which works well w/
	 * various supplicants, we don't force a disassociate if the
J
James Ketrenos 已提交
10839 10840 10841
	 * privacy capability changes ... */
#if 0
	if ((priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) &&
10842
	    (((priv->assoc_request.capability &
10843
	       cpu_to_le16(WLAN_CAPABILITY_PRIVACY)) && !sec->enabled) ||
10844
	     (!(priv->assoc_request.capability &
10845
		cpu_to_le16(WLAN_CAPABILITY_PRIVACY)) && sec->enabled))) {
J
James Ketrenos 已提交
10846 10847 10848 10849 10850 10851 10852
		IPW_DEBUG_ASSOC("Disassociating due to capability "
				"change.\n");
		ipw_disassociate(priv);
	}
#endif
}

10853
static int init_supported_rates(struct ipw_priv *priv,
J
James Ketrenos 已提交
10854 10855 10856 10857 10858
				struct ipw_supported_rates *rates)
{
	/* TODO: Mask out rates based on priv->rates_mask */

	memset(rates, 0, sizeof(*rates));
10859
	/* configure supported rates */
J
James Ketrenos 已提交
10860 10861 10862 10863 10864 10865 10866 10867
	switch (priv->ieee->freq_band) {
	case IEEE80211_52GHZ_BAND:
		rates->ieee_mode = IPW_A_MODE;
		rates->purpose = IPW_RATE_CAPABILITIES;
		ipw_add_ofdm_scan_rates(rates, IEEE80211_CCK_MODULATION,
					IEEE80211_OFDM_DEFAULT_RATES_MASK);
		break;

10868
	default:		/* Mixed or 2.4Ghz */
J
James Ketrenos 已提交
10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882
		rates->ieee_mode = IPW_G_MODE;
		rates->purpose = IPW_RATE_CAPABILITIES;
		ipw_add_cck_scan_rates(rates, IEEE80211_CCK_MODULATION,
				       IEEE80211_CCK_DEFAULT_RATES_MASK);
		if (priv->ieee->modulation & IEEE80211_OFDM_MODULATION) {
			ipw_add_ofdm_scan_rates(rates, IEEE80211_CCK_MODULATION,
						IEEE80211_OFDM_DEFAULT_RATES_MASK);
		}
		break;
	}

	return 0;
}

10883
static int ipw_config(struct ipw_priv *priv)
J
James Ketrenos 已提交
10884 10885 10886 10887
{
	/* This is only called from ipw_up, which resets/reloads the firmware
	   so, we don't need to first disable the card before we configure
	   it */
Z
Zhu Yi 已提交
10888
	if (ipw_set_tx_power(priv))
J
James Ketrenos 已提交
10889 10890 10891 10892 10893 10894 10895 10896
		goto error;

	/* initialize adapter address */
	if (ipw_send_adapter_address(priv, priv->net_dev->dev_addr))
		goto error;

	/* set basic system config settings */
	init_sys_config(&priv->sys_config);
10897 10898 10899 10900

	/* Support Bluetooth if we have BT h/w on board, and user wants to.
	 * Does not support BT priority yet (don't abort or defer our Tx) */
	if (bt_coexist) {
10901
		unsigned char bt_caps = priv->eeprom[EEPROM_SKU_CAPABILITY];
10902 10903 10904

		if (bt_caps & EEPROM_SKU_CAP_BT_CHANNEL_SIG)
			priv->sys_config.bt_coexistence
10905
			    |= CFG_BT_COEXISTENCE_SIGNAL_CHNL;
10906 10907
		if (bt_caps & EEPROM_SKU_CAP_BT_OOB)
			priv->sys_config.bt_coexistence
10908
			    |= CFG_BT_COEXISTENCE_OOB;
10909 10910
	}

10911 10912 10913 10914 10915 10916 10917 10918 10919
#ifdef CONFIG_IPW2200_PROMISCUOUS
	if (priv->prom_net_dev && netif_running(priv->prom_net_dev)) {
		priv->sys_config.accept_all_data_frames = 1;
		priv->sys_config.accept_non_directed_frames = 1;
		priv->sys_config.accept_all_mgmt_bcpr = 1;
		priv->sys_config.accept_all_mgmt_frames = 1;
	}
#endif

10920 10921 10922 10923 10924
	if (priv->ieee->iw_mode == IW_MODE_ADHOC)
		priv->sys_config.answer_broadcast_ssid_probe = 1;
	else
		priv->sys_config.answer_broadcast_ssid_probe = 0;

10925
	if (ipw_send_system_config(priv))
J
James Ketrenos 已提交
10926 10927
		goto error;

10928 10929
	init_supported_rates(priv, &priv->rates);
	if (ipw_send_supported_rates(priv, &priv->rates))
J
James Ketrenos 已提交
10930 10931 10932 10933 10934 10935 10936
		goto error;

	/* Set request-to-send threshold */
	if (priv->rts_threshold) {
		if (ipw_send_rts_threshold(priv, priv->rts_threshold))
			goto error;
	}
10937
#ifdef CONFIG_IPW2200_QOS
10938 10939
	IPW_DEBUG_QOS("QoS: call ipw_qos_activate\n");
	ipw_qos_activate(priv, NULL);
10940
#endif				/* CONFIG_IPW2200_QOS */
J
James Ketrenos 已提交
10941 10942 10943

	if (ipw_set_random_seed(priv))
		goto error;
10944

J
James Ketrenos 已提交
10945 10946 10947 10948
	/* final state transition to the RUN state */
	if (ipw_send_host_complete(priv))
		goto error;

10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959
	priv->status |= STATUS_INIT;

	ipw_led_init(priv);
	ipw_led_radio_on(priv);
	priv->notif_missed_beacons = 0;

	/* Set hardware WEP key if it is configured. */
	if ((priv->capability & CAP_PRIVACY_ON) &&
	    (priv->ieee->sec.level == SEC_LEVEL_1) &&
	    !(priv->ieee->host_encrypt || priv->ieee->host_decrypt))
		ipw_set_hwcrypto_keys(priv);
J
James Ketrenos 已提交
10960 10961

	return 0;
10962

10963
      error:
J
James Ketrenos 已提交
10964 10965 10966
	return -EIO;
}

10967 10968 10969 10970 10971 10972 10973 10974 10975 10976
/*
 * NOTE:
 *
 * These tables have been tested in conjunction with the
 * Intel PRO/Wireless 2200BG and 2915ABG Network Connection Adapters.
 *
 * Altering this values, using it on other hardware, or in geographies
 * not intended for resale of the above mentioned Intel adapters has
 * not been tested.
 *
10977 10978 10979
 * Remember to update the table in README.ipw2200 when changing this
 * table.
 *
10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128
 */
static const struct ieee80211_geo ipw_geos[] = {
	{			/* Restricted */
	 "---",
	 .bg_channels = 11,
	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
		{2427, 4}, {2432, 5}, {2437, 6},
		{2442, 7}, {2447, 8}, {2452, 9},
		{2457, 10}, {2462, 11}},
	 },

	{			/* Custom US/Canada */
	 "ZZF",
	 .bg_channels = 11,
	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
		{2427, 4}, {2432, 5}, {2437, 6},
		{2442, 7}, {2447, 8}, {2452, 9},
		{2457, 10}, {2462, 11}},
	 .a_channels = 8,
	 .a = {{5180, 36},
	       {5200, 40},
	       {5220, 44},
	       {5240, 48},
	       {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
	       {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
	       {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
	       {5320, 64, IEEE80211_CH_PASSIVE_ONLY}},
	 },

	{			/* Rest of World */
	 "ZZD",
	 .bg_channels = 13,
	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
		{2427, 4}, {2432, 5}, {2437, 6},
		{2442, 7}, {2447, 8}, {2452, 9},
		{2457, 10}, {2462, 11}, {2467, 12},
		{2472, 13}},
	 },

	{			/* Custom USA & Europe & High */
	 "ZZA",
	 .bg_channels = 11,
	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
		{2427, 4}, {2432, 5}, {2437, 6},
		{2442, 7}, {2447, 8}, {2452, 9},
		{2457, 10}, {2462, 11}},
	 .a_channels = 13,
	 .a = {{5180, 36},
	       {5200, 40},
	       {5220, 44},
	       {5240, 48},
	       {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
	       {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
	       {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
	       {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
	       {5745, 149},
	       {5765, 153},
	       {5785, 157},
	       {5805, 161},
	       {5825, 165}},
	 },

	{			/* Custom NA & Europe */
	 "ZZB",
	 .bg_channels = 11,
	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
		{2427, 4}, {2432, 5}, {2437, 6},
		{2442, 7}, {2447, 8}, {2452, 9},
		{2457, 10}, {2462, 11}},
	 .a_channels = 13,
	 .a = {{5180, 36},
	       {5200, 40},
	       {5220, 44},
	       {5240, 48},
	       {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
	       {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
	       {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
	       {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
	       {5745, 149, IEEE80211_CH_PASSIVE_ONLY},
	       {5765, 153, IEEE80211_CH_PASSIVE_ONLY},
	       {5785, 157, IEEE80211_CH_PASSIVE_ONLY},
	       {5805, 161, IEEE80211_CH_PASSIVE_ONLY},
	       {5825, 165, IEEE80211_CH_PASSIVE_ONLY}},
	 },

	{			/* Custom Japan */
	 "ZZC",
	 .bg_channels = 11,
	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
		{2427, 4}, {2432, 5}, {2437, 6},
		{2442, 7}, {2447, 8}, {2452, 9},
		{2457, 10}, {2462, 11}},
	 .a_channels = 4,
	 .a = {{5170, 34}, {5190, 38},
	       {5210, 42}, {5230, 46}},
	 },

	{			/* Custom */
	 "ZZM",
	 .bg_channels = 11,
	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
		{2427, 4}, {2432, 5}, {2437, 6},
		{2442, 7}, {2447, 8}, {2452, 9},
		{2457, 10}, {2462, 11}},
	 },

	{			/* Europe */
	 "ZZE",
	 .bg_channels = 13,
	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
		{2427, 4}, {2432, 5}, {2437, 6},
		{2442, 7}, {2447, 8}, {2452, 9},
		{2457, 10}, {2462, 11}, {2467, 12},
		{2472, 13}},
	 .a_channels = 19,
	 .a = {{5180, 36},
	       {5200, 40},
	       {5220, 44},
	       {5240, 48},
	       {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
	       {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
	       {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
	       {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
	       {5500, 100, IEEE80211_CH_PASSIVE_ONLY},
	       {5520, 104, IEEE80211_CH_PASSIVE_ONLY},
	       {5540, 108, IEEE80211_CH_PASSIVE_ONLY},
	       {5560, 112, IEEE80211_CH_PASSIVE_ONLY},
	       {5580, 116, IEEE80211_CH_PASSIVE_ONLY},
	       {5600, 120, IEEE80211_CH_PASSIVE_ONLY},
	       {5620, 124, IEEE80211_CH_PASSIVE_ONLY},
	       {5640, 128, IEEE80211_CH_PASSIVE_ONLY},
	       {5660, 132, IEEE80211_CH_PASSIVE_ONLY},
	       {5680, 136, IEEE80211_CH_PASSIVE_ONLY},
	       {5700, 140, IEEE80211_CH_PASSIVE_ONLY}},
	 },

	{			/* Custom Japan */
	 "ZZJ",
	 .bg_channels = 14,
	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
		{2427, 4}, {2432, 5}, {2437, 6},
		{2442, 7}, {2447, 8}, {2452, 9},
		{2457, 10}, {2462, 11}, {2467, 12},
		{2472, 13}, {2484, 14, IEEE80211_CH_B_ONLY}},
	 .a_channels = 4,
	 .a = {{5170, 34}, {5190, 38},
	       {5210, 42}, {5230, 46}},
	 },

11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139
	{			/* Rest of World */
	 "ZZR",
	 .bg_channels = 14,
	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
		{2427, 4}, {2432, 5}, {2437, 6},
		{2442, 7}, {2447, 8}, {2452, 9},
		{2457, 10}, {2462, 11}, {2467, 12},
		{2472, 13}, {2484, 14, IEEE80211_CH_B_ONLY |
			     IEEE80211_CH_PASSIVE_ONLY}},
	 },

11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224
	{			/* High Band */
	 "ZZH",
	 .bg_channels = 13,
	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
		{2427, 4}, {2432, 5}, {2437, 6},
		{2442, 7}, {2447, 8}, {2452, 9},
		{2457, 10}, {2462, 11},
		{2467, 12, IEEE80211_CH_PASSIVE_ONLY},
		{2472, 13, IEEE80211_CH_PASSIVE_ONLY}},
	 .a_channels = 4,
	 .a = {{5745, 149}, {5765, 153},
	       {5785, 157}, {5805, 161}},
	 },

	{			/* Custom Europe */
	 "ZZG",
	 .bg_channels = 13,
	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
		{2427, 4}, {2432, 5}, {2437, 6},
		{2442, 7}, {2447, 8}, {2452, 9},
		{2457, 10}, {2462, 11},
		{2467, 12}, {2472, 13}},
	 .a_channels = 4,
	 .a = {{5180, 36}, {5200, 40},
	       {5220, 44}, {5240, 48}},
	 },

	{			/* Europe */
	 "ZZK",
	 .bg_channels = 13,
	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
		{2427, 4}, {2432, 5}, {2437, 6},
		{2442, 7}, {2447, 8}, {2452, 9},
		{2457, 10}, {2462, 11},
		{2467, 12, IEEE80211_CH_PASSIVE_ONLY},
		{2472, 13, IEEE80211_CH_PASSIVE_ONLY}},
	 .a_channels = 24,
	 .a = {{5180, 36, IEEE80211_CH_PASSIVE_ONLY},
	       {5200, 40, IEEE80211_CH_PASSIVE_ONLY},
	       {5220, 44, IEEE80211_CH_PASSIVE_ONLY},
	       {5240, 48, IEEE80211_CH_PASSIVE_ONLY},
	       {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
	       {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
	       {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
	       {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
	       {5500, 100, IEEE80211_CH_PASSIVE_ONLY},
	       {5520, 104, IEEE80211_CH_PASSIVE_ONLY},
	       {5540, 108, IEEE80211_CH_PASSIVE_ONLY},
	       {5560, 112, IEEE80211_CH_PASSIVE_ONLY},
	       {5580, 116, IEEE80211_CH_PASSIVE_ONLY},
	       {5600, 120, IEEE80211_CH_PASSIVE_ONLY},
	       {5620, 124, IEEE80211_CH_PASSIVE_ONLY},
	       {5640, 128, IEEE80211_CH_PASSIVE_ONLY},
	       {5660, 132, IEEE80211_CH_PASSIVE_ONLY},
	       {5680, 136, IEEE80211_CH_PASSIVE_ONLY},
	       {5700, 140, IEEE80211_CH_PASSIVE_ONLY},
	       {5745, 149, IEEE80211_CH_PASSIVE_ONLY},
	       {5765, 153, IEEE80211_CH_PASSIVE_ONLY},
	       {5785, 157, IEEE80211_CH_PASSIVE_ONLY},
	       {5805, 161, IEEE80211_CH_PASSIVE_ONLY},
	       {5825, 165, IEEE80211_CH_PASSIVE_ONLY}},
	 },

	{			/* Europe */
	 "ZZL",
	 .bg_channels = 11,
	 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
		{2427, 4}, {2432, 5}, {2437, 6},
		{2442, 7}, {2447, 8}, {2452, 9},
		{2457, 10}, {2462, 11}},
	 .a_channels = 13,
	 .a = {{5180, 36, IEEE80211_CH_PASSIVE_ONLY},
	       {5200, 40, IEEE80211_CH_PASSIVE_ONLY},
	       {5220, 44, IEEE80211_CH_PASSIVE_ONLY},
	       {5240, 48, IEEE80211_CH_PASSIVE_ONLY},
	       {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
	       {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
	       {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
	       {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
	       {5745, 149, IEEE80211_CH_PASSIVE_ONLY},
	       {5765, 153, IEEE80211_CH_PASSIVE_ONLY},
	       {5785, 157, IEEE80211_CH_PASSIVE_ONLY},
	       {5805, 161, IEEE80211_CH_PASSIVE_ONLY},
	       {5825, 165, IEEE80211_CH_PASSIVE_ONLY}},
	 }
11225 11226
};

J
James Ketrenos 已提交
11227 11228 11229
#define MAX_HW_RESTARTS 5
static int ipw_up(struct ipw_priv *priv)
{
11230
	int rc, i, j;
J
James Ketrenos 已提交
11231

11232 11233 11234 11235 11236 11237
	/* Age scan list entries found before suspend */
	if (priv->suspend_time) {
		ieee80211_networks_age(priv->ieee, priv->suspend_time);
		priv->suspend_time = 0;
	}

J
James Ketrenos 已提交
11238 11239 11240
	if (priv->status & STATUS_EXIT_PENDING)
		return -EIO;

11241
	if (cmdlog && !priv->cmdlog) {
11242
		priv->cmdlog = kcalloc(cmdlog, sizeof(*priv->cmdlog),
11243 11244 11245 11246
				       GFP_KERNEL);
		if (priv->cmdlog == NULL) {
			IPW_ERROR("Error allocating %d command log entries.\n",
				  cmdlog);
11247
			return -ENOMEM;
11248 11249 11250 11251 11252
		} else {
			priv->cmdlog_len = cmdlog;
		}
	}

11253
	for (i = 0; i < MAX_HW_RESTARTS; i++) {
11254
		/* Load the microcode, firmware, and eeprom.
J
James Ketrenos 已提交
11255 11256 11257
		 * Also start the clocks. */
		rc = ipw_load(priv);
		if (rc) {
11258
			IPW_ERROR("Unable to load firmware: %d\n", rc);
J
James Ketrenos 已提交
11259 11260 11261 11262 11263 11264 11265 11266
			return rc;
		}

		ipw_init_ordinals(priv);
		if (!(priv->config & CFG_CUSTOM_MAC))
			eeprom_parse_mac(priv, priv->mac_addr);
		memcpy(priv->net_dev->dev_addr, priv->mac_addr, ETH_ALEN);

11267 11268 11269 11270 11271
		for (j = 0; j < ARRAY_SIZE(ipw_geos); j++) {
			if (!memcmp(&priv->eeprom[EEPROM_COUNTRY_CODE],
				    ipw_geos[j].name, 3))
				break;
		}
11272 11273 11274 11275 11276
		if (j == ARRAY_SIZE(ipw_geos)) {
			IPW_WARNING("SKU [%c%c%c] not recognized.\n",
				    priv->eeprom[EEPROM_COUNTRY_CODE + 0],
				    priv->eeprom[EEPROM_COUNTRY_CODE + 1],
				    priv->eeprom[EEPROM_COUNTRY_CODE + 2]);
11277
			j = 0;
11278
		}
11279
		if (ieee80211_set_geo(priv->ieee, &ipw_geos[j])) {
11280 11281 11282 11283
			IPW_WARNING("Could not set geography.");
			return 0;
		}

11284 11285 11286 11287 11288 11289 11290 11291 11292
		if (priv->status & STATUS_RF_KILL_SW) {
			IPW_WARNING("Radio disabled by module parameter.\n");
			return 0;
		} else if (rf_kill_active(priv)) {
			IPW_WARNING("Radio Frequency Kill Switch is On:\n"
				    "Kill switch must be turned off for "
				    "wireless networking to work.\n");
			queue_delayed_work(priv->workqueue, &priv->rf_kill,
					   2 * HZ);
J
James Ketrenos 已提交
11293
			return 0;
11294
		}
J
James Ketrenos 已提交
11295 11296 11297 11298

		rc = ipw_config(priv);
		if (!rc) {
			IPW_DEBUG_INFO("Configured device on count %i\n", i);
11299 11300 11301

			/* If configure to try and auto-associate, kick
			 * off a scan. */
D
David Howells 已提交
11302 11303
			queue_delayed_work(priv->workqueue,
					   &priv->request_scan, 0);
11304

J
James Ketrenos 已提交
11305 11306
			return 0;
		}
11307

11308
		IPW_DEBUG_INFO("Device configuration failed: 0x%08X\n", rc);
J
James Ketrenos 已提交
11309 11310 11311 11312 11313 11314 11315 11316
		IPW_DEBUG_INFO("Failed to config device on retry %d of %d\n",
			       i, MAX_HW_RESTARTS);

		/* We had an error bringing up the hardware, so take it
		 * all the way back down so we can try again */
		ipw_down(priv);
	}

11317
	/* tried to restart and config the device for as long as our
J
James Ketrenos 已提交
11318
	 * patience could withstand */
11319
	IPW_ERROR("Unable to initialize device after %d attempts.\n", i);
11320

J
James Ketrenos 已提交
11321 11322 11323
	return -EIO;
}

D
David Howells 已提交
11324
static void ipw_bg_up(struct work_struct *work)
11325
{
D
David Howells 已提交
11326 11327
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, up);
11328
	mutex_lock(&priv->mutex);
D
David Howells 已提交
11329
	ipw_up(priv);
11330
	mutex_unlock(&priv->mutex);
11331 11332
}

11333
static void ipw_deinit(struct ipw_priv *priv)
J
James Ketrenos 已提交
11334
{
11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362
	int i;

	if (priv->status & STATUS_SCANNING) {
		IPW_DEBUG_INFO("Aborting scan during shutdown.\n");
		ipw_abort_scan(priv);
	}

	if (priv->status & STATUS_ASSOCIATED) {
		IPW_DEBUG_INFO("Disassociating during shutdown.\n");
		ipw_disassociate(priv);
	}

	ipw_led_shutdown(priv);

	/* Wait up to 1s for status to change to not scanning and not
	 * associated (disassociation can take a while for a ful 802.11
	 * exchange */
	for (i = 1000; i && (priv->status &
			     (STATUS_DISASSOCIATING |
			      STATUS_ASSOCIATED | STATUS_SCANNING)); i--)
		udelay(10);

	if (priv->status & (STATUS_DISASSOCIATING |
			    STATUS_ASSOCIATED | STATUS_SCANNING))
		IPW_DEBUG_INFO("Still associated or scanning...\n");
	else
		IPW_DEBUG_INFO("Took %dms to de-init\n", 1000 - i);

J
James Ketrenos 已提交
11363 11364
	/* Attempt to disable the card */
	ipw_send_card_disable(priv, 0);
11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381

	priv->status &= ~STATUS_INIT;
}

static void ipw_down(struct ipw_priv *priv)
{
	int exit_pending = priv->status & STATUS_EXIT_PENDING;

	priv->status |= STATUS_EXIT_PENDING;

	if (ipw_is_init(priv))
		ipw_deinit(priv);

	/* Wipe out the EXIT_PENDING status bit if we are not actually
	 * exiting the module */
	if (!exit_pending)
		priv->status &= ~STATUS_EXIT_PENDING;
J
James Ketrenos 已提交
11382 11383 11384 11385 11386

	/* tell the device to stop sending interrupts */
	ipw_disable_interrupts(priv);

	/* Clear all bits but the RF Kill */
11387
	priv->status &= STATUS_RF_KILL_MASK | STATUS_EXIT_PENDING;
J
James Ketrenos 已提交
11388 11389 11390
	netif_carrier_off(priv->net_dev);

	ipw_stop_nic(priv);
11391 11392

	ipw_led_radio_off(priv);
J
James Ketrenos 已提交
11393 11394
}

D
David Howells 已提交
11395
static void ipw_bg_down(struct work_struct *work)
11396
{
D
David Howells 已提交
11397 11398
	struct ipw_priv *priv =
		container_of(work, struct ipw_priv, down);
11399
	mutex_lock(&priv->mutex);
D
David Howells 已提交
11400
	ipw_down(priv);
11401
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
11402 11403 11404 11405 11406 11407
}

/* Called by register_netdev() */
static int ipw_net_init(struct net_device *dev)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
11408
	mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
11409

11410
	if (ipw_up(priv)) {
11411
		mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
11412
		return -EIO;
11413
	}
J
James Ketrenos 已提交
11414

11415
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438
	return 0;
}

/* PCI driver stuff */
static struct pci_device_id card_ids[] = {
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2701, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2702, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2711, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2712, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2721, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2722, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2731, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2732, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2741, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x103c, 0x2741, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2742, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2751, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2752, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2753, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2754, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2761, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2762, 0, 0, 0},
	{PCI_VENDOR_ID_INTEL, 0x104f, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
11439
	{PCI_VENDOR_ID_INTEL, 0x4220, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},	/* BG */
11440
	{PCI_VENDOR_ID_INTEL, 0x4221, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},	/* BG */
11441 11442
	{PCI_VENDOR_ID_INTEL, 0x4223, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},	/* ABG */
	{PCI_VENDOR_ID_INTEL, 0x4224, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},	/* ABG */
11443

J
James Ketrenos 已提交
11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459
	/* required last entry */
	{0,}
};

MODULE_DEVICE_TABLE(pci, card_ids);

static struct attribute *ipw_sysfs_entries[] = {
	&dev_attr_rf_kill.attr,
	&dev_attr_direct_dword.attr,
	&dev_attr_indirect_byte.attr,
	&dev_attr_indirect_dword.attr,
	&dev_attr_mem_gpio_reg.attr,
	&dev_attr_command_event_reg.attr,
	&dev_attr_nic_type.attr,
	&dev_attr_status.attr,
	&dev_attr_cfg.attr,
11460 11461
	&dev_attr_error.attr,
	&dev_attr_event_log.attr,
11462
	&dev_attr_cmd_log.attr,
J
James Ketrenos 已提交
11463 11464 11465
	&dev_attr_eeprom_delay.attr,
	&dev_attr_ucode_version.attr,
	&dev_attr_rtc.attr,
11466 11467
	&dev_attr_scan_age.attr,
	&dev_attr_led.attr,
11468 11469
	&dev_attr_speed_scan.attr,
	&dev_attr_net_stats.attr,
11470
	&dev_attr_channels.attr,
11471 11472 11473 11474
#ifdef CONFIG_IPW2200_PROMISCUOUS
	&dev_attr_rtap_iface.attr,
	&dev_attr_rtap_filter.attr,
#endif
J
James Ketrenos 已提交
11475 11476 11477 11478 11479
	NULL
};

static struct attribute_group ipw_attribute_group = {
	.name = NULL,		/* put in device directory */
11480
	.attrs = ipw_sysfs_entries,
J
James Ketrenos 已提交
11481 11482
};

11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528
#ifdef CONFIG_IPW2200_PROMISCUOUS
static int ipw_prom_open(struct net_device *dev)
{
	struct ipw_prom_priv *prom_priv = ieee80211_priv(dev);
	struct ipw_priv *priv = prom_priv->priv;

	IPW_DEBUG_INFO("prom dev->open\n");
	netif_carrier_off(dev);

	if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
		priv->sys_config.accept_all_data_frames = 1;
		priv->sys_config.accept_non_directed_frames = 1;
		priv->sys_config.accept_all_mgmt_bcpr = 1;
		priv->sys_config.accept_all_mgmt_frames = 1;

		ipw_send_system_config(priv);
	}

	return 0;
}

static int ipw_prom_stop(struct net_device *dev)
{
	struct ipw_prom_priv *prom_priv = ieee80211_priv(dev);
	struct ipw_priv *priv = prom_priv->priv;

	IPW_DEBUG_INFO("prom dev->stop\n");

	if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
		priv->sys_config.accept_all_data_frames = 0;
		priv->sys_config.accept_non_directed_frames = 0;
		priv->sys_config.accept_all_mgmt_bcpr = 0;
		priv->sys_config.accept_all_mgmt_frames = 0;

		ipw_send_system_config(priv);
	}

	return 0;
}

static int ipw_prom_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
	IPW_DEBUG_INFO("prom dev->xmit\n");
	return -EOPNOTSUPP;
}

11529 11530 11531 11532 11533 11534 11535 11536 11537
static const struct net_device_ops ipw_prom_netdev_ops = {
	.ndo_open 		= ipw_prom_open,
	.ndo_stop		= ipw_prom_stop,
	.ndo_start_xmit		= ipw_prom_hard_start_xmit,
	.ndo_change_mtu		= ieee80211_change_mtu,
	.ndo_set_mac_address 	= eth_mac_addr,
	.ndo_validate_addr	= eth_validate_addr,
};

11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553
static int ipw_prom_alloc(struct ipw_priv *priv)
{
	int rc = 0;

	if (priv->prom_net_dev)
		return -EPERM;

	priv->prom_net_dev = alloc_ieee80211(sizeof(struct ipw_prom_priv));
	if (priv->prom_net_dev == NULL)
		return -ENOMEM;

	priv->prom_priv = ieee80211_priv(priv->prom_net_dev);
	priv->prom_priv->ieee = netdev_priv(priv->prom_net_dev);
	priv->prom_priv->priv = priv;

	strcpy(priv->prom_net_dev->name, "rtap%d");
11554
	memcpy(priv->prom_net_dev->dev_addr, priv->mac_addr, ETH_ALEN);
11555 11556

	priv->prom_net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
11557
	priv->prom_net_dev->netdev_ops = &ipw_prom_netdev_ops;
11558 11559

	priv->prom_priv->ieee->iw_mode = IW_MODE_MONITOR;
11560
	SET_NETDEV_DEV(priv->prom_net_dev, &priv->pci_dev->dev);
11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584

	rc = register_netdev(priv->prom_net_dev);
	if (rc) {
		free_ieee80211(priv->prom_net_dev);
		priv->prom_net_dev = NULL;
		return rc;
	}

	return 0;
}

static void ipw_prom_free(struct ipw_priv *priv)
{
	if (!priv->prom_net_dev)
		return;

	unregister_netdev(priv->prom_net_dev);
	free_ieee80211(priv->prom_net_dev);

	priv->prom_net_dev = NULL;
}

#endif

11585 11586 11587 11588 11589 11590 11591 11592 11593 11594
static const struct net_device_ops ipw_netdev_ops = {
	.ndo_init		= ipw_net_init,
	.ndo_open		= ipw_net_open,
	.ndo_stop		= ipw_net_stop,
	.ndo_set_multicast_list	= ipw_net_set_multicast_list,
	.ndo_set_mac_address	= ipw_net_set_mac_address,
	.ndo_start_xmit		= ieee80211_xmit,
	.ndo_change_mtu		= ieee80211_change_mtu,
	.ndo_validate_addr	= eth_validate_addr,
};
11595

11596 11597
static int __devinit ipw_pci_probe(struct pci_dev *pdev,
				   const struct pci_device_id *ent)
J
James Ketrenos 已提交
11598 11599 11600 11601 11602 11603
{
	int err = 0;
	struct net_device *net_dev;
	void __iomem *base;
	u32 length, val;
	struct ipw_priv *priv;
11604
	int i;
J
James Ketrenos 已提交
11605 11606 11607 11608 11609 11610 11611 11612 11613

	net_dev = alloc_ieee80211(sizeof(struct ipw_priv));
	if (net_dev == NULL) {
		err = -ENOMEM;
		goto out;
	}

	priv = ieee80211_priv(net_dev);
	priv->ieee = netdev_priv(net_dev);
11614

J
James Ketrenos 已提交
11615 11616 11617
	priv->net_dev = net_dev;
	priv->pci_dev = pdev;
	ipw_debug_level = debug;
Z
Zhu Yi 已提交
11618
	spin_lock_init(&priv->irq_lock);
J
James Ketrenos 已提交
11619
	spin_lock_init(&priv->lock);
11620 11621
	for (i = 0; i < IPW_IBSS_MAC_HASH_SIZE; i++)
		INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
J
James Ketrenos 已提交
11622

11623
	mutex_init(&priv->mutex);
J
James Ketrenos 已提交
11624 11625 11626 11627 11628 11629 11630
	if (pci_enable_device(pdev)) {
		err = -ENODEV;
		goto out_free_ieee80211;
	}

	pci_set_master(pdev);

11631
	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
11632
	if (!err)
11633
		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
J
James Ketrenos 已提交
11634 11635 11636 11637 11638 11639 11640 11641
	if (err) {
		printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
		goto out_pci_disable_device;
	}

	pci_set_drvdata(pdev, priv);

	err = pci_request_regions(pdev, DRV_NAME);
11642
	if (err)
J
James Ketrenos 已提交
11643 11644
		goto out_pci_disable_device;

11645
	/* We disable the RETRY_TIMEOUT register (0x41) to keep
J
James Ketrenos 已提交
11646
	 * PCI Tx retries from interfering with C3 CPU state */
11647 11648
	pci_read_config_dword(pdev, 0x40, &val);
	if ((val & 0x0000ff00) != 0)
J
James Ketrenos 已提交
11649
		pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
11650

J
James Ketrenos 已提交
11651 11652
	length = pci_resource_len(pdev, 0);
	priv->hw_len = length;
11653

11654
	base = pci_ioremap_bar(pdev, 0);
J
James Ketrenos 已提交
11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669
	if (!base) {
		err = -ENODEV;
		goto out_pci_release_regions;
	}

	priv->hw_base = base;
	IPW_DEBUG_INFO("pci_resource_len = 0x%08x\n", length);
	IPW_DEBUG_INFO("pci_resource_base = %p\n", base);

	err = ipw_setup_deferred_work(priv);
	if (err) {
		IPW_ERROR("Unable to setup deferred work\n");
		goto out_iounmap;
	}

11670
	ipw_sw_reset(priv, 1);
J
James Ketrenos 已提交
11671

11672
	err = request_irq(pdev->irq, ipw_isr, IRQF_SHARED, DRV_NAME, priv);
J
James Ketrenos 已提交
11673 11674 11675 11676 11677 11678 11679
	if (err) {
		IPW_ERROR("Error allocating IRQ %d\n", pdev->irq);
		goto out_destroy_workqueue;
	}

	SET_NETDEV_DEV(net_dev, &pdev->dev);

11680
	mutex_lock(&priv->mutex);
11681

J
James Ketrenos 已提交
11682 11683
	priv->ieee->hard_start_xmit = ipw_net_hard_start_xmit;
	priv->ieee->set_security = shim__set_security;
11684
	priv->ieee->is_queue_full = ipw_net_is_queue_full;
J
James Ketrenos 已提交
11685

11686
#ifdef CONFIG_IPW2200_QOS
11687
	priv->ieee->is_qos_active = ipw_is_qos_active;
11688 11689 11690
	priv->ieee->handle_probe_response = ipw_handle_beacon;
	priv->ieee->handle_beacon = ipw_handle_probe_response;
	priv->ieee->handle_assoc_response = ipw_handle_assoc_response;
11691
#endif				/* CONFIG_IPW2200_QOS */
11692

11693 11694
	priv->ieee->perfect_rssi = -20;
	priv->ieee->worst_rssi = -85;
J
James Ketrenos 已提交
11695

11696
	net_dev->netdev_ops = &ipw_netdev_ops;
11697 11698
	priv->wireless_data.spy_data = &priv->ieee->spy_data;
	net_dev->wireless_data = &priv->wireless_data;
J
James Ketrenos 已提交
11699 11700 11701
	net_dev->wireless_handlers = &ipw_wx_handler_def;
	net_dev->ethtool_ops = &ipw_ethtool_ops;
	net_dev->irq = pdev->irq;
11702
	net_dev->base_addr = (unsigned long)priv->hw_base;
J
James Ketrenos 已提交
11703 11704 11705 11706 11707 11708
	net_dev->mem_start = pci_resource_start(pdev, 0);
	net_dev->mem_end = net_dev->mem_start + pci_resource_len(pdev, 0) - 1;

	err = sysfs_create_group(&pdev->dev.kobj, &ipw_attribute_group);
	if (err) {
		IPW_ERROR("failed to create sysfs device attributes\n");
11709
		mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
11710 11711 11712
		goto out_release_irq;
	}

11713
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
11714 11715 11716
	err = register_netdev(net_dev);
	if (err) {
		IPW_ERROR("failed to register network device\n");
11717
		goto out_remove_sysfs;
J
James Ketrenos 已提交
11718
	}
11719

11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731
#ifdef CONFIG_IPW2200_PROMISCUOUS
	if (rtap_iface) {
	        err = ipw_prom_alloc(priv);
		if (err) {
			IPW_ERROR("Failed to register promiscuous network "
				  "device (error %d).\n", err);
			unregister_netdev(priv->net_dev);
			goto out_remove_sysfs;
		}
	}
#endif

11732 11733 11734 11735 11736
	printk(KERN_INFO DRV_NAME ": Detected geography %s (%d 802.11bg "
	       "channels, %d 802.11a channels)\n",
	       priv->ieee->geo.name, priv->ieee->geo.bg_channels,
	       priv->ieee->geo.a_channels);

J
James Ketrenos 已提交
11737 11738
	return 0;

11739
      out_remove_sysfs:
J
James Ketrenos 已提交
11740
	sysfs_remove_group(&pdev->dev.kobj, &ipw_attribute_group);
11741
      out_release_irq:
J
James Ketrenos 已提交
11742
	free_irq(pdev->irq, priv);
11743
      out_destroy_workqueue:
J
James Ketrenos 已提交
11744 11745
	destroy_workqueue(priv->workqueue);
	priv->workqueue = NULL;
11746
      out_iounmap:
J
James Ketrenos 已提交
11747
	iounmap(priv->hw_base);
11748
      out_pci_release_regions:
J
James Ketrenos 已提交
11749
	pci_release_regions(pdev);
11750
      out_pci_disable_device:
J
James Ketrenos 已提交
11751 11752
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);
11753
      out_free_ieee80211:
J
James Ketrenos 已提交
11754
	free_ieee80211(priv->net_dev);
11755
      out:
J
James Ketrenos 已提交
11756 11757 11758
	return err;
}

11759
static void __devexit ipw_pci_remove(struct pci_dev *pdev)
J
James Ketrenos 已提交
11760 11761
{
	struct ipw_priv *priv = pci_get_drvdata(pdev);
11762 11763
	struct list_head *p, *q;
	int i;
11764

J
James Ketrenos 已提交
11765 11766 11767
	if (!priv)
		return;

11768
	mutex_lock(&priv->mutex);
J
James Ketrenos 已提交
11769

11770
	priv->status |= STATUS_EXIT_PENDING;
J
James Ketrenos 已提交
11771 11772 11773
	ipw_down(priv);
	sysfs_remove_group(&pdev->dev.kobj, &ipw_attribute_group);

11774
	mutex_unlock(&priv->mutex);
J
James Ketrenos 已提交
11775 11776 11777 11778 11779 11780 11781 11782 11783

	unregister_netdev(priv->net_dev);

	if (priv->rxq) {
		ipw_rx_queue_free(priv, priv->rxq);
		priv->rxq = NULL;
	}
	ipw_tx_queue_free(priv);

11784 11785 11786 11787
	if (priv->cmdlog) {
		kfree(priv->cmdlog);
		priv->cmdlog = NULL;
	}
J
James Ketrenos 已提交
11788 11789
	/* ipw_down will ensure that there is no more pending work
	 * in the workqueue's, so we can safely remove them now. */
11790 11791 11792
	cancel_delayed_work(&priv->adhoc_check);
	cancel_delayed_work(&priv->gather_stats);
	cancel_delayed_work(&priv->request_scan);
D
Dan Williams 已提交
11793 11794
	cancel_delayed_work(&priv->request_direct_scan);
	cancel_delayed_work(&priv->request_passive_scan);
11795
	cancel_delayed_work(&priv->scan_event);
11796 11797 11798 11799
	cancel_delayed_work(&priv->rf_kill);
	cancel_delayed_work(&priv->scan_check);
	destroy_workqueue(priv->workqueue);
	priv->workqueue = NULL;
J
James Ketrenos 已提交
11800

11801 11802 11803 11804
	/* Free MAC hash list for ADHOC */
	for (i = 0; i < IPW_IBSS_MAC_HASH_SIZE; i++) {
		list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
			list_del(p);
11805
			kfree(list_entry(p, struct ipw_ibss_seq, list));
11806 11807 11808
		}
	}

11809 11810
	kfree(priv->error);
	priv->error = NULL;
J
James Ketrenos 已提交
11811

11812 11813 11814 11815
#ifdef CONFIG_IPW2200_PROMISCUOUS
	ipw_prom_free(priv);
#endif

J
James Ketrenos 已提交
11816 11817 11818 11819 11820 11821
	free_irq(pdev->irq, priv);
	iounmap(priv->hw_base);
	pci_release_regions(pdev);
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);
	free_ieee80211(priv->net_dev);
11822
	free_firmware();
J
James Ketrenos 已提交
11823 11824 11825
}

#ifdef CONFIG_PM
11826
static int ipw_pci_suspend(struct pci_dev *pdev, pm_message_t state)
J
James Ketrenos 已提交
11827 11828 11829 11830 11831 11832
{
	struct ipw_priv *priv = pci_get_drvdata(pdev);
	struct net_device *dev = priv->net_dev;

	printk(KERN_INFO "%s: Going into suspend...\n", dev->name);

11833
	/* Take down the device; powers it off, etc. */
J
James Ketrenos 已提交
11834 11835 11836 11837 11838 11839 11840
	ipw_down(priv);

	/* Remove the PRESENT state of the device */
	netif_device_detach(dev);

	pci_save_state(pdev);
	pci_disable_device(pdev);
11841
	pci_set_power_state(pdev, pci_choose_state(pdev, state));
11842

11843 11844
	priv->suspend_at = get_seconds();

J
James Ketrenos 已提交
11845 11846 11847 11848 11849 11850 11851
	return 0;
}

static int ipw_pci_resume(struct pci_dev *pdev)
{
	struct ipw_priv *priv = pci_get_drvdata(pdev);
	struct net_device *dev = priv->net_dev;
11852
	int err;
J
James Ketrenos 已提交
11853
	u32 val;
11854

J
James Ketrenos 已提交
11855 11856
	printk(KERN_INFO "%s: Coming out of suspend...\n", dev->name);

11857
	pci_set_power_state(pdev, PCI_D0);
11858 11859 11860 11861 11862 11863
	err = pci_enable_device(pdev);
	if (err) {
		printk(KERN_ERR "%s: pci_enable_device failed on resume\n",
		       dev->name);
		return err;
	}
J
James Ketrenos 已提交
11864
	pci_restore_state(pdev);
11865

J
James Ketrenos 已提交
11866 11867 11868 11869 11870 11871
	/*
	 * Suspend/Resume resets the PCI configuration space, so we have to
	 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
	 * from interfering with C3 CPU state. pci_restore_state won't help
	 * here since it only restores the first 64 bytes pci config header.
	 */
11872 11873
	pci_read_config_dword(pdev, 0x40, &val);
	if ((val & 0x0000ff00) != 0)
J
James Ketrenos 已提交
11874 11875 11876 11877 11878 11879
		pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);

	/* Set the device back into the PRESENT state; this will also wake
	 * the queue of needed */
	netif_device_attach(dev);

11880 11881
	priv->suspend_time = get_seconds() - priv->suspend_at;

J
James Ketrenos 已提交
11882 11883
	/* Bring the device back up */
	queue_work(priv->workqueue, &priv->up);
11884

J
James Ketrenos 已提交
11885 11886 11887 11888
	return 0;
}
#endif

11889 11890 11891 11892 11893 11894 11895 11896 11897 11898
static void ipw_pci_shutdown(struct pci_dev *pdev)
{
	struct ipw_priv *priv = pci_get_drvdata(pdev);

	/* Take down the device; powers it off, etc. */
	ipw_down(priv);

	pci_disable_device(pdev);
}

J
James Ketrenos 已提交
11899 11900 11901 11902 11903 11904 11905 11906 11907 11908
/* driver initialization stuff */
static struct pci_driver ipw_driver = {
	.name = DRV_NAME,
	.id_table = card_ids,
	.probe = ipw_pci_probe,
	.remove = __devexit_p(ipw_pci_remove),
#ifdef CONFIG_PM
	.suspend = ipw_pci_suspend,
	.resume = ipw_pci_resume,
#endif
11909
	.shutdown = ipw_pci_shutdown,
J
James Ketrenos 已提交
11910 11911 11912 11913 11914 11915 11916 11917 11918
};

static int __init ipw_init(void)
{
	int ret;

	printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
	printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");

11919
	ret = pci_register_driver(&ipw_driver);
J
James Ketrenos 已提交
11920 11921 11922 11923 11924
	if (ret) {
		IPW_ERROR("Unable to initialize PCI module\n");
		return ret;
	}

11925
	ret = driver_create_file(&ipw_driver.driver, &driver_attr_debug_level);
J
James Ketrenos 已提交
11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937 11938 11939 11940 11941 11942 11943 11944
	if (ret) {
		IPW_ERROR("Unable to create driver sysfs file\n");
		pci_unregister_driver(&ipw_driver);
		return ret;
	}

	return ret;
}

static void __exit ipw_exit(void)
{
	driver_remove_file(&ipw_driver.driver, &driver_attr_debug_level);
	pci_unregister_driver(&ipw_driver);
}

module_param(disable, int, 0444);
MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");

module_param(associate, int, 0444);
11945
MODULE_PARM_DESC(associate, "auto associate when scanning (default off)");
J
James Ketrenos 已提交
11946 11947 11948 11949

module_param(auto_create, int, 0444);
MODULE_PARM_DESC(auto_create, "auto create adhoc network (default on)");

11950
module_param(led, int, 0444);
11951
MODULE_PARM_DESC(led, "enable led control on some systems (default 0 off)");
11952

J
James Ketrenos 已提交
11953 11954 11955 11956
module_param(debug, int, 0444);
MODULE_PARM_DESC(debug, "debug output mask");

module_param(channel, int, 0444);
11957
MODULE_PARM_DESC(channel, "channel to limit associate to (default 0 [ANY])");
J
James Ketrenos 已提交
11958

11959 11960 11961 11962 11963
#ifdef CONFIG_IPW2200_PROMISCUOUS
module_param(rtap_iface, int, 0444);
MODULE_PARM_DESC(rtap_iface, "create the rtap interface (1 - create, default 0)");
#endif

11964
#ifdef CONFIG_IPW2200_QOS
11965 11966 11967 11968 11969 11970 11971 11972
module_param(qos_enable, int, 0444);
MODULE_PARM_DESC(qos_enable, "enable all QoS functionalitis");

module_param(qos_burst_enable, int, 0444);
MODULE_PARM_DESC(qos_burst_enable, "enable QoS burst mode");

module_param(qos_no_ack_mask, int, 0444);
MODULE_PARM_DESC(qos_no_ack_mask, "mask Tx_Queue to no ack");
J
James Ketrenos 已提交
11973

11974 11975 11976 11977 11978
module_param(burst_duration_CCK, int, 0444);
MODULE_PARM_DESC(burst_duration_CCK, "set CCK burst value");

module_param(burst_duration_OFDM, int, 0444);
MODULE_PARM_DESC(burst_duration_OFDM, "set OFDM burst value");
11979
#endif				/* CONFIG_IPW2200_QOS */
11980 11981

#ifdef CONFIG_IPW2200_MONITOR
J
James Ketrenos 已提交
11982 11983 11984 11985 11986 11987 11988
module_param(mode, int, 0444);
MODULE_PARM_DESC(mode, "network mode (0=BSS,1=IBSS,2=Monitor)");
#else
module_param(mode, int, 0444);
MODULE_PARM_DESC(mode, "network mode (0=BSS,1=IBSS)");
#endif

11989 11990 11991
module_param(bt_coexist, int, 0444);
MODULE_PARM_DESC(bt_coexist, "enable bluetooth coexistence (default off)");

11992
module_param(hwcrypto, int, 0444);
11993
MODULE_PARM_DESC(hwcrypto, "enable hardware crypto (default off)");
11994

11995 11996 11997 11998
module_param(cmdlog, int, 0444);
MODULE_PARM_DESC(cmdlog,
		 "allocate a ring buffer for logging firmware commands");

11999 12000 12001
module_param(roaming, int, 0444);
MODULE_PARM_DESC(roaming, "enable roaming support (default on)");

12002 12003 12004
module_param(antenna, int, 0444);
MODULE_PARM_DESC(antenna, "select antenna 1=Main, 3=Aux, default 0 [both], 2=slow_diversity (choose the one with lower background noise)");

J
James Ketrenos 已提交
12005 12006
module_exit(ipw_exit);
module_init(ipw_init);