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