PageRenderTime 70ms CodeModel.GetById 10ms app.highlight 47ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/wireless/hostap/hostap_hw.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 3424 lines | 2481 code | 579 blank | 364 comment | 486 complexity | bb1d47b2964c61ccab6534c0a8327f2c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

   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 <asm/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/if_arp.h>
  42#include <linux/delay.h>
  43#include <linux/random.h>
  44#include <linux/wait.h>
  45#include <linux/sched.h>
  46#include <linux/rtnetlink.h>
  47#include <linux/wireless.h>
  48#include <net/iw_handler.h>
  49#include <net/lib80211.h>
  50#include <asm/irq.h>
  51
  52#include "hostap_80211.h"
  53#include "hostap.h"
  54#include "hostap_ap.h"
  55
  56
  57/* #define final_version */
  58
  59static int mtu = 1500;
  60module_param(mtu, int, 0444);
  61MODULE_PARM_DESC(mtu, "Maximum transfer unit");
  62
  63static int channel[MAX_PARM_DEVICES] = { 3, DEF_INTS };
  64module_param_array(channel, int, NULL, 0444);
  65MODULE_PARM_DESC(channel, "Initial channel");
  66
  67static char essid[33] = "test";
  68module_param_string(essid, essid, sizeof(essid), 0444);
  69MODULE_PARM_DESC(essid, "Host AP's ESSID");
  70
  71static int iw_mode[MAX_PARM_DEVICES] = { IW_MODE_MASTER, DEF_INTS };
  72module_param_array(iw_mode, int, NULL, 0444);
  73MODULE_PARM_DESC(iw_mode, "Initial operation mode");
  74
  75static int beacon_int[MAX_PARM_DEVICES] = { 100, DEF_INTS };
  76module_param_array(beacon_int, int, NULL, 0444);
  77MODULE_PARM_DESC(beacon_int, "Beacon interval (1 = 1024 usec)");
  78
  79static int dtim_period[MAX_PARM_DEVICES] = { 1, DEF_INTS };
  80module_param_array(dtim_period, int, NULL, 0444);
  81MODULE_PARM_DESC(dtim_period, "DTIM period");
  82
  83static char dev_template[16] = "wlan%d";
  84module_param_string(dev_template, dev_template, sizeof(dev_template), 0444);
  85MODULE_PARM_DESC(dev_template, "Prefix for network device name (default: "
  86		 "wlan%d)");
  87
  88#ifdef final_version
  89#define EXTRA_EVENTS_WTERR 0
  90#else
  91/* check WTERR events (Wait Time-out) in development versions */
  92#define EXTRA_EVENTS_WTERR HFA384X_EV_WTERR
  93#endif
  94
  95/* Events that will be using BAP0 */
  96#define HFA384X_BAP0_EVENTS \
  97	(HFA384X_EV_TXEXC | HFA384X_EV_RX | HFA384X_EV_INFO | HFA384X_EV_TX)
  98
  99/* event mask, i.e., events that will result in an interrupt */
 100#define HFA384X_EVENT_MASK \
 101	(HFA384X_BAP0_EVENTS | HFA384X_EV_ALLOC | HFA384X_EV_INFDROP | \
 102	HFA384X_EV_CMD | HFA384X_EV_TICK | \
 103	EXTRA_EVENTS_WTERR)
 104
 105/* Default TX control flags: use 802.11 headers and request interrupt for
 106 * failed transmits. Frames that request ACK callback, will add
 107 * _TX_OK flag and _ALT_RTRY flag may be used to select different retry policy.
 108 */
 109#define HFA384X_TX_CTRL_FLAGS \
 110	(HFA384X_TX_CTRL_802_11 | HFA384X_TX_CTRL_TX_EX)
 111
 112
 113/* ca. 1 usec */
 114#define HFA384X_CMD_BUSY_TIMEOUT 5000
 115#define HFA384X_BAP_BUSY_TIMEOUT 50000
 116
 117/* ca. 10 usec */
 118#define HFA384X_CMD_COMPL_TIMEOUT 20000
 119#define HFA384X_DL_COMPL_TIMEOUT 1000000
 120
 121/* Wait times for initialization; yield to other processes to avoid busy
 122 * waiting for long time. */
 123#define HFA384X_INIT_TIMEOUT (HZ / 2) /* 500 ms */
 124#define HFA384X_ALLOC_COMPL_TIMEOUT (HZ / 20) /* 50 ms */
 125
 126
 127static void prism2_hw_reset(struct net_device *dev);
 128static void prism2_check_sta_fw_version(local_info_t *local);
 129
 130#ifdef PRISM2_DOWNLOAD_SUPPORT
 131/* hostap_download.c */
 132static int prism2_download_aux_dump(struct net_device *dev,
 133				    unsigned int addr, int len, u8 *buf);
 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 (atomic_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		atomic_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		printk(KERN_DEBUG "%s: hfa384x_cmd - kmalloc failed\n",
 352		       dev->name);
 353		return -ENOMEM;
 354	}
 355	atomic_set(&entry->usecnt, 1);
 356	entry->type = CMD_SLEEP;
 357	entry->cmd = cmd;
 358	entry->param0 = param0;
 359	if (param1)
 360		entry->param1 = *param1;
 361	init_waitqueue_head(&entry->compl);
 362
 363	/* prepare to wait for command completion event, but do not sleep yet
 364	 */
 365	add_wait_queue(&entry->compl, &wait);
 366	set_current_state(TASK_INTERRUPTIBLE);
 367
 368	spin_lock_irqsave(&local->cmdlock, flags);
 369	issue = list_empty(&local->cmd_queue);
 370	if (issue)
 371		entry->issuing = 1;
 372	list_add_tail(&entry->list, &local->cmd_queue);
 373	local->cmd_queue_len++;
 374	spin_unlock_irqrestore(&local->cmdlock, flags);
 375
 376	err = 0;
 377	if (!issue)
 378		goto wait_completion;
 379
 380	if (signal_pending(current))
 381		err = -EINTR;
 382
 383	if (!err) {
 384		if (hfa384x_cmd_issue(dev, entry))
 385			err = -ETIMEDOUT;
 386		else
 387			issued = 1;
 388	}
 389
 390 wait_completion:
 391	if (!err && entry->type != CMD_COMPLETED) {
 392		/* sleep until command is completed or timed out */
 393		res = schedule_timeout(2 * HZ);
 394	} else
 395		res = -1;
 396
 397	if (!err && signal_pending(current))
 398		err = -EINTR;
 399
 400	if (err && issued) {
 401		/* the command was issued, so a CmdCompl event should occur
 402		 * soon; however, there's a pending signal and
 403		 * schedule_timeout() would be interrupted; wait a short period
 404		 * of time to avoid removing entry from the list before
 405		 * CmdCompl event */
 406		udelay(300);
 407	}
 408
 409	set_current_state(TASK_RUNNING);
 410	remove_wait_queue(&entry->compl, &wait);
 411
 412	/* If entry->list is still in the list, it must be removed
 413	 * first and in this case prism2_cmd_ev() does not yet have
 414	 * local reference to it, and the data can be kfree()'d
 415	 * here. If the command completion event is still generated,
 416	 * it will be assigned to next (possibly) pending command, but
 417	 * the driver will reset the card anyway due to timeout
 418	 *
 419	 * If the entry is not in the list prism2_cmd_ev() has a local
 420	 * reference to it, but keeps cmdlock as long as the data is
 421	 * needed, so the data can be kfree()'d here. */
 422
 423	/* FIX: if the entry->list is in the list, it has not been completed
 424	 * yet, so removing it here is somewhat wrong.. this could cause
 425	 * references to freed memory and next list_del() causing NULL pointer
 426	 * dereference.. it would probably be better to leave the entry in the
 427	 * list and the list should be emptied during hw reset */
 428
 429	spin_lock_irqsave(&local->cmdlock, flags);
 430	if (!list_empty(&entry->list)) {
 431		printk(KERN_DEBUG "%s: hfa384x_cmd: entry still in list? "
 432		       "(entry=%p, type=%d, res=%d)\n", dev->name, entry,
 433		       entry->type, res);
 434		list_del_init(&entry->list);
 435		local->cmd_queue_len--;
 436	}
 437	spin_unlock_irqrestore(&local->cmdlock, flags);
 438
 439	if (err) {
 440		printk(KERN_DEBUG "%s: hfa384x_cmd: interrupted; err=%d\n",
 441		       dev->name, err);
 442		res = err;
 443		goto done;
 444	}
 445
 446	if (entry->type != CMD_COMPLETED) {
 447		u16 reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
 448		printk(KERN_DEBUG "%s: hfa384x_cmd: command was not "
 449		       "completed (res=%d, entry=%p, type=%d, cmd=0x%04x, "
 450		       "param0=0x%04x, EVSTAT=%04x INTEN=%04x)\n", dev->name,
 451		       res, entry, entry->type, entry->cmd, entry->param0, reg,
 452		       HFA384X_INW(HFA384X_INTEN_OFF));
 453		if (reg & HFA384X_EV_CMD) {
 454			/* Command completion event is pending, but the
 455			 * interrupt was not delivered - probably an issue
 456			 * with pcmcia-cs configuration. */
 457			printk(KERN_WARNING "%s: interrupt delivery does not "
 458			       "seem to work\n", dev->name);
 459		}
 460		prism2_io_debug_error(dev, 3);
 461		res = -ETIMEDOUT;
 462		goto done;
 463	}
 464
 465	if (resp0 != NULL)
 466		*resp0 = entry->resp0;
 467#ifndef final_version
 468	if (entry->res) {
 469		printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x, "
 470		       "resp0=0x%04x\n",
 471		       dev->name, cmd, entry->res, entry->resp0);
 472	}
 473#endif /* final_version */
 474
 475	res = entry->res;
 476 done:
 477	hostap_cmd_queue_free(local, entry, 1);
 478	return res;
 479}
 480
 481
 482/**
 483 * hfa384x_cmd_callback - Issue a Prism2 command; callback when completed
 484 * @dev: pointer to net_device
 485 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
 486 * @param0: value for Param0 register
 487 * @callback: command completion callback function (%NULL = no callback)
 488 * @context: context data to be given to the callback function
 489 *
 490 * Issue given command (possibly after waiting in command queue) and use
 491 * callback function to indicate command completion. This can be called both
 492 * from user and interrupt context. The callback function will be called in
 493 * hardware IRQ context. It can be %NULL, when no function is called when
 494 * command is completed.
 495 */
 496static int hfa384x_cmd_callback(struct net_device *dev, u16 cmd, u16 param0,
 497				void (*callback)(struct net_device *dev,
 498						 long context, u16 resp0,
 499						 u16 status),
 500				long context)
 501{
 502	struct hostap_interface *iface;
 503	local_info_t *local;
 504	int issue, ret;
 505	unsigned long flags;
 506	struct hostap_cmd_queue *entry;
 507
 508	iface = netdev_priv(dev);
 509	local = iface->local;
 510
 511	if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN + 2) {
 512		printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
 513		       dev->name);
 514		return -1;
 515	}
 516
 517	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
 518	if (entry == NULL) {
 519		printk(KERN_DEBUG "%s: hfa384x_cmd_callback - kmalloc "
 520		       "failed\n", dev->name);
 521		return -ENOMEM;
 522	}
 523	atomic_set(&entry->usecnt, 1);
 524	entry->type = CMD_CALLBACK;
 525	entry->cmd = cmd;
 526	entry->param0 = param0;
 527	entry->callback = callback;
 528	entry->context = context;
 529
 530	spin_lock_irqsave(&local->cmdlock, flags);
 531	issue = list_empty(&local->cmd_queue);
 532	if (issue)
 533		entry->issuing = 1;
 534	list_add_tail(&entry->list, &local->cmd_queue);
 535	local->cmd_queue_len++;
 536	spin_unlock_irqrestore(&local->cmdlock, flags);
 537
 538	if (issue && hfa384x_cmd_issue(dev, entry))
 539		ret = -ETIMEDOUT;
 540	else
 541		ret = 0;
 542
 543	hostap_cmd_queue_free(local, entry, ret);
 544
 545	return ret;
 546}
 547
 548
 549/**
 550 * __hfa384x_cmd_no_wait - Issue a Prism2 command (private)
 551 * @dev: pointer to net_device
 552 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
 553 * @param0: value for Param0 register
 554 * @io_debug_num: I/O debug error number
 555 *
 556 * Shared helper function for hfa384x_cmd_wait() and hfa384x_cmd_no_wait().
 557 */
 558static int __hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd, u16 param0,
 559				 int io_debug_num)
 560{
 561	int tries;
 562	u16 reg;
 563
 564	/* wait until busy bit is clear; this should always be clear since the
 565	 * commands are serialized */
 566	tries = HFA384X_CMD_BUSY_TIMEOUT;
 567	while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
 568		tries--;
 569		udelay(1);
 570	}
 571	if (tries == 0) {
 572		reg = HFA384X_INW(HFA384X_CMD_OFF);
 573		prism2_io_debug_error(dev, io_debug_num);
 574		printk(KERN_DEBUG "%s: __hfa384x_cmd_no_wait(%d) - timeout - "
 575		       "reg=0x%04x\n", dev->name, io_debug_num, reg);
 576		return -ETIMEDOUT;
 577	}
 578
 579	/* write command */
 580	HFA384X_OUTW(param0, HFA384X_PARAM0_OFF);
 581	HFA384X_OUTW(cmd, HFA384X_CMD_OFF);
 582
 583	return 0;
 584}
 585
 586
 587/**
 588 * hfa384x_cmd_wait - Issue a Prism2 command and busy wait for completion
 589 * @dev: pointer to net_device
 590 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
 591 * @param0: value for Param0 register
 592 */
 593static int hfa384x_cmd_wait(struct net_device *dev, u16 cmd, u16 param0)
 594{
 595	int res, tries;
 596	u16 reg;
 597
 598	res = __hfa384x_cmd_no_wait(dev, cmd, param0, 4);
 599	if (res)
 600		return res;
 601
 602        /* wait for command completion */
 603	if ((cmd & HFA384X_CMDCODE_MASK) == HFA384X_CMDCODE_DOWNLOAD)
 604		tries = HFA384X_DL_COMPL_TIMEOUT;
 605	else
 606		tries = HFA384X_CMD_COMPL_TIMEOUT;
 607
 608        while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
 609               tries > 0) {
 610                tries--;
 611                udelay(10);
 612        }
 613        if (tries == 0) {
 614                reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
 615		prism2_io_debug_error(dev, 5);
 616                printk(KERN_DEBUG "%s: hfa384x_cmd_wait - timeout2 - "
 617		       "reg=0x%04x\n", dev->name, reg);
 618                return -ETIMEDOUT;
 619        }
 620
 621        res = (HFA384X_INW(HFA384X_STATUS_OFF) &
 622               (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) | BIT(9) |
 623                BIT(8))) >> 8;
 624#ifndef final_version
 625	if (res) {
 626		printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x\n",
 627		       dev->name, cmd, res);
 628	}
 629#endif
 630
 631	HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
 632
 633	return res;
 634}
 635
 636
 637/**
 638 * hfa384x_cmd_no_wait - Issue a Prism2 command; do not wait for completion
 639 * @dev: pointer to net_device
 640 * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
 641 * @param0: value for Param0 register
 642 */
 643static inline int hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd,
 644				      u16 param0)
 645{
 646	return __hfa384x_cmd_no_wait(dev, cmd, param0, 6);
 647}
 648
 649
 650/**
 651 * prism2_cmd_ev - Prism2 command completion event handler
 652 * @dev: pointer to net_device
 653 *
 654 * Interrupt handler for command completion events. Called by the main
 655 * interrupt handler in hardware IRQ context. Read Resp0 and status registers
 656 * from the hardware and ACK the event. Depending on the issued command type
 657 * either wake up the sleeping process that is waiting for command completion
 658 * or call the callback function. Issue the next command, if one is pending.
 659 */
 660static void prism2_cmd_ev(struct net_device *dev)
 661{
 662	struct hostap_interface *iface;
 663	local_info_t *local;
 664	struct hostap_cmd_queue *entry = NULL;
 665
 666	iface = netdev_priv(dev);
 667	local = iface->local;
 668
 669	spin_lock(&local->cmdlock);
 670	if (!list_empty(&local->cmd_queue)) {
 671		entry = list_entry(local->cmd_queue.next,
 672				   struct hostap_cmd_queue, list);
 673		atomic_inc(&entry->usecnt);
 674		list_del_init(&entry->list);
 675		local->cmd_queue_len--;
 676
 677		if (!entry->issued) {
 678			printk(KERN_DEBUG "%s: Command completion event, but "
 679			       "cmd not issued\n", dev->name);
 680			__hostap_cmd_queue_free(local, entry, 1);
 681			entry = NULL;
 682		}
 683	}
 684	spin_unlock(&local->cmdlock);
 685
 686	if (!entry) {
 687		HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
 688		printk(KERN_DEBUG "%s: Command completion event, but no "
 689		       "pending commands\n", dev->name);
 690		return;
 691	}
 692
 693	entry->resp0 = HFA384X_INW(HFA384X_RESP0_OFF);
 694	entry->res = (HFA384X_INW(HFA384X_STATUS_OFF) &
 695		      (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) |
 696		       BIT(9) | BIT(8))) >> 8;
 697	HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
 698
 699	/* TODO: rest of the CmdEv handling could be moved to tasklet */
 700	if (entry->type == CMD_SLEEP) {
 701		entry->type = CMD_COMPLETED;
 702		wake_up_interruptible(&entry->compl);
 703	} else if (entry->type == CMD_CALLBACK) {
 704		if (entry->callback)
 705			entry->callback(dev, entry->context, entry->resp0,
 706					entry->res);
 707	} else {
 708		printk(KERN_DEBUG "%s: Invalid command completion type %d\n",
 709		       dev->name, entry->type);
 710	}
 711	hostap_cmd_queue_free(local, entry, 1);
 712
 713	/* issue next command, if pending */
 714	entry = NULL;
 715	spin_lock(&local->cmdlock);
 716	if (!list_empty(&local->cmd_queue)) {
 717		entry = list_entry(local->cmd_queue.next,
 718				   struct hostap_cmd_queue, list);
 719		if (entry->issuing) {
 720			/* hfa384x_cmd() has already started issuing this
 721			 * command, so do not start here */
 722			entry = NULL;
 723		}
 724		if (entry)
 725			atomic_inc(&entry->usecnt);
 726	}
 727	spin_unlock(&local->cmdlock);
 728
 729	if (entry) {
 730		/* issue next command; if command issuing fails, remove the
 731		 * entry from cmd_queue */
 732		int res = hfa384x_cmd_issue(dev, entry);
 733		spin_lock(&local->cmdlock);
 734		__hostap_cmd_queue_free(local, entry, res);
 735		spin_unlock(&local->cmdlock);
 736	}
 737}
 738
 739
 740static int hfa384x_wait_offset(struct net_device *dev, u16 o_off)
 741{
 742	int tries = HFA384X_BAP_BUSY_TIMEOUT;
 743	int res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
 744
 745	while (res && tries > 0) {
 746		tries--;
 747		udelay(1);
 748		res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
 749	}
 750	return res;
 751}
 752
 753
 754/* Offset must be even */
 755static int hfa384x_setup_bap(struct net_device *dev, u16 bap, u16 id,
 756			     int offset)
 757{
 758	u16 o_off, s_off;
 759	int ret = 0;
 760
 761	if (offset % 2 || bap > 1)
 762		return -EINVAL;
 763
 764	if (bap == BAP1) {
 765		o_off = HFA384X_OFFSET1_OFF;
 766		s_off = HFA384X_SELECT1_OFF;
 767	} else {
 768		o_off = HFA384X_OFFSET0_OFF;
 769		s_off = HFA384X_SELECT0_OFF;
 770	}
 771
 772	if (hfa384x_wait_offset(dev, o_off)) {
 773		prism2_io_debug_error(dev, 7);
 774		printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout before\n",
 775		       dev->name);
 776		ret = -ETIMEDOUT;
 777		goto out;
 778	}
 779
 780	HFA384X_OUTW(id, s_off);
 781	HFA384X_OUTW(offset, o_off);
 782
 783	if (hfa384x_wait_offset(dev, o_off)) {
 784		prism2_io_debug_error(dev, 8);
 785		printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout after\n",
 786		       dev->name);
 787		ret = -ETIMEDOUT;
 788		goto out;
 789	}
 790#ifndef final_version
 791	if (HFA384X_INW(o_off) & HFA384X_OFFSET_ERR) {
 792		prism2_io_debug_error(dev, 9);
 793		printk(KERN_DEBUG "%s: hfa384x_setup_bap - offset error "
 794		       "(%d,0x04%x,%d); reg=0x%04x\n",
 795		       dev->name, bap, id, offset, HFA384X_INW(o_off));
 796		ret = -EINVAL;
 797	}
 798#endif
 799
 800 out:
 801	return ret;
 802}
 803
 804
 805static int hfa384x_get_rid(struct net_device *dev, u16 rid, void *buf, int len,
 806			   int exact_len)
 807{
 808	struct hostap_interface *iface;
 809	local_info_t *local;
 810	int res, rlen = 0;
 811	struct hfa384x_rid_hdr rec;
 812
 813	iface = netdev_priv(dev);
 814	local = iface->local;
 815
 816	if (local->no_pri) {
 817		printk(KERN_DEBUG "%s: cannot get RID %04x (len=%d) - no PRI "
 818		       "f/w\n", dev->name, rid, len);
 819		return -ENOTTY; /* Well.. not really correct, but return
 820				 * something unique enough.. */
 821	}
 822
 823	if ((local->func->card_present && !local->func->card_present(local)) ||
 824	    local->hw_downloading)
 825		return -ENODEV;
 826
 827	res = mutex_lock_interruptible(&local->rid_bap_mtx);
 828	if (res)
 829		return res;
 830
 831	res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS, rid, NULL, NULL);
 832	if (res) {
 833		printk(KERN_DEBUG "%s: hfa384x_get_rid: CMDCODE_ACCESS failed "
 834		       "(res=%d, rid=%04x, len=%d)\n",
 835		       dev->name, res, rid, len);
 836		mutex_unlock(&local->rid_bap_mtx);
 837		return res;
 838	}
 839
 840	spin_lock_bh(&local->baplock);
 841
 842	res = hfa384x_setup_bap(dev, BAP0, rid, 0);
 843	if (!res)
 844		res = hfa384x_from_bap(dev, BAP0, &rec, sizeof(rec));
 845
 846	if (le16_to_cpu(rec.len) == 0) {
 847		/* RID not available */
 848		res = -ENODATA;
 849	}
 850
 851	rlen = (le16_to_cpu(rec.len) - 1) * 2;
 852	if (!res && exact_len && rlen != len) {
 853		printk(KERN_DEBUG "%s: hfa384x_get_rid - RID len mismatch: "
 854		       "rid=0x%04x, len=%d (expected %d)\n",
 855		       dev->name, rid, rlen, len);
 856		res = -ENODATA;
 857	}
 858
 859	if (!res)
 860		res = hfa384x_from_bap(dev, BAP0, buf, len);
 861
 862	spin_unlock_bh(&local->baplock);
 863	mutex_unlock(&local->rid_bap_mtx);
 864
 865	if (res) {
 866		if (res != -ENODATA)
 867			printk(KERN_DEBUG "%s: hfa384x_get_rid (rid=%04x, "
 868			       "len=%d) - failed - res=%d\n", dev->name, rid,
 869			       len, res);
 870		if (res == -ETIMEDOUT)
 871			prism2_hw_reset(dev);
 872		return res;
 873	}
 874
 875	return rlen;
 876}
 877
 878
 879static int hfa384x_set_rid(struct net_device *dev, u16 rid, void *buf, int len)
 880{
 881	struct hostap_interface *iface;
 882	local_info_t *local;
 883	struct hfa384x_rid_hdr rec;
 884	int res;
 885
 886	iface = netdev_priv(dev);
 887	local = iface->local;
 888
 889	if (local->no_pri) {
 890		printk(KERN_DEBUG "%s: cannot set RID %04x (len=%d) - no PRI "
 891		       "f/w\n", dev->name, rid, len);
 892		return -ENOTTY; /* Well.. not really correct, but return
 893				 * something unique enough.. */
 894	}
 895
 896	if ((local->func->card_present && !local->func->card_present(local)) ||
 897	    local->hw_downloading)
 898		return -ENODEV;
 899
 900	rec.rid = cpu_to_le16(rid);
 901	/* RID len in words and +1 for rec.rid */
 902	rec.len = cpu_to_le16(len / 2 + len % 2 + 1);
 903
 904	res = mutex_lock_interruptible(&local->rid_bap_mtx);
 905	if (res)
 906		return res;
 907
 908	spin_lock_bh(&local->baplock);
 909	res = hfa384x_setup_bap(dev, BAP0, rid, 0);
 910	if (!res)
 911		res = hfa384x_to_bap(dev, BAP0, &rec, sizeof(rec));
 912	if (!res)
 913		res = hfa384x_to_bap(dev, BAP0, buf, len);
 914	spin_unlock_bh(&local->baplock);
 915
 916	if (res) {
 917		printk(KERN_DEBUG "%s: hfa384x_set_rid (rid=%04x, len=%d) - "
 918		       "failed - res=%d\n", dev->name, rid, len, res);
 919		mutex_unlock(&local->rid_bap_mtx);
 920		return res;
 921	}
 922
 923	res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS_WRITE, rid, NULL, NULL);
 924	mutex_unlock(&local->rid_bap_mtx);
 925
 926	if (res) {
 927		printk(KERN_DEBUG "%s: hfa384x_set_rid: CMDCODE_ACCESS_WRITE "
 928		       "failed (res=%d, rid=%04x, len=%d)\n",
 929		       dev->name, res, rid, len);
 930
 931		if (res == -ETIMEDOUT)
 932			prism2_hw_reset(dev);
 933	}
 934
 935	return res;
 936}
 937
 938
 939static void hfa384x_disable_interrupts(struct net_device *dev)
 940{
 941	/* disable interrupts and clear event status */
 942	HFA384X_OUTW(0, HFA384X_INTEN_OFF);
 943	HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
 944}
 945
 946
 947static void hfa384x_enable_interrupts(struct net_device *dev)
 948{
 949	/* ack pending events and enable interrupts from selected events */
 950	HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
 951	HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
 952}
 953
 954
 955static void hfa384x_events_no_bap0(struct net_device *dev)
 956{
 957	HFA384X_OUTW(HFA384X_EVENT_MASK & ~HFA384X_BAP0_EVENTS,
 958		     HFA384X_INTEN_OFF);
 959}
 960
 961
 962static void hfa384x_events_all(struct net_device *dev)
 963{
 964	HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
 965}
 966
 967
 968static void hfa384x_events_only_cmd(struct net_device *dev)
 969{
 970	HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_INTEN_OFF);
 971}
 972
 973
 974static u16 hfa384x_allocate_fid(struct net_device *dev, int len)
 975{
 976	u16 fid;
 977	unsigned long delay;
 978
 979	/* FIX: this could be replace with hfa384x_cmd() if the Alloc event
 980	 * below would be handled like CmdCompl event (sleep here, wake up from
 981	 * interrupt handler */
 982	if (hfa384x_cmd_wait(dev, HFA384X_CMDCODE_ALLOC, len)) {
 983		printk(KERN_DEBUG "%s: cannot allocate fid, len=%d\n",
 984		       dev->name, len);
 985		return 0xffff;
 986	}
 987
 988	delay = jiffies + HFA384X_ALLOC_COMPL_TIMEOUT;
 989	while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC) &&
 990	       time_before(jiffies, delay))
 991		yield();
 992	if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC)) {
 993		printk("%s: fid allocate, len=%d - timeout\n", dev->name, len);
 994		return 0xffff;
 995	}
 996
 997	fid = HFA384X_INW(HFA384X_ALLOCFID_OFF);
 998	HFA384X_OUTW(HFA384X_EV_ALLOC, HFA384X_EVACK_OFF);
 999
