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