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

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

  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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  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"

#define IPW2200_VERSION "1.0.0"
#define DRV_DESCRIPTION	"Intel(R) PRO/Wireless 2200/2915 Network Driver"
#define DRV_COPYRIGHT	"Copyright(c) 2003-2004 Intel Corporation"
#define DRV_VERSION     IPW2200_VERSION

MODULE_DESCRIPTION(DRV_DESCRIPTION);
MODULE_VERSION(DRV_VERSION);
MODULE_AUTHOR(DRV_COPYRIGHT);
MODULE_LICENSE("GPL");

static int debug = 0;
static int channel = 0;
static char *ifname;
static int mode = 0;

static u32 ipw_debug_level;
static int associate = 1;
static int auto_create = 1;
static int disable = 0;
static const char ipw_modes[] = {
	'a', 'b', 'g', '?'
};

static void ipw_rx(struct ipw_priv *priv);
59
static int ipw_queue_tx_reclaim(struct ipw_priv *priv,
J
James Ketrenos 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
				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 *);
static void ipw_down(struct ipw_priv *);
static int ipw_config(struct ipw_priv *);
75 76
static int init_supported_rates(struct ipw_priv *priv,
				struct ipw_supported_rates *prates);
J
James Ketrenos 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

static u8 band_b_active_channel[MAX_B_CHANNELS] = {
	1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0
};
static u8 band_a_active_channel[MAX_A_CHANNELS] = {
	36, 40, 44, 48, 149, 153, 157, 161, 165, 52, 56, 60, 64, 0
};

static int is_valid_channel(int mode_mask, int channel)
{
	int i;

	if (!channel)
		return 0;

	if (mode_mask & IEEE_A)
		for (i = 0; i < MAX_A_CHANNELS; i++)
			if (band_a_active_channel[i] == channel)
				return IEEE_A;

	if (mode_mask & (IEEE_B | IEEE_G))
		for (i = 0; i < MAX_B_CHANNELS; i++)
			if (band_b_active_channel[i] == channel)
				return mode_mask & (IEEE_B | IEEE_G);

	return 0;
}

105
static char *snprint_line(char *buf, size_t count,
106
			  const u8 * data, u32 len, u32 ofs)
J
James Ketrenos 已提交
107 108 109
{
	int out, i, j, l;
	char c;
110

J
James Ketrenos 已提交
111 112 113 114
	out = snprintf(buf, count, "%08X", ofs);

	for (l = 0, i = 0; i < 2; i++) {
		out += snprintf(buf + out, count - out, " ");
115 116
		for (j = 0; j < 8 && l < len; j++, l++)
			out += snprintf(buf + out, count - out, "%02X ",
J
James Ketrenos 已提交
117 118 119 120
					data[(i * 8 + j)]);
		for (; j < 8; j++)
			out += snprintf(buf + out, count - out, "   ");
	}
121

J
James Ketrenos 已提交
122 123 124 125 126 127 128
	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 = '.';
129

J
James Ketrenos 已提交
130 131 132 133 134 135
			out += snprintf(buf + out, count - out, "%c", c);
		}

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

J
James Ketrenos 已提交
137 138 139
	return buf;
}

140
static void printk_buf(int level, const u8 * data, u32 len)
J
James Ketrenos 已提交
141 142 143 144 145 146 147 148
{
	char line[81];
	u32 ofs = 0;
	if (!(ipw_debug_level & level))
		return;

	while (len) {
		printk(KERN_DEBUG "%s\n",
149
		       snprint_line(line, sizeof(line), &data[ofs],
J
James Ketrenos 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
				    min(len, 16U), ofs));
		ofs += 16;
		len -= min(len, 16U);
	}
}

static u32 _ipw_read_reg32(struct ipw_priv *priv, u32 reg);
#define ipw_read_reg32(a, b) _ipw_read_reg32(a, b)

static u8 _ipw_read_reg8(struct ipw_priv *ipw, u32 reg);
#define ipw_read_reg8(a, b) _ipw_read_reg8(a, b)

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)
{
165 166
	IPW_DEBUG_IO("%s %d: write_indirect8(0x%08X, 0x%08X)\n", __FILE__,
		     __LINE__, (u32) (b), (u32) (c));
J
James Ketrenos 已提交
167 168 169 170 171 172
	_ipw_write_reg8(a, b, c);
}

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)
{
173 174
	IPW_DEBUG_IO("%s %d: write_indirect16(0x%08X, 0x%08X)\n", __FILE__,
		     __LINE__, (u32) (b), (u32) (c));
J
James Ketrenos 已提交
175 176 177 178 179 180
	_ipw_write_reg16(a, b, c);
}

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)
{
181 182
	IPW_DEBUG_IO("%s %d: write_indirect32(0x%08X, 0x%08X)\n", __FILE__,
		     __LINE__, (u32) (b), (u32) (c));
J
James Ketrenos 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	_ipw_write_reg32(a, b, c);
}

#define _ipw_write8(ipw, ofs, val) writeb((val), (ipw)->hw_base + (ofs))
#define ipw_write8(ipw, ofs, val) \
 IPW_DEBUG_IO("%s %d: write_direct8(0x%08X, 0x%08X)\n", __FILE__, __LINE__, (u32)(ofs), (u32)(val)); \
 _ipw_write8(ipw, ofs, val)

#define _ipw_write16(ipw, ofs, val) writew((val), (ipw)->hw_base + (ofs))
#define ipw_write16(ipw, ofs, val) \
 IPW_DEBUG_IO("%s %d: write_direct16(0x%08X, 0x%08X)\n", __FILE__, __LINE__, (u32)(ofs), (u32)(val)); \
 _ipw_write16(ipw, ofs, val)

#define _ipw_write32(ipw, ofs, val) writel((val), (ipw)->hw_base + (ofs))
#define ipw_write32(ipw, ofs, val) \
 IPW_DEBUG_IO("%s %d: write_direct32(0x%08X, 0x%08X)\n", __FILE__, __LINE__, (u32)(ofs), (u32)(val)); \
 _ipw_write32(ipw, ofs, val)

#define _ipw_read8(ipw, ofs) readb((ipw)->hw_base + (ofs))
202 203 204
static inline u8 __ipw_read8(char *f, u32 l, struct ipw_priv *ipw, u32 ofs)
{
	IPW_DEBUG_IO("%s %d: read_direct8(0x%08X)\n", f, l, (u32) (ofs));
J
James Ketrenos 已提交
205 206
	return _ipw_read8(ipw, ofs);
}
207

J
James Ketrenos 已提交
208 209 210
#define ipw_read8(ipw, ofs) __ipw_read8(__FILE__, __LINE__, ipw, ofs)

#define _ipw_read16(ipw, ofs) readw((ipw)->hw_base + (ofs))
211 212 213
static inline u16 __ipw_read16(char *f, u32 l, struct ipw_priv *ipw, u32 ofs)
{
	IPW_DEBUG_IO("%s %d: read_direct16(0x%08X)\n", f, l, (u32) (ofs));
J
James Ketrenos 已提交
214 215
	return _ipw_read16(ipw, ofs);
}
216

J
James Ketrenos 已提交
217 218 219
#define ipw_read16(ipw, ofs) __ipw_read16(__FILE__, __LINE__, ipw, ofs)

#define _ipw_read32(ipw, ofs) readl((ipw)->hw_base + (ofs))
220 221 222
static inline u32 __ipw_read32(char *f, u32 l, struct ipw_priv *ipw, u32 ofs)
{
	IPW_DEBUG_IO("%s %d: read_direct32(0x%08X)\n", f, l, (u32) (ofs));
J
James Ketrenos 已提交
223 224
	return _ipw_read32(ipw, ofs);
}
225

J
James Ketrenos 已提交
226 227 228 229 230 231 232
#define ipw_read32(ipw, ofs) __ipw_read32(__FILE__, __LINE__, ipw, ofs)

static void _ipw_read_indirect(struct ipw_priv *, u32, u8 *, int);
#define ipw_read_indirect(a, b, c, d) \
	IPW_DEBUG_IO("%s %d: read_inddirect(0x%08X) %d bytes\n", __FILE__, __LINE__, (u32)(b), d); \
	_ipw_read_indirect(a, b, c, d)

233 234
static void _ipw_write_indirect(struct ipw_priv *priv, u32 addr, u8 * data,
				int num);
J
James Ketrenos 已提交
235 236 237 238 239
#define ipw_write_indirect(a, b, c, d) \
	IPW_DEBUG_IO("%s %d: write_indirect(0x%08X) %d bytes\n", __FILE__, __LINE__, (u32)(b), d); \
        _ipw_write_indirect(a, b, c, d)

/* indirect write s */
240
static void _ipw_write_reg32(struct ipw_priv *priv, u32 reg, u32 value)
J
James Ketrenos 已提交
241
{
242
	IPW_DEBUG_IO(" %p : reg = 0x%8X : value = 0x%8X\n", priv, reg, value);
J
James Ketrenos 已提交
243 244 245 246 247 248 249 250 251
	_ipw_write32(priv, CX2_INDIRECT_ADDR, reg);
	_ipw_write32(priv, CX2_INDIRECT_DATA, value);
}

static void _ipw_write_reg8(struct ipw_priv *priv, u32 reg, u8 value)
{
	IPW_DEBUG_IO(" reg = 0x%8X : value = 0x%8X\n", reg, value);
	_ipw_write32(priv, CX2_INDIRECT_ADDR, reg & CX2_INDIRECT_ADDR_MASK);
	_ipw_write8(priv, CX2_INDIRECT_DATA, value);
252
	IPW_DEBUG_IO(" reg = 0x%8lX : value = 0x%8X\n",
253
		     (unsigned long)(priv->hw_base + CX2_INDIRECT_DATA), value);
J
James Ketrenos 已提交
254 255
}

256
static void _ipw_write_reg16(struct ipw_priv *priv, u32 reg, u16 value)
J
James Ketrenos 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270
{
	IPW_DEBUG_IO(" reg = 0x%8X : value = 0x%8X\n", reg, value);
	_ipw_write32(priv, CX2_INDIRECT_ADDR, reg & CX2_INDIRECT_ADDR_MASK);
	_ipw_write16(priv, CX2_INDIRECT_DATA, value);
}

/* indirect read s */

static u8 _ipw_read_reg8(struct ipw_priv *priv, u32 reg)
{
	u32 word;
	_ipw_write32(priv, CX2_INDIRECT_ADDR, reg & CX2_INDIRECT_ADDR_MASK);
	IPW_DEBUG_IO(" reg = 0x%8X : \n", reg);
	word = _ipw_read32(priv, CX2_INDIRECT_DATA);
271
	return (word >> ((reg & 0x3) * 8)) & 0xff;
J
James Ketrenos 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
}

static u32 _ipw_read_reg32(struct ipw_priv *priv, u32 reg)
{
	u32 value;

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

	_ipw_write32(priv, CX2_INDIRECT_ADDR, reg);
	value = _ipw_read32(priv, CX2_INDIRECT_DATA);
	IPW_DEBUG_IO(" reg = 0x%4X : value = 0x%4x \n", reg, value);
	return value;
}

/* iterative/auto-increment 32 bit reads and writes */
static void _ipw_read_indirect(struct ipw_priv *priv, u32 addr, u8 * buf,
			       int num)
{
	u32 aligned_addr = addr & CX2_INDIRECT_ADDR_MASK;
	u32 dif_len = addr - aligned_addr;
	u32 aligned_len;
	u32 i;
294

J
James Ketrenos 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	IPW_DEBUG_IO("addr = %i, buf = %p, num = %i\n", addr, buf, num);

	/* Read the first nibble byte by byte */
	if (unlikely(dif_len)) {
		/* Start reading at aligned_addr + dif_len */
		_ipw_write32(priv, CX2_INDIRECT_ADDR, aligned_addr);
		for (i = dif_len; i < 4; i++, buf++)
			*buf = _ipw_read8(priv, CX2_INDIRECT_DATA + i);
		num -= dif_len;
		aligned_addr += 4;
	}

	/* Read DWs through autoinc register */
	_ipw_write32(priv, CX2_AUTOINC_ADDR, aligned_addr);
	aligned_len = num & CX2_INDIRECT_ADDR_MASK;
	for (i = 0; i < aligned_len; i += 4, buf += 4, aligned_addr += 4)
311
		*(u32 *) buf = ipw_read32(priv, CX2_AUTOINC_DATA);
312

J
James Ketrenos 已提交
313 314 315 316 317 318 319
	/* Copy the last nibble */
	dif_len = num - aligned_len;
	_ipw_write32(priv, CX2_INDIRECT_ADDR, aligned_addr);
	for (i = 0; i < dif_len; i++, buf++)
		*buf = ipw_read8(priv, CX2_INDIRECT_DATA + i);
}

320
static void _ipw_write_indirect(struct ipw_priv *priv, u32 addr, u8 * buf,
J
James Ketrenos 已提交
321 322 323 324 325 326
				int num)
{
	u32 aligned_addr = addr & CX2_INDIRECT_ADDR_MASK;
	u32 dif_len = addr - aligned_addr;
	u32 aligned_len;
	u32 i;
327

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

J
James Ketrenos 已提交
330 331 332 333 334 335 336 337 338
	/* Write the first nibble byte by byte */
	if (unlikely(dif_len)) {
		/* Start writing at aligned_addr + dif_len */
		_ipw_write32(priv, CX2_INDIRECT_ADDR, aligned_addr);
		for (i = dif_len; i < 4; i++, buf++)
			_ipw_write8(priv, CX2_INDIRECT_DATA + i, *buf);
		num -= dif_len;
		aligned_addr += 4;
	}
339

J
James Ketrenos 已提交
340 341 342 343
	/* Write DWs through autoinc register */
	_ipw_write32(priv, CX2_AUTOINC_ADDR, aligned_addr);
	aligned_len = num & CX2_INDIRECT_ADDR_MASK;
	for (i = 0; i < aligned_len; i += 4, buf += 4, aligned_addr += 4)
344
		_ipw_write32(priv, CX2_AUTOINC_DATA, *(u32 *) buf);
345

J
James Ketrenos 已提交
346 347 348 349 350 351 352
	/* Copy the last nibble */
	dif_len = num - aligned_len;
	_ipw_write32(priv, CX2_INDIRECT_ADDR, aligned_addr);
	for (i = 0; i < dif_len; i++, buf++)
		_ipw_write8(priv, CX2_INDIRECT_DATA + i, *buf);
}

353
static void ipw_write_direct(struct ipw_priv *priv, u32 addr, void *buf,
J
James Ketrenos 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
			     int num)
{
	memcpy_toio((priv->hw_base + addr), buf, num);
}

static inline void ipw_set_bit(struct ipw_priv *priv, u32 reg, u32 mask)
{
	ipw_write32(priv, reg, ipw_read32(priv, reg) | mask);
}

static inline void ipw_clear_bit(struct ipw_priv *priv, u32 reg, u32 mask)
{
	ipw_write32(priv, reg, ipw_read32(priv, reg) & ~mask);
}

static inline void ipw_enable_interrupts(struct ipw_priv *priv)
{
	if (priv->status & STATUS_INT_ENABLED)
		return;
	priv->status |= STATUS_INT_ENABLED;
	ipw_write32(priv, CX2_INTA_MASK_R, CX2_INTA_MASK_ALL);
}

static inline void ipw_disable_interrupts(struct ipw_priv *priv)
{
	if (!(priv->status & STATUS_INT_ENABLED))
		return;
	priv->status &= ~STATUS_INT_ENABLED;
	ipw_write32(priv, CX2_INTA_MASK_R, ~CX2_INTA_MASK_ALL);
}

static char *ipw_error_desc(u32 val)
{
	switch (val) {
388
	case IPW_FW_ERROR_OK:
J
James Ketrenos 已提交
389
		return "ERROR_OK";
390
	case IPW_FW_ERROR_FAIL:
J
James Ketrenos 已提交
391
		return "ERROR_FAIL";
392
	case IPW_FW_ERROR_MEMORY_UNDERFLOW:
J
James Ketrenos 已提交
393
		return "MEMORY_UNDERFLOW";
394
	case IPW_FW_ERROR_MEMORY_OVERFLOW:
J
James Ketrenos 已提交
395
		return "MEMORY_OVERFLOW";
396
	case IPW_FW_ERROR_BAD_PARAM:
J
James Ketrenos 已提交
397
		return "ERROR_BAD_PARAM";
398
	case IPW_FW_ERROR_BAD_CHECKSUM:
J
James Ketrenos 已提交
399
		return "ERROR_BAD_CHECKSUM";
400
	case IPW_FW_ERROR_NMI_INTERRUPT:
J
James Ketrenos 已提交
401
		return "ERROR_NMI_INTERRUPT";
402
	case IPW_FW_ERROR_BAD_DATABASE:
J
James Ketrenos 已提交
403
		return "ERROR_BAD_DATABASE";
404
	case IPW_FW_ERROR_ALLOC_FAIL:
J
James Ketrenos 已提交
405
		return "ERROR_ALLOC_FAIL";
406
	case IPW_FW_ERROR_DMA_UNDERRUN:
J
James Ketrenos 已提交
407
		return "ERROR_DMA_UNDERRUN";
408
	case IPW_FW_ERROR_DMA_STATUS:
J
James Ketrenos 已提交
409
		return "ERROR_DMA_STATUS";
410
	case IPW_FW_ERROR_DINOSTATUS_ERROR:
J
James Ketrenos 已提交
411
		return "ERROR_DINOSTATUS_ERROR";
412
	case IPW_FW_ERROR_EEPROMSTATUS_ERROR:
J
James Ketrenos 已提交
413
		return "ERROR_EEPROMSTATUS_ERROR";
414
	case IPW_FW_ERROR_SYSASSERT:
J
James Ketrenos 已提交
415
		return "ERROR_SYSASSERT";
416
	case IPW_FW_ERROR_FATAL_ERROR:
J
James Ketrenos 已提交
417
		return "ERROR_FATALSTATUS_ERROR";
418
	default:
J
James Ketrenos 已提交
419 420 421 422 423 424 425 426 427 428
		return "UNKNOWNSTATUS_ERROR";
	}
}

static void ipw_dump_nic_error_log(struct ipw_priv *priv)
{
	u32 desc, time, blink1, blink2, ilink1, ilink2, idata, i, count, base;

	base = ipw_read32(priv, IPWSTATUS_ERROR_LOG);
	count = ipw_read_reg32(priv, base);
429

J
James Ketrenos 已提交
430 431 432 433 434 435
	if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
		IPW_ERROR("Start IPW Error Log Dump:\n");
		IPW_ERROR("Status: 0x%08X, Config: %08X\n",
			  priv->status, priv->config);
	}

436
	for (i = ERROR_START_OFFSET;
437 438 439 440 441 442 443 444
	     i <= count * ERROR_ELEM_SIZE; i += ERROR_ELEM_SIZE) {
		desc = ipw_read_reg32(priv, base + i);
		time = ipw_read_reg32(priv, base + i + 1 * sizeof(u32));
		blink1 = ipw_read_reg32(priv, base + i + 2 * sizeof(u32));
		blink2 = ipw_read_reg32(priv, base + i + 3 * sizeof(u32));
		ilink1 = ipw_read_reg32(priv, base + i + 4 * sizeof(u32));
		ilink2 = ipw_read_reg32(priv, base + i + 5 * sizeof(u32));
		idata = ipw_read_reg32(priv, base + i + 6 * sizeof(u32));
J
James Ketrenos 已提交
445

446 447 448
		IPW_ERROR("%s %i 0x%08x  0x%08x  0x%08x  0x%08x  0x%08x\n",
			  ipw_error_desc(desc), time, blink1, blink2,
			  ilink1, ilink2, idata);
J
James Ketrenos 已提交
449 450 451 452 453 454 455 456 457
	}
}

static void ipw_dump_nic_event_log(struct ipw_priv *priv)
{
	u32 ev, time, data, i, count, base;

	base = ipw_read32(priv, IPW_EVENT_LOG);
	count = ipw_read_reg32(priv, base);
458

J
James Ketrenos 已提交
459 460 461
	if (EVENT_START_OFFSET <= count * EVENT_ELEM_SIZE)
		IPW_ERROR("Start IPW Event Log Dump:\n");

462
	for (i = EVENT_START_OFFSET;
463
	     i <= count * EVENT_ELEM_SIZE; i += EVENT_ELEM_SIZE) {
J
James Ketrenos 已提交
464
		ev = ipw_read_reg32(priv, base + i);
465 466
		time = ipw_read_reg32(priv, base + i + 1 * sizeof(u32));
		data = ipw_read_reg32(priv, base + i + 2 * sizeof(u32));
J
James Ketrenos 已提交
467 468 469 470 471 472 473

#ifdef CONFIG_IPW_DEBUG
		IPW_ERROR("%i\t0x%08x\t%i\n", time, data, ev);
#endif
	}
}

474
static int ipw_get_ordinal(struct ipw_priv *priv, u32 ord, void *val, u32 * len)
J
James Ketrenos 已提交
475 476 477 478 479 480 481 482 483
{
	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;
	}
484

J
James Ketrenos 已提交
485 486 487 488 489 490 491 492 493 494 495
	/* 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
		 *
496
		 * This is a very simple table with the data directly
J
James Ketrenos 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
		 * 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, "
513
				      "need %zd\n", sizeof(u32));
J
James Ketrenos 已提交
514 515 516 517
			return -EINVAL;
		}

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

		*len = sizeof(u32);
		ord <<= 2;
522
		*((u32 *) val) = ipw_read32(priv, priv->table0_addr + ord);
J
James Ketrenos 已提交
523 524 525 526 527
		break;

	case IPW_ORD_TABLE_1_MASK:
		/*
		 * TABLE 1: Indirect access to a table of 32 bit values
528 529
		 *
		 * This is a fairly large table of u32 values each
J
James Ketrenos 已提交
530 531 532 533 534 535
		 * representing starting addr for the data (which is
		 * also a u32)
		 */

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

J
James Ketrenos 已提交
537 538 539 540 541 542 543 544 545
		/* 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, "
546
				      "need %zd\n", sizeof(u32));
J
James Ketrenos 已提交
547 548 549
			return -EINVAL;
		}

550 551
		*((u32 *) val) =
		    ipw_read_reg32(priv, (priv->table1_addr + (ord << 2)));
J
James Ketrenos 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
		*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));
576 577

		/* get the second DW of statistics ;
J
James Ketrenos 已提交
578
		 * two 16-bit words - first is length, second is count */
579 580 581 582
		field_info =
		    ipw_read_reg32(priv,
				   priv->table2_addr + (ord << 3) +
				   sizeof(u32));
583

J
James Ketrenos 已提交
584
		/* get each entry length */
585
		field_len = *((u16 *) & field_info);
586

J
James Ketrenos 已提交
587
		/* get number of entries */
588
		field_count = *(((u16 *) & field_info) + 1);
589

J
James Ketrenos 已提交
590 591 592 593 594 595
		/* abort if not enought memory */
		total_len = field_len * field_count;
		if (total_len > *len) {
			*len = total_len;
			return -EINVAL;
		}
596

J
James Ketrenos 已提交
597 598 599 600 601
		*len = total_len;
		if (!total_len)
			return 0;

		IPW_DEBUG_ORD("addr = 0x%08x, total_len = %i, "
602
			      "field_info = 0x%08x\n",
J
James Ketrenos 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
			      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;
619
	priv->table0_len = ipw_read32(priv, priv->table0_addr);
J
James Ketrenos 已提交
620 621 622 623 624 625 626 627 628 629 630 631

	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);
632
	priv->table2_len &= 0x0000ffff;	/* use first two bytes */
J
James Ketrenos 已提交
633 634 635 636 637 638 639 640 641 642

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

}

/*
 * 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.
643
 *
J
James Ketrenos 已提交
644 645 646 647 648 649
 * 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);
}
650
static ssize_t store_debug_level(struct device_driver *d,
651
				 const char *buf, size_t count)
J
James Ketrenos 已提交
652 653 654 655 656 657 658 659 660 661 662
{
	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);
663 664
	if (p == buf)
		printk(KERN_INFO DRV_NAME
J
James Ketrenos 已提交
665 666 667 668 669 670 671
		       ": %s is not in hex or decimal form.\n", buf);
	else
		ipw_debug_level = val;

	return strnlen(buf, count);
}

672
static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
J
James Ketrenos 已提交
673 674
		   show_debug_level, store_debug_level);

675
static ssize_t show_status(struct device *d,
676
			   struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
677
{
678
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
679 680
	return sprintf(buf, "0x%08x\n", (int)p->status);
}
681

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

684 685
static ssize_t show_cfg(struct device *d, struct device_attribute *attr,
			char *buf)
J
James Ketrenos 已提交
686
{
687
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
688 689
	return sprintf(buf, "0x%08x\n", (int)p->config);
}
690

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

693
static ssize_t show_nic_type(struct device *d,
694
			     struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
695
{
696
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710
	u8 type = p->eeprom[EEPROM_NIC_TYPE];

	switch (type) {
	case EEPROM_NIC_TYPE_STANDARD:
		return sprintf(buf, "STANDARD\n");
	case EEPROM_NIC_TYPE_DELL:
		return sprintf(buf, "DELL\n");
	case EEPROM_NIC_TYPE_FUJITSU:
		return sprintf(buf, "FUJITSU\n");
	case EEPROM_NIC_TYPE_IBM:
		return sprintf(buf, "IBM\n");
	case EEPROM_NIC_TYPE_HP:
		return sprintf(buf, "HP\n");
	}
711

J
James Ketrenos 已提交
712 713
	return sprintf(buf, "UNKNOWN\n");
}
714

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

717
static ssize_t dump_error_log(struct device *d,
718 719
			      struct device_attribute *attr, const char *buf,
			      size_t count)
J
James Ketrenos 已提交
720 721 722
{
	char *p = (char *)buf;

723
	if (p[0] == '1')
724
		ipw_dump_nic_error_log((struct ipw_priv *)d->driver_data);
J
James Ketrenos 已提交
725 726 727

	return strnlen(buf, count);
}
728

J
James Ketrenos 已提交
729 730
static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);

731
static ssize_t dump_event_log(struct device *d,
732 733
			      struct device_attribute *attr, const char *buf,
			      size_t count)
J
James Ketrenos 已提交
734 735 736
{
	char *p = (char *)buf;

737
	if (p[0] == '1')
738
		ipw_dump_nic_event_log((struct ipw_priv *)d->driver_data);
J
James Ketrenos 已提交
739 740 741

	return strnlen(buf, count);
}
742

J
James Ketrenos 已提交
743 744
static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);

745
static ssize_t show_ucode_version(struct device *d,
746
				  struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
747 748
{
	u32 len = sizeof(u32), tmp = 0;
749
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
750

751
	if (ipw_get_ordinal(p, IPW_ORD_STAT_UCODE_VERSION, &tmp, &len))
J
James Ketrenos 已提交
752 753 754 755
		return 0;

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

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

759 760
static ssize_t show_rtc(struct device *d, struct device_attribute *attr,
			char *buf)
J
James Ketrenos 已提交
761 762
{
	u32 len = sizeof(u32), tmp = 0;
763
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
764

765
	if (ipw_get_ordinal(p, IPW_ORD_STAT_RTC, &tmp, &len))
J
James Ketrenos 已提交
766 767 768 769
		return 0;

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

static DEVICE_ATTR(rtc, S_IWUSR | S_IRUGO, show_rtc, NULL);
J
James Ketrenos 已提交
772 773 774 775 776

/*
 * Add a device attribute to view/control the delay between eeprom
 * operations.
 */
