patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / net / wireless / prism54 / islpci_dev.c
1 /*
2  *  
3  *  Copyright (C) 2002 Intersil Americas Inc.
4  *  Copyright (C) 2003 Herbert Valerio Riedel <hvr@gnu.org>
5  *  Copyright (C) 2003 Luis R. Rodriguez <mcgrof@ruslug.rutgers.edu>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include <linux/version.h>
23 #include <linux/module.h>
24
25 #include <linux/netdevice.h>
26 #include <linux/pci.h>
27 #include <linux/etherdevice.h>
28 #include <linux/delay.h>
29 #include <linux/if_arp.h>
30
31 #include <asm/io.h>
32
33 #include "isl_38xx.h"
34 #include "isl_ioctl.h"
35 #include "islpci_dev.h"
36 #include "islpci_mgt.h"
37 #include "islpci_eth.h"
38 #include "oid_mgt.h"
39
40 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0)
41 #define prism54_synchronize_irq(irq) synchronize_irq()
42 #else
43 #define prism54_synchronize_irq(irq) synchronize_irq(irq)
44 #endif
45
46 #define ISL3877_IMAGE_FILE      "isl3877"
47 #define ISL3890_IMAGE_FILE      "isl3890"
48
49 /* Temporary dummy MAC address to use until firmware is loaded.
50  * The idea there is that some tools (such as nameif) may query
51  * the MAC address before the netdev is 'open'. By using a valid
52  * OUI prefix, they can process the netdev properly.
53  * Of course, this is not the final/real MAC address. It doesn't
54  * matter, as you are suppose to be able to change it anytime via
55  * ndev->set_mac_address. Jean II */
56 const unsigned char     dummy_mac[6] = { 0x00, 0x30, 0xB4, 0x00, 0x00, 0x00 };
57
58 /******************************************************************************
59     Device Interrupt Handler
60 ******************************************************************************/
61
62 irqreturn_t
63 islpci_interrupt(int irq, void *config, struct pt_regs *regs)
64 {
65         u32 reg;
66         islpci_private *priv = config;
67         struct net_device *ndev = priv->ndev;
68         void *device = priv->device_base;
69         int powerstate = ISL38XX_PSM_POWERSAVE_STATE;
70
71         /* received an interrupt request on a shared IRQ line
72          * first check whether the device is in sleep mode */
73         reg = readl(device + ISL38XX_CTRL_STAT_REG);
74         if (reg & ISL38XX_CTRL_STAT_SLEEPMODE)
75                 /* device is in sleep mode, IRQ was generated by someone else */
76         {
77                 printk(KERN_DEBUG "Assuming someone else called the IRQ\n");
78                 return IRQ_NONE;
79         }
80
81         if (islpci_get_state(priv) != PRV_STATE_SLEEP)
82                 powerstate = ISL38XX_PSM_ACTIVE_STATE;
83
84         /* lock the interrupt handler */
85         spin_lock(&priv->slock);
86
87         /* check whether there is any source of interrupt on the device */
88         reg = readl(device + ISL38XX_INT_IDENT_REG);
89
90         /* also check the contents of the Interrupt Enable Register, because this
91          * will filter out interrupt sources from other devices on the same irq ! */
92         reg &= readl(device + ISL38XX_INT_EN_REG);
93         reg &= ISL38XX_INT_SOURCES;
94
95         if (reg != 0) {
96                 /* reset the request bits in the Identification register */
97                 isl38xx_w32_flush(device, reg, ISL38XX_INT_ACK_REG);
98
99 #if VERBOSE > SHOW_ERROR_MESSAGES
100                 DEBUG(SHOW_FUNCTION_CALLS,
101                       "IRQ: Identification register 0x%p 0x%x \n", device, reg);
102 #endif
103
104                 /* check for each bit in the register separately */
105                 if (reg & ISL38XX_INT_IDENT_UPDATE) {
106 #if VERBOSE > SHOW_ERROR_MESSAGES
107                         /* Queue has been updated */
108                         DEBUG(SHOW_TRACING, "IRQ: Update flag \n");
109
110                         DEBUG(SHOW_QUEUE_INDEXES,
111                               "CB drv Qs: [%i][%i][%i][%i][%i][%i]\n",
112                               le32_to_cpu(priv->control_block->
113                                           driver_curr_frag[0]),
114                               le32_to_cpu(priv->control_block->
115                                           driver_curr_frag[1]),
116                               le32_to_cpu(priv->control_block->
117                                           driver_curr_frag[2]),
118                               le32_to_cpu(priv->control_block->
119                                           driver_curr_frag[3]),
120                               le32_to_cpu(priv->control_block->
121                                           driver_curr_frag[4]),
122                               le32_to_cpu(priv->control_block->
123                                           driver_curr_frag[5])
124                             );
125
126                         DEBUG(SHOW_QUEUE_INDEXES,
127                               "CB dev Qs: [%i][%i][%i][%i][%i][%i]\n",
128                               le32_to_cpu(priv->control_block->
129                                           device_curr_frag[0]),
130                               le32_to_cpu(priv->control_block->
131                                           device_curr_frag[1]),
132                               le32_to_cpu(priv->control_block->
133                                           device_curr_frag[2]),
134                               le32_to_cpu(priv->control_block->
135                                           device_curr_frag[3]),
136                               le32_to_cpu(priv->control_block->
137                                           device_curr_frag[4]),
138                               le32_to_cpu(priv->control_block->
139                                           device_curr_frag[5])
140                             );
141 #endif
142
143                         /* cleanup the data low transmit queue */
144                         islpci_eth_cleanup_transmit(priv, priv->control_block);
145
146                         /* device is in active state, update the
147                          * powerstate flag if necessary */
148                         powerstate = ISL38XX_PSM_ACTIVE_STATE;
149
150                         /* check all three queues in priority order
151                          * call the PIMFOR receive function until the
152                          * queue is empty */
153                         if (isl38xx_in_queue(priv->control_block,
154                                                 ISL38XX_CB_RX_MGMTQ) != 0) {
155 #if VERBOSE > SHOW_ERROR_MESSAGES
156                                 DEBUG(SHOW_TRACING,
157                                       "Received frame in Management Queue\n");
158 #endif
159                                 islpci_mgt_receive(ndev);
160
161                                 islpci_mgt_cleanup_transmit(ndev);
162
163                                 /* Refill slots in receive queue */
164                                 islpci_mgmt_rx_fill(ndev);
165
166                                 /* no need to trigger the device, next
167                                    islpci_mgt_transaction does it */
168                         }
169
170                         while (isl38xx_in_queue(priv->control_block,
171                                                 ISL38XX_CB_RX_DATA_LQ) != 0) {
172 #if VERBOSE > SHOW_ERROR_MESSAGES
173                                 DEBUG(SHOW_TRACING,
174                                       "Received frame in Data Low Queue \n");
175 #endif
176                                 islpci_eth_receive(priv);
177                         }
178
179                         /* check whether the data transmit queues were full */
180                         if (priv->data_low_tx_full) {
181                                 /* check whether the transmit is not full anymore */
182                                 if (ISL38XX_CB_TX_QSIZE -
183                                     isl38xx_in_queue(priv->control_block,
184                                                      ISL38XX_CB_TX_DATA_LQ) >=
185                                     ISL38XX_MIN_QTHRESHOLD) {
186                                         /* nope, the driver is ready for more network frames */
187                                         netif_wake_queue(priv->ndev);
188
189                                         /* reset the full flag */
190                                         priv->data_low_tx_full = 0;
191                                 }
192                         }
193                 }
194
195                 if (reg & ISL38XX_INT_IDENT_INIT) {
196                         /* Device has been initialized */
197 #if VERBOSE > SHOW_ERROR_MESSAGES
198                         DEBUG(SHOW_TRACING,
199                               "IRQ: Init flag, device initialized \n");
200 #endif
201                         wake_up(&priv->reset_done);
202                 }
203
204                 if (reg & ISL38XX_INT_IDENT_SLEEP) {
205                         /* Device intends to move to powersave state */
206 #if VERBOSE > SHOW_ERROR_MESSAGES
207                         DEBUG(SHOW_TRACING, "IRQ: Sleep flag \n");
208 #endif
209                         isl38xx_handle_sleep_request(priv->control_block,
210                                                      &powerstate,
211                                                      priv->device_base);
212                 }
213
214                 if (reg & ISL38XX_INT_IDENT_WAKEUP) {
215                         /* Device has been woken up to active state */
216 #if VERBOSE > SHOW_ERROR_MESSAGES
217                         DEBUG(SHOW_TRACING, "IRQ: Wakeup flag \n");
218 #endif
219
220                         isl38xx_handle_wakeup(priv->control_block,
221                                               &powerstate, priv->device_base);
222                 }
223         }
224
225         /* sleep -> ready */
226         if (islpci_get_state(priv) == PRV_STATE_SLEEP
227             && powerstate == ISL38XX_PSM_ACTIVE_STATE)
228                 islpci_set_state(priv, PRV_STATE_READY);
229
230         /* !sleep -> sleep */
231         if (islpci_get_state(priv) != PRV_STATE_SLEEP
232             && powerstate == ISL38XX_PSM_POWERSAVE_STATE)
233                 islpci_set_state(priv, PRV_STATE_SLEEP);
234
235         /* unlock the interrupt handler */
236         spin_unlock(&priv->slock);
237
238         return IRQ_HANDLED;
239 }
240
241 /******************************************************************************
242     Network Interface Control & Statistical functions
243 ******************************************************************************/
244 static int
245 islpci_open(struct net_device *ndev)
246 {
247         u32 rc;
248         islpci_private *priv = netdev_priv(ndev);
249
250         printk(KERN_DEBUG "%s: islpci_open()\n", ndev->name);
251
252         /* reset data structures, upload firmware and reset device */
253         rc = islpci_reset(priv,1);
254         if (rc) {
255                 prism54_bring_down(priv);
256                 return rc; /* Returns informative message */
257         }
258
259         netif_start_queue(ndev);
260 /*      netif_mark_up( ndev ); */
261
262         return 0;
263 }
264
265 static int
266 islpci_close(struct net_device *ndev)
267 {
268         islpci_private *priv = netdev_priv(ndev);
269
270         printk(KERN_DEBUG "%s: islpci_close ()\n", ndev->name);
271
272         netif_stop_queue(ndev);
273
274         return prism54_bring_down(priv);
275 }
276
277 int
278 prism54_bring_down(islpci_private *priv)
279 {
280         void *device_base = priv->device_base;
281         u32 reg;
282         /* we are going to shutdown the device */
283         islpci_set_state(priv, PRV_STATE_PREBOOT);
284
285         /* disable all device interrupts in case they weren't */
286         isl38xx_disable_interrupts(priv->device_base);  
287
288         /* For safety reasons, we may want to ensure that no DMA transfer is
289          * currently in progress by emptying the TX and RX queues. */
290
291         /* wait until interrupts have finished executing on other CPUs */
292         prism54_synchronize_irq(priv->pdev->irq);
293
294         reg = readl(device_base + ISL38XX_CTRL_STAT_REG);
295         reg &= ~(ISL38XX_CTRL_STAT_RESET | ISL38XX_CTRL_STAT_RAMBOOT);
296         writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
297         wmb();
298         udelay(ISL38XX_WRITEIO_DELAY);
299
300         reg |= ISL38XX_CTRL_STAT_RESET;
301         writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
302         wmb();
303         udelay(ISL38XX_WRITEIO_DELAY);
304
305         /* clear the Reset bit */
306         reg &= ~ISL38XX_CTRL_STAT_RESET;
307         writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
308         wmb();
309
310         /* wait a while for the device to reset */
311         set_current_state(TASK_UNINTERRUPTIBLE);
312         schedule_timeout(50*HZ/1000);
313
314         return 0;
315 }
316
317 static int
318 islpci_upload_fw(islpci_private *priv)
319 {
320         islpci_state_t old_state;
321         u32 rc;
322
323         old_state = islpci_set_state(priv, PRV_STATE_BOOT);
324
325         printk(KERN_DEBUG "%s: uploading firmware...\n", priv->ndev->name);
326
327         rc = isl38xx_upload_firmware(priv->firmware,
328 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,75))
329                 &priv->pdev->dev,
330 #else
331                 pci_name(priv->pdev),
332 #endif
333                 priv->device_base,
334                 priv->device_host_address);
335         if (rc) {
336                 /* error uploading the firmware */
337                 printk(KERN_ERR "%s: could not upload firmware ('%s')\n",
338                        priv->ndev->name, priv->firmware);
339
340                 islpci_set_state(priv, old_state);
341                 return rc;
342         }
343
344         printk(KERN_DEBUG
345                "%s: firmware uploaded done, now triggering reset...\n",
346                priv->ndev->name);
347
348         islpci_set_state(priv, PRV_STATE_POSTBOOT);
349
350         return 0;
351 }
352
353 static int
354 islpci_reset_if(islpci_private *priv)
355 {
356         long remaining;
357         int result = -ETIME;
358         int count;
359
360 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
361         /* This is 2.6 specific, nicer, shorter, but not in 2.4 yet */
362         DEFINE_WAIT(wait);
363         prepare_to_wait(&priv->reset_done, &wait, TASK_UNINTERRUPTIBLE);
364 #else
365         DECLARE_WAITQUEUE(wait, current);
366         set_current_state(TASK_UNINTERRUPTIBLE);
367         add_wait_queue(&priv->reset_done, &wait);
368 #endif
369         
370         /* now the last step is to reset the interface */
371         isl38xx_interface_reset(priv->device_base, priv->device_host_address);
372         islpci_set_state(priv, PRV_STATE_PREINIT);
373
374         for(count = 0; count < 2 && result; count++) {
375                 /* The software reset acknowledge needs about 220 msec here.
376                  * Be conservative and wait for up to one second. */
377         
378                 remaining = schedule_timeout(HZ);
379
380                 if(remaining > 0) {
381                         result = 0;
382                         break;
383                 }
384
385                 /* If we're here it's because our IRQ hasn't yet gone through. 
386                  * Retry a bit more...
387                  */
388                  printk(KERN_ERR "%s: device soft reset timed out\n",
389                        priv->ndev->name);
390
391         }
392
393 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
394         /* 2.6 specific too */
395         finish_wait(&priv->reset_done, &wait);
396 #else
397         remove_wait_queue(&priv->reset_done, &wait);
398         set_current_state(TASK_RUNNING);
399 #endif
400
401         if(result)
402                 return result;
403
404         islpci_set_state(priv, PRV_STATE_INIT);
405
406         /* Now that the device is 100% up, let's allow
407          * for the other interrupts --
408          * NOTE: this is not *yet* true since we've only allowed the 
409          * INIT interrupt on the IRQ line. We can perhaps poll
410          * the IRQ line until we know for sure the reset went through */
411         isl38xx_enable_common_interrupts(priv->device_base);
412
413         prism54_mib_init_work(priv);
414
415         islpci_set_state(priv, PRV_STATE_READY);
416
417         return 0;
418 }
419
420 int
421 islpci_reset(islpci_private *priv, int reload_firmware)
422 {
423         isl38xx_control_block *cb =    /* volatile not needed */
424                 (isl38xx_control_block *) priv->control_block;
425         unsigned counter;
426         int rc;
427
428         if (reload_firmware)
429                 islpci_set_state(priv, PRV_STATE_PREBOOT);
430         else
431                 islpci_set_state(priv, PRV_STATE_POSTBOOT);
432
433         printk(KERN_DEBUG "%s: resetting device...\n", priv->ndev->name);
434
435         /* disable all device interrupts in case they weren't */
436         isl38xx_disable_interrupts(priv->device_base);
437
438         /* flush all management queues */
439         priv->index_mgmt_tx = 0;
440         priv->index_mgmt_rx = 0;
441
442         /* clear the indexes in the frame pointer */
443         for (counter = 0; counter < ISL38XX_CB_QCOUNT; counter++) {
444                 cb->driver_curr_frag[counter] = cpu_to_le32(0);
445                 cb->device_curr_frag[counter] = cpu_to_le32(0);
446         }
447
448         /* reset the mgmt receive queue */
449         for (counter = 0; counter < ISL38XX_CB_MGMT_QSIZE; counter++) {
450                 isl38xx_fragment *frag = &cb->rx_data_mgmt[counter];
451                 frag->size = MGMT_FRAME_SIZE;
452                 frag->flags = 0;
453                 frag->address = priv->mgmt_rx[counter].pci_addr;
454         }
455
456         for (counter = 0; counter < ISL38XX_CB_RX_QSIZE; counter++) {
457                 cb->rx_data_low[counter].address =
458                     cpu_to_le32((u32) priv->pci_map_rx_address[counter]);
459         }
460
461         /* since the receive queues are filled with empty fragments, now we can
462          * set the corresponding indexes in the Control Block */
463         priv->control_block->driver_curr_frag[ISL38XX_CB_RX_DATA_LQ] =
464             cpu_to_le32(ISL38XX_CB_RX_QSIZE);
465         priv->control_block->driver_curr_frag[ISL38XX_CB_RX_MGMTQ] =
466             cpu_to_le32(ISL38XX_CB_MGMT_QSIZE);
467
468         /* reset the remaining real index registers and full flags */
469         priv->free_data_rx = 0;
470         priv->free_data_tx = 0;
471         priv->data_low_tx_full = 0;
472
473         if (reload_firmware) { /* Should we load the firmware ? */
474         /* now that the data structures are cleaned up, upload
475          * firmware and reset interface */
476                 rc = islpci_upload_fw(priv);
477                 if (rc) 
478                         return rc;
479         }
480
481         /* finally reset interface */
482         rc = islpci_reset_if(priv);
483         if (!rc) /* If successful */
484                 return rc;
485         
486         printk(KERN_DEBUG  "prism54: Your card/socket may be faulty, or IRQ line too busy :(\n");
487         return rc;
488
489 }
490
491 struct net_device_stats *
492 islpci_statistics(struct net_device *ndev)
493 {
494         islpci_private *priv = netdev_priv(ndev);
495
496 #if VERBOSE > SHOW_ERROR_MESSAGES
497         DEBUG(SHOW_FUNCTION_CALLS, "islpci_statistics \n");
498 #endif
499
500         return &priv->statistics;
501 }
502
503 /******************************************************************************
504     Network device configuration functions
505 ******************************************************************************/
506 int
507 islpci_alloc_memory(islpci_private *priv)
508 {
509         int counter;
510
511 #if VERBOSE > SHOW_ERROR_MESSAGES
512         printk(KERN_DEBUG "islpci_alloc_memory\n");
513 #endif
514
515         /* remap the PCI device base address to accessable */
516         if (!(priv->device_base =
517               ioremap(pci_resource_start(priv->pdev, 0),
518                       ISL38XX_PCI_MEM_SIZE))) {
519                 /* error in remapping the PCI device memory address range */
520                 printk(KERN_ERR "PCI memory remapping failed \n");
521                 return -1;
522         }
523
524         /* memory layout for consistent DMA region:
525          *
526          * Area 1: Control Block for the device interface
527          * Area 2: Power Save Mode Buffer for temporary frame storage. Be aware that
528          *         the number of supported stations in the AP determines the minimal
529          *         size of the buffer !
530          */
531
532         /* perform the allocation */
533         priv->driver_mem_address = pci_alloc_consistent(priv->pdev,
534                                                         HOST_MEM_BLOCK,
535                                                         &priv->
536                                                         device_host_address);
537
538         if (!priv->driver_mem_address) {
539                 /* error allocating the block of PCI memory */
540                 printk(KERN_ERR "%s: could not allocate DMA memory, aborting!",
541                        "prism54");
542                 return -1;
543         }
544
545         /* assign the Control Block to the first address of the allocated area */
546         priv->control_block =
547             (isl38xx_control_block *) priv->driver_mem_address;
548
549         /* set the Power Save Buffer pointer directly behind the CB */
550         priv->device_psm_buffer =
551                 priv->device_host_address + CONTROL_BLOCK_SIZE;
552
553         /* make sure all buffer pointers are initialized */
554         for (counter = 0; counter < ISL38XX_CB_QCOUNT; counter++) {
555                 priv->control_block->driver_curr_frag[counter] = cpu_to_le32(0);
556                 priv->control_block->device_curr_frag[counter] = cpu_to_le32(0);
557         }
558
559         priv->index_mgmt_rx = 0;
560         memset(priv->mgmt_rx, 0, sizeof(priv->mgmt_rx));
561         memset(priv->mgmt_tx, 0, sizeof(priv->mgmt_tx));
562
563         /* allocate rx queue for management frames */
564         if (islpci_mgmt_rx_fill(priv->ndev) < 0)
565                 goto out_free;
566
567         /* now get the data rx skb's */
568         memset(priv->data_low_rx, 0, sizeof (priv->data_low_rx));
569         memset(priv->pci_map_rx_address, 0, sizeof (priv->pci_map_rx_address));
570
571         for (counter = 0; counter < ISL38XX_CB_RX_QSIZE; counter++) {
572                 struct sk_buff *skb;
573
574                 /* allocate an sk_buff for received data frames storage
575                  * each frame on receive size consists of 1 fragment
576                  * include any required allignment operations */
577                 if (!(skb = dev_alloc_skb(MAX_FRAGMENT_SIZE_RX + 2))) {
578                         /* error allocating an sk_buff structure elements */
579                         printk(KERN_ERR "Error allocating skb.\n");
580                         skb = NULL;
581                         goto out_free;
582                 }
583                 /* add the new allocated sk_buff to the buffer array */
584                 priv->data_low_rx[counter] = skb;
585
586                 /* map the allocated skb data area to pci */
587                 priv->pci_map_rx_address[counter] =
588                     pci_map_single(priv->pdev, (void *) skb->data,
589                                    MAX_FRAGMENT_SIZE_RX + 2,
590                                    PCI_DMA_FROMDEVICE);
591                 if (!priv->pci_map_rx_address[counter]) {
592                         /* error mapping the buffer to device
593                            accessable memory address */
594                         printk(KERN_ERR "failed to map skb DMA'able\n");
595                         goto out_free;
596                 }
597         }
598
599         prism54_acl_init(&priv->acl);
600         prism54_wpa_ie_init(priv);
601         if (mgt_init(priv)) 
602                 goto out_free;
603
604         return 0;
605  out_free:
606         islpci_free_memory(priv);
607         return -1;
608 }
609
610 int
611 islpci_free_memory(islpci_private *priv)
612 {
613         int counter;
614
615         if (priv->device_base)
616                 iounmap(priv->device_base);
617         priv->device_base = 0;
618
619         /* free consistent DMA area... */
620         if (priv->driver_mem_address)
621                 pci_free_consistent(priv->pdev, HOST_MEM_BLOCK,
622                                     priv->driver_mem_address,
623                                     priv->device_host_address);
624
625         /* clear some dangling pointers */
626         priv->driver_mem_address = 0;
627         priv->device_host_address = 0;
628         priv->device_psm_buffer = 0;
629         priv->control_block = 0;
630
631         /* clean up mgmt rx buffers */
632         for (counter = 0; counter < ISL38XX_CB_MGMT_QSIZE; counter++) {
633                 struct islpci_membuf *buf = &priv->mgmt_rx[counter];
634                 if (buf->pci_addr)
635                         pci_unmap_single(priv->pdev, buf->pci_addr,
636                                          buf->size, PCI_DMA_FROMDEVICE);
637                 buf->pci_addr = 0;
638                 if (buf->mem)
639                         kfree(buf->mem);
640                 buf->size = 0;
641                 buf->mem = NULL;
642         }
643
644         /* clean up data rx buffers */
645         for (counter = 0; counter < ISL38XX_CB_RX_QSIZE; counter++) {
646                 if (priv->pci_map_rx_address[counter])
647                         pci_unmap_single(priv->pdev,
648                                          priv->pci_map_rx_address[counter],
649                                          MAX_FRAGMENT_SIZE_RX + 2,
650                                          PCI_DMA_FROMDEVICE);
651                 priv->pci_map_rx_address[counter] = 0;
652
653                 if (priv->data_low_rx[counter])
654                         dev_kfree_skb(priv->data_low_rx[counter]);
655                 priv->data_low_rx[counter] = 0;
656         }
657
658         /* Free the acces control list and the WPA list */
659         prism54_acl_clean(&priv->acl);
660         prism54_wpa_ie_clean(priv);
661         mgt_clean(priv);
662
663         return 0;
664 }
665
666 #if 0
667 static void
668 islpci_set_multicast_list(struct net_device *dev)
669 {
670         /* put device into promisc mode and let network layer handle it */
671 }
672 #endif
673
674 struct net_device *
675 islpci_setup(struct pci_dev *pdev)
676 {
677         islpci_private *priv;
678         struct net_device *ndev = alloc_etherdev(sizeof (islpci_private));
679
680         if (!ndev)
681                 return ndev;
682
683         SET_MODULE_OWNER(ndev);
684         pci_set_drvdata(pdev, ndev);
685 #if defined(SET_NETDEV_DEV)
686         SET_NETDEV_DEV(ndev, &pdev->dev);
687 #endif
688
689         /* setup the structure members */
690         ndev->base_addr = pci_resource_start(pdev, 0);
691         ndev->irq = pdev->irq;
692
693         /* initialize the function pointers */
694         ndev->open = &islpci_open;
695         ndev->stop = &islpci_close;
696         ndev->get_stats = &islpci_statistics;
697         ndev->get_wireless_stats = &prism54_get_wireless_stats;
698         ndev->do_ioctl = &prism54_ioctl;
699         ndev->wireless_handlers =
700             (struct iw_handler_def *) &prism54_handler_def;
701
702         ndev->hard_start_xmit = &islpci_eth_transmit;
703         /* ndev->set_multicast_list = &islpci_set_multicast_list; */
704         ndev->addr_len = ETH_ALEN;
705         ndev->set_mac_address = &prism54_set_mac_address;
706         /* Get a non-zero dummy MAC address for nameif. Jean II */
707         memcpy(ndev->dev_addr, dummy_mac, 6);
708
709 #ifdef HAVE_TX_TIMEOUT
710         ndev->watchdog_timeo = ISLPCI_TX_TIMEOUT;
711         ndev->tx_timeout = &islpci_eth_tx_timeout;
712 #endif
713
714         /* allocate a private device structure to the network device  */
715         priv = netdev_priv(ndev);
716         priv->ndev = ndev;
717         priv->pdev = pdev;
718         priv->monitor_type = ARPHRD_IEEE80211;
719         priv->ndev->type = (priv->iw_mode == IW_MODE_MONITOR) ?
720                 priv->monitor_type : ARPHRD_ETHER;
721
722         /* save the start and end address of the PCI memory area */
723         ndev->mem_start = (unsigned long) priv->device_base;
724         ndev->mem_end = ndev->mem_start + ISL38XX_PCI_MEM_SIZE;
725
726 #if VERBOSE > SHOW_ERROR_MESSAGES
727         DEBUG(SHOW_TRACING, "PCI Memory remapped to 0x%p\n", priv->device_base);
728 #endif
729
730         init_waitqueue_head(&priv->reset_done);
731
732         /* init the queue read locks, process wait counter */
733         sema_init(&priv->mgmt_sem, 1);
734         priv->mgmt_received = NULL;
735         init_waitqueue_head(&priv->mgmt_wqueue);
736         sema_init(&priv->stats_sem, 1);
737         spin_lock_init(&priv->slock);
738
739         /* init state machine with off#1 state */
740         priv->state = PRV_STATE_OFF;
741         priv->state_off = 1;
742
743         /* initialize workqueue's */
744         INIT_WORK(&priv->stats_work,
745                   (void (*)(void *)) prism54_update_stats, priv);
746         priv->stats_timestamp = 0;
747
748         INIT_WORK(&priv->reset_task, islpci_do_reset_and_wake, priv);
749         priv->reset_task_pending = 0;
750
751         /* allocate various memory areas */
752         if (islpci_alloc_memory(priv))
753                 goto do_free_netdev;
754
755         /* select the firmware file depending on the device id */
756         switch (pdev->device) {
757         case PCIDEVICE_ISL3890:
758         case PCIDEVICE_3COM6001:
759                 strcpy(priv->firmware, ISL3890_IMAGE_FILE);
760                 break;
761         case PCIDEVICE_ISL3877:
762                 strcpy(priv->firmware, ISL3877_IMAGE_FILE);
763                 break;
764
765         default:
766                 strcpy(priv->firmware, ISL3890_IMAGE_FILE);
767                 break;
768         }
769
770         if (register_netdev(ndev)) {
771                 DEBUG(SHOW_ERROR_MESSAGES,
772                       "ERROR: register_netdev() failed \n");
773                 goto do_islpci_free_memory;
774         }
775
776         return ndev;
777
778       do_islpci_free_memory:
779         islpci_free_memory(priv);
780       do_free_netdev:
781         pci_set_drvdata(pdev, 0);
782         free_netdev(ndev);
783         priv = 0;
784         return NULL;
785 }
786
787 islpci_state_t
788 islpci_set_state(islpci_private *priv, islpci_state_t new_state)
789 {
790         islpci_state_t old_state;
791
792         /* lock */
793         old_state = priv->state;
794
795         /* this means either a race condition or some serious error in
796          * the driver code */
797         switch (new_state) {
798         case PRV_STATE_OFF:
799                 priv->state_off++;
800         default:
801                 priv->state = new_state;
802                 break;
803
804         case PRV_STATE_PREBOOT:
805                 /* there are actually many off-states, enumerated by
806                  * state_off */
807                 if (old_state == PRV_STATE_OFF)
808                         priv->state_off--;
809
810                 /* only if hw_unavailable is zero now it means we either
811                  * were in off#1 state, or came here from
812                  * somewhere else */
813                 if (!priv->state_off)
814                         priv->state = new_state;
815                 break;
816         };
817 #if 0
818         printk(KERN_DEBUG "%s: state transition %d -> %d (off#%d)\n",
819                priv->ndev->name, old_state, new_state, priv->state_off);
820 #endif
821
822         /* invariants */
823         BUG_ON(priv->state_off < 0);
824         BUG_ON(priv->state_off && (priv->state != PRV_STATE_OFF));
825         BUG_ON(!priv->state_off && (priv->state == PRV_STATE_OFF));
826
827         /* unlock */
828         return old_state;
829 }