1000	return fid;
1001}
1002
1003
1004static int prism2_reset_port(struct net_device *dev)
1005{
1006	struct hostap_interface *iface;
1007	local_info_t *local;
1008	int res;
1009
1010	iface = netdev_priv(dev);
1011	local = iface->local;
1012
1013	if (!local->dev_enabled)
1014		return 0;
1015
1016	res = hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0,
1017			  NULL, NULL);
1018	if (res)
1019		printk(KERN_DEBUG "%s: reset port failed to disable port\n",
1020		       dev->name);
1021	else {
1022		res = hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0,
1023				  NULL, NULL);
1024		if (res)
1025			printk(KERN_DEBUG "%s: reset port failed to enable "
1026			       "port\n", dev->name);
1027	}
1028
1029	/* It looks like at least some STA firmware versions reset
1030	 * fragmentation threshold back to 2346 after enable command. Restore
1031	 * the configured value, if it differs from this default. */
1032	if (local->fragm_threshold != 2346 &&
1033	    hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
1034			    local->fragm_threshold)) {
1035		printk(KERN_DEBUG "%s: failed to restore fragmentation "
1036		       "threshold (%d) after Port0 enable\n",
1037		       dev->name, local->fragm_threshold);
1038	}
1039
1040	/* Some firmwares lose antenna selection settings on reset */
1041	(void) hostap_set_antsel(local);
1042
1043	return res;
1044}
1045
1046
1047static int prism2_get_version_info(struct net_device *dev, u16 rid,
1048				   const char *txt)
1049{
1050	struct hfa384x_comp_ident comp;
1051	struct hostap_interface *iface;
1052	local_info_t *local;
1053
1054	iface = netdev_priv(dev);
1055	local = iface->local;
1056
1057	if (local->no_pri) {
1058		/* PRI f/w not yet available - cannot read RIDs */
1059		return -1;
1060	}
1061	if (hfa384x_get_rid(dev, rid, &comp, sizeof(comp), 1) < 0) {
1062		printk(KERN_DEBUG "Could not get RID for component %s\n", txt);
1063		return -1;
1064	}
1065
1066	printk(KERN_INFO "%s: %s: id=0x%02x v%d.%d.%d\n", dev->name, txt,
1067	       __le16_to_cpu(comp.id), __le16_to_cpu(comp.major),
1068	       __le16_to_cpu(comp.minor), __le16_to_cpu(comp.variant));
1069	return 0;
1070}
1071
1072
1073static int prism2_setup_rids(struct net_device *dev)
1074{
1075	struct hostap_interface *iface;
1076	local_info_t *local;
1077	__le16 tmp;
1078	int ret = 0;
1079
1080	iface = netdev_priv(dev);
1081	local = iface->local;
1082
1083	hostap_set_word(dev, HFA384X_RID_TICKTIME, 2000);
1084
1085	if (!local->fw_ap) {
1086		u16 tmp1 = hostap_get_porttype(local);
1087		ret = hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE, tmp1);
1088		if (ret) {
1089			printk("%s: Port type setting to %d failed\n",
1090			       dev->name, tmp1);
1091			goto fail;
1092		}
1093	}
1094
1095	/* Setting SSID to empty string seems to kill the card in Host AP mode
1096	 */
1097	if (local->iw_mode != IW_MODE_MASTER || local->essid[0] != '\0') {
1098		ret = hostap_set_string(dev, HFA384X_RID_CNFOWNSSID,
1099					local->essid);
1100		if (ret) {
1101			printk("%s: AP own SSID setting failed\n", dev->name);
1102			goto fail;
1103		}
1104	}
1105
1106	ret = hostap_set_word(dev, HFA384X_RID_CNFMAXDATALEN,
1107			      PRISM2_DATA_MAXLEN);
1108	if (ret) {
1109		printk("%s: MAC data length setting to %d failed\n",
1110		       dev->name, PRISM2_DATA_MAXLEN);
1111		goto fail;
1112	}
1113
1114	if (hfa384x_get_rid(dev, HFA384X_RID_CHANNELLIST, &tmp, 2, 1) < 0) {
1115		printk("%s: Channel list read failed\n", dev->name);
1116		ret = -EINVAL;
1117		goto fail;
1118	}
1119	local->channel_mask = le16_to_cpu(tmp);
1120
1121	if (local->channel < 1 || local->channel > 14 ||
1122	    !(local->channel_mask & (1 << (local->channel - 1)))) {
1123		printk(KERN_WARNING "%s: Channel setting out of range "
1124		       "(%d)!\n", dev->name, local->channel);
1125		ret = -EBUSY;
1126		goto fail;
1127	}
1128
1129	ret = hostap_set_word(dev, HFA384X_RID_CNFOWNCHANNEL, local->channel);
1130	if (ret) {
1131		printk("%s: Channel setting to %d failed\n",
1132		       dev->name, local->channel);
1133		goto fail;
1134	}
1135
1136	ret = hostap_set_word(dev, HFA384X_RID_CNFBEACONINT,
1137			      local->beacon_int);
1138	if (ret) {
1139		printk("%s: Beacon interval setting to %d failed\n",
1140		       dev->name, local->beacon_int);
1141		/* this may fail with Symbol/Lucent firmware */
1142		if (ret == -ETIMEDOUT)
1143			goto fail;
1144	}
1145
1146	ret = hostap_set_word(dev, HFA384X_RID_CNFOWNDTIMPERIOD,
1147			      local->dtim_period);
1148	if (ret) {
1149		printk("%s: DTIM period setting to %d failed\n",
1150		       dev->name, local->dtim_period);
1151		/* this may fail with Symbol/Lucent firmware */
1152		if (ret == -ETIMEDOUT)
1153			goto fail;
1154	}
1155
1156	ret = hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
1157			      local->is_promisc);
1158	if (ret)
1159		printk(KERN_INFO "%s: Setting promiscuous mode (%d) failed\n",
1160		       dev->name, local->is_promisc);
1161
1162	if (!local->fw_ap) {
1163		ret = hostap_set_string(dev, HFA384X_RID_CNFDESIREDSSID,
1164					local->essid);
1165		if (ret) {
1166			printk("%s: Desired SSID setting failed\n", dev->name);
1167			goto fail;
1168		}
1169	}
1170
1171	/* Setup TXRateControl, defaults to allow use of 1, 2, 5.5, and
1172	 * 11 Mbps in automatic TX rate fallback and 1 and 2 Mbps as basic
1173	 * rates */
1174	if (local->tx_rate_control == 0) {
1175		local->tx_rate_control =
1176			HFA384X_RATES_1MBPS |
1177			HFA384X_RATES_2MBPS |
1178			HFA384X_RATES_5MBPS |
1179			HFA384X_RATES_11MBPS;
1180	}
1181	if (local->basic_rates == 0)
1182		local->basic_rates = HFA384X_RATES_1MBPS | HFA384X_RATES_2MBPS;
1183
1184	if (!local->fw_ap) {
1185		ret = hostap_set_word(dev, HFA384X_RID_TXRATECONTROL,
1186				      local->tx_rate_control);
1187		if (ret) {
1188			printk("%s: TXRateControl setting to %d failed\n",
1189			       dev->name, local->tx_rate_control);
1190			goto fail;
1191		}
1192
1193		ret = hostap_set_word(dev, HFA384X_RID_CNFSUPPORTEDRATES,
1194				      local->tx_rate_control);
1195		if (ret) {
1196			printk("%s: cnfSupportedRates setting to %d failed\n",
1197			       dev->name, local->tx_rate_control);
1198		}
1199
1200		ret = hostap_set_word(dev, HFA384X_RID_CNFBASICRATES,
1201				      local->basic_rates);
1202		if (ret) {
1203			printk("%s: cnfBasicRates setting to %d failed\n",
1204			       dev->name, local->basic_rates);
1205		}
1206
1207		ret = hostap_set_word(dev, HFA384X_RID_CREATEIBSS, 1);
1208		if (ret) {
1209			printk("%s: Create IBSS setting to 1 failed\n",
1210			       dev->name);
1211		}
1212	}
1213
1214	if (local->name_set)
1215		(void) hostap_set_string(dev, HFA384X_RID_CNFOWNNAME,
1216					 local->name);
1217
1218	if (hostap_set_encryption(local)) {
1219		printk(KERN_INFO "%s: could not configure encryption\n",
1220		       dev->name);
1221	}
1222
1223	(void) hostap_set_antsel(local);
1224
1225	if (hostap_set_roaming(local)) {
1226		printk(KERN_INFO "%s: could not set host roaming\n",
1227		       dev->name);
1228	}
1229
1230	if (local->sta_fw_ver >= PRISM2_FW_VER(1,6,3) &&
1231	    hostap_set_word(dev, HFA384X_RID_CNFENHSECURITY, local->enh_sec))
1232		printk(KERN_INFO "%s: cnfEnhSecurity setting to 0x%x failed\n",
1233		       dev->name, local->enh_sec);
1234
1235	/* 32-bit tallies were added in STA f/w 0.8.0, but they were apparently
1236	 * not working correctly (last seven counters report bogus values).
1237	 * This has been fixed in 0.8.2, so enable 32-bit tallies only
1238	 * beginning with that firmware version. Another bug fix for 32-bit
1239	 * tallies in 1.4.0; should 16-bit tallies be used for some other
1240	 * versions, too? */
1241	if (local->sta_fw_ver >= PRISM2_FW_VER(0,8,2)) {
1242		if (hostap_set_word(dev, HFA384X_RID_CNFTHIRTY2TALLY, 1)) {
1243			printk(KERN_INFO "%s: cnfThirty2Tally setting "
1244			       "failed\n", dev->name);
1245			local->tallies32 = 0;
1246		} else
1247			local->tallies32 = 1;
1248	} else
1249		local->tallies32 = 0;
1250
1251	hostap_set_auth_algs(local);
1252
1253	if (hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
1254			    local->fragm_threshold)) {
1255		printk(KERN_INFO "%s: setting FragmentationThreshold to %d "
1256		       "failed\n", dev->name, local->fragm_threshold);
1257	}
1258
1259	if (hostap_set_word(dev, HFA384X_RID_RTSTHRESHOLD,
1260			    local->rts_threshold)) {
1261		printk(KERN_INFO "%s: setting RTSThreshold to %d failed\n",
1262		       dev->name, local->rts_threshold);
1263	}
1264
1265	if (local->manual_retry_count >= 0 &&
1266	    hostap_set_word(dev, HFA384X_RID_CNFALTRETRYCOUNT,
1267			    local->manual_retry_count)) {
1268		printk(KERN_INFO "%s: setting cnfAltRetryCount to %d failed\n",
1269		       dev->name, local->manual_retry_count);
1270	}
1271
1272	if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1) &&
1273	    hfa384x_get_rid(dev, HFA384X_RID_CNFDBMADJUST, &tmp, 2, 1) == 2) {
1274		local->rssi_to_dBm = le16_to_cpu(tmp);
1275	}
1276
1277	if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->wpa &&
1278	    hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE, 1)) {
1279		printk(KERN_INFO "%s: setting ssnHandlingMode to 1 failed\n",
1280		       dev->name);
1281	}
1282
1283	if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->generic_elem &&
1284	    hfa384x_set_rid(dev, HFA384X_RID_GENERICELEMENT,
1285			    local->generic_elem, local->generic_elem_len)) {
1286		printk(KERN_INFO "%s: setting genericElement failed\n",
1287		       dev->name);
1288	}
1289
1290 fail:
1291	return ret;
1292}
1293
1294
1295static int prism2_hw_init(struct net_device *dev, int initial)
1296{
1297	struct hostap_interface *iface;
1298	local_info_t *local;
1299	int ret, first = 1;
1300	unsigned long start, delay;
1301
1302	PDEBUG(DEBUG_FLOW, "prism2_hw_init()\n");
1303
1304	iface = netdev_priv(dev);
1305	local = iface->local;
1306
1307	clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits);
1308
1309 init:
1310	/* initialize HFA 384x */
1311	ret = hfa384x_cmd_no_wait(dev, HFA384X_CMDCODE_INIT, 0);
1312	if (ret) {
1313		printk(KERN_INFO "%s: first command failed - assuming card "
1314		       "does not have primary firmware\n", dev_info);
1315	}
1316
1317	if (first && (HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
1318		/* EvStat has Cmd bit set in some cases, so retry once if no
1319		 * wait was needed */
1320		HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
1321		printk(KERN_DEBUG "%s: init command completed too quickly - "
1322		       "retrying\n", dev->name);
1323		first = 0;
1324		goto init;
1325	}
1326
1327	start = jiffies;
1328	delay = jiffies + HFA384X_INIT_TIMEOUT;
1329	while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
1330	       time_before(jiffies, delay))
1331		yield();
1332	if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
1333		printk(KERN_DEBUG "%s: assuming no Primary image in "
1334		       "flash - card initialization not completed\n",
1335		       dev_info);
1336		local->no_pri = 1;
1337#ifdef PRISM2_DOWNLOAD_SUPPORT
1338			if (local->sram_type == -1)
1339				local->sram_type = prism2_get_ram_size(local);
1340#endif /* PRISM2_DOWNLOAD_SUPPORT */
1341		return 1;
1342	}
1343	local->no_pri = 0;
1344	printk(KERN_DEBUG "prism2_hw_init: initialized in %lu ms\n",
1345	       (jiffies - start) * 1000 / HZ);
1346	HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
1347	return 0;
1348}
1349
1350
1351static int prism2_hw_init2(struct net_device *dev, int initial)
1352{
1353	struct hostap_interface *iface;
1354	local_info_t *local;
1355	int i;
1356
1357	iface = netdev_priv(dev);
1358	local = iface->local;
1359
1360#ifdef PRISM2_DOWNLOAD_SUPPORT
1361	kfree(local->pda);
1362	if (local->no_pri)
1363		local->pda = NULL;
1364	else
1365		local->pda = prism2_read_pda(dev);
1366#endif /* PRISM2_DOWNLOAD_SUPPORT */
1367
1368	hfa384x_disable_interrupts(dev);
1369
1370#ifndef final_version
1371	HFA384X_OUTW(HFA384X_MAGIC, HFA384X_SWSUPPORT0_OFF);
1372	if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
1373		printk("SWSUPPORT0 write/read failed: %04X != %04X\n",
1374		       HFA384X_INW(HFA384X_SWSUPPORT0_OFF), HFA384X_MAGIC);
1375		goto failed;
1376	}
1377#endif
1378
1379	if (initial || local->pri_only) {
1380		hfa384x_events_only_cmd(dev);
1381		/* get card version information */
1382		if (prism2_get_version_info(dev, HFA384X_RID_NICID, "NIC") ||
1383		    prism2_get_version_info(dev, HFA384X_RID_PRIID, "PRI")) {
1384			hfa384x_disable_interrupts(dev);
1385			goto failed;
1386		}
1387
1388		if (prism2_get_version_info(dev, HFA384X_RID_STAID, "STA")) {
1389			printk(KERN_DEBUG "%s: Failed to read STA f/w version "
1390			       "- only Primary f/w present\n", dev->name);
1391			local->pri_only = 1;
1392			return 0;
1393		}
1394		local->pri_only = 0;
1395		hfa384x_disable_interrupts(dev);
1396	}
1397
1398	/* FIX: could convert allocate_fid to use sleeping CmdCompl wait and
1399	 * enable interrupts before this. This would also require some sort of
1400	 * sleeping AllocEv waiting */
1401
1402	/* allocate TX FIDs */
1403	local->txfid_len = PRISM2_TXFID_LEN;
1404	for (i = 0; i < PRISM2_TXFID_COUNT; i++) {
1405		local->txfid[i] = hfa384x_allocate_fid(dev, local->txfid_len);
1406		if (local->txfid[i] == 0xffff && local->txfid_len > 1600) {
1407			local->txfid[i] = hfa384x_allocate_fid(dev, 1600);
1408			if (local->txfid[i] != 0xffff) {
1409				printk(KERN_DEBUG "%s: Using shorter TX FID "
1410				       "(1600 bytes)\n", dev->name);
1411				local->txfid_len = 1600;
1412			}
1413		}
1414		if (local->txfid[i] == 0xffff)
1415			goto failed;
1416		local->intransmitfid[i] = PRISM2_TXFID_EMPTY;
1417	}
1418
1419	hfa384x_events_only_cmd(dev);
1420
1421	if (initial) {
1422		struct list_head *ptr;
1423		prism2_check_sta_fw_version(local);
1424
1425		if (hfa384x_get_rid(dev, HFA384X_RID_CNFOWNMACADDR,
1426				    dev->dev_addr, 6, 1) < 0) {
1427			printk("%s: could not get own MAC address\n",
1428			       dev->name);
1429		}
1430		list_for_each(ptr, &local->hostap_interfaces) {
1431			iface = list_entry(ptr, struct hostap_interface, list);
1432			memcpy(iface->dev->dev_addr, dev->dev_addr, ETH_ALEN);
1433		}
1434	} else if (local->fw_ap)
1435		prism2_check_sta_fw_version(local);
1436
1437	prism2_setup_rids(dev);
1438
1439	/* MAC is now configured, but port 0 is not yet enabled */
1440	return 0;
1441
1442 failed:
1443	if (!local->no_pri)
1444		printk(KERN_WARNING "%s: Initialization failed\n", dev_info);
1445	return 1;
1446}
1447
1448
1449static int prism2_hw_enable(struct net_device *dev, int initial)
1450{
1451	struct hostap_interface *iface;
1452	local_info_t *local;
1453	int was_resetting;
1454
1455	iface = netdev_priv(dev);
1456	local = iface->local;
1457	was_resetting = local->hw_resetting;
1458
1459	if (hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0, NULL, NULL)) {
1460		printk("%s: MAC port 0 enabling failed\n", dev->name);
1461		return 1;
1462	}
1463
1464	local->hw_ready = 1;
1465	local->hw_reset_tries = 0;
1466	local->hw_resetting = 0;
1467	hfa384x_enable_interrupts(dev);
1468
1469	/* at least D-Link DWL-650 seems to require additional port reset
1470	 * before it starts acting as an AP, so reset port automatically
1471	 * here just in case */
1472	if (initial && prism2_reset_port(dev)) {
1473		printk("%s: MAC port 0 reseting failed\n", dev->name);
1474		return 1;
1475	}
1476
1477	if (was_resetting && netif_queue_stopped(dev)) {
1478		/* If hw_reset() was called during pending transmit, netif
1479		 * queue was stopped. Wake it up now since the wlan card has
1480		 * been resetted. */
1481		netif_wake_queue(dev);
1482	}
1483
1484	return 0;
1485}
1486
1487
1488static int prism2_hw_config(struct net_device *dev, int initial)
1489{
1490	struct hostap_interface *iface;
1491	local_info_t *local;
1492
1493	iface = netdev_priv(dev);
1494	local = iface->local;
1495
1496	if (local->hw_downloading)
1497		return 1;
1498
1499	if (prism2_hw_init(dev, initial)) {
1500		return local->no_pri ? 0 : 1;
1501	}
1502
1503	if (prism2_hw_init2(dev, initial))
1504		return 1;
1505
1506	/* Enable firmware if secondary image is loaded and at least one of the
1507	 * netdevices is up. */
1508	if (!local->pri_only &&
1509	    (initial == 0 || (initial == 2 && local->num_dev_open > 0))) {
1510		if (!local->dev_enabled)
1511			prism2_callback(local, PRISM2_CALLBACK_ENABLE);
1512		local->dev_enabled = 1;
1513		return prism2_hw_enable(dev, initial);
1514	}
1515
1516	return 0;
1517}
1518
1519
1520static void prism2_hw_shutdown(struct net_device *dev, int no_disable)
1521{
1522	struct hostap_interface *iface;
1523	local_info_t *local;
1524
1525	iface = netdev_priv(dev);
1526	local = iface->local;
1527
1528	/* Allow only command completion events during disable */
1529	hfa384x_events_only_cmd(dev);
1530
1531	local->hw_ready = 0;
1532	if (local->dev_enabled)
1533		prism2_callback(local, PRISM2_CALLBACK_DISABLE);
1534	local->dev_enabled = 0;
1535
1536	if (local->func->card_present && !local->func->card_present(local)) {
1537		printk(KERN_DEBUG "%s: card already removed or not configured "
1538		       "during shutdown\n", dev->name);
1539		return;
1540	}
1541
1542	if ((no_disable & HOSTAP_HW_NO_DISABLE) == 0 &&
1543	    hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0, NULL, NULL))
1544		printk(KERN_WARNING "%s: Shutdown failed\n", dev_info);
1545
1546	hfa384x_disable_interrupts(dev);
1547
1548	if (no_disable & HOSTAP_HW_ENABLE_CMDCOMPL)
1549		hfa384x_events_only_cmd(dev);
1550	else
1551		prism2_clear_cmd_queue(local);
1552}
1553
1554
1555static void prism2_hw_reset(struct net_device *dev)
1556{
1557	struct hostap_interface *iface;
1558	local_info_t *local;
1559
1560#if 0
1561	static long last_reset = 0;
1562
1563	/* do not reset card more than once per second to avoid ending up in a
1564	 * busy loop reseting the card */
1565	if (time_before_eq(jiffies, last_reset + HZ))
1566		return;
1567	last_reset = jiffies;
1568#endif
1569
1570	iface = netdev_priv(dev);
1571	local = iface->local;
1572
1573	if (in_interrupt()) {
1574		printk(KERN_DEBUG "%s: driver bug - prism2_hw_reset() called "
1575		       "in interrupt context\n", dev->name);
1576		return;
1577	}
1578
1579	if (local->hw_downloading)
1580		return;
1581
1582	if (local->hw_resetting) {
1583		printk(KERN_WARNING "%s: %s: already resetting card - "
1584		       "ignoring reset request\n", dev_info, dev->name);
1585		return;
1586	}
1587
1588	local->hw_reset_tries++;
1589	if (local->hw_reset_tries > 10) {
1590		printk(KERN_WARNING "%s: too many reset tries, skipping\n",
1591		       dev->name);
1592		return;
1593	}
1594
1595	printk(KERN_WARNING "%s: %s: resetting card\n", dev_info, dev->name);
1596	hfa384x_disable_interrupts(dev);
1597	local->hw_resetting = 1;
1598	if (local->func->cor_sreset) {
1599		/* Host system seems to hang in some cases with high traffic
1600		 * load or shared interrupts during COR sreset. Disable shared
1601		 * interrupts during reset to avoid these crashes. COS sreset
1602		 * takes quite a long time, so it is unfortunate that this
1603		 * seems to be needed. Anyway, I do not know of any better way
1604		 * of avoiding the crash. */
1605		disable_irq(dev->irq);
1606		local->func->cor_sreset(local);
1607		enable_irq(dev->irq);
1608	}
1609	prism2_hw_shutdown(dev, 1);
1610	prism2_hw_config(dev, 0);
1611	local->hw_resetting = 0;
1612
1613#ifdef PRISM2_DOWNLOAD_SUPPORT
1614	if (local->dl_pri) {
1615		printk(KERN_DEBUG "%s: persistent download of primary "
1616		       "firmware\n", dev->name);
1617		if (prism2_download_genesis(local, local->dl_pri) < 0)
1618			printk(KERN_WARNING "%s: download (PRI) failed\n",
1619			       dev->name);
1620	}
1621
1622	if (local->dl_sec) {
1623		printk(KERN_DEBUG "%s: persistent download of secondary "
1624		       "firmware\n", dev->name);
1625		if (prism2_download_volatile(local, local->dl_sec) < 0)
1626			printk(KERN_WARNING "%s: download (SEC) failed\n",
1627			       dev->name);
1628	}
1629#endif /* PRISM2_DOWNLOAD_SUPPORT */
1630
1631	/* TODO: restore beacon TIM bits for STAs that have buffered frames */
1632}
1633
1634
1635static void prism2_schedule_reset(local_info_t *local)
1636{
1637	schedule_work(&local->reset_queue);
1638}
1639
1640
1641/* Called only as scheduled task after noticing card timeout in interrupt
1642 * context */
1643static void handle_reset_queue(struct work_struct *work)
1644{
1645	local_info_t *local = container_of(work, local_info_t, reset_queue);
1646
1647	printk(KERN_DEBUG "%s: scheduled card reset\n", local->dev->name);
1648	prism2_hw_reset(local->dev);
1649
1650	if (netif_queue_stopped(local->dev)) {
1651		int i;
1652
1653		for (i = 0; i < PRISM2_TXFID_COUNT; i++)
1654			if (local->intransmitfid[i] == PRISM2_TXFID_EMPTY) {
1655				PDEBUG(DEBUG_EXTRA, "prism2_tx_timeout: "
1656				       "wake up queue\n");
1657				netif_wake_queue(local->dev);
1658				break;
1659			}
1660	}
1661}
1662
1663
1664static int prism2_get_txfid_idx(local_info_t *local)
1665{
1666	int idx, end;
1667	unsigned long flags;
1668
1669	spin_lock_irqsave(&local->txfidlock, flags);
1670	end = idx = local->next_txfid;
1671	do {
1672		if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
1673			local->intransmitfid[idx] = PRISM2_TXFID_RESERVED;
1674			spin_unlock_irqrestore(&local->txfidlock, flags);
1675			return idx;
1676		}
1677		idx++;
1678		if (idx >= PRISM2_TXFID_COUNT)
1679			idx = 0;
1680	} while (idx != end);
1681	spin_unlock_irqrestore(&local->txfidlock, flags);
1682
1683	PDEBUG(DEBUG_EXTRA2, "prism2_get_txfid_idx: no room in txfid buf: "
1684	       "packet dropped\n");
1685	local->dev->stats.tx_dropped++;
1686
1687	return -1;
1688}
1689
1690
1691/* Called only from hardware IRQ */
1692static void prism2_transmit_cb(struct net_device *dev, long context,
1693			       u16 resp0, u16 res)
1694{
1695	struct hostap_interface *iface;
1696	local_info_t *local;
1697	int idx = (int) context;
1698
1699	iface = netdev_priv(dev);
1700	local = iface->local;
1701
1702	if (res) {
1703		printk(KERN_DEBUG "%s: prism2_transmit_cb - res=0x%02x\n",
1704		       dev->name, res);
1705		return;
1706	}
1707
1708	if (idx < 0 || idx >= PRISM2_TXFID_COUNT) {
1709		printk(KERN_DEBUG "%s: prism2_transmit_cb called with invalid "
1710		       "idx=%d\n", dev->name, idx);
1711		return;
1712	}
1713
1714	if (!test_and_clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
1715		printk(KERN_DEBUG "%s: driver bug: prism2_transmit_cb called "
1716		       "with no pending transmit\n", dev->name);
1717	}
1718
1719	if (netif_queue_stopped(dev)) {
1720		/* ready for next TX, so wake up queue that was stopped in
1721		 * prism2_transmit() */
1722		netif_wake_queue(dev);
1723	}
1724
1725	spin_lock(&local->txfidlock);
1726
1727	/* With reclaim, Resp0 contains new txfid for transmit; the old txfid
1728	 * will be automatically allocated for the next TX frame */
1729	local->intransmitfid[idx] = resp0;
1730
1731	PDEBUG(DEBUG_FID, "%s: prism2_transmit_cb: txfid[%d]=0x%04x, "
1732	       "resp0=0x%04x, transmit_txfid=0x%04x\n",
1733	       dev->name, idx, local->txfid[idx],
1734	       resp0, local->intransmitfid[local->next_txfid]);
1735
1736	idx++;
1737	if (idx >= PRISM2_TXFID_COUNT)
1738		idx = 0;
1739	local->next_txfid = idx;
1740
1741	/* check if all TX buffers are occupied */
1742	do {
1743		if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
1744			spin_unlock(&local->txfidlock);
1745			return;
1746		}
1747		idx++;
1748		if (idx >= PRISM2_TXFID_COUNT)
1749			idx = 0;
1750	} while (idx != local->next_txfid);
1751	spin_unlock(&local->txfidlock);
1752
1753	/* no empty TX buffers, stop queue */
1754	netif_stop_queue(dev);
1755}
1756
1757
1758/* Called only from software IRQ if PCI bus master is not used (with bus master
1759 * this can be called both from software and hardware IRQ) */
1760static int prism2_transmit(struct net_device *dev, int idx)
1761{
1762	struct hostap_interface *iface;
1763	local_info_t *local;
1764	int res;
1765
1766	iface = netdev_priv(dev);
1767	local = iface->local;
1768
1769	/* The driver tries to stop netif queue so that there would not be
1770	 * more than one attempt to transmit frames going on; check that this
1771	 * is really the case */
1772
1773	if (test_and_set_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
1774		printk(KERN_DEBUG "%s: driver bug - prism2_transmit() called "
1775		       "when previous TX was pending\n", dev->name);
1776		return -1;
1777	}
1778
1779	/* stop the queue for the time that transmit is pending */
1780	netif_stop_queue(dev);
1781
1782	/* transmit packet */
1783	res = hfa384x_cmd_callback(
1784		dev,
1785		HFA384X_CMDCODE_TRANSMIT | HFA384X_CMD_TX_RECLAIM,
1786		local->txfid[idx],
1787		prism2_transmit_cb, (long) idx);
1788
1789	if (res) {
1790		printk(KERN_DEBUG "%s: prism2_transmit: CMDCODE_TRANSMIT "
1791		       "failed (res=%d)\n", dev

Large files files are truncated, but you can click here to view the full file