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