777
static ssize_t show_eeprom_delay(struct device *d,
778
				 struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
779
{
780
	int n = ((struct ipw_priv *)d->driver_data)->eeprom_delay;
J
James Ketrenos 已提交
781 782
	return sprintf(buf, "%i\n", n);
}
783
static ssize_t store_eeprom_delay(struct device *d,
784 785
				  struct device_attribute *attr,
				  const char *buf, size_t count)
J
James Ketrenos 已提交
786
{
787
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
788 789 790
	sscanf(buf, "%i", &p->eeprom_delay);
	return strnlen(buf, count);
}
791 792 793

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

795
static ssize_t show_command_event_reg(struct device *d,
796
				      struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
797 798
{
	u32 reg = 0;
799
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
800 801 802 803

	reg = ipw_read_reg32(p, CX2_INTERNAL_CMD_EVENT);
	return sprintf(buf, "0x%08x\n", reg);
}
804
static ssize_t store_command_event_reg(struct device *d,
805 806
				       struct device_attribute *attr,
				       const char *buf, size_t count)
J
James Ketrenos 已提交
807 808
{
	u32 reg;
809
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
810 811 812 813 814

	sscanf(buf, "%x", &reg);
	ipw_write_reg32(p, CX2_INTERNAL_CMD_EVENT, reg);
	return strnlen(buf, count);
}
815 816 817

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

819
static ssize_t show_mem_gpio_reg(struct device *d,
820
				 struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
821 822
{
	u32 reg = 0;
823
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
824 825 826 827

	reg = ipw_read_reg32(p, 0x301100);
	return sprintf(buf, "0x%08x\n", reg);
}
828
static ssize_t store_mem_gpio_reg(struct device *d,
829 830
				  struct device_attribute *attr,
				  const char *buf, size_t count)
J
James Ketrenos 已提交
831 832
{
	u32 reg;
833
	struct ipw_priv *p = d->driver_data;
J
James Ketrenos 已提交
834 835 836 837 838

	sscanf(buf, "%x", &reg);
	ipw_write_reg32(p, 0x301100, reg);
	return strnlen(buf, count);
}
839 840 841

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

843
static ssize_t show_indirect_dword(struct device *d,
844
				   struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
845 846
{
	u32 reg = 0;
847
	struct ipw_priv *priv = d->driver_data;
848
	if (priv->status & STATUS_INDIRECT_DWORD)
J
James Ketrenos 已提交
849
		reg = ipw_read_reg32(priv, priv->indirect_dword);
850
	else
J
James Ketrenos 已提交
851
		reg = 0;
852

J
James Ketrenos 已提交
853 854
	return sprintf(buf, "0x%08x\n", reg);
}
855
static ssize_t store_indirect_dword(struct device *d,
856 857
				    struct device_attribute *attr,
				    const char *buf, size_t count)
J
James Ketrenos 已提交
858
{
859
	struct ipw_priv *priv = d->driver_data;
J
James Ketrenos 已提交
860 861 862 863 864

	sscanf(buf, "%x", &priv->indirect_dword);
	priv->status |= STATUS_INDIRECT_DWORD;
	return strnlen(buf, count);
}
865 866 867

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

869
static ssize_t show_indirect_byte(struct device *d,
870
				  struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
871 872
{
	u8 reg = 0;
873
	struct ipw_priv *priv = d->driver_data;
874
	if (priv->status & STATUS_INDIRECT_BYTE)
J
James Ketrenos 已提交
875
		reg = ipw_read_reg8(priv, priv->indirect_byte);
876
	else
J
James Ketrenos 已提交
877 878 879 880
		reg = 0;

	return sprintf(buf, "0x%02x\n", reg);
}
881
static ssize_t store_indirect_byte(struct device *d,
882 883
				   struct device_attribute *attr,
				   const char *buf, size_t count)
J
James Ketrenos 已提交
884
{
885
	struct ipw_priv *priv = d->driver_data;
J
James Ketrenos 已提交
886 887 888 889 890

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

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

895
static ssize_t show_direct_dword(struct device *d,
896
				 struct device_attribute *attr, char *buf)
J
James Ketrenos 已提交
897 898
{
	u32 reg = 0;
899
	struct ipw_priv *priv = d->driver_data;
J
James Ketrenos 已提交
900

901
	if (priv->status & STATUS_DIRECT_DWORD)
J
James Ketrenos 已提交
902
		reg = ipw_read32(priv, priv->direct_dword);
903
	else
J
James Ketrenos 已提交
904 905 906 907
		reg = 0;

	return sprintf(buf, "0x%08x\n", reg);
}
908
static ssize_t store_direct_dword(struct device *d,
909 910
				  struct device_attribute *attr,
				  const char *buf, size_t count)
J
James Ketrenos 已提交
911
{
912
	struct ipw_priv *priv = d->driver_data;
J
James Ketrenos 已提交
913 914 915 916 917 918

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

919 920
static DEVICE_ATTR(direct_dword, S_IWUSR | S_IRUGO,
		   show_direct_dword, store_direct_dword);
J
James Ketrenos 已提交
921 922 923 924 925 926 927 928 929 930 931

static inline int rf_kill_active(struct ipw_priv *priv)
{
	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;
}

932
static ssize_t show_rf_kill(struct device *d, struct device_attribute *attr,
933
			    char *buf)
J
James Ketrenos 已提交
934 935
{
	/* 0 - RF kill not enabled
936
	   1 - SW based RF kill active (sysfs)
J
James Ketrenos 已提交
937 938
	   2 - HW based RF kill active
	   3 - Both HW and SW baed RF kill active */
939
	struct ipw_priv *priv = d->driver_data;
J
James Ketrenos 已提交
940
	int val = ((priv->status & STATUS_RF_KILL_SW) ? 0x1 : 0x0) |
941
	    (rf_kill_active(priv) ? 0x2 : 0x0);
J
James Ketrenos 已提交
942 943 944 945 946
	return sprintf(buf, "%i\n", val);
}

static int ipw_radio_kill_sw(struct ipw_priv *priv, int disable_radio)
{
947
	if ((disable_radio ? 1 : 0) ==
J
James Ketrenos 已提交
948
	    (priv->status & STATUS_RF_KILL_SW ? 1 : 0))
949
		return 0;
J
James Ketrenos 已提交
950 951 952 953 954 955 956

	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;

957
		if (priv->workqueue) {
J
James Ketrenos 已提交
958 959 960 961 962 963 964 965 966 967 968
			cancel_delayed_work(&priv->request_scan);
		}
		wake_up_interruptible(&priv->wait_command_queue);
		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);
969
			queue_delayed_work(priv->workqueue, &priv->rf_kill,
J
James Ketrenos 已提交
970
					   2 * HZ);
971
		} else
J
James Ketrenos 已提交
972 973 974 975 976 977
			queue_work(priv->workqueue, &priv->up);
	}

	return 1;
}

978 979
static ssize_t store_rf_kill(struct device *d, struct device_attribute *attr,
			     const char *buf, size_t count)
J
James Ketrenos 已提交
980
{
981
	struct ipw_priv *priv = d->driver_data;
982

J
James Ketrenos 已提交
983 984 985 986
	ipw_radio_kill_sw(priv, buf[0] == '1');

	return count;
}
987 988

static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
J
James Ketrenos 已提交
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012

static void ipw_irq_tasklet(struct ipw_priv *priv)
{
	u32 inta, inta_mask, handled = 0;
	unsigned long flags;
	int rc = 0;

	spin_lock_irqsave(&priv->lock, flags);

	inta = ipw_read32(priv, CX2_INTA_RW);
	inta_mask = ipw_read32(priv, CX2_INTA_MASK_R);
	inta &= (CX2_INTA_MASK_ALL & inta_mask);

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

	/* handle all the justifications for the interrupt */
	if (inta & CX2_INTA_BIT_RX_TRANSFER) {
		ipw_rx(priv);
		handled |= CX2_INTA_BIT_RX_TRANSFER;
	}

	if (inta & CX2_INTA_BIT_TX_CMD_QUEUE) {
		IPW_DEBUG_HC("Command completed.\n");
1013
		rc = ipw_queue_tx_reclaim(priv, &priv->txq_cmd, -1);
J
James Ketrenos 已提交
1014 1015 1016 1017 1018 1019 1020
		priv->status &= ~STATUS_HCMD_ACTIVE;
		wake_up_interruptible(&priv->wait_command_queue);
		handled |= CX2_INTA_BIT_TX_CMD_QUEUE;
	}

	if (inta & CX2_INTA_BIT_TX_QUEUE_1) {
		IPW_DEBUG_TX("TX_QUEUE_1\n");
1021
		rc = ipw_queue_tx_reclaim(priv, &priv->txq[0], 0);
J
James Ketrenos 已提交
1022 1023 1024 1025 1026
		handled |= CX2_INTA_BIT_TX_QUEUE_1;
	}

	if (inta & CX2_INTA_BIT_TX_QUEUE_2) {
		IPW_DEBUG_TX("TX_QUEUE_2\n");
1027
		rc = ipw_queue_tx_reclaim(priv, &priv->txq[1], 1);
J
James Ketrenos 已提交
1028 1029 1030 1031 1032
		handled |= CX2_INTA_BIT_TX_QUEUE_2;
	}

	if (inta & CX2_INTA_BIT_TX_QUEUE_3) {
		IPW_DEBUG_TX("TX_QUEUE_3\n");
1033
		rc = ipw_queue_tx_reclaim(priv, &priv->txq[2], 2);
J
James Ketrenos 已提交
1034 1035 1036 1037 1038
		handled |= CX2_INTA_BIT_TX_QUEUE_3;
	}

	if (inta & CX2_INTA_BIT_TX_QUEUE_4) {
		IPW_DEBUG_TX("TX_QUEUE_4\n");
1039
		rc = ipw_queue_tx_reclaim(priv, &priv->txq[3], 3);
J
James Ketrenos 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
		handled |= CX2_INTA_BIT_TX_QUEUE_4;
	}

	if (inta & CX2_INTA_BIT_STATUS_CHANGE) {
		IPW_WARNING("STATUS_CHANGE\n");
		handled |= CX2_INTA_BIT_STATUS_CHANGE;
	}

	if (inta & CX2_INTA_BIT_BEACON_PERIOD_EXPIRED) {
		IPW_WARNING("TX_PERIOD_EXPIRED\n");
		handled |= CX2_INTA_BIT_BEACON_PERIOD_EXPIRED;
	}

	if (inta & CX2_INTA_BIT_SLAVE_MODE_HOST_CMD_DONE) {
		IPW_WARNING("HOST_CMD_DONE\n");
		handled |= CX2_INTA_BIT_SLAVE_MODE_HOST_CMD_DONE;
	}

	if (inta & CX2_INTA_BIT_FW_INITIALIZATION_DONE) {
		IPW_WARNING("FW_INITIALIZATION_DONE\n");
		handled |= CX2_INTA_BIT_FW_INITIALIZATION_DONE;
	}

	if (inta & CX2_INTA_BIT_FW_CARD_DISABLE_PHY_OFF_DONE) {
		IPW_WARNING("PHY_OFF_DONE\n");
		handled |= CX2_INTA_BIT_FW_CARD_DISABLE_PHY_OFF_DONE;
	}

	if (inta & CX2_INTA_BIT_RF_KILL_DONE) {
		IPW_DEBUG_RF_KILL("RF_KILL_DONE\n");
		priv->status |= STATUS_RF_KILL_HW;
		wake_up_interruptible(&priv->wait_command_queue);
		netif_carrier_off(priv->net_dev);
		netif_stop_queue(priv->net_dev);
		cancel_delayed_work(&priv->request_scan);
		queue_delayed_work(priv->workqueue, &priv->rf_kill, 2 * HZ);
		handled |= CX2_INTA_BIT_RF_KILL_DONE;
	}
1078

J
James Ketrenos 已提交
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	if (inta & CX2_INTA_BIT_FATAL_ERROR) {
		IPW_ERROR("Firmware error detected.  Restarting.\n");
#ifdef CONFIG_IPW_DEBUG
		if (ipw_debug_level & IPW_DL_FW_ERRORS) {
			ipw_dump_nic_error_log(priv);
			ipw_dump_nic_event_log(priv);
		}
#endif
		queue_work(priv->workqueue, &priv->adapter_restart);
		handled |= CX2_INTA_BIT_FATAL_ERROR;
	}

	if (inta & CX2_INTA_BIT_PARITY_ERROR) {
		IPW_ERROR("Parity error\n");
		handled |= CX2_INTA_BIT_PARITY_ERROR;
	}

	if (handled != inta) {
1097
		IPW_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
J
James Ketrenos 已提交
1098 1099 1100 1101 1102 1103 1104
	}

	/* enable all interrupts */
	ipw_enable_interrupts(priv);

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

J
James Ketrenos 已提交
1106 1107 1108 1109 1110 1111
#ifdef CONFIG_IPW_DEBUG
#define IPW_CMD(x) case IPW_CMD_ ## x : return #x
static char *get_cmd_string(u8 cmd)
{
	switch (cmd) {
		IPW_CMD(HOST_COMPLETE);
1112 1113 1114 1115 1116 1117 1118 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 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
		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 已提交
1162 1163 1164
		return "UNKNOWN";
	}
}
1165
#endif				/* CONFIG_IPW_DEBUG */
J
James Ketrenos 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177

#define HOST_COMPLETE_TIMEOUT HZ
static int ipw_send_cmd(struct ipw_priv *priv, struct host_cmd *cmd)
{
	int rc = 0;

	if (priv->status & STATUS_HCMD_ACTIVE) {
		IPW_ERROR("Already sending a command\n");
		return -1;
	}

	priv->status |= STATUS_HCMD_ACTIVE;
1178 1179

	IPW_DEBUG_HC("Sending %s command (#%d), %d bytes\n",
J
James Ketrenos 已提交
1180
		     get_cmd_string(cmd->cmd), cmd->cmd, cmd->len);
1181
	printk_buf(IPW_DL_HOST_COMMAND, (u8 *) cmd->param, cmd->len);
J
James Ketrenos 已提交
1182 1183 1184 1185 1186

	rc = ipw_queue_tx_hcmd(priv, cmd->cmd, &cmd->param, cmd->len, 0);
	if (rc)
		return rc;

1187 1188 1189 1190
	rc = wait_event_interruptible_timeout(priv->wait_command_queue,
					      !(priv->
						status & STATUS_HCMD_ACTIVE),
					      HOST_COMPLETE_TIMEOUT);
J
James Ketrenos 已提交
1191 1192
	if (rc == 0) {
		IPW_DEBUG_INFO("Command completion failed out after %dms.\n",
1193
			       jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
J
James Ketrenos 已提交
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
		priv->status &= ~STATUS_HCMD_ACTIVE;
		return -EIO;
	}
	if (priv->status & STATUS_RF_KILL_MASK) {
		IPW_DEBUG_INFO("Command aborted due to RF Kill Switch\n");
		return -EIO;
	}

	return 0;
}

static int ipw_send_host_complete(struct ipw_priv *priv)
{
	struct host_cmd cmd = {
		.cmd = IPW_CMD_HOST_COMPLETE,
		.len = 0
	};

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

	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send HOST_COMPLETE command\n");
		return -1;
	}
1221

J
James Ketrenos 已提交
1222 1223 1224
	return 0;
}

1225
static int ipw_send_system_config(struct ipw_priv *priv,
J
James Ketrenos 已提交
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
				  struct ipw_sys_config *config)
{
	struct host_cmd cmd = {
		.cmd = IPW_CMD_SYSTEM_CONFIG,
		.len = sizeof(*config)
	};

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

1238
	memcpy(&cmd.param, config, sizeof(*config));
J
James Ketrenos 已提交
1239 1240 1241 1242 1243 1244 1245 1246
	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send SYSTEM_CONFIG command\n");
		return -1;
	}

	return 0;
}

1247
static int ipw_send_ssid(struct ipw_priv *priv, u8 * ssid, int len)
J
James Ketrenos 已提交
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
{
	struct host_cmd cmd = {
		.cmd = IPW_CMD_SSID,
		.len = min(len, IW_ESSID_MAX_SIZE)
	};

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

	memcpy(&cmd.param, ssid, cmd.len);
	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send SSID command\n");
		return -1;
	}
1264

J
James Ketrenos 已提交
1265 1266 1267
	return 0;
}

1268
static int ipw_send_adapter_address(struct ipw_priv *priv, u8 * mac)
J
James Ketrenos 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
{
	struct host_cmd cmd = {
		.cmd = IPW_CMD_ADAPTER_ADDRESS,
		.len = ETH_ALEN
	};

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

	IPW_DEBUG_INFO("%s: Setting MAC to " MAC_FMT "\n",
		       priv->net_dev->name, MAC_ARG(mac));

	memcpy(&cmd.param, mac, ETH_ALEN);

	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send ADAPTER_ADDRESS command\n");
		return -1;
	}
1289

J
James Ketrenos 已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
	return 0;
}

static void ipw_adapter_restart(void *adapter)
{
	struct ipw_priv *priv = adapter;

	if (priv->status & STATUS_RF_KILL_MASK)
		return;

	ipw_down(priv);
	if (ipw_up(priv)) {
		IPW_ERROR("Failed to up device\n");
		return;
	}
}

#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 "
1314
			       "adapter (%dms).\n",
J
James Ketrenos 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
			       IPW_SCAN_CHECK_WATCHDOG / 100);
		ipw_adapter_restart(priv);
	}
}

static int ipw_send_scan_request_ext(struct ipw_priv *priv,
				     struct ipw_scan_request_ext *request)
{
	struct host_cmd cmd = {
		.cmd = IPW_CMD_SCAN_REQUEST_EXT,
		.len = sizeof(*request)
	};

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

1333
	memcpy(&cmd.param, request, sizeof(*request));
J
James Ketrenos 已提交
1334 1335 1336 1337
	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send SCAN_REQUEST_EXT command\n");
		return -1;
	}
1338 1339

	queue_delayed_work(priv->workqueue, &priv->scan_check,
J
James Ketrenos 已提交
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
			   IPW_SCAN_CHECK_WATCHDOG);
	return 0;
}

static int ipw_send_scan_abort(struct ipw_priv *priv)
{
	struct host_cmd cmd = {
		.cmd = IPW_CMD_SCAN_ABORT,
		.len = 0
	};

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

	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send SCAN_ABORT command\n");
		return -1;
	}
1360

J
James Ketrenos 已提交
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
	return 0;
}

static int ipw_set_sensitivity(struct ipw_priv *priv, u16 sens)
{
	struct host_cmd cmd = {
		.cmd = IPW_CMD_SENSITIVITY_CALIB,
		.len = sizeof(struct ipw_sensitivity_calib)
	};
	struct ipw_sensitivity_calib *calib = (struct ipw_sensitivity_calib *)
1371
	    &cmd.param;
J
James Ketrenos 已提交
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
	calib->beacon_rssi_raw = sens;
	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send SENSITIVITY CALIB command\n");
		return -1;
	}

	return 0;
}

static int ipw_send_associate(struct ipw_priv *priv,
			      struct ipw_associate *associate)
{
	struct host_cmd cmd = {
		.cmd = IPW_CMD_ASSOCIATE,
		.len = sizeof(*associate)
	};

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

1394
	memcpy(&cmd.param, associate, sizeof(*associate));
J
James Ketrenos 已提交
1395 1396 1397 1398
	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send ASSOCIATE command\n");
		return -1;
	}
1399

J
James Ketrenos 已提交
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
	return 0;
}

static int ipw_send_supported_rates(struct ipw_priv *priv,
				    struct ipw_supported_rates *rates)
{
	struct host_cmd cmd = {
		.cmd = IPW_CMD_SUPPORTED_RATES,
		.len = sizeof(*rates)
	};

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

1416
	memcpy(&cmd.param, rates, sizeof(*rates));
J
James Ketrenos 已提交
1417 1418 1419 1420
	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send SUPPORTED_RATES command\n");
		return -1;
	}
1421

J
James Ketrenos 已提交
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
	return 0;
}

static int ipw_set_random_seed(struct ipw_priv *priv)
{
	struct host_cmd cmd = {
		.cmd = IPW_CMD_SEED_NUMBER,
		.len = sizeof(u32)
	};

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

	get_random_bytes(&cmd.param, sizeof(u32));

	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send SEED_NUMBER command\n");
		return -1;
	}
1443

J
James Ketrenos 已提交
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
	return 0;
}

#if 0
static int ipw_send_card_disable(struct ipw_priv *priv, u32 phy_off)
{
	struct host_cmd cmd = {
		.cmd = IPW_CMD_CARD_DISABLE,
		.len = sizeof(u32)
	};

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

1460
	*((u32 *) & cmd.param) = phy_off;
J
James Ketrenos 已提交
1461 1462 1463 1464 1465

	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send CARD_DISABLE command\n");
		return -1;
	}
1466

J
James Ketrenos 已提交
1467 1468 1469 1470
	return 0;
}
#endif

1471
static int ipw_send_tx_power(struct ipw_priv *priv, struct ipw_tx_power *power)
J
James Ketrenos 已提交
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
{
	struct host_cmd cmd = {
		.cmd = IPW_CMD_TX_POWER,
		.len = sizeof(*power)
	};

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

1483
	memcpy(&cmd.param, power, sizeof(*power));
J
James Ketrenos 已提交
1484 1485 1486 1487
	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send TX_POWER command\n");
		return -1;
	}
1488

J
James Ketrenos 已提交
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 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
	return 0;
}

static int ipw_send_rts_threshold(struct ipw_priv *priv, u16 rts)
{
	struct ipw_rts_threshold rts_threshold = {
		.rts_threshold = rts,
	};
	struct host_cmd cmd = {
		.cmd = IPW_CMD_RTS_THRESHOLD,
		.len = sizeof(rts_threshold)
	};

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

	memcpy(&cmd.param, &rts_threshold, sizeof(rts_threshold));
	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send RTS_THRESHOLD command\n");
		return -1;
	}

	return 0;
}

static int ipw_send_frag_threshold(struct ipw_priv *priv, u16 frag)
{
	struct ipw_frag_threshold frag_threshold = {
		.frag_threshold = frag,
	};
	struct host_cmd cmd = {
		.cmd = IPW_CMD_FRAG_THRESHOLD,
		.len = sizeof(frag_threshold)
	};

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

	memcpy(&cmd.param, &frag_threshold, sizeof(frag_threshold));
	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send FRAG_THRESHOLD command\n");
		return -1;
	}

	return 0;
}

static int ipw_send_power_mode(struct ipw_priv *priv, u32 mode)
{
	struct host_cmd cmd = {
		.cmd = IPW_CMD_POWER_MODE,
		.len = sizeof(u32)
	};
1546
	u32 *param = (u32 *) (&cmd.param);
J
James Ketrenos 已提交
1547 1548 1549 1550 1551

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

J
James Ketrenos 已提交
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
	/* If on battery, set to 3, if AC set to CAM, else user
	 * level */
	switch (mode) {
	case IPW_POWER_BATTERY:
		*param = IPW_POWER_INDEX_3;
		break;
	case IPW_POWER_AC:
		*param = IPW_POWER_MODE_CAM;
		break;
	default:
		*param = mode;
		break;
	}

	if (ipw_send_cmd(priv, &cmd)) {
		IPW_ERROR("failed to send POWER_MODE command\n");
		return -1;
	}

	return 0;
}

/*
 * 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);
1596

J
James Ketrenos 已提交
1597 1598 1599 1600 1601 1602 1603
	/* the eeprom requires some time to complete the operation */
	udelay(p->eeprom_delay);

	return;
}

/* perform a chip select operation */
1604
static inline void eeprom_cs(struct ipw_priv *priv)
J
James Ketrenos 已提交
1605
{
1606 1607 1608 1609
	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 已提交
1610 1611 1612
}

/* perform a chip select operation */
1613
static inline void eeprom_disable_cs(struct ipw_priv *priv)
J
James Ketrenos 已提交
1614
{
1615 1616 1617
	eeprom_write_reg(priv, EEPROM_BIT_CS);
	eeprom_write_reg(priv, 0);
	eeprom_write_reg(priv, EEPROM_BIT_SK);
J
James Ketrenos 已提交
1618 1619 1620
}

/* push a single bit down to the eeprom */
1621
static inline void eeprom_write_bit(struct ipw_priv *p, u8 bit)
J
James Ketrenos 已提交
1622
{
1623 1624 1625
	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 已提交
1626 1627 1628
}

/* push an opcode followed by an address down to the eeprom */
1629
static void eeprom_op(struct ipw_priv *priv, u8 op, u8 addr)
J
James Ketrenos 已提交
1630 1631 1632 1633
{
	int i;

	eeprom_cs(priv);
1634 1635 1636 1637 1638
	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 已提交
1639 1640 1641 1642
	}
}

/* pull 16 bits off the eeprom, one bit at a time */
1643
static u16 eeprom_read_u16(struct ipw_priv *priv, u8 addr)
J
James Ketrenos 已提交
1644 1645
{
	int i;
1646
	u16 r = 0;
1647

J
James Ketrenos 已提交
1648
	/* Send READ Opcode */
1649
	eeprom_op(priv, EEPROM_CMD_READ, addr);
J
James Ketrenos 已提交
1650 1651

	/* Send dummy bit */
1652
	eeprom_write_reg(priv, EEPROM_BIT_CS);
J
James Ketrenos 已提交
1653 1654

	/* Read the byte off the eeprom one bit at a time */
1655
	for (i = 0; i < 16; i++) {
J
James Ketrenos 已提交
1656
		u32 data = 0;
1657 1658 1659 1660
		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 已提交
1661
	}
1662

J
James Ketrenos 已提交
1663
	/* Send another dummy bit */
1664
	eeprom_write_reg(priv, 0);
J
James Ketrenos 已提交
1665
	eeprom_disable_cs(priv);
1666

J
James Ketrenos 已提交
1667 1668 1669 1670 1671
	return r;
}

/* helper function for pulling the mac address out of the private */
/* data's copy of the eeprom data                                 */
1672
static void eeprom_parse_mac(struct ipw_priv *priv, u8 * mac)
J
James Ketrenos 已提交
1673
{
1674
	u8 *ee = (u8 *) priv->eeprom;
J
James Ketrenos 已提交
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
	memcpy(mac, &ee[EEPROM_MAC_ADDRESS], 6);
}

/*
 * 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;
1689
	u16 *eeprom = (u16 *) priv->eeprom;
1690

J
James Ketrenos 已提交
1691 1692 1693
	IPW_DEBUG_TRACE(">>\n");

	/* read entire contents of eeprom into private buffer */
1694 1695
	for (i = 0; i < 128; i++)
		eeprom[i] = eeprom_read_u16(priv, (u8) i);
J
James Ketrenos 已提交
1696

1697 1698
	/*
	   If the data looks correct, then copy it to our private
J
James Ketrenos 已提交
1699 1700
	   copy.  Otherwise let the firmware know to perform the operation
	   on it's own
1701
	 */
J
James Ketrenos 已提交
1702 1703 1704 1705
	if ((priv->eeprom + EEPROM_VERSION) != 0) {
		IPW_DEBUG_INFO("Writing EEPROM data into SRAM\n");

		/* write the eeprom data to sram */
1706 1707
		for (i = 0; i < CX2_EEPROM_IMAGE_SIZE; i++)
			ipw_write8(priv, IPW_EEPROM_DATA + i, priv->eeprom[i]);
J
James Ketrenos 已提交
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723

		/* 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");
}

static inline void ipw_zero_memory(struct ipw_priv *priv, u32 start, u32 count)
{
	count >>= 2;
1724 1725
	if (!count)
		return;
J
James Ketrenos 已提交
1726
	_ipw_write32(priv, CX2_AUTOINC_ADDR, start);
1727
	while (count--)
J
James Ketrenos 已提交
1728 1729 1730 1731 1732 1733
		_ipw_write32(priv, CX2_AUTOINC_DATA, 0);
}

static inline void ipw_fw_dma_reset_command_blocks(struct ipw_priv *priv)
{
	ipw_zero_memory(priv, CX2_SHARED_SRAM_DMA_CONTROL,
1734
			CB_NUMBER_OF_ELEMENTS_SMALL *
J
James Ketrenos 已提交
1735 1736 1737 1738
			sizeof(struct command_block));
}

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

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

J
James Ketrenos 已提交
1743 1744
	/* Start the dma */
	ipw_fw_dma_reset_command_blocks(priv);
1745

J
James Ketrenos 已提交
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
	/* Write CB base address */
	ipw_write_reg32(priv, CX2_DMA_I_CB_BASE, CX2_SHARED_SRAM_DMA_CONTROL);

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

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

	IPW_DEBUG_FW(">> :\n");
1758 1759

	//set the Stop and Abort bit
J
James Ketrenos 已提交
1760 1761 1762
	control = DMA_CONTROL_SMALL_CB_CONST_VALUE | DMA_CB_STOP_AND_ABORT;
	ipw_write_reg32(priv, CX2_DMA_I_DMA_CONTROL, control);
	priv->sram_desc.last_cb_index = 0;
1763

J
James Ketrenos 已提交
1764 1765 1766
	IPW_DEBUG_FW("<< \n");
}

1767 1768
static int ipw_fw_dma_write_command_block(struct ipw_priv *priv, int index,
					  struct command_block *cb)
J
James Ketrenos 已提交
1769
{
1770 1771 1772
	u32 address =
	    CX2_SHARED_SRAM_DMA_CONTROL +
	    (sizeof(struct command_block) * index);
J
James Ketrenos 已提交
1773 1774
	IPW_DEBUG_FW(">> :\n");

1775 1776
	ipw_write_indirect(priv, address, (u8 *) cb,
			   (int)sizeof(struct command_block));
J
James Ketrenos 已提交
1777 1778 1779 1780 1781 1782 1783 1784 1785

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

}

static int ipw_fw_dma_kick(struct ipw_priv *priv)
{
	u32 control = 0;
1786
	u32 index = 0;
J
James Ketrenos 已提交
1787 1788

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

J
James Ketrenos 已提交
1790
	for (index = 0; index < priv->sram_desc.last_cb_index; index++)
1791 1792
		ipw_fw_dma_write_command_block(priv, index,
					       &priv->sram_desc.cb_list[index]);
J
James Ketrenos 已提交
1793 1794

	/* Enable the DMA in the CSR register */
1795 1796 1797
	ipw_clear_bit(priv, CX2_RESET_REG,
		      CX2_RESET_REG_MASTER_DISABLED |
		      CX2_RESET_REG_STOP_MASTER);
1798

1799
	/* Set the Start bit. */
J
James Ketrenos 已提交
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
	control = DMA_CONTROL_SMALL_CB_CONST_VALUE | DMA_CB_START;
	ipw_write_reg32(priv, CX2_DMA_I_DMA_CONTROL, control);

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

static void ipw_fw_dma_dump_command_block(struct ipw_priv *priv)
{
	u32 address;
1810 1811
	u32 register_value = 0;
	u32 cb_fields_address = 0;
J
James Ketrenos 已提交
1812 1813

	IPW_DEBUG_FW(">> :\n");
1814 1815
	address = ipw_read_reg32(priv, CX2_DMA_I_CURRENT_CB);
	IPW_DEBUG_FW_INFO("Current CB is 0x%x \n", address);
J
James Ketrenos 已提交
1816 1817 1818

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

1821
	/* Print the CB values */
J
James Ketrenos 已提交
1822 1823
	cb_fields_address = address;
	register_value = ipw_read_reg32(priv, cb_fields_address);
1824
	IPW_DEBUG_FW_INFO("Current CB ControlField is 0x%x \n", register_value);
J
James Ketrenos 已提交
1825 1826 1827

	cb_fields_address += sizeof(u32);
	register_value = ipw_read_reg32(priv, cb_fields_address);
1828
	IPW_DEBUG_FW_INFO("Current CB Source Field is 0x%x \n", register_value);
J
James Ketrenos 已提交
1829 1830 1831 1832 1833 1834 1835 1836

	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);
1837
	IPW_DEBUG_FW_INFO("Current CB Status Field is 0x%x \n", register_value);
J
James Ketrenos 已提交
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847

	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");
1848
	current_cb_address = ipw_read_reg32(priv, CX2_DMA_I_CURRENT_CB);
1849

1850 1851
	current_cb_index = (current_cb_address - CX2_SHARED_SRAM_DMA_CONTROL) /
	    sizeof(struct command_block);
1852

J
James Ketrenos 已提交
1853
	IPW_DEBUG_FW_INFO("Current CB index 0x%x address = 0x%X \n",
1854
			  current_cb_index, current_cb_address);
J
James Ketrenos 已提交
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864

	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,
1865
					int interrupt_enabled, int is_last)
