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