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