J
James Ketrenos 已提交
1866 1867
{

1868
	u32 control = CB_VALID | CB_SRC_LE | CB_DEST_LE | CB_SRC_AUTOINC |
1869 1870
	    CB_SRC_IO_GATED | CB_DEST_AUTOINC | CB_SRC_SIZE_LONG |
	    CB_DEST_SIZE_LONG;
J
James Ketrenos 已提交
1871
	struct command_block *cb;
1872
	u32 last_cb_element = 0;
J
James Ketrenos 已提交
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884

	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 */
1885
	if (interrupt_enabled)
J
James Ketrenos 已提交
1886 1887 1888 1889
		control |= CB_INT_ENABLED;

	if (is_last)
		control |= CB_LAST_VALID;
1890

J
James Ketrenos 已提交
1891 1892 1893
	control |= length;

	/* Calculate the CB Element's checksum value */
1894
	cb->status = control ^ src_address ^ dest_address;
J
James Ketrenos 已提交
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906

	/* 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,
1907
				 u32 src_phys, u32 dest_address, u32 length)
J
James Ketrenos 已提交
1908 1909
{
	u32 bytes_left = length;
1910 1911
	u32 src_offset = 0;
	u32 dest_offset = 0;
J
James Ketrenos 已提交
1912 1913 1914 1915 1916
	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) {
1917 1918 1919 1920 1921
		status = ipw_fw_dma_add_command_block(priv,
						      src_phys + src_offset,
						      dest_address +
						      dest_offset,
						      CB_MAX_LENGTH, 0, 0);
J
James Ketrenos 已提交
1922 1923 1924
		if (status) {
			IPW_DEBUG_FW_INFO(": Failed\n");
			return -1;
1925
		} else
J
James Ketrenos 已提交
1926 1927 1928 1929 1930 1931 1932 1933 1934
			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) {
1935 1936 1937 1938
		status =
		    ipw_fw_dma_add_command_block(priv, src_phys + src_offset,
						 dest_address + dest_offset,
						 bytes_left, 0, 0);
J
James Ketrenos 已提交
1939 1940 1941
		if (status) {
			IPW_DEBUG_FW_INFO(": Failed on the buffer tail\n");
			return -1;
1942
		} else
1943 1944
			IPW_DEBUG_FW_INFO
			    (": Adding new cb - the buffer tail\n");
J
James Ketrenos 已提交
1945
	}
1946

J
James Ketrenos 已提交
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
	IPW_DEBUG_FW("<< \n");
	return 0;
}

static int ipw_fw_dma_wait(struct ipw_priv *priv)
{
	u32 current_index = 0;
	u32 watchdog = 0;

	IPW_DEBUG_FW(">> : \n");

	current_index = ipw_fw_dma_command_block_index(priv);
1959
	IPW_DEBUG_FW_INFO("sram_desc.last_cb_index:0x%8X\n",
1960
			  (int)priv->sram_desc.last_cb_index);
J
James Ketrenos 已提交
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977

	while (current_index < priv->sram_desc.last_cb_index) {
		udelay(50);
		current_index = ipw_fw_dma_command_block_index(priv);

		watchdog++;

		if (watchdog > 400) {
			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);

1978 1979
	/*Disable the DMA in the CSR register */
	ipw_set_bit(priv, CX2_RESET_REG,
J
James Ketrenos 已提交
1980 1981 1982 1983 1984 1985
		    CX2_RESET_REG_MASTER_DISABLED | CX2_RESET_REG_STOP_MASTER);

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

1986
static void ipw_remove_current_network(struct ipw_priv *priv)
J
James Ketrenos 已提交
1987 1988
{
	struct list_head *element, *safe;
1989
	struct ieee80211_network *network = NULL;
J
James Ketrenos 已提交
1990 1991 1992 1993
	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);
1994
			list_add_tail(&network->list,
J
James Ketrenos 已提交
1995 1996 1997 1998 1999 2000
				      &priv->ieee->network_free_list);
		}
	}
}

/**
2001
 * Check that card is still alive.
J
James Ketrenos 已提交
2002 2003 2004
 * Reads debug register from domain0.
 * If card is present, pre-defined value should
 * be found there.
2005
 *
J
James Ketrenos 已提交
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
 * @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;
}

static inline int ipw_poll_bit(struct ipw_priv *priv, u32 addr, u32 mask,
			       int timeout)
{
	int i = 0;

	do {
2020
		if ((ipw_read32(priv, addr) & mask) == mask)
J
James Ketrenos 已提交
2021 2022 2023 2024
			return i;
		mdelay(10);
		i += 10;
	} while (i < timeout);
2025

J
James Ketrenos 已提交
2026 2027 2028
	return -ETIME;
}

2029
/* These functions load the firmware and micro code for the operation of
J
James Ketrenos 已提交
2030 2031 2032 2033
 * 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.
 */

2034
static int ipw_stop_master(struct ipw_priv *priv)
J
James Ketrenos 已提交
2035 2036
{
	int rc;
2037

J
James Ketrenos 已提交
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
	IPW_DEBUG_TRACE(">> \n");
	/* stop master. typical delay - 0 */
	ipw_set_bit(priv, CX2_RESET_REG, CX2_RESET_REG_STOP_MASTER);

	rc = ipw_poll_bit(priv, CX2_RESET_REG,
			  CX2_RESET_REG_MASTER_DISABLED, 100);
	if (rc < 0) {
		IPW_ERROR("stop master failed in 10ms\n");
		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);

	ipw_clear_bit(priv, CX2_RESET_REG, CBD_RESET_REG_PRINCETON_RESET);

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

struct fw_header {
	u32 version;
	u32 mode;
};

struct fw_chunk {
	u32 address;
	u32 length;
};

#define IPW_FW_MAJOR_VERSION 2
#define IPW_FW_MINOR_VERSION 2

#define IPW_FW_MINOR(x) ((x & 0xff) >> 8)
#define IPW_FW_MAJOR(x) (x & 0xff)

#define IPW_FW_VERSION ((IPW_FW_MINOR_VERSION << 8) | \
                         IPW_FW_MAJOR_VERSION)

#define IPW_FW_PREFIX "ipw-" __stringify(IPW_FW_MAJOR_VERSION) \
"." __stringify(IPW_FW_MINOR_VERSION) "-"

#if IPW_FW_MAJOR_VERSION >= 2 && IPW_FW_MINOR_VERSION > 0
#define IPW_FW_NAME(x) IPW_FW_PREFIX "" x ".fw"
#else
#define IPW_FW_NAME(x) "ipw2200_" x ".fw"
#endif

2093
static int ipw_load_ucode(struct ipw_priv *priv, u8 * data, size_t len)
J
James Ketrenos 已提交
2094 2095 2096 2097 2098
{
	int rc = 0, i, addr;
	u8 cr = 0;
	u16 *image;

2099
	image = (u16 *) data;
2100

J
James Ketrenos 已提交
2101 2102 2103 2104 2105 2106
	IPW_DEBUG_TRACE(">> \n");

	rc = ipw_stop_master(priv);

	if (rc < 0)
		return rc;
2107

2108
//      spin_lock_irqsave(&priv->lock, flags);
2109

J
James Ketrenos 已提交
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
	for (addr = CX2_SHARED_LOWER_BOUND;
	     addr < CX2_REGISTER_DOMAIN1_END; addr += 4) {
		ipw_write32(priv, addr, 0);
	}

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

2120
	ipw_write_reg32(priv, CX2_MEM_HALT_AND_RESET, CX2_BIT_HALT_RESET_ON);
J
James Ketrenos 已提交
2121 2122 2123 2124 2125 2126 2127
	ipw_arc_release(priv);
	ipw_write_reg32(priv, CX2_MEM_HALT_AND_RESET, CX2_BIT_HALT_RESET_OFF);
	mdelay(1);

	/* reset PHY */
	ipw_write_reg32(priv, CX2_INTERNAL_CMD_EVENT, CX2_BASEBAND_POWER_DOWN);
	mdelay(1);
2128

J
James Ketrenos 已提交
2129 2130
	ipw_write_reg32(priv, CX2_INTERNAL_CMD_EVENT, 0);
	mdelay(1);
2131

J
James Ketrenos 已提交
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
	/* enable ucode store */
	ipw_write_reg8(priv, DINO_CONTROL_REG, 0x0);
	ipw_write_reg8(priv, DINO_CONTROL_REG, DINO_ENABLE_CS);
	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++)
		ipw_write_reg16(priv, CX2_BASEBAND_CONTROL_STORE, image[i]);

	/* enable DINO */
	ipw_write_reg8(priv, CX2_BASEBAND_CONTROL_STATUS, 0);
2151
	ipw_write_reg8(priv, CX2_BASEBAND_CONTROL_STATUS, DINO_ENABLE_SYSTEM);
J
James Ketrenos 已提交
2152

2153
	/* this is where the igx / win driver deveates from the VAP driver. */
J
James Ketrenos 已提交
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166

	/* wait for alive response */
	for (i = 0; i < 100; i++) {
		/* poll for incoming data */
		cr = ipw_read_reg8(priv, CX2_BASEBAND_CONTROL_STATUS);
		if (cr & DINO_RXFIFO_DATA)
			break;
		mdelay(1);
	}

	if (cr & DINO_RXFIFO_DATA) {
		/* alive_command_responce size is NOT multiple of 4 */
		u32 response_buffer[(sizeof(priv->dino_alive) + 3) / 4];
2167 2168

		for (i = 0; i < ARRAY_SIZE(response_buffer); i++)
J
James Ketrenos 已提交
2169
			response_buffer[i] =
2170
			    ipw_read_reg32(priv, CX2_BASEBAND_RX_FIFO_READ);
J
James Ketrenos 已提交
2171 2172 2173 2174 2175
		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;
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
			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 已提交
2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
		} 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. */
	ipw_write_reg8(priv, CX2_BASEBAND_CONTROL_STATUS, 0);

2201
//      spin_unlock_irqrestore(&priv->lock, flags);
J
James Ketrenos 已提交
2202 2203 2204 2205

	return rc;
}

2206
static int ipw_load_firmware(struct ipw_priv *priv, u8 * data, size_t len)
J
James Ketrenos 已提交
2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
{
	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);

	if (priv->sram_desc.last_cb_index > 0) {
		/* the DMA is already ready this would be a bug. */
		BUG();
		goto out;
	}

	do {
		chunk = (struct fw_chunk *)(data + offset);
		offset += sizeof(struct fw_chunk);
		/* build DMA packet and queue up for sending */
2235
		/* dma to chunk->address, the chunk->length bytes from data +
J
James Ketrenos 已提交
2236 2237 2238 2239 2240 2241 2242 2243
		 * offeset*/
		/* Dma loading */
		rc = ipw_fw_dma_add_buffer(priv, shared_phys + offset,
					   chunk->address, chunk->length);
		if (rc) {
			IPW_DEBUG_INFO("dmaAddBuffer Failed\n");
			goto out;
		}
2244

J
James Ketrenos 已提交
2245 2246 2247
		offset += chunk->length;
	} while (offset < len);

2248
	/* Run the DMA and wait for the answer */
J
James Ketrenos 已提交
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259
	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;
	}
2260 2261
      out:
	pci_free_consistent(priv->pci_dev, len, shared_virt, shared_phys);
J
James Ketrenos 已提交
2262 2263 2264 2265 2266 2267 2268 2269
	return rc;
}

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

2270
	/* stop */
J
James Ketrenos 已提交
2271
	ipw_write32(priv, CX2_RESET_REG, CX2_RESET_REG_STOP_MASTER);
2272 2273 2274

	rc = ipw_poll_bit(priv, CX2_RESET_REG,
			  CX2_RESET_REG_MASTER_DISABLED, 500);
J
James Ketrenos 已提交
2275 2276 2277
	if (rc < 0) {
		IPW_ERROR("wait for reg master disabled failed\n");
		return rc;
2278
	}
J
James Ketrenos 已提交
2279 2280

	ipw_set_bit(priv, CX2_RESET_REG, CBD_RESET_REG_PRINCETON_RESET);
2281

J
James Ketrenos 已提交
2282 2283 2284 2285 2286 2287 2288
	return rc;
}

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

2289
	/* prvHwStartNic  release ARC */
J
James Ketrenos 已提交
2290
	ipw_clear_bit(priv, CX2_RESET_REG,
2291 2292
		      CX2_RESET_REG_MASTER_DISABLED |
		      CX2_RESET_REG_STOP_MASTER |
J
James Ketrenos 已提交
2293
		      CBD_RESET_REG_PRINCETON_RESET);
2294

J
James Ketrenos 已提交
2295
	/* enable power management */
2296 2297
	ipw_set_bit(priv, CX2_GP_CNTRL_RW,
		    CX2_GP_CNTRL_BIT_HOST_ALLOWS_STANDBY);
J
James Ketrenos 已提交
2298 2299 2300

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

J
James Ketrenos 已提交
2302 2303 2304 2305 2306
static int ipw_init_nic(struct ipw_priv *priv)
{
	int rc;

	IPW_DEBUG_TRACE(">>\n");
2307
	/* reset */
J
James Ketrenos 已提交
2308 2309 2310 2311 2312
	/*prvHwInitNic */
	/* set "initialization complete" bit to move adapter to D0 state */
	ipw_set_bit(priv, CX2_GP_CNTRL_RW, CX2_GP_CNTRL_BIT_INIT_DONE);

	/* low-level PLL activation */
2313 2314
	ipw_write32(priv, CX2_READ_INT_REGISTER,
		    CX2_BIT_INT_HOST_SRAM_READ_INT_REGISTER);
J
James Ketrenos 已提交
2315 2316

	/* wait for clock stabilization */
2317 2318
	rc = ipw_poll_bit(priv, CX2_GP_CNTRL_RW,
			  CX2_GP_CNTRL_BIT_CLOCK_READY, 250);
2319
	if (rc < 0)
J
James Ketrenos 已提交
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
		IPW_DEBUG_INFO("FAILED wait for clock stablization\n");

	/* assert SW reset */
	ipw_set_bit(priv, CX2_RESET_REG, CX2_RESET_REG_SW_RESET);

	udelay(10);

	/* set "initialization complete" bit to move adapter to D0 state */
	ipw_set_bit(priv, CX2_GP_CNTRL_RW, CX2_GP_CNTRL_BIT_INIT_DONE);

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

2334
/* Call this function from process context, it will sleep in request_firmware.
J
James Ketrenos 已提交
2335 2336 2337 2338 2339 2340 2341
 * Probe is an ok place to call this from.
 */
static int ipw_reset_nic(struct ipw_priv *priv)
{
	int rc = 0;

	IPW_DEBUG_TRACE(">>\n");
2342

J
James Ketrenos 已提交
2343
	rc = ipw_init_nic(priv);
2344

J
James Ketrenos 已提交
2345 2346 2347 2348 2349 2350
	/* Clear the 'host command active' bit... */
	priv->status &= ~STATUS_HCMD_ACTIVE;
	wake_up_interruptible(&priv->wait_command_queue);

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

2353
static int ipw_get_fw(struct ipw_priv *priv,
J
James Ketrenos 已提交
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363
		      const struct firmware **fw, const char *name)
{
	struct fw_header *header;
	int rc;

	/* ask firmware_class module to get the boot firmware off disk */
	rc = request_firmware(fw, name, &priv->pci_dev->dev);
	if (rc < 0) {
		IPW_ERROR("%s load failed: Reason %d\n", name, rc);
		return rc;
2364
	}
J
James Ketrenos 已提交
2365 2366 2367 2368 2369 2370 2371 2372 2373

	header = (struct fw_header *)(*fw)->data;
	if (IPW_FW_MAJOR(header->version) != IPW_FW_MAJOR_VERSION) {
		IPW_ERROR("'%s' firmware version not compatible (%d != %d)\n",
			  name,
			  IPW_FW_MAJOR(header->version), IPW_FW_MAJOR_VERSION);
		return -EINVAL;
	}

2374
	IPW_DEBUG_INFO("Loading firmware '%s' file v%d.%d (%zd bytes)\n",
J
James Ketrenos 已提交
2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400
		       name,
		       IPW_FW_MAJOR(header->version),
		       IPW_FW_MINOR(header->version),
		       (*fw)->size - sizeof(struct fw_header));
	return 0;
}

#define CX2_RX_BUF_SIZE (3000)

static inline void ipw_rx_queue_reset(struct ipw_priv *priv,
				      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,
2401
					 CX2_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
J
James Ketrenos 已提交
2402 2403 2404 2405
			dev_kfree_skb(rxq->pool[i].skb);
		}
		list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
	}
2406

J
James Ketrenos 已提交
2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434
	/* 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->processed = RX_QUEUE_SIZE - 1;
	rxq->free_count = 0;
	spin_unlock_irqrestore(&rxq->lock, flags);
}

#ifdef CONFIG_PM
static int fw_loaded = 0;
static const struct firmware *bootfw = NULL;
static const struct firmware *firmware = NULL;
static const struct firmware *ucode = NULL;
#endif

static int ipw_load(struct ipw_priv *priv)
{
#ifndef CONFIG_PM
	const struct firmware *bootfw = NULL;
	const struct firmware *firmware = NULL;
	const struct firmware *ucode = NULL;
#endif
	int rc = 0, retries = 3;

#ifdef CONFIG_PM
	if (!fw_loaded) {
#endif
		rc = ipw_get_fw(priv, &bootfw, IPW_FW_NAME("boot"));
2435
		if (rc)
J
James Ketrenos 已提交
2436
			goto error;
2437

J
James Ketrenos 已提交
2438 2439
		switch (priv->ieee->iw_mode) {
		case IW_MODE_ADHOC:
2440
			rc = ipw_get_fw(priv, &ucode,
J
James Ketrenos 已提交
2441
					IPW_FW_NAME("ibss_ucode"));
2442
			if (rc)
J
James Ketrenos 已提交
2443
				goto error;
2444

J
James Ketrenos 已提交
2445 2446
			rc = ipw_get_fw(priv, &firmware, IPW_FW_NAME("ibss"));
			break;
2447

J
James Ketrenos 已提交
2448 2449
#ifdef CONFIG_IPW_PROMISC
		case IW_MODE_MONITOR:
2450
			rc = ipw_get_fw(priv, &ucode,
J
James Ketrenos 已提交
2451
					IPW_FW_NAME("ibss_ucode"));
2452
			if (rc)
J
James Ketrenos 已提交
2453
				goto error;
2454

2455 2456
			rc = ipw_get_fw(priv, &firmware,
					IPW_FW_NAME("sniffer"));
J
James Ketrenos 已提交
2457 2458 2459
			break;
#endif
		case IW_MODE_INFRA:
2460
			rc = ipw_get_fw(priv, &ucode, IPW_FW_NAME("bss_ucode"));
2461
			if (rc)
J
James Ketrenos 已提交
2462
				goto error;
2463

J
James Ketrenos 已提交
2464 2465
			rc = ipw_get_fw(priv, &firmware, IPW_FW_NAME("bss"));
			break;
2466

J
James Ketrenos 已提交
2467 2468 2469 2470
		default:
			rc = -EINVAL;
		}

2471
		if (rc)
J
James Ketrenos 已提交
2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487
			goto error;

#ifdef CONFIG_PM
		fw_loaded = 1;
	}
#endif

	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;
	}

2488
      retry:
J
James Ketrenos 已提交
2489 2490 2491 2492 2493 2494
	/* Ensure interrupts are disabled */
	ipw_write32(priv, CX2_INTA_MASK_R, ~CX2_INTA_MASK_ALL);
	priv->status &= ~STATUS_INT_ENABLED;

	/* ack pending interrupts */
	ipw_write32(priv, CX2_INTA_RW, CX2_INTA_MASK_ALL);
2495

J
James Ketrenos 已提交
2496 2497 2498 2499 2500 2501 2502 2503
	ipw_stop_nic(priv);

	rc = ipw_reset_nic(priv);
	if (rc) {
		IPW_ERROR("Unable to reset NIC\n");
		goto error;
	}

2504
	ipw_zero_memory(priv, CX2_NIC_SRAM_LOWER_BOUND,
J
James Ketrenos 已提交
2505 2506 2507
			CX2_NIC_SRAM_UPPER_BOUND - CX2_NIC_SRAM_LOWER_BOUND);

	/* DMA the initial boot firmware into the device */
2508
	rc = ipw_load_firmware(priv, bootfw->data + sizeof(struct fw_header),
J
James Ketrenos 已提交
2509 2510 2511 2512 2513 2514 2515 2516 2517 2518
			       bootfw->size - sizeof(struct fw_header));
	if (rc < 0) {
		IPW_ERROR("Unable to load boot firmware\n");
		goto error;
	}

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

	/* wait for the device to finish it's initial startup sequence */
2519 2520
	rc = ipw_poll_bit(priv, CX2_INTA_RW,
			  CX2_INTA_BIT_FW_INITIALIZATION_DONE, 500);
J
James Ketrenos 已提交
2521 2522 2523 2524 2525 2526
	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);

2527
	/* ack fw init done interrupt */
J
James Ketrenos 已提交
2528 2529 2530
	ipw_write32(priv, CX2_INTA_RW, CX2_INTA_BIT_FW_INITIALIZATION_DONE);

	/* DMA the ucode into the device */
2531
	rc = ipw_load_ucode(priv, ucode->data + sizeof(struct fw_header),
J
James Ketrenos 已提交
2532 2533 2534 2535 2536
			    ucode->size - sizeof(struct fw_header));
	if (rc < 0) {
		IPW_ERROR("Unable to load ucode\n");
		goto error;
	}
2537

J
James Ketrenos 已提交
2538 2539 2540 2541
	/* stop nic */
	ipw_stop_nic(priv);

	/* DMA bss firmware into the device */
2542 2543
	rc = ipw_load_firmware(priv, firmware->data +
			       sizeof(struct fw_header),
J
James Ketrenos 已提交
2544
			       firmware->size - sizeof(struct fw_header));
2545
	if (rc < 0) {
J
James Ketrenos 已提交
2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559
		IPW_ERROR("Unable to load firmware\n");
		goto error;
	}

	ipw_write32(priv, IPW_EEPROM_LOAD_DISABLE, 0);

	rc = ipw_queue_reset(priv);
	if (rc) {
		IPW_ERROR("Unable to initialize queues\n");
		goto error;
	}

	/* Ensure interrupts are disabled */
	ipw_write32(priv, CX2_INTA_MASK_R, ~CX2_INTA_MASK_ALL);
2560

J
James Ketrenos 已提交
2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576
	/* kick start the device */
	ipw_start_nic(priv);

	if (ipw_read32(priv, CX2_INTA_RW) & CX2_INTA_BIT_PARITY_ERROR) {
		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 */
2577 2578
	rc = ipw_poll_bit(priv, CX2_INTA_RW,
			  CX2_INTA_BIT_FW_INITIALIZATION_DONE, 500);
J
James Ketrenos 已提交
2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589
	if (rc < 0) {
		IPW_ERROR("device failed to start after 500ms\n");
		goto error;
	}
	IPW_DEBUG_INFO("device response after %dms\n", rc);

	/* ack fw init done interrupt */
	ipw_write32(priv, CX2_INTA_RW, CX2_INTA_BIT_FW_INITIALIZATION_DONE);

	/* read eeprom data and initialize the eeprom region of sram */
	priv->eeprom_delay = 1;
2590
	ipw_eeprom_init_sram(priv);
J
James Ketrenos 已提交
2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609

	/* enable interrupts */
	ipw_enable_interrupts(priv);

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

	ipw_write32(priv, CX2_RX_READ_INDEX, priv->rxq->read);

	/* ack pending interrupts */
	ipw_write32(priv, CX2_INTA_RW, CX2_INTA_MASK_ALL);

#ifndef CONFIG_PM
	release_firmware(bootfw);
	release_firmware(ucode);
	release_firmware(firmware);
#endif
	return 0;

2610
      error:
J
James Ketrenos 已提交
2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629
	if (priv->rxq) {
		ipw_rx_queue_free(priv, priv->rxq);
		priv->rxq = NULL;
	}
	ipw_tx_queue_free(priv);
	if (bootfw)
		release_firmware(bootfw);
	if (ucode)
		release_firmware(ucode);
	if (firmware)
		release_firmware(firmware);
#ifdef CONFIG_PM
	fw_loaded = 0;
	bootfw = ucode = firmware = NULL;
#endif

	return rc;
}

2630
/**
J
James Ketrenos 已提交
2631 2632 2633 2634 2635 2636 2637 2638
 * 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
2639 2640
 * 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 已提交
2641 2642 2643 2644
 * 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,
2645
 * and four transmit queues for data.
J
James Ketrenos 已提交
2646
 *
2647
 * The four transmit queues allow for performing quality of service (qos)
J
James Ketrenos 已提交
2648
 * transmissions as per the 802.11 protocol.  Currently Linux does not
2649
 * provide a mechanism to the user for utilizing prioritized queues, so
J
James Ketrenos 已提交
2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
 * we only utilize the first data transmit queue (queue1).
 */

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

static inline int ipw_queue_space(const struct clx2_queue *q)
{
	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
2675
 *
J
James Ketrenos 已提交
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
 * @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)
 */
2687
static void ipw_queue_init(struct ipw_priv *priv, struct clx2_queue *q,
2688
			   int count, u32 read, u32 write, u32 base, u32 size)
J
James Ketrenos 已提交
2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
{
	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);
}

2712
static int ipw_queue_tx_init(struct ipw_priv *priv,
J
James Ketrenos 已提交
2713
			     struct clx2_tx_queue *q,
2714
			     int count, u32 read, u32 write, u32 base, u32 size)
J
James Ketrenos 已提交
2715 2716 2717 2718 2719 2720 2721 2722 2723
{
	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;
	}

2724 2725
	q->bd =
	    pci_alloc_consistent(dev, sizeof(q->bd[0]) * count, &q->q.dma_addr);
J
James Ketrenos 已提交
2726
	if (!q->bd) {
2727
		IPW_ERROR("pci_alloc_consistent(%zd) failed\n",
2728
			  sizeof(q->bd[0]) * count);
J
James Ketrenos 已提交
2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740
		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
2741
 *
J
James Ketrenos 已提交
2742 2743 2744 2745 2746 2747 2748 2749 2750
 * @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;
2751

J
James Ketrenos 已提交
2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776
	/* classify bd */
	if (bd->control_flags.message_type == TX_HOST_COMMAND_TYPE)
		/* nothing to cleanup after for host commands */
		return;

	/* sanity check */
	if (bd->u.data.num_chunks > NUM_TFD_CHUNKS) {
		IPW_ERROR("Too many chunks: %i\n", bd->u.data.num_chunks);
		/** @todo issue fatal error, it is quite serious situation */
		return;
	}

	/* unmap chunks if any */
	for (i = 0; i < bd->u.data.num_chunks; i++) {
		pci_unmap_single(dev, bd->u.data.chunk_ptr[i],
				 bd->u.data.chunk_len[i], PCI_DMA_TODEVICE);
		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.
2777
 *
J
James Ketrenos 已提交
2778 2779
 * Empty queue by removing and destroying all BD's.
 * Free all buffers.
2780
 *
J
James Ketrenos 已提交
2781 2782 2783
 * @param dev
 * @param q
 */
2784
static void ipw_queue_tx_free(struct ipw_priv *priv, struct clx2_tx_queue *txq)
J
James Ketrenos 已提交
2785 2786 2787 2788
{
	struct clx2_queue *q = &txq->q;
	struct pci_dev *dev = priv->pci_dev;

2789 2790
	if (q->n_bd == 0)
		return;
J
James Ketrenos 已提交
2791 2792 2793 2794 2795 2796

	/* 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);
	}
2797

J
James Ketrenos 已提交
2798
	/* free buffers belonging to queue itself */
2799
	pci_free_consistent(dev, sizeof(txq->bd[0]) * q->n_bd, txq->bd,
J
James Ketrenos 已提交
2800 2801 2802 2803 2804 2805 2806 2807 2808
			    q->dma_addr);
	kfree(txq->txb);

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

/**
 * Destroy all DMA queues and structures
2809
 *
J
James Ketrenos 已提交
2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
 * @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]);
}

static void inline __maybe_wake_tx(struct ipw_priv *priv)
{
	if (netif_running(priv->net_dev)) {
		switch (priv->port_type) {
		case DCR_TYPE_MU_BSS:
		case DCR_TYPE_MU_IBSS:
			if (!(priv->status & STATUS_ASSOCIATED)) {
				return;
			}
		}
		netif_wake_queue(priv->net_dev);
	}

}

2839
static inline void ipw_create_bssid(struct ipw_priv *priv, u8 * bssid)
J
James Ketrenos 已提交
2840 2841 2842 2843 2844 2845 2846
{
	/* 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 */
2847
	get_random_bytes(&bssid[3], ETH_ALEN - 3);
J
James Ketrenos 已提交
2848

2849 2850
	bssid[0] &= 0xfe;	/* clear multicast bit */
	bssid[0] |= 0x02;	/* set local assignment bit (IEEE802) */
J
James Ketrenos 已提交
2851 2852
}

2853
static inline u8 ipw_add_station(struct ipw_priv *priv, u8 * bssid)
J
James Ketrenos 已提交
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879
{
	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;

	IPW_DEBUG_SCAN("Adding AdHoc station: " MAC_FMT "\n", MAC_ARG(bssid));

	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),
2880
			 &entry, sizeof(entry));
J
James Ketrenos 已提交
2881 2882 2883 2884 2885
	priv->num_stations++;

	return i;
}

