Linux Audio

Check our new training course

Loading...
v4.17
 
   1/*
   2 * Host AP (software wireless LAN access point) driver for
   3 * Intersil Prism2/2.5/3.
   4 *
   5 * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
   6 * <j@w1.fi>
   7 * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License version 2 as
  11 * published by the Free Software Foundation. See README and COPYING for
  12 * more details.
  13 *
  14 * FIX:
  15 * - there is currently no way of associating TX packets to correct wds device
  16 *   when TX Exc/OK event occurs, so all tx_packets and some
  17 *   tx_errors/tx_dropped are added to the main netdevice; using sw_support
  18 *   field in txdesc might be used to fix this (using Alloc event to increment
  19 *   tx_packets would need some further info in txfid table)
  20 *
  21 * Buffer Access Path (BAP) usage:
  22 *   Prism2 cards have two separate BAPs for accessing the card memory. These
  23 *   should allow concurrent access to two different frames and the driver
  24 *   previously used BAP0 for sending data and BAP1 for receiving data.
  25 *   However, there seems to be number of issues with concurrent access and at
  26 *   least one know hardware bug in using BAP0 and BAP1 concurrently with PCI
  27 *   Prism2.5. Therefore, the driver now only uses BAP0 for moving data between
  28 *   host and card memories. BAP0 accesses are protected with local->baplock
  29 *   (spin_lock_bh) to prevent concurrent use.
  30 */
  31
  32
  33
  34#include <asm/delay.h>
  35#include <linux/uaccess.h>
  36
  37#include <linux/slab.h>
  38#include <linux/netdevice.h>
  39#include <linux/etherdevice.h>
  40#include <linux/proc_fs.h>
  41#include <linux/seq_file.h>
  42#include <linux/if_arp.h>
  43#include <linux/delay.h>
  44#include <linux/random.h>
  45#include <linux/wait.h>
  46#include <linux/sched/signal.h>
  47#include <linux/rtnetlink.h>
  48#include <linux/wireless.h>
  49#include <net/iw_handler.h>
  50#include <net/lib80211.h>
  51#include <asm/irq.h>
  52
  53#include "hostap_80211.h"
  54#include "hostap.h"
  55#include "hostap_ap.h"
  56
  57
  58/* #define final_version */
  59
  60static int mtu = 1500;
  61module_param(mtu, int, 0444);
  62MODULE_PARM_DESC(mtu, "Maximum transfer unit");
  63
  64static int channel[MAX_PARM_DEVICES] = { 3, DEF_INTS };
  65module_param_array(channel, int, NULL, 0444);
  66MODULE_PARM_DESC(channel, "Initial channel");
  67
  68static char essid[33] = "test";
  69module_param_string(essid, essid, sizeof(essid), 0444);
  70MODULE_PARM_DESC(essid, "Host AP's ESSID");
  71
  72static int iw_mode[MAX_PARM_DEVICES] = { IW_MODE_MASTER, DEF_INTS };
  73module_param_array(iw_mode, int, NULL, 0444);
  74MODULE_PARM_DESC(iw_mode, "Initial operation mode");
  75
  76static int beacon_int[MAX_PARM_DEVICES] = { 100, DEF_INTS };
  77module_param_array(beacon_int, int, NULL, 0444);
  78MODULE_PARM_DESC(beacon_int, "Beacon interval (1 = 1024 usec)");
  79
  80static int dtim_period[MAX_PARM_DEVICES] = { 1, DEF_INTS };
  81module_param_array(dtim_period, int, NULL, 0444);
  82MODULE_PARM_DESC(dtim_period, "DTIM period");
  83
  84static char dev_template[16] = "wlan%d";
  85module_param_string(dev_template, dev_template, sizeof(dev_template), 0444);
  86MODULE_PARM_DESC(dev_template, "Prefix for network device name (default: "
  87		 "wlan%d)");
  88
  89#ifdef final_version
  90#define EXTRA_EVENTS_WTERR 0
  91#else
  92/* check WTERR events (Wait Time-out) in development versions */
  93#define EXTRA_EVENTS_WTERR HFA384X_EV_WTERR
  94#endif
  95
  96/* Events that will be using BAP0 */
  97#define HFA384X_BAP0_EVENTS \
  98	(HFA384X_EV_TXEXC | HFA384X_EV_RX | HFA384X_EV_INFO | HFA384X_EV_TX)
  99
 100/* event mask, i.e., events that will result in an interrupt */
 101#define HFA384X_EVENT_MASK \
 102	(HFA384X_BAP0_EVENTS | HFA384X_EV_ALLOC | HFA384X_EV_INFDROP | \
 103	HFA384X_EV_CMD | HFA384X_EV_TICK | \
 104	EXTRA_EVENTS_WTERR)
 105
 106/* Default TX control flags: use 802.11 headers and request interrupt for
 107 * failed transmits. Frames that request ACK callback, will add
 108 * _TX_OK flag and _ALT_RTRY flag may be used to select different retry policy.
 109 */
 110#define HFA384X_TX_CTRL_FLAGS \
 111	(HFA384X_TX_CTRL_802_11 | HFA384X_TX_CTRL_TX_EX)
 112
 113
 114/* ca. 1 usec */
 115#define HFA384X_CMD_BUSY_TIMEOUT 5000
 116#define HFA384X_BAP_BUSY_TIMEOUT 50000
 117
 118/* ca. 10 usec */
 119#define HFA384X_CMD_COMPL_TIMEOUT 20000
 120#define HFA384X_DL_COMPL_TIMEOUT 1000000
 121
 122/* Wait times for initialization; yield to other processes to avoid busy
 123 * waiting for long time. */
 124#define HFA384X_INIT_TIMEOUT (HZ / 2) /* 500 ms */
 125#define HFA384X_ALLOC_COMPL_TIMEOUT (HZ / 20) /* 50 ms */
 126
 127
 128static void prism2_hw_reset(struct net_device *dev);
 129static void prism2_check_sta_fw_version(local_info_t *local);
 130
 131#ifdef PRISM2_DOWNLOAD_SUPPORT
 132/* hostap_download.c */
 133static const struct file_operations prism2_download_aux_dump_proc_fops;
 134static u8 * prism2_read_pda(struct net_device *dev);
 135static int prism2_download(local_info_t *local,
 136			   struct prism2_download_param *param);
 137static void prism2_download_free_data(struct prism2_download_data *dl);
 138static int prism2_download_volatile(local_info_t *local,
 139				    struct prism2_download_data *param);
 140static int prism2_download_genesis(local_info_t *local,
 141				   struct prism2_download_data *param);
 142static int prism2_get_ram_size(local_info_t *local);
 143#endif /* PRISM2_DOWNLOAD_SUPPORT */
 144
 145
 146
 147
 148#ifndef final_version
 149/* magic value written to SWSUPPORT0 reg. for detecting whether card is still
 150 * present */
 151#define HFA384X_MAGIC 0x8A32
 152#endif
 153
 154
 155static u16 hfa384x_read_reg(struct net_device *dev, u16 reg)
 156{
 157	return HFA384X_INW(reg);
 158}
 159
 160
 161static void hfa384x_read_regs(struct net_device *dev,
 162			      struct hfa384x_regs *regs)
 163{
 164	regs->cmd = HFA384X_INW(HFA384X_CMD_OFF);
 165	regs->evstat = HFA384X_INW(HFA384X_EVSTAT_OFF);
 166	regs->offset0 = HFA384X_INW(HFA384X_OFFSET0_OFF);
 167	regs->offset1 = HFA384X_INW(HFA384X_OFFSET1_OFF);
 168	regs->swsupport0 = HFA384X_INW(HFA384X_SWSUPPORT0_OFF);
 169}
 170
 171
 172/**
 173 * __hostap_cmd_queue_free - Free Prism2 command queue entry (private)
 174 * @local: pointer to private Host AP driver data
 175 * @entry: Prism2 command queue entry to be freed
 176 * @del_req: request the entry to be removed
 177 *
 178 * Internal helper function for freeing Prism2 command queue entries.
 179 * Caller must have acquired local->cmdlock before calling this function.
 180 */
 181static inline void __hostap_cmd_queue_free(local_info_t *local,
 182					   struct hostap_cmd_queue *entry,
 183					   int del_req)
 184{
 185	if (del_req) {
 186		entry->del_req = 1;
 187		if (!list_empty(&entry->list)) {
 188			list_del_init(&entry->list);
 189			local->cmd_queue_len--;
 190		}
 191	}
 192
 193	if (refcount_dec_and_test(&entry->usecnt) && entry->del_req)
 194		kfree(entry);
 195}
 196
 197
 198/**
 199 * hostap_cmd_queue_free - Free Prism2 command queue entry
 200 * @local: pointer to private Host AP driver data
 201 * @entry: Prism2 command queue entry to be freed
 202 * @del_req: request the entry to be removed
 203 *
 204 * Free a Prism2 command queue entry.
 205 */
 206static inline void hostap_cmd_queue_free(local_info_t *local,
 207					 struct hostap_cmd_queue *entry,
 208					 int del_req)
 209{
 210	unsigned long flags;
 211
 212	spin_lock_irqsave(&local->cmdlock, flags);
 213	__hostap_cmd_queue_free(local, entry, del_req);
 214	spin_unlock_irqrestore(&local->cmdlock, flags);
 215}
 216
 217
 218/**
 219 * prism2_clear_cmd_queue - Free all pending Prism2 command queue entries
 220 * @local: pointer to private Host AP driver data
 221 */
 222static void prism2_clear_cmd_queue(local_info_t *local)
 223{
 224	struct list_head *ptr, *n;
 225	unsigned long flags;
 226	struct hostap_cmd_queue *entry;
 227
 228	spin_lock_irqsave(&local->cmdlock, flags);
 229	list_for_each_safe(ptr, n, &local->cmd_queue) {
 230		entry = list_entry(ptr, struct hostap_cmd_queue, list);
 231		refcount_inc(&entry->usecnt);
 232		printk(KERN_DEBUG "%s: removed pending cmd_queue entry "
 233		       "(type=%d, cmd=0x%04x, param0=0x%04x)\n",
 234		       local->dev->name, entry->type, entry->cmd,
 235		       entry->param0);
 236		__hostap_cmd_queue_free(local, entry, 1);
 237	}
 238	if (local->cmd_queue_len) {
 239		/* This should not happen; print debug message and clear
 240		 * queue length. */
 241		printk(KERN_DEBUG "%s: cmd_queue_len (%d) not zero after "
 242		       "flush\n", local->dev->name, local->cmd_queue_len);
 243		local->cmd_queue_len = 0;
 244	}
 245	spin_unlock_irqrestore(&local->cmdlock, flags);
 246}
 247
 248
 249/**
 250 * hfa384x_cmd_issue - Issue a Prism2 command to the hardware
 251 * @dev: pointer to net_device
 252 * @entry: Prism2 command queue entry to be issued
 253 */
 254static int hfa384x_cmd_issue(struct net_device *dev,
 255				    struct hostap_cmd_queue *entry)
 256{
 257	struct hostap_interface *iface;
 258	local_info_t *local;
 259	int tries;
 260	u16 reg;
 261	unsigned long flags;
 262
 263	iface = netdev_priv(dev);
 264	local = iface->local;
 265
 266	if (local->func->card_present && !local->func->card_present(local))
 267		return -ENODEV;
 268
 269	if (entry->issued) {
 270		printk(KERN_DEBUG "%s: driver bug - re-issuing command @%p\n",
 271		       dev->name, entry);
 272	}
 273
 274	/* wait until busy bit is clear; this should always be clear since the
 275	 * commands are serialized */
 276	tries = HFA384X_CMD_BUSY_TIMEOUT;
 277	while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
 278		tries--;
 279		udelay(1);
 280	}
 281#ifndef final_version
 282	if (tries != HFA384X_CMD_BUSY_TIMEOUT) {
 283		prism2_io_debug_error(dev, 1);
 284		printk(KERN_DEBUG "%s: hfa384x_cmd_issue: cmd reg was busy "
 285		       "for %d usec\n", dev->name,
 286		       HFA384X_CMD_BUSY_TIMEOUT - tries);
 287	}
 288#endif
 289	if (tries == 0) {
 290		reg = HFA384X_INW(HFA384X_CMD_OFF);
 291		prism2_io_debug_error(dev, 2);
 292		printk(KERN_DEBUG "%s: hfa384x_cmd_issue - timeout - "
 293		       "reg=0x%04x\n", dev->name, reg);
 294		return -ETIMEDOUT;
 295	}
 296
 297	/* write command */
 298	spin_lock_irqsave(&local->cmdlock, flags);
 299	HFA384X_OUTW(entry->param0, HFA384X_PARAM0_OFF);
 300	HFA384X_OUTW(entry->param1, HFA384X_PARAM1_OFF);
 301	HFA384X_OUTW(entry->cmd, HFA384X_CMD_OFF);
 302	entry->issued = 1;
 303	spin_unlock_irqrestore(&local->cmdlock, flags);
 304
 305	return 0;
 306}
 307
 308
 309/**
 310 * hfa384x_cmd - Issue a Prism2 command and wait (sleep) for completion
 311 * @dev: pointer to net_device
 312 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
 313 * @param0: value for Param0 register
 314 * @param1: value for Param1 register (pointer; %NULL if not used)
 315 * @resp0: pointer for Resp0 data or %NULL if Resp0 is not needed
 316 *
 317 * Issue given command (possibly after waiting in command queue) and sleep
 318 * until the command is completed (or timed out or interrupted). This can be
 319 * called only from user process context.
 320 */
 321static int hfa384x_cmd(struct net_device *dev, u16 cmd, u16 param0,
 322		       u16 *param1, u16 *resp0)
 323{
 324	struct hostap_interface *iface;
 325	local_info_t *local;
 326	int err, res, issue, issued = 0;
 327	unsigned long flags;
 328	struct hostap_cmd_queue *entry;
 329	DECLARE_WAITQUEUE(wait, current);
 330
 331	iface = netdev_priv(dev);
 332	local = iface->local;
 333
 334	if (in_interrupt()) {
 335		printk(KERN_DEBUG "%s: hfa384x_cmd called from interrupt "
 336		       "context\n", dev->name);
 337		return -1;
 338	}
 339
 340	if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN) {
 341		printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
 342		       dev->name);
 343		return -1;
 344	}
 345
 346	if (signal_pending(current))
 347		return -EINTR;
 348
 349	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
 350	if (entry == NULL)
 351		return -ENOMEM;
 352
 353	refcount_set(&entry->usecnt, 1);
 354	entry->type = CMD_SLEEP;
 355	entry->cmd = cmd;
 356	entry->param0 = param0;
 357	if (param1)
 358		entry->param1 = *param1;
 359	init_waitqueue_head(&entry->compl);
 360
 361	/* prepare to wait for command completion event, but do not sleep yet
 362	 */
 363	add_wait_queue(&entry->compl, &wait);
 364	set_current_state(TASK_INTERRUPTIBLE);
 365
 366	spin_lock_irqsave(&local->cmdlock, flags);
 367	issue = list_empty(&local->cmd_queue);
 368	if (issue)
 369		entry->issuing = 1;
 370	list_add_tail(&entry->list, &local->cmd_queue);
 371	local->cmd_queue_len++;
 372	spin_unlock_irqrestore(&local->cmdlock, flags);
 373
 374	err = 0;
 375	if (!issue)
 376		goto wait_completion;
 377
 378	if (signal_pending(current))
 379		err = -EINTR;
 380
 381	if (!err) {
 382		if (hfa384x_cmd_issue(dev, entry))
 383			err = -ETIMEDOUT;
 384		else
 385			issued = 1;
 386	}
 387
 388 wait_completion:
 389	if (!err && entry->type != CMD_COMPLETED) {
 390		/* sleep until command is completed or timed out */
 391		res = schedule_timeout(2 * HZ);
 392	} else
 393		res = -1;
 394
 395	if (!err && signal_pending(current))
 396		err = -EINTR;
 397
 398	if (err && issued) {
 399		/* the command was issued, so a CmdCompl event should occur
 400		 * soon; however, there's a pending signal and
 401		 * schedule_timeout() would be interrupted; wait a short period
 402		 * of time to avoid removing entry from the list before
 403		 * CmdCompl event */
 404		udelay(300);
 405	}
 406
 407	set_current_state(TASK_RUNNING);
 408	remove_wait_queue(&entry->compl, &wait);
 409
 410	/* If entry->list is still in the list, it must be removed
 411	 * first and in this case prism2_cmd_ev() does not yet have
 412	 * local reference to it, and the data can be kfree()'d
 413	 * here. If the command completion event is still generated,
 414	 * it will be assigned to next (possibly) pending command, but
 415	 * the driver will reset the card anyway due to timeout
 416	 *
 417	 * If the entry is not in the list prism2_cmd_ev() has a local
 418	 * reference to it, but keeps cmdlock as long as the data is
 419	 * needed, so the data can be kfree()'d here. */
 420
 421	/* FIX: if the entry->list is in the list, it has not been completed
 422	 * yet, so removing it here is somewhat wrong.. this could cause
 423	 * references to freed memory and next list_del() causing NULL pointer
 424	 * dereference.. it would probably be better to leave the entry in the
 425	 * list and the list should be emptied during hw reset */
 426
 427	spin_lock_irqsave(&local->cmdlock, flags);
 428	if (!list_empty(&entry->list)) {
 429		printk(KERN_DEBUG "%s: hfa384x_cmd: entry still in list? "
 430		       "(entry=%p, type=%d, res=%d)\n", dev->name, entry,
 431		       entry->type, res);
 432		list_del_init(&entry->list);
 433		local->cmd_queue_len--;
 434	}
 435	spin_unlock_irqrestore(&local->cmdlock, flags);
 436
 437	if (err) {
 438		printk(KERN_DEBUG "%s: hfa384x_cmd: interrupted; err=%d\n",
 439		       dev->name, err);
 440		res = err;
 441		goto done;
 442	}
 443
 444	if (entry->type != CMD_COMPLETED) {
 445		u16 reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
 446		printk(KERN_DEBUG "%s: hfa384x_cmd: command was not "
 447		       "completed (res=%d, entry=%p, type=%d, cmd=0x%04x, "
 448		       "param0=0x%04x, EVSTAT=%04x INTEN=%04x)\n", dev->name,
 449		       res, entry, entry->type, entry->cmd, entry->param0, reg,
 450		       HFA384X_INW(HFA384X_INTEN_OFF));
 451		if (reg & HFA384X_EV_CMD) {
 452			/* Command completion event is pending, but the
 453			 * interrupt was not delivered - probably an issue
 454			 * with pcmcia-cs configuration. */
 455			printk(KERN_WARNING "%s: interrupt delivery does not "
 456			       "seem to work\n", dev->name);
 457		}
 458		prism2_io_debug_error(dev, 3);
 459		res = -ETIMEDOUT;
 460		goto done;
 461	}
 462
 463	if (resp0 != NULL)
 464		*resp0 = entry->resp0;
 465#ifndef final_version
 466	if (entry->res) {
 467		printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x, "
 468		       "resp0=0x%04x\n",
 469		       dev->name, cmd, entry->res, entry->resp0);
 470	}
 471#endif /* final_version */
 472
 473	res = entry->res;
 474 done:
 475	hostap_cmd_queue_free(local, entry, 1);
 476	return res;
 477}
 478
 479
 480/**
 481 * hfa384x_cmd_callback - Issue a Prism2 command; callback when completed
 482 * @dev: pointer to net_device
 483 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
 484 * @param0: value for Param0 register
 485 * @callback: command completion callback function (%NULL = no callback)
 486 * @context: context data to be given to the callback function
 487 *
 488 * Issue given command (possibly after waiting in command queue) and use
 489 * callback function to indicate command completion. This can be called both
 490 * from user and interrupt context. The callback function will be called in
 491 * hardware IRQ context. It can be %NULL, when no function is called when
 492 * command is completed.
 493 */
 494static int hfa384x_cmd_callback(struct net_device *dev, u16 cmd, u16 param0,
 495				void (*callback)(struct net_device *dev,
 496						 long context, u16 resp0,
 497						 u16 status),
 498				long context)
 499{
 500	struct hostap_interface *iface;
 501	local_info_t *local;
 502	int issue, ret;
 503	unsigned long flags;
 504	struct hostap_cmd_queue *entry;
 505
 506	iface = netdev_priv(dev);
 507	local = iface->local;
 508
 509	if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN + 2) {
 510		printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
 511		       dev->name);
 512		return -1;
 513	}
 514
 515	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
 516	if (entry == NULL)
 517		return -ENOMEM;
 518
 519	refcount_set(&entry->usecnt, 1);
 520	entry->type = CMD_CALLBACK;
 521	entry->cmd = cmd;
 522	entry->param0 = param0;
 523	entry->callback = callback;
 524	entry->context = context;
 525
 526	spin_lock_irqsave(&local->cmdlock, flags);
 527	issue = list_empty(&local->cmd_queue);
 528	if (issue)
 529		entry->issuing = 1;
 530	list_add_tail(&entry->list, &local->cmd_queue);
 531	local->cmd_queue_len++;
 532	spin_unlock_irqrestore(&local->cmdlock, flags);
 533
 534	if (issue && hfa384x_cmd_issue(dev, entry))
 535		ret = -ETIMEDOUT;
 536	else
 537		ret = 0;
 538
 539	hostap_cmd_queue_free(local, entry, ret);
 540
 541	return ret;
 542}
 543
 544
 545/**
 546 * __hfa384x_cmd_no_wait - Issue a Prism2 command (private)
 547 * @dev: pointer to net_device
 548 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
 549 * @param0: value for Param0 register
 550 * @io_debug_num: I/O debug error number
 551 *
 552 * Shared helper function for hfa384x_cmd_wait() and hfa384x_cmd_no_wait().
 553 */
 554static int __hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd, u16 param0,
 555				 int io_debug_num)
 556{
 557	int tries;
 558	u16 reg;
 559
 560	/* wait until busy bit is clear; this should always be clear since the
 561	 * commands are serialized */
 562	tries = HFA384X_CMD_BUSY_TIMEOUT;
 563	while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
 564		tries--;
 565		udelay(1);
 566	}
 567	if (tries == 0) {
 568		reg = HFA384X_INW(HFA384X_CMD_OFF);
 569		prism2_io_debug_error(dev, io_debug_num);
 570		printk(KERN_DEBUG "%s: __hfa384x_cmd_no_wait(%d) - timeout - "
 571		       "reg=0x%04x\n", dev->name, io_debug_num, reg);
 572		return -ETIMEDOUT;
 573	}
 574
 575	/* write command */
 576	HFA384X_OUTW(param0, HFA384X_PARAM0_OFF);
 577	HFA384X_OUTW(cmd, HFA384X_CMD_OFF);
 578
 579	return 0;
 580}
 581
 582
 583/**
 584 * hfa384x_cmd_wait - Issue a Prism2 command and busy wait for completion
 585 * @dev: pointer to net_device
 586 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
 587 * @param0: value for Param0 register
 588 */
 589static int hfa384x_cmd_wait(struct net_device *dev, u16 cmd, u16 param0)
 590{
 591	int res, tries;
 592	u16 reg;
 593
 594	res = __hfa384x_cmd_no_wait(dev, cmd, param0, 4);
 595	if (res)
 596		return res;
 597
 598        /* wait for command completion */
 599	if ((cmd & HFA384X_CMDCODE_MASK) == HFA384X_CMDCODE_DOWNLOAD)
 600		tries = HFA384X_DL_COMPL_TIMEOUT;
 601	else
 602		tries = HFA384X_CMD_COMPL_TIMEOUT;
 603
 604        while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
 605               tries > 0) {
 606                tries--;
 607                udelay(10);
 608        }
 609        if (tries == 0) {
 610                reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
 611		prism2_io_debug_error(dev, 5);
 612                printk(KERN_DEBUG "%s: hfa384x_cmd_wait - timeout2 - "
 613		       "reg=0x%04x\n", dev->name, reg);
 614                return -ETIMEDOUT;
 615        }
 616
 617        res = (HFA384X_INW(HFA384X_STATUS_OFF) &
 618               (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) | BIT(9) |
 619                BIT(8))) >> 8;
 620#ifndef final_version
 621	if (res) {
 622		printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x\n",
 623		       dev->name, cmd, res);
 624	}
 625#endif
 626
 627	HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
 628
 629	return res;
 630}
 631
 632
 633/**
 634 * hfa384x_cmd_no_wait - Issue a Prism2 command; do not wait for completion
 635 * @dev: pointer to net_device
 636 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
 637 * @param0: value for Param0 register
 638 */
 639static inline int hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd,
 640				      u16 param0)
 641{
 642	return __hfa384x_cmd_no_wait(dev, cmd, param0, 6);
 643}
 644
 645
 646/**
 647 * prism2_cmd_ev - Prism2 command completion event handler
 648 * @dev: pointer to net_device
 649 *
 650 * Interrupt handler for command completion events. Called by the main
 651 * interrupt handler in hardware IRQ context. Read Resp0 and status registers
 652 * from the hardware and ACK the event. Depending on the issued command type
 653 * either wake up the sleeping process that is waiting for command completion
 654 * or call the callback function. Issue the next command, if one is pending.
 655 */
 656static void prism2_cmd_ev(struct net_device *dev)
 657{
 658	struct hostap_interface *iface;
 659	local_info_t *local;
 660	struct hostap_cmd_queue *entry = NULL;
 661
 662	iface = netdev_priv(dev);
 663	local = iface->local;
 664
 665	spin_lock(&local->cmdlock);
 666	if (!list_empty(&local->cmd_queue)) {
 667		entry = list_entry(local->cmd_queue.next,
 668				   struct hostap_cmd_queue, list);
 669		refcount_inc(&entry->usecnt);
 670		list_del_init(&entry->list);
 671		local->cmd_queue_len--;
 672
 673		if (!entry->issued) {
 674			printk(KERN_DEBUG "%s: Command completion event, but "
 675			       "cmd not issued\n", dev->name);
 676			__hostap_cmd_queue_free(local, entry, 1);
 677			entry = NULL;
 678		}
 679	}
 680	spin_unlock(&local->cmdlock);
 681
 682	if (!entry) {
 683		HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
 684		printk(KERN_DEBUG "%s: Command completion event, but no "
 685		       "pending commands\n", dev->name);
 686		return;
 687	}
 688
 689	entry->resp0 = HFA384X_INW(HFA384X_RESP0_OFF);
 690	entry->res = (HFA384X_INW(HFA384X_STATUS_OFF) &
 691		      (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) |
 692		       BIT(9) | BIT(8))) >> 8;
 693	HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
 694
 695	/* TODO: rest of the CmdEv handling could be moved to tasklet */
 696	if (entry->type == CMD_SLEEP) {
 697		entry->type = CMD_COMPLETED;
 698		wake_up_interruptible(&entry->compl);
 699	} else if (entry->type == CMD_CALLBACK) {
 700		if (entry->callback)
 701			entry->callback(dev, entry->context, entry->resp0,
 702					entry->res);
 703	} else {
 704		printk(KERN_DEBUG "%s: Invalid command completion type %d\n",
 705		       dev->name, entry->type);
 706	}
 707	hostap_cmd_queue_free(local, entry, 1);
 708
 709	/* issue next command, if pending */
 710	entry = NULL;
 711	spin_lock(&local->cmdlock);
 712	if (!list_empty(&local->cmd_queue)) {
 713		entry = list_entry(local->cmd_queue.next,
 714				   struct hostap_cmd_queue, list);
 715		if (entry->issuing) {
 716			/* hfa384x_cmd() has already started issuing this
 717			 * command, so do not start here */
 718			entry = NULL;
 719		}
 720		if (entry)
 721			refcount_inc(&entry->usecnt);
 722	}
 723	spin_unlock(&local->cmdlock);
 724
 725	if (entry) {
 726		/* issue next command; if command issuing fails, remove the
 727		 * entry from cmd_queue */
 728		int res = hfa384x_cmd_issue(dev, entry);
 729		spin_lock(&local->cmdlock);
 730		__hostap_cmd_queue_free(local, entry, res);
 731		spin_unlock(&local->cmdlock);
 732	}
 733}
 734
 735
 736static int hfa384x_wait_offset(struct net_device *dev, u16 o_off)
 737{
 738	int tries = HFA384X_BAP_BUSY_TIMEOUT;
 739	int res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
 740
 741	while (res && tries > 0) {
 742		tries--;
 743		udelay(1);
 744		res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
 745	}
 746	return res;
 747}
 748
 749
 750/* Offset must be even */
 751static int hfa384x_setup_bap(struct net_device *dev, u16 bap, u16 id,
 752			     int offset)
 753{
 754	u16 o_off, s_off;
 755	int ret = 0;
 756
 757	if (offset % 2 || bap > 1)
 758		return -EINVAL;
 759
 760	if (bap == BAP1) {
 761		o_off = HFA384X_OFFSET1_OFF;
 762		s_off = HFA384X_SELECT1_OFF;
 763	} else {
 764		o_off = HFA384X_OFFSET0_OFF;
 765		s_off = HFA384X_SELECT0_OFF;
 766	}
 767
 768	if (hfa384x_wait_offset(dev, o_off)) {
 769		prism2_io_debug_error(dev, 7);
 770		printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout before\n",
 771		       dev->name);
 772		ret = -ETIMEDOUT;
 773		goto out;
 774	}
 775
 776	HFA384X_OUTW(id, s_off);
 777	HFA384X_OUTW(offset, o_off);
 778
 779	if (hfa384x_wait_offset(dev, o_off)) {
 780		prism2_io_debug_error(dev, 8);
 781		printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout after\n",
 782		       dev->name);
 783		ret = -ETIMEDOUT;
 784		goto out;
 785	}
 786#ifndef final_version
 787	if (HFA384X_INW(o_off) & HFA384X_OFFSET_ERR) {
 788		prism2_io_debug_error(dev, 9);
 789		printk(KERN_DEBUG "%s: hfa384x_setup_bap - offset error "
 790		       "(%d,0x04%x,%d); reg=0x%04x\n",
 791		       dev->name, bap, id, offset, HFA384X_INW(o_off));
 792		ret = -EINVAL;
 793	}
 794#endif
 795
 796 out:
 797	return ret;
 798}
 799
 800
 801static int hfa384x_get_rid(struct net_device *dev, u16 rid, void *buf, int len,
 802			   int exact_len)
 803{
 804	struct hostap_interface *iface;
 805	local_info_t *local;
 806	int res, rlen = 0;
 807	struct hfa384x_rid_hdr rec;
 808
 809	iface = netdev_priv(dev);
 810	local = iface->local;
 811
 812	if (local->no_pri) {
 813		printk(KERN_DEBUG "%s: cannot get RID %04x (len=%d) - no PRI "
 814		       "f/w\n", dev->name, rid, len);
 815		return -ENOTTY; /* Well.. not really correct, but return
 816				 * something unique enough.. */
 817	}
 818
 819	if ((local->func->card_present && !local->func->card_present(local)) ||
 820	    local->hw_downloading)
 821		return -ENODEV;
 822
 823	res = mutex_lock_interruptible(&local->rid_bap_mtx);
 824	if (res)
 825		return res;
 826
 827	res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS, rid, NULL, NULL);
 828	if (res) {
 829		printk(KERN_DEBUG "%s: hfa384x_get_rid: CMDCODE_ACCESS failed "
 830		       "(res=%d, rid=%04x, len=%d)\n",
 831		       dev->name, res, rid, len);
 832		mutex_unlock(&local->rid_bap_mtx);
 833		return res;
 834	}
 835
 836	spin_lock_bh(&local->baplock);
 837
 838	res = hfa384x_setup_bap(dev, BAP0, rid, 0);
 839	if (res)
 840		goto unlock;
 841
 842	res = hfa384x_from_bap(dev, BAP0, &rec, sizeof(rec));
 843	if (res)
 844		goto unlock;
 845
 846	if (le16_to_cpu(rec.len) == 0) {
 847		/* RID not available */
 848		res = -ENODATA;
 849		goto unlock;
 850	}
 851
 852	rlen = (le16_to_cpu(rec.len) - 1) * 2;
 853	if (exact_len && rlen != len) {
 854		printk(KERN_DEBUG "%s: hfa384x_get_rid - RID len mismatch: "
 855		       "rid=0x%04x, len=%d (expected %d)\n",
 856		       dev->name, rid, rlen, len);
 857		res = -ENODATA;
 858	}
 859
 860	res = hfa384x_from_bap(dev, BAP0, buf, len);
 861
 862unlock:
 863	spin_unlock_bh(&local->baplock);
 864	mutex_unlock(&local->rid_bap_mtx);
 865
 866	if (res) {
 867		if (res != -ENODATA)
 868			printk(KERN_DEBUG "%s: hfa384x_get_rid (rid=%04x, "
 869			       "len=%d) - failed - res=%d\n", dev->name, rid,
 870			       len, res);
 871		if (res == -ETIMEDOUT)
 872			prism2_hw_reset(dev);
 873		return res;
 874	}
 875
 876	return rlen;
 877}
 878
 879
 880static int hfa384x_set_rid(struct net_device *dev, u16 rid, void *buf, int len)
 881{
 882	struct hostap_interface *iface;
 883	local_info_t *local;
 884	struct hfa384x_rid_hdr rec;
 885	int res;
 886
 887	iface = netdev_priv(dev);
 888	local = iface->local;
 889
 890	if (local->no_pri) {
 891		printk(KERN_DEBUG "%s: cannot set RID %04x (len=%d) - no PRI "
 892		       "f/w\n", dev->name, rid, len);
 893		return -ENOTTY; /* Well.. not really correct, but return
 894				 * something unique enough.. */
 895	}
 896
 897	if ((local->func->card_present && !local->func->card_present(local)) ||
 898	    local->hw_downloading)
 899		return -ENODEV;
 900
 901	rec.rid = cpu_to_le16(rid);
 902	/* RID len in words and +1 for rec.rid */
 903	rec.len = cpu_to_le16(len / 2 + len % 2 + 1);
 904
 905	res = mutex_lock_interruptible(&local->rid_bap_mtx);
 906	if (res)
 907		return res;
 908
 909	spin_lock_bh(&local->baplock);
 910	res = hfa384x_setup_bap(dev, BAP0, rid, 0);
 911	if (!res)
 912		res = hfa384x_to_bap(dev, BAP0, &rec, sizeof(rec));
 913	if (!res)
 914		res = hfa384x_to_bap(dev, BAP0, buf, len);
 915	spin_unlock_bh(&local->baplock);
 916
 917	if (res) {
 918		printk(KERN_DEBUG "%s: hfa384x_set_rid (rid=%04x, len=%d) - "
 919		       "failed - res=%d\n", dev->name, rid, len, res);
 920		mutex_unlock(&local->rid_bap_mtx);
 921		return res;
 922	}
 923
 924	res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS_WRITE, rid, NULL, NULL);
 925	mutex_unlock(&local->rid_bap_mtx);
 926
 927	if (res) {
 928		printk(KERN_DEBUG "%s: hfa384x_set_rid: CMDCODE_ACCESS_WRITE "
 929		       "failed (res=%d, rid=%04x, len=%d)\n",
 930		       dev->name, res, rid, len);
 931
 932		if (res == -ETIMEDOUT)
 933			prism2_hw_reset(dev);
 934	}
 935
 936	return res;
 937}
 938
 939
 940static void hfa384x_disable_interrupts(struct net_device *dev)
 941{
 942	/* disable interrupts and clear event status */
 943	HFA384X_OUTW(0, HFA384X_INTEN_OFF);
 944	HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
 945}
 946
 947
 948static void hfa384x_enable_interrupts(struct net_device *dev)
 949{
 950	/* ack pending events and enable interrupts from selected events */
 951	HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
 952	HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
 953}
 954
 955
 956static void hfa384x_events_no_bap0(struct net_device *dev)
 957{
 958	HFA384X_OUTW(HFA384X_EVENT_MASK & ~HFA384X_BAP0_EVENTS,
 959		     HFA384X_INTEN_OFF);
 960}
 961
 962
 963static void hfa384x_events_all(struct net_device *dev)
 964{
 965	HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
 966}
 967
 968
 969static void hfa384x_events_only_cmd(struct net_device *dev)
 970{
 971	HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_INTEN_OFF);
 972}
 973
 974
 975static u16 hfa384x_allocate_fid(struct net_device *dev, int len)
 976{
 977	u16 fid;
 978	unsigned long delay;
 979
 980	/* FIX: this could be replace with hfa384x_cmd() if the Alloc event
 981	 * below would be handled like CmdCompl event (sleep here, wake up from
 982	 * interrupt handler */
 983	if (hfa384x_cmd_wait(dev, HFA384X_CMDCODE_ALLOC, len)) {
 984		printk(KERN_DEBUG "%s: cannot allocate fid, len=%d\n",
 985		       dev->name, len);
 986		return 0xffff;
 987	}
 988
 989	delay = jiffies + HFA384X_ALLOC_COMPL_TIMEOUT;
 990	while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC) &&
 991	       time_before(jiffies, delay))
 992		yield();
 993	if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC)) {
 994		printk("%s: fid allocate, len=%d - timeout\n", dev->name, len);
 995		return 0xffff;
 996	}
 997
 998	fid = HFA384X_INW(HFA384X_ALLOCFID_OFF);
 999	HFA384X_OUTW(HFA384X_EV_ALLOC, HFA384X_EVACK_OFF);
