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