2886
static inline u8 ipw_find_station(struct ipw_priv *priv, u8 * bssid)
J
James Ketrenos 已提交
2887 2888 2889
{
	int i;

2890 2891
	for (i = 0; i < priv->num_stations; i++)
		if (!memcmp(priv->stations[i], bssid, ETH_ALEN))
J
James Ketrenos 已提交
2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907
			return i;

	return IPW_INVALID_STATION;
}

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

	if (!(priv->status & (STATUS_ASSOCIATING | STATUS_ASSOCIATED))) {
		IPW_DEBUG_ASSOC("Disassociating while not associated.\n");
		return;
	}

	IPW_DEBUG_ASSOC("Disassocation attempt from " MAC_FMT " "
			"on channel %d.\n",
2908
			MAC_ARG(priv->assoc_request.bssid),
J
James Ketrenos 已提交
2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956
			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;
	err = ipw_send_associate(priv, &priv->assoc_request);
	if (err) {
		IPW_DEBUG_HC("Attempt to send [dis]associate command "
			     "failed.\n");
		return;
	}

}

static void ipw_disassociate(void *data)
{
	ipw_send_disassociate(data, 0);
}

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);
}

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"},
2957 2958
	{0x0D,
	 "Responding station does not support the specified authentication "
J
James Ketrenos 已提交
2959
	 "algorithm"},
2960 2961
	{0x0E,
	 "Received an Authentication frame with authentication sequence "
J
James Ketrenos 已提交
2962 2963 2964 2965 2966 2967
	 "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"},
2968 2969
	{0x12,
	 "Association denied due to requesting station not supporting all "
J
James Ketrenos 已提交
2970
	 "of the datarates in the BSSBasicServiceSet Parameter"},
2971 2972
	{0x13,
	 "Association denied due to requesting station not supporting "
J
James Ketrenos 已提交
2973
	 "short preamble operation"},
2974 2975
	{0x14,
	 "Association denied due to requesting station not supporting "
J
James Ketrenos 已提交
2976
	 "PBCC encoding"},
2977 2978
	{0x15,
	 "Association denied due to requesting station not supporting "
J
James Ketrenos 已提交
2979
	 "channel agility"},
2980 2981
	{0x19,
	 "Association denied due to requesting station not supporting "
J
James Ketrenos 已提交
2982
	 "short slot operation"},
2983 2984
	{0x1A,
	 "Association denied due to requesting station not supporting "
J
James Ketrenos 已提交
2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995
	 "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"},
};

#ifdef CONFIG_IPW_DEBUG
2996
static const char *ipw_get_status_code(u16 status)
J
James Ketrenos 已提交
2997 2998
{
	int i;
2999
	for (i = 0; i < ARRAY_SIZE(ipw_status_codes); i++)
J
James Ketrenos 已提交
3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047
		if (ipw_status_codes[i].status == status)
			return ipw_status_codes[i].reason;
	return "Unknown status value.";
}
#endif

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

static void inline average_add(struct average *avg, s16 val)
{
	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;
	}
}

static s16 inline average_value(struct average *avg)
{
	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);
	average_init(&priv->average_rssi);
	average_init(&priv->average_noise);

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

J
James Ketrenos 已提交
3049 3050
	/* Firmware managed, reset only when NIC is restarted, so we have to
	 * normalize on the current value */
3051
	ipw_get_ordinal(priv, IPW_ORD_STAT_RX_ERR_CRC,
J
James Ketrenos 已提交
3052
			&priv->last_rx_err, &len);
3053
	ipw_get_ordinal(priv, IPW_ORD_STAT_TX_FAILURE,
J
James Ketrenos 已提交
3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075
			&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;

}

static inline u32 ipw_get_max_rate(struct ipw_priv *priv)
{
	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. */

3076 3077
	while (i && !(mask & i))
		i >>= 1;
J
James Ketrenos 已提交
3078
	switch (i) {
3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090
	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 已提交
3091 3092
	}

3093
	if (priv->ieee->mode == IEEE_B)
J
James Ketrenos 已提交
3094 3095 3096 3097 3098 3099 3100 3101 3102 3103
		return 11000000;
	else
		return 54000000;
}

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

3104
	if (!(priv->status & STATUS_ASSOCIATED))
J
James Ketrenos 已提交
3105 3106 3107
		return 0;

	if (priv->tx_packets > IPW_REAL_RATE_RX_PACKET_THRESHOLD) {
3108
		err = ipw_get_ordinal(priv, IPW_ORD_STAT_TX_CURR_RATE, &rate,
J
James Ketrenos 已提交
3109 3110 3111 3112 3113
				      &len);
		if (err) {
			IPW_DEBUG_INFO("failed querying ordinals.\n");
			return 0;
		}
3114
	} else
J
James Ketrenos 已提交
3115 3116 3117
		return ipw_get_max_rate(priv);

	switch (rate) {
3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129
	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 已提交
3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145
	}

	return 0;
}

#define PERFECT_RSSI (-50)
#define WORST_RSSI   (-85)
#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;
3146
	u32 beacon_quality, signal_quality, tx_quality, rx_quality,
3147
	    rate_quality;
J
James Ketrenos 已提交
3148 3149 3150 3151 3152 3153 3154

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

	/* Update the statistics */
3155
	ipw_get_ordinal(priv, IPW_ORD_STAT_MISSED_BEACONS,
J
James Ketrenos 已提交
3156
			&priv->missed_beacons, &len);
3157
	missed_beacons_delta = priv->missed_beacons - priv->last_missed_beacons;
J
James Ketrenos 已提交
3158 3159 3160
	priv->last_missed_beacons = priv->missed_beacons;
	if (priv->assoc_request.beacon_interval) {
		missed_beacons_percent = missed_beacons_delta *
3161 3162
		    (HZ * priv->assoc_request.beacon_interval) /
		    (IPW_STATS_INTERVAL * 10);
J
James Ketrenos 已提交
3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182
	} 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:
3183
	 *
J
James Ketrenos 已提交
3184 3185 3186 3187 3188
	 * 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
3189
	 *
J
James Ketrenos 已提交
3190 3191 3192 3193 3194 3195 3196 3197
	 * 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
3198
		beacon_quality = (beacon_quality - BEACON_THRESHOLD) * 100 /
3199
		    (100 - BEACON_THRESHOLD);
3200
	IPW_DEBUG_STATS("Missed beacon: %3d%% (%d%%)\n",
J
James Ketrenos 已提交
3201
			beacon_quality, missed_beacons_percent);
3202

J
James Ketrenos 已提交
3203
	priv->last_rate = ipw_get_current_rate(priv);
3204
	rate_quality = priv->last_rate * 40 / priv->last_rate + 60;
J
James Ketrenos 已提交
3205 3206
	IPW_DEBUG_STATS("Rate quality : %3d%% (%dMbs)\n",
			rate_quality, priv->last_rate / 1000000);
3207

3208
	if (rx_packets_delta > 100 && rx_packets_delta + rx_err_delta)
3209
		rx_quality = 100 - (rx_err_delta * 100) /
3210
		    (rx_packets_delta + rx_err_delta);
J
James Ketrenos 已提交
3211 3212 3213 3214
	else
		rx_quality = 100;
	IPW_DEBUG_STATS("Rx quality   : %3d%% (%u errors, %u packets)\n",
			rx_quality, rx_err_delta, rx_packets_delta);
3215

3216
	if (tx_packets_delta > 100 && tx_packets_delta + tx_failures_delta)
3217
		tx_quality = 100 - (tx_failures_delta * 100) /
3218
		    (tx_packets_delta + tx_failures_delta);
J
James Ketrenos 已提交
3219 3220 3221 3222
	else
		tx_quality = 100;
	IPW_DEBUG_STATS("Tx quality   : %3d%% (%u errors, %u packets)\n",
			tx_quality, tx_failures_delta, tx_packets_delta);
3223

J
James Ketrenos 已提交
3224 3225 3226 3227 3228 3229
	rssi = average_value(&priv->average_rssi);
	if (rssi > PERFECT_RSSI)
		signal_quality = 100;
	else if (rssi < WORST_RSSI)
		signal_quality = 0;
	else
3230
		signal_quality = (rssi - WORST_RSSI) * 100 /
3231
		    (PERFECT_RSSI - WORST_RSSI);
J
James Ketrenos 已提交
3232 3233
	IPW_DEBUG_STATS("Signal level : %3d%% (%d dBm)\n",
			signal_quality, rssi);
3234 3235

	quality = min(beacon_quality,
J
James Ketrenos 已提交
3236 3237 3238
		      min(rate_quality,
			  min(tx_quality, min(rx_quality, signal_quality))));
	if (quality == beacon_quality)
3239 3240
		IPW_DEBUG_STATS("Quality (%d%%): Clamped to missed beacons.\n",
				quality);
J
James Ketrenos 已提交
3241
	if (quality == rate_quality)
3242 3243
		IPW_DEBUG_STATS("Quality (%d%%): Clamped to rate quality.\n",
				quality);
J
James Ketrenos 已提交
3244
	if (quality == tx_quality)
3245 3246
		IPW_DEBUG_STATS("Quality (%d%%): Clamped to Tx quality.\n",
				quality);
J
James Ketrenos 已提交
3247
	if (quality == rx_quality)
3248 3249
		IPW_DEBUG_STATS("Quality (%d%%): Clamped to Rx quality.\n",
				quality);
J
James Ketrenos 已提交
3250
	if (quality == signal_quality)
3251 3252
		IPW_DEBUG_STATS("Quality (%d%%): Clamped to signal quality.\n",
				quality);
J
James Ketrenos 已提交
3253 3254

	priv->quality = quality;
3255 3256

	queue_delayed_work(priv->workqueue, &priv->gather_stats,
J
James Ketrenos 已提交
3257 3258 3259 3260 3261 3262 3263
			   IPW_STATS_INTERVAL);
}

/**
 * Handle host notification packet.
 * Called from interrupt routine
 */
3264
static inline void ipw_rx_notification(struct ipw_priv *priv,
J
James Ketrenos 已提交
3265 3266
				       struct ipw_rx_notification *notif)
{
3267
	IPW_DEBUG_NOTIF("type = %i (%d bytes)\n", notif->subtype, notif->size);
3268

J
James Ketrenos 已提交
3269
	switch (notif->subtype) {
3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328
	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,
						  "associated: '%s' " MAC_FMT
						  " \n",
						  escape_essid(priv->essid,
							       priv->essid_len),
						  MAC_ARG(priv->bssid));

					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,
								   priv->
								   assoc_request.
								   beacon_interval);
						break;
					}

					priv->status &= ~STATUS_ASSOCIATING;
					priv->status |= STATUS_ASSOCIATED;

					netif_carrier_on(priv->net_dev);
					if (netif_queue_stopped(priv->net_dev)) {
						IPW_DEBUG_NOTIF
						    ("waking queue\n");
						netif_wake_queue(priv->net_dev);
					} else {
						IPW_DEBUG_NOTIF
						    ("starting queue\n");
						netif_start_queue(priv->
								  net_dev);
					}

					ipw_reset_stats(priv);
					/* Ensure the rate is updated immediately */
					priv->last_rate =
					    ipw_get_current_rate(priv);
					schedule_work(&priv->gather_stats);
					notify_wx_assoc_event(priv);
J
James Ketrenos 已提交
3329

3330
/*			queue_delayed_work(priv->workqueue,
J
James Ketrenos 已提交
3331 3332 3333
					   &priv->request_scan,
					   SCAN_ASSOCIATED_INTERVAL);
*/
3334 3335
					break;
				}
3336

3337 3338 3339 3340
			case CMAS_AUTHENTICATED:{
					if (priv->
					    status & (STATUS_ASSOCIATED |
						      STATUS_AUTH)) {
J
James Ketrenos 已提交
3341
#ifdef CONFIG_IPW_DEBUG
3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358
						struct notif_authenticate *auth
						    = &notif->u.auth;
						IPW_DEBUG(IPW_DL_NOTIF |
							  IPW_DL_STATE |
							  IPW_DL_ASSOC,
							  "deauthenticated: '%s' "
							  MAC_FMT
							  ": (0x%04X) - %s \n",
							  escape_essid(priv->
								       essid,
								       priv->
								       essid_len),
							  MAC_ARG(priv->bssid),
							  ntohs(auth->status),
							  ipw_get_status_code
							  (ntohs
							   (auth->status)));
J
James Ketrenos 已提交
3359 3360
#endif

3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425
						priv->status &=
						    ~(STATUS_ASSOCIATING |
						      STATUS_AUTH |
						      STATUS_ASSOCIATED);

						netif_carrier_off(priv->
								  net_dev);
						netif_stop_queue(priv->net_dev);
						queue_work(priv->workqueue,
							   &priv->request_scan);
						notify_wx_assoc_event(priv);
						break;
					}

					IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
						  IPW_DL_ASSOC,
						  "authenticated: '%s' " MAC_FMT
						  "\n",
						  escape_essid(priv->essid,
							       priv->essid_len),
						  MAC_ARG(priv->bssid));
					break;
				}

			case CMAS_INIT:{
					IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
						  IPW_DL_ASSOC,
						  "disassociated: '%s' " MAC_FMT
						  " \n",
						  escape_essid(priv->essid,
							       priv->essid_len),
						  MAC_ARG(priv->bssid));

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

					netif_stop_queue(priv->net_dev);
					if (!(priv->status & STATUS_ROAMING)) {
						netif_carrier_off(priv->
								  net_dev);
						notify_wx_assoc_event(priv);

						/* Cancel any queued work ... */
						cancel_delayed_work(&priv->
								    request_scan);
						cancel_delayed_work(&priv->
								    adhoc_check);

						/* Queue up another scan... */
						queue_work(priv->workqueue,
							   &priv->request_scan);

						cancel_delayed_work(&priv->
								    gather_stats);
					} else {
						priv->status |= STATUS_ROAMING;
						queue_work(priv->workqueue,
							   &priv->request_scan);
					}

					ipw_reset_stats(priv);
					break;
				}
J
James Ketrenos 已提交
3426

3427 3428 3429
			default:
				IPW_ERROR("assoc: unknown (%d)\n",
					  assoc->state);
J
James Ketrenos 已提交
3430
				break;
3431
			}
J
James Ketrenos 已提交
3432 3433 3434

			break;
		}
3435

3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446
	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,
					  "authenticated: '%s' " MAC_FMT " \n",
					  escape_essid(priv->essid,
						       priv->essid_len),
					  MAC_ARG(priv->bssid));
				priv->status |= STATUS_AUTH;
				break;
J
James Ketrenos 已提交
3447

3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463
			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",
						  ntohs(auth->status),
						  ipw_get_status_code(ntohs
								      (auth->
								       status)));
				}
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
					  IPW_DL_ASSOC,
					  "deauthenticated: '%s' " MAC_FMT "\n",
					  escape_essid(priv->essid,
						       priv->essid_len),
					  MAC_ARG(priv->bssid));
3464

3465 3466 3467
				priv->status &= ~(STATUS_ASSOCIATING |
						  STATUS_AUTH |
						  STATUS_ASSOCIATED);
J
James Ketrenos 已提交
3468

3469 3470
				netif_carrier_off(priv->net_dev);
				netif_stop_queue(priv->net_dev);
3471
				queue_work(priv->workqueue,
J
James Ketrenos 已提交
3472
					   &priv->request_scan);
3473 3474
				notify_wx_assoc_event(priv);
				break;
J
James Ketrenos 已提交
3475

3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523
			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");
				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 已提交
3524 3525 3526 3527
			}
			break;
		}

3528 3529 3530
	case HOST_NOTIFICATION_STATUS_SCAN_CHANNEL_RESULT:{
			struct notif_channel_result *x =
			    &notif->u.channel_result;
J
James Ketrenos 已提交
3531

3532 3533 3534 3535 3536 3537 3538
			if (notif->size == sizeof(*x)) {
				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",
					       notif->size, sizeof(*x));
3539
			}
J
James Ketrenos 已提交
3540 3541 3542
			break;
		}

3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554
	case HOST_NOTIFICATION_STATUS_SCAN_COMPLETED:{
			struct notif_scan_complete *x = &notif->u.scan_complete;
			if (notif->size == sizeof(*x)) {
				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",
					  notif->size, sizeof(*x));
			}
J
James Ketrenos 已提交
3555

3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574
			priv->status &=
			    ~(STATUS_SCANNING | STATUS_SCAN_ABORTING);

			cancel_delayed_work(&priv->scan_check);

			if (!(priv->status & (STATUS_ASSOCIATED |
					      STATUS_ASSOCIATING |
					      STATUS_ROAMING |
					      STATUS_DISASSOCIATING)))
				queue_work(priv->workqueue, &priv->associate);
			else if (priv->status & STATUS_ROAMING) {
				/* 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 if (priv->status & STATUS_SCAN_PENDING)
				queue_work(priv->workqueue,
					   &priv->request_scan);
J
James Ketrenos 已提交
3575

3576 3577
			priv->ieee->scans++;
			break;
J
James Ketrenos 已提交
3578 3579
		}

3580 3581
	case HOST_NOTIFICATION_STATUS_FRAG_LENGTH:{
			struct notif_frag_length *x = &notif->u.frag_len;
J
James Ketrenos 已提交
3582

3583 3584 3585 3586 3587 3588 3589 3590
			if (notif->size == sizeof(*x)) {
				IPW_ERROR("Frag length: %d\n", x->frag_length);
			} else {
				IPW_ERROR("Frag length of wrong size %d "
					  "(should be %zd)\n",
					  notif->size, sizeof(*x));
			}
			break;
J
James Ketrenos 已提交
3591 3592
		}

3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608
	case HOST_NOTIFICATION_STATUS_LINK_DETERIORATION:{
			struct notif_link_deterioration *x =
			    &notif->u.link_deterioration;
			if (notif->size == sizeof(*x)) {
				IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
					  "link deterioration: '%s' " MAC_FMT
					  " \n", escape_essid(priv->essid,
							      priv->essid_len),
					  MAC_ARG(priv->bssid));
				memcpy(&priv->last_link_deterioration, x,
				       sizeof(*x));
			} else {
				IPW_ERROR("Link Deterioration of wrong size %d "
					  "(should be %zd)\n",
					  notif->size, sizeof(*x));
			}
J
James Ketrenos 已提交
3609 3610 3611
			break;
		}

3612 3613 3614 3615 3616 3617 3618
	case HOST_NOTIFICATION_DINO_CONFIG_RESPONSE:{
			IPW_ERROR("Dino config\n");
			if (priv->hcmd
			    && priv->hcmd->cmd == HOST_CMD_DINO_CONFIG) {
				/* TODO: Do anything special? */
			} else {
				IPW_ERROR("Unexpected DINO_CONFIG_RESPONSE\n");
J
James Ketrenos 已提交
3619
			}
3620 3621
			break;
		}
J
James Ketrenos 已提交
3622

3623 3624 3625 3626 3627 3628 3629
	case HOST_NOTIFICATION_STATUS_BEACON_STATE:{
			struct notif_beacon_state *x = &notif->u.beacon_state;
			if (notif->size != sizeof(*x)) {
				IPW_ERROR
				    ("Beacon state of wrong size %d (should "
				     "be %zd)\n", notif->size, sizeof(*x));
				break;
J
James Ketrenos 已提交
3630 3631
			}

3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659
			if (x->state == HOST_NOTIFICATION_STATUS_BEACON_MISSING) {
				if (priv->status & STATUS_SCANNING) {
					/* Stop scan to keep fw from getting
					 * stuck... */
					queue_work(priv->workqueue,
						   &priv->abort_scan);
				}

				if (x->number > priv->missed_beacon_threshold &&
				    priv->status & STATUS_ASSOCIATED) {
					IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF |
						  IPW_DL_STATE,
						  "Missed beacon: %d - disassociate\n",
						  x->number);
					queue_work(priv->workqueue,
						   &priv->disassociate);
				} else if (x->number > priv->roaming_threshold) {
					IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
						  "Missed beacon: %d - initiate "
						  "roaming\n", x->number);
					queue_work(priv->workqueue,
						   &priv->roam);
				} else {
					IPW_DEBUG_NOTIF("Missed beacon: %d\n",
							x->number);
				}

				priv->notif_missed_beacons = x->number;
J
James Ketrenos 已提交
3660

3661
			}
J
James Ketrenos 已提交
3662

3663 3664
			break;
		}
J
James Ketrenos 已提交
3665

3666 3667 3668 3669 3670 3671 3672 3673 3674
	case HOST_NOTIFICATION_STATUS_TGI_TX_KEY:{
			struct notif_tgi_tx_key *x = &notif->u.tgi_tx_key;
			if (notif->size == sizeof(*x)) {
				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 已提交
3675

3676 3677 3678
			IPW_ERROR
			    ("TGi Tx Key of wrong size %d (should be %zd)\n",
			     notif->size, sizeof(*x));
J
James Ketrenos 已提交
3679
			break;
3680
		}
J
James Ketrenos 已提交
3681

3682 3683
	case HOST_NOTIFICATION_CALIB_KEEP_RESULTS:{
			struct notif_calibration *x = &notif->u.calibration;
J
James Ketrenos 已提交
3684

3685 3686 3687 3688 3689
			if (notif->size == sizeof(*x)) {
				memcpy(&priv->calib, x, sizeof(*x));
				IPW_DEBUG_INFO("TODO: Calibration\n");
				break;
			}
J
James Ketrenos 已提交
3690

3691 3692 3693
			IPW_ERROR
			    ("Calibration of wrong size %d (should be %zd)\n",
			     notif->size, sizeof(*x));
J
James Ketrenos 已提交
3694
			break;
3695 3696
		}

3697 3698 3699 3700 3701 3702 3703 3704
	case HOST_NOTIFICATION_NOISE_STATS:{
			if (notif->size == sizeof(u32)) {
				priv->last_noise =
				    (u8) (notif->u.noise.value & 0xff);
				average_add(&priv->average_noise,
					    priv->last_noise);
				break;
			}
J
James Ketrenos 已提交
3705

3706 3707 3708
			IPW_ERROR
			    ("Noise stat is wrong size %d (should be %zd)\n",
			     notif->size, sizeof(u32));
J
James Ketrenos 已提交
3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720
			break;
		}

	default:
		IPW_ERROR("Unknown notification: "
			  "subtype=%d,flags=0x%2x,size=%d\n",
			  notif->subtype, notif->flags, notif->size);
	}
}

/**
 * Destroys all DMA structures and initialise them again
3721
 *
J
James Ketrenos 已提交
3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744
 * @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,
			       CX2_TX_CMD_QUEUE_READ_INDEX,
			       CX2_TX_CMD_QUEUE_WRITE_INDEX,
			       CX2_TX_CMD_QUEUE_BD_BASE,
			       CX2_TX_CMD_QUEUE_BD_SIZE);
	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,
			       CX2_TX_QUEUE_0_READ_INDEX,
			       CX2_TX_QUEUE_0_WRITE_INDEX,
3745
			       CX2_TX_QUEUE_0_BD_BASE, CX2_TX_QUEUE_0_BD_SIZE);
J
James Ketrenos 已提交
3746 3747 3748 3749 3750 3751 3752
	if (rc) {
		IPW_ERROR("Tx 0 queue init failed\n");
		goto error;
	}
	rc = ipw_queue_tx_init(priv, &priv->txq[1], nTx,
			       CX2_TX_QUEUE_1_READ_INDEX,
			       CX2_TX_QUEUE_1_WRITE_INDEX,
3753
			       CX2_TX_QUEUE_1_BD_BASE, CX2_TX_QUEUE_1_BD_SIZE);
J
James Ketrenos 已提交
3754 3755 3756 3757 3758 3759 3760
	if (rc) {
		IPW_ERROR("Tx 1 queue init failed\n");
		goto error;
	}
	rc = ipw_queue_tx_init(priv, &priv->txq[2], nTx,
			       CX2_TX_QUEUE_2_READ_INDEX,
			       CX2_TX_QUEUE_2_WRITE_INDEX,
3761
			       CX2_TX_QUEUE_2_BD_BASE, CX2_TX_QUEUE_2_BD_SIZE);
J
James Ketrenos 已提交
3762 3763 3764 3765 3766 3767 3768
	if (rc) {
		IPW_ERROR("Tx 2 queue init failed\n");
		goto error;
	}
	rc = ipw_queue_tx_init(priv, &priv->txq[3], nTx,
			       CX2_TX_QUEUE_3_READ_INDEX,
			       CX2_TX_QUEUE_3_WRITE_INDEX,
3769
			       CX2_TX_QUEUE_3_BD_BASE, CX2_TX_QUEUE_3_BD_SIZE);
J
James Ketrenos 已提交
3770 3771 3772 3773 3774 3775 3776 3777 3778
	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;

3779
      error:
J
James Ketrenos 已提交
3780 3781 3782 3783 3784 3785
	ipw_tx_queue_free(priv);
	return rc;
}

/**
 * Reclaim Tx queue entries no more used by NIC.
3786
 *
J
James Ketrenos 已提交
3787 3788 3789
 * When FW adwances 'R' index, all entries between old and
 * new 'R' index need to be reclaimed. As result, some free space
 * forms. If there is enough free space (> low mark), wake Tx queue.
3790
 *
J
James Ketrenos 已提交
3791 3792 3793 3794 3795 3796
 * @note Need to protect against garbage in 'R' index
 * @param priv
 * @param txq
 * @param qindex
 * @return Number of used entries remains in the queue
 */
3797
static int ipw_queue_tx_reclaim(struct ipw_priv *priv,
J
James Ketrenos 已提交
3798 3799 3800 3801 3802 3803 3804 3805 3806
				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
3807 3808
		    ("Read index for DMA queue (%d) is out of range [0-%d)\n",
		     hw_tail, q->n_bd);
J
James Ketrenos 已提交
3809 3810 3811 3812 3813 3814 3815
		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++;
	}
3816
      done:
J
James Ketrenos 已提交
3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855
	if (ipw_queue_space(q) > q->low_mark && qindex >= 0) {
		__maybe_wake_tx(priv);
	}
	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;

	if (ipw_queue_space(q) < (sync ? 1 : 2)) {
		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;
}

3856
/*
J
James Ketrenos 已提交
3857 3858 3859 3860 3861 3862 3863 3864 3865
 * Rx theory of operation
 *
 * The host allocates 32 DMA target addresses and passes the host address
 * to the firmware at register CX2_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
 * 0 to 31
 *
 * Rx Queue Indexes
 * The host/firmware share two index registers for managing the Rx buffers.
 *
3866 3867 3868
 * 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 已提交
3869 3870 3871 3872 3873 3874
 * 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
3875
 * WRITE = READ.
J
James Ketrenos 已提交
3876
 *
3877
 * During initialization the host sets up the READ queue position to the first
J
James Ketrenos 已提交
3878 3879 3880 3881 3882 3883
 * 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.
3884
 *
J
James Ketrenos 已提交
3885
 * The management in the driver is as follows:
3886
 * + A list of pre-allocated SKBs is stored in ipw->rxq->rx_free.  When
J
James Ketrenos 已提交
3887
 *   ipw->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
3888
 *   to replensish the ipw->rxq->rx_free.
J
James Ketrenos 已提交
3889 3890 3891 3892 3893 3894
 * + 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
3895 3896
 *   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 已提交
3897 3898 3899 3900 3901
 *   were enough free buffers and RX_STALLED is set it is cleared.
 *
 *
 * Driver sequence:
 *
3902
 * ipw_rx_queue_alloc()       Allocates rx_free
J
James Ketrenos 已提交
3903 3904 3905 3906 3907 3908 3909 3910 3911
 * 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
3912
 *                            READ INDEX, detaching the SKB from the pool.
J
James Ketrenos 已提交
3913 3914 3915 3916 3917 3918 3919
 *                            Moves the packet buffer from queue to rx_used.
 *                            Calls ipw_rx_queue_restock to refill any empty
 *                            slots.
 * ...
 *
 */

3920
/*
J
James Ketrenos 已提交
3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951
 * 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;
	while ((rxq->write != rxq->processed) && (rxq->free_count)) {
		element = rxq->rx_free.next;
		rxb = list_entry(element, struct ipw_rx_mem_buffer, list);
		list_del(element);

		ipw_write32(priv, CX2_RFDS_TABLE_LOWER + rxq->write * RFD_SIZE,
			    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);

3952
	/* If the pre-allocated buffer pool is dropping low, schedule to
J
James Ketrenos 已提交
3953 3954 3955 3956 3957
	 * 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 */
3958
	if (write != rxq->write)
J
James Ketrenos 已提交
3959 3960 3961 3962 3963
		ipw_write32(priv, CX2_RX_WRITE_INDEX, rxq->write);
}

/*
 * Move all used packet from rx_used to rx_free, allocating a new SKB for each.
3964 3965
 * Also restock the Rx queue via ipw_rx_queue_restock.
 *
J
James Ketrenos 已提交
3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989
 * 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);
		rxb->skb = alloc_skb(CX2_RX_BUF_SIZE, GFP_ATOMIC);
		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);
3990

J
James Ketrenos 已提交
3991
		rxb->rxb = (struct ipw_rx_buffer *)rxb->skb->data;
3992 3993 3994
		rxb->dma_addr =
		    pci_map_single(priv->pci_dev, rxb->skb->data,
				   CX2_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3995

J
James Ketrenos 已提交
3996 3997 3998 3999 4000 4001 4002 4003 4004 4005
		list_add_tail(&rxb->list, &rxq->rx_free);
		rxq->free_count++;
	}
	spin_unlock_irqrestore(&rxq->lock, flags);

	ipw_rx_queue_restock(priv);
}

/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
 * If an SKB has been detached, the POOL needs to have it's SKB set to NULL
4006
 * This free routine walks the list of POOL entries and if SKB is set to
J
James Ketrenos 已提交
4007 4008
 * non NULL it is unmapped and freed
 */