1000
1001	return fid;
1002}
1003
1004
1005static int prism2_reset_port(struct net_device *dev)
1006{
1007	struct hostap_interface *iface;
1008	local_info_t *local;
1009	int res;
1010
1011	iface = netdev_priv(dev);
1012	local = iface->local;
1013
1014	if (!local->dev_enabled)
1015		return 0;
1016
1017	res = hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0,
1018			  NULL, NULL);
1019	if (res)
1020		printk(KERN_DEBUG "%s: reset port failed to disable port\n",
1021		       dev->name);
1022	else {
1023		res = hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0,
1024				  NULL, NULL);
1025		if (res)
1026			printk(KERN_DEBUG "%s: reset port failed to enable "
1027			       "port\n", dev->name);
1028	}
1029
1030	/* It looks like at least some STA firmware versions reset
1031	 * fragmentation threshold back to 2346 after enable command. Restore
1032	 * the configured value, if it differs from this default. */
1033	if (local->fragm_threshold != 2346 &&
1034	    hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
1035			    local->fragm_threshold)) {
1036		printk(KERN_DEBUG "%s: failed to restore fragmentation "
1037		       "threshold (%d) after Port0 enable\n",
1038		       dev->name, local->fragm_threshold);
1039	}
1040
1041	/* Some firmwares lose antenna selection settings on reset */
1042	(void) hostap_set_antsel(local);
1043
1044	return res;
1045}
1046
1047
1048static int prism2_get_version_info(struct net_device *dev, u16 rid,
1049				   const char *txt)
1050{
1051	struct hfa384x_comp_ident comp;
1052	struct hostap_interface *iface;
1053	local_info_t *local;
1054
1055	iface = netdev_priv(dev);
1056	local = iface->local;
1057
1058	if (local->no_pri) {
1059		/* PRI f/w not yet available - cannot read RIDs */
1060		return -1;
1061	}
1062	if (hfa384x_get_rid(dev, rid, &comp, sizeof(comp), 1) < 0) {
1063		printk(KERN_DEBUG "Could not get RID for component %s\n", txt);
1064		return -1;
1065	}
1066
1067	printk(KERN_INFO "%s: %s: id=0x%02x v%d.%d.%d\n", dev->name, txt,
1068	       __le16_to_cpu(comp.id), __le16_to_cpu(comp.major),
1069	       __le16_to_cpu(comp.minor), __le16_to_cpu(comp.variant));
1070	return 0;
1071}
1072
1073
1074static int prism2_setup_rids(struct net_device *dev)
1075{
1076	struct hostap_interface *iface;
1077	local_info_t *local;
1078	__le16 tmp;
1079	int ret = 0;
1080
1081	iface = netdev_priv(dev);
1082	local = iface->local;
1083
1084	hostap_set_word(dev, HFA384X_RID_TICKTIME, 2000);
1085
1086	if (!local->fw_ap) {
1087		u16 tmp1 = hostap_get_porttype(local);
1088		ret = hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE, tmp1);
1089		if (ret) {
1090			printk("%s: Port type setting to %d failed\n",
1091			       dev->name, tmp1);
1092			goto fail;
1093		}
1094	}
1095
1096	/* Setting SSID to empty string seems to kill the card in Host AP mode
1097	 */
1098	if (local->iw_mode != IW_MODE_MASTER || local->essid[0] != '\0') {
1099		ret = hostap_set_string(dev, HFA384X_RID_CNFOWNSSID,
1100					local->essid);
1101		if (ret) {
1102			printk("%s: AP own SSID setting failed\n", dev->name);
1103			goto fail;
1104		}
1105	}
1106
1107	ret = hostap_set_word(dev, HFA384X_RID_CNFMAXDATALEN,
1108			      PRISM2_DATA_MAXLEN);
1109	if (ret) {
1110		printk("%s: MAC data length setting to %d failed\n",
1111		       dev->name, PRISM2_DATA_MAXLEN);
1112		goto fail;
1113	}
1114
1115	if (hfa384x_get_rid(dev, HFA384X_RID_CHANNELLIST, &tmp, 2, 1) < 0) {
1116		printk("%s: Channel list read failed\n", dev->name);
1117		ret = -EINVAL;
1118		goto fail;
1119	}
1120	local->channel_mask = le16_to_cpu(tmp);
1121
1122	if (local->channel < 1 || local->channel > 14 ||
1123	    !(local->channel_mask & (1 << (local->channel - 1)))) {
1124		printk(KERN_WARNING "%s: Channel setting out of range "
1125		       "(%d)!\n", dev->name, local->channel);
1126		ret = -EBUSY;
1127		goto fail;
1128	}
1129
1130	ret = hostap_set_word(dev, HFA384X_RID_CNFOWNCHANNEL, local->channel);
1131	if (ret) {
1132		printk("%s: Channel setting to %d failed\n",
1133		       dev->name, local->channel);
1134		goto fail;
1135	}
1136
1137	ret = hostap_set_word(dev, HFA384X_RID_CNFBEACONINT,
1138			      local->beacon_int);
1139	if (ret) {
1140		printk("%s: Beacon interval setting to %d failed\n",
1141		       dev->name, local->beacon_int);
1142		/* this may fail with Symbol/Lucent firmware */
1143		if (ret == -ETIMEDOUT)
1144			goto fail;
1145	}
1146
1147	ret = hostap_set_word(dev, HFA384X_RID_CNFOWNDTIMPERIOD,
1148			      local->dtim_period);
1149	if (ret) {
1150		printk("%s: DTIM period setting to %d failed\n",
1151		       dev->name, local->dtim_period);
1152		/* this may fail with Symbol/Lucent firmware */
1153		if (ret == -ETIMEDOUT)
1154			goto fail;
1155	}
1156
1157	ret = hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
1158			      local->is_promisc);
1159	if (ret)
1160		printk(KERN_INFO "%s: Setting promiscuous mode (%d) failed\n",
1161		       dev->name, local->is_promisc);
1162
1163	if (!local->fw_ap) {
1164		ret = hostap_set_string(dev, HFA384X_RID_CNFDESIREDSSID,
1165					local->essid);
1166		if (ret) {
1167			printk("%s: Desired SSID setting failed\n", dev->name);
1168			goto fail;
1169		}
1170	}
1171
1172	/* Setup TXRateControl, defaults to allow use of 1, 2, 5.5, and
1173	 * 11 Mbps in automatic TX rate fallback and 1 and 2 Mbps as basic
1174	 * rates */
1175	if (local->tx_rate_control == 0) {
1176		local->tx_rate_control =
1177			HFA384X_RATES_1MBPS |
1178			HFA384X_RATES_2MBPS |
1179			HFA384X_RATES_5MBPS |
1180			HFA384X_RATES_11MBPS;
1181	}
1182	if (local->basic_rates == 0)
1183		local->basic_rates = HFA384X_RATES_1MBPS | HFA384X_RATES_2MBPS;
1184
1185	if (!local->fw_ap) {
1186		ret = hostap_set_word(dev, HFA384X_RID_TXRATECONTROL,
1187				      local->tx_rate_control);
1188		if (ret) {
1189			printk("%s: TXRateControl setting to %d failed\n",
1190			       dev->name, local->tx_rate_control);
1191			goto fail;
1192		}
1193
1194		ret = hostap_set_word(dev, HFA384X_RID_CNFSUPPORTEDRATES,
1195				      local->tx_rate_control);
1196		if (ret) {
1197			printk("%s: cnfSupportedRates setting to %d failed\n",
1198			       dev->name, local->tx_rate_control);
1199		}
1200
1201		ret = hostap_set_word(dev, HFA384X_RID_CNFBASICRATES,
1202				      local->basic_rates);
1203		if (ret) {
1204			printk("%s: cnfBasicRates setting to %d failed\n",
1205			       dev->name, local->basic_rates);
1206		}
1207
1208		ret = hostap_set_word(dev, HFA384X_RID_CREATEIBSS, 1);
1209		if (ret) {
1210			printk("%s: Create IBSS setting to 1 failed\n",
1211			       dev->name);
1212		}
1213	}
1214
1215	if (local->name_set)
1216		(void) hostap_set_string(dev, HFA384X_RID_CNFOWNNAME,
1217					 local->name);
1218
1219	if (hostap_set_encryption(local)) {
1220		printk(KERN_INFO "%s: could not configure encryption\n",
1221		       dev->name);
1222	}
1223
1224	(void) hostap_set_antsel(local);
1225
1226	if (hostap_set_roaming(local)) {
1227		printk(KERN_INFO "%s: could not set host roaming\n",
1228		       dev->name);
1229	}
1230
1231	if (local->sta_fw_ver >= PRISM2_FW_VER(1,6,3) &&
1232	    hostap_set_word(dev, HFA384X_RID_CNFENHSECURITY, local->enh_sec))
1233		printk(KERN_INFO "%s: cnfEnhSecurity setting to 0x%x failed\n",
1234		       dev->name, local->enh_sec);
1235
1236	/* 32-bit tallies were added in STA f/w 0.8.0, but they were apparently
1237	 * not working correctly (last seven counters report bogus values).
1238	 * This has been fixed in 0.8.2, so enable 32-bit tallies only
1239	 * beginning with that firmware version. Another bug fix for 32-bit
1240	 * tallies in 1.4.0; should 16-bit tallies be used for some other
1241	 * versions, too? */
1242	if (local->sta_fw_ver >= PRISM2_FW_VER(0,8,2)) {
1243		if (hostap_set_word(dev, HFA384X_RID_CNFTHIRTY2TALLY, 1)) {
1244			printk(KERN_INFO "%s: cnfThirty2Tally setting "
1245			       "failed\n", dev->name);
1246			local->tallies32 = 0;
1247		} else
1248			local->tallies32 = 1;
1249	} else
1250		local->tallies32 = 0;
1251
1252	hostap_set_auth_algs(local);
1253
1254	if (hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
1255			    local->fragm_threshold)) {
1256		printk(KERN_INFO "%s: setting FragmentationThreshold to %d "
1257		       "failed\n", dev->name, local->fragm_threshold);
1258	}
1259
1260	if (hostap_set_word(dev, HFA384X_RID_RTSTHRESHOLD,
1261			    local->rts_threshold)) {
1262		printk(KERN_INFO "%s: setting RTSThreshold to %d failed\n",
1263		       dev->name, local->rts_threshold);
1264	}
1265
1266	if (local->manual_retry_count >= 0 &&
1267	    hostap_set_word(dev, HFA384X_RID_CNFALTRETRYCOUNT,
1268			    local->manual_retry_count)) {
1269		printk(KERN_INFO "%s: setting cnfAltRetryCount to %d failed\n",
1270		       dev->name, local->manual_retry_count);
1271	}
1272
1273	if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1) &&
1274	    hfa384x_get_rid(dev, HFA384X_RID_CNFDBMADJUST, &tmp, 2, 1) == 2) {
1275		local->rssi_to_dBm = le16_to_cpu(tmp);
1276	}
1277
1278	if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->wpa &&
1279	    hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE, 1)) {
1280		printk(KERN_INFO "%s: setting ssnHandlingMode to 1 failed\n",
1281		       dev->name);
1282	}
1283
1284	if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->generic_elem &&
1285	    hfa384x_set_rid(dev, HFA384X_RID_GENERICELEMENT,
1286			    local->generic_elem, local->generic_elem_len)) {
1287		printk(KERN_INFO "%s: setting genericElement failed\n",
1288		       dev->name);
1289	}
1290
1291 fail:
1292	return ret;
1293}
1294
1295
1296static int prism2_hw_init(struct net_device *dev, int initial)
1297{
1298	struct hostap_interface *iface;
1299	local_info_t *local;
1300	int ret, first = 1;
1301	unsigned long start, delay;
1302
1303	PDEBUG(DEBUG_FLOW, "prism2_hw_init()\n");
1304
1305	iface = netdev_priv(dev);
1306	local = iface->local;
1307
1308	clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits);
1309
1310 init:
1311	/* initialize HFA 384x */
1312	ret = hfa384x_cmd_no_wait(dev, HFA384X_CMDCODE_INIT, 0);
1313	if (ret) {
1314		printk(KERN_INFO "%s: first command failed - assuming card "
1315		       "does not have primary firmware\n", dev_info);
1316	}
1317
1318	if (first && (HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
1319		/* EvStat has Cmd bit set in some cases, so retry once if no
1320		 * wait was needed */
1321		HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
1322		printk(KERN_DEBUG "%s: init command completed too quickly - "
1323		       "retrying\n", dev->name);
1324		first = 0;
1325		goto init;
1326	}
1327
1328	start = jiffies;
1329	delay = jiffies + HFA384X_INIT_TIMEOUT;
1330	while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
1331	       time_before(jiffies, delay))
1332		yield();
1333	if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
1334		printk(KERN_DEBUG "%s: assuming no Primary image in "
1335		       "flash - card initialization not completed\n",
1336		       dev_info);
1337		local->no_pri = 1;
1338#ifdef PRISM2_DOWNLOAD_SUPPORT
1339			if (local->sram_type == -1)
1340				local->sram_type = prism2_get_ram_size(local);
1341#endif /* PRISM2_DOWNLOAD_SUPPORT */
1342		return 1;
1343	}
1344	local->no_pri = 0;
1345	printk(KERN_DEBUG "prism2_hw_init: initialized in %lu ms\n",
1346	       (jiffies - start) * 1000 / HZ);
1347	HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
1348	return 0;
1349}
1350
1351
1352static int prism2_hw_init2(struct net_device *dev, int initial)
1353{
1354	struct hostap_interface *iface;
1355	local_info_t *local;
1356	int i;
1357
1358	iface = netdev_priv(dev);
1359	local = iface->local;
1360
1361#ifdef PRISM2_DOWNLOAD_SUPPORT
1362	kfree(local->pda);
1363	if (local->no_pri)
1364		local->pda = NULL;
1365	else
1366		local->pda = prism2_read_pda(dev);
1367#endif /* PRISM2_DOWNLOAD_SUPPORT */
1368
1369	hfa384x_disable_interrupts(dev);
1370
1371#ifndef final_version
1372	HFA384X_OUTW(HFA384X_MAGIC, HFA384X_SWSUPPORT0_OFF);
1373	if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
1374		printk("SWSUPPORT0 write/read failed: %04X != %04X\n",
1375		       HFA384X_INW(HFA384X_SWSUPPORT0_OFF), HFA384X_MAGIC);
1376		goto failed;
1377	}
1378#endif
1379
1380	if (initial || local->pri_only) {
1381		hfa384x_events_only_cmd(dev);
1382		/* get card version information */
1383		if (prism2_get_version_info(dev, HFA384X_RID_NICID, "NIC") ||
1384		    prism2_get_version_info(dev, HFA384X_RID_PRIID, "PRI")) {
1385			hfa384x_disable_interrupts(dev);
1386			goto failed;
1387		}
1388
1389		if (prism2_get_version_info(dev, HFA384X_RID_STAID, "STA")) {
1390			printk(KERN_DEBUG "%s: Failed to read STA f/w version "
1391			       "- only Primary f/w present\n", dev->name);
1392			local->pri_only = 1;
1393			return 0;
1394		}
1395		local->pri_only = 0;
1396		hfa384x_disable_interrupts(dev);
1397	}
1398
1399	/* FIX: could convert allocate_fid to use sleeping CmdCompl wait and
1400	 * enable interrupts before this. This would also require some sort of
1401	 * sleeping AllocEv waiting */
1402
1403	/* allocate TX FIDs */
1404	local->txfid_len = PRISM2_TXFID_LEN;
1405	for (i = 0; i < PRISM2_TXFID_COUNT; i++) {
1406		local->txfid[i] = hfa384x_allocate_fid(dev, local->txfid_len);
1407		if (local->txfid[i] == 0xffff && local->txfid_len > 1600) {
1408			local->txfid[i] = hfa384x_allocate_fid(dev, 1600);
1409			if (local->txfid[i] != 0xffff) {
1410				printk(KERN_DEBUG "%s: Using shorter TX FID "
1411				       "(1600 bytes)\n", dev->name);
1412				local->txfid_len = 1600;
1413			}
1414		}
1415		if (local->txfid[i] == 0xffff)
1416			goto failed;
1417		local->intransmitfid[i] = PRISM2_TXFID_EMPTY;
1418	}
1419
1420	hfa384x_events_only_cmd(dev);
1421
1422	if (initial) {
 
1423		struct list_head *ptr;
 
1424		prism2_check_sta_fw_version(local);
1425
1426		if (hfa384x_get_rid(dev, HFA384X_RID_CNFOWNMACADDR,
1427				    dev->dev_addr, 6, 1) < 0) {
1428			printk("%s: could not get own MAC address\n",
1429			       dev->name);
1430		}
 
1431		list_for_each(ptr, &local->hostap_interfaces) {
1432			iface = list_entry(ptr, struct hostap_interface, list);
1433			eth_hw_addr_inherit(iface->dev, dev);
1434		}
1435	} else if (local->fw_ap)
1436		prism2_check_sta_fw_version(local);
1437
1438	prism2_setup_rids(dev);
1439
1440	/* MAC is now configured, but port 0 is not yet enabled */
1441	return 0;
1442
1443 failed:
1444	if (!local->no_pri)
1445		printk(KERN_WARNING "%s: Initialization failed\n", dev_info);
1446	return 1;
1447}
1448
1449
1450static int prism2_hw_enable(struct net_device *dev, int initial)
1451{
1452	struct hostap_interface *iface;
1453	local_info_t *local;
1454	int was_resetting;
1455
1456	iface = netdev_priv(dev);
1457	local = iface->local;
1458	was_resetting = local->hw_resetting;
1459
1460	if (hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0, NULL, NULL)) {
1461		printk("%s: MAC port 0 enabling failed\n", dev->name);
1462		return 1;
1463	}
1464
1465	local->hw_ready = 1;
1466	local->hw_reset_tries = 0;
1467	local->hw_resetting = 0;
1468	hfa384x_enable_interrupts(dev);
1469
1470	/* at least D-Link DWL-650 seems to require additional port reset
1471	 * before it starts acting as an AP, so reset port automatically
1472	 * here just in case */
1473	if (initial && prism2_reset_port(dev)) {
1474		printk("%s: MAC port 0 resetting failed\n", dev->name);
1475		return 1;
1476	}
1477
1478	if (was_resetting && netif_queue_stopped(dev)) {
1479		/* If hw_reset() was called during pending transmit, netif
1480		 * queue was stopped. Wake it up now since the wlan card has
1481		 * been resetted. */
1482		netif_wake_queue(dev);
1483	}
1484
1485	return 0;
1486}
1487
1488
1489static int prism2_hw_config(struct net_device *dev, int initial)
1490{
1491	struct hostap_interface *iface;
1492	local_info_t *local;
1493
1494	iface = netdev_priv(dev);
1495	local = iface->local;
1496
1497	if (local->hw_downloading)
1498		return 1;
1499
1500	if (prism2_hw_init(dev, initial)) {
1501		return local->no_pri ? 0 : 1;
1502	}
1503
1504	if (prism2_hw_init2(dev, initial))
1505		return 1;
1506
1507	/* Enable firmware if secondary image is loaded and at least one of the
1508	 * netdevices is up. */
1509	if (!local->pri_only &&
1510	    (initial == 0 || (initial == 2 && local->num_dev_open > 0))) {
1511		if (!local->dev_enabled)
1512			prism2_callback(local, PRISM2_CALLBACK_ENABLE);
1513		local->dev_enabled = 1;
1514		return prism2_hw_enable(dev, initial);
1515	}
1516
1517	return 0;
1518}
1519
1520
1521static void prism2_hw_shutdown(struct net_device *dev, int no_disable)
1522{
1523	struct hostap_interface *iface;
1524	local_info_t *local;
1525
1526	iface = netdev_priv(dev);
1527	local = iface->local;
1528
1529	/* Allow only command completion events during disable */
1530	hfa384x_events_only_cmd(dev);
1531
1532	local->hw_ready = 0;
1533	if (local->dev_enabled)
1534		prism2_callback(local, PRISM2_CALLBACK_DISABLE);
1535	local->dev_enabled = 0;
1536
1537	if (local->func->card_present && !local->func->card_present(local)) {
1538		printk(KERN_DEBUG "%s: card already removed or not configured "
1539		       "during shutdown\n", dev->name);
1540		return;
1541	}
1542
1543	if ((no_disable & HOSTAP_HW_NO_DISABLE) == 0 &&
1544	    hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0, NULL, NULL))
1545		printk(KERN_WARNING "%s: Shutdown failed\n", dev_info);
1546
1547	hfa384x_disable_interrupts(dev);
1548
1549	if (no_disable & HOSTAP_HW_ENABLE_CMDCOMPL)
1550		hfa384x_events_only_cmd(dev);
1551	else
1552		prism2_clear_cmd_queue(local);
1553}
1554
1555
1556static void prism2_hw_reset(struct net_device *dev)
1557{
1558	struct hostap_interface *iface;
1559	local_info_t *local;
1560
1561#if 0
1562	static long last_reset = 0;
1563
1564	/* do not reset card more than once per second to avoid ending up in a
1565	 * busy loop resetting the card */
1566	if (time_before_eq(jiffies, last_reset + HZ))
1567		return;
1568	last_reset = jiffies;
1569#endif
1570
1571	iface = netdev_priv(dev);
1572	local = iface->local;
1573
1574	if (in_interrupt()) {
1575		printk(KERN_DEBUG "%s: driver bug - prism2_hw_reset() called "
1576		       "in interrupt context\n", dev->name);
1577		return;
1578	}
1579
1580	if (local->hw_downloading)
1581		return;
1582
1583	if (local->hw_resetting) {
1584		printk(KERN_WARNING "%s: %s: already resetting card - "
1585		       "ignoring reset request\n", dev_info, dev->name);
1586		return;
1587	}
1588
1589	local->hw_reset_tries++;
1590	if (local->hw_reset_tries > 10) {
1591		printk(KERN_WARNING "%s: too many reset tries, skipping\n",
1592		       dev->name);
1593		return;
1594	}
1595
1596	printk(KERN_WARNING "%s: %s: resetting card\n", dev_info, dev->name);
1597	hfa384x_disable_interrupts(dev);
1598	local->hw_resetting = 1;
1599	if (local->func->cor_sreset) {
1600		/* Host system seems to hang in some cases with high traffic
1601		 * load or shared interrupts during COR sreset. Disable shared
1602		 * interrupts during reset to avoid these crashes. COS sreset
1603		 * takes quite a long time, so it is unfortunate that this
1604		 * seems to be needed. Anyway, I do not know of any better way
1605		 * of avoiding the crash. */
1606		disable_irq(dev->irq);
1607		local->func->cor_sreset(local);
1608		enable_irq(dev->irq);
1609	}
1610	prism2_hw_shutdown(dev, 1);
1611	prism2_hw_config(dev, 0);
1612	local->hw_resetting = 0;
1613
1614#ifdef PRISM2_DOWNLOAD_SUPPORT
1615	if (local->dl_pri) {
1616		printk(KERN_DEBUG "%s: persistent download of primary "
1617		       "firmware\n", dev->name);
1618		if (prism2_download_genesis(local, local->dl_pri) < 0)
1619			printk(KERN_WARNING "%s: download (PRI) failed\n",
1620			       dev->name);
1621	}
1622
1623	if (local->dl_sec) {
1624		printk(KERN_DEBUG "%s: persistent download of secondary "
1625		       "firmware\n", dev->name);
1626		if (prism2_download_volatile(local, local->dl_sec) < 0)
1627			printk(KERN_WARNING "%s: download (SEC) failed\n",
1628			       dev->name);
1629	}
1630#endif /* PRISM2_DOWNLOAD_SUPPORT */
1631
1632	/* TODO: restore beacon TIM bits for STAs that have buffered frames */
1633}
1634
1635
1636static void prism2_schedule_reset(local_info_t *local)
1637{
1638	schedule_work(&local->reset_queue);
1639}
1640
1641
1642/* Called only as scheduled task after noticing card timeout in interrupt
1643 * context */
1644static void handle_reset_queue(struct work_struct *work)
1645{
1646	local_info_t *local = container_of(work, local_info_t, reset_queue);
1647
1648	printk(KERN_DEBUG "%s: scheduled card reset\n", local->dev->name);
1649	prism2_hw_reset(local->dev);
1650
1651	if (netif_queue_stopped(local->dev)) {
1652		int i;
1653
1654		for (i = 0; i < PRISM2_TXFID_COUNT; i++)
1655			if (local->intransmitfid[i] == PRISM2_TXFID_EMPTY) {
1656				PDEBUG(DEBUG_EXTRA, "prism2_tx_timeout: "
1657				       "wake up queue\n");
1658				netif_wake_queue(local->dev);
1659				break;
1660			}
1661	}
1662}
1663
1664
1665static int prism2_get_txfid_idx(local_info_t *local)
1666{
1667	int idx, end;
1668	unsigned long flags;
1669
1670	spin_lock_irqsave(&local->txfidlock, flags);
1671	end = idx = local->next_txfid;
1672	do {
1673		if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
1674			local->intransmitfid[idx] = PRISM2_TXFID_RESERVED;
1675			spin_unlock_irqrestore(&local->txfidlock, flags);
1676			return idx;
1677		}
1678		idx++;
1679		if (idx >= PRISM2_TXFID_COUNT)
1680			idx = 0;
1681	} while (idx != end);
1682	spin_unlock_irqrestore(&local->txfidlock, flags);
1683
1684	PDEBUG(DEBUG_EXTRA2, "prism2_get_txfid_idx: no room in txfid buf: "
1685	       "packet dropped\n");
1686	local->dev->stats.tx_dropped++;
1687
1688	return -1;
1689}
1690
1691
1692/* Called only from hardware IRQ */
1693static void prism2_transmit_cb(struct net_device *dev, long context,
1694			       u16 resp0, u16 res)
1695{
1696	struct hostap_interface *iface;
1697	local_info_t *local;
1698	int idx = (int) context;
1699
1700	iface = netdev_priv(dev);
1701	local = iface->local;
1702
1703	if (res) {
1704		printk(KERN_DEBUG "%s: prism2_transmit_cb - res=0x%02x\n",
1705		       dev->name, res);
1706		return;
1707	}
1708
1709	if (idx < 0 || idx >= PRISM2_TXFID_COUNT) {
1710		printk(KERN_DEBUG "%s: prism2_transmit_cb called with invalid "
1711		       "idx=%d\n", dev->name, idx);
1712		return;
1713	}
1714
1715	if (!test_and_clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
1716		printk(KERN_DEBUG "%s: driver bug: prism2_transmit_cb called "
1717		       "with no pending transmit\n", dev->name);
1718	}
1719
1720	if (netif_queue_stopped(dev)) {
1721		/* ready for next TX, so wake up queue that was stopped in
1722		 * prism2_transmit() */
1723		netif_wake_queue(dev);
1724	}
1725
1726	spin_lock(&local->txfidlock);
1727
1728	/* With reclaim, Resp0 contains new txfid for transmit; the old txfid
1729	 * will be automatically allocated for the next TX frame */
1730	local->intransmitfid[idx] = resp0;
1731
1732	PDEBUG(DEBUG_FID, "%s: prism2_transmit_cb: txfid[%d]=0x%04x, "
1733	       "resp0=0x%04x, transmit_txfid=0x%04x\n",
1734	       dev->name, idx, local->txfid[idx],
1735	       resp0, local->intransmitfid[local->next_txfid]);
1736
1737	idx++;
1738	if (idx >= PRISM2_TXFID_COUNT)
1739		idx = 0;
1740	local->next_txfid = idx;
1741
1742	/* check if all TX buffers are occupied */
1743	do {
1744		if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
1745			spin_unlock(&local->txfidlock);
1746			return;
1747		}
1748		idx++;
1749		if (idx >= PRISM2_TXFID_COUNT)
1750			idx = 0;
1751	} while (idx != local->next_txfid);
1752	spin_unlock(&local->txfidlock);
1753
1754	/* no empty TX buffers, stop queue */
1755	netif_stop_queue(dev);
1756}
1757
1758
1759/* Called only from software IRQ if PCI bus master is not used (with bus master
1760 * this can be called both from software and hardware IRQ) */
1761static int prism2_transmit(struct net_device *dev, int idx)
1762{
1763	struct hostap_interface *iface;
1764	local_info_t *local;
1765	int res;
1766
1767	iface = netdev_priv(dev);
1768	local = iface->local;
1769
1770	/* The driver tries to stop netif queue so that there would not be
1771	 * more than one attempt to transmit frames going on; check that this
1772	 * is really the case */
1773
1774	if (test_and_set_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
1775		printk(KERN_DEBUG "%s: driver bug - prism2_transmit() called "
1776		       "when previous TX was pending\n", dev->name);
1777		return -1;
1778	}
1779
1780	/* stop the queue for the time that transmit is pending */
1781	netif_stop_queue(dev);
1782
1783	/* transmit packet */
1784	res = hfa384x_cmd_callback(
1785		dev,
1786		HFA384X_CMDCODE_TRANSMIT | HFA384X_CMD_TX_RECLAIM,
1787		local->txfid[idx],
1788		prism2_transmit_cb, (long) idx);
1789
1790	if (res) {
1791		printk(KERN_DEBUG "%s: prism2_transmit: CMDCODE_TRANSMIT "
1792		       "failed (res=%d)\n", dev->name, res);
1793		dev->stats.tx_dropped++;
1794		netif_wake_queue(dev);
1795		return -1;
1796	}
1797	netif_trans_update(dev);
1798
1799	/* Since we did not wait for command completion, the card continues
1800	 * to process on the background and we will finish handling when
1801	 * command completion event is handled (prism2_cmd_ev() function) */
1802
1803	return 0;
1804}
1805
1806
1807/* Send IEEE 802.11 frame (convert the header into Prism2 TX descriptor and
1808 * send the payload with this descriptor) */
1809/* Called only from software IRQ */
1810static int prism2_tx_80211(struct sk_buff *skb, struct net_device *dev)
1811{
1812	struct hostap_interface *iface;
1813	local_info_t *local;
1814	struct hfa384x_tx_frame txdesc;
1815	struct hostap_skb_tx_data *meta;
1816	int hdr_len, data_len, idx, res, ret = -1;
1817	u16 tx_control, fc;
1818
1819	iface = netdev_priv(dev);
1820	local = iface->local;
1821
1822	meta = (struct hostap_skb_tx_data *) skb->cb;
1823
1824	prism2_callback(local, PRISM2_CALLBACK_TX_START);
1825
1826	if ((local->func->card_present && !local->func->card_present(local)) ||
1827	    !local->hw_ready || local->hw_downloading || local->pri_only) {
1828		if (net_ratelimit()) {
1829			printk(KERN_DEBUG "%s: prism2_tx_80211: hw not ready -"
1830			       " skipping\n", dev->name);
1831		}
1832		goto fail;
1833	}
1834
1835	memset(&txdesc, 0, sizeof(txdesc));
1836
1837	/* skb->data starts with txdesc->frame_control */
1838	hdr_len = 24;
1839	skb_copy_from_linear_data(skb, &txdesc.frame_control, hdr_len);
1840 	fc = le16_to_cpu(txdesc.frame_control);
1841	if (ieee80211_is_data(txdesc.frame_control) &&
1842	    ieee80211_has_a4(txdesc.frame_control) &&
1843	    skb->len >= 30) {
1844		/* Addr4 */
1845		skb_copy_from_linear_data_offset(skb, hdr_len, txdesc.addr4,
1846						 ETH_ALEN);
1847		hdr_len += ETH_ALEN;
1848	}
1849
1850	tx_control = local->tx_control;
1851	if (meta->tx_cb_idx) {
1852		tx_control |= HFA384X_TX_CTRL_TX_OK;
1853		txdesc.sw_support = cpu_to_le32(meta->tx_cb_idx);
1854	}
1855	txdesc.tx_control = cpu_to_le16(tx_control);
1856	txdesc.tx_rate = meta->rate;
1857
1858	data_len = skb->len - hdr_len;
1859	txdesc.data_len = cpu_to_le16(data_len);
1860	txdesc.len = cpu_to_be16(data_len);
1861
1862	idx = prism2_get_txfid_idx(local);
1863	if (idx < 0)
1864		goto fail;
1865
1866	if (local->frame_dump & PRISM2_DUMP_TX_HDR)
1867		hostap_dump_tx_header(dev->name, &txdesc);
1868
1869	spin_lock(&local->baplock);
1870	res = hfa384x_setup_bap(dev, BAP0, local->txfid[idx], 0);
1871
1872	if (!res)
1873		res = hfa384x_to_bap(dev, BAP0, &txdesc, sizeof(txdesc));
1874	if (!res)
1875		res = hfa384x_to_bap(dev, BAP0, skb->data + hdr_len,
1876				     skb->len - hdr_len);
1877	spin_unlock(&local->baplock);
1878
1879	if (!res)
1880		res = prism2_transmit(dev, idx);
1881	if (res) {
1882		printk(KERN_DEBUG "%s: prism2_tx_80211 - to BAP0 failed\n",
1883		       dev->name);
1884		local->intransmitfid[idx] = PRISM2_TXFID_EMPTY;
1885		schedule_work(&local->reset_queue);
1886		goto fail;
1887	}
1888
1889	ret = 0;
1890
1891fail:
1892	prism2_callback(local, PRISM2_CALLBACK_TX_END);
1893	return ret;
1894}
1895
1896
1897/* Some SMP systems have reported number of odd errors with hostap_pci. fid
1898 * register has changed values between consecutive reads for an unknown reason.
1899 * This should really not happen, so more debugging is needed. This test
1900 * version is a bit slower, but it will detect most of such register changes
1901 * and will try to get the correct fid eventually. */
1902#define EXTRA_FID_READ_TESTS
1903
1904static u16 prism2_read_fid_reg(struct net_device *dev, u16 reg)
1905{
1906#ifdef EXTRA_FID_READ_TESTS
1907	u16 val, val2, val3;
1908	int i;
1909
1910	for (i = 0; i < 10; i++) {
1911		val = HFA384X_INW(reg);
1912		val2 = HFA384X_INW(reg);
1913		val3 = HFA384X_INW(reg);
1914
1915		if (val == val2 && val == val3)
1916			return val;
1917
1918		printk(KERN_DEBUG "%s: detected fid change (try=%d, reg=%04x):"
1919		       " %04x %04x %04x\n",
1920		       dev->name, i, reg, val, val2, val3);
1921		if ((val == val2 || val == val3) && val != 0)
1922			return val;
1923		if (val2 == val3 && val2 != 0)
1924			return val2;
1925	}
1926	printk(KERN_WARNING "%s: Uhhuh.. could not read good fid from reg "
1927	       "%04x (%04x %04x %04x)\n", dev->name, reg, val, val2, val3);
1928	return val;
1929#else /* EXTRA_FID_READ_TESTS */
1930	return HFA384X_INW(reg);
1931#endif /* EXTRA_FID_READ_TESTS */
1932}
1933
1934
1935/* Called only as a tasklet (software IRQ) */
1936static void prism2_rx(local_info_t *local)
1937{
1938	struct net_device *dev = local->dev;
1939	int res, rx_pending = 0;
1940	u16 len, hdr_len, rxfid, status, macport;
1941	struct hfa384x_rx_frame rxdesc;
1942	struct sk_buff *skb = NULL;
1943
1944	prism2_callback(local, PRISM2_CALLBACK_RX_START);
1945
1946	rxfid = prism2_read_fid_reg(dev, HFA384X_RXFID_OFF);
1947#ifndef final_version
1948	if (rxfid == 0) {
1949		rxfid = HFA384X_INW(HFA384X_RXFID_OFF);
1950		printk(KERN_DEBUG "prism2_rx: rxfid=0 (next 0x%04x)\n",
1951		       rxfid);
1952		if (rxfid == 0) {
1953			schedule_work(&local->reset_queue);
1954			goto rx_dropped;
1955		}
1956		/* try to continue with the new rxfid value */
1957	}
1958#endif
1959
1960	spin_lock(&local->baplock);
1961	res = hfa384x_setup_bap(dev, BAP0, rxfid, 0);
1962	if (!res)
1963		res = hfa384x_from_bap(dev, BAP0, &rxdesc, sizeof(rxdesc));
1964
1965	if (res) {
1966		spin_unlock(&local->baplock);
1967		printk(KERN_DEBUG "%s: copy from BAP0 failed %d\n", dev->name,
1968		       res);
1969		if (res == -ETIMEDOUT) {
1970			schedule_work(&local->reset_queue);
1971		}
1972		goto rx_dropped;
1973	}
1974
1975	len = le16_to_cpu(rxdesc.data_len);
1976	hdr_len = sizeof(rxdesc);
1977	status = le16_to_cpu(rxdesc.status);
1978	macport = (status >> 8) & 0x07;
1979
1980	/* Drop frames with too large reported payload length. Monitor mode
1981	 * seems to sometimes pass frames (e.g., ctrl::ack) with signed and
1982	 * negative value, so allow also values 65522 .. 65534 (-14 .. -2) for
1983	 * macport 7 */
1984	if (len > PRISM2_DATA_MAXLEN + 8 /* WEP */) {
1985		if (macport == 7 && local->iw_mode == IW_MODE_MONITOR) {
1986			if (len >= (u16) -14) {
1987				hdr_len -= 65535 - len;
1988				hdr_len--;
1989			}
1990			len = 0;
1991		} else {
1992			spin_unlock(&local->baplock);
1993			printk(KERN_DEBUG "%s: Received frame with invalid "
1994			       "length 0x%04x\n", dev->name, len);
1995			hostap_dump_rx_header(dev->name, &rxdesc);
1996			goto rx_dropped;
1997		}
1998	}
1999
2000	skb = dev_alloc_skb(len + hdr_len);
2001	if (!skb) {
2002		spin_unlock(&local->baplock);
2003		printk(KERN_DEBUG "%s: RX failed to allocate skb\n",
2004		       dev->name);
2005		goto rx_dropped;
2006	}
2007	skb->dev = dev;
2008	skb_put_data(skb, &rxdesc, hdr_len);
2009
2010	if (len > 0)
2011		res = hfa384x_from_bap(dev, BAP0, skb_put(skb, len), len);
2012	spin_unlock(&local->baplock);
2013	if (res) {
2014		printk(KERN_DEBUG "%s: RX failed to read "
2015		       "frame data\n", dev->name);
2016		goto rx_dropped;
2017	}
2018
2019	skb_queue_tail(&local->rx_list, skb);
2020	tasklet_schedule(&local->rx_tasklet);
2021
2022 rx_exit:
2023	prism2_callback(local, PRISM2_CALLBACK_RX_END);
2024	if (!rx_pending) {
2025		HFA384X_OUTW(HFA384X_EV_RX, HFA384X_EVACK_OFF);
2026	}
2027
2028	return;
2029
2030 rx_dropped:
2031	dev->stats.rx_dropped++;
2032	if (skb)
2033		dev_kfree_skb(skb);
2034	goto rx_exit;
2035}
2036
2037
2038/* Called only as a tasklet (software IRQ) */
2039static void hostap_rx_skb(local_info_t *local, struct sk_buff *skb)
2040{
2041	struct hfa384x_rx_frame *rxdesc;
2042	struct net_device *dev = skb->dev;
2043	struct hostap_80211_rx_status stats;
2044	int hdrlen, rx_hdrlen;
2045
2046	rx_hdrlen = sizeof(*rxdesc);
2047	if (skb->len < sizeof(*rxdesc)) {
2048		/* Allow monitor mode to receive shorter frames */
2049		if (local->iw_mode == IW_MODE_MONITOR &&
2050		    skb->len >= sizeof(*rxdesc) - 30) {
2051			rx_hdrlen = skb->len;
2052		} else {
2053			dev_kfree_skb(skb);
2054			return;
2055		}
2056	}
2057
2058	rxdesc = (struct hfa384x_rx_frame *) skb->data;
2059
2060	if (local->frame_dump & PRISM2_DUMP_RX_HDR &&
2061	    skb->len >= sizeof(*rxdesc))
2062		hostap_dump_rx_header(dev->name, rxdesc);
2063
2064	if (le16_to_cpu(rxdesc->status) & HFA384X_RX_STATUS_FCSERR &&
2065	    (!local->monitor_allow_fcserr ||
2066	     local->iw_mode != IW_MODE_MONITOR))
2067		goto drop;
2068
2069	if (skb->len > PRISM2_DATA_MAXLEN) {
2070		printk(KERN_DEBUG "%s: RX: len(%d) > MAX(%d)\n",
2071		       dev->name, skb->len, PRISM2_DATA_MAXLEN);
2072		goto drop;
2073	}
2074
2075	stats.mac_time = le32_to_cpu(rxdesc->time);
2076	stats.signal = rxdesc->signal - local->rssi_to_dBm;
2077	stats.noise = rxdesc->silence - local->rssi_to_dBm;
2078	stats.rate = rxdesc->rate;
2079
2080	/* Convert Prism2 RX structure into IEEE 802.11 header */
2081	hdrlen = hostap_80211_get_hdrlen(rxdesc->frame_control);
2082	if (hdrlen > rx_hdrlen)
2083		hdrlen = rx_hdrlen;
2084
2085	memmove(skb_pull(skb, rx_hdrlen - hdrlen),
2086		&rxdesc->frame_control, hdrlen);
2087
2088	hostap_80211_rx(dev, skb, &stats);
2089	return;
2090
2091 drop:
2092	dev_kfree_skb(skb);
2093}
2094
2095
2096/* Called only as a tasklet (software IRQ) */
2097static void hostap_rx_tasklet(unsigned long data)
2098{
2099	local_info_t *local = (local_info_t *) data;
2100	struct sk_buff *skb;
2101
2102	while ((skb = skb_dequeue(&local->rx_list)) != NULL)
2103		hostap_rx_skb(local, skb);
2104}
2105
2106
2107/* Called only from hardware IRQ */
2108static void prism2_alloc_ev(struct net_device *dev)
2109{
2110	struct hostap_interface *iface;
2111	local_info_t *local;
2112	int idx;
2113	u16 fid;
2114
2115	iface = netdev_priv(dev);
2116	local = iface->local;
2117
2118	fid = prism2_read_fid_reg(dev, HFA384X_ALLOCFID_OFF);
2119
2120	PDEBUG(DEBUG_FID, "FID: interrupt: ALLOC - fid=0x%04x\n", fid);
2121
2122	spin_lock(&local->txfidlock);
2123	idx = local->next_alloc;
2124
2125	do {
2126		if (local->txfid[idx] == fid) {
2127			PDEBUG(DEBUG_FID, "FID: found matching txfid[%d]\n",
2128			       idx);
2129
2130#ifndef final_version
2131			if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY)
2132				printk("Already released txfid found at idx "
2133				       "%d\n", idx);
2134			if (local->intransmitfid[idx] == PRISM2_TXFID_RESERVED)
2135				printk("Already reserved txfid found at idx "
2136				       "%d\n", idx);
2137#endif
2138			local->intransmitfid[idx] = PRISM2_TXFID_EMPTY;
2139			idx++;
2140			local->next_alloc = idx >= PRISM2_TXFID_COUNT ? 0 :
2141				idx;
2142
2143			if (!test_bit(HOSTAP_BITS_TRANSMIT, &local->bits) &&
2144			    netif_queue_stopped(dev))
2145				netif_wake_queue(dev);
2146
2147			spin_unlock(&local->txfidlock);
2148			return;
2149		}
2150
2151		idx++;
2152		if (idx >= PRISM2_TXFID_COUNT)
2153			idx = 0;
2154	} while (idx != local->next_alloc);
2155
2156	printk(KERN_WARNING "%s: could not find matching txfid (0x%04x, new "
2157	       "read 0x%04x) for alloc event\n", dev->name, fid,
2158	       HFA384X_INW(HFA384X_ALLOCFID_OFF));
2159	printk(KERN_DEBUG "TXFIDs:");
2160	for (idx = 0; idx < PRISM2_TXFID_COUNT; idx++)
2161		printk(" %04x[%04x]", local->txfid[idx],
2162		       local->intransmitfid[idx]);
2163	printk("\n");
2164	spin_unlock(&local->txfidlock);
2165
2166	/* FIX: should probably schedule reset; reference to one txfid was lost
2167	 * completely.. Bad things will happen if we run out of txfids
2168	 * Actually, this will cause netdev watchdog to notice TX timeout and
2169	 * then card reset after all txfids have been leaked. */
2170}
2171
2172
2173/* Called only as a tasklet (software IRQ) */
2174static void hostap_tx_callback(local_info_t *local,
2175			       struct hfa384x_tx_frame *txdesc, int ok,
2176			       char *payload)
2177{
2178	u16 sw_support, hdrlen, len;
2179	struct sk_buff *skb;
2180	struct hostap_tx_callback_info *cb;
2181
2182	/* Make sure that frame was from us. */
2183	if (!ether_addr_equal(txdesc->addr2, local->dev->dev_addr)) {
2184		printk(KERN_DEBUG "%s: TX callback - foreign frame\n",
2185		       local->dev->name);
2186		return;
2187	}
2188
2189	sw_support = le32_to_cpu(txdesc->sw_support);
2190
2191	spin_lock(&local->lock);
2192	cb = local->tx_callback;
2193	while (cb != NULL && cb->idx != sw_support)
2194		cb = cb->next;
2195	spin_unlock(&local->lock);
2196
2197	if (cb == NULL) {
2198		printk(KERN_DEBUG "%s: could not find TX callback (idx %d)\n",
2199		       local->dev->name, sw_support);
2200		return;
2201	}
2202
2203	hdrlen = hostap_80211_get_hdrlen(txdesc->frame_control);
2204	len = le16_to_cpu(txdesc->data_len);
2205	skb = dev_alloc_skb(hdrlen + len);
2206	if (skb == NULL) {
2207		printk(KERN_DEBUG "%s: hostap_tx_callback failed to allocate "
2208		       "skb\n", local->dev->name);
2209		return;
2210	}
2211
2212	skb_put_data(skb, (void *)&txdesc->frame_control, hdrlen);
2213	if (payload)
2214		skb_put_data(skb, payload, len);
2215
2216	skb->dev = local->dev;
2217	skb_reset_mac_header(skb);
2218
2219	cb->func(skb, ok, cb->data);
2220}
2221
2222
2223/* Called only as a tasklet (software IRQ) */
2224static int hostap_tx_compl_read(local_info_t *local, int error,
2225				struct hfa384x_tx_frame *txdesc,
2226				char **payload)
2227{
2228	u16 fid, len;
2229	int res, ret = 0;
2230	struct net_device *dev = local->dev;
2231
2232	fid = prism2_read_fid_reg(dev, HFA384X_TXCOMPLFID_OFF);
2233
2234	PDEBUG(DEBUG_FID, "interrupt: TX (err=%d) - fid=0x%04x\n", fid, error);
2235
2236	spin_lock(&local->baplock);
2237	res = hfa384x_setup_bap(dev, BAP0, fid, 0);
2238	if (!res)
2239		res = hfa384x_from_bap(dev, BAP0, txdesc, sizeof(*txdesc));
2240	if (res) {
2241		PDEBUG(DEBUG_EXTRA, "%s: TX (err=%d) - fid=0x%04x - could not "
2242		       "read txdesc\n", dev->name, error, fid);
2243		if (res == -ETIMEDOUT) {
2244			schedule_work(&local->reset_queue);
2245		}
2246		ret = -1;
2247		goto fail;
2248	}
2249	if (txdesc->sw_support) {
2250		len = le16_to_cpu(txdesc->data_len);
2251		if (len < PRISM2_DATA_MAXLEN) {
2252			*payload = kmalloc(len, GFP_ATOMIC);
2253			if (*payload == NULL ||
2254			    hfa384x_from_bap(dev, BAP0, *payload, len)) {
2255				PDEBUG(DEBUG_EXTRA, "%s: could not read TX "
2256				       "frame payload\n", dev->name);
2257				kfree(*payload);
2258				*payload = NULL;
2259				ret = -1;
2260				goto fail;
2261			}
2262		}
2263	}
2264
2265 fail:
2266	spin_unlock(&local->baplock);
2267
2268	return ret;
2269}
2270
2271
2272/* Called only as a tasklet (software IRQ) */
2273static void prism2_tx_ev(local_info_t *local)
2274{
2275	struct net_device *dev = local->dev;
2276	char *payload = NULL;
2277	struct hfa384x_tx_frame txdesc;
2278
2279	if (hostap_tx_compl_read(local, 0, &txdesc, &payload))
2280		goto fail;
2281
2282	if (local->frame_dump & PRISM2_DUMP_TX_HDR) {
2283		PDEBUG(DEBUG_EXTRA, "%s: TX - status=0x%04x "
2284		       "retry_count=%d tx_rate=%d seq_ctrl=%d "
2285		       "duration_id=%d\n",
2286		       dev->name, le16_to_cpu(txdesc.status),
2287		       txdesc.retry_count, txdesc.tx_rate,
2288		       le16_to_cpu(txdesc.seq_ctrl),
2289		       le16_to_cpu(txdesc.duration_id));
2290	}
2291
2292	if (txdesc.sw_support)
2293		hostap_tx_callback(local, &txdesc, 1, payload);
2294	kfree(payload);
2295
2296 fail:
2297	HFA384X_OUTW(HFA384X_EV_TX, HFA384X_EVACK_OFF);
2298}
2299
2300
2301/* Called only as a tasklet (software IRQ) */
2302static void hostap_sta_tx_exc_tasklet(unsigned long data)
2303{
2304	local_info_t *local = (local_info_t *) data;
2305	struct sk_buff *skb;
2306
2307	while ((skb = skb_dequeue(&local->sta_tx_exc_list)) != NULL) {
2308		struct hfa384x_tx_frame *txdesc =
2309			(struct hfa384x_tx_frame *) skb->data;
2310
2311		if (skb->len >= sizeof(*txdesc)) {
2312			/* Convert Prism2 RX structure into IEEE 802.11 header
2313			 */
2314			int hdrlen = hostap_80211_get_hdrlen(txdesc->frame_control);
2315			memmove(skb_pull(skb, sizeof(*txdesc) - hdrlen),
2316				&txdesc->frame_control, hdrlen);
2317
2318			hostap_handle_sta_tx_exc(local, skb);
2319		}
2320		dev_kfree_skb(skb);
2321	}
2322}
2323
2324
2325/* Called only as a tasklet (software IRQ) */
2326static void prism2_txexc(local_info_t *local)
2327{
2328	struct net_device *dev = local->dev;
2329	u16 status, fc;
2330	int show_dump, res;
2331	char *payload = NULL;
2332	struct hfa384x_tx_frame txdesc;
2333
2334	show_dump = local->frame_dump & PRISM2_DUMP_TXEXC_HDR;
2335	dev->stats.tx_errors++;
2336
2337	res = hostap_tx_compl_read(local, 1, &txdesc, &payload);
2338	HFA384X_OUTW(HFA384X_EV_TXEXC, HFA384X_EVACK_OFF);
2339	if (res)
2340		return;
2341
2342	status = le16_to_cpu(txdesc.status);
2343
2344	/* We produce a TXDROP event only for retry or lifetime
2345	 * exceeded, because that's the only status that really mean
2346	 * that this particular node went away.
2347	 * Other errors means that *we* screwed up. - Jean II */
2348	if (status & (HFA384X_TX_STATUS_RETRYERR | HFA384X_TX_STATUS_AGEDERR))
2349	{
2350		union iwreq_data wrqu;
2351
2352		/* Copy 802.11 dest address. */
2353		memcpy(wrqu.addr.sa_data, txdesc.addr1, ETH_ALEN);
2354		wrqu.addr.sa_family = ARPHRD_ETHER;
2355		wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
2356	} else
2357		show_dump = 1;
2358
2359	if (local->iw_mode == IW_MODE_MASTER ||
2360	    local->iw_mode == IW_MODE_REPEAT ||
2361	    local->wds_type & HOSTAP_WDS_AP_CLIENT) {
2362		struct sk_buff *skb;
2363		skb = dev_alloc_skb(sizeof(txdesc));
2364		if (skb) {
2365			skb_put_data(skb, &txdesc, sizeof(txdesc));
2366			skb_queue_tail(&local->sta_tx_exc_list, skb);
2367			tasklet_schedule(&local->sta_tx_exc_tasklet);
2368		}
2369	}
2370
2371	if (txdesc.sw_support)
2372		hostap_tx_callback(local, &txdesc, 0, payload);
2373	kfree(payload);
2374
2375	if (!show_dump)
2376		return;
2377
2378	PDEBUG(DEBUG_EXTRA, "%s: TXEXC - status=0x%04x (%s%s%s%s)"
2379	       " tx_control=%04x\n",
2380	       dev->name, status,
2381	       status & HFA384X_TX_STATUS_RETRYERR ? "[RetryErr]" : "",
2382	       status & HFA384X_TX_STATUS_AGEDERR ? "[AgedErr]" : "",
2383	       status & HFA384X_TX_STATUS_DISCON ? "[Discon]" : "",
2384	       status & HFA384X_TX_STATUS_FORMERR ? "[FormErr]" : "",
2385	       le16_to_cpu(txdesc.tx_control));
2386
2387	fc = le16_to_cpu(txdesc.frame_control);
2388	PDEBUG(DEBUG_EXTRA, "   retry_count=%d tx_rate=%d fc=0x%04x "
2389	       "(%s%s%s::%d%s%s)\n",
2390	       txdesc.retry_count, txdesc.tx_rate, fc,
2391	       ieee80211_is_mgmt(txdesc.frame_control) ? "Mgmt" : "",
2392	       ieee80211_is_ctl(txdesc.frame_control) ? "Ctrl" : "",
2393	       ieee80211_is_data(txdesc.frame_control) ? "Data" : "",
2394	       (fc & IEEE80211_FCTL_STYPE) >> 4,
2395	       ieee80211_has_tods(txdesc.frame_control) ? " ToDS" : "",
2396	       ieee80211_has_fromds(txdesc.frame_control) ? " FromDS" : "");
2397	PDEBUG(DEBUG_EXTRA, "   A1=%pM A2=%pM A3=%pM A4=%pM\n",
2398	       txdesc.addr1, txdesc.addr2,
2399	       txdesc.addr3, txdesc.addr4);
2400}
2401
2402
2403/* Called only as a tasklet (software IRQ) */
2404static void hostap_info_tasklet(unsigned long data)
2405{
2406	local_info_t *local = (local_info_t *) data;
2407	struct sk_buff *skb;
2408
2409	while ((skb = skb_dequeue(&local->info_list)) != NULL) {
2410		hostap_info_process(local, skb);
2411		dev_kfree_skb(skb);
2412	}
2413}
2414
2415
2416/* Called only as a tasklet (software IRQ) */
2417static void prism2_info(local_info_t *local)
2418{
2419	struct net_device *dev = local->dev;
2420	u16 fid;
2421	int res, left;
2422	struct hfa384x_info_frame info;
2423	struct sk_buff *skb;
2424
2425	fid = HFA384X_INW(HFA384X_INFOFID_OFF);
2426
2427	spin_lock(&local->baplock);
2428	res = hfa384x_setup_bap(dev, BAP0, fid, 0);
2429	if (!res)
2430		res = hfa384x_from_bap(dev, BAP0, &info, sizeof(info));
2431	if (res) {
2432		spin_unlock(&local->baplock);
2433		printk(KERN_DEBUG "Could not get info frame (fid=0x%04x)\n",
2434		       fid);
2435		if (res == -ETIMEDOUT) {
2436			schedule_work(&local->reset_queue);
2437		}
2438		goto out;
2439	}
2440
2441	left = (le16_to_cpu(info.len) - 1) * 2;
2442
2443	if (info.len & cpu_to_le16(0x8000) || info.len == 0 || left > 2060) {
2444		/* data register seems to give 0x8000 in some error cases even
2445		 * though busy bit is not set in offset register;
2446		 * in addition, length must be at least 1 due to type field */
2447		spin_unlock(&local->baplock);
2448		printk(KERN_DEBUG "%s: Received info frame with invalid "
2449		       "length 0x%04x (type 0x%04x)\n", dev->name,
2450		       le16_to_cpu(info.len), le16_to_cpu(info.type));
2451		goto out;
2452	}
2453
2454	skb = dev_alloc_skb(sizeof(info) + left);
2455	if (skb == NULL) {
2456		spin_unlock(&local->baplock);
2457		printk(KERN_DEBUG "%s: Could not allocate skb for info "
2458		       "frame\n", dev->name);
2459		goto out;
2460	}
2461
2462	skb_put_data(skb, &info, sizeof(info));
2463	if (left > 0 && hfa384x_from_bap(dev, BAP0, skb_put(skb, left), left))
2464	{
2465		spin_unlock(&local->baplock);
2466		printk(KERN_WARNING "%s: Info frame read failed (fid=0x%04x, "
2467		       "len=0x%04x, type=0x%04x\n", dev->name, fid,
2468		       le16_to_cpu(info.len), le16_to_cpu(info.type));
2469		dev_kfree_skb(skb);
2470		goto out;
2471	}
2472	spin_unlock(&local->baplock);
2473
2474	skb_queue_tail(&local->info_list, skb);
2475	tasklet_schedule(&local->info_tasklet);
2476
2477 out:
2478	HFA384X_OUTW(HFA384X_EV_INFO, HFA384X_EVACK_OFF);
2479}
2480
2481
2482/* Called only as a tasklet (software IRQ) */
2483static void hostap_bap_tasklet(unsigned long data)
2484{
2485	local_info_t *local = (local_info_t *) data;
2486	struct net_device *dev = local->dev;
2487	u16 ev;
2488	int frames = 30;
2489
2490	if (local->func->card_present && !local->func->card_present(local))
2491		return;
2492
2493	set_bit(HOSTAP_BITS_BAP_TASKLET, &local->bits);
2494
2495	/* Process all pending BAP events without generating new interrupts
2496	 * for them */
2497	while (frames-- > 0) {
2498		ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
2499		if (ev == 0xffff || !(ev & HFA384X_BAP0_EVENTS))
2500			break;
2501		if (ev & HFA384X_EV_RX)
2502			prism2_rx(local);
2503		if (ev & HFA384X_EV_INFO)
2504			prism2_info(local);
2505		if (ev & HFA384X_EV_TX)
2506			prism2_tx_ev(local);
2507		if (ev & HFA384X_EV_TXEXC)
2508			prism2_txexc(local);
2509	}
2510
2511	set_bit(HOSTAP_BITS_BAP_TASKLET2, &local->bits);
2512	clear_bit(HOSTAP_BITS_BAP_TASKLET, &local->bits);
2513
2514	/* Enable interrupts for new BAP events */
2515	hfa384x_events_all(dev);
2516	clear_bit(HOSTAP_BITS_BAP_TASKLET2, &local->bits);
2517}
2518
2519
2520/* Called only from hardware IRQ */
2521static void prism2_infdrop(struct net_device *dev)
2522{
2523	static unsigned long last_inquire = 0;
2524
2525	PDEBUG(DEBUG_EXTRA, "%s: INFDROP event\n", dev->name);
2526
2527	/* some firmware versions seem to get stuck with
2528	 * full CommTallies in high traffic load cases; every
2529	 * packet will then cause INFDROP event and CommTallies
2530	 * info frame will not be sent automatically. Try to
2531	 * get out of this state by inquiring CommTallies. */
2532	if (!last_inquire || time_after(jiffies, last_inquire + HZ)) {
2533		hfa384x_cmd_callback(dev, HFA384X_CMDCODE_INQUIRE,
2534				     HFA384X_INFO_COMMTALLIES, NULL, 0);
2535		last_inquire = jiffies;
2536	}
2537}
2538
2539
2540/* Called only from hardware IRQ */
2541static void prism2_ev_tick(struct net_device *dev)
2542{
2543	struct hostap_interface *iface;
2544	local_info_t *local;
2545	u16 evstat, inten;
2546	static int prev_stuck = 0;
2547
2548	iface = netdev_priv(dev);
2549	local = iface->local;
2550
2551	if (time_after(jiffies, local->last_tick_timer + 5 * HZ) &&
2552	    local->last_tick_timer) {
2553		evstat = HFA384X_INW(HFA384X_EVSTAT_OFF);
2554		inten = HFA384X_INW(HFA384X_INTEN_OFF);
2555		if (!prev_stuck) {
2556			printk(KERN_INFO "%s: SW TICK stuck? "
2557			       "bits=0x%lx EvStat=%04x IntEn=%04x\n",
2558			       dev->name, local->bits, evstat, inten);
2559		}
2560		local->sw_tick_stuck++;
2561		if ((evstat & HFA384X_BAP0_EVENTS) &&
2562		    (inten & HFA384X_BAP0_EVENTS)) {
2563			printk(KERN_INFO "%s: trying to recover from IRQ "
2564			       "hang\n", dev->name);
2565			hfa384x_events_no_bap0(dev);
2566		}
2567		prev_stuck = 1;
2568	} else
2569		prev_stuck = 0;
2570}
2571
2572
2573/* Called only from hardware IRQ */
2574static void prism2_check_magic(local_info_t *local)
2575{
2576	/* at least PCI Prism2.5 with bus mastering seems to sometimes
2577	 * return 0x0000 in SWSUPPORT0 for unknown reason, but re-reading the
2578	 * register once or twice seems to get the correct value.. PCI cards
2579	 * cannot anyway be removed during normal operation, so there is not
2580	 * really any need for this verification with them. */
2581
2582#ifndef PRISM2_PCI
2583#ifndef final_version
2584	static unsigned long last_magic_err = 0;
2585	struct net_device *dev = local->dev;
2586
2587	if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
2588		if (!local->hw_ready)
2589			return;
2590		HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
2591		if (time_after(jiffies, last_magic_err + 10 * HZ)) {
2592			printk("%s: Interrupt, but SWSUPPORT0 does not match: "
2593			       "%04X != %04X - card removed?\n", dev->name,
2594			       HFA384X_INW(HFA384X_SWSUPPORT0_OFF),
2595			       HFA384X_MAGIC);
2596			last_magic_err = jiffies;
2597		} else if (net_ratelimit()) {
2598			printk(KERN_DEBUG "%s: interrupt - SWSUPPORT0=%04x "
2599			       "MAGIC=%04x\n", dev->name,
2600			       HFA384X_INW(HFA384X_SWSUPPORT0_OFF),
2601			       HFA384X_MAGIC);
2602		}
2603		if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != 0xffff)
2604			schedule_work(&local->reset_queue);
2605		return;
2606	}
2607#endif /* final_version */
2608#endif /* !PRISM2_PCI */
2609}
2610
2611
2612/* Called only from hardware IRQ */
2613static irqreturn_t prism2_interrupt(int irq, void *dev_id)
2614{
2615	struct net_device *dev = dev_id;
2616	struct hostap_interface *iface;
2617	local_info_t *local;
2618	int events = 0;
2619	u16 ev;
2620
2621	iface = netdev_priv(dev);
2622	local = iface->local;
2623
2624	/* Detect early interrupt before driver is fully configured */
2625	spin_lock(&local->irq_init_lock);
2626	if (!dev->base_addr) {
2627		if (net_ratelimit()) {
2628			printk(KERN_DEBUG "%s: Interrupt, but dev not configured\n",
2629			       dev->name);
2630		}
2631		spin_unlock(&local->irq_init_lock);
2632		return IRQ_HANDLED;
2633	}
2634	spin_unlock(&local->irq_init_lock);
2635
2636	prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INTERRUPT, 0, 0);
2637
2638	if (local->func->card_present && !local->func->card_present(local)) {
2639		if (net_ratelimit()) {
2640			printk(KERN_DEBUG "%s: Interrupt, but dev not OK\n",
2641			       dev->name);
2642		}
2643		return IRQ_HANDLED;
2644	}
2645
2646	prism2_check_magic(local);
2647
2648	for (;;) {
2649		ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
2650		if (ev == 0xffff) {
2651			if (local->shutdown)
2652				return IRQ_HANDLED;
2653			HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
2654			printk(KERN_DEBUG "%s: prism2_interrupt: ev=0xffff\n",
2655			       dev->name);
2656			return IRQ_HANDLED;
2657		}
2658
2659		ev &= HFA384X_INW(HFA384X_INTEN_OFF);
2660		if (ev == 0)
2661			break;
2662
2663		if (ev & HFA384X_EV_CMD) {
2664			prism2_cmd_ev(dev);
2665		}
2666
2667		/* Above events are needed even before hw is ready, but other
2668		 * events should be skipped during initialization. This may
2669		 * change for AllocEv if allocate_fid is implemented without
2670		 * busy waiting. */
2671		if (!local->hw_ready || local->hw_resetting ||
2672		    !local->dev_enabled) {
2673			ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
2674			if (ev & HFA384X_EV_CMD)
2675				goto next_event;
2676			if ((ev & HFA384X_EVENT_MASK) == 0)
2677				return IRQ_HANDLED;
2678			if (local->dev_enabled && (ev & ~HFA384X_EV_TICK) &&
2679			    net_ratelimit()) {
2680				printk(KERN_DEBUG "%s: prism2_interrupt: hw "
2681				       "not ready; skipping events 0x%04x "
2682				       "(IntEn=0x%04x)%s%s%s\n",
2683				       dev->name, ev,
2684				       HFA384X_INW(HFA384X_INTEN_OFF),
2685				       !local->hw_ready ? " (!hw_ready)" : "",
2686				       local->hw_resetting ?
2687				       " (hw_resetting)" : "",
2688				       !local->dev_enabled ?
2689				       " (!dev_enabled)" : "");
2690			}
2691			HFA384X_OUTW(ev, HFA384X_EVACK_OFF);
2692			return IRQ_HANDLED;
2693		}
2694
2695		if (ev & HFA384X_EV_TICK) {
2696			prism2_ev_tick(dev);
2697			HFA384X_OUTW(HFA384X_EV_TICK, HFA384X_EVACK_OFF);
2698		}
2699
2700		if (ev & HFA384X_EV_ALLOC) {
2701			prism2_alloc_ev(dev);
2702			HFA384X_OUTW(HFA384X_EV_ALLOC, HFA384X_EVACK_OFF);
2703		}
2704
2705		/* Reading data from the card is quite time consuming, so do it
2706		 * in tasklets. TX, TXEXC, RX, and INFO events will be ACKed
2707		 * and unmasked after needed data has been read completely. */
2708		if (ev & HFA384X_BAP0_EVENTS) {
2709			hfa384x_events_no_bap0(dev);
2710			tasklet_schedule(&local->bap_tasklet);
2711		}
2712
2713#ifndef final_version
2714		if (ev & HFA384X_EV_WTERR) {
2715			PDEBUG(DEBUG_EXTRA, "%s: WTERR event\n", dev->name);
2716			HFA384X_OUTW(HFA384X_EV_WTERR, HFA384X_EVACK_OFF);
2717		}
2718#endif /* final_version */
2719
2720		if (ev & HFA384X_EV_INFDROP) {
2721			prism2_infdrop(dev);
2722			HFA384X_OUTW(HFA384X_EV_INFDROP, HFA384X_EVACK_OFF);
2723		}
2724
2725	next_event:
2726		events++;
2727		if (events >= PRISM2_MAX_INTERRUPT_EVENTS) {
2728			PDEBUG(DEBUG_EXTRA, "prism2_interrupt: >%d events "
2729			       "(EvStat=0x%04x)\n",
2730			       PRISM2_MAX_INTERRUPT_EVENTS,
2731			       HFA384X_INW(HFA384X_EVSTAT_OFF));
2732			break;
2733		}
2734	}
2735	prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INTERRUPT, 0, 1);
2736	return IRQ_RETVAL(events);
2737}
2738
2739
2740static void prism2_check_sta_fw_version(local_info_t *local)
2741{
2742	struct hfa384x_comp_ident comp;
2743	int id, variant, major, minor;
2744
2745	if (hfa384x_get_rid(local->dev, HFA384X_RID_STAID,
2746			    &comp, sizeof(comp), 1) < 0)
2747		return;
2748
2749	local->fw_ap = 0;
2750	id = le16_to_cpu(comp.id);
2751	if (id != HFA384X_COMP_ID_STA) {
2752		if (id == HFA384X_COMP_ID_FW_AP)
2753			local->fw_ap = 1;
2754		return;
2755	}
2756
2757	major = __le16_to_cpu(comp.major);
2758	minor = __le16_to_cpu(comp.minor);
2759	variant = __le16_to_cpu(comp.variant);
2760	local->sta_fw_ver = PRISM2_FW_VER(major, minor, variant);
2761
2762	/* Station firmware versions before 1.4.x seem to have a bug in
2763	 * firmware-based WEP encryption when using Host AP mode, so use
2764	 * host_encrypt as a default for them. Firmware version 1.4.9 is the
2765	 * first one that has been seen to produce correct encryption, but the
2766	 * bug might be fixed before that (although, at least 1.4.2 is broken).
2767	 */
2768	local->fw_encrypt_ok = local->sta_fw_ver >= PRISM2_FW_VER(1,4,9);
2769
2770	if (local->iw_mode == IW_MODE_MASTER && !local->host_encrypt &&
2771	    !local->fw_encrypt_ok) {
2772		printk(KERN_DEBUG "%s: defaulting to host-based encryption as "
2773		       "a workaround for firmware bug in Host AP mode WEP\n",
2774		       local->dev->name);
2775		local->host_encrypt = 1;
2776	}
2777
2778	/* IEEE 802.11 standard compliant WDS frames (4 addresses) were broken
2779	 * in station firmware versions before 1.5.x. With these versions, the
2780	 * driver uses a workaround with bogus frame format (4th address after
2781	 * the payload). This is not compatible with other AP devices. Since
2782	 * the firmware bug is fixed in the latest station firmware versions,
2783	 * automatically enable standard compliant mode for cards using station
2784	 * firmware version 1.5.0 or newer. */
2785	if (local->sta_fw_ver >= PRISM2_FW_VER(1,5,0))
2786		local->wds_type |= HOSTAP_WDS_STANDARD_FRAME;
2787	else {
2788		printk(KERN_DEBUG "%s: defaulting to bogus WDS frame as a "
2789		       "workaround for firmware bug in Host AP mode WDS\n",
2790		       local->dev->name);
2791	}
2792
2793	hostap_check_sta_fw_version(local->ap, local->sta_fw_ver);
2794}
2795
2796
2797static void hostap_passive_scan(struct timer_list *t)
2798{
2799	local_info_t *local = from_timer(local, t, passive_scan_timer);
2800	struct net_device *dev = local->dev;
2801	u16 chan;
2802
2803	if (local->passive_scan_interval <= 0)
2804		return;
2805
2806	if (local->passive_scan_state == PASSIVE_SCAN_LISTEN) {
2807		int max_tries = 16;
2808
2809		/* Even though host system does not really know when the WLAN
2810		 * MAC is sending frames, try to avoid changing channels for
2811		 * passive scanning when a host-generated frame is being
2812		 * transmitted */
2813		if (test_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
2814			printk(KERN_DEBUG "%s: passive scan detected pending "
2815			       "TX - delaying\n", dev->name);
2816			local->passive_scan_timer.expires = jiffies + HZ / 10;
2817			add_timer(&local->passive_scan_timer);
2818			return;
2819		}
2820
2821		do {
2822			local->passive_scan_channel++;
2823			if (local->passive_scan_channel > 14)
2824				local->passive_scan_channel = 1;
2825			max_tries--;
2826		} while (!(local->channel_mask &
2827			   (1 << (local->passive_scan_channel - 1))) &&
2828			 max_tries > 0);
2829
2830		if (max_tries == 0) {
2831			printk(KERN_INFO "%s: no allowed passive scan channels"
2832			       " found\n", dev->name);
2833			return;
2834		}
2835
2836		printk(KERN_DEBUG "%s: passive scan channel %d\n",
2837		       dev->name, local->passive_scan_channel);
2838		chan = local->passive_scan_channel;
2839		local->passive_scan_state = PASSIVE_SCAN_WAIT;
2840		local->passive_scan_timer.expires = jiffies + HZ / 10;
2841	} else {
2842		chan = local->channel;
2843		local->passive_scan_state = PASSIVE_SCAN_LISTEN;
2844		local->passive_scan_timer.expires = jiffies +
2845			local->passive_scan_interval * HZ;
2846	}
2847
2848	if (hfa384x_cmd_callback(dev, HFA384X_CMDCODE_TEST |
2849				 (HFA384X_TEST_CHANGE_CHANNEL << 8),
2850				 chan, NULL, 0))
2851		printk(KERN_ERR "%s: passive scan channel set %d "
2852		       "failed\n", dev->name, chan);
2853
2854	add_timer(&local->passive_scan_timer);
2855}
2856
2857
2858/* Called only as a scheduled task when communications quality values should
2859 * be updated. */
2860static void handle_comms_qual_update(struct work_struct *work)
2861{
2862	local_info_t *local =
2863		container_of(work, local_info_t, comms_qual_update);
2864	prism2_update_comms_qual(local->dev);
2865}
2866
2867
2868/* Software watchdog - called as a timer. Hardware interrupt (Tick event) is
2869 * used to monitor that local->last_tick_timer is being updated. If not,
2870 * interrupt busy-loop is assumed and driver tries to recover by masking out
2871 * some events. */
2872static void hostap_tick_timer(struct timer_list *t)
2873{
2874	static unsigned long last_inquire = 0;
2875	local_info_t *local = from_timer(local, t, tick_timer);
2876	local->last_tick_timer = jiffies;
2877
2878	/* Inquire CommTallies every 10 seconds to keep the statistics updated
2879	 * more often during low load and when using 32-bit tallies. */
2880	if ((!last_inquire || time_after(jiffies, last_inquire + 10 * HZ)) &&
2881	    !local->hw_downloading && local->hw_ready &&
2882	    !local->hw_resetting && local->dev_enabled) {
2883		hfa384x_cmd_callback(local->dev, HFA384X_CMDCODE_INQUIRE,
2884				     HFA384X_INFO_COMMTALLIES, NULL, 0);
2885		last_inquire = jiffies;
2886	}
2887
2888	if ((local->last_comms_qual_update == 0 ||
2889	     time_after(jiffies, local->last_comms_qual_update + 10 * HZ)) &&
2890	    (local->iw_mode == IW_MODE_INFRA ||
2891	     local->iw_mode == IW_MODE_ADHOC)) {
2892		schedule_work(&local->comms_qual_update);
2893	}
2894
2895	local->tick_timer.expires = jiffies + 2 * HZ;
2896	add_timer(&local->tick_timer);
2897}
2898
2899
2900#ifndef PRISM2_NO_PROCFS_DEBUG
 
 
 
 
 
