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