4009
static void ipw_rx_queue_free(struct ipw_priv *priv, struct ipw_rx_queue *rxq)
J
James Ketrenos 已提交
4010 4011 4012 4013 4014
{
	int i;

	if (!rxq)
		return;
4015

J
James Ketrenos 已提交
4016 4017 4018
	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,
4019
					 CX2_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
J
James Ketrenos 已提交
4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038
			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;

	rxq = (struct ipw_rx_queue *)kmalloc(sizeof(*rxq), GFP_KERNEL);
	memset(rxq, 0, sizeof(*rxq));
	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 */
4039
	for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
J
James Ketrenos 已提交
4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055
		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->processed = RX_QUEUE_SIZE - 1;
	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) {
4056 4057
		case IEEE80211_OFDM_RATE_6MB:
			return priv->rates_mask & IEEE80211_OFDM_RATE_6MB_MASK ?
4058
			    1 : 0;
4059 4060
		case IEEE80211_OFDM_RATE_9MB:
			return priv->rates_mask & IEEE80211_OFDM_RATE_9MB_MASK ?
4061
			    1 : 0;
4062
		case IEEE80211_OFDM_RATE_12MB:
4063 4064
			return priv->
			    rates_mask & IEEE80211_OFDM_RATE_12MB_MASK ? 1 : 0;
4065
		case IEEE80211_OFDM_RATE_18MB:
4066 4067
			return priv->
			    rates_mask & IEEE80211_OFDM_RATE_18MB_MASK ? 1 : 0;
4068
		case IEEE80211_OFDM_RATE_24MB:
4069 4070
			return priv->
			    rates_mask & IEEE80211_OFDM_RATE_24MB_MASK ? 1 : 0;
4071
		case IEEE80211_OFDM_RATE_36MB:
4072 4073
			return priv->
			    rates_mask & IEEE80211_OFDM_RATE_36MB_MASK ? 1 : 0;
4074
		case IEEE80211_OFDM_RATE_48MB:
4075 4076
			return priv->
			    rates_mask & IEEE80211_OFDM_RATE_48MB_MASK ? 1 : 0;
4077
		case IEEE80211_OFDM_RATE_54MB:
4078 4079
			return priv->
			    rates_mask & IEEE80211_OFDM_RATE_54MB_MASK ? 1 : 0;
J
James Ketrenos 已提交
4080 4081 4082 4083
		default:
			return 0;
		}
	}
4084

J
James Ketrenos 已提交
4085 4086
	/* B and G mixed */
	switch (rate) {
4087
	case IEEE80211_CCK_RATE_1MB:
J
James Ketrenos 已提交
4088
		return priv->rates_mask & IEEE80211_CCK_RATE_1MB_MASK ? 1 : 0;
4089
	case IEEE80211_CCK_RATE_2MB:
J
James Ketrenos 已提交
4090
		return priv->rates_mask & IEEE80211_CCK_RATE_2MB_MASK ? 1 : 0;
4091
	case IEEE80211_CCK_RATE_5MB:
J
James Ketrenos 已提交
4092
		return priv->rates_mask & IEEE80211_CCK_RATE_5MB_MASK ? 1 : 0;
4093
	case IEEE80211_CCK_RATE_11MB:
J
James Ketrenos 已提交
4094 4095 4096 4097 4098 4099 4100 4101 4102
		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) {
4103
	case IEEE80211_OFDM_RATE_6MB:
J
James Ketrenos 已提交
4104
		return priv->rates_mask & IEEE80211_OFDM_RATE_6MB_MASK ? 1 : 0;
4105
	case IEEE80211_OFDM_RATE_9MB:
J
James Ketrenos 已提交
4106
		return priv->rates_mask & IEEE80211_OFDM_RATE_9MB_MASK ? 1 : 0;
4107
	case IEEE80211_OFDM_RATE_12MB:
J
James Ketrenos 已提交
4108
		return priv->rates_mask & IEEE80211_OFDM_RATE_12MB_MASK ? 1 : 0;
4109
	case IEEE80211_OFDM_RATE_18MB:
J
James Ketrenos 已提交
4110
		return priv->rates_mask & IEEE80211_OFDM_RATE_18MB_MASK ? 1 : 0;
4111
	case IEEE80211_OFDM_RATE_24MB:
J
James Ketrenos 已提交
4112
		return priv->rates_mask & IEEE80211_OFDM_RATE_24MB_MASK ? 1 : 0;
4113
	case IEEE80211_OFDM_RATE_36MB:
J
James Ketrenos 已提交
4114
		return priv->rates_mask & IEEE80211_OFDM_RATE_36MB_MASK ? 1 : 0;
4115
	case IEEE80211_OFDM_RATE_48MB:
J
James Ketrenos 已提交
4116
		return priv->rates_mask & IEEE80211_OFDM_RATE_48MB_MASK ? 1 : 0;
4117
	case IEEE80211_OFDM_RATE_54MB:
J
James Ketrenos 已提交
4118 4119 4120 4121 4122 4123
		return priv->rates_mask & IEEE80211_OFDM_RATE_54MB_MASK ? 1 : 0;
	}

	return 0;
}

4124
static int ipw_compatible_rates(struct ipw_priv *priv,
J
James Ketrenos 已提交
4125 4126 4127 4128 4129 4130
				const struct ieee80211_network *network,
				struct ipw_supported_rates *rates)
{
	int num_rates, i;

	memset(rates, 0, sizeof(*rates));
4131
	num_rates = min(network->rates_len, (u8) IPW_MAX_RATES);
J
James Ketrenos 已提交
4132 4133
	rates->num_rates = 0;
	for (i = 0; i < num_rates; i++) {
4134 4135
		if (!ipw_is_rate_in_mask
		    (priv, network->mode, network->rates[i])) {
J
James Ketrenos 已提交
4136 4137 4138 4139
			IPW_DEBUG_SCAN("Rate %02X masked : 0x%08X\n",
				       network->rates[i], priv->rates_mask);
			continue;
		}
4140

J
James Ketrenos 已提交
4141 4142 4143
		rates->supported_rates[rates->num_rates++] = network->rates[i];
	}

4144 4145
	num_rates =
	    min(network->rates_ex_len, (u8) (IPW_MAX_RATES - num_rates));
J
James Ketrenos 已提交
4146
	for (i = 0; i < num_rates; i++) {
4147 4148
		if (!ipw_is_rate_in_mask
		    (priv, network->mode, network->rates_ex[i])) {
J
James Ketrenos 已提交
4149 4150 4151 4152
			IPW_DEBUG_SCAN("Rate %02X masked : 0x%08X\n",
				       network->rates_ex[i], priv->rates_mask);
			continue;
		}
4153

4154 4155
		rates->supported_rates[rates->num_rates++] =
		    network->rates_ex[i];
J
James Ketrenos 已提交
4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173
	}

	return rates->num_rates;
}

static inline void ipw_copy_rates(struct ipw_supported_rates *dest,
				  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,
4174
				   u8 modulation, u32 rate_mask)
J
James Ketrenos 已提交
4175
{
4176
	u8 basic_mask = (IEEE80211_OFDM_MODULATION == modulation) ?
4177
	    IEEE80211_BASIC_RATE_MASK : 0;
4178

J
James Ketrenos 已提交
4179
	if (rate_mask & IEEE80211_CCK_RATE_1MB_MASK)
4180
		rates->supported_rates[rates->num_rates++] =
4181
		    IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
J
James Ketrenos 已提交
4182 4183

	if (rate_mask & IEEE80211_CCK_RATE_2MB_MASK)
4184
		rates->supported_rates[rates->num_rates++] =
4185
		    IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
J
James Ketrenos 已提交
4186 4187

	if (rate_mask & IEEE80211_CCK_RATE_5MB_MASK)
4188
		rates->supported_rates[rates->num_rates++] = basic_mask |
4189
		    IEEE80211_CCK_RATE_5MB;
J
James Ketrenos 已提交
4190 4191

	if (rate_mask & IEEE80211_CCK_RATE_11MB_MASK)
4192
		rates->supported_rates[rates->num_rates++] = basic_mask |
4193
		    IEEE80211_CCK_RATE_11MB;
J
James Ketrenos 已提交
4194 4195 4196
}

static void ipw_add_ofdm_scan_rates(struct ipw_supported_rates *rates,
4197
				    u8 modulation, u32 rate_mask)
J
James Ketrenos 已提交
4198
{
4199
	u8 basic_mask = (IEEE80211_OFDM_MODULATION == modulation) ?
4200
	    IEEE80211_BASIC_RATE_MASK : 0;
J
James Ketrenos 已提交
4201 4202

	if (rate_mask & IEEE80211_OFDM_RATE_6MB_MASK)
4203
		rates->supported_rates[rates->num_rates++] = basic_mask |
4204
		    IEEE80211_OFDM_RATE_6MB;
J
James Ketrenos 已提交
4205 4206

	if (rate_mask & IEEE80211_OFDM_RATE_9MB_MASK)
4207
		rates->supported_rates[rates->num_rates++] =
4208
		    IEEE80211_OFDM_RATE_9MB;
J
James Ketrenos 已提交
4209 4210

	if (rate_mask & IEEE80211_OFDM_RATE_12MB_MASK)
4211
		rates->supported_rates[rates->num_rates++] = basic_mask |
4212
		    IEEE80211_OFDM_RATE_12MB;
J
James Ketrenos 已提交
4213 4214

	if (rate_mask & IEEE80211_OFDM_RATE_18MB_MASK)
4215
		rates->supported_rates[rates->num_rates++] =
4216
		    IEEE80211_OFDM_RATE_18MB;
J
James Ketrenos 已提交
4217 4218

	if (rate_mask & IEEE80211_OFDM_RATE_24MB_MASK)
4219
		rates->supported_rates[rates->num_rates++] = basic_mask |
4220
		    IEEE80211_OFDM_RATE_24MB;
J
James Ketrenos 已提交
4221 4222

	if (rate_mask & IEEE80211_OFDM_RATE_36MB_MASK)
4223
		rates->supported_rates[rates->num_rates++] =
4224
		    IEEE80211_OFDM_RATE_36MB;
J
James Ketrenos 已提交
4225 4226

	if (rate_mask & IEEE80211_OFDM_RATE_48MB_MASK)
4227
		rates->supported_rates[rates->num_rates++] =
4228
		    IEEE80211_OFDM_RATE_48MB;
J
James Ketrenos 已提交
4229 4230

	if (rate_mask & IEEE80211_OFDM_RATE_54MB_MASK)
4231
		rates->supported_rates[rates->num_rates++] =
4232
		    IEEE80211_OFDM_RATE_54MB;
J
James Ketrenos 已提交
4233 4234 4235 4236 4237 4238 4239
}

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

4240 4241 4242
static int ipw_best_network(struct ipw_priv *priv,
			    struct ipw_network_match *match,
			    struct ieee80211_network *network, int roaming)
J
James Ketrenos 已提交
4243 4244 4245 4246 4247 4248
{
	struct ipw_supported_rates rates;

	/* Verify that this network's capability is compatible with the
	 * current mode (AdHoc or Infrastructure) */
	if ((priv->ieee->iw_mode == IW_MODE_INFRA &&
4249
	     !(network->capability & WLAN_CAPABILITY_ESS)) ||
J
James Ketrenos 已提交
4250 4251 4252
	    (priv->ieee->iw_mode == IW_MODE_ADHOC &&
	     !(network->capability & WLAN_CAPABILITY_IBSS))) {
		IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded due to "
4253
				"capability mismatch.\n",
J
James Ketrenos 已提交
4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267
				escape_essid(network->ssid, network->ssid_len),
				MAC_ARG(network->bssid));
		return 0;
	}

	/* If we do not have an ESSID for this AP, we can not associate with
	 * it */
	if (network->flags & NETWORK_EMPTY_ESSID) {
		IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
				"because of hidden ESSID.\n",
				escape_essid(network->ssid, network->ssid_len),
				MAC_ARG(network->bssid));
		return 0;
	}
4268

J
James Ketrenos 已提交
4269 4270 4271 4272
	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) ||
4273
		    memcmp(network->ssid, match->network->ssid,
J
James Ketrenos 已提交
4274 4275 4276
			   network->ssid_len)) {
			IPW_DEBUG_ASSOC("Netowrk '%s (" MAC_FMT ")' excluded "
					"because of non-network ESSID.\n",
4277
					escape_essid(network->ssid,
J
James Ketrenos 已提交
4278 4279 4280 4281 4282
						     network->ssid_len),
					MAC_ARG(network->bssid));
			return 0;
		}
	} else {
4283 4284 4285
		/* If an ESSID has been configured then compare the broadcast
		 * ESSID to ours */
		if ((priv->config & CFG_STATIC_ESSID) &&
J
James Ketrenos 已提交
4286
		    ((network->ssid_len != priv->essid_len) ||
4287
		     memcmp(network->ssid, priv->essid,
J
James Ketrenos 已提交
4288 4289
			    min(network->ssid_len, priv->essid_len)))) {
			char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
4290 4291
			strncpy(escaped,
				escape_essid(network->ssid, network->ssid_len),
J
James Ketrenos 已提交
4292 4293
				sizeof(escaped));
			IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
4294
					"because of ESSID mismatch: '%s'.\n",
J
James Ketrenos 已提交
4295
					escaped, MAC_ARG(network->bssid),
4296 4297
					escape_essid(priv->essid,
						     priv->essid_len));
J
James Ketrenos 已提交
4298 4299 4300 4301 4302 4303
			return 0;
		}
	}

	/* If the old network rate is better than this one, don't bother
	 * testing everything else. */
4304
	if (match->network && match->network->stats.rssi > network->stats.rssi) {
J
James Ketrenos 已提交
4305
		char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
4306 4307
		strncpy(escaped,
			escape_essid(network->ssid, network->ssid_len),
J
James Ketrenos 已提交
4308 4309 4310 4311 4312 4313 4314 4315 4316
			sizeof(escaped));
		IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded because "
				"'%s (" MAC_FMT ")' has a stronger signal.\n",
				escaped, MAC_ARG(network->bssid),
				escape_essid(match->network->ssid,
					     match->network->ssid_len),
				MAC_ARG(match->network->bssid));
		return 0;
	}
4317

J
James Ketrenos 已提交
4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331
	/* If this network has already had an association attempt within the
	 * last 3 seconds, do not try and associate again... */
	if (network->last_associate &&
	    time_after(network->last_associate + (HZ * 5UL), jiffies)) {
		IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
				"because of storming (%lu since last "
				"assoc attempt).\n",
				escape_essid(network->ssid, network->ssid_len),
				MAC_ARG(network->bssid),
				(jiffies - network->last_associate) / HZ);
		return 0;
	}

	/* Now go through and see if the requested network is valid... */
4332
	if (priv->ieee->scan_age != 0 &&
J
James Ketrenos 已提交
4333 4334 4335 4336 4337 4338 4339
	    jiffies - network->last_scanned > priv->ieee->scan_age) {
		IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
				"because of age: %lums.\n",
				escape_essid(network->ssid, network->ssid_len),
				MAC_ARG(network->bssid),
				(jiffies - network->last_scanned) / (HZ / 100));
		return 0;
4340
	}
J
James Ketrenos 已提交
4341

4342
	if ((priv->config & CFG_STATIC_CHANNEL) &&
J
James Ketrenos 已提交
4343 4344 4345 4346 4347 4348 4349 4350
	    (network->channel != priv->channel)) {
		IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
				"because of channel mismatch: %d != %d.\n",
				escape_essid(network->ssid, network->ssid_len),
				MAC_ARG(network->bssid),
				network->channel, priv->channel);
		return 0;
	}
4351

J
James Ketrenos 已提交
4352
	/* Verify privacy compatability */
4353
	if (((priv->capability & CAP_PRIVACY_ON) ? 1 : 0) !=
J
James Ketrenos 已提交
4354 4355 4356 4357 4358
	    ((network->capability & WLAN_CAPABILITY_PRIVACY) ? 1 : 0)) {
		IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
				"because of privacy mismatch: %s != %s.\n",
				escape_essid(network->ssid, network->ssid_len),
				MAC_ARG(network->bssid),
4359
				priv->capability & CAP_PRIVACY_ON ? "on" :
J
James Ketrenos 已提交
4360
				"off",
4361
				network->capability &
4362
				WLAN_CAPABILITY_PRIVACY ? "on" : "off");
J
James Ketrenos 已提交
4363 4364
		return 0;
	}
4365 4366

	if ((priv->config & CFG_STATIC_BSSID) &&
J
James Ketrenos 已提交
4367 4368 4369 4370
	    memcmp(network->bssid, priv->bssid, ETH_ALEN)) {
		IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
				"because of BSSID mismatch: " MAC_FMT ".\n",
				escape_essid(network->ssid, network->ssid_len),
4371
				MAC_ARG(network->bssid), MAC_ARG(priv->bssid));
J
James Ketrenos 已提交
4372 4373
		return 0;
	}
4374

J
James Ketrenos 已提交
4375 4376 4377 4378 4379 4380 4381 4382 4383
	/* Filter out any incompatible freq / mode combinations */
	if (!ieee80211_is_valid_mode(priv->ieee, network->mode)) {
		IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
				"because of invalid frequency/mode "
				"combination.\n",
				escape_essid(network->ssid, network->ssid_len),
				MAC_ARG(network->bssid));
		return 0;
	}
4384

J
James Ketrenos 已提交
4385 4386 4387 4388 4389 4390 4391 4392
	ipw_compatible_rates(priv, network, &rates);
	if (rates.num_rates == 0) {
		IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
				"because of no compatible rates.\n",
				escape_essid(network->ssid, network->ssid_len),
				MAC_ARG(network->bssid));
		return 0;
	}
4393

J
James Ketrenos 已提交
4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408
	/* 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;

	IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' is a viable match.\n",
			escape_essid(network->ssid, network->ssid_len),
			MAC_ARG(network->bssid));

	return 1;
}

4409
static void ipw_adhoc_create(struct ipw_priv *priv,
4410
			     struct ieee80211_network *network)
J
James Ketrenos 已提交
4411 4412 4413 4414 4415 4416 4417
{
	/*
	 * 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.
	 *
4418
	 * We also have the possibility of an invalid channel for the
J
James Ketrenos 已提交
4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449
	 * chossen band.  Attempting to create a new ad-hoc network
	 * with an invalid channel for wireless mode will trigger a
	 * FW fatal error.
	 */
	network->mode = is_valid_channel(priv->ieee->mode, priv->channel);
	if (network->mode) {
		network->channel = priv->channel;
	} else {
		IPW_WARNING("Overriding invalid channel\n");
		if (priv->ieee->mode & IEEE_A) {
			network->mode = IEEE_A;
			priv->channel = band_a_active_channel[0];
		} else if (priv->ieee->mode & IEEE_G) {
			network->mode = IEEE_G;
			priv->channel = band_b_active_channel[0];
		} else {
			network->mode = IEEE_B;
			priv->channel = band_b_active_channel[0];
		}
	}

	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;
	if (priv->capability & CAP_PRIVACY_ON)
		network->capability |= WLAN_CAPABILITY_PRIVACY;
	network->rates_len = min(priv->rates.num_rates, MAX_RATES_LENGTH);
4450
	memcpy(network->rates, priv->rates.supported_rates, network->rates_len);
J
James Ketrenos 已提交
4451
	network->rates_ex_len = priv->rates.num_rates - network->rates_len;
4452
	memcpy(network->rates_ex,
J
James Ketrenos 已提交
4453 4454 4455 4456 4457 4458 4459
	       &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;
4460 4461 4462
	network->beacon_interval = 100;	/* Default */
	network->listen_interval = 10;	/* Default */
	network->atim_window = 0;	/* Default */
4463
#ifdef CONFIG_IEEE80211_WPA
J
James Ketrenos 已提交
4464 4465
	network->wpa_ie_len = 0;
	network->rsn_ie_len = 0;
4466
#endif				/* CONFIG_IEEE80211_WPA */
J
James Ketrenos 已提交
4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481
}

static void ipw_send_wep_keys(struct ipw_priv *priv)
{
	struct ipw_wep_key *key;
	int i;
	struct host_cmd cmd = {
		.cmd = IPW_CMD_WEP_KEY,
		.len = sizeof(*key)
	};

	key = (struct ipw_wep_key *)&cmd.param;
	key->cmd_id = DINO_CMD_WEP_KEY;
	key->seq_num = 0;

4482
	for (i = 0; i < 4; i++) {
J
James Ketrenos 已提交
4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494
		key->key_index = i;
		if (!(priv->sec.flags & (1 << i))) {
			key->key_size = 0;
		} else {
			key->key_size = priv->sec.key_sizes[i];
			memcpy(key->key, priv->sec.keys[i], key->key_size);
		}

		if (ipw_send_cmd(priv, &cmd)) {
			IPW_ERROR("failed to send WEP_KEY command\n");
			return;
		}
4495
	}
J
James Ketrenos 已提交
4496 4497 4498 4499 4500
}

static void ipw_adhoc_check(void *data)
{
	struct ipw_priv *priv = data;
4501

J
James Ketrenos 已提交
4502 4503 4504 4505 4506 4507 4508 4509
	if (priv->missed_adhoc_beacons++ > priv->missed_beacon_threshold &&
	    !(priv->config & CFG_ADHOC_PERSIST)) {
		IPW_DEBUG_SCAN("Disassociating due to missed beacons\n");
		ipw_remove_current_network(priv);
		ipw_disassociate(priv);
		return;
	}

4510
	queue_delayed_work(priv->workqueue, &priv->adhoc_check,
J
James Ketrenos 已提交
4511 4512 4513 4514 4515 4516 4517 4518 4519
			   priv->assoc_request.beacon_interval);
}

#ifdef CONFIG_IPW_DEBUG
static void ipw_debug_config(struct ipw_priv *priv)
{
	IPW_DEBUG_INFO("Scan completed, no valid APs matched "
		       "[CFG 0x%08X]\n", priv->config);
	if (priv->config & CFG_STATIC_CHANNEL)
4520
		IPW_DEBUG_INFO("Channel locked to %d\n", priv->channel);
J
James Ketrenos 已提交
4521 4522 4523
	else
		IPW_DEBUG_INFO("Channel unlocked.\n");
	if (priv->config & CFG_STATIC_ESSID)
4524
		IPW_DEBUG_INFO("ESSID locked to '%s'\n",
4525
			       escape_essid(priv->essid, priv->essid_len));
J
James Ketrenos 已提交
4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538
	else
		IPW_DEBUG_INFO("ESSID unlocked.\n");
	if (priv->config & CFG_STATIC_BSSID)
		IPW_DEBUG_INFO("BSSID locked to %d\n", priv->channel);
	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);
}
#else
J
Jiri Benc 已提交
4539
#define ipw_debug_config(x) do {} while (0)
J
James Ketrenos 已提交
4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551
#endif

static inline void ipw_set_fixed_rate(struct ipw_priv *priv,
				      struct ieee80211_network *network)
{
	/* TODO: Verify that this works... */
	struct ipw_fixed_rate fr = {
		.tx_rates = priv->rates_mask
	};
	u32 reg;
	u16 mask = 0;

4552
	/* Identify 'current FW band' and match it with the fixed
J
James Ketrenos 已提交
4553
	 * Tx rates */
4554

J
James Ketrenos 已提交
4555
	switch (priv->ieee->freq_band) {
4556
	case IEEE80211_52GHZ_BAND:	/* A only */
J
James Ketrenos 已提交
4557 4558 4559 4560 4561 4562
		/* IEEE_A */
		if (priv->rates_mask & ~IEEE80211_OFDM_RATES_MASK) {
			/* Invalid fixed rate mask */
			fr.tx_rates = 0;
			break;
		}
4563

J
James Ketrenos 已提交
4564 4565 4566
		fr.tx_rates >>= IEEE80211_OFDM_SHIFT_MASK_A;
		break;

4567
	default:		/* 2.4Ghz or Mixed */
J
James Ketrenos 已提交
4568 4569 4570 4571 4572 4573 4574
		/* IEEE_B */
		if (network->mode == IEEE_B) {
			if (fr.tx_rates & ~IEEE80211_CCK_RATES_MASK) {
				/* Invalid fixed rate mask */
				fr.tx_rates = 0;
			}
			break;
4575
		}
J
James Ketrenos 已提交
4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588

		/* IEEE_G */
		if (fr.tx_rates & ~(IEEE80211_CCK_RATES_MASK |
				    IEEE80211_OFDM_RATES_MASK)) {
			/* Invalid fixed rate mask */
			fr.tx_rates = 0;
			break;
		}

		if (IEEE80211_OFDM_RATE_6MB_MASK & fr.tx_rates) {
			mask |= (IEEE80211_OFDM_RATE_6MB_MASK >> 1);
			fr.tx_rates &= ~IEEE80211_OFDM_RATE_6MB_MASK;
		}
4589

J
James Ketrenos 已提交
4590 4591 4592 4593
		if (IEEE80211_OFDM_RATE_9MB_MASK & fr.tx_rates) {
			mask |= (IEEE80211_OFDM_RATE_9MB_MASK >> 1);
			fr.tx_rates &= ~IEEE80211_OFDM_RATE_9MB_MASK;
		}
4594

J
James Ketrenos 已提交
4595 4596 4597 4598
		if (IEEE80211_OFDM_RATE_12MB_MASK & fr.tx_rates) {
			mask |= (IEEE80211_OFDM_RATE_12MB_MASK >> 1);
			fr.tx_rates &= ~IEEE80211_OFDM_RATE_12MB_MASK;
		}
4599

J
James Ketrenos 已提交
4600 4601 4602 4603 4604
		fr.tx_rates |= mask;
		break;
	}

	reg = ipw_read32(priv, IPW_MEM_FIXED_OVERRIDE);
4605
	ipw_write_reg32(priv, reg, *(u32 *) & fr);
J
James Ketrenos 已提交
4606 4607 4608 4609
}

static int ipw_associate_network(struct ipw_priv *priv,
				 struct ieee80211_network *network,
4610
				 struct ipw_supported_rates *rates, int roaming)
J
James Ketrenos 已提交
4611 4612 4613 4614 4615 4616 4617
{
	int err;

	if (priv->config & CFG_FIXED_RATE)
		ipw_set_fixed_rate(priv, network);

	if (!(priv->config & CFG_STATIC_ESSID)) {
4618
		priv->essid_len = min(network->ssid_len,
4619
				      (u8) IW_ESSID_MAX_SIZE);
J
James Ketrenos 已提交
4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635
		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;
	if ((priv->capability & CAP_PRIVACY_ON) &&
	    (priv->capability & CAP_SHARED_KEY)) {
		priv->assoc_request.auth_type = AUTH_SHARED_KEY;
		priv->assoc_request.auth_key = priv->sec.active_key;
	} else {
		priv->assoc_request.auth_type = AUTH_OPEN;
		priv->assoc_request.auth_key = 0;
	}

4636
	if (priv->capability & CAP_PRIVACY_ON)
J
James Ketrenos 已提交
4637 4638
		ipw_send_wep_keys(priv);

4639 4640 4641
	/*
	 * 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 已提交
4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653
	 * 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;

	IPW_DEBUG_ASSOC("%sssocation attempt: '%s', channel %d, "
			"802.11%c [%d], enc=%s%s%s%c%c\n",
			roaming ? "Rea" : "A",
4654 4655 4656 4657
			escape_essid(priv->essid, priv->essid_len),
			network->channel,
			ipw_modes[priv->assoc_request.ieee_mode],
			rates->num_rates,
J
James Ketrenos 已提交
4658
			priv->capability & CAP_PRIVACY_ON ? "on " : "off",
4659 4660
			priv->capability & CAP_PRIVACY_ON ?
			(priv->capability & CAP_SHARED_KEY ? "(shared)" :
J
James Ketrenos 已提交
4661 4662
			 "(open)") : "",
			priv->capability & CAP_PRIVACY_ON ? " key=" : "",
4663
			priv->capability & CAP_PRIVACY_ON ?
J
James Ketrenos 已提交
4664
			'1' + priv->sec.active_key : '.',
4665
			priv->capability & CAP_PRIVACY_ON ? '.' : ' ');
J
James Ketrenos 已提交
4666 4667 4668

	priv->assoc_request.beacon_interval = network->beacon_interval;
	if ((priv->ieee->iw_mode == IW_MODE_ADHOC) &&
4669
	    (network->time_stamp[0] == 0) && (network->time_stamp[1] == 0)) {
J
James Ketrenos 已提交
4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687
		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;
		priv->assoc_request.assoc_tsf_msw = network->time_stamp[1];
		priv->assoc_request.assoc_tsf_lsw = network->time_stamp[0];
	}

	memcpy(&priv->assoc_request.bssid, network->bssid, ETH_ALEN);

	if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
		memset(&priv->assoc_request.dest, 0xFF, ETH_ALEN);
		priv->assoc_request.atim_window = network->atim_window;
	} else {
4688
		memcpy(&priv->assoc_request.dest, network->bssid, ETH_ALEN);
J
James Ketrenos 已提交
4689 4690 4691 4692 4693
		priv->assoc_request.atim_window = 0;
	}

	priv->assoc_request.capability = network->capability;
	priv->assoc_request.listen_interval = network->listen_interval;
4694

J
James Ketrenos 已提交
4695 4696 4697 4698 4699 4700 4701 4702 4703
	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);
4704

J
James Ketrenos 已提交
4705 4706 4707 4708 4709 4710 4711 4712 4713
	if (priv->assoc_request.ieee_mode == IPW_G_MODE)
		priv->sys_config.dot11g_auto_detection = 1;
	else
		priv->sys_config.dot11g_auto_detection = 0;
	err = ipw_send_system_config(priv, &priv->sys_config);
	if (err) {
		IPW_DEBUG_HC("Attempt to send sys config command failed.\n");
		return err;
	}
4714

J
James Ketrenos 已提交
4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728
	IPW_DEBUG_ASSOC("Association sensitivity: %d\n", network->stats.rssi);
	err = ipw_set_sensitivity(priv, network->stats.rssi);
	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);
4729
	priv->status |= STATUS_ASSOCIATING;
J
James Ketrenos 已提交
4730 4731 4732 4733 4734 4735 4736 4737 4738
	priv->status &= ~STATUS_SECURITY_UPDATED;

	priv->assoc_network = network;

	err = ipw_send_associate(priv, &priv->assoc_request);
	if (err) {
		IPW_DEBUG_HC("Attempt to send associate command failed.\n");
		return err;
	}
4739 4740

	IPW_DEBUG(IPW_DL_STATE, "associating: '%s' " MAC_FMT " \n",
J
James Ketrenos 已提交
4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755
		  escape_essid(priv->essid, priv->essid_len),
		  MAC_ARG(priv->bssid));

	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:
4756 4757
	 *
	 * 1.  Missed beacon threshold triggers the roaming process by
J
James Ketrenos 已提交
4758 4759 4760 4761 4762 4763 4764 4765 4766 4767
	 *     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
4768
	 *     association request.
J
James Ketrenos 已提交
4769 4770 4771 4772 4773 4774 4775 4776
	 * 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;
4777

J
James Ketrenos 已提交
4778
	if (priv->status & STATUS_ASSOCIATED) {
4779
		/* First pass through ROAM process -- look for a better
J
James Ketrenos 已提交
4780 4781 4782 4783 4784 4785 4786 4787
		 * network */
		u8 rssi = priv->assoc_network->stats.rssi;
		priv->assoc_network->stats.rssi = -128;
		list_for_each_entry(network, &priv->ieee->network_list, list) {
			if (network != priv->assoc_network)
				ipw_best_network(priv, &match, network, 1);
		}
		priv->assoc_network->stats.rssi = rssi;
4788

J
James Ketrenos 已提交
4789 4790 4791 4792 4793 4794 4795
		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;
		}
4796

J
James Ketrenos 已提交
4797 4798 4799 4800
		ipw_send_disassociate(priv, 1);
		priv->assoc_network = match.network;

		return;
4801
	}