2901static int prism2_registers_proc_show(struct seq_file *m, void *v)
2902{
2903	local_info_t *local = m->private;
2904
2905#define SHOW_REG(n) \
2906  seq_printf(m, #n "=%04x\n", hfa384x_read_reg(local->dev, HFA384X_##n##_OFF))
2907
2908	SHOW_REG(CMD);
2909	SHOW_REG(PARAM0);
2910	SHOW_REG(PARAM1);
2911	SHOW_REG(PARAM2);
2912	SHOW_REG(STATUS);
2913	SHOW_REG(RESP0);
2914	SHOW_REG(RESP1);
2915	SHOW_REG(RESP2);
2916	SHOW_REG(INFOFID);
2917	SHOW_REG(CONTROL);
2918	SHOW_REG(SELECT0);
2919	SHOW_REG(SELECT1);
2920	SHOW_REG(OFFSET0);
2921	SHOW_REG(OFFSET1);
2922	SHOW_REG(RXFID);
2923	SHOW_REG(ALLOCFID);
2924	SHOW_REG(TXCOMPLFID);
2925	SHOW_REG(SWSUPPORT0);
2926	SHOW_REG(SWSUPPORT1);
2927	SHOW_REG(SWSUPPORT2);
2928	SHOW_REG(EVSTAT);
2929	SHOW_REG(INTEN);
2930	SHOW_REG(EVACK);
2931	/* Do not read data registers, because they change the state of the
2932	 * MAC (offset += 2) */
2933	/* SHOW_REG(DATA0); */
2934	/* SHOW_REG(DATA1); */
2935	SHOW_REG(AUXPAGE);
2936	SHOW_REG(AUXOFFSET);
2937	/* SHOW_REG(AUXDATA); */
2938#ifdef PRISM2_PCI
2939	SHOW_REG(PCICOR);
2940	SHOW_REG(PCIHCR);
2941	SHOW_REG(PCI_M0_ADDRH);
2942	SHOW_REG(PCI_M0_ADDRL);
2943	SHOW_REG(PCI_M0_LEN);
2944	SHOW_REG(PCI_M0_CTL);
2945	SHOW_REG(PCI_STATUS);
2946	SHOW_REG(PCI_M1_ADDRH);
2947	SHOW_REG(PCI_M1_ADDRL);
2948	SHOW_REG(PCI_M1_LEN);
2949	SHOW_REG(PCI_M1_CTL);
2950#endif /* PRISM2_PCI */
2951
2952	return 0;
2953}
2954
2955static int prism2_registers_proc_open(struct inode *inode, struct file *file)
2956{
2957	return single_open(file, prism2_registers_proc_show, PDE_DATA(inode));
2958}
2959
2960static const struct file_operations prism2_registers_proc_fops = {
2961	.open		= prism2_registers_proc_open,
2962	.read		= seq_read,
2963	.llseek		= seq_lseek,
2964	.release	= single_release,
2965};
2966
2967#endif /* PRISM2_NO_PROCFS_DEBUG */
2968
2969
2970struct set_tim_data {
2971	struct list_head list;
2972	int aid;
2973	int set;
2974};
2975
2976static int prism2_set_tim(struct net_device *dev, int aid, int set)
2977{
2978	struct list_head *ptr;
2979	struct set_tim_data *new_entry;
2980	struct hostap_interface *iface;
2981	local_info_t *local;
2982
2983	iface = netdev_priv(dev);
2984	local = iface->local;
2985
2986	new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
2987	if (new_entry == NULL)
2988		return -ENOMEM;
2989
2990	new_entry->aid = aid;
2991	new_entry->set = set;
2992
2993	spin_lock_bh(&local->set_tim_lock);
2994	list_for_each(ptr, &local->set_tim_list) {
2995		struct set_tim_data *entry =
2996			list_entry(ptr, struct set_tim_data, list);
2997		if (entry->aid == aid) {
2998			PDEBUG(DEBUG_PS2, "%s: prism2_set_tim: aid=%d "
2999			       "set=%d ==> %d\n",
3000			       local->dev->name, aid, entry->set, set);
3001			entry->set = set;
3002			kfree(new_entry);
3003			new_entry = NULL;
3004			break;
3005		}
3006	}
3007	if (new_entry)
3008		list_add_tail(&new_entry->list, &local->set_tim_list);
3009	spin_unlock_bh(&local->set_tim_lock);
3010
3011	schedule_work(&local->set_tim_queue);
3012
3013	return 0;
3014}
3015
3016
3017static void handle_set_tim_queue(struct work_struct *work)
3018{
3019	local_info_t *local = container_of(work, local_info_t, set_tim_queue);
3020	struct set_tim_data *entry;
3021	u16 val;
3022
3023	for (;;) {
3024		entry = NULL;
3025		spin_lock_bh(&local->set_tim_lock);
3026		if (!list_empty(&local->set_tim_list)) {
3027			entry = list_entry(local->set_tim_list.next,
3028					   struct set_tim_data, list);
3029			list_del(&entry->list);
3030		}
3031		spin_unlock_bh(&local->set_tim_lock);
3032		if (!entry)
3033			break;
3034
3035		PDEBUG(DEBUG_PS2, "%s: handle_set_tim_queue: aid=%d set=%d\n",
3036		       local->dev->name, entry->aid, entry->set);
3037
3038		val = entry->aid;
3039		if (entry->set)
3040			val |= 0x8000;
3041		if (hostap_set_word(local->dev, HFA384X_RID_CNFTIMCTRL, val)) {
3042			printk(KERN_DEBUG "%s: set_tim failed (aid=%d "
3043			       "set=%d)\n",
3044			       local->dev->name, entry->aid, entry->set);
3045		}
3046
3047		kfree(entry);
3048	}
3049}
3050
3051
3052static void prism2_clear_set_tim_queue(local_info_t *local)
3053{
3054	struct list_head *ptr, *n;
3055
3056	list_for_each_safe(ptr, n, &local->set_tim_list) {
3057		struct set_tim_data *entry;
3058		entry = list_entry(ptr, struct set_tim_data, list);
3059		list_del(&entry->list);
3060		kfree(entry);
3061	}
3062}
3063
3064
3065/*
3066 * HostAP uses two layers of net devices, where the inner
3067 * layer gets called all the time from the outer layer.
3068 * This is a natural nesting, which needs a split lock type.
3069 */
3070static struct lock_class_key hostap_netdev_xmit_lock_key;
3071static struct lock_class_key hostap_netdev_addr_lock_key;
3072
3073static void prism2_set_lockdep_class_one(struct net_device *dev,
3074					 struct netdev_queue *txq,
3075					 void *_unused)
3076{
3077	lockdep_set_class(&txq->_xmit_lock,
3078			  &hostap_netdev_xmit_lock_key);
3079}
3080
3081static void prism2_set_lockdep_class(struct net_device *dev)
3082{
3083	lockdep_set_class(&dev->addr_list_lock,
3084			  &hostap_netdev_addr_lock_key);
3085	netdev_for_each_tx_queue(dev, prism2_set_lockdep_class_one, NULL);
3086}
3087
3088static struct net_device *
3089prism2_init_local_data(struct prism2_helper_functions *funcs, int card_idx,
3090		       struct device *sdev)
3091{
3092	struct net_device *dev;
3093	struct hostap_interface *iface;
3094	struct local_info *local;
3095	int len, i, ret;
3096
3097	if (funcs == NULL)
3098		return NULL;
3099
3100	len = strlen(dev_template);
3101	if (len >= IFNAMSIZ || strstr(dev_template, "%d") == NULL) {
3102		printk(KERN_WARNING "hostap: Invalid dev_template='%s'\n",
3103		       dev_template);
3104		return NULL;
3105	}
3106
3107	len = sizeof(struct hostap_interface) +
3108		3 + sizeof(struct local_info) +
3109		3 + sizeof(struct ap_data);
3110
3111	dev = alloc_etherdev(len);
3112	if (dev == NULL)
3113		return NULL;
3114
3115	iface = netdev_priv(dev);
3116	local = (struct local_info *) ((((long) (iface + 1)) + 3) & ~3);
3117	local->ap = (struct ap_data *) ((((long) (local + 1)) + 3) & ~3);
3118	local->dev = iface->dev = dev;
3119	iface->local = local;
3120	iface->type = HOSTAP_INTERFACE_MASTER;
3121	INIT_LIST_HEAD(&local->hostap_interfaces);
3122
3123	local->hw_module = THIS_MODULE;
3124
3125#ifdef PRISM2_IO_DEBUG
3126	local->io_debug_enabled = 1;
3127#endif /* PRISM2_IO_DEBUG */
3128
3129	local->func = funcs;
3130	local->func->cmd = hfa384x_cmd;
3131	local->func->read_regs = hfa384x_read_regs;
3132	local->func->get_rid = hfa384x_get_rid;
3133	local->func->set_rid = hfa384x_set_rid;
3134	local->func->hw_enable = prism2_hw_enable;
3135	local->func->hw_config = prism2_hw_config;
3136	local->func->hw_reset = prism2_hw_reset;
3137	local->func->hw_shutdown = prism2_hw_shutdown;
3138	local->func->reset_port = prism2_reset_port;
3139	local->func->schedule_reset = prism2_schedule_reset;
3140#ifdef PRISM2_DOWNLOAD_SUPPORT
3141	local->func->read_aux_fops = &prism2_download_aux_dump_proc_fops;
3142	local->func->download = prism2_download;
3143#endif /* PRISM2_DOWNLOAD_SUPPORT */
3144	local->func->tx = prism2_tx_80211;
3145	local->func->set_tim = prism2_set_tim;
3146	local->func->need_tx_headroom = 0; /* no need to add txdesc in
3147					    * skb->data (FIX: maybe for DMA bus
3148					    * mastering? */
3149
3150	local->mtu = mtu;
3151
3152	rwlock_init(&local->iface_lock);
3153	spin_lock_init(&local->txfidlock);
3154	spin_lock_init(&local->cmdlock);
3155	spin_lock_init(&local->baplock);
3156	spin_lock_init(&local->lock);
3157	spin_lock_init(&local->irq_init_lock);
3158	mutex_init(&local->rid_bap_mtx);
3159
3160	if (card_idx < 0 || card_idx >= MAX_PARM_DEVICES)
3161		card_idx = 0;
3162	local->card_idx = card_idx;
3163
3164	len = strlen(essid);
3165	memcpy(local->essid, essid,
3166	       len > MAX_SSID_LEN ? MAX_SSID_LEN : len);
3167	local->essid[MAX_SSID_LEN] = '\0';
3168	i = GET_INT_PARM(iw_mode, card_idx);
3169	if ((i >= IW_MODE_ADHOC && i <= IW_MODE_REPEAT) ||
3170	    i == IW_MODE_MONITOR) {
3171		local->iw_mode = i;
3172	} else {
3173		printk(KERN_WARNING "prism2: Unknown iw_mode %d; using "
3174		       "IW_MODE_MASTER\n", i);
3175		local->iw_mode = IW_MODE_MASTER;
3176	}
3177	local->channel = GET_INT_PARM(channel, card_idx);
3178	local->beacon_int = GET_INT_PARM(beacon_int, card_idx);
3179	local->dtim_period = GET_INT_PARM(dtim_period, card_idx);
3180	local->wds_max_connections = 16;
3181	local->tx_control = HFA384X_TX_CTRL_FLAGS;
3182	local->manual_retry_count = -1;
3183	local->rts_threshold = 2347;
3184	local->fragm_threshold = 2346;
3185	local->rssi_to_dBm = 100; /* default; to be overriden by
3186				   * cnfDbmAdjust, if available */
3187	local->auth_algs = PRISM2_AUTH_OPEN | PRISM2_AUTH_SHARED_KEY;
3188	local->sram_type = -1;
3189	local->scan_channel_mask = 0xffff;
3190	local->monitor_type = PRISM2_MONITOR_RADIOTAP;
3191
3192	/* Initialize task queue structures */
3193	INIT_WORK(&local->reset_queue, handle_reset_queue);
3194	INIT_WORK(&local->set_multicast_list_queue,
3195		  hostap_set_multicast_list_queue);
3196
3197	INIT_WORK(&local->set_tim_queue, handle_set_tim_queue);
3198	INIT_LIST_HEAD(&local->set_tim_list);
3199	spin_lock_init(&local->set_tim_lock);
3200
3201	INIT_WORK(&local->comms_qual_update, handle_comms_qual_update);
3202
3203	/* Initialize tasklets for handling hardware IRQ related operations
3204	 * outside hw IRQ handler */
3205#define HOSTAP_TASKLET_INIT(q, f, d) \
3206do { memset((q), 0, sizeof(*(q))); (q)->func = (f); (q)->data = (d); } \
3207while (0)
3208	HOSTAP_TASKLET_INIT(&local->bap_tasklet, hostap_bap_tasklet,
3209			    (unsigned long) local);
3210
3211	HOSTAP_TASKLET_INIT(&local->info_tasklet, hostap_info_tasklet,
3212			    (unsigned long) local);
3213	hostap_info_init(local);
3214
3215	HOSTAP_TASKLET_INIT(&local->rx_tasklet,
3216			    hostap_rx_tasklet, (unsigned long) local);
3217	skb_queue_head_init(&local->rx_list);
3218
3219	HOSTAP_TASKLET_INIT(&local->sta_tx_exc_tasklet,
3220			    hostap_sta_tx_exc_tasklet, (unsigned long) local);
3221	skb_queue_head_init(&local->sta_tx_exc_list);
3222
3223	INIT_LIST_HEAD(&local->cmd_queue);
3224	init_waitqueue_head(&local->hostscan_wq);
3225
3226	lib80211_crypt_info_init(&local->crypt_info, dev->name, &local->lock);
3227
3228	timer_setup(&local->passive_scan_timer, hostap_passive_scan, 0);
3229	timer_setup(&local->tick_timer, hostap_tick_timer, 0);
3230	local->tick_timer.expires = jiffies + 2 * HZ;
3231	add_timer(&local->tick_timer);
3232
3233	INIT_LIST_HEAD(&local->bss_list);
3234
3235	hostap_setup_dev(dev, local, HOSTAP_INTERFACE_MASTER);
3236
3237	dev->type = ARPHRD_IEEE80211;
3238	dev->header_ops = &hostap_80211_ops;
3239
3240	rtnl_lock();
3241	ret = dev_alloc_name(dev, "wifi%d");
3242	SET_NETDEV_DEV(dev, sdev);
3243	if (ret >= 0)
3244		ret = register_netdevice(dev);
3245
3246	prism2_set_lockdep_class(dev);
3247	rtnl_unlock();
3248	if (ret < 0) {
3249		printk(KERN_WARNING "%s: register netdevice failed!\n",
3250		       dev_info);
3251		goto fail;
3252	}
3253	printk(KERN_INFO "%s: Registered netdevice %s\n", dev_info, dev->name);
3254
3255	hostap_init_data(local);
3256	return dev;
3257
3258 fail:
3259	free_netdev(dev);
3260	return NULL;
3261}
3262
3263
3264static int hostap_hw_ready(struct net_device *dev)
3265{
3266	struct hostap_interface *iface;
3267	struct local_info *local;
3268
3269	iface = netdev_priv(dev);
3270	local = iface->local;
3271	local->ddev = hostap_add_interface(local, HOSTAP_INTERFACE_MAIN, 0,
3272					   "", dev_template);
3273
3274	if (local->ddev) {
3275		if (local->iw_mode == IW_MODE_INFRA ||
3276		    local->iw_mode == IW_MODE_ADHOC) {
3277			netif_carrier_off(local->dev);
3278			netif_carrier_off(local->ddev);
3279		}
3280		hostap_init_proc(local);
3281#ifndef PRISM2_NO_PROCFS_DEBUG
3282		proc_create_data("registers", 0, local->proc,
3283				 &prism2_registers_proc_fops, local);
3284#endif /* PRISM2_NO_PROCFS_DEBUG */
3285		hostap_init_ap_proc(local);
3286		return 0;
3287	}
3288
3289	return -1;
3290}
3291
3292
3293static void prism2_free_local_data(struct net_device *dev)
3294{
3295	struct hostap_tx_callback_info *tx_cb, *tx_cb_prev;
3296	int i;
3297	struct hostap_interface *iface;
3298	struct local_info *local;
3299	struct list_head *ptr, *n;
3300
3301	if (dev == NULL)
3302		return;
3303
3304	iface = netdev_priv(dev);
3305	local = iface->local;
3306
3307	/* Unregister all netdevs before freeing local data. */
3308	list_for_each_safe(ptr, n, &local->hostap_interfaces) {
3309		iface = list_entry(ptr, struct hostap_interface, list);
3310		if (iface->type == HOSTAP_INTERFACE_MASTER) {
3311			/* special handling for this interface below */
3312			continue;
3313		}
3314		hostap_remove_interface(iface->dev, 0, 1);
3315	}
3316
3317	unregister_netdev(local->dev);
3318
3319	flush_work(&local->reset_queue);
3320	flush_work(&local->set_multicast_list_queue);
3321	flush_work(&local->set_tim_queue);
3322#ifndef PRISM2_NO_STATION_MODES
3323	flush_work(&local->info_queue);
3324#endif
3325	flush_work(&local->comms_qual_update);
3326
3327	lib80211_crypt_info_free(&local->crypt_info);
3328
3329	if (timer_pending(&local->passive_scan_timer))
3330		del_timer(&local->passive_scan_timer);
3331
3332	if (timer_pending(&local->tick_timer))
3333		del_timer(&local->tick_timer);
3334
3335	prism2_clear_cmd_queue(local);
3336
3337	skb_queue_purge(&local->info_list);
3338	skb_queue_purge(&local->rx_list);
3339	skb_queue_purge(&local->sta_tx_exc_list);
3340
3341	if (local->dev_enabled)
3342		prism2_callback(local, PRISM2_CALLBACK_DISABLE);
3343
3344	if (local->ap != NULL)
3345		hostap_free_data(local->ap);
3346
3347#ifndef PRISM2_NO_PROCFS_DEBUG
3348	if (local->proc != NULL)
3349		remove_proc_entry("registers", local->proc);
3350#endif /* PRISM2_NO_PROCFS_DEBUG */
3351	hostap_remove_proc(local);
3352
3353	tx_cb = local->tx_callback;
3354	while (tx_cb != NULL) {
3355		tx_cb_prev = tx_cb;
3356		tx_cb = tx_cb->next;
3357		kfree(tx_cb_prev);
3358	}
3359
3360	hostap_set_hostapd(local, 0, 0);
3361	hostap_set_hostapd_sta(local, 0, 0);
3362
3363	for (i = 0; i < PRISM2_FRAG_CACHE_LEN; i++) {
3364		if (local->frag_cache[i].skb != NULL)
3365			dev_kfree_skb(local->frag_cache[i].skb);
3366	}
3367
3368#ifdef PRISM2_DOWNLOAD_SUPPORT
3369	prism2_download_free_data(local->dl_pri);
3370	prism2_download_free_data(local->dl_sec);
3371#endif /* PRISM2_DOWNLOAD_SUPPORT */
3372
3373	prism2_clear_set_tim_queue(local);
3374
3375	list_for_each_safe(ptr, n, &local->bss_list) {
3376		struct hostap_bss_info *bss =
3377			list_entry(ptr, struct hostap_bss_info, list);
3378		kfree(bss);
3379	}
3380
3381	kfree(local->pda);
3382	kfree(local->last_scan_results);
3383	kfree(local->generic_elem);
3384
3385	free_netdev(local->dev);
3386}
3387
3388
3389#if (defined(PRISM2_PCI) && defined(CONFIG_PM)) || defined(PRISM2_PCCARD)
3390static void prism2_suspend(struct net_device *dev)
3391{
3392	struct hostap_interface *iface;
3393	struct local_info *local;
3394	union iwreq_data wrqu;
3395
3396	iface = netdev_priv(dev);
3397	local = iface->local;
3398
3399	/* Send disconnect event, e.g., to trigger reassociation after resume
3400	 * if wpa_supplicant is used. */
3401	memset(&wrqu, 0, sizeof(wrqu));
3402	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
3403	wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
3404
3405	/* Disable hardware and firmware */
3406	prism2_hw_shutdown(dev, 0);
3407}
3408#endif /* (PRISM2_PCI && CONFIG_PM) || PRISM2_PCCARD */
3409
3410
3411/* These might at some point be compiled separately and used as separate
3412 * kernel modules or linked into one */
3413#ifdef PRISM2_DOWNLOAD_SUPPORT
3414#include "hostap_download.c"
3415#endif /* PRISM2_DOWNLOAD_SUPPORT */
3416
3417#ifdef PRISM2_CALLBACK
3418/* External hostap_callback.c file can be used to, e.g., blink activity led.
3419 * This can use platform specific code and must define prism2_callback()
3420 * function (if PRISM2_CALLBACK is not defined, these function calls are not
3421 * used. */
3422#include "hostap_callback.c"
3423#endif /* PRISM2_CALLBACK */
v6.2
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Host AP (software wireless LAN access point) driver for
   4 * Intersil Prism2/2.5/3.
   5 *
   6 * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
   7 * <j@w1.fi>
   8 * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
   9 *
 
 
 
 
 
  10 * FIX:
  11 * - there is currently no way of associating TX packets to correct wds device
  12 *   when TX Exc/OK event occurs, so all tx_packets and some
  13 *   tx_errors/tx_dropped are added to the main netdevice; using sw_support
  14 *   field in txdesc might be used to fix this (using Alloc event to increment
  15 *   tx_packets would need some further info in txfid table)
  16 *
  17 * Buffer Access Path (BAP) usage:
  18 *   Prism2 cards have two separate BAPs for accessing the card memory. These
  19 *   should allow concurrent access to two different frames and the driver
  20 *   previously used BAP0 for sending data and BAP1 for receiving data.
  21 *   However, there seems to be number of issues with concurrent access and at
  22 *   least one know hardware bug in using BAP0 and BAP1 concurrently with PCI
  23 *   Prism2.5. Therefore, the driver now only uses BAP0 for moving data between
  24 *   host and card memories. BAP0 accesses are protected with local->baplock
  25 *   (spin_lock_bh) to prevent concurrent use.
  26 */
  27
  28
  29
  30#include <asm/delay.h>
  31#include <linux/uaccess.h>
  32
  33#include <linux/slab.h>
  34#include <linux/netdevice.h>
  35#include <linux/etherdevice.h>
  36#include <linux/proc_fs.h>
  37#include <linux/seq_file.h>
  38#include <linux/if_arp.h>
  39#include <linux/delay.h>
  40#include <linux/random.h>
  41#include <linux/wait.h>
  42#include <linux/sched/signal.h>
  43#include <linux/rtnetlink.h>
  44#include <linux/wireless.h>
  45#include <net/iw_handler.h>
  46#include <net/lib80211.h>
  47#include <asm/irq.h>
  48
  49#include "hostap_80211.h"
  50#include "hostap.h"
  51#include "hostap_ap.h"
  52
  53
  54/* #define final_version */
  55
  56static int mtu = 1500;
  57module_param(mtu, int, 0444);
  58MODULE_PARM_DESC(mtu, "Maximum transfer unit");
  59
  60static int channel[MAX_PARM_DEVICES] = { 3, DEF_INTS };
  61module_param_array(channel, int, NULL, 0444);
  62MODULE_PARM_DESC(channel, "Initial channel");
  63
  64static char essid[33] = "test";
  65module_param_string(essid, essid, sizeof(essid), 0444);
  66MODULE_PARM_DESC(essid, "Host AP's ESSID");
  67
  68static int iw_mode[MAX_PARM_DEVICES] = { IW_MODE_MASTER, DEF_INTS };
  69module_param_array(iw_mode, int, NULL, 0444);
  70MODULE_PARM_DESC(iw_mode, "Initial operation mode");
  71
  72static int beacon_int[MAX_PARM_DEVICES] = { 100, DEF_INTS };
  73module_param_array(beacon_int, int, NULL, 0444);
  74MODULE_PARM_DESC(beacon_int, "Beacon interval (1 = 1024 usec)");
  75
  76static int dtim_period[MAX_PARM_DEVICES] = { 1, DEF_INTS };
  77module_param_array(dtim_period, int, NULL, 0444);
  78MODULE_PARM_DESC(dtim_period, "DTIM period");
  79
  80static char dev_template[16] = "wlan%d";
  81module_param_string(dev_template, dev_template, sizeof(dev_template), 0444);
  82MODULE_PARM_DESC(dev_template, "Prefix for network device name (default: "
  83		 "wlan%d)");
  84
  85#ifdef final_version
  86#define EXTRA_EVENTS_WTERR 0
  87#else
  88/* check WTERR events (Wait Time-out) in development versions */
  89#define EXTRA_EVENTS_WTERR HFA384X_EV_WTERR
  90#endif
  91
  92/* Events that will be using BAP0 */
  93#define HFA384X_BAP0_EVENTS \
  94	(HFA384X_EV_TXEXC | HFA384X_EV_RX | HFA384X_EV_INFO | HFA384X_EV_TX)
  95
  96/* event mask, i.e., events that will result in an interrupt */
  97#define HFA384X_EVENT_MASK \
  98	(HFA384X_BAP0_EVENTS | HFA384X_EV_ALLOC | HFA384X_EV_INFDROP | \
  99	HFA384X_EV_CMD | HFA384X_EV_TICK | \
 100	EXTRA_EVENTS_WTERR)
 101
 102/* Default TX control flags: use 802.11 headers and request interrupt for
 103 * failed transmits. Frames that request ACK callback, will add
 104 * _TX_OK flag and _ALT_RTRY flag may be used to select different retry policy.
 105 */
 106#define HFA384X_TX_CTRL_FLAGS \
 107	(HFA384X_TX_CTRL_802_11 | HFA384X_TX_CTRL_TX_EX)
 108
 109
 110/* ca. 1 usec */
 111#define HFA384X_CMD_BUSY_TIMEOUT 5000
 112#define HFA384X_BAP_BUSY_TIMEOUT 50000
 113
 114/* ca. 10 usec */
 115#define HFA384X_CMD_COMPL_TIMEOUT 20000
 116#define HFA384X_DL_COMPL_TIMEOUT 1000000
 117
 118/* Wait times for initialization; yield to other processes to avoid busy
 119 * waiting for long time. */
 120#define HFA384X_INIT_TIMEOUT (HZ / 2) /* 500 ms */
 121#define HFA384X_ALLOC_COMPL_TIMEOUT (HZ / 20) /* 50 ms */
 122
 123
 124static void prism2_hw_reset(struct net_device *dev);
 125static void prism2_check_sta_fw_version(local_info_t *local);
 126
 127#ifdef PRISM2_DOWNLOAD_SUPPORT
 128/* hostap_download.c */
 129static const struct proc_ops prism2_download_aux_dump_proc_ops;
 130static u8 * prism2_read_pda(struct net_device *dev);
 131static int prism2_download(local_info_t *local,
 132			   struct prism2_download_param *param);
 133static void prism2_download_free_data(struct prism2_download_data *dl);
 134static int prism2_download_volatile(local_info_t *local,
 135				    struct prism2_download_data *param);
 136static int prism2_download_genesis(local_info_t *local,
 137				   struct prism2_download_data *param);
 138static int prism2_get_ram_size(local_info_t *local);
 139#endif /* PRISM2_DOWNLOAD_SUPPORT */
 140
 141
 142
 143
 144#ifndef final_version
 145/* magic value written to SWSUPPORT0 reg. for detecting whether card is still
 146 * present */
 147#define HFA384X_MAGIC 0x8A32
 148#endif
 149
 
 
 
 
 
 
 
 150static void hfa384x_read_regs(struct net_device *dev,
 151			      struct hfa384x_regs *regs)
 152{
 153	regs->cmd = HFA384X_INW(HFA384X_CMD_OFF);
 154	regs->evstat = HFA384X_INW(HFA384X_EVSTAT_OFF);
 155	regs->offset0 = HFA384X_INW(HFA384X_OFFSET0_OFF);
 156	regs->offset1 = HFA384X_INW(HFA384X_OFFSET1_OFF);
 157	regs->swsupport0 = HFA384X_INW(HFA384X_SWSUPPORT0_OFF);
 158}
 159
 160
 161/**
 162 * __hostap_cmd_queue_free - Free Prism2 command queue entry (private)
 163 * @local: pointer to private Host AP driver data
 164 * @entry: Prism2 command queue entry to be freed
 165 * @del_req: request the entry to be removed
 166 *
 167 * Internal helper function for freeing Prism2 command queue entries.
 168 * Caller must have acquired local->cmdlock before calling this function.
 169 */
 170static inline void __hostap_cmd_queue_free(local_info_t *local,
 171					   struct hostap_cmd_queue *entry,
 172					   int del_req)
 173{
 174	if (del_req) {
 175		entry->del_req = 1;
 176		if (!list_empty(&entry->list)) {
 177			list_del_init(&entry->list);
 178			local->cmd_queue_len--;
 179		}
 180	}
 181
 182	if (refcount_dec_and_test(&entry->usecnt) && entry->del_req)
 183		kfree(entry);
 184}
 185
 186
 187/**
 188 * hostap_cmd_queue_free - Free Prism2 command queue entry
 189 * @local: pointer to private Host AP driver data
 190 * @entry: Prism2 command queue entry to be freed
 191 * @del_req: request the entry to be removed
 192 *
 193 * Free a Prism2 command queue entry.
 194 */
 195static inline void hostap_cmd_queue_free(local_info_t *local,
 196					 struct hostap_cmd_queue *entry,
 197					 int del_req)
 198{
 199	unsigned long flags;
 200
 201	spin_lock_irqsave(&local->cmdlock, flags);
 202	__hostap_cmd_queue_free(local, entry, del_req);
 203	spin_unlock_irqrestore(&local->cmdlock, flags);
 204}
 205
 206
 207/**
 208 * prism2_clear_cmd_queue - Free all pending Prism2 command queue entries
 209 * @local: pointer to private Host AP driver data
 210 */
 211static void prism2_clear_cmd_queue(local_info_t *local)
 212{
 213	struct list_head *ptr, *n;
 214	unsigned long flags;
 215	struct hostap_cmd_queue *entry;
 216
 217	spin_lock_irqsave(&local->cmdlock, flags);
 218	list_for_each_safe(ptr, n, &local->cmd_queue) {
 219		entry = list_entry(ptr, struct hostap_cmd_queue, list);
 220		refcount_inc(&entry->usecnt);
 221		printk(KERN_DEBUG "%s: removed pending cmd_queue entry "
 222		       "(type=%d, cmd=0x%04x, param0=0x%04x)\n",
 223		       local->dev->name, entry->type, entry->cmd,
 224		       entry->param0);
 225		__hostap_cmd_queue_free(local, entry, 1);
 226	}
 227	if (local->cmd_queue_len) {
 228		/* This should not happen; print debug message and clear
 229		 * queue length. */
 230		printk(KERN_DEBUG "%s: cmd_queue_len (%d) not zero after "
 231		       "flush\n", local->dev->name, local->cmd_queue_len);
 232		local->cmd_queue_len = 0;
 233	}
 234	spin_unlock_irqrestore(&local->cmdlock, flags);
 235}
 236
 237
 238/**
 239 * hfa384x_cmd_issue - Issue a Prism2 command to the hardware
 240 * @dev: pointer to net_device
 241 * @entry: Prism2 command queue entry to be issued
 242 */
 243static int hfa384x_cmd_issue(struct net_device *dev,
 244				    struct hostap_cmd_queue *entry)
 245{
 246	struct hostap_interface *iface;
 247	local_info_t *local;
 248	int tries;
 249	u16 reg;
 250	unsigned long flags;
 251
 252	iface = netdev_priv(dev);
 253	local = iface->local;
 254
 255	if (local->func->card_present && !local->func->card_present(local))
 256		return -ENODEV;
 257
 258	if (entry->issued) {
 259		printk(KERN_DEBUG "%s: driver bug - re-issuing command @%p\n",
 260		       dev->name, entry);
 261	}
 262
 263	/* wait until busy bit is clear; this should always be clear since the
 264	 * commands are serialized */
 265	tries = HFA384X_CMD_BUSY_TIMEOUT;
 266	while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
 267		tries--;
 268		udelay(1);
 269	}
 270#ifndef final_version
 271	if (tries != HFA384X_CMD_BUSY_TIMEOUT) {
 272		prism2_io_debug_error(dev, 1);
 273		printk(KERN_DEBUG "%s: hfa384x_cmd_issue: cmd reg was busy "
 274		       "for %d usec\n", dev->name,
 275		       HFA384X_CMD_BUSY_TIMEOUT - tries);
 276	}
 277#endif
 278	if (tries == 0) {
 279		reg = HFA384X_INW(HFA384X_CMD_OFF);
 280		prism2_io_debug_error(dev, 2);
 281		printk(KERN_DEBUG "%s: hfa384x_cmd_issue - timeout - "
 282		       "reg=0x%04x\n", dev->name, reg);
 283		return -ETIMEDOUT;
 284	}
 285
 286	/* write command */
 287	spin_lock_irqsave(&local->cmdlock, flags);
 288	HFA384X_OUTW(entry->param0, HFA384X_PARAM0_OFF);
 289	HFA384X_OUTW(entry->param1, HFA384X_PARAM1_OFF);
 290	HFA384X_OUTW(entry->cmd, HFA384X_CMD_OFF);
 291	entry->issued = 1;
 292	spin_unlock_irqrestore(&local->cmdlock, flags);
 293
 294	return 0;
 295}
 296
 297
 298/**
 299 * hfa384x_cmd - Issue a Prism2 command and wait (sleep) for completion
 300 * @dev: pointer to net_device
 301 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
 302 * @param0: value for Param0 register
 303 * @param1: value for Param1 register (pointer; %NULL if not used)
 304 * @resp0: pointer for Resp0 data or %NULL if Resp0 is not needed
 305 *
 306 * Issue given command (possibly after waiting in command queue) and sleep
 307 * until the command is completed (or timed out or interrupted). This can be
 308 * called only from user process context.
 309 */
 310static int hfa384x_cmd(struct net_device *dev, u16 cmd, u16 param0,
 311		       u16 *param1, u16 *resp0)
 312{
 313	struct hostap_interface *iface;
 314	local_info_t *local;
 315	int err, res, issue, issued = 0;
 316	unsigned long flags;
 317	struct hostap_cmd_queue *entry;
 318	DECLARE_WAITQUEUE(wait, current);
 319
 320	iface = netdev_priv(dev);
 321	local = iface->local;
 322
 
 
 
 
 
 
 323	if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN) {
 324		printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
 325		       dev->name);
 326		return -1;
 327	}
 328
 329	if (signal_pending(current))
 330		return -EINTR;
 331
 332	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
 333	if (entry == NULL)
 334		return -ENOMEM;
 335
 336	refcount_set(&entry->usecnt, 1);
 337	entry->type = CMD_SLEEP;
 338	entry->cmd = cmd;
 339	entry->param0 = param0;
 340	if (param1)
 341		entry->param1 = *param1;
 342	init_waitqueue_head(&entry->compl);
 343
 344	/* prepare to wait for command completion event, but do not sleep yet
 345	 */
 346	add_wait_queue(&entry->compl, &wait);
 347	set_current_state(TASK_INTERRUPTIBLE);
 348
 349	spin_lock_irqsave(&local->cmdlock, flags);
 350	issue = list_empty(&local->cmd_queue);
 351	if (issue)
 352		entry->issuing = 1;
 353	list_add_tail(&entry->list, &local->cmd_queue);
 354	local->cmd_queue_len++;
 355	spin_unlock_irqrestore(&local->cmdlock, flags);
 356
 357	err = 0;
 358	if (!issue)
 359		goto wait_completion;
 360
 361	if (signal_pending(current))
 362		err = -EINTR;
 363
 364	if (!err) {
 365		if (hfa384x_cmd_issue(dev, entry))
 366			err = -ETIMEDOUT;
 367		else
 368			issued = 1;
 369	}
 370
 371 wait_completion:
 372	if (!err && entry->type != CMD_COMPLETED) {
 373		/* sleep until command is completed or timed out */
 374		res = schedule_timeout(2 * HZ);
 375	} else
 376		res = -1;
 377
 378	if (!err && signal_pending(current))
 379		err = -EINTR;
 380
 381	if (err && issued) {
 382		/* the command was issued, so a CmdCompl event should occur
 383		 * soon; however, there's a pending signal and
 384		 * schedule_timeout() would be interrupted; wait a short period
 385		 * of time to avoid removing entry from the list before
 386		 * CmdCompl event */
 387		udelay(300);
 388	}
 389
 390	set_current_state(TASK_RUNNING);
 391	remove_wait_queue(&entry->compl, &wait);
 392
 393	/* If entry->list is still in the list, it must be removed
 394	 * first and in this case prism2_cmd_ev() does not yet have
 395	 * local reference to it, and the data can be kfree()'d
 396	 * here. If the command completion event is still generated,
 397	 * it will be assigned to next (possibly) pending command, but
 398	 * the driver will reset the card anyway due to timeout
 399	 *
 400	 * If the entry is not in the list prism2_cmd_ev() has a local
 401	 * reference to it, but keeps cmdlock as long as the data is
 402	 * needed, so the data can be kfree()'d here. */
 403
 404	/* FIX: if the entry->list is in the list, it has not been completed
 405	 * yet, so removing it here is somewhat wrong.. this could cause
 406	 * references to freed memory and next list_del() causing NULL pointer
 407	 * dereference.. it would probably be better to leave the entry in the
 408	 * list and the list should be emptied during hw reset */
 409
 410	spin_lock_irqsave(&local->cmdlock, flags);
 411	if (!list_empty(&entry->list)) {
 412		printk(KERN_DEBUG "%s: hfa384x_cmd: entry still in list? "
 413		       "(entry=%p, type=%d, res=%d)\n", dev->name, entry,
 414		       entry->type, res);
 415		list_del_init(&entry->list);
 416		local->cmd_queue_len--;
 417	}
 418	spin_unlock_irqrestore(&local->cmdlock, flags);
 419
 420	if (err) {
 421		printk(KERN_DEBUG "%s: hfa384x_cmd: interrupted; err=%d\n",
 422		       dev->name, err);
 423		res = err;
 424		goto done;
 425	}
 426
 427	if (entry->type != CMD_COMPLETED) {
 428		u16 reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
 429		printk(KERN_DEBUG "%s: hfa384x_cmd: command was not "
 430		       "completed (res=%d, entry=%p, type=%d, cmd=0x%04x, "
 431		       "param0=0x%04x, EVSTAT=%04x INTEN=%04x)\n", dev->name,
 432		       res, entry, entry->type, entry->cmd, entry->param0, reg,
 433		       HFA384X_INW(HFA384X_INTEN_OFF));
 434		if (reg & HFA384X_EV_CMD) {
 435			/* Command completion event is pending, but the
 436			 * interrupt was not delivered - probably an issue
 437			 * with pcmcia-cs configuration. */
 438			printk(KERN_WARNING "%s: interrupt delivery does not "
 439			       "seem to work\n", dev->name);
 440		}
 441		prism2_io_debug_error(dev, 3);
 442		res = -ETIMEDOUT;
 443		goto done;
 444	}
 445
 446	if (resp0 != NULL)
 447		*resp0 = entry->resp0;
 448#ifndef final_version
 449	if (entry->res) {
 450		printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x, "
 451		       "resp0=0x%04x\n",
 452		       dev->name, cmd, entry->res, entry->resp0);
 453	}
 454#endif /* final_version */
 455
 456	res = entry->res;
 457 done:
 458	hostap_cmd_queue_free(local, entry, 1);
 459	return res;
 460}
 461
 462
 463/**
 464 * hfa384x_cmd_callback - Issue a Prism2 command; callback when completed
 465 * @dev: pointer to net_device
 466 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
 467 * @param0: value for Param0 register
 468 * @callback: command completion callback function (%NULL = no callback)
 469 * @context: context data to be given to the callback function
 470 *
 471 * Issue given command (possibly after waiting in command queue) and use
 472 * callback function to indicate command completion. This can be called both
 473 * from user and interrupt context. The callback function will be called in
 474 * hardware IRQ context. It can be %NULL, when no function is called when
 475 * command is completed.
 476 */
 477static int hfa384x_cmd_callback(struct net_device *dev, u16 cmd, u16 param0,
 478				void (*callback)(struct net_device *dev,
 479						 long context, u16 resp0,
 480						 u16 status),
 481				long context)
 482{
 483	struct hostap_interface *iface;
 484	local_info_t *local;
 485	int issue, ret;
 486	unsigned long flags;
 487	struct hostap_cmd_queue *entry;
 488
 489	iface = netdev_priv(dev);
 490	local = iface->local;
 491
 492	if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN + 2) {
 493		printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
 494		       dev->name);
 495		return -1;
 496	}
 497
 498	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
 499	if (entry == NULL)
 500		return -ENOMEM;
 501
 502	refcount_set(&entry->usecnt, 1);
 503	entry->type = CMD_CALLBACK;
 504	entry->cmd = cmd;
 505	entry->param0 = param0;
 506	entry->callback = callback;
 507	entry->context = context;
 508
 509	spin_lock_irqsave(&local->cmdlock, flags);
 510	issue = list_empty(&local->cmd_queue);
 511	if (issue)
 512		entry->issuing = 1;
 513	list_add_tail(&entry->list, &local->cmd_queue);
 514	local->cmd_queue_len++;
 515	spin_unlock_irqrestore(&local->cmdlock, flags);
 516
 517	if (issue && hfa384x_cmd_issue(dev, entry))
 518		ret = -ETIMEDOUT;
 519	else
 520		ret = 0;
 521
 522	hostap_cmd_queue_free(local, entry, ret);
 523
 524	return ret;
 525}
 526
 527
 528/**
 529 * __hfa384x_cmd_no_wait - Issue a Prism2 command (private)
 530 * @dev: pointer to net_device
 531 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
 532 * @param0: value for Param0 register
 533 * @io_debug_num: I/O debug error number
 534 *
 535 * Shared helper function for hfa384x_cmd_wait() and hfa384x_cmd_no_wait().
 536 */
 537static int __hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd, u16 param0,
 538				 int io_debug_num)
 539{
 540	int tries;
 541	u16 reg;
 542
 543	/* wait until busy bit is clear; this should always be clear since the
 544	 * commands are serialized */
 545	tries = HFA384X_CMD_BUSY_TIMEOUT;
 546	while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
 547		tries--;
 548		udelay(1);
 549	}
 550	if (tries == 0) {
 551		reg = HFA384X_INW(HFA384X_CMD_OFF);
 552		prism2_io_debug_error(dev, io_debug_num);
 553		printk(KERN_DEBUG "%s: __hfa384x_cmd_no_wait(%d) - timeout - "
 554		       "reg=0x%04x\n", dev->name, io_debug_num, reg);
 555		return -ETIMEDOUT;
 556	}
 557
 558	/* write command */
 559	HFA384X_OUTW(param0, HFA384X_PARAM0_OFF);
 560	HFA384X_OUTW(cmd, HFA384X_CMD_OFF);
 561
 562	return 0;
 563}
 564
 565
 566/**
 567 * hfa384x_cmd_wait - Issue a Prism2 command and busy wait for completion
 568 * @dev: pointer to net_device
 569 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
 570 * @param0: value for Param0 register
 571 */
 572static int hfa384x_cmd_wait(struct net_device *dev, u16 cmd, u16 param0)
 573{
 574	int res, tries;
 575	u16 reg;
 576
 577	res = __hfa384x_cmd_no_wait(dev, cmd, param0, 4);
 578	if (res)
 579		return res;
 580
 581        /* wait for command completion */
 582	if ((cmd & HFA384X_CMDCODE_MASK) == HFA384X_CMDCODE_DOWNLOAD)
 583		tries = HFA384X_DL_COMPL_TIMEOUT;
 584	else
 585		tries = HFA384X_CMD_COMPL_TIMEOUT;
 586
 587        while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
 588               tries > 0) {
 589                tries--;
 590                udelay(10);
 591        }
 592        if (tries == 0) {
 593                reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
 594		prism2_io_debug_error(dev, 5);
 595                printk(KERN_DEBUG "%s: hfa384x_cmd_wait - timeout2 - "
 596		       "reg=0x%04x\n", dev->name, reg);
 597                return -ETIMEDOUT;
 598        }
 599
 600        res = (HFA384X_INW(HFA384X_STATUS_OFF) &
 601               (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) | BIT(9) |
 602                BIT(8))) >> 8;
 603#ifndef final_version
 604	if (res) {
 605		printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x\n",
 606		       dev->name, cmd, res);
 607	}
 608#endif
 609
 610	HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
 611
 612	return res;
 613}
 614
 615
 616/**
 617 * hfa384x_cmd_no_wait - Issue a Prism2 command; do not wait for completion
 618 * @dev: pointer to net_device
 619 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
 620 * @param0: value for Param0 register
 621 */
 622static inline int hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd,
 623				      u16 param0)
 624{
 625	return __hfa384x_cmd_no_wait(dev, cmd, param0, 6);
 626}
 627
 628
 629/**
 630 * prism2_cmd_ev - Prism2 command completion event handler
 631 * @dev: pointer to net_device
 632 *
 633 * Interrupt handler for command completion events. Called by the main
 634 * interrupt handler in hardware IRQ context. Read Resp0 and status registers
 635 * from the hardware and ACK the event. Depending on the issued command type
 636 * either wake up the sleeping process that is waiting for command completion
 637 * or call the callback function. Issue the next command, if one is pending.
 638 */
 639static void prism2_cmd_ev(struct net_device *dev)
 640{
 641	struct hostap_interface *iface;
 642	local_info_t *local;
 643	struct hostap_cmd_queue *entry = NULL;
 644
 645	iface = netdev_priv(dev);
 646	local = iface->local;
 647
 648	spin_lock(&local->cmdlock);
 649	if (!list_empty(&local->cmd_queue)) {
 650		entry = list_entry(local->cmd_queue.next,
 651				   struct hostap_cmd_queue, list);
 652		refcount_inc(&entry->usecnt);
 653		list_del_init(&entry->list);
 654		local->cmd_queue_len--;
 655
 656		if (!entry->issued) {
 657			printk(KERN_DEBUG "%s: Command completion event, but "
 658			       "cmd not issued\n", dev->name);
 659			__hostap_cmd_queue_free(local, entry, 1);
 660			entry = NULL;
 661		}
 662	}
 663	spin_unlock(&local->cmdlock);
 664
 665	if (!entry) {
 666		HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
 667		printk(KERN_DEBUG "%s: Command completion event, but no "
 668		       "pending commands\n", dev->name);
 669		return;
 670	}
 671
 672	entry->resp0 = HFA384X_INW(HFA384X_RESP0_OFF);
 673	entry->res = (HFA384X_INW(HFA384X_STATUS_OFF) &
 674		      (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) |
 675		       BIT(9) | BIT(8))) >> 8;
 676	HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
 677
 678	/* TODO: rest of the CmdEv handling could be moved to tasklet */
 679	if (entry->type == CMD_SLEEP) {
 680		entry->type = CMD_COMPLETED;
 681		wake_up_interruptible(&entry->compl);
 682	} else if (entry->type == CMD_CALLBACK) {
 683		if (entry->callback)
 684			entry->callback(dev, entry->context, entry->resp0,
 685					entry->res);
 686	} else {
 687		printk(KERN_DEBUG "%s: Invalid command completion type %d\n",
 688		       dev->name, entry->type);
 689	}
 690	hostap_cmd_queue_free(local, entry, 1);
 691
 692	/* issue next command, if pending */
 693	entry = NULL;
 694	spin_lock(&local->cmdlock);
 695	if (!list_empty(&local->cmd_queue)) {
 696		entry = list_entry(local->cmd_queue.next,
 697				   struct hostap_cmd_queue, list);
 698		if (entry->issuing) {
 699			/* hfa384x_cmd() has already started issuing this
 700			 * command, so do not start here */
 701			entry = NULL;
 702		}
 703		if (entry)
 704			refcount_inc(&entry->usecnt);
 705	}
 706	spin_unlock(&local->cmdlock);
 707
 708	if (entry) {
 709		/* issue next command; if command issuing fails, remove the
 710		 * entry from cmd_queue */
 711		int res = hfa384x_cmd_issue(dev, entry);
 712		spin_lock(&local->cmdlock);
 713		__hostap_cmd_queue_free(local, entry, res);
 714		spin_unlock(&local->cmdlock);
 715	}
 716}
 717
 718
 719static int hfa384x_wait_offset(struct net_device *dev, u16 o_off)
 720{
 721	int tries = HFA384X_BAP_BUSY_TIMEOUT;
 722	int res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
 723
 724	while (res && tries > 0) {
 725		tries--;
 726		udelay(1);
 727		res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
 728	}
 729	return res;
 730}
 731
 732
 733/* Offset must be even */
 734static int hfa384x_setup_bap(struct net_device *dev, u16 bap, u16 id,
 735			     int offset)
 736{
 737	u16 o_off, s_off;
 738	int ret = 0;
 739
 740	if (offset % 2 || bap > 1)
 741		return -EINVAL;
 742
 743	if (bap == BAP1) {
 744		o_off = HFA384X_OFFSET1_OFF;
 745		s_off = HFA384X_SELECT1_OFF;
 746	} else {
 747		o_off = HFA384X_OFFSET0_OFF;
 748		s_off = HFA384X_SELECT0_OFF;
 749	}
 750
 751	if (hfa384x_wait_offset(dev, o_off)) {
 752		prism2_io_debug_error(dev, 7);
 753		printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout before\n",
 754		       dev->name);
 755		ret = -ETIMEDOUT;
 756		goto out;
 757	}
 758
 759	HFA384X_OUTW(id, s_off);
 760	HFA384X_OUTW(offset, o_off);
 761
 762	if (hfa384x_wait_offset(dev, o_off)) {
 763		prism2_io_debug_error(dev, 8);
 764		printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout after\n",
 765		       dev->name);
 766		ret = -ETIMEDOUT;
 767		goto out;
 768	}
 769#ifndef final_version
 770	if (HFA384X_INW(o_off) & HFA384X_OFFSET_ERR) {
 771		prism2_io_debug_error(dev, 9);
 772		printk(KERN_DEBUG "%s: hfa384x_setup_bap - offset error "
 773		       "(%d,0x04%x,%d); reg=0x%04x\n",
 774		       dev->name, bap, id, offset, HFA384X_INW(o_off));
 775		ret = -EINVAL;
 776	}
 777#endif
 778
 779 out:
 780	return ret;
 781}
 782
 783
 784static int hfa384x_get_rid(struct net_device *dev, u16 rid, void *buf, int len,
 785			   int exact_len)
 786{
 787	struct hostap_interface *iface;
 788	local_info_t *local;
 789	int res, rlen = 0;
 790	struct hfa384x_rid_hdr rec;
 791
 792	iface = netdev_priv(dev);
 793	local = iface->local;
 794
 795	if (local->no_pri) {
 796		printk(KERN_DEBUG "%s: cannot get RID %04x (len=%d) - no PRI "
 797		       "f/w\n", dev->name, rid, len);
 798		return -ENOTTY; /* Well.. not really correct, but return
 799				 * something unique enough.. */
 800	}
 801
 802	if ((local->func->card_present && !local->func->card_present(local)) ||
 803	    local->hw_downloading)
 804		return -ENODEV;
 805
 806	res = mutex_lock_interruptible(&local->rid_bap_mtx);
 807	if (res)
 808		return res;
 809
 810	res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS, rid, NULL, NULL);
 811	if (res) {
 812		printk(KERN_DEBUG "%s: hfa384x_get_rid: CMDCODE_ACCESS failed "
 813		       "(res=%d, rid=%04x, len=%d)\n",
 814		       dev->name, res, rid, len);
 815		mutex_unlock(&local->rid_bap_mtx);
 816		return res;
 817	}
 818
 819	spin_lock_bh(&local->baplock);
 820
 821	res = hfa384x_setup_bap(dev, BAP0, rid, 0);
 822	if (res)
 823		goto unlock;
 824
 825	res = hfa384x_from_bap(dev, BAP0, &rec, sizeof(rec));
 826	if (res)
 827		goto unlock;
 828
 829	if (le16_to_cpu(rec.len) == 0) {
 830		/* RID not available */
 831		res = -ENODATA;
 832		goto unlock;
 833	}
 834
 835	rlen = (le16_to_cpu(rec.len) - 1) * 2;
 836	if (exact_len && rlen != len) {
 837		printk(KERN_DEBUG "%s: hfa384x_get_rid - RID len mismatch: "
 838		       "rid=0x%04x, len=%d (expected %d)\n",
 839		       dev->name, rid, rlen, len);
 840		res = -ENODATA;
 841	}
 842
 843	res = hfa384x_from_bap(dev, BAP0, buf, len);
 844
 845unlock:
 846	spin_unlock_bh(&local->baplock);
 847	mutex_unlock(&local->rid_bap_mtx);
 848
 849	if (res) {
 850		if (res != -ENODATA)
 851			printk(KERN_DEBUG "%s: hfa384x_get_rid (rid=%04x, "
 852			       "len=%d) - failed - res=%d\n", dev->name, rid,
 853			       len, res);
 854		if (res == -ETIMEDOUT)
 855			prism2_hw_reset(dev);
 856		return res;
 857	}
 858
 859	return rlen;
 860}
 861
 862
 863static int hfa384x_set_rid(struct net_device *dev, u16 rid, void *buf, int len)
 864{
 865	struct hostap_interface *iface;
 866	local_info_t *local;
 867	struct hfa384x_rid_hdr rec;
 868	int res;
 869
 870	iface = netdev_priv(dev);
 871	local = iface->local;
 872
 873	if (local->no_pri) {
 874		printk(KERN_DEBUG "%s: cannot set RID %04x (len=%d) - no PRI "
 875		       "f/w\n", dev->name, rid, len);
 876		return -ENOTTY; /* Well.. not really correct, but return
 877				 * something unique enough.. */
 878	}
 879
 880	if ((local->func->card_present && !local->func->card_present(local)) ||
 881	    local->hw_downloading)
 882		return -ENODEV;
 883
 884	rec.rid = cpu_to_le16(rid);
 885	/* RID len in words and +1 for rec.rid */
 886	rec.len = cpu_to_le16(len / 2 + len % 2 + 1);
 887
 888	res = mutex_lock_interruptible(&local->rid_bap_mtx);
 889	if (res)
 890		return res;
 891
 892	spin_lock_bh(&local->baplock);
 893	res = hfa384x_setup_bap(dev, BAP0, rid, 0);
 894	if (!res)
 895		res = hfa384x_to_bap(dev, BAP0, &rec, sizeof(rec));
 896	if (!res)
 897		res = hfa384x_to_bap(dev, BAP0, buf, len);
 898	spin_unlock_bh(&local->baplock);
 899
 900	if (res) {
 901		printk(KERN_DEBUG "%s: hfa384x_set_rid (rid=%04x, len=%d) - "
 902		       "failed - res=%d\n", dev->name, rid, len, res);
 903		mutex_unlock(&local->rid_bap_mtx);
 904		return res;
 905	}
 906
 907	res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS_WRITE, rid, NULL, NULL);
 908	mutex_unlock(&local->rid_bap_mtx);
 909
 910	if (res) {
 911		printk(KERN_DEBUG "%s: hfa384x_set_rid: CMDCODE_ACCESS_WRITE "
 912		       "failed (res=%d, rid=%04x, len=%d)\n",
 913		       dev->name, res, rid, len);
 914
 915		if (res == -ETIMEDOUT)
 916			prism2_hw_reset(dev);
 917	}
 918
 919	return res;
 920}
 921
 922
 923static void hfa384x_disable_interrupts(struct net_device *dev)
 924{
 925	/* disable interrupts and clear event status */
 926	HFA384X_OUTW(0, HFA384X_INTEN_OFF);
 927	HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
 928}
 929
 930
 931static void hfa384x_enable_interrupts(struct net_device *dev)
 932{
 933	/* ack pending events and enable interrupts from selected events */
 934	HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
 935	HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
 936}
 937
 938
 939static void hfa384x_events_no_bap0(struct net_device *dev)
 940{
 941	HFA384X_OUTW(HFA384X_EVENT_MASK & ~HFA384X_BAP0_EVENTS,
 942		     HFA384X_INTEN_OFF);
 943}
 944
 945
 946static void hfa384x_events_all(struct net_device *dev)
 947{
 948	HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
 949}
 950
 951
 952static void hfa384x_events_only_cmd(struct net_device *dev)
 953{
 954	HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_INTEN_OFF);
 955}
 956
 957
 958static u16 hfa384x_allocate_fid(struct net_device *dev, int len)
 959{
 960	u16 fid;
 961	unsigned long delay;
 962
 963	/* FIX: this could be replace with hfa384x_cmd() if the Alloc event
 964	 * below would be handled like CmdCompl event (sleep here, wake up from
 965	 * interrupt handler */
 966	if (hfa384x_cmd_wait(dev, HFA384X_CMDCODE_ALLOC, len)) {
 967		printk(KERN_DEBUG "%s: cannot allocate fid, len=%d\n",
 968		       dev->name, len);
 969		return 0xffff;
 970	}
 971
 972	delay = jiffies + HFA384X_ALLOC_COMPL_TIMEOUT;
 973	while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC) &&
 974	       time_before(jiffies, delay))
 975		yield();
 976	if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC)) {
 977		printk("%s: fid allocate, len=%d - timeout\n", dev->name, len);
 978		return 0xffff;
 979	}
 980
 981	fid = HFA384X_INW(HFA384X_ALLOCFID_OFF);
 982	HFA384X_OUTW(HFA384X_EV_ALLOC, HFA384X_EVACK_OFF);
 983
 984	return fid;
 985}
 986
 987
 988static int prism2_reset_port(struct net_device *dev)
 989{
 990	struct hostap_interface *iface;
 991	local_info_t *local;
 992	int res;
 993
 994	iface = netdev_priv(dev);
 995	local = iface->local;
 996
 997	if (!local->dev_enabled)
 998		return 0;
 999
1000	res = hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0,
1001			  NULL, NULL);
1002	if (res)
1003		printk(KERN_DEBUG "%s: reset port failed to disable port\n",
1004		       dev->name);
1005	else {
1006		res = hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0,
1007				  NULL, NULL);
1008		if (res)
1009			printk(KERN_DEBUG "%s: reset port failed to enable "
1010			       "port\n", dev->name);
1011	}
1012
1013	/* It looks like at least some STA firmware versions reset
1014	 * fragmentation threshold back to 2346 after enable command. Restore
1015	 * the configured value, if it differs from this default. */
1016	if (local->fragm_threshold != 2346 &&
1017	    hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
1018			    local->fragm_threshold)) {
1019		printk(KERN_DEBUG "%s: failed to restore fragmentation "
1020		       "threshold (%d) after Port0 enable\n",
1021		       dev->name, local->fragm_threshold);
1022	}
1023
1024	/* Some firmwares lose antenna selection settings on reset */
1025	(void) hostap_set_antsel(local);
1026
1027	return res;
1028}
1029
1030
1031static int prism2_get_version_info(struct net_device *dev, u16 rid,
1032				   const char *txt)
1033{
1034	struct hfa384x_comp_ident comp;
1035	struct hostap_interface *iface;
1036	local_info_t *local;
1037
1038	iface = netdev_priv(dev);
1039	local = iface->local;
1040
1041	if (local->no_pri) {
1042		/* PRI f/w not yet available - cannot read RIDs */
1043		return -1;
1044	}
1045	if (hfa384x_get_rid(dev, rid, &comp, sizeof(comp), 1) < 0) {
1046		printk(KERN_DEBUG "Could not get RID for component %s\n", txt);
1047		return -1;
1048	}
1049
1050	printk(KERN_INFO "%s: %s: id=0x%02x v%d.%d.%d\n", dev->name, txt,
1051	       __le16_to_cpu(comp.id), __le16_to_cpu(comp.major),
1052	       __le16_to_cpu(comp.minor), __le16_to_cpu(comp.variant));
1053	return 0;
1054}
1055
1056
1057static int prism2_setup_rids(struct net_device *dev)
1058{
1059	struct hostap_interface *iface;
1060	local_info_t *local;
1061	__le16 tmp;
1062	int ret = 0;
1063
1064	iface = netdev_priv(dev);
1065	local = iface->local;
1066
1067	hostap_set_word(dev, HFA384X_RID_TICKTIME, 2000);
1068
1069	if (!local->fw_ap) {
1070		u16 tmp1 = hostap_get_porttype(local);
1071		ret = hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE, tmp1);
1072		if (ret) {
1073			printk("%s: Port type setting to %d failed\n",
1074			       dev->name, tmp1);
1075			goto fail;
1076		}
1077	}
1078
1079	/* Setting SSID to empty string seems to kill the card in Host AP mode
1080	 */
1081	if (local->iw_mode != IW_MODE_MASTER || local->essid[0] != '\0') {
1082		ret = hostap_set_string(dev, HFA384X_RID_CNFOWNSSID,
1083					local->essid);
1084		if (ret) {
1085			printk("%s: AP own SSID setting failed\n", dev->name);
1086			goto fail;
1087		}
1088	}
1089
1090	ret = hostap_set_word(dev, HFA384X_RID_CNFMAXDATALEN,
1091			      PRISM2_DATA_MAXLEN);
1092	if (ret) {
1093		printk("%s: MAC data length setting to %d failed\n",
1094		       dev->name, PRISM2_DATA_MAXLEN);
1095		goto fail;
1096	}
1097
1098	if (hfa384x_get_rid(dev, HFA384X_RID_CHANNELLIST, &tmp, 2, 1) < 0) {
1099		printk("%s: Channel list read failed\n", dev->name);
1100		ret = -EINVAL;
1101		goto fail;
1102	}
1103	local->channel_mask = le16_to_cpu(tmp);
1104
1105	if (local->channel < 1 || local->channel > 14 ||
1106	    !(local->channel_mask & (1 << (local->channel - 1)))) {
1107		printk(KERN_WARNING "%s: Channel setting out of range "
1108		       "(%d)!\n", dev->name, local->channel);
1109		ret = -EBUSY;
1110		goto fail;
1111	}
1112
1113	ret = hostap_set_word(dev, HFA384X_RID_CNFOWNCHANNEL, local->channel);
1114	if (ret) {
1115		printk("%s: Channel setting to %d failed\n",
1116		       dev->name, local->channel);
1117		goto fail;
1118	}
1119
1120	ret = hostap_set_word(dev, HFA384X_RID_CNFBEACONINT,
1121			      local->beacon_int);
1122	if (ret) {
1123		printk("%s: Beacon interval setting to %d failed\n",
1124		       dev->name, local->beacon_int);
1125		/* this may fail with Symbol/Lucent firmware */
1126		if (ret == -ETIMEDOUT)
1127			goto fail;
1128	}
1129
1130	ret = hostap_set_word(dev, HFA384X_RID_CNFOWNDTIMPERIOD,
1131			      local->dtim_period);
1132	if (ret) {
1133		printk("%s: DTIM period setting to %d failed\n",
1134		       dev->name, local->dtim_period);
1135		/* this may fail with Symbol/Lucent firmware */
1136		if (ret == -ETIMEDOUT)
1137			goto fail;
1138	}
1139
1140	ret = hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
1141			      local->is_promisc);
1142	if (ret)
1143		printk(KERN_INFO "%s: Setting promiscuous mode (%d) failed\n",
1144		       dev->name, local->is_promisc);
1145
1146	if (!local->fw_ap) {
1147		ret = hostap_set_string(dev, HFA384X_RID_CNFDESIREDSSID,
1148					local->essid);
1149		if (ret) {
1150			printk("%s: Desired SSID setting failed\n", dev->name);
1151			goto fail;
1152		}
1153	}
1154
1155	/* Setup TXRateControl, defaults to allow use of 1, 2, 5.5, and
1156	 * 11 Mbps in automatic TX rate fallback and 1 and 2 Mbps as basic
1157	 * rates */
1158	if (local->tx_rate_control == 0) {
1159		local->tx_rate_control =
1160			HFA384X_RATES_1MBPS |
1161			HFA384X_RATES_2MBPS |
1162			HFA384X_RATES_5MBPS |
1163			HFA384X_RATES_11MBPS;
1164	}
1165	if (local->basic_rates == 0)
1166		local->basic_rates = HFA384X_RATES_1MBPS | HFA384X_RATES_2MBPS;
1167
1168	if (!local->fw_ap) {
1169		ret = hostap_set_word(dev, HFA384X_RID_TXRATECONTROL,
1170				      local->tx_rate_control);
1171		if (ret) {
1172			printk("%s: TXRateControl setting to %d failed\n",
1173			       dev->name, local->tx_rate_control);
1174			goto fail;
1175		}
1176
1177		ret = hostap_set_word(dev, HFA384X_RID_CNFSUPPORTEDRATES,
1178				      local->tx_rate_control);
1179		if (ret) {
1180			printk("%s: cnfSupportedRates setting to %d failed\n",
1181			       dev->name, local->tx_rate_control);
1182		}
1183
1184		ret = hostap_set_word(dev, HFA384X_RID_CNFBASICRATES,
1185				      local->basic_rates);
1186		if (ret) {
1187			printk("%s: cnfBasicRates setting to %d failed\n",
1188			       dev->name, local->basic_rates);
1189		}
1190
1191		ret = hostap_set_word(dev, HFA384X_RID_CREATEIBSS, 1);
1192		if (ret) {
1193			printk("%s: Create IBSS setting to 1 failed\n",
1194			       dev->name);
1195		}
1196	}
1197
1198	if (local->name_set)
1199		(void) hostap_set_string(dev, HFA384X_RID_CNFOWNNAME,
1200					 local->name);
1201
1202	if (hostap_set_encryption(local)) {
1203		printk(KERN_INFO "%s: could not configure encryption\n",
1204		       dev->name);
1205	}
1206
1207	(void) hostap_set_antsel(local);
1208
1209	if (hostap_set_roaming(local)) {
1210		printk(KERN_INFO "%s: could not set host roaming\n",
1211		       dev->name);
1212	}
1213
1214	if (local->sta_fw_ver >= PRISM2_FW_VER(1,6,3) &&
1215	    hostap_set_word(dev, HFA384X_RID_CNFENHSECURITY, local->enh_sec))
1216		printk(KERN_INFO "%s: cnfEnhSecurity setting to 0x%x failed\n",
1217		       dev->name, local->enh_sec);
1218
1219	/* 32-bit tallies were added in STA f/w 0.8.0, but they were apparently
1220	 * not working correctly (last seven counters report bogus values).
1221	 * This has been fixed in 0.8.2, so enable 32-bit tallies only
1222	 * beginning with that firmware version. Another bug fix for 32-bit
1223	 * tallies in 1.4.0; should 16-bit tallies be used for some other
1224	 * versions, too? */
1225	if (local->sta_fw_ver >= PRISM2_FW_VER(0,8,2)) {
1226		if (hostap_set_word(dev, HFA384X_RID_CNFTHIRTY2TALLY, 1)) {
1227			printk(KERN_INFO "%s: cnfThirty2Tally setting "
1228			       "failed\n", dev->name);
1229			local->tallies32 = 0;
1230		} else
1231			local->tallies32 = 1;
1232	} else
1233		local->tallies32 = 0;
1234
1235	hostap_set_auth_algs(local);
1236
1237	if (hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
1238			    local->fragm_threshold)) {
1239		printk(KERN_INFO "%s: setting FragmentationThreshold to %d "
1240		       "failed\n", dev->name, local->fragm_threshold);
1241	}
1242
1243	if (hostap_set_word(dev, HFA384X_RID_RTSTHRESHOLD,
1244			    local->rts_threshold)) {
1245		printk(KERN_INFO "%s: setting RTSThreshold to %d failed\n",
1246		       dev->name, local->rts_threshold);
1247	}
1248
1249	if (local->manual_retry_count >= 0 &&
1250	    hostap_set_word(dev, HFA384X_RID_CNFALTRETRYCOUNT,
1251			    local->manual_retry_count)) {
1252		printk(KERN_INFO "%s: setting cnfAltRetryCount to %d failed\n",
1253		       dev->name, local->manual_retry_count);
1254	}
1255
1256	if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1) &&
1257	    hfa384x_get_rid(dev, HFA384X_RID_CNFDBMADJUST, &tmp, 2, 1) == 2) {
1258		local->rssi_to_dBm = le16_to_cpu(tmp);
1259	}
1260
1261	if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->wpa &&
1262	    hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE, 1)) {
1263		printk(KERN_INFO "%s: setting ssnHandlingMode to 1 failed\n",
1264		       dev->name);
1265	}
1266
1267	if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->generic_elem &&
1268	    hfa384x_set_rid(dev, HFA384X_RID_GENERICELEMENT,
1269			    local->generic_elem, local->generic_elem_len)) {
1270		printk(KERN_INFO "%s: setting genericElement failed\n",
1271		       dev->name);
1272	}
1273
1274 fail:
1275	return ret;
1276}
1277
1278
1279static int prism2_hw_init(struct net_device *dev, int initial)
1280{
1281	struct hostap_interface *iface;
1282	local_info_t *local;
1283	int ret, first = 1;
1284	unsigned long start, delay;
1285
1286	PDEBUG(DEBUG_FLOW, "prism2_hw_init()\n");
1287
1288	iface = netdev_priv(dev);
1289	local = iface->local;
1290
1291	clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits);
1292
1293 init:
1294	/* initialize HFA 384x */
1295	ret = hfa384x_cmd_no_wait(dev, HFA384X_CMDCODE_INIT, 0);
1296	if (ret) {
1297		printk(KERN_INFO "%s: first command failed - assuming card "
1298		       "does not have primary firmware\n", dev_info);
1299	}
1300
1301	if (first && (HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
1302		/* EvStat has Cmd bit set in some cases, so retry once if no
1303		 * wait was needed */
1304		HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
1305		printk(KERN_DEBUG "%s: init command completed too quickly - "
1306		       "retrying\n", dev->name);
1307		first = 0;
1308		goto init;
1309	}
1310
1311	start = jiffies;
1312	delay = jiffies + HFA384X_INIT_TIMEOUT;
1313	while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
1314	       time_before(jiffies, delay))
1315		yield();
1316	if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
1317		printk(KERN_DEBUG "%s: assuming no Primary image in "
1318		       "flash - card initialization not completed\n",
1319		       dev_info);
1320		local->no_pri = 1;
1321#ifdef PRISM2_DOWNLOAD_SUPPORT
1322			if (local->sram_type == -1)
1323				local->sram_type = prism2_get_ram_size(local);
1324#endif /* PRISM2_DOWNLOAD_SUPPORT */
1325		return 1;
1326	}
1327	local->no_pri = 0;
1328	printk(KERN_DEBUG "prism2_hw_init: initialized in %lu ms\n",
1329	       (jiffies - start) * 1000 / HZ);
1330	HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
1331	return 0;
1332}
1333
1334
1335static int prism2_hw_init2(struct net_device *dev, int initial)
1336{
1337	struct hostap_interface *iface;
1338	local_info_t *local;
1339	int i;
1340
1341	iface = netdev_priv(dev);
1342	local = iface->local;
1343
1344#ifdef PRISM2_DOWNLOAD_SUPPORT
1345	kfree(local->pda);
1346	if (local->no_pri)
1347		local->pda = NULL;
1348	else
1349		local->pda = prism2_read_pda(dev);
1350#endif /* PRISM2_DOWNLOAD_SUPPORT */
1351
1352	hfa384x_disable_interrupts(dev);
1353
1354#ifndef final_version
1355	HFA384X_OUTW(HFA384X_MAGIC, HFA384X_SWSUPPORT0_OFF);
1356	if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
1357		printk("SWSUPPORT0 write/read failed: %04X != %04X\n",
1358		       HFA384X_INW(HFA384X_SWSUPPORT0_OFF), HFA384X_MAGIC);
1359		goto failed;
1360	}
1361#endif
1362
1363	if (initial || local->pri_only) {
1364		hfa384x_events_only_cmd(dev);
1365		/* get card version information */
1366		if (prism2_get_version_info(dev, HFA384X_RID_NICID, "NIC") ||
1367		    prism2_get_version_info(dev, HFA384X_RID_PRIID, "PRI")) {
1368			hfa384x_disable_interrupts(dev);
1369			goto failed;
1370		}
1371
1372		if (prism2_get_version_info(dev, HFA384X_RID_STAID, "STA")) {
1373			printk(KERN_DEBUG "%s: Failed to read STA f/w version "
1374			       "- only Primary f/w present\n", dev->name);
1375			local->pri_only = 1;
1376			return 0;
1377		}
1378		local->pri_only = 0;
1379		hfa384x_disable_interrupts(dev);
1380	}
1381
1382	/* FIX: could convert allocate_fid to use sleeping CmdCompl wait and
1383	 * enable interrupts before this. This would also require some sort of
1384	 * sleeping AllocEv waiting */
1385
1386	/* allocate TX FIDs */
1387	local->txfid_len = PRISM2_TXFID_LEN;
1388	for (i = 0; i < PRISM2_TXFID_COUNT; i++) {
1389		local->txfid[i] = hfa384x_allocate_fid(dev, local->txfid_len);
1390		if (local->txfid[i] == 0xffff && local->txfid_len > 1600) {
1391			local->txfid[i] = hfa384x_allocate_fid(dev, 1600);
1392			if (local->txfid[i] != 0xffff) {
1393				printk(KERN_DEBUG "%s: Using shorter TX FID "
1394				       "(1600 bytes)\n", dev->name);
1395				local->txfid_len = 1600;
1396			}
1397		}
1398		if (local->txfid[i] == 0xffff)
1399			goto failed;
1400		local->intransmitfid[i] = PRISM2_TXFID_EMPTY;
1401	}
1402
1403	hfa384x_events_only_cmd(dev);
1404
1405	if (initial) {
1406		u8 addr[ETH_ALEN] = {};
1407		struct list_head *ptr;
1408
1409		prism2_check_sta_fw_version(local);
1410
1411		if (hfa384x_get_rid(dev, HFA384X_RID_CNFOWNMACADDR,
1412				    addr, ETH_ALEN, 1) < 0) {
1413			printk("%s: could not get own MAC address\n",
1414			       dev->name);
1415		}
1416		eth_hw_addr_set(dev, addr);
1417		list_for_each(ptr, &local->hostap_interfaces) {
1418			iface = list_entry(ptr, struct hostap_interface, list);
1419			eth_hw_addr_inherit(iface->dev, dev);
1420		}
1421	} else if (local->fw_ap)
1422		prism2_check_sta_fw_version(local);
1423
1424	prism2_setup_rids(dev);
1425
1426	/* MAC is now configured, but port 0 is not yet enabled */
1427	return 0;
1428
1429 failed:
1430	if (!local->no_pri)
1431		printk(KERN_WARNING "%s: Initialization failed\n", dev_info);
1432	return 1;
1433}
1434
1435
1436static int prism2_hw_enable(struct net_device *dev, int initial)
1437{
1438	struct hostap_interface *iface;
1439	local_info_t *local;
1440	int was_resetting;
1441
1442	iface = netdev_priv(dev);
1443	local = iface->local;
1444	was_resetting = local->hw_resetting;
1445
1446	if (hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0, NULL, NULL)) {
1447		printk("%s: MAC port 0 enabling failed\n", dev->name);
1448		return 1;
1449	}
1450
1451	local->hw_ready = 1;
1452	local->hw_reset_tries = 0;
1453	local->hw_resetting = 0;
1454	hfa384x_enable_interrupts(dev);
1455
1456	/* at least D-Link DWL-650 seems to require additional port reset
1457	 * before it starts acting as an AP, so reset port automatically
1458	 * here just in case */
1459	if (initial && prism2_reset_port(dev)) {
1460		printk("%s: MAC port 0 resetting failed\n", dev->name);
1461		return 1;
1462	}
1463
1464	if (was_resetting && netif_queue_stopped(dev)) {
1465		/* If hw_reset() was called during pending transmit, netif
1466		 * queue was stopped. Wake it up now since the wlan card has
1467		 * been resetted. */
1468		netif_wake_queue(dev);
1469	}
1470
1471	return 0;
1472}
1473
1474
1475static int prism2_hw_config(struct net_device *dev, int initial)
1476{
1477	struct hostap_interface *iface;
1478	local_info_t *local;
1479
1480	iface = netdev_priv(dev);
1481	local = iface->local;
1482
1483	if (local->hw_downloading)
1484		return 1;
1485
1486	if (prism2_hw_init(dev, initial)) {
1487		return local->no_pri ? 0 : 1;
1488	}
1489
1490	if (prism2_hw_init2(dev, initial))
1491		return 1;
1492
1493	/* Enable firmware if secondary image is loaded and at least one of the
1494	 * netdevices is up. */
1495	if (!local->pri_only &&
1496	    (initial == 0 || (initial == 2 && local->num_dev_open > 0))) {
1497		if (!local->dev_enabled)
1498			prism2_callback(local, PRISM2_CALLBACK_ENABLE);
1499		local->dev_enabled = 1;
1500		return prism2_hw_enable(dev, initial);
1501	}
1502
1503	return 0;
1504}
1505
1506
1507static void prism2_hw_shutdown(struct net_device *dev, int no_disable)
1508{
1509	struct hostap_interface *iface;
1510	local_info_t *local;
1511
1512	iface = netdev_priv(dev);
1513	local = iface->local;
1514
1515	/* Allow only command completion events during disable */
1516	hfa384x_events_only_cmd(dev);
1517
1518	local->hw_ready = 0;
1519	if (local->dev_enabled)
1520		prism2_callback(local, PRISM2_CALLBACK_DISABLE);
1521	local->dev_enabled = 0;
1522
1523	if (local->func->card_present && !local->func->card_present(local)) {
1524		printk(KERN_DEBUG "%s: card already removed or not configured "
1525		       "during shutdown\n", dev->name);
1526		return;
1527	}
1528
1529	if ((no_disable & HOSTAP_HW_NO_DISABLE) == 0 &&
1530	    hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0, NULL, NULL))
1531		printk(KERN_WARNING "%s: Shutdown failed\n", dev_info);
1532
1533	hfa384x_disable_interrupts(dev);
1534
1535	if (no_disable & HOSTAP_HW_ENABLE_CMDCOMPL)
1536		hfa384x_events_only_cmd(dev);
1537	else
1538		prism2_clear_cmd_queue(local);
1539}
1540
1541
1542static void prism2_hw_reset(struct net_device *dev)
1543{
1544	struct hostap_interface *iface;
1545	local_info_t *local;
1546
1547#if 0
1548	static long last_reset = 0;
1549
1550	/* do not reset card more than once per second to avoid ending up in a
1551	 * busy loop resetting the card */
1552	if (time_before_eq(jiffies, last_reset + HZ))
1553		return;
1554	last_reset = jiffies;
1555#endif
1556
1557	iface = netdev_priv(dev);
1558	local = iface->local;
1559
 
 
 
 
 
 
1560	if (local->hw_downloading)
1561		return;
1562
1563	if (local->hw_resetting) {
1564		printk(KERN_WARNING "%s: %s: already resetting card - "
1565		       "ignoring reset request\n", dev_info, dev->name);
1566		return;
1567	}
1568
1569	local->hw_reset_tries++;
1570	if (local->hw_reset_tries > 10) {
1571		printk(KERN_WARNING "%s: too many reset tries, skipping\n",
1572		       dev->name);
1573		return;
1574	}
1575
1576	printk(KERN_WARNING "%s: %s: resetting card\n", dev_info, dev->name);
1577	hfa384x_disable_interrupts(dev);
1578	local->hw_resetting = 1;
1579	if (local->func->cor_sreset) {
1580		/* Host system seems to hang in some cases with high traffic
1581		 * load or shared interrupts during COR sreset. Disable shared
1582		 * interrupts during reset to avoid these crashes. COS sreset
1583		 * takes quite a long time, so it is unfortunate that this
1584		 * seems to be needed. Anyway, I do not know of any better way
1585		 * of avoiding the crash. */
1586		disable_irq(dev->irq);
1587		local->func->cor_sreset(local);
1588		enable_irq(dev->irq);
1589	}
1590	prism2_hw_shutdown(dev, 1);
1591	prism2_hw_config(dev, 0);
1592	local->hw_resetting = 0;
1593
1594#ifdef PRISM2_DOWNLOAD_SUPPORT
1595	if (local->dl_pri) {
1596		printk(KERN_DEBUG "%s: persistent download of primary "
1597		       "firmware\n", dev->name);
1598		if (prism2_download_genesis(local, local->dl_pri) < 0)
1599			printk(KERN_WARNING "%s: download (PRI) failed\n",
1600			       dev->name);
1601	}
1602
1603	if (local->dl_sec) {
1604		printk(KERN_DEBUG "%s: persistent download of secondary "
1605		       "firmware\n", dev->name);
1606		if (prism2_download_volatile(local, local->dl_sec) < 0)
1607			printk(KERN_WARNING "%s: download (SEC) failed\n",
1608			       dev->name);
1609	}
1610#endif /* PRISM2_DOWNLOAD_SUPPORT */
1611
1612	/* TODO: restore beacon TIM bits for STAs that have buffered frames */
1613}
1614
1615
1616static void prism2_schedule_reset(local_info_t *local)
1617{
1618	schedule_work(&local->reset_queue);
1619}
1620
1621
1622/* Called only as scheduled task after noticing card timeout in interrupt
1623 * context */
1624static void handle_reset_queue(struct work_struct *work)
1625{
1626	local_info_t *local = container_of(work, local_info_t, reset_queue);
1627
1628	printk(KERN_DEBUG "%s: scheduled card reset\n", local->dev->name);
1629	prism2_hw_reset(local->dev);
1630
1631	if (netif_queue_stopped(local->dev)) {
1632		int i;
1633
1634		for (i = 0; i < PRISM2_TXFID_COUNT; i++)
1635			if (local->intransmitfid[i] == PRISM2_TXFID_EMPTY) {
1636				PDEBUG(DEBUG_EXTRA, "prism2_tx_timeout: "
1637				       "wake up queue\n");
1638				netif_wake_queue(local->dev);
1639				break;
1640			}
1641	}
1642}
1643
1644
1645static int prism2_get_txfid_idx(local_info_t *local)
1646{
1647	int idx, end;
1648	unsigned long flags;
1649
1650	spin_lock_irqsave(&local->txfidlock, flags);
1651	end = idx = local->next_txfid;
1652	do {
1653		if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
1654			local->intransmitfid[idx] = PRISM2_TXFID_RESERVED;
1655			spin_unlock_irqrestore(&local->txfidlock, flags);
1656			return idx;
1657		}
1658		idx++;
1659		if (idx >= PRISM2_TXFID_COUNT)
1660			idx = 0;
1661	} while (idx != end);
1662	spin_unlock_irqrestore(&local->txfidlock, flags);
1663
1664	PDEBUG(DEBUG_EXTRA2, "prism2_get_txfid_idx: no room in txfid buf: "
1665	       "packet dropped\n");
1666	local->dev->stats.tx_dropped++;
1667
1668	return -1;
1669}
1670
1671
1672/* Called only from hardware IRQ */
1673static void prism2_transmit_cb(struct net_device *dev, long context,
1674			       u16 resp0, u16 res)
1675{
1676	struct hostap_interface *iface;
1677	local_info_t *local;
1678	int idx = (int) context;
1679
1680	iface = netdev_priv(dev);
1681	local = iface->local;
1682
1683	if (res) {
1684		printk(KERN_DEBUG "%s: prism2_transmit_cb - res=0x%02x\n",
1685		       dev->name, res);
1686		return;
1687	}
1688
1689	if (idx < 0 || idx >= PRISM2_TXFID_COUNT) {
1690		printk(KERN_DEBUG "%s: prism2_transmit_cb called with invalid "
1691		       "idx=%d\n", dev->name, idx);
1692		return;
1693	}
1694
1695	if (!test_and_clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
1696		printk(KERN_DEBUG "%s: driver bug: prism2_transmit_cb called "
1697		       "with no pending transmit\n", dev->name);
1698	}
1699
1700	if (netif_queue_stopped(dev)) {
1701		/* ready for next TX, so wake up queue that was stopped in
1702		 * prism2_transmit() */
1703		netif_wake_queue(dev);
1704	}
1705
1706	spin_lock(&local->txfidlock);
1707
1708	/* With reclaim, Resp0 contains new txfid for transmit; the old txfid
1709	 * will be automatically allocated for the next TX frame */
1710	local->intransmitfid[idx] = resp0;
1711
1712	PDEBUG(DEBUG_FID, "%s: prism2_transmit_cb: txfid[%d]=0x%04x, "
1713	       "resp0=0x%04x, transmit_txfid=0x%04x\n",
1714	       dev->name, idx, local->txfid[idx],
1715	       resp0, local->intransmitfid[local->next_txfid]);
1716
1717	idx++;
1718	if (idx >= PRISM2_TXFID_COUNT)
1719		idx = 0;
1720	local->next_txfid = idx;
1721
1722	/* check if all TX buffers are occupied */
1723	do {
1724		if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
1725			spin_unlock(&local->txfidlock);
1726			return;
1727		}
1728		idx++;
1729		if (idx >= PRISM2_TXFID_COUNT)
1730			idx = 0;
1731	} while (idx != local->next_txfid);
1732	spin_unlock(&local->txfidlock);
1733
1734	/* no empty TX buffers, stop queue */
1735	netif_stop_queue(dev);
1736}
1737
1738
1739/* Called only from software IRQ if PCI bus master is not used (with bus master
1740 * this can be called both from software and hardware IRQ) */
1741static int prism2_transmit(struct net_device *dev, int idx)
1742{
1743	struct hostap_interface *iface;
1744	local_info_t *local;
1745	int res;
1746
1747	iface = netdev_priv(dev);
1748	local = iface->local;
1749
1750	/* The driver tries to stop netif queue so that there would not be
1751	 * more than one attempt to transmit frames going on; check that this
1752	 * is really the case */
1753
1754	if (test_and_set_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
1755		printk(KERN_DEBUG "%s: driver bug - prism2_transmit() called "
1756		       "when previous TX was pending\n", dev->name);
1757		return -1;
1758	}
1759
1760	/* stop the queue for the time that transmit is pending */
1761	netif_stop_queue(dev);
1762
1763	/* transmit packet */
1764	res = hfa384x_cmd_callback(
1765		dev,
1766		HFA384X_CMDCODE_TRANSMIT | HFA384X_CMD_TX_RECLAIM,
1767		local->txfid[idx],
1768		prism2_transmit_cb, (long) idx);
1769
1770	if (res) {
1771		printk(KERN_DEBUG "%s: prism2_transmit: CMDCODE_TRANSMIT "
1772		       "failed (res=%d)\n", dev->name, res);
1773		dev->stats.tx_dropped++;
1774		netif_wake_queue(dev);
1775		return -1;
1776	}
1777	netif_trans_update(dev);
1778
1779	/* Since we did not wait for command completion, the card continues
1780	 * to process on the background and we will finish handling when
1781	 * command completion event is handled (prism2_cmd_ev() function) */
1782
1783	return 0;
1784}
1785
1786
1787/* Send IEEE 802.11 frame (convert the header into Prism2 TX descriptor and
1788 * send the payload with this descriptor) */
1789/* Called only from software IRQ */
1790static int prism2_tx_80211(struct sk_buff *skb, struct net_device *dev)
1791{
1792	struct hostap_interface *iface;
1793	local_info_t *local;
1794	struct hfa384x_tx_frame txdesc;
1795	struct hostap_skb_tx_data *meta;
1796	int hdr_len, data_len, idx, res, ret = -1;
1797	u16 tx_control;
1798
1799	iface = netdev_priv(dev);
1800	local = iface->local;
1801
1802	meta = (struct hostap_skb_tx_data *) skb->cb;
1803
1804	prism2_callback(local, PRISM2_CALLBACK_TX_START);
1805
1806	if ((local->func->card_present && !local->func->card_present(local)) ||
1807	    !local->hw_ready || local->hw_downloading || local->pri_only) {
1808		if (net_ratelimit()) {
1809			printk(KERN_DEBUG "%s: prism2_tx_80211: hw not ready -"
1810			       " skipping\n", dev->name);
1811		}
1812		goto fail;
1813	}
1814
1815	memset(&txdesc, 0, sizeof(txdesc));
1816
1817	/* skb->data starts with txdesc->frame_control */
1818	hdr_len = sizeof(txdesc.header);
1819	BUILD_BUG_ON(hdr_len != 24);
1820	skb_copy_from_linear_data(skb, &txdesc.header, hdr_len);
1821	if (ieee80211_is_data(txdesc.frame_control) &&
1822	    ieee80211_has_a4(txdesc.frame_control) &&
1823	    skb->len >= 30) {
1824		/* Addr4 */
1825		skb_copy_from_linear_data_offset(skb, hdr_len, txdesc.addr4,
1826						 ETH_ALEN);
1827		hdr_len += ETH_ALEN;
1828	}
1829
1830	tx_control = local->tx_control;
1831	if (meta->tx_cb_idx) {
1832		tx_control |= HFA384X_TX_CTRL_TX_OK;
1833		txdesc.sw_support = cpu_to_le32(meta->tx_cb_idx);
1834	}
1835	txdesc.tx_control = cpu_to_le16(tx_control);
1836	txdesc.tx_rate = meta->rate;
1837
1838	data_len = skb->len - hdr_len;
1839	txdesc.data_len = cpu_to_le16(data_len);
1840	txdesc.len = cpu_to_be16(data_len);
1841
1842	idx = prism2_get_txfid_idx(local);
1843	if (idx < 0)
1844		goto fail;
1845
1846	if (local->frame_dump & PRISM2_DUMP_TX_HDR)
1847		hostap_dump_tx_header(dev->name, &txdesc);
1848
1849	spin_lock(&local->baplock);
1850	res = hfa384x_setup_bap(dev, BAP0, local->txfid[idx], 0);
1851
1852	if (!res)
1853		res = hfa384x_to_bap(dev, BAP0, &txdesc, sizeof(txdesc));
1854	if (!res)
1855		res = hfa384x_to_bap(dev, BAP0, skb->data + hdr_len,
1856				     skb->len - hdr_len);
1857	spin_unlock(&local->baplock);
1858
1859	if (!res)
1860		res = prism2_transmit(dev, idx);
1861	if (res) {
1862		printk(KERN_DEBUG "%s: prism2_tx_80211 - to BAP0 failed\n",
1863		       dev->name);
1864		local->intransmitfid[idx] = PRISM2_TXFID_EMPTY;
1865		schedule_work(&local->reset_queue);
1866		goto fail;
1867	}
1868
1869	ret = 0;
1870
1871fail:
1872	prism2_callback(local, PRISM2_CALLBACK_TX_END);
1873	return ret;
1874}
1875
1876
1877/* Some SMP systems have reported number of odd errors with hostap_pci. fid
1878 * register has changed values between consecutive reads for an unknown reason.
1879 * This should really not happen, so more debugging is needed. This test
1880 * version is a bit slower, but it will detect most of such register changes
1881 * and will try to get the correct fid eventually. */
1882#define EXTRA_FID_READ_TESTS
1883
1884static u16 prism2_read_fid_reg(struct net_device *dev, u16 reg)
1885{
1886#ifdef EXTRA_FID_READ_TESTS
1887	u16 val, val2, val3;
1888	int i;
1889
1890	for (i = 0; i < 10; i++) {
1891		val = HFA384X_INW(reg);
1892		val2 = HFA384X_INW(reg);
1893		val3 = HFA384X_INW(reg);
1894
1895		if (val == val2 && val == val3)
1896			return val;
1897
1898		printk(KERN_DEBUG "%s: detected fid change (try=%d, reg=%04x):"
1899		       " %04x %04x %04x\n",
1900		       dev->name, i, reg, val, val2, val3);
1901		if ((val == val2 || val == val3) && val != 0)
1902			return val;
1903		if (val2 == val3 && val2 != 0)
1904			return val2;
1905	}
1906	printk(KERN_WARNING "%s: Uhhuh.. could not read good fid from reg "
1907	       "%04x (%04x %04x %04x)\n", dev->name, reg, val, val2, val3);
1908	return val;
1909#else /* EXTRA_FID_READ_TESTS */
1910	return HFA384X_INW(reg);
1911#endif /* EXTRA_FID_READ_TESTS */
1912}
1913
1914
1915/* Called only as a tasklet (software IRQ) */
1916static void prism2_rx(local_info_t *local)
1917{
1918	struct net_device *dev = local->dev;
1919	int res, rx_pending = 0;
1920	u16 len, hdr_len, rxfid, status, macport;
1921	struct hfa384x_rx_frame rxdesc;
1922	struct sk_buff *skb = NULL;
1923
1924	prism2_callback(local, PRISM2_CALLBACK_RX_START);
1925
1926	rxfid = prism2_read_fid_reg(dev, HFA384X_RXFID_OFF);
1927#ifndef final_version
1928	if (rxfid == 0) {
1929		rxfid = HFA384X_INW(HFA384X_RXFID_OFF);
1930		printk(KERN_DEBUG "prism2_rx: rxfid=0 (next 0x%04x)\n",
1931		       rxfid);
1932		if (rxfid == 0) {
1933			schedule_work(&local->reset_queue);
1934			goto rx_dropped;
1935		}
1936		/* try to continue with the new rxfid value */
1937	}
1938#endif
1939
1940	spin_lock(&local->baplock);
1941	res = hfa384x_setup_bap(dev, BAP0, rxfid, 0);
1942	if (!res)
1943		res = hfa384x_from_bap(dev, BAP0, &rxdesc, sizeof(rxdesc));
1944
1945	if (res) {
1946		spin_unlock(&local->baplock);
1947		printk(KERN_DEBUG "%s: copy from BAP0 failed %d\n", dev->name,
1948		       res);
1949		if (res == -ETIMEDOUT) {
1950			schedule_work(&local->reset_queue);
1951		}
1952		goto rx_dropped;
1953	}
1954
1955	len = le16_to_cpu(rxdesc.data_len);
1956	hdr_len = sizeof(rxdesc);
1957	status = le16_to_cpu(rxdesc.status);
1958	macport = (status >> 8) & 0x07;
1959
1960	/* Drop frames with too large reported payload length. Monitor mode
1961	 * seems to sometimes pass frames (e.g., ctrl::ack) with signed and
1962	 * negative value, so allow also values 65522 .. 65534 (-14 .. -2) for
1963	 * macport 7 */
1964	if (len > PRISM2_DATA_MAXLEN + 8 /* WEP */) {
1965		if (macport == 7 && local->iw_mode == IW_MODE_MONITOR) {
1966			if (len >= (u16) -14) {
1967				hdr_len -= 65535 - len;
1968				hdr_len--;
1969			}
1970			len = 0;
1971		} else {
1972			spin_unlock(&local->baplock);
1973			printk(KERN_DEBUG "%s: Received frame with invalid "
1974			       "length 0x%04x\n", dev->name, len);
1975			hostap_dump_rx_header(dev->name, &rxdesc);
1976			goto rx_dropped;
1977		}
1978	}
1979
1980	skb = dev_alloc_skb(len + hdr_len);
1981	if (!skb) {
1982		spin_unlock(&local->baplock);
1983		printk(KERN_DEBUG "%s: RX failed to allocate skb\n",
1984		       dev->name);
1985		goto rx_dropped;
1986	}
1987	skb->dev = dev;
1988	skb_put_data(skb, &rxdesc, hdr_len);
1989
1990	if (len > 0)
1991		res = hfa384x_from_bap(dev, BAP0, skb_put(skb, len), len);
1992	spin_unlock(&local->baplock);
1993	if (res) {
1994		printk(KERN_DEBUG "%s: RX failed to read "
1995		       "frame data\n", dev->name);
1996		goto rx_dropped;
1997	}
1998
1999	skb_queue_tail(&local->rx_list, skb);
2000	tasklet_schedule(&local->rx_tasklet);
2001
2002 rx_exit:
2003	prism2_callback(local, PRISM2_CALLBACK_RX_END);
2004	if (!rx_pending) {
2005		HFA384X_OUTW(HFA384X_EV_RX, HFA384X_EVACK_OFF);
2006	}
2007
2008	return;
2009
2010 rx_dropped:
2011	dev->stats.rx_dropped++;
2012	if (skb)
2013		dev_kfree_skb(skb);
2014	goto rx_exit;
2015}
2016
2017
2018/* Called only as a tasklet (software IRQ) */
2019static void hostap_rx_skb(local_info_t *local, struct sk_buff *skb)
2020{
2021	struct hfa384x_rx_frame *rxdesc;
2022	struct net_device *dev = skb->dev;
2023	struct hostap_80211_rx_status stats;
2024	int hdrlen, rx_hdrlen;
2025
2026	rx_hdrlen = sizeof(*rxdesc);
2027	if (skb->len < sizeof(*rxdesc)) {
2028		/* Allow monitor mode to receive shorter frames */
2029		if (local->iw_mode == IW_MODE_MONITOR &&
2030		    skb->len >= sizeof(*rxdesc) - 30) {
2031			rx_hdrlen = skb->len;
2032		} else {
2033			dev_kfree_skb(skb);
2034			return;
2035		}
2036	}
2037
2038	rxdesc = (struct hfa384x_rx_frame *) skb->data;
2039
2040	if (local->frame_dump & PRISM2_DUMP_RX_HDR &&
2041	    skb->len >= sizeof(*rxdesc))
2042		hostap_dump_rx_header(dev->name, rxdesc);
2043
2044	if (le16_to_cpu(rxdesc->status) & HFA384X_RX_STATUS_FCSERR &&
2045	    (!local->monitor_allow_fcserr ||
2046	     local->iw_mode != IW_MODE_MONITOR))
2047		goto drop;
2048
2049	if (skb->len > PRISM2_DATA_MAXLEN) {
2050		printk(KERN_DEBUG "%s: RX: len(%d) > MAX(%d)\n",
2051		       dev->name, skb->len, PRISM2_DATA_MAXLEN);
2052		goto drop;
2053	}
2054
2055	stats.mac_time = le32_to_cpu(rxdesc->time);
2056	stats.signal = rxdesc->signal - local->rssi_to_dBm;
2057	stats.noise = rxdesc->silence - local->rssi_to_dBm;
2058	stats.rate = rxdesc->rate;
2059
2060	/* Convert Prism2 RX structure into IEEE 802.11 header */
2061	hdrlen = hostap_80211_get_hdrlen(rxdesc->frame_control);
2062	if (hdrlen > rx_hdrlen)
2063		hdrlen = rx_hdrlen;
2064
2065	memmove(skb_pull(skb, rx_hdrlen - hdrlen),
2066		&rxdesc->frame_control, hdrlen);
2067
2068	hostap_80211_rx(dev, skb, &stats);
2069	return;
2070
2071 drop:
2072	dev_kfree_skb(skb);
2073}
2074
2075
2076/* Called only as a tasklet (software IRQ) */
2077static void hostap_rx_tasklet(struct tasklet_struct *t)
2078{
2079	local_info_t *local = from_tasklet(local, t, rx_tasklet);
2080	struct sk_buff *skb;
2081
2082	while ((skb = skb_dequeue(&local->rx_list)) != NULL)
2083		hostap_rx_skb(local, skb);
2084}
2085
2086
2087/* Called only from hardware IRQ */
2088static void prism2_alloc_ev(struct net_device *dev)
2089{
2090	struct hostap_interface *iface;
2091	local_info_t *local;
2092	int idx;
2093	u16 fid;
2094
2095	iface = netdev_priv(dev);
2096	local = iface->local;
2097
2098	fid = prism2_read_fid_reg(dev, HFA384X_ALLOCFID_OFF);
2099
2100	PDEBUG(DEBUG_FID, "FID: interrupt: ALLOC - fid=0x%04x\n", fid);
2101
2102	spin_lock(&local->txfidlock);
2103	idx = local->next_alloc;
2104
2105	do {
2106		if (local->txfid[idx] == fid) {
2107			PDEBUG(DEBUG_FID, "FID: found matching txfid[%d]\n",
2108			       idx);
2109
2110#ifndef final_version
2111			if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY)
2112				printk("Already released txfid found at idx "
2113				       "%d\n", idx);
2114			if (local->intransmitfid[idx] == PRISM2_TXFID_RESERVED)
2115				printk("Already reserved txfid found at idx "
2116				       "%d\n", idx);
2117#endif
2118			local->intransmitfid[idx] = PRISM2_TXFID_EMPTY;
2119			idx++;
2120			local->next_alloc = idx >= PRISM2_TXFID_COUNT ? 0 :
2121				idx;
2122
2123			if (!test_bit(HOSTAP_BITS_TRANSMIT, &local->bits) &&
2124			    netif_queue_stopped(dev))
2125				netif_wake_queue(dev);
2126
2127			spin_unlock(&local->txfidlock);
2128			return;
2129		}
2130
2131		idx++;
2132		if (idx >= PRISM2_TXFID_COUNT)
2133			idx = 0;
2134	} while (idx != local->next_alloc);
2135
2136	printk(KERN_WARNING "%s: could not find matching txfid (0x%04x, new "
2137	       "read 0x%04x) for alloc event\n", dev->name, fid,
2138	       HFA384X_INW(HFA384X_ALLOCFID_OFF));
2139	printk(KERN_DEBUG "TXFIDs:");
2140	for (idx = 0; idx < PRISM2_TXFID_COUNT; idx++)
2141		printk(" %04x[%04x]", local->txfid[idx],
2142		       local->intransmitfid[idx]);
2143	printk("\n");
2144	spin_unlock(&local->txfidlock);
2145
2146	/* FIX: should probably schedule reset; reference to one txfid was lost
2147	 * completely.. Bad things will happen if we run out of txfids
2148	 * Actually, this will cause netdev watchdog to notice TX timeout and
2149	 * then card reset after all txfids have been leaked. */
2150}
2151
2152
2153/* Called only as a tasklet (software IRQ) */
2154static void hostap_tx_callback(local_info_t *local,
2155			       struct hfa384x_tx_frame *txdesc, int ok,
2156			       char *payload)
2157{
2158	u16 sw_support, hdrlen, len;
2159	struct sk_buff *skb;
2160	struct hostap_tx_callback_info *cb;
2161
2162	/* Make sure that frame was from us. */
2163	if (!ether_addr_equal(txdesc->addr2, local->dev->dev_addr)) {
2164		printk(KERN_DEBUG "%s: TX callback - foreign frame\n",
2165		       local->dev->name);
2166		return;
2167	}
2168
2169	sw_support = le32_to_cpu(txdesc->sw_support);
2170
2171	spin_lock(&local->lock);
2172	cb = local->tx_callback;
2173	while (cb != NULL && cb->idx != sw_support)
2174		cb = cb->next;
2175	spin_unlock(&local->lock);
2176
2177	if (cb == NULL) {
2178		printk(KERN_DEBUG "%s: could not find TX callback (idx %d)\n",
2179		       local->dev->name, sw_support);
2180		return;
2181	}
2182
2183	hdrlen = hostap_80211_get_hdrlen(txdesc->frame_control);
2184	len = le16_to_cpu(txdesc->data_len);
2185	skb = dev_alloc_skb(hdrlen + len);
2186	if (skb == NULL) {
2187		printk(KERN_DEBUG "%s: hostap_tx_callback failed to allocate "
2188		       "skb\n", local->dev->name);
2189		return;
2190	}
2191
2192	skb_put_data(skb, (void *)&txdesc->frame_control, hdrlen);
2193	if (payload)
2194		skb_put_data(skb, payload, len);
2195
2196	skb->dev = local->dev;
2197	skb_reset_mac_header(skb);
2198
2199	cb->func(skb, ok, cb->data);
2200}
2201
2202
2203/* Called only as a tasklet (software IRQ) */
2204static int hostap_tx_compl_read(local_info_t *local, int error,
2205				struct hfa384x_tx_frame *txdesc,
2206				char **payload)
2207{
2208	u16 fid, len;
2209	int res, ret = 0;
2210	struct net_device *dev = local->dev;
2211
2212	fid = prism2_read_fid_reg(dev, HFA384X_TXCOMPLFID_OFF);
2213
2214	PDEBUG(DEBUG_FID, "interrupt: TX (err=%d) - fid=0x%04x\n", fid, error);
2215
2216	spin_lock(&local->baplock);
2217	res = hfa384x_setup_bap(dev, BAP0, fid, 0);
2218	if (!res)
2219		res = hfa384x_from_bap(dev, BAP0, txdesc, sizeof(*txdesc));
2220	if (res) {
2221		PDEBUG(DEBUG_EXTRA, "%s: TX (err=%d) - fid=0x%04x - could not "
2222		       "read txdesc\n", dev->name, error, fid);
2223		if (res == -ETIMEDOUT) {
2224			schedule_work(&local->reset_queue);
2225		}
2226		ret = -1;
2227		goto fail;
2228	}
2229	if (txdesc->sw_support) {
2230		len = le16_to_cpu(txdesc->data_len);
2231		if (len < PRISM2_DATA_MAXLEN) {
2232			*payload = kmalloc(len, GFP_ATOMIC);
2233			if (*payload == NULL ||
2234			    hfa384x_from_bap(dev, BAP0, *payload, len)) {
2235				PDEBUG(DEBUG_EXTRA, "%s: could not read TX "
2236				       "frame payload\n", dev->name);
2237				kfree(*payload);
2238				*payload = NULL;
2239				ret = -1;
2240				goto fail;
2241			}
2242		}
2243	}
2244
2245 fail:
2246	spin_unlock(&local->baplock);
2247
2248	return ret;
2249}
2250
2251
2252/* Called only as a tasklet (software IRQ) */
2253static void prism2_tx_ev(local_info_t *local)
2254{
2255	struct net_device *dev = local->dev;
2256	char *payload = NULL;
2257	struct hfa384x_tx_frame txdesc;
2258
2259	if (hostap_tx_compl_read(local, 0, &txdesc, &payload))
2260		goto fail;
2261
2262	if (local->frame_dump & PRISM2_DUMP_TX_HDR) {
2263		PDEBUG(DEBUG_EXTRA, "%s: TX - status=0x%04x "
2264		       "retry_count=%d tx_rate=%d seq_ctrl=%d "
2265		       "duration_id=%d\n",
2266		       dev->name, le16_to_cpu(txdesc.status),
2267		       txdesc.retry_count, txdesc.tx_rate,
2268		       le16_to_cpu(txdesc.seq_ctrl),
2269		       le16_to_cpu(txdesc.duration_id));
2270	}
2271
2272	if (txdesc.sw_support)
2273		hostap_tx_callback(local, &txdesc, 1, payload);
2274	kfree(payload);
2275
2276 fail:
2277	HFA384X_OUTW(HFA384X_EV_TX, HFA384X_EVACK_OFF);
2278}
2279
2280
2281/* Called only as a tasklet (software IRQ) */
2282static void hostap_sta_tx_exc_tasklet(struct tasklet_struct *t)
2283{
2284	local_info_t *local = from_tasklet(local, t, sta_tx_exc_tasklet);
2285	struct sk_buff *skb;
2286
2287	while ((skb = skb_dequeue(&local->sta_tx_exc_list)) != NULL) {
2288		struct hfa384x_tx_frame *txdesc =
2289			(struct hfa384x_tx_frame *) skb->data;
2290
2291		if (skb->len >= sizeof(*txdesc)) {
2292			/* Convert Prism2 RX structure into IEEE 802.11 header
2293			 */
2294			int hdrlen = hostap_80211_get_hdrlen(txdesc->frame_control);
2295			memmove(skb_pull(skb, sizeof(*txdesc) - hdrlen),
2296				&txdesc->frame_control, hdrlen);
2297
2298			hostap_handle_sta_tx_exc(local, skb);
2299		}
2300		dev_kfree_skb(skb);
2301	}
2302}
2303
2304
2305/* Called only as a tasklet (software IRQ) */
2306static void prism2_txexc(local_info_t *local)
2307{
2308	struct net_device *dev = local->dev;
2309	u16 status, fc;
2310	int show_dump, res;
2311	char *payload = NULL;
2312	struct hfa384x_tx_frame txdesc;
2313
2314	show_dump = local->frame_dump & PRISM2_DUMP_TXEXC_HDR;
2315	dev->stats.tx_errors++;
2316
2317	res = hostap_tx_compl_read(local, 1, &txdesc, &payload);
2318	HFA384X_OUTW(HFA384X_EV_TXEXC, HFA384X_EVACK_OFF);
2319	if (res)
2320		return;
2321
2322	status = le16_to_cpu(txdesc.status);
2323
2324	/* We produce a TXDROP event only for retry or lifetime
2325	 * exceeded, because that's the only status that really mean
2326	 * that this particular node went away.
2327	 * Other errors means that *we* screwed up. - Jean II */
2328	if (status & (HFA384X_TX_STATUS_RETRYERR | HFA384X_TX_STATUS_AGEDERR))
2329	{
2330		union iwreq_data wrqu;
2331
2332		/* Copy 802.11 dest address. */
2333		memcpy(wrqu.addr.sa_data, txdesc.addr1, ETH_ALEN);
2334		wrqu.addr.sa_family = ARPHRD_ETHER;
2335		wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
2336	} else
2337		show_dump = 1;
2338
2339	if (local->iw_mode == IW_MODE_MASTER ||
2340	    local->iw_mode == IW_MODE_REPEAT ||
2341	    local->wds_type & HOSTAP_WDS_AP_CLIENT) {
2342		struct sk_buff *skb;
2343		skb = dev_alloc_skb(sizeof(txdesc));
2344		if (skb) {
2345			skb_put_data(skb, &txdesc, sizeof(txdesc));
2346			skb_queue_tail(&local->sta_tx_exc_list, skb);
2347			tasklet_schedule(&local->sta_tx_exc_tasklet);
2348		}
2349	}
2350
2351	if (txdesc.sw_support)
2352		hostap_tx_callback(local, &txdesc, 0, payload);
2353	kfree(payload);
2354
2355	if (!show_dump)
2356		return;
2357
2358	PDEBUG(DEBUG_EXTRA, "%s: TXEXC - status=0x%04x (%s%s%s%s)"
2359	       " tx_control=%04x\n",
2360	       dev->name, status,
2361	       status & HFA384X_TX_STATUS_RETRYERR ? "[RetryErr]" : "",
2362	       status & HFA384X_TX_STATUS_AGEDERR ? "[AgedErr]" : "",
2363	       status & HFA384X_TX_STATUS_DISCON ? "[Discon]" : "",
2364	       status & HFA384X_TX_STATUS_FORMERR ? "[FormErr]" : "",
2365	       le16_to_cpu(txdesc.tx_control));
2366
2367	fc = le16_to_cpu(txdesc.frame_control);
2368	PDEBUG(DEBUG_EXTRA, "   retry_count=%d tx_rate=%d fc=0x%04x "
2369	       "(%s%s%s::%d%s%s)\n",
2370	       txdesc.retry_count, txdesc.tx_rate, fc,
2371	       ieee80211_is_mgmt(txdesc.frame_control) ? "Mgmt" : "",
2372	       ieee80211_is_ctl(txdesc.frame_control) ? "Ctrl" : "",
2373	       ieee80211_is_data(txdesc.frame_control) ? "Data" : "",
2374	       (fc & IEEE80211_FCTL_STYPE) >> 4,
2375	       ieee80211_has_tods(txdesc.frame_control) ? " ToDS" : "",
2376	       ieee80211_has_fromds(txdesc.frame_control) ? " FromDS" : "");
2377	PDEBUG(DEBUG_EXTRA, "   A1=%pM A2=%pM A3=%pM A4=%pM\n",
2378	       txdesc.addr1, txdesc.addr2,
2379	       txdesc.addr3, txdesc.addr4);
2380}
2381
2382
2383/* Called only as a tasklet (software IRQ) */
2384static void hostap_info_tasklet(struct tasklet_struct *t)
2385{
2386	local_info_t *local = from_tasklet(local, t, info_tasklet);
2387	struct sk_buff *skb;
2388
2389	while ((skb = skb_dequeue(&local->info_list)) != NULL) {
2390		hostap_info_process(local, skb);
2391		dev_kfree_skb(skb);
2392	}
2393}
2394
2395
2396/* Called only as a tasklet (software IRQ) */
2397static void prism2_info(local_info_t *local)
2398{
2399	struct net_device *dev = local->dev;
2400	u16 fid;
2401	int res, left;
2402	struct hfa384x_info_frame info;
2403	struct sk_buff *skb;
2404
2405	fid = HFA384X_INW(HFA384X_INFOFID_OFF);
2406
2407	spin_lock(&local->baplock);
2408	res = hfa384x_setup_bap(dev, BAP0, fid, 0);
2409	if (!res)
2410		res = hfa384x_from_bap(dev, BAP0, &info, sizeof(info));
2411	if (res) {
2412		spin_unlock(&local->baplock);
2413		printk(KERN_DEBUG "Could not get info frame (fid=0x%04x)\n",
2414		       fid);
2415		if (res == -ETIMEDOUT) {
2416			schedule_work(&local->reset_queue);
2417		}
2418		goto out;
2419	}
2420
2421	left = (le16_to_cpu(info.len) - 1) * 2;
2422
2423	if (info.len & cpu_to_le16(0x8000) || info.len == 0 || left > 2060) {
2424		/* data register seems to give 0x8000 in some error cases even
2425		 * though busy bit is not set in offset register;
2426		 * in addition, length must be at least 1 due to type field */
2427		spin_unlock(&local->baplock);
2428		printk(KERN_DEBUG "%s: Received info frame with invalid "
2429		       "length 0x%04x (type 0x%04x)\n", dev->name,
2430		       le16_to_cpu(info.len), le16_to_cpu(info.type));
2431		goto out;
2432	}
2433
2434	skb = dev_alloc_skb(sizeof(info) + left);
2435	if (skb == NULL) {
2436		spin_unlock(&local->baplock);
2437		printk(KERN_DEBUG "%s: Could not allocate skb for info "
2438		       "frame\n", dev->name);
2439		goto out;
2440	}
2441
2442	skb_put_data(skb, &info, sizeof(info));
2443	if (left > 0 && hfa384x_from_bap(dev, BAP0, skb_put(skb, left), left))
2444	{
2445		spin_unlock(&local->baplock);
2446		printk(KERN_WARNING "%s: Info frame read failed (fid=0x%04x, "
2447		       "len=0x%04x, type=0x%04x\n", dev->name, fid,
2448		       le16_to_cpu(info.len), le16_to_cpu(info.type));
2449		dev_kfree_skb(skb);
2450		goto out;
2451	}
2452	spin_unlock(&local->baplock);
2453
2454	skb_queue_tail(&local->info_list, skb);
2455	tasklet_schedule(&local->info_tasklet);
2456
2457 out:
2458	HFA384X_OUTW(HFA384X_EV_INFO, HFA384X_EVACK_OFF);
2459}
2460
2461
2462/* Called only as a tasklet (software IRQ) */
2463static void hostap_bap_tasklet(struct tasklet_struct *t)
2464{
2465	local_info_t *local = from_tasklet(local, t, bap_tasklet);
2466	struct net_device *dev = local->dev;
2467	u16 ev;
2468	int frames = 30;
2469
2470	if (local->func->card_present && !local->func->card_present(local))
2471		return;
2472
2473	set_bit(HOSTAP_BITS_BAP_TASKLET, &local->bits);
2474
2475	/* Process all pending BAP events without generating new interrupts
2476	 * for them */
2477	while (frames-- > 0) {
2478		ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
2479		if (ev == 0xffff || !(ev & HFA384X_BAP0_EVENTS))
2480			break;
2481		if (ev & HFA384X_EV_RX)
2482			prism2_rx(local);
2483		if (ev & HFA384X_EV_INFO)
2484			prism2_info(local);
2485		if (ev & HFA384X_EV_TX)
2486			prism2_tx_ev(local);
2487		if (ev & HFA384X_EV_TXEXC)
2488			prism2_txexc(local);
2489	}
2490
2491	set_bit(HOSTAP_BITS_BAP_TASKLET2, &local->bits);
2492	clear_bit(HOSTAP_BITS_BAP_TASKLET, &local->bits);
2493
2494	/* Enable interrupts for new BAP events */
2495	hfa384x_events_all(dev);
2496	clear_bit(HOSTAP_BITS_BAP_TASKLET2, &local->bits);
2497}
2498
2499
2500/* Called only from hardware IRQ */
2501static void prism2_infdrop(struct net_device *dev)
2502{
2503	static unsigned long last_inquire = 0;
2504
2505	PDEBUG(DEBUG_EXTRA, "%s: INFDROP event\n", dev->name);
2506
2507	/* some firmware versions seem to get stuck with
2508	 * full CommTallies in high traffic load cases; every
2509	 * packet will then cause INFDROP event and CommTallies
2510	 * info frame will not be sent automatically. Try to
2511	 * get out of this state by inquiring CommTallies. */
2512	if (!last_inquire || time_after(jiffies, last_inquire + HZ)) {
2513		hfa384x_cmd_callback(dev, HFA384X_CMDCODE_INQUIRE,
2514				     HFA384X_INFO_COMMTALLIES, NULL, 0);
2515		last_inquire = jiffies;
2516	}
2517}
2518
2519
2520/* Called only from hardware IRQ */
2521static void prism2_ev_tick(struct net_device *dev)
2522{
2523	struct hostap_interface *iface;
2524	local_info_t *local;
2525	u16 evstat, inten;
2526	static int prev_stuck = 0;
2527
2528	iface = netdev_priv(dev);
2529	local = iface->local;
2530
2531	if (time_after(jiffies, local->last_tick_timer + 5 * HZ) &&
2532	    local->last_tick_timer) {
2533		evstat = HFA384X_INW(HFA384X_EVSTAT_OFF);
2534		inten = HFA384X_INW(HFA384X_INTEN_OFF);
2535		if (!prev_stuck) {
2536			printk(KERN_INFO "%s: SW TICK stuck? "
2537			       "bits=0x%lx EvStat=%04x IntEn=%04x\n",
2538			       dev->name, local->bits, evstat, inten);
2539		}
2540		local->sw_tick_stuck++;
2541		if ((evstat & HFA384X_BAP0_EVENTS) &&
2542		    (inten & HFA384X_BAP0_EVENTS)) {
2543			printk(KERN_INFO "%s: trying to recover from IRQ "
2544			       "hang\n", dev->name);
2545			hfa384x_events_no_bap0(dev);
2546		}
2547		prev_stuck = 1;
2548	} else
2549		prev_stuck = 0;
2550}
2551
2552
2553/* Called only from hardware IRQ */
2554static void prism2_check_magic(local_info_t *local)
2555{
2556	/* at least PCI Prism2.5 with bus mastering seems to sometimes
2557	 * return 0x0000 in SWSUPPORT0 for unknown reason, but re-reading the
2558	 * register once or twice seems to get the correct value.. PCI cards
2559	 * cannot anyway be removed during normal operation, so there is not
2560	 * really any need for this verification with them. */
2561
2562#ifndef PRISM2_PCI
2563#ifndef final_version
2564	static unsigned long last_magic_err = 0;
2565	struct net_device *dev = local->dev;
2566
2567	if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
2568		if (!local->hw_ready)
2569			return;
2570		HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
2571		if (time_after(jiffies, last_magic_err + 10 * HZ)) {
2572			printk("%s: Interrupt, but SWSUPPORT0 does not match: "
2573			       "%04X != %04X - card removed?\n", dev->name,
2574			       HFA384X_INW(HFA384X_SWSUPPORT0_OFF),
2575			       HFA384X_MAGIC);
2576			last_magic_err = jiffies;
2577		} else if (net_ratelimit()) {
2578			printk(KERN_DEBUG "%s: interrupt - SWSUPPORT0=%04x "
2579			       "MAGIC=%04x\n", dev->name,
2580			       HFA384X_INW(HFA384X_SWSUPPORT0_OFF),
2581			       HFA384X_MAGIC);
2582		}
2583		if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != 0xffff)
2584			schedule_work(&local->reset_queue);
2585		return;
2586	}
2587#endif /* final_version */
2588#endif /* !PRISM2_PCI */
2589}
2590
2591
2592/* Called only from hardware IRQ */
2593static irqreturn_t prism2_interrupt(int irq, void *dev_id)
2594{
2595	struct net_device *dev = dev_id;
2596	struct hostap_interface *iface;
2597	local_info_t *local;
2598	int events = 0;
2599	u16 ev;
2600
2601	iface = netdev_priv(dev);
2602	local = iface->local;
2603
2604	/* Detect early interrupt before driver is fully configured */
2605	spin_lock(&local->irq_init_lock);
2606	if (!dev->base_addr) {
2607		if (net_ratelimit()) {
2608			printk(KERN_DEBUG "%s: Interrupt, but dev not configured\n",
2609			       dev->name);
2610		}
2611		spin_unlock(&local->irq_init_lock);
2612		return IRQ_HANDLED;
2613	}
2614	spin_unlock(&local->irq_init_lock);
2615
2616	prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INTERRUPT, 0, 0);
2617
2618	if (local->func->card_present && !local->func->card_present(local)) {
2619		if (net_ratelimit()) {
2620			printk(KERN_DEBUG "%s: Interrupt, but dev not OK\n",
2621			       dev->name);
2622		}
2623		return IRQ_HANDLED;
2624	}
2625
2626	prism2_check_magic(local);
2627
2628	for (;;) {
2629		ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
2630		if (ev == 0xffff) {
2631			if (local->shutdown)
2632				return IRQ_HANDLED;
2633			HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
2634			printk(KERN_DEBUG "%s: prism2_interrupt: ev=0xffff\n",
2635			       dev->name);
2636			return IRQ_HANDLED;
2637		}
2638
2639		ev &= HFA384X_INW(HFA384X_INTEN_OFF);
2640		if (ev == 0)
2641			break;
2642
2643		if (ev & HFA384X_EV_CMD) {
2644			prism2_cmd_ev(dev);
2645		}
2646
2647		/* Above events are needed even before hw is ready, but other
2648		 * events should be skipped during initialization. This may
2649		 * change for AllocEv if allocate_fid is implemented without
2650		 * busy waiting. */
2651		if (!local->hw_ready || local->hw_resetting ||
2652		    !local->dev_enabled) {
2653			ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
2654			if (ev & HFA384X_EV_CMD)
2655				goto next_event;
2656			if ((ev & HFA384X_EVENT_MASK) == 0)
2657				return IRQ_HANDLED;
2658			if (local->dev_enabled && (ev & ~HFA384X_EV_TICK) &&
2659			    net_ratelimit()) {
2660				printk(KERN_DEBUG "%s: prism2_interrupt: hw "
2661				       "not ready; skipping events 0x%04x "
2662				       "(IntEn=0x%04x)%s%s%s\n",
2663				       dev->name, ev,
2664				       HFA384X_INW(HFA384X_INTEN_OFF),
2665				       !local->hw_ready ? " (!hw_ready)" : "",
2666				       local->hw_resetting ?
2667				       " (hw_resetting)" : "",
2668				       !local->dev_enabled ?
2669				       " (!dev_enabled)" : "");
2670			}
2671			HFA384X_OUTW(ev, HFA384X_EVACK_OFF);
2672			return IRQ_HANDLED;
2673		}
2674
2675		if (ev & HFA384X_EV_TICK) {
2676			prism2_ev_tick(dev);
2677			HFA384X_OUTW(HFA384X_EV_TICK, HFA384X_EVACK_OFF);
2678		}
2679
2680		if (ev & HFA384X_EV_ALLOC) {
2681			prism2_alloc_ev(dev);
2682			HFA384X_OUTW(HFA384X_EV_ALLOC, HFA384X_EVACK_OFF);
2683		}
2684
2685		/* Reading data from the card is quite time consuming, so do it
2686		 * in tasklets. TX, TXEXC, RX, and INFO events will be ACKed
2687		 * and unmasked after needed data has been read completely. */
2688		if (ev & HFA384X_BAP0_EVENTS) {
2689			hfa384x_events_no_bap0(dev);
2690			tasklet_schedule(&local->bap_tasklet);
2691		}
2692
2693#ifndef final_version
2694		if (ev & HFA384X_EV_WTERR) {
2695			PDEBUG(DEBUG_EXTRA, "%s: WTERR event\n", dev->name);
2696			HFA384X_OUTW(HFA384X_EV_WTERR, HFA384X_EVACK_OFF);
2697		}
2698#endif /* final_version */
2699
2700		if (ev & HFA384X_EV_INFDROP) {
2701			prism2_infdrop(dev);
2702			HFA384X_OUTW(HFA384X_EV_INFDROP, HFA384X_EVACK_OFF);
2703		}
2704
2705	next_event:
2706		events++;
2707		if (events >= PRISM2_MAX_INTERRUPT_EVENTS) {
2708			PDEBUG(DEBUG_EXTRA, "prism2_interrupt: >%d events "
2709			       "(EvStat=0x%04x)\n",
2710			       PRISM2_MAX_INTERRUPT_EVENTS,
2711			       HFA384X_INW(HFA384X_EVSTAT_OFF));
2712			break;
2713		}
2714	}
2715	prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INTERRUPT, 0, 1);
2716	return IRQ_RETVAL(events);
2717}
2718
2719
2720static void prism2_check_sta_fw_version(local_info_t *local)
2721{
2722	struct hfa384x_comp_ident comp;
2723	int id, variant, major, minor;
2724
2725	if (hfa384x_get_rid(local->dev, HFA384X_RID_STAID,
2726			    &comp, sizeof(comp), 1) < 0)
2727		return;
2728
2729	local->fw_ap = 0;
2730	id = le16_to_cpu(comp.id);
2731	if (id != HFA384X_COMP_ID_STA) {
2732		if (id == HFA384X_COMP_ID_FW_AP)
2733			local->fw_ap = 1;
2734		return;
2735	}
2736
2737	major = __le16_to_cpu(comp.major);
2738	minor = __le16_to_cpu(comp.minor);
2739	variant = __le16_to_cpu(comp.variant);
2740	local->sta_fw_ver = PRISM2_FW_VER(major, minor, variant);
2741
2742	/* Station firmware versions before 1.4.x seem to have a bug in
2743	 * firmware-based WEP encryption when using Host AP mode, so use
2744	 * host_encrypt as a default for them. Firmware version 1.4.9 is the
2745	 * first one that has been seen to produce correct encryption, but the
2746	 * bug might be fixed before that (although, at least 1.4.2 is broken).
2747	 */
2748	local->fw_encrypt_ok = local->sta_fw_ver >= PRISM2_FW_VER(1,4,9);
2749
2750	if (local->iw_mode == IW_MODE_MASTER && !local->host_encrypt &&
2751	    !local->fw_encrypt_ok) {
2752		printk(KERN_DEBUG "%s: defaulting to host-based encryption as "
2753		       "a workaround for firmware bug in Host AP mode WEP\n",
2754		       local->dev->name);
2755		local->host_encrypt = 1;
2756	}
2757
2758	/* IEEE 802.11 standard compliant WDS frames (4 addresses) were broken
2759	 * in station firmware versions before 1.5.x. With these versions, the
2760	 * driver uses a workaround with bogus frame format (4th address after
2761	 * the payload). This is not compatible with other AP devices. Since
2762	 * the firmware bug is fixed in the latest station firmware versions,
2763	 * automatically enable standard compliant mode for cards using station
2764	 * firmware version 1.5.0 or newer. */
2765	if (local->sta_fw_ver >= PRISM2_FW_VER(1,5,0))
2766		local->wds_type |= HOSTAP_WDS_STANDARD_FRAME;
2767	else {
2768		printk(KERN_DEBUG "%s: defaulting to bogus WDS frame as a "
2769		       "workaround for firmware bug in Host AP mode WDS\n",
2770		       local->dev->name);
2771	}
2772
2773	hostap_check_sta_fw_version(local->ap, local->sta_fw_ver);
2774}
2775
2776
2777static void hostap_passive_scan(struct timer_list *t)
2778{
2779	local_info_t *local = from_timer(local, t, passive_scan_timer);
2780	struct net_device *dev = local->dev;
2781	u16 chan;
2782
2783	if (local->passive_scan_interval <= 0)
2784		return;
2785
2786	if (local->passive_scan_state == PASSIVE_SCAN_LISTEN) {
2787		int max_tries = 16;
2788
2789		/* Even though host system does not really know when the WLAN
2790		 * MAC is sending frames, try to avoid changing channels for
2791		 * passive scanning when a host-generated frame is being
2792		 * transmitted */
2793		if (test_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
2794			printk(KERN_DEBUG "%s: passive scan detected pending "
2795			       "TX - delaying\n", dev->name);
2796			local->passive_scan_timer.expires = jiffies + HZ / 10;
2797			add_timer(&local->passive_scan_timer);
2798			return;
2799		}
2800
2801		do {
2802			local->passive_scan_channel++;
2803			if (local->passive_scan_channel > 14)
2804				local->passive_scan_channel = 1;
2805			max_tries--;
2806		} while (!(local->channel_mask &
2807			   (1 << (local->passive_scan_channel - 1))) &&
2808			 max_tries > 0);
2809
2810		if (max_tries == 0) {
2811			printk(KERN_INFO "%s: no allowed passive scan channels"
2812			       " found\n", dev->name);
2813			return;
2814		}
2815
2816		printk(KERN_DEBUG "%s: passive scan channel %d\n",
2817		       dev->name, local->passive_scan_channel);
2818		chan = local->passive_scan_channel;
2819		local->passive_scan_state = PASSIVE_SCAN_WAIT;
2820		local->passive_scan_timer.expires = jiffies + HZ / 10;
2821	} else {
2822		chan = local->channel;
2823		local->passive_scan_state = PASSIVE_SCAN_LISTEN;
2824		local->passive_scan_timer.expires = jiffies +
2825			local->passive_scan_interval * HZ;
2826	}
2827
2828	if (hfa384x_cmd_callback(dev, HFA384X_CMDCODE_TEST |
2829				 (HFA384X_TEST_CHANGE_CHANNEL << 8),
2830				 chan, NULL, 0))
2831		printk(KERN_ERR "%s: passive scan channel set %d "
2832		       "failed\n", dev->name, chan);
2833
2834	add_timer(&local->passive_scan_timer);
2835}
2836
2837
2838/* Called only as a scheduled task when communications quality values should
2839 * be updated. */
2840static void handle_comms_qual_update(struct work_struct *work)
2841{
2842	local_info_t *local =
2843		container_of(work, local_info_t, comms_qual_update);
2844	prism2_update_comms_qual(local->dev);
2845}
2846
2847
2848/* Software watchdog - called as a timer. Hardware interrupt (Tick event) is
2849 * used to monitor that local->last_tick_timer is being updated. If not,
2850 * interrupt busy-loop is assumed and driver tries to recover by masking out
2851 * some events. */
2852static void hostap_tick_timer(struct timer_list *t)
2853{
2854	static unsigned long last_inquire = 0;
2855	local_info_t *local = from_timer(local, t, tick_timer);
2856	local->last_tick_timer = jiffies;
2857
2858	/* Inquire CommTallies every 10 seconds to keep the statistics updated
2859	 * more often during low load and when using 32-bit tallies. */
2860	if ((!last_inquire || time_after(jiffies, last_inquire + 10 * HZ)) &&
2861	    !local->hw_downloading && local->hw_ready &&
2862	    !local->hw_resetting && local->dev_enabled) {
2863		hfa384x_cmd_callback(local->dev, HFA384X_CMDCODE_INQUIRE,
2864				     HFA384X_INFO_COMMTALLIES, NULL, 0);
2865		last_inquire = jiffies;
2866	}
2867
2868	if ((local->last_comms_qual_update == 0 ||
2869	     time_after(jiffies, local->last_comms_qual_update + 10 * HZ)) &&
2870	    (local->iw_mode == IW_MODE_INFRA ||
2871	     local->iw_mode == IW_MODE_ADHOC)) {
2872		schedule_work(&local->comms_qual_update);
2873	}
2874
2875	local->tick_timer.expires = jiffies + 2 * HZ;
2876	add_timer(&local->tick_timer);
2877}
2878
2879
2880#if !defined(PRISM2_NO_PROCFS_DEBUG) && defined(CONFIG_PROC_FS)
2881static u16 hfa384x_read_reg(struct net_device *dev, u16 reg)
2882{
2883	return HFA384X_INW(reg);
2884}
2885
2886static int prism2_registers_proc_show(struct seq_file *m, void *v)
2887{
2888	local_info_t *local = m->private;
2889
2890#define SHOW_REG(n) \
2891  seq_printf(m, #n "=%04x\n", hfa384x_read_reg(local->dev, HFA384X_##n##_OFF))
2892
2893	SHOW_REG(CMD);
2894	SHOW_REG(PARAM0);
2895	SHOW_REG(PARAM1);
2896	SHOW_REG(PARAM2);
2897	SHOW_REG(STATUS);
2898	SHOW_REG(RESP0);
2899	SHOW_REG(RESP1);
2900	SHOW_REG(RESP2);
2901	SHOW_REG(INFOFID);
2902	SHOW_REG(CONTROL);
2903	SHOW_REG(SELECT0);
2904	SHOW_REG(SELECT1);
2905	SHOW_REG(OFFSET0);
2906	SHOW_REG(OFFSET1);
2907	SHOW_REG(RXFID);
2908	SHOW_REG(ALLOCFID);
2909	SHOW_REG(TXCOMPLFID);
2910	SHOW_REG(SWSUPPORT0);
2911	SHOW_REG(SWSUPPORT1);
2912	SHOW_REG(SWSUPPORT2);
2913	SHOW_REG(EVSTAT);
2914	SHOW_REG(INTEN);
2915	SHOW_REG(EVACK);
2916	/* Do not read data registers, because they change the state of the
2917	 * MAC (offset += 2) */
2918	/* SHOW_REG(DATA0); */
2919	/* SHOW_REG(DATA1); */
2920	SHOW_REG(AUXPAGE);
2921	SHOW_REG(AUXOFFSET);
2922	/* SHOW_REG(AUXDATA); */
2923#ifdef PRISM2_PCI
2924	SHOW_REG(PCICOR);
2925	SHOW_REG(PCIHCR);
2926	SHOW_REG(PCI_M0_ADDRH);
2927	SHOW_REG(PCI_M0_ADDRL);
2928	SHOW_REG(PCI_M0_LEN);
2929	SHOW_REG(PCI_M0_CTL);
2930	SHOW_REG(PCI_STATUS);
2931	SHOW_REG(PCI_M1_ADDRH);
2932	SHOW_REG(PCI_M1_ADDRL);
2933	SHOW_REG(PCI_M1_LEN);
2934	SHOW_REG(PCI_M1_CTL);
2935#endif /* PRISM2_PCI */
2936
2937	return 0;
2938}
2939#endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2940
2941struct set_tim_data {
2942	struct list_head list;
2943	int aid;
2944	int set;
2945};
2946
2947static int prism2_set_tim(struct net_device *dev, int aid, int set)
2948{
2949	struct list_head *ptr;
2950	struct set_tim_data *new_entry;
2951	struct hostap_interface *iface;
2952	local_info_t *local;
2953
2954	iface = netdev_priv(dev);
2955	local = iface->local;
2956
2957	new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
2958	if (new_entry == NULL)
2959		return -ENOMEM;
2960
2961	new_entry->aid = aid;
2962	new_entry->set = set;
2963
2964	spin_lock_bh(&local->set_tim_lock);
2965	list_for_each(ptr, &local->set_tim_list) {
2966		struct set_tim_data *entry =
2967			list_entry(ptr, struct set_tim_data, list);
2968		if (entry->aid == aid) {
2969			PDEBUG(DEBUG_PS2, "%s: prism2_set_tim: aid=%d "
2970			       "set=%d ==> %d\n",
2971			       local->dev->name, aid, entry->set, set);
2972			entry->set = set;
2973			kfree(new_entry);
2974			new_entry = NULL;
2975			break;
2976		}
2977	}
2978	if (new_entry)
2979		list_add_tail(&new_entry->list, &local->set_tim_list);
2980	spin_unlock_bh(&local->set_tim_lock);
2981
2982	schedule_work(&local->set_tim_queue);
2983
2984	return 0;
2985}
2986
2987
2988static void handle_set_tim_queue(struct work_struct *work)
2989{
2990	local_info_t *local = container_of(work, local_info_t, set_tim_queue);
2991	struct set_tim_data *entry;
2992	u16 val;
2993
2994	for (;;) {
2995		entry = NULL;
2996		spin_lock_bh(&local->set_tim_lock);
2997		if (!list_empty(&local->set_tim_list)) {
2998			entry = list_entry(local->set_tim_list.next,
2999					   struct set_tim_data, list);
3000			list_del(&entry->list);
3001		}
3002		spin_unlock_bh(&local->set_tim_lock);
3003		if (!entry)
3004			break;
3005
3006		PDEBUG(DEBUG_PS2, "%s: handle_set_tim_queue: aid=%d set=%d\n",
3007		       local->dev->name, entry->aid, entry->set);
3008
3009		val = entry->aid;
3010		if (entry->set)
3011			val |= 0x8000;
3012		if (hostap_set_word(local->dev, HFA384X_RID_CNFTIMCTRL, val)) {
3013			printk(KERN_DEBUG "%s: set_tim failed (aid=%d "
3014			       "set=%d)\n",
3015			       local->dev->name, entry->aid, entry->set);
3016		}
3017
3018		kfree(entry);
3019	}
3020}
3021
3022
3023static void prism2_clear_set_tim_queue(local_info_t *local)
3024{
3025	struct list_head *ptr, *n;
3026
3027	list_for_each_safe(ptr, n, &local->set_tim_list) {
3028		struct set_tim_data *entry;
3029		entry = list_entry(ptr, struct set_tim_data, list);
3030		list_del(&entry->list);
3031		kfree(entry);
3032	}
3033}
3034
3035
3036/*
3037 * HostAP uses two layers of net devices, where the inner
3038 * layer gets called all the time from the outer layer.
3039 * This is a natural nesting, which needs a split lock type.
3040 */
3041static struct lock_class_key hostap_netdev_xmit_lock_key;
3042static struct lock_class_key hostap_netdev_addr_lock_key;
3043
3044static void prism2_set_lockdep_class_one(struct net_device *dev,
3045					 struct netdev_queue *txq,
3046					 void *_unused)
3047{
3048	lockdep_set_class(&txq->_xmit_lock,
3049			  &hostap_netdev_xmit_lock_key);
3050}
3051
3052static void prism2_set_lockdep_class(struct net_device *dev)
3053{
3054	lockdep_set_class(&dev->addr_list_lock,
3055			  &hostap_netdev_addr_lock_key);
3056	netdev_for_each_tx_queue(dev, prism2_set_lockdep_class_one, NULL);
3057}
3058
3059static struct net_device *
3060prism2_init_local_data(struct prism2_helper_functions *funcs, int card_idx,
3061		       struct device *sdev)
3062{
3063	struct net_device *dev;
3064	struct hostap_interface *iface;
3065	struct local_info *local;
3066	int len, i, ret;
3067
3068	if (funcs == NULL)
3069		return NULL;
3070
3071	len = strlen(dev_template);
3072	if (len >= IFNAMSIZ || strstr(dev_template, "%d") == NULL) {
3073		printk(KERN_WARNING "hostap: Invalid dev_template='%s'\n",
3074		       dev_template);
3075		return NULL;
3076	}
3077
3078	len = sizeof(struct hostap_interface) +
3079		3 + sizeof(struct local_info) +
3080		3 + sizeof(struct ap_data);
3081
3082	dev = alloc_etherdev(len);
3083	if (dev == NULL)
3084		return NULL;
3085
3086	iface = netdev_priv(dev);
3087	local = (struct local_info *) ((((long) (iface + 1)) + 3) & ~3);
3088	local->ap = (struct ap_data *) ((((long) (local + 1)) + 3) & ~3);
3089	local->dev = iface->dev = dev;
3090	iface->local = local;
3091	iface->type = HOSTAP_INTERFACE_MASTER;
3092	INIT_LIST_HEAD(&local->hostap_interfaces);
3093
3094	local->hw_module = THIS_MODULE;
3095
3096#ifdef PRISM2_IO_DEBUG
3097	local->io_debug_enabled = 1;
3098#endif /* PRISM2_IO_DEBUG */
3099
3100	local->func = funcs;
3101	local->func->cmd = hfa384x_cmd;
3102	local->func->read_regs = hfa384x_read_regs;
3103	local->func->get_rid = hfa384x_get_rid;
3104	local->func->set_rid = hfa384x_set_rid;
3105	local->func->hw_enable = prism2_hw_enable;
3106	local->func->hw_config = prism2_hw_config;
3107	local->func->hw_reset = prism2_hw_reset;
3108	local->func->hw_shutdown = prism2_hw_shutdown;
3109	local->func->reset_port = prism2_reset_port;
3110	local->func->schedule_reset = prism2_schedule_reset;
3111#ifdef PRISM2_DOWNLOAD_SUPPORT
3112	local->func->read_aux_proc_ops = &prism2_download_aux_dump_proc_ops;
3113	local->func->download = prism2_download;
3114#endif /* PRISM2_DOWNLOAD_SUPPORT */
3115	local->func->tx = prism2_tx_80211;
3116	local->func->set_tim = prism2_set_tim;
3117	local->func->need_tx_headroom = 0; /* no need to add txdesc in
3118					    * skb->data (FIX: maybe for DMA bus
3119					    * mastering? */
3120
3121	local->mtu = mtu;
3122
3123	rwlock_init(&local->iface_lock);
3124	spin_lock_init(&local->txfidlock);
3125	spin_lock_init(&local->cmdlock);
3126	spin_lock_init(&local->baplock);
3127	spin_lock_init(&local->lock);
3128	spin_lock_init(&local->irq_init_lock);
3129	mutex_init(&local->rid_bap_mtx);
3130
3131	if (card_idx < 0 || card_idx >= MAX_PARM_DEVICES)
3132		card_idx = 0;
3133	local->card_idx = card_idx;
3134
3135	len = strlen(essid);
3136	memcpy(local->essid, essid,
3137	       len > MAX_SSID_LEN ? MAX_SSID_LEN : len);
3138	local->essid[MAX_SSID_LEN] = '\0';
3139	i = GET_INT_PARM(iw_mode, card_idx);
3140	if ((i >= IW_MODE_ADHOC && i <= IW_MODE_REPEAT) ||
3141	    i == IW_MODE_MONITOR) {
3142		local->iw_mode = i;
3143	} else {
3144		printk(KERN_WARNING "prism2: Unknown iw_mode %d; using "
3145		       "IW_MODE_MASTER\n", i);
3146		local->iw_mode = IW_MODE_MASTER;
3147	}
3148	local->channel = GET_INT_PARM(channel, card_idx);
3149	local->beacon_int = GET_INT_PARM(beacon_int, card_idx);
3150	local->dtim_period = GET_INT_PARM(dtim_period, card_idx);
3151	local->wds_max_connections = 16;
3152	local->tx_control = HFA384X_TX_CTRL_FLAGS;
3153	local->manual_retry_count = -1;
3154	local->rts_threshold = 2347;
3155	local->fragm_threshold = 2346;
3156	local->rssi_to_dBm = 100; /* default; to be overriden by
3157				   * cnfDbmAdjust, if available */
3158	local->auth_algs = PRISM2_AUTH_OPEN | PRISM2_AUTH_SHARED_KEY;
3159	local->sram_type = -1;
3160	local->scan_channel_mask = 0xffff;
3161	local->monitor_type = PRISM2_MONITOR_RADIOTAP;
3162
3163	/* Initialize task queue structures */
3164	INIT_WORK(&local->reset_queue, handle_reset_queue);
3165	INIT_WORK(&local->set_multicast_list_queue,
3166		  hostap_set_multicast_list_queue);
3167
3168	INIT_WORK(&local->set_tim_queue, handle_set_tim_queue);
3169	INIT_LIST_HEAD(&local->set_tim_list);
3170	spin_lock_init(&local->set_tim_lock);
3171
3172	INIT_WORK(&local->comms_qual_update, handle_comms_qual_update);
3173
3174	/* Initialize tasklets for handling hardware IRQ related operations
3175	 * outside hw IRQ handler */
3176	tasklet_setup(&local->bap_tasklet, hostap_bap_tasklet);
3177	tasklet_setup(&local->info_tasklet, hostap_info_tasklet);
 
 
 
 
 
 
3178	hostap_info_init(local);
3179
3180	tasklet_setup(&local->rx_tasklet, hostap_rx_tasklet);
 
3181	skb_queue_head_init(&local->rx_list);
3182
3183	tasklet_setup(&local->sta_tx_exc_tasklet,
3184			    hostap_sta_tx_exc_tasklet);
3185	skb_queue_head_init(&local->sta_tx_exc_list);
3186
3187	INIT_LIST_HEAD(&local->cmd_queue);
3188	init_waitqueue_head(&local->hostscan_wq);
3189
3190	lib80211_crypt_info_init(&local->crypt_info, dev->name, &local->lock);
3191
3192	timer_setup(&local->passive_scan_timer, hostap_passive_scan, 0);
3193	timer_setup(&local->tick_timer, hostap_tick_timer, 0);
3194	local->tick_timer.expires = jiffies + 2 * HZ;
3195	add_timer(&local->tick_timer);
3196
3197	INIT_LIST_HEAD(&local->bss_list);
3198
3199	hostap_setup_dev(dev, local, HOSTAP_INTERFACE_MASTER);
3200
3201	dev->type = ARPHRD_IEEE80211;
3202	dev->header_ops = &hostap_80211_ops;
3203
3204	rtnl_lock();
3205	ret = dev_alloc_name(dev, "wifi%d");
3206	SET_NETDEV_DEV(dev, sdev);
3207	if (ret >= 0)
3208		ret = register_netdevice(dev);
3209
3210	prism2_set_lockdep_class(dev);
3211	rtnl_unlock();
3212	if (ret < 0) {
3213		printk(KERN_WARNING "%s: register netdevice failed!\n",
3214		       dev_info);
3215		goto fail;
3216	}
3217	printk(KERN_INFO "%s: Registered netdevice %s\n", dev_info, dev->name);
3218
3219	hostap_init_data(local);
3220	return dev;
3221
3222 fail:
3223	free_netdev(dev);
3224	return NULL;
3225}
3226
3227
3228static int hostap_hw_ready(struct net_device *dev)
3229{
3230	struct hostap_interface *iface;
3231	struct local_info *local;
3232
3233	iface = netdev_priv(dev);
3234	local = iface->local;
3235	local->ddev = hostap_add_interface(local, HOSTAP_INTERFACE_MAIN, 0,
3236					   "", dev_template);
3237
3238	if (local->ddev) {
3239		if (local->iw_mode == IW_MODE_INFRA ||
3240		    local->iw_mode == IW_MODE_ADHOC) {
3241			netif_carrier_off(local->dev);
3242			netif_carrier_off(local->ddev);
3243		}
3244		hostap_init_proc(local);
3245#ifndef PRISM2_NO_PROCFS_DEBUG
3246		proc_create_single_data("registers", 0, local->proc,
3247				 prism2_registers_proc_show, local);
3248#endif /* PRISM2_NO_PROCFS_DEBUG */
3249		hostap_init_ap_proc(local);
3250		return 0;
3251	}
3252
3253	return -1;
3254}
3255
3256
3257static void prism2_free_local_data(struct net_device *dev)
3258{
3259	struct hostap_tx_callback_info *tx_cb, *tx_cb_prev;
3260	int i;
3261	struct hostap_interface *iface;
3262	struct local_info *local;
3263	struct list_head *ptr, *n;
3264
3265	if (dev == NULL)
3266		return;
3267
3268	iface = netdev_priv(dev);
3269	local = iface->local;
3270
3271	/* Unregister all netdevs before freeing local data. */
3272	list_for_each_safe(ptr, n, &local->hostap_interfaces) {
3273		iface = list_entry(ptr, struct hostap_interface, list);
3274		if (iface->type == HOSTAP_INTERFACE_MASTER) {
3275			/* special handling for this interface below */
3276			continue;
3277		}
3278		hostap_remove_interface(iface->dev, 0, 1);
3279	}
3280
3281	unregister_netdev(local->dev);
3282
3283	flush_work(&local->reset_queue);
3284	flush_work(&local->set_multicast_list_queue);
3285	flush_work(&local->set_tim_queue);
3286#ifndef PRISM2_NO_STATION_MODES
3287	flush_work(&local->info_queue);
3288#endif
3289	flush_work(&local->comms_qual_update);
3290
3291	lib80211_crypt_info_free(&local->crypt_info);
3292
3293	if (timer_pending(&local->passive_scan_timer))
3294		del_timer(&local->passive_scan_timer);
3295
3296	if (timer_pending(&local->tick_timer))
3297		del_timer(&local->tick_timer);
3298
3299	prism2_clear_cmd_queue(local);
3300
3301	skb_queue_purge(&local->info_list);
3302	skb_queue_purge(&local->rx_list);
3303	skb_queue_purge(&local->sta_tx_exc_list);
3304
3305	if (local->dev_enabled)
3306		prism2_callback(local, PRISM2_CALLBACK_DISABLE);
3307
3308	if (local->ap != NULL)
3309		hostap_free_data(local->ap);
3310
3311#ifndef PRISM2_NO_PROCFS_DEBUG
3312	if (local->proc != NULL)
3313		remove_proc_entry("registers", local->proc);
3314#endif /* PRISM2_NO_PROCFS_DEBUG */
3315	hostap_remove_proc(local);
3316
3317	tx_cb = local->tx_callback;
3318	while (tx_cb != NULL) {
3319		tx_cb_prev = tx_cb;
3320		tx_cb = tx_cb->next;
3321		kfree(tx_cb_prev);
3322	}
3323
3324	hostap_set_hostapd(local, 0, 0);
3325	hostap_set_hostapd_sta(local, 0, 0);
3326
3327	for (i = 0; i < PRISM2_FRAG_CACHE_LEN; i++) {
3328		if (local->frag_cache[i].skb != NULL)
3329			dev_kfree_skb(local->frag_cache[i].skb);
3330	}
3331
3332#ifdef PRISM2_DOWNLOAD_SUPPORT
3333	prism2_download_free_data(local->dl_pri);
3334	prism2_download_free_data(local->dl_sec);
3335#endif /* PRISM2_DOWNLOAD_SUPPORT */
3336
3337	prism2_clear_set_tim_queue(local);
3338
3339	list_for_each_safe(ptr, n, &local->bss_list) {
3340		struct hostap_bss_info *bss =
3341			list_entry(ptr, struct hostap_bss_info, list);
3342		kfree(bss);
3343	}
3344
3345	kfree(local->pda);
3346	kfree(local->last_scan_results);
3347	kfree(local->generic_elem);
3348
3349	free_netdev(local->dev);
3350}
3351
3352
3353#if defined(PRISM2_PCI) || defined(PRISM2_PCCARD)
3354static void __maybe_unused prism2_suspend(struct net_device *dev)
3355{
3356	struct hostap_interface *iface;
3357	struct local_info *local;
3358	union iwreq_data wrqu;
3359
3360	iface = netdev_priv(dev);
3361	local = iface->local;
3362
3363	/* Send disconnect event, e.g., to trigger reassociation after resume
3364	 * if wpa_supplicant is used. */
3365	memset(&wrqu, 0, sizeof(wrqu));
3366	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
3367	wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
3368
3369	/* Disable hardware and firmware */
3370	prism2_hw_shutdown(dev, 0);
3371}
3372#endif /* PRISM2_PCI || PRISM2_PCCARD */
3373
3374
3375/* These might at some point be compiled separately and used as separate
3376 * kernel modules or linked into one */
3377#ifdef PRISM2_DOWNLOAD_SUPPORT
3378#include "hostap_download.c"
3379#endif /* PRISM2_DOWNLOAD_SUPPORT */
3380
3381#ifdef PRISM2_CALLBACK
3382/* External hostap_callback.c file can be used to, e.g., blink activity led.
3383 * This can use platform specific code and must define prism2_callback()
3384 * function (if PRISM2_CALLBACK is not defined, these function calls are not
3385 * used. */
3386#include "hostap_callback.c"
3387#endif /* PRISM2_CALLBACK */