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