J
James Ketrenos 已提交
4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821

	/* 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;
}

static void ipw_associate(void *data)
{
	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;

	if (!(priv->config & CFG_ASSOCIATE) &&
	    !(priv->config & (CFG_STATIC_ESSID |
4822
			      CFG_STATIC_CHANNEL | CFG_STATIC_BSSID))) {
J
James Ketrenos 已提交
4823 4824 4825 4826
		IPW_DEBUG_ASSOC("Not attempting association (associate=0)\n");
		return;
	}

4827
	list_for_each_entry(network, &priv->ieee->network_list, list)
4828
	    ipw_best_network(priv, &match, network, 0);
J
James Ketrenos 已提交
4829 4830 4831 4832 4833 4834 4835 4836 4837 4838

	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 &&
	    !list_empty(&priv->ieee->network_free_list)) {
		element = priv->ieee->network_free_list.next;
4839
		network = list_entry(element, struct ieee80211_network, list);
J
James Ketrenos 已提交
4840 4841 4842 4843 4844
		ipw_adhoc_create(priv, network);
		rates = &priv->rates;
		list_del(element);
		list_add_tail(&network->list, &priv->ieee->network_list);
	}
4845

J
James Ketrenos 已提交
4846 4847 4848 4849 4850
	/* If we reached the end of the list, then we don't have any valid
	 * matching APs */
	if (!network) {
		ipw_debug_config(priv);

4851
		queue_delayed_work(priv->workqueue, &priv->request_scan,
J
James Ketrenos 已提交
4852
				   SCAN_INTERVAL);
4853

J
James Ketrenos 已提交
4854 4855 4856 4857 4858
		return;
	}

	ipw_associate_network(priv, network, rates, 0);
}
4859 4860

static inline void ipw_handle_data_packet(struct ipw_priv *priv,
4861 4862
					  struct ipw_rx_mem_buffer *rxb,
					  struct ieee80211_rx_stats *stats)
J
James Ketrenos 已提交
4863 4864 4865 4866 4867 4868
{
	struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)rxb->skb->data;

	/* We received data from the HW, so stop the watchdog */
	priv->net_dev->trans_start = jiffies;

4869
	/* We only process data packets if the
J
James Ketrenos 已提交
4870
	 * interface is open */
4871
	if (unlikely((pkt->u.frame.length + IPW_RX_FRAME_SIZE) >
J
James Ketrenos 已提交
4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884
		     skb_tailroom(rxb->skb))) {
		priv->ieee->stats.rx_errors++;
		priv->wstats.discard.misc++;
		IPW_DEBUG_DROP("Corruption detected! Oh no!\n");
		return;
	} else if (unlikely(!netif_running(priv->net_dev))) {
		priv->ieee->stats.rx_dropped++;
		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 */
4885
	skb_reserve(rxb->skb, offsetof(struct ipw_rx_packet, u.frame.data));
J
James Ketrenos 已提交
4886 4887 4888 4889 4890 4891

	/* Set the size of the skb to the size of the frame */
	skb_put(rxb->skb, pkt->u.frame.length);

	IPW_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);

4892
	if (!ieee80211_rx(priv->ieee, rxb->skb, stats))
J
James Ketrenos 已提交
4893
		priv->ieee->stats.rx_errors++;
4894
	else			/* ieee80211_rx succeeded, so it now owns the SKB */
J
James Ketrenos 已提交
4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925
		rxb->skb = NULL;
}

/*
 * 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;
	struct ieee80211_hdr *header;
	u32 r, w, i;
	u8 network_packet;

	r = ipw_read32(priv, CX2_RX_READ_INDEX);
	w = ipw_read32(priv, CX2_RX_WRITE_INDEX);
	i = (priv->rxq->processed + 1) % RX_QUEUE_SIZE;

	while (i != r) {
		rxb = priv->rxq->queue[i];
#ifdef CONFIG_IPW_DEBUG
		if (unlikely(rxb == NULL)) {
			printk(KERN_CRIT "Queue not allocated!\n");
			break;
		}
#endif
		priv->rxq->queue[i] = NULL;

		pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
4926
					    CX2_RX_BUF_SIZE,
J
James Ketrenos 已提交
4927 4928 4929 4930 4931
					    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,
4932
			     pkt->header.rx_seq_num, pkt->header.control_bits);
J
James Ketrenos 已提交
4933 4934

		switch (pkt->header.message_type) {
4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959
		case RX_FRAME_TYPE:	/* 802.11 frame */  {
				struct ieee80211_rx_stats stats = {
					.rssi = pkt->u.frame.rssi_dbm -
					    IPW_RSSI_TO_DBM,
					.signal = pkt->u.frame.signal,
					.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,
					.len = pkt->u.frame.length,
				};

				if (stats.rssi != 0)
					stats.mask |= IEEE80211_STATMASK_RSSI;
				if (stats.signal != 0)
					stats.mask |= IEEE80211_STATMASK_SIGNAL;
				if (stats.rate != 0)
					stats.mask |= IEEE80211_STATMASK_RATE;

				priv->rx_packets++;
J
James Ketrenos 已提交
4960 4961

#ifdef CONFIG_IPW_PROMISC
4962 4963 4964 4965 4966
				if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
					ipw_handle_data_packet(priv, rxb,
							       &stats);
					break;
				}
J
James Ketrenos 已提交
4967
#endif
4968

4969 4970 4971
				header =
				    (struct ieee80211_hdr *)(rxb->skb->data +
							     IPW_RX_FRAME_SIZE);
J
James Ketrenos 已提交
4972 4973
				/* TODO: Check Ad-Hoc dest/source and make sure
				 * that we are actually parsing these packets
4974
				 * correctly -- we should probably use the
J
James Ketrenos 已提交
4975 4976
				 * frame control of the packet and disregard
				 * the current iw_mode */
4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065
				switch (priv->ieee->iw_mode) {
				case IW_MODE_ADHOC:
					network_packet =
					    !memcmp(header->addr1,
						    priv->net_dev->dev_addr,
						    ETH_ALEN) ||
					    !memcmp(header->addr3,
						    priv->bssid, ETH_ALEN) ||
					    is_broadcast_ether_addr(header->
								    addr1)
					    || is_multicast_ether_addr(header->
								       addr1);
					break;

				case IW_MODE_INFRA:
				default:
					network_packet =
					    !memcmp(header->addr3,
						    priv->bssid, ETH_ALEN) ||
					    !memcmp(header->addr1,
						    priv->net_dev->dev_addr,
						    ETH_ALEN) ||
					    is_broadcast_ether_addr(header->
								    addr1)
					    || is_multicast_ether_addr(header->
								       addr1);
					break;
				}

				if (network_packet && priv->assoc_network) {
					priv->assoc_network->stats.rssi =
					    stats.rssi;
					average_add(&priv->average_rssi,
						    stats.rssi);
					priv->last_rx_rssi = stats.rssi;
				}

				IPW_DEBUG_RX("Frame: len=%u\n",
					     pkt->u.frame.length);

				if (pkt->u.frame.length < frame_hdr_len(header)) {
					IPW_DEBUG_DROP
					    ("Received packet is too small. "
					     "Dropping.\n");
					priv->ieee->stats.rx_errors++;
					priv->wstats.discard.misc++;
					break;
				}

				switch (WLAN_FC_GET_TYPE(header->frame_ctl)) {
				case IEEE80211_FTYPE_MGMT:
					ieee80211_rx_mgt(priv->ieee, header,
							 &stats);
					if (priv->ieee->iw_mode == IW_MODE_ADHOC
					    &&
					    ((WLAN_FC_GET_STYPE
					      (header->frame_ctl) ==
					      IEEE80211_STYPE_PROBE_RESP)
					     ||
					     (WLAN_FC_GET_STYPE
					      (header->frame_ctl) ==
					      IEEE80211_STYPE_BEACON))
					    && !memcmp(header->addr3,
						       priv->bssid, ETH_ALEN))
						ipw_add_station(priv,
								header->addr2);
					break;

				case IEEE80211_FTYPE_CTL:
					break;

				case IEEE80211_FTYPE_DATA:
					if (network_packet)
						ipw_handle_data_packet(priv,
								       rxb,
								       &stats);
					else
						IPW_DEBUG_DROP("Dropping: "
							       MAC_FMT ", "
							       MAC_FMT ", "
							       MAC_FMT "\n",
							       MAC_ARG(header->
								       addr1),
							       MAC_ARG(header->
								       addr2),
							       MAC_ARG(header->
								       addr3));
					break;
				}
J
James Ketrenos 已提交
5066 5067
				break;
			}
5068

5069 5070 5071
		case RX_HOST_NOTIFICATION_TYPE:{
				IPW_DEBUG_RX
				    ("Notification: subtype=%02X flags=%02X size=%d\n",
J
James Ketrenos 已提交
5072 5073 5074
				     pkt->u.notification.subtype,
				     pkt->u.notification.flags,
				     pkt->u.notification.size);
5075 5076 5077
				ipw_rx_notification(priv, &pkt->u.notification);
				break;
			}
J
James Ketrenos 已提交
5078 5079 5080 5081 5082 5083

		default:
			IPW_DEBUG_RX("Bad Rx packet of type %d\n",
				     pkt->header.message_type);
			break;
		}
5084 5085 5086

		/* 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 已提交
5087 5088 5089 5090 5091
		 * fail to Rx correctly */
		if (rxb->skb != NULL) {
			dev_kfree_skb_any(rxb->skb);
			rxb->skb = NULL;
		}
5092

J
James Ketrenos 已提交
5093 5094 5095
		pci_unmap_single(priv->pci_dev, rxb->dma_addr,
				 CX2_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
		list_add_tail(&rxb->list, &priv->rxq->rx_used);
5096

J
James Ketrenos 已提交
5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116
		i = (i + 1) % RX_QUEUE_SIZE;
	}

	/* Backtrack one entry */
	priv->rxq->processed = (i ? i : RX_QUEUE_SIZE) - 1;

	ipw_rx_queue_restock(priv);
}

static void ipw_abort_scan(struct ipw_priv *priv)
{
	int err;

	if (priv->status & STATUS_SCAN_ABORTING) {
		IPW_DEBUG_HC("Ignoring concurrent scan abort request.\n");
		return;
	}
	priv->status |= STATUS_SCAN_ABORTING;

	err = ipw_send_scan_abort(priv);
5117
	if (err)
J
James Ketrenos 已提交
5118 5119 5120 5121 5122 5123 5124 5125
		IPW_DEBUG_HC("Request to abort scan failed.\n");
}

static int ipw_request_scan(struct ipw_priv *priv)
{
	struct ipw_scan_request_ext scan;
	int channel_index = 0;
	int i, err, scan_type;
5126

J
James Ketrenos 已提交
5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138
	if (priv->status & STATUS_EXIT_PENDING) {
		IPW_DEBUG_SCAN("Aborting scan due to device shutdown\n");
		priv->status |= STATUS_SCAN_PENDING;
		return 0;
	}

	if (priv->status & STATUS_SCANNING) {
		IPW_DEBUG_HC("Concurrent scan requested.  Aborting first.\n");
		priv->status |= STATUS_SCAN_PENDING;
		ipw_abort_scan(priv);
		return 0;
	}
5139

J
James Ketrenos 已提交
5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159
	if (priv->status & STATUS_SCAN_ABORTING) {
		IPW_DEBUG_HC("Scan request while abort pending.  Queuing.\n");
		priv->status |= STATUS_SCAN_PENDING;
		return 0;
	}

	if (priv->status & STATUS_RF_KILL_MASK) {
		IPW_DEBUG_HC("Aborting scan due to RF Kill activation\n");
		priv->status |= STATUS_SCAN_PENDING;
		return 0;
	}

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

	scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_SCAN] = 20;
	scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN] = 20;
	scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] = 20;

	scan.full_scan_index = ieee80211_get_scans(priv->ieee);
	/* If we are roaming, then make this a directed scan for the current
5160
	 * network.  Otherwise, ensure that every other scan is a fast
J
James Ketrenos 已提交
5161
	 * channel hop scan */
5162 5163 5164 5165
	if ((priv->status & STATUS_ROAMING)
	    || (!(priv->status & STATUS_ASSOCIATED)
		&& (priv->config & CFG_STATIC_ESSID)
		&& (scan.full_scan_index % 2))) {
J
James Ketrenos 已提交
5166 5167 5168 5169 5170
		err = ipw_send_ssid(priv, priv->essid, priv->essid_len);
		if (err) {
			IPW_DEBUG_HC("Attempt to send SSID command failed.\n");
			return err;
		}
5171

J
James Ketrenos 已提交
5172 5173 5174 5175
		scan_type = IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN;
	} else {
		scan_type = IPW_SCAN_ACTIVE_BROADCAST_SCAN;
	}
5176

5177
	if (priv->ieee->freq_band & IEEE80211_52GHZ_BAND) {
J
James Ketrenos 已提交
5178 5179 5180 5181 5182 5183 5184 5185
		int start = channel_index;
		for (i = 0; i < MAX_A_CHANNELS; i++) {
			if (band_a_active_channel[i] == 0)
				break;
			if ((priv->status & STATUS_ASSOCIATED) &&
			    band_a_active_channel[i] == priv->channel)
				continue;
			channel_index++;
5186
			scan.channels_list[channel_index] =
5187
			    band_a_active_channel[i];
J
James Ketrenos 已提交
5188 5189
			ipw_set_scan_type(&scan, channel_index, scan_type);
		}
5190

J
James Ketrenos 已提交
5191
		if (start != channel_index) {
5192 5193
			scan.channels_list[start] = (u8) (IPW_A_MODE << 6) |
			    (channel_index - start);
J
James Ketrenos 已提交
5194 5195 5196 5197
			channel_index++;
		}
	}

5198
	if (priv->ieee->freq_band & IEEE80211_24GHZ_BAND) {
J
James Ketrenos 已提交
5199 5200 5201 5202 5203 5204 5205 5206
		int start = channel_index;
		for (i = 0; i < MAX_B_CHANNELS; i++) {
			if (band_b_active_channel[i] == 0)
				break;
			if ((priv->status & STATUS_ASSOCIATED) &&
			    band_b_active_channel[i] == priv->channel)
				continue;
			channel_index++;
5207
			scan.channels_list[channel_index] =
5208
			    band_b_active_channel[i];
J
James Ketrenos 已提交
5209 5210 5211 5212
			ipw_set_scan_type(&scan, channel_index, scan_type);
		}

		if (start != channel_index) {
5213 5214
			scan.channels_list[start] = (u8) (IPW_B_MODE << 6) |
			    (channel_index - start);
J
James Ketrenos 已提交
5215 5216
		}
	}
5217

J
James Ketrenos 已提交
5218 5219
	err = ipw_send_scan_request_ext(priv, &scan);
	if (err) {
5220
		IPW_DEBUG_HC("Sending scan command failed: %08X\n", err);
J
James Ketrenos 已提交
5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233
		return -EIO;
	}

	priv->status |= STATUS_SCANNING;
	priv->status &= ~STATUS_SCAN_PENDING;

	return 0;
}

/*
 * 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.
5234 5235
 *
 * The exception to this is the use of the ipw_get_ordinal()
J
James Ketrenos 已提交
5236 5237 5238 5239
 * function used to poll the hardware vs. making unecessary calls.
 *
 */

5240 5241
static int ipw_wx_get_name(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
5242 5243 5244 5245 5246
			   union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	if (!(priv->status & STATUS_ASSOCIATED))
		strcpy(wrqu->name, "unassociated");
5247
	else
J
James Ketrenos 已提交
5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271
		snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11%c",
			 ipw_modes[priv->assoc_request.ieee_mode]);
	IPW_DEBUG_WX("Name: %s\n", wrqu->name);
	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;
		if (!(priv->status & (STATUS_SCANNING | STATUS_ASSOCIATED |
				      STATUS_ASSOCIATING))) {
			IPW_DEBUG_ASSOC("Attempting to associate with new "
					"parameters.\n");
			ipw_associate(priv);
		}

		return 0;
	}

	priv->config |= CFG_STATIC_CHANNEL;

	if (priv->channel == channel) {
5272 5273
		IPW_DEBUG_INFO("Request to set channel to current value (%d)\n",
			       channel);
J
James Ketrenos 已提交
5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291
		return 0;
	}

	IPW_DEBUG_INFO("Setting channel to %i\n", (int)channel);
	priv->channel = channel;

	/* If we are currently associated, or trying to associate
	 * then see if this is a new channel (causing us to disassociate) */
	if (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
		IPW_DEBUG_ASSOC("Disassociating due to channel change.\n");
		ipw_disassociate(priv);
	} else {
		ipw_associate(priv);
	}

	return 0;
}

5292 5293 5294
static int ipw_wx_set_freq(struct net_device *dev,
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
5295 5296 5297
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	struct iw_freq *fwrq = &wrqu->freq;
5298

J
James Ketrenos 已提交
5299 5300
	/* if setting by freq convert to channel */
	if (fwrq->e == 1) {
5301
		if ((fwrq->m >= (int)2.412e8 && fwrq->m <= (int)2.487e8)) {
J
James Ketrenos 已提交
5302 5303
			int f = fwrq->m / 100000;
			int c = 0;
5304

J
James Ketrenos 已提交
5305 5306 5307
			while ((c < REG_MAX_CHANNEL) &&
			       (f != ipw_frequencies[c]))
				c++;
5308

J
James Ketrenos 已提交
5309 5310 5311 5312 5313
			/* hack to fall through */
			fwrq->e = 0;
			fwrq->m = c + 1;
		}
	}
5314 5315

	if (fwrq->e > 0 || fwrq->m > 1000)
J
James Ketrenos 已提交
5316 5317 5318
		return -EOPNOTSUPP;

	IPW_DEBUG_WX("SET Freq/Channel -> %d \n", fwrq->m);
5319
	return ipw_set_channel(priv, (u8) fwrq->m);
5320

J
James Ketrenos 已提交
5321 5322 5323
	return 0;
}

5324 5325
static int ipw_wx_get_freq(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336
			   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 */
	if (priv->config & CFG_STATIC_CHANNEL ||
	    priv->status & (STATUS_ASSOCIATING | STATUS_ASSOCIATED))
		wrqu->freq.m = priv->channel;
5337
	else
J
James Ketrenos 已提交
5338 5339 5340 5341 5342 5343
		wrqu->freq.m = 0;

	IPW_DEBUG_WX("GET Freq/Channel -> %d \n", priv->channel);
	return 0;
}

5344 5345
static int ipw_wx_set_mode(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370
			   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);

	if (wrqu->mode == priv->ieee->iw_mode)
		return 0;

	switch (wrqu->mode) {
#ifdef CONFIG_IPW_PROMISC
	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;
	}

#ifdef CONFIG_IPW_PROMISC
5371
	if (priv->ieee->iw_mode == IW_MODE_MONITOR)
J
James Ketrenos 已提交
5372
		priv->net_dev->type = ARPHRD_ETHER;
5373 5374

	if (wrqu->mode == IW_MODE_MONITOR)
J
James Ketrenos 已提交
5375
		priv->net_dev->type = ARPHRD_IEEE80211;
5376
#endif				/* CONFIG_IPW_PROMISC */
5377

J
James Ketrenos 已提交
5378
#ifdef CONFIG_PM
5379
	/* Free the existing firmware and reset the fw_loaded
J
James Ketrenos 已提交
5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392
	 * flag so ipw_load() will bring in the new firmawre */
	if (fw_loaded) {
		fw_loaded = 0;
	}

	release_firmware(bootfw);
	release_firmware(ucode);
	release_firmware(firmware);
	bootfw = ucode = firmware = NULL;
#endif

	priv->ieee->iw_mode = wrqu->mode;
	ipw_adapter_restart(priv);
5393

5394
	return err;
J
James Ketrenos 已提交
5395 5396
}

5397
static int ipw_wx_get_mode(struct net_device *dev,
5398 5399
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432
{
	struct ipw_priv *priv = ieee80211_priv(dev);

	wrqu->mode = priv->ieee->iw_mode;
	IPW_DEBUG_WX("Get MODE -> %d\n", wrqu->mode);

	return 0;
}

#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

/* Values are in microsecond */
static const s32 timeout_duration[] = {
	350000,
	250000,
	75000,
	37000,
	25000,
};

static const s32 period_duration[] = {
	400000,
	700000,
	1000000,
	1000000,
	1000000
};

5433 5434
static int ipw_wx_get_range(struct net_device *dev,
			    struct iw_request_info *info,
J
James Ketrenos 已提交
5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445
			    union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	struct iw_range *range = (struct iw_range *)extra;
	u16 val;
	int i;

	wrqu->data.length = sizeof(*range);
	memset(range, 0, sizeof(*range));

	/* 54Mbs == ~27 Mb/s real (802.11g) */
5446
	range->throughput = 27 * 1000 * 1000;
J
James Ketrenos 已提交
5447 5448 5449 5450 5451

	range->max_qual.qual = 100;
	/* TODO: Find real max RSSI and stick here */
	range->max_qual.level = 0;
	range->max_qual.noise = 0;
5452
	range->max_qual.updated = 7;	/* Updated all three */
J
James Ketrenos 已提交
5453 5454 5455

	range->avg_qual.qual = 70;
	/* TODO: Find real 'good' to 'bad' threshol value for RSSI */
5456
	range->avg_qual.level = 0;	/* FIXME to real average level */
J
James Ketrenos 已提交
5457
	range->avg_qual.noise = 0;
5458
	range->avg_qual.updated = 7;	/* Updated all three */
J
James Ketrenos 已提交
5459

5460
	range->num_bitrates = min(priv->rates.num_rates, (u8) IW_MAX_BITRATES);
J
James Ketrenos 已提交
5461

5462 5463
	for (i = 0; i < range->num_bitrates; i++)
		range->bitrate[i] = (priv->rates.supported_rates[i] & 0x7F) *
5464
		    500000;
5465

J
James Ketrenos 已提交
5466 5467 5468 5469 5470
	range->max_rts = DEFAULT_RTS_THRESHOLD;
	range->min_frag = MIN_FRAG_THRESHOLD;
	range->max_frag = MAX_FRAG_THRESHOLD;

	range->encoding_size[0] = 5;
5471
	range->encoding_size[1] = 13;
J
James Ketrenos 已提交
5472 5473 5474 5475 5476 5477 5478
	range->num_encoding_sizes = 2;
	range->max_encoding_tokens = WEP_KEYS;

	/* Set the Wireless Extension versions */
	range->we_version_compiled = WIRELESS_EXT;
	range->we_version_source = 16;

5479
	range->num_channels = FREQ_COUNT;
J
James Ketrenos 已提交
5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496

	val = 0;
	for (i = 0; i < FREQ_COUNT; i++) {
		range->freq[val].i = i + 1;
		range->freq[val].m = ipw_frequencies[i] * 100000;
		range->freq[val].e = 1;
		val++;

		if (val == IW_MAX_FREQUENCIES)
			break;
	}
	range->num_frequency = val;

	IPW_DEBUG_WX("GET Range\n");
	return 0;
}

5497 5498
static int ipw_wx_set_wap(struct net_device *dev,
			  struct iw_request_info *info,
J
James Ketrenos 已提交
5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509
			  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
	};

5510
	if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
J
James Ketrenos 已提交
5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550
		return -EINVAL;

	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;
		if (!(priv->status & (STATUS_SCANNING | STATUS_ASSOCIATED |
				      STATUS_ASSOCIATING))) {
			IPW_DEBUG_ASSOC("Attempting to associate with new "
					"parameters.\n");
			ipw_associate(priv);
		}

		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");
		return 0;
	}

	IPW_DEBUG_WX("Setting mandatory BSSID to " MAC_FMT "\n",
		     MAC_ARG(wrqu->ap_addr.sa_data));

	memcpy(priv->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);

	/* If we are currently associated, or trying to associate
	 * then see if this is a new BSSID (causing us to disassociate) */
	if (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
		IPW_DEBUG_ASSOC("Disassociating due to BSSID change.\n");
		ipw_disassociate(priv);
	} else {
		ipw_associate(priv);
	}

	return 0;
}

5551 5552
static int ipw_wx_get_wap(struct net_device *dev,
			  struct iw_request_info *info,
J
James Ketrenos 已提交
5553 5554 5555 5556 5557
			  union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	/* If we are associated, trying to associate, or have a statically
	 * configured BSSID then return that; otherwise return ANY */
5558
	if (priv->config & CFG_STATIC_BSSID ||
J
James Ketrenos 已提交
5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569
	    priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
		wrqu->ap_addr.sa_family = ARPHRD_ETHER;
		memcpy(wrqu->ap_addr.sa_data, &priv->bssid, ETH_ALEN);
	} else
		memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);

	IPW_DEBUG_WX("Getting WAP BSSID: " MAC_FMT "\n",
		     MAC_ARG(wrqu->ap_addr.sa_data));
	return 0;
}

5570 5571
static int ipw_wx_set_essid(struct net_device *dev,
			    struct iw_request_info *info,
J
James Ketrenos 已提交
5572 5573 5574
			    union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
5575
	char *essid = "";	/* ANY */
J
James Ketrenos 已提交
5576
	int length = 0;
5577

J
James Ketrenos 已提交
5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608
	if (wrqu->essid.flags && wrqu->essid.length) {
		length = wrqu->essid.length - 1;
		essid = extra;
	}
	if (length == 0) {
		IPW_DEBUG_WX("Setting ESSID to ANY\n");
		priv->config &= ~CFG_STATIC_ESSID;
		if (!(priv->status & (STATUS_SCANNING | STATUS_ASSOCIATED |
				      STATUS_ASSOCIATING))) {
			IPW_DEBUG_ASSOC("Attempting to associate with new "
					"parameters.\n");
			ipw_associate(priv);
		}

		return 0;
	}

	length = min(length, IW_ESSID_MAX_SIZE);

	priv->config |= CFG_STATIC_ESSID;

	if (priv->essid_len == length && !memcmp(priv->essid, extra, length)) {
		IPW_DEBUG_WX("ESSID set to current ESSID.\n");
		return 0;
	}

	IPW_DEBUG_WX("Setting ESSID: '%s' (%d)\n", escape_essid(essid, length),
		     length);

	priv->essid_len = length;
	memcpy(priv->essid, essid, priv->essid_len);
5609

J
James Ketrenos 已提交
5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621
	/* If we are currently associated, or trying to associate
	 * then see if this is a new ESSID (causing us to disassociate) */
	if (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
		IPW_DEBUG_ASSOC("Disassociating due to ESSID change.\n");
		ipw_disassociate(priv);
	} else {
		ipw_associate(priv);
	}

	return 0;
}

