Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6
[linux-2.6] / 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/ethtool.h>
25 #include <linux/pci.h>
26 #include <linux/etherdevice.h>
27 #include <linux/delay.h>
28 #include <linux/if_arp.h>
29
30 #include <asm/io.h>
31
32 #include "prismcompat.h"
33 #include "isl_38xx.h"
34 #include "isl_ioctl.h"
35 #include "islpci_dev.h"
36 #include "islpci_mgt.h"
37 #include "islpci_eth.h"
38 #include "oid_mgt.h"
39
40 #define ISL3877_IMAGE_FILE      "isl3877"
41 #define ISL3886_IMAGE_FILE      "isl3886"
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 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 card's base address for writing 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
390         /* Turn off carrier if in STA or Ad-hoc mode. It will be turned on
391          * once the firmware receives a trap of being associated
392          * (GEN_OID_LINKSTATE). In other modes (AP or WDS or monitor) we
393          * should just leave the carrier on as its expected the firmware
394          * won't send us a trigger. */
395         if (priv->iw_mode == IW_MODE_INFRA || priv->iw_mode == IW_MODE_ADHOC)
396                 netif_carrier_off(ndev);
397         else
398                 netif_carrier_on(ndev);
399
400         return 0;
401 }
402
403 static int
404 islpci_close(struct net_device *ndev)
405 {
406         islpci_private *priv = netdev_priv(ndev);
407
408         printk(KERN_DEBUG "%s: islpci_close ()\n", ndev->name);
409
410         netif_stop_queue(ndev);
411
412         return prism54_bring_down(priv);
413 }
414
415 static int
416 prism54_bring_down(islpci_private *priv)
417 {
418         void __iomem *device_base = priv->device_base;
419         u32 reg;
420         /* we are going to shutdown the device */
421         islpci_set_state(priv, PRV_STATE_PREBOOT);
422
423         /* disable all device interrupts in case they weren't */
424         isl38xx_disable_interrupts(priv->device_base);
425
426         /* For safety reasons, we may want to ensure that no DMA transfer is
427          * currently in progress by emptying the TX and RX queues. */
428
429         /* wait until interrupts have finished executing on other CPUs */
430         synchronize_irq(priv->pdev->irq);
431
432         reg = readl(device_base + ISL38XX_CTRL_STAT_REG);
433         reg &= ~(ISL38XX_CTRL_STAT_RESET | ISL38XX_CTRL_STAT_RAMBOOT);
434         writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
435         wmb();
436         udelay(ISL38XX_WRITEIO_DELAY);
437
438         reg |= ISL38XX_CTRL_STAT_RESET;
439         writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
440         wmb();
441         udelay(ISL38XX_WRITEIO_DELAY);
442
443         /* clear the Reset bit */
444         reg &= ~ISL38XX_CTRL_STAT_RESET;
445         writel(reg, device_base + ISL38XX_CTRL_STAT_REG);
446         wmb();
447
448         /* wait a while for the device to reset */
449         schedule_timeout_uninterruptible(msecs_to_jiffies(50));
450
451         return 0;
452 }
453
454 static int
455 islpci_upload_fw(islpci_private *priv)
456 {
457         islpci_state_t old_state;
458         u32 rc;
459
460         old_state = islpci_set_state(priv, PRV_STATE_BOOT);
461
462         printk(KERN_DEBUG "%s: uploading firmware...\n", priv->ndev->name);
463
464         rc = isl_upload_firmware(priv);
465         if (rc) {
466                 /* error uploading the firmware */
467                 printk(KERN_ERR "%s: could not upload firmware ('%s')\n",
468                        priv->ndev->name, priv->firmware);
469
470                 islpci_set_state(priv, old_state);
471                 return rc;
472         }
473
474         printk(KERN_DEBUG "%s: firmware upload complete\n",
475                priv->ndev->name);
476
477         islpci_set_state(priv, PRV_STATE_POSTBOOT);
478
479         return 0;
480 }
481
482 static int
483 islpci_reset_if(islpci_private *priv)
484 {
485         long remaining;
486         int result = -ETIME;
487         int count;
488
489         DEFINE_WAIT(wait);
490         prepare_to_wait(&priv->reset_done, &wait, TASK_UNINTERRUPTIBLE);
491
492         /* now the last step is to reset the interface */
493         isl38xx_interface_reset(priv->device_base, priv->device_host_address);
494         islpci_set_state(priv, PRV_STATE_PREINIT);
495
496         for(count = 0; count < 2 && result; count++) {
497                 /* The software reset acknowledge needs about 220 msec here.
498                  * Be conservative and wait for up to one second. */
499
500                 remaining = schedule_timeout_uninterruptible(HZ);
501
502                 if(remaining > 0) {
503                         result = 0;
504                         break;
505                 }
506
507                 /* If we're here it's because our IRQ hasn't yet gone through.
508                  * Retry a bit more...
509                  */
510                 printk(KERN_ERR "%s: no 'reset complete' IRQ seen - retrying\n",
511                         priv->ndev->name);
512         }
513
514         finish_wait(&priv->reset_done, &wait);
515
516         if (result) {
517                 printk(KERN_ERR "%s: interface reset failure\n", priv->ndev->name);
518                 return result;
519         }
520
521         islpci_set_state(priv, PRV_STATE_INIT);
522
523         /* Now that the device is 100% up, let's allow
524          * for the other interrupts --
525          * NOTE: this is not *yet* true since we've only allowed the
526          * INIT interrupt on the IRQ line. We can perhaps poll
527          * the IRQ line until we know for sure the reset went through */
528         isl38xx_enable_common_interrupts(priv->device_base);
529
530         down_write(&priv->mib_sem);
531         result = mgt_commit(priv);
532         if (result) {
533                 printk(KERN_ERR "%s: interface reset failure\n", priv->ndev->name);
534                 up_write(&priv->mib_sem);
535                 return result;
536         }
537         up_write(&priv->mib_sem);
538
539         islpci_set_state(priv, PRV_STATE_READY);
540
541         printk(KERN_DEBUG "%s: interface reset complete\n", priv->ndev->name);
542         return 0;
543 }
544
545 int
546 islpci_reset(islpci_private *priv, int reload_firmware)
547 {
548         isl38xx_control_block *cb =    /* volatile not needed */
549                 (isl38xx_control_block *) priv->control_block;
550         unsigned counter;
551         int rc;
552
553         if (reload_firmware)
554                 islpci_set_state(priv, PRV_STATE_PREBOOT);
555         else
556                 islpci_set_state(priv, PRV_STATE_POSTBOOT);
557
558         printk(KERN_DEBUG "%s: resetting device...\n", priv->ndev->name);
559
560         /* disable all device interrupts in case they weren't */
561         isl38xx_disable_interrupts(priv->device_base);
562
563         /* flush all management queues */
564         priv->index_mgmt_tx = 0;
565         priv->index_mgmt_rx = 0;
566
567         /* clear the indexes in the frame pointer */
568         for (counter = 0; counter < ISL38XX_CB_QCOUNT; counter++) {
569                 cb->driver_curr_frag[counter] = cpu_to_le32(0);
570                 cb->device_curr_frag[counter] = cpu_to_le32(0);
571         }
572
573         /* reset the mgmt receive queue */
574         for (counter = 0; counter < ISL38XX_CB_MGMT_QSIZE; counter++) {
575                 isl38xx_fragment *frag = &cb->rx_data_mgmt[counter];
576                 frag->size = cpu_to_le16(MGMT_FRAME_SIZE);
577                 frag->flags = 0;
578                 frag->address = cpu_to_le32(priv->mgmt_rx[counter].pci_addr);
579         }
580
581         for (counter = 0; counter < ISL38XX_CB_RX_QSIZE; counter++) {
582                 cb->rx_data_low[counter].address =
583                     cpu_to_le32((u32) priv->pci_map_rx_address[counter]);
584         }
585
586         /* since the receive queues are filled with empty fragments, now we can
587          * set the corresponding indexes in the Control Block */
588         priv->control_block->driver_curr_frag[ISL38XX_CB_RX_DATA_LQ] =
589             cpu_to_le32(ISL38XX_CB_RX_QSIZE);
590         priv->control_block->driver_curr_frag[ISL38XX_CB_RX_MGMTQ] =
591             cpu_to_le32(ISL38XX_CB_MGMT_QSIZE);
592
593         /* reset the remaining real index registers and full flags */
594         priv->free_data_rx = 0;
595         priv->free_data_tx = 0;
596         priv->data_low_tx_full = 0;
597
598         if (reload_firmware) { /* Should we load the firmware ? */
599         /* now that the data structures are cleaned up, upload
600          * firmware and reset interface */
601                 rc = islpci_upload_fw(priv);
602                 if (rc) {
603                         printk(KERN_ERR "%s: islpci_reset: failure\n",
604                                 priv->ndev->name);
605                         return rc;
606                 }
607         }
608
609         /* finally reset interface */
610         rc = islpci_reset_if(priv);
611         if (rc)
612                 printk(KERN_ERR "prism54: Your card/socket may be faulty, or IRQ line too busy :(\n");
613         return rc;
614 }
615
616 /******************************************************************************
617     Network device configuration functions
618 ******************************************************************************/
619 static int
620 islpci_alloc_memory(islpci_private *priv)
621 {
622         int counter;
623
624 #if VERBOSE > SHOW_ERROR_MESSAGES
625         printk(KERN_DEBUG "islpci_alloc_memory\n");
626 #endif
627
628         /* remap the PCI device base address to accessable */
629         if (!(priv->device_base =
630               ioremap(pci_resource_start(priv->pdev, 0),
631                       ISL38XX_PCI_MEM_SIZE))) {
632                 /* error in remapping the PCI device memory address range */
633                 printk(KERN_ERR "PCI memory remapping failed \n");
634                 return -1;
635         }
636
637         /* memory layout for consistent DMA region:
638          *
639          * Area 1: Control Block for the device interface
640          * Area 2: Power Save Mode Buffer for temporary frame storage. Be aware that
641          *         the number of supported stations in the AP determines the minimal
642          *         size of the buffer !
643          */
644
645         /* perform the allocation */
646         priv->driver_mem_address = pci_alloc_consistent(priv->pdev,
647                                                         HOST_MEM_BLOCK,
648                                                         &priv->
649                                                         device_host_address);
650
651         if (!priv->driver_mem_address) {
652                 /* error allocating the block of PCI memory */
653                 printk(KERN_ERR "%s: could not allocate DMA memory, aborting!",
654                        "prism54");
655                 return -1;
656         }
657
658         /* assign the Control Block to the first address of the allocated area */
659         priv->control_block =
660             (isl38xx_control_block *) priv->driver_mem_address;
661
662         /* set the Power Save Buffer pointer directly behind the CB */
663         priv->device_psm_buffer =
664                 priv->device_host_address + CONTROL_BLOCK_SIZE;
665
666         /* make sure all buffer pointers are initialized */
667         for (counter = 0; counter < ISL38XX_CB_QCOUNT; counter++) {
668                 priv->control_block->driver_curr_frag[counter] = cpu_to_le32(0);
669                 priv->control_block->device_curr_frag[counter] = cpu_to_le32(0);
670         }
671
672         priv->index_mgmt_rx = 0;
673         memset(priv->mgmt_rx, 0, sizeof(priv->mgmt_rx));
674         memset(priv->mgmt_tx, 0, sizeof(priv->mgmt_tx));
675
676         /* allocate rx queue for management frames */
677         if (islpci_mgmt_rx_fill(priv->ndev) < 0)
678                 goto out_free;
679
680         /* now get the data rx skb's */
681         memset(priv->data_low_rx, 0, sizeof (priv->data_low_rx));
682         memset(priv->pci_map_rx_address, 0, sizeof (priv->pci_map_rx_address));
683
684         for (counter = 0; counter < ISL38XX_CB_RX_QSIZE; counter++) {
685                 struct sk_buff *skb;
686
687                 /* allocate an sk_buff for received data frames storage
688                  * each frame on receive size consists of 1 fragment
689                  * include any required allignment operations */
690                 if (!(skb = dev_alloc_skb(MAX_FRAGMENT_SIZE_RX + 2))) {
691                         /* error allocating an sk_buff structure elements */
692                         printk(KERN_ERR "Error allocating skb.\n");
693                         skb = NULL;
694                         goto out_free;
695                 }
696                 skb_reserve(skb, (4 - (long) skb->data) & 0x03);
697                 /* add the new allocated sk_buff to the buffer array */
698                 priv->data_low_rx[counter] = skb;
699
700                 /* map the allocated skb data area to pci */
701                 priv->pci_map_rx_address[counter] =
702                     pci_map_single(priv->pdev, (void *) skb->data,
703                                    MAX_FRAGMENT_SIZE_RX + 2,
704                                    PCI_DMA_FROMDEVICE);
705                 if (!priv->pci_map_rx_address[counter]) {
706                         /* error mapping the buffer to device
707                            accessable memory address */
708                         printk(KERN_ERR "failed to map skb DMA'able\n");
709                         goto out_free;
710                 }
711         }
712
713         prism54_acl_init(&priv->acl);
714         prism54_wpa_bss_ie_init(priv);
715         if (mgt_init(priv))
716                 goto out_free;
717
718         return 0;
719  out_free:
720         islpci_free_memory(priv);
721         return -1;
722 }
723
724 int
725 islpci_free_memory(islpci_private *priv)
726 {
727         int counter;
728
729         if (priv->device_base)
730                 iounmap(priv->device_base);
731         priv->device_base = NULL;
732
733         /* free consistent DMA area... */
734         if (priv->driver_mem_address)
735                 pci_free_consistent(priv->pdev, HOST_MEM_BLOCK,
736                                     priv->driver_mem_address,
737                                     priv->device_host_address);
738
739         /* clear some dangling pointers */
740         priv->driver_mem_address = NULL;
741         priv->device_host_address = 0;
742         priv->device_psm_buffer = 0;
743         priv->control_block = NULL;
744
745         /* clean up mgmt rx buffers */
746         for (counter = 0; counter < ISL38XX_CB_MGMT_QSIZE; counter++) {
747                 struct islpci_membuf *buf = &priv->mgmt_rx[counter];
748                 if (buf->pci_addr)
749                         pci_unmap_single(priv->pdev, buf->pci_addr,
750                                          buf->size, PCI_DMA_FROMDEVICE);
751                 buf->pci_addr = 0;
752                 kfree(buf->mem);
753                 buf->size = 0;
754                 buf->mem = NULL;
755         }
756
757         /* clean up data rx buffers */
758         for (counter = 0; counter < ISL38XX_CB_RX_QSIZE; counter++) {
759                 if (priv->pci_map_rx_address[counter])
760                         pci_unmap_single(priv->pdev,
761                                          priv->pci_map_rx_address[counter],
762                                          MAX_FRAGMENT_SIZE_RX + 2,
763                                          PCI_DMA_FROMDEVICE);
764                 priv->pci_map_rx_address[counter] = 0;
765
766                 if (priv->data_low_rx[counter])
767                         dev_kfree_skb(priv->data_low_rx[counter]);
768                 priv->data_low_rx[counter] = NULL;
769         }
770
771         /* Free the acces control list and the WPA list */
772         prism54_acl_clean(&priv->acl);
773         prism54_wpa_bss_ie_clean(priv);
774         mgt_clean(priv);
775
776         return 0;
777 }
778
779 #if 0
780 static void
781 islpci_set_multicast_list(struct net_device *dev)
782 {
783         /* put device into promisc mode and let network layer handle it */
784 }
785 #endif
786
787 static void islpci_ethtool_get_drvinfo(struct net_device *dev,
788                                        struct ethtool_drvinfo *info)
789 {
790         strcpy(info->driver, DRV_NAME);
791         strcpy(info->version, DRV_VERSION);
792 }
793
794 static const struct ethtool_ops islpci_ethtool_ops = {
795         .get_drvinfo = islpci_ethtool_get_drvinfo,
796 };
797
798 static const struct net_device_ops islpci_netdev_ops = {
799         .ndo_open               = islpci_open,
800         .ndo_stop               = islpci_close,
801         .ndo_do_ioctl           = prism54_ioctl,
802         .ndo_start_xmit         = islpci_eth_transmit,
803         .ndo_tx_timeout         = islpci_eth_tx_timeout,
804         .ndo_set_mac_address    = prism54_set_mac_address,
805         .ndo_change_mtu         = eth_change_mtu,
806         .ndo_set_mac_address    = eth_mac_addr,
807         .ndo_validate_addr      = eth_validate_addr,
808 };
809
810 struct net_device *
811 islpci_setup(struct pci_dev *pdev)
812 {
813         islpci_private *priv;
814         struct net_device *ndev = alloc_etherdev(sizeof (islpci_private));
815
816         if (!ndev)
817                 return ndev;
818
819         pci_set_drvdata(pdev, ndev);
820 #if defined(SET_NETDEV_DEV)
821         SET_NETDEV_DEV(ndev, &pdev->dev);
822 #endif
823
824         /* setup the structure members */
825         ndev->base_addr = pci_resource_start(pdev, 0);
826         ndev->irq = pdev->irq;
827
828         /* initialize the function pointers */
829         ndev->netdev_ops = &islpci_netdev_ops;
830         ndev->wireless_handlers = &prism54_handler_def;
831         ndev->ethtool_ops = &islpci_ethtool_ops;
832
833         /* ndev->set_multicast_list = &islpci_set_multicast_list; */
834         ndev->addr_len = ETH_ALEN;
835         /* Get a non-zero dummy MAC address for nameif. Jean II */
836         memcpy(ndev->dev_addr, dummy_mac, 6);
837
838         ndev->watchdog_timeo = ISLPCI_TX_TIMEOUT;
839
840         /* allocate a private device structure to the network device  */
841         priv = netdev_priv(ndev);
842         priv->ndev = ndev;
843         priv->pdev = pdev;
844         priv->monitor_type = ARPHRD_IEEE80211;
845         priv->ndev->type = (priv->iw_mode == IW_MODE_MONITOR) ?
846                 priv->monitor_type : ARPHRD_ETHER;
847
848         /* Add pointers to enable iwspy support. */
849         priv->wireless_data.spy_data = &priv->spy_data;
850         ndev->wireless_data = &priv->wireless_data;
851
852         /* save the start and end address of the PCI memory area */
853         ndev->mem_start = (unsigned long) priv->device_base;
854         ndev->mem_end = ndev->mem_start + ISL38XX_PCI_MEM_SIZE;
855
856 #if VERBOSE > SHOW_ERROR_MESSAGES
857         DEBUG(SHOW_TRACING, "PCI Memory remapped to 0x%p\n", priv->device_base);
858 #endif
859
860         init_waitqueue_head(&priv->reset_done);
861
862         /* init the queue read locks, process wait counter */
863         mutex_init(&priv->mgmt_lock);
864         priv->mgmt_received = NULL;
865         init_waitqueue_head(&priv->mgmt_wqueue);
866         mutex_init(&priv->stats_lock);
867         spin_lock_init(&priv->slock);
868
869         /* init state machine with off#1 state */
870         priv->state = PRV_STATE_OFF;
871         priv->state_off = 1;
872
873         /* initialize workqueue's */
874         INIT_WORK(&priv->stats_work, prism54_update_stats);
875         priv->stats_timestamp = 0;
876
877         INIT_WORK(&priv->reset_task, islpci_do_reset_and_wake);
878         priv->reset_task_pending = 0;
879
880         /* allocate various memory areas */
881         if (islpci_alloc_memory(priv))
882                 goto do_free_netdev;
883
884         /* select the firmware file depending on the device id */
885         switch (pdev->device) {
886         case 0x3877:
887                 strcpy(priv->firmware, ISL3877_IMAGE_FILE);
888                 break;
889
890         case 0x3886:
891                 strcpy(priv->firmware, ISL3886_IMAGE_FILE);
892                 break;
893
894         default:
895                 strcpy(priv->firmware, ISL3890_IMAGE_FILE);
896                 break;
897         }
898
899         if (register_netdev(ndev)) {
900                 DEBUG(SHOW_ERROR_MESSAGES,
901                       "ERROR: register_netdev() failed \n");
902                 goto do_islpci_free_memory;
903         }
904
905         return ndev;
906
907       do_islpci_free_memory:
908         islpci_free_memory(priv);
909       do_free_netdev:
910         pci_set_drvdata(pdev, NULL);
911         free_netdev(ndev);
912         priv = NULL;
913         return NULL;
914 }
915
916 islpci_state_t
917 islpci_set_state(islpci_private *priv, islpci_state_t new_state)
918 {
919         islpci_state_t old_state;
920
921         /* lock */
922         old_state = priv->state;
923
924         /* this means either a race condition or some serious error in
925          * the driver code */
926         switch (new_state) {
927         case PRV_STATE_OFF:
928                 priv->state_off++;
929         default:
930                 priv->state = new_state;
931                 break;
932
933         case PRV_STATE_PREBOOT:
934                 /* there are actually many off-states, enumerated by
935                  * state_off */
936                 if (old_state == PRV_STATE_OFF)
937                         priv->state_off--;
938
939                 /* only if hw_unavailable is zero now it means we either
940                  * were in off#1 state, or came here from
941                  * somewhere else */
942                 if (!priv->state_off)
943                         priv->state = new_state;
944                 break;
945         };
946 #if 0
947         printk(KERN_DEBUG "%s: state transition %d -> %d (off#%d)\n",
948                priv->ndev->name, old_state, new_state, priv->state_off);
949 #endif
950
951         /* invariants */
952         BUG_ON(priv->state_off < 0);
953         BUG_ON(priv->state_off && (priv->state != PRV_STATE_OFF));
954         BUG_ON(!priv->state_off && (priv->state == PRV_STATE_OFF));
955
956         /* unlock */
957         return old_state;
958 }