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