5622 5623
static int ipw_wx_get_essid(struct net_device *dev,
			    struct iw_request_info *info,
J
James Ketrenos 已提交
5624 5625 5626 5627 5628 5629 5630
			    union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);

	/* If we are associated, trying to associate, or have a statically
	 * configured ESSID then return that; otherwise return ANY */
	if (priv->config & CFG_STATIC_ESSID ||
5631 5632
	    priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
		IPW_DEBUG_WX("Getting essid: '%s'\n",
J
James Ketrenos 已提交
5633
			     escape_essid(priv->essid, priv->essid_len));
5634
		memcpy(extra, priv->essid, priv->essid_len);
J
James Ketrenos 已提交
5635
		wrqu->essid.length = priv->essid_len;
5636
		wrqu->essid.flags = 1;	/* active */
J
James Ketrenos 已提交
5637 5638 5639
	} else {
		IPW_DEBUG_WX("Getting essid: ANY\n");
		wrqu->essid.length = 0;
5640
		wrqu->essid.flags = 0;	/* active */
J
James Ketrenos 已提交
5641 5642 5643 5644 5645
	}

	return 0;
}

5646 5647
static int ipw_wx_set_nick(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
5648
			   union iwreq_data *wrqu, char *extra)
5649
{
J
James Ketrenos 已提交
5650 5651 5652 5653 5654 5655
	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;

5656
	wrqu->data.length = min((size_t) wrqu->data.length, sizeof(priv->nick));
J
James Ketrenos 已提交
5657
	memset(priv->nick, 0, sizeof(priv->nick));
5658
	memcpy(priv->nick, extra, wrqu->data.length);
J
James Ketrenos 已提交
5659 5660 5661 5662 5663
	IPW_DEBUG_TRACE("<<\n");
	return 0;

}

5664 5665
static int ipw_wx_get_nick(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
5666
			   union iwreq_data *wrqu, char *extra)
5667
{
J
James Ketrenos 已提交
5668 5669 5670 5671
	struct ipw_priv *priv = ieee80211_priv(dev);
	IPW_DEBUG_WX("Getting nick\n");
	wrqu->data.length = strlen(priv->nick) + 1;
	memcpy(extra, priv->nick, wrqu->data.length);
5672
	wrqu->data.flags = 1;	/* active */
J
James Ketrenos 已提交
5673 5674 5675 5676 5677 5678
	return 0;
}

static int ipw_wx_set_rate(struct net_device *dev,
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
5679
{
J
James Ketrenos 已提交
5680
	IPW_DEBUG_WX("0x%p, 0x%p, 0x%p\n", dev, info, wrqu);
5681
	return -EOPNOTSUPP;
J
James Ketrenos 已提交
5682 5683
}

5684 5685
static int ipw_wx_get_rate(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
5686
			   union iwreq_data *wrqu, char *extra)
5687
{
5688
	struct ipw_priv *priv = ieee80211_priv(dev);
J
James Ketrenos 已提交
5689 5690 5691 5692 5693 5694
	wrqu->bitrate.value = priv->last_rate;

	IPW_DEBUG_WX("GET Rate -> %d \n", wrqu->bitrate.value);
	return 0;
}

5695 5696
static int ipw_wx_set_rts(struct net_device *dev,
			  struct iw_request_info *info,
J
James Ketrenos 已提交
5697
			  union iwreq_data *wrqu, char *extra)
5698
{
J
James Ketrenos 已提交
5699 5700 5701 5702 5703 5704 5705 5706
	struct ipw_priv *priv = ieee80211_priv(dev);

	if (wrqu->rts.disabled)
		priv->rts_threshold = DEFAULT_RTS_THRESHOLD;
	else {
		if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
		    wrqu->rts.value > MAX_RTS_THRESHOLD)
			return -EINVAL;
5707

J
James Ketrenos 已提交
5708 5709 5710 5711 5712 5713 5714 5715
		priv->rts_threshold = wrqu->rts.value;
	}

	ipw_send_rts_threshold(priv, priv->rts_threshold);
	IPW_DEBUG_WX("SET RTS Threshold -> %d \n", priv->rts_threshold);
	return 0;
}

5716 5717
static int ipw_wx_get_rts(struct net_device *dev,
			  struct iw_request_info *info,
J
James Ketrenos 已提交
5718
			  union iwreq_data *wrqu, char *extra)
5719
{
J
James Ketrenos 已提交
5720 5721 5722
	struct ipw_priv *priv = ieee80211_priv(dev);
	wrqu->rts.value = priv->rts_threshold;
	wrqu->rts.fixed = 0;	/* no auto select */
5723
	wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
J
James Ketrenos 已提交
5724 5725 5726 5727 5728

	IPW_DEBUG_WX("GET RTS Threshold -> %d \n", wrqu->rts.value);
	return 0;
}

5729 5730
static int ipw_wx_set_txpow(struct net_device *dev,
			    struct iw_request_info *info,
J
James Ketrenos 已提交
5731
			    union iwreq_data *wrqu, char *extra)
5732
{
J
James Ketrenos 已提交
5733 5734 5735 5736 5737 5738 5739 5740 5741 5742
	struct ipw_priv *priv = ieee80211_priv(dev);
	struct ipw_tx_power tx_power;
	int i;

	if (ipw_radio_kill_sw(priv, wrqu->power.disabled))
		return -EINPROGRESS;

	if (wrqu->power.flags != IW_TXPOW_DBM)
		return -EINVAL;

5743
	if ((wrqu->power.value > 20) || (wrqu->power.value < -12))
J
James Ketrenos 已提交
5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766
		return -EINVAL;

	priv->tx_power = wrqu->power.value;

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

	/* configure device for 'G' band */
	tx_power.ieee_mode = IPW_G_MODE;
	tx_power.num_channels = 11;
	for (i = 0; i < 11; i++) {
		tx_power.channels_tx_power[i].channel_number = i + 1;
		tx_power.channels_tx_power[i].tx_power = priv->tx_power;
	}
	if (ipw_send_tx_power(priv, &tx_power))
		goto error;

	/* configure device to also handle 'B' band */
	tx_power.ieee_mode = IPW_B_MODE;
	if (ipw_send_tx_power(priv, &tx_power))
		goto error;

	return 0;

5767
      error:
J
James Ketrenos 已提交
5768 5769 5770
	return -EIO;
}

5771 5772
static int ipw_wx_get_txpow(struct net_device *dev,
			    struct iw_request_info *info,
J
James Ketrenos 已提交
5773
			    union iwreq_data *wrqu, char *extra)
5774
{
J
James Ketrenos 已提交
5775 5776 5777 5778 5779 5780 5781
	struct ipw_priv *priv = ieee80211_priv(dev);

	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;

5782
	IPW_DEBUG_WX("GET TX Power -> %s %d \n",
5783
		     wrqu->power.disabled ? "ON" : "OFF", wrqu->power.value);
J
James Ketrenos 已提交
5784 5785 5786 5787

	return 0;
}

5788
static int ipw_wx_set_frag(struct net_device *dev,
5789 5790
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
5791 5792 5793 5794 5795 5796 5797 5798 5799
{
	struct ipw_priv *priv = ieee80211_priv(dev);

	if (wrqu->frag.disabled)
		priv->ieee->fts = DEFAULT_FTS;
	else {
		if (wrqu->frag.value < MIN_FRAG_THRESHOLD ||
		    wrqu->frag.value > MAX_FRAG_THRESHOLD)
			return -EINVAL;
5800

J
James Ketrenos 已提交
5801 5802 5803 5804 5805 5806 5807 5808
		priv->ieee->fts = wrqu->frag.value & ~0x1;
	}

	ipw_send_frag_threshold(priv, wrqu->frag.value);
	IPW_DEBUG_WX("SET Frag Threshold -> %d \n", wrqu->frag.value);
	return 0;
}

5809
static int ipw_wx_get_frag(struct net_device *dev,
5810 5811
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
5812 5813 5814 5815
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	wrqu->frag.value = priv->ieee->fts;
	wrqu->frag.fixed = 0;	/* no auto select */
5816
	wrqu->frag.disabled = (wrqu->frag.value == DEFAULT_FTS);
J
James Ketrenos 已提交
5817 5818 5819 5820 5821 5822

	IPW_DEBUG_WX("GET Frag Threshold -> %d \n", wrqu->frag.value);

	return 0;
}

5823 5824
static int ipw_wx_set_retry(struct net_device *dev,
			    struct iw_request_info *info,
J
James Ketrenos 已提交
5825
			    union iwreq_data *wrqu, char *extra)
5826
{
J
James Ketrenos 已提交
5827
	IPW_DEBUG_WX("0x%p, 0x%p, 0x%p\n", dev, info, wrqu);
5828
	return -EOPNOTSUPP;
J
James Ketrenos 已提交
5829 5830
}

5831 5832
static int ipw_wx_get_retry(struct net_device *dev,
			    struct iw_request_info *info,
J
James Ketrenos 已提交
5833
			    union iwreq_data *wrqu, char *extra)
5834
{
J
James Ketrenos 已提交
5835
	IPW_DEBUG_WX("0x%p, 0x%p, 0x%p\n", dev, info, wrqu);
5836
	return -EOPNOTSUPP;
J
James Ketrenos 已提交
5837 5838
}

5839 5840
static int ipw_wx_set_scan(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
5841 5842 5843 5844 5845 5846 5847 5848 5849
			   union iwreq_data *wrqu, char *extra)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	IPW_DEBUG_WX("Start scan\n");
	if (ipw_request_scan(priv))
		return -EIO;
	return 0;
}

5850 5851
static int ipw_wx_get_scan(struct net_device *dev,
			   struct iw_request_info *info,
J
James Ketrenos 已提交
5852
			   union iwreq_data *wrqu, char *extra)
5853
{
J
James Ketrenos 已提交
5854 5855 5856 5857
	struct ipw_priv *priv = ieee80211_priv(dev);
	return ieee80211_wx_get_scan(priv->ieee, info, wrqu, extra);
}

5858
static int ipw_wx_set_encode(struct net_device *dev,
5859 5860
			     struct iw_request_info *info,
			     union iwreq_data *wrqu, char *key)
J
James Ketrenos 已提交
5861 5862 5863 5864 5865
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	return ieee80211_wx_set_encode(priv->ieee, info, wrqu, key);
}

5866
static int ipw_wx_get_encode(struct net_device *dev,
5867 5868
			     struct iw_request_info *info,
			     union iwreq_data *wrqu, char *key)
J
James Ketrenos 已提交
5869 5870 5871 5872 5873
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	return ieee80211_wx_get_encode(priv->ieee, info, wrqu, key);
}

5874
static int ipw_wx_set_power(struct net_device *dev,
5875 5876
			    struct iw_request_info *info,
			    union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	int err;

	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");
			return err;
		}

		IPW_DEBUG_WX("SET Power Management Mode -> off\n");

		return 0;
5892
	}
J
James Ketrenos 已提交
5893 5894

	switch (wrqu->power.flags & IW_POWER_MODE) {
5895 5896 5897
	case IW_POWER_ON:	/* If not specified */
	case IW_POWER_MODE:	/* If set all mask */
	case IW_POWER_ALL_R:	/* If explicitely state all */
J
James Ketrenos 已提交
5898
		break;
5899
	default:		/* Otherwise we don't support it */
J
James Ketrenos 已提交
5900 5901
		IPW_DEBUG_WX("SET PM Mode: %X not supported.\n",
			     wrqu->power.flags);
5902
		return -EOPNOTSUPP;
J
James Ketrenos 已提交
5903
	}
5904

J
James Ketrenos 已提交
5905 5906
	/* If the user hasn't specified a power management mode yet, default
	 * to BATTERY */
5907
	if (IPW_POWER_LEVEL(priv->power_mode) == IPW_POWER_AC)
J
James Ketrenos 已提交
5908
		priv->power_mode = IPW_POWER_ENABLED | IPW_POWER_BATTERY;
5909
	else
J
James Ketrenos 已提交
5910 5911 5912 5913 5914 5915 5916
		priv->power_mode = IPW_POWER_ENABLED | priv->power_mode;
	err = ipw_send_power_mode(priv, IPW_POWER_LEVEL(priv->power_mode));
	if (err) {
		IPW_DEBUG_WX("failed setting power mode.\n");
		return err;
	}

5917
	IPW_DEBUG_WX("SET Power Management Mode -> 0x%02X\n", priv->power_mode);
5918

J
James Ketrenos 已提交
5919 5920 5921
	return 0;
}

5922
static int ipw_wx_get_power(struct net_device *dev,
5923 5924
			    struct iw_request_info *info,
			    union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
5925 5926 5927 5928 5929 5930 5931 5932 5933 5934
{
	struct ipw_priv *priv = ieee80211_priv(dev);

	if (!(priv->power_mode & IPW_POWER_ENABLED)) {
		wrqu->power.disabled = 1;
	} else {
		wrqu->power.disabled = 0;
	}

	IPW_DEBUG_WX("GET Power Management Mode -> %02X\n", priv->power_mode);
5935

J
James Ketrenos 已提交
5936 5937 5938
	return 0;
}

5939
static int ipw_wx_set_powermode(struct net_device *dev,
5940 5941
				struct iw_request_info *info,
				union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
5942 5943 5944 5945
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	int mode = *(int *)extra;
	int err;
5946

J
James Ketrenos 已提交
5947 5948 5949 5950 5951 5952
	if ((mode < 1) || (mode > IPW_POWER_LIMIT)) {
		mode = IPW_POWER_AC;
		priv->power_mode = mode;
	} else {
		priv->power_mode = IPW_POWER_ENABLED | mode;
	}
5953

J
James Ketrenos 已提交
5954 5955
	if (priv->power_mode != mode) {
		err = ipw_send_power_mode(priv, mode);
5956

J
James Ketrenos 已提交
5957 5958 5959 5960 5961
		if (err) {
			IPW_DEBUG_WX("failed setting power mode.\n");
			return err;
		}
	}
5962

J
James Ketrenos 已提交
5963 5964 5965 5966
	return 0;
}

#define MAX_WX_STRING 80
5967
static int ipw_wx_get_powermode(struct net_device *dev,
5968 5969
				struct iw_request_info *info,
				union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985
{
	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),
5986
			      "(Timeout %dms, Period %dms)",
J
James Ketrenos 已提交
5987 5988 5989 5990 5991
			      timeout_duration[level - 1] / 1000,
			      period_duration[level - 1] / 1000);
	}

	if (!(priv->power_mode & IPW_POWER_ENABLED))
5992
		p += snprintf(p, MAX_WX_STRING - (p - extra), " OFF");
J
James Ketrenos 已提交
5993 5994 5995 5996 5997 5998 5999

	wrqu->data.length = p - extra + 1;

	return 0;
}

static int ipw_wx_set_wireless_mode(struct net_device *dev,
6000 6001
				    struct iw_request_info *info,
				    union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
6002
{
6003
	struct ipw_priv *priv = ieee80211_priv(dev);
J
James Ketrenos 已提交
6004 6005 6006 6007
	int mode = *(int *)extra;
	u8 band = 0, modulation = 0;

	if (mode == 0 || mode & ~IEEE_MODE_MASK) {
6008
		IPW_WARNING("Attempt to set invalid wireless mode: %d\n", mode);
J
James Ketrenos 已提交
6009 6010
		return -EINVAL;
	}
6011

J
James Ketrenos 已提交
6012
	if (priv->adapter == IPW_2915ABG) {
6013
		priv->ieee->abg_true = 1;
J
James Ketrenos 已提交
6014 6015 6016 6017
		if (mode & IEEE_A) {
			band |= IEEE80211_52GHZ_BAND;
			modulation |= IEEE80211_OFDM_MODULATION;
		} else
6018
			priv->ieee->abg_true = 0;
J
James Ketrenos 已提交
6019 6020 6021 6022 6023 6024 6025
	} else {
		if (mode & IEEE_A) {
			IPW_WARNING("Attempt to set 2200BG into "
				    "802.11a mode\n");
			return -EINVAL;
		}

6026
		priv->ieee->abg_true = 0;
J
James Ketrenos 已提交
6027 6028 6029 6030 6031 6032
	}

	if (mode & IEEE_B) {
		band |= IEEE80211_24GHZ_BAND;
		modulation |= IEEE80211_CCK_MODULATION;
	} else
6033
		priv->ieee->abg_true = 0;
6034

J
James Ketrenos 已提交
6035 6036 6037 6038
	if (mode & IEEE_G) {
		band |= IEEE80211_24GHZ_BAND;
		modulation |= IEEE80211_OFDM_MODULATION;
	} else
6039
		priv->ieee->abg_true = 0;
J
James Ketrenos 已提交
6040 6041 6042 6043

	priv->ieee->mode = mode;
	priv->ieee->freq_band = band;
	priv->ieee->modulation = modulation;
6044
	init_supported_rates(priv, &priv->rates);
J
James Ketrenos 已提交
6045 6046

	/* If we are currently associated, or trying to associate
6047
	 * then see if this is a new configuration (causing us to
J
James Ketrenos 已提交
6048
	 * disassociate) */
6049
	if (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
6050
		/* The resulting association will trigger
J
James Ketrenos 已提交
6051
		 * the new rates to be sent to the device */
6052 6053
		IPW_DEBUG_ASSOC("Disassociating due to mode change.\n");
		ipw_disassociate(priv);
J
James Ketrenos 已提交
6054 6055 6056
	} else
		ipw_send_supported_rates(priv, &priv->rates);

6057
	IPW_DEBUG_WX("PRIV SET MODE: %c%c%c\n",
J
James Ketrenos 已提交
6058
		     mode & IEEE_A ? 'a' : '.',
6059
		     mode & IEEE_B ? 'b' : '.', mode & IEEE_G ? 'g' : '.');
J
James Ketrenos 已提交
6060 6061 6062 6063
	return 0;
}

static int ipw_wx_get_wireless_mode(struct net_device *dev,
6064 6065
				    struct iw_request_info *info,
				    union iwreq_data *wrqu, char *extra)
J
James Ketrenos 已提交
6066
{
6067
	struct ipw_priv *priv = ieee80211_priv(dev);
J
James Ketrenos 已提交
6068 6069 6070 6071 6072 6073 6074

	switch (priv->ieee->freq_band) {
	case IEEE80211_24GHZ_BAND:
		switch (priv->ieee->modulation) {
		case IEEE80211_CCK_MODULATION:
			strncpy(extra, "802.11b (2)", MAX_WX_STRING);
			break;
6075
		case IEEE80211_OFDM_MODULATION:
J
James Ketrenos 已提交
6076 6077 6078 6079 6080 6081 6082 6083
			strncpy(extra, "802.11g (4)", MAX_WX_STRING);
			break;
		default:
			strncpy(extra, "802.11bg (6)", MAX_WX_STRING);
			break;
		}
		break;

6084
	case IEEE80211_52GHZ_BAND:
J
James Ketrenos 已提交
6085 6086 6087
		strncpy(extra, "802.11a (1)", MAX_WX_STRING);
		break;

6088
	default:		/* Mixed Band */
J
James Ketrenos 已提交
6089 6090 6091 6092
		switch (priv->ieee->modulation) {
		case IEEE80211_CCK_MODULATION:
			strncpy(extra, "802.11ab (3)", MAX_WX_STRING);
			break;
6093
		case IEEE80211_OFDM_MODULATION:
J
James Ketrenos 已提交
6094 6095 6096 6097 6098 6099 6100
			strncpy(extra, "802.11ag (5)", MAX_WX_STRING);
			break;
		default:
			strncpy(extra, "802.11abg (7)", MAX_WX_STRING);
			break;
		}
		break;
6101 6102
	}

J
James Ketrenos 已提交
6103 6104
	IPW_DEBUG_WX("PRIV GET MODE: %s\n", extra);

6105
	wrqu->data.length = strlen(extra) + 1;
J
James Ketrenos 已提交
6106

6107
	return 0;
J
James Ketrenos 已提交
6108 6109 6110
}

#ifdef CONFIG_IPW_PROMISC
6111 6112
static int ipw_wx_set_promisc(struct net_device *dev,
			      struct iw_request_info *info,
J
James Ketrenos 已提交
6113
			      union iwreq_data *wrqu, char *extra)
6114
{
J
James Ketrenos 已提交
6115 6116 6117 6118 6119 6120 6121 6122 6123 6124
	struct ipw_priv *priv = ieee80211_priv(dev);
	int *parms = (int *)extra;
	int enable = (parms[0] > 0);

	IPW_DEBUG_WX("SET PROMISC: %d %d\n", enable, parms[1]);
	if (enable) {
		if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
			priv->net_dev->type = ARPHRD_IEEE80211;
			ipw_adapter_restart(priv);
		}
6125

J
James Ketrenos 已提交
6126 6127 6128 6129 6130 6131 6132 6133 6134 6135
		ipw_set_channel(priv, parms[1]);
	} else {
		if (priv->ieee->iw_mode != IW_MODE_MONITOR)
			return 0;
		priv->net_dev->type = ARPHRD_ETHER;
		ipw_adapter_restart(priv);
	}
	return 0;
}

6136 6137
static int ipw_wx_reset(struct net_device *dev,
			struct iw_request_info *info,
J
James Ketrenos 已提交
6138
			union iwreq_data *wrqu, char *extra)
6139
{
J
James Ketrenos 已提交
6140 6141 6142 6143 6144
	struct ipw_priv *priv = ieee80211_priv(dev);
	IPW_DEBUG_WX("RESET\n");
	ipw_adapter_restart(priv);
	return 0;
}
6145
#endif				// CONFIG_IPW_PROMISC
J
James Ketrenos 已提交
6146 6147 6148

/* Rebase the WE IOCTLs to zero for the handler array */
#define IW_IOCTL(x) [(x)-SIOCSIWCOMMIT]
6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177
static iw_handler ipw_wx_handlers[] = {
	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,
	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,
J
James Ketrenos 已提交
6178 6179 6180 6181 6182 6183 6184 6185 6186
};

#define IPW_PRIV_SET_POWER	SIOCIWFIRSTPRIV
#define IPW_PRIV_GET_POWER	SIOCIWFIRSTPRIV+1
#define IPW_PRIV_SET_MODE	SIOCIWFIRSTPRIV+2
#define IPW_PRIV_GET_MODE	SIOCIWFIRSTPRIV+3
#define IPW_PRIV_SET_PROMISC	SIOCIWFIRSTPRIV+4
#define IPW_PRIV_RESET		SIOCIWFIRSTPRIV+5

6187
static struct iw_priv_args ipw_priv_args[] = {
J
James Ketrenos 已提交
6188
	{
6189 6190 6191
	 .cmd = IPW_PRIV_SET_POWER,
	 .set_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
	 .name = "set_power"},
J
James Ketrenos 已提交
6192
	{
6193 6194 6195
	 .cmd = IPW_PRIV_GET_POWER,
	 .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
	 .name = "get_power"},
J
James Ketrenos 已提交
6196
	{
6197 6198 6199
	 .cmd = IPW_PRIV_SET_MODE,
	 .set_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
	 .name = "set_mode"},
J
James Ketrenos 已提交
6200
	{
6201 6202 6203
	 .cmd = IPW_PRIV_GET_MODE,
	 .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
	 .name = "get_mode"},
J
James Ketrenos 已提交
6204 6205
#ifdef CONFIG_IPW_PROMISC
	{
6206 6207
	 IPW_PRIV_SET_PROMISC,
	 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "monitor"},
J
James Ketrenos 已提交
6208
	{
6209 6210 6211
	 IPW_PRIV_RESET,
	 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 0, 0, "reset"},
#endif				/* CONFIG_IPW_PROMISC */
J
James Ketrenos 已提交
6212 6213 6214 6215 6216 6217 6218 6219 6220
};

static iw_handler ipw_priv_handler[] = {
	ipw_wx_set_powermode,
	ipw_wx_get_powermode,
	ipw_wx_set_wireless_mode,
	ipw_wx_get_wireless_mode,
#ifdef CONFIG_IPW_PROMISC
	ipw_wx_set_promisc,
6221
	ipw_wx_reset,
J
James Ketrenos 已提交
6222 6223 6224
#endif
};

6225 6226 6227 6228 6229 6230 6231
static struct iw_handler_def ipw_wx_handler_def = {
	.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,
J
James Ketrenos 已提交
6232 6233 6234 6235 6236 6237 6238
};

/*
 * Get wireless statistics.
 * Called by /proc/net/wireless
 * Also called by SIOCGIWSTATS
 */
6239
static struct iw_statistics *ipw_get_wireless_stats(struct net_device *dev)
J
James Ketrenos 已提交
6240 6241 6242
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	struct iw_statistics *wstats;
6243

J
James Ketrenos 已提交
6244 6245 6246
	wstats = &priv->wstats;

	/* if hw is disabled, then ipw2100_get_ordinal() can't be called.
6247
	 * ipw2100_wx_wireless_stats seems to be called before fw is
J
James Ketrenos 已提交
6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258
	 * 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 |
6259
		    IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID;
J
James Ketrenos 已提交
6260
		return wstats;
6261
	}
J
James Ketrenos 已提交
6262 6263 6264 6265 6266

	wstats->qual.qual = priv->quality;
	wstats->qual.level = average_value(&priv->average_rssi);
	wstats->qual.noise = average_value(&priv->average_noise);
	wstats->qual.updated = IW_QUAL_QUAL_UPDATED | IW_QUAL_LEVEL_UPDATED |
6267
	    IW_QUAL_NOISE_UPDATED;
J
James Ketrenos 已提交
6268 6269 6270 6271

	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;
6272

J
James Ketrenos 已提交
6273 6274 6275
/*	if (ipw_get_ordinal(priv, IPW_ORD_STAT_TX_RETRY, &tx_retry, &len))
	goto fail_get_ordinal;
	wstats->discard.retries += tx_retry; */
6276

J
James Ketrenos 已提交
6277 6278 6279 6280 6281 6282 6283
	return wstats;
}

/* net device stuff */

static inline void init_sys_config(struct ipw_sys_config *sys_config)
{
6284 6285
	memset(sys_config, 0, sizeof(struct ipw_sys_config));
	sys_config->bt_coexistence = 1;	/* We may need to look into prvStaBtConfig */
J
James Ketrenos 已提交
6286 6287 6288 6289 6290 6291 6292 6293
	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;
	sys_config->antenna_diversity = CFG_SYS_ANTENNA_BOTH;
6294
	sys_config->pass_crc_to_host = 0;	/* TODO: See if 1 gives us FCS */
J
James Ketrenos 已提交
6295
	sys_config->dot11g_auto_detection = 0;
6296
	sys_config->enable_cts_to_self = 0;
J
James Ketrenos 已提交
6297 6298 6299 6300 6301 6302 6303 6304 6305
	sys_config->bt_coexist_collision_thr = 0;
	sys_config->pass_noise_stats_to_host = 1;
}

static int ipw_net_open(struct net_device *dev)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	IPW_DEBUG_INFO("dev->open\n");
	/* we should be verifying the device is ready to be opened */
6306 6307
	if (!(priv->status & STATUS_RF_KILL_MASK) &&
	    (priv->status & STATUS_ASSOCIATED))
J
James Ketrenos 已提交
6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328
		netif_start_queue(dev);
	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.
*/

static inline void ipw_tx_skb(struct ipw_priv *priv, struct ieee80211_txb *txb)
{
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)
6329
	    txb->fragments[0]->data;
J
James Ketrenos 已提交
6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340
	int i = 0;
	struct tfd_frame *tfd;
	struct clx2_tx_queue *txq = &priv->txq[0];
	struct clx2_queue *q = &txq->q;
	u8 id, hdr_len, unicast;
	u16 remaining_bytes;

	switch (priv->ieee->iw_mode) {
	case IW_MODE_ADHOC:
		hdr_len = IEEE80211_3ADDR_LEN;
		unicast = !is_broadcast_ether_addr(hdr->addr1) &&
6341
		    !is_multicast_ether_addr(hdr->addr1);
J
James Ketrenos 已提交
6342 6343 6344 6345 6346
		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 "
6347
					    "invalid cell: " MAC_FMT "\n",
J
James Ketrenos 已提交
6348 6349 6350 6351 6352 6353 6354 6355 6356
					    MAC_ARG(hdr->addr1));
				goto drop;
			}
		}
		break;

	case IW_MODE_INFRA:
	default:
		unicast = !is_broadcast_ether_addr(hdr->addr3) &&
6357
		    !is_multicast_ether_addr(hdr->addr3);
J
James Ketrenos 已提交
6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377
		hdr_len = IEEE80211_3ADDR_LEN;
		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;
	tfd->u.data.len = txb->payload_size;
	remaining_bytes = txb->payload_size;
	if (unlikely(!unicast))
		tfd->u.data.tx_flags = DCT_FLAG_NO_WEP;
	else
		tfd->u.data.tx_flags = DCT_FLAG_NO_WEP | DCT_FLAG_ACK_REQD;
6378

J
James Ketrenos 已提交
6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389
	if (priv->assoc_request.ieee_mode == IPW_B_MODE)
		tfd->u.data.tx_flags_ext = DCT_FLAG_EXT_MODE_CCK;
	else
		tfd->u.data.tx_flags_ext = DCT_FLAG_EXT_MODE_OFDM;

	if (priv->config & CFG_PREAMBLE)
		tfd->u.data.tx_flags |= DCT_FLAG_SHORT_PREMBL;

	memcpy(&tfd->u.data.tfd.tfd_24.mchdr, hdr, hdr_len);

	/* payload */
6390
	tfd->u.data.num_chunks = min((u8) (NUM_TFD_CHUNKS - 2), txb->nr_frags);
