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