J
James Ketrenos 已提交
6391
	for (i = 0; i < tfd->u.data.num_chunks; i++) {
6392
		IPW_DEBUG_TX("Dumping TX packet frag %i of %i (%d bytes):\n",
J
James Ketrenos 已提交
6393 6394
			     i, tfd->u.data.num_chunks,
			     txb->fragments[i]->len - hdr_len);
6395
		printk_buf(IPW_DL_TX, txb->fragments[i]->data + hdr_len,
J
James Ketrenos 已提交
6396 6397
			   txb->fragments[i]->len - hdr_len);

6398 6399 6400 6401 6402
		tfd->u.data.chunk_ptr[i] =
		    pci_map_single(priv->pci_dev,
				   txb->fragments[i]->data + hdr_len,
				   txb->fragments[i]->len - hdr_len,
				   PCI_DMA_TODEVICE);
J
James Ketrenos 已提交
6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421
		tfd->u.data.chunk_len[i] = txb->fragments[i]->len - hdr_len;
	}

	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) {
			tfd->u.data.chunk_len[i] = remaining_bytes;
			for (j = i; j < txb->nr_frags; j++) {
				int size = txb->fragments[j]->len - hdr_len;
				printk(KERN_INFO "Adding frag %d %d...\n",
6422
				       j, size);
J
James Ketrenos 已提交
6423
				memcpy(skb_put(skb, size),
6424
				       txb->fragments[j]->data + hdr_len, size);
J
James Ketrenos 已提交
6425 6426 6427
			}
			dev_kfree_skb_any(txb->fragments[i]);
			txb->fragments[i] = skb;
6428 6429 6430 6431
			tfd->u.data.chunk_ptr[i] =
			    pci_map_single(priv->pci_dev, skb->data,
					   tfd->u.data.chunk_len[i],
					   PCI_DMA_TODEVICE);
J
James Ketrenos 已提交
6432
			tfd->u.data.num_chunks++;
6433
		}
J
James Ketrenos 已提交
6434 6435 6436 6437 6438 6439
	}

	/* kick DMA */
	q->first_empty = ipw_queue_inc_wrap(q->first_empty, q->n_bd);
	ipw_write32(priv, q->reg_w, q->first_empty);

6440
	if (ipw_queue_space(q) < q->high_mark)
J
James Ketrenos 已提交
6441 6442 6443 6444
		netif_stop_queue(priv->net_dev);

	return;

6445
      drop:
J
James Ketrenos 已提交
6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471
	IPW_DEBUG_DROP("Silently dropping Tx packet.\n");
	ieee80211_txb_free(txb);
}

static int ipw_net_hard_start_xmit(struct ieee80211_txb *txb,
				   struct net_device *dev)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
	unsigned long flags;

	IPW_DEBUG_TX("dev->xmit(%d bytes)\n", txb->payload_size);

	spin_lock_irqsave(&priv->lock, flags);

	if (!(priv->status & STATUS_ASSOCIATED)) {
		IPW_DEBUG_INFO("Tx attempt while not associated.\n");
		priv->ieee->stats.tx_carrier_errors++;
		netif_stop_queue(dev);
		goto fail_unlock;
	}

	ipw_tx_skb(priv, txb);

	spin_unlock_irqrestore(&priv->lock, flags);
	return 0;

6472
      fail_unlock:
J
James Ketrenos 已提交
6473 6474 6475 6476 6477 6478 6479
	spin_unlock_irqrestore(&priv->lock, flags);
	return 1;
}

static struct net_device_stats *ipw_net_get_stats(struct net_device *dev)
{
	struct ipw_priv *priv = ieee80211_priv(dev);
6480

J
James Ketrenos 已提交
6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504
	priv->ieee->stats.tx_packets = priv->tx_packets;
	priv->ieee->stats.rx_packets = priv->rx_packets;
	return &priv->ieee->stats;
}

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;
	if (!is_valid_ether_addr(addr->sa_data))
		return -EADDRNOTAVAIL;
	priv->config |= CFG_CUSTOM_MAC;
	memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
	printk(KERN_INFO "%s: Setting MAC to " MAC_FMT "\n",
	       priv->net_dev->name, MAC_ARG(priv->mac_addr));
	ipw_adapter_restart(priv);
	return 0;
}

6505
static void ipw_ethtool_get_drvinfo(struct net_device *dev,
J
James Ketrenos 已提交
6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520
				    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);

6521
	snprintf(info->fw_version, sizeof(info->fw_version), "%s (%s)",
J
James Ketrenos 已提交
6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538
		 vers, date);
	strcpy(info->bus_info, pci_name(p->pci_dev));
	info->eedump_len = CX2_EEPROM_IMAGE_SIZE;
}

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)
{
	return CX2_EEPROM_IMAGE_SIZE;
}

static int ipw_ethtool_get_eeprom(struct net_device *dev,
6539
				  struct ethtool_eeprom *eeprom, u8 * bytes)
J
James Ketrenos 已提交
6540 6541 6542 6543 6544
{
	struct ipw_priv *p = ieee80211_priv(dev);

	if (eeprom->offset + eeprom->len > CX2_EEPROM_IMAGE_SIZE)
		return -EINVAL;
6545

6546
	memcpy(bytes, &((u8 *) p->eeprom)[eeprom->offset], eeprom->len);
J
James Ketrenos 已提交
6547 6548 6549 6550
	return 0;
}

static int ipw_ethtool_set_eeprom(struct net_device *dev,
6551
				  struct ethtool_eeprom *eeprom, u8 * bytes)
J
James Ketrenos 已提交
6552 6553 6554 6555 6556 6557 6558
{
	struct ipw_priv *p = ieee80211_priv(dev);
	int i;

	if (eeprom->offset + eeprom->len > CX2_EEPROM_IMAGE_SIZE)
		return -EINVAL;

6559
	memcpy(&((u8 *) p->eeprom)[eeprom->offset], bytes, eeprom->len);
6560
	for (i = IPW_EEPROM_DATA;
6561
	     i < IPW_EEPROM_DATA + CX2_EEPROM_IMAGE_SIZE; i++)
J
James Ketrenos 已提交
6562 6563 6564 6565 6566 6567
		ipw_write8(p, i, p->eeprom[i]);

	return 0;
}

static struct ethtool_ops ipw_ethtool_ops = {
6568 6569 6570 6571 6572
	.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 已提交
6573 6574 6575 6576 6577 6578
};

static irqreturn_t ipw_isr(int irq, void *data, struct pt_regs *regs)
{
	struct ipw_priv *priv = data;
	u32 inta, inta_mask;
6579

J
James Ketrenos 已提交
6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591
	if (!priv)
		return IRQ_NONE;

	spin_lock(&priv->lock);

	if (!(priv->status & STATUS_INT_ENABLED)) {
		/* Shared IRQ */
		goto none;
	}

	inta = ipw_read32(priv, CX2_INTA_RW);
	inta_mask = ipw_read32(priv, CX2_INTA_MASK_R);
6592

J
James Ketrenos 已提交
6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605
	if (inta == 0xFFFFFFFF) {
		/* Hardware disappeared */
		IPW_WARNING("IRQ INTA == 0xFFFFFFFF\n");
		goto none;
	}

	if (!(inta & (CX2_INTA_MASK_ALL & inta_mask))) {
		/* Shared interrupt */
		goto none;
	}

	/* tell the device to stop sending interrupts */
	ipw_disable_interrupts(priv);
6606

J
James Ketrenos 已提交
6607 6608 6609
	/* ack current interrupts */
	inta &= (CX2_INTA_MASK_ALL & inta_mask);
	ipw_write32(priv, CX2_INTA_RW, inta);
6610

J
James Ketrenos 已提交
6611 6612 6613 6614 6615
	/* Cache INTA value for our tasklet */
	priv->isr_inta = inta;

	tasklet_schedule(&priv->irq_tasklet);

6616
	spin_unlock(&priv->lock);
J
James Ketrenos 已提交
6617 6618

	return IRQ_HANDLED;
6619
      none:
J
James Ketrenos 已提交
6620 6621 6622 6623 6624 6625 6626 6627
	spin_unlock(&priv->lock);
	return IRQ_NONE;
}

static void ipw_rf_kill(void *adapter)
{
	struct ipw_priv *priv = adapter;
	unsigned long flags;
6628

J
James Ketrenos 已提交
6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646
	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);
6647
	} else
J
James Ketrenos 已提交
6648 6649 6650
		IPW_DEBUG_RF_KILL("HW RF Kill deactivated.  SW RF Kill still "
				  "enabled\n");

6651
      exit_unlock:
J
James Ketrenos 已提交
6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669
	spin_unlock_irqrestore(&priv->lock, flags);
}

static int ipw_setup_deferred_work(struct ipw_priv *priv)
{
	int ret = 0;

	priv->workqueue = create_workqueue(DRV_NAME);
	init_waitqueue_head(&priv->wait_command_queue);

	INIT_WORK(&priv->adhoc_check, ipw_adhoc_check, priv);
	INIT_WORK(&priv->associate, ipw_associate, priv);
	INIT_WORK(&priv->disassociate, ipw_disassociate, priv);
	INIT_WORK(&priv->rx_replenish, ipw_rx_queue_replenish, priv);
	INIT_WORK(&priv->adapter_restart, ipw_adapter_restart, priv);
	INIT_WORK(&priv->rf_kill, ipw_rf_kill, priv);
	INIT_WORK(&priv->up, (void (*)(void *))ipw_up, priv);
	INIT_WORK(&priv->down, (void (*)(void *))ipw_down, priv);
6670
	INIT_WORK(&priv->request_scan,
J
James Ketrenos 已提交
6671
		  (void (*)(void *))ipw_request_scan, priv);
6672
	INIT_WORK(&priv->gather_stats,
J
James Ketrenos 已提交
6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689
		  (void (*)(void *))ipw_gather_stats, priv);
	INIT_WORK(&priv->abort_scan, (void (*)(void *))ipw_abort_scan, priv);
	INIT_WORK(&priv->roam, ipw_roam, priv);
	INIT_WORK(&priv->scan_check, ipw_scan_check, priv);

	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;

6690
	for (i = 0; i < 4; i++) {
J
James Ketrenos 已提交
6691 6692 6693 6694 6695
		if (sec->flags & (1 << i)) {
			priv->sec.key_sizes[i] = sec->key_sizes[i];
			if (sec->key_sizes[i] == 0)
				priv->sec.flags &= ~(1 << i);
			else
6696
				memcpy(priv->sec.keys[i], sec->keys[i],
J
James Ketrenos 已提交
6697 6698 6699
				       sec->key_sizes[i]);
			priv->sec.flags |= (1 << i);
			priv->status |= STATUS_SECURITY_UPDATED;
6700
		}
J
James Ketrenos 已提交
6701 6702 6703 6704 6705 6706 6707
	}

	if ((sec->flags & SEC_ACTIVE_KEY) &&
	    priv->sec.active_key != sec->active_key) {
		if (sec->active_key <= 3) {
			priv->sec.active_key = sec->active_key;
			priv->sec.flags |= SEC_ACTIVE_KEY;
6708
		} else
J
James Ketrenos 已提交
6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722
			priv->sec.flags &= ~SEC_ACTIVE_KEY;
		priv->status |= STATUS_SECURITY_UPDATED;
	}

	if ((sec->flags & SEC_AUTH_MODE) &&
	    (priv->sec.auth_mode != sec->auth_mode)) {
		priv->sec.auth_mode = sec->auth_mode;
		priv->sec.flags |= SEC_AUTH_MODE;
		if (sec->auth_mode == WLAN_AUTH_SHARED_KEY)
			priv->capability |= CAP_SHARED_KEY;
		else
			priv->capability &= ~CAP_SHARED_KEY;
		priv->status |= STATUS_SECURITY_UPDATED;
	}
6723

6724
	if (sec->flags & SEC_ENABLED && priv->sec.enabled != sec->enabled) {
J
James Ketrenos 已提交
6725 6726 6727
		priv->sec.flags |= SEC_ENABLED;
		priv->sec.enabled = sec->enabled;
		priv->status |= STATUS_SECURITY_UPDATED;
6728
		if (sec->enabled)
J
James Ketrenos 已提交
6729 6730 6731 6732
			priv->capability |= CAP_PRIVACY_ON;
		else
			priv->capability &= ~CAP_PRIVACY_ON;
	}
6733

6734
	if (sec->flags & SEC_LEVEL && priv->sec.level != sec->level) {
J
James Ketrenos 已提交
6735 6736 6737 6738 6739
		priv->sec.level = sec->level;
		priv->sec.flags |= SEC_LEVEL;
		priv->status |= STATUS_SECURITY_UPDATED;
	}

6740 6741
	/* To match current functionality of ipw2100 (which works well w/
	 * various supplicants, we don't force a disassociate if the
J
James Ketrenos 已提交
6742 6743 6744
	 * privacy capability changes ... */
#if 0
	if ((priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) &&
6745
	    (((priv->assoc_request.capability &
J
James Ketrenos 已提交
6746
	       WLAN_CAPABILITY_PRIVACY) && !sec->enabled) ||
6747
	     (!(priv->assoc_request.capability &
6748
		WLAN_CAPABILITY_PRIVACY) && sec->enabled))) {
J
James Ketrenos 已提交
6749 6750 6751 6752 6753 6754 6755
		IPW_DEBUG_ASSOC("Disassociating due to capability "
				"change.\n");
		ipw_disassociate(priv);
	}
#endif
}

6756
static int init_supported_rates(struct ipw_priv *priv,
J
James Ketrenos 已提交
6757 6758 6759 6760 6761
				struct ipw_supported_rates *rates)
{
	/* TODO: Mask out rates based on priv->rates_mask */

	memset(rates, 0, sizeof(*rates));
6762
	/* configure supported rates */
J
James Ketrenos 已提交
6763 6764 6765 6766 6767 6768 6769 6770
	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;

6771
	default:		/* Mixed or 2.4Ghz */
J
James Ketrenos 已提交
6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785
		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;
}

6786
static int ipw_config(struct ipw_priv *priv)
J
James Ketrenos 已提交
6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821
{
	int i;
	struct ipw_tx_power tx_power;

	memset(&priv->sys_config, 0, sizeof(priv->sys_config));
	memset(&tx_power, 0, sizeof(tx_power));

	/* 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 */

	/* configure device for 'G' band */
	tx_power.ieee_mode = IPW_G_MODE;
	tx_power.num_channels = 11;
	for (i = 0; i < 11; i++) {
		tx_power.channels_tx_power[i].channel_number = i + 1;
		tx_power.channels_tx_power[i].tx_power = priv->tx_power;
	}
	if (ipw_send_tx_power(priv, &tx_power))
		goto error;

	/* configure device to also handle 'B' band */
	tx_power.ieee_mode = IPW_B_MODE;
	if (ipw_send_tx_power(priv, &tx_power))
		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);
	if (ipw_send_system_config(priv, &priv->sys_config))
		goto error;

6822 6823
	init_supported_rates(priv, &priv->rates);
	if (ipw_send_supported_rates(priv, &priv->rates))
J
James Ketrenos 已提交
6824 6825 6826 6827 6828 6829 6830 6831 6832 6833
		goto error;

	/* Set request-to-send threshold */
	if (priv->rts_threshold) {
		if (ipw_send_rts_threshold(priv, priv->rts_threshold))
			goto error;
	}

	if (ipw_set_random_seed(priv))
		goto error;
6834

J
James Ketrenos 已提交
6835 6836 6837 6838 6839 6840 6841 6842 6843
	/* final state transition to the RUN state */
	if (ipw_send_host_complete(priv))
		goto error;

	/* If configured to try and auto-associate, kick off a scan */
	if ((priv->config & CFG_ASSOCIATE) && ipw_request_scan(priv))
		goto error;

	return 0;
6844

6845
      error:
J
James Ketrenos 已提交
6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856
	return -EIO;
}

#define MAX_HW_RESTARTS 5
static int ipw_up(struct ipw_priv *priv)
{
	int rc, i;

	if (priv->status & STATUS_EXIT_PENDING)
		return -EIO;

6857
	for (i = 0; i < MAX_HW_RESTARTS; i++) {
6858
		/* Load the microcode, firmware, and eeprom.
J
James Ketrenos 已提交
6859 6860 6861
		 * Also start the clocks. */
		rc = ipw_load(priv);
		if (rc) {
6862
			IPW_ERROR("Unable to load firmware: 0x%08X\n", rc);
J
James Ketrenos 已提交
6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883
			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);

		if (priv->status & STATUS_RF_KILL_MASK)
			return 0;

		rc = ipw_config(priv);
		if (!rc) {
			IPW_DEBUG_INFO("Configured device on count %i\n", i);
			priv->notif_missed_beacons = 0;
			netif_start_queue(priv->net_dev);
			return 0;
		} else {
			IPW_DEBUG_INFO("Device configuration failed: 0x%08X\n",
				       rc);
		}
6884

J
James Ketrenos 已提交
6885 6886 6887 6888 6889 6890 6891 6892
		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);
	}

6893
	/* tried to restart and config the device for as long as our
J
James Ketrenos 已提交
6894
	 * patience could withstand */
6895
	IPW_ERROR("Unable to initialize device after %d attempts.\n", i);
J
James Ketrenos 已提交
6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 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 -EIO;
}

static void ipw_down(struct ipw_priv *priv)
{
	/* Attempt to disable the card */
#if 0
	ipw_send_card_disable(priv, 0);
#endif

	/* tell the device to stop sending interrupts */
	ipw_disable_interrupts(priv);

	/* Clear all bits but the RF Kill */
	priv->status &= STATUS_RF_KILL_MASK;

	netif_carrier_off(priv->net_dev);
	netif_stop_queue(priv->net_dev);

	ipw_stop_nic(priv);
}

/* Called by register_netdev() */
static int ipw_net_init(struct net_device *dev)
{
	struct ipw_priv *priv = ieee80211_priv(dev);

	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);
		return 0;
	}

	if (ipw_up(priv))
		return -EIO;

	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},
6960 6961 6962 6963
	{PCI_VENDOR_ID_INTEL, 0x4220, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},	/* BG */
	{PCI_VENDOR_ID_INTEL, 0x4221, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},	/* 2225BG */
	{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 */
6964

J
James Ketrenos 已提交
6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990
	/* 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,
	&dev_attr_dump_errors.attr,
	&dev_attr_dump_events.attr,
	&dev_attr_eeprom_delay.attr,
	&dev_attr_ucode_version.attr,
	&dev_attr_rtc.attr,
	NULL
};

static struct attribute_group ipw_attribute_group = {
	.name = NULL,		/* put in device directory */
6991
	.attrs = ipw_sysfs_entries,
J
James Ketrenos 已提交
6992 6993
};

6994
static int ipw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
J
James Ketrenos 已提交
6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024
{
	int err = 0;
	struct net_device *net_dev;
	void __iomem *base;
	u32 length, val;
	struct ipw_priv *priv;
	int band, modulation;

	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);
	priv->net_dev = net_dev;
	priv->pci_dev = pdev;
#ifdef CONFIG_IPW_DEBUG
	ipw_debug_level = debug;
#endif
	spin_lock_init(&priv->lock);

	if (pci_enable_device(pdev)) {
		err = -ENODEV;
		goto out_free_ieee80211;
	}

	pci_set_master(pdev);

7025
	err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7026
	if (!err)
7027
		err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
J
James Ketrenos 已提交
7028 7029 7030 7031 7032 7033 7034 7035
	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);
7036
	if (err)
J
James Ketrenos 已提交
7037 7038
		goto out_pci_disable_device;

7039
	/* We disable the RETRY_TIMEOUT register (0x41) to keep
J
James Ketrenos 已提交
7040
	 * PCI Tx retries from interfering with C3 CPU state */
7041 7042
	pci_read_config_dword(pdev, 0x40, &val);
	if ((val & 0x0000ff00) != 0)
J
James Ketrenos 已提交
7043
		pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
7044

J
James Ketrenos 已提交
7045 7046
	length = pci_resource_len(pdev, 0);
	priv->hw_len = length;
7047

J
James Ketrenos 已提交
7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067
	base = ioremap_nocache(pci_resource_start(pdev, 0), length);
	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;
	}

	/* Initialize module parameter values here */
	if (ifname)
		strncpy(net_dev->name, ifname, IFNAMSIZ);

7068
	if (associate)
J
James Ketrenos 已提交
7069 7070 7071
		priv->config |= CFG_ASSOCIATE;
	else
		IPW_DEBUG_INFO("Auto associate disabled.\n");
7072 7073

	if (auto_create)
J
James Ketrenos 已提交
7074 7075 7076
		priv->config |= CFG_ADHOC_CREATE;
	else
		IPW_DEBUG_INFO("Auto adhoc creation disabled.\n");
7077

J
James Ketrenos 已提交
7078 7079 7080 7081 7082 7083 7084 7085 7086
	if (disable) {
		priv->status |= STATUS_RF_KILL_SW;
		IPW_DEBUG_INFO("Radio disabled.\n");
	}

	if (channel != 0) {
		priv->config |= CFG_STATIC_CHANNEL;
		priv->channel = channel;
		IPW_DEBUG_INFO("Bind to static channel %d\n", channel);
7087
		IPW_DEBUG_INFO("Bind to static channel %d\n", channel);
J
James Ketrenos 已提交
7088 7089 7090 7091 7092 7093 7094
		/* TODO: Validate that provided channel is in range */
	}

	switch (mode) {
	case 1:
		priv->ieee->iw_mode = IW_MODE_ADHOC;
		break;
7095
#ifdef CONFIG_IPW_PROMISC
J
James Ketrenos 已提交
7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107
	case 2:
		priv->ieee->iw_mode = IW_MODE_MONITOR;
		break;
#endif
	default:
	case 0:
		priv->ieee->iw_mode = IW_MODE_INFRA;
		break;
	}

	if ((priv->pci_dev->device == 0x4223) ||
	    (priv->pci_dev->device == 0x4224)) {
7108
		printk(KERN_INFO DRV_NAME
J
James Ketrenos 已提交
7109 7110
		       ": Detected Intel PRO/Wireless 2915ABG Network "
		       "Connection\n");
7111
		priv->ieee->abg_true = 1;
J
James Ketrenos 已提交
7112 7113
		band = IEEE80211_52GHZ_BAND | IEEE80211_24GHZ_BAND;
		modulation = IEEE80211_OFDM_MODULATION |
7114
		    IEEE80211_CCK_MODULATION;
J
James Ketrenos 已提交
7115
		priv->adapter = IPW_2915ABG;
7116
		priv->ieee->mode = IEEE_A | IEEE_G | IEEE_B;
J
James Ketrenos 已提交
7117
	} else {
7118 7119
		if (priv->pci_dev->device == 0x4221)
			printk(KERN_INFO DRV_NAME
J
James Ketrenos 已提交
7120 7121 7122
			       ": Detected Intel PRO/Wireless 2225BG Network "
			       "Connection\n");
		else
7123
			printk(KERN_INFO DRV_NAME
J
James Ketrenos 已提交
7124 7125
			       ": Detected Intel PRO/Wireless 2200BG Network "
			       "Connection\n");
7126

7127
		priv->ieee->abg_true = 0;
J
James Ketrenos 已提交
7128 7129
		band = IEEE80211_24GHZ_BAND;
		modulation = IEEE80211_OFDM_MODULATION |
7130
		    IEEE80211_CCK_MODULATION;
J
James Ketrenos 已提交
7131
		priv->adapter = IPW_2200BG;
7132
		priv->ieee->mode = IEEE_G | IEEE_B;
J
James Ketrenos 已提交
7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145
	}

	priv->ieee->freq_band = band;
	priv->ieee->modulation = modulation;

	priv->rates_mask = IEEE80211_DEFAULT_RATES_MASK;

	priv->missed_beacon_threshold = IPW_MB_DISASSOCIATE_THRESHOLD_DEFAULT;
	priv->roaming_threshold = IPW_MB_ROAMING_THRESHOLD_DEFAULT;

	priv->rts_threshold = DEFAULT_RTS_THRESHOLD;

	/* If power management is turned on, default to AC mode */
7146
	priv->power_mode = IPW_POWER_AC;
J
James Ketrenos 已提交
7147 7148
	priv->tx_power = IPW_DEFAULT_TX_POWER;

7149
	err = request_irq(pdev->irq, ipw_isr, SA_SHIRQ, DRV_NAME, priv);
J
James Ketrenos 已提交
7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170
	if (err) {
		IPW_ERROR("Error allocating IRQ %d\n", pdev->irq);
		goto out_destroy_workqueue;
	}

	SET_MODULE_OWNER(net_dev);
	SET_NETDEV_DEV(net_dev, &pdev->dev);

	priv->ieee->hard_start_xmit = ipw_net_hard_start_xmit;
	priv->ieee->set_security = shim__set_security;

	net_dev->open = ipw_net_open;
	net_dev->stop = ipw_net_stop;
	net_dev->init = ipw_net_init;
	net_dev->get_stats = ipw_net_get_stats;
	net_dev->set_multicast_list = ipw_net_set_multicast_list;
	net_dev->set_mac_address = ipw_net_set_mac_address;
	net_dev->get_wireless_stats = ipw_get_wireless_stats;
	net_dev->wireless_handlers = &ipw_wx_handler_def;
	net_dev->ethtool_ops = &ipw_ethtool_ops;
	net_dev->irq = pdev->irq;
7171
	net_dev->base_addr = (unsigned long)priv->hw_base;
J
James Ketrenos 已提交
7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188
	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");
		goto out_release_irq;
	}

	err = register_netdev(net_dev);
	if (err) {
		IPW_ERROR("failed to register network device\n");
		goto out_remove_group;
	}

	return 0;

7189
      out_remove_group:
J
James Ketrenos 已提交
7190
	sysfs_remove_group(&pdev->dev.kobj, &ipw_attribute_group);
7191
      out_release_irq:
J
James Ketrenos 已提交
7192
	free_irq(pdev->irq, priv);
7193
      out_destroy_workqueue:
J
James Ketrenos 已提交
7194 7195
	destroy_workqueue(priv->workqueue);
	priv->workqueue = NULL;
7196
      out_iounmap:
J
James Ketrenos 已提交
7197
	iounmap(priv->hw_base);
7198
      out_pci_release_regions:
J
James Ketrenos 已提交
7199
	pci_release_regions(pdev);
7200
      out_pci_disable_device:
J
James Ketrenos 已提交
7201 7202
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);
7203
      out_free_ieee80211:
J
James Ketrenos 已提交
7204
	free_ieee80211(priv->net_dev);
7205
      out:
J
James Ketrenos 已提交
7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230
	return err;
}

static void ipw_pci_remove(struct pci_dev *pdev)
{
	struct ipw_priv *priv = pci_get_drvdata(pdev);
	if (!priv)
		return;

	priv->status |= STATUS_EXIT_PENDING;

	sysfs_remove_group(&pdev->dev.kobj, &ipw_attribute_group);

	ipw_down(priv);

	unregister_netdev(priv->net_dev);

	if (priv->rxq) {
		ipw_rx_queue_free(priv, priv->rxq);
		priv->rxq = NULL;
	}
	ipw_tx_queue_free(priv);

	/* ipw_down will ensure that there is no more pending work
	 * in the workqueue's, so we can safely remove them now. */
7231
	if (priv->workqueue) {
J
James Ketrenos 已提交
7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258
		cancel_delayed_work(&priv->adhoc_check);
		cancel_delayed_work(&priv->gather_stats);
		cancel_delayed_work(&priv->request_scan);
		cancel_delayed_work(&priv->rf_kill);
		cancel_delayed_work(&priv->scan_check);
		destroy_workqueue(priv->workqueue);
		priv->workqueue = NULL;
	}

	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);

#ifdef CONFIG_PM
	if (fw_loaded) {
		release_firmware(bootfw);
		release_firmware(ucode);
		release_firmware(firmware);
		fw_loaded = 0;
	}
#endif
}

#ifdef CONFIG_PM
7259
static int ipw_pci_suspend(struct pci_dev *pdev, pm_message_t state)
J
James Ketrenos 已提交
7260 7261 7262 7263 7264 7265
{
	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);

7266
	/* Take down the device; powers it off, etc. */
J
James Ketrenos 已提交
7267 7268 7269 7270 7271 7272 7273
	ipw_down(priv);

	/* Remove the PRESENT state of the device */
	netif_device_detach(dev);

	pci_save_state(pdev);
	pci_disable_device(pdev);
7274
	pci_set_power_state(pdev, pci_choose_state(pdev, state));
7275

J
James Ketrenos 已提交
7276 7277 7278 7279 7280 7281 7282 7283
	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;
	u32 val;
7284

J
James Ketrenos 已提交
7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299
	printk(KERN_INFO "%s: Coming out of suspend...\n", dev->name);

	pci_set_power_state(pdev, 0);
	pci_enable_device(pdev);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
	pci_restore_state(pdev, priv->pm_state);
#else
	pci_restore_state(pdev);
#endif
	/*
	 * 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.
	 */
7300 7301
	pci_read_config_dword(pdev, 0x40, &val);
	if ((val & 0x0000ff00) != 0)
J
James Ketrenos 已提交
7302 7303 7304 7305 7306 7307 7308 7309
		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);

	/* Bring the device back up */
	queue_work(priv->workqueue, &priv->up);
7310

J
James Ketrenos 已提交
7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339
	return 0;
}
#endif

/* 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
};

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");

	ret = pci_module_init(&ipw_driver);
	if (ret) {
		IPW_ERROR("Unable to initialize PCI module\n");
		return ret;
	}

7340
	ret = driver_create_file(&ipw_driver.driver, &driver_attr_debug_level);
J
James Ketrenos 已提交
7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368
	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);
MODULE_PARM_DESC(associate, "auto associate when scanning (default on)");

module_param(auto_create, int, 0444);
MODULE_PARM_DESC(auto_create, "auto create adhoc network (default on)");

module_param(debug, int, 0444);
MODULE_PARM_DESC(debug, "debug output mask");

module_param(channel, int, 0444);
7369
MODULE_PARM_DESC(channel, "channel to limit associate to (default 0 [ANY])");
J
James Ketrenos 已提交
7370 7371 7372 7373

module_param(ifname, charp, 0444);
MODULE_PARM_DESC(ifname, "network device name (default eth%d)");

7374
#ifdef CONFIG_IPW_PROMISC
J
James Ketrenos 已提交
7375 7376 7377 7378 7379 7380 7381 7382 7383
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

module_exit(ipw_exit);
module_init(ipw_init);